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// The shader that goes with `extended_material_bindless.rs`.
2//
3// This code demonstrates how to write shaders that are compatible with both
4// bindless and non-bindless mode. See the `#ifdef BINDLESS` blocks.
6#import bevy_pbr::{
7 forward_io::{FragmentOutput, VertexOutput},
8 mesh_bindings::mesh,
9 pbr_fragment::pbr_input_from_standard_material,
10 pbr_functions::{apply_pbr_lighting, main_pass_post_lighting_processing},
12#import bevy_render::bindless::{bindless_samplers_filtering, bindless_textures_2d}
14#ifdef BINDLESS
15#import bevy_pbr::pbr_bindings::{material_array, material_indices}
16#else // BINDLESS
17#import bevy_pbr::pbr_bindings::material
18#endif // BINDLESS
20// Stores the indices of the bindless resources in the bindless resource arrays,
21// for the `ExampleBindlessExtension` fields.
22struct ExampleBindlessExtendedMaterialIndices {
23 // The index of the `ExampleBindlessExtendedMaterial` data in
24 // `example_extended_material`.
25 material: u32,
26 // The index of the texture we're going to modulate the base color with in
27 // the `bindless_textures_2d` array.
28 modulate_texture: u32,
29 // The index of the sampler we're going to sample the modulated texture with
30 // in the `bindless_samplers_filtering` array.
31 modulate_texture_sampler: u32,
34// Plain data associated with this example material.
35struct ExampleBindlessExtendedMaterial {
36 // The color that we multiply the base color, base color texture, and
37 // modulated texture with.
38 modulate_color: vec4<f32>,
41#ifdef BINDLESS
43// The indices of the bindless resources in the bindless resource arrays, for
44// the `ExampleBindlessExtension` fields.
45@group(2) @binding(100) var<storage> example_extended_material_indices:
46 array<ExampleBindlessExtendedMaterialIndices>;
47// An array that holds the `ExampleBindlessExtendedMaterial` plain old data,
48// indexed by `ExampleBindlessExtendedMaterialIndices.material`.
49@group(2) @binding(101) var<storage> example_extended_material:
50 array<ExampleBindlessExtendedMaterial>;
52#else // BINDLESS
54// In non-bindless mode, we simply use a uniform for the plain old data.
55@group(2) @binding(50) var<uniform> example_extended_material: ExampleBindlessExtendedMaterial;
56@group(2) @binding(51) var modulate_texture: texture_2d<f32>;
57@group(2) @binding(52) var modulate_sampler: sampler;
59#endif // BINDLESS
61@fragment
62fn fragment(
63 in: VertexOutput,
64 @builtin(front_facing) is_front: bool,
65) -> FragmentOutput {
66#ifdef BINDLESS
67 // Fetch the material slot. We'll use this in turn to fetch the bindless
68 // indices from `example_extended_material_indices`.
69 let slot = mesh[in.instance_index].material_and_lightmap_bind_group_slot & 0xffffu;
70#endif // BINDLESS
72 // Generate a `PbrInput` struct from the `StandardMaterial` bindings.
73 var pbr_input = pbr_input_from_standard_material(in, is_front);
75 // Calculate the UV for the texture we're about to sample.
76#ifdef BINDLESS
77 let uv_transform = material_array[material_indices[slot].material].uv_transform;
78#else // BINDLESS
79 let uv_transform = material.uv_transform;
80#endif // BINDLESS
81 let uv = (uv_transform * vec3(in.uv, 1.0)).xy;
83 // Multiply the base color by the `modulate_texture` and `modulate_color`.
84#ifdef BINDLESS
85 // Notice how we fetch the texture, sampler, and plain extended material
86 // data from the appropriate arrays.
87 pbr_input.material.base_color *= textureSample(
88 bindless_textures_2d[example_extended_material_indices[slot].modulate_texture],
89 bindless_samplers_filtering[
90 example_extended_material_indices[slot].modulate_texture_sampler
91 ],
92 uv
93 ) * example_extended_material[example_extended_material_indices[slot].material].modulate_color;
94#else // BINDLESS
95 pbr_input.material.base_color *= textureSample(modulate_texture, modulate_sampler, uv) *
96 example_extended_material.modulate_color;
97#endif // BINDLESS
99 var out: FragmentOutput;
100 // Apply lighting.
101 out.color = apply_pbr_lighting(pbr_input);
102 // Apply in-shader post processing (fog, alpha-premultiply, and also
103 // tonemapping, debanding if the camera is non-HDR). Note this does not
104 // include fullscreen postprocessing effects like bloom.
105 out.color = main_pass_post_lighting_processing(pbr_input, out.color);
106 return out;
Contents