|
| 1 | +// detail_cull.cs - GPU-driven frustum culling for detail objects |
| 2 | +// Processes all detail instances and outputs visible indices for indirect drawing |
| 3 | +// |
| 4 | +#define SM_5_0 |
| 5 | +#include "common.h" |
| 6 | + |
| 7 | +// =========================== |
| 8 | +// Structures (must match C++) |
| 9 | +// =========================== |
| 10 | + |
| 11 | +struct DetailInstanceGPU |
| 12 | +{ |
| 13 | + // Transform data (48 bytes) |
| 14 | + float3 position; |
| 15 | + float scale; |
| 16 | + float rotation_y; |
| 17 | + float3 padding0; |
| 18 | + |
| 19 | + // Rendering data (32 bytes) |
| 20 | + float c_hemi; |
| 21 | + float c_sun; |
| 22 | + uint object_id; |
| 23 | + uint vis_id; |
| 24 | + float3 color_rgb; |
| 25 | + float padding1; |
| 26 | + |
| 27 | + // Bounding data (32 bytes) |
| 28 | + float3 bounds_min; |
| 29 | + float bounds_radius; |
| 30 | + float3 bounds_max; |
| 31 | + float padding2; |
| 32 | + |
| 33 | + // Metadata (16 bytes) |
| 34 | + uint slot_x; |
| 35 | + uint slot_z; |
| 36 | + uint flags; |
| 37 | + float fade_distance_sqr; |
| 38 | +}; |
| 39 | + |
| 40 | +struct FrustumPlane |
| 41 | +{ |
| 42 | + float4 plane; // xyz = normal, w = distance |
| 43 | +}; |
| 44 | + |
| 45 | +struct IndirectDrawArgs |
| 46 | +{ |
| 47 | + uint index_count; |
| 48 | + uint instance_count; |
| 49 | + uint start_index; |
| 50 | + int base_vertex; |
| 51 | + uint start_instance; |
| 52 | +}; |
| 53 | + |
| 54 | +// =========================== |
| 55 | +// Input Buffers |
| 56 | +// =========================== |
| 57 | + |
| 58 | +cbuffer CullParams : register(b0) |
| 59 | +{ |
| 60 | + float3 g_camera_pos; |
| 61 | + float g_fade_limit_sqr; |
| 62 | + |
| 63 | + float3 g_camera_dir; |
| 64 | + float g_fade_start_sqr; |
| 65 | + |
| 66 | + float g_r_ssa_discard; |
| 67 | + float g_r_ssa_cheap; |
| 68 | + uint g_instance_count; |
| 69 | + uint g_frame_number; |
| 70 | + |
| 71 | + float4 g_frustum_planes[6]; // Left, Right, Top, Bottom, Near, Far |
| 72 | +}; |
| 73 | + |
| 74 | +// All instances (input, read-only) |
| 75 | +StructuredBuffer<DetailInstanceGPU> g_instances : register(t0); |
| 76 | + |
| 77 | +// =========================== |
| 78 | +// Output Buffers |
| 79 | +// =========================== |
| 80 | + |
| 81 | +// Visible instance indices per vis_id (output, append) |
| 82 | +RWStructuredBuffer<uint> g_visible_still : register(u0); // vis_id = 0 |
| 83 | +RWStructuredBuffer<uint> g_visible_wave1 : register(u1); // vis_id = 1 |
| 84 | +RWStructuredBuffer<uint> g_visible_wave2 : register(u2); // vis_id = 2 |
| 85 | + |
| 86 | +// Atomic counters for each vis_id |
| 87 | +RWStructuredBuffer<uint> g_counters : register(u3); // [0]=still, [1]=wave1, [2]=wave2 |
| 88 | + |
| 89 | +// Indirect draw arguments per vis_id |
| 90 | +RWStructuredBuffer<IndirectDrawArgs> g_indirect_args : register(u4); |
| 91 | + |
| 92 | +// =========================== |
| 93 | +// Culling Functions |
| 94 | +// =========================== |
| 95 | + |
| 96 | +// Test if a sphere intersects or is inside a frustum plane |
| 97 | +bool SphereInsidePlane(float3 center, float radius, float4 plane) |
| 98 | +{ |
| 99 | + float distance = dot(plane.xyz, center) + plane.w; |
| 100 | + return distance > -radius; |
| 101 | +} |
| 102 | + |
| 103 | +// Frustum culling: test sphere against all 6 planes |
| 104 | +bool FrustumCullSphere(float3 center, float radius) |
| 105 | +{ |
| 106 | + [unroll] |
| 107 | + for (uint i = 0; i < 6; ++i) |
| 108 | + { |
| 109 | + if (!SphereInsidePlane(center, radius, g_frustum_planes[i])) |
| 110 | + return false; // Outside frustum |
| 111 | + } |
| 112 | + return true; // Inside or intersecting |
| 113 | +} |
| 114 | + |
| 115 | +// Compute Screen Space Area (SSA) for LOD selection |
| 116 | +float ComputeSSA(float3 world_pos, float radius, float scale) |
| 117 | +{ |
| 118 | + float dist_sqr = dot(world_pos - g_camera_pos, world_pos - g_camera_pos); |
| 119 | + if (dist_sqr < 0.001f) |
| 120 | + return 1e6f; // Very close, assume visible |
| 121 | + |
| 122 | + // SSA = (scale * radius)^2 / distance^2 |
| 123 | + float scaled_radius = scale * radius; |
| 124 | + return (scaled_radius * scaled_radius) / dist_sqr; |
| 125 | +} |
| 126 | + |
| 127 | +// =========================== |
| 128 | +// Compute Shader Entry Point |
| 129 | +// =========================== |
| 130 | + |
| 131 | +[numthreads(256, 1, 1)] |
| 132 | +void main(uint3 dispatch_thread_id : SV_DispatchThreadID) |
| 133 | +{ |
| 134 | + uint instance_idx = dispatch_thread_id.x; |
| 135 | + |
| 136 | + // Early exit if beyond instance count |
| 137 | + if (instance_idx >= g_instance_count) |
| 138 | + return; |
| 139 | + |
| 140 | + // Load instance data |
| 141 | + DetailInstanceGPU inst = g_instances[instance_idx]; |
| 142 | + |
| 143 | + // ========================= |
| 144 | + // Distance Culling |
| 145 | + // ========================= |
| 146 | + float3 to_camera = g_camera_pos - inst.position; |
| 147 | + float dist_sqr = dot(to_camera, to_camera); |
| 148 | + |
| 149 | + // Fade limit culling |
| 150 | + if (dist_sqr > g_fade_limit_sqr) |
| 151 | + return; // Too far, cull |
| 152 | + |
| 153 | + // ========================= |
| 154 | + // Frustum Culling |
| 155 | + // ========================= |
| 156 | + // Use bounding sphere for conservative culling |
| 157 | + float world_radius = inst.bounds_radius * inst.scale; |
| 158 | + if (!FrustumCullSphere(inst.position, world_radius)) |
| 159 | + return; // Outside frustum, cull |
| 160 | + |
| 161 | + // ========================= |
| 162 | + // SSA (Screen Space Area) Culling |
| 163 | + // ========================= |
| 164 | + float ssa = ComputeSSA(inst.position, inst.bounds_radius, inst.scale); |
| 165 | + if (ssa < g_r_ssa_discard) |
| 166 | + return; // Too small, cull |
| 167 | + |
| 168 | + // ========================= |
| 169 | + // Fade Factor (for future use) |
| 170 | + // ========================= |
| 171 | + float fade_alpha = 1.0f; |
| 172 | + if (dist_sqr > g_fade_start_sqr) |
| 173 | + { |
| 174 | + float fade_range = g_fade_limit_sqr - g_fade_start_sqr; |
| 175 | + fade_alpha = 1.0f - ((dist_sqr - g_fade_start_sqr) / fade_range); |
| 176 | + } |
| 177 | + |
| 178 | + // ========================= |
| 179 | + // Append to Visible List |
| 180 | + // ========================= |
| 181 | + uint output_idx = 0; |
| 182 | + |
| 183 | + // Determine which output buffer based on vis_id |
| 184 | + if (inst.vis_id == 0) |
| 185 | + { |
| 186 | + // Still (no animation) |
| 187 | + InterlockedAdd(g_counters[0], 1, output_idx); |
| 188 | + g_visible_still[output_idx] = instance_idx; |
| 189 | + } |
| 190 | + else if (inst.vis_id == 1) |
| 191 | + { |
| 192 | + // Wave 1 |
| 193 | + InterlockedAdd(g_counters[1], 1, output_idx); |
| 194 | + g_visible_wave1[output_idx] = instance_idx; |
| 195 | + } |
| 196 | + else // inst.vis_id == 2 |
| 197 | + { |
| 198 | + // Wave 2 |
| 199 | + InterlockedAdd(g_counters[2], 1, output_idx); |
| 200 | + g_visible_wave2[output_idx] = instance_idx; |
| 201 | + } |
| 202 | +} |
0 commit comments