@@ -31,6 +31,10 @@ struct SLight
31
31
vec3 intensity;
32
32
};
33
33
34
+ layout (push_constant) uniform Block {
35
+ float camTformDeterminant;
36
+ } PC;
37
+
34
38
layout (set = 2, binding = 0, std430) readonly restrict buffer Lights
35
39
{
36
40
SLight lights[];
@@ -92,6 +96,29 @@ vec3 nbl_computeLighting(inout nbl_glsl_IsotropicViewSurfaceInteraction out_inte
92
96
return color+emissive;
93
97
}
94
98
)" ;
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
+ )" ;
95
122
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)
96
123
{
97
124
const asset::ICPUShader* unspec = _fs->getUnspecialized ();
@@ -108,6 +135,7 @@ static core::smart_refctd_ptr<asset::ICPUSpecializedShader> createModifiedFragSh
108
135
GLSL_COMPUTE_LIGHTING;
109
136
110
137
glsl.insert (glsl.find (" #ifndef _NBL_COMPUTE_LIGHTING_DEFINED_" ), extra);
138
+ glsl.insert (glsl.find (" #ifndef _NBL_FRAG_MAIN_DEFINED_" ), GLSL_FRAG_MAIN);
111
139
112
140
// auto* f = fopen("fs.glsl","w");
113
141
// fwrite(glsl.c_str(), 1, glsl.size(), f);
@@ -271,9 +299,9 @@ int main()
271
299
return 1 ;
272
300
273
301
bool leftHandedCamera = false ;
302
+ auto cameraTransform = sensor.transform .matrix .extractSub3x4 ();
274
303
{
275
- auto relativeTransform = sensor.transform .matrix .extractSub3x4 ();
276
- if (relativeTransform.getPseudoDeterminant ().x < 0 .f )
304
+ if (cameraTransform.getPseudoDeterminant ().x < 0 .f )
277
305
leftHandedCamera = true ;
278
306
}
279
307
@@ -422,7 +450,13 @@ int main()
422
450
// modify pipeline layouts with our custom DS2 layout (DS2 will be used for lights buffer)
423
451
for (uint32_t i = 0u ; i < mesh->getMeshBufferCount (); ++i)
424
452
{
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;
426
460
if (modifiedPipelines.find (pipeline) == modifiedPipelines.end ())
427
461
{
428
462
// if (!pipeline->getLayout()->getDescriptorSetLayout(2u))
@@ -438,8 +472,14 @@ int main()
438
472
}
439
473
// invert what is recognized as frontface in case of RH camera
440
474
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));
441
479
modifiedPipelines.insert (pipeline);
442
480
}
481
+
482
+ reinterpret_cast <float *>(meshbuffer->getPushConstantsDataPtr () + pcr.offset )[0 ] = cameraTransform.getPseudoDeterminant ().x ;
443
483
}
444
484
}
445
485
modifiedShaders.clear ();
0 commit comments