mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-16 07:31:06 -04:00
The game's ACES tonemap shader gets a GLSL function linked into its SPIR-V where it clamps each channel: the game's SDR result upgraded to the HDR range by luminance and mapped to the display with the RenoDRT curve, exposure, highlights, shadows, contrast, saturation, highlight saturation, blowout, flare and hue correction. The settings reach it as four vec4 the support buffer gains at 1440. The present encode is a port of the swap chain pass: gamma correction, gamut compression, the peak clamp and PQ. Without an HDR10 swapchain the shader gives the game's own SDR picture; native HDR is unchanged. The menu offers Off, RenoDX and Native, with gamma correction, the tone mapper and its grading rows. The shader build compiles sources marked "keep functions" without optimisation so their functions survive for the splice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
983 lines
54 KiB
C
983 lines
54 KiB
C
// HDR output (NX2NAVI_HDR=1): an HDR10 swapchain (Rec. 2020 primaries, PQ) and a game picture tone mapped to the
|
|
// display's range. Settings: NX2NAVI_HDR (read at start); the nits NX2NAVI_HDR_PEAK_NITS (the display's peak),
|
|
// NX2NAVI_HDR_GAME_NITS (the picture's white, "paper white") and NX2NAVI_HDR_UI_NITS (the HUD's and this menu's white);
|
|
// NX2NAVI_HDR_GAMMA, NX2NAVI_HDR_TONEMAP and the grading sliders NX2NAVI_HDR_EXPOSURE .. NX2NAVI_HDR_GRADE, all read
|
|
// when the settings change (vk_hdr_support). With NX2NAVI_HDR=0 nothing here runs and the present is what it was.
|
|
//
|
|
// The game's picture (docs/architecture.md, "HDR"). The game tonemaps its scene into an r11g11b10f target, upscales it
|
|
// and draws the HUD into a second one (float, nothing in these passes clamps), and one last draw copies that into the
|
|
// 8-bit sRGB window texture it presents. So:
|
|
// 1. vk_hdr_shader_swap, on each fragment shader as it loads (in memory only; the cache keeps the translator's): 1.1.1's
|
|
// tonemap ends in the fitted ACES curve and a clamp to 0..1 per channel. tonemap_splice links the function
|
|
// nx_tonemap of shaders/hdr_tonemap.frag into that shader and stores its result in place of the three clamped
|
|
// channels: the game's SDR result upgraded to the HDR range and mapped onto the display (that file says how), with
|
|
// the settings in four vec4 the support buffer gains at 1440. Recognised by the curve's constants, not by a hash, so
|
|
// another translator still finds it. The game's FSR 1 upscale after it would clamp the picture to 1 again;
|
|
// fsr_unclamp drops that clamp in its SPIR-V.
|
|
// 2. vk_hdr_draw / vk_hdr_present_source: a draw into a presented window texture that samples one float texture of
|
|
// its size is that copy; the present then reads the float texture instead of the window texture. Any other last
|
|
// draw into it leaves the window texture, decoded from sRGB: menus and movies stay SDR at the HUD's white.
|
|
// The present is queued before the next frame's work (vk_backend.c), so the float target is read before the game
|
|
// draws into it again.
|
|
// 3. vk_hdr_encode: on the present, the crop rectangle goes to PQ code values in an RGBA16F image (shaders/hdr_encode.comp:
|
|
// gamma correction, gamut compression, the HUD's white and the peak) that FSR 1 or the blit scale into the
|
|
// swapchain. vk_hdr_ui_*: the in-game menu draws into an RGBA8 image and shaders/hdr_ui.frag blends it onto the
|
|
// swapchain at the HUD's white.
|
|
#include "vk.h"
|
|
|
|
#include <math.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "cfg.h"
|
|
#include "gfx.h"
|
|
#include "hdr_encode_spv.h"
|
|
#include "hdr_fullscreen_spv.h"
|
|
#include "hdr_tonemap_spv.h"
|
|
#include "hdr_ui_spv.h"
|
|
#include "host.h"
|
|
#include "nvn_api.h"
|
|
#include "vk_exec.h"
|
|
#include "vk_fg.h"
|
|
#include "platform/platform.h"
|
|
|
|
#define SLOTS 8
|
|
|
|
// ---- settings
|
|
|
|
static int g_wanted = -1;
|
|
|
|
int vk_hdr_wanted(void) {
|
|
if (g_wanted < 0) g_wanted = cfg_bool("NX2NAVI_HDR");
|
|
return g_wanted;
|
|
}
|
|
|
|
// NX2NAVI_HDR = native: the game's own HDR. The display is reported as HDR10 (ov_gfx.c QueryDisplayHdrStatus, with
|
|
// the peak and paper white of the settings), and the game then renders its HDR picture itself and presents it as
|
|
// PQ-encoded Rec. 2020 in A2B10G10R10 (format 64), which goes to the HDR10 swapchain as it is: no tonemap splice, no
|
|
// float picture, no encode (pictures still presented in 8 bits, before the switch, are encoded as for 1).
|
|
static int g_native = -1;
|
|
int vk_hdr_native(void) {
|
|
if (g_native < 0) g_native = !strcmp(cfg_value("NX2NAVI_HDR"), "native");
|
|
return g_native && vk_hdr_wanted();
|
|
}
|
|
|
|
void vk_hdr_off(void) { g_wanted = 0; }
|
|
|
|
// The settings, read again when they change. The replay (every draw's support buffer) and the present thread read
|
|
// them: an update fills the snapshot not in use and then publishes it.
|
|
typedef struct {
|
|
float peak, game, ui; // nits
|
|
float gamma; // 0 none, 1: 2.2, 2: 2.4 (the present's correction, hdr_encode.comp)
|
|
float tm[16]; // the support buffer's four vec4 (shaders/hdr_tonemap.frag p0..p3)
|
|
} Nits;
|
|
static struct { unsigned seen; int cur; Nits snap[2]; PlatMutex lock; } N = {.seen = ~0u, .lock = PLAT_MUTEX_INIT};
|
|
|
|
static float clampf(float v, float lo, float hi) { return fminf(fmaxf(v, lo), hi); }
|
|
|
|
static Nits nits(void) {
|
|
unsigned v = cfg_version();
|
|
if (__atomic_load_n(&N.seen, __ATOMIC_ACQUIRE) != v) {
|
|
plat_mutex_lock(&N.lock);
|
|
if (N.seen != v) {
|
|
Nits *n = &N.snap[!N.cur];
|
|
n->peak = clampf((float)cfg_double("NX2NAVI_HDR_PEAK_NITS"), 80.0f, 10000.0f);
|
|
n->game = clampf((float)cfg_double("NX2NAVI_HDR_GAME_NITS"), 48.0f, 500.0f);
|
|
n->ui = clampf((float)cfg_double("NX2NAVI_HDR_UI_NITS"), 48.0f, 500.0f);
|
|
const char *g = cfg_value("NX2NAVI_HDR_GAMMA");
|
|
n->gamma = !strcmp(g, "2.2") ? 1.0f : !strcmp(g, "2.4") ? 2.0f : 0.0f;
|
|
// the sliders' 0..100 with 50 unchanged become factors around 1; blowout and hue are fractions
|
|
float pct[8];
|
|
static const char *const keys[8] = {"NX2NAVI_HDR_HIGHLIGHTS", "NX2NAVI_HDR_SHADOWS", "NX2NAVI_HDR_CONTRAST",
|
|
"NX2NAVI_HDR_SATURATION", "NX2NAVI_HDR_HIGHLIGHT_SAT", "NX2NAVI_HDR_BLOWOUT",
|
|
"NX2NAVI_HDR_FLARE", "NX2NAVI_HDR_HUE"};
|
|
for (int i = 0; i < 8; i++) pct[i] = clampf((float)cfg_double(keys[i]), 0.0f, 100.0f);
|
|
float *t = n->tm;
|
|
t[0] = n->peak, t[1] = n->game, t[2] = n->ui;
|
|
t[3] = !strcmp(cfg_value("NX2NAVI_HDR_TONEMAP"), "vanilla") ? 0.0f : 3.0f;
|
|
t[4] = clampf((float)cfg_double("NX2NAVI_HDR_EXPOSURE"), 0.0f, 200.0f) / 100.0f;
|
|
t[5] = pct[0] * 0.02f, t[6] = pct[1] * 0.02f, t[7] = pct[2] * 0.02f;
|
|
t[8] = pct[3] * 0.02f, t[9] = pct[4] * 0.02f, t[10] = pct[5] * 0.01f, t[11] = pct[6] * 0.02f;
|
|
t[12] = pct[7] * 0.01f;
|
|
t[13] = clampf((float)cfg_double("NX2NAVI_HDR_GRADE"), 0.0f, 100.0f) / 100.0f;
|
|
t[14] = n->gamma, t[15] = 0;
|
|
__atomic_store_n(&N.cur, !N.cur, __ATOMIC_RELEASE);
|
|
__atomic_store_n(&N.seen, v, __ATOMIC_RELEASE);
|
|
}
|
|
plat_mutex_unlock(&N.lock);
|
|
}
|
|
return N.snap[__atomic_load_n(&N.cur, __ATOMIC_ACQUIRE)];
|
|
}
|
|
|
|
void vk_hdr_support(float out[16]) {
|
|
Nits n = nits();
|
|
memcpy(out, n.tm, sizeof n.tm);
|
|
if (!vk_hdr_active()) out[3] = -1.0f; // no HDR10 swapchain (yet): the game's own SDR picture
|
|
}
|
|
|
|
// ---- 1. the tonemap shader
|
|
|
|
enum { SPV_OP_NAME = 5, SPV_OP_EXT_INST_IMPORT = 11, SPV_OP_EXT_INST = 12, SPV_OP_MEMORY_MODEL = 14,
|
|
SPV_OP_ENTRY_POINT = 15, SPV_OP_CAPABILITY = 17, SPV_OP_TYPE_BOOL = 20, SPV_OP_TYPE_INT = 21,
|
|
SPV_OP_TYPE_FLOAT = 22, SPV_OP_TYPE_VECTOR = 23, SPV_OP_TYPE_MATRIX = 24, SPV_OP_TYPE_STRUCT = 30,
|
|
SPV_OP_TYPE_POINTER = 32, SPV_OP_TYPE_FUNCTION = 33, SPV_OP_CONSTANT_TRUE = 41, SPV_OP_CONSTANT_FALSE = 42,
|
|
SPV_OP_CONSTANT = 43, SPV_OP_CONSTANT_COMPOSITE = 44, SPV_OP_CONSTANT_NULL = 46, SPV_OP_FUNCTION = 54,
|
|
SPV_OP_FUNCTION_PARAMETER = 55, SPV_OP_FUNCTION_END = 56, SPV_OP_FUNCTION_CALL = 57, SPV_OP_VARIABLE = 59,
|
|
SPV_OP_LOAD = 61, SPV_OP_STORE = 62, SPV_OP_ACCESS_CHAIN = 65, SPV_OP_DECORATE = 71, SPV_OP_MEMBER_DECORATE = 72,
|
|
SPV_OP_VECTOR_SHUFFLE = 79, SPV_OP_COMPOSITE_CONSTRUCT = 80, SPV_OP_COMPOSITE_EXTRACT = 81,
|
|
SPV_OP_COMPOSITE_INSERT = 82, SPV_OP_FMUL = 133, SPV_OP_FDIV = 136, SPV_OP_LOOP_MERGE = 246,
|
|
SPV_OP_SELECTION_MERGE = 247, SPV_OP_LABEL = 248, SPV_OP_BRANCH_CONDITIONAL = 250, SPV_OP_SWITCH = 251,
|
|
SPV_OP_LINE = 8, SPV_OP_NO_LINE = 317, SPV_DEC_OFFSET = 35, SPV_SC_UNIFORM = 2, SPV_SC_OUTPUT = 3,
|
|
SPV_SC_FUNCTION = 7, SPV_CAP_SHADER = 1, GLSL_FMA = 50, GLSL_FMAX = 40, GLSL_FCLAMP = 43, GLSL_NMAX = 80,
|
|
GLSL_NCLAMP = 81 };
|
|
|
|
static int name_is(const uint32_t *a, uint32_t len, const char *s) { // OpName operands after the id
|
|
size_t n = strlen(s);
|
|
return (len - 2) * 4 >= n + 1 && !memcmp(a + 1, s, n + 1);
|
|
}
|
|
|
|
// A 0..1 clamp: translators write FClamp, or NClamp (NaN gives the low bound, as the GPU's saturate does).
|
|
static int is_clamp(uint32_t inst) { return inst == GLSL_FCLAMP || inst == GLSL_NCLAMP; }
|
|
|
|
// The game's FSR 1 pass after the tonemap (some frames take it in place of the plain pass, at 1:1 as well as when it
|
|
// scales up; what picks it is not known yet) ends with clamp(colour, 0.0, 1.0) per channel, which cuts the HDR
|
|
// highlights back to the HUD's white for as long as the game takes it. Its own 0..1 clamps of the filter weights each
|
|
// feed a float bit trick (OpBitcast), so only the clamps whose result is stored to the shader's output change, to
|
|
// max(x, 0.0): one word shorter, so the words after it move down and the module shrinks. Recognised by FSR's two
|
|
// reciprocal constants (0x7ef07ebb, 0x7ef19fff) and its one texture fp_t_tcb_8. Returns the clamps changed, -1 for
|
|
// that shader with no output clamp found (a translator that stores the colour another way), 0 for any other shader.
|
|
static int fsr_unclamp(uint32_t *w, uint64_t *words) {
|
|
uint64_t n = *words;
|
|
uint32_t bound = w[3], glsl = 0, float_type = 0, zero = 0, one = 0;
|
|
int magic = 0, tex = 0;
|
|
for (uint64_t i = 5; i < n;) {
|
|
uint32_t op = w[i] & 0xffff, len = w[i] >> 16;
|
|
if (!len || i + len > n) return 0;
|
|
const uint32_t *a = w + i + 1;
|
|
if (op == SPV_OP_EXT_INST_IMPORT && len >= 3 && name_is(a, len, "GLSL.std.450")) glsl = a[0];
|
|
else if (op == SPV_OP_TYPE_FLOAT && len == 3 && a[1] == 32) float_type = a[0];
|
|
else if (op == SPV_OP_CONSTANT && len == 4) {
|
|
magic |= (a[2] == 0x7ef07ebbu) | (a[2] == 0x7ef19fffu) << 1;
|
|
if (a[0] == float_type && float_type && a[2] == 0) zero = a[1];
|
|
if (a[0] == float_type && float_type && a[2] == 0x3f800000u) one = a[1];
|
|
} else if (op == SPV_OP_NAME && len >= 3 && name_is(a, len, "fp_t_tcb_8")) tex = 1;
|
|
i += len;
|
|
}
|
|
if (magic != 3 || !tex) return 0;
|
|
if (!glsl || !zero || !one || !bound) return -1;
|
|
uint8_t *out = calloc(bound, 1); // id -> 1: a pointer into an Output variable, 2: a value stored through one
|
|
if (!out) return 0;
|
|
for (uint64_t i = 5; i < n; i += w[i] >> 16) {
|
|
uint32_t op = w[i] & 0xffff, len = w[i] >> 16;
|
|
const uint32_t *a = w + i + 1;
|
|
if (op == SPV_OP_VARIABLE && len >= 4 && a[2] == SPV_SC_OUTPUT && a[1] < bound) out[a[1]] = 1;
|
|
else if (op == SPV_OP_ACCESS_CHAIN && len >= 4 && a[2] < bound && out[a[2]] == 1 && a[1] < bound) out[a[1]] = 1;
|
|
else if (op == SPV_OP_STORE && len >= 3 && a[0] < bound && out[a[0]] == 1 && a[1] < bound) out[a[1]] = 2;
|
|
}
|
|
int changed = 0;
|
|
uint64_t k = 5; // the write position: never past the read position, as instructions only shrink
|
|
for (uint64_t i = 5; i < n;) {
|
|
uint32_t op = w[i] & 0xffff, len = w[i] >> 16;
|
|
const uint32_t *a = w + i + 1; // result type, result, set, instruction, x, min, max
|
|
if (op == SPV_OP_EXT_INST && len == 8 && a[2] == glsl && is_clamp(a[3]) && a[1] < bound && out[a[1]] == 2 &&
|
|
a[5] == zero && a[6] == one) {
|
|
uint32_t ins[7] = {7u << 16 | SPV_OP_EXT_INST, a[0], a[1], a[2], a[3] == GLSL_NCLAMP ? GLSL_NMAX : GLSL_FMAX,
|
|
a[4], zero};
|
|
memmove(w + k, ins, sizeof ins);
|
|
k += 7, changed++;
|
|
} else {
|
|
memmove(w + k, w + i, len * sizeof *w);
|
|
k += len;
|
|
}
|
|
i += len;
|
|
}
|
|
free(out);
|
|
*words = k;
|
|
return changed ? changed : -1;
|
|
}
|
|
|
|
// Which words of an instruction are literals rather than ids, for the opcodes the tonemap library uses (word j, 1 is
|
|
// the first after the opcode word). An opcode it does not know makes the splice give up (-1).
|
|
static int spv_literal(uint32_t op, uint32_t j) {
|
|
switch (op) {
|
|
case SPV_OP_FUNCTION: case SPV_OP_VARIABLE: return j == 3;
|
|
case SPV_OP_LOAD: return j >= 4;
|
|
case SPV_OP_STORE: return j >= 3;
|
|
case SPV_OP_COMPOSITE_EXTRACT: return j >= 4;
|
|
case SPV_OP_COMPOSITE_INSERT: case SPV_OP_VECTOR_SHUFFLE: return j >= 5;
|
|
case SPV_OP_EXT_INST: return j == 4;
|
|
case SPV_OP_SELECTION_MERGE: return j == 2;
|
|
case SPV_OP_LOOP_MERGE: return j >= 3;
|
|
case SPV_OP_BRANCH_CONDITIONAL: return j >= 4;
|
|
case SPV_OP_CONSTANT: return j >= 3;
|
|
case SPV_OP_TYPE_INT: case SPV_OP_TYPE_FLOAT: return j >= 2;
|
|
case SPV_OP_TYPE_VECTOR: case SPV_OP_TYPE_MATRIX: return j == 3;
|
|
case SPV_OP_TYPE_POINTER: return j == 2;
|
|
case SPV_OP_SWITCH: case SPV_OP_LINE: case SPV_OP_NO_LINE: case 63 /* OpCopyMemory */: return -1;
|
|
default: return 0;
|
|
}
|
|
}
|
|
|
|
typedef struct { uint32_t *w; uint64_t n, cap; int bad; } SpvOut;
|
|
|
|
static void put(SpvOut *o, const uint32_t *ins, uint32_t len) {
|
|
if (o->n + len > o->cap) {
|
|
uint64_t cap = (o->n + len) * 2;
|
|
uint32_t *w = realloc(o->w, cap * sizeof *w);
|
|
if (!w) { o->bad = 1; return; }
|
|
o->w = w, o->cap = cap;
|
|
}
|
|
memcpy(o->w + o->n, ins, len * sizeof *ins);
|
|
o->n += len;
|
|
}
|
|
#define PUT(o, ...) do { const uint32_t ins_[] = {__VA_ARGS__}; const uint32_t n_ = sizeof ins_ / sizeof *ins_; \
|
|
uint32_t h_[1] = {n_ << 16 | ins_[0]}; put(o, h_, 1); put(o, ins_ + 1, n_ - 1); } while (0)
|
|
|
|
// One library instruction with its ids mapped (map[id], new ones already assigned); 0 when an id has no mapping.
|
|
static int put_mapped(SpvOut *o, const uint32_t *ins, const uint32_t *map, uint32_t lbound) {
|
|
uint32_t op = ins[0] & 0xffff, len = ins[0] >> 16, tmp[64];
|
|
if (len > 64) return 0;
|
|
tmp[0] = ins[0];
|
|
for (uint32_t j = 1; j < len; j++) {
|
|
int lit = spv_literal(op, j);
|
|
if (lit < 0) return 0;
|
|
if (lit) { tmp[j] = ins[j]; continue; }
|
|
if (ins[j] >= lbound || !map[ins[j]]) return 0;
|
|
tmp[j] = map[ins[j]];
|
|
}
|
|
put(o, tmp, len);
|
|
return 1;
|
|
}
|
|
|
|
static int is_type_op(uint32_t op) { return op >= 19 && op <= 39; }
|
|
static int is_const_op(uint32_t op) { return op >= SPV_OP_CONSTANT_TRUE && op <= SPV_OP_CONSTANT_NULL && op != 45; }
|
|
|
|
// The fitted ACES curve the game ends its tonemap with: FClamp/NClamp(FMul(FMul(x, Fma(x, 2.51, 0.03)), rcp), 0, 1)
|
|
// per channel, each result stored to a function variable that the output store of its channel loads. tonemap_splice
|
|
// keeps everything the game computes and only changes what the three colour channels store: nx_tonemap of the three x
|
|
// (shaders/hdr_tonemap.frag), called just before the first of those stores, with the settings from four vec4 the
|
|
// support buffer gains at 1440 (vk_exec_draw.c support_fill writes them; the translator's SupportBuffer ends there).
|
|
// The library's types are matched to the game's where the same (SPIR-V allows one declaration of each), its constants
|
|
// and functions appended with new ids. Gives up (0) on any shape it does not expect, and the shader stays as it was.
|
|
static int tonemap_splice(uint32_t **spirv, uint64_t *words) {
|
|
uint32_t *w = *spirv;
|
|
uint64_t n = *words;
|
|
uint32_t bound = w[3];
|
|
if (bound < 8 || bound > (1u << 22)) return 0;
|
|
const float f251 = 2.51f, f003 = 0.03f, fone = 1.0f;
|
|
uint32_t b251, b003, bone;
|
|
memcpy(&b251, &f251, 4), memcpy(&b003, &f003, 4), memcpy(&bone, &fone, 4);
|
|
uint32_t glsl = 0, ft = 0, c0 = 0, c1 = 0, c251 = 0, c003 = 0, sb = 0, sb_ptr = 0, st = 0, st_len = 0, st_v4 = 0,
|
|
it = 0, ptr_v4u = 0, last_offset = 0;
|
|
uint64_t first_fn = 0, first_label = 0, st_at = 0, dec_at = 0;
|
|
int nfn = 0;
|
|
uint32_t *def = calloc(bound, sizeof *def); // result id -> word index of its instruction
|
|
int32_t *cval = calloc(bound, sizeof *cval); // integer constant id -> value + 1
|
|
uint8_t *outv = calloc(bound, 1); // 1: an Output variable
|
|
if (!def || !cval || !outv) goto fail;
|
|
for (uint64_t i = 5; i < n;) {
|
|
uint32_t op = w[i] & 0xffff, len = w[i] >> 16;
|
|
if (!len || i + len > n) goto fail;
|
|
const uint32_t *a = w + i + 1;
|
|
if (op == SPV_OP_EXT_INST_IMPORT && len >= 3 && name_is(a, len, "GLSL.std.450")) glsl = a[0];
|
|
else if (op == SPV_OP_TYPE_FLOAT && len == 3 && a[1] == 32) ft = a[0];
|
|
else if (op == SPV_OP_TYPE_INT && len == 4 && a[1] == 32 && (!it || a[2] == 1)) it = a[0];
|
|
else if (op == SPV_OP_CONSTANT && len == 4 && a[0] == ft && ft) {
|
|
if (a[2] == 0) c0 = a[1];
|
|
else if (a[2] == bone) c1 = a[1];
|
|
else if (a[2] == b251) c251 = a[1];
|
|
else if (a[2] == b003) c003 = a[1];
|
|
} else if (op == SPV_OP_CONSTANT && len == 4 && a[1] < bound && a[2] < 0x10000) cval[a[1]] = (int32_t)a[2] + 1;
|
|
else if (op == SPV_OP_NAME && len >= 3 && name_is(a, len, "support_buffer")) sb = a[0];
|
|
else if (op == SPV_OP_MEMBER_DECORATE && len == 5 && a[1] == 10 && a[2] == SPV_DEC_OFFSET && a[3] == 1424)
|
|
last_offset = a[0], dec_at = i; // the struct whose member 10 is at 1424
|
|
else if (op == SPV_OP_FUNCTION) { nfn++; if (!first_fn) first_fn = i; }
|
|
else if (op == SPV_OP_LABEL && !first_label) first_label = i;
|
|
if (op == SPV_OP_VARIABLE && len >= 4 && a[1] < bound) {
|
|
if (a[1] == sb && sb) sb_ptr = a[0];
|
|
if (a[2] == SPV_SC_OUTPUT) outv[a[1]] = 1;
|
|
}
|
|
if (len >= 3 && a[1] < bound && (op == SPV_OP_FMUL || op == SPV_OP_FDIV || op == SPV_OP_EXT_INST ||
|
|
op == SPV_OP_LOAD || op == SPV_OP_ACCESS_CHAIN || op == SPV_OP_VARIABLE))
|
|
def[a[1]] = (uint32_t)i;
|
|
i += len;
|
|
}
|
|
for (uint64_t i = 5; i < n; i += w[i] >> 16) { // the support buffer's block and a Uniform vec4 pointer
|
|
uint32_t op = w[i] & 0xffff, len = w[i] >> 16;
|
|
if (op == SPV_OP_TYPE_POINTER && len == 4 && w[i + 1] == sb_ptr && sb_ptr) st = w[i + 3];
|
|
}
|
|
for (uint64_t i = 5; i < n && st; i += w[i] >> 16) {
|
|
uint32_t op = w[i] & 0xffff, len = w[i] >> 16;
|
|
if (op == SPV_OP_TYPE_STRUCT && w[i + 1] == st) st_len = len, st_at = i, st_v4 = w[i + len - 1];
|
|
}
|
|
for (uint64_t i = 5; i < n && st_v4; i += w[i] >> 16)
|
|
if ((w[i] & 0xffff) == SPV_OP_TYPE_POINTER && (w[i] >> 16) == 4 && w[i + 2] == SPV_SC_UNIFORM && w[i + 3] == st_v4)
|
|
ptr_v4u = w[i + 1];
|
|
if (!glsl || !ft || !it || !c0 || !c1 || !c251 || !c003 || !sb || st_len != 13 || last_offset != st || nfn != 1 ||
|
|
!first_label || first_label < first_fn)
|
|
goto fail;
|
|
// the clamp sites and their x
|
|
enum { MAX_SITES = 8 };
|
|
uint64_t site[MAX_SITES];
|
|
uint32_t site_x[MAX_SITES], site_var[MAX_SITES];
|
|
int nsite = 0;
|
|
for (uint64_t i = first_fn; i < n && nsite < MAX_SITES; i += w[i] >> 16) {
|
|
const uint32_t *a = w + i + 1;
|
|
if ((w[i] & 0xffff) != SPV_OP_EXT_INST || (w[i] >> 16) != 8 || a[2] != glsl || !is_clamp(a[3]) ||
|
|
a[5] != c0 || a[6] != c1 || a[4] >= bound || !def[a[4]])
|
|
continue;
|
|
const uint32_t *m = w + def[a[4]]; // FMul(num, rcp)
|
|
if ((m[0] & 0xffff) != SPV_OP_FMUL || m[3] >= bound || !def[m[3]]) continue;
|
|
const uint32_t *num = w + def[m[3]]; // FMul(x, Fma(x, 2.51, 0.03))
|
|
if ((num[0] & 0xffff) != SPV_OP_FMUL || num[4] >= bound || !def[num[4]]) continue;
|
|
const uint32_t *f = w + def[num[4]];
|
|
if ((f[0] & 0xffff) != SPV_OP_EXT_INST || (f[0] >> 16) != 8 || f[4] != GLSL_FMA || f[6] != c251 || f[7] != c003)
|
|
continue;
|
|
site[nsite] = i, site_x[nsite] = num[3], site_var[nsite] = 0;
|
|
for (uint64_t j = i; j < n; j += w[j] >> 16) // OpStore var, result
|
|
if ((w[j] & 0xffff) == SPV_OP_STORE && (w[j] >> 16) >= 3 && w[j + 2] == a[1]) { site_var[nsite] = w[j + 1]; break; }
|
|
if (site_var[nsite]) nsite++;
|
|
}
|
|
if (nsite < 3) goto fail;
|
|
// the output stores of channels 0..2: OpStore (AccessChain out, c), value, with a load of a site's variable between
|
|
uint64_t chain_at[3] = {0}, store_at[3] = {0};
|
|
int chan_site[3] = {-1, -1, -1};
|
|
for (uint64_t i = first_fn; i < n; i += w[i] >> 16) {
|
|
const uint32_t *a = w + i + 1;
|
|
if ((w[i] & 0xffff) != SPV_OP_STORE || (w[i] >> 16) < 3 || a[0] >= bound || !def[a[0]]) continue;
|
|
const uint32_t *ch = w + def[a[0]];
|
|
if ((ch[0] & 0xffff) != SPV_OP_ACCESS_CHAIN || (ch[0] >> 16) != 5 || ch[3] >= bound || !outv[ch[3]] ||
|
|
ch[4] >= bound || cval[ch[4]] < 1 || cval[ch[4]] > 3)
|
|
continue;
|
|
int c = cval[ch[4]] - 1;
|
|
if (store_at[c]) goto fail; // a channel stored twice
|
|
chain_at[c] = def[a[0]], store_at[c] = i;
|
|
for (uint64_t j = chain_at[c]; j < i; j += w[j] >> 16)
|
|
if ((w[j] & 0xffff) == SPV_OP_LOAD)
|
|
for (int s = 0; s < nsite; s++)
|
|
if (w[j + 3] == site_var[s]) chan_site[c] = s;
|
|
}
|
|
uint64_t at = ~0ull, lo = ~0ull, hi = 0; // the insertion point; the span that must be one block
|
|
for (int c = 0; c < 3; c++) {
|
|
if (chan_site[c] < 0 || !store_at[c]) goto fail;
|
|
for (int d = 0; d < c; d++)
|
|
if (chan_site[d] == chan_site[c]) goto fail;
|
|
int s = chan_site[c];
|
|
at = chain_at[c] < at ? chain_at[c] : at;
|
|
uint64_t xd = def[site_x[s]] ? def[site_x[s]] : site[s];
|
|
lo = xd < lo ? xd : lo, lo = site[s] < lo ? site[s] : lo;
|
|
hi = store_at[c] > hi ? store_at[c] : hi;
|
|
}
|
|
for (int c = 0; c < 3; c++)
|
|
if (site[chan_site[c]] >= at || def[site_x[chan_site[c]]] >= at) goto fail;
|
|
for (uint64_t i = lo; i < hi; i += w[i] >> 16)
|
|
if ((w[i] & 0xffff) == SPV_OP_LABEL) goto fail;
|
|
|
|
// the library: its ids mapped into the game's id space
|
|
const uint32_t *L = hdr_tonemap_spv;
|
|
const uint64_t ln = sizeof hdr_tonemap_spv / 4;
|
|
const uint32_t lbound = L[3];
|
|
uint32_t *map = calloc(lbound, sizeof *map), id = bound;
|
|
SpvOut g = {0}, f = {0}, o = {0}; // the library's globals, its functions, the result
|
|
uint32_t entry = 0, fn = 0, fn_type = 0;
|
|
if (!map) goto fail;
|
|
for (uint64_t i = 5; i < ln; i += L[i] >> 16) {
|
|
uint32_t op = L[i] & 0xffff, len = L[i] >> 16;
|
|
if (op == SPV_OP_ENTRY_POINT) entry = L[i + 2];
|
|
else if (op == SPV_OP_NAME && len >= 3 && !memcmp(L + i + 2, "nx_tonemap(", 11)) fn = L[i + 1];
|
|
}
|
|
for (uint64_t i = 5; i < ln;) {
|
|
uint32_t op = L[i] & 0xffff, len = L[i] >> 16;
|
|
const uint32_t *a = L + i + 1;
|
|
if (op == SPV_OP_FUNCTION) break;
|
|
if (op == SPV_OP_CAPABILITY && a[0] != SPV_CAP_SHADER) goto lib_fail;
|
|
if (op == SPV_OP_EXT_INST_IMPORT) {
|
|
if (!name_is(a, len, "GLSL.std.450")) goto lib_fail;
|
|
map[a[0]] = glsl;
|
|
} else if (is_type_op(op)) {
|
|
uint32_t tmp[64];
|
|
if (len > 64) goto lib_fail;
|
|
tmp[0] = L[i], tmp[1] = 0;
|
|
for (uint32_t j = 2; j < len; j++) {
|
|
int lit = spv_literal(op, j);
|
|
if (!lit && (a[j - 1] >= lbound || !map[a[j - 1]])) goto lib_fail;
|
|
tmp[j] = lit ? a[j - 1] : map[a[j - 1]];
|
|
}
|
|
uint32_t same = 0; // the game's own declaration of this type (any but a struct)
|
|
for (uint64_t k = 5; k < first_fn && op != SPV_OP_TYPE_STRUCT && !same; k += w[k] >> 16)
|
|
if (w[k] == L[i] && !memcmp(w + k + 2, tmp + 2, (len - 2) * 4)) same = w[k + 1];
|
|
if (same) {
|
|
map[a[0]] = same;
|
|
} else {
|
|
map[a[0]] = tmp[1] = id++;
|
|
put(&g, tmp, len);
|
|
}
|
|
} else if (is_const_op(op)) {
|
|
map[a[1]] = id++;
|
|
if (!put_mapped(&g, L + i, map, lbound)) goto lib_fail;
|
|
} else if (op == SPV_OP_VARIABLE && a[2] != 1 /* Input */ && a[2] != SPV_SC_OUTPUT) {
|
|
goto lib_fail; // a global the functions could use; main's inputs and output are left behind with main
|
|
}
|
|
// names, decorations (the inputs' locations), the entry point and the memory model stay the game's
|
|
i += len;
|
|
}
|
|
for (uint64_t i = 5; i < ln; i += L[i] >> 16) // nx_tonemap's function type
|
|
if ((L[i] & 0xffff) == SPV_OP_FUNCTION && L[i + 2] == fn) fn_type = L[i + 4];
|
|
if (!entry || !fn || !fn_type || !map[fn_type]) goto lib_fail;
|
|
// nx_tonemap(vec3, vec4 x 4) with its parameters by pointer, as the compiler writes them
|
|
const uint32_t *ftype = NULL;
|
|
for (uint64_t i = 5; i < ln && !ftype; i += L[i] >> 16)
|
|
if ((L[i] & 0xffff) == SPV_OP_TYPE_FUNCTION && L[i + 1] == fn_type && (L[i] >> 16) == 8) ftype = L + i;
|
|
if (!ftype) goto lib_fail;
|
|
uint32_t param_ptr[5], v3 = map[ftype[2]], v4 = 0, ptr_types_ok = 1;
|
|
for (int p = 0; p < 5; p++) {
|
|
param_ptr[p] = map[ftype[3 + p]];
|
|
uint32_t pointee = 0;
|
|
for (uint64_t i = 5; i < ln; i += L[i] >> 16)
|
|
if ((L[i] & 0xffff) == SPV_OP_TYPE_POINTER && L[i + 1] == ftype[3 + p] && L[i + 2] == SPV_SC_FUNCTION)
|
|
pointee = map[L[i + 3]];
|
|
if (!pointee || (p == 0 && pointee != v3) || (p > 0 && v4 && pointee != v4)) ptr_types_ok = 0;
|
|
if (p > 0) v4 = pointee;
|
|
}
|
|
if (!ptr_types_ok || v4 != st_v4) goto lib_fail; // the library's vec4 is the support buffer's
|
|
// the library's functions, all but its main. First a new id for every result in them (calls may refer ahead):
|
|
// OpLabel's is its first word, any other instruction's with a result its second.
|
|
uint64_t lib_fn = 0;
|
|
for (uint64_t i = 5; i < ln && !lib_fn; i += L[i] >> 16)
|
|
if ((L[i] & 0xffff) == SPV_OP_FUNCTION) lib_fn = i;
|
|
for (uint64_t i = lib_fn; i < ln && lib_fn; i += L[i] >> 16) {
|
|
uint32_t op = L[i] & 0xffff, len = L[i] >> 16, r;
|
|
switch (op) {
|
|
case SPV_OP_STORE: case SPV_OP_SELECTION_MERGE: case SPV_OP_LOOP_MERGE: case SPV_OP_BRANCH_CONDITIONAL:
|
|
case 249 /* OpBranch */: case 253 /* OpReturn */: case 254 /* OpReturnValue */: case SPV_OP_FUNCTION_END:
|
|
case 252 /* OpKill */: case 255 /* OpUnreachable */:
|
|
continue;
|
|
case SPV_OP_LABEL: r = len >= 2 ? L[i + 1] : 0; break;
|
|
default: r = len >= 3 ? L[i + 2] : 0; break;
|
|
}
|
|
if (!r || r >= lbound) goto lib_fail;
|
|
if (!map[r]) map[r] = id++;
|
|
}
|
|
int in_main = 0;
|
|
for (uint64_t i = lib_fn; i < ln && lib_fn; i += L[i] >> 16) {
|
|
uint32_t op = L[i] & 0xffff;
|
|
if (op == SPV_OP_FUNCTION) in_main = L[i + 2] == entry;
|
|
if (!in_main && !put_mapped(&f, L + i, map, lbound)) goto lib_fail;
|
|
if (op == SPV_OP_FUNCTION_END) in_main = 0;
|
|
}
|
|
if (!lib_fn || !map[fn] || g.bad || f.bad) goto lib_fail;
|
|
|
|
// the result: the game's module with the support block grown, the library's globals before the first function,
|
|
// the call in main, the three stores changed, and the library's functions at the end
|
|
uint32_t idx[4], ptr_v4 = ptr_v4u ? ptr_v4u : id++, var[5], pv[4], pl[4], xv = id++, res = id++, e[3];
|
|
for (int m = 0; m < 4; m++) idx[m] = id++, pv[m] = id++, pl[m] = id++;
|
|
for (int p = 0; p < 5; p++) var[p] = id++;
|
|
for (int c = 0; c < 3; c++) e[c] = id++;
|
|
put(&o, w, 5);
|
|
for (uint64_t i = 5; i < n;) {
|
|
uint32_t op = w[i] & 0xffff, len = w[i] >> 16;
|
|
if (i == first_fn) {
|
|
put(&o, g.w, (uint32_t)g.n);
|
|
if (!ptr_v4u) PUT(&o, SPV_OP_TYPE_POINTER, ptr_v4, SPV_SC_UNIFORM, st_v4);
|
|
for (int m = 0; m < 4; m++) PUT(&o, SPV_OP_CONSTANT, it, idx[m], 11 + m);
|
|
}
|
|
if (i == st_at) { // the support block gains four vec4 at 1440
|
|
put(&o, w + i, len);
|
|
o.w[o.n - len] = (len + 4) << 16 | op;
|
|
for (int m = 0; m < 4; m++) put(&o, &st_v4, 1);
|
|
} else if (i == dec_at) {
|
|
put(&o, w + i, len);
|
|
for (int m = 0; m < 4; m++) PUT(&o, SPV_OP_MEMBER_DECORATE, st, 11 + m, SPV_DEC_OFFSET, 1440 + 16 * m);
|
|
} else if (i == first_label) {
|
|
put(&o, w + i, len);
|
|
for (int p = 0; p < 5; p++) PUT(&o, SPV_OP_VARIABLE, param_ptr[p], var[p], SPV_SC_FUNCTION);
|
|
} else if (i == store_at[0] || i == store_at[1] || i == store_at[2]) {
|
|
int c = i == store_at[0] ? 0 : i == store_at[1] ? 1 : 2;
|
|
put(&o, w + i, len);
|
|
o.w[o.n - len + 2] = e[c];
|
|
} else {
|
|
if (i == at) {
|
|
for (int m = 0; m < 4; m++) {
|
|
PUT(&o, SPV_OP_ACCESS_CHAIN, ptr_v4, pv[m], sb, idx[m]);
|
|
PUT(&o, SPV_OP_LOAD, st_v4, pl[m], pv[m]);
|
|
}
|
|
PUT(&o, SPV_OP_COMPOSITE_CONSTRUCT, v3, xv, site_x[chan_site[0]], site_x[chan_site[1]],
|
|
site_x[chan_site[2]]);
|
|
PUT(&o, SPV_OP_STORE, var[0], xv);
|
|
for (int m = 0; m < 4; m++) PUT(&o, SPV_OP_STORE, var[m + 1], pl[m]);
|
|
PUT(&o, SPV_OP_FUNCTION_CALL, v3, res, map[fn], var[0], var[1], var[2], var[3], var[4]);
|
|
for (int c = 0; c < 3; c++) PUT(&o, SPV_OP_COMPOSITE_EXTRACT, ft, e[c], res, c);
|
|
}
|
|
put(&o, w + i, len);
|
|
}
|
|
i += len;
|
|
}
|
|
put(&o, f.w, (uint32_t)f.n);
|
|
if (o.bad) goto lib_fail;
|
|
o.w[3] = id;
|
|
free(map), free(g.w), free(f.w), free(def), free(cval), free(outv);
|
|
free(*spirv);
|
|
*spirv = o.w;
|
|
*words = o.n;
|
|
return 1;
|
|
lib_fail:
|
|
free(map), free(g.w), free(f.w), free(o.w);
|
|
fail:
|
|
free(def), free(cval), free(outv);
|
|
return 0;
|
|
}
|
|
#undef PUT
|
|
|
|
void vk_hdr_shader_swap(uint32_t **spirv, uint64_t *words) {
|
|
if (!vk_hdr_wanted() || vk_hdr_native() || !*spirv || *words < 5) return;
|
|
if (tonemap_splice(spirv, words)) {
|
|
static volatile int32_t spliced;
|
|
rt_log("hdr: the game's tonemap maps its picture onto the display's range (%d shaders so far)",
|
|
plat_inc32(&spliced));
|
|
}
|
|
int unclamped = fsr_unclamp(*spirv, words);
|
|
if (unclamped > 0) {
|
|
static volatile int32_t fsrs;
|
|
rt_log("hdr: the game's FSR pass keeps values above 1 (%d output clamps; %d so far)", unclamped,
|
|
plat_inc32(&fsrs));
|
|
} else if (unclamped < 0) {
|
|
static volatile int32_t missed;
|
|
if (plat_inc32(&missed) == 1)
|
|
rt_log("hdr: the game's FSR pass was found but no clamp of its output; it still clips HDR highlights");
|
|
}
|
|
}
|
|
|
|
|
|
// ---- 2. the picture the window texture is copied from
|
|
|
|
int vk_hdr_track; // vk_exec_draw.c calls vk_hdr_draw: set at the first present with NX2NAVI_HDR=1
|
|
static volatile int32_t g_active; // the swapchain is HDR10 (gfx_present.c vk_hdr_set_active)
|
|
static PlatMutex rec_lock = PLAT_MUTEX_INIT;
|
|
static struct { VkImage win, src; VkFormat fmt; } recs[4]; // per presented window image: what the last draw copied
|
|
static int nrecs;
|
|
|
|
void vk_hdr_set_active(int on) { plat_xchg32(&g_active, on); }
|
|
int vk_hdr_active(void) { return plat_cas32(&g_active, 0, 0); }
|
|
|
|
static int is_float(VkFormat f) {
|
|
return f == VK_FORMAT_B10G11R11_UFLOAT_PACK32 || f == VK_FORMAT_R16G16B16A16_SFLOAT ||
|
|
f == VK_FORMAT_R32G32B32A32_SFLOAT;
|
|
}
|
|
|
|
void vk_hdr_draw(VkExec *ex, const VkPipeInfo *info) {
|
|
const VkExecTarget *t = &ex->color[0];
|
|
if (!t->image) return;
|
|
int known = 0;
|
|
for (int i = 0; i < 4; i++) // unlocked first look: most draws go elsewhere (a stale slot only costs the lock below)
|
|
known |= __atomic_load_n(&recs[i].win, __ATOMIC_RELAXED) == t->image;
|
|
if (!known) return;
|
|
VkImage src = VK_NULL_HANDLE;
|
|
VkFormat fmt = VK_FORMAT_UNDEFINED;
|
|
int ntex = 0, slot = -1;
|
|
for (uint32_t i = 0; i < info->n_bindings; i++) {
|
|
const VkPipeBinding *bd = &info->bindings[i];
|
|
if (bd->kind != GFX_RES_TEX || bd->stage != GFX_STAGE_FS || !bd->res) continue;
|
|
ntex += bd->count ? bd->count : 1;
|
|
if ((bd->res->type & 0x7ff) == 3 && !bd->res->separate) slot = bd->res->slot; // a plain 2D texture
|
|
}
|
|
if (ntex == 1 && slot >= 0 && slot < 128 && ex->tex[1][slot]) {
|
|
const VkTexImage *s = vk_texture_by_id(NVN_HANDLE_TEX(ex->tex[1][slot]), NULL, NULL);
|
|
if (s && is_float(s->format) && s->width == t->width && s->height == t->height && s->samples == 1)
|
|
src = s->image, fmt = s->format;
|
|
}
|
|
plat_mutex_lock(&rec_lock);
|
|
for (int i = 0; i < nrecs; i++)
|
|
if (recs[i].win == t->image) recs[i].src = src, recs[i].fmt = fmt;
|
|
plat_mutex_unlock(&rec_lock);
|
|
}
|
|
|
|
void vk_hdr_present_source(const VkTexImage *win, VkImage *image, VkFormat *format) {
|
|
if (!vk_hdr_wanted() || vk_hdr_native()) return;
|
|
vk_hdr_track = 1;
|
|
plat_mutex_lock(&rec_lock);
|
|
int r = -1;
|
|
for (int i = 0; i < nrecs; i++)
|
|
if (recs[i].win == win->image) r = i;
|
|
if (r < 0 && nrecs < 4) r = nrecs++, recs[r].win = win->image, recs[r].src = VK_NULL_HANDLE;
|
|
// kept until the next draw into the window texture: a present again without a new copy shows the same picture
|
|
if (r >= 0 && recs[r].src && vk_hdr_active()) *image = recs[r].src, *format = recs[r].fmt;
|
|
plat_mutex_unlock(&rec_lock);
|
|
static int logged;
|
|
if (!logged && *image != win->image) logged = 1, rt_log("hdr: the present reads the game's float picture");
|
|
}
|
|
|
|
void vk_hdr_image_gone(VkImage image) { // a texture image went away: forget it as window or source
|
|
if (!vk_hdr_track) return; // no records before the first HDR present (vk_hdr_present_source): no lock per image
|
|
plat_mutex_lock(&rec_lock);
|
|
for (int i = 0; i < nrecs; i++) {
|
|
if (recs[i].src == image) recs[i].src = VK_NULL_HANDLE;
|
|
if (recs[i].win == image) recs[i] = recs[--nrecs], recs[nrecs] = (typeof(recs[0])){0}, i--;
|
|
}
|
|
plat_mutex_unlock(&rec_lock);
|
|
}
|
|
|
|
// ---- swapchain
|
|
|
|
// With NX2NAVI_HDR=1, the HDR10 format among the surface's (10-bit UNORM, ST 2084): 1 with *out set, else 0 and the
|
|
// reason logged once.
|
|
int vk_hdr_surface_format(const VkSurfaceFormatKHR *f, uint32_t n, VkSurfaceFormatKHR *out) {
|
|
if (!vk_hdr_wanted()) return 0;
|
|
static int logged;
|
|
if (!logged) {
|
|
char b[1024];
|
|
int o = 0;
|
|
for (uint32_t i = 0; i < n && o < (int)sizeof b - 32; i++)
|
|
o += snprintf(b + o, sizeof b - (size_t)o, " %d/%d", (int)f[i].format, (int)f[i].colorSpace);
|
|
rt_log("hdr: surface formats (VkFormat/VkColorSpaceKHR):%s", b);
|
|
}
|
|
for (int pass = 0; pass < 2; pass++)
|
|
for (uint32_t i = 0; i < n; i++)
|
|
if (f[i].colorSpace == VK_COLOR_SPACE_HDR10_ST2084_EXT &&
|
|
f[i].format == (pass ? VK_FORMAT_A2R10G10B10_UNORM_PACK32 : VK_FORMAT_A2B10G10R10_UNORM_PACK32)) {
|
|
*out = f[i];
|
|
if (!logged) rt_log("hdr: HDR10 swapchain (format %d)", (int)f[i].format);
|
|
logged = 1;
|
|
return 1;
|
|
}
|
|
if (!logged)
|
|
rt_log("hdr: the display offers no HDR10 format, so the picture stays SDR (Windows: turn on \"Use HDR\" in the "
|
|
"display settings; Linux: a Wayland compositor with colour management and HDR on)");
|
|
logged = 1;
|
|
return 0;
|
|
}
|
|
|
|
// The display's peak and the picture's white for sc; again when the settings change (gfx_present.c calls it on every
|
|
// present to an HDR10 swapchain, with new = 1 for a new swapchain).
|
|
void vk_hdr_metadata(VkSwapchainKHR sc, int new_sc) {
|
|
static unsigned sent = ~0u;
|
|
VkCtx *c = vk_ctx();
|
|
if (!c->has_hdr_metadata || (!new_sc && cfg_version() == sent)) return;
|
|
sent = cfg_version();
|
|
// AMD's frame generation swapchain takes it through its own call
|
|
PFN_vkSetHdrMetadataEXT set = vk_fg_hdr_metadata_fn();
|
|
if (!set) set = (PFN_vkSetHdrMetadataEXT)vk_dpa(c->device, "vkSetHdrMetadataEXT");
|
|
if (!set) return;
|
|
Nits n = nits();
|
|
VkHdrMetadataEXT m = {.sType = VK_STRUCTURE_TYPE_HDR_METADATA_EXT,
|
|
.displayPrimaryRed = {0.708f, 0.292f}, .displayPrimaryGreen = {0.170f, 0.797f},
|
|
.displayPrimaryBlue = {0.131f, 0.046f}, .whitePoint = {0.3127f, 0.3290f},
|
|
.maxLuminance = n.peak, .minLuminance = 0.0f, .maxContentLightLevel = n.peak,
|
|
.maxFrameAverageLightLevel = n.game};
|
|
set(c->device, 1, &sc, &m);
|
|
}
|
|
|
|
// ---- 3. present passes
|
|
|
|
static void barrier(VkCommandBuffer cb, VkImage img, VkImageLayout from, VkImageLayout to, VkPipelineStageFlags2 ss,
|
|
VkAccessFlags2 sa, VkPipelineStageFlags2 ds, VkAccessFlags2 da) {
|
|
VkImageMemoryBarrier2 b = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, .srcStageMask = ss, .srcAccessMask = sa,
|
|
.dstStageMask = ds, .dstAccessMask = da, .oldLayout = from, .newLayout = to,
|
|
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
.image = img, .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
|
|
VkDependencyInfo d = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .imageMemoryBarrierCount = 1,
|
|
.pImageMemoryBarriers = &b};
|
|
vkCmdPipelineBarrier2(cb, &d);
|
|
}
|
|
|
|
typedef struct { VkImage img; VmaAllocation alloc; VkImageView view; uint32_t w, h; } Img;
|
|
|
|
static void img_free(VkCtx *c, Img *m) {
|
|
if (m->view) vkDestroyImageView(c->device, m->view, NULL);
|
|
if (m->img) vmaDestroyImage(c->vma, m->img, m->alloc);
|
|
*m = (Img){0};
|
|
}
|
|
|
|
// m at w x h in fmt; another size waits for the device first (earlier presents may still read the old image).
|
|
static int img_ensure(VkCtx *c, Img *m, uint32_t w, uint32_t h, VkFormat fmt, VkImageUsageFlags usage) {
|
|
if (m->img && m->w == w && m->h == h) return 1;
|
|
if (m->img) {
|
|
vk_queue_lock();
|
|
vkDeviceWaitIdle(c->device);
|
|
vk_queue_unlock();
|
|
img_free(c, m);
|
|
}
|
|
VkImageCreateInfo ii = {.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, .imageType = VK_IMAGE_TYPE_2D, .format = fmt,
|
|
.extent = {w, h, 1}, .mipLevels = 1, .arrayLayers = 1, .samples = VK_SAMPLE_COUNT_1_BIT,
|
|
.tiling = VK_IMAGE_TILING_OPTIMAL, .usage = usage};
|
|
VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE};
|
|
if (vmaCreateImage(c->vma, &ii, &ai, &m->img, &m->alloc, NULL) != VK_SUCCESS) {
|
|
m->img = VK_NULL_HANDLE;
|
|
return 0;
|
|
}
|
|
VkImageViewCreateInfo vi = {.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .image = m->img,
|
|
.viewType = VK_IMAGE_VIEW_TYPE_2D, .format = fmt,
|
|
.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
|
|
if (vkCreateImageView(c->device, &vi, NULL, &m->view) != VK_SUCCESS) {
|
|
m->view = VK_NULL_HANDLE;
|
|
img_free(c, m);
|
|
return 0;
|
|
}
|
|
m->w = w, m->h = h;
|
|
return 1;
|
|
}
|
|
|
|
static struct {
|
|
int tried, ok;
|
|
PFN_vkCmdPushDescriptorSetKHR push;
|
|
VkSampler sampler;
|
|
VkDescriptorSetLayout dsl_enc, dsl_ui;
|
|
VkPipelineLayout enc_layout, ui_layout;
|
|
VkPipeline enc;
|
|
VkPipeline ui; // for ui_fmt
|
|
VkFormat ui_fmt;
|
|
Img out; // PQ code values of the crop, RGBA16F
|
|
Img menu; // the in-game menu, RGBA8
|
|
VkImageView src_view[SLOTS];
|
|
} H;
|
|
|
|
static VkShaderModule module(VkCtx *c, const uint32_t *code, size_t size) {
|
|
VkShaderModuleCreateInfo mi = {.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, .codeSize = size, .pCode = code};
|
|
VkShaderModule m = VK_NULL_HANDLE;
|
|
return vkCreateShaderModule(c->device, &mi, NULL, &m) == VK_SUCCESS ? m : VK_NULL_HANDLE;
|
|
}
|
|
|
|
static VkDescriptorSetLayout set_layout(VkCtx *c, const VkDescriptorSetLayoutBinding *b, uint32_t n) {
|
|
VkDescriptorSetLayoutCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
|
|
.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR,
|
|
.bindingCount = n, .pBindings = b};
|
|
VkDescriptorSetLayout l = VK_NULL_HANDLE;
|
|
return vkCreateDescriptorSetLayout(c->device, &ci, NULL, &l) == VK_SUCCESS ? l : VK_NULL_HANDLE;
|
|
}
|
|
|
|
static VkPipelineLayout pipe_layout(VkCtx *c, VkDescriptorSetLayout *dsl, VkShaderStageFlags st, uint32_t pc) {
|
|
VkPushConstantRange pr = {st, 0, pc};
|
|
VkPipelineLayoutCreateInfo li = {.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, .setLayoutCount = 1,
|
|
.pSetLayouts = dsl, .pushConstantRangeCount = 1, .pPushConstantRanges = &pr};
|
|
VkPipelineLayout l = VK_NULL_HANDLE;
|
|
return vkCreatePipelineLayout(c->device, &li, NULL, &l) == VK_SUCCESS ? l : VK_NULL_HANDLE;
|
|
}
|
|
|
|
static int init(VkCtx *c) {
|
|
H.tried = 1;
|
|
H.push = (PFN_vkCmdPushDescriptorSetKHR)vk_dpa(c->device, "vkCmdPushDescriptorSetKHR");
|
|
VkDescriptorSetLayoutBinding enc[2] = {
|
|
{.binding = 0, .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .descriptorCount = 1,
|
|
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT},
|
|
{.binding = 1, .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, .descriptorCount = 1,
|
|
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT}};
|
|
VkDescriptorSetLayoutBinding ui = {.binding = 0, .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
.descriptorCount = 1, .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT};
|
|
VkSamplerCreateInfo si = {.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, .magFilter = VK_FILTER_NEAREST,
|
|
.minFilter = VK_FILTER_NEAREST, .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
|
|
.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
|
|
.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
|
|
.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, .maxLod = 0};
|
|
if (!H.push || vkCreateSampler(c->device, &si, NULL, &H.sampler) != VK_SUCCESS ||
|
|
!(H.dsl_enc = set_layout(c, enc, 2)) || !(H.dsl_ui = set_layout(c, &ui, 1)) ||
|
|
!(H.enc_layout = pipe_layout(c, &H.dsl_enc, VK_SHADER_STAGE_COMPUTE_BIT, 32)) ||
|
|
!(H.ui_layout = pipe_layout(c, &H.dsl_ui, VK_SHADER_STAGE_FRAGMENT_BIT, 16))) {
|
|
rt_log("hdr: the present passes could not be created; the picture goes out unconverted");
|
|
return 0;
|
|
}
|
|
VkShaderModule m = module(c, hdr_encode_spv, sizeof hdr_encode_spv);
|
|
VkComputePipelineCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
|
|
.stage = {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
|
|
.stage = VK_SHADER_STAGE_COMPUTE_BIT, .module = m, .pName = "main"},
|
|
.layout = H.enc_layout};
|
|
VkResult r = m ? vkCreateComputePipelines(c->device, VK_NULL_HANDLE, 1, &ci, NULL, &H.enc) : VK_ERROR_UNKNOWN;
|
|
if (m) vkDestroyShaderModule(c->device, m, NULL);
|
|
if (r != VK_SUCCESS) {
|
|
rt_log("hdr: the present passes could not be created; the picture goes out unconverted");
|
|
return 0;
|
|
}
|
|
return H.ok = 1;
|
|
}
|
|
|
|
// The game's own HDR picture: PQ codes in A2B10G10R10, the format of NVN's RGB10A2 (vk_formats.c); no guest format
|
|
// becomes A2R10G10B10.
|
|
int vk_hdr_native_picture(VkFormat f) { return vk_hdr_native() && f == VK_FORMAT_A2B10G10R10_UNORM_PACK32; }
|
|
|
|
VkImage vk_hdr_encode(VkCommandBuffer cb, unsigned slot, VkImage src, VkFormat fmt, const int crop[4]) {
|
|
VkCtx *c = vk_ctx();
|
|
// native HDR: the game's picture holds PQ codes already; the present scales it into the swapchain as it is
|
|
if (vk_hdr_native_picture(fmt)) {
|
|
static int told;
|
|
if (!told++) rt_log("hdr: native: the game's own HDR10 picture goes to the swapchain as it is");
|
|
return VK_NULL_HANDLE;
|
|
}
|
|
if (!H.tried) init(c);
|
|
if (!H.ok || !img_ensure(c, &H.out, (uint32_t)crop[2], (uint32_t)crop[3], VK_FORMAT_R16G16B16A16_SFLOAT,
|
|
VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT))
|
|
return VK_NULL_HANDLE;
|
|
slot %= SLOTS;
|
|
if (H.src_view[slot]) vkDestroyImageView(c->device, H.src_view[slot], NULL);
|
|
VkImageViewCreateInfo vi = {.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .image = src,
|
|
.viewType = VK_IMAGE_VIEW_TYPE_2D, .format = fmt,
|
|
.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
|
|
if (vkCreateImageView(c->device, &vi, NULL, &H.src_view[slot]) != VK_SUCCESS) {
|
|
H.src_view[slot] = VK_NULL_HANDLE;
|
|
return VK_NULL_HANDLE;
|
|
}
|
|
Nits n = nits();
|
|
// an sRGB format decodes itself through the view; 8-bit UNORM holds sRGB codes the shader decodes
|
|
int decode = fmt == VK_FORMAT_R8G8B8A8_UNORM || fmt == VK_FORMAT_B8G8R8A8_UNORM;
|
|
const VkPipelineStageFlags2 CS = VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT, ALL = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;
|
|
barrier(cb, src, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL, ALL, VK_ACCESS_2_MEMORY_WRITE_BIT, CS,
|
|
VK_ACCESS_2_SHADER_SAMPLED_READ_BIT);
|
|
barrier(cb, H.out.img, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL, ALL, 0, CS,
|
|
VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT);
|
|
VkDescriptorImageInfo ii = {H.sampler, H.src_view[slot], VK_IMAGE_LAYOUT_GENERAL},
|
|
oi = {VK_NULL_HANDLE, H.out.view, VK_IMAGE_LAYOUT_GENERAL};
|
|
VkWriteDescriptorSet w[2] = {
|
|
{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 0, .descriptorCount = 1,
|
|
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .pImageInfo = &ii},
|
|
{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 1, .descriptorCount = 1,
|
|
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, .pImageInfo = &oi}};
|
|
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, H.enc);
|
|
rec_push(H.push, cb, VK_PIPELINE_BIND_POINT_COMPUTE, H.enc_layout, 0, 2, w);
|
|
struct { int32_t offset[2], size[2]; float p[4]; } pc = {{crop[0], crop[1]}, {crop[2], crop[3]},
|
|
{n.ui, n.peak, decode ? 1.0f : 0.0f, n.gamma}};
|
|
vkCmdPushConstants(cb, H.enc_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof pc, &pc);
|
|
vkCmdDispatch(cb, ((uint32_t)crop[2] + 7) / 8, ((uint32_t)crop[3] + 7) / 8, 1);
|
|
barrier(cb, src, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL, CS, 0, ALL, 0);
|
|
barrier(cb, H.out.img, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL, CS, VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT, ALL,
|
|
VK_ACCESS_2_MEMORY_READ_BIT);
|
|
return H.out.img;
|
|
}
|
|
|
|
// The in-game menu's target: an RGBA8 image of the swapchain's size in COLOR_ATTACHMENT_OPTIMAL (its old content
|
|
// dropped); the caller renders the menu into the view with a clear to 0.
|
|
VkImageView vk_hdr_ui_target(VkCommandBuffer cb, uint32_t w, uint32_t h) {
|
|
VkCtx *c = vk_ctx();
|
|
if (!H.tried) init(c);
|
|
if (!H.ok || !img_ensure(c, &H.menu, w, h, VK_FORMAT_R8G8B8A8_UNORM,
|
|
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT))
|
|
return VK_NULL_HANDLE;
|
|
barrier(cb, H.menu.img, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
|
VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, 0, VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT,
|
|
VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT);
|
|
return H.menu.view;
|
|
}
|
|
|
|
static int ui_pipeline(VkCtx *c, VkFormat fmt) {
|
|
if (H.ui && H.ui_fmt == fmt) return 1;
|
|
if (H.ui) { // another swapchain format: earlier presents may still use the old pipeline
|
|
vk_queue_lock();
|
|
vkDeviceWaitIdle(c->device);
|
|
vk_queue_unlock();
|
|
vkDestroyPipeline(c->device, H.ui, NULL);
|
|
H.ui = VK_NULL_HANDLE;
|
|
}
|
|
VkShaderModule vs = module(c, hdr_fullscreen_spv, sizeof hdr_fullscreen_spv),
|
|
fs = module(c, hdr_ui_spv, sizeof hdr_ui_spv);
|
|
VkPipelineShaderStageCreateInfo st[2] = {
|
|
{.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .stage = VK_SHADER_STAGE_VERTEX_BIT, .module = vs,
|
|
.pName = "main"},
|
|
{.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .stage = VK_SHADER_STAGE_FRAGMENT_BIT, .module = fs,
|
|
.pName = "main"}};
|
|
VkPipelineVertexInputStateCreateInfo vi = {.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO};
|
|
VkPipelineInputAssemblyStateCreateInfo ia = {.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
|
|
.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST};
|
|
VkPipelineViewportStateCreateInfo vp = {.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
|
|
.viewportCount = 1, .scissorCount = 1};
|
|
VkPipelineRasterizationStateCreateInfo rs = {.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
|
|
.polygonMode = VK_POLYGON_MODE_FILL, .cullMode = VK_CULL_MODE_NONE,
|
|
.lineWidth = 1.0f};
|
|
VkPipelineMultisampleStateCreateInfo ms = {.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
|
|
.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT};
|
|
VkPipelineColorBlendAttachmentState ba = {
|
|
.blendEnable = VK_TRUE, .srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA,
|
|
.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, .colorBlendOp = VK_BLEND_OP_ADD,
|
|
.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE, .dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
|
|
.alphaBlendOp = VK_BLEND_OP_ADD, .colorWriteMask = 0xf};
|
|
VkPipelineColorBlendStateCreateInfo cb = {.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
|
|
.attachmentCount = 1, .pAttachments = &ba};
|
|
VkDynamicState dyn[2] = {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR};
|
|
VkPipelineDynamicStateCreateInfo ds = {.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
|
|
.dynamicStateCount = 2, .pDynamicStates = dyn};
|
|
VkPipelineRenderingCreateInfo ri = {.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO,
|
|
.colorAttachmentCount = 1, .pColorAttachmentFormats = &fmt};
|
|
VkGraphicsPipelineCreateInfo gi = {.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, .pNext = &ri,
|
|
.stageCount = 2, .pStages = st, .pVertexInputState = &vi,
|
|
.pInputAssemblyState = &ia, .pViewportState = &vp, .pRasterizationState = &rs,
|
|
.pMultisampleState = &ms, .pColorBlendState = &cb, .pDynamicState = &ds,
|
|
.layout = H.ui_layout};
|
|
VkResult r = vs && fs ? vkCreateGraphicsPipelines(c->device, VK_NULL_HANDLE, 1, &gi, NULL, &H.ui) : VK_ERROR_UNKNOWN;
|
|
if (vs) vkDestroyShaderModule(c->device, vs, NULL);
|
|
if (fs) vkDestroyShaderModule(c->device, fs, NULL);
|
|
if (r != VK_SUCCESS) {
|
|
H.ui = VK_NULL_HANDLE;
|
|
rt_log("hdr: the menu pass could not be created; the menu is not shown");
|
|
return 0;
|
|
}
|
|
H.ui_fmt = fmt;
|
|
return 1;
|
|
}
|
|
|
|
// The menu image onto dst (a swapchain image in COLOR_ATTACHMENT_OPTIMAL, view dst_view of format fmt).
|
|
void vk_hdr_ui_compose(VkCommandBuffer cb, VkImageView dst_view, VkFormat fmt, uint32_t w, uint32_t h) {
|
|
VkCtx *c = vk_ctx();
|
|
if (!H.ok || !H.menu.img || !ui_pipeline(c, fmt)) return;
|
|
barrier(cb, H.menu.img, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
|
VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT,
|
|
VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT, VK_ACCESS_2_SHADER_SAMPLED_READ_BIT);
|
|
VkRenderingAttachmentInfo ca = {.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO, .imageView = dst_view,
|
|
.imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
|
.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD, .storeOp = VK_ATTACHMENT_STORE_OP_STORE};
|
|
VkRenderingInfo ri = {.sType = VK_STRUCTURE_TYPE_RENDERING_INFO, .renderArea = {{0, 0}, {w, h}}, .layerCount = 1,
|
|
.colorAttachmentCount = 1, .pColorAttachments = &ca};
|
|
vkCmdBeginRendering(cb, &ri);
|
|
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_GRAPHICS, H.ui);
|
|
VkViewport v = {0, 0, (float)w, (float)h, 0, 1};
|
|
VkRect2D s = {{0, 0}, {w, h}};
|
|
vkCmdSetViewport(cb, 0, 1, &v);
|
|
vkCmdSetScissor(cb, 0, 1, &s);
|
|
VkDescriptorImageInfo ii = {H.sampler, H.menu.view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL};
|
|
VkWriteDescriptorSet wr = {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 0, .descriptorCount = 1,
|
|
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .pImageInfo = &ii};
|
|
rec_push(H.push, cb, VK_PIPELINE_BIND_POINT_GRAPHICS, H.ui_layout, 0, 1, &wr);
|
|
Nits n = nits();
|
|
float pc[4] = {n.ui, n.gamma, 0, 0};
|
|
vkCmdPushConstants(cb, H.ui_layout, VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof pc, pc);
|
|
vkCmdDraw(cb, 3, 1, 0, 0);
|
|
vkCmdEndRendering(cb);
|
|
}
|
|
|
|
void vk_hdr_release(void) {
|
|
VkCtx *c = vk_ctx();
|
|
for (int i = 0; i < SLOTS; i++)
|
|
if (H.src_view[i]) vkDestroyImageView(c->device, H.src_view[i], NULL), H.src_view[i] = VK_NULL_HANDLE;
|
|
img_free(c, &H.out);
|
|
img_free(c, &H.menu);
|
|
if (H.enc) vkDestroyPipeline(c->device, H.enc, NULL);
|
|
if (H.ui) vkDestroyPipeline(c->device, H.ui, NULL);
|
|
if (H.enc_layout) vkDestroyPipelineLayout(c->device, H.enc_layout, NULL);
|
|
if (H.ui_layout) vkDestroyPipelineLayout(c->device, H.ui_layout, NULL);
|
|
if (H.dsl_enc) vkDestroyDescriptorSetLayout(c->device, H.dsl_enc, NULL);
|
|
if (H.dsl_ui) vkDestroyDescriptorSetLayout(c->device, H.dsl_ui, NULL);
|
|
if (H.sampler) vkDestroySampler(c->device, H.sampler, NULL);
|
|
memset(&H, 0, sizeof H);
|
|
}
|
|
|
|
// PQ code (0..1023) of a 10-bit channel to the sRGB byte of the same colour at the HUD's white (menu test shots and
|
|
// flight thumbnails of an HDR10 swapchain).
|
|
static float pq_nits(float e) {
|
|
const float m1 = 0.1593017578125f, m2 = 78.84375f, c1 = 0.8359375f, c2 = 18.8515625f, c3 = 18.6875f;
|
|
float p = powf(e, 1.0f / m2);
|
|
return 10000.0f * powf(fmaxf(p - c1, 0.0f) / (c2 - c3 * p), 1.0f / m1);
|
|
}
|
|
|
|
void vk_hdr_to_srgb8(const uint8_t *in, uint8_t *out, size_t px, int a2r) {
|
|
float ui = nits().ui;
|
|
// Rec. 2020 to Rec. 709 (the inverse of ITU-R BT.2087's matrix)
|
|
static const float m[9] = {1.6605f, -0.5876f, -0.0728f, -0.1246f, 1.1329f, -0.0083f, -0.0182f, -0.1006f, 1.1187f};
|
|
for (size_t i = 0; i < px; i++) {
|
|
uint32_t v;
|
|
memcpy(&v, in + 4 * i, 4);
|
|
float c[3] = {(float)(v & 1023), (float)(v >> 10 & 1023), (float)(v >> 20 & 1023)}; // A2B10G10R10: R low
|
|
if (a2r) { float t = c[0]; c[0] = c[2]; c[2] = t; }
|
|
for (int k = 0; k < 3; k++) c[k] = pq_nits(c[k] / 1023.0f) / ui;
|
|
for (int k = 0; k < 3; k++) {
|
|
float l = fminf(fmaxf(m[3 * k] * c[0] + m[3 * k + 1] * c[1] + m[3 * k + 2] * c[2], 0.0f), 1.0f);
|
|
float s = l <= 0.0031308f ? 12.92f * l : 1.055f * powf(l, 1.0f / 2.4f) - 0.055f;
|
|
out[4 * i + k] = (uint8_t)(s * 255.0f + 0.5f);
|
|
}
|
|
out[4 * i + 3] = 255;
|
|
}
|
|
}
|