Skip to content
Draft
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
17 changes: 17 additions & 0 deletions clean/FORMATS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,23 @@ with FP16 arithmetic: each weight rounded to FP16 once, `a ← h(fma(ŵ_k, t_k,
FP16 rounding per step), then the clamp to the range of the five taps; the same in both passes. **analytic**: the
Lanczos set of draft 3 with FP32 sums, the constants `kLz0 … kLz4` of `post.co` / `post_w64.co`, unchanged.

Runtime option `e1b` (`runtime_options` / `WG_RUNTIME_OPTIONS`; model M on the native D3D12 executor; default `off`):
`e1b=on` runs the input encoder forms `e1b/input_encoder[_direct]` (kernels/encoder.hlsl, `ENC_E1 == 2`). Each wave
issues the loads of its four history taps first, then tests per tap whether every lane's near / far tap position equals
its centre tap position. Where it does, that tap's values are copied from the centre tap instead of being computed
again. On the validated driver the output is byte for byte the default encoder's (the two forms compile to the same
floating-point arithmetic instructions). The gain depends on camera motion: still and evenly panning views save the
most; strong parallax gains nothing and can be slightly slower.

The choice is made once at create (runtime.cpp `e1b_resolve`). E1b runs only for model M on the native D3D12 executor
without motion at output resolution, and only when its form for the context's route (direct or buffer) prepares:
shader file or built-in copy, digest, pipeline. The default warm-up never compiles the E1b forms (device_d3d12.cpp
`optional_kernel`), so a missing or failing E1b shader cannot fail a create. The log's kernel line shows `e1b=on`, or
`e1b=off (<reason>)`: Model L, Model K, not the native D3D12 executor, motion at output resolution, not in the manifest,
or could not be prepared. The default encoder then runs. `e1b=off` or no key: the default encoder, and no `e1b` item.
Any other value (a typo) does not fail create: E1b stays off and the log gets one line,
`e1b: unknown value '<value>', using off` (clean/runtime/e1b_option.h).

Runtime option `output_filter` (`runtime_options` / `WG_RUNTIME_OPTIONS`):

| Value | Filter |
Expand Down
8 changes: 8 additions & 0 deletions clean/dx12/kernel_sets.json
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,14 @@
[
"w64/post_tf_p12_diet_vsit_tex",
"post_w64__orig.co"
],
[
"e1b/input_encoder",
"-"
],
[
"e1b/input_encoder_direct",
"-"
]
],
"model_l": [
Expand Down
91 changes: 91 additions & 0 deletions clean/dx12/kernels/encoder.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
// and only the store is conditional: the same bytes.
// @wg-kernel input_encoder co=encoder.co block=256 wave=32 args=p,p,p,p,p,p,p,p,p,p,p,p,b116
// @wg-kernel input_encoder_direct co=encoder.co entry=input_encoder block=256 wave=32 args=p,p,p,p,p,p,p,p,p,p,p,p,b116 rs=direct defines=WG_DIRECT_TEXTURES=1
// E1b (runtime option e1b=on, opt-in, default off): the near / far history taps copied from the centre tap when the
// whole wave's tap positions match (pixel_values, ENC_E1 == 2: all four taps' loads issued first, then one wave-wide
// test per tap); byte for byte the default encoder. ENC_E1 == 1 is the same test without the early loads (kept as the
// reference form; not declared). D3D12-only forms.
// @wg-kernel e1b/input_encoder co=- entry=input_encoder block=256 wave=32 args=p,p,p,p,p,p,p,p,p,p,p,p,b116 defines=ENC_E1=2
// @wg-kernel e1b/input_encoder_direct co=- entry=input_encoder block=256 wave=32 args=p,p,p,p,p,p,p,p,p,p,p,p,b116 rs=direct defines=WG_DIRECT_TEXTURES=1,ENC_E1=2
//
// Motion at output resolution (WG_ENC_MO; HIP: input_encoder_mo of kernels/encoder_mo.hip, code object encoder_mo.co
// under the tag "emo"; encoder_motion_outres.h): no depth neighbourhood; per pixel (x, y) one point read of the raw motion
Expand Down Expand Up @@ -45,6 +51,9 @@
// @wg-kernel lev/input_encoder_q20 co=- entry=input_encoder block=256 wave=32 args=p,p,p,p,p,p,p,p,p,p,p,p,b116 defines=WG_ENC_VALUES_OUT=1,WG_ENC_Q20=1
// @wg-kernel lev/input_encoder_direct_q20 co=- entry=input_encoder block=256 wave=32 args=p,p,p,p,p,p,p,p,p,p,p,p,b116 rs=direct defines=WG_DIRECT_TEXTURES=1,WG_ENC_VALUES_OUT=1,WG_ENC_Q20=1
#include "wg_platform.hlsli"
#ifndef ENC_E1
#define ENC_E1 0
#endif
#ifndef WG_ENC_VALUES_OUT
#define WG_ENC_VALUES_OUT 0
#endif
Expand Down Expand Up @@ -163,6 +172,52 @@ float3 hist_values(int mode, Uni U, float px, float py, bool valid, float3 gc)
return o;
}

#if ENC_E1 == 2
// E1b: hist_values split into its fetch (footprint + the two history loads) and its arithmetic, text for text, so that
// all four taps' loads are issued before the wave-uniform skip decisions (today's code overlaps them across taps).
struct HTap { uint2 t0, t1; float fx, fy; };
HTap hist_fetch(Uni U, float px, float py)
{
Foot ft = enc_footprint(px * U.shx - 0.5f, py * U.shy - 0.5f, P_hw, P_hh, 4u);
HTap h;
h.t0 = wg_ld64(hist, ft.off0);
h.t1 = wg_ld64(hist, ft.off1);
h.fx = ft.fx;
h.fy = ft.fy;
return h;
}
float3 hist_math(int mode, Uni U, HTap h, bool valid, float3 gc)
{
uint2 t0 = h.t0, t1 = h.t1;
float kS = mode == kHistSame ? kS_SAME : kS_GEN;
float wy1 = h.fy * kS, wy0 = kS - wy1;
float ifx = 1.0f - h.fx;
float w00 = ifx * wy0, w10 = h.fx * wy0, w01 = ifx * wy1, w11 = h.fx * wy1;
float3 o;
[unroll] for (uint c = 0; c < 3; ++c) {
float a0 = (float)((t0.x >> (10u * c)) & 1023u), b0 = (float)((t0.y >> (10u * c)) & 1023u);
float a1 = (float)((t1.x >> (10u * c)) & 1023u), b1 = (float)((t1.y >> (10u * c)) & 1023u);
float x = mad(w00, a0, mad(w10, b0, 0.0f));
x = mad(w01, a1, mad(w11, b1, x));
float g;
if (mode == kHistSame) {
g = wg_med3f(x, k1em4, 3.0f);
} else {
float a;
if (mode == kHistGeneral) {
float pm1 = wg_exp2(wg_med3f(x, kL2_1em4, kL2_3)) - 1.0f;
a = mad(U.rr, pm1, 1.0f);
} else {
a = mad(x, U.ce, 1.0f);
}
g = kG * wg_log2(wg_fmax(a, k1em4));
}
o[c] = valid ? g : gc[c];
}
return o;
}
#endif

// The 20 values of the pixel at render column x, row y (pixel_values of encoder.hip).
void pixel_values(int mode, Uni U, int x, int y, float4 c, float2 mn, float2 mf, float2 mc, out float v[20]
#if defined(WG_DIRECT_TEXTURES)
Expand Down Expand Up @@ -204,10 +259,46 @@ void pixel_values(int mode, Uni U, int x, int y, float4 c, float2 mn, float2 mf,
[unroll] for (uint i2 = 15; i2 < 20; ++i2) v[i2] = 0.0f;
return;
}
#if ENC_E1 == 2
// E1b (runtime option e1b=on): as E1, with all four taps' loads issued first (hist_fetch), then the skip decisions, then the
// arithmetic of the taps that are not copies (hist_math). Same operations per tap, same bytes.
{
const HTap f0 = hist_fetch(U, qx[0], qy[0]), f1 = hist_fetch(U, qx[1], qy[1]);
const HTap f2 = hist_fetch(U, qx[2], qy[2]), f3 = hist_fetch(U, qx[3], qy[3]);
const bool s0 = WaveActiveAllTrue(qx[0] == qx[1] && qy[0] == qy[1]);
const bool s2 = WaveActiveAllTrue(qx[2] == qx[1] && qy[2] == qy[1]);
const float3 h1 = hist_math(mode, U, f1, valid[1], gc);
float3 h0 = h1, h2 = h1;
if (!s0) h0 = hist_math(mode, U, f0, valid[0], gc);
if (!s2) h2 = hist_math(mode, U, f2, valid[2], gc);
const float3 h3 = hist_math(mode, U, f3, valid[3], gc);
v[3] = h0.x; v[4] = h0.y; v[5] = h0.z;
v[6] = h1.x; v[7] = h1.y; v[8] = h1.z;
v[9] = h2.x; v[10] = h2.y; v[11] = h2.z;
v[12] = h3.x; v[13] = h3.y; v[14] = h3.z;
}
#elif ENC_E1
// E1 (reference form, not declared): the near (0) / far (2) tap is the centre tap (1) whenever its position is the centre's: same
// qx, qy -> same footprint, same valid flag, same values. When that holds for every lane of the wave (a ballot),
// the copy replaces the evaluation; otherwise the tap is computed as in the default. NaN positions compare unequal
// and are computed; +0 / -0 positions compare equal and give the same footprint. Same bytes in every case.
{
const float3 h1 = hist_values(mode, U, qx[1], qy[1], valid[1], gc);
float3 h0 = h1, h2 = h1;
if (!WaveActiveAllTrue(qx[0] == qx[1] && qy[0] == qy[1])) h0 = hist_values(mode, U, qx[0], qy[0], valid[0], gc);
if (!WaveActiveAllTrue(qx[2] == qx[1] && qy[2] == qy[1])) h2 = hist_values(mode, U, qx[2], qy[2], valid[2], gc);
const float3 h3 = hist_values(mode, U, qx[3], qy[3], valid[3], gc);
v[3] = h0.x; v[4] = h0.y; v[5] = h0.z;
v[6] = h1.x; v[7] = h1.y; v[8] = h1.z;
v[9] = h2.x; v[10] = h2.y; v[11] = h2.z;
v[12] = h3.x; v[13] = h3.y; v[14] = h3.z;
}
#else
[unroll] for (uint k2 = 0; k2 < 4; ++k2) {
float3 h = hist_values(mode, U, qx[k2], qy[k2], valid[k2], gc);
v[3 + 3 * k2] = h.x; v[4 + 3 * k2] = h.y; v[5 + 3 * k2] = h.z;
}
#endif

// Motion disagreement (value 15)
{
Expand Down
4 changes: 3 additions & 1 deletion clean/dx12/vgpr_limits.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@
"bneck/swin_bneck6_mb_nc": 192,
"bneck/swin_bneck6_mbo_nc": 192,
"d1p12/swin_block_c64_add_dec0_nc": 192,
"d1p12/swin_block_c64_add_dec0_xd_nc": 192
"d1p12/swin_block_c64_add_dec0_xd_nc": 192,
"e1b/input_encoder": 128,
"e1b/input_encoder_direct": 128
}
6 changes: 5 additions & 1 deletion clean/runtime/device_d3d12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,11 +502,15 @@ class NativeD3D12Device final : public Device, public NativeD3D12Ext {
return v;
}
bool external_markers() const override { return marker_map_ != nullptr; }
// Opt-in kernel forms (selected by a runtime option, e.g. e1b=on): never compiled with the default set. The runtime
// prepares the one form it selects at create (prepare()), and falls back to the default kernel if that fails, so a
// missing or failing optional shader cannot fail a default create.
static bool optional_kernel(const std::string& name) { return name.compare(0, 4, "e1b/") == 0; }
bool warm_up(std::string* error) override {
if (external_) return extension_result(fail("native D3D12 warm_up: recording into the caller's list"), error);
for (auto& entry : kernels_) {
NativeKernel& k = entry.second;
if (k.pso || !variant_matches(k) || (k.rs != "buffer" && k.rs != "direct")) continue;
if (k.pso || optional_kernel(k.name) || !variant_matches(k) || (k.rs != "buffer" && k.rs != "direct")) continue;
if (!create_pso(k)) return extension_result(false, error);
}
return extension_result(true, error);
Expand Down
33 changes: 33 additions & 0 deletions clean/runtime/e1b_option.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: MIT
// The runtime option e1b (runtime.cpp e1b_resolve; clean/FORMATS.md "Runtime option e1b"). "on" asks for E1b, "off"
// turns it off. Any other value is most likely a typo in a player's ini, so it never fails create: E1b stays off and
// *note gets one line for the log. A valid value clears *note, so the last e1b key decides.
#pragma once
#include <string>

inline bool e1b_option_on(const std::string& value, std::string* note) {
if (note) note->clear();
if (value == "on") return true;
if (value != "off" && note) *note = "e1b: unknown value '" + value + "', using off";
return false;
}

// The e1b key of a runtime options string ("key=value" items separated by ';' or ','; other keys are ignored).
// No e1b key: off, no note.
inline void e1b_scan(const std::string& options, bool* on, std::string* note) {
*on = false;
note->clear();
for (size_t a = 0; a <= options.size();) {
size_t b = options.find_first_of(";,", a);
if (b == std::string::npos) b = options.size();
const std::string kv = options.substr(a, b - a);
a = b + 1;
const size_t eq = kv.find('=');
if (eq != std::string::npos && kv.compare(0, eq, "e1b") == 0 && eq == 3) *on = e1b_option_on(kv.substr(eq + 1), note);
}
}

// The kernel report's e1b item (only when the option asked for E1b): the route chosen at create, never a side effect.
inline std::string e1b_report(bool on, const std::string& reason) {
return on ? std::string(" e1b=on") : " e1b=off (" + reason + ")";
}
21 changes: 20 additions & 1 deletion clean/runtime/interop/tests/test_interop_d3d12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,17 @@ struct Scene {
g.upload(depth, tw, th, 4, d.data(), depthState);
std::vector<uint16_t> m((size_t)mw * mh * 4);
motionF.assign((size_t)mw * mh * 2, 0.0f);
// WG_TEST_MOTION_PATTERN (input encoder E1b timing): ramp (default: the field below), zero (a static scene), pan (one
// constant vector), parallax (the ramp plus a depth term: every neighbour differs, and more so near the camera)
const char* mpat = std::getenv("WG_TEST_MOTION_PATTERN");
const int mp = !mpat ? 0 : !std::strcmp(mpat, "zero") ? 1 : !std::strcmp(mpat, "pan") ? 2 : !std::strcmp(mpat, "parallax") ? 3 : 0;
for (UINT y = 0; y < mh; ++y)
for (UINT x = 0; x < mw; ++x) {
const float a = q16(x / 128.0f - 0.25f), b = q16(0.5f - y / 64.0f);
const float dd = (x < tw && y < th) ? d[(size_t)y * tw + x] : 0.0f;
const float a = mp == 1 ? 0.0f : mp == 2 ? q16(0.75f) : mp == 3 ? q16(x / 128.0f - 0.25f + 48.0f * dd)
: q16(x / 128.0f - 0.25f);
const float b = mp == 1 ? 0.0f : mp == 2 ? q16(-0.375f) : mp == 3 ? q16(0.5f - y / 64.0f - 24.0f * dd)
: q16(0.5f - y / 64.0f);
uint16_t* p = &m[4 * ((size_t)y * mw + x)];
p[0] = to_half(a), p[1] = to_half(b), p[2] = to_half(7.0f), p[3] = 0;
motionF[2 * ((size_t)y * mw + x)] = a;
Expand Down Expand Up @@ -2184,6 +2192,17 @@ bool case_soak_pipe(Gpu& g, const char* dll, const std::string& options, int fra
f.jitter[0] = jitter[(t - 1) % 4][0];
f.jitter[1] = jitter[(t - 1) % 4][1];
}
// WG_TEST_RESET_AT=<frame>[,<frame>...] (this test program only): a history reset on those frames as well
// (mid-stream resets for the byte legs; the timing runs set none)
if (const char* ra = std::getenv("WG_TEST_RESET_AT")) {
for (const char* p = ra; *p;) {
char* e = nullptr;
const long v = std::strtol(p, &e, 10);
if (e == p) break;
if (v == t) f.reset = 1;
p = *e ? e + 1 : e;
}
}
f.command_list = g.list;
const WgResult rr = c.be.b->dispatch(c.u, &f);
if (rr != WG_OK) { std::printf(" soak-pipe frame %d: dispatch %d%c", t, (int)rr, 10); CHECK(rr == WG_OK); return false; }
Expand Down
Loading
Loading