Skip to content

Commit

Permalink
Evolve depth clamping into clip control
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Nov 10, 2021
1 parent cf2fb0b commit 722d707
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 120 deletions.
123 changes: 34 additions & 89 deletions deno_webgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ pub fn init(unstable: bool) -> Extension {
fn deserialize_features(features: &wgpu_types::Features) -> Vec<&'static str> {
let mut return_features: Vec<&'static str> = vec![];

if features.contains(wgpu_types::Features::DEPTH_CLAMPING) {
return_features.push("depth-clamping");
if features.contains(wgpu_types::Features::DEPTH_CLIP_CONTROL) {
return_features.push("depth-clip-control");
}
if features.contains(wgpu_types::Features::PIPELINE_STATISTICS_QUERY) {
return_features.push("pipeline-statistics-query");
Expand Down Expand Up @@ -309,105 +309,50 @@ pub struct GpuRequiredFeatures(HashSet<String>);
impl From<GpuRequiredFeatures> for wgpu_types::Features {
fn from(required_features: GpuRequiredFeatures) -> wgpu_types::Features {
let mut features: wgpu_types::Features = wgpu_types::Features::empty();

if required_features.0.contains("depth-clamping") {
features.set(wgpu_types::Features::DEPTH_CLAMPING, true);
}
if required_features.0.contains("pipeline-statistics-query") {
features.set(wgpu_types::Features::PIPELINE_STATISTICS_QUERY, true);
}
if required_features.0.contains("texture-compression-bc") {
features.set(wgpu_types::Features::TEXTURE_COMPRESSION_BC, true);
}
if required_features.0.contains("timestamp-query") {
features.set(wgpu_types::Features::TIMESTAMP_QUERY, true);
}
features.set(wgpu_types::Features::DEPTH_CLIP_CONTROL, required_features.0.contains("depth-clip-control"));
features.set(wgpu_types::Features::PIPELINE_STATISTICS_QUERY, required_features.0.contains("pipeline-statistics-query"));
features.set(wgpu_types::Features::TEXTURE_COMPRESSION_BC, required_features.0.contains("texture-compression-bc"));
features.set(wgpu_types::Features::TIMESTAMP_QUERY, required_features.0.contains("timestamp-query"));

// extended from spec
if required_features.0.contains("mappable-primary-buffers") {
features.set(wgpu_types::Features::MAPPABLE_PRIMARY_BUFFERS, true);
}
if required_features.0.contains("texture-binding-array") {
features.set(wgpu_types::Features::TEXTURE_BINDING_ARRAY, true);
}
if required_features.0.contains("buffer-binding-array") {
features.set(wgpu_types::Features::BUFFER_BINDING_ARRAY, true);
}
if required_features
features.set(wgpu_types::Features::MAPPABLE_PRIMARY_BUFFERS, required_features.0.contains("mappable-primary-buffers"));
features.set(wgpu_types::Features::TEXTURE_BINDING_ARRAY, required_features.0.contains("texture-binding-array"));
features.set(wgpu_types::Features::BUFFER_BINDING_ARRAY, required_features.0.contains("buffer-binding-array"));
features.set(wgpu_types::Features::STORAGE_RESOURCE_BINDING_ARRAY, required_features
.0
.contains("storage-resource-binding-array")
{
features.set(wgpu_types::Features::STORAGE_RESOURCE_BINDING_ARRAY, true);
}
if required_features
.0
.contains("sampled-texture-and-storage-buffer-array-non-uniform-indexing")
{
.contains("storage-resource-binding-array"));
features.set(
wgpu_types::Features::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING,
true,
);
}
if required_features
required_features
.0
.contains("uniform-buffer-and-storage-buffer-texture-non-uniform-indexing")
{
.contains("sampled-texture-and-storage-buffer-array-non-uniform-indexing"),
);
features.set(
wgpu_types::Features::UNIFORM_BUFFER_AND_STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING,
true,
);
}
if required_features.0.contains("unsized-binding-array") {
features.set(wgpu_types::Features::UNSIZED_BINDING_ARRAY, true);
}
if required_features.0.contains("multi-draw-indirect") {
features.set(wgpu_types::Features::MULTI_DRAW_INDIRECT, true);
}
if required_features.0.contains("multi-draw-indirect-count") {
features.set(wgpu_types::Features::MULTI_DRAW_INDIRECT_COUNT, true);
}
if required_features.0.contains("push-constants") {
features.set(wgpu_types::Features::PUSH_CONSTANTS, true);
}
if required_features.0.contains("address-mode-clamp-to-border") {
features.set(wgpu_types::Features::ADDRESS_MODE_CLAMP_TO_BORDER, true);
}
if required_features.0.contains("texture-compression-etc2") {
features.set(wgpu_types::Features::TEXTURE_COMPRESSION_ETC2, true);
}
if required_features.0.contains("texture-compression-astc-ldr") {
features.set(wgpu_types::Features::TEXTURE_COMPRESSION_ASTC_LDR, true);
}
if required_features
required_features
.0
.contains("texture-adapter-specific-format-features")
{
.contains("uniform-buffer-and-storage-buffer-texture-non-uniform-indexing"),
);
features.set(wgpu_types::Features::UNSIZED_BINDING_ARRAY, required_features.0.contains("unsized-binding-array"));
features.set(wgpu_types::Features::MULTI_DRAW_INDIRECT, required_features.0.contains("multi-draw-indirect"));
features.set(wgpu_types::Features::MULTI_DRAW_INDIRECT_COUNT, required_features.0.contains("multi-draw-indirect-count"));
features.set(wgpu_types::Features::PUSH_CONSTANTS, required_features.0.contains("push-constants"));
features.set(wgpu_types::Features::ADDRESS_MODE_CLAMP_TO_BORDER, required_features.0.contains("address-mode-clamp-to-border"));
features.set(wgpu_types::Features::TEXTURE_COMPRESSION_ETC2, required_features.0.contains("texture-compression-etc2"));
features.set(wgpu_types::Features::TEXTURE_COMPRESSION_ASTC_LDR, required_features.0.contains("texture-compression-astc-ldr"));
features.set(
wgpu_types::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES,
true,
required_features
.0
.contains("texture-adapter-specific-format-features"),
);
}
if required_features.0.contains("shader-float64") {
features.set(wgpu_types::Features::SHADER_FLOAT64, true);
}
if required_features.0.contains("vertex-attribute-64bit") {
features.set(wgpu_types::Features::VERTEX_ATTRIBUTE_64BIT, true);
}
if required_features.0.contains("conservative-rasterization") {
features.set(wgpu_types::Features::CONSERVATIVE_RASTERIZATION, true);
}
if required_features.0.contains("vertex-writable-storage") {
features.set(wgpu_types::Features::VERTEX_WRITABLE_STORAGE, true);
}
if required_features.0.contains("clear-commands") {
features.set(wgpu_types::Features::CLEAR_COMMANDS, true);
}
if required_features.0.contains("spirv-shader-passthrough") {
features.set(wgpu_types::Features::SPIRV_SHADER_PASSTHROUGH, true);
}
if required_features.0.contains("shader-primitive-index") {
features.set(wgpu_types::Features::SHADER_PRIMITIVE_INDEX, true);
}
features.set(wgpu_types::Features::SHADER_FLOAT64, required_features.0.contains("shader-float64"));
features.set(wgpu_types::Features::VERTEX_ATTRIBUTE_64BIT, required_features.0.contains("vertex-attribute-64bit"));
features.set(wgpu_types::Features::CONSERVATIVE_RASTERIZATION, required_features.0.contains("conservative-rasterization"));
features.set(wgpu_types::Features::VERTEX_WRITABLE_STORAGE, required_features.0.contains("vertex-writable-storage"));
features.set(wgpu_types::Features::CLEAR_COMMANDS, required_features.0.contains("clear-commands"));
features.set(wgpu_types::Features::SPIRV_SHADER_PASSTHROUGH, required_features.0.contains("spirv-shader-passthrough"));
features.set(wgpu_types::Features::SHADER_PRIMITIVE_INDEX, required_features.0.contains("shader-primitive-index"));

features
}
Expand Down
4 changes: 2 additions & 2 deletions deno_webgpu/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ struct GpuPrimitiveState {
strip_index_format: Option<wgpu_types::IndexFormat>,
front_face: wgpu_types::FrontFace,
cull_mode: GpuCullMode,
clamp_depth: bool,
unclipped_depth: bool,
}

impl From<GpuPrimitiveState> for wgpu_types::PrimitiveState {
Expand All @@ -181,7 +181,7 @@ impl From<GpuPrimitiveState> for wgpu_types::PrimitiveState {
strip_index_format: value.strip_index_format,
front_face: value.front_face,
cull_mode: value.cull_mode.into(),
clamp_depth: value.clamp_depth,
unclipped_depth: value.unclipped_depth,
polygon_mode: Default::default(), // native-only
conservative: false, // native-only
}
Expand Down
4 changes: 2 additions & 2 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2211,8 +2211,8 @@ impl<A: HalApi> Device<A> {
);
}

if desc.primitive.clamp_depth {
self.require_features(wgt::Features::DEPTH_CLAMPING)?;
if desc.primitive.unclipped_depth {
self.require_features(wgt::Features::DEPTH_CLIP_CONTROL)?;
}

if desc.primitive.polygon_mode == wgt::PolygonMode::Line {
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/dx12/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl super::Adapter {
};

let mut features = wgt::Features::empty()
| wgt::Features::DEPTH_CLAMPING
| wgt::Features::DEPTH_CLIP_CONTROL
| wgt::Features::MAPPABLE_PRIMARY_BUFFERS
//TODO: Naga part
//| wgt::Features::TEXTURE_BINDING_ARRAY
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/dx12/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,7 @@ impl crate::Device<super::Api> for super::Device {
DepthBias: bias.constant,
DepthBiasClamp: bias.clamp,
SlopeScaledDepthBias: bias.slope_scale,
DepthClipEnable: if desc.primitive.clamp_depth { 0 } else { 1 },
DepthClipEnable: if desc.primitive.unclipped_depth { 0 } else { 1 },
MultisampleEnable: if desc.multisample.count > 1 { 1 } else { 0 },
ForcedSampleCount: 0,
AntialiasedLineEnable: 0,
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/gles/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ impl super::Adapter {
| wgt::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES
| wgt::Features::CLEAR_COMMANDS;
features.set(
wgt::Features::DEPTH_CLAMPING,
wgt::Features::DEPTH_CLIP_CONTROL,
extensions.contains("GL_EXT_depth_clamp"),
);
features.set(
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/gles/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ pub(super) fn map_primitive_state(state: &wgt::PrimitiveState) -> super::Primiti
Some(wgt::Face::Back) => glow::BACK,
None => 0,
},
clamp_depth: state.clamp_depth,
unclipped_depth: state.unclipped_depth,
}
}

Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/gles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ struct StencilState {
struct PrimitiveState {
front_face: u32,
cull_face: u32,
clamp_depth: bool,
unclipped_depth: bool,
}

type InvalidatedAttachments = ArrayVec<u32, { crate::MAX_COLOR_TARGETS + 2 }>;
Expand Down
7 changes: 4 additions & 3 deletions wgpu-hal/src/gles/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl super::Queue {
gl.disable(glow::BLEND);
gl.disable(glow::CULL_FACE);
gl.disable(glow::POLYGON_OFFSET_FILL);
if self.features.contains(wgt::Features::DEPTH_CLAMPING) {
if self.features.contains(wgt::Features::DEPTH_CLIP_CONTROL) {
gl.disable(glow::DEPTH_CLAMP);
}
}
Expand Down Expand Up @@ -927,8 +927,9 @@ impl super::Queue {
} else {
gl.disable(glow::CULL_FACE);
}
if self.features.contains(wgt::Features::DEPTH_CLAMPING) {
if state.clamp_depth {
if self.features.contains(wgt::Features::DEPTH_CLIP_CONTROL) {
//Note: this is a bit tricky, since we are controlling the clip, not the clamp.
if state.unclipped_depth {
gl.enable(glow::DEPTH_CLAMP);
} else {
gl.disable(glow::DEPTH_CLAMP);
Expand Down
5 changes: 3 additions & 2 deletions wgpu-hal/src/metal/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,8 @@ impl super::PrivateCapabilities {
Self::version_at_least(major, minor, 11, 0)
},
//Depth clipping is supported on all macOS GPU families and iOS family 4 and later
supports_depth_clamping: device.supports_feature_set(MTLFeatureSet::iOS_GPUFamily4_v1)
supports_depth_clip_control: device
.supports_feature_set(MTLFeatureSet::iOS_GPUFamily4_v1)
|| os_is_mac,
}
}
Expand All @@ -877,7 +878,7 @@ impl super::PrivateCapabilities {
| F::POLYGON_MODE_LINE
| F::CLEAR_COMMANDS;

features.set(F::DEPTH_CLAMPING, self.supports_depth_clamping);
features.set(F::DEPTH_CLIP_CONTROL, self.supports_depth_clip_control);

features.set(
F::TEXTURE_BINDING_ARRAY
Expand Down
4 changes: 2 additions & 2 deletions wgpu-hal/src/metal/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -906,8 +906,8 @@ impl crate::Device<super::Api> for super::Device {
raw_triangle_fill_mode,
raw_front_winding: conv::map_winding(desc.primitive.front_face),
raw_cull_mode: conv::map_cull_mode(desc.primitive.cull_mode),
raw_depth_clip_mode: if self.features.contains(wgt::Features::DEPTH_CLAMPING) {
Some(if desc.primitive.clamp_depth {
raw_depth_clip_mode: if self.features.contains(wgt::Features::DEPTH_CLIP_CONTROL) {
Some(if desc.primitive.unclipped_depth {
mtl::MTLDepthClipMode::Clamp
} else {
mtl::MTLDepthClipMode::Clip
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ struct PrivateCapabilities {
supports_arrays_of_textures: bool,
supports_arrays_of_textures_write: bool,
supports_mutability: bool,
supports_depth_clamping: bool,
supports_depth_clip_control: bool,
}

#[derive(Clone, Debug)]
Expand Down
25 changes: 23 additions & 2 deletions wgpu-hal/src/vulkan/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct PhysicalDeviceFeatures {
timeline_semaphore: Option<vk::PhysicalDeviceTimelineSemaphoreFeaturesKHR>,
image_robustness: Option<vk::PhysicalDeviceImageRobustnessFeaturesEXT>,
robustness2: Option<vk::PhysicalDeviceRobustness2FeaturesEXT>,
depth_clip_enable: Option<vk::PhysicalDeviceDepthClipEnableFeaturesEXT>,
}

// This is safe because the structs have `p_next: *mut c_void`, which we null out/never read.
Expand Down Expand Up @@ -53,6 +54,9 @@ impl PhysicalDeviceFeatures {
if let Some(ref mut feature) = self.robustness2 {
info = info.push_next(feature);
}
if let Some(ref mut feature) = self.depth_clip_enable {
info = info.push_next(feature);
}
info
}

Expand Down Expand Up @@ -102,7 +106,6 @@ impl PhysicalDeviceFeatures {
.multi_draw_indirect(
requested_features.contains(wgt::Features::MULTI_DRAW_INDIRECT),
)
.depth_clamp(requested_features.contains(wgt::Features::DEPTH_CLAMPING))
.fill_mode_non_solid(requested_features.intersects(
wgt::Features::POLYGON_MODE_LINE | wgt::Features::POLYGON_MODE_POINT,
))
Expand Down Expand Up @@ -281,6 +284,17 @@ impl PhysicalDeviceFeatures {
} else {
None
},
depth_clip_enable: if enabled_extensions.contains(&vk::ExtRobustness2Fn::name()) {
Some(
vk::PhysicalDeviceDepthClipEnableFeaturesEXT::builder()
.depth_clip_enable(
requested_features.contains(wgt::Features::DEPTH_CLIP_CONTROL),
)
.build(),
)
} else {
None
},
}
}

Expand All @@ -307,7 +321,6 @@ impl PhysicalDeviceFeatures {

//if self.core.dual_src_blend != 0
features.set(F::MULTI_DRAW_INDIRECT, self.core.multi_draw_indirect != 0);
features.set(F::DEPTH_CLAMPING, self.core.depth_clamp != 0);
features.set(F::POLYGON_MODE_LINE, self.core.fill_mode_non_solid != 0);
features.set(F::POLYGON_MODE_POINT, self.core.fill_mode_non_solid != 0);
//if self.core.depth_bounds != 0 {
Expand Down Expand Up @@ -462,6 +475,10 @@ impl PhysicalDeviceFeatures {
}
}

if let Some(ref feature) = self.depth_clip_enable {
features.set(F::DEPTH_CLIP_CONTROL, feature.depth_clip_enable != 0);
}

(features, dl_flags)
}

Expand Down Expand Up @@ -540,6 +557,10 @@ impl PhysicalDeviceCapabilities {
extensions.push(vk::ExtConservativeRasterizationFn::name());
}

if requested_features.contains(wgt::Features::DEPTH_CLIP_CONTROL) {
extensions.push(vk::ExtDepthClipEnableFn::name());
}

extensions
}

Expand Down
8 changes: 7 additions & 1 deletion wgpu-hal/src/vulkan/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,6 @@ impl crate::Device<super::Api> for super::Device {
};

let mut vk_rasterization = vk::PipelineRasterizationStateCreateInfo::builder()
.depth_clamp_enable(desc.primitive.clamp_depth)
.polygon_mode(conv::map_polygon_mode(desc.primitive.polygon_mode))
.front_face(conv::map_front_face(desc.primitive.front_face))
.line_width(1.0);
Expand All @@ -1396,6 +1395,13 @@ impl crate::Device<super::Api> for super::Device {
if desc.primitive.conservative {
vk_rasterization = vk_rasterization.push_next(&mut vk_rasterization_conservative_state);
}
let mut vk_depth_clip_state =
vk::PipelineRasterizationDepthClipStateCreateInfoEXT::builder()
.depth_clip_enable(true)
.build();
if desc.primitive.unclipped_depth {
vk_rasterization = vk_rasterization.push_next(&mut vk_depth_clip_state);
}

let mut vk_depth_stencil = vk::PipelineDepthStencilStateCreateInfo::builder();
if let Some(ref ds) = desc.depth_stencil {
Expand Down
Loading

0 comments on commit 722d707

Please sign in to comment.