Skip to content

Commit

Permalink
Begin WGSL port (sprites work, pbr lights are broken)
Browse files Browse the repository at this point in the history
  • Loading branch information
cart committed Jun 28, 2021
1 parent cf7e24c commit b6d9277
Show file tree
Hide file tree
Showing 13 changed files with 620 additions and 776 deletions.
3 changes: 0 additions & 3 deletions examples/3d/3d_scene_pipelined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ fn setup(
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(StandardMaterial {
base_color: Color::PINK,
perceptual_roughness: 0.0,
metallic: 1.0,
reflectance: 1.0,
..Default::default()
}),
transform: Transform::from_xyz(0.0, 1.0, 0.0),
Expand Down
9 changes: 5 additions & 4 deletions pipelined/bevy_pbr2/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ impl FromWorld for ShadowShaders {
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: true,
// TODO: verify this is correct
min_binding_size: BufferSize::new(ViewUniform::std140_size_static() as u64),
// TODO: change this to ViewUniform::std140_size_static once crevice fixes this!
// Context: https://github.com/LPGhatguy/crevice/issues/29
min_binding_size: BufferSize::new(80),
},
count: None,
},
Expand Down Expand Up @@ -120,8 +121,8 @@ impl FromWorld for ShadowShaders {
},
],
}],
module: &pbr_shaders.vertex_shader_module,
entry_point: "main",
module: &pbr_shaders.shader_module,
entry_point: "vertex",
},
fragment: None,
depth_stencil: Some(DepthStencilState {
Expand Down
51 changes: 19 additions & 32 deletions pipelined/bevy_pbr2/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ use bevy_render2::{
renderer::{RenderContext, RenderDevice, RenderQueue},
shader::Shader,
texture::{BevyDefault, GpuImage, Image, TextureFormatPixelInfo},
view::{ExtractedView, ViewMeta, ViewUniform, ViewUniformOffset},
view::{ExtractedView, ViewMeta, ViewUniformOffset},
};
use bevy_transform::components::GlobalTransform;
use bevy_utils::slab::{FrameSlabMap, FrameSlabMapKey};
use crevice::std140::AsStd140;
use std::borrow::Cow;
use wgpu::{
Extent3d, ImageCopyTexture, ImageDataLayout, Origin3d, TextureDimension, TextureFormat,
TextureViewDescriptor,
Expand All @@ -29,7 +28,7 @@ use crate::{StandardMaterial, StandardMaterialUniformData};

pub struct PbrShaders {
pipeline: RenderPipeline,
vertex_shader_module: ShaderModule,
shader_module: ShaderModule,
view_layout: BindGroupLayout,
material_layout: BindGroupLayout,
mesh_layout: BindGroupLayout,
Expand All @@ -41,26 +40,9 @@ pub struct PbrShaders {
impl FromWorld for PbrShaders {
fn from_world(world: &mut World) -> Self {
let render_device = world.get_resource::<RenderDevice>().unwrap();
let vertex_shader = Shader::from_glsl(ShaderStage::VERTEX, include_str!("pbr.vert"))
.get_spirv_shader(None)
.unwrap();
let fragment_shader = Shader::from_glsl(ShaderStage::FRAGMENT, include_str!("pbr.frag"))
.get_spirv_shader(None)
.unwrap();

let vertex_spirv = vertex_shader.get_spirv(None).unwrap();
let fragment_spirv = fragment_shader.get_spirv(None).unwrap();

let vertex_shader_module = render_device.create_shader_module(&ShaderModuleDescriptor {
flags: ShaderFlags::default(),
label: None,
source: ShaderSource::SpirV(Cow::Borrowed(&vertex_spirv)),
});
let fragment_shader_module = render_device.create_shader_module(&ShaderModuleDescriptor {
flags: ShaderFlags::default(),
label: None,
source: ShaderSource::SpirV(Cow::Borrowed(&fragment_spirv)),
});
let shader = Shader::from_wgsl(include_str!("pbr.wgsl"));
let shader_module = render_device.create_shader_module(&shader);
println!("{}", GpuLights::std140_size_static());

// TODO: move this into ViewMeta?
let view_layout = render_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
Expand All @@ -72,8 +54,9 @@ impl FromWorld for PbrShaders {
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: true,
// TODO: verify this is correct
min_binding_size: BufferSize::new(ViewUniform::std140_size_static() as u64),
// TODO: change this to ViewUniform::std140_size_static once crevice fixes this!
// Context: https://github.com/LPGhatguy/crevice/issues/29
min_binding_size: BufferSize::new(80),
},
count: None,
},
Expand All @@ -84,7 +67,9 @@ impl FromWorld for PbrShaders {
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: true,
min_binding_size: BufferSize::new(GpuLights::std140_size_static() as u64),
// TODO: change this to ViewUniform::std140_size_static once crevice fixes this!
// Context: https://github.com/LPGhatguy/crevice/issues/29
min_binding_size: BufferSize::new(1264),
},
count: None,
},
Expand Down Expand Up @@ -262,12 +247,12 @@ impl FromWorld for PbrShaders {
},
],
}],
module: &&vertex_shader_module,
entry_point: "main",
module: &shader_module,
entry_point: "vertex",
},
fragment: Some(FragmentState {
module: &&fragment_shader_module,
entry_point: "main",
module: &shader_module,
entry_point: "fragment",
targets: &[ColorTargetState {
format: TextureFormat::bevy_default(),
blend: Some(BlendState {
Expand Down Expand Up @@ -359,7 +344,7 @@ impl FromWorld for PbrShaders {
view_layout,
material_layout,
mesh_layout,
vertex_shader_module,
shader_module,
dummy_white_gpu_image,
}
}
Expand Down Expand Up @@ -473,7 +458,9 @@ fn image_handle_to_view_sampler<'a>(
&pbr_shaders.dummy_white_gpu_image.sampler,
),
|image_handle| {
let gpu_image = gpu_images.get(image_handle).expect("only materials with valid textures should be drawn");
let gpu_image = gpu_images
.get(image_handle)
.expect("only materials with valid textures should be drawn");
(&gpu_image.texture_view, &gpu_image.sampler)
},
)
Expand Down
Loading

0 comments on commit b6d9277

Please sign in to comment.