harness: textures, sampler states, and two sampling workloads
The harness timed no sampling kernel. Plans now make textures (2D, RGBA16Float, one level, Private, ShaderRead|ShaderWrite), bind them with setTexture:atIndex:, and bind sampler states (linear or nearest, normalized, clamped to the edge) made once per plan. The harness fills a texture from a Shared buffer and reads one back with its own kernels (texels_in, texels_out), made only for a plan with a texture, so a session that runs none makes the pipelines it made before. Two workloads: sampling, a 2x downsample of a 2048x2048 texture through a runtime sampler, and constexpr_sampling, a 2x upsample of a 1024x1024 texture through a constexpr sampler; each one sample and one texture write a thread. Their inputs are sixteenths below 4, so under weights of halves and quarters every result is exact in half; the CPU reference allows two last-place steps of a half for the device's own filter and conversion. The old runtime runs both. The new runtime makes no texture or sampler state yet: its texture and sampler types have no value, and both workloads are typed refusals.
This commit is contained in:
1 parent
54618f54e9
commit
36295733ea
8 files changed
+531
-18
No files matched your search
@@ -3,8 +3,8 @@
|
||||
//! exact on any device but the ALU workload's, whose `float` chains are held to a tolerance.
|
||||
|
||||
use crate::plan::{
|
||||
seeded_words as words, Binding, BufferPlan, Constant, Dispatch, Expect, Grid, Plan, Source,
|
||||
Values,
|
||||
seeded_words as words, Binding, BufferPlan, Constant, Dispatch, Expect, Filter, Grid, Plan,
|
||||
Source, TextureExpect, TexturePlan, Values,
|
||||
};
|
||||
use crate::{Result, Storage};
|
||||
|
||||
@@ -19,7 +19,7 @@ pub struct Workload {
|
||||
}
|
||||
|
||||
/// The authored workloads.
|
||||
pub const WORKLOADS: [Workload; 9] = [
|
||||
pub const WORKLOADS: [Workload; 11] = [
|
||||
Workload {
|
||||
name: "bandwidth",
|
||||
plan: bandwidth,
|
||||
@@ -56,6 +56,14 @@ pub const WORKLOADS: [Workload; 9] = [
|
||||
name: "zero_products",
|
||||
plan: zero_products,
|
||||
},
|
||||
Workload {
|
||||
name: "sampling",
|
||||
plan: sampling,
|
||||
},
|
||||
Workload {
|
||||
name: "constexpr_sampling",
|
||||
plan: constexpr_sampling,
|
||||
},
|
||||
];
|
||||
|
||||
/// A creation workload: `Shared` buffers of one length made and released in turn, so that each
|
||||
@@ -117,6 +125,8 @@ fn bandwidth() -> Plan {
|
||||
buffer: 1,
|
||||
values: Values::Words(output),
|
||||
}],
|
||||
textures: Vec::new(),
|
||||
expect_textures: Vec::new(),
|
||||
outputs: Vec::new(),
|
||||
per_command_buffer: None,
|
||||
}
|
||||
@@ -155,6 +165,8 @@ fn alu() -> Plan {
|
||||
tolerance: 1e-4,
|
||||
},
|
||||
}],
|
||||
textures: Vec::new(),
|
||||
expect_textures: Vec::new(),
|
||||
outputs: Vec::new(),
|
||||
per_command_buffer: None,
|
||||
}
|
||||
@@ -187,6 +199,8 @@ fn threadgroup_scan() -> Plan {
|
||||
buffer: 1,
|
||||
values: Values::Words(output),
|
||||
}],
|
||||
textures: Vec::new(),
|
||||
expect_textures: Vec::new(),
|
||||
outputs: Vec::new(),
|
||||
per_command_buffer: None,
|
||||
}
|
||||
@@ -239,6 +253,8 @@ fn simd_reduce() -> Plan {
|
||||
values: Values::Words(vec![total, 0, 0, 0]),
|
||||
},
|
||||
],
|
||||
textures: Vec::new(),
|
||||
expect_textures: Vec::new(),
|
||||
outputs: Vec::new(),
|
||||
per_command_buffer: None,
|
||||
}
|
||||
@@ -292,6 +308,8 @@ fn image_filter() -> Plan {
|
||||
buffer: 1,
|
||||
values: Values::Words(output),
|
||||
}],
|
||||
textures: Vec::new(),
|
||||
expect_textures: Vec::new(),
|
||||
outputs: Vec::new(),
|
||||
per_command_buffer: None,
|
||||
}
|
||||
@@ -334,6 +352,8 @@ fn dispatch_chain() -> Plan {
|
||||
buffer: 0,
|
||||
values: Values::Words(output),
|
||||
}],
|
||||
textures: Vec::new(),
|
||||
expect_textures: Vec::new(),
|
||||
outputs: Vec::new(),
|
||||
per_command_buffer: None,
|
||||
}
|
||||
@@ -413,6 +433,8 @@ fn frame() -> Plan {
|
||||
buffer: 0,
|
||||
values: Values::Words(output),
|
||||
}],
|
||||
textures: Vec::new(),
|
||||
expect_textures: Vec::new(),
|
||||
outputs: Vec::new(),
|
||||
per_command_buffer: Some(STEPS / COMMAND_BUFFERS),
|
||||
}
|
||||
@@ -453,6 +475,8 @@ fn narrow_ints() -> Plan {
|
||||
buffer: 0,
|
||||
values: Values::Words(output),
|
||||
}],
|
||||
textures: Vec::new(),
|
||||
expect_textures: Vec::new(),
|
||||
outputs: Vec::new(),
|
||||
per_command_buffer: None,
|
||||
}
|
||||
@@ -505,6 +529,8 @@ fn zero_products() -> Plan {
|
||||
tolerance: 1e-4,
|
||||
},
|
||||
}],
|
||||
textures: Vec::new(),
|
||||
expect_textures: Vec::new(),
|
||||
outputs: Vec::new(),
|
||||
per_command_buffer: None,
|
||||
}
|
||||
@@ -523,3 +549,139 @@ fn buffer(index: u32, buffer: usize) -> Binding {
|
||||
fn bytes(index: u32, bytes: Vec<u8>) -> Binding {
|
||||
Binding::Bytes { index, bytes }
|
||||
}
|
||||
|
||||
/// `setTexture:atIndex:` of the plan's texture `texture`.
|
||||
fn texture(index: u32, texture: usize) -> Binding {
|
||||
Binding::Texture { index, texture }
|
||||
}
|
||||
|
||||
/// How far a sampled texel may be from its reference. A filter's weights and the conversion to
|
||||
/// `RGBA16Float` are the device's own; the sampling workloads' inputs make each result exact in
|
||||
/// `half` under weights of halves and quarters, so this allows two last-place steps of a `half`
|
||||
/// below 4.
|
||||
const TEXEL_TOLERANCE: f32 = 1.0 / 256.0;
|
||||
|
||||
/// A `side` by `side` texture's starting texels, four channels each, every one a sixteenth of a
|
||||
/// whole number below 64: exact in `half`.
|
||||
fn sixteenths(seed: u64, side: u32) -> Vec<f32> {
|
||||
let count = 4 * side as usize * side as usize;
|
||||
(words(seed, count).into_iter())
|
||||
.map(|word| (word & 63) as f32 / 16.0)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// One thread a texel of a `side` by `side` texture, in threadgroups of 16x16.
|
||||
fn texel_grid(side: u32) -> Grid {
|
||||
let tiles = u64::from(side / 16);
|
||||
Grid::Threadgroups {
|
||||
groups: [tiles, tiles, 1],
|
||||
threads: [16, 16, 1],
|
||||
}
|
||||
}
|
||||
|
||||
/// A sampling workload's plan: `function` from a `from`-sided texture of `source` texels into a
|
||||
/// `to`-sided one, which must then hold `expected`.
|
||||
fn sampling_plan(
|
||||
name: &str,
|
||||
function: &str,
|
||||
(from, source): (u32, Vec<f32>),
|
||||
(to, expected): (u32, Vec<f32>),
|
||||
sampler: Option<Filter>,
|
||||
) -> Plan {
|
||||
let mut bindings = vec![texture(0, 0), texture(1, 1)];
|
||||
bindings.extend(sampler.map(|filter| Binding::Sampler { index: 0, filter }));
|
||||
Plan {
|
||||
name: name.to_owned(),
|
||||
library: Source::Authored,
|
||||
buffers: Vec::new(),
|
||||
textures: vec![
|
||||
TexturePlan {
|
||||
width: from,
|
||||
height: from,
|
||||
texels: Some(source),
|
||||
},
|
||||
TexturePlan {
|
||||
width: to,
|
||||
height: to,
|
||||
texels: None,
|
||||
},
|
||||
],
|
||||
dispatches: vec![Dispatch {
|
||||
function: function.to_owned(),
|
||||
constants: Vec::new(),
|
||||
bindings,
|
||||
grid: texel_grid(to),
|
||||
}],
|
||||
expect: Vec::new(),
|
||||
expect_textures: vec![TextureExpect {
|
||||
texture: 1,
|
||||
values: Values::Floats {
|
||||
values: expected,
|
||||
tolerance: TEXEL_TOLERANCE,
|
||||
},
|
||||
}],
|
||||
outputs: Vec::new(),
|
||||
per_command_buffer: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Sampling through a runtime sampler (`setSamplerState:atIndex:`, linear, clamped to the edge):
|
||||
/// a 2x downsample of a 2048x2048 `RGBA16Float` texture into a 1024x1024 one (`sample_down`),
|
||||
/// each texel one sample at the corner its four source texels share, one texture write a thread.
|
||||
/// Each filter weight is one half, so each texel is the mean of its four source texels.
|
||||
fn sampling() -> Plan {
|
||||
const SIDE: u32 = 2048;
|
||||
let source = sixteenths(15, SIDE);
|
||||
let half = SIDE / 2;
|
||||
let at = |x: u32, y: u32, channel: u32| source[(4 * (y * SIDE + x) + channel) as usize];
|
||||
let expected = (0..half)
|
||||
.flat_map(|y| (0..half).flat_map(move |x| (0..4).map(move |c| (x, y, c))))
|
||||
.map(|(x, y, c)| {
|
||||
let (x, y) = (2 * x, 2 * y);
|
||||
(at(x, y, c) + at(x + 1, y, c) + at(x, y + 1, c) + at(x + 1, y + 1, c)) / 4.0
|
||||
})
|
||||
.collect();
|
||||
sampling_plan(
|
||||
"sampling",
|
||||
"sample_down",
|
||||
(SIDE, source),
|
||||
(half, expected),
|
||||
Some(Filter::Linear),
|
||||
)
|
||||
}
|
||||
|
||||
/// Sampling through a constexpr sampler (linear, clamped to the edge): a 2x upsample of a
|
||||
/// 1024x1024 `RGBA16Float` texture into a 2048x2048 one (`sample_up`), each texel one sample a
|
||||
/// quarter of a source texel from a source texel's center, one texture write a thread. Each
|
||||
/// filter weight is a quarter or three quarters.
|
||||
fn constexpr_sampling() -> Plan {
|
||||
const SIDE: u32 = 1024;
|
||||
let source = sixteenths(16, SIDE);
|
||||
// A destination coordinate's two source texels along its axis, and the second's weight: the
|
||||
// sample is at `x / 2 - 1/4` source texels from the first texel's center, clamped to the edge.
|
||||
let taps = |x: u32| {
|
||||
let k = x / 2;
|
||||
if x % 2 == 0 {
|
||||
(k.saturating_sub(1), k, 0.75f32)
|
||||
} else {
|
||||
(k, (k + 1).min(SIDE - 1), 0.25f32)
|
||||
}
|
||||
};
|
||||
let at = |x: u32, y: u32, channel: u32| source[(4 * (y * SIDE + x) + channel) as usize];
|
||||
let double = 2 * SIDE;
|
||||
let expected = (0..double)
|
||||
.flat_map(|y| (0..double).flat_map(move |x| (0..4).map(move |c| (x, y, c))))
|
||||
.map(|(x, y, c)| {
|
||||
let ((x0, x1, wx), (y0, y1, wy)) = (taps(x), taps(y));
|
||||
let row = |y| at(x0, y, c) * (1.0 - wx) + at(x1, y, c) * wx;
|
||||
row(y0) * (1.0 - wy) + row(y1) * wy
|
||||
})
|
||||
.collect();
|
||||
sampling_plan(
|
||||
"constexpr_sampling",
|
||||
"sample_up",
|
||||
(SIDE, source),
|
||||
(double, expected),
|
||||
None,
|
||||
)
|
||||
}
|
||||
@@ -88,6 +88,8 @@ impl Described {
|
||||
buffers: Vec::new(),
|
||||
dispatches: Vec::new(),
|
||||
expect: Vec::new(),
|
||||
textures: Vec::new(),
|
||||
expect_textures: Vec::new(),
|
||||
outputs: Vec::new(),
|
||||
per_command_buffer: None,
|
||||
};
|
||||
|
||||
@@ -197,6 +197,10 @@ pub trait ComputeDevice: Device {
|
||||
type CommandBuffer: CommandBuffer<Self>;
|
||||
/// `MTLComputeCommandEncoder`.
|
||||
type ComputeCommandEncoder: ComputeCommandEncoder<Self>;
|
||||
/// `MTLTexture`.
|
||||
type Texture;
|
||||
/// `MTLSamplerState`.
|
||||
type SamplerState;
|
||||
|
||||
/// `newLibraryWithData:error:` of a metallib; of AIR bitcode, the translator's library.
|
||||
fn new_library(&self, code: Code<'_>) -> Result<Self::Library>;
|
||||
@@ -209,6 +213,14 @@ pub trait ComputeDevice: Device {
|
||||
|
||||
/// `newCommandQueue`.
|
||||
fn new_command_queue(&self) -> Result<Self::CommandQueue>;
|
||||
|
||||
/// `newTextureWithDescriptor:` of a [`TexturePlan`](plan::TexturePlan)'s texture: 2D,
|
||||
/// `RGBA16Float`, one level, `Private`, of `ShaderRead | ShaderWrite` usage.
|
||||
fn new_texture(&self, width: u32, height: u32) -> Result<Self::Texture>;
|
||||
|
||||
/// `newSamplerStateWithDescriptor:` of `filter`'s min and mag filter, normalized coordinates
|
||||
/// and every address mode clamped to the edge.
|
||||
fn new_sampler_state(&self, filter: plan::Filter) -> Result<Self::SamplerState>;
|
||||
}
|
||||
|
||||
/// `MTLBuffer`.
|
||||
@@ -259,6 +271,12 @@ pub trait ComputeCommandEncoder<D: ComputeDevice> {
|
||||
/// `setBytes:length:atIndex:`.
|
||||
fn set_bytes(&mut self, bytes: &[u8], index: u32) -> Result<()>;
|
||||
|
||||
/// `setTexture:atIndex:`.
|
||||
fn set_texture(&mut self, texture: &D::Texture, index: u32) -> Result<()>;
|
||||
|
||||
/// `setSamplerState:atIndex:`.
|
||||
fn set_sampler_state(&mut self, sampler: &D::SamplerState, index: u32) -> Result<()>;
|
||||
|
||||
/// `dispatchThreadgroups:threadsPerThreadgroup:`.
|
||||
fn dispatch_threadgroups(&mut self, groups: Size, threads: Size) -> Result<()>;
|
||||
|
||||
|
||||
@@ -2,10 +2,12 @@
|
||||
//! the same name. The cutover deletes this file, its `mod` line, the `--runtime old` arm of the
|
||||
//! command and the crate's dependency on the old runtime.
|
||||
|
||||
use crate::plan::{self, Constant};
|
||||
use crate::plan::{self, Constant, Filter};
|
||||
use crate::{Code, Result, Size, Storage};
|
||||
use metal2vulkan::bind::SamplerDescriptor;
|
||||
use metal2vulkan::metal::{PixelFormat, SamplerMinMagFilter, TextureUsage};
|
||||
use metal2vulkan_runtime as runtime;
|
||||
use runtime::{CommandBufferStatus, PipelineOptions, ResourceOptions};
|
||||
use runtime::{CommandBufferStatus, PipelineOptions, ResourceOptions, TextureDescriptor};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
/// The old runtime's `MTLCreateSystemDefaultDevice`: the preferred Vulkan physical device
|
||||
@@ -78,6 +80,8 @@ impl crate::ComputeDevice for runtime::Device {
|
||||
type CommandQueue = runtime::CommandQueue;
|
||||
type CommandBuffer = runtime::CommandBuffer;
|
||||
type ComputeCommandEncoder = runtime::ComputeCommandEncoder;
|
||||
type Texture = runtime::Texture;
|
||||
type SamplerState = runtime::SamplerState;
|
||||
|
||||
fn new_library(&self, code: Code<'_>) -> Result<runtime::Library> {
|
||||
match code {
|
||||
@@ -99,6 +103,29 @@ impl crate::ComputeDevice for runtime::Device {
|
||||
fn new_command_queue(&self) -> Result<runtime::CommandQueue> {
|
||||
Ok(runtime::Device::new_command_queue(self)?)
|
||||
}
|
||||
|
||||
fn new_texture(&self, width: u32, height: u32) -> Result<runtime::Texture> {
|
||||
let mut descriptor = TextureDescriptor::texture_2d(
|
||||
PixelFormat::RGBA16Float,
|
||||
width.into(),
|
||||
height.into(),
|
||||
false,
|
||||
);
|
||||
descriptor.usage = TextureUsage::SHADER_READ | TextureUsage::SHADER_WRITE;
|
||||
descriptor.resource_options = ResourceOptions::private();
|
||||
Ok(runtime::Device::new_texture(self, &descriptor)?)
|
||||
}
|
||||
|
||||
fn new_sampler_state(&self, filter: Filter) -> Result<runtime::SamplerState> {
|
||||
let filter = match filter {
|
||||
Filter::Nearest => SamplerMinMagFilter::Nearest,
|
||||
Filter::Linear => SamplerMinMagFilter::Linear,
|
||||
};
|
||||
let mut descriptor = SamplerDescriptor::new();
|
||||
descriptor.min_filter = filter;
|
||||
descriptor.mag_filter = filter;
|
||||
Ok(runtime::Device::new_sampler_state(self, &descriptor)?)
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::Buffer for runtime::Buffer {
|
||||
@@ -172,6 +199,22 @@ impl crate::ComputeCommandEncoder<runtime::Device> for runtime::ComputeCommandEn
|
||||
)?)
|
||||
}
|
||||
|
||||
fn set_texture(&mut self, texture: &runtime::Texture, index: u32) -> Result<()> {
|
||||
Ok(runtime::ComputeCommandEncoder::set_texture(
|
||||
self,
|
||||
Some(texture),
|
||||
index,
|
||||
)?)
|
||||
}
|
||||
|
||||
fn set_sampler_state(&mut self, sampler: &runtime::SamplerState, index: u32) -> Result<()> {
|
||||
Ok(runtime::ComputeCommandEncoder::set_sampler_state(
|
||||
self,
|
||||
Some(sampler),
|
||||
index,
|
||||
)?)
|
||||
}
|
||||
|
||||
fn dispatch_threadgroups(&mut self, groups: Size, threads: Size) -> Result<()> {
|
||||
Ok(runtime::ComputeCommandEncoder::dispatch_threadgroups(
|
||||
self,
|
||||
|
||||
@@ -15,6 +15,8 @@ pub struct Plan {
|
||||
pub library: Source,
|
||||
/// The buffers it makes, in order; dispatches, expectations and outputs name them by index.
|
||||
pub buffers: Vec<BufferPlan>,
|
||||
/// The textures it makes, in order; dispatches and expectations name them by index.
|
||||
pub textures: Vec<TexturePlan>,
|
||||
/// The dispatches of one run, in order, in one compute encoder of each command buffer.
|
||||
pub dispatches: Vec<Dispatch>,
|
||||
/// How many dispatches each command buffer of a run holds, its command buffers committed in
|
||||
@@ -23,6 +25,8 @@ pub struct Plan {
|
||||
pub per_command_buffer: Option<usize>,
|
||||
/// What the first run must leave in its buffers: a CPU reference.
|
||||
pub expect: Vec<Expect>,
|
||||
/// What the first run must leave in its textures: a CPU reference.
|
||||
pub expect_textures: Vec<TextureExpect>,
|
||||
/// The buffers whose contents after the first run each runtime's must match the first
|
||||
/// runtime's, for a workload with no CPU reference.
|
||||
pub outputs: Vec<Output>,
|
||||
@@ -50,6 +54,29 @@ pub struct BufferPlan {
|
||||
pub contents: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
/// One texture of a plan: 2D, `RGBA16Float`, one level, `Private`, of `ShaderRead | ShaderWrite`
|
||||
/// usage. Its width and height are multiples of 16, the harness's transfer kernels' threadgroups.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct TexturePlan {
|
||||
/// Its width in texels.
|
||||
pub width: u32,
|
||||
/// Its height in texels.
|
||||
pub height: u32,
|
||||
/// The texels it starts with, row by row, four `float`s each, written in by the harness's own
|
||||
/// kernel (`texels_in`) from a `Shared` buffer. `None`: as `newTextureWithDescriptor:` made it.
|
||||
pub texels: Option<Vec<f32>>,
|
||||
}
|
||||
|
||||
/// A sampler state's filter: its `minFilter` and `magFilter`. Its coordinates are normalized and
|
||||
/// each address mode clamps to the edge.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Filter {
|
||||
/// `MTLSamplerMinMagFilterNearest`.
|
||||
Nearest,
|
||||
/// `MTLSamplerMinMagFilterLinear`.
|
||||
Linear,
|
||||
}
|
||||
|
||||
/// One dispatch: the calls an encoder makes for it. The pipeline state is set where it differs
|
||||
/// from the dispatch before's; bindings stay set for the dispatches after, as Metal's do.
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -105,6 +132,20 @@ pub enum Binding {
|
||||
/// The bytes.
|
||||
bytes: Vec<u8>,
|
||||
},
|
||||
/// `setTexture:atIndex:` of the plan's texture `texture`.
|
||||
Texture {
|
||||
/// The texture table index.
|
||||
index: u32,
|
||||
/// The plan's texture.
|
||||
texture: usize,
|
||||
},
|
||||
/// `setSamplerState:atIndex:` of a sampler state of `filter`, made once per plan.
|
||||
Sampler {
|
||||
/// The sampler table index.
|
||||
index: u32,
|
||||
/// Its filter.
|
||||
filter: Filter,
|
||||
},
|
||||
}
|
||||
|
||||
/// A dispatch call.
|
||||
@@ -145,6 +186,16 @@ pub struct Expect {
|
||||
pub values: Values,
|
||||
}
|
||||
|
||||
/// What a texture must hold after the first run.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct TextureExpect {
|
||||
/// The plan's texture.
|
||||
pub texture: usize,
|
||||
/// Its texels from its first, row by row, four `float`s each, as the harness's own kernel
|
||||
/// reads them out (`texels_out`).
|
||||
pub values: Values,
|
||||
}
|
||||
|
||||
/// A buffer whose contents after the first run runtimes compare.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Output {
|
||||
@@ -273,16 +324,37 @@ impl Plan {
|
||||
}
|
||||
}
|
||||
}
|
||||
for (i, texture) in self.textures.iter().enumerate() {
|
||||
let (width, height) = (texture.width, texture.height);
|
||||
if width == 0 || height == 0 || width % 16 != 0 || height % 16 != 0 {
|
||||
return Err(format!(
|
||||
"{name}: texture {i}'s size {width}x{height} is not in whole 16x16 tiles"
|
||||
)
|
||||
.into());
|
||||
}
|
||||
if let Some(texels) = &texture.texels {
|
||||
if texels.len() as u64 != 4 * u64::from(width) * u64::from(height) {
|
||||
return Err(format!(
|
||||
"{name}: texture {i} has {} floats of texels for {width}x{height}",
|
||||
texels.len()
|
||||
)
|
||||
.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
let known = |buffer: usize| buffer < self.buffers.len();
|
||||
for (i, dispatch) in self.dispatches.iter().enumerate() {
|
||||
for binding in &dispatch.bindings {
|
||||
if let Binding::Buffer { buffer, .. } = binding {
|
||||
if !known(*buffer) {
|
||||
return Err(format!(
|
||||
"{name}: dispatch {i} binds buffer {buffer}, which the plan lacks"
|
||||
)
|
||||
.into());
|
||||
}
|
||||
let (kind, at, count) = match binding {
|
||||
Binding::Buffer { buffer, .. } => ("buffer", *buffer, self.buffers.len()),
|
||||
Binding::Texture { texture, .. } => ("texture", *texture, self.textures.len()),
|
||||
Binding::Bytes { .. } | Binding::Sampler { .. } => continue,
|
||||
};
|
||||
if at >= count {
|
||||
return Err(format!(
|
||||
"{name}: dispatch {i} binds {kind} {at}, which the plan lacks"
|
||||
)
|
||||
.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -302,6 +374,22 @@ impl Plan {
|
||||
.into());
|
||||
}
|
||||
}
|
||||
for expect in &self.expect_textures {
|
||||
let Some(texture) = self.textures.get(expect.texture) else {
|
||||
return Err(format!(
|
||||
"{name}: an expectation names texture {}, which the plan lacks",
|
||||
expect.texture
|
||||
)
|
||||
.into());
|
||||
};
|
||||
if expect.values.length() > 16 * u64::from(texture.width) * u64::from(texture.height) {
|
||||
return Err(format!(
|
||||
"{name}: texture {} is smaller than its expected values",
|
||||
expect.texture
|
||||
)
|
||||
.into());
|
||||
}
|
||||
}
|
||||
for output in &self.outputs {
|
||||
if !known(output.buffer) {
|
||||
return Err(format!(
|
||||
|
||||
@@ -10,13 +10,16 @@
|
||||
//! the same range, which the runtime zeroes as `newBufferWithLength:options:` does.
|
||||
//!
|
||||
//! Its known gaps and the device's limits (`Error::Unsupported`, `Error::Limit`) are typed
|
||||
//! refusals ([`Refused`]); its other errors are failures.
|
||||
//! refusals ([`Refused`]); its other errors are failures. It makes no texture or sampler state
|
||||
//! yet: a plan with either is refused when it is made, so its texture and sampler types have no
|
||||
//! value.
|
||||
|
||||
use crate::plan::{self, Constant};
|
||||
use crate::plan::{self, Constant, Filter};
|
||||
use crate::{Code, Refused, Result, Size, Storage, Waits};
|
||||
use ash::vk;
|
||||
use metal2vulkan_rt as rt;
|
||||
use std::cell::RefCell;
|
||||
use std::convert::Infallible;
|
||||
use std::ops::Range;
|
||||
use std::ptr::NonNull;
|
||||
use std::rc::Rc;
|
||||
@@ -300,6 +303,8 @@ impl crate::ComputeDevice for Device {
|
||||
type CommandQueue = rt::CommandQueue;
|
||||
type CommandBuffer = rt::CommandBuffer;
|
||||
type ComputeCommandEncoder = rt::ComputeCommandEncoder;
|
||||
type Texture = Infallible;
|
||||
type SamplerState = Infallible;
|
||||
|
||||
fn new_library(&self, code: Code<'_>) -> Result<rt::Library> {
|
||||
Ok(match code {
|
||||
@@ -324,6 +329,20 @@ impl crate::ComputeDevice for Device {
|
||||
.new_command_queue(QUEUE_COMMAND_BUFFERS)
|
||||
.map_err(error)
|
||||
}
|
||||
|
||||
/// A typed refusal: the runtime makes no texture yet.
|
||||
fn new_texture(&self, _width: u32, _height: u32) -> Result<Infallible> {
|
||||
Err(Box::new(Refused(
|
||||
"the new runtime makes no texture yet".to_owned(),
|
||||
)))
|
||||
}
|
||||
|
||||
/// A typed refusal: the runtime makes no sampler state yet.
|
||||
fn new_sampler_state(&self, _filter: Filter) -> Result<Infallible> {
|
||||
Err(Box::new(Refused(
|
||||
"the new runtime makes no sampler state yet".to_owned(),
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::Library<Device> for rt::Library {
|
||||
@@ -386,6 +405,14 @@ impl crate::ComputeCommandEncoder<Device> for rt::ComputeCommandEncoder {
|
||||
rt::ComputeCommandEncoder::set_bytes(self, bytes, index).map_err(error)
|
||||
}
|
||||
|
||||
fn set_texture(&mut self, texture: &Infallible, _index: u32) -> Result<()> {
|
||||
match *texture {}
|
||||
}
|
||||
|
||||
fn set_sampler_state(&mut self, sampler: &Infallible, _index: u32) -> Result<()> {
|
||||
match *sampler {}
|
||||
}
|
||||
|
||||
fn dispatch_threadgroups(&mut self, groups: Size, threads: Size) -> Result<()> {
|
||||
rt::ComputeCommandEncoder::dispatch_threadgroups(self, size(groups)?, size(threads)?)
|
||||
.map_err(error)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! A plan on a device: its objects made, its first run checked, the runs after it timed.
|
||||
|
||||
use crate::plan::{Binding, BufferPlan, Constant, Grid, Plan, Source};
|
||||
use crate::plan::{Binding, BufferPlan, Constant, Filter, Grid, Plan, Source, TexturePlan};
|
||||
use crate::{
|
||||
clocks, Buffer, Code, CommandBuffer, CommandQueue, ComputeCommandEncoder, ComputeDevice,
|
||||
Device, Library, Refused, Result, Storage, Waits,
|
||||
@@ -13,6 +13,10 @@ use std::time::{Duration, Instant};
|
||||
/// The kernel of the authored library that moves bytes for the harness, 16 a thread.
|
||||
const COPY_FUNCTION: &str = "copy_words";
|
||||
|
||||
/// The kernels of the authored library that move texels for the harness, between a buffer of
|
||||
/// `float4`s and a texture, one a thread, in threadgroups of 16x16: into the texture, and out.
|
||||
const TEXELS_FUNCTIONS: [&str; 2] = ["texels_in", "texels_out"];
|
||||
|
||||
/// The most threadgroups a dispatch of the copy kernel takes in x: the least
|
||||
/// `maxComputeWorkGroupCount[0]` Vulkan allows.
|
||||
const COPY_GROUPS: u64 = 65535;
|
||||
@@ -217,11 +221,14 @@ pub fn percentile(values: &[f64], p: u32) -> Option<f64> {
|
||||
sorted.get(rank - 1).copied()
|
||||
}
|
||||
|
||||
/// The harness on a device: its own queue, the authored library and its copy kernel, and the
|
||||
/// metallibs the plans named so far.
|
||||
/// The harness on a device: its own queue, the authored library and its copy kernel, its texel
|
||||
/// kernels once a plan has textures, and the metallibs the plans named so far.
|
||||
pub struct Harness<D: ComputeDevice> {
|
||||
queue: D::CommandQueue,
|
||||
copy: D::ComputePipelineState,
|
||||
/// The pipeline states of [`TEXELS_FUNCTIONS`], made for the first plan with a texture, so a
|
||||
/// session that runs none makes no more pipelines than before.
|
||||
texels: Option<[D::ComputePipelineState; 2]>,
|
||||
authored: D::Library,
|
||||
metallibs: HashMap<PathBuf, D::Library>,
|
||||
device: D,
|
||||
@@ -230,6 +237,9 @@ pub struct Harness<D: ComputeDevice> {
|
||||
/// A plan's objects on the device.
|
||||
struct Made<D: ComputeDevice> {
|
||||
buffers: Vec<D::Buffer>,
|
||||
textures: Vec<D::Texture>,
|
||||
/// A sampler state for each filter the plan's bindings name.
|
||||
samplers: Vec<(Filter, D::SamplerState)>,
|
||||
states: Vec<D::ComputePipelineState>,
|
||||
/// The index in `states` of each dispatch's.
|
||||
state_of: Vec<usize>,
|
||||
@@ -245,6 +255,7 @@ impl<D: ComputeDevice> Harness<D> {
|
||||
Ok(Harness {
|
||||
queue: device.new_command_queue()?,
|
||||
copy,
|
||||
texels: None,
|
||||
authored,
|
||||
metallibs: HashMap::new(),
|
||||
device,
|
||||
@@ -312,8 +323,25 @@ impl<D: ComputeDevice> Harness<D> {
|
||||
let buffers = (plan.buffers.iter())
|
||||
.map(|buffer| self.device.new_buffer(buffer.length, buffer.storage))
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
let textures = (plan.textures.iter())
|
||||
.map(|texture| self.device.new_texture(texture.width, texture.height))
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
let mut samplers: Vec<(Filter, D::SamplerState)> = Vec::new();
|
||||
for binding in plan
|
||||
.dispatches
|
||||
.iter()
|
||||
.flat_map(|dispatch| &dispatch.bindings)
|
||||
{
|
||||
if let Binding::Sampler { filter, .. } = binding {
|
||||
if !samplers.iter().any(|(made, _)| made == filter) {
|
||||
samplers.push((*filter, self.device.new_sampler_state(*filter)?));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Made {
|
||||
buffers,
|
||||
textures,
|
||||
samplers,
|
||||
states,
|
||||
state_of,
|
||||
})
|
||||
@@ -356,6 +384,16 @@ impl<D: ComputeDevice> Harness<D> {
|
||||
offset,
|
||||
} => encoder.set_buffer(&made.buffers[*buffer], *offset, *index)?,
|
||||
Binding::Bytes { index, bytes } => encoder.set_bytes(bytes, *index)?,
|
||||
Binding::Texture { index, texture } => {
|
||||
encoder.set_texture(&made.textures[*texture], *index)?
|
||||
}
|
||||
Binding::Sampler { index, filter } => {
|
||||
// `make` made one for every filter a binding names.
|
||||
let made = made.samplers.iter().find(|(made, _)| made == filter);
|
||||
let (_, sampler) =
|
||||
made.ok_or("a sampler state the plan names is unmade")?;
|
||||
encoder.set_sampler_state(sampler, *index)?
|
||||
}
|
||||
}
|
||||
}
|
||||
match dispatch.grid {
|
||||
@@ -402,6 +440,75 @@ impl<D: ComputeDevice> Harness<D> {
|
||||
command_buffer.wait_until_completed()
|
||||
}
|
||||
|
||||
/// The texel kernels' pipeline states.
|
||||
fn make_texels(&self) -> Result<[D::ComputePipelineState; 2]> {
|
||||
let [into, out] = TEXELS_FUNCTIONS.map(|name| {
|
||||
let function = self.authored.new_function(name, &[])?;
|
||||
self.device.new_compute_pipeline_state(&function)
|
||||
});
|
||||
Ok([into?, out?])
|
||||
}
|
||||
|
||||
/// The texel kernels' pipeline states, which `run` makes for the first plan with a texture.
|
||||
fn texels(&self) -> Result<&[D::ComputePipelineState; 2]> {
|
||||
Ok(self.texels.as_ref().ok_or("the texel kernels are unmade")?)
|
||||
}
|
||||
|
||||
/// Every texture's starting texels, written in from `Shared` buffers by the texel kernel,
|
||||
/// all in one command buffer, waited for.
|
||||
fn fill_textures(&self, plans: &[TexturePlan], textures: &[D::Texture]) -> Result<()> {
|
||||
let mut staged = Vec::new();
|
||||
for (plan, texture) in plans.iter().zip(textures) {
|
||||
let Some(texels) = &plan.texels else {
|
||||
continue;
|
||||
};
|
||||
let bytes: Vec<u8> = texels
|
||||
.iter()
|
||||
.flat_map(|value| value.to_le_bytes())
|
||||
.collect();
|
||||
let stage = self
|
||||
.device
|
||||
.new_buffer(bytes.len() as u64, Storage::Shared)?;
|
||||
write(&stage, &bytes)?;
|
||||
staged.push((stage, texture, plan));
|
||||
}
|
||||
if staged.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
let [into, _] = self.texels()?;
|
||||
let command_buffer = self.queue.command_buffer()?;
|
||||
let mut encoder = command_buffer.compute_command_encoder()?;
|
||||
encoder.set_compute_pipeline_state(into)?;
|
||||
for (stage, texture, plan) in &staged {
|
||||
encoder.set_buffer(stage, 0, 0)?;
|
||||
encoder.set_bytes(&plan.width.to_le_bytes(), 1)?;
|
||||
encoder.set_texture(texture, 0)?;
|
||||
encoder.dispatch_threadgroups(tiles(plan), [16, 16, 1])?;
|
||||
}
|
||||
encoder.end_encoding()?;
|
||||
command_buffer.commit()?;
|
||||
command_buffer.wait_until_completed()
|
||||
}
|
||||
|
||||
/// A texture's texels, row by row, four `float`s each: read out to a `Shared` buffer by the
|
||||
/// texel kernel.
|
||||
fn read_texture(&self, texture: &D::Texture, plan: &TexturePlan) -> Result<Vec<u8>> {
|
||||
let length = 16 * u64::from(plan.width) * u64::from(plan.height);
|
||||
let stage = self.device.new_buffer(length, Storage::Shared)?;
|
||||
let [_, out] = self.texels()?;
|
||||
let command_buffer = self.queue.command_buffer()?;
|
||||
let mut encoder = command_buffer.compute_command_encoder()?;
|
||||
encoder.set_compute_pipeline_state(out)?;
|
||||
encoder.set_texture(texture, 0)?;
|
||||
encoder.set_buffer(&stage, 0, 0)?;
|
||||
encoder.set_bytes(&plan.width.to_le_bytes(), 1)?;
|
||||
encoder.dispatch_threadgroups(tiles(plan), [16, 16, 1])?;
|
||||
encoder.end_encoding()?;
|
||||
command_buffer.commit()?;
|
||||
command_buffer.wait_until_completed()?;
|
||||
bytes(&stage, length)
|
||||
}
|
||||
|
||||
/// The first `length` bytes of `buffer`: a `Shared` one's through `contents`, a `Private`
|
||||
/// one's copied out to a `Shared` buffer first.
|
||||
fn read(&self, buffer: &D::Buffer, storage: Storage, length: u64) -> Result<Vec<u8>> {
|
||||
@@ -431,7 +538,11 @@ impl<D: ComputeDevice> Session for Harness<D> {
|
||||
plan.validate()?;
|
||||
self.load(&plan.library)?;
|
||||
let made = self.make(plan)?;
|
||||
if !plan.textures.is_empty() && self.texels.is_none() {
|
||||
self.texels = Some(self.make_texels()?);
|
||||
}
|
||||
self.fill(&plan.buffers, &made.buffers)?;
|
||||
self.fill_textures(&plan.textures, &made.textures)?;
|
||||
wait_run::<D>(&self.commit_run(plan, &made)?)?;
|
||||
for expect in &plan.expect {
|
||||
let storage = plan.buffers[expect.buffer].storage;
|
||||
@@ -444,6 +555,12 @@ impl<D: ComputeDevice> Session for Harness<D> {
|
||||
(expect.values.compare(&bytes))
|
||||
.map_err(|why| format!("{}: buffer {buffer}: {why}", plan.name))?;
|
||||
}
|
||||
for expect in &plan.expect_textures {
|
||||
let texture = expect.texture;
|
||||
let bytes = self.read_texture(&made.textures[texture], &plan.textures[texture])?;
|
||||
(expect.values.compare(&bytes))
|
||||
.map_err(|why| format!("{}: texture {texture}: {why}", plan.name))?;
|
||||
}
|
||||
let mut outputs = Vec::with_capacity(plan.outputs.len());
|
||||
for output in &plan.outputs {
|
||||
let buffer = &plan.buffers[output.buffer];
|
||||
@@ -541,6 +658,11 @@ fn create<D: Device>(device: &D, length: u64, runs: Runs) -> Result<CreateReport
|
||||
Ok(report)
|
||||
}
|
||||
|
||||
/// The threadgroups of 16x16 that cover a texture (whole ones: `Plan::validate`).
|
||||
fn tiles(plan: &TexturePlan) -> crate::Size {
|
||||
[u64::from(plan.width / 16), u64::from(plan.height / 16), 1]
|
||||
}
|
||||
|
||||
/// Dispatches of the copy kernel, already set on `encoder`, moving the first `length` bytes (a
|
||||
/// multiple of 16) of `from` to `to` ([`copy_dispatches`]).
|
||||
fn copy<D: ComputeDevice>(
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
use crate::authored::WORKLOADS;
|
||||
use crate::description;
|
||||
use crate::plan::{
|
||||
Binding, BufferPlan, Comparison, Constant, Dispatch, Expect, Grid, Output, Plan, Source, Values,
|
||||
Binding, BufferPlan, Comparison, Constant, Dispatch, Expect, Filter, Grid, Output, Plan,
|
||||
Source, TextureExpect, TexturePlan, Values,
|
||||
};
|
||||
use crate::rt::{self, Ranges};
|
||||
use crate::run::{
|
||||
@@ -103,6 +104,8 @@ fn one_buffer_plan(length: u64, contents: Option<Vec<u8>>, bound: usize) -> Plan
|
||||
buffer: 0,
|
||||
values: Values::Words(vec![0; 4]),
|
||||
}],
|
||||
textures: Vec::new(),
|
||||
expect_textures: Vec::new(),
|
||||
outputs: Vec::new(),
|
||||
per_command_buffer: None,
|
||||
}
|
||||
@@ -127,6 +130,54 @@ fn a_plan_with_a_part_length_a_missing_buffer_or_no_dispatch_is_refused() {
|
||||
assert!(idle.validate().is_err());
|
||||
}
|
||||
|
||||
/// A plan of one `side` by `side` texture, bound at `bound`, with a sampler, and its
|
||||
/// expectation of `expected` floats.
|
||||
fn one_texture_plan(side: u32, texels: Option<usize>, bound: usize, expected: usize) -> Plan {
|
||||
let mut plan = one_buffer_plan(16, None, 0);
|
||||
plan.textures = vec![TexturePlan {
|
||||
width: side,
|
||||
height: side,
|
||||
texels: texels.map(|count| vec![0.0; count]),
|
||||
}];
|
||||
plan.dispatches[0].bindings = vec![
|
||||
Binding::Texture {
|
||||
index: 0,
|
||||
texture: bound,
|
||||
},
|
||||
Binding::Sampler {
|
||||
index: 0,
|
||||
filter: Filter::Linear,
|
||||
},
|
||||
];
|
||||
plan.expect_textures = vec![TextureExpect {
|
||||
texture: 0,
|
||||
values: Values::Floats {
|
||||
values: vec![0.0; expected],
|
||||
tolerance: 0.0,
|
||||
},
|
||||
}];
|
||||
plan
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_plan_with_a_part_tile_texture_a_missing_texture_or_too_many_texels_is_refused() {
|
||||
assert!(one_texture_plan(16, Some(4 * 256), 0, 4 * 256)
|
||||
.validate()
|
||||
.is_ok());
|
||||
assert!(one_texture_plan(16, None, 0, 4).validate().is_ok());
|
||||
assert!(one_texture_plan(24, None, 0, 4).validate().is_err());
|
||||
assert!(one_texture_plan(16, Some(4 * 255), 0, 4)
|
||||
.validate()
|
||||
.is_err());
|
||||
assert!(one_texture_plan(16, None, 1, 4).validate().is_err());
|
||||
assert!(one_texture_plan(16, None, 0, 4 * 256 + 1)
|
||||
.validate()
|
||||
.is_err());
|
||||
let mut unknown = one_texture_plan(16, None, 0, 4);
|
||||
unknown.expect_textures[0].texture = 1;
|
||||
assert!(unknown.validate().is_err());
|
||||
}
|
||||
|
||||
/// Each authored plan is whole: its builder's inputs, kernels and references agree in shape.
|
||||
#[test]
|
||||
fn every_authored_plan_validates() {
|
||||
|
||||
Reference in new issue
Block a user