Skip to content

Commit ff05a25

Browse files
committed
Added cam tform determinant to front/back side recognition
1 parent 64a7c91 commit ff05a25

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

examples_tests/18.MitsubaLoader/main.cpp

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ struct SLight
3131
vec3 intensity;
3232
};
3333
34+
layout (push_constant) uniform Block {
35+
float camTformDeterminant;
36+
} PC;
37+
3438
layout (set = 2, binding = 0, std430) readonly restrict buffer Lights
3539
{
3640
SLight lights[];
@@ -92,6 +96,29 @@ vec3 nbl_computeLighting(inout nbl_glsl_IsotropicViewSurfaceInteraction out_inte
9296
return color+emissive;
9397
}
9498
)";
99+
constexpr const char* GLSL_FRAG_MAIN = R"(
100+
#define _NBL_FRAG_MAIN_DEFINED_
101+
void main()
102+
{
103+
mat2 dUV = mat2(dFdx(UV),dFdy(UV));
104+
105+
// "The sign of this computation is negated when the value of GL_CLIP_ORIGIN (the clip volume origin, set with glClipControl) is GL_UPPER_LEFT."
106+
const bool front = (!gl_FrontFacing) != (PC.camTformDeterminant*InstData.data[InstanceIndex].determinant < 0.0);
107+
nbl_glsl_MC_precomputed_t precomp = nbl_glsl_precomputeData(front);
108+
#ifdef TEX_PREFETCH_STREAM
109+
nbl_glsl_runTexPrefetchStream(getTexPrefetchStream(precomp), UV, dUV);
110+
#endif
111+
#ifdef NORM_PRECOMP_STREAM
112+
nbl_glsl_runNormalPrecompStream(getNormalPrecompStream(precomp), dUV, precomp);
113+
#endif
114+
115+
116+
nbl_glsl_IsotropicViewSurfaceInteraction inter;
117+
vec3 color = nbl_computeLighting(inter, dUV, precomp);
118+
119+
OutColor = vec4(color, 1.0);
120+
}
121+
)";
95122
static core::smart_refctd_ptr<asset::ICPUSpecializedShader> createModifiedFragShader(const asset::ICPUSpecializedShader* _fs, uint32_t viewport_w, uint32_t viewport_h, uint32_t lightCnt, uint32_t smplCnt, float intensityScale)
96123
{
97124
const asset::ICPUShader* unspec = _fs->getUnspecialized();
@@ -108,6 +135,7 @@ static core::smart_refctd_ptr<asset::ICPUSpecializedShader> createModifiedFragSh
108135
GLSL_COMPUTE_LIGHTING;
109136

110137
glsl.insert(glsl.find("#ifndef _NBL_COMPUTE_LIGHTING_DEFINED_"), extra);
138+
glsl.insert(glsl.find("#ifndef _NBL_FRAG_MAIN_DEFINED_"), GLSL_FRAG_MAIN);
111139

112140
//auto* f = fopen("fs.glsl","w");
113141
//fwrite(glsl.c_str(), 1, glsl.size(), f);
@@ -271,9 +299,9 @@ int main()
271299
return 1;
272300

273301
bool leftHandedCamera = false;
302+
auto cameraTransform = sensor.transform.matrix.extractSub3x4();
274303
{
275-
auto relativeTransform = sensor.transform.matrix.extractSub3x4();
276-
if (relativeTransform.getPseudoDeterminant().x < 0.f)
304+
if (cameraTransform.getPseudoDeterminant().x < 0.f)
277305
leftHandedCamera = true;
278306
}
279307

@@ -422,7 +450,13 @@ int main()
422450
//modify pipeline layouts with our custom DS2 layout (DS2 will be used for lights buffer)
423451
for (uint32_t i = 0u; i < mesh->getMeshBufferCount(); ++i)
424452
{
425-
auto* pipeline = mesh->getMeshBuffer(i)->getPipeline();
453+
auto* meshbuffer = mesh->getMeshBuffer(i);
454+
auto* pipeline = meshbuffer->getPipeline();
455+
456+
asset::SPushConstantRange pcr;
457+
pcr.offset = 0u;
458+
pcr.size = sizeof(float);
459+
pcr.stageFlags = asset::ISpecializedShader::ESS_FRAGMENT;
426460
if (modifiedPipelines.find(pipeline) == modifiedPipelines.end())
427461
{
428462
//if (!pipeline->getLayout()->getDescriptorSetLayout(2u))
@@ -438,8 +472,14 @@ int main()
438472
}
439473
// invert what is recognized as frontface in case of RH camera
440474
pipeline->getRasterizationParams().frontFaceIsCCW = !leftHandedCamera;
475+
476+
auto pc = core::make_refctd_dynamic_array<core::smart_refctd_dynamic_array<asset::SPushConstantRange>>(1u);
477+
(*pc)[0] = pcr;
478+
pipeline->getLayout()->setPushConstantRanges(std::move(pc));
441479
modifiedPipelines.insert(pipeline);
442480
}
481+
482+
reinterpret_cast<float*>(meshbuffer->getPushConstantsDataPtr() + pcr.offset)[0] = cameraTransform.getPseudoDeterminant().x;
443483
}
444484
}
445485
modifiedShaders.clear();

0 commit comments

Comments
 (0)