feat(gpu): fetch vertex formats the vertex input lacks as words that vertex functions decode
This commit is contained in:
1 parent
6467c52173
commit
8b6fbb6c01
11 files changed
+239
-23
No files matched your search
@@ -2418,6 +2418,36 @@ stage_variable(struct translator *t, const struct stela_air_stage_io *io, bool o
|
||||
return var;
|
||||
}
|
||||
|
||||
/* In *def, a vertex input its pipeline fetches as 32-bit words, decoded
|
||||
* into its type; *def stays NULL for one fetched in its own format. False
|
||||
* after failing. */
|
||||
static bool
|
||||
vertex_input_words(struct translator *t, const struct stela_air_stage_io *io, nir_def **def)
|
||||
{
|
||||
const struct stela_air_stage_options *options = t->stage_options;
|
||||
unsigned words = options->vertex_input_words && io->location < AIR_MAX_VERTEX_ATTRIBUTES ?
|
||||
options->vertex_input_words[io->location] : 0;
|
||||
*def = NULL;
|
||||
if (!words)
|
||||
return true;
|
||||
if (words > 4 || !options->decode_vertex_input)
|
||||
return fail(t, STELA_AIR_ARGUMENT, "vertex input %u has no decoding of its words",
|
||||
io->location);
|
||||
char name[32];
|
||||
snprintf(name, sizeof(name), "air.vertex_words%u", io->location);
|
||||
nir_variable *var = nir_variable_create(t->b.shader, nir_var_shader_in,
|
||||
glsl_vector_type(GLSL_TYPE_UINT, words), name);
|
||||
if (!var)
|
||||
return fail(t, STELA_AIR_ALLOCATION, "render IO variable allocation failed");
|
||||
var->data.location = VERT_ATTRIB_GENERIC0 + io->location;
|
||||
nir_def *lanes = options->decode_vertex_input(&t->b, io->location, nir_load_var(&t->b, var),
|
||||
options->resources.binding_data);
|
||||
if (!lanes || lanes->num_components != 4 || lanes->bit_size != 32)
|
||||
return fail(t, STELA_AIR_ARGUMENT, "vertex input %u does not decode", io->location);
|
||||
*def = stage_narrow(&t->b, nir_trim_vector(&t->b, lanes, io->components), io->primitive);
|
||||
return true;
|
||||
}
|
||||
|
||||
/* A mesh function's shader info and outputs: its data and primitive
|
||||
* indices. */
|
||||
static bool
|
||||
@@ -3225,6 +3255,13 @@ stage_arguments(struct translator *t, LLVMValueRef fn)
|
||||
} else {
|
||||
/* A vertex input the function never reads is not fetched. */
|
||||
if (io->semantic == STELA_AIR_VERTEX_INPUT && !io->used) continue;
|
||||
if (io->semantic == STELA_AIR_VERTEX_INPUT) {
|
||||
if (!vertex_input_words(t, io, &def)) return false;
|
||||
if (def) {
|
||||
if (!put_value(t, param, def, io->buffer.access)) return false;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
nir_variable *var = stage_variable(t, io, false);
|
||||
if (!var) return false;
|
||||
if (io->interpolant) continue; /* Calls interpolate it. */
|
||||
|
||||
@@ -454,6 +454,14 @@ struct stela_air_stage_options {
|
||||
* one sample. */
|
||||
const struct stela_air_attachment_access *attachments;
|
||||
unsigned samples;
|
||||
/* A vertex function's inputs, by location, that its pipeline fetches as
|
||||
* this many 32-bit words, the vertex input lacking their formats: each
|
||||
* reads as decode_vertex_input makes four 32-bit lanes of its words, with
|
||||
* the resources' binding data. NULL, or zero words, for an input fetched
|
||||
* in its own format. */
|
||||
const uint8_t *vertex_input_words;
|
||||
nir_def *(*decode_vertex_input)(nir_builder *, unsigned location, nir_def *words,
|
||||
void *binding_data);
|
||||
};
|
||||
nir_shader *stela_air_stage_to_nir(const void *, size_t, const char *entry,
|
||||
const nir_shader_compiler_options *,
|
||||
|
||||
@@ -650,6 +650,41 @@ fetch_attribute(nir_builder *b, unsigned location, nir_def *at, nir_def *point,
|
||||
return point ? NULL : stela_metal_fetch_stage_in(b, specialization->stage_in, location, at, integer);
|
||||
}
|
||||
|
||||
/* decode_vertex_input of the vertex inputs the pipeline fetches as words,
|
||||
* in the formats the specialization names. */
|
||||
static nir_def *
|
||||
decode_vertex_input(nir_builder *b, unsigned location, nir_def *words, void *data)
|
||||
{
|
||||
const struct stela_metal_specialization *specialization =
|
||||
((const struct binding *)data)->specialization;
|
||||
unsigned format = location < ARRAY_SIZE(specialization->vertex_formats) ?
|
||||
specialization->vertex_formats[location] : 0;
|
||||
unsigned bytes = stela_metal_vertex_format_bytes(format);
|
||||
if (!bytes || words->bit_size != 32 || words->num_components * 4 != bytes)
|
||||
return NULL;
|
||||
return stela_metal_decode_attribute(b, words, format);
|
||||
}
|
||||
|
||||
/* The 32-bit words of each vertex input the pipeline fetches as words, by
|
||||
* location; false when a format named is none of whole words. */
|
||||
static bool
|
||||
vertex_words(const struct stela_metal_specialization *specialization,
|
||||
uint8_t words[static AIR_MAX_VERTEX_ATTRIBUTES], bool *any)
|
||||
{
|
||||
*any = false;
|
||||
for (unsigned location = 0; location < ARRAY_SIZE(specialization->vertex_formats); location++) {
|
||||
unsigned format = specialization->vertex_formats[location];
|
||||
if (!format)
|
||||
continue;
|
||||
unsigned bytes = stela_metal_vertex_format_bytes(format);
|
||||
if (location >= AIR_MAX_VERTEX_ATTRIBUTES || !bytes || bytes % 4)
|
||||
return false;
|
||||
words[location] = bytes / 4;
|
||||
*any = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static enum stela_metal_table
|
||||
stage_table(enum stela_metal_function_stage stage)
|
||||
{
|
||||
@@ -866,6 +901,14 @@ stela_metal_compile_reflected(const void *air, size_t size, const char *entry,
|
||||
fail(error, STELA_AIR_INPUT, "stage-in for a function that does not fetch it");
|
||||
goto done;
|
||||
}
|
||||
/* Only a vertex input fetches words, for a vertex function. */
|
||||
uint8_t words[AIR_MAX_VERTEX_ATTRIBUTES] = {0};
|
||||
bool fetches_words;
|
||||
if (!vertex_words(specialization, words, &fetches_words) ||
|
||||
(fetches_words && (stage != STELA_METAL_VERTEX || patch))) {
|
||||
fail(error, STELA_AIR_INPUT, "vertex input words for a function that does not take them");
|
||||
goto done;
|
||||
}
|
||||
struct binding binding = {{stage_table(stage)}, specialization};
|
||||
struct stela_air_options resources = {
|
||||
.max_instructions = target->max_instructions,
|
||||
@@ -947,6 +990,8 @@ stela_metal_compile_reflected(const void *air, size_t size, const char *entry,
|
||||
.raster_order_groups = target->raster_order_groups != 0,
|
||||
.attachments = specialization->attachment_count ? attachments : NULL,
|
||||
.samples = specialization->samples,
|
||||
.vertex_input_words = fetches_words ? words : NULL,
|
||||
.decode_vertex_input = decode_vertex_input,
|
||||
};
|
||||
shader = stela_air_metal_stage_to_nir(air, size, entry, target->nir, &options, error);
|
||||
done:
|
||||
|
||||
@@ -175,8 +175,12 @@ struct stela_metal_specialization {
|
||||
/* Narrowing floating-point conversions round toward zero, as the
|
||||
* library's MTLCompileOptions ask, not to nearest even. */
|
||||
uint32_t round_toward_zero, reserved;
|
||||
/* A vertex function's MTLVertexFormat of each input, by location, that
|
||||
* its pipeline fetches as 32-bit words, the device's vertex input
|
||||
* lacking the format; zero for the rest. */
|
||||
uint8_t vertex_formats[32];
|
||||
};
|
||||
#define STELA_METAL_SPECIALIZATION_BYTES 144u
|
||||
#define STELA_METAL_SPECIALIZATION_BYTES 176u
|
||||
#define STELA_METAL_ATTACHMENT_BYTES 16u
|
||||
#define STELA_METAL_CONSTEXPR_SAMPLER_BYTES 16u
|
||||
#define STELA_METAL_MODULE_BYTES 32u
|
||||
|
||||
@@ -1391,6 +1391,32 @@ kernels_compile_with_their_stage_in(void)
|
||||
assert(!compile(vertex, "vertex", &fetched, &error) && error.kind == STELA_AIR_INPUT);
|
||||
}
|
||||
|
||||
/* A vertex input in a format the device's vertex input lacks arrives as its
|
||||
* words, which the vertex function decodes; no other function, and no format
|
||||
* of no whole words, takes them. */
|
||||
static void
|
||||
vertex_inputs_decode_their_words(void)
|
||||
{
|
||||
struct stela_air_error error = {0};
|
||||
const struct stela_metal_linked_varying linked[] = {{"user(uv)", 8, 0}};
|
||||
/* MTLVertexFormatFloatRGB9E5 at the function's location 3. */
|
||||
struct stela_metal_specialization specialization = {.varyings = linked, .varying_count = 1};
|
||||
specialization.vertex_formats[3] = 55;
|
||||
nir_shader *shader = compile(vertex, "vertex", &specialization, &error);
|
||||
if (!shader)
|
||||
fprintf(stderr, "vertex words: %u %s\n", error.kind, error.detail);
|
||||
assert(shader);
|
||||
nir_variable *words = varying(shader, nir_var_shader_in, VERT_ATTRIB_GENERIC0 + 3);
|
||||
assert(words && glsl_get_base_type(words->type) == GLSL_TYPE_UINT &&
|
||||
glsl_get_vector_elements(words->type) == 1);
|
||||
stela_metal_release(shader);
|
||||
|
||||
assert(!compile(fragment, "fragment", &specialization, &error) && error.kind == STELA_AIR_INPUT);
|
||||
/* MTLVertexFormatUChar3 is three bytes. */
|
||||
specialization.vertex_formats[3] = 2;
|
||||
assert(!compile(vertex, "vertex", &specialization, &error) && error.kind == STELA_AIR_INPUT);
|
||||
}
|
||||
|
||||
static void
|
||||
mesh_stages_compile_with_their_launch(void)
|
||||
{
|
||||
@@ -3135,6 +3161,7 @@ main(void)
|
||||
render_stages_compile_at_their_linked_locations();
|
||||
patch_functions_compile_behind_their_tessellation_stages();
|
||||
kernels_compile_with_their_stage_in();
|
||||
vertex_inputs_decode_their_words();
|
||||
mesh_stages_compile_with_their_launch();
|
||||
function_constants_reflect_and_specialize();
|
||||
derived_constants_are_constructed_from_their_function_constants();
|
||||
|
||||
@@ -1041,14 +1041,20 @@ impl Derivation<'_> {
|
||||
}
|
||||
|
||||
fn vertex_formats(&mut self) -> Vec<VertexFormat> {
|
||||
let (supported, refused): (Vec<_>, Vec<_>) = VertexFormat::ALL.iter().partition(|format| {
|
||||
self.device
|
||||
.formats
|
||||
.get(format.vulkan().0)
|
||||
.buffer
|
||||
.contains(FormatFeatures::VERTEX_BUFFER)
|
||||
});
|
||||
if !refused.is_empty() {
|
||||
let (supported, refused): (Vec<VertexFormat>, Vec<VertexFormat>) =
|
||||
VertexFormat::ALL.iter().partition(|format| {
|
||||
self.device
|
||||
.formats
|
||||
.get(format.vulkan().0)
|
||||
.buffer
|
||||
.contains(FormatFeatures::VERTEX_BUFFER)
|
||||
});
|
||||
// Vertex functions decode the formats of whole words that the
|
||||
// pipeline fetches as words (`vertex::word_formats`).
|
||||
if refused
|
||||
.iter()
|
||||
.any(|format| !format.vulkan().1.is_multiple_of(4))
|
||||
{
|
||||
self.gap("vertex formats the driver cannot fetch", Cause::Driver);
|
||||
}
|
||||
supported
|
||||
|
||||
@@ -41,7 +41,7 @@ use super::{
|
||||
Stage, Tessellation, TessellationStages, TextureUse, ThreadgroupUse, Varying,
|
||||
},
|
||||
stitching::{StitchedGraph, StitchedNode},
|
||||
vertex::{Attribute, STRIDE_DYNAMIC, StageInputDescriptor, VertexDescriptor},
|
||||
vertex::{Attribute, STRIDE_DYNAMIC, StageInputDescriptor, VertexDescriptor, VertexFormat},
|
||||
vulkan::{object::ShaderCode, physical::PhysicalDevice},
|
||||
};
|
||||
use alloc::{
|
||||
@@ -956,6 +956,7 @@ impl AirCompiler {
|
||||
samples: specialization.samples.max(1),
|
||||
round_toward_zero: specialization.round_toward_zero.into(),
|
||||
reserved: 0,
|
||||
vertex_formats: vertex_formats(&specialization.vertex_words),
|
||||
};
|
||||
let mut error = ffi::AirError::NONE;
|
||||
let air = selected.module.bitcode;
|
||||
@@ -1563,3 +1564,14 @@ fn interface(reflection: &ffi::Reflection) -> Result<Interface> {
|
||||
interface.validate()?;
|
||||
Ok(interface)
|
||||
}
|
||||
|
||||
/// The raw `MTLVertexFormat` of each vertex input fetched as words, zero for
|
||||
/// the rest.
|
||||
fn vertex_formats(words: &[Option<VertexFormat>]) -> [u8; 32] {
|
||||
let mut raw = [0; 32];
|
||||
for (slot, format) in raw.iter_mut().zip(words) {
|
||||
// Every format's value is below 64.
|
||||
*slot = format.map_or(0, |format| format.raw() as u8);
|
||||
}
|
||||
raw
|
||||
}
|
||||
@@ -436,6 +436,7 @@ pub struct Specialization {
|
||||
pub(super) samples: u32,
|
||||
pub(super) round_toward_zero: u32,
|
||||
pub(super) reserved: u32,
|
||||
pub(super) vertex_formats: [u8; 32],
|
||||
}
|
||||
|
||||
/// `struct stela_metal_attachment`: a color attachment a fragment function
|
||||
|
||||
@@ -26,7 +26,7 @@ use crate::metal::{
|
||||
Specialization, Stage, Tessellation, TessellationStages, Varying,
|
||||
},
|
||||
stitching::StitchedGraph,
|
||||
vertex::{StageInputDescriptor, VertexDescriptor},
|
||||
vertex::{self, StageInputDescriptor, VertexDescriptor},
|
||||
vulkan::{
|
||||
ShaderStages,
|
||||
backend::{Backend, Object},
|
||||
@@ -1055,10 +1055,17 @@ impl<B: Backend, C: Compiler> Device<B, C> {
|
||||
&request.tessellation,
|
||||
request.vertex_descriptor.as_ref(),
|
||||
)?;
|
||||
// The evaluation stage of a tessellated pipeline fetches its
|
||||
// stage-in itself; a vertex input fetches the rest.
|
||||
let vertex_words = match (&tessellation, &request.vertex_descriptor) {
|
||||
(None, Some(layout)) => vertex::word_formats(&self.capabilities, layout),
|
||||
_ => Default::default(),
|
||||
};
|
||||
let specialization = Specialization {
|
||||
amplified: request.max_vertex_amplification_count > 1,
|
||||
varyings: varyings.clone(),
|
||||
linking: vertex_linked.linking.clone(),
|
||||
vertex_words,
|
||||
..Specialization::default()
|
||||
};
|
||||
let (vertex, feeding) = match &tessellation {
|
||||
|
||||
@@ -13,14 +13,15 @@ use super::{
|
||||
},
|
||||
binding::TableSlot,
|
||||
capabilities::{
|
||||
MAX_BUFFER_BINDINGS, MAX_SAMPLER_BINDINGS, MAX_TEXTURE_BINDINGS, ShaderFeatures,
|
||||
MAX_BUFFER_BINDINGS, MAX_SAMPLER_BINDINGS, MAX_TEXTURE_BINDINGS, MAX_VERTEX_ATTRIBUTES,
|
||||
ShaderFeatures,
|
||||
},
|
||||
error::{Result, ensure},
|
||||
pipeline::MAX_COLOR_ATTACHMENTS,
|
||||
pixel_format::Numeric,
|
||||
reflection::Binding,
|
||||
stitching::StitchedGraph,
|
||||
vertex::{StageInputDescriptor, VertexDescriptor},
|
||||
vertex::{StageInputDescriptor, VertexDescriptor, VertexFormat},
|
||||
vulkan::{ShaderStages, object::ShaderCode},
|
||||
};
|
||||
use alloc::{boxed::Box, string::String, vec::Vec};
|
||||
@@ -535,6 +536,12 @@ pub struct Specialization {
|
||||
/// The function's library's narrowing floating-point conversions round
|
||||
/// toward zero (`MTLFloatingPointConversionRoundingModeTowardZero`).
|
||||
pub round_toward_zero: bool,
|
||||
/// A vertex function's inputs, by location, that its pipeline fetches
|
||||
/// as 32-bit words in these formats, which the device's vertex input
|
||||
/// lacks ([`word_formats`]): the function decodes them.
|
||||
///
|
||||
/// [`word_formats`]: super::vertex::word_formats
|
||||
pub vertex_words: [Option<VertexFormat>; MAX_VERTEX_ATTRIBUTES as usize],
|
||||
}
|
||||
|
||||
/// A color attachment a fragment function reads, which the function also
|
||||
|
||||
@@ -357,21 +357,63 @@ pub fn vertex_input(
|
||||
stride,
|
||||
});
|
||||
}
|
||||
let words = word_formats(capabilities, descriptor);
|
||||
for attribute in &descriptor.attributes {
|
||||
require(
|
||||
capabilities.supports_vertex_format(attribute.format),
|
||||
"vertex attribute format",
|
||||
)?;
|
||||
let format = match words.get(attribute.index as usize).copied().flatten() {
|
||||
Some(format) => word_format(format),
|
||||
None => {
|
||||
require(
|
||||
capabilities.supports_vertex_format(attribute.format),
|
||||
"vertex attribute format",
|
||||
)?;
|
||||
attribute.format.vulkan().0
|
||||
}
|
||||
};
|
||||
input.attributes.push(VertexAttribute {
|
||||
location: attribute.index,
|
||||
binding: attribute.buffer_index,
|
||||
format: attribute.format.vulkan().0,
|
||||
format,
|
||||
offset: attribute.offset as u32,
|
||||
});
|
||||
}
|
||||
Ok(input)
|
||||
}
|
||||
|
||||
/// The formats of a vertex descriptor's attributes that the device's vertex
|
||||
/// input does not fetch, by location: the pipeline fetches each as its
|
||||
/// 32-bit words, and its vertex function decodes them
|
||||
/// ([`Specialization::vertex_words`]). `None` for the attributes fetched in
|
||||
/// their own formats, and for those of no whole number of words, which stay
|
||||
/// refused.
|
||||
///
|
||||
/// [`Specialization::vertex_words`]: super::shader::Specialization::vertex_words
|
||||
pub fn word_formats(
|
||||
capabilities: &Capabilities,
|
||||
descriptor: &VertexDescriptor,
|
||||
) -> [Option<VertexFormat>; MAX_VERTEX_ATTRIBUTES as usize] {
|
||||
let mut words = [None; MAX_VERTEX_ATTRIBUTES as usize];
|
||||
for attribute in &descriptor.attributes {
|
||||
let format = attribute.format;
|
||||
if !capabilities.supports_vertex_format(format)
|
||||
&& format.vulkan().1.is_multiple_of(4)
|
||||
&& let Some(slot) = words.get_mut(attribute.index as usize)
|
||||
{
|
||||
*slot = Some(format);
|
||||
}
|
||||
}
|
||||
words
|
||||
}
|
||||
|
||||
/// The format of the words a format's attribute is fetched as.
|
||||
fn word_format(format: VertexFormat) -> Format {
|
||||
match format.vulkan().1 / 4 {
|
||||
1 => Format::R32Uint,
|
||||
2 => Format::R32G32Uint,
|
||||
3 => Format::R32G32B32Uint,
|
||||
_ => Format::R32G32B32A32Uint,
|
||||
}
|
||||
}
|
||||
|
||||
/// Validates the stage-in layout of a post-tessellation vertex function,
|
||||
/// whose evaluation stage fetches it from the bound buffers.
|
||||
pub fn patch_input(capabilities: &Capabilities, descriptor: &VertexDescriptor) -> Result<()> {
|
||||
@@ -668,12 +710,32 @@ mod tests {
|
||||
),
|
||||
Err(Error::Unsupported("vertex attribute offset"))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn formats_the_device_cannot_fetch_are_fetched_as_words() {
|
||||
let descriptor = VertexDescriptor {
|
||||
attributes: alloc::vec![
|
||||
attribute(0, VertexFormat::Float2, 0, 0),
|
||||
attribute(3, VertexFormat::FloatRgb9E5, 8, 0),
|
||||
],
|
||||
layouts: alloc::vec![layout(0, 12, StepFunction::PerVertex, 1)],
|
||||
};
|
||||
let capabilities = capabilities();
|
||||
assert!(!capabilities.supports_vertex_format(VertexFormat::FloatRgb9E5));
|
||||
let words = word_formats(&capabilities, &descriptor);
|
||||
assert_eq!(words[3], Some(VertexFormat::FloatRgb9E5));
|
||||
assert_eq!(words.iter().flatten().count(), 1);
|
||||
let input = vertex_input(&capabilities, &descriptor).unwrap();
|
||||
assert_eq!(input.attributes[0].format, Format::R32G32Sfloat);
|
||||
assert_eq!(
|
||||
check(
|
||||
alloc::vec![attribute(0, VertexFormat::FloatRgb9E5, 0, 0)],
|
||||
alloc::vec![per_vertex]
|
||||
),
|
||||
Err(Error::Unsupported("vertex attribute format"))
|
||||
input.attributes[1],
|
||||
VertexAttribute {
|
||||
location: 3,
|
||||
binding: 0,
|
||||
format: Format::R32Uint,
|
||||
offset: 8
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user