Skip to content

Commit 28798f9

Browse files
authored
Implement wgpuCommandEncoderClearTexture (#580)
1 parent 15601bb commit 28798f9

4 files changed

Lines changed: 66 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ The format is loosely based on [Keep a Changelog](https://keepachangelog.com/en/
6666
- `WGPUNativeLimits::maxBindingArraySamplerElementsPerShaderStage` @lisyarus
6767
- `WGPUNativeLimits::maxMultiviewViewCount` @lisyarus
6868
- `WGPUNativeFeature_StorageTextureArrayNonUniformIndexing`, `WGPUNativeFeature_Multiview`, `WGPUNativeFeature_ShaderFloat32Atomic`, `WGPUNativeFeature_TextureAtomic`, `WGPUNativeFeature_TextureFormatP010`, `WGPUNativeFeature_PipelineCache`, `WGPUNativeFeature_ShaderInt64AtomicMinMax`, `WGPUNativeFeature_ShaderInt64AtomicAllOps`, `WGPUNativeFeature_TextureInt64Atomic`, `WGPUNativeFeature_ShaderBarycentrics`, `WGPUNativeFeature_SelectiveMultiview`, `WGPUNativeFeature_MultisampleArray`, `WGPUNativeFeature_CooperativeMatrix`, `WGPUNativeFeature_ShaderPerVertex`, `WGPUNativeFeature_ShaderDrawIndex`, `WGPUNativeFeature_AccelerationStructureBindingArray`, `WGPUNativeFeature_MemoryDecorationCoherent`, `WGPUNativeFeature_MemoryDecorationVolatile` @lisyarus
69+
- `wgpuCommandEncoderClearTexture` @lisyarus
6970
7071
### Removed
7172

ffi/wgpu.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,15 @@ typedef enum WGPUNativeFeature
379379
* This is a native only feature.
380380
*/
381381
WGPUNativeFeature_ConservativeRasterization = 0x00030015,
382-
// TODO: requires wgpu.h api change
383-
// WGPUNativeFeature_ClearTexture = 0x00030016,
382+
/**
383+
* Enables clear to zero for textures.
384+
*
385+
* Supported platforms:
386+
* - All
387+
*
388+
* This is a native only feature.
389+
*/
390+
WGPUNativeFeature_ClearTexture = 0x00030016,
384391
/**
385392
* Enables multiview render passes and `builtin(view_index)` in vertex/mesh shaders.
386393
*
@@ -1456,6 +1463,14 @@ typedef enum WGPUNativeTextureFormat
14561463
WGPUNativeTextureFormat_P010 = 0x00030008,
14571464
} WGPUNativeTextureFormat;
14581465

1466+
typedef struct WGPUImageSubresourceRange {
1467+
WGPUTextureAspect aspect;
1468+
uint32_t baseMipLevel;
1469+
uint32_t mipLevelCount;
1470+
uint32_t baseArrayLayer;
1471+
uint32_t arrayLayerCount;
1472+
} WGPUImageSubresourceRange WGPU_STRUCTURE_ATTRIBUTE;
1473+
14591474
#ifdef __cplusplus
14601475
extern "C"
14611476
{
@@ -1526,6 +1541,8 @@ extern "C"
15261541
WGPUBool wgpuDeviceStartGraphicsDebuggerCapture(WGPUDevice device);
15271542
void wgpuDeviceStopGraphicsDebuggerCapture(WGPUDevice device);
15281543

1544+
void wgpuCommandEncoderClearTexture(WGPUCommandEncoder commandEncoder, WGPUTexture texture, WGPUImageSubresourceRange const * range);
1545+
15291546
#ifdef __cplusplus
15301547
} // extern "C"
15311548
#endif

src/conv.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,10 +1302,9 @@ pub fn features_to_native(features: wgt::Features) -> Vec<native::WGPUFeatureNam
13021302
if features.contains(wgt::Features::CONSERVATIVE_RASTERIZATION) {
13031303
temp.push(native::WGPUNativeFeature_ConservativeRasterization);
13041304
}
1305-
// TODO: requires wgpu.h api change
1306-
// if features.contains(wgt::Features::CLEAR_TEXTURE) {
1307-
// temp.push(native::WGPUNativeFeature_ClearTexture);
1308-
// }
1305+
if features.contains(wgt::Features::CLEAR_TEXTURE) {
1306+
temp.push(native::WGPUNativeFeature_ClearTexture);
1307+
}
13091308
if features.contains(wgt::Features::MULTIVIEW) {
13101309
temp.push(native::WGPUNativeFeature_Multiview);
13111310
}
@@ -1477,7 +1476,7 @@ pub fn map_feature(feature: native::WGPUFeatureName) -> Option<wgt::Features> {
14771476
native::WGPUNativeFeature_PolygonModePoint => Some(Features::POLYGON_MODE_POINT),
14781477
native::WGPUNativeFeature_ConservativeRasterization => Some(Features::CONSERVATIVE_RASTERIZATION),
14791478
// TODO: requires wgpu.h api change
1480-
// native::WGPUNativeFeature_ClearTexture => Some(Features::CLEAR_TEXTURE),
1479+
native::WGPUNativeFeature_ClearTexture => Some(Features::CLEAR_TEXTURE),
14811480
native::WGPUNativeFeature_Multiview => Some(Features::MULTIVIEW),
14821481
native::WGPUNativeFeature_VertexAttribute64bit => Some(Features::VERTEX_ATTRIBUTE_64BIT),
14831482
native::WGPUNativeFeature_TextureFormatNv12 => Some(Features::TEXTURE_FORMAT_NV12),

src/lib.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,48 @@ pub unsafe extern "C" fn wgpuCommandEncoderClearBuffer(
13001300
}
13011301
}
13021302

1303+
#[no_mangle]
1304+
pub unsafe extern "C" fn wgpuCommandEncoderClearTexture(
1305+
command_encoder: native::WGPUCommandEncoder,
1306+
texture: native::WGPUTexture,
1307+
range: Option<&native::WGPUImageSubresourceRange>,
1308+
) {
1309+
let (command_encoder_id, context, error_sink) = {
1310+
let command_encoder = command_encoder.as_ref().expect("invalid command encoder");
1311+
(
1312+
command_encoder.id,
1313+
&command_encoder.context,
1314+
&command_encoder.error_sink,
1315+
)
1316+
};
1317+
let texture_id = texture.as_ref().expect("invalid texture").id;
1318+
1319+
let subresource_range = match range {
1320+
Some(range) => wgt::ImageSubresourceRange {
1321+
aspect: conv::map_texture_aspect(range.aspect).unwrap_or(wgt::TextureAspect::All),
1322+
base_mip_level: range.baseMipLevel,
1323+
mip_level_count: match range.mipLevelCount {
1324+
0 => panic!("invalid mipLevelCount"),
1325+
native::WGPU_MIP_LEVEL_COUNT_UNDEFINED => None,
1326+
_ => Some(range.mipLevelCount),
1327+
},
1328+
base_array_layer: range.baseArrayLayer,
1329+
array_layer_count: match range.arrayLayerCount {
1330+
0 => panic!("invalid arrayLayerCount"),
1331+
native::WGPU_ARRAY_LAYER_COUNT_UNDEFINED => None,
1332+
_ => Some(range.arrayLayerCount),
1333+
},
1334+
},
1335+
None => wgt::ImageSubresourceRange::default(),
1336+
};
1337+
1338+
if let Err(cause) =
1339+
context.command_encoder_clear_texture(command_encoder_id, texture_id, &subresource_range)
1340+
{
1341+
handle_error(error_sink, cause, None, "wgpuCommandEncoderClearTexture");
1342+
}
1343+
}
1344+
13031345
#[no_mangle]
13041346
pub unsafe extern "C" fn wgpuCommandEncoderCopyBufferToBuffer(
13051347
command_encoder: native::WGPUCommandEncoder,

0 commit comments

Comments
 (0)