diff options
| author | Alex Arenas <106154424+al3xar@users.noreply.github.com> | 2026-11-22 15:26:27 +0100 |
|---|---|---|
| committer | Alex Arenas <106154424+al3xar@users.noreply.github.com> | 2026-11-22 15:26:27 +0100 |
| commit | d38daa4e9bf1a9abe7aa81028e75cf4b0e1388e4 (patch) | |
| tree | 723410e6e5a01d0ba9506fc69541ff3b55c6b12d /runtime | |
| parent | 003e78129303643a3fdc9e5f0e31a01b09759236 (diff) | |
runtime: implement ID3D12Device1::CreatePipelineLibrary and ID3D12PipelineLibrary1
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/CMakeLists.txt | 12 | ||||
| -rw-r--r-- | runtime/shims/d3d12_vulkan_device.cpp | 106 | ||||
| -rw-r--r-- | runtime/test/d3d12_pipeline_library_test.cpp | 316 |
3 files changed, 433 insertions, 1 deletions
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 6f135558a..ef11fc745 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt | |||
| @@ -819,7 +819,7 @@ if(TARGET Vulkan::Headers) | |||
| 819 | ${_stubs_header} | 819 | ${_stubs_header} |
| 820 | ID3D12GraphicsCommandList8 ID3D12CommandQueue ID3D12CommandAllocator ID3D12Fence1 | 820 | ID3D12GraphicsCommandList8 ID3D12CommandQueue ID3D12CommandAllocator ID3D12Fence1 |
| 821 | ID3D12Device11 ID3D12DescriptorHeap ID3D12Heap ID3D12Resource ID3D12QueryHeap ID3D12RootSignature | 821 | ID3D12Device11 ID3D12DescriptorHeap ID3D12Heap ID3D12Resource ID3D12QueryHeap ID3D12RootSignature |
| 822 | ID3D12PipelineState ID3D12CommandSignature | 822 | ID3D12PipelineState ID3D12CommandSignature ID3D12PipelineLibrary1 |
| 823 | DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../tools/gen_d3d12_stubs.py ${LEONIDA_D3D12_HEADER} | 823 | DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../tools/gen_d3d12_stubs.py ${LEONIDA_D3D12_HEADER} |
| 824 | COMMENT "Generating D3D12 COM stubs" | 824 | COMMENT "Generating D3D12 COM stubs" |
| 825 | VERBATIM) | 825 | VERBATIM) |
| @@ -1332,6 +1332,16 @@ if(TARGET Vulkan::Headers) | |||
| 1332 | target_compile_options(d3d12_set0_compute_test PRIVATE /W4 /permissive- /utf-8) | 1332 | target_compile_options(d3d12_set0_compute_test PRIVATE /W4 /permissive- /utf-8) |
| 1333 | add_test(NAME d3d12_set0_compute COMMAND d3d12_set0_compute_test) | 1333 | add_test(NAME d3d12_set0_compute COMMAND d3d12_set0_compute_test) |
| 1334 | set_tests_properties(d3d12_set0_compute PROPERTIES SKIP_RETURN_CODE 77) | 1334 | set_tests_properties(d3d12_set0_compute PROPERTIES SKIP_RETURN_CODE 77) |
| 1335 | add_executable(d3d12_pipeline_library_test test/d3d12_pipeline_library_test.cpp | ||
| 1336 | ${LEONIDA_TEST_CS} ${LEONIDA_TEST_VS} ${LEONIDA_TEST_PS}) | ||
| 1337 | target_link_libraries(d3d12_pipeline_library_test PRIVATE gta6rt_d3d12_com) | ||
| 1338 | target_compile_definitions(d3d12_pipeline_library_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX | ||
| 1339 | LEONIDA_TEST_CS_SPV="${LEONIDA_TEST_CS}" | ||
| 1340 | LEONIDA_TEST_VS_SPV="${LEONIDA_TEST_VS}" | ||
| 1341 | LEONIDA_TEST_PS_SPV="${LEONIDA_TEST_PS}") | ||
| 1342 | target_compile_options(d3d12_pipeline_library_test PRIVATE /W4 /permissive- /utf-8) | ||
| 1343 | add_test(NAME d3d12_pipeline_library COMMAND d3d12_pipeline_library_test) | ||
| 1344 | set_tests_properties(d3d12_pipeline_library PROPERTIES SKIP_RETURN_CODE 77) | ||
| 1335 | else() | 1345 | else() |
| 1336 | message(STATUS "glslangValidator not found: d3d12_compute_test, d3d12_draw_test and d3d12_set0_compute_test are not built") | 1346 | message(STATUS "glslangValidator not found: d3d12_compute_test, d3d12_draw_test and d3d12_set0_compute_test are not built") |
| 1337 | endif() | 1347 | endif() |
diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 8d0cd21e4..fd43cda8f 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp | |||
| @@ -2597,6 +2597,99 @@ struct TextureView { | |||
| 2597 | float min_lod = 0.0f; | 2597 | float min_lod = 0.0f; |
| 2598 | }; | 2598 | }; |
| 2599 | 2599 | ||
| 2600 | // In-memory pipeline library (docs/d3d12_vulkan.md sections 3.6 and 4). D3D12 cached blobs are driver-specific | ||
| 2601 | // and ignored across runs; Serialize writes a versioned header so CreatePipelineLibrary recognizes our own blobs | ||
| 2602 | // and returns D3D12_ERROR_DRIVER_VERSION_MISMATCH for foreign or corrupt ones. | ||
| 2603 | class PipelineLibrary final | ||
| 2604 | : public Child<PipelineLibrary, ID3D12PipelineLibrary1Stubs, ID3D12Object, ID3D12DeviceChild, ID3D12Pageable, | ||
| 2605 | ID3D12PipelineLibrary, ID3D12PipelineLibrary1> { | ||
| 2606 | public: | ||
| 2607 | // {6e2a9d41-7b5f-4c83-8e14-2a9d6f3c5b10} | ||
| 2608 | static constexpr GUID kTag{0x6e2a9d41, 0x7b5f, 0x4c83, {0x8e, 0x14, 0x2a, 0x9d, 0x6f, 0x3c, 0x5b, 0x10}}; | ||
| 2609 | |||
| 2610 | struct Header { | ||
| 2611 | std::uint32_t magic; | ||
| 2612 | std::uint32_t version; | ||
| 2613 | std::uint32_t count; | ||
| 2614 | std::uint32_t reserved; | ||
| 2615 | }; | ||
| 2616 | static constexpr std::uint32_t kMagic = 0x314C504Cu; // "LPL1" | ||
| 2617 | static constexpr std::uint32_t kVersion = 1; | ||
| 2618 | |||
| 2619 | ~PipelineLibrary() override { | ||
| 2620 | std::lock_guard lock(mutex_); | ||
| 2621 | for (auto& [name, pso] : entries_) | ||
| 2622 | if (pso) pso->Release(); | ||
| 2623 | entries_.clear(); | ||
| 2624 | } | ||
| 2625 | |||
| 2626 | static bool valid_blob(const void* data, SIZE_T size) { | ||
| 2627 | if (!data || size < sizeof(Header)) return false; | ||
| 2628 | Header h{}; | ||
| 2629 | std::memcpy(&h, data, sizeof(h)); | ||
| 2630 | return h.magic == kMagic && h.version == kVersion && h.reserved == 0; | ||
| 2631 | } | ||
| 2632 | |||
| 2633 | HRESULT STDMETHODCALLTYPE StorePipeline(LPCWSTR name, ID3D12PipelineState* pipeline) override { | ||
| 2634 | if (!name || !*name || !pipeline) return E_INVALIDARG; | ||
| 2635 | if (!PipelineState::from<PipelineState>(pipeline)) return E_INVALIDARG; | ||
| 2636 | std::lock_guard lock(mutex_); | ||
| 2637 | auto [it, inserted] = entries_.emplace(name, pipeline); | ||
| 2638 | if (!inserted) return E_INVALIDARG; | ||
| 2639 | pipeline->AddRef(); | ||
| 2640 | return S_OK; | ||
| 2641 | } | ||
| 2642 | |||
| 2643 | HRESULT STDMETHODCALLTYPE LoadGraphicsPipeline(LPCWSTR name, const D3D12_GRAPHICS_PIPELINE_STATE_DESC* desc, | ||
| 2644 | REFIID riid, void** out) override { | ||
| 2645 | if (!out) return E_POINTER; | ||
| 2646 | *out = nullptr; | ||
| 2647 | if (!name || !*name || !desc) return E_INVALIDARG; | ||
| 2648 | return load_entry(name, false, true, riid, out); | ||
| 2649 | } | ||
| 2650 | |||
| 2651 | HRESULT STDMETHODCALLTYPE LoadComputePipeline(LPCWSTR name, const D3D12_COMPUTE_PIPELINE_STATE_DESC* desc, | ||
| 2652 | REFIID riid, void** out) override { | ||
| 2653 | if (!out) return E_POINTER; | ||
| 2654 | *out = nullptr; | ||
| 2655 | if (!name || !*name || !desc) return E_INVALIDARG; | ||
| 2656 | return load_entry(name, true, false, riid, out); | ||
| 2657 | } | ||
| 2658 | |||
| 2659 | HRESULT STDMETHODCALLTYPE LoadPipeline(LPCWSTR name, const D3D12_PIPELINE_STATE_STREAM_DESC* desc, | ||
| 2660 | REFIID riid, void** out) override { | ||
| 2661 | if (!out) return E_POINTER; | ||
| 2662 | *out = nullptr; | ||
| 2663 | if (!name || !*name || !desc || !desc->SizeInBytes || !desc->pPipelineStateSubobjectStream) return E_INVALIDARG; | ||
| 2664 | return load_entry(name, false, false, riid, out); | ||
| 2665 | } | ||
| 2666 | |||
| 2667 | SIZE_T STDMETHODCALLTYPE GetSerializedSize() override { return sizeof(Header); } | ||
| 2668 | |||
| 2669 | HRESULT STDMETHODCALLTYPE Serialize(void* data, SIZE_T size) override { | ||
| 2670 | if (!data || size < sizeof(Header)) return E_INVALIDARG; | ||
| 2671 | std::lock_guard lock(mutex_); | ||
| 2672 | const Header h{kMagic, kVersion, static_cast<std::uint32_t>(entries_.size()), 0}; | ||
| 2673 | std::memcpy(data, &h, sizeof(h)); | ||
| 2674 | return S_OK; | ||
| 2675 | } | ||
| 2676 | |||
| 2677 | private: | ||
| 2678 | HRESULT load_entry(LPCWSTR name, bool require_compute, bool require_graphics, REFIID riid, void** out) { | ||
| 2679 | std::lock_guard lock(mutex_); | ||
| 2680 | const auto it = entries_.find(name); | ||
| 2681 | if (it == entries_.end()) return E_INVALIDARG; | ||
| 2682 | PipelineState* ps = PipelineState::from<PipelineState>(it->second); | ||
| 2683 | if (!ps) return E_INVALIDARG; | ||
| 2684 | if (require_compute && !ps->compute()) return E_INVALIDARG; | ||
| 2685 | if (require_graphics && !ps->graphics()) return E_INVALIDARG; | ||
| 2686 | return it->second->QueryInterface(riid, out); | ||
| 2687 | } | ||
| 2688 | |||
| 2689 | std::mutex mutex_; | ||
| 2690 | std::unordered_map<std::wstring, ID3D12PipelineState*> entries_; | ||
| 2691 | }; | ||
| 2692 | |||
| 2600 | // The T# of a view: what the shader gets to know about it, in the structure the console's shaders read. | 2693 | // The T# of a view: what the shader gets to know about it, in the structure the console's shaders read. |
| 2601 | // False (with a log) for a view set 0 cannot show yet. | 2694 | // False (with a log) for a view set 0 cannot show yet. |
| 2602 | bool image_descriptor(const Resource& resource, const TextureView& view, leonida::gfx10::Image& out) { | 2695 | bool image_descriptor(const Resource& resource, const TextureView& view, leonida::gfx10::Image& out) { |
| @@ -3686,6 +3779,19 @@ public: | |||
| 3686 | return hand_out(pipeline, riid, out); | 3779 | return hand_out(pipeline, riid, out); |
| 3687 | } | 3780 | } |
| 3688 | 3781 | ||
| 3782 | // Device1: in-memory pipeline library (docs/d3d12_vulkan.md sections 3.6 and 4). | ||
| 3783 | HRESULT STDMETHODCALLTYPE CreatePipelineLibrary(const void* blob, SIZE_T size, REFIID riid, void** out) override { | ||
| 3784 | constexpr auto method = "ID3D12Device1::CreatePipelineLibrary"; | ||
| 3785 | if (out) *out = nullptr; | ||
| 3786 | if (size > 0 && !blob) return d3d12_trace::result(method, E_INVALIDARG); | ||
| 3787 | if (size > 0 && !PipelineLibrary::valid_blob(blob, size)) | ||
| 3788 | return d3d12_trace::result(method, D3D12_ERROR_DRIVER_VERSION_MISMATCH); | ||
| 3789 | if (!out) return d3d12_trace::result(method, S_FALSE); | ||
| 3790 | auto* lib = new PipelineLibrary(); | ||
| 3791 | lib->attach_device(identity()); | ||
| 3792 | return d3d12_trace::result(method, hand_out(lib, riid, out)); | ||
| 3793 | } | ||
| 3794 | |||
| 3689 | UINT STDMETHODCALLTYPE GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE) override { | 3795 | UINT STDMETHODCALLTYPE GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE) override { |
| 3690 | return kDescriptorSize; | 3796 | return kDescriptorSize; |
| 3691 | } | 3797 | } |
diff --git a/runtime/test/d3d12_pipeline_library_test.cpp b/runtime/test/d3d12_pipeline_library_test.cpp new file mode 100644 index 000000000..cd7b411e5 --- /dev/null +++ b/runtime/test/d3d12_pipeline_library_test.cpp | |||
| @@ -0,0 +1,316 @@ | |||
| 1 | // Synthetic GPU test for ID3D12Device1::CreatePipelineLibrary and ID3D12PipelineLibrary / ID3D12PipelineLibrary1: | ||
| 2 | // - empty library creation (0-byte blob), null-output capability check (S_FALSE), and invalid/foreign/corrupt | ||
| 3 | // blob rejection (E_INVALIDARG for null pointer with non-zero size, D3D12_ERROR_DRIVER_VERSION_MISMATCH for | ||
| 4 | // unrecognized or corrupt headers); | ||
| 5 | // - StorePipeline (unique names, duplicate rejection, null/empty name rejection, null/foreign PSO rejection); | ||
| 6 | // - LoadComputePipeline, LoadGraphicsPipeline, and ID3D12PipelineLibrary1::LoadPipeline (including cross-type | ||
| 7 | // mismatch rejection when a compute PSO is requested through LoadGraphicsPipeline or vice versa); | ||
| 8 | // - GetSerializedSize + Serialize round-trip into a second CreatePipelineLibrary; | ||
| 9 | // - GPU execution of both loaded compute and loaded graphics PSOs under LEONIDA_VK_VALIDATION=1. | ||
| 10 | // No game data. | ||
| 11 | #include "vk_test_device.h" | ||
| 12 | #include "d3d12_test_shaders.h" | ||
| 13 | #include "../shims/d3d12_com.h" | ||
| 14 | #include "../src/shader_library.h" | ||
| 15 | |||
| 16 | #include <cstring> | ||
| 17 | #include <filesystem> | ||
| 18 | #include <fstream> | ||
| 19 | #include <vector> | ||
| 20 | |||
| 21 | #if !defined(LEONIDA_TEST_CS_SPV) || !defined(LEONIDA_TEST_VS_SPV) || !defined(LEONIDA_TEST_PS_SPV) | ||
| 22 | #error "LEONIDA_TEST_CS_SPV, LEONIDA_TEST_VS_SPV and LEONIDA_TEST_PS_SPV must be defined" | ||
| 23 | #endif | ||
| 24 | |||
| 25 | #define EXPECT(condition) do { \ | ||
| 26 | if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \ | ||
| 27 | } while (0) | ||
| 28 | |||
| 29 | using namespace d3d12test; | ||
| 30 | |||
| 31 | namespace { | ||
| 32 | |||
| 33 | struct Vertex { | ||
| 34 | float x, y; | ||
| 35 | std::uint8_t r, g, b, a; | ||
| 36 | }; | ||
| 37 | |||
| 38 | D3D12_RESOURCE_DESC tex2d_desc(UINT w, UINT h, DXGI_FORMAT fmt, D3D12_RESOURCE_FLAGS flags) { | ||
| 39 | D3D12_RESOURCE_DESC d{}; | ||
| 40 | d.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; | ||
| 41 | d.Width = w; | ||
| 42 | d.Height = h; | ||
| 43 | d.DepthOrArraySize = 1; | ||
| 44 | d.MipLevels = 1; | ||
| 45 | d.Format = fmt; | ||
| 46 | d.SampleDesc = {1, 0}; | ||
| 47 | d.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; | ||
| 48 | d.Flags = flags; | ||
| 49 | return d; | ||
| 50 | } | ||
| 51 | |||
| 52 | } // namespace | ||
| 53 | |||
| 54 | int main() { | ||
| 55 | vktest::Device d; | ||
| 56 | const auto status = d.initialize(); | ||
| 57 | if (status == vktest::DeviceStatus::unavailable) { | ||
| 58 | std::fprintf(stderr, "SKIP: no usable Vulkan 1.3 device\n"); | ||
| 59 | return 77; | ||
| 60 | } | ||
| 61 | EXPECT(status == vktest::DeviceStatus::ready); | ||
| 62 | |||
| 63 | const auto cs_prog = fssm_program({0x43532001, 0xbf810000}); | ||
| 64 | const auto vs_prog = fssm_program({0x56532001, 0xbf810000}); | ||
| 65 | const auto ps_prog = fssm_program({0x50532001, 0xbf810000}); | ||
| 66 | const auto cs_dxbc = shader_container(cs_prog); | ||
| 67 | const auto vs_dxbc = shader_container(vs_prog); | ||
| 68 | const auto ps_dxbc = shader_container(ps_prog); | ||
| 69 | |||
| 70 | const auto lib_dir = std::filesystem::temp_directory_path() / "leonida_pipeline_library_test"; | ||
| 71 | std::filesystem::create_directories(lib_dir); | ||
| 72 | std::filesystem::copy_file(LEONIDA_TEST_CS_SPV, lib_dir / "cs.spv", | ||
| 73 | std::filesystem::copy_options::overwrite_existing); | ||
| 74 | std::filesystem::copy_file(LEONIDA_TEST_VS_SPV, lib_dir / "vs.spv", | ||
| 75 | std::filesystem::copy_options::overwrite_existing); | ||
| 76 | std::filesystem::copy_file(LEONIDA_TEST_PS_SPV, lib_dir / "ps.spv", | ||
| 77 | std::filesystem::copy_options::overwrite_existing); | ||
| 78 | { | ||
| 79 | std::ofstream idx(lib_dir / "index.json", std::ios::binary); | ||
| 80 | idx << "{\"cs\": {\"stage\": \"cs\", \"fssm_sha256\": \"" << leonida::sha256_hex(cs_prog) | ||
| 81 | << "\", \"bindings\": [], \"push_constant_bytes\": 144},\n" | ||
| 82 | " \"vs\": {\"stage\": \"vs\", \"fssm_sha256\": \"" << leonida::sha256_hex(vs_prog) | ||
| 83 | << "\", \"bindings\": [], \"push_constant_bytes\": 144, \"input_signature\": [" | ||
| 84 | "{\"semantic\": \"COLOR\", \"semantic_index\": 0, \"register\": 0, \"mask\": 15, \"rw_mask\": 15, " | ||
| 85 | "\"system_value\": 0, \"component_type\": \"float\"}, " | ||
| 86 | "{\"semantic\": \"POSITION\", \"semantic_index\": 0, \"register\": 1, \"mask\": 3, \"rw_mask\": 3, " | ||
| 87 | "\"system_value\": 0, \"component_type\": \"float\"}]},\n" | ||
| 88 | " \"ps\": {\"stage\": \"ps\", \"fssm_sha256\": \"" << leonida::sha256_hex(ps_prog) | ||
| 89 | << "\", \"bindings\": [], \"push_constant_bytes\": 144, \"outputs\": [0]}}"; | ||
| 90 | } | ||
| 91 | |||
| 92 | rt::d3d12::VulkanDeviceInfo info{}; | ||
| 93 | info.get_instance_proc = d.instance_proc; | ||
| 94 | info.instance = d.instance; | ||
| 95 | info.physical = d.physical; | ||
| 96 | info.device = d.device; | ||
| 97 | info.queue = d.queue; | ||
| 98 | info.queue_family = d.family; | ||
| 99 | info.buffer_device_address = true; | ||
| 100 | info.shader_dir = lib_dir.string(); | ||
| 101 | |||
| 102 | Ref<ID3D12Device1> device; | ||
| 103 | EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&device.p)))); | ||
| 104 | |||
| 105 | // 1. CreatePipelineLibrary creation, capability check (null output -> S_FALSE), and invalid/corrupt blobs. | ||
| 106 | { | ||
| 107 | EXPECT(device->CreatePipelineLibrary(nullptr, 0, __uuidof(ID3D12PipelineLibrary), nullptr) == S_FALSE); | ||
| 108 | Ref<ID3D12PipelineLibrary> bad; | ||
| 109 | EXPECT(device->CreatePipelineLibrary(nullptr, 16, IID_PPV_ARGS(&bad.p)) == E_INVALIDARG && !bad.p); | ||
| 110 | const std::uint32_t foreign[4] = {0xDEADBEEFu, 1u, 0u, 0u}; | ||
| 111 | EXPECT(device->CreatePipelineLibrary(foreign, sizeof(foreign), IID_PPV_ARGS(&bad.p)) == | ||
| 112 | D3D12_ERROR_DRIVER_VERSION_MISMATCH && | ||
| 113 | !bad.p); | ||
| 114 | const std::uint8_t tiny[4] = {1, 2, 3, 4}; | ||
| 115 | EXPECT(device->CreatePipelineLibrary(tiny, sizeof(tiny), IID_PPV_ARGS(&bad.p)) == | ||
| 116 | D3D12_ERROR_DRIVER_VERSION_MISMATCH && | ||
| 117 | !bad.p); | ||
| 118 | } | ||
| 119 | |||
| 120 | // 2. Build one compute PSO and one graphics PSO. | ||
| 121 | D3D12_DESCRIPTOR_RANGE uav_range{D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, 1, 0, 0}; | ||
| 122 | D3D12_ROOT_PARAMETER cs_params[3]{}; | ||
| 123 | cs_params[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; | ||
| 124 | cs_params[0].Constants = {0, 0, 1}; | ||
| 125 | cs_params[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_UAV; | ||
| 126 | cs_params[1].Descriptor = {0, 0}; | ||
| 127 | cs_params[2].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; | ||
| 128 | cs_params[2].DescriptorTable = {1, &uav_range}; | ||
| 129 | D3D12_VERSIONED_ROOT_SIGNATURE_DESC cs_rs_desc{}; | ||
| 130 | cs_rs_desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_0; | ||
| 131 | cs_rs_desc.Desc_1_0 = {3, cs_params, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE}; | ||
| 132 | const auto cs_rs_blob = serialize_root_signature(cs_rs_desc); | ||
| 133 | Ref<ID3D12RootSignature> cs_rs; | ||
| 134 | EXPECT(SUCCEEDED(device->CreateRootSignature(0, cs_rs_blob.data(), cs_rs_blob.size(), IID_PPV_ARGS(&cs_rs.p)))); | ||
| 135 | |||
| 136 | D3D12_COMPUTE_PIPELINE_STATE_DESC cs_desc{}; | ||
| 137 | cs_desc.pRootSignature = cs_rs.p; | ||
| 138 | cs_desc.CS = {cs_dxbc.data(), cs_dxbc.size()}; | ||
| 139 | Ref<ID3D12PipelineState> cs_pso; | ||
| 140 | EXPECT(SUCCEEDED(device->CreateComputePipelineState(&cs_desc, IID_PPV_ARGS(&cs_pso.p)))); | ||
| 141 | |||
| 142 | D3D12_ROOT_PARAMETER gfx_params[2]{}; | ||
| 143 | gfx_params[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; | ||
| 144 | gfx_params[0].Constants = {0, 0, 2}; | ||
| 145 | gfx_params[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV; | ||
| 146 | gfx_params[1].Descriptor = {1, 0}; | ||
| 147 | D3D12_VERSIONED_ROOT_SIGNATURE_DESC gfx_rs_desc{}; | ||
| 148 | gfx_rs_desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_0; | ||
| 149 | gfx_rs_desc.Desc_1_0 = {2, gfx_params, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT}; | ||
| 150 | const auto gfx_rs_blob = serialize_root_signature(gfx_rs_desc); | ||
| 151 | Ref<ID3D12RootSignature> gfx_rs; | ||
| 152 | EXPECT(SUCCEEDED(device->CreateRootSignature(0, gfx_rs_blob.data(), gfx_rs_blob.size(), IID_PPV_ARGS(&gfx_rs.p)))); | ||
| 153 | |||
| 154 | const D3D12_INPUT_ELEMENT_DESC input_elems[2] = { | ||
| 155 | {"POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0}, | ||
| 156 | {"COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, D3D12_APPEND_ALIGNED_ELEMENT, | ||
| 157 | D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0}, | ||
| 158 | }; | ||
| 159 | D3D12_GRAPHICS_PIPELINE_STATE_DESC gfx_desc{}; | ||
| 160 | gfx_desc.pRootSignature = gfx_rs.p; | ||
| 161 | gfx_desc.VS = {vs_dxbc.data(), vs_dxbc.size()}; | ||
| 162 | gfx_desc.PS = {ps_dxbc.data(), ps_dxbc.size()}; | ||
| 163 | gfx_desc.BlendState.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL; | ||
| 164 | gfx_desc.SampleMask = 0xFFFFFFFFu; | ||
| 165 | gfx_desc.RasterizerState.FillMode = D3D12_FILL_MODE_SOLID; | ||
| 166 | gfx_desc.RasterizerState.CullMode = D3D12_CULL_MODE_BACK; | ||
| 167 | gfx_desc.RasterizerState.DepthClipEnable = TRUE; | ||
| 168 | gfx_desc.InputLayout = {input_elems, 2}; | ||
| 169 | gfx_desc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE; | ||
| 170 | gfx_desc.NumRenderTargets = 1; | ||
| 171 | gfx_desc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM; | ||
| 172 | gfx_desc.SampleDesc = {1, 0}; | ||
| 173 | Ref<ID3D12PipelineState> gfx_pso; | ||
| 174 | EXPECT(SUCCEEDED(device->CreateGraphicsPipelineState(&gfx_desc, IID_PPV_ARGS(&gfx_pso.p)))); | ||
| 175 | |||
| 176 | // 3. Store, load, cross-type rejection, and serialize round-trip. | ||
| 177 | Ref<ID3D12PipelineLibrary1> lib; | ||
| 178 | EXPECT(SUCCEEDED(device->CreatePipelineLibrary(nullptr, 0, IID_PPV_ARGS(&lib.p))) && lib.p); | ||
| 179 | Ref<ID3D12Device> parent_dev; | ||
| 180 | EXPECT(SUCCEEDED(lib->GetDevice(IID_PPV_ARGS(&parent_dev.p))) && parent_dev.p); | ||
| 181 | |||
| 182 | EXPECT(lib->StorePipeline(nullptr, cs_pso.p) == E_INVALIDARG); | ||
| 183 | EXPECT(lib->StorePipeline(L"", cs_pso.p) == E_INVALIDARG); | ||
| 184 | EXPECT(lib->StorePipeline(L"cs", nullptr) == E_INVALIDARG); | ||
| 185 | EXPECT(SUCCEEDED(lib->StorePipeline(L"cs", cs_pso.p))); | ||
| 186 | EXPECT(lib->StorePipeline(L"cs", cs_pso.p) == E_INVALIDARG); // duplicate name | ||
| 187 | EXPECT(SUCCEEDED(lib->StorePipeline(L"gfx", gfx_pso.p))); | ||
| 188 | |||
| 189 | Ref<ID3D12PipelineState> loaded_cs, loaded_gfx, loaded_any, wrong; | ||
| 190 | EXPECT(lib->LoadComputePipeline(L"missing", &cs_desc, IID_PPV_ARGS(&wrong.p)) == E_INVALIDARG && !wrong.p); | ||
| 191 | EXPECT(lib->LoadComputePipeline(L"gfx", &cs_desc, IID_PPV_ARGS(&wrong.p)) == E_INVALIDARG && !wrong.p); | ||
| 192 | EXPECT(lib->LoadGraphicsPipeline(L"cs", &gfx_desc, IID_PPV_ARGS(&wrong.p)) == E_INVALIDARG && !wrong.p); | ||
| 193 | EXPECT(SUCCEEDED(lib->LoadComputePipeline(L"cs", &cs_desc, IID_PPV_ARGS(&loaded_cs.p))) && loaded_cs.p); | ||
| 194 | EXPECT(SUCCEEDED(lib->LoadGraphicsPipeline(L"gfx", &gfx_desc, IID_PPV_ARGS(&loaded_gfx.p))) && loaded_gfx.p); | ||
| 195 | |||
| 196 | std::uint32_t dummy_stream = 0; | ||
| 197 | D3D12_PIPELINE_STATE_STREAM_DESC stream_desc{sizeof(dummy_stream), &dummy_stream}; | ||
| 198 | D3D12_PIPELINE_STATE_STREAM_DESC empty_stream{0, nullptr}; | ||
| 199 | EXPECT(lib->LoadPipeline(L"gfx", &empty_stream, IID_PPV_ARGS(&wrong.p)) == E_INVALIDARG && !wrong.p); | ||
| 200 | EXPECT(SUCCEEDED(lib->LoadPipeline(L"gfx", &stream_desc, IID_PPV_ARGS(&loaded_any.p))) && loaded_any.p); | ||
| 201 | |||
| 202 | const SIZE_T blob_size = lib->GetSerializedSize(); | ||
| 203 | EXPECT(blob_size >= 16); | ||
| 204 | std::vector<std::uint8_t> blob(blob_size); | ||
| 205 | EXPECT(lib->Serialize(nullptr, blob_size) == E_INVALIDARG); | ||
| 206 | EXPECT(lib->Serialize(blob.data(), blob_size - 1) == E_INVALIDARG); | ||
| 207 | EXPECT(SUCCEEDED(lib->Serialize(blob.data(), blob_size))); | ||
| 208 | |||
| 209 | Ref<ID3D12PipelineLibrary> reloaded; | ||
| 210 | EXPECT(SUCCEEDED(device->CreatePipelineLibrary(blob.data(), blob.size(), IID_PPV_ARGS(&reloaded.p))) && reloaded.p); | ||
| 211 | |||
| 212 | // 4. Execute the loaded compute PSO and loaded graphics PSO on the GPU. | ||
| 213 | Ref<ID3D12CommandQueue> queue; | ||
| 214 | const D3D12_COMMAND_QUEUE_DESC qd{D3D12_COMMAND_LIST_TYPE_DIRECT, 0, D3D12_COMMAND_QUEUE_FLAG_NONE, 0}; | ||
| 215 | EXPECT(SUCCEEDED(device->CreateCommandQueue(&qd, IID_PPV_ARGS(&queue.p)))); | ||
| 216 | Ref<ID3D12CommandAllocator> alloc; | ||
| 217 | EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&alloc.p)))); | ||
| 218 | Ref<ID3D12GraphicsCommandList> list; | ||
| 219 | EXPECT(SUCCEEDED( | ||
| 220 | device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, alloc.p, nullptr, IID_PPV_ARGS(&list.p)))); | ||
| 221 | Ref<ID3D12Fence> fence; | ||
| 222 | EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)))); | ||
| 223 | |||
| 224 | constexpr UINT W = 16, H = 16; | ||
| 225 | const D3D12_HEAP_PROPERTIES hp_def{D3D12_HEAP_TYPE_DEFAULT}; | ||
| 226 | const D3D12_HEAP_PROPERTIES hp_up{D3D12_HEAP_TYPE_UPLOAD}; | ||
| 227 | const D3D12_HEAP_PROPERTIES hp_rb{D3D12_HEAP_TYPE_READBACK}; | ||
| 228 | const auto rtd = tex2d_desc(W, H, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET); | ||
| 229 | const Vertex verts[4] = { | ||
| 230 | {-1.0f, 1.0f, 255, 0, 0, 255}, | ||
| 231 | { 0.0f, 1.0f, 255, 0, 0, 255}, | ||
| 232 | { 0.0f, 0.0f, 255, 0, 0, 255}, | ||
| 233 | {-1.0f, 0.0f, 255, 0, 0, 255}, | ||
| 234 | }; | ||
| 235 | const std::uint16_t indices[6] = {0, 1, 2, 0, 2, 3}; | ||
| 236 | const float tint[4] = {1.0f, 0.0f, 1.0f, 1.0f}; | ||
| 237 | const auto vbd = buffer_desc(sizeof(verts)); | ||
| 238 | const auto ibd = buffer_desc(sizeof(indices)); | ||
| 239 | const auto cbd = buffer_desc(256); | ||
| 240 | Ref<ID3D12Resource> rt, vb, ib, cb, rb; | ||
| 241 | EXPECT(SUCCEEDED(device->CreateCommittedResource(&hp_def, D3D12_HEAP_FLAG_NONE, &rtd, | ||
| 242 | D3D12_RESOURCE_STATE_RENDER_TARGET, nullptr, IID_PPV_ARGS(&rt.p)))); | ||
| 243 | EXPECT(SUCCEEDED(device->CreateCommittedResource(&hp_up, D3D12_HEAP_FLAG_NONE, &vbd, | ||
| 244 | D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&vb.p)))); | ||
| 245 | EXPECT(SUCCEEDED(device->CreateCommittedResource(&hp_up, D3D12_HEAP_FLAG_NONE, &ibd, | ||
| 246 | D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&ib.p)))); | ||
| 247 | EXPECT(SUCCEEDED(device->CreateCommittedResource(&hp_up, D3D12_HEAP_FLAG_NONE, &cbd, | ||
| 248 | D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&cb.p)))); | ||
| 249 | void* p = nullptr; | ||
| 250 | EXPECT(SUCCEEDED(vb->Map(0, nullptr, &p))); | ||
| 251 | std::memcpy(p, verts, sizeof(verts)); | ||
| 252 | vb->Unmap(0, nullptr); | ||
| 253 | EXPECT(SUCCEEDED(ib->Map(0, nullptr, &p))); | ||
| 254 | std::memcpy(p, indices, sizeof(indices)); | ||
| 255 | ib->Unmap(0, nullptr); | ||
| 256 | EXPECT(SUCCEEDED(cb->Map(0, nullptr, &p))); | ||
| 257 | std::memcpy(p, tint, sizeof(tint)); | ||
| 258 | cb->Unmap(0, nullptr); | ||
| 259 | |||
| 260 | D3D12_PLACED_SUBRESOURCE_FOOTPRINT fp{}; | ||
| 261 | UINT64 total_bytes = 0; | ||
| 262 | device->GetCopyableFootprints(&rtd, 0, 1, 0, &fp, nullptr, nullptr, &total_bytes); | ||
| 263 | const auto rbd = buffer_desc(total_bytes); | ||
| 264 | EXPECT(SUCCEEDED(device->CreateCommittedResource(&hp_rb, D3D12_HEAP_FLAG_NONE, &rbd, | ||
| 265 | D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&rb.p)))); | ||
| 266 | |||
| 267 | Ref<ID3D12DescriptorHeap> rtv_heap; | ||
| 268 | const D3D12_DESCRIPTOR_HEAP_DESC rhd{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}; | ||
| 269 | EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&rhd, IID_PPV_ARGS(&rtv_heap.p)))); | ||
| 270 | const auto rtv = rtv_heap->GetCPUDescriptorHandleForHeapStart(); | ||
| 271 | device->CreateRenderTargetView(rt.p, nullptr, rtv); | ||
| 272 | |||
| 273 | const float blue[4] = {0.0f, 0.0f, 1.0f, 1.0f}; | ||
| 274 | list->ClearRenderTargetView(rtv, blue, 0, nullptr); | ||
| 275 | list->OMSetRenderTargets(1, &rtv, FALSE, nullptr); | ||
| 276 | const D3D12_VIEWPORT vp{0, 0, float(W), float(H), 0.0f, 1.0f}; | ||
| 277 | const D3D12_RECT sc{0, 0, LONG(W), LONG(H)}; | ||
| 278 | list->RSSetViewports(1, &vp); | ||
| 279 | list->RSSetScissorRects(1, &sc); | ||
| 280 | list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); | ||
| 281 | const D3D12_VERTEX_BUFFER_VIEW vbv{vb->GetGPUVirtualAddress(), sizeof(verts), sizeof(Vertex)}; | ||
| 282 | const D3D12_INDEX_BUFFER_VIEW ibv{ib->GetGPUVirtualAddress(), sizeof(indices), DXGI_FORMAT_R16_UINT}; | ||
| 283 | list->IASetVertexBuffers(0, 1, &vbv); | ||
| 284 | list->IASetIndexBuffer(&ibv); | ||
| 285 | list->SetGraphicsRootSignature(gfx_rs.p); | ||
| 286 | const float offset[2] = {0.0f, 0.0f}; | ||
| 287 | list->SetGraphicsRoot32BitConstants(0, 2, offset, 0); | ||
| 288 | list->SetGraphicsRootConstantBufferView(1, cb->GetGPUVirtualAddress()); | ||
| 289 | list->SetPipelineState(loaded_gfx.p); | ||
| 290 | list->DrawIndexedInstanced(6, 1, 0, 0, 0); | ||
| 291 | |||
| 292 | const auto to_src = transition(rt.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE); | ||
| 293 | list->ResourceBarrier(1, &to_src); | ||
| 294 | D3D12_TEXTURE_COPY_LOCATION src_loc{rt.p, D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, {0}}; | ||
| 295 | D3D12_TEXTURE_COPY_LOCATION dst_loc{rb.p, D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, {}}; | ||
| 296 | dst_loc.PlacedFootprint = fp; | ||
| 297 | list->CopyTextureRegion(&dst_loc, 0, 0, 0, &src_loc, nullptr); | ||
| 298 | EXPECT(SUCCEEDED(list->Close())); | ||
| 299 | ID3D12CommandList* lists[] = {list.p}; | ||
| 300 | queue->ExecuteCommandLists(1, lists); | ||
| 301 | EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success); | ||
| 302 | EXPECT(SUCCEEDED(queue->Signal(fence.p, 1)) && SUCCEEDED(fence->SetEventOnCompletion(1, nullptr))); | ||
| 303 | |||
| 304 | EXPECT(SUCCEEDED(rb->Map(0, nullptr, &p))); | ||
| 305 | const auto* bytes = static_cast<const std::uint8_t*>(p); | ||
| 306 | std::uint32_t tl = 0, br = 0; | ||
| 307 | std::memcpy(&tl, bytes + 2 * fp.Footprint.RowPitch + 2 * 4, 4); | ||
| 308 | std::memcpy(&br, bytes + 12 * fp.Footprint.RowPitch + 12 * 4, 4); | ||
| 309 | EXPECT(tl == 0xFF0000FFu); // red quad drawn by PSO loaded from PipelineLibrary | ||
| 310 | EXPECT(br == 0xFFFF0000u); // blue background clear | ||
| 311 | rb->Unmap(0, nullptr); | ||
| 312 | |||
| 313 | EXPECT(d.validation_errors() == 0); | ||
| 314 | std::printf("d3d12_pipeline_library_test: ok\n"); | ||
| 315 | return 0; | ||
| 316 | } | ||