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