🐛 Normalize a GL patch control point input's fields
94b38e703 took a GL kernel's inputs through the lowering's input rules, and
said the tessellated draw's kernel had the generator's bare attribute name
in its argument 0. It does, but one level down. The kernel the renderer
makes of a tessellated draw's vertex and control shaders takes the draw's
vertices as a patch control point input, and the bare name is in that
input's field node. The field also has the key air.arg_type_name where the
type name goes. The library dumped as the device made it shows both
(measured: macOS 27.0.1 arm64, m3-max-30158). metal2vulkan reads a field
through the same attributes as any input, so it still refused the kernel's
compute pipeline ("UnknownKey" at argument 0, "attrib_0"), and the renderer
still drew the draw's vertices in software.
A patch control point input's field node k now loses the bare name. Where
its type name is the key, it gets the Metal name of member k of the control
point function's result, which is what `metal` writes: one field per
member, in order. The rule applies in vertex functions and kernels alike.
gl_tests holds the generator's shape in a kernel and in a
post-tessellation vertex function, and checks that the input as `metal`
writes it comes through byte for byte. The header and the gl_tests
comment no longer put the name in the kernel's input node itself.
For the guest: the fields of a GL tessellated draw's kernel are now what
`metal` writes. The device still refuses the kernel, now at its patch
control point role (m3-max-30171). metal2vulkan refuses that role in a
kernel as Metal does: Metal compiles no kernel with a patch control point
input (measured on the M3 by metal2vulkan, m3-max-30197, m3-max-30201 and
m3-max-30203). The renderer draws the draw's vertices in software until the
device makes the kernel one Metal compiles.
This commit is contained in:
1 parent
927cafcf6f
commit
4059880823
3 files changed
+213
-28
No files matched your search
@@ -464,6 +464,104 @@ bool llvmTypeSize(std::string t, uint64_t &size, uint64_t &align)
|
||||
return true;
|
||||
}
|
||||
|
||||
// The Metal name of LLVM type `t` as a module's text writes it, as metalTypeName names a type:
|
||||
// "float2" for <2 x float>, "uint" for i32; integers unsigned. "" for any other type.
|
||||
std::string metalTextTypeName(std::string t)
|
||||
{
|
||||
while (!t.empty() && t.front() == ' ')
|
||||
t.erase(0, 1);
|
||||
while (!t.empty() && t.back() == ' ')
|
||||
t.pop_back();
|
||||
unsigned lanes = 1;
|
||||
if (t.size() > 2 && t.front() == '<' && t.back() == '>') {
|
||||
const std::string inner = t.substr(1, t.size() - 2);
|
||||
const size_t x = inner.find(" x ");
|
||||
char *end = nullptr;
|
||||
lanes = static_cast<unsigned>(std::strtoul(inner.c_str(), &end, 10));
|
||||
if (x == std::string::npos || end != inner.c_str() + x || lanes < 2)
|
||||
return "";
|
||||
t = inner.substr(x + 3);
|
||||
}
|
||||
static const struct {
|
||||
const char *llvm, *metal;
|
||||
} kNames[] = {{"half", "half"}, {"float", "float"}, {"i1", "bool"}, {"i8", "uchar"},
|
||||
{"i16", "ushort"}, {"i32", "uint"}, {"i64", "ulong"}};
|
||||
for (const auto &n : kNames)
|
||||
if (t == n.llvm)
|
||||
return lanes > 1 ? n.metal + std::to_string(lanes) : n.metal;
|
||||
return "";
|
||||
}
|
||||
|
||||
// The member types of the result of the function type an operand holds
|
||||
// ("{ <4 x float>, <2 x float> } (i32, i8*)* @fn"): a struct's members, or the one result.
|
||||
std::vector<std::string> resultMembers(const std::string &op)
|
||||
{
|
||||
std::vector<std::string> members;
|
||||
if (op.empty() || op.front() != '{') {
|
||||
const size_t open = op.find(" (");
|
||||
if (open != std::string::npos)
|
||||
members.push_back(op.substr(0, open));
|
||||
return members;
|
||||
}
|
||||
std::string member;
|
||||
int depth = 0;
|
||||
for (size_t i = 1; i < op.size(); i++) {
|
||||
const char c = op[i];
|
||||
if (c == '(' || c == '{' || c == '<' || c == '[')
|
||||
depth++;
|
||||
else if (c == ')' || c == '}' || c == '>' || c == ']')
|
||||
depth--;
|
||||
if (depth < 0 || (depth == 0 && c == ',')) {
|
||||
members.push_back(member);
|
||||
member.clear();
|
||||
if (depth < 0)
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
member += c;
|
||||
}
|
||||
return members;
|
||||
}
|
||||
|
||||
// Drops an input's attribute name, which the generator writes as a bare string after its
|
||||
// location's two operands and `metal` does not; true where it did.
|
||||
bool dropAttributeName(NodeText &n)
|
||||
{
|
||||
for (size_t i = 0; i + 3 < n.ops.size(); i++) {
|
||||
std::string key, bare;
|
||||
unsigned a = 0, b = 0;
|
||||
if (stringOp(n.ops[i], key) && key == "air.location_index" && intOp(n.ops[i + 1], a) &&
|
||||
intOp(n.ops[i + 2], b) && stringOp(n.ops[i + 3], bare) && bare.compare(0, 4, "air.") != 0) {
|
||||
n.ops.erase(n.ops.begin() + static_cast<std::ptrdiff_t>(i + 3));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Makes a field node of a patch control point input what `metal` writes: no attribute name, and
|
||||
// the Metal name `type` of the field's member of the control point function's result where the
|
||||
// generator wrote the key air.arg_type_name in place of a type name. True where it changed.
|
||||
bool normalizeControlPointField(std::string &line, const std::string &type)
|
||||
{
|
||||
NodeText n;
|
||||
if (!splitNode(line, n))
|
||||
return false;
|
||||
bool edited = dropAttributeName(n);
|
||||
for (size_t i = 0; i + 1 < n.ops.size(); i++) {
|
||||
std::string key, value;
|
||||
if (stringOp(n.ops[i], key) && key == "air.arg_type_name" && stringOp(n.ops[i + 1], value) &&
|
||||
value.compare(0, 4, "air.") == 0 && !type.empty()) {
|
||||
n.ops[i + 1] = "!\"" + type + "\"";
|
||||
edited = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (edited)
|
||||
line = joinNode(n);
|
||||
return edited;
|
||||
}
|
||||
|
||||
// Makes one output or input node's text what the rules in RgfxGLLowering.hpp say; true where it
|
||||
// changed. `type` is the Metal name of the value's type from the entry point's signature, and
|
||||
// `param` an input's parameter in the entry point's function type (parameterTypes).
|
||||
@@ -472,21 +570,11 @@ bool normalizeNode(std::string &line, bool input, const std::string &type, const
|
||||
NodeText n;
|
||||
if (!splitNode(line, n))
|
||||
return false;
|
||||
// An input's attribute name, which the generator writes as a bare string after the location's
|
||||
// two operands (a vertex function's vertex input, a kernel's input), is not in the node `metal`
|
||||
// writes.
|
||||
bool edited = false;
|
||||
for (size_t i = 0; input && i + 3 < n.ops.size(); i++) {
|
||||
std::string key, bare;
|
||||
unsigned a = 0, b = 0;
|
||||
if (stringOp(n.ops[i], key) && key == "air.location_index" && intOp(n.ops[i + 1], a) &&
|
||||
intOp(n.ops[i + 2], b) && stringOp(n.ops[i + 3], bare) && bare.compare(0, 4, "air.") != 0) {
|
||||
n.ops.erase(n.ops.begin() + static_cast<std::ptrdiff_t>(i + 3));
|
||||
line = joinNode(n);
|
||||
edited = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// An input's attribute name (a vertex function's vertex input, a kernel's input) is not in the
|
||||
// node `metal` writes.
|
||||
bool edited = input && dropAttributeName(n);
|
||||
if (edited)
|
||||
line = joinNode(n);
|
||||
// A flat fragment input's sampling key: `metal` writes [[flat]] as air.flat alone.
|
||||
bool flat = false, fragmentInput = false;
|
||||
for (const std::string &op : n.ops) {
|
||||
@@ -740,6 +828,21 @@ bool RgfxGLNormalizeStageText(std::string &text, const RgfxGLSignature &signatur
|
||||
param < signature.parameters.size() ? signature.parameters[param]
|
||||
: std::string(),
|
||||
param < params.size() ? params[param] : ParameterType());
|
||||
// A patch control point input's fields, one per member of its control point function's
|
||||
// result, in order.
|
||||
std::string kind;
|
||||
unsigned function = 0;
|
||||
NodeText f;
|
||||
if (in.ops.size() < 3 || !stringOp(in.ops[1], kind) || kind != "air.patch_control_point_input" ||
|
||||
!refOp(in.ops[2], function) || !node(function, f) || f.ops.size() < 2)
|
||||
continue;
|
||||
const std::vector<std::string> members = resultMembers(f.ops[1]);
|
||||
for (size_t k = 0; k + 3 < in.ops.size(); k++) {
|
||||
unsigned field = 0;
|
||||
if (refOp(in.ops[k + 3], field) && field < nodeLine.size() && nodeLine[field] < lines.size())
|
||||
changed |= normalizeControlPointField(
|
||||
lines[nodeLine[field]], k < members.size() ? metalTextTypeName(members[k]) : std::string());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!changed)
|
||||
|
||||
@@ -55,11 +55,15 @@
|
||||
* 4096 buffers, not the builtins' 31);
|
||||
* - an input's attribute name, a bare string after its air.location_index's two operands, is
|
||||
* dropped (the input keeps its air.arg_name, which holds the same name), in a vertex
|
||||
* function's inputs and a kernel's alike. The kernel the renderer makes for a tessellated
|
||||
* draw has one at its argument 0 ("attrib_0"): metal2vulkan refused that kernel's compute
|
||||
* pipeline ("UnknownKey"), and the renderer drew the draw's vertices in software (measured:
|
||||
* macOS 27.0.1 arm64, a quad patch through a tessellation control and evaluation shader,
|
||||
* m3-max-30129);
|
||||
* function's inputs and a kernel's alike, and so is a patch control point input's field's.
|
||||
* Where such a field's air.arg_type_name holds the key itself in place of a type name, it
|
||||
* gets the Metal name of its member of the control point function's result (`metal` writes
|
||||
* one field per member, in order). The kernel the renderer makes for a tessellated draw, of
|
||||
* its vertex and control shaders, takes the draw's vertices as a patch control point input
|
||||
* whose field has both ("attrib_0", and the key for its type): metal2vulkan refused that
|
||||
* kernel's compute pipeline ("UnknownKey" at its argument 0, "attrib_0"), and the renderer
|
||||
* drew the draw's vertices in software (measured: macOS 27.0.1 arm64, a quad patch through a
|
||||
* tessellation control and evaluation shader, m3-max-30129 and m3-max-30158);
|
||||
* - a flat fragment input has no sampling key: `metal` writes [[flat]] as air.flat alone, and
|
||||
* the generator writes air.center before it;
|
||||
* - a buffer input has the air.address_space `metal` writes, its parameter's pointer's, and,
|
||||
@@ -114,9 +118,10 @@ struct RgfxGLSignature {
|
||||
/// Makes `text`, a module as LLVM prints it, follow the rules above: the module's (no gld.*
|
||||
/// lists; one version, its language Metal's; its own compile options and module flags, once
|
||||
/// each), and those of the stage metadata of its vertex, fragment or kernel entry point where it
|
||||
/// has one: an integer user() varying's air.arg_type_name is the unsigned name, an input has no
|
||||
/// bare attribute name, a flat fragment input no sampling key, a buffer input its address space
|
||||
/// and pointee size, and an output or input node without air.arg_type_name gets the name
|
||||
/// has one: an integer user() varying's air.arg_type_name is the unsigned name, an input or a
|
||||
/// patch control point input's field has no bare attribute name (and such a field its member's
|
||||
/// type name), a flat fragment input no sampling key, a buffer input its address space and
|
||||
/// pointee size, and an output or input node without air.arg_type_name gets the name
|
||||
/// `signature` gives its value (an input's by its parameter index), and an air.arg_name where it
|
||||
/// has none. True where it changed `text`; `text` is unchanged otherwise.
|
||||
bool RgfxGLNormalizeStageText(std::string &text, const RgfxGLSignature &signature);
|
||||
|
||||
@@ -973,11 +973,10 @@ void test_stage_metadata_normalized()
|
||||
// The module's rules hold for a kernel too: the generator's lists go.
|
||||
text = kernel + "!gld.plugin_info = !{!1}\n";
|
||||
CHECK(RgfxGLNormalizeStageText(text, vertex) && text == kernel);
|
||||
// So do the input rules: a kernel's input loses the bare attribute name after its location,
|
||||
// as the generator writes one in the kernel of a tessellated draw ("attrib_0" at its argument
|
||||
// 0, which metal2vulkan refuses), and its buffer gets the address space of its parameter's
|
||||
// pointer. Our own text: the inputs as `metal` writes a stage_in kernel's, the bare name added
|
||||
// and the buffer's address space taken out.
|
||||
// So do the input rules: a kernel's input loses a bare attribute name after its location, and
|
||||
// its buffer gets the address space of its parameter's pointer. Our own text: the inputs as
|
||||
// `metal` writes a stage_in kernel's, a bare name added and the buffer's address space taken
|
||||
// out.
|
||||
{
|
||||
const std::string inputs = "!air.kernel = !{!0}\n"
|
||||
"!0 = !{void (<2 x float>, float addrspace(1)*, i32)* @__main, !1, !2}\n"
|
||||
@@ -1007,6 +1006,84 @@ void test_stage_metadata_normalized()
|
||||
CHECK(RgfxGLNormalizeStageText(text, compute) && text == normalized);
|
||||
CHECK(!RgfxGLNormalizeStageText(text, compute) && text == normalized);
|
||||
}
|
||||
// A patch control point input's fields as the generator writes them in the kernel of a
|
||||
// tessellated draw (measured: macOS 27.0.1 arm64, m3-max-30158): a bare attribute name after
|
||||
// the location, and the key air.arg_type_name where the type name goes. Each field loses the
|
||||
// name and gets its member's type name from the control point function's result, in order, in
|
||||
// a kernel and in a post-tessellation vertex function alike (three fields, an integer's name
|
||||
// unsigned). Our own text.
|
||||
{
|
||||
const std::string kernelHead = "!air.kernel = !{!0}\n"
|
||||
"!0 = !{void (i8*, <2 x i32>)* @__main, !1, !2, !6}\n"
|
||||
"!1 = !{}\n"
|
||||
"!2 = !{!3, !5}\n"
|
||||
"!3 = !{i32 0, !\"air.patch_control_point_input\", !4, !7}\n"
|
||||
"!4 = !{!\"air.patch_control_point_function\", { <2 x float> } (i32, i8*)* @cpf}\n"
|
||||
"!5 = !{i32 1, !\"air.thread_position_in_grid\", !\"air.arg_type_name\", "
|
||||
"!\"uint2\", !\"air.arg_name\", !\"i\"}\n"
|
||||
"!6 = !{!\"air.max_work_group_size\", i32 32}\n";
|
||||
RgfxGLSignature compute;
|
||||
compute.parameters = {"", "uint2"};
|
||||
text = kernelHead +
|
||||
"!7 = !{!\"air.location_index\", i32 0, i32 1, !\"attrib_0\", !\"air.arg_type_name\", "
|
||||
"!\"air.arg_type_name\", !\"air.arg_name\", !\"attrib_0\"}\n";
|
||||
const std::string kernelFields = kernelHead + "!7 = !{!\"air.location_index\", i32 0, i32 1, "
|
||||
"!\"air.arg_type_name\", !\"float2\", !\"air.arg_name\", "
|
||||
"!\"attrib_0\"}\n";
|
||||
CHECK(RgfxGLNormalizeStageText(text, compute) && text == kernelFields);
|
||||
CHECK(!RgfxGLNormalizeStageText(text, compute) && text == kernelFields);
|
||||
|
||||
const std::string vertexHead =
|
||||
"!air.vertex = !{!0}\n"
|
||||
"!0 = !{<4 x float> (i8*, <2 x float>)* @__main, !1, !3, !9}\n"
|
||||
"!1 = !{!2}\n"
|
||||
"!2 = !{!\"air.position\", !\"air.arg_type_name\", !\"float4\", !\"air.arg_name\", !\"#air_position\"}\n"
|
||||
"!3 = !{!4, !8}\n"
|
||||
"!4 = !{i32 0, !\"air.patch_control_point_input\", !5, !6, !7, !10}\n"
|
||||
"!5 = !{!\"air.patch_control_point_function\", { <4 x float>, <2 x float>, i32 } (i32, i8*)* @cpf}\n";
|
||||
const std::string vertexTail =
|
||||
"!8 = !{i32 1, !\"air.position_in_patch\", !\"air.arg_type_name\", !\"float2\", !\"air.arg_name\", "
|
||||
"!\"uv\"}\n"
|
||||
"!9 = !{!\"air.patch\", !\"quad\", !\"air.patch_control_point\", i32 4}\n";
|
||||
auto field = [](unsigned node, unsigned location, const char *type, bool generated) {
|
||||
const std::string name = "attrib_" + std::to_string(location);
|
||||
return "!" + std::to_string(node) + " = !{!\"air.location_index\", i32 " + std::to_string(location) +
|
||||
", i32 1, " + (generated ? "!\"" + name + "\", " : std::string()) + "!\"air.arg_type_name\", !\"" +
|
||||
type + "\", !\"air.arg_name\", !\"" + name + "\"}\n";
|
||||
};
|
||||
RgfxGLSignature patch;
|
||||
patch.outputs = {"float4"};
|
||||
patch.parameters = {"", "float2"};
|
||||
text = vertexHead + field(6, 3, "air.arg_type_name", true) + field(7, 0, "air.arg_type_name", true) +
|
||||
vertexTail + field(10, 1, "air.arg_type_name", true);
|
||||
const std::string vertexFields = vertexHead + field(6, 3, "float4", false) + field(7, 0, "float2", false) +
|
||||
vertexTail + field(10, 1, "uint", false);
|
||||
CHECK(RgfxGLNormalizeStageText(text, patch) && text == vertexFields);
|
||||
|
||||
// The same input as `metal` writes it (an integer field named "int", the function's
|
||||
// parameter its opaque pointer, the position output without air.arg_name) comes through
|
||||
// unchanged, byte for byte.
|
||||
const std::string metal =
|
||||
"!air.vertex = !{!9}\n"
|
||||
"!9 = !{<4 x float> (%struct._patch_control_point_t*, <2 x float>)* @v, !10, !12, !19}\n"
|
||||
"!10 = !{!11}\n"
|
||||
"!11 = !{!\"air.position\", !\"air.arg_type_name\", !\"float4\"}\n"
|
||||
"!12 = !{!13, !18}\n"
|
||||
"!13 = !{i32 0, !\"air.patch_control_point_input\", !14, !15, !16, !17}\n"
|
||||
"!14 = !{!\"air.patch_control_point_function\", { <4 x float>, <2 x float>, i32 } (i32, "
|
||||
"%struct._patch_control_point_t*)* @cp}\n"
|
||||
"!15 = !{!\"air.location_index\", i32 3, i32 1, !\"air.arg_type_name\", !\"float4\", !\"air.arg_name\", "
|
||||
"!\"c\"}\n"
|
||||
"!16 = !{!\"air.location_index\", i32 0, i32 1, !\"air.arg_type_name\", !\"float2\", !\"air.arg_name\", "
|
||||
"!\"p\"}\n"
|
||||
"!17 = !{!\"air.location_index\", i32 1, i32 1, !\"air.arg_type_name\", !\"int\", !\"air.arg_name\", "
|
||||
"!\"k\"}\n"
|
||||
"!18 = !{i32 1, !\"air.position_in_patch\", !\"air.arg_type_name\", !\"float2\", !\"air.arg_name\", "
|
||||
"!\"uv\"}\n"
|
||||
"!19 = !{!\"air.patch\", !\"quad\", !\"air.patch_control_point\", i32 4}\n";
|
||||
text = metal;
|
||||
CHECK(!RgfxGLNormalizeStageText(text, patch) && text == metal);
|
||||
}
|
||||
text = "";
|
||||
CHECK(!RgfxGLNormalizeStageText(text, vertex) && text.empty());
|
||||
// An entry node naming nodes the text lacks.
|
||||
|
||||
Reference in new issue
Block a user