Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add AddressMode::ClampToZero #2364

Merged
merged 12 commits into from
Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,14 @@ impl<A: HalApi> Device<A> {
self.require_features(wgt::Features::ADDRESS_MODE_CLAMP_TO_BORDER)?;
}

if desc
.address_modes
.iter()
.any(|am| am == &wgt::AddressMode::ClampToZero)
{
self.require_features(wgt::Features::ADDRESS_MODE_CLAMP_TO_ZERO)?;
}

let lod_clamp = if desc.lod_min_clamp > 0.0 || desc.lod_max_clamp < 32.0 {
Some(desc.lod_min_clamp..desc.lod_max_clamp)
} else {
Expand Down
1 change: 1 addition & 0 deletions wgpu-hal/src/dx12/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ impl super::Adapter {
| wgt::Features::MULTI_DRAW_INDIRECT
| wgt::Features::MULTI_DRAW_INDIRECT_COUNT
| wgt::Features::ADDRESS_MODE_CLAMP_TO_BORDER
| wgt::Features::ADDRESS_MODE_CLAMP_TO_ZERO
| wgt::Features::POLYGON_MODE_LINE
| wgt::Features::POLYGON_MODE_POINT
| wgt::Features::VERTEX_WRITABLE_STORAGE
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/dx12/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ pub fn map_address_mode(mode: wgt::AddressMode) -> d3d12::D3D12_TEXTURE_ADDRESS_
Am::Repeat => d3d12::D3D12_TEXTURE_ADDRESS_MODE_WRAP,
Am::MirrorRepeat => d3d12::D3D12_TEXTURE_ADDRESS_MODE_MIRROR,
Am::ClampToEdge => d3d12::D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
Am::ClampToBorder => d3d12::D3D12_TEXTURE_ADDRESS_MODE_BORDER,
Am::ClampToBorder | Am::ClampToZero => d3d12::D3D12_TEXTURE_ADDRESS_MODE_BORDER,
//Am::MirrorClamp => d3d12::D3D12_TEXTURE_ADDRESS_MODE_MIRROR_ONCE,
}
}
Expand Down
12 changes: 11 additions & 1 deletion wgpu-hal/src/dx12/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,16 @@ impl crate::Device<super::Api> for super::Device {
.anisotropy_clamp
.map_or(0, |_| d3d12::D3D12_FILTER_ANISOTROPIC);

let border_color = if desc
.address_modes
.iter()
.any(|am| am == &wgt::AddressMode::ClampToZero)
{
[0.0; 4]
} else {
conv::map_border_color(desc.border_color)
laptou marked this conversation as resolved.
Show resolved Hide resolved
};

self.raw.create_sampler(
handle.raw,
filter,
Expand All @@ -631,7 +641,7 @@ impl crate::Device<super::Api> for super::Device {
0.0,
desc.anisotropy_clamp.map_or(0, |aniso| aniso.get() as u32),
conv::map_comparison(desc.compare.unwrap_or(wgt::CompareFunction::Always)),
conv::map_border_color(desc.border_color),
border_color,
desc.lod_clamp.clone().unwrap_or(0.0..16.0),
);

Expand Down
4 changes: 4 additions & 0 deletions wgpu-hal/src/gles/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ impl super::Adapter {
wgt::Features::ADDRESS_MODE_CLAMP_TO_BORDER,
extensions.contains("GL_EXT_texture_border_clamp"),
);
features.set(
wgt::Features::ADDRESS_MODE_CLAMP_TO_ZERO,
laptou marked this conversation as resolved.
Show resolved Hide resolved
extensions.contains("GL_EXT_texture_border_clamp"),
);
features.set(
wgt::Features::DEPTH_CLIP_CONTROL,
extensions.contains("GL_EXT_depth_clamp"),
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 @@ -226,7 +226,7 @@ pub fn map_address_mode(mode: wgt::AddressMode) -> u32 {
wgt::AddressMode::Repeat => glow::REPEAT,
wgt::AddressMode::MirrorRepeat => glow::MIRRORED_REPEAT,
wgt::AddressMode::ClampToEdge => glow::CLAMP_TO_EDGE,
wgt::AddressMode::ClampToBorder => glow::CLAMP_TO_BORDER,
wgt::AddressMode::ClampToBorder | wgt::AddressMode::ClampToZero => glow::CLAMP_TO_BORDER,
//wgt::AddressMode::MirrorClamp => glow::MIRROR_CLAMP_TO_EDGE,
}
}
Expand Down
6 changes: 6 additions & 0 deletions wgpu-hal/src/gles/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,12 @@ impl crate::Device<super::Api> for super::Device {
wgt::SamplerBorderColor::OpaqueWhite => [1.0; 4],
};
gl.sampler_parameter_f32_slice(raw, glow::TEXTURE_BORDER_COLOR, &border);
} else if desc
.address_modes
.iter()
.any(|am| am == &wgt::AddressMode::ClampToBorder)
{
gl.sampler_parameter_f32_slice(raw, glow::TEXTURE_BORDER_COLOR, &[0.0; 4]);
}

if let Some(ref range) = desc.lod_clamp {
Expand Down
1 change: 1 addition & 0 deletions wgpu-hal/src/metal/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,7 @@ impl super::PrivateCapabilities {
F::ADDRESS_MODE_CLAMP_TO_BORDER,
self.sampler_clamp_to_border,
);
features.set(F::ADDRESS_MODE_CLAMP_TO_ZERO, true);

features
}
Expand Down
1 change: 1 addition & 0 deletions wgpu-hal/src/metal/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub fn map_address_mode(address: wgt::AddressMode) -> mtl::MTLSamplerAddressMode
Fm::MirrorRepeat => MirrorRepeat,
Fm::ClampToEdge => ClampToEdge,
Fm::ClampToBorder => ClampToBorderColor,
Fm::ClampToZero => ClampToZero,
//Fm::MirrorClamp => MirrorClampToEdge,
}
}
Expand Down
1 change: 1 addition & 0 deletions wgpu-hal/src/metal/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ impl crate::Device<super::Api> for super::Device {
if let Some(fun) = desc.compare {
descriptor.set_compare_function(conv::map_compare_function(fun));
}

if let Some(border_color) = desc.border_color {
descriptor.set_border_color(conv::map_border_color(border_color));
}
Expand Down
1 change: 1 addition & 0 deletions wgpu-hal/src/vulkan/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ impl PhysicalDeviceFeatures {
| F::MAPPABLE_PRIMARY_BUFFERS
| F::PUSH_CONSTANTS
| F::ADDRESS_MODE_CLAMP_TO_BORDER
| F::ADDRESS_MODE_CLAMP_TO_ZERO
| F::TIMESTAMP_QUERY
| F::PIPELINE_STATISTICS_QUERY
| F::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES
Expand Down
5 changes: 3 additions & 2 deletions wgpu-hal/src/vulkan/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,9 @@ pub fn map_address_mode(mode: wgt::AddressMode) -> vk::SamplerAddressMode {
wgt::AddressMode::ClampToEdge => vk::SamplerAddressMode::CLAMP_TO_EDGE,
wgt::AddressMode::Repeat => vk::SamplerAddressMode::REPEAT,
wgt::AddressMode::MirrorRepeat => vk::SamplerAddressMode::MIRRORED_REPEAT,
wgt::AddressMode::ClampToBorder => vk::SamplerAddressMode::CLAMP_TO_BORDER,
//wgt::AddressMode::MirrorClamp => vk::SamplerAddressMode::MIRROR_CLAMP_TO_EDGE,
wgt::AddressMode::ClampToBorder | wgt::AddressMode::ClampToZero => {
vk::SamplerAddressMode::CLAMP_TO_BORDER
} //wgt::AddressMode::MirrorClamp => vk::SamplerAddressMode::MIRROR_CLAMP_TO_EDGE,
}
}

Expand Down
7 changes: 7 additions & 0 deletions wgpu-hal/src/vulkan/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -974,8 +974,15 @@ impl crate::Device<super::Api> for super::Device {
.max_anisotropy(aniso.get() as f32);
}
}

if let Some(color) = desc.border_color {
vk_info = vk_info.border_color(conv::map_border_color(color));
} else if desc
.address_modes
.iter()
.any(|am| am == &wgt::AddressMode::ClampToBorder)
{
vk_info = vk_info.border_color(vk::BorderColor::FLOAT_TRANSPARENT_BLACK);
}

let raw = self.shared.raw.create_sampler(&vk_info, None)?;
Expand Down
17 changes: 17 additions & 0 deletions wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,17 @@ bitflags::bitflags! {
///
/// This is a native only feature.
const TEXTURE_FORMAT_16BIT_NORM = 1 << 41;
/// Allows the use of [`AddressMode::ClampToZero`].
///
/// Supported platforms:
/// - DX12
/// - Vulkan
/// - Metal
/// - DX11
/// - OpenGL
///
/// This is a web and native feature.
const ADDRESS_MODE_CLAMP_TO_ZERO = 1 << 42;
}
}

Expand Down Expand Up @@ -3048,6 +3059,12 @@ pub enum AddressMode {
/// -0.25 -> border
/// 1.25 -> border
ClampToBorder = 3,
/// Clamp the value to zero outside of the texture
/// Requires feature [`Features::ADDRESS_MODE_CLAMP_TO_ZERO`]
///
/// -0.25 -> zero
/// 1.25 -> zero
ClampToZero = 4,
}

impl Default for AddressMode {
Expand Down
1 change: 1 addition & 0 deletions wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,7 @@ fn map_address_mode(mode: wgt::AddressMode) -> web_sys::GpuAddressMode {
wgt::AddressMode::Repeat => web_sys::GpuAddressMode::Repeat,
wgt::AddressMode::MirrorRepeat => web_sys::GpuAddressMode::MirrorRepeat,
wgt::AddressMode::ClampToBorder => panic!("Clamp to border is not supported"),
wgt::AddressMode::ClampToZero => panic!("Clamp to zero is not supported"),
}
}

Expand Down