mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-24 08:21:25 +01:00
Vertex buffer description in the pipelines
This commit is contained in:
1 parent
3d47436129
commit
3ec4ff99b0
8 files changed
+145
-21
No files matched your search
@@ -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")]
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
+12
-1
@@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1044,11 +1044,37 @@ pub extern "C" fn wgpu_device_create_render_pipeline(
|
||||
conservative: false,
|
||||
};
|
||||
|
||||
// TODO
|
||||
let vertex_buffers: Vec<hal::pso::VertexBufferDesc> = Vec::new();
|
||||
|
||||
// TODO
|
||||
let attributes: Vec<hal::pso::AttributeDesc> = 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<hal::pso::VertexBufferDesc> = Vec::with_capacity(desc_vbs.len());
|
||||
let mut attributes: Vec<hal::pso::AttributeDesc> = 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),
|
||||
|
||||
+10
-11
@@ -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<B: hal::Backend> {
|
||||
|
||||
@@ -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<Attachment>,
|
||||
}
|
||||
|
||||
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::<ArrayVec<[_; 2]>>();
|
||||
|
||||
let temp_blend_states = desc.blend_states.iter().map(|bs| bs.id).collect::<Vec<_>>();
|
||||
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::<Vec<_>>();
|
||||
|
||||
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(),
|
||||
},
|
||||
},
|
||||
),
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user