Skip to content

Commit efd1ebb

Browse files
committed
Luma2: Step 4B, texture mirrors and sampler mirrors on D3D11, FF7R's upgrade rules
Resources::Upgrade ports 1.0's rule matching (Game::Resources::SetUpgrades, DXGI formats); D3D11 mirrors are made with the game's textures and views, swapped at binds, and copies, resolves, clears and CPU uploads follow them. Sampler mirrors add 16x anisotropy and the LOD bias offset. Resources tab, luma_status resources, tests.
1 parent dc26fa2 commit efd1ebb

29 files changed

Lines changed: 2349 additions & 31 deletions

‎Luma2/Docs/Guidelines.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,3 +530,7 @@ Add a row whenever 2.0 behaves differently from 1.0 on purpose, or when an impro
530530
| Recipe resources | `managed_resources` entries, `luma_settings`/`luma_data` handles, `fast_noise` mapped to "FAST Noise" by name | `Resources::Database` entries by name (textures from the Textures folders, `LumaSettings`, `LumaData`) as requests; bound before the draw events as 1.0 | one database for everything Luma owns | decided, Step 3 |
531531
| Textures from disk | `LoadTexture2DArray` of a PNG sequence behind `ENABLE_FAST_NOISE_TEXTURES` | `.dds` files in the Textures folders, all loaded when the game sets `Options::textures.load` (inclusion and exclusion lists later); FAST noise converted to one DDS array | a per-game option instead of a define; any texture by name | decided, Step 3 |
532532
| Patches with bindings on D3D12 | (no D3D12) | not published: DXP adds SM5.0 bindings in space 0, which the game's signature doesn't describe; the game's pipeline runs | needs DXP's upgrade to SM5.1 (Plan.md Step 3) | idea, DXP |
533+
| Sampler mirrors | made for every sampler at once when the LOD bias offset changes (at present) | made at the first bind of a sampler after the offset changed, kept per offset | no sampler list to walk; D3D11 shares equal state objects, so a creation is cheap | decided, Step 4 |
534+
| Copy compatibility | `AreFormatsCopyCompatible` took two formats without a typeless family (R11G11B10, R9G9B9E5) as compatible | equal, or the same typeless family, or two 8 bit RGBA/BGRA | 1.0 would `CopyResource` R11G11B10 into R9G9B9E5 | decided, Step 4 |
535+
| CPU data into mirrors | `ConvertResourceData`: 8 bit RGBA and BGRA to RGBA16F | the same plus R10G10B10A2_UNORM and R11G11B10_FLOAT, pitches respected | FF7R's upgraded formats | decided, Step 4 |
536+
| Readback targets | PS and CS passes, through the debug draw | any call, by its primary shader (a depth only VS pass too), the call run by Luma before the copy | the readback isn't tied to the debug draw any more | decided, Step 4 |

‎Luma2/Docs/Plan.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,18 @@ Built on part C's answers; the shapes below are the starting point.
677677
* Memory: accept the doubled targets, measure at 4K, note the number.
678678
* Mirror creation on D3D12 synchronous at the original's creation (it's the game's creation call, already allowed to be slow), not a job.
679679
680+
**Notes (parts A and B built, commits `09e8e428` to the part B commit; nothing verified in the game yet):**
681+
* Readback (`Trace/Readback.*`, `Trace/D3D11/D3D11Readback.*`, `Trace/D3D12/D3D12Readback.*`, Development): one target at a time, armed by the pipe thread, which waits for the result and writes the `.bin`. The target call is the n-th call of a shader at its stage in the frame (Track's instance; `Capture::GetInstance` shared with the Trace panel). By `index` it is the entry's primary shader (PS, else VS, else CS), so any pass with views is read, where 1.0 took PS and CS only. At the call the handler runs the game's call itself (unless a callback already ran, skipped or replaced it, as 1.0's `replaced`) and copies the view; the present reads the copy. D3D11: rtv, srv, uav, depth, stencil (MSAA resolved first), on the immediate context only. D3D12: a readback buffer filled on the game's list right after the call, read once a queue executed that list (a fence wait at the next present); the resource state is the call's own for render targets and UAVs, and for shader resources the last transition the list recorded (new `ResourceBarrier` hook, Development); depth, stencil and MSAA aren't read on D3D12 yet. `view=swapchain` reads the back buffer at present on both APIs, before the overlay (D3D12 on a list of Luma's on the presenting queue). `ui` and `registered:<name>` wait for Step 5. `luma_compare` and `luma_golden` are the bridge's (`Scripts/luma_mcp.py`): nothing in the game. `luma_read_cbuffer` runs on Track (`frames` for a series, the overlay's tracking put back after).
682+
* Trace: copies, resolves, clears and CPU writes are events on both APIs (`EventType::Copy`, `Clear`, `CpuWrite`; MCP types `copy`, `clear`, `cpu_write`, `copy_kind`, and in `trace_get` 1.0's `source`, `target`, `copy`, `clear_values`), recorded as the game made the call, before the mirrors redirect it. Buffer writes are hidden unless `include_buffer_writes` (the panel's "Buffer writes" chip). D3D12 gained hooks for `CopyResource`, `CopyTextureRegion`, `CopyBufferRegion`, `ResolveSubresource` and the four clears (`Function::Copy`, `Clear`), whose handlers part D extends. A trace resource that is a mirror is described as the game's texture with `mirror` (format and size).
683+
* Game API (Guidelines section 8): Core now hooks texture and view creation, view and sampler binds, writes, copies and clears, so the native callbacks on those are `Game::D3D11::Events` fields with the same signatures: `on_create_buffer`/`on_init_buffer`, `on_create_texture_2d`/`on_init_texture_2d`, `on_resource_destroyed`, `on_init_*_view`, `on_view_destroyed`, `on_create_sampler_state`, `on_bind_render_targets*`, `on_map`, `on_unmap`, `on_unmapped`, `on_update_subresource`, `on_copy_resource`, `on_copy_subresource_region`, `on_resolve_subresource`, `on_clear_render_target_view`, `on_clear_view`. `Callbacks` keeps blend state creation, the IA binds, `CopyStructureCount` and deferred contexts. No ported game used any of them.
684+
* Upgrade rules: `Game::Resources::SetUpgrades(Resources::Upgrade::Rules)` with `DXGI_FORMAT` (decided with the owner after reviewing the sketch); `Resources::Upgrade::Match` is 1.0's `GetOptionalResourceUpgradeDesc` for what Step 4 needs: render targets, UAVs and depth of GPU-only textures, the 2D size filters, LUTs. Not ported: writable shader resources (1.0 took them with its chains: Step 6), the Development warning about 1x1 aspect matches; `custom_dimensions` is `LUMA_UNIMPLEMENTED` (a size change is Step 6). The display size is the primary screen's until Step 5.
685+
* Mirrors on D3D11 (`Resources/Mirrors.*`, `Resources/D3D11/D3D11Mirrors.*`): made in `PostCreateTexture1D/2D/3D` with the game's desc in the upgraded format, `SHADER_RESOURCE` added (Luma's converting copies and readbacks read it), shared and CPU flags dropped; initial data converted. State in private data both ways (`MirrorState` on the game's texture, `OriginalState` on the mirror, `ViewState` on each game view), so a mirror and its views go with the game's objects (checked on WARP). Swapped at `*SetShaderResources`, `CSSetUnorderedAccessViews`, `OMSetRenderTargets(AndUnorderedAccessViews)` (after the game's event) while `Mirrors::IsSwapping()` (`Luma.Enabled` and `Luma.Resources.Upgrades`, Development setting); a bind costs one atomic load when no view is mirrored, else one `GetPrivateData` per view. Copies and region copies between copy-compatible formats are issued on the mirrors; between a mirror and a texture of another format through `Draw::D3D11::Copy`/`Scale` (1.0's `OnCopyResource_Internal`), with proxies made per copy where a side lacks the bind flag (1.0 cached two); a region copy converts only a whole single subresource, else it asserts and the originals are copied. A resolve between a mirror and another family is `LUMA_UNIMPLEMENTED`. Clears go to the mirror's view. `UpdateSubresource` into a mirrored texture is converted into the mirror always (1.0's `ConvertResourceData`, with R10G10B10A2 and R11G11B10 added: FF7R's formats), the original written too. `Map` isn't handled: textures the rules match are GPU only, so they can't be mapped.
686+
* Samplers (`Resources/Samplers.*`, `Resources/D3D11/D3D11Samplers.*`, `Options::samplers.upgrade`): 1.0's Publishing branch of `CreateCustomSampler` (mode 4, comparison samplers too) in every build. A mirror is made at the first bind after the offset's generation moved, not at the present (1.0 made every sampler's at once when the offset changed): no list of samplers to walk, and the cost is one `CreateSamplerState` that D3D11 usually answers with a shared object. Development settings `Luma.Development.Samplers.CustomBias` and `.Bias` stand for 1.0's `custom_texture_mip_lod_bias_offset`; 1.0's Development-only modes 5 to 7 and `samplers_upgrade_mode_2` aren't ported. `SetLodBiasOffset` is for Step 7's SR.
687+
* Dev UI: "Resources" tab (rules, mirror count, views and memory, the mirror list newest first, sampler state, Luma's database entries). `luma_status` has `resources` (`upgrades`, `samplers`, `mirrors`, `mirror_mb`, `views`, `lod_bias_offset`).
688+
* FF7R: `SetUpgrades` with 1.0's formats, filters and 3D LUT, `.samplers = {.upgrade = true}`.
689+
* Tests (94 cases): FF7R's rules against the textures 1.0 upgraded, the size filters and view formats, copy compatibility, the conversions, the sampler mirror desc, and on WARP a mirror with its view, swapped at bind, written by the CPU and released with the game's texture.
690+
* Not done yet: everything in Verify A and B (in the game, both APIs where it says), the 1.0 goldens (the owner packages `5788b1c6`), the bind cost measured on WARP through the real hooks, part C (a D3D12 research session) and part D.
691+
680692
---
681693
682694
## Step 5: Swapchain, display and HDR

‎Luma2/Source/Core/Game/Game.hpp‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
#include "Resources/Bindings.hpp"
1919
#include "Resources/LumaCBuffers.hpp"
2020
#include "Resources/ResourceDatabase.hpp"
21+
#include "Resources/Samplers.hpp"
2122
#include "Resources/Textures.hpp"
23+
#include "Resources/Upgrade.hpp"
2224
#include "Settings/Keys.hpp"
2325
#include "Settings/Registry.hpp"
2426
#include "Shaders/Defines.hpp"
@@ -91,13 +93,15 @@ struct GameDeviceDataType
9193
struct Options
9294
{
9395
// The LumaData writer runs at every draw with a Luma shader; LumaData uploads only when its bytes changed (1.0)
94-
Resources::LumaCBuffers::Options cbuffers;
96+
Luma::Resources::LumaCBuffers::Options cbuffers;
9597
// The bound-state cache's groups
9698
Draw::Options draw;
9799
// Where Luma's own bindings go (D3D12's register space)
98-
Resources::Bindings::Options bindings;
100+
Luma::Resources::Bindings::Options bindings;
99101
// The .dds files of the Textures folders, as Resources::Database entries
100-
Resources::Textures::Options textures;
102+
Luma::Resources::Textures::Options textures;
103+
// 1.0's enable_samplers_upgrade: the game's anisotropic samplers get Luma's mirrors (16x, SR's LOD bias)
104+
Luma::Resources::Samplers::Options samplers;
101105
// The game's own bytecode patches (recipes are files in the Recipes folder)
102106
Luma::Shaders::Patching::Options patching;
103107
GameDeviceDataType device_data;
@@ -106,6 +110,12 @@ struct Options
106110
// Once, in Init. A second call is ignored.
107111
void SetOptions(const Options& options);
108112

113+
// The game's textures Luma upgrades, through mirrors (1.0's texture_upgrade_formats and the other texture_format_upgrades_* statics). The
114+
// rules name DXGI formats, which D3D11 and D3D12 share: one call for both (Guidelines.md section 4). In Init.
115+
namespace Resources {
116+
void SetUpgrades(const Luma::Resources::Upgrade::Rules& rules);
117+
} // namespace Resources
118+
109119
// Drawn with UI::Widgets. Plain settings go through the registry instead.
110120
struct Panels
111121
{
@@ -118,7 +128,7 @@ struct Panels
118128
void SetPanels(const Panels& panels);
119129

120130
// The game's part is its GameSettings member. Written on the presenting thread only; contexts upload what was last published.
121-
using Resources::LumaCBuffers::cb_luma_global_settings;
131+
using Luma::Resources::LumaCBuffers::cb_luma_global_settings;
122132

123133
// Core publishes at every present: this shows a change made elsewhere (on_init_swapchain) sooner. Presenting thread only.
124134
void MarkLumaSettingsDirty();
@@ -140,7 +150,7 @@ void RunOriginal(const Call& call);
140150
// Until the game binds another shader on `stage`. Nothing on a stage without a variant.
141151
void UseOriginal(const Call& call, ShaderStage stage);
142152
// With the resources the shader requests. From a draw event, Core then binds no variant's requests for that call.
143-
void BindLumaShader(ID3D11DeviceContext* context, uint64_t name_hash, const Resources::Database::CustomData& custom = {});
153+
void BindLumaShader(ID3D11DeviceContext* context, uint64_t name_hash, const Luma::Resources::Database::CustomData& custom = {});
144154
// A texture of the Textures folders (Options::textures), by its file's name: "fast_noise"_h. Null if it didn't load. Lives as long
145155
// as the device.
146156
ID3D11ShaderResourceView* GetTexture(ID3D11Device* device, uint64_t name_hash);

‎Luma2/Source/Core/Game/Registry.cpp‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
#include "Hooks/Lifetime.hpp"
1515
#include "Resources/Bindings.hpp"
1616
#include "Resources/LumaCBuffers.hpp"
17+
#include "Resources/Samplers.hpp"
1718
#include "Resources/Textures.hpp"
19+
#include "Resources/Upgrade.hpp"
1820
#include "Shaders/Patching.hpp"
1921
#include "Shaders/Database.hpp"
2022
#include "Shaders/Files.hpp"
@@ -153,17 +155,23 @@ void SetOptions(const Options& options)
153155
return;
154156
}
155157
declarations.options_set = true;
156-
Resources::LumaCBuffers::SetOptions(options.cbuffers);
157-
Resources::Bindings::SetOptions(options.bindings);
158-
Resources::Textures::SetOptions(options.textures);
158+
Luma::Resources::LumaCBuffers::SetOptions(options.cbuffers);
159+
Luma::Resources::Bindings::SetOptions(options.bindings);
160+
Luma::Resources::Textures::SetOptions(options.textures);
159161
Luma::Shaders::Patching::SetOptions(options.patching);
162+
Luma::Resources::Samplers::SetOptions(options.samplers);
160163
Draw::SetOptions(options.draw);
161164
declarations.device_data = options.device_data;
162165
}
163166

167+
void Resources::SetUpgrades(const Luma::Resources::Upgrade::Rules& rules)
168+
{
169+
Luma::Resources::Upgrade::SetRules(rules);
170+
}
171+
164172
void MarkLumaSettingsDirty()
165173
{
166-
Resources::LumaCBuffers::MarkSettingsDirty();
174+
Luma::Resources::LumaCBuffers::MarkSettingsDirty();
167175
}
168176

169177
void SetPanels(const Panels& panels)
@@ -347,10 +355,12 @@ void LogDeclarations()
347355
void Reset()
348356
{
349357
declarations = {};
350-
Resources::LumaCBuffers::SetOptions({});
351-
Resources::Bindings::SetOptions({});
352-
Resources::Textures::SetOptions({});
358+
Luma::Resources::LumaCBuffers::SetOptions({});
359+
Luma::Resources::Bindings::SetOptions({});
360+
Luma::Resources::Textures::SetOptions({});
353361
Luma::Shaders::Patching::SetOptions({});
362+
Luma::Resources::Samplers::SetOptions({});
363+
Luma::Resources::Upgrade::SetRules({});
354364
Draw::SetOptions({});
355365
}
356366

0 commit comments

Comments
 (0)