Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions dm/gfx/nvn/dmsh_abi.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@
// The corrections' function constant (bool, every stage): true turns them off, for a pipeline whose draw or dispatch binds no sampler with a
// twin (nvn_mtl.m border_need, fn_off). The twin samples go, and the registers they kept: Ocarina of Time's 1080p material passes spilled 300-400 bytes a thread with them. Undefined, every sample corrects as its bit says
#define DMSH_BORDER_FC 216
// The blend fix-up's function constant (bool, pixel shaders with dm_blend0, dmsh_rewrite.py blend_zero): true for a pipeline whose blend words are both 0 (nvn_mtl.m bz_word / rop_word:
// no zero-weight NaN rule and no NVIDIA rounding for any of its targets, which 89 % of Ocarina of Time's draws are), so the fix-up and the read of its words go. Undefined or false, the
// words are read at DMSH_BLEND_BUFFER as before (T-126 v2: the read alone cost 2-6 % of the hottest pixel shaders on an M2)
#define DMSH_BLEND_OFF_FC 217
// The logic operation's state (dm/gfx/tools/dmsh_logic.py, the _l entry points of the <key>_bx libraries): uint operation (NVN_LOGIC_OP_*), 12 bytes of padding, a uint4 channel mask per colour target, at this fragment buffer index (nvn_mtl.m lo_bind)
#define DMSH_LOGIC_BUFFER 29
// Its function constants: bool, colour target n is attached (the variant reads it only then), at this index + n (nvn_mtl.m lo_fn specializes the entry point per pass's targets)
Expand Down
2 changes: 1 addition & 1 deletion dm/gfx/nvn/dmsh_chain.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Generated file (dm/gfx/tools/dmsh_chain.py --header): the B shader library translation chain version this source tree expects (default options), one for every game;
// the host loads only <library dir>/b-<V>/. Run dmsh_chain.py --header after changing the chain's tools and commit this file with the change
#define DMSH_CHAIN "648a020c093b"
#define DMSH_CHAIN "1ae20f269d98"
59 changes: 36 additions & 23 deletions dm/gfx/nvn/nvn_mtl.m

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions dm/gfx/nvn/nvn_pso_skey.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ typedef struct {
uint32_t atest, logic; // atest: 1 = drawn with the alpha test, the fragment from the shader's side library <key>_bx (nvn_mtl.m atest_key); last, so a prewarm row written before it reads as 0
// logic: 1 = drawn with a logic operation, the fragment from the side library <key>_bx (lop_key); in what was padding (0 in older rows)
va_st attr_hi[16]; // NVN2's attributes 16 to 31 (pso_key_attr); last, so a prewarm row written before them reads as 0
uint32_t border, border_spare; // border: 1 = a sampler the draw binds has a twin, so the shaders keep their border colour corrections (nvn_mtl.m border_need); 0 = specialized without
// them (fn_off); last, so a prewarm row written before it reads as 0. border_spare keeps the key a whole number of 8-byte words
uint32_t border, blend_live; // border: 1 = a sampler the draw binds has a twin, so the shaders keep their border colour corrections (nvn_mtl.m border_need); 0 = specialized without
// them (fn_off); last, so a prewarm row written before it reads as 0. blend_live (T-126 v2; was a spare word, 0 in older rows): 1 = the draw's blend words
// (bz_word / rop_word) are not both 0, so the pixel shader keeps its blend fix-up; 0 = specialized without it (DMSH_BLEND_OFF_FC, fn_off)
} pso_key;
static inline va_st pso_key_attr(const pso_key *k, int a) { return a < 16 ? k->attr[a] : k->attr_hi[a - 16]; }

Expand Down Expand Up @@ -53,7 +54,7 @@ static inline void pso_skey_hex(const pso_skey *key, char *out) {
}

static inline int pso_skey_parse(const char *hex, pso_skey *out) {
size_t n = sizeof *out * 2, old2 = n - 2 * (sizeof out->k.border + sizeof out->k.border_spare), old1 = old2 - 2 * sizeof out->k.attr_hi, old = old1 - 2 * (sizeof out->k.atest + sizeof out->k.logic); // a row from before the border field, before the high attributes, or also before the alpha test field: the key without them
size_t n = sizeof *out * 2, old2 = n - 2 * (sizeof out->k.border + sizeof out->k.blend_live), old1 = old2 - 2 * sizeof out->k.attr_hi, old = old1 - 2 * (sizeof out->k.atest + sizeof out->k.logic); // a row from before the border field, before the high attributes, or also before the alpha test field: the key without them
if (!hex || (strlen(hex) != n && strlen(hex) != old2 && strlen(hex) != old1 && strlen(hex) != old)) return -1;
if (strlen(hex) != n) { n = strlen(hex); memset(out, 0, sizeof *out); }
uint8_t *p = (uint8_t *)out;
Expand Down
4 changes: 2 additions & 2 deletions dm/gfx/tests/t_blend_zero.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static void merged_case(NSString *source, prog_t *program, int on) {
" out.m_0 = float4(v, v, v, x < 4u ? 0.0 : 1.0);\n dm_blend0(out, dm_blend);\n dm_merged_out r = {};\n r.m_1 = out.m_0;\n return r;\n}\n"];
NSError *error = nil; id<MTLLibrary> lib = [g_dev newLibraryWithSource:merged options:nil error:&error];
CHECK(lib != nil); if (!lib) { fprintf(stderr, "%s\n", error.localizedDescription.UTF8String); return; }
id<MTLFunction> fn = fn4_note([lib newFunctionWithName:@"main_m"], lib, @"main_m", nil);
id<MTLFunction> fn = fn_spec(lib, @"main_m", [MTLFunctionConstantValues new], nil); // as shader_load: a function with constants (dm_blend_off) is specialized
CHECK(fn != nil); if (!fn) return;
static tex_t unused = {.magic = MAGIC('T', 'E', 'X', 0), .serial = 98202}, target = {.magic = MAGIC('T', 'E', 'X', 0), .serial = 98203};
unused.d = (nvn_texdesc){.width = 8, .height = 1, .depth = 1, .samples = 1, .levels = 1, .target = NVN_TEXTURE_TARGET_2D, .format = NVN_FORMAT_RGBA8};
Expand Down Expand Up @@ -113,7 +113,7 @@ int main(int argc, char **argv) { @autoreleasepool {
NvProg *pr = [NvProg new];
pr->sh[0] = [NvShader new]; pr->sh[0]->fn = [lib newFunctionWithName:@"vertex_main"]; pr->sh[0]->stage = 0; snprintf(pr->sh[0]->key, sizeof pr->sh[0]->key, "vblend_b");
NvShader *ps = [NvShader new]; pr->sh[1] = ps;
ps->fn = fn4_note([lib newFunctionWithName:@"main0"], lib, @"main0", nil); ps->stage = 1; ps->has_bz = 1; snprintf(ps->key, sizeof ps->key, "pblend_b");
ps->fn = fn_spec(lib, @"main0", [MTLFunctionConstantValues new], nil); ps->stage = 1; ps->has_bz = 1; snprintf(ps->key, sizeof ps->key, "pblend_b"); // as shader_load: a function with constants (dm_blend_off) is specialized
CHECK(ps->fn != nil);
static prog_t program; program.serial = 98200; serial_put(program.serial, pr);
merged_case(source, &program, on);
Expand Down
2 changes: 1 addition & 1 deletion dm/gfx/tests/t_border.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static void specialization_cases(void) {
id<MTLFunction> f = deferred == 2 ? geo_mesh_entry(lib, @"probe") : deferred ? fn_spec(lib, @"probe", cv, &error) : fn_lib_spec(lib, @"probe", cv, &error);
CHECK(f != nil, "original specialization exists (%d)", deferred); if (!f) continue;
id<MTLFunction> off = fn_dev_b(f, 1); // the variant is no function object of its own (fn_off): a descriptor and a device function beside f's
CHECK(fn_off(f) && fn4_of_b(f, 1) != fn4_of(f) && fn4_of_b(f, 1) == fn4_of_b(f, 1) && fn4_of_b(f, 0) == fn4_of(f), "border specialization has a descriptor of its own, cached (%d)", deferred);
CHECK(fn_off(f, FN_OFF_BORDER) && fn4_of_b(f, 1) != fn4_of(f) && fn4_of_b(f, 1) == fn4_of_b(f, 1) && fn4_of_b(f, 0) == fn4_of(f), "border specialization has a descriptor of its own, cached (%d)", deferred);
CHECK(off && off != fn_dev(f) && off == fn_dev_b(f, 1) && fn_dev_b(f, 0) == fn_dev(f), "its device function is distinct and cached (%d)", deferred);
MTL4SpecializedFunctionDescriptor *pre = (MTL4SpecializedFunctionDescriptor *)pre_fn(lib, @"probe");
id<MTLFunction> warm = fn_lib_spec(lib, @"probe", pre.constantValues, &error);
Expand Down
2 changes: 1 addition & 1 deletion dm/gfx/tests/t_pso_bg_look.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ int main(void) { @autoreleasepool {
// is then a specialization of that base, with no compile
{ NvProg *p = program(library(.25f), "pull", 1); static prog_t pg = {.serial = 990100}; serial_put(pg.serial, p);
pso_key k; state_reset(); S.prog = &pg; pso_key_fill(&k, p->sh[0], p->sh[1], (uint32_t[8]){70}, 0, 0, 1, 0);
NSError *e = nil; CHECK(pso_build(pso_desc_pull(&k, p->sh[0], p->sh[1]), !k.border, &e) != nil, "the pulling base builds (the prebuild's part)");
NSError *e = nil; CHECK(pso_build(pso_desc_pull(&k, p->sh[0], p->sh[1]), pso_off(&k), &e) != nil, "the pulling base builds (the prebuild's part)");
uint64_t made = g_hy_made, n0 = g_bgl_n, found = g_bgl_found, admit = g_pp_admit, kicked = g_pp_kicked;
predict(&pg);
CHECK(hm_get_id(&g_pso_hy, &k) == nil && g_hy_made == made, "the prediction makes no stand-in on the submit thread");
Expand Down
14 changes: 7 additions & 7 deletions dm/gfx/tests/t_pso_skey.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,29 @@ int main(void) {
NvShader *vs = [NvShader new]; snprintf(vs->key, sizeof vs->key, "vs-a");
pso_skey a, b; char hex[sizeof a * 2 + 1];
pso_skey_from(&k, vs, nil, 0, NULL, &a); pso_skey_hex(&a, hex);
size_t old = strlen(hex) - 2 * (sizeof k.attr_hi + sizeof k.atest + sizeof k.logic + sizeof k.border + sizeof k.border_spare); hex[old] = 0;
size_t old = strlen(hex) - 2 * (sizeof k.attr_hi + sizeof k.atest + sizeof k.logic + sizeof k.border + sizeof k.blend_live); hex[old] = 0;
assert(pso_skey_parse(hex, &b) == 0); assert(b.k.atest == 0 && b.k.logic == 0);
a.k.atest = a.k.logic = a.k.border = a.k.border_spare = 0; memset(a.k.attr_hi, 0, sizeof a.k.attr_hi); assert(memcmp(&a, &b, sizeof a) == 0); // NOLINT(bugprone-suspicious-memory-comparison): a pso_skey is its bytes (pso_skey_from zeroes it, pso_skey_hex writes them, the maps key on them)
a.k.atest = a.k.logic = a.k.border = a.k.blend_live = 0; memset(a.k.attr_hi, 0, sizeof a.k.attr_hi); assert(memcmp(&a, &b, sizeof a) == 0); // NOLINT(bugprone-suspicious-memory-comparison): a pso_skey is its bytes (pso_skey_from zeroes it, pso_skey_hex writes them, the maps key on them)
}
// A row written before pso_key.attr_hi (NVN2's vertex attributes 16 to 31): they read as 0, the fields before them stay
{
pso_key k; fill(&k, 11); k.atest = 1; k.logic = 1; pso_key_norm(&k, 0);
NvShader *vs = [NvShader new]; snprintf(vs->key, sizeof vs->key, "vs-h");
pso_skey a, b; char hex[sizeof a * 2 + 1];
pso_skey_from(&k, vs, nil, 0, NULL, &a); pso_skey_hex(&a, hex);
hex[strlen(hex) - 2 * (sizeof k.attr_hi + sizeof k.border + sizeof k.border_spare)] = 0;
hex[strlen(hex) - 2 * (sizeof k.attr_hi + sizeof k.border + sizeof k.blend_live)] = 0;
assert(pso_skey_parse(hex, &b) == 0); assert(b.k.atest == 1 && b.k.logic == 1);
a.k.border = a.k.border_spare = 0; memset(a.k.attr_hi, 0, sizeof a.k.attr_hi); assert(memcmp(&a, &b, sizeof a) == 0); // NOLINT(bugprone-suspicious-memory-comparison): the key is defined byte by byte, padding included: pso_skey_from clears it and its hex form is every byte
a.k.border = a.k.blend_live = 0; memset(a.k.attr_hi, 0, sizeof a.k.attr_hi); assert(memcmp(&a, &b, sizeof a) == 0); // NOLINT(bugprone-suspicious-memory-comparison): the key is defined byte by byte, padding included: pso_skey_from clears it and its hex form is every byte
}
// A row written before the border specialization keeps its high attributes and prewarms the no-correction pipeline. Live draws needing corrections have a different key.
{
pso_key k; fill(&k, 13); pso_key_norm(&k, 0);
NvShader *vs = [NvShader new]; snprintf(vs->key, sizeof vs->key, "vs-b");
pso_skey a, b; char hex[sizeof a * 2 + 1];
pso_skey_from(&k, vs, nil, 0, NULL, &a); pso_skey_hex(&a, hex);
hex[strlen(hex) - 2 * (sizeof k.border + sizeof k.border_spare)] = 0;
assert(pso_skey_parse(hex, &b) == 0 && b.k.border == 0 && b.k.border_spare == 0);
a.k.border = a.k.border_spare = 0; assert(memcmp(&a, &b, sizeof a) == 0); // NOLINT(bugprone-suspicious-memory-comparison): a pso_skey is its bytes (pso_skey_from zeroes it, pso_skey_hex writes them, the maps key on them)
hex[strlen(hex) - 2 * (sizeof k.border + sizeof k.blend_live)] = 0;
assert(pso_skey_parse(hex, &b) == 0 && b.k.border == 0 && b.k.blend_live == 0);
a.k.border = a.k.blend_live = 0; assert(memcmp(&a, &b, sizeof a) == 0); // NOLINT(bugprone-suspicious-memory-comparison): a pso_skey is its bytes (pso_skey_from zeroes it, pso_skey_hex writes them, the maps key on them)
a.k.border = 1; assert(memcmp(&a, &b, sizeof a) != 0); // NOLINT(bugprone-suspicious-memory-comparison): a pso_skey is its bytes (pso_skey_from zeroes it, pso_skey_hex writes them, the maps key on them)
}
// pso_key_attr: attribute 16 is the first of the high table
Expand Down
4 changes: 2 additions & 2 deletions dm/gfx/tests/t_rop_round.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static void merged_case(NSString *source, prog_t *program, int on) {
"{\n main0_out out = {};\n out.m_0 = float4(as_type<float>(0x3e858586u));\n dm_blend0(out, dm_blend);\n dm_merged_out r = {};\n r.m_1 = out.m_0;\n return r;\n}\n"];
NSError *error = nil; id<MTLLibrary> lib = [g_dev newLibraryWithSource:merged options:nil error:&error];
CHECK(lib != nil); if (!lib) { fprintf(stderr, "%s\n", error.localizedDescription.UTF8String); return; }
id<MTLFunction> fn = fn4_note([lib newFunctionWithName:@"main_m"], lib, @"main_m", nil);
id<MTLFunction> fn = fn_spec(lib, @"main_m", [MTLFunctionConstantValues new], nil); // as shader_load: a function with constants (dm_blend_off) is specialized
CHECK(fn != nil); if (!fn) return;
static tex_t unused = {.magic = MAGIC('T', 'E', 'X', 0), .serial = 98310}, target = {.magic = MAGIC('T', 'E', 'X', 0), .serial = 98311};
unused.d = (nvn_texdesc){.width = 8, .height = 1, .depth = 1, .samples = 1, .levels = 1, .target = NVN_TEXTURE_TARGET_2D, .format = NVN_FORMAT_RGBA16F};
Expand Down Expand Up @@ -90,7 +90,7 @@ int main(int argc, char **argv) { @autoreleasepool {
NvProg *pr = [NvProg new];
pr->sh[0] = [NvShader new]; pr->sh[0]->fn = [lib newFunctionWithName:@"vertex_main"]; pr->sh[0]->stage = 0; snprintf(pr->sh[0]->key, sizeof pr->sh[0]->key, "vrop_b");
NvShader *ps = [NvShader new]; pr->sh[1] = ps;
ps->fn = fn4_note([lib newFunctionWithName:@"main0"], lib, @"main0", nil); ps->stage = 1; ps->has_bz = 1; snprintf(ps->key, sizeof ps->key, "prop_b");
ps->fn = fn_spec(lib, @"main0", [MTLFunctionConstantValues new], nil); ps->stage = 1; ps->has_bz = 1; snprintf(ps->key, sizeof ps->key, "prop_b"); // as shader_load: a function with constants (dm_blend_off) is specialized
CHECK(ps->fn != nil);
static prog_t program; program.serial = 98300; serial_put(program.serial, pr);
merged_case(source, &program, on);
Expand Down
6 changes: 6 additions & 0 deletions dm/gfx/tests/test_dmsh_blend_zero.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ def test_every_return_of_main0(self):
self.assertLess(out.index('struct main0_out'), out.index('void dm_blend0(thread main0_out& dm_o')) # the helper takes main0's local output: after its struct, before main0
self.assertLess(out.index('void dm_blend0('), out.index('fragment main0_out main0('))

def test_blend_off_constant_returns_before_the_words(self): # T-126 v2: a pipeline specialized dm_blend_off = true reads no word and changes nothing
out, _ = blend_zero(PLAIN)
self.assertIn(f'constant bool dm_blend_off [[function_constant({dmsh_abi.BLEND_OFF_FC})]];', out)
h = out[out.index('void dm_blend0('):]; h = h[:h.index('\n}\n')]
self.assertLess(h.index('if (is_function_constant_defined(dm_blend_off) && dm_blend_off) return;'), h.index('dm_s.zero'))

def test_each_float4_colour_by_its_index(self):
out, _ = blend_zero(PLAIN)
bits = dmsh_abi.BLEND_ZERO_BITS
Expand Down
4 changes: 3 additions & 1 deletion dm/gfx/tools/dmsh_rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,8 +930,10 @@ def frag_z_snap(msl):
float4 s = m == %du ? float4(255.0f) : (m == %du ? float4(65535.0f) : float4(1023.0f, 1023.0f, 1023.0f, 3.0f));
return rint(precise::clamp(c, 0.0f, 1.0f) * s) / s; // precise: NaN goes to 0, as both store it; the product is float32's, rint rounds it to nearest even
}
constant bool dm_blend_off [[function_constant(%d)]]; // abi.BLEND_OFF_FC: the pipeline's blend words are both 0 (nvn_mtl.m fn_off), so nothing to fix up
static inline __attribute__((always_inline)) void dm_blend0(thread main0_out& dm_o, constant dm_blend_state& dm_s)
{
if (is_function_constant_defined(dm_blend_off) && dm_blend_off) return;
uint m = dm_s.zero, r = dm_s.round;
if ((m | r) == 0u) return;
%s}
Expand All @@ -953,6 +955,6 @@ def blend_zero(msl):
body, k = re.subn(r'^([ \t]*)return out;$', lambda m: f'{m.group(1)}dm_blend0(out, dm_blend);\n{m.group(1)}return out;', body, flags=re.M)
if not k or k != n: return msl, 0
bits, rb = abi.BLEND_ZERO_BITS, abi.BLEND_ROUND_BITS
helper = BLEND_ZERO % ((1 << bits) - 1, (1 << rb) - 1, abi.ROUND_8, abi.ROUND_16,
helper = BLEND_ZERO % ((1 << bits) - 1, (1 << rb) - 1, abi.ROUND_8, abi.ROUND_16, abi.BLEND_OFF_FC,
''.join(f' dm_o.{nm} = dm_rop(dm_bz(dm_o.{nm}, m >> {bits * c}u), r >> {rb * c}u);\n' for nm, c in cols))
return msl[:e['start']] + helper + '\n' + msl[e['start']:e['head'] - 1] + (', ' if e['params'] else '') + BLEND_ZERO_PARAM + ')' + body + msl[e['end']:], k
2 changes: 1 addition & 1 deletion dm/gfx/tools/pso_offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

# The function constants the host's prebuild sets in a base's functions (nvn_mtl.m pre_fn), by name: (index, bool value). A pair whose functions declare another constant is left to the
# device: the host leaves it at its default, which metal-tt can do too, but no other constant was tried there, and one function metal-tt fails fails its whole group
HOST_CONSTANTS = {'dm_border_off': (abi.BORDER_FC, True)}
HOST_CONSTANTS = {'dm_border_off': (abi.BORDER_FC, True), 'dm_blend_off': (abi.BLEND_OFF_FC, True)} # the common draw's specialization, as nvn_mtl.m pre_fn

BATCH = 1000 # bases per metal-tt run: Odyssey's largest compiler lifetime footprint at this size was 2.065 GiB on an 8 GB M2
MAX_FAILED = 8 # pipelines of one group metal-tt may fail on before the run counts as failed (compile_isolating)
Expand Down
Loading