From 64b897016c09f01afb7fcf29e504b9fb7438c5ba Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Tue, 10 Mar 2020 21:57:57 -0700 Subject: [PATCH] swap out remaining wgpu references --- src/render/pass/passes/forward.rs | 8 +- src/render/pipeline/mod.rs | 1 + src/render/pipeline/pipeline.rs | 66 ++-- src/render/pipeline/pipelines/forward/mod.rs | 48 +-- .../pipeline/pipelines/forward_flat/mod.rs | 36 +- src/render/pipeline/pipelines/ui/mod.rs | 44 +-- src/render/pipeline/state_descriptors.rs | 174 ++++++++++ src/render/render_resource/resource_info.rs | 8 +- .../light_resource_provider.rs | 6 +- .../uniform_resource_provider.rs | 10 +- .../renderers/wgpu_renderer/wgpu_renderer.rs | 24 +- .../renderers/wgpu_renderer/wgpu_resources.rs | 24 +- .../wgpu_renderer/wgpu_type_converter.rs | 318 +++++++++++++++++- src/render/texture/sampler_descriptor.rs | 71 ++-- src/render/texture/texture_descriptor.rs | 30 +- src/render/texture/texture_dimension.rs | 110 ++++-- 16 files changed, 790 insertions(+), 188 deletions(-) create mode 100644 src/render/pipeline/state_descriptors.rs diff --git a/src/render/pass/passes/forward.rs b/src/render/pass/passes/forward.rs index 00922d82ed..5b4eec75c4 100644 --- a/src/render/pass/passes/forward.rs +++ b/src/render/pass/passes/forward.rs @@ -5,7 +5,7 @@ use crate::render::{ }, render_graph::RenderGraphBuilder, render_resource::{resource_name, resource_providers::FrameTextureResourceProvider}, - texture::{TextureDescriptor, TextureDimension}, + texture::{Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsage}, Color, }; @@ -18,7 +18,7 @@ impl ForwardPassBuilder for RenderGraphBuilder { self.add_resource_provider(FrameTextureResourceProvider::new( resource_name::texture::DEPTH, TextureDescriptor { - size: wgpu::Extent3d { + size: Extent3d { depth: 1, width: 1, height: 1, @@ -27,8 +27,8 @@ impl ForwardPassBuilder for RenderGraphBuilder { mip_level_count: 1, sample_count: 1, dimension: TextureDimension::D2, - format: wgpu::TextureFormat::Depth32Float, - usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT, + format: TextureFormat::Depth32Float, + usage: TextureUsage::OUTPUT_ATTACHMENT, }, )) .add_pass( diff --git a/src/render/pipeline/mod.rs b/src/render/pipeline/mod.rs index 0848591fc4..f18ada46ff 100644 --- a/src/render/pipeline/mod.rs +++ b/src/render/pipeline/mod.rs @@ -4,6 +4,7 @@ mod pipeline; mod pipeline_layout; pub mod pipelines; mod vertex_buffer_descriptor; +pub mod state_descriptors; pub use bind_group::*; pub use binding::*; diff --git a/src/render/pipeline/pipeline.rs b/src/render/pipeline/pipeline.rs index e84809a895..482a47003b 100644 --- a/src/render/pipeline/pipeline.rs +++ b/src/render/pipeline/pipeline.rs @@ -1,9 +1,17 @@ -use super::{BindGroup, PipelineLayout, VertexBufferDescriptor}; +use super::{ + state_descriptors::{ + BlendDescriptor, ColorStateDescriptor, ColorWrite, CompareFunction, CullMode, + DepthStencilStateDescriptor, FrontFace, PrimitiveTopology, RasterizationStateDescriptor, + StencilStateFaceDescriptor, IndexFormat, + }, + BindGroup, PipelineLayout, VertexBufferDescriptor, +}; use crate::{ asset::{AssetStorage, Handle}, render::{ render_resource::resource_name, shader::{Shader, ShaderStages}, + texture::TextureFormat, Vertex, }, }; @@ -20,19 +28,19 @@ pub struct PipelineDescriptor { pub draw_targets: Vec, pub layout: PipelineLayoutType, pub shader_stages: ShaderStages, - pub rasterization_state: Option, + pub rasterization_state: Option, /// The primitive topology used to interpret vertices. - pub primitive_topology: wgpu::PrimitiveTopology, + pub primitive_topology: PrimitiveTopology, /// The effect of draw calls on the color aspect of the output target. - pub color_states: Vec, + pub color_states: Vec, /// The effect of draw calls on the depth and stencil aspects of the output target, if any. - pub depth_stencil_state: Option, + pub depth_stencil_state: Option, /// The format of any index buffers used with this pipeline. - pub index_format: wgpu::IndexFormat, + pub index_format: IndexFormat, /// The format of any vertex buffers used with this pipeline. pub vertex_buffer_descriptors: Vec, @@ -61,15 +69,15 @@ impl PipelineDescriptor { draw_targets: Vec::new(), shader_stages: ShaderStages::new(vertex_shader), vertex_buffer_descriptors: Vec::new(), - rasterization_state: Some(wgpu::RasterizationStateDescriptor { - front_face: wgpu::FrontFace::Ccw, - cull_mode: wgpu::CullMode::Back, + rasterization_state: Some(RasterizationStateDescriptor { + front_face: FrontFace::Ccw, + cull_mode: CullMode::Back, depth_bias: 0, depth_bias_slope_scale: 0.0, depth_bias_clamp: 0.0, }), - primitive_topology: wgpu::PrimitiveTopology::TriangleList, - index_format: wgpu::IndexFormat::Uint16, + primitive_topology: PrimitiveTopology::TriangleList, + index_format: IndexFormat::Uint16, sample_count: 1, sample_mask: !0, alpha_to_coverage_enabled: false, @@ -107,7 +115,11 @@ pub struct PipelineBuilder<'a> { } impl<'a> PipelineBuilder<'a> { - pub fn new(name: &str, shader_storage: &'a mut AssetStorage, vertex_shader: Shader) -> Self { + pub fn new( + name: &str, + shader_storage: &'a mut AssetStorage, + vertex_shader: Shader, + ) -> Self { let vertex_shader_handle = shader_storage.add(vertex_shader); PipelineBuilder { pipeline: PipelineDescriptor::new(Some(name), vertex_shader_handle), @@ -125,14 +137,14 @@ impl<'a> PipelineBuilder<'a> { self } - pub fn add_color_state(mut self, color_state_descriptor: wgpu::ColorStateDescriptor) -> Self { + pub fn add_color_state(mut self, color_state_descriptor: ColorStateDescriptor) -> Self { self.pipeline.color_states.push(color_state_descriptor); self } pub fn with_depth_stencil_state( mut self, - depth_stencil_state: wgpu::DepthStencilStateDescriptor, + depth_stencil_state: DepthStencilStateDescriptor, ) -> Self { if let Some(_) = self.pipeline.depth_stencil_state { panic!("Depth stencil state has already been set"); @@ -163,7 +175,7 @@ impl<'a> PipelineBuilder<'a> { self } - pub fn with_index_format(mut self, index_format: wgpu::IndexFormat) -> Self { + pub fn with_index_format(mut self, index_format: IndexFormat) -> Self { self.pipeline.index_format = index_format; self } @@ -175,13 +187,13 @@ impl<'a> PipelineBuilder<'a> { pub fn with_rasterization_state( mut self, - rasterization_state: wgpu::RasterizationStateDescriptor, + rasterization_state: RasterizationStateDescriptor, ) -> Self { self.pipeline.rasterization_state = Some(rasterization_state); self } - pub fn with_primitive_topology(mut self, primitive_topology: wgpu::PrimitiveTopology) -> Self { + pub fn with_primitive_topology(mut self, primitive_topology: PrimitiveTopology) -> Self { self.pipeline.primitive_topology = primitive_topology; self } @@ -202,20 +214,20 @@ impl<'a> PipelineBuilder<'a> { } pub fn with_standard_config(self) -> Self { - self.with_depth_stencil_state(wgpu::DepthStencilStateDescriptor { - format: wgpu::TextureFormat::Depth32Float, + self.with_depth_stencil_state(DepthStencilStateDescriptor { + format: TextureFormat::Depth32Float, depth_write_enabled: true, - depth_compare: wgpu::CompareFunction::Less, - stencil_front: wgpu::StencilStateFaceDescriptor::IGNORE, - stencil_back: wgpu::StencilStateFaceDescriptor::IGNORE, + depth_compare: CompareFunction::Less, + stencil_front: StencilStateFaceDescriptor::IGNORE, + stencil_back: StencilStateFaceDescriptor::IGNORE, stencil_read_mask: 0, stencil_write_mask: 0, }) - .add_color_state(wgpu::ColorStateDescriptor { - format: wgpu::TextureFormat::Bgra8UnormSrgb, - color_blend: wgpu::BlendDescriptor::REPLACE, - alpha_blend: wgpu::BlendDescriptor::REPLACE, - write_mask: wgpu::ColorWrite::ALL, + .add_color_state(ColorStateDescriptor { + format: TextureFormat::Bgra8UnormSrgb, + color_blend: BlendDescriptor::REPLACE, + alpha_blend: BlendDescriptor::REPLACE, + write_mask: ColorWrite::ALL, }) .add_vertex_buffer_descriptor(Vertex::get_vertex_buffer_descriptor()) .add_draw_target(resource_name::draw_target::ASSIGNED_MESHES) diff --git a/src/render/pipeline/pipelines/forward/mod.rs b/src/render/pipeline/pipelines/forward/mod.rs index bc31abef7b..005f96ac6f 100644 --- a/src/render/pipeline/pipelines/forward/mod.rs +++ b/src/render/pipeline/pipelines/forward/mod.rs @@ -1,10 +1,18 @@ use crate::{ asset::AssetStorage, render::{ - pipeline::PipelineDescriptor, + pipeline::{ + state_descriptors::{ + BlendDescriptor, BlendFactor, BlendOperation, ColorStateDescriptor, ColorWrite, + CompareFunction, CullMode, DepthStencilStateDescriptor, FrontFace, + RasterizationStateDescriptor, StencilStateFaceDescriptor, + }, + PipelineDescriptor, + }, render_graph::RenderGraphBuilder, render_resource::resource_name, shader::{Shader, ShaderStage}, + texture::TextureFormat, Vertex, }, }; @@ -33,35 +41,35 @@ impl ForwardPipelineBuilder for RenderGraphBuilder { ShaderStage::Fragment, include_str!("forward.frag"), )) - .with_rasterization_state(wgpu::RasterizationStateDescriptor { - front_face: wgpu::FrontFace::Ccw, - cull_mode: wgpu::CullMode::Back, + .with_rasterization_state(RasterizationStateDescriptor { + front_face: FrontFace::Ccw, + cull_mode: CullMode::Back, depth_bias: 0, depth_bias_slope_scale: 0.0, depth_bias_clamp: 0.0, }) - .with_depth_stencil_state(wgpu::DepthStencilStateDescriptor { - format: wgpu::TextureFormat::Depth32Float, + .with_depth_stencil_state(DepthStencilStateDescriptor { + format: TextureFormat::Depth32Float, depth_write_enabled: true, - depth_compare: wgpu::CompareFunction::Less, - stencil_front: wgpu::StencilStateFaceDescriptor::IGNORE, - stencil_back: wgpu::StencilStateFaceDescriptor::IGNORE, + depth_compare: CompareFunction::Less, + stencil_front: StencilStateFaceDescriptor::IGNORE, + stencil_back: StencilStateFaceDescriptor::IGNORE, stencil_read_mask: 0, stencil_write_mask: 0, }) - .add_color_state(wgpu::ColorStateDescriptor { - format: wgpu::TextureFormat::Bgra8UnormSrgb, - color_blend: wgpu::BlendDescriptor { - src_factor: wgpu::BlendFactor::SrcAlpha, - dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha, - operation: wgpu::BlendOperation::Add, + .add_color_state(ColorStateDescriptor { + format: TextureFormat::Bgra8UnormSrgb, + color_blend: BlendDescriptor { + src_factor: BlendFactor::SrcAlpha, + dst_factor: BlendFactor::OneMinusSrcAlpha, + operation: BlendOperation::Add, }, - alpha_blend: wgpu::BlendDescriptor { - src_factor: wgpu::BlendFactor::One, - dst_factor: wgpu::BlendFactor::One, - operation: wgpu::BlendOperation::Add, + alpha_blend: BlendDescriptor { + src_factor: BlendFactor::One, + dst_factor: BlendFactor::One, + operation: BlendOperation::Add, }, - write_mask: wgpu::ColorWrite::ALL, + write_mask: ColorWrite::ALL, }) .add_vertex_buffer_descriptor(Vertex::get_vertex_buffer_descriptor()) .add_draw_target(resource_name::draw_target::ASSIGNED_MESHES) diff --git a/src/render/pipeline/pipelines/forward_flat/mod.rs b/src/render/pipeline/pipelines/forward_flat/mod.rs index ccfb84c808..25afd3a7f1 100644 --- a/src/render/pipeline/pipelines/forward_flat/mod.rs +++ b/src/render/pipeline/pipelines/forward_flat/mod.rs @@ -1,10 +1,18 @@ use crate::{ asset::AssetStorage, render::{ - pipeline::PipelineDescriptor, + pipeline::{ + state_descriptors::{ + BlendDescriptor, ColorStateDescriptor, ColorWrite, CompareFunction, CullMode, + DepthStencilStateDescriptor, FrontFace, RasterizationStateDescriptor, + StencilStateFaceDescriptor, + }, + PipelineDescriptor, + }, render_graph::RenderGraphBuilder, render_resource::resource_name, shader::{Shader, ShaderStage}, + texture::TextureFormat, Vertex, }, }; @@ -34,27 +42,27 @@ impl ForwardFlatPipelineBuilder for RenderGraphBuilder { ShaderStage::Fragment, include_str!("forward_flat.frag"), )) - .with_rasterization_state(wgpu::RasterizationStateDescriptor { - front_face: wgpu::FrontFace::Ccw, - cull_mode: wgpu::CullMode::Back, + .with_rasterization_state(RasterizationStateDescriptor { + front_face: FrontFace::Ccw, + cull_mode: CullMode::Back, depth_bias: 0, depth_bias_slope_scale: 0.0, depth_bias_clamp: 0.0, }) - .with_depth_stencil_state(wgpu::DepthStencilStateDescriptor { - format: wgpu::TextureFormat::Depth32Float, + .with_depth_stencil_state(DepthStencilStateDescriptor { + format: TextureFormat::Depth32Float, depth_write_enabled: true, - depth_compare: wgpu::CompareFunction::Less, - stencil_front: wgpu::StencilStateFaceDescriptor::IGNORE, - stencil_back: wgpu::StencilStateFaceDescriptor::IGNORE, + depth_compare: CompareFunction::Less, + stencil_front: StencilStateFaceDescriptor::IGNORE, + stencil_back: StencilStateFaceDescriptor::IGNORE, stencil_read_mask: 0, stencil_write_mask: 0, }) - .add_color_state(wgpu::ColorStateDescriptor { - format: wgpu::TextureFormat::Bgra8UnormSrgb, - color_blend: wgpu::BlendDescriptor::REPLACE, - alpha_blend: wgpu::BlendDescriptor::REPLACE, - write_mask: wgpu::ColorWrite::ALL, + .add_color_state(ColorStateDescriptor { + format: TextureFormat::Bgra8UnormSrgb, + color_blend: BlendDescriptor::REPLACE, + alpha_blend: BlendDescriptor::REPLACE, + write_mask: ColorWrite::ALL, }) .add_vertex_buffer_descriptor(Vertex::get_vertex_buffer_descriptor()) .add_draw_target(resource_name::draw_target::MESHES) diff --git a/src/render/pipeline/pipelines/ui/mod.rs b/src/render/pipeline/pipelines/ui/mod.rs index 28bf80a9a7..dad9597994 100644 --- a/src/render/pipeline/pipelines/ui/mod.rs +++ b/src/render/pipeline/pipelines/ui/mod.rs @@ -2,12 +2,18 @@ use crate::{ asset::AssetStorage, render::{ pipeline::{ + state_descriptors::{ + BlendDescriptor, BlendFactor, BlendOperation, ColorStateDescriptor, ColorWrite, + CompareFunction, CullMode, DepthStencilStateDescriptor, FrontFace, + RasterizationStateDescriptor, StencilStateFaceDescriptor, + }, InputStepMode, PipelineDescriptor, VertexAttributeDescriptor, VertexBufferDescriptor, VertexFormat, }, render_graph::RenderGraphBuilder, render_resource::{resource_name, resource_providers::RectData}, shader::{Shader, ShaderStage}, + texture::TextureFormat, Vertex, }, }; @@ -36,35 +42,35 @@ impl UiPipelineBuilder for RenderGraphBuilder { ShaderStage::Fragment, include_str!("ui.frag"), )) - .with_rasterization_state(wgpu::RasterizationStateDescriptor { - front_face: wgpu::FrontFace::Ccw, - cull_mode: wgpu::CullMode::None, + .with_rasterization_state(RasterizationStateDescriptor { + front_face: FrontFace::Ccw, + cull_mode: CullMode::None, depth_bias: 0, depth_bias_slope_scale: 0.0, depth_bias_clamp: 0.0, }) - .with_depth_stencil_state(wgpu::DepthStencilStateDescriptor { - format: wgpu::TextureFormat::Depth32Float, + .with_depth_stencil_state(DepthStencilStateDescriptor { + format: TextureFormat::Depth32Float, depth_write_enabled: false, - depth_compare: wgpu::CompareFunction::Always, - stencil_front: wgpu::StencilStateFaceDescriptor::IGNORE, - stencil_back: wgpu::StencilStateFaceDescriptor::IGNORE, + depth_compare: CompareFunction::Always, + stencil_front: StencilStateFaceDescriptor::IGNORE, + stencil_back: StencilStateFaceDescriptor::IGNORE, stencil_read_mask: 0, stencil_write_mask: 0, }) - .add_color_state(wgpu::ColorStateDescriptor { - format: wgpu::TextureFormat::Bgra8UnormSrgb, - color_blend: wgpu::BlendDescriptor { - src_factor: wgpu::BlendFactor::SrcAlpha, - dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha, - operation: wgpu::BlendOperation::Add, + .add_color_state(ColorStateDescriptor { + format: TextureFormat::Bgra8UnormSrgb, + color_blend: BlendDescriptor { + src_factor: BlendFactor::SrcAlpha, + dst_factor: BlendFactor::OneMinusSrcAlpha, + operation: BlendOperation::Add, }, - alpha_blend: wgpu::BlendDescriptor { - src_factor: wgpu::BlendFactor::One, - dst_factor: wgpu::BlendFactor::One, - operation: wgpu::BlendOperation::Add, + alpha_blend: BlendDescriptor { + src_factor: BlendFactor::One, + dst_factor: BlendFactor::One, + operation: BlendOperation::Add, }, - write_mask: wgpu::ColorWrite::ALL, + write_mask: ColorWrite::ALL, }) .add_vertex_buffer_descriptor(Vertex::get_vertex_buffer_descriptor()) .add_vertex_buffer_descriptor(VertexBufferDescriptor { diff --git a/src/render/pipeline/state_descriptors.rs b/src/render/pipeline/state_descriptors.rs new file mode 100644 index 0000000000..22315012ad --- /dev/null +++ b/src/render/pipeline/state_descriptors.rs @@ -0,0 +1,174 @@ +use crate::render::texture::TextureFormat; + +#[derive(Clone, Debug)] +pub struct DepthStencilStateDescriptor { + pub format: TextureFormat, + pub depth_write_enabled: bool, + pub depth_compare: CompareFunction, + pub stencil_front: StencilStateFaceDescriptor, + pub stencil_back: StencilStateFaceDescriptor, + pub stencil_read_mask: u32, + pub stencil_write_mask: u32, +} +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] +pub enum StencilOperation { + Keep = 0, + Zero = 1, + Replace = 2, + Invert = 3, + IncrementClamp = 4, + DecrementClamp = 5, + IncrementWrap = 6, + DecrementWrap = 7, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct StencilStateFaceDescriptor { + pub compare: CompareFunction, + pub fail_op: StencilOperation, + pub depth_fail_op: StencilOperation, + pub pass_op: StencilOperation, +} + +impl StencilStateFaceDescriptor { + pub const IGNORE: Self = StencilStateFaceDescriptor { + compare: CompareFunction::Always, + fail_op: StencilOperation::Keep, + depth_fail_op: StencilOperation::Keep, + pass_op: StencilOperation::Keep, + }; +} + +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] +pub enum CompareFunction { + Never = 0, + Less = 1, + Equal = 2, + LessEqual = 3, + Greater = 4, + NotEqual = 5, + GreaterEqual = 6, + Always = 7, +} + +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] +pub enum PrimitiveTopology { + PointList = 0, + LineList = 1, + LineStrip = 2, + TriangleList = 3, + TriangleStrip = 4, +} + +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] +pub enum FrontFace { + Ccw = 0, + Cw = 1, +} + +impl Default for FrontFace { + fn default() -> Self { + FrontFace::Ccw + } +} + +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] +pub enum CullMode { + None = 0, + Front = 1, + Back = 2, +} + +impl Default for CullMode { + fn default() -> Self { + CullMode::None + } +} + +#[derive(Clone, Debug, Default)] +pub struct RasterizationStateDescriptor { + pub front_face: FrontFace, + pub cull_mode: CullMode, + pub depth_bias: i32, + pub depth_bias_slope_scale: f32, + pub depth_bias_clamp: f32, +} + +#[derive(Clone, Debug)] +pub struct ColorStateDescriptor { + pub format: TextureFormat, + pub alpha_blend: BlendDescriptor, + pub color_blend: BlendDescriptor, + pub write_mask: ColorWrite, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct BlendDescriptor { + pub src_factor: BlendFactor, + pub dst_factor: BlendFactor, + pub operation: BlendOperation, +} + +impl BlendDescriptor { + pub const REPLACE: Self = BlendDescriptor { + src_factor: BlendFactor::One, + dst_factor: BlendFactor::Zero, + operation: BlendOperation::Add, + }; +} + +bitflags::bitflags! { + #[repr(transparent)] + pub struct ColorWrite: u32 { + const RED = 1; + const GREEN = 2; + const BLUE = 4; + const ALPHA = 8; + const COLOR = 7; + const ALL = 15; + } +} + +impl Default for ColorWrite { + fn default() -> Self { + ColorWrite::ALL + } +} + +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] +pub enum BlendFactor { + Zero = 0, + One = 1, + SrcColor = 2, + OneMinusSrcColor = 3, + SrcAlpha = 4, + OneMinusSrcAlpha = 5, + DstColor = 6, + OneMinusDstColor = 7, + DstAlpha = 8, + OneMinusDstAlpha = 9, + SrcAlphaSaturated = 10, + BlendColor = 11, + OneMinusBlendColor = 12, +} + +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] +pub enum BlendOperation { + Add = 0, + Subtract = 1, + ReverseSubtract = 2, + Min = 3, + Max = 4, +} + +impl Default for BlendOperation { + fn default() -> Self { + BlendOperation::Add + } +} + +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] +pub enum IndexFormat { + Uint16 = 0, + Uint32 = 1, +} \ No newline at end of file diff --git a/src/render/render_resource/resource_info.rs b/src/render/render_resource/resource_info.rs index 4704b6f8ba..4bff5a8c72 100644 --- a/src/render/render_resource/resource_info.rs +++ b/src/render/render_resource/resource_info.rs @@ -1,15 +1,15 @@ +use crate::render::render_resource::BufferUsage; + pub enum ResourceInfo { Buffer { size: u64, - buffer_usage: wgpu::BufferUsage, - // pub layout: Option< + buffer_usage: BufferUsage, }, InstanceBuffer { size: usize, count: usize, - buffer_usage: wgpu::BufferUsage, + buffer_usage: BufferUsage, mesh_id: usize, - // pub layout: Option< }, Texture, Sampler, diff --git a/src/render/render_resource/resource_providers/light_resource_provider.rs b/src/render/render_resource/resource_providers/light_resource_provider.rs index 546a76f8dd..e517dee0ac 100644 --- a/src/render/render_resource/resource_providers/light_resource_provider.rs +++ b/src/render/render_resource/resource_providers/light_resource_provider.rs @@ -42,7 +42,7 @@ impl ResourceProvider for LightResourceProvider { ) { let light_uniform_size = (std::mem::size_of::() + self.max_lights * std::mem::size_of::()) - as wgpu::BufferAddress; + as u64; let buffer = renderer.create_buffer( light_uniform_size, @@ -102,7 +102,7 @@ impl ResourceProvider for LightResourceProvider { 0, self.light_buffer.unwrap(), 0, - light_count_size as wgpu::BufferAddress, + light_count_size as u64, ); renderer.copy_buffer_to_buffer( @@ -110,7 +110,7 @@ impl ResourceProvider for LightResourceProvider { 0, self.light_buffer.unwrap(), light_count_size as u64, - total_size as wgpu::BufferAddress, + total_size as u64, ); } } diff --git a/src/render/render_resource/resource_providers/uniform_resource_provider.rs b/src/render/render_resource/resource_providers/uniform_resource_provider.rs index 1eaaa057fb..58ef5741f6 100644 --- a/src/render/render_resource/resource_providers/uniform_resource_provider.rs +++ b/src/render/render_resource/resource_providers/uniform_resource_provider.rs @@ -15,7 +15,7 @@ use std::{ marker::PhantomData, ops::Deref, }; - +pub const BIND_BUFFER_ALIGNMENT: u64 = 256; pub struct UniformResourceProvider where T: AsUniforms + Send + Sync, @@ -150,7 +150,7 @@ where // allocate enough space for twice as many entities as there are currently; let capacity = count * 2; - let size = wgpu::BIND_BUFFER_ALIGNMENT * capacity; + let size = BIND_BUFFER_ALIGNMENT * capacity; let created_resource = renderer.create_buffer(size, BufferUsage::COPY_DST | BufferUsage::UNIFORM); @@ -169,10 +169,10 @@ where let resource = resource.unwrap(); let size = { let info = renderer.get_dynamic_uniform_buffer_info(resource).unwrap(); - wgpu::BIND_BUFFER_ALIGNMENT * info.count + BIND_BUFFER_ALIGNMENT * info.count }; - let alignment = wgpu::BIND_BUFFER_ALIGNMENT as usize; + let alignment = BIND_BUFFER_ALIGNMENT as usize; let mut offset = 0usize; let info = renderer .get_dynamic_uniform_buffer_info_mut(resource) @@ -193,7 +193,7 @@ where size as usize, BufferUsage::COPY_SRC, &mut |mapped| { - let alignment = wgpu::BIND_BUFFER_ALIGNMENT as usize; + let alignment = BIND_BUFFER_ALIGNMENT as usize; let mut offset = 0usize; for (entity, (uniforms, _renderable)) in query.iter_entities(world) { if !entities.contains(&entity) { diff --git a/src/render/renderer/renderers/wgpu_renderer/wgpu_renderer.rs b/src/render/renderer/renderers/wgpu_renderer/wgpu_renderer.rs index fe26f1ed45..499205075a 100644 --- a/src/render/renderer/renderers/wgpu_renderer/wgpu_renderer.rs +++ b/src/render/renderer/renderers/wgpu_renderer/wgpu_renderer.rs @@ -167,6 +167,12 @@ impl WgpuRenderer { .map(|v| v.into()) .collect::>(); + let color_states = pipeline_descriptor + .color_states + .iter() + .map(|c| c.into()) + .collect::>(); + let mut render_pipeline_descriptor = wgpu::RenderPipelineDescriptor { layout: &pipeline_layout, vertex_stage: wgpu::ProgrammableStageDescriptor { @@ -180,11 +186,17 @@ impl WgpuRenderer { }), None => None, }, - rasterization_state: pipeline_descriptor.rasterization_state.clone(), - primitive_topology: pipeline_descriptor.primitive_topology, - color_states: &pipeline_descriptor.color_states, - depth_stencil_state: pipeline_descriptor.depth_stencil_state.clone(), - index_format: pipeline_descriptor.index_format, + rasterization_state: pipeline_descriptor + .rasterization_state + .as_ref() + .map(|r| r.into()), + primitive_topology: pipeline_descriptor.primitive_topology.into(), + color_states: &color_states, + depth_stencil_state: pipeline_descriptor + .depth_stencil_state + .as_ref() + .map(|d| d.into()), + index_format: pipeline_descriptor.index_format.into(), vertex_buffers: &owned_vertex_buffer_descriptors .iter() .map(|v| v.into()) @@ -689,4 +701,4 @@ impl Renderer for WgpuRenderer { } } } -} \ No newline at end of file +} diff --git a/src/render/renderer/renderers/wgpu_renderer/wgpu_resources.rs b/src/render/renderer/renderers/wgpu_renderer/wgpu_resources.rs index dac7ad2b22..cceedacfb4 100644 --- a/src/render/renderer/renderers/wgpu_renderer/wgpu_resources.rs +++ b/src/render/renderer/renderers/wgpu_renderer/wgpu_resources.rs @@ -2,7 +2,7 @@ use crate::{ legion::prelude::*, render::{ pipeline::{BindGroup, BindType}, - render_resource::{RenderResource, RenderResources, ResourceInfo}, + render_resource::{BufferUsage, RenderResource, RenderResources, ResourceInfo}, shader::DynamicUniformBufferInfo, texture::{SamplerDescriptor, TextureDescriptor}, }, @@ -72,7 +72,7 @@ impl WgpuResources { let resource = self.create_buffer( device, size, - wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST, + BufferUsage::UNIFORM | BufferUsage::COPY_DST, ); self.render_resources @@ -216,11 +216,11 @@ impl WgpuResources { &mut self, device: &wgpu::Device, size: u64, - buffer_usage: wgpu::BufferUsage, + buffer_usage: BufferUsage, ) -> RenderResource { let buffer = device.create_buffer(&wgpu::BufferDescriptor { size, - usage: buffer_usage, + usage: buffer_usage.into(), }); let resource = self.render_resources.get_next_resource(); @@ -234,10 +234,10 @@ impl WgpuResources { &mut self, device: &wgpu::Device, data: &[u8], - buffer_usage: wgpu::BufferUsage, + buffer_usage: BufferUsage, ) -> RenderResource { let resource = self.render_resources.get_next_resource(); - let buffer = device.create_buffer_with_data(data, buffer_usage); + let buffer = device.create_buffer_with_data(data, buffer_usage.into()); self.add_resource_info( resource, ResourceInfo::Buffer { @@ -256,11 +256,11 @@ impl WgpuResources { mesh_id: usize, size: usize, count: usize, - buffer_usage: wgpu::BufferUsage, + buffer_usage: BufferUsage, ) -> RenderResource { let buffer = device.create_buffer(&wgpu::BufferDescriptor { size: (size * count) as u64, - usage: buffer_usage, + usage: buffer_usage.into(), }); let resource = self.render_resources.get_next_resource(); @@ -285,9 +285,9 @@ impl WgpuResources { data: &[u8], size: usize, count: usize, - buffer_usage: wgpu::BufferUsage, + buffer_usage: BufferUsage, ) -> RenderResource { - let buffer = device.create_buffer_with_data(data, buffer_usage); + let buffer = device.create_buffer_with_data(data, buffer_usage.into()); let resource = self.render_resources.get_next_resource(); self.add_resource_info( @@ -317,10 +317,10 @@ impl WgpuResources { &mut self, device: &wgpu::Device, size: usize, - buffer_usage: wgpu::BufferUsage, + buffer_usage: BufferUsage, setup_data: &mut dyn FnMut(&mut [u8]), ) -> RenderResource { - let mut mapped = device.create_buffer_mapped(size, buffer_usage); + let mut mapped = device.create_buffer_mapped(size, buffer_usage.into()); setup_data(&mut mapped.data); let buffer = mapped.finish(); diff --git a/src/render/renderer/renderers/wgpu_renderer/wgpu_type_converter.rs b/src/render/renderer/renderers/wgpu_renderer/wgpu_type_converter.rs index 0ffc199080..feb437a803 100644 --- a/src/render/renderer/renderers/wgpu_renderer/wgpu_type_converter.rs +++ b/src/render/renderer/renderers/wgpu_renderer/wgpu_type_converter.rs @@ -3,9 +3,20 @@ use crate::{ render::{ pass::{LoadOp, StoreOp}, pipeline::{ - InputStepMode, VertexAttributeDescriptor, VertexBufferDescriptor, VertexFormat, BindType + state_descriptors::{ + BlendDescriptor, BlendFactor, BlendOperation, ColorStateDescriptor, ColorWrite, + CompareFunction, CullMode, DepthStencilStateDescriptor, FrontFace, IndexFormat, + PrimitiveTopology, RasterizationStateDescriptor, StencilOperation, + StencilStateFaceDescriptor, + }, + BindType, InputStepMode, VertexAttributeDescriptor, VertexBufferDescriptor, + VertexFormat, }, render_resource::BufferUsage, + texture::{ + AddressMode, Extent3d, SamplerDescriptor, TextureDescriptor, TextureDimension, + TextureFormat, TextureUsage, TextureViewDimension, FilterMode, + }, }, }; @@ -156,4 +167,309 @@ impl From<&BindType> for wgpu::BindingType { }, } } +} + +impl From for wgpu::Extent3d { + fn from(val: Extent3d) -> Self { + wgpu::Extent3d { + depth: val.depth, + height: val.height, + width: val.width, + } + } +} + +impl From for wgpu::TextureDescriptor { + fn from(texture_descriptor: TextureDescriptor) -> Self { + wgpu::TextureDescriptor { + size: texture_descriptor.size.into(), + array_layer_count: texture_descriptor.array_layer_count, + mip_level_count: texture_descriptor.mip_level_count, + sample_count: texture_descriptor.sample_count, + dimension: texture_descriptor.dimension.into(), + format: texture_descriptor.format.into(), + usage: texture_descriptor.usage.into(), + } + } +} + +impl From for wgpu::TextureViewDimension { + fn from(dimension: TextureViewDimension) -> Self { + match dimension { + TextureViewDimension::D1 => wgpu::TextureViewDimension::D1, + TextureViewDimension::D2 => wgpu::TextureViewDimension::D2, + TextureViewDimension::D2Array => wgpu::TextureViewDimension::D2Array, + TextureViewDimension::Cube => wgpu::TextureViewDimension::Cube, + TextureViewDimension::CubeArray => wgpu::TextureViewDimension::CubeArray, + TextureViewDimension::D3 => wgpu::TextureViewDimension::D3, + } + } +} + +impl From for wgpu::TextureDimension { + fn from(dimension: TextureDimension) -> Self { + match dimension { + TextureDimension::D1 => wgpu::TextureDimension::D1, + TextureDimension::D2 => wgpu::TextureDimension::D2, + TextureDimension::D3 => wgpu::TextureDimension::D3, + } + } +} + +impl From for wgpu::TextureFormat { + fn from(val: TextureFormat) -> Self { + match val { + TextureFormat::R8Unorm => wgpu::TextureFormat::R8Unorm, + TextureFormat::R8Snorm => wgpu::TextureFormat::R8Snorm, + TextureFormat::R8Uint => wgpu::TextureFormat::R8Uint, + TextureFormat::R8Sint => wgpu::TextureFormat::R8Sint, + TextureFormat::R16Unorm => wgpu::TextureFormat::R16Unorm, + TextureFormat::R16Snorm => wgpu::TextureFormat::R16Snorm, + TextureFormat::R16Uint => wgpu::TextureFormat::R16Uint, + TextureFormat::R16Sint => wgpu::TextureFormat::R16Sint, + TextureFormat::R16Float => wgpu::TextureFormat::R16Float, + TextureFormat::Rg8Unorm => wgpu::TextureFormat::Rg8Unorm, + TextureFormat::Rg8Snorm => wgpu::TextureFormat::Rg8Snorm, + TextureFormat::Rg8Uint => wgpu::TextureFormat::Rg8Uint, + TextureFormat::Rg8Sint => wgpu::TextureFormat::Rg8Sint, + TextureFormat::R32Uint => wgpu::TextureFormat::R32Uint, + TextureFormat::R32Sint => wgpu::TextureFormat::R32Sint, + TextureFormat::R32Float => wgpu::TextureFormat::R32Float, + TextureFormat::Rg16Unorm => wgpu::TextureFormat::Rg16Unorm, + TextureFormat::Rg16Snorm => wgpu::TextureFormat::Rg16Snorm, + TextureFormat::Rg16Uint => wgpu::TextureFormat::Rg16Uint, + TextureFormat::Rg16Sint => wgpu::TextureFormat::Rg16Sint, + TextureFormat::Rg16Float => wgpu::TextureFormat::Rg16Float, + TextureFormat::Rgba8Unorm => wgpu::TextureFormat::Rgba8Unorm, + TextureFormat::Rgba8UnormSrgb => wgpu::TextureFormat::Rgba8UnormSrgb, + TextureFormat::Rgba8Snorm => wgpu::TextureFormat::Rgba8Snorm, + TextureFormat::Rgba8Uint => wgpu::TextureFormat::Rgba8Uint, + TextureFormat::Rgba8Sint => wgpu::TextureFormat::Rgba8Sint, + TextureFormat::Bgra8Unorm => wgpu::TextureFormat::Bgra8Unorm, + TextureFormat::Bgra8UnormSrgb => wgpu::TextureFormat::Bgra8UnormSrgb, + TextureFormat::Rgb10a2Unorm => wgpu::TextureFormat::Rgb10a2Unorm, + TextureFormat::Rg11b10Float => wgpu::TextureFormat::Rg11b10Float, + TextureFormat::Rg32Uint => wgpu::TextureFormat::Rg32Uint, + TextureFormat::Rg32Sint => wgpu::TextureFormat::Rg32Sint, + TextureFormat::Rg32Float => wgpu::TextureFormat::Rg32Float, + TextureFormat::Rgba16Unorm => wgpu::TextureFormat::Rgba16Unorm, + TextureFormat::Rgba16Snorm => wgpu::TextureFormat::Rgba16Snorm, + TextureFormat::Rgba16Uint => wgpu::TextureFormat::Rgba16Uint, + TextureFormat::Rgba16Sint => wgpu::TextureFormat::Rgba16Sint, + TextureFormat::Rgba16Float => wgpu::TextureFormat::Rgba16Float, + TextureFormat::Rgba32Uint => wgpu::TextureFormat::Rgba32Uint, + TextureFormat::Rgba32Sint => wgpu::TextureFormat::Rgba32Sint, + TextureFormat::Rgba32Float => wgpu::TextureFormat::Rgba32Float, + TextureFormat::Depth32Float => wgpu::TextureFormat::Depth32Float, + TextureFormat::Depth24Plus => wgpu::TextureFormat::Depth24Plus, + TextureFormat::Depth24PlusStencil8 => wgpu::TextureFormat::Depth24PlusStencil8, + } + } +} + +impl From for wgpu::TextureUsage { + fn from(val: TextureUsage) -> Self { + wgpu::TextureUsage::from_bits(val.bits()).unwrap() + } +} + +impl From<&DepthStencilStateDescriptor> for wgpu::DepthStencilStateDescriptor { + fn from(val: &DepthStencilStateDescriptor) -> Self { + wgpu::DepthStencilStateDescriptor { + depth_compare: val.depth_compare.into(), + depth_write_enabled: val.depth_write_enabled, + format: val.format.into(), + stencil_back: (&val.stencil_back).into(), + stencil_front: (&val.stencil_front).into(), + stencil_read_mask: val.stencil_read_mask, + stencil_write_mask: val.stencil_write_mask, + } + } +} + +impl From<&StencilStateFaceDescriptor> for wgpu::StencilStateFaceDescriptor { + fn from(val: &StencilStateFaceDescriptor) -> Self { + wgpu::StencilStateFaceDescriptor { + compare: val.compare.into(), + depth_fail_op: val.depth_fail_op.into(), + fail_op: val.fail_op.into(), + pass_op: val.pass_op.into(), + } + } +} + +impl From for wgpu::CompareFunction { + fn from(val: CompareFunction) -> Self { + match val { + CompareFunction::Never => wgpu::CompareFunction::Never, + CompareFunction::Less => wgpu::CompareFunction::Less, + CompareFunction::Equal => wgpu::CompareFunction::Equal, + CompareFunction::LessEqual => wgpu::CompareFunction::LessEqual, + CompareFunction::Greater => wgpu::CompareFunction::Greater, + CompareFunction::NotEqual => wgpu::CompareFunction::NotEqual, + CompareFunction::GreaterEqual => wgpu::CompareFunction::GreaterEqual, + CompareFunction::Always => wgpu::CompareFunction::Always, + } + } +} + +impl From for wgpu::StencilOperation { + fn from(val: StencilOperation) -> Self { + match val { + StencilOperation::Keep => wgpu::StencilOperation::Keep, + StencilOperation::Zero => wgpu::StencilOperation::Zero, + StencilOperation::Replace => wgpu::StencilOperation::Replace, + StencilOperation::Invert => wgpu::StencilOperation::Invert, + StencilOperation::IncrementClamp => wgpu::StencilOperation::IncrementClamp, + StencilOperation::DecrementClamp => wgpu::StencilOperation::DecrementClamp, + StencilOperation::IncrementWrap => wgpu::StencilOperation::IncrementWrap, + StencilOperation::DecrementWrap => wgpu::StencilOperation::DecrementWrap, + } + } +} + +impl From for wgpu::PrimitiveTopology { + fn from(val: PrimitiveTopology) -> Self { + match val { + PrimitiveTopology::PointList => wgpu::PrimitiveTopology::PointList, + PrimitiveTopology::LineList => wgpu::PrimitiveTopology::LineList, + PrimitiveTopology::LineStrip => wgpu::PrimitiveTopology::LineStrip, + PrimitiveTopology::TriangleList => wgpu::PrimitiveTopology::TriangleList, + PrimitiveTopology::TriangleStrip => wgpu::PrimitiveTopology::TriangleStrip, + } + } +} + +impl From for wgpu::FrontFace { + fn from(val: FrontFace) -> Self { + match val { + FrontFace::Ccw => wgpu::FrontFace::Ccw, + FrontFace::Cw => wgpu::FrontFace::Cw, + } + } +} + +impl From for wgpu::CullMode { + fn from(val: CullMode) -> Self { + match val { + CullMode::None => wgpu::CullMode::None, + CullMode::Front => wgpu::CullMode::Front, + CullMode::Back => wgpu::CullMode::Back, + } + } +} + +impl From<&RasterizationStateDescriptor> for wgpu::RasterizationStateDescriptor { + fn from(val: &RasterizationStateDescriptor) -> Self { + wgpu::RasterizationStateDescriptor { + front_face: val.front_face.into(), + cull_mode: val.cull_mode.into(), + depth_bias: val.depth_bias, + depth_bias_slope_scale: val.depth_bias_slope_scale, + depth_bias_clamp: val.depth_bias_clamp, + } + } +} + +impl From<&ColorStateDescriptor> for wgpu::ColorStateDescriptor { + fn from(val: &ColorStateDescriptor) -> Self { + wgpu::ColorStateDescriptor { + format: val.format.into(), + alpha_blend: (&val.alpha_blend).into(), + color_blend: (&val.color_blend).into(), + write_mask: val.write_mask.into(), + } + } +} + +impl From for wgpu::ColorWrite { + fn from(val: ColorWrite) -> Self { + wgpu::ColorWrite::from_bits(val.bits()).unwrap() + } +} + +impl From<&BlendDescriptor> for wgpu::BlendDescriptor { + fn from(val: &BlendDescriptor) -> Self { + wgpu::BlendDescriptor { + src_factor: val.src_factor.into(), + dst_factor: val.dst_factor.into(), + operation: val.operation.into(), + } + } +} + +impl From for wgpu::BlendFactor { + fn from(val: BlendFactor) -> Self { + match val { + BlendFactor::Zero => wgpu::BlendFactor::Zero, + BlendFactor::One => wgpu::BlendFactor::One, + BlendFactor::SrcColor => wgpu::BlendFactor::SrcColor, + BlendFactor::OneMinusSrcColor => wgpu::BlendFactor::OneMinusSrcColor, + BlendFactor::SrcAlpha => wgpu::BlendFactor::SrcAlpha, + BlendFactor::OneMinusSrcAlpha => wgpu::BlendFactor::OneMinusSrcAlpha, + BlendFactor::DstColor => wgpu::BlendFactor::DstColor, + BlendFactor::OneMinusDstColor => wgpu::BlendFactor::OneMinusDstColor, + BlendFactor::DstAlpha => wgpu::BlendFactor::DstAlpha, + BlendFactor::OneMinusDstAlpha => wgpu::BlendFactor::OneMinusDstAlpha, + BlendFactor::SrcAlphaSaturated => wgpu::BlendFactor::SrcAlphaSaturated, + BlendFactor::BlendColor => wgpu::BlendFactor::BlendColor, + BlendFactor::OneMinusBlendColor => wgpu::BlendFactor::OneMinusBlendColor, + } + } +} + +impl From for wgpu::BlendOperation { + fn from(val: BlendOperation) -> Self { + match val { + BlendOperation::Add => wgpu::BlendOperation::Add, + BlendOperation::Subtract => wgpu::BlendOperation::Subtract, + BlendOperation::ReverseSubtract => wgpu::BlendOperation::ReverseSubtract, + BlendOperation::Min => wgpu::BlendOperation::Min, + BlendOperation::Max => wgpu::BlendOperation::Max, + } + } +} + +impl From for wgpu::IndexFormat { + fn from(val: IndexFormat) -> Self { + match val { + IndexFormat::Uint16 => wgpu::IndexFormat::Uint16, + IndexFormat::Uint32 => wgpu::IndexFormat::Uint32, + } + } +} + +impl From for wgpu::SamplerDescriptor { + fn from(sampler_descriptor: SamplerDescriptor) -> Self { + wgpu::SamplerDescriptor { + address_mode_u: sampler_descriptor.address_mode_u.into(), + address_mode_v: sampler_descriptor.address_mode_v.into(), + address_mode_w: sampler_descriptor.address_mode_w.into(), + mag_filter: sampler_descriptor.mag_filter.into(), + min_filter: sampler_descriptor.min_filter.into(), + mipmap_filter: sampler_descriptor.mipmap_filter.into(), + lod_min_clamp: sampler_descriptor.lod_min_clamp, + lod_max_clamp: sampler_descriptor.lod_max_clamp, + compare_function: sampler_descriptor.compare_function.into(), + } + } +} + +impl From for wgpu::AddressMode { + fn from(val: AddressMode) -> Self { + match val { + AddressMode::ClampToEdge => wgpu::AddressMode::ClampToEdge, + AddressMode::Repeat => wgpu::AddressMode::Repeat, + AddressMode::MirrorRepeat => wgpu::AddressMode::MirrorRepeat, + } + } +} + +impl From for wgpu::FilterMode { + fn from(val: FilterMode) -> Self { + match val { + FilterMode::Nearest => wgpu::FilterMode::Nearest, + FilterMode::Linear => wgpu::FilterMode::Linear, + } + } } \ No newline at end of file diff --git a/src/render/texture/sampler_descriptor.rs b/src/render/texture/sampler_descriptor.rs index fda8abe2f6..0b524854fd 100644 --- a/src/render/texture/sampler_descriptor.rs +++ b/src/render/texture/sampler_descriptor.rs @@ -1,46 +1,55 @@ -use crate::asset::Texture; +use crate::{asset::Texture, render::pipeline::state_descriptors::CompareFunction}; #[derive(Copy, Clone)] pub struct SamplerDescriptor { - pub address_mode_u: wgpu::AddressMode, - pub address_mode_v: wgpu::AddressMode, - pub address_mode_w: wgpu::AddressMode, - pub mag_filter: wgpu::FilterMode, - pub min_filter: wgpu::FilterMode, - pub mipmap_filter: wgpu::FilterMode, + pub address_mode_u: AddressMode, + pub address_mode_v: AddressMode, + pub address_mode_w: AddressMode, + pub mag_filter: FilterMode, + pub min_filter: FilterMode, + pub mipmap_filter: FilterMode, pub lod_min_clamp: f32, pub lod_max_clamp: f32, - pub compare_function: wgpu::CompareFunction, -} - -impl From for wgpu::SamplerDescriptor { - fn from(sampler_descriptor: SamplerDescriptor) -> Self { - wgpu::SamplerDescriptor { - address_mode_u: sampler_descriptor.address_mode_u, - address_mode_v: sampler_descriptor.address_mode_v, - address_mode_w: sampler_descriptor.address_mode_w, - mag_filter: sampler_descriptor.mag_filter, - min_filter: sampler_descriptor.min_filter, - mipmap_filter: sampler_descriptor.mipmap_filter, - lod_min_clamp: sampler_descriptor.lod_min_clamp, - lod_max_clamp: sampler_descriptor.lod_max_clamp, - compare_function: sampler_descriptor.compare_function, - } - } + pub compare_function: CompareFunction, } impl From<&Texture> for SamplerDescriptor { fn from(_texture: &Texture) -> Self { SamplerDescriptor { - address_mode_u: wgpu::AddressMode::ClampToEdge, - address_mode_v: wgpu::AddressMode::ClampToEdge, - address_mode_w: wgpu::AddressMode::ClampToEdge, - mag_filter: wgpu::FilterMode::Nearest, - min_filter: wgpu::FilterMode::Linear, - mipmap_filter: wgpu::FilterMode::Nearest, + address_mode_u: AddressMode::ClampToEdge, + address_mode_v: AddressMode::ClampToEdge, + address_mode_w: AddressMode::ClampToEdge, + mag_filter: FilterMode::Nearest, + min_filter: FilterMode::Linear, + mipmap_filter: FilterMode::Nearest, lod_min_clamp: -100.0, lod_max_clamp: 100.0, - compare_function: wgpu::CompareFunction::Always, + compare_function: CompareFunction::Always, } } } + +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] +pub enum AddressMode { + ClampToEdge = 0, + Repeat = 1, + MirrorRepeat = 2, +} + +impl Default for AddressMode { + fn default() -> Self { + AddressMode::ClampToEdge + } +} + +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] +pub enum FilterMode { + Nearest = 0, + Linear = 1, +} + +impl Default for FilterMode { + fn default() -> Self { + FilterMode::Nearest + } +} diff --git a/src/render/texture/texture_descriptor.rs b/src/render/texture/texture_descriptor.rs index 07c6458b8e..efbb621303 100644 --- a/src/render/texture/texture_descriptor.rs +++ b/src/render/texture/texture_descriptor.rs @@ -1,21 +1,21 @@ -use super::TextureDimension; -use crate::asset::Texture; +use super::{Extent3d, TextureDimension, TextureFormat}; +use crate::{asset::Texture, render::texture::TextureUsage}; #[derive(Copy, Clone)] pub struct TextureDescriptor { - pub size: wgpu::Extent3d, + pub size: Extent3d, pub array_layer_count: u32, pub mip_level_count: u32, pub sample_count: u32, pub dimension: TextureDimension, - pub format: wgpu::TextureFormat, - pub usage: wgpu::TextureUsage, + pub format: TextureFormat, + pub usage: TextureUsage, } impl From<&Texture> for TextureDescriptor { fn from(texture: &Texture) -> Self { TextureDescriptor { - size: wgpu::Extent3d { + size: Extent3d { height: texture.height as u32, width: texture.width as u32, depth: 1, @@ -24,22 +24,8 @@ impl From<&Texture> for TextureDescriptor { mip_level_count: 1, sample_count: 1, dimension: TextureDimension::D2, - format: wgpu::TextureFormat::Rgba8UnormSrgb, - usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST, - } - } -} - -impl From for wgpu::TextureDescriptor { - fn from(texture_descriptor: TextureDescriptor) -> Self { - wgpu::TextureDescriptor { - size: texture_descriptor.size, - array_layer_count: texture_descriptor.array_layer_count, - mip_level_count: texture_descriptor.mip_level_count, - sample_count: texture_descriptor.sample_count, - dimension: texture_descriptor.dimension.into(), - format: texture_descriptor.format, - usage: texture_descriptor.usage, + format: TextureFormat::Rgba8UnormSrgb, + usage: TextureUsage::SAMPLED | TextureUsage::COPY_DST, } } } diff --git a/src/render/texture/texture_dimension.rs b/src/render/texture/texture_dimension.rs index 386ada7d40..e13010dd93 100644 --- a/src/render/texture/texture_dimension.rs +++ b/src/render/texture/texture_dimension.rs @@ -1,3 +1,5 @@ +// NOTE: These are currently just copies of the wgpu types, but they might change in the future + #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)] pub enum TextureViewDimension { D1, @@ -8,18 +10,6 @@ pub enum TextureViewDimension { D3, } -impl From for wgpu::TextureViewDimension { - fn from(dimension: TextureViewDimension) -> Self { - match dimension { - TextureViewDimension::D1 => wgpu::TextureViewDimension::D1, - TextureViewDimension::D2 => wgpu::TextureViewDimension::D2, - TextureViewDimension::D2Array => wgpu::TextureViewDimension::D2Array, - TextureViewDimension::Cube => wgpu::TextureViewDimension::Cube, - TextureViewDimension::CubeArray => wgpu::TextureViewDimension::CubeArray, - TextureViewDimension::D3 => wgpu::TextureViewDimension::D3, - } - } -} #[derive(Copy, Clone, Debug, Hash)] pub enum TextureDimension { @@ -28,12 +18,92 @@ pub enum TextureDimension { D3, } -impl From for wgpu::TextureDimension { - fn from(dimension: TextureDimension) -> Self { - match dimension { - TextureDimension::D1 => wgpu::TextureDimension::D1, - TextureDimension::D2 => wgpu::TextureDimension::D2, - TextureDimension::D3 => wgpu::TextureDimension::D3, - } - } +#[derive(Clone, Copy, Debug)] +pub struct Extent3d { + pub width: u32, + pub height: u32, + pub depth: u32, } + +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] +pub enum TextureFormat { + // Normal 8 bit formats + R8Unorm = 0, + R8Snorm = 1, + R8Uint = 2, + R8Sint = 3, + + // Normal 16 bit formats + R16Unorm = 4, + R16Snorm = 5, + R16Uint = 6, + R16Sint = 7, + R16Float = 8, + + Rg8Unorm = 9, + Rg8Snorm = 10, + Rg8Uint = 11, + Rg8Sint = 12, + + // Normal 32 bit formats + R32Uint = 13, + R32Sint = 14, + R32Float = 15, + Rg16Unorm = 16, + Rg16Snorm = 17, + Rg16Uint = 18, + Rg16Sint = 19, + Rg16Float = 20, + Rgba8Unorm = 21, + Rgba8UnormSrgb = 22, + Rgba8Snorm = 23, + Rgba8Uint = 24, + Rgba8Sint = 25, + Bgra8Unorm = 26, + Bgra8UnormSrgb = 27, + + // Packed 32 bit formats + Rgb10a2Unorm = 28, + Rg11b10Float = 29, + + // Normal 64 bit formats + Rg32Uint = 30, + Rg32Sint = 31, + Rg32Float = 32, + Rgba16Unorm = 33, + Rgba16Snorm = 34, + Rgba16Uint = 35, + Rgba16Sint = 36, + Rgba16Float = 37, + + // Normal 128 bit formats + Rgba32Uint = 38, + Rgba32Sint = 39, + Rgba32Float = 40, + + // Depth and stencil formats + Depth32Float = 41, + Depth24Plus = 42, + Depth24PlusStencil8 = 43, +} + +bitflags::bitflags! { + #[repr(transparent)] + pub struct TextureUsage: u32 { + const COPY_SRC = 1; + const COPY_DST = 2; + const SAMPLED = 4; + const STORAGE = 8; + const OUTPUT_ATTACHMENT = 16; + const NONE = 0; + /// The combination of all read-only usages. + const READ_ALL = Self::COPY_SRC.bits | Self::SAMPLED.bits; + /// The combination of all write-only and read-write usages. + const WRITE_ALL = Self::COPY_DST.bits | Self::STORAGE.bits | Self::OUTPUT_ATTACHMENT.bits; + /// The combination of all usages that the are guaranteed to be be ordered by the hardware. + /// If a usage is not ordered, then even if it doesn't change between draw calls, there + /// still need to be pipeline barriers inserted for synchronization. + const ORDERED = Self::READ_ALL.bits | Self::OUTPUT_ATTACHMENT.bits; + const UNINITIALIZED = 0xFFFF; + } +} \ No newline at end of file