@@ -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[];
@@ -94,6 +98,29 @@ vec3 nbl_computeLighting(inout nbl_glsl_AnisotropicViewSurfaceInteraction out_in
94
98
return color+emissive;
95
99
}
96
100
)" ;
101
+ constexpr const char * GLSL_FRAG_MAIN = R"(
102
+ #define _NBL_FRAG_MAIN_DEFINED_
103
+ void main()
104
+ {
105
+ mat2 dUV = mat2(dFdx(UV),dFdy(UV));
106
+
107
+ // "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."
108
+ const bool front = (!gl_FrontFacing) != (PC.camTformDeterminant*InstData.data[InstanceIndex].determinant < 0.0);
109
+ nbl_glsl_MC_precomputed_t precomp = nbl_glsl_precomputeData(front);
110
+ #ifdef TEX_PREFETCH_STREAM
111
+ nbl_glsl_runTexPrefetchStream(getTexPrefetchStream(precomp), UV, dUV);
112
+ #endif
113
+ #ifdef NORM_PRECOMP_STREAM
114
+ nbl_glsl_runNormalPrecompStream(getNormalPrecompStream(precomp), dUV, precomp);
115
+ #endif
116
+
117
+
118
+ nbl_glsl_IsotropicViewSurfaceInteraction inter;
119
+ vec3 color = nbl_computeLighting(inter, dUV, precomp);
120
+
121
+ OutColor = vec4(color, 1.0);
122
+ }
123
+ )" ;
97
124
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)
98
125
{
99
126
const asset::ICPUShader* unspec = _fs->getUnspecialized ();
@@ -110,6 +137,7 @@ static core::smart_refctd_ptr<asset::ICPUSpecializedShader> createModifiedFragSh
110
137
GLSL_COMPUTE_LIGHTING;
111
138
112
139
glsl.insert (glsl.find (" #ifndef _NBL_COMPUTE_LIGHTING_DEFINED_" ), extra);
140
+ glsl.insert (glsl.find (" #ifndef _NBL_FRAG_MAIN_DEFINED_" ), GLSL_FRAG_MAIN);
113
141
114
142
// auto* f = fopen("fs.glsl","w");
115
143
// fwrite(glsl.c_str(), 1, glsl.size(), f);
@@ -273,9 +301,9 @@ int main()
273
301
return 1 ;
274
302
275
303
bool leftHandedCamera = false ;
304
+ auto cameraTransform = sensor.transform .matrix .extractSub3x4 ();
276
305
{
277
- auto relativeTransform = sensor.transform .matrix .extractSub3x4 ();
278
- if (relativeTransform.getPseudoDeterminant ().x < 0 .f )
306
+ if (cameraTransform.getPseudoDeterminant ().x < 0 .f )
279
307
leftHandedCamera = true ;
280
308
}
281
309
@@ -429,7 +457,13 @@ int main()
429
457
// modify pipeline layouts with our custom DS2 layout (DS2 will be used for lights buffer)
430
458
for (uint32_t i = 0u ; i < mesh->getMeshBufferCount (); ++i)
431
459
{
432
- auto * pipeline = mesh->getMeshBuffer (i)->getPipeline ();
460
+ auto * meshbuffer = mesh->getMeshBuffer (i);
461
+ auto * pipeline = meshbuffer->getPipeline ();
462
+
463
+ asset::SPushConstantRange pcr;
464
+ pcr.offset = 0u ;
465
+ pcr.size = sizeof (float );
466
+ pcr.stageFlags = asset::ISpecializedShader::ESS_FRAGMENT;
433
467
if (modifiedPipelines.find (pipeline) == modifiedPipelines.end ())
434
468
{
435
469
// if (!pipeline->getLayout()->getDescriptorSetLayout(2u))
@@ -443,10 +477,14 @@ int main()
443
477
modifiedShaders.insert ({ core::smart_refctd_ptr<asset::ICPUSpecializedShader>(fs),newfs });
444
478
pipeline->setShaderAtStage (asset::ICPUSpecializedShader::ESS_FRAGMENT, newfs.get ());
445
479
}
446
- // invert what is recognized as frontface in case of RH camera
447
- pipeline->getRasterizationParams ().frontFaceIsCCW = !leftHandedCamera;
480
+
481
+ auto pc = core::make_refctd_dynamic_array<core::smart_refctd_dynamic_array<asset::SPushConstantRange>>(1u );
482
+ (*pc)[0 ] = pcr;
483
+ pipeline->getLayout ()->setPushConstantRanges (std::move (pc));
448
484
modifiedPipelines.insert (pipeline);
449
485
}
486
+
487
+ reinterpret_cast <float *>(meshbuffer->getPushConstantsDataPtr () + pcr.offset )[0 ] = cameraTransform.getPseudoDeterminant ().x ;
450
488
}
451
489
}
452
490
modifiedShaders.clear ();
@@ -728,7 +766,7 @@ int main()
728
766
const video::IGPUDescriptorSet* ds[3 ]{ gpuds0.get (), gpuds1.get (), gpuds2.get () };
729
767
driver->bindGraphicsPipeline (pipeline);
730
768
driver->bindDescriptorSets (video::EPBP_GRAPHICS, pipeline->getLayout (), 0u , 3u , ds, nullptr );
731
- driver->pushConstants (pipeline->getLayout (), video::IGPUSpecializedShader::ESS_VERTEX|video::IGPUSpecializedShader::ESS_FRAGMENT, 0u , sizeof (uint32_t ), mb->getPushConstantsDataPtr ());
769
+ driver->pushConstants (pipeline->getLayout (), video::IGPUSpecializedShader::ESS_VERTEX|video::IGPUSpecializedShader::ESS_FRAGMENT, 0u , sizeof (float ), mb->getPushConstantsDataPtr ());
732
770
733
771
driver->drawMeshBuffer (mb);
734
772
}
0 commit comments