Skip to content

Commit 430a326

Browse files
Merge branch 'master' into erfan_revive_raytracer
2 parents 67ee973 + 1d5b85a commit 430a326

10 files changed

+674
-177
lines changed
+68-16
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,102 @@
11
#version 430 core
2+
#extension GL_EXT_shader_16bit_storage : require
23

34
#include "rasterizationCommon.h"
45
layout(local_size_x = WORKGROUP_SIZE) in;
56

6-
#include <nbl/builtin/glsl/utils/indirect_commands.glsl>
7-
87
layout(set=0, binding=0, std430, row_major) restrict readonly buffer PerInstanceCull
98
{
109
CullData_t cullData[];
1110
};
12-
layout(set=0, binding=1, std430) restrict coherent buffer IndirectDraws
13-
{
14-
nbl_glsl_DrawElementsIndirectCommand_t draws[];
15-
} commandBuff;
16-
layout(set=0, binding=2, std430) restrict coherent buffer MVPs
11+
layout(set=0, binding=1, std430) restrict buffer MVPs
1712
{
1813
mat4 mvps[];
1914
} mvpBuff;
2015

21-
16+
layout(set=0, binding=2, std430, column_major) restrict buffer CubeMVPs
17+
{
18+
mat4 cubeMVPs[];
19+
} cubeMvpBuff;
20+
21+
#define CUBE_COMMAND_BUFF_SET 0
22+
#define CUBE_COMMAND_BUFF_BINDING 3
23+
#define FRUSTUM_CULLED_COMMAND_BUFF_SET 0
24+
#define FRUSTUM_CULLED_COMMAND_BUFF_BINDING 4
25+
#define OCCLUSION_CULLED_COMMAND_BUFF_SET 0
26+
#define OCCLUSION_CULLED_COMMAND_BUFF_BINDING 5
27+
#define CUBE_DRAW_GUID_BUFF_SET 0
28+
#define CUBE_DRAW_GUID_BUFF_BINDING 6
29+
#define OCCLUSION_DISPATCH_INDIRECT_BUFF_SET 0
30+
#define OCCLUSION_DISPATCH_INDIRECT_BUFF_BINDING 7
31+
#define VISIBLE_BUFF_SET 0
32+
#define VISIBLE_BUFF_BINDING 8
33+
#include "occlusionCullingShaderCommon.glsl"
2234

2335
layout(push_constant, row_major) uniform PushConstants
2436
{
2537
CullShaderData_t data;
2638
} pc;
2739

28-
29-
3040
#include <nbl/builtin/glsl/utils/culling.glsl>
3141
#include <nbl/builtin/glsl/utils/transform.glsl>
3242

43+
bool unpackFreezeCullFlag(in uint packedVal)
44+
{
45+
return bool(packedVal >> 16u);
46+
}
47+
48+
uint unpackMaxBatchCount(in uint packedVal)
49+
{
50+
return packedVal & 0x0000FFFFu;
51+
}
3352

3453
void main()
3554
{
36-
if (gl_GlobalInvocationID.x >= pc.data.maxBatchCount)
55+
if (gl_GlobalInvocationID.x >= unpackMaxBatchCount(pc.data.freezeCullingAndMaxBatchCountPacked))
3756
return;
57+
58+
mvpBuff.mvps[gl_GlobalInvocationID.x] = pc.data.viewProjMatrix; // no model matrices
3859

39-
mvpBuff.mvps[gl_GlobalInvocationID.x] = pc.data.viewProjMatrix;
60+
const CullData_t batchCullData = cullData[gl_GlobalInvocationID.x];
61+
const uint drawCommandGUID = batchCullData.drawCommandGUID;
62+
occlusionCommandBuff.draws[drawCommandGUID].instanceCount = 0;
4063

41-
if (bool(pc.data.freezeCulling))
64+
if (unpackFreezeCullFlag(pc.data.freezeCullingAndMaxBatchCountPacked))
4265
return;
4366

44-
const CullData_t batchCullData = cullData[gl_GlobalInvocationID.x];
4567

4668
const mat2x3 bbox = mat2x3(batchCullData.aabbMinEdge,batchCullData.aabbMaxEdge);
4769
bool couldBeVisible = nbl_glsl_couldBeVisible(pc.data.viewProjMatrix,bbox);
48-
49-
commandBuff.draws[batchCullData.drawCommandGUID].instanceCount = couldBeVisible == true ? 1 : 0;
70+
71+
if (couldBeVisible)
72+
{
73+
const vec3 localCameraPos = pc.data.worldCamPos; // true in this case
74+
const bool cameraInsideAABB = all(greaterThanEqual(localCameraPos, batchCullData.aabbMinEdge)) && all(lessThanEqual(localCameraPos, batchCullData.aabbMaxEdge));
75+
const bool assumedVisible = uint(visibleBuff.visible[gl_GlobalInvocationID.x]) == 1u || cameraInsideAABB;
76+
frustumCommandBuff.draws[drawCommandGUID].instanceCount = assumedVisible ? 1u : 0u;
77+
// if not frustum culled and batch was not visible in the last frame, and it makes sense to test
78+
if(!assumedVisible)
79+
{
80+
const uint currCubeIdx = atomicAdd(cubeIndirectDraw.draw.instanceCount, 1);
81+
82+
if(currCubeIdx % WORKGROUP_SIZE == 0)
83+
atomicAdd(occlusionDispatchIndirect.di.num_groups_x, 1);
84+
85+
// only works for a source geometry box which is [0,1]^2, the geometry creator box is [-0.5,0.5]^2, so either make your own box, or work out the math for this
86+
vec3 aabbExtent = batchCullData.aabbMaxEdge - batchCullData.aabbMinEdge;
87+
cubeMvpBuff.cubeMVPs[currCubeIdx] = mat4(
88+
pc.data.viewProjMatrix[0]*aabbExtent.x,
89+
pc.data.viewProjMatrix[1]*aabbExtent.y,
90+
pc.data.viewProjMatrix[2]*aabbExtent.z,
91+
pc.data.viewProjMatrix*vec4(batchCullData.aabbMinEdge,1)
92+
);
93+
94+
cubeDrawGUIDBuffer.drawGUID[currCubeIdx] = drawCommandGUID;
95+
}
96+
}
97+
else
98+
frustumCommandBuff.draws[drawCommandGUID].instanceCount = 0;
99+
100+
// does `freezeCulling` affect this negatively?
101+
visibleBuff.visible[gl_GlobalInvocationID.x] = uint16_t(0u);
50102
}

examples_tests/41.VisibilityBuffer/fillVBuffer.vert

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
layout (push_constant) uniform Block
66
{
7-
uint dataBufferOffset;
7+
uint dataBufferOffset;
88
} pc;
99

1010
layout(location = 0) flat out uint drawGUID;

0 commit comments

Comments
 (0)