Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1#define_import_path bevy_core_pipeline::oit
3#import bevy_pbr::mesh_view_bindings::{view, oit_layers, oit_layer_ids, oit_settings}
5#ifdef OIT_ENABLED
6// Add the fragment to the oit buffer
7fn oit_draw(position: vec4f, color: vec4f) {
8 // Don't add fully transparent fragments to the list
9 // because we don't want to have to sort them in the resolve pass
10 if color.a < oit_settings.alpha_threshold {
11 return;
12 }
13 // get the index of the current fragment relative to the screen size
14 let screen_index = i32(floor(position.x) + floor(position.y) * view.viewport.z);
15 // get the size of the buffer.
16 // It's always the size of the screen
17 let buffer_size = i32(view.viewport.z * view.viewport.w);
19 // gets the layer index of the current fragment
20 var layer_id = atomicAdd(&oit_layer_ids[screen_index], 1);
21 // exit early if we've reached the maximum amount of fragments per layer
22 if layer_id >= oit_settings.layers_count {
23 // force to store the oit_layers_count to make sure we don't
24 // accidentally increase the index above the maximum value
25 atomicStore(&oit_layer_ids[screen_index], oit_settings.layers_count);
26 // TODO for tail blending we should return the color here
27 return;
28 }
30 // get the layer_index from the screen
31 let layer_index = screen_index + layer_id * buffer_size;
32 let rgb9e5_color = bevy_pbr::rgb9e5::vec3_to_rgb9e5_(color.rgb);
33 let depth_alpha = pack_24bit_depth_8bit_alpha(position.z, color.a);
34 oit_layers[layer_index] = vec2(rgb9e5_color, depth_alpha);
36#endif // OIT_ENABLED
38fn pack_24bit_depth_8bit_alpha(depth: f32, alpha: f32) -> u32 {
39 let depth_bits = u32(saturate(depth) * f32(0xFFFFFFu) + 0.5);
40 let alpha_bits = u32(saturate(alpha) * f32(0xFFu) + 0.5);
41 return (depth_bits & 0xFFFFFFu) | ((alpha_bits & 0xFFu) << 24u);
44fn unpack_24bit_depth_8bit_alpha(packed: u32) -> vec2<f32> {
45 let depth_bits = packed & 0xFFFFFFu;
46 let alpha_bits = (packed >> 24u) & 0xFFu;
47 return vec2(f32(depth_bits) / f32(0xFFFFFFu), f32(alpha_bits) / f32(0xFFu));
Contents