Merge pull request #2522 from Tyrannys/rt-com-null-barriers-entry

d3d12, process: null barrier arrays, CopyTiles in a bundle, calibration wait, entry thread argument, getmainargs expand (RT-COM-NULL-BARRIERS-ENTRY)
This commit is contained in:
MoonEyes
2026-11-29 02:03:37 +01:00
committed by GitHub
5 changed files with 121 additions and 15 deletions

View File

@ -572,6 +572,7 @@ public:
void STDMETHODCALLTYPE ResourceBarrier(UINT count, const D3D12_RESOURCE_BARRIER* barriers) override {
d3d12_trace::record("ID3D12GraphicsCommandList::ResourceBarrier");
if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
if (count && !barriers) return check(false); // as WriteBufferImmediate: the list fails, nothing is read
for (UINT i = 0; i < count; ++i) {
const auto& b = barriers[i];
switch (b.Type) {
@ -608,7 +609,14 @@ public:
void STDMETHODCALLTYPE Barrier(UINT32 groups, const D3D12_BARRIER_GROUP* group) override {
d3d12_trace::record("ID3D12GraphicsCommandList7::Barrier");
if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
if (groups && !group) return check(false);
for (UINT32 g = 0; g < groups; ++g) {
// A group with barriers and no array fails the list; nothing is read. The three array pointers are one
// union, so any of them says whether there is an array.
if (group[g].NumBarriers && !group[g].pGlobalBarriers) {
check(false);
continue;
}
for (UINT32 i = 0; i < group[g].NumBarriers; ++i) {
switch (group[g].Type) {
case D3D12_BARRIER_TYPE_GLOBAL: {
@ -684,6 +692,7 @@ public:
const D3D12_TILE_REGION_SIZE* size, ID3D12Resource* linear,
UINT64 linear_offset, D3D12_TILE_COPY_FLAGS flags) override {
d3d12_trace::record("ID3D12GraphicsCommandList::CopyTiles");
if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); // no copy is allowed in a bundle
const auto direction = flags & ~D3D12_TILE_COPY_FLAG_NO_HAZARD;
const bool to_linear = direction == D3D12_TILE_COPY_FLAG_SWIZZLED_TILED_RESOURCE_TO_LINEAR_BUFFER;
const auto grid = tile_grid(tiled);
@ -697,7 +706,7 @@ public:
const auto buffer = linear->GetDesc();
const auto desc = tiled->GetDesc();
const auto count = size->UseBox ? std::uint64_t(size->Width) * size->Height : size->NumTiles;
if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE || buffer.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER ||
if (buffer.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER ||
linear_offset > buffer.Width || count > (buffer.Width - linear_offset) / leonida::tile_size_in_bytes)
return check(false);
constexpr auto shape = leonida::element_tile_shape(false, 4);
@ -2717,7 +2726,7 @@ private:
std::uint64_t stamp = 0;
if (context_.observe_result(stamp_.get_query_pool_results(context_.device(), stamp_.queries, 0, 1,
sizeof(stamp), &stamp, sizeof(stamp),
VK_QUERY_RESULT_64_BIT),
VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT),
"vkGetQueryPoolResults (calibration)") != VK_SUCCESS)
return E_FAIL;
if (clock_.timestamp_valid_bits < 64) stamp &= (std::uint64_t(1) << clock_.timestamp_valid_bits) - 1;

View File

@ -489,30 +489,30 @@ wchar_t** current_wide_environment() {
return _wenviron;
}
// msvcrt.dll: int __getmainargs(int* argc, char*** argv, char*** env, int expand, void* startinfo)
int __cdecl game_getmainargs(int* argc, char*** argv, char*** env, int, void*) {
if (const int error = game_configure_narrow_argv(1)) return error;
// msvcrt.dll: int __getmainargs(int* argc, char*** argv, char*** env, int expand, void* startinfo). A nonzero
// `expand` (the CRT's _dowildcard) expands wildcards in the arguments, as _configure_narrow_argv's mode 2 does.
int __cdecl game_getmainargs(int* argc, char*** argv, char*** env, int expand, void*) {
if (!argc || !argv) return -1;
if (const int error = game_configure_narrow_argv(expand ? 2 : 1)) return error;
*argc = g_argc;
*argv = g_argv_ptr;
if (env) *env = current_environment();
return 0;
}
int __cdecl game_wgetmainargs(int* argc, wchar_t*** argv, wchar_t*** env, int, void*) {
if (const int error = game_configure_wide_argv(1)) return error;
int __cdecl game_wgetmainargs(int* argc, wchar_t*** argv, wchar_t*** env, int expand, void*) {
if (!argc || !argv) return -1;
if (const int error = game_configure_wide_argv(expand ? 2 : 1)) return error;
*argc = g_argc;
*argv = g_wargv_ptr;
if (env) *env = current_wide_environment();
return 0;
}
struct EntryCall {
void* entry;
};
// `p` is the entry point itself: run_entry can return (another thread ended the run) before this thread first runs,
// so nothing of run_entry's frame may be read here.
DWORD WINAPI game_thread(void* p) {
auto* call = static_cast<EntryCall*>(p);
// An EXE entry point (mainCRTStartup and friends) takes the PEB pointer and returns the process exit code.
auto entry = reinterpret_cast<DWORD(WINAPI*)(void*)>(call->entry);
auto entry = reinterpret_cast<DWORD(WINAPI*)(void*)>(p);
const DWORD code = entry(nullptr);
end_run(static_cast<int>(code)); // returning from the entry point is an exit too
}
@ -554,8 +554,7 @@ int run_entry(void* entry, size_t stack_bytes) {
g_done = CreateEventW(nullptr, TRUE, FALSE, nullptr);
if (!g_done) return -1;
g_running = true;
EntryCall call{entry};
HANDLE thread = CreateThread(nullptr, stack_bytes, game_thread, &call, STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr);
HANDLE thread = CreateThread(nullptr, stack_bytes, game_thread, entry, STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr);
if (!thread) {
g_running = false;
CloseHandle(g_done);

View File

@ -113,6 +113,18 @@ bool run(ID3D12Device& device, Results& result, bool layer = false) {
const D3D12_TILE_REGION_SIZE all{tiles_per_buffer, FALSE, 0, 0, 0};
queue->CopyTileMappings(b.Get(), &origin, a.Get(), &origin, &all, D3D12_TILE_MAPPING_FLAG_NONE);
EXPECT(SUCCEEDED(device.GetDeviceRemovedReason()));
if (layer) {
// No copy is allowed in a bundle: CopyTiles of a reserved buffer fails the bundle's Close, as it does for a
// sparse image and as CopyBufferRegion does (the same call is valid on the direct list below).
ComPtr<ID3D12CommandAllocator> bundle_allocator;
ComPtr<ID3D12GraphicsCommandList> bundle;
EXPECT(SUCCEEDED(device.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_BUNDLE, IID_PPV_ARGS(&bundle_allocator))));
EXPECT(SUCCEEDED(device.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_BUNDLE, bundle_allocator.Get(), nullptr,
IID_PPV_ARGS(&bundle))));
bundle->CopyTiles(a.Get(), &origin, &all, upload.Get(), tile_bytes,
D3D12_TILE_COPY_FLAG_LINEAR_BUFFER_TO_SWIZZLED_TILED_RESOURCE);
EXPECT(bundle->Close() == E_INVALIDARG);
}
lists[0]->CopyTiles(a.Get(), &origin, &all, upload.Get(), tile_bytes,
D3D12_TILE_COPY_FLAG_LINEAR_BUFFER_TO_SWIZZLED_TILED_RESOURCE);
D3D12_RESOURCE_BARRIER alias{};

View File

@ -337,6 +337,38 @@ int main(int argc, char** argv) {
const D3D12_WRITEBUFFERIMMEDIATE_PARAMETER write{0x1000, 1};
b->WriteBufferImmediate(1, &write, nullptr); }));
// A barrier call with a count and no array fails a direct list's Close instead of reading through null, as
// WriteBufferImmediate does; a count of 0 needs no array.
auto invalid_direct_call = [&](auto record) {
Ref<ID3D12CommandAllocator> test_allocator;
Ref<ID3D12GraphicsCommandList8> test_list;
if (FAILED(rt::d3d12::create_command_allocator(D3D12_COMMAND_LIST_TYPE_DIRECT,
IID_PPV_ARGS(&test_allocator.p))))
return false;
if (FAILED(rt::d3d12::create_command_list(D3D12_COMMAND_LIST_TYPE_DIRECT, test_allocator.p,
IID_PPV_ARGS(&test_list.p))))
return false;
record(test_list.p);
return test_list->Close() == E_INVALIDARG;
};
EXPECT(invalid_direct_call([&](auto* l) { l->ResourceBarrier(1, nullptr); }));
EXPECT(invalid_direct_call([&](auto* l) { l->Barrier(1, nullptr); }));
for (const auto type : {D3D12_BARRIER_TYPE_GLOBAL, D3D12_BARRIER_TYPE_TEXTURE, D3D12_BARRIER_TYPE_BUFFER}) {
D3D12_BARRIER_GROUP no_array{};
no_array.Type = type;
no_array.NumBarriers = 1;
EXPECT(invalid_direct_call([&](auto* l) { l->Barrier(1, &no_array); }));
// The same group after a valid one: the valid one is recorded, the list still fails.
const D3D12_BARRIER_GROUP both[] = {group, no_array};
EXPECT(invalid_direct_call([&](auto* l) { l->Barrier(2, both); }));
}
EXPECT(!invalid_direct_call([&](auto* l) { l->ResourceBarrier(0, nullptr); }));
EXPECT(!invalid_direct_call([&](auto* l) { l->Barrier(0, nullptr); }));
D3D12_BARRIER_GROUP empty_group{};
empty_group.Type = D3D12_BARRIER_TYPE_TEXTURE; // no barriers, no array
EXPECT(!invalid_direct_call([&](auto* l) { l->Barrier(1, &empty_group); }));
EXPECT(!invalid_direct_call([&](auto* l) { l->ResourceBarrier(1, &transition); })); // the control
list->Barrier(1, &group);
// Not implemented: reported once by its generated stub, recording continues.
list->SetProtectedResourceSession(nullptr);

View File

@ -218,6 +218,59 @@ void check_getmainargs_environment() {
++failures;
}
}
// __getmainargs/__wgetmainargs's `expand` (the CRT's _dowildcard): nonzero expands wildcards in the arguments, as
// _configure_*_argv's mode 2 does; zero leaves them as typed. A null argc or argv fails instead of being written
// through. Synthetic files in a temporary folder.
void check_getmainargs_expand() {
using Narrow = int(__cdecl*)(int*, char***, char***, int, void*);
using Wide = int(__cdecl*)(int*, wchar_t***, wchar_t***, int, void*);
const auto narrow_fn = reinterpret_cast<Narrow>(rt::shims::find("msvcrt.dll", "__getmainargs"));
const auto wide_fn = reinterpret_cast<Wide>(rt::shims::find("msvcrt.dll", "__wgetmainargs"));
if (!narrow_fn || !wide_fn) return; // check_getmainargs_environment reports it
wchar_t temp[MAX_PATH]{};
if (!GetTempPathW(MAX_PATH, temp)) {
std::fprintf(stderr, "FAIL GetTempPathW\n");
++failures;
return;
}
const std::wstring dir = std::wstring(temp) + L"leonida_getmainargs_" + std::to_wstring(GetCurrentProcessId());
CreateDirectoryW(dir.c_str(), nullptr);
const std::wstring files[] = {dir + L"\\a1.lx", dir + L"\\a2.lx"};
for (const auto& f : files) {
const HANDLE h = CreateFileW(f.c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (h != INVALID_HANDLE_VALUE) CloseHandle(h);
}
const std::wstring pattern = dir + L"\\*.lx";
rt::process::set_command_line(L"game.exe", {pattern});
for (const int expand : {0, 1}) {
const int want = expand ? 3 : 2; // game.exe and the two files, or game.exe and the pattern
int argc = 0;
char** argv = nullptr;
if (narrow_fn(&argc, &argv, nullptr, expand, nullptr) != 0 || argc != want || !argv ||
(!expand && std::strchr(argv[1], '*') == nullptr) || (expand && std::strchr(argv[1], '*') != nullptr)) {
std::fprintf(stderr, "FAIL __getmainargs(expand=%d): argc %d, want %d\n", expand, argc, want);
++failures;
}
int wargc = 0;
wchar_t** wargv = nullptr;
if (wide_fn(&wargc, &wargv, nullptr, expand, nullptr) != 0 || wargc != want || !wargv ||
(!expand && std::wcschr(wargv[1], L'*') == nullptr) || (expand && std::wcschr(wargv[1], L'*') != nullptr)) {
std::fprintf(stderr, "FAIL __wgetmainargs(expand=%d): argc %d, want %d\n", expand, wargc, want);
++failures;
}
}
int argc = 0;
char** argv = nullptr;
wchar_t** wargv = nullptr;
if (narrow_fn(nullptr, &argv, nullptr, 0, nullptr) == 0 || narrow_fn(&argc, nullptr, nullptr, 0, nullptr) == 0 ||
wide_fn(nullptr, &wargv, nullptr, 0, nullptr) == 0 || wide_fn(&argc, nullptr, nullptr, 0, nullptr) == 0) {
std::fprintf(stderr, "FAIL __getmainargs/__wgetmainargs accepted a null argc or argv\n");
++failures;
}
for (const auto& f : files) DeleteFileW(f.c_str());
RemoveDirectoryW(dir.c_str());
}
} // namespace
int main() {
@ -234,6 +287,7 @@ int main() {
check_import_names();
check_argv_reset();
check_getmainargs_environment();
check_getmainargs_expand();
check_winmain(L"game.exe", {L"-foo", L"with space"}, L"-foo \"with space\"", "arguments after a plain name");
check_winmain(L"C:\\My Games\\game.exe", {L"-x"}, L"-x", "a quoted program name with a space");
check_winmain(L"game.exe", {}, L"", "no arguments");