Skip to content
Merged
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
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,10 @@ endforeach()
add_executable(startup_command_list_slot_names tests/startup_command_list_slot_names.cpp)
target_compile_features(startup_command_list_slot_names PRIVATE cxx_std_17)
add_test(NAME startup_command_list_slot_names COMMAND startup_command_list_slot_names)
# Root constants kept per recorded list (src/startup_stubs/root_constant_store.h, compute list slots 37 and 39, #870; slot 40 keeps its own graphics words).
add_executable(startup_root_constant_store tests/startup_root_constant_store.cpp)
target_compile_features(startup_root_constant_store PRIVATE cxx_std_17)
add_test(NAME startup_root_constant_store COMMAND startup_root_constant_store)
# Game-save stand-in storage (src/startup_stubs/save_store.h): a blob written by one process is read by a second.
add_executable(startup_gamesave_persist tests/startup_gamesave_persist.cpp)
target_compile_features(startup_gamesave_persist PRIVATE cxx_std_17)
Expand Down
21 changes: 21 additions & 0 deletions src/startup_stubs/d3d12_draw.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2446,6 +2446,17 @@ void __stdcall list_compute_table(Wrapper* self, UINT parameter, u64 gpu_handle)
log("x2w-render compute descriptor table dropped: parameter %u (%u parameters are kept)", parameter,
x2w_startup::ComputeTables::max_parameters);
}
// Keeps the words of a root constants call; a call outside the 16 x 8 words kept is logged and cut or dropped.
static void store_root_constants(x2w_root_constants::Store& store, const char* kind, UINT parameter, UINT count,
const std::uint32_t* data, UINT offset)
{
if (!count || !x2w_local::readable(data, std::size_t{count} * 4)) return;
const auto result = store.set(parameter, count, data, offset);
static LONG refused;
if (result != x2w_root_constants::Result::stored && first_calls(refused, 4))
log("x2w-render %s root constants %s: parameter %u, offset %u, count %u (16 parameters of 8 words are kept)", kind,
result == x2w_root_constants::Result::dropped ? "dropped" : "truncated", parameter, offset, count);
}
// List slot 40 (desktop 36 + 4), SetGraphicsRoot32BitConstants(parameter, count, data, offset).
void __stdcall list_root_constants(Wrapper* self, UINT parameter, UINT count, const std::uint32_t* data, UINT offset)
{
Expand All @@ -2461,6 +2472,16 @@ void __stdcall list_root_constants(Wrapper* self, UINT parameter, UINT count, co
count <= 8 - offset && offset + count <= (*pass.root)[parameter].constants_count)
pass.constant_words_bound[parameter] |= static_cast<unsigned char>(((1u << count) - 1) << offset);
}
// List slot 37 (desktop 33 + 4), SetComputeRoot32BitConstant(parameter, value, offset).
void __stdcall list_compute_constant(Wrapper* self, UINT parameter, UINT value, UINT offset)
{
store_root_constants(pass_of(self).compute_constants, "compute", parameter, 1, &value, offset);
}
// List slot 39 (desktop 35 + 4), SetComputeRoot32BitConstants(parameter, count, data, offset).
void __stdcall list_compute_constants(Wrapper* self, UINT parameter, UINT count, const std::uint32_t* data, UINT offset)
{
store_root_constants(pass_of(self).compute_constants, "compute", parameter, count, data, offset);
}
// List slot 47 (desktop 43 + 4), IASetIndexBuffer(view).
void __stdcall list_index_buffer(Wrapper* self, const D3D12_INDEX_BUFFER_VIEW* view)
{
Expand Down
8 changes: 8 additions & 0 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include "descriptor_heap_type.h"
#include "small_texture_4k.h"
#include "null_binding_cause.h"
#include "root_constant_store.h"
#include "null_texture_view.h"
#include "packed_target_format.h"
#include "present_view_format.h"
Expand Down Expand Up @@ -2364,6 +2365,8 @@ struct Pass {
u64 root_address[16] = {}; // title address of each root descriptor
std::uint32_t constants[16][8] = {};
unsigned char constant_words_bound[16] = {};
x2w_root_constants::Store compute_constants; // compute root constants (slots 37 and 39)
Wrapper* compute_root_wrapper = nullptr; // compute root signature last set by slot 33
D3D12_VERTEX_BUFFER_VIEW vb[8] = {};
PlacedVertexView placed_vb[8] = {};
D3D12_INDEX_BUFFER_VIEW ib = {};
Expand Down Expand Up @@ -2561,6 +2564,9 @@ void __stdcall list_compute_root(Wrapper* self, void* root)
if (!list) return;
Wrapper* w = as_wrapper(root);
Pass& pass = pass_of(self);
// A different root signature invalidates the title's cached compute root arguments.
if (pass.compute_root_wrapper != w) pass.compute_constants.clear();
pass.compute_root_wrapper = w;
const bool real = w && w->own().kind == root_signature_kind && w->own().real && !root_heap_flags(w);
pass.compute_root = real ? static_cast<ID3D12RootSignature*>(w->own().real) : nullptr;
pass.compute_tables.set_signature(w ? root_info(w) : nullptr); // a different signature leaves no table bound
Expand Down Expand Up @@ -6111,6 +6117,8 @@ void build_tables()
list_typed[13] = reinterpret_cast<ListTyped>(&list_draw_indexed);
list_typed[35] = reinterpret_cast<ListTyped>(&list_compute_table);
list_typed[36] = reinterpret_cast<ListTyped>(&list_table);
list_typed[37] = reinterpret_cast<ListTyped>(&list_compute_constant);
list_typed[39] = reinterpret_cast<ListTyped>(&list_compute_constants);
list_typed[40] = reinterpret_cast<ListTyped>(&list_root_constants);
list_typed[47] = reinterpret_cast<ListTyped>(&list_index_buffer);
list_typed[48] = reinterpret_cast<ListTyped>(&list_vertex_buffers);
Expand Down
64 changes: 64 additions & 0 deletions src/startup_stubs/root_constant_store.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Experimental (docs/startup-runtime.md, #870): the 32-bit compute root arguments a recorded command list keeps
// (list slots 37 and 39; the graphics slot 40 keeps its own words). Plain standard C++ with no Windows types, so it can be tested on its own.
//
// A list keeps 16 root parameters of 8 words each. A write that starts past those limits is dropped and one
// that runs past them is cut off at the end of the parameter, so the caller can log either case.
#pragma once

#include <cstddef>
#include <cstdint>

namespace x2w_root_constants {

inline constexpr unsigned parameter_count = 16;
inline constexpr unsigned words_per_parameter = 8;

enum class Result { stored, truncated, dropped };

struct Store {
std::uint32_t words[parameter_count][words_per_parameter] = {};
// One bit per word that a write stored, like the graphics path's `constant_words_bound`, so a later
// dispatch can tell "set to 0" from "never set".
std::uint8_t bound[parameter_count] = {};

void clear() noexcept
{
for (auto& row : words)
for (auto& word : row) word = 0;
for (auto& mask : bound) mask = 0;
}

// True when every word in [offset, offset + count) of `parameter` was written since the last clear.
bool is_bound(unsigned parameter, unsigned offset, unsigned count) const noexcept
{
if (!count || parameter >= parameter_count || offset >= words_per_parameter || count > words_per_parameter - offset)
return false;
const unsigned mask = ((1u << count) - 1) << offset;
return (bound[parameter] & mask) == mask;
}

// SetXxxRoot32BitConstants(parameter, count, data, offset). `data` must hold `count` words.
Result set(unsigned parameter, unsigned count, const std::uint32_t* data, unsigned offset) noexcept
{
if (!count || !data || parameter >= parameter_count || offset >= words_per_parameter) return Result::dropped;
const bool cut = count > words_per_parameter - offset;
const unsigned n = cut ? words_per_parameter - offset : count;
for (unsigned i = 0; i < n; ++i) words[parameter][offset + i] = data[i];
bound[parameter] |= static_cast<std::uint8_t>(((1u << n) - 1) << offset);
return cut ? Result::truncated : Result::stored;
}

// SetXxxRoot32BitConstant(parameter, value, offset).
Result set_one(unsigned parameter, std::uint32_t value, unsigned offset) noexcept { return set(parameter, 1, &value, offset); }

// The word a shader reads; 0 outside the kept range.
std::uint32_t word(int parameter, int offset) const noexcept
{
return parameter >= 0 && parameter < static_cast<int>(parameter_count) && offset >= 0 &&
offset < static_cast<int>(words_per_parameter)
? words[parameter][offset]
: 0;
}
};

} // namespace x2w_root_constants
62 changes: 62 additions & 0 deletions tests/startup_root_constant_store.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Experimental (docs/startup-runtime.md, #870): the root constants a command list keeps for compute
// (list slots 37 and 39). Plain standard C++; synthetic values, no title data.
#include "../src/startup_stubs/root_constant_store.h"

#include <cstdio>
#include <string>

namespace {
using x2w_root_constants::Result;
using x2w_root_constants::Store;
int failures;

void expect(bool ok, const std::string& what)
{
if (!ok) {
std::fprintf(stderr, "FAIL: %s\n", what.c_str());
++failures;
}
}
} // namespace

int main()
{
Store store;
const std::uint32_t data[4] = {11, 22, 33, 44};

expect(store.set(3, 4, data, 2) == Result::stored, "four words at offset 2 are stored");
expect(store.word(3, 2) == 11 && store.word(3, 5) == 44, "stored words read back at their offsets");
expect(store.word(3, 1) == 0 && store.word(3, 6) == 0, "neighbouring words stay zero");

expect(store.set_one(5, 77, 7) == Result::stored, "a single word at the last offset is stored");
expect(store.word(5, 7) == 77, "the single word reads back");

expect(store.set(6, 4, data, 6) == Result::truncated, "a write past the end of the parameter is cut off");
expect(store.word(6, 6) == 11 && store.word(6, 7) == 22, "the words that fit are kept");
expect(store.word(7, 0) == 0, "a cut-off write does not spill into the next parameter");

expect(store.set(16, 1, data, 0) == Result::dropped, "parameter 16 is refused");
expect(store.set(0, 1, data, 8) == Result::dropped, "offset 8 is refused");
expect(store.set(0, 0, data, 0) == Result::dropped, "an empty write is refused");
expect(store.set(0, 1, nullptr, 0) == Result::dropped, "a null source is refused");
expect(store.set_one(15, 5, 0) == Result::stored && store.word(15, 0) == 5, "parameter 15 is the last one kept");
expect(store.word(16, 0) == 0 && store.word(-1, 0) == 0 && store.word(0, 8) == 0, "reads outside the range give 0");

expect(store.is_bound(3, 2, 4) && store.is_bound(3, 3, 1), "written words are marked set");
expect(!store.is_bound(3, 1, 1) && !store.is_bound(3, 1, 2) && !store.is_bound(3, 6, 1),
"words never written are not set, and a range with one unset word is not set");
expect(store.is_bound(6, 6, 2) && !store.is_bound(6, 6, 3), "a cut-off write marks only the words that fit");
expect(!store.is_bound(0, 0, 1), "a dropped write marks nothing");
expect(!store.is_bound(16, 0, 1) && !store.is_bound(0, 8, 1) && !store.is_bound(0, 0, 0) && !store.is_bound(0, 7, 2),
"ranges outside the kept words are never set");
expect(store.set_one(1, 0, 0) == Result::stored && store.is_bound(1, 0, 1) && store.word(1, 0) == 0,
"writing 0 is set, unlike never written");

store.clear();
expect(store.word(3, 2) == 0 && store.word(15, 0) == 0, "clear zeroes every word");
expect(!store.is_bound(3, 2, 4) && !store.is_bound(1, 0, 1), "clear forgets which words were set");

if (failures) return 1;
std::puts("root constant store ok");
return 0;
}
Loading