Skip to content

Commit

Permalink
bevy_pbr2: Reorder bind groups to be view, material, mesh order
Browse files Browse the repository at this point in the history
  • Loading branch information
superdump committed Aug 25, 2021
1 parent 38d6e63 commit 24d9d0e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
1 change: 0 additions & 1 deletion pipelined/bevy_pbr2/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use bevy_render2::{
use bevy_transform::components::GlobalTransform;
use crevice::std140::AsStd140;
use std::num::NonZeroU32;
use wgpu::ShaderModel;

pub struct ExtractedAmbientLight {
color: Color,
Expand Down
46 changes: 22 additions & 24 deletions pipelined/bevy_pbr2/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use wgpu::{

pub struct PbrShaders {
pipeline: RenderPipeline,
shader_module: ShaderModule,
view_layout: BindGroupLayout,
material_layout: BindGroupLayout,
mesh_layout: BindGroupLayout,
Expand Down Expand Up @@ -117,20 +116,6 @@ impl FromWorld for PbrShaders {
label: None,
});

let mesh_layout = render_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
entries: &[BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStage::VERTEX | ShaderStage::FRAGMENT,
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: true,
min_binding_size: BufferSize::new(80),
},
count: None,
}],
label: None,
});

let material_layout = render_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
entries: &[
BindGroupLayoutEntry {
Expand Down Expand Up @@ -233,10 +218,24 @@ impl FromWorld for PbrShaders {
label: None,
});

let mesh_layout = render_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
entries: &[BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStage::VERTEX | ShaderStage::FRAGMENT,
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: true,
min_binding_size: BufferSize::new(80),
},
count: None,
}],
label: None,
});

let pipeline_layout = render_device.create_pipeline_layout(&PipelineLayoutDescriptor {
label: None,
push_constant_ranges: &[],
bind_group_layouts: &[&view_layout, &mesh_layout, &material_layout],
bind_group_layouts: &[&view_layout, &material_layout, &mesh_layout],
});

let pipeline = render_device.create_render_pipeline(&RenderPipelineDescriptor {
Expand Down Expand Up @@ -360,7 +359,6 @@ impl FromWorld for PbrShaders {
};
PbrShaders {
pipeline,
shader_module,
view_layout,
material_layout,
mesh_layout,
Expand Down Expand Up @@ -818,21 +816,21 @@ impl Draw for DrawPbr {
&mesh_view_bind_groups.view,
&[view_uniforms.offset, view_lights.gpu_light_binding_index],
);
let mesh_draw_info = &mesh_meta.mesh_draw_info[draw_key];
pass.set_bind_group(
1,
// &mesh_meta.material_bind_groups[sort_key & ((1 << 10) - 1)],
&mesh_meta.material_bind_groups[mesh_draw_info.material_bind_group_key],
&[],
);
pass.set_bind_group(
2,
mesh_meta
.mesh_transform_bind_group
.get_value(mesh_meta.mesh_transform_bind_group_key.unwrap())
.unwrap(),
&[extracted_mesh.transform_binding_offset],
);
let mesh_draw_info = &mesh_meta.mesh_draw_info[draw_key];
pass.set_bind_group(
2,
// &mesh_meta.material_bind_groups[sort_key & ((1 << 10) - 1)],
&mesh_meta.material_bind_groups[mesh_draw_info.material_bind_group_key],
&[],
);

let gpu_mesh = meshes.into_inner().get(&extracted_mesh.mesh).unwrap();
pass.set_vertex_buffer(0, gpu_mesh.vertex_buffer.slice(..));
Expand Down
20 changes: 10 additions & 10 deletions pipelined/bevy_pbr2/src/render/pbr.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let MESH_FLAGS_SHADOW_RECEIVER_BIT: u32 = 1u;

[[group(0), binding(0)]]
var view: View;
[[group(1), binding(0)]]
[[group(2), binding(0)]]
var mesh: Mesh;

struct Vertex {
Expand Down Expand Up @@ -142,23 +142,23 @@ var directional_shadow_textures: texture_depth_2d_array;
[[group(0), binding(5)]]
var directional_shadow_textures_sampler: sampler_comparison;

[[group(2), binding(0)]]
[[group(1), binding(0)]]
var material: StandardMaterial;
[[group(2), binding(1)]]
[[group(1), binding(1)]]
var base_color_texture: texture_2d<f32>;
[[group(2), binding(2)]]
[[group(1), binding(2)]]
var base_color_sampler: sampler;
[[group(2), binding(3)]]
[[group(1), binding(3)]]
var emissive_texture: texture_2d<f32>;
[[group(2), binding(4)]]
[[group(1), binding(4)]]
var emissive_sampler: sampler;
[[group(2), binding(5)]]
[[group(1), binding(5)]]
var metallic_roughness_texture: texture_2d<f32>;
[[group(2), binding(6)]]
[[group(1), binding(6)]]
var metallic_roughness_sampler: sampler;
[[group(2), binding(7)]]
[[group(1), binding(7)]]
var occlusion_texture: texture_2d<f32>;
[[group(2), binding(8)]]
[[group(1), binding(8)]]
var occlusion_sampler: sampler;

let PI: f32 = 3.141592653589793;
Expand Down

0 comments on commit 24d9d0e

Please sign in to comment.