Files
mandy/runtime/test/vk_sample_mask_test.cpp

946 lines
48 KiB
C++

// Real Vulkan GPU rendering test for OMSetSampleMaskX (slot 142, 0x470):
// - Exercises sample_mask_patch_fragment() + VulkanContext/vulkan_replay.h on-demand masked twin
// pipelines (148-byte masked_layout) and plain pipelines (144-byte layout).
// - Tests BOTH a fragment shader WITH EarlyFragmentTests & WITHOUT FragCoord AND one WITH
// EarlyFragmentTests & WITH FragCoord.
// - Draws quads under 2x2 quad masks 0x5, 0xA, 0x1, 0x0, 0xF and verifies EXACT per-pixel
// color AND depth readback (confirming discarded fragments write NEITHER color NOR depth!).
// - Tests complementary 2-pass LOD fade (0x5 + 0xA in one command list), depth-only PSO no-op,
// and single masked-twin caching (only 1 twin VkPipeline built across all masks 0..14).
// - Verifies 0 Khronos Validation Layer errors when LEONIDA_VK_VALIDATION=1.
// Exits 77 without a Vulkan 1.3 / dynamicRendering device. No game data.
#include "vk_test_device.h"
#include "../src/sample_mask_fragment.h"
#include "../src/vulkan_backend.h"
#include <array>
#include <bit>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <string>
#include <unordered_map>
#include <vector>
#define EXPECT(cond) \
do { \
if (!(cond)) { \
std::fprintf(stderr, "vk_sample_mask_test: line %d failed: %s\n", __LINE__, #cond); \
return 1; \
} \
} while (0)
namespace {
using namespace leonida;
using namespace leonida::spirv_sample_mask;
constexpr std::uint32_t kWidth = 16;
constexpr std::uint32_t kHeight = 16;
constexpr std::uint32_t kRowPitch = 256; // D3D12_TEXTURE_DATA_PITCH_ALIGNMENT
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);
}
// Fullscreen triangle vertex shader (3 vertices, no vertex buffers):
// x = float((VertexIndex & 1) << 2) - 1.0
// y = float((VertexIndex & 2) << 1) - 1.0
// z = bitcast<float>(user_data.s[0])
// gl_Position = vec4(x, y, z, 1.0)
std::vector<std::uint32_t> make_fullscreen_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_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_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_c0}));
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;
}
// Fragment shader with EarlyFragmentTests (critical: must be stripped by patcher so discarded
// pixels do not write depth!), optional FragCoord input, and 144-byte PushConstant UserData
// reading RGBA color from user_data.s[1..4].
std::vector<std::uint32_t> make_color_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, 1}));
insts.push_back(make(op_constant, {id_u32, id_c2, 2}));
insts.push_back(make(op_constant, {id_u32, id_c3, 3}));
insts.push_back(make(op_constant, {id_u32, id_c4, 4}));
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;
}
// Fragment shader that reads vec4 color from descriptor set 0, binding 0 (Uniform buffer)
// plus EarlyFragmentTests + 144-byte PushConstant UserData. Used to exercise descriptor-set
// cache invalidation across incompatible 144B vs 148B pipeline layouts under VVL.
std::vector<std::uint32_t> make_ubo_color_ps() {
constexpr std::uint32_t op_capability = 17, op_memory_model = 14, op_type_array = 28, op_type_function = 33,
op_store = 62, op_return = 253;
constexpr std::uint32_t mode_origin_upper_left = 7, storage_uniform = 2, storage_output = 3;
constexpr std::uint32_t dec_location = 30, dec_array_stride = 6, dec_binding = 33, dec_set = 34;
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();
const std::uint32_t id_arr = id(), id_pc_struct = id(), id_ptr_pc = id(), id_pc_var = id();
const std::uint32_t id_ubo_struct = id(), id_ptr_ubo = id(), id_ptr_ubo_v4f = id(), id_ubo_var = id();
const std::uint32_t id_ptr_out = id(), id_out_color = id();
const std::uint32_t id_l0 = id(), id_col_ptr = id(), id_col = 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);
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}));
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_decorate, {id_ubo_struct, decoration_block}));
insts.push_back(make(op_member_decorate, {id_ubo_struct, 0, decoration_offset, 0}));
insts.push_back(make(op_decorate, {id_ubo_var, dec_set, 0}));
insts.push_back(make(op_decorate, {id_ubo_var, dec_binding, 0}));
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_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_struct, {id_ubo_struct, id_v4f}));
insts.push_back(make(op_type_pointer, {id_ptr_ubo, storage_uniform, id_ubo_struct}));
insts.push_back(make(op_type_pointer, {id_ptr_ubo_v4f, storage_uniform, id_v4f}));
insts.push_back(make(op_type_pointer, {id_ptr_out, 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_ubo, id_ubo_var, storage_uniform}));
insts.push_back(make(op_variable, {id_ptr_out, id_out_color, storage_output}));
insts.push_back(make(op_function, {id_void, id_main, 0, id_fn}));
insts.push_back(make(op_label, {id_l0}));
insts.push_back(make(op_access_chain, {id_ptr_ubo_v4f, id_col_ptr, id_ubo_var, id_c0}));
insts.push_back(make(op_load, {id_v4f, id_col, id_col_ptr}));
insts.push_back(make(op_store, {id_out_color, id_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;
}
struct PipelineHolder {
VkDevice dev = VK_NULL_HANDLE;
PFN_vkCreateShaderModule create_mod = nullptr;
PFN_vkDestroyShaderModule destroy_mod = nullptr;
PFN_vkCreatePipelineLayout create_layout = nullptr;
PFN_vkDestroyPipelineLayout destroy_layout = nullptr;
PFN_vkCreateGraphicsPipelines create_pipe = nullptr;
PFN_vkDestroyPipeline destroy_pipe = nullptr;
VkShaderModule vs_mod = VK_NULL_HANDLE;
VkShaderModule ps_mod = VK_NULL_HANDLE;
VkShaderModule masked_ps_mod = VK_NULL_HANDLE;
std::vector<std::uint32_t> ps_words;
VkPipelineLayout plain_layout = VK_NULL_HANDLE;
VkPipelineLayout masked_layout = VK_NULL_HANDLE;
VkPipeline plain_pipeline = VK_NULL_HANDLE;
std::unordered_map<std::uint32_t, VkPipeline> variants;
unsigned masked_builds = 0;
bool has_color = true;
~PipelineHolder() {
for (auto& [k, p] : variants)
if (p) destroy_pipe(dev, p, nullptr);
if (plain_pipeline) destroy_pipe(dev, plain_pipeline, nullptr);
if (masked_layout) destroy_layout(dev, masked_layout, nullptr);
if (plain_layout) destroy_layout(dev, plain_layout, nullptr);
if (masked_ps_mod) destroy_mod(dev, masked_ps_mod, nullptr);
if (ps_mod) destroy_mod(dev, ps_mod, nullptr);
if (vs_mod) destroy_mod(dev, vs_mod, nullptr);
}
VkShaderModule make_module(const std::vector<std::uint32_t>& words) {
VkShaderModuleCreateInfo info{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO};
info.codeSize = words.size() * sizeof(std::uint32_t);
info.pCode = words.data();
VkShaderModule m = VK_NULL_HANDLE;
if (create_mod(dev, &info, nullptr, &m) != VK_SUCCESS) return VK_NULL_HANDLE;
return m;
}
VkPipeline build_pipeline(bool sample_masked) {
VkPipelineShaderStageCreateInfo stages[2]{};
std::uint32_t stage_count = 0;
stages[stage_count++] = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, nullptr, 0,
VK_SHADER_STAGE_VERTEX_BIT, vs_mod, "main", nullptr};
if (ps_mod) {
VkShaderModule frag = ps_mod;
if (sample_masked) {
if (!masked_ps_mod) {
std::string why;
const auto patched = sample_mask_patch_fragment(ps_words, why);
if (patched.empty()) {
std::fprintf(stderr, "sample_mask_patch_fragment failed: %s\n", why.c_str());
return VK_NULL_HANDLE;
}
masked_ps_mod = make_module(patched);
if (!masked_ps_mod) return VK_NULL_HANDLE;
}
frag = masked_ps_mod;
}
stages[stage_count++] = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, nullptr, 0,
VK_SHADER_STAGE_FRAGMENT_BIT, frag, "main", nullptr};
}
const VkFormat color_fmt = VK_FORMAT_R8G8B8A8_UNORM;
VkPipelineRenderingCreateInfo rendering{VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO};
rendering.colorAttachmentCount = has_color ? 1u : 0u;
rendering.pColorAttachmentFormats = has_color ? &color_fmt : nullptr;
rendering.depthAttachmentFormat = VK_FORMAT_D32_SFLOAT;
VkPipelineVertexInputStateCreateInfo vi{VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO};
VkPipelineInputAssemblyStateCreateInfo ia{VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO};
ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
VkPipelineViewportStateCreateInfo vp{VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO};
vp.viewportCount = 1;
vp.scissorCount = 1;
VkPipelineRasterizationStateCreateInfo rs{VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO};
rs.polygonMode = VK_POLYGON_MODE_FILL;
rs.cullMode = VK_CULL_MODE_NONE;
rs.frontFace = VK_FRONT_FACE_CLOCKWISE;
rs.lineWidth = 1.0f;
VkPipelineMultisampleStateCreateInfo ms{VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO};
ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
VkPipelineDepthStencilStateCreateInfo ds{VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO};
ds.depthTestEnable = VK_TRUE;
ds.depthWriteEnable = VK_TRUE;
ds.depthCompareOp = VK_COMPARE_OP_LESS;
VkPipelineColorBlendAttachmentState att{};
att.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
VkPipelineColorBlendStateCreateInfo cb{VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO};
cb.attachmentCount = has_color ? 1u : 0u;
cb.pAttachments = has_color ? &att : nullptr;
const VkDynamicState dyn[] = {
VK_DYNAMIC_STATE_VIEWPORT,
VK_DYNAMIC_STATE_SCISSOR,
VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY,
VK_DYNAMIC_STATE_PRIMITIVE_RESTART_ENABLE,
VK_DYNAMIC_STATE_STENCIL_REFERENCE,
VK_DYNAMIC_STATE_BLEND_CONSTANTS,
};
VkPipelineDynamicStateCreateInfo dy{VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO};
dy.dynamicStateCount = static_cast<std::uint32_t>(std::size(dyn));
dy.pDynamicStates = dyn;
VkGraphicsPipelineCreateInfo info{VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, &rendering};
info.stageCount = stage_count;
info.pStages = stages;
info.pVertexInputState = &vi;
info.pInputAssemblyState = &ia;
info.pViewportState = &vp;
info.pRasterizationState = &rs;
info.pMultisampleState = &ms;
info.pDepthStencilState = &ds;
info.pColorBlendState = &cb;
info.pDynamicState = &dy;
info.layout = sample_masked ? masked_layout : plain_layout;
VkPipeline p = VK_NULL_HANDLE;
if (create_pipe(dev, VK_NULL_HANDLE, 1, &info, nullptr, &p) != VK_SUCCESS) return VK_NULL_HANDLE;
if (sample_masked) ++masked_builds;
return p;
}
bool init(vktest::Device& d, const std::vector<std::uint32_t>& vs_words,
const std::vector<std::uint32_t>& frag_words, bool color_target, GraphicsPipeline& out,
VkDescriptorSetLayout set0_layout = VK_NULL_HANDLE, VkDescriptorSet set0 = VK_NULL_HANDLE,
bool fail_masked_variant = false) {
dev = d.device;
create_mod = d.fn<PFN_vkCreateShaderModule>("vkCreateShaderModule");
destroy_mod = d.fn<PFN_vkDestroyShaderModule>("vkDestroyShaderModule");
create_layout = d.fn<PFN_vkCreatePipelineLayout>("vkCreatePipelineLayout");
destroy_layout = d.fn<PFN_vkDestroyPipelineLayout>("vkDestroyPipelineLayout");
create_pipe = d.fn<PFN_vkCreateGraphicsPipelines>("vkCreateGraphicsPipelines");
destroy_pipe = d.fn<PFN_vkDestroyPipeline>("vkDestroyPipeline");
has_color = color_target;
vs_mod = make_module(vs_words);
if (!vs_mod) return false;
if (!frag_words.empty()) {
ps_words = frag_words;
ps_mod = make_module(ps_words);
if (!ps_mod) return false;
}
const VkShaderStageFlags push_stages = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
const VkPushConstantRange plain_range{push_stages, 0, sizeof(ShaderUserData)};
VkPipelineLayoutCreateInfo li{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
li.pushConstantRangeCount = 1;
li.pPushConstantRanges = &plain_range;
if (set0_layout) {
li.setLayoutCount = 1;
li.pSetLayouts = &set0_layout;
}
if (create_layout(dev, &li, nullptr, &plain_layout) != VK_SUCCESS) return false;
if (ps_mod) {
const VkPushConstantRange masked_range{push_stages, 0, kPixelMaskPushBytes};
li.pPushConstantRanges = &masked_range;
if (create_layout(dev, &li, nullptr, &masked_layout) != VK_SUCCESS) return false;
}
plain_pipeline = build_pipeline(false);
if (!plain_pipeline) return false;
out = {};
out.pipeline = plain_pipeline;
out.layout = plain_layout;
out.masked_layout = masked_layout;
out.push_stages = push_stages;
out.bindless_set = set0;
if (has_color) out.color_formats = {VK_FORMAT_R8G8B8A8_UNORM};
out.depth_format = VK_FORMAT_D32_SFLOAT;
out.depth_aspects = VK_IMAGE_ASPECT_DEPTH_BIT;
out.samples = 1;
out.depth_writes = true;
out.stencil_writes = false;
out.variant = [this, fail_masked_variant](std::uint32_t flags) -> VkPipeline {
if (auto it = variants.find(flags); it != variants.end()) return it->second;
const bool masked = (flags & GraphicsPipeline::sample_masked) != 0;
if (masked && (!masked_layout || fail_masked_variant)) return VK_NULL_HANDLE;
VkPipeline p = build_pipeline(masked);
if (p) variants.emplace(flags, p);
return p;
};
out.built = [this](std::uint32_t flags) -> VkPipeline {
if (auto it = variants.find(flags); it != variants.end()) return it->second;
return VK_NULL_HANDLE;
};
return true;
}
};
std::array<std::uint32_t, 32> make_user_data(float z, float r, float g, float b, float a) {
std::array<std::uint32_t, 32> ud{};
ud[0] = std::bit_cast<std::uint32_t>(z);
ud[1] = std::bit_cast<std::uint32_t>(r);
ud[2] = std::bit_cast<std::uint32_t>(g);
ud[3] = std::bit_cast<std::uint32_t>(b);
ud[4] = std::bit_cast<std::uint32_t>(a);
return ud;
}
Draw make_fullscreen_draw(std::uintptr_t pipe_id, ResourceId color_id, ResourceId depth_id,
const std::array<std::uint32_t, 32>& ud, std::uint8_t pixel_mask) {
Draw d{};
d.pipeline = pipe_id;
d.user_data = ud;
auto views = std::make_shared<DrawViews>();
if (color_id) {
views->render_target_count = 1;
views->render_targets[0] = {color_id, 0, 0, 1};
}
d.views = std::move(views);
if (depth_id) d.depth = {depth_id, 0, 0, 1};
d.topology = 4; // D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST
d.viewport = {0.0f, 0.0f, float(kWidth), float(kHeight), 0.0f, 1.0f};
d.scissor = {0, 0, std::int32_t(kWidth), std::int32_t(kHeight)};
d.count = 3;
d.instances = 1;
d.pixel_mask = pixel_mask;
return d;
}
} // namespace
int main() {
vktest::Device d;
const vktest::DeviceStatus status = d.initialize();
if (status == vktest::DeviceStatus::unavailable) {
std::printf("vk_sample_mask_test: SKIP (no Vulkan 1.3 device)\n");
return 77;
}
EXPECT(status == vktest::DeviceStatus::ready);
// Check maxPushConstantsSize >= 148.
VkPhysicalDeviceProperties props{};
auto get_props = reinterpret_cast<PFN_vkGetPhysicalDeviceProperties>(
d.instance_proc(d.instance, "vkGetPhysicalDeviceProperties"));
EXPECT(get_props != nullptr);
get_props(d.physical, &props);
if (props.limits.maxPushConstantsSize < kPixelMaskPushBytes) {
std::printf("vk_sample_mask_test: SKIP (maxPushConstantsSize %u < %u)\n",
props.limits.maxPushConstantsSize, kPixelMaskPushBytes);
return 77;
}
VulkanContext ctx(d.device_proc, d.device, true);
EXPECT(ctx.loaded() && ctx.vk.graphics_complete());
VulkanQueue queue(ctx, d.queue, d.family);
EXPECT(queue.ok());
const VkImage color_img = d.image(VK_FORMAT_R8G8B8A8_UNORM, kWidth, kHeight, 1, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
const VkImage depth_img = d.image(VK_FORMAT_D32_SFLOAT, kWidth, kHeight, 1, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT);
EXPECT(color_img && depth_img);
void* color_rb_ptr = nullptr;
void* depth_rb_ptr = nullptr;
const VkDeviceSize rb_bytes = VkDeviceSize(kRowPitch) * kHeight;
const VkBuffer color_rb = d.buffer(rb_bytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT, &color_rb_ptr);
const VkBuffer depth_rb = d.buffer(rb_bytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT, &depth_rb_ptr);
EXPECT(color_rb && depth_rb && color_rb_ptr && depth_rb_ptr);
constexpr ResourceId kColorId = 1, kDepthId = 2, kColorRbId = 3, kDepthRbId = 4;
ResourceBinding rb_color{};
rb_color.image = color_img;
rb_color.format = VK_FORMAT_R8G8B8A8_UNORM;
rb_color.aspects = VK_IMAGE_ASPECT_COLOR_BIT;
rb_color.width = kWidth;
rb_color.height = kHeight;
rb_color.depth = 1;
rb_color.mip_levels = 1;
rb_color.array_layers = 1;
rb_color.samples = 1;
ctx.add_resource(kColorId, rb_color, VK_IMAGE_LAYOUT_UNDEFINED);
ResourceBinding rb_depth{};
rb_depth.image = depth_img;
rb_depth.format = VK_FORMAT_D32_SFLOAT;
rb_depth.aspects = VK_IMAGE_ASPECT_DEPTH_BIT;
rb_depth.width = kWidth;
rb_depth.height = kHeight;
rb_depth.depth = 1;
rb_depth.mip_levels = 1;
rb_depth.array_layers = 1;
rb_depth.samples = 1;
ctx.add_resource(kDepthId, rb_depth, VK_IMAGE_LAYOUT_UNDEFINED);
ResourceBinding rb_cbuf{};
rb_cbuf.buffer = color_rb;
rb_cbuf.size = rb_bytes;
rb_cbuf.host_readable = true;
ctx.add_resource(kColorRbId, rb_cbuf);
ResourceBinding rb_dbuf{};
rb_dbuf.buffer = depth_rb;
rb_dbuf.size = rb_bytes;
rb_dbuf.host_readable = true;
ctx.add_resource(kDepthRbId, rb_dbuf);
const auto vs_words = make_fullscreen_vs();
const auto ps_no_fc_words = make_color_ps(false);
const auto ps_with_fc_words = make_color_ps(true);
constexpr std::uintptr_t kPipeNoFc = 101, kPipeWithFc = 102, kPipeDepthOnly = 103;
PipelineHolder holder_no_fc, holder_with_fc, holder_depth_only;
GraphicsPipeline gp_no_fc, gp_with_fc, gp_depth_only;
EXPECT(holder_no_fc.init(d, vs_words, ps_no_fc_words, true, gp_no_fc));
EXPECT(holder_with_fc.init(d, vs_words, ps_with_fc_words, true, gp_with_fc));
EXPECT(holder_depth_only.init(d, vs_words, {}, false, gp_depth_only));
ctx.add_graphics_pipeline(kPipeNoFc, gp_no_fc);
ctx.add_graphics_pipeline(kPipeWithFc, gp_with_fc);
ctx.add_graphics_pipeline(kPipeDepthOnly, gp_depth_only);
// Helper: clear color=(0,0,0,1) & depth=1.0, execute draws, copy color+depth to readback buffers.
const auto run_draws = [&](const std::vector<Draw>& draws) -> bool {
CommandList list;
if (!list.transition({kColorId, all_subresources, d3d12_state::common, d3d12_state::render_target})) return false;
if (!list.transition({kDepthId, all_subresources, d3d12_state::common, d3d12_state::depth_write})) return false;
if (!list.clear_render_target({kColorId, 0, 0, 1}, {0.0f, 0.0f, 0.0f, 1.0f})) return false;
if (!list.clear_depth_stencil({kDepthId, 0, 0, 1}, clear_depth, 1.0f, 0)) return false;
for (const Draw& dr : draws) {
if (!list.draw(dr)) return false;
}
if (!list.transition({kColorId, all_subresources, d3d12_state::render_target, d3d12_state::copy_source})) return false;
if (!list.transition({kDepthId, all_subresources, d3d12_state::depth_write, d3d12_state::copy_source})) return false;
CopyTexture copy_c{};
copy_c.src = {kColorId, false, 0, 0, 0, 0, 0, 0};
copy_c.dst = {kColorRbId, true, 0, 0, kWidth, kHeight, 1, kRowPitch};
if (!list.copy_texture(copy_c)) return false;
CopyTexture copy_d{};
copy_d.src = {kDepthId, false, 0, 0, 0, 0, 0, 0};
copy_d.dst = {kDepthRbId, true, 0, 0, kWidth, kHeight, 1, kRowPitch};
if (!list.copy_texture(copy_d)) return false;
if (!list.transition({kColorId, all_subresources, d3d12_state::copy_source, d3d12_state::common})) return false;
if (!list.transition({kDepthId, all_subresources, d3d12_state::copy_source, d3d12_state::common})) return false;
if (!list.close()) return false;
const CommandList* ptr = &list;
const ReplayResult rr = queue.execute({&ptr, 1});
if (rr != ReplayResult::success) {
std::fprintf(stderr, "queue.execute failed with ReplayResult=%d\n", int(rr));
return false;
}
return queue.idle();
};
const auto pixel_rgba = [&](std::uint32_t x, std::uint32_t y) -> const std::uint8_t* {
return static_cast<const std::uint8_t*>(color_rb_ptr) + y * kRowPitch + x * 4;
};
const auto pixel_depth = [&](std::uint32_t x, std::uint32_t y) -> float {
float v = 0.0f;
std::memcpy(&v, static_cast<const std::uint8_t*>(depth_rb_ptr) + y * kRowPitch + x * 4, sizeof(float));
return v;
};
// 1. Test both fragment shaders (WITHOUT FragCoord + EarlyFragmentTests, AND WITH FragCoord + EarlyFragmentTests)
// across masks 0xF (plain first: 0 twins built!), then 0x5, 0xA, 0x1, 0x0, and back to 0xF.
const std::pair<std::uintptr_t, PipelineHolder*> pipes[] = {
{kPipeNoFc, &holder_no_fc},
{kPipeWithFc, &holder_with_fc},
};
for (const auto& [pipe_id, holder] : pipes) {
// First draw with 0xF -> plain pipeline, ZERO masked twins built!
EXPECT(run_draws({make_fullscreen_draw(pipe_id, kColorId, kDepthId,
make_user_data(0.25f, 1.0f, 0.0f, 0.0f, 1.0f), 0xFu)}));
EXPECT(holder->masked_builds == 0);
for (std::uint32_t y = 0; y < kHeight; ++y) {
for (std::uint32_t x = 0; x < kWidth; ++x) {
const auto* px = pixel_rgba(x, y);
EXPECT(px[0] == 255 && px[1] == 0 && px[2] == 0 && px[3] == 255);
EXPECT(std::fabs(pixel_depth(x, y) - 0.25f) < 1e-5f);
}
}
// Now test 0x5, 0xA, 0x1, 0x0, and 0xF again -> EXACTLY 1 masked twin built across all 4 masked draws!
const std::uint8_t masks[] = {0x5u, 0xAu, 0x1u, 0x0u, 0xFu};
for (const std::uint8_t mask : masks) {
EXPECT(run_draws({make_fullscreen_draw(pipe_id, kColorId, kDepthId,
make_user_data(0.25f, 0.0f, 1.0f, 0.0f, 1.0f), mask)}));
for (std::uint32_t y = 0; y < kHeight; ++y) {
for (std::uint32_t x = 0; x < kWidth; ++x) {
const auto* px = pixel_rgba(x, y);
const std::uint32_t bit = (x & 1u) | ((y & 1u) << 1);
const bool kept = ((mask >> bit) & 1u) != 0u;
if (kept) {
EXPECT(px[0] == 0 && px[1] == 255 && px[2] == 0 && px[3] == 255);
EXPECT(std::fabs(pixel_depth(x, y) - 0.25f) < 1e-5f);
} else {
// Discarded pixel MUST keep clear color AND clear depth (EarlyFragmentTests stripped)!
EXPECT(px[0] == 0 && px[1] == 0 && px[2] == 0 && px[3] == 255);
EXPECT(std::fabs(pixel_depth(x, y) - 1.0f) < 1e-5f);
}
}
}
}
EXPECT(holder->masked_builds == 1);
}
// 2. Complementary 2-pass LOD fade in a SINGLE command list (0x5 red @ z=0.25, 0xA green @ z=0.50).
// Also tests ReplayStateCache 4-byte delta push at offset 144 between consecutive masked draws!
{
std::vector<Draw> two_pass;
two_pass.push_back(make_fullscreen_draw(kPipeNoFc, kColorId, kDepthId,
make_user_data(0.25f, 1.0f, 0.0f, 0.0f, 1.0f), 0x5u));
two_pass.push_back(make_fullscreen_draw(kPipeNoFc, kColorId, kDepthId,
make_user_data(0.50f, 0.0f, 1.0f, 0.0f, 1.0f), 0xAu));
EXPECT(run_draws(two_pass));
for (std::uint32_t y = 0; y < kHeight; ++y) {
for (std::uint32_t x = 0; x < kWidth; ++x) {
const auto* px = pixel_rgba(x, y);
const bool even_x = (x & 1u) == 0u;
if (even_x) {
EXPECT(px[0] == 255 && px[1] == 0 && px[2] == 0);
EXPECT(std::fabs(pixel_depth(x, y) - 0.25f) < 1e-5f);
} else {
EXPECT(px[0] == 0 && px[1] == 255 && px[2] == 0);
EXPECT(std::fabs(pixel_depth(x, y) - 0.50f) < 1e-5f);
}
}
}
}
// 3. Depth-only PSO (no fragment shader -> masked_layout == VK_NULL_HANDLE): safe no-op under mask 0x1.
{
EXPECT(run_draws({make_fullscreen_draw(kPipeDepthOnly, 0, kDepthId,
make_user_data(0.25f, 0.0f, 0.0f, 0.0f, 0.0f), 0x1u)}));
for (std::uint32_t y = 0; y < kHeight; ++y) {
for (std::uint32_t x = 0; x < kWidth; ++x) {
EXPECT(std::fabs(pixel_depth(x, y) - 0.25f) < 1e-5f);
}
}
}
// 4. VVL regression test for ReplayStateCache descriptor-set invalidation across incompatible
// 144B vs 148B push-constant pipeline layouts (VUID-vkCmdDraw-None-08600):
// a) plain bindless (144B) -> masked non-bindless (148B) -> plain bindless (144B)
// b) masked bindless (148B) -> plain non-bindless (144B) -> masked bindless (148B)
{
void* ubo_ptr = nullptr;
const VkBuffer ubo_buf = d.buffer(64, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, &ubo_ptr);
EXPECT(ubo_buf && ubo_ptr);
const float blue[4] = {0.0f, 0.0f, 1.0f, 1.0f};
std::memcpy(ubo_ptr, blue, sizeof(blue));
auto create_dsl = d.fn<PFN_vkCreateDescriptorSetLayout>("vkCreateDescriptorSetLayout");
auto destroy_dsl = d.fn<PFN_vkDestroyDescriptorSetLayout>("vkDestroyDescriptorSetLayout");
auto create_pool = d.fn<PFN_vkCreateDescriptorPool>("vkCreateDescriptorPool");
auto destroy_pool = d.fn<PFN_vkDestroyDescriptorPool>("vkDestroyDescriptorPool");
auto alloc_sets = d.fn<PFN_vkAllocateDescriptorSets>("vkAllocateDescriptorSets");
auto update_sets = d.fn<PFN_vkUpdateDescriptorSets>("vkUpdateDescriptorSets");
VkDescriptorSetLayoutBinding b0{};
b0.binding = 0;
b0.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
b0.descriptorCount = 1;
b0.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
VkDescriptorSetLayoutCreateInfo dsl_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
dsl_info.bindingCount = 1;
dsl_info.pBindings = &b0;
VkDescriptorSetLayout set0_layout = VK_NULL_HANDLE;
EXPECT(create_dsl(d.device, &dsl_info, nullptr, &set0_layout) == VK_SUCCESS);
VkDescriptorPoolSize pool_size{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1};
VkDescriptorPoolCreateInfo pool_info{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO};
pool_info.maxSets = 1;
pool_info.poolSizeCount = 1;
pool_info.pPoolSizes = &pool_size;
VkDescriptorPool pool = VK_NULL_HANDLE;
EXPECT(create_pool(d.device, &pool_info, nullptr, &pool) == VK_SUCCESS);
VkDescriptorSetAllocateInfo alloc_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO};
alloc_info.descriptorPool = pool;
alloc_info.descriptorSetCount = 1;
alloc_info.pSetLayouts = &set0_layout;
VkDescriptorSet set0 = VK_NULL_HANDLE;
EXPECT(alloc_sets(d.device, &alloc_info, &set0) == VK_SUCCESS);
VkDescriptorBufferInfo buf_info{ubo_buf, 0, 16};
VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
write.dstSet = set0;
write.dstBinding = 0;
write.descriptorCount = 1;
write.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
write.pBufferInfo = &buf_info;
update_sets(d.device, 1, &write, 0, nullptr);
constexpr std::uintptr_t kPipeBindless = 104;
PipelineHolder holder_bindless;
GraphicsPipeline gp_bindless;
EXPECT(holder_bindless.init(d, vs_words, make_ubo_color_ps(), true, gp_bindless, set0_layout, set0));
ctx.add_graphics_pipeline(kPipeBindless, gp_bindless);
// 4a: plain bindless (144B) -> masked non-bindless (148B) -> plain bindless (144B)
{
std::vector<Draw> seq;
seq.push_back(make_fullscreen_draw(kPipeBindless, kColorId, kDepthId,
make_user_data(0.75f, 0, 0, 0, 0), 0xFu));
seq.push_back(make_fullscreen_draw(kPipeNoFc, kColorId, kDepthId,
make_user_data(0.50f, 1.0f, 0.0f, 0.0f, 1.0f), 0x5u));
seq.push_back(make_fullscreen_draw(kPipeBindless, kColorId, kDepthId,
make_user_data(0.25f, 0, 0, 0, 0), 0xFu));
EXPECT(run_draws(seq));
for (std::uint32_t y = 0; y < kHeight; ++y) {
for (std::uint32_t x = 0; x < kWidth; ++x) {
const auto* px = pixel_rgba(x, y);
EXPECT(px[0] == 0 && px[1] == 0 && px[2] == 255 && px[3] == 255);
EXPECT(std::fabs(pixel_depth(x, y) - 0.25f) < 1e-5f);
}
}
}
// 4b: masked bindless (148B) -> plain non-bindless (144B) -> masked bindless (148B)
{
std::vector<Draw> seq;
seq.push_back(make_fullscreen_draw(kPipeBindless, kColorId, kDepthId,
make_user_data(0.75f, 0, 0, 0, 0), 0xAu));
seq.push_back(make_fullscreen_draw(kPipeNoFc, kColorId, kDepthId,
make_user_data(0.50f, 1.0f, 0.0f, 0.0f, 1.0f), 0xFu));
seq.push_back(make_fullscreen_draw(kPipeBindless, kColorId, kDepthId,
make_user_data(0.25f, 0, 0, 0, 0), 0xAu));
EXPECT(run_draws(seq));
for (std::uint32_t y = 0; y < kHeight; ++y) {
for (std::uint32_t x = 0; x < kWidth; ++x) {
const auto* px = pixel_rgba(x, y);
const bool odd_x = (x & 1u) == 1u;
if (odd_x) {
EXPECT(px[0] == 0 && px[1] == 0 && px[2] == 255 && px[3] == 255);
EXPECT(std::fabs(pixel_depth(x, y) - 0.25f) < 1e-5f);
} else {
EXPECT(px[0] == 255 && px[1] == 0 && px[2] == 0 && px[3] == 255);
EXPECT(std::fabs(pixel_depth(x, y) - 0.50f) < 1e-5f);
}
}
}
}
ctx.remove_pipeline(kPipeBindless);
destroy_pool(d.device, pool, nullptr);
destroy_dsl(d.device, set0_layout, nullptr);
}
// 5. Batch PipelineCompiles fallback test: when variant(sample_masked) fails on a non-patchable
// PSO, both VulkanQueue::execute() (batch prior + PipelineCompiles) and build_pipelines_serial()
// fall back cleanly to the plain pipeline (sample_masked=false) instead of failing the submission.
{
constexpr std::uintptr_t kPipeFallback = 105;
PipelineHolder holder_fallback;
GraphicsPipeline gp_fallback;
EXPECT(holder_fallback.init(d, vs_words, ps_no_fc_words, true, gp_fallback,
VK_NULL_HANDLE, VK_NULL_HANDLE, /*fail_masked_variant=*/true));
ctx.add_graphics_pipeline(kPipeFallback, gp_fallback);
// Full VulkanQueue submission (exercises collect_unbuilt_pipelines + build_pipelines + finish_compiled_pipelines).
EXPECT(run_draws({make_fullscreen_draw(kPipeFallback, kColorId, kDepthId,
make_user_data(0.25f, 1.0f, 1.0f, 0.0f, 1.0f), 0x5u)}));
for (std::uint32_t y = 0; y < kHeight; ++y) {
for (std::uint32_t x = 0; x < kWidth; ++x) {
const auto* px = pixel_rgba(x, y);
EXPECT(px[0] == 255 && px[1] == 255 && px[2] == 0 && px[3] == 255);
EXPECT(std::fabs(pixel_depth(x, y) - 0.25f) < 1e-5f);
}
}
// Direct PipelineCompiles + finish_compiled_pipelines check with prior pre-pass.
{
CommandList list;
EXPECT(list.draw(make_fullscreen_draw(kPipeFallback, kColorId, kDepthId,
make_user_data(0.25f, 1.0f, 1.0f, 0.0f, 1.0f), 0x5u)));
EXPECT(list.close());
const CommandList* lp = &list;
ReplayFunctions rf{};
rf.graphics_pipeline = [&](std::uintptr_t id) -> const GraphicsPipeline* {
return id == kPipeFallback ? &gp_fallback : nullptr;
};
rf.buffer_at = [](std::uint64_t, std::uint64_t&, VkBuffer&, VkDeviceSize&) { return false; };
rf.image_view = [&](const ResourceBinding&, const VkImageSubresourceRange&) -> VkImageView {
return reinterpret_cast<VkImageView>(std::uintptr_t{1});
};
rf.begin_rendering = ctx.vk.vkCmdBeginRendering;
rf.end_rendering = ctx.vk.vkCmdEndRendering;
rf.bind_vertex_buffers2 = ctx.vk.vkCmdBindVertexBuffers2;
rf.bind_index_buffer = ctx.vk.vkCmdBindIndexBuffer;
rf.set_viewport = ctx.vk.vkCmdSetViewport;
rf.set_scissor = ctx.vk.vkCmdSetScissor;
rf.set_primitive_topology = ctx.vk.vkCmdSetPrimitiveTopology;
rf.set_primitive_restart_enable = ctx.vk.vkCmdSetPrimitiveRestartEnable;
rf.set_stencil_reference = ctx.vk.vkCmdSetStencilReference;
rf.set_blend_constants = ctx.vk.vkCmdSetBlendConstants;
rf.draw = ctx.vk.vkCmdDraw;
rf.draw_indexed = ctx.vk.vkCmdDrawIndexed;
rf.bind_pipeline = ctx.vk.vkCmdBindPipeline;
rf.push_constants = ctx.vk.vkCmdPushConstants;
PipelineCompiles prior;
collect_unbuilt_pipelines({&lp, 1}, rf, prior);
prior.build_serial();
PipelineCompiles compiles;
rf.compiles = &compiles;
PreparedList prepared;
const auto res = [&](ResourceId id) -> const ResourceBinding* {
if (id == kColorId) return &rb_color;
if (id == kDepthId) return &rb_depth;
return nullptr;
};
const ReplayResult pr = prepare(list, res, rf, prepared);
EXPECT(pr == ReplayResult::success);
EXPECT(prepared.draw_count == 1 && prepared.draws[0].compile != 0 && prepared.draws[0].sample_masked);
const ReplayResult br = build_pipelines_serial(compiles, {&prepared, 1}, pr, &prior);
EXPECT(br == ReplayResult::success);
EXPECT(prepared.draws[0].pipeline == gp_fallback.pipeline);
EXPECT(!prepared.draws[0].sample_masked);
}
ctx.remove_pipeline(kPipeFallback);
}
ctx.remove_pipeline(kPipeNoFc);
ctx.remove_pipeline(kPipeWithFc);
ctx.remove_pipeline(kPipeDepthOnly);
ctx.remove_resource(kColorId);
ctx.remove_resource(kDepthId);
ctx.remove_resource(kColorRbId);
ctx.remove_resource(kDepthRbId);
EXPECT(d.validation_errors() == 0);
std::printf("vk_sample_mask_test: PASS (validation_errors=0, masked_builds=%u/%u)\n",
holder_no_fc.masked_builds, holder_with_fc.masked_builds);
return 0;
}