From 3ec4ff99b05027e47d17a7675c9f007a9e26c2b8 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Sun, 3 Feb 2019 20:39:08 -0500 Subject: [PATCH] Vertex buffer description in the pipelines --- examples/hello_triangle_rust/main.rs | 2 ++ gfx-examples/data/cube.frag | 4 +-- gfx-examples/src/cube.rs | 23 +++++++++++++-- wgpu-bindings/wgpu.h | 44 ++++++++++++++++++++++++++++ wgpu-native/src/conv.rs | 13 +++++++- wgpu-native/src/device.rs | 36 +++++++++++++++++++---- wgpu-native/src/pipeline.rs | 21 +++++++------ wgpu-rs/src/lib.rs | 23 +++++++++++++++ 8 files changed, 145 insertions(+), 21 deletions(-) diff --git a/examples/hello_triangle_rust/main.rs b/examples/hello_triangle_rust/main.rs index b1c800cc4..9120a4b0b 100644 --- a/examples/hello_triangle_rust/main.rs +++ b/examples/hello_triangle_rust/main.rs @@ -54,6 +54,8 @@ fn main() { }, blend_states: &[&blend_state0], depth_stencil_state: &depth_stencil_state, + index_format: wgpu::IndexFormat::Uint16, + vertex_buffers: &[], }); #[cfg(feature = "winit")] diff --git a/gfx-examples/data/cube.frag b/gfx-examples/data/cube.frag index 64bfb9d60..826ebf436 100644 --- a/gfx-examples/data/cube.frag +++ b/gfx-examples/data/cube.frag @@ -3,8 +3,8 @@ layout(location = 0) in vec2 v_TexCoord; layout(location = 0) out vec4 o_Target; -layout(set = 0, binding = 0) uniform texture2D t_Color; -layout(set = 0, binding = 1) uniform sampler s_Color; +layout(set = 0, binding = 1) uniform texture2D t_Color; +layout(set = 0, binding = 2) uniform sampler s_Color; void main() { vec4 tex = texture(sampler2D(t_Color, s_Color), v_TexCoord); diff --git a/gfx-examples/src/cube.rs b/gfx-examples/src/cube.rs index 92be14edb..61dd63ba9 100644 --- a/gfx-examples/src/cube.rs +++ b/gfx-examples/src/cube.rs @@ -96,12 +96,12 @@ impl fw::Example for Cube { let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { bindings: &[ wgpu::BindGroupLayoutBinding { - binding: 0, + binding: 1, visibility: wgpu::ShaderStageFlags::FRAGMENT, ty: wgpu::BindingType::SampledTexture, }, wgpu::BindGroupLayoutBinding { - binding: 1, + binding: 2, visibility: wgpu::ShaderStageFlags::FRAGMENT, ty: wgpu::BindingType::Sampler, }, @@ -193,6 +193,25 @@ impl fw::Example for Cube { }, blend_states: &[&blend_state0], depth_stencil_state: &depth_stencil_state, + index_format: wgpu::IndexFormat::Uint16, + vertex_buffers: &[ + wgpu::VertexBufferDescriptor { + stride: vertex_size as u32, + step_mode: wgpu::InputStepMode::Vertex, + attributes: &[ + wgpu::VertexAttributeDescriptor { + attribute_index: 0, + format: wgpu::VertexFormat::FloatR32G32B32A32, + offset: 0, + }, + wgpu::VertexAttributeDescriptor { + attribute_index: 1, + format: wgpu::VertexFormat::FloatR32G32, + offset: 4 * 4, + }, + ], + }, + ], }); // Done diff --git a/wgpu-bindings/wgpu.h b/wgpu-bindings/wgpu.h index 12cc9eec4..94cf5ccb3 100644 --- a/wgpu-bindings/wgpu.h +++ b/wgpu-bindings/wgpu.h @@ -69,6 +69,16 @@ typedef enum { WGPUFilterMode_Linear = 1, } WGPUFilterMode; +typedef enum { + WGPUIndexFormat_Uint16 = 0, + WGPUIndexFormat_Uint32 = 1, +} WGPUIndexFormat; + +typedef enum { + WGPUInputStepMode_Vertex = 0, + WGPUInputStepMode_Instance = 1, +} WGPUInputStepMode; + typedef enum { WGPULoadOp_Clear = 0, WGPULoadOp_Load = 1, @@ -131,6 +141,13 @@ typedef enum { WGPUTextureViewDimension_D3, } WGPUTextureViewDimension; +typedef enum { + WGPUVertexFormat_FloatR32G32B32A32 = 0, + WGPUVertexFormat_FloatR32G32B32 = 1, + WGPUVertexFormat_FloatR32G32 = 2, + WGPUVertexFormat_FloatR32 = 3, +} WGPUVertexFormat; + typedef WGPUId WGPUDeviceId; typedef WGPUId WGPUAdapterId; @@ -324,6 +341,27 @@ typedef struct { const WGPUAttachment *depth_stencil_attachment; } WGPUAttachmentsState; +typedef uint32_t WGPUShaderAttributeIndex; + +typedef struct { + uint32_t offset; + WGPUVertexFormat format; + WGPUShaderAttributeIndex attribute_index; +} WGPUVertexAttributeDescriptor; + +typedef struct { + uint32_t stride; + WGPUInputStepMode step_mode; + const WGPUVertexAttributeDescriptor *attributes; + uintptr_t attributes_count; +} WGPUVertexBufferDescriptor; + +typedef struct { + WGPUIndexFormat index_format; + const WGPUVertexBufferDescriptor *vertex_buffers; + uintptr_t vertex_buffers_count; +} WGPUVertexBufferStateDescriptor; + typedef struct { WGPUPipelineLayoutId layout; const WGPUPipelineStageDescriptor *stages; @@ -333,6 +371,7 @@ typedef struct { const WGPUBlendStateId *blend_states; uintptr_t blend_states_length; WGPUDepthStencilStateId depth_stencil_state; + WGPUVertexBufferStateDescriptor vertex_buffer_state; } WGPURenderPipelineDescriptor; typedef struct { @@ -588,6 +627,11 @@ void wgpu_render_pass_set_index_buffer(WGPURenderPassId pass_id, void wgpu_render_pass_set_pipeline(WGPURenderPassId pass_id, WGPURenderPipelineId pipeline_id); +void wgpu_render_pass_set_vertex_buffers(WGPURenderPassId pass_id, + const WGPUBufferId *buffer_ptr, + const uint32_t *offset_ptr, + uintptr_t count); + WGPUSwapChainOutput wgpu_swap_chain_get_next_texture(WGPUSwapChainId swap_chain_id); void wgpu_swap_chain_present(WGPUSwapChainId swap_chain_id); diff --git a/wgpu-native/src/conv.rs b/wgpu-native/src/conv.rs index be48535d7..0ce936f19 100644 --- a/wgpu-native/src/conv.rs +++ b/wgpu-native/src/conv.rs @@ -264,6 +264,17 @@ pub fn map_texture_format(texture_format: resource::TextureFormat) -> hal::forma } } +pub fn map_vertex_format(vertex_format: pipeline::VertexFormat) -> hal::format::Format { + use hal::format::Format as H; + use crate::pipeline::VertexFormat::*; + match vertex_format { + FloatR32G32B32A32 => H::Rgba32Float, + FloatR32G32B32 => H::Rgb32Float, + FloatR32G32 => H::Rg32Float, + FloatR32 => H::R32Float, + } +} + fn checked_u32_as_u16(value: u32) -> u16 { assert!(value <= ::std::u16::MAX as u32); value as u16 @@ -429,4 +440,4 @@ pub fn map_wrap(address: resource::AddressMode) -> hal::image::WrapMode { } Am::ClampToBorderColor => W::Border, } -} \ No newline at end of file +} diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index b327daae7..42f495982 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -1044,11 +1044,37 @@ pub extern "C" fn wgpu_device_create_render_pipeline( conservative: false, }; - // TODO - let vertex_buffers: Vec = Vec::new(); - - // TODO - let attributes: Vec = Vec::new(); + let desc_vbs = unsafe { + slice::from_raw_parts(desc.vertex_buffer_state.vertex_buffers, desc.vertex_buffer_state.vertex_buffers_count) + }; + let mut vertex_buffers: Vec = Vec::with_capacity(desc_vbs.len()); + let mut attributes: Vec = Vec::new(); + for (i, vb_state) in desc_vbs.iter().enumerate() { + if vb_state.attributes_count == 0 { + continue + } + vertex_buffers.push(hal::pso::VertexBufferDesc { + binding: i as u32, + stride: vb_state.stride, + rate: match vb_state.step_mode { + pipeline::InputStepMode::Vertex => 0, + pipeline::InputStepMode::Instance => 1, + }, + }); + let desc_atts = unsafe { + slice::from_raw_parts(vb_state.attributes, vb_state.attributes_count) + }; + for attribute in desc_atts { + attributes.push(hal::pso::AttributeDesc { + location: attribute.attribute_index, + binding: i as u32, + element: hal::pso::Element { + format: conv::map_vertex_format(attribute.format), + offset: attribute.offset, + }, + }); + } + } let input_assembler = hal::pso::InputAssemblerDesc { primitive: conv::map_primitive_topology(desc.primitive_topology), diff --git a/wgpu-native/src/pipeline.rs b/wgpu-native/src/pipeline.rs index 8e978e2e0..dde4cc93d 100644 --- a/wgpu-native/src/pipeline.rs +++ b/wgpu-native/src/pipeline.rs @@ -7,6 +7,8 @@ use crate::{ use bitflags::bitflags; +pub type ShaderAttributeIndex = u32; + #[repr(C)] #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] pub enum BlendFactor { @@ -163,28 +165,24 @@ pub enum InputStepMode { #[repr(C)] pub struct VertexAttributeDescriptor { - pub shader_location: u32, - pub input_slot: u32, pub offset: u32, pub format: VertexFormat, + pub attribute_index: ShaderAttributeIndex, } #[repr(C)] -pub struct VertexInputDescriptor { - pub input_slot: u32, +pub struct VertexBufferDescriptor { pub stride: u32, pub step_mode: InputStepMode, + pub attributes: *const VertexAttributeDescriptor, + pub attributes_count: usize, } #[repr(C)] -pub struct InputStateDescriptor<'a> { +pub struct VertexBufferStateDescriptor { pub index_format: IndexFormat, - pub attributes: &'a [VertexAttributeDescriptor], - pub inputs: &'a [VertexInputDescriptor], -} - -pub struct InputState { - // TODO + pub vertex_buffers: *const VertexBufferDescriptor, + pub vertex_buffers_count: usize, } #[repr(C)] @@ -251,6 +249,7 @@ pub struct RenderPipelineDescriptor { pub blend_states: *const BlendStateId, pub blend_states_length: usize, pub depth_stencil_state: DepthStencilStateId, + pub vertex_buffer_state: VertexBufferStateDescriptor, } pub(crate) struct RenderPipeline { diff --git a/wgpu-rs/src/lib.rs b/wgpu-rs/src/lib.rs index ffa8b61c2..1acf7be00 100644 --- a/wgpu-rs/src/lib.rs +++ b/wgpu-rs/src/lib.rs @@ -11,6 +11,7 @@ use std::ptr; pub use wgn::{ AdapterDescriptor, Attachment, BindGroupLayoutBinding, BindingType, BlendStateDescriptor, BufferDescriptor, BufferUsageFlags, + IndexFormat, VertexFormat, InputStepMode, ShaderAttributeIndex, VertexAttributeDescriptor, Color, ColorWriteFlags, CommandBufferDescriptor, DepthStencilStateDescriptor, DeviceDescriptor, Extensions, Extent3d, LoadOp, Origin3d, PowerPreference, PrimitiveTopology, RenderPassColorAttachmentDescriptor, RenderPassDepthStencilAttachmentDescriptor, @@ -144,6 +145,12 @@ pub struct AttachmentsState<'a> { pub depth_stencil_attachment: Option, } +pub struct VertexBufferDescriptor<'a> { + pub stride: u32, + pub step_mode: InputStepMode, + pub attributes: &'a [VertexAttributeDescriptor], +} + pub struct RenderPipelineDescriptor<'a> { pub layout: &'a PipelineLayout, pub stages: &'a [PipelineStageDescriptor<'a>], @@ -151,6 +158,8 @@ pub struct RenderPipelineDescriptor<'a> { pub attachments_state: AttachmentsState<'a>, pub blend_states: &'a [&'a BlendState], pub depth_stencil_state: &'a DepthStencilState, + pub index_format: IndexFormat, + pub vertex_buffers: &'a [VertexBufferDescriptor<'a>], } pub struct RenderPassDescriptor<'a> { @@ -316,6 +325,15 @@ impl Device { .collect::>(); let temp_blend_states = desc.blend_states.iter().map(|bs| bs.id).collect::>(); + let temp_vertex_buffers = desc.vertex_buffers + .iter() + .map(|vbuf| wgn::VertexBufferDescriptor { + stride: vbuf.stride, + step_mode: vbuf.step_mode, + attributes: vbuf.attributes.as_ptr(), + attributes_count: vbuf.attributes.len(), + }) + .collect::>(); RenderPipeline { id: wgn::wgpu_device_create_render_pipeline( @@ -338,6 +356,11 @@ impl Device { blend_states: temp_blend_states.as_ptr(), blend_states_length: temp_blend_states.len(), depth_stencil_state: desc.depth_stencil_state.id, + vertex_buffer_state: wgn::VertexBufferStateDescriptor { + index_format: desc.index_format, + vertex_buffers: temp_vertex_buffers.as_ptr(), + vertex_buffers_count: temp_vertex_buffers.len(), + }, }, ), }