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.
1import package::{
2 render::{
3 pbr_prepass_functions,
4 pbr_bindings,
5 pbr_bindings::material,
6 pbr_types,
7 pbr_functions,
8 pbr_functions::SampleBias,
9 mesh_bindings::mesh,
10 mesh_view_bindings::view,
11 },
12 prepass::io as prepass_io,
13};
15import bevy_render::bindless::{bindless_samplers_filtering, bindless_textures_2d};
17@if(MESHLET_MESH_MATERIAL_PASS)
18import package::meshlet::visibility_buffer_resolve::resolve_vertex_output;
20@if(BINDLESS)
21import package::render::pbr_bindings::material_indices;
23@if(PREPASS_FRAGMENT)
24@fragment
25fn fragment(
26@if(MESHLET_MESH_MATERIAL_PASS)
27 @builtin(position) frag_coord: vec4<f32>,
28@if(!MESHLET_MESH_MATERIAL_PASS)
29 in: prepass_io::VertexOutput,
30@if(!MESHLET_MESH_MATERIAL_PASS)
31 @builtin(front_facing) is_front: bool,
32) -> prepass_io::FragmentOutput {
33@if(MESHLET_MESH_MATERIAL_PASS)
34 let in = resolve_vertex_output(frag_coord);
35@if(MESHLET_MESH_MATERIAL_PASS)
36 let is_front = true;
38@if(!MESHLET_MESH_MATERIAL_PASS && BINDLESS)
39 let slot = mesh[in.instance_index].material_and_lightmap_bind_group_slot & 0xffffu;
40@if(!MESHLET_MESH_MATERIAL_PASS && BINDLESS)
41 let flags = pbr_bindings::material_array[material_indices[slot].material].flags;
42@if(!MESHLET_MESH_MATERIAL_PASS && BINDLESS)
43 let uv_transform = pbr_bindings::material_array[material_indices[slot].material].uv_transform;
44@if(!MESHLET_MESH_MATERIAL_PASS && !BINDLESS)
45 let flags = pbr_bindings::material.flags;
46@if(!MESHLET_MESH_MATERIAL_PASS && !BINDLESS)
47 let uv_transform = pbr_bindings::material.uv_transform;
49 // If we're in the crossfade section of a visibility range, conditionally
50 // discard the fragment according to the visibility pattern.
51@if(!MESHLET_MESH_MATERIAL_PASS && VISIBILITY_RANGE_DITHER)
52 pbr_functions::visibility_range_dither(in.position, in.visibility_range_dither);
54@if(!MESHLET_MESH_MATERIAL_PASS)
55 pbr_prepass_functions::prepass_sample_color_and_alpha_discard(in);
57 var out: prepass_io::FragmentOutput;
59@if(UNCLIPPED_DEPTH_ORTHO_EMULATION)
60 out.frag_depth = in.unclipped_depth;
62 // NOTE: Unlit bit not set means == 0 is true, so the true case is if lit
63@if(NORMAL_PREPASS)
64 if (flags & pbr_types::STANDARD_MATERIAL_FLAGS_UNLIT_BIT) == 0u {
65 let double_sided = (flags & pbr_types::STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u;
66@if(MESHLET_MESH_MATERIAL_PASS)
67 let mesh_flags = in.mesh_flags;
68@else
69 let mesh_flags = mesh[in.instance_index].flags;
70 let is_logically_front = pbr_functions::winding_corrected_front_facing(mesh_flags, is_front);
72 let world_normal = pbr_functions::prepare_world_normal(
73 in.world_normal,
74 double_sided,
75 is_logically_front,
76 );
78 var normal = world_normal;
80@if(VERTEX_UVS) {
81@if(VERTEX_TANGENTS) {
82@if(STANDARD_MATERIAL_NORMAL_MAP) {
84// TODO: Transforming UVs mean we need to apply derivative chain rule for meshlet mesh material pass
85@if(STANDARD_MATERIAL_NORMAL_MAP_UV_B)
86 let uv = (uv_transform * vec3(in.uv_b, 1.0)).xy;
87@else
88 let uv = (uv_transform * vec3(in.uv, 1.0)).xy;
90 // Fill in the sample bias so we can sample from textures.
91 var bias: SampleBias;
92@if(MESHLET_MESH_MATERIAL_PASS) {
93 // Apply mip bias by scaling the gradients used by textureSampleGrad.
94 let mip_scale = exp2(view.mip_bias);
95 bias.ddx_uv = in.ddx_uv * mip_scale;
96 bias.ddy_uv = in.ddy_uv * mip_scale;
98@if(!MESHLET_MESH_MATERIAL_PASS)
99 bias.mip_bias = view.mip_bias;
101@if(BINDLESS)
102 let texture = bindless_textures_2d[material_indices[slot].normal_map_texture];
103@else
104 let texture = pbr_bindings::normal_map_texture;
105@if(BINDLESS)
106 let sampler_ = bindless_samplers_filtering[material_indices[slot].normal_map_sampler];
107@else
108 let sampler_ = pbr_bindings::normal_map_sampler;
109@if(MESHLET_MESH_MATERIAL_PASS)
110 let Nt =
111 textureSampleGrad(
112 texture,
113 sampler_,
114 uv,
115 bias.ddx_uv,
116 bias.ddy_uv,
117 ).rgb;
118@if(!MESHLET_MESH_MATERIAL_PASS)
119 let Nt =
120 textureSampleBias(
121 texture,
122 sampler_,
123 uv,
124 bias.mip_bias,
125 ).rgb;
126 let TBN = pbr_functions::calculate_tbn_mikktspace(normal, in.world_tangent);
128 normal = pbr_functions::apply_normal_mapping(
129 flags,
130 TBN,
131 double_sided,
132 is_logically_front,
133 Nt,
134 );
140 out.normal = vec4(normal * 0.5 + vec3(0.5), 1.0);
141 } else {
142 out.normal = vec4(in.world_normal * 0.5 + vec3(0.5), 1.0);
143 }
145@if(MOTION_VECTOR_PREPASS && MESHLET_MESH_MATERIAL_PASS)
146 out.motion_vector = in.motion_vector;
147@if(MOTION_VECTOR_PREPASS && !MESHLET_MESH_MATERIAL_PASS)
148 out.motion_vector = pbr_prepass_functions::calculate_motion_vector(in.world_position, in.previous_world_position);
150 return out;
152@else
153@fragment
154fn fragment(in: prepass_io::VertexOutput) {
155 pbr_prepass_functions::prepass_sample_color_and_alpha_discard(in);
Contents