"""RenoDX HDR's rows for RenoDRT and NX2navi's other HDR settings (gamma correction, match tone slope, tone slope, hue clip, and RenoDRT's highlight saturation, blowout, flare, hue correction and colour grading), without the game, a GPU, RenoDX's sources or the Slang compiler: - the rows: their place on the Display tab, ranges and steps (NX2navi's), their defaults, the keys they are saved under (gpu/vk/vk_menu.cpp's own ini_key, cut out and run), and which show with which tonemapper (its rdx_row and rdx_applies, cut out and run); - the settings the shaders are given (gpu/vk/vk_renodx.cpp's own fill_settings and peak_before_gamma, cut out and run against gpu/vk/vk_menu.h's own Options): with the new rows out of the box, Vanilla's and PsychoV35's are bit for bit what they were before the rows, written out again here from the code as it was; then every row against the field it sets, and the peak the tone mappers are given with the gamma correction; - that the block the glue reads (gpu/vk/shaders/renodx_glue/triforce_settings.slangi) is the one vk_renodx.cpp fills and binds. What the shaders do with the settings is not checked here: that takes RenoDX's sources and the Slang compiler (tests/test_renodx_shaders.py compiles them where a build's folder of them is given). python -I -B tests/test_renodx_rows.py [--cxx g++] """ import argparse import re import struct import subprocess import tempfile from pathlib import Path RUN_ALL_ARGS = ["--cxx", "g++"] ROOT = Path(__file__).resolve().parents[1] # (the row's name, its field of Options, lowest, highest, step, the function that words its value, its default, the # tonemapper it shows with: None any, 1 PsychoV35, 2 RenoDRT). NX2navi's menu: runtime/ui/menu.cpp row_hdr NEW_ROWS = [ ("RenoDX gamma correction", "rdx_gamma", 0, 2, 1, "tx_rdx_gamma", 0, None), ("RenoDX match tone slope", "rdx_match_slope", 0, 1, 1, "tx_onoff", 1, 1), ("RenoDX tone slope", "rdx_tone_slope", 0, 100, 1, "tx_rdx_grade", 50, 1), ("RenoDX hue clip", "rdx_hue_clip", 0, 100, 5, "tx_percent", 50, 1), ("RenoDX highlight saturation", "rdx_highlight_sat", 0, 100, 1, "tx_rdx_grade", 50, 2), ("RenoDX blowout", "rdx_blowout", 0, 100, 1, "tx_rdx_grade", 0, 2), ("RenoDX flare", "rdx_flare", 0, 100, 1, "tx_rdx_grade", 0, 2), ("RenoDX hue correction", "rdx_hue", 0, 100, 5, "tx_percent", 100, 2), ("RenoDX color grading", "rdx_grade", 0, 100, 5, "tx_percent", 100, 2), ] KEYS = {"rdx_tonemap": "renodx_tonemapper", "rdx_gamma": "renodx_gamma_correction", "rdx_match_slope": "renodx_match_tone_slope", "rdx_tone_slope": "renodx_tone_slope", "rdx_hue_clip": "renodx_hue_clip", "rdx_highlight_sat": "renodx_highlight_saturation", "rdx_blowout": "renodx_blowout", "rdx_flare": "renodx_flare", "rdx_hue": "renodx_hue_correction", "rdx_grade": "renodx_color_grading"} # (the rows RenoDX HDR had before: shown with any tonemapper, as before) OLD_FIELDS = ["rdx_tonemap", "rdx_midgray", "rdx_ui_white", "rdx_space", "rdx_exposure", "rdx_highlights", "rdx_shadows", "rdx_contrast", "rdx_saturation", "rdx_bloom"] # (struct Injection's 17 floats, then struct Extra's 6) INJECTION = ["tone_map_type", "peak_white_nits", "diffuse_white_nits", "graphics_white_nits", "tone_map_highlights", "tone_map_shadows", "tone_map_contrast", "tone_map_saturation", "tone_map_cone_contrast", "bloom_strength", "custom_hue_clip", "custom_flags", "local_exposure", "tone_map_exposure", "adaptation_target", "adaptation_min_stops", "adaptation_max_stops"] EXTRA = ["gamma_correction", "highlight_saturation", "blowout", "flare", "hue_correction", "color_grade_strength"] def check(cond, what): if not cond: raise SystemExit("FAIL: " + what) def block(text, at): """text from at to the end of the first {...} after it""" start = text.index("{", at) depth = 0 for i in range(start, len(text)): depth += text[i] == "{" depth -= text[i] == "}" if depth == 0: return text[at:i + 1] raise SystemExit("FAIL: no end for the block at %d" % at) def definition(text, head): found = re.search(re.escape(head) + r"\([^;{]*\)\s*\{", text) check(found, "no definition of " + head) return block(text, found.start()) def f32(x): return struct.unpack(" len("100%"), "%s has no room for 100" % fn) # (kept in the options file as every row: written by its key, read back clamped to its range) check('if (ini_row(r)) put(ini_key(r), std::to_string(*r.value));' in definition(menu, "void save"), "save no longer writes every row with a value under its key") init = definition(menu, "void menu_init") check("if (!ini_row(r) || strcmp(line, ini_key(r))) continue;" in init and "std::min(std::max(v, r.lo), r.hi)" in init, "menu_init no longer reads every row by its key, clamped to its range") line = re.search(r"char line\[(\d+)\];\s*bool has_resolution", init) check(line and all(len(k) + len("=100\n") < int(line.group(1)) for k in KEYS.values()), "an options line of a new row does not fit menu_init's buffer") shown = definition(menu, "bool row_shown") check("(!rdx_row(rows[i].value) || (opts.hdr == 2 && renodx_built() && opts.renodx && rdx_applies(rows[i].value)))" in shown, "row_shown no longer shows RenoDX's rows with HDR Native, RenoDX on and, of one tonemapper's rows, those of the one chosen") return [field for tab, _, field in order if field.startswith("rdx_")], [field for _, _, field in order] MENU_FIXTURE = r""" #include #include #include #include #define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); return 1; } } while (0) @OPTIONS@ Options opts; struct Row { const char* name; int* value; }; @INI_KEY@ @RDX_ROW@ @RDX_APPLIES@ struct Want { const char* name; int* value; const char* key; int tonemap; int def; }; int main() { const Want news[] = {@NEWS@}; int* const olds[] = {@OLDS@}; int* const others[] = {&opts.hdr, &opts.hdr_white, &opts.hdr_peak, &opts.renodx, &opts.fps_cap, &opts.vsync}; /* out of the box: each new row at the value that leaves the picture alone */ for (const Want& w : news) CHECK(*w.value == w.def); CHECK(opts.rdx_tonemap == 1); /* the keys the rows are saved under */ for (const Want& w : news) { const Row r{w.name, w.value}; CHECK(!std::strcmp(ini_key(r), w.key)); } const Row tonemapper{"RenoDX tonemapper", &opts.rdx_tonemap}; CHECK(!std::strcmp(ini_key(tonemapper), "renodx_tonemapper")); /* every one of them is a RenoDX row (hidden without HDR Native and RenoDX HDR), and no other row is */ for (const Want& w : news) CHECK(rdx_row(w.value)); for (int* v : olds) CHECK(rdx_row(v)); for (int* v : others) CHECK(!rdx_row(v) && rdx_applies(v)); /* which show with which tonemapper: 0 Vanilla, 1 PsychoV35, 2 RenoDRT */ for (opts.rdx_tonemap = 0; opts.rdx_tonemap <= 2; opts.rdx_tonemap++) { for (const Want& w : news) CHECK(rdx_applies(w.value) == (w.tonemap < 0 || w.tonemap == opts.rdx_tonemap)); for (int* v : olds) CHECK(rdx_applies(v)); /* (the rows from before: whatever is chosen, as before) */ } std::puts("renodx rows: all checks passed"); return 0; } """ # ---- the settings ---- SETTINGS_FIXTURE = r""" #include #include #include #include #include #include @OPTIONS@ Options opts; @INJECTION@ @EXTRA@ @PEAK@ @FILL@ /* one case a line: field=value pairs over the defaults; answers the 17 + 6 floats as their bits */ int main() { static_assert(sizeof(Injection) == 17 * sizeof(float) && sizeof(Extra) == 6 * sizeof(float)); char line[512]; while (std::fgets(line, sizeof line, stdin)) { opts = Options{}; for (char* p = std::strtok(line, " \n"); p; p = std::strtok(nullptr, " \n")) { char* eq = std::strchr(p, '='); if (!eq) return 2; *eq = 0; const int v = std::atoi(eq + 1); if (false) {} @SETTERS@ else return 3; } Injection in{}; Extra ex{}; fill_settings(in, ex); uint32_t w[23]; std::memcpy(w, &in, sizeof in); std::memcpy(w + 17, &ex, sizeof ex); for (uint32_t x : w) std::printf("%08x ", x); std::printf("\n"); } return 0; } """ SETTABLE = OLD_FIELDS + [r[1] for r in NEW_ROWS] + ["hdr_peak", "hdr_white"] def before(o): """The 17 floats as gpu/vk/vk_renodx.cpp write_settings made them before these rows (commit f79f41d), written out again: int * 0.02f is the float product, correctly rounded""" midgray = 16 if o["rdx_midgray"] == 2 else 1 if o["rdx_midgray"] == 1 else 0 flags = midgray | 2 | (min(max(o["rdx_space"], 0), 2) << 2) two, one = f32(0.02), f32(0.01) return [bits(9.0 if o["rdx_tonemap"] else 0.0), bits(float(o["hdr_peak"])), bits(float(o["hdr_white"])), bits(float(o["rdx_ui_white"])), bits(o["rdx_highlights"] * two), bits(o["rdx_shadows"] * two), bits(o["rdx_contrast"] * two), bits(o["rdx_saturation"] * two), bits(1.0), bits(o["rdx_bloom"] * two), bits(0.5), flags, bits(0.0), bits(o["rdx_exposure"] * one), bits(0.18), bits(-4.0), bits(2.0)] def srgb_code(v): return v * 12.92 if v <= 0.0031308 else 1.055 * v ** (1 / 2.4) - 0.055 def check_settings(run, defaults): def ask(cases): text = "".join(" ".join("%s=%d" % kv for kv in c.items()) + "\n" for c in cases) out = run(text).split("\n") check(len(out) == len(cases) + 1, "the settings fixture answered %d lines for %d cases" % (len(out) - 1, len(cases))) return [[int(x, 16) for x in l.split()] for l in out[:-1]] def fl(word): return struct.unpack(" peak if gamma == 1 else given < peak, "%s: the tone mappers are given %r nits for a peak of %d" % (case, given, peak)) # 3. the tonemapper: RenoDX's numbers, and what follows it for tonemap, number, local in ((0, 0.0, 0.0), (1, 9.0, 0.0), (2, 3.0, 1.0)): got = ask([{"rdx_tonemap": tonemap}])[0] check(fl(got[0]) == number, "tonemapper %d is given to the shaders as %r, not %r" % (tonemap, fl(got[0]), number)) check(fl(got[12]) == local, "tonemapper %d: local exposure %r, not %r" % (tonemap, fl(got[12]), local)) # (RenoDRT differs from PsychoV35 in the tone mapper's number and the local exposure alone: the grading rows, the whites # and the peak reach it as they reach PsychoV35) mix = {"rdx_exposure": 135, "rdx_highlights": 65, "rdx_shadows": 35, "rdx_contrast": 55, "rdx_saturation": 45, "rdx_bloom": 80, "hdr_peak": 1500, "hdr_white": 250, "rdx_ui_white": 150, "rdx_highlight_sat": 70, "rdx_blowout": 20, "rdx_flare": 10, "rdx_hue": 60, "rdx_grade": 80, "rdx_gamma": 1} psycho, drt = ask([dict(mix, rdx_tonemap=1), dict(mix, rdx_tonemap=2)]) check([i for i in range(23) if psycho[i] != drt[i]] == [0, 12], "RenoDRT's settings differ from PsychoV35's in more than the tone mapper and the local exposure") check([fl(w) for w in drt[17:]] == [1.0, f32(70 / 50.0), f32(0.2), f32(0.2), f32(0.6), f32(0.8)], "RenoDRT's rows as the glue is given them: %s" % [fl(w) for w in drt[17:]]) def check_binding(source): """The block the glue declares is the one vk_renodx.cpp fills, in the same order, at the binding it binds""" glue = (ROOT / "gpu/vk/shaders/renodx_glue/triforce_settings.slangi").read_text(encoding="utf-8") fields = re.search(r"struct TriforceSettings \{(.*?)\n\};", glue, re.S) check(fields and re.findall(r"^\s*float (\w+);", fields.group(1), re.M) == EXTRA, "triforce_settings.slangi's TriforceSettings is not the six floats of Extra, in order") extra = re.search(r"struct Extra \{\s*float ([^;]+);\s*\};", source) check(extra and [n.strip() for n in extra.group(1).split(",")] == EXTRA, "vk_renodx.cpp's Extra is not those six floats, in order") inject = re.search(r"struct Injection \{(.*?)\n\};", source, re.S) check(inject and [n.strip() for part in re.findall(r"float ([^;]+);", inject.group(1)) for n in part.split(",")] == INJECTION, "vk_renodx.cpp's Injection is no longer RenoDX's 17 floats in the order this check takes them in") check(re.search(r"layout\(set = 1, binding = 5, scalar\) uniform TriforceShaderConstants \{\s*TriforceSettings settings;\s*\} triforce_constants;", glue), "the glue's settings are not a scalar block at set 1, binding 5") check("{5, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, stages, nullptr}" in source and "li.bindingCount = 6, li.pBindings = bindings;" in source, "RenoDX's descriptor set has no binding 5 for the glue's settings") # (the set's uniform buffers in binding order from 2: the fourth is binding 5) buffers = re.search(r"const VkDescriptorBufferInfo buffers\[4\] = \{(.*?)\};", source, re.S) check(buffers and re.findall(r"\{st\.buffer, (\w+)", buffers.group(1))[2:] == ["kInjectionAt", "kExtraAt"] and "{st.buffer, kExtraAt, sizeof(Extra)}" in buffers.group(1) and "pBufferInfo = &buffers[i - 2]" in source and "vkUpdateDescriptorSets(dev.device, 6, w, 0, nullptr);" in source and "{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 4 * kSets}" in source, "binding 5 is not Extra's place in the buffer, or the pool has no room for it") # (its place in the buffer is its own: a slot between the seed's and the adaptation's) places = re.search(r"constexpr uint64_t kInjectionAt = 0, kAaAt = kSlot, kSeedAt = 2 \* kSlot, kExtraAt = 3 \* kSlot, kAdaptationAt = 4 \* kSlot;", source) slot = re.search(r"constexpr uint64_t kSlot = (\d+);", source) check(places and slot and int(slot.group(1)) >= 17 * 4, "Extra's place in the buffer is not a slot of its own") write = definition(source, "void write_settings") check("fill_settings(in, ex);" in write and "std::memcpy(m + kInjectionAt, &in, sizeof in);" in write and "std::memcpy(m + kExtraAt, &ex, sizeof ex);" in write, "write_settings does not write what fill_settings made to the two blocks") # (the eye adaptation is PsychoV35's: with RenoDRT no draw is told to meter) check("bool adapting() { return opts.rdx_tonemap == 1 && opts.rdx_midgray == 2; }" in source, "the eye adaptation runs with another tonemapper than PsychoV35") # (shaders compiled without the glue, a header from before it: said in the log) check(re.search(r"#ifndef RENODX_SPV_GLUE\s.*?logf\(", source, re.S), "vk_renodx.cpp does not say when its shaders are from before the glue") scene = (ROOT / "gpu/vk/shaders/renodx_glue/triforce_scene.slangi").read_text(encoding="utf-8") check(re.search(r'#define ApplySceneToneMap RenoDXApplySceneToneMap\s+#include "scene_tonemap\.slangi"[^\n]*\s+#undef ApplySceneToneMap', scene) and "if (RENODX_TONE_MAP_TYPE == renodx::draw::TONE_MAP_TYPE_RENO_DRT)" in scene and "return RenoDXApplySceneToneMap(custom_untonemapped, custom_native_gain);" in scene, "the scene glue no longer hands every tonemapper but RenoDRT to RenoDX's own mapper as it is") # (RenoDRT starts from what the game's curve takes: the scene times the game's one exposure number, the same for all # three channels, which is the first entry of the gain RenoDX's shaders pass) check("return TriforceRenoDRT(custom_untonemapped * custom_native_gain.x, renodx::draw::BuildConfig()," in scene, "RenoDRT is no longer given the scene times the game's exposure") out = (ROOT / "gpu/vk/shaders/renodx_glue/triforce_output.slangi").read_text(encoding="utf-8") correct = definition(out, "float3 TriforceGammaCorrect") check("if (gamma_correction == 1.f) return renodx::color::correct::GammaSafe(color, false, 2.2f);" in correct and "if (gamma_correction == 2.f) return renodx::color::correct::GammaSafe(color, false, 2.4f);" in correct and correct.rstrip("}").rstrip().endswith("return color;"), "the encoder's glue does not leave the picture as it is with the gamma correction off") def check_docs(): readme = (ROOT / "readme.md").read_text(encoding="utf-8") row = re.search(r"^\| RenoDX HDR \|(.*)\|$", readme, re.M) check(row, "readme.md's settings table has no RenoDX HDR row") for word in ("Vanilla", "PsychoV35", "RenoDRT", "gamma correction", "tone slope", "hue clip", "highlight saturation", "blowout", "flare", "hue correction", "colour grading"): check(word in row.group(1), "readme.md's RenoDX HDR row does not name: " + word) guide = (ROOT / "docs/development.md").read_text(encoding="utf-8") for line in ("| `renodx_tonemapper` | 0 Vanilla, 1 PsychoV35, 2 RenoDRT |", "| `renodx_gamma_correction` | 0 Off, 1 2.2, 2 BT.1886 (2.4) |"): check(line in guide, "docs/development.md's options table does not have: " + line) for key in KEYS.values(): check("`%s`" % key in guide, "docs/development.md's options table does not name " + key) def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--cxx", default="g++") args = parser.parse_args() vk = ROOT / "gpu" / "vk" menu = (vk / "vk_menu.cpp").read_text(encoding="utf-8") header = (vk / "vk_menu.h").read_text(encoding="utf-8") source = (vk / "vk_renodx.cpp").read_text(encoding="utf-8") rdx_fields, all_fields = check_rows(menu, header) check(set(rdx_fields) == set(OLD_FIELDS) | {r[1] for r in NEW_ROWS}, "RenoDX's rows are not the ten from before and the nine new ones: %s" % sorted(rdx_fields)) check(len(set(KEYS.values())) == len(KEYS), "two rows share a key") check_binding(source) check_docs() options = re.search(r"struct Options \{.*?\n\};", header, re.S).group(0) defaults = {f: int(re.search(r"\bint %s = (-?\d+)[;,]" % f, options).group(1)) for f in SETTABLE if f not in ("rdx_shadows", "rdx_contrast", "rdx_saturation")} defaults.update(rdx_shadows=50, rdx_contrast=50, rdx_saturation=50) # (declared on rdx_highlights' line) check("int rdx_highlights = 50, rdx_shadows = 50, rdx_contrast = 50, rdx_saturation = 50;" in options, "vk_menu.h: the grading rows' defaults moved") with tempfile.TemporaryDirectory(prefix="renodx-rows-") as tmp: work = Path(tmp) flags = [args.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror"] fixture = MENU_FIXTURE for mark, text in (("@OPTIONS@", options), ("@INI_KEY@", definition(menu, "const char* ini_key")), ("@RDX_ROW@", definition(menu, "bool rdx_row")), ("@RDX_APPLIES@", definition(menu, "bool rdx_applies")), ("@NEWS@", ", ".join('{"%s", &opts.%s, "%s", %d, %d}' % (name, field, KEYS[field], -1 if tm is None else tm, default) for name, field, _, _, _, _, default, tm in NEW_ROWS)), ("@OLDS@", ", ".join("&opts.%s" % f for f in OLD_FIELDS))): fixture = fixture.replace(mark, text) src, exe = work / "menu.cpp", work / "menu.exe" src.write_text(fixture, encoding="utf-8") subprocess.run(flags + [str(src), "-o", str(exe)], check=True) subprocess.run([str(exe)], check=True) fixture = SETTINGS_FIXTURE for mark, text in (("@OPTIONS@", options), ("@INJECTION@", re.search(r"struct Injection \{.*?\n\};", source, re.S).group(0)), ("@EXTRA@", re.search(r"struct Extra \{.*?\n\};", source, re.S).group(0)), ("@PEAK@", definition(source, "float peak_before_gamma")), ("@FILL@", definition(source, "void fill_settings")), ("@SETTERS@", "\n".join(' else if (!std::strcmp(p, "%s")) opts.%s = v;' % (f, f) for f in SETTABLE))): fixture = fixture.replace(mark, text) src, exe = work / "settings.cpp", work / "settings.exe" src.write_text(fixture, encoding="utf-8") subprocess.run(flags + [str(src), "-o", str(exe)], check=True) check_settings(lambda text: subprocess.run([str(exe)], input=text.encode(), stdout=subprocess.PIPE, check=True).stdout.decode().replace("\r", ""), defaults) print("PASS: RenoDX's rows for RenoDRT and NX2navi's HDR settings, and the settings the shaders are given") if __name__ == "__main__": main()