mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-22 15:31:03 +01:00
Move clear_texture impl from wgpu-hal to wgpu-core (#2046)
* new test for clearing textures of all formats * clear_texture is now always implemented in wgpu-core instead of wgpu-hal * add 1D/array/volume texture to clear_texture test * set mip_level_count to 1 for D1 textures in clear_texture test * fix warning & clippy lints * Fix web build * [pr feedback] fix potentially skipped resource transition, fix wrong mip size for texture clear * collect_zero_buffer_copies_for_clear_texture aligns rows now properly
This commit is contained in:
1 parent
42de30ad13
commit
a28c52873f
18 files changed
+435
-313
No files matched your search
@@ -3,9 +3,11 @@ use std::{num::NonZeroU32, ops::Range};
|
||||
#[cfg(feature = "trace")]
|
||||
use crate::device::trace::Command as TraceCommand;
|
||||
use crate::{
|
||||
align_to,
|
||||
command::CommandBuffer,
|
||||
get_lowest_common_denom,
|
||||
hub::{Global, GlobalIdentityHandlerFactory, HalApi, Token},
|
||||
id::{BufferId, CommandEncoderId, TextureId},
|
||||
id::{BufferId, CommandEncoderId, DeviceId, TextureId},
|
||||
init_tracker::MemoryInitKind,
|
||||
track::TextureSelector,
|
||||
};
|
||||
@@ -23,6 +25,8 @@ pub enum ClearError {
|
||||
MissingClearCommandsFeature,
|
||||
#[error("command encoder {0:?} is invalid")]
|
||||
InvalidCommandEncoder(CommandEncoderId),
|
||||
#[error("device {0:?} is invalid")]
|
||||
InvalidDevice(DeviceId),
|
||||
#[error("buffer {0:?} is invalid or destroyed")]
|
||||
InvalidBuffer(BufferId),
|
||||
#[error("texture {0:?} is invalid or destroyed")]
|
||||
@@ -158,6 +162,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
|
||||
|
||||
let hub = A::hub(self);
|
||||
let mut token = Token::root();
|
||||
let (device_guard, mut token) = hub.devices.write(&mut token);
|
||||
let (mut cmd_buf_guard, mut token) = hub.command_buffers.write(&mut token);
|
||||
let cmd_buf = CommandBuffer::get_encoder_mut(&mut *cmd_buf_guard, command_encoder_id)
|
||||
.map_err(|_| ClearError::InvalidCommandEncoder(command_encoder_id))?;
|
||||
@@ -251,11 +256,101 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
|
||||
|
||||
// actual hal barrier & operation
|
||||
let dst_barrier = dst_pending.map(|pending| pending.into_hal(dst_texture));
|
||||
let cmd_buf_raw = cmd_buf.encoder.open();
|
||||
let encoder = cmd_buf.encoder.open();
|
||||
let device = &device_guard[cmd_buf.device_id.value];
|
||||
|
||||
let mut zero_buffer_copy_regions = Vec::new();
|
||||
collect_zero_buffer_copies_for_clear_texture(
|
||||
&dst_texture.desc,
|
||||
device.alignments.buffer_copy_pitch.get() as u32,
|
||||
subresource_range.base_mip_level..subresource_level_end,
|
||||
subresource_range.base_array_layer..subresource_layer_end,
|
||||
&mut zero_buffer_copy_regions,
|
||||
);
|
||||
unsafe {
|
||||
cmd_buf_raw.transition_textures(dst_barrier);
|
||||
cmd_buf_raw.clear_texture(dst_raw, subresource_range);
|
||||
encoder.transition_textures(dst_barrier);
|
||||
if !zero_buffer_copy_regions.is_empty() {
|
||||
encoder.copy_buffer_to_texture(
|
||||
&device.zero_buffer,
|
||||
dst_raw,
|
||||
zero_buffer_copy_regions.into_iter(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn collect_zero_buffer_copies_for_clear_texture(
|
||||
texture_desc: &wgt::TextureDescriptor<()>,
|
||||
buffer_copy_pitch: u32,
|
||||
mip_range: Range<u32>,
|
||||
layer_range: Range<u32>,
|
||||
out_copy_regions: &mut Vec<hal::BufferTextureCopy>, // TODO: Something better than Vec
|
||||
) {
|
||||
let format_desc = texture_desc.format.describe();
|
||||
|
||||
let bytes_per_row_alignment =
|
||||
get_lowest_common_denom(buffer_copy_pitch, format_desc.block_size as u32);
|
||||
|
||||
for mip_level in mip_range {
|
||||
let mip_size = texture_desc.mip_level_size(mip_level).unwrap();
|
||||
|
||||
let bytes_per_row = align_to(
|
||||
// row is at least one block wide, need to round up
|
||||
(mip_size.width + format_desc.block_dimensions.1 as u32 - 1)
|
||||
/ format_desc.block_dimensions.0 as u32
|
||||
* format_desc.block_size as u32,
|
||||
bytes_per_row_alignment,
|
||||
);
|
||||
|
||||
let max_rows_per_copy = crate::device::ZERO_BUFFER_SIZE as u32 / bytes_per_row;
|
||||
// round down to a multiple of rows needed by the texture format
|
||||
let max_rows_per_copy = max_rows_per_copy / format_desc.block_dimensions.1 as u32
|
||||
* format_desc.block_dimensions.1 as u32;
|
||||
assert!(max_rows_per_copy > 0, "Zero buffer size is too small to fill a single row of a texture with format {:?} and desc {:?}",
|
||||
texture_desc.format, texture_desc.size);
|
||||
|
||||
let z_range = 0..(if texture_desc.dimension == wgt::TextureDimension::D3 {
|
||||
mip_size.depth_or_array_layers
|
||||
} else {
|
||||
1
|
||||
});
|
||||
|
||||
for array_layer in layer_range.clone() {
|
||||
// TODO: Only doing one layer at a time for volume textures right now.
|
||||
for z in z_range.clone() {
|
||||
// May need multiple copies for each subresource! However, we assume that we never need to split a row.
|
||||
let mut num_rows_left = mip_size.height;
|
||||
while num_rows_left > 0 {
|
||||
let num_rows = num_rows_left.min(max_rows_per_copy);
|
||||
|
||||
out_copy_regions.push(hal::BufferTextureCopy {
|
||||
buffer_layout: wgt::ImageDataLayout {
|
||||
offset: 0,
|
||||
bytes_per_row: NonZeroU32::new(bytes_per_row),
|
||||
rows_per_image: None,
|
||||
},
|
||||
texture_base: hal::TextureCopyBase {
|
||||
mip_level,
|
||||
array_layer,
|
||||
origin: wgt::Origin3d {
|
||||
x: 0, // Always full rows
|
||||
y: mip_size.height - num_rows_left,
|
||||
z,
|
||||
},
|
||||
aspect: hal::FormatAspects::all(),
|
||||
},
|
||||
size: hal::CopyExtent {
|
||||
width: mip_size.width, // full row
|
||||
height: num_rows,
|
||||
depth: 1, // Only single slice of volume texture at a time right now
|
||||
},
|
||||
});
|
||||
|
||||
num_rows_left -= num_rows;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,9 @@ pub mod queue;
|
||||
pub mod trace;
|
||||
|
||||
pub const SHADER_STAGE_COUNT: usize = 3;
|
||||
// Should be large enough for the largest possible texture row. This value is enough for a 16k texture with float4 format.
|
||||
pub(crate) const ZERO_BUFFER_SIZE: BufferAddress = 512 << 10;
|
||||
|
||||
const CLEANUP_WAIT_MS: u32 = 5000;
|
||||
|
||||
const IMPLICIT_FAILURE: &str = "failed implicit";
|
||||
@@ -232,7 +235,7 @@ impl<A: hal::Api> CommandAllocator<A> {
|
||||
/// Structure describing a logical device. Some members are internally mutable,
|
||||
/// stored behind mutexes.
|
||||
/// TODO: establish clear order of locking for these:
|
||||
/// `mem_allocator`, `desc_allocator`, `life_tracke`, `trackers`,
|
||||
/// `mem_allocator`, `desc_allocator`, `life_tracker`, `trackers`,
|
||||
/// `render_passes`, `pending_writes`, `trace`.
|
||||
///
|
||||
/// Currently, the rules are:
|
||||
@@ -243,6 +246,7 @@ pub struct Device<A: hal::Api> {
|
||||
pub(crate) raw: A::Device,
|
||||
pub(crate) adapter_id: Stored<id::AdapterId>,
|
||||
pub(crate) queue: A::Queue,
|
||||
pub(crate) zero_buffer: A::Buffer,
|
||||
//pub(crate) cmd_allocator: command::CommandAllocator<A>,
|
||||
//mem_allocator: Mutex<alloc::MemoryAllocator<A>>,
|
||||
//desc_allocator: Mutex<descriptor::DescriptorAllocator<A>>,
|
||||
@@ -271,6 +275,8 @@ pub struct Device<A: hal::Api> {
|
||||
pub enum CreateDeviceError {
|
||||
#[error("not enough memory left")]
|
||||
OutOfMemory,
|
||||
#[error("failed to create internal buffer for initializing textures")]
|
||||
FailedToCreateZeroBuffer(#[from] DeviceError),
|
||||
}
|
||||
|
||||
impl<A: hal::Api> Device<A> {
|
||||
@@ -317,12 +323,43 @@ impl<A: HalApi> Device<A> {
|
||||
let pending_encoder = com_alloc
|
||||
.acquire_encoder(&open.device, &open.queue)
|
||||
.map_err(|_| CreateDeviceError::OutOfMemory)?;
|
||||
let pending_writes = queue::PendingWrites::new(pending_encoder);
|
||||
let mut pending_writes = queue::PendingWrites::<A>::new(pending_encoder);
|
||||
|
||||
// Create zeroed buffer used for texture clears.
|
||||
let zero_buffer = unsafe {
|
||||
open.device
|
||||
.create_buffer(&hal::BufferDescriptor {
|
||||
label: Some("wgpu zero init buffer"),
|
||||
size: ZERO_BUFFER_SIZE,
|
||||
usage: hal::BufferUses::COPY_SRC | hal::BufferUses::COPY_DST,
|
||||
memory_flags: hal::MemoryFlags::empty(),
|
||||
})
|
||||
.map_err(DeviceError::from)?
|
||||
};
|
||||
pending_writes.activate();
|
||||
unsafe {
|
||||
pending_writes
|
||||
.command_encoder
|
||||
.transition_buffers(iter::once(hal::BufferBarrier {
|
||||
buffer: &zero_buffer,
|
||||
usage: hal::BufferUses::empty()..hal::BufferUses::COPY_DST,
|
||||
}));
|
||||
pending_writes
|
||||
.command_encoder
|
||||
.clear_buffer(&zero_buffer, 0..ZERO_BUFFER_SIZE);
|
||||
pending_writes
|
||||
.command_encoder
|
||||
.transition_buffers(iter::once(hal::BufferBarrier {
|
||||
buffer: &zero_buffer,
|
||||
usage: hal::BufferUses::COPY_DST..hal::BufferUses::COPY_SRC,
|
||||
}));
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
raw: open.device,
|
||||
adapter_id,
|
||||
queue: open.queue,
|
||||
zero_buffer,
|
||||
life_guard: LifeGuard::new("<device>"),
|
||||
command_allocator: Mutex::new(com_alloc),
|
||||
active_submission_index: 0,
|
||||
@@ -2560,6 +2597,7 @@ impl<A: hal::Api> Device<A> {
|
||||
self.pending_writes.dispose(&self.raw);
|
||||
self.command_allocator.into_inner().dispose(&self.raw);
|
||||
unsafe {
|
||||
self.raw.destroy_buffer(self.zero_buffer);
|
||||
self.raw.destroy_fence(self.fence);
|
||||
self.raw.exit(self.queue);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
#[cfg(feature = "trace")]
|
||||
use crate::device::trace::Action;
|
||||
use crate::{
|
||||
align_to,
|
||||
command::{
|
||||
extract_texture_selector, validate_linear_texture_data, validate_texture_copy_range,
|
||||
CommandBuffer, CopySide, ImageCopyTexture, TransferError,
|
||||
},
|
||||
conv,
|
||||
device::{DeviceError, WaitIdleError},
|
||||
get_lowest_common_denom,
|
||||
hub::{Global, GlobalIdentityHandlerFactory, HalApi, Token},
|
||||
id,
|
||||
resource::{BufferAccessError, BufferMapState, TextureInner},
|
||||
@@ -878,47 +880,3 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn get_lowest_common_denom(a: u32, b: u32) -> u32 {
|
||||
let gcd = if a >= b {
|
||||
get_greatest_common_divisor(a, b)
|
||||
} else {
|
||||
get_greatest_common_divisor(b, a)
|
||||
};
|
||||
a * b / gcd
|
||||
}
|
||||
|
||||
fn get_greatest_common_divisor(mut a: u32, mut b: u32) -> u32 {
|
||||
assert!(a >= b);
|
||||
loop {
|
||||
let c = a % b;
|
||||
if c == 0 {
|
||||
return b;
|
||||
} else {
|
||||
a = b;
|
||||
b = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn align_to(value: u32, alignment: u32) -> u32 {
|
||||
match value % alignment {
|
||||
0 => value,
|
||||
other => value - other + alignment,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lcd() {
|
||||
assert_eq!(get_lowest_common_denom(2, 2), 2);
|
||||
assert_eq!(get_lowest_common_denom(2, 3), 6);
|
||||
assert_eq!(get_lowest_common_denom(6, 4), 12);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gcd() {
|
||||
assert_eq!(get_greatest_common_divisor(5, 1), 1);
|
||||
assert_eq!(get_greatest_common_divisor(4, 2), 2);
|
||||
assert_eq!(get_greatest_common_divisor(6, 4), 2);
|
||||
assert_eq!(get_greatest_common_divisor(7, 7), 7);
|
||||
}
|
||||
@@ -227,3 +227,50 @@ type FastHashMap<K, V> =
|
||||
std::collections::HashMap<K, V, std::hash::BuildHasherDefault<fxhash::FxHasher>>;
|
||||
/// Fast hash set used internally.
|
||||
type FastHashSet<K> = std::collections::HashSet<K, std::hash::BuildHasherDefault<fxhash::FxHasher>>;
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn get_lowest_common_denom(a: u32, b: u32) -> u32 {
|
||||
let gcd = if a >= b {
|
||||
get_greatest_common_divisor(a, b)
|
||||
} else {
|
||||
get_greatest_common_divisor(b, a)
|
||||
};
|
||||
a * b / gcd
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn get_greatest_common_divisor(mut a: u32, mut b: u32) -> u32 {
|
||||
assert!(a >= b);
|
||||
loop {
|
||||
let c = a % b;
|
||||
if c == 0 {
|
||||
return b;
|
||||
} else {
|
||||
a = b;
|
||||
b = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn align_to(value: u32, alignment: u32) -> u32 {
|
||||
match value % alignment {
|
||||
0 => value,
|
||||
other => value - other + alignment,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lcd() {
|
||||
assert_eq!(get_lowest_common_denom(2, 2), 2);
|
||||
assert_eq!(get_lowest_common_denom(2, 3), 6);
|
||||
assert_eq!(get_lowest_common_denom(6, 4), 12);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gcd() {
|
||||
assert_eq!(get_greatest_common_divisor(5, 1), 1);
|
||||
assert_eq!(get_greatest_common_divisor(4, 2), 2);
|
||||
assert_eq!(get_greatest_common_divisor(6, 4), 2);
|
||||
assert_eq!(get_greatest_common_divisor(7, 7), 7);
|
||||
}
|
||||
@@ -367,100 +367,6 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn clear_texture(
|
||||
&mut self,
|
||||
texture: &super::Texture,
|
||||
subresource_range: &wgt::ImageSubresourceRange,
|
||||
) {
|
||||
// Note that CopyTextureRegion for depth/stencil or multisample resources would require full subresource copies.
|
||||
// Meaning we'd need a much larger pre-zeroed buffer
|
||||
// (but instead we just define clear_texture to not support these)
|
||||
|
||||
let list = self.list.unwrap();
|
||||
let mut src_location = d3d12::D3D12_TEXTURE_COPY_LOCATION {
|
||||
pResource: self.shared.zero_buffer.as_mut_ptr(),
|
||||
Type: d3d12::D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT,
|
||||
u: mem::zeroed(),
|
||||
};
|
||||
let mut dst_location = d3d12::D3D12_TEXTURE_COPY_LOCATION {
|
||||
pResource: texture.resource.as_mut_ptr(),
|
||||
Type: d3d12::D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX,
|
||||
u: mem::zeroed(),
|
||||
};
|
||||
let raw_format = conv::map_texture_format(texture.format);
|
||||
let format_desc = texture.format.describe();
|
||||
|
||||
let mip_range = subresource_range.base_mip_level..match subresource_range.mip_level_count {
|
||||
Some(c) => subresource_range.base_mip_level + c.get(),
|
||||
None => texture.mip_level_count,
|
||||
};
|
||||
let array_range = subresource_range.base_array_layer
|
||||
..match subresource_range.array_layer_count {
|
||||
Some(c) => subresource_range.base_array_layer + c.get(),
|
||||
None => texture.array_layer_count(),
|
||||
};
|
||||
for mip_level in mip_range {
|
||||
let mip_size = texture
|
||||
.size
|
||||
.mip_level_size(mip_level, texture.dimension == wgt::TextureDimension::D3);
|
||||
let depth = if texture.dimension == wgt::TextureDimension::D3 {
|
||||
mip_size.depth_or_array_layers
|
||||
} else {
|
||||
1
|
||||
};
|
||||
let bytes_per_row = mip_size.width / format_desc.block_dimensions.0 as u32
|
||||
* format_desc.block_size as u32;
|
||||
// round up to a multiple of d3d12::D3D12_TEXTURE_DATA_PITCH_ALIGNMENT
|
||||
let bytes_per_row = (bytes_per_row + d3d12::D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)
|
||||
/ d3d12::D3D12_TEXTURE_DATA_PITCH_ALIGNMENT
|
||||
* d3d12::D3D12_TEXTURE_DATA_PITCH_ALIGNMENT;
|
||||
|
||||
let max_rows_per_copy = super::ZERO_BUFFER_SIZE as u32 / bytes_per_row;
|
||||
// round down to a multiple of rows needed by the texture format
|
||||
let max_rows_per_copy = max_rows_per_copy / format_desc.block_dimensions.1 as u32
|
||||
* format_desc.block_dimensions.1 as u32;
|
||||
assert!(max_rows_per_copy > 0, "Zero buffer size is too small to fill a single row of a texture with dimension {:?}, size {:?} and format {:?}", texture.dimension, texture.size, texture.format);
|
||||
|
||||
for array_layer in array_range.clone() {
|
||||
// We excluded depth/stencil, so plane should be always zero
|
||||
*dst_location.u.SubresourceIndex_mut() =
|
||||
texture.calc_subresource(mip_level, array_layer, 0);
|
||||
// 3D textures are quickly massive in memory size, so we don't bother trying to do more than one layer at once.
|
||||
for z in 0..depth {
|
||||
// May need multiple copies for each subresource!
|
||||
// We assume that we never need to split a row. Back of the envelope calculation tells us a 512kb byte buffer is enough for this for most extreme known cases.
|
||||
// max_texture_width * max_pixel_size = 32768 * 16 = 512kb
|
||||
let mut num_rows_left = mip_size.height;
|
||||
while num_rows_left > 0 {
|
||||
let num_rows = num_rows_left.min(max_rows_per_copy);
|
||||
|
||||
*src_location.u.PlacedFootprint_mut() =
|
||||
d3d12::D3D12_PLACED_SUBRESOURCE_FOOTPRINT {
|
||||
Offset: 0,
|
||||
Footprint: d3d12::D3D12_SUBRESOURCE_FOOTPRINT {
|
||||
Format: raw_format,
|
||||
Width: mip_size.width,
|
||||
Height: num_rows,
|
||||
Depth: 1,
|
||||
RowPitch: bytes_per_row,
|
||||
},
|
||||
};
|
||||
|
||||
list.CopyTextureRegion(
|
||||
&dst_location,
|
||||
0,
|
||||
mip_size.height - num_rows_left,
|
||||
z,
|
||||
&src_location,
|
||||
std::ptr::null(),
|
||||
);
|
||||
num_rows_left -= num_rows;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn copy_buffer_to_buffer<T>(
|
||||
&mut self,
|
||||
src: &super::Buffer,
|
||||
|
||||
@@ -120,7 +120,7 @@ impl<T> HResult<T> for (T, i32) {
|
||||
|
||||
// Limited by D3D12's root signature size of 64. Each element takes 1 or 2 entries.
|
||||
const MAX_ROOT_ELEMENTS: usize = 64;
|
||||
const ZERO_BUFFER_SIZE: wgt::BufferAddress = 512 << 10;
|
||||
const ZERO_BUFFER_SIZE: wgt::BufferAddress = 256 << 10;
|
||||
|
||||
pub struct Instance {
|
||||
factory: native::Factory4,
|
||||
|
||||
@@ -255,13 +255,6 @@ impl crate::CommandEncoder<Api> for Encoder {
|
||||
|
||||
unsafe fn clear_buffer(&mut self, buffer: &Resource, range: crate::MemoryRange) {}
|
||||
|
||||
unsafe fn clear_texture(
|
||||
&mut self,
|
||||
texture: &Resource,
|
||||
subresource_range: &wgt::ImageSubresourceRange,
|
||||
) {
|
||||
}
|
||||
|
||||
unsafe fn copy_buffer_to_buffer<T>(&mut self, src: &Resource, dst: &Resource, regions: T) {}
|
||||
|
||||
unsafe fn copy_texture_to_texture<T>(
|
||||
|
||||
@@ -244,19 +244,6 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
|
||||
});
|
||||
}
|
||||
|
||||
unsafe fn clear_texture(
|
||||
&mut self,
|
||||
texture: &super::Texture,
|
||||
subresource_range: &wgt::ImageSubresourceRange,
|
||||
) {
|
||||
let (dst, dst_target) = texture.inner.as_native();
|
||||
self.cmd_buffer.commands.push(C::ClearTexture {
|
||||
dst,
|
||||
dst_target,
|
||||
subresource_range: subresource_range.clone(),
|
||||
});
|
||||
}
|
||||
|
||||
unsafe fn copy_buffer_to_buffer<T>(
|
||||
&mut self,
|
||||
src: &super::Buffer,
|
||||
|
||||
@@ -570,12 +570,6 @@ enum Command {
|
||||
dst_target: BindTarget,
|
||||
range: crate::MemoryRange,
|
||||
},
|
||||
#[allow(unused)] // yet unimplemented
|
||||
ClearTexture {
|
||||
dst: glow::Texture,
|
||||
dst_target: BindTarget,
|
||||
subresource_range: wgt::ImageSubresourceRange,
|
||||
},
|
||||
CopyBufferToBuffer {
|
||||
src: glow::Buffer,
|
||||
src_target: BindTarget,
|
||||
|
||||
@@ -213,20 +213,6 @@ impl super::Queue {
|
||||
dst_offset += size;
|
||||
}
|
||||
}
|
||||
C::ClearTexture {
|
||||
dst: _,
|
||||
dst_target: _,
|
||||
subresource_range: _,
|
||||
} => {
|
||||
// Should EXT_clear_texture when possible.
|
||||
// https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_clear_texture.txt
|
||||
// But support is not very widespread. Need to fallback to do zero_buffer copies
|
||||
|
||||
// TODO: Need to invoke calls into CopyBufferToTexture using zero_buffer.
|
||||
// To do that determine how many rows zero_buffer can fill and then chunk the texture up
|
||||
// (do *not* repeat the exact logic of CopyBufferToTexture, it's way too much!)
|
||||
//unimplemented!("texture clearing for GLES is not implemented yet");
|
||||
}
|
||||
C::CopyBufferToBuffer {
|
||||
src,
|
||||
src_target,
|
||||
|
||||
@@ -366,13 +366,6 @@ pub trait CommandEncoder<A: Api>: Send + Sync {
|
||||
|
||||
unsafe fn clear_buffer(&mut self, buffer: &A::Buffer, range: MemoryRange);
|
||||
|
||||
// Does not support depth/stencil or multisampled textures
|
||||
unsafe fn clear_texture(
|
||||
&mut self,
|
||||
texture: &A::Texture,
|
||||
subresource_range: &wgt::ImageSubresourceRange,
|
||||
);
|
||||
|
||||
unsafe fn copy_buffer_to_buffer<T>(&mut self, src: &A::Buffer, dst: &A::Buffer, regions: T)
|
||||
where
|
||||
T: Iterator<Item = BufferCopy>;
|
||||
|
||||
@@ -132,74 +132,6 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
|
||||
encoder.fill_buffer(&buffer.raw, conv::map_range(&range), 0);
|
||||
}
|
||||
|
||||
unsafe fn clear_texture(
|
||||
&mut self,
|
||||
texture: &super::Texture,
|
||||
subresource_range: &wgt::ImageSubresourceRange,
|
||||
) {
|
||||
let shared = self.shared.clone();
|
||||
let encoder = self.enter_blit();
|
||||
|
||||
let format_desc = texture.format.describe();
|
||||
|
||||
let mip_range = subresource_range.base_mip_level..match subresource_range.mip_level_count {
|
||||
Some(c) => subresource_range.base_mip_level + c.get(),
|
||||
None => texture.mip_levels,
|
||||
};
|
||||
let array_range = subresource_range.base_array_layer
|
||||
..match subresource_range.array_layer_count {
|
||||
Some(c) => subresource_range.base_array_layer + c.get(),
|
||||
None => texture.array_layers,
|
||||
};
|
||||
|
||||
for mip_level in mip_range {
|
||||
// Note that Metal requires this only to be a multiple of the pixel size, not some other constant like in other APIs.
|
||||
let mip_size = texture.copy_size.at_mip_level(mip_level);
|
||||
let bytes_per_row = mip_size.width as u64 / format_desc.block_dimensions.0 as u64
|
||||
* format_desc.block_size as u64;
|
||||
let max_rows_per_copy = super::ZERO_BUFFER_SIZE / bytes_per_row;
|
||||
// round down to a multiple of rows needed by the texture format
|
||||
let max_rows_per_copy = max_rows_per_copy / format_desc.block_dimensions.1 as u64
|
||||
* format_desc.block_dimensions.1 as u64;
|
||||
assert!(max_rows_per_copy > 0, "Zero buffer size is too small to fill a single row of a texture of type {:?}, size {:?} and format {:?}",
|
||||
texture.raw_type, texture.copy_size, texture.format);
|
||||
|
||||
for array_layer in array_range.clone() {
|
||||
// 3D textures are quickly massive in memory size, so we don't bother trying to do more than one layer at once.
|
||||
for z in 0..mip_size.depth as u64 {
|
||||
// May need multiple copies for each subresource! We assume that we never need to split a row.
|
||||
let mut num_rows_left = mip_size.height as u64;
|
||||
while num_rows_left > 0 {
|
||||
let num_rows = num_rows_left.min(max_rows_per_copy);
|
||||
let source_size = mtl::MTLSize {
|
||||
width: mip_size.width as u64,
|
||||
height: num_rows,
|
||||
depth: 1,
|
||||
};
|
||||
let destination_origion = mtl::MTLOrigin {
|
||||
x: 0,
|
||||
y: mip_size.height as u64 - num_rows_left,
|
||||
z,
|
||||
};
|
||||
encoder.copy_from_buffer_to_texture(
|
||||
&shared.zero_buffer,
|
||||
0,
|
||||
bytes_per_row,
|
||||
bytes_per_row * num_rows,
|
||||
source_size,
|
||||
&texture.raw,
|
||||
array_layer as u64,
|
||||
mip_level as u64,
|
||||
destination_origion,
|
||||
mtl::MTLBlitOption::empty(),
|
||||
);
|
||||
num_rows_left -= num_rows;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn copy_buffer_to_buffer<T>(
|
||||
&mut self,
|
||||
src: &super::Buffer,
|
||||
|
||||
@@ -284,7 +284,6 @@ impl crate::Device<super::Api> for super::Device {
|
||||
|
||||
Ok(super::Texture {
|
||||
raw,
|
||||
format: desc.format,
|
||||
raw_format: mtl_format,
|
||||
raw_type: mtl_type,
|
||||
mip_levels: desc.mip_level_count,
|
||||
|
||||
@@ -238,17 +238,11 @@ struct Settings {
|
||||
retain_command_buffer_references: bool,
|
||||
}
|
||||
|
||||
// Using max copyable texture row
|
||||
// https://developer.apple.com/documentation/metal/mtlblitcommandencoder/1400752-copyfrombuffer?language=objc
|
||||
// "The value must be less than or equal to 32767 multiplied by the destination texture’s pixel size."
|
||||
const ZERO_BUFFER_SIZE: wgt::BufferAddress = 32767 * 16; // 512kb
|
||||
|
||||
struct AdapterShared {
|
||||
device: Mutex<mtl::Device>,
|
||||
disabilities: PrivateDisabilities,
|
||||
private_caps: PrivateCapabilities,
|
||||
settings: Settings,
|
||||
zero_buffer: mtl::Buffer,
|
||||
}
|
||||
|
||||
unsafe impl Send for AdapterShared {}
|
||||
@@ -259,20 +253,11 @@ impl AdapterShared {
|
||||
let private_caps = PrivateCapabilities::new(&device);
|
||||
log::debug!("{:#?}", private_caps);
|
||||
|
||||
// buffers created this way are zero initialized
|
||||
// see https://developer.apple.com/documentation/metal/mtldevice/1433375-newbufferwithlength?language=objc
|
||||
let zero_buffer = device.new_buffer(
|
||||
ZERO_BUFFER_SIZE,
|
||||
mtl::MTLResourceOptions::CPUCacheModeWriteCombined
|
||||
| mtl::MTLResourceOptions::StorageModePrivate,
|
||||
);
|
||||
|
||||
Self {
|
||||
disabilities: PrivateDisabilities::new(&device),
|
||||
private_caps: PrivateCapabilities::new(&device),
|
||||
device: Mutex::new(device),
|
||||
settings: Settings::default(),
|
||||
zero_buffer,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -296,7 +281,6 @@ pub struct Device {
|
||||
pub struct Surface {
|
||||
view: Option<NonNull<objc::runtime::Object>>,
|
||||
render_layer: Mutex<mtl::MetalLayer>,
|
||||
swapchain_format: wgt::TextureFormat,
|
||||
raw_swapchain_format: mtl::MTLPixelFormat,
|
||||
extent: wgt::Extent3d,
|
||||
main_thread_id: thread::ThreadId,
|
||||
@@ -422,7 +406,6 @@ impl Buffer {
|
||||
#[derive(Debug)]
|
||||
pub struct Texture {
|
||||
raw: mtl::Texture,
|
||||
format: wgt::TextureFormat,
|
||||
raw_format: mtl::MTLPixelFormat,
|
||||
raw_type: mtl::MTLTextureType,
|
||||
array_layers: u32,
|
||||
|
||||
@@ -60,7 +60,6 @@ impl super::Surface {
|
||||
Self {
|
||||
view,
|
||||
render_layer: Mutex::new(layer),
|
||||
swapchain_format: wgt::TextureFormat::Bgra8UnormSrgb, // no value invalid, pick something not too far-fetched
|
||||
raw_swapchain_format: mtl::MTLPixelFormat::Invalid,
|
||||
extent: wgt::Extent3d::default(),
|
||||
main_thread_id: thread::current().id(),
|
||||
@@ -211,7 +210,6 @@ impl crate::Surface<super::Api> for super::Surface {
|
||||
log::info!("build swapchain {:?}", config);
|
||||
|
||||
let caps = &device.shared.private_caps;
|
||||
self.swapchain_format = config.format;
|
||||
self.raw_swapchain_format = caps.map_format(config.format);
|
||||
self.extent = config.extent;
|
||||
|
||||
@@ -279,7 +277,6 @@ impl crate::Surface<super::Api> for super::Surface {
|
||||
let suf_texture = super::SurfaceTexture {
|
||||
texture: super::Texture {
|
||||
raw: texture,
|
||||
format: self.swapchain_format,
|
||||
raw_format: self.raw_swapchain_format,
|
||||
raw_type: mtl::MTLTextureType::D2,
|
||||
array_layers: 1,
|
||||
|
||||
@@ -194,38 +194,6 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
|
||||
);
|
||||
}
|
||||
|
||||
unsafe fn clear_texture(
|
||||
&mut self,
|
||||
texture: &super::Texture,
|
||||
subresource_range: &wgt::ImageSubresourceRange,
|
||||
) {
|
||||
self.device.raw.cmd_clear_color_image(
|
||||
self.active,
|
||||
texture.raw,
|
||||
DST_IMAGE_LAYOUT,
|
||||
&vk::ClearColorValue {
|
||||
float32: [0.0, 0.0, 0.0, 0.0],
|
||||
},
|
||||
&[conv::map_subresource_range(
|
||||
subresource_range,
|
||||
texture.aspects,
|
||||
)],
|
||||
);
|
||||
|
||||
// The Vulkan api could easily support depth/stencil formats for clearing as well.
|
||||
// But in other APIs this is more challenging which is why clear_texture excludes support for these formats.
|
||||
// self.device.raw.cmd_clear_depth_stencil_image(
|
||||
// self.active,
|
||||
// texture.raw,
|
||||
// DST_IMAGE_LAYOUT,
|
||||
// &vk::ClearDepthStencilValue {
|
||||
// depth: 0.0,
|
||||
// stencil: 0,
|
||||
// },
|
||||
// &[range],
|
||||
// );
|
||||
}
|
||||
|
||||
unsafe fn copy_buffer_to_buffer<T>(
|
||||
&mut self,
|
||||
src: &super::Buffer,
|
||||
|
||||
@@ -0,0 +1,245 @@
|
||||
use crate::common::{initialize_test, TestParameters, TestingContext};
|
||||
|
||||
static TEXTURE_FORMATS_UNCOMPRESSED: &[wgpu::TextureFormat] = &[
|
||||
wgpu::TextureFormat::R8Unorm,
|
||||
wgpu::TextureFormat::R8Snorm,
|
||||
wgpu::TextureFormat::R8Uint,
|
||||
wgpu::TextureFormat::R8Sint,
|
||||
wgpu::TextureFormat::R16Uint,
|
||||
wgpu::TextureFormat::R16Sint,
|
||||
wgpu::TextureFormat::R16Float,
|
||||
wgpu::TextureFormat::Rg8Unorm,
|
||||
wgpu::TextureFormat::Rg8Snorm,
|
||||
wgpu::TextureFormat::Rg8Uint,
|
||||
wgpu::TextureFormat::Rg8Sint,
|
||||
wgpu::TextureFormat::R32Uint,
|
||||
wgpu::TextureFormat::R32Sint,
|
||||
wgpu::TextureFormat::R32Float,
|
||||
wgpu::TextureFormat::Rg16Uint,
|
||||
wgpu::TextureFormat::Rg16Sint,
|
||||
wgpu::TextureFormat::Rg16Float,
|
||||
wgpu::TextureFormat::Rgba8Unorm,
|
||||
wgpu::TextureFormat::Rgba8UnormSrgb,
|
||||
wgpu::TextureFormat::Rgba8Snorm,
|
||||
wgpu::TextureFormat::Rgba8Uint,
|
||||
wgpu::TextureFormat::Rgba8Sint,
|
||||
wgpu::TextureFormat::Bgra8Unorm,
|
||||
wgpu::TextureFormat::Bgra8UnormSrgb,
|
||||
wgpu::TextureFormat::Rgb10a2Unorm,
|
||||
wgpu::TextureFormat::Rg11b10Float,
|
||||
wgpu::TextureFormat::Rg32Uint,
|
||||
wgpu::TextureFormat::Rg32Sint,
|
||||
wgpu::TextureFormat::Rg32Float,
|
||||
wgpu::TextureFormat::Rgba16Uint,
|
||||
wgpu::TextureFormat::Rgba16Sint,
|
||||
wgpu::TextureFormat::Rgba16Float,
|
||||
wgpu::TextureFormat::Rgba32Uint,
|
||||
wgpu::TextureFormat::Rgba32Sint,
|
||||
wgpu::TextureFormat::Rgba32Float,
|
||||
wgpu::TextureFormat::Depth32Float,
|
||||
wgpu::TextureFormat::Depth24Plus,
|
||||
wgpu::TextureFormat::Depth24PlusStencil8,
|
||||
wgpu::TextureFormat::Rgb9e5Ufloat,
|
||||
];
|
||||
|
||||
// needs TEXTURE_COMPRESSION_BC
|
||||
static TEXTURE_FORMATS_BC: &[wgpu::TextureFormat] = &[
|
||||
wgpu::TextureFormat::Bc1RgbaUnorm,
|
||||
wgpu::TextureFormat::Bc1RgbaUnormSrgb,
|
||||
wgpu::TextureFormat::Bc2RgbaUnorm,
|
||||
wgpu::TextureFormat::Bc2RgbaUnormSrgb,
|
||||
wgpu::TextureFormat::Bc3RgbaUnorm,
|
||||
wgpu::TextureFormat::Bc3RgbaUnormSrgb,
|
||||
wgpu::TextureFormat::Bc4RUnorm,
|
||||
wgpu::TextureFormat::Bc4RSnorm,
|
||||
wgpu::TextureFormat::Bc5RgUnorm,
|
||||
wgpu::TextureFormat::Bc5RgSnorm,
|
||||
wgpu::TextureFormat::Bc6hRgbUfloat,
|
||||
wgpu::TextureFormat::Bc6hRgbSfloat,
|
||||
wgpu::TextureFormat::Bc7RgbaUnorm,
|
||||
wgpu::TextureFormat::Bc7RgbaUnormSrgb,
|
||||
];
|
||||
|
||||
// needs TEXTURE_COMPRESSION_ETC2
|
||||
static TEXTURE_FORMATS_ETC2: &[wgpu::TextureFormat] = &[
|
||||
wgpu::TextureFormat::Etc2RgbUnorm,
|
||||
wgpu::TextureFormat::Etc2RgbUnormSrgb,
|
||||
wgpu::TextureFormat::Etc2RgbA1Unorm,
|
||||
wgpu::TextureFormat::Etc2RgbA1UnormSrgb,
|
||||
wgpu::TextureFormat::EacRUnorm,
|
||||
wgpu::TextureFormat::EacRSnorm,
|
||||
wgpu::TextureFormat::EacRgUnorm,
|
||||
wgpu::TextureFormat::EacRgSnorm,
|
||||
];
|
||||
|
||||
// needs TEXTURE_COMPRESSION_ASTC_LDR
|
||||
static TEXTURE_FORMATS_ASTC: &[wgpu::TextureFormat] = &[
|
||||
wgpu::TextureFormat::Astc4x4RgbaUnorm,
|
||||
wgpu::TextureFormat::Astc4x4RgbaUnormSrgb,
|
||||
wgpu::TextureFormat::Astc5x4RgbaUnorm,
|
||||
wgpu::TextureFormat::Astc5x4RgbaUnormSrgb,
|
||||
wgpu::TextureFormat::Astc5x5RgbaUnorm,
|
||||
wgpu::TextureFormat::Astc5x5RgbaUnormSrgb,
|
||||
wgpu::TextureFormat::Astc6x5RgbaUnorm,
|
||||
wgpu::TextureFormat::Astc6x5RgbaUnormSrgb,
|
||||
wgpu::TextureFormat::Astc6x6RgbaUnorm,
|
||||
wgpu::TextureFormat::Astc6x6RgbaUnormSrgb,
|
||||
wgpu::TextureFormat::Astc8x5RgbaUnorm,
|
||||
wgpu::TextureFormat::Astc8x5RgbaUnormSrgb,
|
||||
wgpu::TextureFormat::Astc8x6RgbaUnorm,
|
||||
wgpu::TextureFormat::Astc8x6RgbaUnormSrgb,
|
||||
wgpu::TextureFormat::Astc10x5RgbaUnorm,
|
||||
wgpu::TextureFormat::Astc10x5RgbaUnormSrgb,
|
||||
wgpu::TextureFormat::Astc10x6RgbaUnorm,
|
||||
wgpu::TextureFormat::Astc10x6RgbaUnormSrgb,
|
||||
wgpu::TextureFormat::Astc8x8RgbaUnorm,
|
||||
wgpu::TextureFormat::Astc8x8RgbaUnormSrgb,
|
||||
wgpu::TextureFormat::Astc10x8RgbaUnorm,
|
||||
wgpu::TextureFormat::Astc10x8RgbaUnormSrgb,
|
||||
wgpu::TextureFormat::Astc10x10RgbaUnorm,
|
||||
wgpu::TextureFormat::Astc10x10RgbaUnormSrgb,
|
||||
wgpu::TextureFormat::Astc12x10RgbaUnorm,
|
||||
wgpu::TextureFormat::Astc12x10RgbaUnormSrgb,
|
||||
wgpu::TextureFormat::Astc12x12RgbaUnorm,
|
||||
wgpu::TextureFormat::Astc12x12RgbaUnormSrgb,
|
||||
];
|
||||
|
||||
fn single_texture_clear_test(
|
||||
ctx: &TestingContext,
|
||||
format: wgpu::TextureFormat,
|
||||
size: wgpu::Extent3d,
|
||||
dimension: wgpu::TextureDimension,
|
||||
) {
|
||||
// clear_texture not supported for depth textures.
|
||||
if format.describe().sample_type == wgpu::TextureSampleType::Depth {
|
||||
return;
|
||||
}
|
||||
|
||||
println!("clearing texture with {:?}", format);
|
||||
|
||||
let texture = ctx.device.create_texture(&wgpu::TextureDescriptor {
|
||||
label: Some(&format!("texture {:?}", format)),
|
||||
size,
|
||||
mip_level_count: if dimension == wgpu::TextureDimension::D1 {
|
||||
1
|
||||
} else {
|
||||
// arbitrary value between 2 and max
|
||||
3
|
||||
},
|
||||
sample_count: 1, // multisampling is not supported for clear
|
||||
dimension,
|
||||
format,
|
||||
usage: wgpu::TextureUsages::COPY_DST,
|
||||
});
|
||||
let mut encoder = ctx
|
||||
.device
|
||||
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
|
||||
encoder.clear_texture(
|
||||
&texture,
|
||||
&wgpu::ImageSubresourceRange {
|
||||
aspect: wgpu::TextureAspect::All,
|
||||
base_mip_level: 0,
|
||||
mip_level_count: None,
|
||||
base_array_layer: 0,
|
||||
array_layer_count: None,
|
||||
},
|
||||
);
|
||||
ctx.queue.submit([encoder.finish()]);
|
||||
|
||||
// TODO: Read back and check zeroness?
|
||||
}
|
||||
|
||||
fn clear_texture_tests(ctx: &TestingContext, formats: &[wgpu::TextureFormat], supports_1d: bool) {
|
||||
for &format in formats {
|
||||
// 1D texture
|
||||
if supports_1d {
|
||||
single_texture_clear_test(
|
||||
ctx,
|
||||
format,
|
||||
wgpu::Extent3d {
|
||||
width: 64,
|
||||
height: 1,
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
wgpu::TextureDimension::D1,
|
||||
);
|
||||
}
|
||||
// 2D texture
|
||||
single_texture_clear_test(
|
||||
ctx,
|
||||
format,
|
||||
wgpu::Extent3d {
|
||||
width: 64,
|
||||
height: 64,
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
wgpu::TextureDimension::D2,
|
||||
);
|
||||
// 2D array texture
|
||||
single_texture_clear_test(
|
||||
ctx,
|
||||
format,
|
||||
wgpu::Extent3d {
|
||||
width: 64,
|
||||
height: 64,
|
||||
depth_or_array_layers: 4,
|
||||
},
|
||||
wgpu::TextureDimension::D2,
|
||||
);
|
||||
// volume texture
|
||||
single_texture_clear_test(
|
||||
ctx,
|
||||
format,
|
||||
wgpu::Extent3d {
|
||||
width: 16,
|
||||
height: 16,
|
||||
depth_or_array_layers: 16,
|
||||
},
|
||||
wgpu::TextureDimension::D3,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clear_texture_2d_uncompressed() {
|
||||
initialize_test(
|
||||
TestParameters::default().features(wgpu::Features::CLEAR_COMMANDS),
|
||||
|ctx| {
|
||||
clear_texture_tests(&ctx, TEXTURE_FORMATS_UNCOMPRESSED, true);
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clear_texture_2d_bc() {
|
||||
initialize_test(
|
||||
TestParameters::default()
|
||||
.features(wgpu::Features::CLEAR_COMMANDS | wgpu::Features::TEXTURE_COMPRESSION_BC),
|
||||
|ctx| {
|
||||
clear_texture_tests(&ctx, TEXTURE_FORMATS_BC, false);
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clear_texture_2d_astc() {
|
||||
initialize_test(
|
||||
TestParameters::default().features(
|
||||
wgpu::Features::CLEAR_COMMANDS | wgpu::Features::TEXTURE_COMPRESSION_ASTC_LDR,
|
||||
),
|
||||
|ctx| {
|
||||
clear_texture_tests(&ctx, TEXTURE_FORMATS_ASTC, false);
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clear_texture_2d_etc2() {
|
||||
initialize_test(
|
||||
TestParameters::default()
|
||||
.features(wgpu::Features::CLEAR_COMMANDS | wgpu::Features::TEXTURE_COMPRESSION_ETC2),
|
||||
|ctx| {
|
||||
clear_texture_tests(&ctx, TEXTURE_FORMATS_ETC2, false);
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
// All files containing tests
|
||||
mod common;
|
||||
|
||||
mod clear_texture;
|
||||
mod device;
|
||||
mod example_wgsl;
|
||||
mod instance;
|
||||
|
||||
Reference in new issue
Block a user