feat(compiler): lower AIR clip distance outputs
This commit is contained in:
1 parent
79a3332883
commit
fcfac48aa7
4 files changed
+200
-34
No files matched your search
@@ -1134,7 +1134,12 @@ stela_air_metadata_stage(struct stela_air_metadata *m, LLVMValueRef fn,
|
||||
} else if (vertex && stela_air_md_string(fields[0], "air.viewport_array_index")) {
|
||||
io->semantic = STELA_AIR_VIEWPORT;
|
||||
} else if (vertex && stela_air_md_string(fields[0], "air.clip_distance")) {
|
||||
return fail(m, STELA_AIR_ARGUMENT, "clip distance outputs are unsupported");
|
||||
io->semantic = STELA_AIR_CLIP_DISTANCE;
|
||||
if (!stela_air_md_string(fields[f++], "air.clip_distance_array_size") ||
|
||||
!stela_air_md_uint(fields[f++], &io->clip_distances) || !io->clip_distances ||
|
||||
io->clip_distances > AIR_MAX_CLIP_DISTANCES)
|
||||
return fail(m, STELA_AIR_ARGUMENT, "clip distances need an array size of 1 to %u",
|
||||
AIR_MAX_CLIP_DISTANCES);
|
||||
} else if (fragment && stela_air_md_string(fields[0], "air.render_target")) {
|
||||
io->semantic = STELA_AIR_RENDER_TARGET;
|
||||
/* Dual-source blending writes index 1 of attachment 0. */
|
||||
@@ -1181,6 +1186,11 @@ stela_air_metadata_stage(struct stela_air_metadata *m, LLVMValueRef fn,
|
||||
case STELA_AIR_POSITION: type = io->primitive == STELA_AIR_FLOAT && io->components == 4; break;
|
||||
case STELA_AIR_VARYING: case STELA_AIR_RENDER_TARGET: type = stage_varying(io->primitive); break;
|
||||
case STELA_AIR_POINT_SIZE: case STELA_AIR_DEPTH: type = io->primitive == STELA_AIR_FLOAT && scalar; break;
|
||||
case STELA_AIR_CLIP_DISTANCE:
|
||||
type = io->primitive == STELA_AIR_FLOAT && scalar && LLVMGetTypeKind(leaf) == LLVMArrayTypeKind &&
|
||||
LLVMGetArrayLength2(leaf) == io->clip_distances;
|
||||
leaf = type ? LLVMGetElementType(leaf) : leaf;
|
||||
break;
|
||||
default: type = integer && scalar; break; /* Layer, viewport, sample mask and stencil. */
|
||||
}
|
||||
if (!type || !stage_type_matches(leaf, io->primitive, io->components))
|
||||
|
||||
@@ -9,6 +9,7 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
#define AIR_MAX_ARGUMENTS 128
|
||||
#define AIR_MAX_CLIP_DISTANCES 8
|
||||
#define AIR_MAX_VERTEX_ATTRIBUTES 31u
|
||||
#define AIR_MAX_RENDER_TARGETS 8u
|
||||
#define AIR_MAX_METADATA_OPERANDS 256
|
||||
@@ -180,6 +181,7 @@ enum stela_air_stage_semantic {
|
||||
STELA_AIR_VERTEX_INPUT, /* A stage-in attribute at location. */
|
||||
/* Vertex outputs. */
|
||||
STELA_AIR_POINT_SIZE, STELA_AIR_LAYER, STELA_AIR_VIEWPORT,
|
||||
STELA_AIR_CLIP_DISTANCE, /* A [clip_distances x float] leaf. */
|
||||
/* Fragment outputs. */
|
||||
STELA_AIR_DEPTH, STELA_AIR_SAMPLE_MASK, STELA_AIR_STENCIL,
|
||||
};
|
||||
@@ -217,6 +219,7 @@ struct stela_air_stage_io {
|
||||
bool used, invariant;
|
||||
/* RENDER_TARGET attachment and dual-source index; VERTEX_INPUT attribute. */
|
||||
unsigned location, index;
|
||||
unsigned clip_distances; /* Only CLIP_DISTANCE: 1 to AIR_MAX_CLIP_DISTANCES. */
|
||||
const char *name, *type_name, *identifier;
|
||||
size_t name_bytes, type_name_bytes, identifier_bytes;
|
||||
unsigned member_count;
|
||||
|
||||
@@ -1298,6 +1298,56 @@ put_lanes(struct translator *t, LLVMValueRef llvm, nir_def **lanes, unsigned n,
|
||||
fail(t, STELA_AIR_ALLOCATION, "render lane map allocation failed");
|
||||
}
|
||||
|
||||
/* A clip-distance array, the one aggregate a render aggregate holds. */
|
||||
static bool
|
||||
clip_array(LLVMTypeRef type)
|
||||
{
|
||||
return LLVMGetTypeKind(type) == LLVMArrayTypeKind && LLVMGetArrayLength2(type) &&
|
||||
LLVMGetArrayLength2(type) <= AIR_MAX_CLIP_DISTANCES &&
|
||||
LLVMGetTypeKind(LLVMGetElementType(type)) == LLVMFloatTypeKind;
|
||||
}
|
||||
|
||||
/* A render aggregate's element count: a packed struct of scalar, vector and
|
||||
* clip-array leaves, or a clip array. Zero for any other type. */
|
||||
static unsigned
|
||||
aggregate_count(LLVMTypeRef type)
|
||||
{
|
||||
if (clip_array(type))
|
||||
return (unsigned)LLVMGetArrayLength2(type);
|
||||
if (LLVMGetTypeKind(type) != LLVMStructTypeKind || LLVMIsOpaqueStruct(type) ||
|
||||
!LLVMIsPackedStruct(type) || !LLVMCountStructElementTypes(type) ||
|
||||
LLVMCountStructElementTypes(type) > AIR_MAX_ARGUMENTS)
|
||||
return 0;
|
||||
for (unsigned i = 0; i < LLVMCountStructElementTypes(type); i++) {
|
||||
LLVMTypeRef element = LLVMStructGetTypeAtIndex(type, i);
|
||||
if (!leaf_shape(element) && !clip_array(element))
|
||||
return 0;
|
||||
}
|
||||
return LLVMCountStructElementTypes(type);
|
||||
}
|
||||
|
||||
static LLVMTypeRef
|
||||
aggregate_element_type(LLVMTypeRef type, unsigned index)
|
||||
{
|
||||
return LLVMGetTypeKind(type) == LLVMStructTypeKind ? LLVMStructGetTypeAtIndex(type, index) :
|
||||
LLVMGetElementType(type);
|
||||
}
|
||||
|
||||
/* An aggregate of `count` elements, copied from `from` or undefined. */
|
||||
static struct value *
|
||||
new_aggregate(struct translator *t, unsigned count, const struct value *from)
|
||||
{
|
||||
struct value *v = rzalloc(t->values, struct value);
|
||||
if (!v || !(v->elements = rzalloc_array(t->values, struct value *, count))) {
|
||||
fail(t, STELA_AIR_ALLOCATION, "aggregate value allocation failed");
|
||||
return NULL;
|
||||
}
|
||||
v->aggregate_count = count;
|
||||
if (from)
|
||||
memcpy(v->elements, from->elements, count * sizeof(*v->elements));
|
||||
return v;
|
||||
}
|
||||
|
||||
static struct value *
|
||||
aggregate_value(struct translator *t, LLVMValueRef llvm)
|
||||
{
|
||||
@@ -1306,53 +1356,73 @@ aggregate_value(struct translator *t, LLVMValueRef llvm)
|
||||
if (v->aggregate_count) return v;
|
||||
fail(t, STELA_AIR_TYPE, "nonaggregate render value"); return NULL;
|
||||
}
|
||||
LLVMTypeRef type = LLVMTypeOf(llvm);
|
||||
if (LLVMGetTypeKind(type) != LLVMStructTypeKind || LLVMIsOpaqueStruct(type) ||
|
||||
!LLVMIsPackedStruct(type) || !LLVMCountStructElementTypes(type) ||
|
||||
LLVMCountStructElementTypes(type) > AIR_MAX_ARGUMENTS ||
|
||||
(!LLVMIsUndef(llvm) && !LLVMIsPoison(llvm))) {
|
||||
unsigned count = aggregate_count(LLVMTypeOf(llvm));
|
||||
if (!count || (!LLVMIsUndef(llvm) && !LLVMIsPoison(llvm))) {
|
||||
fail(t, STELA_AIR_TYPE, "unsupported/forward render aggregate"); return NULL;
|
||||
}
|
||||
unsigned count = LLVMCountStructElementTypes(type);
|
||||
for (unsigned i = 0; i < count; i++) {
|
||||
if (!leaf_shape(LLVMStructGetTypeAtIndex(type, i))) {
|
||||
fail(t, STELA_AIR_TYPE, "aggregate requires flat scalar/vector leaves"); return NULL;
|
||||
}
|
||||
}
|
||||
v = rzalloc(t->values, struct value);
|
||||
if (!v || !(v->elements = rzalloc_array(t->values, struct value *, count)) ||
|
||||
!_mesa_hash_table_insert(t->values, llvm, v)) {
|
||||
v = new_aggregate(t, count, NULL);
|
||||
if (v && !_mesa_hash_table_insert(t->values, llvm, v)) {
|
||||
fail(t, STELA_AIR_ALLOCATION, "aggregate value allocation failed"); return NULL;
|
||||
}
|
||||
v->aggregate_count = count;
|
||||
return v;
|
||||
}
|
||||
|
||||
/* `a`, of LLVM type `type`, with `leaf` at the index path, copying each
|
||||
* aggregate on the path. */
|
||||
static struct value *
|
||||
aggregate_insert(struct translator *t, const struct value *a, LLVMTypeRef type,
|
||||
const unsigned *indices, unsigned depth, struct value *leaf)
|
||||
{
|
||||
if (indices[0] >= a->aggregate_count) {
|
||||
fail(t, STELA_AIR_INSTRUCTION, "invalid aggregate leaf index");
|
||||
return NULL;
|
||||
}
|
||||
struct value *v = new_aggregate(t, a->aggregate_count, a);
|
||||
if (!v)
|
||||
return NULL;
|
||||
if (depth == 1) {
|
||||
v->elements[indices[0]] = leaf;
|
||||
return v;
|
||||
}
|
||||
LLVMTypeRef inner_type = aggregate_element_type(type, indices[0]);
|
||||
const struct value *inner = a->elements[indices[0]];
|
||||
if (!inner && !(inner = new_aggregate(t, aggregate_count(inner_type), NULL)))
|
||||
return NULL;
|
||||
if (!inner->aggregate_count) {
|
||||
fail(t, STELA_AIR_INSTRUCTION, "aggregate index path enters a leaf");
|
||||
return NULL;
|
||||
}
|
||||
v->elements[indices[0]] = aggregate_insert(t, inner, inner_type, indices + 1, depth - 1, leaf);
|
||||
return v->elements[indices[0]] ? v : NULL;
|
||||
}
|
||||
|
||||
static bool
|
||||
stage_value_instruction(struct translator *t, LLVMValueRef inst, LLVMOpcode op)
|
||||
{
|
||||
if (op == LLVMInsertValue || op == LLVMExtractValue) {
|
||||
if (LLVMGetNumIndices(inst) != 1)
|
||||
return fail(t, STELA_AIR_INSTRUCTION, "nested aggregate indexing unsupported");
|
||||
/* Paths are a struct leaf, an array element or a struct's array element. */
|
||||
unsigned depth = LLVMGetNumIndices(inst);
|
||||
const unsigned *indices = LLVMGetIndices(inst);
|
||||
struct value *a = aggregate_value(t, LLVMGetOperand(inst, 0));
|
||||
unsigned index = LLVMGetIndices(inst)[0];
|
||||
if (!a || index >= a->aggregate_count)
|
||||
return fail(t, STELA_AIR_INSTRUCTION, "invalid aggregate leaf index");
|
||||
if (!a || !depth || depth > 2)
|
||||
return fail(t, STELA_AIR_INSTRUCTION, "invalid aggregate index path");
|
||||
if (op == LLVMExtractValue) {
|
||||
struct value *leaf = a->elements[index];
|
||||
struct value *leaf = a;
|
||||
for (unsigned i = 0; leaf && i < depth; i++)
|
||||
leaf = indices[i] < leaf->aggregate_count ? leaf->elements[indices[i]] : NULL;
|
||||
if (leaf && leaf->aggregate_count)
|
||||
return _mesa_hash_table_insert(t->values, inst, leaf) != NULL ||
|
||||
fail(t, STELA_AIR_ALLOCATION, "aggregate extraction map failed");
|
||||
return (leaf && leaf->def && put_value(t, inst, leaf->def, 0)) ||
|
||||
fail(t, STELA_AIR_INSTRUCTION, "undefined/poison aggregate leaf escapes");
|
||||
}
|
||||
struct value *leaf = partial_value(t, LLVMGetOperand(inst, 1));
|
||||
if (!leaf) return false;
|
||||
struct value *v = rzalloc(t->values, struct value);
|
||||
if (!v || !(v->elements = ralloc_array(t->values, struct value *, a->aggregate_count)))
|
||||
return fail(t, STELA_AIR_ALLOCATION, "aggregate insertion allocation failed");
|
||||
v->aggregate_count = a->aggregate_count;
|
||||
memcpy(v->elements, a->elements, a->aggregate_count * sizeof(*v->elements));
|
||||
v->elements[index] = leaf;
|
||||
return _mesa_hash_table_insert(t->values, inst, v) != NULL ||
|
||||
fail(t, STELA_AIR_ALLOCATION, "aggregate insertion map failed");
|
||||
LLVMValueRef inserted = LLVMGetOperand(inst, 1);
|
||||
struct value *leaf = aggregate_count(LLVMTypeOf(inserted)) ? aggregate_value(t, inserted) :
|
||||
partial_value(t, inserted);
|
||||
struct value *v = leaf ? aggregate_insert(t, a, LLVMTypeOf(LLVMGetOperand(inst, 0)), indices,
|
||||
depth, leaf) : NULL;
|
||||
return v && (_mesa_hash_table_insert(t->values, inst, v) != NULL ||
|
||||
fail(t, STELA_AIR_ALLOCATION, "aggregate insertion map failed"));
|
||||
}
|
||||
struct value *a = partial_value(t, LLVMGetOperand(inst, 0));
|
||||
if (!a) return false;
|
||||
@@ -1399,6 +1469,7 @@ stage_location(struct translator *t, const struct stela_air_stage_io *io)
|
||||
case STELA_AIR_POINT_SIZE: return VARYING_SLOT_PSIZ;
|
||||
case STELA_AIR_LAYER: return VARYING_SLOT_LAYER;
|
||||
case STELA_AIR_VIEWPORT: return VARYING_SLOT_VIEWPORT;
|
||||
case STELA_AIR_CLIP_DISTANCE: return VARYING_SLOT_CLIP_DIST0;
|
||||
case STELA_AIR_DEPTH: return FRAG_RESULT_DEPTH;
|
||||
case STELA_AIR_SAMPLE_MASK: return FRAG_RESULT_SAMPLE_MASK;
|
||||
case STELA_AIR_STENCIL: return FRAG_RESULT_STENCIL;
|
||||
@@ -1461,10 +1532,17 @@ stage_variable(struct translator *t, const struct stela_air_stage_io *io, bool o
|
||||
if (io->semantic == STELA_AIR_LAYER || io->semantic == STELA_AIR_VIEWPORT ||
|
||||
io->semantic == STELA_AIR_SAMPLE_MASK || io->semantic == STELA_AIR_STENCIL)
|
||||
base = GLSL_TYPE_INT;
|
||||
/* Clip distances are a compact float array, as SPIR-V's ClipDistance is. */
|
||||
bool clip = io->semantic == STELA_AIR_CLIP_DISTANCE;
|
||||
const struct glsl_type *type = clip ? glsl_array_type(glsl_float_type(), io->clip_distances, 0) :
|
||||
glsl_vector_type(base, io->components);
|
||||
nir_variable *var = nir_variable_create(t->b.shader, output ? nir_var_shader_out : nir_var_shader_in,
|
||||
glsl_vector_type(base, io->components), name);
|
||||
type, name);
|
||||
if (!var) { fail(t, STELA_AIR_ALLOCATION, "render IO variable allocation failed"); return NULL; }
|
||||
var->data.location = stage_location(t, io);
|
||||
var->data.compact = clip;
|
||||
if (clip)
|
||||
t->b.shader->info.clip_distance_array_size = io->clip_distances;
|
||||
var->data.index = io->semantic == STELA_AIR_RENDER_TARGET ? io->index : 0;
|
||||
var->data.invariant = io->invariant;
|
||||
if (io->semantic == STELA_AIR_DEPTH) {
|
||||
@@ -1583,14 +1661,32 @@ stage_return(struct translator *t, LLVMValueRef inst)
|
||||
return fail(t, STELA_AIR_TYPE, "render output aggregate mismatch");
|
||||
}
|
||||
nir_def *values[AIR_MAX_ARGUMENTS];
|
||||
nir_def *clip[AIR_MAX_CLIP_DISTANCES];
|
||||
for (unsigned i = 0; i < t->render.entry.output_count; i++) {
|
||||
values[i] = aggregate ? (aggregate->elements[i] ? aggregate->elements[i]->def : NULL) : get_value(t, returned);
|
||||
const struct stela_air_stage_io *io = &t->render.outputs[i];
|
||||
const struct value *leaf = aggregate ? aggregate->elements[i] : NULL;
|
||||
if (io->semantic == STELA_AIR_CLIP_DISTANCE) {
|
||||
/* The one clip-distance output: every distance is defined. */
|
||||
for (unsigned j = 0; leaf && j < io->clip_distances; j++) {
|
||||
clip[j] = leaf->elements[j] ? leaf->elements[j]->def : NULL;
|
||||
if (!clip[j]) leaf = NULL;
|
||||
}
|
||||
values[i] = leaf ? clip[0] : NULL;
|
||||
} else {
|
||||
values[i] = aggregate ? (leaf ? leaf->def : NULL) : get_value(t, returned);
|
||||
}
|
||||
if (!values[i]) return fail(t, STELA_AIR_INSTRUCTION, "undefined/poison render output leaf/lane");
|
||||
}
|
||||
/* Validate ALL leaves before emitting any output effect. */
|
||||
nir_def *layer = NULL, *viewport = NULL;
|
||||
for (unsigned i = 0; i < t->render.entry.output_count; i++) {
|
||||
const struct stela_air_stage_io *io = &t->render.outputs[i];
|
||||
if (io->semantic == STELA_AIR_CLIP_DISTANCE) {
|
||||
nir_deref_instr *array = nir_build_deref_var(&t->b, t->outputs[i]);
|
||||
for (unsigned j = 0; j < io->clip_distances; j++)
|
||||
nir_store_deref(&t->b, nir_build_deref_array_imm(&t->b, array, j), clip[j], 1);
|
||||
continue;
|
||||
}
|
||||
nir_def *value = stage_widen(&t->b, values[i], io->primitive);
|
||||
if (t->stage_options->amplified && t->outputs[i] == t->layer)
|
||||
layer = value;
|
||||
|
||||
@@ -223,6 +223,62 @@ vertex_functions_fetch_attributes_and_write_builtins(void)
|
||||
ralloc_free(shader);
|
||||
}
|
||||
|
||||
#define CLIP_RESULT "<{ <4 x float>, [2 x float] }>"
|
||||
|
||||
/* A vertex function writing its position and two clip distances, the second
|
||||
* through a nested index path, then the whole array. */
|
||||
static const char *clip_vertex =
|
||||
"define " CLIP_RESULT " @vertex(<4 x float> %p) {\n"
|
||||
"%x = extractelement <4 x float> %p, i32 0\n"
|
||||
"%a0 = insertvalue " CLIP_RESULT " undef, <4 x float> %p, 0\n"
|
||||
"%a1 = insertvalue " CLIP_RESULT " %a0, float %x, 1, 0\n"
|
||||
"%a2 = insertvalue " CLIP_RESULT " %a1, float 2.0, 1, 1\n"
|
||||
"%c0 = insertvalue [2 x float] undef, float 3.0, 0\n"
|
||||
"%c1 = insertvalue [2 x float] %c0, float %x, 1\n"
|
||||
"%a3 = insertvalue " CLIP_RESULT " %a2, [2 x float] %c1, 1\n"
|
||||
"ret " CLIP_RESULT " %a3\n}\n"
|
||||
"!air.vertex = !{!2}\n"
|
||||
"!2 = !{ptr @vertex, !3, !4}\n!3 = !{!30, !31}\n"
|
||||
"!30 = !{!\"air.position\", !\"air.arg_type_name\", !\"float4\", !\"air.arg_name\", !\"position\"}\n"
|
||||
"!31 = !{!\"air.clip_distance\", !\"air.clip_distance_array_size\", i32 2, !\"air.arg_type_name\","
|
||||
" !\"float\", !\"air.arg_name\", !\"mtl_clip_distance\"}\n"
|
||||
"!4 = !{!40}\n"
|
||||
"!40 = !{i32 0, !\"air.vertex_input\", !\"air.location_index\", i32 0, i32 1,"
|
||||
" !\"air.arg_type_name\", !\"float4\", !\"air.arg_name\", !\"p\"}\n";
|
||||
|
||||
static void
|
||||
vertex_functions_write_clip_distances(void)
|
||||
{
|
||||
struct stela_air_error error = {0};
|
||||
nir_shader *shader = translate(clip_vertex, STELA_AIR_VERTEX, false, &error);
|
||||
if (!shader)
|
||||
fprintf(stderr, "clip distances: %u %s\n", error.kind, error.detail);
|
||||
assert(shader);
|
||||
nir_variable *clip = variable(shader, nir_var_shader_out, VARYING_SLOT_CLIP_DIST0, 0);
|
||||
assert(clip && clip->data.compact && glsl_get_length(clip->type) == 2 &&
|
||||
shader->info.clip_distance_array_size == 2);
|
||||
ralloc_free(shader);
|
||||
|
||||
const struct {
|
||||
const char *old, *replacement;
|
||||
enum stela_air_error_kind kind;
|
||||
} cases[] = {
|
||||
/* Every distance is written. */
|
||||
{"%a3 = insertvalue " CLIP_RESULT " %a2, [2 x float] %c1, 1\n",
|
||||
"%a3 = insertvalue " CLIP_RESULT " %a1, [2 x float] undef, 1\n", STELA_AIR_INSTRUCTION},
|
||||
{"i32 2, !\"air.arg_type_name\"", "i32 9, !\"air.arg_type_name\"", STELA_AIR_ARGUMENT},
|
||||
{"i32 2, !\"air.arg_type_name\"", "i32 3, !\"air.arg_type_name\"", STELA_AIR_TYPE},
|
||||
};
|
||||
for (unsigned i = 0; i < ARRAY_SIZE(cases); i++) {
|
||||
char *text = change(clip_vertex, cases[i].old, cases[i].replacement);
|
||||
shader = translate(text, STELA_AIR_VERTEX, false, &error);
|
||||
if (shader || error.kind != cases[i].kind)
|
||||
fprintf(stderr, "clip case %u: %u %s\n", i, error.kind, error.detail);
|
||||
assert(!shader && error.kind == cases[i].kind);
|
||||
free(text);
|
||||
}
|
||||
}
|
||||
|
||||
/* position_only with a buffer `v` in `space` and its body. */
|
||||
static char *
|
||||
with_buffer(unsigned space, const char *body)
|
||||
@@ -378,6 +434,7 @@ main(void)
|
||||
vertex_functions_fetch_attributes_and_write_builtins();
|
||||
vertex_functions_read_constant_data();
|
||||
vertex_functions_access_device_buffers();
|
||||
vertex_functions_write_clip_distances();
|
||||
fragment_functions_interpolate_and_write_targets();
|
||||
refuses_unrepresentable_render_io();
|
||||
glsl_type_singleton_decref();
|
||||
|
||||
Reference in new issue
Block a user