mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 23:38:37 +00:00
- CreateGraphicsPipelineState (PC slot 10): VS and PS found by the SHA-256 of their FSSM programs in the translator's index. Input-layout elements are matched to the VS input signature by semantic name and index; location = the element's register (the translator's vertex-fetch rule); a layout that leaves an input unfed is refused, as D3D12 does. Rasterizer, blend and depth-stencil state map one to one; viewport, scissor, topology, vertex strides, stencil references and blend factor are dynamic; dynamic rendering takes the RTV/DSV formats. Hull/domain/geometry, stream output, MSAA, set-0 bindings and two code-base stages (question 14) are refused with a log. - Command list: IASetPrimitiveTopology / VertexBuffers / IndexBuffer, RSSetViewports / ScissorRects (first of each), OMSetRenderTargets (views read at the call, single-range handles too), OMSetBlendFactor, OMSetStencilRef and CommandList8's front/back references (now applied), DrawInstanced / DrawIndexedInstanced recording a copy of that state. CreateCommandList's initial pipeline state is applied. Reset and ClearState share one state reset. - Replay: vertex/index views resolve to buffers by VA (arena or upload), attachments move to attachment layouts through the tracker (first use zero-fills), the draw runs in a dynamic-rendering scope with a flipped viewport (translated VS write D3D clip coordinates), stencil references per face, then vkCmdDraw / vkCmdDrawIndexed. Render-target views take the pipeline's formats (the view cache keys on format). - shader_library.h reads the index's input_signature and PS outputs. Test: d3d12_draw_test (through the proxy) builds a VS+PS PSO whose input layout order differs from the VS registers, uploads a vertex buffer into the arena, reads indices from an upload heap, and draws two indexed quads with different root constants and root CBVs plus a back-facing triangle: all 4096 read-back pixels match (which also checks the viewport flip and winding). Shaders are GLSL written to the translator's contract. d3d12_com_test now uses SetMarker as its unimplemented-method example, since DrawInstanced is implemented. 13/13 ctest; all six GPU tests clean under the Khronos validation layer.
125 lines
4.9 KiB
C++
125 lines
4.9 KiB
C++
// Helpers for the D3D12 pipeline tests: wrap a synthetic FSSM program in a shader container shaped like the game's
|
|
// (DXBC { DXIL { stub, XDAT { 0x0D: XBCH + zlib(fssm) } } }, the layout tools/fssm_unpack.py reads), serialize a
|
|
// root signature with the host PC runtime, and write a translator-style index next to compiled test modules.
|
|
// No game data: the programs are a few marker words, and the modules are GLSL written to the translator's contract.
|
|
#pragma once
|
|
|
|
#include <windows.h>
|
|
#include <d3d12.h>
|
|
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace d3d12test {
|
|
|
|
inline void put32(std::vector<std::uint8_t>& out, std::uint32_t v) {
|
|
for (int i = 0; i < 4; ++i) out.push_back(std::uint8_t(v >> (8 * i)));
|
|
}
|
|
|
|
// zlib with one stored block (RFC 1950/1951), enough to wrap a test program.
|
|
inline std::vector<std::uint8_t> zlib_stored(const std::vector<std::uint8_t>& data) {
|
|
std::vector<std::uint8_t> out = {0x78, 0x01, 0x01};
|
|
const auto length = std::uint16_t(data.size());
|
|
out.insert(out.end(), {std::uint8_t(length), std::uint8_t(length >> 8), std::uint8_t(~length),
|
|
std::uint8_t(~length >> 8)});
|
|
out.insert(out.end(), data.begin(), data.end());
|
|
std::uint32_t a = 1, b = 0;
|
|
for (const std::uint8_t x : data) {
|
|
a = (a + x) % 65521;
|
|
b = (b + a) % 65521;
|
|
}
|
|
const std::uint32_t adler = (b << 16) | a;
|
|
out.insert(out.end(), {std::uint8_t(adler >> 24), std::uint8_t(adler >> 16), std::uint8_t(adler >> 8),
|
|
std::uint8_t(adler)});
|
|
return out;
|
|
}
|
|
|
|
// A synthetic FSSM program: the magic, then `words` (distinct per test shader so their hashes differ).
|
|
inline std::vector<std::uint8_t> fssm_program(std::initializer_list<std::uint32_t> words) {
|
|
std::vector<std::uint8_t> out = {'F', 'S', 'S', 'M'};
|
|
for (const std::uint32_t w : words) put32(out, w);
|
|
return out;
|
|
}
|
|
|
|
inline std::vector<std::uint8_t> shader_container(const std::vector<std::uint8_t>& fssm) {
|
|
const auto stream = zlib_stored(fssm);
|
|
std::vector<std::uint8_t> xbch = {'X', 'B', 'C', 'H'};
|
|
for (const std::uint32_t v : {1u, std::uint32_t(stream.size()), 0u, std::uint32_t(fssm.size()), 0u}) put32(xbch, v);
|
|
xbch.insert(xbch.end(), stream.begin(), stream.end());
|
|
std::vector<std::uint8_t> dxil(16, 0);
|
|
dxil.insert(dxil.end(), {'X', 'D', 'A', 'T'});
|
|
put32(dxil, 1);
|
|
put32(dxil, 12);
|
|
put32(dxil, 0x0d);
|
|
put32(dxil, 16 + 12 + 12);
|
|
put32(dxil, std::uint32_t(xbch.size()));
|
|
dxil.insert(dxil.end(), xbch.begin(), xbch.end());
|
|
std::vector<std::uint8_t> out = {'D', 'X', 'B', 'C'};
|
|
out.resize(28, 0);
|
|
put32(out, 1);
|
|
put32(out, 36);
|
|
out.insert(out.end(), {'D', 'X', 'I', 'L'});
|
|
put32(out, std::uint32_t(dxil.size()));
|
|
out.insert(out.end(), dxil.begin(), dxil.end());
|
|
return out;
|
|
}
|
|
|
|
using SerializeFn = HRESULT(WINAPI*)(const D3D12_VERSIONED_ROOT_SIGNATURE_DESC*, ID3DBlob**, ID3DBlob**);
|
|
|
|
inline std::vector<std::uint8_t> serialize_root_signature(const D3D12_VERSIONED_ROOT_SIGNATURE_DESC& desc) {
|
|
const HMODULE d3d12 = LoadLibraryExA("d3d12.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
|
|
const auto fn = d3d12 ? reinterpret_cast<SerializeFn>(
|
|
reinterpret_cast<void*>(GetProcAddress(d3d12, "D3D12SerializeVersionedRootSignature")))
|
|
: nullptr;
|
|
std::vector<std::uint8_t> out;
|
|
ID3DBlob* blob = nullptr;
|
|
if (fn && SUCCEEDED(fn(&desc, &blob, nullptr)) && blob) {
|
|
const auto* p = static_cast<const std::uint8_t*>(blob->GetBufferPointer());
|
|
out.assign(p, p + blob->GetBufferSize());
|
|
blob->Release();
|
|
}
|
|
return out;
|
|
}
|
|
|
|
inline D3D12_RESOURCE_DESC buffer_desc(UINT64 bytes, D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE) {
|
|
D3D12_RESOURCE_DESC desc{};
|
|
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
|
|
desc.Width = bytes;
|
|
desc.Height = 1;
|
|
desc.DepthOrArraySize = 1;
|
|
desc.MipLevels = 1;
|
|
desc.SampleDesc.Count = 1;
|
|
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
|
|
desc.Flags = flags;
|
|
return desc;
|
|
}
|
|
|
|
inline D3D12_RESOURCE_BARRIER transition(ID3D12Resource* resource, D3D12_RESOURCE_STATES before,
|
|
D3D12_RESOURCE_STATES after) {
|
|
D3D12_RESOURCE_BARRIER barrier{};
|
|
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
|
|
barrier.Transition.pResource = resource;
|
|
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
|
|
barrier.Transition.StateBefore = before;
|
|
barrier.Transition.StateAfter = after;
|
|
return barrier;
|
|
}
|
|
|
|
template<class T>
|
|
struct Ref {
|
|
T* p = nullptr;
|
|
~Ref() { reset(); }
|
|
void reset() {
|
|
if (p) p->Release();
|
|
p = nullptr;
|
|
}
|
|
T* operator->() const { return p; }
|
|
};
|
|
|
|
inline void* slot(void* object, unsigned index) { return (*reinterpret_cast<void***>(object))[index]; }
|
|
|
|
} // namespace d3d12test
|