mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-23 07:51:08 +01:00
Implement TEXTURE_BINDING_ARRAY extension
This commit is contained in:
1 parent
7b866b1a1b
commit
f8be38cc01
16 files changed
+419
-190
No files matched your search
+16
-65
@@ -1,14 +1,15 @@
|
||||
use crate::{
|
||||
backend::native_gpu_future, BindGroupDescriptor, BindGroupLayoutDescriptor, BindingResource,
|
||||
BindingType, BufferDescriptor, CommandEncoderDescriptor, ComputePipelineDescriptor, Extensions,
|
||||
Limits, MapMode, PipelineLayoutDescriptor, RenderPipelineDescriptor, SamplerDescriptor,
|
||||
SwapChainStatus, TextureDescriptor, TextureViewDescriptor, TextureViewDimension,
|
||||
BufferDescriptor, CommandEncoderDescriptor, ComputePipelineDescriptor, Extensions, Limits,
|
||||
MapMode, PipelineLayoutDescriptor, RenderPipelineDescriptor, SamplerDescriptor,
|
||||
SwapChainStatus, TextureDescriptor, TextureViewDescriptor,
|
||||
};
|
||||
|
||||
use arrayvec::ArrayVec;
|
||||
use futures::future::{ready, Ready};
|
||||
use smallvec::SmallVec;
|
||||
use std::{ffi::CString, marker::PhantomData, ops::Range, ptr, slice};
|
||||
use typed_arena::Arena;
|
||||
|
||||
macro_rules! gfx_select {
|
||||
($id:expr => $global:ident.$method:ident( $($param:expr),+ )) => {
|
||||
@@ -291,64 +292,9 @@ impl crate::Context for Context {
|
||||
device: &Self::DeviceId,
|
||||
desc: &BindGroupLayoutDescriptor,
|
||||
) -> Self::BindGroupLayoutId {
|
||||
use wgc::binding_model as bm;
|
||||
|
||||
let temp_layouts = desc
|
||||
.bindings
|
||||
.iter()
|
||||
.map(|bind| bm::BindGroupLayoutEntry {
|
||||
binding: bind.binding,
|
||||
visibility: bind.visibility,
|
||||
ty: match bind.ty {
|
||||
BindingType::UniformBuffer { .. } => bm::BindingType::UniformBuffer,
|
||||
BindingType::StorageBuffer {
|
||||
readonly: false, ..
|
||||
} => bm::BindingType::StorageBuffer,
|
||||
BindingType::StorageBuffer { readonly: true, .. } => {
|
||||
bm::BindingType::ReadonlyStorageBuffer
|
||||
}
|
||||
BindingType::Sampler { comparison: false } => bm::BindingType::Sampler,
|
||||
BindingType::Sampler { .. } => bm::BindingType::ComparisonSampler,
|
||||
BindingType::SampledTexture { .. } => bm::BindingType::SampledTexture,
|
||||
BindingType::StorageTexture { readonly: true, .. } => {
|
||||
bm::BindingType::ReadonlyStorageTexture
|
||||
}
|
||||
BindingType::StorageTexture { .. } => bm::BindingType::WriteonlyStorageTexture,
|
||||
},
|
||||
has_dynamic_offset: match bind.ty {
|
||||
BindingType::UniformBuffer { dynamic }
|
||||
| BindingType::StorageBuffer { dynamic, .. } => dynamic,
|
||||
_ => false,
|
||||
},
|
||||
multisampled: match bind.ty {
|
||||
BindingType::SampledTexture { multisampled, .. } => multisampled,
|
||||
_ => false,
|
||||
},
|
||||
view_dimension: match bind.ty {
|
||||
BindingType::SampledTexture { dimension, .. }
|
||||
| BindingType::StorageTexture { dimension, .. } => dimension,
|
||||
_ => TextureViewDimension::D2,
|
||||
},
|
||||
texture_component_type: match bind.ty {
|
||||
BindingType::SampledTexture { component_type, .. }
|
||||
| BindingType::StorageTexture { component_type, .. } => component_type,
|
||||
_ => wgt::TextureComponentType::Float,
|
||||
},
|
||||
storage_texture_format: match bind.ty {
|
||||
BindingType::StorageTexture { format, .. } => format,
|
||||
_ => wgt::TextureFormat::Rgb10a2Unorm, // doesn't matter
|
||||
},
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let owned_label = OwnedLabel::new(desc.label.as_deref());
|
||||
gfx_select!(*device => self.device_create_bind_group_layout(
|
||||
*device,
|
||||
&bm::BindGroupLayoutDescriptor {
|
||||
entries: temp_layouts.as_ptr(),
|
||||
entries_length: temp_layouts.len(),
|
||||
label: owned_label.as_ptr(),
|
||||
},
|
||||
desc,
|
||||
PhantomData
|
||||
))
|
||||
.unwrap()
|
||||
@@ -361,13 +307,14 @@ impl crate::Context for Context {
|
||||
) -> Self::BindGroupId {
|
||||
use wgc::binding_model as bm;
|
||||
|
||||
let texture_view_arena: Arena<wgc::id::TextureViewId> = Arena::new();
|
||||
let bindings = desc
|
||||
.bindings
|
||||
.iter()
|
||||
.map(|binding| bm::BindGroupEntry {
|
||||
binding: binding.binding,
|
||||
resource: match &binding.resource {
|
||||
BindingResource::Buffer(buffer_slice) => {
|
||||
resource: match binding.resource {
|
||||
BindingResource::Buffer(ref buffer_slice) => {
|
||||
bm::BindingResource::Buffer(bm::BufferBinding {
|
||||
buffer: buffer_slice.buffer.id,
|
||||
offset: buffer_slice.offset,
|
||||
@@ -380,18 +327,22 @@ impl crate::Context for Context {
|
||||
BindingResource::TextureView(ref texture_view) => {
|
||||
bm::BindingResource::TextureView(texture_view.id)
|
||||
}
|
||||
BindingResource::TextureViewArray(texture_view_array) => {
|
||||
bm::BindingResource::TextureViewArray(
|
||||
texture_view_arena
|
||||
.alloc_extend(texture_view_array.iter().map(|view| view.id)),
|
||||
)
|
||||
}
|
||||
},
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let owned_label = OwnedLabel::new(desc.label.as_deref());
|
||||
gfx_select!(*device => self.device_create_bind_group(
|
||||
*device,
|
||||
&bm::BindGroupDescriptor {
|
||||
label: desc.label,
|
||||
layout: desc.layout.id,
|
||||
entries: bindings.as_ptr(),
|
||||
entries_length: bindings.len(),
|
||||
label: owned_label.as_ptr(),
|
||||
bindings: &bindings,
|
||||
},
|
||||
PhantomData
|
||||
))
|
||||
|
||||
@@ -773,8 +773,14 @@ impl crate::Context for Context {
|
||||
bt::ReadonlyStorageTexture
|
||||
}
|
||||
BindingType::StorageTexture { .. } => bt::WriteonlyStorageTexture,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
assert!(
|
||||
bind.count.is_none(),
|
||||
"The web backend doesn't support arrays of bindings"
|
||||
);
|
||||
|
||||
let mut mapped_entry = web_sys::GpuBindGroupLayoutEntry::new(
|
||||
bind.binding,
|
||||
mapped_type,
|
||||
@@ -848,6 +854,9 @@ impl crate::Context for Context {
|
||||
BindingResource::TextureView(ref texture_view) => {
|
||||
JsValue::from(texture_view.id.0.clone())
|
||||
}
|
||||
BindingResource::TextureViewArray(..) => {
|
||||
panic!("Web backend does not support TEXTURE_BINDING_ARRAY extension")
|
||||
}
|
||||
};
|
||||
|
||||
web_sys::GpuBindGroupEntry::new(binding.binding, &mapped_resource)
|
||||
|
||||
+11
-121
@@ -19,15 +19,15 @@ use parking_lot::Mutex;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub use wgc::instance::{AdapterInfo, DeviceType};
|
||||
pub use wgt::{
|
||||
read_spirv, AddressMode, Backend, BackendBit, BlendDescriptor, BlendFactor, BlendOperation,
|
||||
BufferAddress, BufferSize, BufferUsage, Color, ColorStateDescriptor, ColorWrite,
|
||||
CommandBufferDescriptor, CompareFunction, CullMode, DepthStencilStateDescriptor,
|
||||
DeviceDescriptor, DynamicOffset, Extensions, Extent3d, FilterMode, FrontFace, IndexFormat,
|
||||
InputStepMode, Limits, LoadOp, Origin3d, PowerPreference, PresentMode, PrimitiveTopology,
|
||||
RasterizationStateDescriptor, ShaderLocation, ShaderStage, StencilOperation,
|
||||
StencilStateFaceDescriptor, StoreOp, SwapChainDescriptor, SwapChainStatus, TextureAspect,
|
||||
TextureComponentType, TextureDataLayout, TextureDimension, TextureFormat, TextureUsage,
|
||||
TextureViewDimension, UnsafeExtensions, VertexAttributeDescriptor, VertexFormat,
|
||||
read_spirv, AddressMode, Backend, BackendBit, BindGroupLayoutDescriptor, BindGroupLayoutEntry,
|
||||
BindingType, BlendDescriptor, BlendFactor, BlendOperation, BufferAddress, BufferSize,
|
||||
BufferUsage, Color, ColorStateDescriptor, ColorWrite, CommandBufferDescriptor, CompareFunction,
|
||||
CullMode, DepthStencilStateDescriptor, DeviceDescriptor, DynamicOffset, Extensions, Extent3d,
|
||||
FilterMode, FrontFace, IndexFormat, InputStepMode, Limits, LoadOp, Origin3d, PowerPreference,
|
||||
PresentMode, PrimitiveTopology, RasterizationStateDescriptor, ShaderLocation, ShaderStage,
|
||||
StencilOperation, StencilStateFaceDescriptor, StoreOp, SwapChainDescriptor, SwapChainStatus,
|
||||
TextureAspect, TextureComponentType, TextureDataLayout, TextureDimension, TextureFormat,
|
||||
TextureUsage, TextureViewDimension, UnsafeExtensions, VertexAttributeDescriptor, VertexFormat,
|
||||
BIND_BUFFER_ALIGNMENT, COPY_BYTES_PER_ROW_ALIGNMENT,
|
||||
};
|
||||
|
||||
@@ -606,10 +606,12 @@ pub struct Queue {
|
||||
}
|
||||
|
||||
/// A resource that can be bound to a pipeline.
|
||||
#[non_exhaustive]
|
||||
pub enum BindingResource<'a> {
|
||||
Buffer(BufferSlice<'a>),
|
||||
Sampler(&'a Sampler),
|
||||
TextureView(&'a TextureView),
|
||||
TextureViewArray(&'a [TextureView]),
|
||||
}
|
||||
|
||||
/// A bindable resource and the slot to bind it to.
|
||||
@@ -618,118 +620,6 @@ pub struct Binding<'a> {
|
||||
pub resource: BindingResource<'a>,
|
||||
}
|
||||
|
||||
/// Specific type of a binding.
|
||||
/// WebGPU spec: https://gpuweb.github.io/gpuweb/#dictdef-gpubindgrouplayoutentry
|
||||
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
|
||||
pub enum BindingType {
|
||||
/// A buffer for uniform values.
|
||||
///
|
||||
/// Example GLSL syntax:
|
||||
/// ```cpp,ignore
|
||||
/// layout(std140, binding = 0)
|
||||
/// uniform Globals {
|
||||
/// vec2 aUniform;
|
||||
/// vec2 anotherUniform;
|
||||
/// };
|
||||
/// ```
|
||||
UniformBuffer {
|
||||
/// Indicates that the binding has a dynamic offset.
|
||||
/// One offset must be passed to [RenderPass::set_bind_group] for each dynamic binding in increasing order of binding number.
|
||||
dynamic: bool,
|
||||
},
|
||||
/// A storage buffer.
|
||||
///
|
||||
/// Example GLSL syntax:
|
||||
/// ```cpp,ignore
|
||||
/// layout (set=0, binding=0) buffer myStorageBuffer {
|
||||
/// vec4 myElement[];
|
||||
/// };
|
||||
/// ```
|
||||
StorageBuffer {
|
||||
/// Indicates that the binding has a dynamic offset.
|
||||
/// One offset must be passed to [RenderPass::set_bind_group] for each dynamic binding in increasing order of binding number.
|
||||
dynamic: bool,
|
||||
/// The buffer can only be read in the shader and it must be annotated with `readonly`.
|
||||
///
|
||||
/// Example GLSL syntax:
|
||||
/// ```cpp,ignore
|
||||
/// layout (set=0, binding=0) readonly buffer myStorageBuffer {
|
||||
/// vec4 myElement[];
|
||||
/// };
|
||||
/// ```
|
||||
readonly: bool,
|
||||
},
|
||||
/// A sampler that can be used to sample a texture.
|
||||
///
|
||||
/// Example GLSL syntax:
|
||||
/// ```cpp,ignore
|
||||
/// layout(binding = 0)
|
||||
/// uniform sampler s;
|
||||
/// ```
|
||||
Sampler {
|
||||
/// Use as a comparison sampler instead of a normal sampler.
|
||||
/// For more info take a look at the analogous functionality in OpenGL: https://www.khronos.org/opengl/wiki/Sampler_Object#Comparison_mode.
|
||||
comparison: bool,
|
||||
},
|
||||
/// A texture.
|
||||
///
|
||||
/// Example GLSL syntax:
|
||||
/// ```cpp,ignore
|
||||
/// layout(binding = 0)
|
||||
/// uniform texture2D t;
|
||||
/// ```
|
||||
SampledTexture {
|
||||
/// Dimension of the texture view that is going to be sampled.
|
||||
dimension: TextureViewDimension,
|
||||
/// Component type of the texture.
|
||||
/// This must be compatible with the format of the texture.
|
||||
component_type: TextureComponentType,
|
||||
/// True if the texture has a sample count greater than 1.
|
||||
multisampled: bool,
|
||||
},
|
||||
/// A storage texture.
|
||||
/// Example GLSL syntax:
|
||||
/// ```cpp,ignore
|
||||
/// layout(set=0, binding=0, r32f) uniform image2D myStorageImage;
|
||||
/// ```
|
||||
/// Note that the texture format must be specified in the shader as well.
|
||||
/// A list of valid formats can be found in the specification here: https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.4.60.html#layout-qualifiers
|
||||
StorageTexture {
|
||||
/// Dimension of the texture view that is going to be sampled.
|
||||
dimension: TextureViewDimension,
|
||||
/// Component type of the texture.
|
||||
/// This must be compatible with the format of the texture.
|
||||
component_type: TextureComponentType,
|
||||
/// Format of the texture.
|
||||
format: TextureFormat,
|
||||
/// The texture can only be read in the shader and it must be annotated with `readonly`.
|
||||
///
|
||||
/// Example GLSL syntax:
|
||||
/// ```cpp,ignore
|
||||
/// layout(set=0, binding=0, r32f) readonly uniform image2D myStorageImage;
|
||||
/// ```
|
||||
readonly: bool,
|
||||
},
|
||||
}
|
||||
|
||||
/// A description of a single binding inside a bind group.
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
pub struct BindGroupLayoutEntry {
|
||||
pub binding: u32,
|
||||
pub visibility: ShaderStage,
|
||||
pub ty: BindingType,
|
||||
}
|
||||
|
||||
/// A description of a bind group layout.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct BindGroupLayoutDescriptor<'a> {
|
||||
pub bindings: &'a [BindGroupLayoutEntry],
|
||||
|
||||
/// An optional label to apply to the bind group layout.
|
||||
/// This can be useful for debugging and performance analysis.
|
||||
pub label: Option<&'a str>,
|
||||
}
|
||||
|
||||
/// A description of a group of bindings and the resources to be bound.
|
||||
#[derive(Clone)]
|
||||
pub struct BindGroupDescriptor<'a> {
|
||||
|
||||
Reference in new issue
Block a user