Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a bindless mode to AsBindGroup. #16368

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3811,6 +3811,17 @@ description = "Shows how to use animation clips to animate UI properties"
category = "Animation"
wasm = true

[[example]]
name = "shader_material_bindless"
path = "examples/shader/shader_material_bindless.rs"
doc-scrape-examples = true

[package.metadata.example.shader_material_bindless]
name = "Material - Bindless"
description = "Demonstrates how to make materials that use bindless textures"
category = "Shaders"
wasm = true

[profile.wasm-release]
inherits = "release"
opt-level = "z"
Expand Down
38 changes: 38 additions & 0 deletions assets/shaders/bindless_material.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#import bevy_pbr::forward_io::VertexOutput
#import bevy_pbr::mesh_bindings::mesh

struct Color {
base_color: vec4<f32>,
}

#ifdef BINDLESS
@group(2) @binding(0) var<storage> material_color: binding_array<Color, 4>;
@group(2) @binding(1) var material_color_texture: binding_array<texture_2d<f32>, 4>;
@group(2) @binding(2) var material_color_sampler: binding_array<sampler, 4>;
#else // BINDLESS
@group(2) @binding(0) var<uniform> material_color: Color;
@group(2) @binding(1) var material_color_texture: texture_2d<f32>;
@group(2) @binding(2) var material_color_sampler: sampler;
#endif // BINDLESS

@fragment
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
#ifdef BINDLESS
let slot = mesh[in.instance_index].material_bind_group_slot;
let base_color = material_color[slot].base_color;
#else // BINDLESS
let base_color = material_color.base_color;
#endif // BINDLESS

return base_color * textureSampleLevel(
#ifdef BINDLESS
material_color_texture[slot],
material_color_sampler[slot],
#else // BINDLESS
material_color_texture,
material_color_sampler,
#endif // BINDLESS
in.uv,
0.0
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ impl RenderAsset for GpuAutoExposureCompensationCurve {

fn prepare_asset(
source: Self::SourceAsset,
_: AssetId<Self::SourceAsset>,
(render_device, render_queue): &mut SystemParamItem<Self::Param>,
) -> Result<Self, bevy_render::render_asset::PrepareAssetError<Self::SourceAsset>> {
let texture = render_device.create_texture_with_data(
Expand Down
12 changes: 6 additions & 6 deletions crates/bevy_core_pipeline/src/core_3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ use bevy_render::{
ViewSortedRenderPhases,
},
render_resource::{
BindGroupId, CachedRenderPipelineId, Extent3d, FilterMode, Sampler, SamplerDescriptor,
CachedRenderPipelineId, Extent3d, FilterMode, Sampler, SamplerDescriptor,
Texture, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, TextureView,
},
renderer::RenderDevice,
Expand Down Expand Up @@ -233,17 +233,17 @@ pub struct Opaque3dBinKey {
/// The function used to draw.
pub draw_function: DrawFunctionId,

/// The ID of a bind group specific to the material.
///
/// In the case of PBR, this is the `MaterialBindGroupIndex`.
pub material_bind_group_index: Option<u32>,

/// The asset that this phase item is associated with.
///
/// Normally, this is the ID of the mesh, but for non-mesh items it might be
/// the ID of another type of asset.
pub asset_id: UntypedAssetId,

/// The ID of a bind group specific to the material.
///
/// In the case of PBR, this is the `MaterialBindGroupId`.
pub material_bind_group_id: Option<BindGroupId>,

/// The lightmap, if present.
pub lightmap_image: Option<AssetId<Image>>,
}
Expand Down
14 changes: 7 additions & 7 deletions crates/bevy_core_pipeline/src/prepass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ use bevy_render::{
PhaseItemExtraIndex,
},
render_resource::{
BindGroupId, CachedRenderPipelineId, ColorTargetState, ColorWrites, DynamicUniformBuffer,
Extent3d, ShaderType, TextureFormat, TextureView,
CachedRenderPipelineId, ColorTargetState, ColorWrites, DynamicUniformBuffer, Extent3d,
ShaderType, TextureFormat, TextureView,
},
texture::ColorAttachment,
};
Expand Down Expand Up @@ -158,13 +158,13 @@ pub struct OpaqueNoLightmap3dBinKey {
/// The function used to draw the mesh.
pub draw_function: DrawFunctionId,

/// The ID of the asset.
pub asset_id: UntypedAssetId,

/// The ID of a bind group specific to the material.
///
/// In the case of PBR, this is the `MaterialBindGroupId`.
pub material_bind_group_id: Option<BindGroupId>,
/// In the case of PBR, this is the `MaterialBindGroupIndex`.
pub material_bind_group_index: Option<u32>,

/// The ID of the asset.
pub asset_id: UntypedAssetId,
}

impl PhaseItem for Opaque3dPrepass {
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_gizmos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub mod prelude {
}

use bevy_app::{App, FixedFirst, FixedLast, Last, Plugin, RunFixedMainLoop};
use bevy_asset::{Asset, AssetApp, Assets, Handle};
use bevy_asset::{Asset, AssetApp, AssetId, Assets, Handle};
use bevy_color::LinearRgba;
use bevy_ecs::{
schedule::{IntoSystemConfigs, SystemSet},
Expand Down Expand Up @@ -520,6 +520,7 @@ impl RenderAsset for GpuLineGizmo {

fn prepare_asset(
gizmo: Self::SourceAsset,
_: AssetId<Self::SourceAsset>,
render_device: &mut SystemParamItem<Self::Param>,
) -> Result<Self, PrepareAssetError<Self::SourceAsset>> {
let position_buffer_data = cast_slice(&gizmo.positions);
Expand Down
7 changes: 6 additions & 1 deletion crates/bevy_pbr/src/extended_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct MaterialExtensionPipeline {
pub material_layout: BindGroupLayout,
pub vertex_shader: Option<Handle<Shader>>,
pub fragment_shader: Option<Handle<Shader>>,
pub bindless: bool,
}

pub struct MaterialExtensionKey<E: MaterialExtension> {
Expand Down Expand Up @@ -163,7 +164,7 @@ impl<B: Material, E: MaterialExtension> AsBindGroup for ExtendedMaterial<B, E> {
let extended_bindgroup =
E::unprepared_bind_group(&self.extension, layout, render_device, extended_param)?;

bindings.extend(extended_bindgroup.bindings);
bindings.extend(extended_bindgroup.bindings.0);

Ok(UnpreparedBindGroup {
bindings,
Expand Down Expand Up @@ -279,13 +280,15 @@ impl<B: Material, E: MaterialExtension> Material for ExtendedMaterial<B, E> {
material_layout,
vertex_shader,
fragment_shader,
bindless,
..
} = pipeline.clone();
let base_pipeline = MaterialPipeline::<B> {
mesh_pipeline,
material_layout,
vertex_shader,
fragment_shader,
bindless,
marker: Default::default(),
};
let base_key = MaterialPipelineKey::<B> {
Expand All @@ -300,6 +303,7 @@ impl<B: Material, E: MaterialExtension> Material for ExtendedMaterial<B, E> {
material_layout,
vertex_shader,
fragment_shader,
bindless,
..
} = pipeline.clone();

Expand All @@ -309,6 +313,7 @@ impl<B: Material, E: MaterialExtension> Material for ExtendedMaterial<B, E> {
material_layout,
vertex_shader,
fragment_shader,
bindless,
},
descriptor,
layout,
Expand Down
6 changes: 5 additions & 1 deletion crates/bevy_pbr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mod light;
mod light_probe;
mod lightmap;
mod material;
mod material_bind_groups;
mod mesh_material;
mod parallax;
mod pbr_material;
Expand All @@ -43,6 +44,8 @@ mod ssao;
mod ssr;
mod volumetric_fog;

use crate::material_bind_groups::FallbackBindlessResources;

use bevy_color::{Color, LinearRgba};
use core::marker::PhantomData;

Expand Down Expand Up @@ -474,7 +477,8 @@ impl Plugin for PbrPlugin {
// Extract the required data from the main world
render_app
.init_resource::<ShadowSamplers>()
.init_resource::<GlobalClusterableObjectMeta>();
.init_resource::<GlobalClusterableObjectMeta>()
.init_resource::<FallbackBindlessResources>();
}
}

Expand Down
Loading
Loading