| 1 |
#[vertex] |
| 2 |
|
| 3 |
#version 450 |
| 4 |
|
| 5 |
#VERSION_DEFINES |
| 6 |
|
| 7 |
layout(location = 0) in vec3 vertex_attrib; |
| 8 |
|
| 9 |
layout(location = 0) out float depth_interp; |
| 10 |
layout(location = 1) out flat uint element_index; |
| 11 |
|
| 12 |
layout(push_constant, std430) uniform Params { |
| 13 |
uint base_index; |
| 14 |
uint pad0; |
| 15 |
uint pad1; |
| 16 |
uint pad2; |
| 17 |
} |
| 18 |
params; |
| 19 |
|
| 20 |
layout(set = 0, binding = 1, std140) uniform State { |
| 21 |
mat4 projection; |
| 22 |
|
| 23 |
float inv_z_far; |
| 24 |
uint screen_to_clusters_shift; // shift to obtain coordinates in block indices |
| 25 |
uint cluster_screen_width; // |
| 26 |
uint cluster_data_size; // how much data for a single cluster takes |
| 27 |
|
| 28 |
uint cluster_depth_offset; |
| 29 |
uint pad0; |
| 30 |
uint pad1; |
| 31 |
uint pad2; |
| 32 |
} |
| 33 |
state; |
| 34 |
|
| 35 |
struct RenderElement { |
| 36 |
uint type; //0-4 |
| 37 |
bool touches_near; |
| 38 |
bool touches_far; |
| 39 |
uint original_index; |
| 40 |
mat3x4 transform_inv; |
| 41 |
vec3 scale; |
| 42 |
uint pad; |
| 43 |
}; |
| 44 |
|
| 45 |
layout(set = 0, binding = 2, std430) buffer restrict readonly RenderElements { |
| 46 |
RenderElement data[]; |
| 47 |
} |
| 48 |
render_elements; |
| 49 |
|
| 50 |
void main() { |
| 51 |
element_index = params.base_index + gl_InstanceIndex; |
| 52 |
|
| 53 |
vec3 vertex = vertex_attrib; |
| 54 |
vertex *= render_elements.data[element_index].scale; |
| 55 |
|
| 56 |
vertex = vec4(vertex, 1.0) * render_elements.data[element_index].transform_inv; |
| 57 |
depth_interp = -vertex.z; |
| 58 |
|
| 59 |
gl_Position = state.projection * vec4(vertex, 1.0); |
| 60 |
} |
| 61 |
|
| 62 |
#[fragment] |
| 63 |
|
| 64 |
#version 450 |
| 65 |
|
| 66 |
#VERSION_DEFINES |
| 67 |
|
| 68 |
#extension GL_KHR_shader_subgroup_ballot : enable |
| 69 |
#extension GL_KHR_shader_subgroup_arithmetic : enable |
| 70 |
#extension GL_KHR_shader_subgroup_vote : enable |
| 71 |
|
| 72 |
layout(location = 0) in float depth_interp; |
| 73 |
layout(location = 1) in flat uint element_index; |
| 74 |
|
| 75 |
layout(set = 0, binding = 1, std140) uniform State { |
| 76 |
mat4 projection; |
| 77 |
float inv_z_far; |
| 78 |
uint screen_to_clusters_shift; // shift to obtain coordinates in block indices |
| 79 |
uint cluster_screen_width; // |
| 80 |
uint cluster_data_size; // how much data for a single cluster takes |
| 81 |
uint cluster_depth_offset; |
| 82 |
uint pad0; |
| 83 |
uint pad1; |
| 84 |
uint pad2; |
| 85 |
} |
| 86 |
state; |
| 87 |
|
| 88 |
//cluster data is layout linearly, each cell contains the follow information: |
| 89 |
// - list of bits for every element to mark as used, so (max_elem_count/32)*4 uints |
| 90 |
// - a uint for each element to mark the depth bits used when rendering (0-31) |
| 91 |
|
| 92 |
layout(set = 0, binding = 3, std430) buffer restrict ClusterRender { |
| 93 |
uint data[]; |
| 94 |
} |
| 95 |
cluster_render; |
| 96 |
|
| 97 |
#ifdef USE_ATTACHMENT |
| 98 |
layout(location = 0) out vec4 frag_color; |
| 99 |
#endif |
| 100 |
|
| 101 |
void main() { |
| 102 |
//convert from screen to cluster |
| 103 |
uvec2 cluster = uvec2(gl_FragCoord.xy) >> state.screen_to_clusters_shift; |
| 104 |
|
| 105 |
//get linear cluster offset from screen poss |
| 106 |
uint cluster_offset = cluster.x + state.cluster_screen_width * cluster.y; |
| 107 |
//multiply by data size to position at the beginning of the element list for this cluster |
| 108 |
cluster_offset *= state.cluster_data_size; |
| 109 |
|
| 110 |
//find the current element in the list and plot the bit to mark it as used |
| 111 |
uint usage_write_offset = cluster_offset + (element_index >> 5); |
| 112 |
uint usage_write_bit = 1 << (element_index & 0x1F); |
| 113 |
|
| 114 |
uint aux = 0; |
| 115 |
|
| 116 |
uint cluster_thread_group_index; |
| 117 |
#ifndef MOLTENVK_USED |
| 118 |
if (!gl_HelperInvocation) { |
| 119 |
#else |
| 120 |
{ |
| 121 |
#endif |
| 122 |
//https://advances.realtimerendering.com/s2017/2017_Sig_Improved_Culling_final.pdf |
| 123 |
|
| 124 |
uvec4 mask; |
| 125 |
|
| 126 |
while (true) { |
| 127 |
// find the cluster offset of the first active thread |
| 128 |
// threads that did break; go inactive and no longer count |
| 129 |
uint first = subgroupBroadcastFirst(cluster_offset); |
| 130 |
// update the mask for thread that match this cluster |
| 131 |
mask = subgroupBallot(first == cluster_offset); |
| 132 |
if (first == cluster_offset) { |
| 133 |
// This thread belongs to the group of threads that match this offset, |
| 134 |
// so exit the loop. |
| 135 |
break; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
cluster_thread_group_index = subgroupBallotExclusiveBitCount(mask); |
| 140 |
|
| 141 |
if (cluster_thread_group_index == 0) { |
| 142 |
aux = atomicOr(cluster_render.data[usage_write_offset], usage_write_bit); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
//find the current element in the depth usage list and mark the current depth as used |
| 147 |
float unit_depth = depth_interp * state.inv_z_far; |
| 148 |
|
| 149 |
uint z_bit = clamp(uint(floor(unit_depth * 32.0)), 0, 31); |
| 150 |
|
| 151 |
uint z_write_offset = cluster_offset + state.cluster_depth_offset + element_index; |
| 152 |
uint z_write_bit = 1 << z_bit; |
| 153 |
|
| 154 |
#ifndef MOLTENVK_USED |
| 155 |
if (!gl_HelperInvocation) { |
| 156 |
#else |
| 157 |
{ |
| 158 |
#endif |
| 159 |
z_write_bit = subgroupOr(z_write_bit); //merge all Zs |
| 160 |
if (cluster_thread_group_index == 0) { |
| 161 |
aux = atomicOr(cluster_render.data[z_write_offset], z_write_bit); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
#ifdef USE_ATTACHMENT |
| 166 |
frag_color = vec4(float(aux)); |
| 167 |
#endif |
| 168 |
} |