Compute shader frustum culling and lod selection example (wip)
This commit is contained in:
parent
d95d5c8991
commit
6a0340ff21
11 changed files with 1785 additions and 0 deletions
559
data/models/suzanne_lods.dae
Normal file
559
data/models/suzanne_lods.dae
Normal file
File diff suppressed because one or more lines are too long
125
data/shaders/computecullandlod/cull.comp
Normal file
125
data/shaders/computecullandlod/cull.comp
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
#version 450
|
||||
|
||||
#extension GL_ARB_separate_shader_objects : enable
|
||||
#extension GL_ARB_shading_language_420pack : enable
|
||||
|
||||
layout (constant_id = 0) const int MAX_LOD_LEVEL = 5;
|
||||
|
||||
struct InstanceData
|
||||
{
|
||||
vec4 pos;
|
||||
vec4 rot;
|
||||
};
|
||||
|
||||
// Binding 0: Instance input data for culling
|
||||
layout (binding = 0, std140) buffer Instances
|
||||
{
|
||||
InstanceData instances[ ];
|
||||
};
|
||||
|
||||
// Same layout as VkDrawIndexedIndirectCommand
|
||||
struct IndexedIndirectCommand
|
||||
{
|
||||
uint indexCount;
|
||||
uint instanceCount;
|
||||
uint firstIndex;
|
||||
uint vertexOffset;
|
||||
uint firstInstance;
|
||||
};
|
||||
|
||||
// Binding 1: Multi draw output
|
||||
layout (binding = 1, std430) writeonly buffer IndirectDraws
|
||||
{
|
||||
IndexedIndirectCommand indirectDraws[ ];
|
||||
};
|
||||
|
||||
// Binding 2: Uniform block object with matrices
|
||||
layout (binding = 2) uniform UBO
|
||||
{
|
||||
mat4 projection;
|
||||
mat4 modelview;
|
||||
vec4 cameraPos;
|
||||
vec4 frustumPlanes[6];
|
||||
} ubo;
|
||||
|
||||
// Binding 3: Indirect draw stats
|
||||
layout (binding = 3) buffer UBOOut
|
||||
{
|
||||
uint drawCount;
|
||||
uint lodCount[MAX_LOD_LEVEL + 1];
|
||||
} uboOut;
|
||||
|
||||
// Binding 4: level-of-detail information
|
||||
struct LOD
|
||||
{
|
||||
uint firstIndex;
|
||||
uint indexCount;
|
||||
float distance;
|
||||
float _pad0;
|
||||
};
|
||||
layout (binding = 4) readonly buffer LODs
|
||||
{
|
||||
LOD lods[ ];
|
||||
};
|
||||
|
||||
layout (local_size_x = 16) in;
|
||||
|
||||
bool frustumCheck(vec4 pos, float radius)
|
||||
{
|
||||
// Check sphere against frustum planes
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
if (dot(pos, ubo.frustumPlanes[i]) + radius < 0.0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
layout (local_size_x = 16) in;
|
||||
|
||||
void main()
|
||||
{
|
||||
uint idx = gl_GlobalInvocationID.x + gl_GlobalInvocationID.y * gl_NumWorkGroups.x * gl_WorkGroupSize.x;
|
||||
|
||||
// Clear stats on first invocation
|
||||
if (idx == 0)
|
||||
{
|
||||
atomicExchange(uboOut.drawCount, 0);
|
||||
for (uint i = 0; i < MAX_LOD_LEVEL + 1; i++)
|
||||
{
|
||||
atomicExchange(uboOut.lodCount[i], 0);
|
||||
}
|
||||
}
|
||||
|
||||
vec4 pos = vec4(instances[idx].pos.xyz, 1.0);
|
||||
|
||||
// Check if object is within current viewing frustum
|
||||
if (frustumCheck(pos, 1.0))
|
||||
{
|
||||
indirectDraws[idx].instanceCount = 1;
|
||||
|
||||
// Increase number of indirect draw counts
|
||||
atomicAdd(uboOut.drawCount, 1);
|
||||
|
||||
// Select appropriate LOD level based on distance to camera
|
||||
uint lodLevel = MAX_LOD_LEVEL;
|
||||
for (uint i = 0; i < MAX_LOD_LEVEL; i++)
|
||||
{
|
||||
if (distance(instances[idx].pos.xyz, ubo.cameraPos.xyz) < lods[i].distance)
|
||||
{
|
||||
lodLevel = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
indirectDraws[idx].firstIndex = lods[lodLevel].firstIndex;
|
||||
indirectDraws[idx].indexCount = lods[lodLevel].indexCount;
|
||||
// Update stats
|
||||
atomicAdd(uboOut.lodCount[lodLevel], 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
indirectDraws[idx].instanceCount = 0;
|
||||
}
|
||||
}
|
||||
BIN
data/shaders/computecullandlod/cull.comp.spv
Normal file
BIN
data/shaders/computecullandlod/cull.comp.spv
Normal file
Binary file not shown.
21
data/shaders/computecullandlod/indirectdraw.frag
Normal file
21
data/shaders/computecullandlod/indirectdraw.frag
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#version 450
|
||||
|
||||
#extension GL_ARB_separate_shader_objects : enable
|
||||
#extension GL_ARB_shading_language_420pack : enable
|
||||
|
||||
layout (location = 0) in vec3 inNormal;
|
||||
layout (location = 1) in vec3 inColor;
|
||||
layout (location = 2) in vec3 inUV;
|
||||
layout (location = 3) in vec3 inViewVec;
|
||||
layout (location = 4) in vec3 inLightVec;
|
||||
|
||||
layout (location = 0) out vec4 outFragColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 N = normalize(inNormal);
|
||||
vec3 L = normalize(inLightVec);
|
||||
vec3 ambient = vec3(0.25);
|
||||
vec3 diffuse = vec3(max(dot(N, L), 0.0));
|
||||
outFragColor = vec4((ambient + diffuse) * inColor, 1.0);
|
||||
}
|
||||
BIN
data/shaders/computecullandlod/indirectdraw.frag.spv
Normal file
BIN
data/shaders/computecullandlod/indirectdraw.frag.spv
Normal file
Binary file not shown.
51
data/shaders/computecullandlod/indirectdraw.vert
Normal file
51
data/shaders/computecullandlod/indirectdraw.vert
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
#version 450
|
||||
|
||||
#extension GL_ARB_separate_shader_objects : enable
|
||||
#extension GL_ARB_shading_language_420pack : enable
|
||||
|
||||
// Vertex attributes
|
||||
layout (location = 0) in vec4 inPos;
|
||||
layout (location = 1) in vec3 inNormal;
|
||||
layout (location = 2) in vec2 inUV;
|
||||
layout (location = 3) in vec3 inColor;
|
||||
|
||||
// Instanced attributes
|
||||
layout (location = 4) in vec3 instancePos;
|
||||
layout (location = 5) in vec3 instanceRot;
|
||||
layout (location = 6) in float instanceScale;
|
||||
layout (location = 7) in float instanceTexIndex;
|
||||
|
||||
layout (binding = 0) uniform UBO
|
||||
{
|
||||
mat4 projection;
|
||||
mat4 modelview;
|
||||
} ubo;
|
||||
|
||||
layout (location = 0) out vec3 outNormal;
|
||||
layout (location = 1) out vec3 outColor;
|
||||
layout (location = 2) out vec3 outUV;
|
||||
layout (location = 3) out vec3 outViewVec;
|
||||
layout (location = 4) out vec3 outLightVec;
|
||||
|
||||
out gl_PerVertex
|
||||
{
|
||||
vec4 gl_Position;
|
||||
};
|
||||
|
||||
void main()
|
||||
{
|
||||
outColor = inColor;
|
||||
outUV = vec3(inUV, instanceTexIndex);
|
||||
outUV.t = 1.0 - outUV.t;
|
||||
|
||||
outNormal = inNormal;// * mat3(rotMat);
|
||||
|
||||
vec4 pos = vec4((inPos.xyz * instanceScale) + instancePos, 1.0)/* rotMat*/;
|
||||
|
||||
gl_Position = ubo.projection * ubo.modelview * pos;
|
||||
|
||||
vec4 wPos = ubo.modelview * vec4(pos.xyz, 1.0);
|
||||
vec4 lPos = vec4(0.0, 10.0, 50.0, 1.0);
|
||||
outLightVec = lPos.xyz - pos.xyz;
|
||||
outViewVec = -pos.xyz;
|
||||
}
|
||||
BIN
data/shaders/computecullandlod/indirectdraw.vert.spv
Normal file
BIN
data/shaders/computecullandlod/indirectdraw.vert.spv
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue