17#import bevy_pbr::mesh_preprocess_types::{ 18 IndirectParametersCpuMetadata, IndirectParametersGpuMetadata, MeshInput, PreprocessWorkItem 20#import bevy_pbr::mesh_types::{ 21 Mesh, MESH_FLAGS_AABB_BASED_VISIBILITY_RANGE_BIT, MESH_FLAGS_NO_FRUSTUM_CULLING_BIT, 22 MESH_FLAGS_VISIBILITY_RANGE_INDEX_BITS 24#import bevy_pbr::mesh_view_bindings::view 25#import bevy_pbr::occlusion_culling 26#import bevy_pbr::prepass_bindings::previous_view_uniforms 27#import bevy_pbr::view_transformations::{ 28 position_world_to_ndc, position_world_to_view, ndc_to_uv, view_z_to_depth_ndc, 29 position_world_to_prev_ndc, position_world_to_prev_view, prev_view_z_to_depth_ndc 31#import bevy_render::maths 32#import bevy_render::view::View 37struct MeshCullingData { 40 aabb_center: vec4<f32>, 43 aabb_half_extents: vec4<f32>, 48struct LatePreprocessWorkItemIndirectParameters { 52 dispatch_x: atomic<u32>, 58 work_item_count: atomic<u32>, 71 late_preprocess_work_item_indirect_offset: u32, 75@group(0) @binding(3) var<storage> current_input: array<MeshInput>; 77@group(0) @binding(4) var<storage> previous_input: array<MeshInput>; 81@group(0) @binding(5) var<storage> work_items: array<PreprocessWorkItem>; 83@group(0) @binding(6) var<storage, read_write> output: array<Mesh>; 87@group(0) @binding(7) var<storage> indirect_parameters_cpu_metadata: 88 array<IndirectParametersCpuMetadata>; 90@group(0) @binding(8) var<storage, read_write> indirect_parameters_gpu_metadata: 91 array<IndirectParametersGpuMetadata>; 98@group(0) @binding(9) var<storage> mesh_culling_data: array<MeshCullingData>; 100@group(0) @binding(10) var<storage> visibility_ranges: array<vec4<f32>>; 101#endif // FRUSTUM_CULLING 103#ifdef OCCLUSION_CULLING 104@group(0) @binding(11) var depth_pyramid: texture_2d<f32>; 107@group(0) @binding(12) var<storage, read_write> late_preprocess_work_items: 108 array<PreprocessWorkItem>; 110@group(0) @binding(13) var<storage, read_write> late_preprocess_work_item_indirect_parameters: 111 array<LatePreprocessWorkItemIndirectParameters>; 115@group(0) @binding(13) var<storage, read> late_preprocess_work_item_indirect_parameters: 116 array<LatePreprocessWorkItemIndirectParameters>; 119var<immediate> immediates: Immediates; 120#endif // OCCLUSION_CULLING 122#ifdef FRUSTUM_CULLING 126fn view_frustum_intersects_obb( 127 world_from_local: mat4x4<f32>, 128 aabb_center: vec4<f32>, 129 aabb_half_extents: vec3<f32>, 132 for (var i = 0; i < 5; i += 1) { 134 let plane_normal = view.frustum[i]; 135 let relative_radius = dot( 138 dot(plane_normal.xyz, world_from_local[0].xyz), 139 dot(plane_normal.xyz, world_from_local[1].xyz), 140 dot(plane_normal.xyz, world_from_local[2].xyz), 147 if (!maths::sphere_intersects_plane_half_space( 148 plane_normal, aabb_center, relative_radius)) { 159fn main(@builtin(global_invocation_id) global_invocation_id: vec3<u32>) { 162 let instance_index = global_invocation_id.x; 165 if (instance_index >= atomicLoad(&late_preprocess_work_item_indirect_parameters[ 166 immediates.late_preprocess_work_item_indirect_offset].work_item_count)) { 170 if (instance_index >= arrayLength(&work_items)) { 176 let input_index = work_items[instance_index].input_index; 178 let indirect_parameters_index = work_items[instance_index].output_or_indirect_parameters_index; 184 if (instance_index == 0u) || (work_items[instance_index - 1].output_or_indirect_parameters_index != indirect_parameters_index) { 185 indirect_parameters_gpu_metadata[indirect_parameters_index].mesh_index = input_index; 190 let mesh_output_index = work_items[instance_index].output_or_indirect_parameters_index; 194 let world_from_local_affine_transpose = current_input[input_index].world_from_local; 195 let world_from_local = maths::affine3_to_square(world_from_local_affine_transpose); 197#ifdef FRUSTUM_CULLING 199 if ((current_input[input_index].flags & MESH_FLAGS_NO_FRUSTUM_CULLING_BIT) == 0u) { 200 let aabb_center = mesh_culling_data[input_index].aabb_center.xyz; 201 let aabb_half_extents = mesh_culling_data[input_index].aabb_half_extents.xyz; 204 let model_center = world_from_local * vec4(aabb_center, 1.0); 205 if (!view_frustum_intersects_obb(world_from_local, model_center, aabb_half_extents)) { 211 let visibility_buffer_array_len = arrayLength(&visibility_ranges); 212 let visibility_buffer_index = 213 current_input[input_index].flags & MESH_FLAGS_VISIBILITY_RANGE_INDEX_BITS; 214 if (visibility_buffer_index < visibility_buffer_array_len) { 215 let lod_range = visibility_ranges[visibility_buffer_index]; 219 var world_pos: vec3<f32>; 220 if ((current_input[input_index].flags & MESH_FLAGS_AABB_BASED_VISIBILITY_RANGE_BIT) != 0u) { 221 let aabb_center = mesh_culling_data[input_index].aabb_center.xyz; 222 world_pos = (world_from_local * vec4(aabb_center, 1.0)).xyz; 224 world_pos = world_from_local[3].xyz; 227 let camera_distance = length(world_pos - view.lod_view_world_position); 229 if (camera_distance < lod_range.x || camera_distance >= lod_range.w) { 233#endif // FRUSTUM_CULLING 240 let timestamp = current_input[input_index].timestamp; 241 let mesh_changed_this_frame = timestamp == view.frame_count; 244 let previous_input_index = current_input[input_index].previous_input_index; 245 var previous_world_from_local_affine_transpose: mat3x4<f32>; 246 if (mesh_changed_this_frame && previous_input_index != 0xffffffffu) { 247 previous_world_from_local_affine_transpose = 248 previous_input[previous_input_index].world_from_local; 250 previous_world_from_local_affine_transpose = world_from_local_affine_transpose; 252 let previous_world_from_local = 253 maths::affine3_to_square(previous_world_from_local_affine_transpose); 261#ifdef OCCLUSION_CULLING 262 let aabb_center = mesh_culling_data[input_index].aabb_center.xyz; 263 let aabb_half_extents = mesh_culling_data[input_index].aabb_half_extents.xyz; 266 let infinity = bitcast<f32>(0x7f800000u); 267 let neg_infinity = bitcast<f32>(0xff800000u); 268 var aabb = vec4(infinity, infinity, neg_infinity, neg_infinity); 269 var max_depth_view = neg_infinity; 273 for (var i = 0u; i < 8u; i += 1u) { 274 let local_pos = aabb_center + select( 277 vec3((i & 1) != 0, (i & 2) != 0, (i & 4) != 0) 278 ) * aabb_half_extents; 283 let prev_world_pos = (previous_world_from_local * vec4(local_pos, 1.0)).xyz; 284 let view_pos = position_world_to_prev_view(prev_world_pos); 285 let ndc_pos = position_world_to_prev_ndc(prev_world_pos); 289 let world_pos = (world_from_local * vec4(local_pos, 1.0)).xyz; 290 let view_pos = position_world_to_view(world_pos); 291 let ndc_pos = position_world_to_ndc(world_pos); 294 let uv_pos = ndc_to_uv(ndc_pos.xy); 297 aabb = vec4(min(aabb.xy, uv_pos), max(aabb.zw, uv_pos)); 298 max_depth_view = max(max_depth_view, view_pos.z); 303 max_depth_view = min(-previous_view_uniforms.clip_from_view[3][2], max_depth_view); 305 max_depth_view = min(-view.clip_from_view[3][2], max_depth_view); 310 let aabb_pixel_size = occlusion_culling::get_aabb_size_in_pixels(aabb, depth_pyramid); 311 let occluder_depth_ndc = 312 occlusion_culling::get_occluder_depth(aabb, aabb_pixel_size, depth_pyramid); 315 let max_depth_ndc = prev_view_z_to_depth_ndc(max_depth_view); 317 let max_depth_ndc = view_z_to_depth_ndc(max_depth_view); 321 if (max_depth_ndc < occluder_depth_ndc) { 327 let output_work_item_index = atomicAdd(&late_preprocess_work_item_indirect_parameters[ 328 immediates.late_preprocess_work_item_indirect_offset].work_item_count, 1u); 329 if (output_work_item_index % 64u == 0u) { 334 atomicAdd(&late_preprocess_work_item_indirect_parameters[ 335 immediates.late_preprocess_work_item_indirect_offset].dispatch_x, 1u); 339 late_preprocess_work_items[output_work_item_index].input_index = input_index; 340 late_preprocess_work_items[output_work_item_index].output_or_indirect_parameters_index = 341 indirect_parameters_index; 346#endif // OCCLUSION_CULLING 349 let local_from_world_transpose = transpose(maths::inverse_affine3(transpose( 350 world_from_local_affine_transpose))); 353 let local_from_world_transpose_a = mat2x4<f32>( 354 vec4<f32>(local_from_world_transpose[0].xyz, local_from_world_transpose[1].x), 355 vec4<f32>(local_from_world_transpose[1].yz, local_from_world_transpose[2].xy)); 356 let local_from_world_transpose_b = local_from_world_transpose[2].z; 364 let batch_output_index = atomicLoad( 365 &indirect_parameters_gpu_metadata[indirect_parameters_index].early_instance_count 367 &indirect_parameters_gpu_metadata[indirect_parameters_index].late_instance_count, 371 let batch_output_index = atomicAdd( 372 &indirect_parameters_gpu_metadata[indirect_parameters_index].early_instance_count, 377 let mesh_output_index = 378 indirect_parameters_cpu_metadata[indirect_parameters_index].base_output_index + 384 output[mesh_output_index].world_from_local = world_from_local_affine_transpose; 385 output[mesh_output_index].previous_world_from_local = 386 previous_world_from_local_affine_transpose; 387 output[mesh_output_index].local_from_world_transpose_a = local_from_world_transpose_a; 388 output[mesh_output_index].local_from_world_transpose_b = local_from_world_transpose_b; 389 output[mesh_output_index].flags = current_input[input_index].flags; 390 output[mesh_output_index].lightmap_uv_rect = current_input[input_index].lightmap_uv_rect; 391 output[mesh_output_index].first_vertex_index = current_input[input_index].first_vertex_index; 392 output[mesh_output_index].current_skin_index = current_input[input_index].current_skin_index; 393 output[mesh_output_index].material_and_lightmap_bind_group_slot = 394 current_input[input_index].material_and_lightmap_bind_group_slot; 395 output[mesh_output_index].tag = current_input[input_index].tag; 396 output[mesh_output_index].morph_descriptor_index = current_input[input_index].morph_descriptor_index;