Skip to content

Commit dc26fa2

Browse files
committed
Luma2: Step 4A, copies, clears and CPU writes in trace; Core's resource functions report through events
Mirrors (Step 4B) and trace hook texture and view creation, view and sampler binds, writes, copies and clears, so the native callbacks on those become Game::D3D11::Events fields with their signatures (Guidelines section 8). Trace lists copy, clear and cpu_write entries on both APIs (D3D12 copy and clear hooks), 1.0's MCP names.
1 parent 09e8e42 commit dc26fa2

20 files changed

Lines changed: 678 additions & 99 deletions

‎Luma2/Docs/Guidelines.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ Add a row whenever 2.0 behaves differently from 1.0 on purpose, or when an impro
481481
| User-facing errors | `MessageBox`, on-screen text | toasts | section 13 | decided |
482482
| Old shader file cleanup | `old_shader_file_names` per game | keep as is | no reason found to change | keep |
483483
| Native game callbacks | one virtual per native function and stage (`PreVSSetConstantBuffers`, `PreDraw`...), Core's functions included | one struct of function pointers (`Game::D3D11::Callbacks`) for the functions Core doesn't hook only; callbacks no 1.0 game overrides are not offered (texture 1D/3D, view `Pre`s, input layouts, samplers destruction, per-stage SRV/sampler binds, clears other than RTV and `ClearView`, `PreCreateDevice`, `PreFlush`) | a game's work never runs in the middle of Core's on the same function | decided |
484-
| Game's part of Core's functions | 1.0's Core virtuals (`OnInitDevice`, `OnInitSwapchain`, `OnPresent`, `OnDestroyDeviceData`), plus `Pre`/`Post` virtuals on the same functions | events only, one struct per API (`Game::D3D11::Events`, `Game::D3D12::Events`), each called where 1.0's Core called its virtual: `on_init_device`, `on_destroy_device`, `on_create_swapchain` (one normalized `SwapchainDesc` for the three creation and resize functions, both APIs), `on_init_swapchain` (creation and each resize), `on_present`, `on_create_shader`, `on_bind_*`, `on_clear_state`, `on_finish_command_list`, `on_execute_command_list`; no `Pre`/`Post` present, swapchain, shader, bind or command list callbacks, no `SetFullscreenState` or `*SetShader` ones | Core keeps tracking what the game does, in 1.0's order; FF7R, Borderlands 2, Persona 5 Royal and Far Cry 5 port to them | decided |
484+
| Game's part of Core's functions | 1.0's Core virtuals (`OnInitDevice`, `OnInitSwapchain`, `OnPresent`, `OnDestroyDeviceData`), plus `Pre`/`Post` virtuals on the same functions | events only, one struct per API (`Game::D3D11::Events`, `Game::D3D12::Events`), each called where 1.0's Core called its virtual: `on_init_device`, `on_destroy_device`, `on_create_swapchain` (one normalized `SwapchainDesc` for the three creation and resize functions, both APIs), `on_init_swapchain` (creation and each resize), `on_present`, `on_create_shader`, `on_bind_*`, `on_clear_state`, and from Step 4 the resource events (`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`: the native callbacks they were, same signatures, where an `on_create_*` or a bind may still edit and a copy or clear may skip, as 1.0's virtuals could), `on_finish_command_list`, `on_execute_command_list`; no `Pre`/`Post` present, swapchain, shader, bind or command list callbacks, no `SetFullscreenState` or `*SetShader` ones | Core keeps tracking what the game does, in 1.0's order; FF7R, Borderlands 2, Persona 5 Royal and Far Cry 5 port to them | decided |
485485
| Shader creation callbacks | `PreCreate*Shader` could edit the bytecode, which Core then hashed | `on_create_shader(device, stage, hash, bytecode, shader)` after Core tracked it, read-only; the hash is the game's bytecode's; editing goes through variants (patches, Step 3) | a shader's hash stays what replacements, draw callbacks and debug names know it by | decided |
486486
| Game's requested swapchain desc | map keyed by swapchain pointer | the swapchain's private data | section 5 | decided |
487487
| Asserts | `MessageBox` asking to break | logged, `__debugbreak()` only with a debugger attached, Development only | a message box can hide behind a fullscreen game | decided |
@@ -511,7 +511,7 @@ Add a row whenever 2.0 behaves differently from 1.0 on purpose, or when an impro
511511
| Draw callbacks by hash | a list per stage, all listed stages must match (`ContainsAll`) | any listed hash bound on any stage (pixel first, then vertex, geometry, hull, domain) | games list one stage's hashes (FF7R: pixel); the stage of a hash is known from its shader | decided |
512512
| Bound-state cache | the current draw's arguments (`ENABLE_DRAW_DISPATCH_DATA_CACHE`) | the arguments are in `Call`; viewports and scissors, blend and depth are recorded at bind for the groups a game declares (all in Development); views, buffers and samplers are queried | the API unbinds views on its own, a record would hold dangling pointers or extend lifetimes | decided |
513513
| Game's LumaData part | `UpdateLumaInstanceDataCB(data, cmd_list_data, device_data)` at every draw binding LumaData, bytes compared | the same flow: `Options::cbuffers.update_luma_instance_data(CB::LumaInstanceData&, const GameDeviceData&, const Frame::Resolutions&)`, called at every draw with a Luma shader with the device's `GameDeviceData` and resolutions (cached in the context's state), the bytes compared with that context's last upload (a dirty flag version was tried and reverted) | no command list data to pass; Core's resolutions passed apart, as they're `Luma::Frame`'s | decided |
514-
| Trace entries | one per pipeline stage (VS entries hidden by default), copies, clears, binds, CPU writes, merged deferred lists | one per draw or dispatch with every stage's shader, labelled by its primary shader (PS, else VS, else CS), filters matching any stage; draws without a pixel shader hidden by default (1.0's VS entries); Luma's passes shown by default (1.0 hid custom draws); copies, clears, binds, CPU writes and the deferred merge not in v1, so no "Ignore Bindings" or "Ignore Buffer Writes" filter yet | the draw is the unit of the state shown; the rest comes with the features that need it (Step 4) | v1, extend |
514+
| Trace entries | one per pipeline stage (VS entries hidden by default), copies, clears, binds, CPU writes, merged deferred lists | one per draw or dispatch with every stage's shader, labelled by its primary shader (PS, else VS, else CS), filters matching any stage; draws without a pixel shader hidden by default (1.0's VS entries); Luma's passes shown by default (1.0 hid custom draws); copies, resolves, clears and CPU writes as entries since Step 4 (`copy`, `clear`, `cpu_write`, buffer writes hidden by default as 1.0); binds and the deferred merge not yet, so no "Ignore Bindings" filter | the draw is the unit of the state shown; the rest comes with the features that need it (Step 4) | v1, extend |
515515
| Overlay on HDR | drawn by ReShade's overlay, which encodes for the back buffer's color space at its "HDR overlay brightness" (203 nits) | Luma's own ImGui renderers with an encoding pixel shader, the color space from the game's `SetColorSpace1`, the brightness an Overlay setting | Luma draws its own overlay (section 14) | decided |
516516
| Constant buffer tracking | one slot index for all stages, the tracked buffer read at every matching draw, floats and ints listed | a slot per stage chosen in the capture, read once a frame for the selected call's instance, typed fields when a layout is known, changes highlighted | readable, and what changed is what's looked for | decided |
517517
| Cbuffer restoration | `ENABLE_AUTO_CBUFFER_RESTORATION` define, records all 14 slots of every stage | `Options::cbuffers.restore_after_draw`, records only Luma's two slots per stage; rebinds after a call whose requests were fulfilled, and after a callback's call (run, skipped or replaced) that bound Luma's shaders with `BindLumaShader` | a define per game becomes an init option (no preprocessor feature flags); only those two slots can be changed by Luma | decided |

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

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,17 @@
66
namespace Luma {
77
namespace Game {
88
namespace D3D11 {
9-
// Only for the functions no Core feature hooks (Startup::GetOwnedFunctions). Setting one installs its hook, so they are set in
10-
// Init, in field order. They get the real API objects, not Luma's proxies. A Pre returning true skips the call.
9+
// Only for the functions no Core feature hooks (Startup::GetOwnedFunctions): the others have events. Setting one installs its hook,
10+
// so they are set in Init, in field order. They get the real API objects, not Luma's proxies. A Pre returning true skips the call.
1111
struct Callbacks
1212
{
13-
// Resources
14-
void (*pre_create_buffer)(ID3D11Device* device, D3D11_BUFFER_DESC& desc, const D3D11_SUBRESOURCE_DATA*& initial_data) = nullptr;
15-
void (*post_create_buffer)(ID3D11Device* device, const D3D11_BUFFER_DESC& desc, const D3D11_SUBRESOURCE_DATA* initial_data, ID3D11Buffer* buffer) = nullptr;
16-
void (*pre_create_texture_2d)(ID3D11Device* device, D3D11_TEXTURE2D_DESC1& desc, const D3D11_SUBRESOURCE_DATA*& initial_data) = nullptr;
17-
void (*post_create_texture_2d)(ID3D11Device* device, const D3D11_TEXTURE2D_DESC1& desc, const D3D11_SUBRESOURCE_DATA* initial_data, ID3D11Texture2D* texture) = nullptr;
18-
// Buffers and textures of every dimension
19-
void (*on_resource_destroyed)(ID3D11Device* device, const ID3D11Resource* resource) = nullptr;
20-
21-
// Views: a null desc is the game's null one
22-
void (*post_create_shader_resource_view)(ID3D11Device* device, ID3D11Resource* resource, const D3D11_SHADER_RESOURCE_VIEW_DESC1* desc, ID3D11ShaderResourceView* view) = nullptr;
23-
void (*post_create_render_target_view)(ID3D11Device* device, ID3D11Resource* resource, const D3D11_RENDER_TARGET_VIEW_DESC1* desc, ID3D11RenderTargetView* view) = nullptr;
24-
void (*post_create_unordered_access_view)(ID3D11Device* device, ID3D11Resource* resource, const D3D11_UNORDERED_ACCESS_VIEW_DESC1* desc, ID3D11UnorderedAccessView* view) = nullptr;
25-
void (*post_create_depth_stencil_view)(ID3D11Device* device, ID3D11Resource* resource, const D3D11_DEPTH_STENCIL_VIEW_DESC* desc, ID3D11DepthStencilView* view) = nullptr;
26-
void (*on_view_destroyed)(ID3D11Device* device, const ID3D11View* view) = nullptr;
27-
28-
// States. CreateBlendState is reported as a DESC1 with logic ops disabled.
29-
void (*pre_create_sampler_state)(ID3D11Device* device, D3D11_SAMPLER_DESC& desc) = nullptr;
13+
// CreateBlendState is reported as a DESC1 with logic ops disabled
3014
void (*pre_create_blend_state)(ID3D11Device* device, D3D11_BLEND_DESC1& desc) = nullptr;
3115

32-
// Binds: the arrays are copies of the game's, which can be edited. Null arrays when the game keeps the bound ones (D3D11_KEEP_*).
33-
bool (*pre_om_set_render_targets)(ID3D11DeviceContext* context, UINT count, ID3D11RenderTargetView** views, ID3D11DepthStencilView*& depth_stencil) = nullptr;
34-
bool (*pre_om_set_render_targets_and_unordered_access_views)(ID3D11DeviceContext* context, UINT count, ID3D11RenderTargetView** views,
35-
ID3D11DepthStencilView*& depth_stencil, UINT uav_start, UINT uav_count, ID3D11UnorderedAccessView** uavs, const UINT* initial_counts) = nullptr;
3616
void (*post_ia_set_vertex_buffers)(ID3D11DeviceContext* context, UINT start, UINT count, ID3D11Buffer* const* buffers, const UINT* strides, const UINT* offsets) = nullptr;
3717
void (*post_ia_set_index_buffer)(ID3D11DeviceContext* context, ID3D11Buffer* buffer, DXGI_FORMAT format, UINT offset) = nullptr;
3818

39-
// Data
40-
void (*post_map)(ID3D11DeviceContext* context, ID3D11Resource* resource, UINT subresource, D3D11_MAP type, UINT flags, D3D11_MAPPED_SUBRESOURCE& mapped) = nullptr;
41-
bool (*pre_unmap)(ID3D11DeviceContext* context, ID3D11Resource* resource, UINT subresource) = nullptr;
42-
// After the original returned (the mapped memory is gone)
43-
void (*post_unmap)(ID3D11DeviceContext* context, ID3D11Resource* resource, UINT subresource) = nullptr;
44-
// `copy_flags` is only set by UpdateSubresource1
45-
bool (*pre_update_subresource)(ID3D11DeviceContext* context, ID3D11Resource* resource, UINT subresource, const D3D11_BOX* box, const void* data,
46-
UINT row_pitch, UINT depth_pitch, UINT copy_flags) = nullptr;
47-
bool (*pre_copy_resource)(ID3D11DeviceContext* context, ID3D11Resource* destination, ID3D11Resource* source) = nullptr;
48-
// A whole-subresource copy has no box, as the game passed it
49-
bool (*pre_copy_subresource_region)(ID3D11DeviceContext* context, ID3D11Resource* destination, UINT destination_subresource, UINT x, UINT y, UINT z,
50-
ID3D11Resource* source, UINT source_subresource, const D3D11_BOX* box, UINT copy_flags) = nullptr;
51-
bool (*pre_resolve_subresource)(ID3D11DeviceContext* context, ID3D11Resource* destination, UINT destination_subresource, ID3D11Resource* source,
52-
UINT source_subresource, DXGI_FORMAT format) = nullptr;
53-
bool (*pre_clear_render_target_view)(ID3D11DeviceContext* context, ID3D11RenderTargetView* view, const FLOAT color[4]) = nullptr;
5419
bool (*pre_copy_structure_count)(ID3D11DeviceContext* context, ID3D11Buffer* destination, UINT destination_offset, ID3D11UnorderedAccessView* source) = nullptr;
55-
// Not reported as a render target clear, even on a render target view
56-
bool (*pre_clear_view)(ID3D11DeviceContext* context, ID3D11View* view, const FLOAT color[4], const D3D11_RECT* rects, UINT rect_count) = nullptr;
5720

5821
// Deferred contexts
5922
void (*post_create_deferred_context)(ID3D11Device* device, ID3D11DeviceContext* context) = nullptr;

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,48 @@ struct Events
9191
// After Core tracked it. `hash` is the game's bytecode's: shaders are changed through variants, never here.
9292
void (*on_create_shader)(ID3D11Device* device, ShaderStage stage, uint32_t hash, std::span<const uint8_t> bytecode, ID3D11DeviceChild* shader) = nullptr;
9393

94+
// Resources and views (Core's mirrors and trace hook them). As 1.0's: an on_create_* runs before Core and may edit the desc (Core
95+
// sees the edit), an on_init_* after Core with the created object. Objects are always the game's, never Luma's mirrors.
96+
void (*on_create_buffer)(ID3D11Device* device, D3D11_BUFFER_DESC& desc, const D3D11_SUBRESOURCE_DATA*& initial_data) = nullptr;
97+
void (*on_init_buffer)(ID3D11Device* device, const D3D11_BUFFER_DESC& desc, const D3D11_SUBRESOURCE_DATA* initial_data, ID3D11Buffer* buffer) = nullptr;
98+
void (*on_create_texture_2d)(ID3D11Device* device, D3D11_TEXTURE2D_DESC1& desc, const D3D11_SUBRESOURCE_DATA*& initial_data) = nullptr;
99+
void (*on_init_texture_2d)(ID3D11Device* device, const D3D11_TEXTURE2D_DESC1& desc, const D3D11_SUBRESOURCE_DATA* initial_data, ID3D11Texture2D* texture) = nullptr;
100+
// Buffers and textures of every dimension
101+
void (*on_resource_destroyed)(ID3D11Device* device, const ID3D11Resource* resource) = nullptr;
102+
// A null desc is the game's null one
103+
void (*on_init_shader_resource_view)(ID3D11Device* device, ID3D11Resource* resource, const D3D11_SHADER_RESOURCE_VIEW_DESC1* desc, ID3D11ShaderResourceView* view) = nullptr;
104+
void (*on_init_render_target_view)(ID3D11Device* device, ID3D11Resource* resource, const D3D11_RENDER_TARGET_VIEW_DESC1* desc, ID3D11RenderTargetView* view) = nullptr;
105+
void (*on_init_unordered_access_view)(ID3D11Device* device, ID3D11Resource* resource, const D3D11_UNORDERED_ACCESS_VIEW_DESC1* desc, ID3D11UnorderedAccessView* view) = nullptr;
106+
void (*on_init_depth_stencil_view)(ID3D11Device* device, ID3D11Resource* resource, const D3D11_DEPTH_STENCIL_VIEW_DESC* desc, ID3D11DepthStencilView* view) = nullptr;
107+
void (*on_view_destroyed)(ID3D11Device* device, const ID3D11View* view) = nullptr;
108+
// Before Core's sampler mirror: may edit the desc
109+
void (*on_create_sampler_state)(ID3D11Device* device, D3D11_SAMPLER_DESC& desc) = nullptr;
110+
111+
// Render target binds, before Core swaps in mirrors: may edit the arrays (copies of the game's; null when the game keeps the bound
112+
// ones, D3D11_KEEP_*). True skips the bind.
113+
bool (*on_bind_render_targets)(ID3D11DeviceContext* context, UINT count, ID3D11RenderTargetView** views, ID3D11DepthStencilView*& depth_stencil) = nullptr;
114+
bool (*on_bind_render_targets_and_unordered_access_views)(ID3D11DeviceContext* context, UINT count, ID3D11RenderTargetView** views,
115+
ID3D11DepthStencilView*& depth_stencil, UINT uav_start, UINT uav_count, ID3D11UnorderedAccessView** uavs, const UINT* initial_counts) = nullptr;
116+
117+
// CPU writes, copies and clears, before Core redirects them to mirrors (on_map and on_unmapped after the call). True skips the
118+
// call.
119+
void (*on_map)(ID3D11DeviceContext* context, ID3D11Resource* resource, UINT subresource, D3D11_MAP type, UINT flags, D3D11_MAPPED_SUBRESOURCE& mapped) = nullptr;
120+
bool (*on_unmap)(ID3D11DeviceContext* context, ID3D11Resource* resource, UINT subresource) = nullptr;
121+
// After the original returned (the mapped memory is gone)
122+
void (*on_unmapped)(ID3D11DeviceContext* context, ID3D11Resource* resource, UINT subresource) = nullptr;
123+
// `copy_flags` is only set by UpdateSubresource1
124+
bool (*on_update_subresource)(ID3D11DeviceContext* context, ID3D11Resource* resource, UINT subresource, const D3D11_BOX* box, const void* data,
125+
UINT row_pitch, UINT depth_pitch, UINT copy_flags) = nullptr;
126+
bool (*on_copy_resource)(ID3D11DeviceContext* context, ID3D11Resource* destination, ID3D11Resource* source) = nullptr;
127+
// A whole-subresource copy has no box, as the game passed it
128+
bool (*on_copy_subresource_region)(ID3D11DeviceContext* context, ID3D11Resource* destination, UINT destination_subresource, UINT x, UINT y, UINT z,
129+
ID3D11Resource* source, UINT source_subresource, const D3D11_BOX* box, UINT copy_flags) = nullptr;
130+
bool (*on_resolve_subresource)(ID3D11DeviceContext* context, ID3D11Resource* destination, UINT destination_subresource, ID3D11Resource* source,
131+
UINT source_subresource, DXGI_FORMAT format) = nullptr;
132+
bool (*on_clear_render_target_view)(ID3D11DeviceContext* context, ID3D11RenderTargetView* view, const FLOAT color[4]) = nullptr;
133+
// Not reported as a render target clear, even on a render target view
134+
bool (*on_clear_view)(ID3D11DeviceContext* context, ID3D11View* view, const FLOAT color[4], const D3D11_RECT* rects, UINT rect_count) = nullptr;
135+
94136
// After Core records them, except constant buffers, reported first (1.0)
95137
void (*on_bind_constant_buffers)(ID3D11DeviceContext* context, ShaderStage stage, UINT start, UINT count, ID3D11Buffer* const* buffers) = nullptr;
96138
void (*on_bind_viewports)(ID3D11DeviceContext* context, UINT count, const D3D11_VIEWPORT* viewports) = nullptr;

0 commit comments

Comments
 (0)