mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 18:28:38 +00:00
636 lines
33 KiB
C++
636 lines
33 KiB
C++
// End-to-end D3D12 -> Vulkan GPU test for Xbox console list slot 142 (OMSetSampleMaskX, 0x470):
|
|
// - Exercises d3d12_xs_device (slot 142 + LEONIDA_SAMPLE_MASK=0) -> d3d12_com (pixel_mask state &
|
|
// SetPipelineState / Reset lifecycle) -> d3d12_vulkan_device (on-demand masked twin PSO +
|
|
// 148-byte masked_layout) -> vulkan_replay -> real Vulkan GPU.
|
|
// - Draws quads under 2x2 masks 0x5, 0xA, 0x1, 0x0, 0xF and verifies exact per-pixel color AND
|
|
// depth buffer (confirming EarlyFragmentTests is stripped so discarded pixels write neither
|
|
// color nor depth).
|
|
// - Verifies SetPipelineState(graphics) resets sample mask to 0xF while SetPipelineState(compute)
|
|
// preserves it, Reset() resets it to 0xF, depth-only PSOs are unaffected, and
|
|
// LEONIDA_SAMPLE_MASK=0 ignores slot 142.
|
|
// - Verifies 0 Khronos Validation Layer errors when LEONIDA_VK_VALIDATION=1.
|
|
// Exits 77 without a Vulkan 1.3 device. No game data.
|
|
#include "test_temp_dir.h"
|
|
#include "vk_test_device.h"
|
|
#include "d3d12_test_shaders.h"
|
|
#include "../shims/d3d12_com.h"
|
|
#include "../shims/d3d12_xs_device.h"
|
|
#include "../src/sample_mask_fragment.h"
|
|
#include "../src/shader_library.h"
|
|
|
|
#include <bit>
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#define EXPECT(cond) \
|
|
do { \
|
|
if (!(cond)) { \
|
|
std::fprintf(stderr, "d3d12_sample_mask_test: line %d failed: %s\n", __LINE__, #cond); \
|
|
return 1; \
|
|
} \
|
|
} while (0)
|
|
|
|
namespace {
|
|
|
|
using namespace d3d12test;
|
|
using namespace leonida;
|
|
using namespace leonida::spirv_sample_mask;
|
|
|
|
constexpr UINT kSide = 16;
|
|
|
|
void append_str(Inst& i, const char* s) {
|
|
const std::size_t n = std::strlen(s) + 1;
|
|
const std::size_t words = (n + 3) / 4;
|
|
const std::size_t at = i.size();
|
|
i.resize(at + words, 0);
|
|
std::memcpy(&i[at], s, n);
|
|
fix_length(i);
|
|
}
|
|
|
|
// Build a minimal DXBC container with a version 1.0 RTS0 chunk: 1 root parameter with 5 32-bit
|
|
// constants (register b0, space 0, visibility ALL). No host d3d12.dll required.
|
|
std::vector<std::uint8_t> make_root_sig_5_constants() {
|
|
// RTS0 v1.0 header (24 bytes): version=1, num_params=1, params_offset=24, num_samplers=0,
|
|
// samplers_offset=24, flags=0.
|
|
// Param 0 (12 bytes): type=1 (32BIT_CONSTANTS), visibility=0 (ALL), offset=36.
|
|
// Constants payload (12 bytes): shader_reg=0, reg_space=0, num_32bit_values=5.
|
|
std::vector<std::uint8_t> rts0;
|
|
for (std::uint32_t w : {1u, 1u, 24u, 0u, 24u, 0u, 1u, 0u, 36u, 0u, 0u, 5u}) put32(rts0, w);
|
|
|
|
std::vector<std::uint8_t> dxbc = {'D', 'X', 'B', 'C'};
|
|
dxbc.resize(28, 0); // hash + version + total_size placeholder
|
|
put32(dxbc, 1u); // chunk count
|
|
put32(dxbc, 36u); // chunk 0 offset
|
|
dxbc.insert(dxbc.end(), {'R', 'T', 'S', '0'});
|
|
put32(dxbc, static_cast<std::uint32_t>(rts0.size()));
|
|
dxbc.insert(dxbc.end(), rts0.begin(), rts0.end());
|
|
const auto total = static_cast<std::uint32_t>(dxbc.size());
|
|
std::memcpy(dxbc.data() + 24, &total, 4);
|
|
return dxbc;
|
|
}
|
|
|
|
// Fullscreen triangle VS reading z from user_data.s[8] (graphics root constants start at s8).
|
|
std::vector<std::uint32_t> make_vs() {
|
|
constexpr std::uint32_t op_capability = 17, op_memory_model = 14, op_type_array = 28, op_type_function = 33,
|
|
op_store = 62, op_composite_construct = 80, op_convert_u_to_f = 112, op_bitcast = 124,
|
|
op_f_sub = 131, op_return = 253;
|
|
constexpr std::uint32_t model_vertex = 0, storage_output = 3, dec_array_stride = 6;
|
|
constexpr std::uint32_t builtin_position = 0, builtin_vertex_index = 42;
|
|
|
|
std::uint32_t next = 1;
|
|
const auto id = [&] { return next++; };
|
|
const std::uint32_t id_main = id(), id_void = id(), id_fn = id(), id_f32 = id(), id_v4f = id();
|
|
const std::uint32_t id_u32 = id(), id_i32 = id();
|
|
const std::uint32_t id_c32 = id(), id_c0 = id(), id_c1 = id(), id_c2 = id(), id_c8 = id(), id_f1 = id();
|
|
const std::uint32_t id_arr = id(), id_pc_struct = id(), id_ptr_pc = id(), id_ptr_pc_u32 = id(), id_pc_var = id();
|
|
const std::uint32_t id_ptr_in_i32 = id(), id_vidx_var = id();
|
|
const std::uint32_t id_ptr_out_v4f = id(), id_pos_var = id();
|
|
const std::uint32_t id_l0 = id();
|
|
|
|
std::vector<Inst> insts;
|
|
insts.push_back(make(op_capability, {1}));
|
|
insts.push_back(make(op_memory_model, {0, 1}));
|
|
Inst ep = make(op_entry_point, {model_vertex, id_main});
|
|
append_str(ep, "main");
|
|
ep.push_back(id_vidx_var);
|
|
ep.push_back(id_pos_var);
|
|
fix_length(ep);
|
|
insts.push_back(std::move(ep));
|
|
|
|
insts.push_back(make(op_decorate, {id_vidx_var, decoration_builtin, builtin_vertex_index}));
|
|
insts.push_back(make(op_decorate, {id_pos_var, decoration_builtin, builtin_position}));
|
|
insts.push_back(make(op_decorate, {id_arr, dec_array_stride, 4}));
|
|
insts.push_back(make(op_decorate, {id_pc_struct, decoration_block}));
|
|
insts.push_back(make(op_member_decorate, {id_pc_struct, 0, decoration_offset, 0}));
|
|
insts.push_back(make(op_member_decorate, {id_pc_struct, 1, decoration_offset, 128}));
|
|
insts.push_back(make(op_member_decorate, {id_pc_struct, 2, decoration_offset, 132}));
|
|
insts.push_back(make(op_member_decorate, {id_pc_struct, 3, decoration_offset, 136}));
|
|
insts.push_back(make(op_member_decorate, {id_pc_struct, 4, decoration_offset, 140}));
|
|
|
|
insts.push_back(make(op_type_void, {id_void}));
|
|
insts.push_back(make(op_type_function, {id_fn, id_void}));
|
|
insts.push_back(make(op_type_float, {id_f32, 32}));
|
|
insts.push_back(make(op_type_vector, {id_v4f, id_f32, 4}));
|
|
insts.push_back(make(op_type_int, {id_u32, 32, 0}));
|
|
insts.push_back(make(op_type_int, {id_i32, 32, 1}));
|
|
insts.push_back(make(op_constant, {id_u32, id_c32, 32}));
|
|
insts.push_back(make(op_constant, {id_u32, id_c0, 0}));
|
|
insts.push_back(make(op_constant, {id_u32, id_c1, 1}));
|
|
insts.push_back(make(op_constant, {id_u32, id_c2, 2}));
|
|
insts.push_back(make(op_constant, {id_u32, id_c8, 8}));
|
|
insts.push_back(make(op_constant, {id_f32, id_f1, std::bit_cast<std::uint32_t>(1.0f)}));
|
|
insts.push_back(make(op_type_array, {id_arr, id_u32, id_c32}));
|
|
insts.push_back(make(op_type_struct, {id_pc_struct, id_arr, id_u32, id_u32, id_u32, id_u32}));
|
|
insts.push_back(make(op_type_pointer, {id_ptr_pc, storage_push_constant, id_pc_struct}));
|
|
insts.push_back(make(op_type_pointer, {id_ptr_pc_u32, storage_push_constant, id_u32}));
|
|
insts.push_back(make(op_type_pointer, {id_ptr_in_i32, storage_input, id_i32}));
|
|
insts.push_back(make(op_type_pointer, {id_ptr_out_v4f, storage_output, id_v4f}));
|
|
|
|
insts.push_back(make(op_variable, {id_ptr_pc, id_pc_var, storage_push_constant}));
|
|
insts.push_back(make(op_variable, {id_ptr_in_i32, id_vidx_var, storage_input}));
|
|
insts.push_back(make(op_variable, {id_ptr_out_v4f, id_pos_var, storage_output}));
|
|
|
|
insts.push_back(make(op_function, {id_void, id_main, 0, id_fn}));
|
|
insts.push_back(make(op_label, {id_l0}));
|
|
const std::uint32_t vi_s = id(), vi = id(), x0 = id(), x1 = id(), xf = id(), x = id();
|
|
const std::uint32_t y0 = id(), y1 = id(), yf = id(), y = id();
|
|
const std::uint32_t zp = id(), zu = id(), z = id(), pos = id();
|
|
insts.push_back(make(op_load, {id_i32, vi_s, id_vidx_var}));
|
|
insts.push_back(make(op_bitcast, {id_u32, vi, vi_s}));
|
|
insts.push_back(make(op_bitwise_and, {id_u32, x0, vi, id_c1}));
|
|
insts.push_back(make(op_shift_left_logical, {id_u32, x1, x0, id_c2}));
|
|
insts.push_back(make(op_convert_u_to_f, {id_f32, xf, x1}));
|
|
insts.push_back(make(op_f_sub, {id_f32, x, xf, id_f1}));
|
|
insts.push_back(make(op_bitwise_and, {id_u32, y0, vi, id_c2}));
|
|
insts.push_back(make(op_shift_left_logical, {id_u32, y1, y0, id_c1}));
|
|
insts.push_back(make(op_convert_u_to_f, {id_f32, yf, y1}));
|
|
insts.push_back(make(op_f_sub, {id_f32, y, yf, id_f1}));
|
|
insts.push_back(make(op_access_chain, {id_ptr_pc_u32, zp, id_pc_var, id_c0, id_c8}));
|
|
insts.push_back(make(op_load, {id_u32, zu, zp}));
|
|
insts.push_back(make(op_bitcast, {id_f32, z, zu}));
|
|
insts.push_back(make(op_composite_construct, {id_v4f, pos, x, y, z, id_f1}));
|
|
insts.push_back(make(op_store, {id_pos_var, pos}));
|
|
insts.push_back(make(op_return, {}));
|
|
insts.push_back(make(op_function_end, {}));
|
|
|
|
std::vector<std::uint32_t> words = {0x07230203u, 0x00010000u, 0, next, 0};
|
|
for (const Inst& i : insts) words.insert(words.end(), i.begin(), i.end());
|
|
return words;
|
|
}
|
|
|
|
std::vector<std::uint32_t> make_ps(bool with_frag_coord) {
|
|
constexpr std::uint32_t op_capability = 17, op_memory_model = 14, op_type_array = 28, op_type_function = 33,
|
|
op_store = 62, op_composite_construct = 80, op_bitcast = 124, op_return = 253;
|
|
constexpr std::uint32_t mode_origin_upper_left = 7, storage_output = 3, dec_location = 30, dec_array_stride = 6;
|
|
|
|
std::uint32_t next = 1;
|
|
const auto id = [&] { return next++; };
|
|
const std::uint32_t id_main = id(), id_void = id(), id_fn = id(), id_f32 = id(), id_v4f = id(), id_u32 = id();
|
|
const std::uint32_t id_c32 = id(), id_c0 = id(), id_c1 = id(), id_c2 = id(), id_c3 = id(), id_c4 = id();
|
|
const std::uint32_t id_arr = id(), id_pc_struct = id(), id_ptr_pc = id(), id_ptr_pc_u32 = id(), id_pc_var = id();
|
|
const std::uint32_t id_ptr_out = id(), id_out_color = id();
|
|
const std::uint32_t id_ptr_in = with_frag_coord ? id() : 0;
|
|
const std::uint32_t id_frag_coord = with_frag_coord ? id() : 0;
|
|
const std::uint32_t id_l0 = id();
|
|
|
|
std::vector<Inst> insts;
|
|
insts.push_back(make(op_capability, {1}));
|
|
insts.push_back(make(op_memory_model, {0, 1}));
|
|
Inst ep = make(op_entry_point, {model_fragment, id_main});
|
|
append_str(ep, "main");
|
|
ep.push_back(id_out_color);
|
|
if (with_frag_coord) ep.push_back(id_frag_coord);
|
|
fix_length(ep);
|
|
insts.push_back(std::move(ep));
|
|
|
|
insts.push_back(make(op_execution_mode, {id_main, mode_origin_upper_left}));
|
|
insts.push_back(make(op_execution_mode, {id_main, mode_early_fragment_tests}));
|
|
insts.push_back(make(op_decorate, {id_out_color, dec_location, 0}));
|
|
if (with_frag_coord) insts.push_back(make(op_decorate, {id_frag_coord, decoration_builtin, builtin_frag_coord}));
|
|
insts.push_back(make(op_decorate, {id_arr, dec_array_stride, 4}));
|
|
insts.push_back(make(op_decorate, {id_pc_struct, decoration_block}));
|
|
insts.push_back(make(op_member_decorate, {id_pc_struct, 0, decoration_offset, 0}));
|
|
insts.push_back(make(op_member_decorate, {id_pc_struct, 1, decoration_offset, 128}));
|
|
insts.push_back(make(op_member_decorate, {id_pc_struct, 2, decoration_offset, 132}));
|
|
insts.push_back(make(op_member_decorate, {id_pc_struct, 3, decoration_offset, 136}));
|
|
insts.push_back(make(op_member_decorate, {id_pc_struct, 4, decoration_offset, 140}));
|
|
|
|
insts.push_back(make(op_type_void, {id_void}));
|
|
insts.push_back(make(op_type_function, {id_fn, id_void}));
|
|
insts.push_back(make(op_type_float, {id_f32, 32}));
|
|
insts.push_back(make(op_type_vector, {id_v4f, id_f32, 4}));
|
|
insts.push_back(make(op_type_int, {id_u32, 32, 0}));
|
|
insts.push_back(make(op_constant, {id_u32, id_c32, 32}));
|
|
insts.push_back(make(op_constant, {id_u32, id_c0, 0}));
|
|
insts.push_back(make(op_constant, {id_u32, id_c1, 9}));
|
|
insts.push_back(make(op_constant, {id_u32, id_c2, 10}));
|
|
insts.push_back(make(op_constant, {id_u32, id_c3, 11}));
|
|
insts.push_back(make(op_constant, {id_u32, id_c4, 12}));
|
|
insts.push_back(make(op_type_array, {id_arr, id_u32, id_c32}));
|
|
insts.push_back(make(op_type_struct, {id_pc_struct, id_arr, id_u32, id_u32, id_u32, id_u32}));
|
|
insts.push_back(make(op_type_pointer, {id_ptr_pc, storage_push_constant, id_pc_struct}));
|
|
insts.push_back(make(op_type_pointer, {id_ptr_pc_u32, storage_push_constant, id_u32}));
|
|
insts.push_back(make(op_type_pointer, {id_ptr_out, storage_output, id_v4f}));
|
|
if (with_frag_coord) insts.push_back(make(op_type_pointer, {id_ptr_in, storage_input, id_v4f}));
|
|
|
|
insts.push_back(make(op_variable, {id_ptr_pc, id_pc_var, storage_push_constant}));
|
|
insts.push_back(make(op_variable, {id_ptr_out, id_out_color, storage_output}));
|
|
if (with_frag_coord) insts.push_back(make(op_variable, {id_ptr_in, id_frag_coord, storage_input}));
|
|
|
|
insts.push_back(make(op_function, {id_void, id_main, 0, id_fn}));
|
|
insts.push_back(make(op_label, {id_l0}));
|
|
if (with_frag_coord) {
|
|
const std::uint32_t fc = id();
|
|
insts.push_back(make(op_load, {id_v4f, fc, id_frag_coord}));
|
|
}
|
|
const std::uint32_t cidx[4] = {id_c1, id_c2, id_c3, id_c4};
|
|
std::uint32_t comp[4]{};
|
|
for (int k = 0; k < 4; ++k) {
|
|
const std::uint32_t p = id(), u = id(), f = id();
|
|
insts.push_back(make(op_access_chain, {id_ptr_pc_u32, p, id_pc_var, id_c0, cidx[k]}));
|
|
insts.push_back(make(op_load, {id_u32, u, p}));
|
|
insts.push_back(make(op_bitcast, {id_f32, f, u}));
|
|
comp[k] = f;
|
|
}
|
|
const std::uint32_t col = id();
|
|
insts.push_back(make(op_composite_construct, {id_v4f, col, comp[0], comp[1], comp[2], comp[3]}));
|
|
insts.push_back(make(op_store, {id_out_color, col}));
|
|
insts.push_back(make(op_return, {}));
|
|
insts.push_back(make(op_function_end, {}));
|
|
|
|
std::vector<std::uint32_t> words = {0x07230203u, 0x00010000u, 0, next, 0};
|
|
for (const Inst& i : insts) words.insert(words.end(), i.begin(), i.end());
|
|
return words;
|
|
}
|
|
|
|
std::vector<std::uint32_t> make_noop_cs() {
|
|
constexpr std::uint32_t op_capability = 17, op_memory_model = 14, op_type_function = 33, op_return = 253;
|
|
constexpr std::uint32_t model_glcompute = 5, mode_local_size = 17;
|
|
std::vector<Inst> insts;
|
|
insts.push_back(make(op_capability, {1}));
|
|
insts.push_back(make(op_memory_model, {0, 1}));
|
|
Inst ep = make(op_entry_point, {model_glcompute, 1});
|
|
append_str(ep, "main");
|
|
fix_length(ep);
|
|
insts.push_back(std::move(ep));
|
|
insts.push_back(make(op_execution_mode, {1, mode_local_size, 1, 1, 1}));
|
|
insts.push_back(make(op_type_void, {2}));
|
|
insts.push_back(make(op_type_function, {3, 2}));
|
|
insts.push_back(make(op_function, {2, 1, 0, 3}));
|
|
insts.push_back(make(op_label, {4}));
|
|
insts.push_back(make(op_return, {}));
|
|
insts.push_back(make(op_function_end, {}));
|
|
std::vector<std::uint32_t> words = {0x07230203u, 0x00010000u, 0, 5, 0};
|
|
for (const Inst& i : insts) words.insert(words.end(), i.begin(), i.end());
|
|
return words;
|
|
}
|
|
|
|
bool write_spv(const std::filesystem::path& path, const std::vector<std::uint32_t>& words) {
|
|
std::ofstream f(path, std::ios::binary | std::ios::trunc);
|
|
if (!f) return false;
|
|
f.write(reinterpret_cast<const char*>(words.data()),
|
|
static_cast<std::streamsize>(words.size() * sizeof(std::uint32_t)));
|
|
return bool(f);
|
|
}
|
|
|
|
std::uint64_t quad_to_xbox_mask(std::uint8_t quad) {
|
|
return (std::uint64_t((quad >> 0) & 1u) << 0) |
|
|
(std::uint64_t((quad >> 1) & 1u) << 16) |
|
|
(std::uint64_t((quad >> 2) & 1u) << 32) |
|
|
(std::uint64_t((quad >> 3) & 1u) << 48);
|
|
}
|
|
|
|
D3D12_RESOURCE_DESC tex2d_desc(DXGI_FORMAT fmt, D3D12_RESOURCE_FLAGS flags) {
|
|
D3D12_RESOURCE_DESC d{};
|
|
d.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
|
|
d.Width = kSide;
|
|
d.Height = kSide;
|
|
d.DepthOrArraySize = 1;
|
|
d.MipLevels = 1;
|
|
d.Format = fmt;
|
|
d.SampleDesc = {1, 0};
|
|
d.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
|
|
d.Flags = flags;
|
|
return d;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
vktest::Device d;
|
|
const auto status = d.initialize();
|
|
if (status == vktest::DeviceStatus::unavailable) {
|
|
std::printf("d3d12_sample_mask_test: SKIP (no Vulkan 1.3 device)\n");
|
|
return 77;
|
|
}
|
|
EXPECT(status == vktest::DeviceStatus::ready);
|
|
|
|
const auto vs_prog = fssm_program({0x534D0001u, 0x00000001u});
|
|
const auto ps_nofc_prog = fssm_program({0x534D0002u, 0x00000002u});
|
|
const auto ps_fc_prog = fssm_program({0x534D0003u, 0x00000003u});
|
|
const auto cs_prog = fssm_program({0x534D0004u, 0x00000004u});
|
|
const auto vs_dxbc = shader_container(vs_prog);
|
|
const auto ps_nofc_dxbc = shader_container(ps_nofc_prog);
|
|
const auto ps_fc_dxbc = shader_container(ps_fc_prog);
|
|
const auto cs_dxbc = shader_container(cs_prog);
|
|
|
|
const testfiles::ScratchDir scratch("leonida_d3d12_sample_mask_test");
|
|
const auto& lib_dir = scratch.path();
|
|
EXPECT(write_spv(lib_dir / "vs.spv", make_vs()));
|
|
EXPECT(write_spv(lib_dir / "ps_nofc.spv", make_ps(false)));
|
|
EXPECT(write_spv(lib_dir / "ps_fc.spv", make_ps(true)));
|
|
EXPECT(write_spv(lib_dir / "cs.spv", make_noop_cs()));
|
|
{
|
|
std::ofstream idx(lib_dir / "index.json", std::ios::binary);
|
|
idx << "{\"vs\": {\"stage\": \"vs\", \"fssm_sha256\": \"" << sha256_hex(vs_prog)
|
|
<< "\", \"bindings\": [], \"push_constant_bytes\": 144, \"input_signature\": []},\n"
|
|
" \"ps_nofc\": {\"stage\": \"ps\", \"fssm_sha256\": \"" << sha256_hex(ps_nofc_prog)
|
|
<< "\", \"bindings\": [], \"push_constant_bytes\": 144, \"outputs\": [0]},\n"
|
|
" \"ps_fc\": {\"stage\": \"ps\", \"fssm_sha256\": \"" << sha256_hex(ps_fc_prog)
|
|
<< "\", \"bindings\": [], \"push_constant_bytes\": 144, \"outputs\": [0]},\n"
|
|
" \"cs\": {\"stage\": \"cs\", \"fssm_sha256\": \"" << sha256_hex(cs_prog)
|
|
<< "\", \"bindings\": [], \"push_constant_bytes\": 144, \"local_size\": [1, 1, 1], \"wave64\": false}}";
|
|
}
|
|
|
|
rt::d3d12::VulkanDeviceInfo info{};
|
|
info.get_instance_proc = d.instance_proc;
|
|
info.instance = d.instance;
|
|
info.physical = d.physical;
|
|
info.device = d.device;
|
|
info.queue = d.queue;
|
|
info.queue_family = d.family;
|
|
info.buffer_device_address = true;
|
|
info.shader_dir = lib_dir.string();
|
|
|
|
Ref<ID3D12Device> device;
|
|
EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&device.p))));
|
|
if (rt::d3d12::device_max_push_constants_size(device.p) < kPixelMaskPushBytes) {
|
|
std::printf("d3d12_sample_mask_test: SKIP (maxPushConstantsSize < %u)\n", kPixelMaskPushBytes);
|
|
return 77;
|
|
}
|
|
|
|
const auto rs_blob = make_root_sig_5_constants();
|
|
Ref<ID3D12RootSignature> rs;
|
|
EXPECT(SUCCEEDED(device->CreateRootSignature(0, rs_blob.data(), rs_blob.size(), IID_PPV_ARGS(&rs.p))));
|
|
|
|
auto make_gfx_pso = [&](const std::vector<std::uint8_t>* ps_blob, Ref<ID3D12PipelineState>& out) -> bool {
|
|
D3D12_GRAPHICS_PIPELINE_STATE_DESC desc{};
|
|
desc.pRootSignature = rs.p;
|
|
desc.VS = {vs_dxbc.data(), vs_dxbc.size()};
|
|
if (ps_blob) desc.PS = {ps_blob->data(), ps_blob->size()};
|
|
desc.BlendState.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL;
|
|
desc.SampleMask = UINT_MAX;
|
|
desc.RasterizerState.FillMode = D3D12_FILL_MODE_SOLID;
|
|
desc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE;
|
|
desc.RasterizerState.DepthClipEnable = TRUE;
|
|
desc.DepthStencilState.DepthEnable = TRUE;
|
|
desc.DepthStencilState.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
|
|
desc.DepthStencilState.DepthFunc = D3D12_COMPARISON_FUNC_LESS;
|
|
desc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
|
|
if (ps_blob) {
|
|
desc.NumRenderTargets = 1;
|
|
desc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
}
|
|
desc.DSVFormat = DXGI_FORMAT_D32_FLOAT;
|
|
desc.SampleDesc = {1, 0};
|
|
return SUCCEEDED(device->CreateGraphicsPipelineState(&desc, IID_PPV_ARGS(&out.p)));
|
|
};
|
|
|
|
Ref<ID3D12PipelineState> pso_nofc, pso_fc, pso_depth_only, pso_cs;
|
|
EXPECT(make_gfx_pso(&ps_nofc_dxbc, pso_nofc));
|
|
EXPECT(make_gfx_pso(&ps_fc_dxbc, pso_fc));
|
|
EXPECT(make_gfx_pso(nullptr, pso_depth_only));
|
|
{
|
|
D3D12_COMPUTE_PIPELINE_STATE_DESC cdesc{};
|
|
cdesc.pRootSignature = rs.p;
|
|
cdesc.CS = {cs_dxbc.data(), cs_dxbc.size()};
|
|
EXPECT(SUCCEEDED(device->CreateComputePipelineState(&cdesc, IID_PPV_ARGS(&pso_cs.p))));
|
|
}
|
|
|
|
D3D12_COMMAND_QUEUE_DESC qdesc{};
|
|
qdesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
|
|
Ref<ID3D12CommandQueue> queue;
|
|
EXPECT(SUCCEEDED(device->CreateCommandQueue(&qdesc, IID_PPV_ARGS(&queue.p))));
|
|
Ref<ID3D12CommandAllocator> alloc;
|
|
EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&alloc.p))));
|
|
Ref<ID3D12GraphicsCommandList> list;
|
|
EXPECT(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, alloc.p, nullptr,
|
|
IID_PPV_ARGS(&list.p))));
|
|
EXPECT(SUCCEEDED(list->Close()));
|
|
Ref<ID3D12Fence> fence;
|
|
EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p))));
|
|
UINT64 fence_val = 0;
|
|
|
|
// Create color + depth targets and readback buffers.
|
|
D3D12_HEAP_PROPERTIES def_heap{};
|
|
def_heap.Type = D3D12_HEAP_TYPE_DEFAULT;
|
|
D3D12_HEAP_PROPERTIES rb_heap{};
|
|
rb_heap.Type = D3D12_HEAP_TYPE_READBACK;
|
|
const auto color_desc = tex2d_desc(DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
|
|
const auto depth_desc = tex2d_desc(DXGI_FORMAT_D32_FLOAT, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL);
|
|
D3D12_CLEAR_VALUE color_clear{};
|
|
color_clear.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
color_clear.Color[3] = 1.0f;
|
|
D3D12_CLEAR_VALUE depth_clear{};
|
|
depth_clear.Format = DXGI_FORMAT_D32_FLOAT;
|
|
depth_clear.DepthStencil.Depth = 1.0f;
|
|
|
|
Ref<ID3D12Resource> color_tex, depth_tex, color_rb, depth_rb;
|
|
EXPECT(SUCCEEDED(device->CreateCommittedResource(&def_heap, D3D12_HEAP_FLAG_NONE, &color_desc,
|
|
D3D12_RESOURCE_STATE_RENDER_TARGET, &color_clear,
|
|
IID_PPV_ARGS(&color_tex.p))));
|
|
EXPECT(SUCCEEDED(device->CreateCommittedResource(&def_heap, D3D12_HEAP_FLAG_NONE, &depth_desc,
|
|
D3D12_RESOURCE_STATE_DEPTH_WRITE, &depth_clear,
|
|
IID_PPV_ARGS(&depth_tex.p))));
|
|
|
|
D3D12_PLACED_SUBRESOURCE_FOOTPRINT color_fp{}, depth_fp{};
|
|
UINT64 color_rb_bytes = 0, depth_rb_bytes = 0;
|
|
device->GetCopyableFootprints(&color_desc, 0, 1, 0, &color_fp, nullptr, nullptr, &color_rb_bytes);
|
|
device->GetCopyableFootprints(&depth_desc, 0, 1, 0, &depth_fp, nullptr, nullptr, &depth_rb_bytes);
|
|
const auto crb_desc = buffer_desc(color_rb_bytes);
|
|
const auto drb_desc = buffer_desc(depth_rb_bytes);
|
|
EXPECT(SUCCEEDED(device->CreateCommittedResource(&rb_heap, D3D12_HEAP_FLAG_NONE, &crb_desc,
|
|
D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
|
|
IID_PPV_ARGS(&color_rb.p))));
|
|
EXPECT(SUCCEEDED(device->CreateCommittedResource(&rb_heap, D3D12_HEAP_FLAG_NONE, &drb_desc,
|
|
D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
|
|
IID_PPV_ARGS(&depth_rb.p))));
|
|
|
|
D3D12_DESCRIPTOR_HEAP_DESC rtv_hdesc{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0};
|
|
D3D12_DESCRIPTOR_HEAP_DESC dsv_hdesc{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0};
|
|
Ref<ID3D12DescriptorHeap> rtv_heap, dsv_heap;
|
|
EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&rtv_hdesc, IID_PPV_ARGS(&rtv_heap.p))));
|
|
EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&dsv_hdesc, IID_PPV_ARGS(&dsv_heap.p))));
|
|
const D3D12_CPU_DESCRIPTOR_HANDLE rtv = rtv_heap->GetCPUDescriptorHandleForHeapStart();
|
|
const D3D12_CPU_DESCRIPTOR_HANDLE dsv = dsv_heap->GetCPUDescriptorHandleForHeapStart();
|
|
device->CreateRenderTargetView(color_tex.p, nullptr, rtv);
|
|
device->CreateDepthStencilView(depth_tex.p, nullptr, dsv);
|
|
|
|
auto exec_and_readback = [&](std::vector<std::uint8_t>& out_rgba, std::vector<float>& out_depth) -> bool {
|
|
const D3D12_RESOURCE_BARRIER to_src[2] = {
|
|
transition(color_tex.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE),
|
|
transition(depth_tex.p, D3D12_RESOURCE_STATE_DEPTH_WRITE, D3D12_RESOURCE_STATE_COPY_SOURCE),
|
|
};
|
|
list->ResourceBarrier(2, to_src);
|
|
D3D12_TEXTURE_COPY_LOCATION src_c{};
|
|
src_c.pResource = color_tex.p;
|
|
src_c.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
|
|
src_c.SubresourceIndex = 0;
|
|
D3D12_TEXTURE_COPY_LOCATION dst_c{};
|
|
dst_c.pResource = color_rb.p;
|
|
dst_c.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
|
|
dst_c.PlacedFootprint = color_fp;
|
|
list->CopyTextureRegion(&dst_c, 0, 0, 0, &src_c, nullptr);
|
|
D3D12_TEXTURE_COPY_LOCATION src_d{};
|
|
src_d.pResource = depth_tex.p;
|
|
src_d.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
|
|
src_d.SubresourceIndex = 0;
|
|
D3D12_TEXTURE_COPY_LOCATION dst_d{};
|
|
dst_d.pResource = depth_rb.p;
|
|
dst_d.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
|
|
dst_d.PlacedFootprint = depth_fp;
|
|
list->CopyTextureRegion(&dst_d, 0, 0, 0, &src_d, nullptr);
|
|
const D3D12_RESOURCE_BARRIER to_rt[2] = {
|
|
transition(color_tex.p, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET),
|
|
transition(depth_tex.p, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_DEPTH_WRITE),
|
|
};
|
|
list->ResourceBarrier(2, to_rt);
|
|
if (FAILED(list->Close())) return false;
|
|
ID3D12CommandList* lists[1] = {list.p};
|
|
queue->ExecuteCommandLists(1, lists);
|
|
++fence_val;
|
|
if (FAILED(queue->Signal(fence.p, fence_val))) return false;
|
|
if (FAILED(fence->SetEventOnCompletion(fence_val, nullptr))) return false;
|
|
|
|
void* cptr = nullptr;
|
|
void* dptr = nullptr;
|
|
if (FAILED(color_rb->Map(0, nullptr, &cptr)) || FAILED(depth_rb->Map(0, nullptr, &dptr))) return false;
|
|
out_rgba.resize(kSide * kSide * 4);
|
|
out_depth.resize(kSide * kSide);
|
|
for (UINT y = 0; y < kSide; ++y) {
|
|
std::memcpy(&out_rgba[y * kSide * 4],
|
|
static_cast<const std::uint8_t*>(cptr) + y * color_fp.Footprint.RowPitch, kSide * 4);
|
|
std::memcpy(&out_depth[y * kSide],
|
|
static_cast<const std::uint8_t*>(dptr) + y * depth_fp.Footprint.RowPitch, kSide * 4);
|
|
}
|
|
color_rb->Unmap(0, nullptr);
|
|
depth_rb->Unmap(0, nullptr);
|
|
return true;
|
|
};
|
|
|
|
const D3D12_VIEWPORT vp{0.0f, 0.0f, float(kSide), float(kSide), 0.0f, 1.0f};
|
|
const D3D12_RECT sc{0, 0, LONG(kSide), LONG(kSide)};
|
|
const float clear_rgba[4] = {0.0f, 0.0f, 0.0f, 1.0f};
|
|
|
|
// 1. State lifecycle checks on GraphicsCommandList:
|
|
// - Reset() resets sample mask to 0xF.
|
|
// - OMSetSampleMaskX sets quad_pixels(mask).
|
|
// - SetPipelineState(compute) PRESERVES sample mask!
|
|
// - SetPipelineState(graphics) RESETS sample mask to 0xF!
|
|
{
|
|
EXPECT(SUCCEEDED(alloc->Reset()));
|
|
EXPECT(SUCCEEDED(list->Reset(alloc.p, pso_nofc.p)));
|
|
EXPECT(rt::d3d12::current_sample_mask(list.p) == 0xFu);
|
|
EXPECT(rt::d3d12::record_sample_mask(list.p, quad_to_xbox_mask(0x5u)));
|
|
EXPECT(rt::d3d12::current_sample_mask(list.p) == 0x5u);
|
|
list->SetPipelineState(pso_cs.p); // compute PSO must preserve graphics sample mask!
|
|
EXPECT(rt::d3d12::current_sample_mask(list.p) == 0x5u);
|
|
list->SetPipelineState(pso_fc.p); // graphics PSO must reset sample mask to 0xF!
|
|
EXPECT(rt::d3d12::current_sample_mask(list.p) == 0xFu);
|
|
EXPECT(rt::d3d12::record_sample_mask(list.p, quad_to_xbox_mask(0x1u)));
|
|
EXPECT(rt::d3d12::current_sample_mask(list.p) == 0x1u);
|
|
EXPECT(SUCCEEDED(list->Close()));
|
|
EXPECT(SUCCEEDED(list->Reset(alloc.p, nullptr)));
|
|
EXPECT(rt::d3d12::current_sample_mask(list.p) == 0xFu); // Reset resets mask to 0xF!
|
|
EXPECT(SUCCEEDED(list->Close()));
|
|
}
|
|
|
|
// 2. Real GPU rendering across masks {0x5, 0xA, 0x1, 0x0, 0xF} for both PS without FragCoord and PS with FragCoord.
|
|
for (ID3D12PipelineState* pso : {pso_nofc.p, pso_fc.p}) {
|
|
for (const std::uint8_t mask : {std::uint8_t(0x5), std::uint8_t(0xA), std::uint8_t(0x1),
|
|
std::uint8_t(0x0), std::uint8_t(0xF)}) {
|
|
EXPECT(SUCCEEDED(alloc->Reset()));
|
|
EXPECT(SUCCEEDED(list->Reset(alloc.p, pso)));
|
|
list->SetGraphicsRootSignature(rs.p);
|
|
const std::uint32_t cvals[5] = {
|
|
std::bit_cast<std::uint32_t>(0.25f),
|
|
std::bit_cast<std::uint32_t>(0.0f),
|
|
std::bit_cast<std::uint32_t>(1.0f),
|
|
std::bit_cast<std::uint32_t>(0.0f),
|
|
std::bit_cast<std::uint32_t>(1.0f),
|
|
};
|
|
list->SetGraphicsRoot32BitConstants(0, 5, cvals, 0);
|
|
list->RSSetViewports(1, &vp);
|
|
list->RSSetScissorRects(1, &sc);
|
|
list->OMSetRenderTargets(1, &rtv, FALSE, &dsv);
|
|
list->ClearRenderTargetView(rtv, clear_rgba, 0, nullptr);
|
|
list->ClearDepthStencilView(dsv, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);
|
|
list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
|
EXPECT(rt::d3d12::record_sample_mask(list.p, quad_to_xbox_mask(mask)));
|
|
list->DrawInstanced(3, 1, 0, 0);
|
|
|
|
std::vector<std::uint8_t> rgba;
|
|
std::vector<float> depth;
|
|
EXPECT(exec_and_readback(rgba, depth));
|
|
for (UINT y = 0; y < kSide; ++y) {
|
|
for (UINT x = 0; x < kSide; ++x) {
|
|
const std::size_t idx = y * kSide + x;
|
|
const bool kept = ((mask >> ((x & 1u) | ((y & 1u) << 1))) & 1u) != 0u;
|
|
if (kept) {
|
|
EXPECT(rgba[idx * 4 + 0] == 0 && rgba[idx * 4 + 1] == 255 &&
|
|
rgba[idx * 4 + 2] == 0 && rgba[idx * 4 + 3] == 255);
|
|
EXPECT(std::fabs(depth[idx] - 0.25f) < 1e-5f);
|
|
} else {
|
|
EXPECT(rgba[idx * 4 + 0] == 0 && rgba[idx * 4 + 1] == 0 &&
|
|
rgba[idx * 4 + 2] == 0 && rgba[idx * 4 + 3] == 255);
|
|
EXPECT(std::fabs(depth[idx] - 1.0f) < 1e-5f);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 3. GPU proof that SetPipelineState(graphics) resets sample mask to 0xF:
|
|
// Set mask 0x1, then SetPipelineState(pso_fc) -> draw must cover ALL pixels!
|
|
{
|
|
EXPECT(SUCCEEDED(alloc->Reset()));
|
|
EXPECT(SUCCEEDED(list->Reset(alloc.p, pso_nofc.p)));
|
|
list->SetGraphicsRootSignature(rs.p);
|
|
const std::uint32_t cvals[5] = {
|
|
std::bit_cast<std::uint32_t>(0.25f),
|
|
std::bit_cast<std::uint32_t>(1.0f),
|
|
std::bit_cast<std::uint32_t>(0.0f),
|
|
std::bit_cast<std::uint32_t>(0.0f),
|
|
std::bit_cast<std::uint32_t>(1.0f),
|
|
};
|
|
list->SetGraphicsRoot32BitConstants(0, 5, cvals, 0);
|
|
list->RSSetViewports(1, &vp);
|
|
list->RSSetScissorRects(1, &sc);
|
|
list->OMSetRenderTargets(1, &rtv, FALSE, &dsv);
|
|
list->ClearRenderTargetView(rtv, clear_rgba, 0, nullptr);
|
|
list->ClearDepthStencilView(dsv, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);
|
|
list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
|
EXPECT(rt::d3d12::record_sample_mask(list.p, quad_to_xbox_mask(0x1u)));
|
|
list->SetPipelineState(pso_fc.p); // resets mask back to 0xF!
|
|
list->DrawInstanced(3, 1, 0, 0);
|
|
|
|
std::vector<std::uint8_t> rgba;
|
|
std::vector<float> depth;
|
|
EXPECT(exec_and_readback(rgba, depth));
|
|
for (UINT i = 0; i < kSide * kSide; ++i) {
|
|
EXPECT(rgba[i * 4 + 0] == 255 && rgba[i * 4 + 1] == 0 && rgba[i * 4 + 2] == 0);
|
|
EXPECT(std::fabs(depth[i] - 0.25f) < 1e-5f);
|
|
}
|
|
}
|
|
|
|
// 4. Depth-only PSO (no PS): mask 0x1 is a safe no-op, writes depth to all pixels.
|
|
{
|
|
EXPECT(SUCCEEDED(alloc->Reset()));
|
|
EXPECT(SUCCEEDED(list->Reset(alloc.p, pso_depth_only.p)));
|
|
list->SetGraphicsRootSignature(rs.p);
|
|
const std::uint32_t cvals[5] = {std::bit_cast<std::uint32_t>(0.25f), 0, 0, 0, 0};
|
|
list->SetGraphicsRoot32BitConstants(0, 5, cvals, 0);
|
|
list->RSSetViewports(1, &vp);
|
|
list->RSSetScissorRects(1, &sc);
|
|
list->OMSetRenderTargets(0, nullptr, FALSE, &dsv);
|
|
list->ClearDepthStencilView(dsv, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);
|
|
list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
|
EXPECT(rt::d3d12::record_sample_mask(list.p, quad_to_xbox_mask(0x1u)));
|
|
list->DrawInstanced(3, 1, 0, 0);
|
|
|
|
std::vector<std::uint8_t> rgba;
|
|
std::vector<float> depth;
|
|
EXPECT(exec_and_readback(rgba, depth));
|
|
for (UINT i = 0; i < kSide * kSide; ++i) EXPECT(std::fabs(depth[i] - 0.25f) < 1e-5f);
|
|
}
|
|
|
|
EXPECT(d.validation_errors() == 0);
|
|
std::printf("d3d12_sample_mask_test: PASS (validation_errors=0)\n");
|
|
return 0;
|
|
}
|