Skip to content

Commit

Permalink
[mtl 2D texel buffer view
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Aug 24, 2018
1 parent 3273a05 commit 4f7100f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/backend/dx11/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ impl hal::Instance for Instance {

let limits = hal::Limits {
max_texture_size: d3d11::D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION as _,
max_texel_elements: d3d11::D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION as _, //TODO
max_patch_size: 0, // TODO
max_viewports: d3d11::D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE as _,
max_compute_group_count: [
Expand Down
1 change: 1 addition & 0 deletions src/backend/dx12/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,7 @@ impl hal::Instance for Instance {
if depth_bounds_test_supported { Features::DEPTH_BOUNDS } else { Features::empty() },
limits: Limits { // TODO
max_texture_size: 0,
max_texel_elements: 0,
max_patch_size: 0,
max_viewports: 0,
max_compute_group_count: [
Expand Down
1 change: 1 addition & 0 deletions src/backend/gl/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ pub fn query_all(gl: &gl::Gl) -> (Info, Features, LegacyFeatures, Limits, Privat

let mut limits = Limits {
max_texture_size: get_usize(gl, gl::MAX_TEXTURE_SIZE),
max_texel_elements: get_usize(gl, gl::MAX_TEXTURE_BUFFER_SIZE),
max_viewports: 1,
min_buffer_copy_offset_alignment: 1,
min_buffer_copy_pitch_alignment: 1,
Expand Down
26 changes: 15 additions & 11 deletions src/backend/metal/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,6 @@ const BASE_INSTANCE_SUPPORT: &[MTLFeatureSet] = &[
const PUSH_CONSTANTS_DESC_SET: u32 = !0;
const PUSH_CONSTANTS_DESC_BINDING: u32 = 0;

//The offset and bytesPerRow parameters must be byte aligned to the size returned by the
// minimumLinearTextureAlignmentForPixelFormat: method. The bytesPerRow parameter must also be
// greater than or equal to the size of one pixel, in bytes, multiplied by the pixel width of one row.
const STRIDE_MASK: u64 = 0xFF;

/// Emit error during shader module parsing.
fn gen_parse_error(err: SpirvErrorCode) -> ShaderError {
Expand Down Expand Up @@ -382,6 +378,7 @@ impl PhysicalDevice {
} else {
1 << 28 // 256MB otherwise
},
max_texture_size: 4096, //TODO
}
};

Expand Down Expand Up @@ -523,7 +520,8 @@ impl hal::PhysicalDevice<Backend> for PhysicalDevice {

fn limits(&self) -> hal::Limits {
hal::Limits {
max_texture_size: 4096, // TODO: feature set
max_texture_size: self.private_caps.max_texture_size as usize,
max_texel_elements: (self.private_caps.max_texture_size * self.private_caps.max_texture_size) as usize,
max_patch_size: 0, // No tessellation

// Note: The maximum number of supported viewports and scissor rectangles varies by device.
Expand Down Expand Up @@ -1957,22 +1955,27 @@ impl hal::Device<Backend> for Device {
// Vadlidator says "Linear texture: cannot create compressed, depth, or stencil textures"
return Err(buffer::ViewCreationError::UnsupportedFormat { format: format_maybe })
}
let block_count = (end_rough - start) * 8 / format_desc.bits as u64;

//Note: we rely on SPIRV-Cross to use the proper 2D texel indexing here
let texel_count = (end_rough - start) * 8 / format_desc.bits as u64;
let col_count = cmp::min(texel_count, self.private_caps.max_texture_size);
let row_count = cmp::max(1, texel_count / self.private_caps.max_texture_size);
let mtl_format = self.private_caps
.map_format(format)
.ok_or(buffer::ViewCreationError::UnsupportedFormat { format: format_maybe })?;

let descriptor = metal::TextureDescriptor::new();
descriptor.set_texture_type(MTLTextureType::D1);
descriptor.set_width(block_count);
descriptor.set_texture_type(MTLTextureType::D2);
descriptor.set_width(col_count);
descriptor.set_height(row_count);
descriptor.set_mipmap_level_count(1);
descriptor.set_pixel_format(mtl_format);
descriptor.set_resource_options(buffer.res_options);
descriptor.set_storage_mode(buffer.raw.storage_mode());
descriptor.set_usage(metal::MTLTextureUsage::ShaderRead);

let size = block_count * (format_desc.bits as u64 / 8);
let stride = (size + STRIDE_MASK) & !STRIDE_MASK;
let alignment = self.private_caps.buffer_alignment;
let stride = (col_count * (format_desc.bits as u64 / 8) + alignment - 1) & !alignment;

Ok(n::BufferView {
raw: buffer.raw.new_texture_from_contents(&descriptor, start, stride),
Expand Down Expand Up @@ -2165,7 +2168,8 @@ impl hal::Device<Backend> for Device {
},
n::MemoryHeap::Public(memory_type, ref cpu_buffer) => {
let row_size = image.kind.extent().width as u64 * (format_desc.bits as u64 / 8);
let stride = (row_size + STRIDE_MASK) & !STRIDE_MASK;
let alignment = self.private_caps.buffer_alignment;
let stride = (row_size + alignment - 1) & !alignment;

let (storage_mode, cache_mode) = MemoryTypes::describe(memory_type.0);
image.texture_desc.set_storage_mode(storage_mode);
Expand Down
1 change: 1 addition & 0 deletions src/backend/metal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ struct PrivateCapabilities {
max_samplers_per_stage: ResourceIndex,
buffer_alignment: u64,
max_buffer_size: u64,
max_texture_size: u64,
}

#[derive(Clone, Copy, Debug)]
Expand Down
1 change: 1 addition & 0 deletions src/backend/vulkan/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ impl hal::PhysicalDevice<Backend> for PhysicalDevice {

Limits {
max_texture_size: limits.max_image_dimension3d as _,
max_texel_elements: limits.max_texel_buffer_elements as _,
max_patch_size: limits.max_tessellation_patch_size as PatchSize,
max_viewports: limits.max_viewports as _,
max_compute_group_count: [max_group_count[0] as _, max_group_count[1] as _, max_group_count[2] as _],
Expand Down
2 changes: 2 additions & 0 deletions src/hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ bitflags! {
pub struct Limits {
/// Maximum supported texture size.
pub max_texture_size: usize,
/// Maximum number of elements for the BufferView to see.
pub max_texel_elements: usize,
/// Maximum number of vertices for each patch.
pub max_patch_size: PatchSize,
/// Maximum number of viewports.
Expand Down

0 comments on commit 4f7100f

Please sign in to comment.