Skip to content

Commit 558f1fb

Browse files
Merge pull request #41 from Devsh-Graphics-Programming/for_merging_half_baked_crap
Bugfixes and some raytracing
2 parents ae953af + 78808d4 commit 558f1fb

36 files changed

+621
-629
lines changed

examples_tests/18.MitsubaLoader/main.cpp

Lines changed: 44 additions & 6 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[];
@@ -94,6 +98,29 @@ vec3 nbl_computeLighting(inout nbl_glsl_AnisotropicViewSurfaceInteraction out_in
9498
return color+emissive;
9599
}
96100
)";
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+
)";
97124
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)
98125
{
99126
const asset::ICPUShader* unspec = _fs->getUnspecialized();
@@ -110,6 +137,7 @@ static core::smart_refctd_ptr<asset::ICPUSpecializedShader> createModifiedFragSh
110137
GLSL_COMPUTE_LIGHTING;
111138

112139
glsl.insert(glsl.find("#ifndef _NBL_COMPUTE_LIGHTING_DEFINED_"), extra);
140+
glsl.insert(glsl.find("#ifndef _NBL_FRAG_MAIN_DEFINED_"), GLSL_FRAG_MAIN);
113141

114142
//auto* f = fopen("fs.glsl","w");
115143
//fwrite(glsl.c_str(), 1, glsl.size(), f);
@@ -273,9 +301,9 @@ int main()
273301
return 1;
274302

275303
bool leftHandedCamera = false;
304+
auto cameraTransform = sensor.transform.matrix.extractSub3x4();
276305
{
277-
auto relativeTransform = sensor.transform.matrix.extractSub3x4();
278-
if (relativeTransform.getPseudoDeterminant().x < 0.f)
306+
if (cameraTransform.getPseudoDeterminant().x < 0.f)
279307
leftHandedCamera = true;
280308
}
281309

@@ -429,7 +457,13 @@ int main()
429457
//modify pipeline layouts with our custom DS2 layout (DS2 will be used for lights buffer)
430458
for (uint32_t i = 0u; i < mesh->getMeshBufferCount(); ++i)
431459
{
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;
433467
if (modifiedPipelines.find(pipeline) == modifiedPipelines.end())
434468
{
435469
//if (!pipeline->getLayout()->getDescriptorSetLayout(2u))
@@ -443,10 +477,14 @@ int main()
443477
modifiedShaders.insert({ core::smart_refctd_ptr<asset::ICPUSpecializedShader>(fs),newfs });
444478
pipeline->setShaderAtStage(asset::ICPUSpecializedShader::ESS_FRAGMENT, newfs.get());
445479
}
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));
448484
modifiedPipelines.insert(pipeline);
449485
}
486+
487+
reinterpret_cast<float*>(meshbuffer->getPushConstantsDataPtr() + pcr.offset)[0] = cameraTransform.getPseudoDeterminant().x;
450488
}
451489
}
452490
modifiedShaders.clear();
@@ -728,7 +766,7 @@ int main()
728766
const video::IGPUDescriptorSet* ds[3]{ gpuds0.get(), gpuds1.get(), gpuds2.get() };
729767
driver->bindGraphicsPipeline(pipeline);
730768
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());
732770

733771
driver->drawMeshBuffer(mb);
734772
}

examples_tests/22.RaytracedAO/common.glsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
{
2020
float x,y,z;
2121
};
22+
#define vec4 nbl::core::vectorSIMDf
2223
#define mat4 nbl::core::matrix4SIMD
2324
#define mat4x3 nbl::core::matrix3x4SIMD
2425
#endif

examples_tests/22.RaytracedAO/cull.comp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,18 @@
44
#include "drawCommon.glsl"
55
layout(local_size_x = WORKGROUP_SIZE) in;
66

7+
#include <nbl/builtin/glsl/ext/MitsubaLoader/instance_data_descriptor.glsl>
78

89
#include <nbl/builtin/glsl/utils/indirect_commands.glsl>
9-
layout(set=1, binding=0, std430, row_major) restrict readonly buffer PerInstanceStatic
10-
{
11-
ObjectStaticData_t staticData[];
12-
};
13-
layout(set=1, binding=1, row_major) writeonly restrict buffer PerInstancePerCamera
10+
layout(set=1, binding=0, row_major) writeonly restrict buffer PerInstancePerCamera
1411
{
1512
DrawData_t data[];
1613
} instanceDataPerCamera;
17-
layout(set=1, binding=2, std430, row_major) restrict readonly buffer PerInstanceCull
14+
layout(set=1, binding=1, std430, row_major) restrict readonly buffer PerInstanceCull
1815
{
1916
CullData_t cullData[];
2017
};
21-
layout(set=1, binding=3, std430) restrict coherent buffer IndirectDraws
18+
layout(set=1, binding=2, std430) restrict coherent buffer IndirectDraws
2219
{
2320
nbl_glsl_DrawElementsIndirectCommand_t draws[];
2421
} commandBuff[2];
@@ -40,33 +37,36 @@ layout(push_constant, row_major) uniform PushConstants
4037
// we just do atomic add on the instance count
4138
void main()
4239
{
43-
uint globalObjectID = gl_GlobalInvocationID.x;
44-
if (globalObjectID>=pc.data.maxObjectCount)
40+
if (gl_GlobalInvocationID.x<pc.data.maxDrawCount)
41+
commandBuff[pc.data.currentCommandBufferIx^0x1u].draws[gl_GlobalInvocationID.x].instanceCount = 0u;
42+
43+
uint instanceMeshBufferID = gl_GlobalInvocationID.x;
44+
if (instanceMeshBufferID>=pc.data.maxObjectCount)
4545
return;
4646

47-
const mat4x3 worldMatrix = cullData[globalObjectID].worldMatrix;
48-
const uint drawID = cullData[globalObjectID].drawID;
47+
// fetch instance data
48+
const CullData_t instanceMeshBufferData = cullData[instanceMeshBufferID];
49+
const uint globalObjectID = instanceMeshBufferData.globalObjectID;
4950

50-
// clear drawcount for next buffer
51-
commandBuff[pc.data.currentCommandBufferIx^0x1u].draws[drawID].instanceCount = 0u;
51+
const mat4x3 worldMatrix = InstData.data[globalObjectID].tform;
52+
const mat4 MVP = nbl_glsl_pseudoMul4x4with4x3(pc.data.viewProjMatrix,worldMatrix);
5253

5354
// cull
54-
const mat4 MVP = nbl_glsl_pseudoMul4x4with4x3(pc.data.viewProjMatrix,worldMatrix);
55-
bool notCulled = true;
56-
if (false)
55+
bool notCulled;
5756
{
58-
mat2x3 bbox;
59-
bbox[0] = cullData[globalObjectID].aabbMinEdge;
60-
bbox[1] = cullData[globalObjectID].aabbMaxEdge;
57+
const mat2x3 bbox = mat2x3(instanceMeshBufferData.aabbMinEdge,instanceMeshBufferData.aabbMaxEdge);
6158
notCulled = nbl_glsl_couldBeVisible(MVP,bbox);
6259
}
6360

61+
// set up MDI
6462
if (notCulled)
6563
{
66-
const uint instanceID = atomicAdd(commandBuff[pc.data.currentCommandBufferIx].draws[drawID].instanceCount,1u)+cullData[globalObjectID].baseInstance;
64+
const uint drawID = instanceMeshBufferData.drawID;
65+
const uint drawInstanceID = atomicAdd(commandBuff[pc.data.currentCommandBufferIx].draws[drawID].instanceCount,1u)
66+
+commandBuff[pc.data.currentCommandBufferIx].draws[drawID].baseInstance;
6767

68-
instanceDataPerCamera.data[instanceID].MVP = MVP;
69-
instanceDataPerCamera.data[instanceID].detMVP = pc.data.viewProjDeterminant*staticData[globalObjectID].detWorldMatrix;
70-
instanceDataPerCamera.data[instanceID].objectID = globalObjectID;
68+
instanceDataPerCamera.data[drawInstanceID].MVP = MVP;
69+
float detMVP = pc.data.viewProjDeterminant*InstData.data[globalObjectID].determinant;
70+
instanceDataPerCamera.data[drawInstanceID].backfacingBit_objectID = globalObjectID|(floatBitsToUint(detMVP)&0x80000000u); // use MSB to denote if face orientation should be flipped
7171
}
7272
}

0 commit comments

Comments
 (0)