Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions docs/pipeline_check.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
# GPU pipeline corpus check

`runtime/test/vk_pipeline_check.cpp` creates a `VkShaderModule` for every entry in the translator's
`index.json` and, for compute shaders, a real compute pipeline with the runtime's descriptor and push
constant contract. It is a driver-level check: it proves the modules and pipelines are accepted by
the Vulkan implementation, not that the shader results are correct. Value-level checks are in
`docs/shader_validation.md`.
`index.json` and builds a Vulkan pipeline with the runtime's descriptor and push-constant contract:
compute pipelines for `cs` entries, and dynamic-rendering graphics pipelines paired with minimal partner
stubs from `<spirv dir>/stubs/` (`tools/spirv_stubs.py`) for `vs`, `ps`, `ms` and `gs` entries. When a
pixel shader carries `"bary_fallback_input": {"location": N}` (`tools/rdna2spirv.py --bary-fallback`, for
GPUs without `VK_KHR_fragment_shader_barycentric`), the checker synthesizes the triangle pass-through
geometry shader in memory (`runtime/src/spirv_bary_fallback.h`) and checks the three-stage `VS + GS + PS`
pipeline, skipping with `no geometryShader` (or `no shaderClipDistance` / `no shaderCullDistance` when
the partner VS exports clip or cull distances) if the device lacks the required feature, and counting an
incompatible `VS`/`PS` interface as a pipeline failure. It is a driver-level check: it proves the modules
and pipelines are accepted by the Vulkan implementation, not that the shader results are correct.
Value-level checks are in `docs/shader_validation.md`.

Timing and memory figures in this page were measured with `MESA_SHADER_CACHE_DISABLE=true`. Without it, RADV's
on-disk shader cache (`~/.cache/mesa_shader_cache`, about 1.1 GB after a full corpus run) turns a repeated module
Expand Down
16 changes: 16 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,8 @@ if(BUILD_TESTING)
add_test(NAME spirv_f16_halves COMMAND spirv_f16_halves_test)
add_executable(dual_source_fragment_test test/dual_source_fragment_test.cpp)
add_test(NAME dual_source_fragment COMMAND dual_source_fragment_test)
add_executable(spirv_bary_fallback_test test/spirv_bary_fallback_test.cpp)
add_test(NAME spirv_bary_fallback COMMAND spirv_bary_fallback_test)
# Optional: skips unless LEONIDA_SHADERS names a translated work folder (<work>/shaders/spirv).
add_executable(shader_library_corpus_test test/shader_library_corpus_test.cpp)
add_test(NAME shader_library_corpus COMMAND shader_library_corpus_test)
Expand Down Expand Up @@ -1997,6 +1999,12 @@ if(TARGET Vulkan::Headers)
"-DWANT=${LEONIDA_PIPELINE_CHECK_STACK}"
-P "${CMAKE_CURRENT_SOURCE_DIR}/test/check_stack_reserve.cmake")
endif()
if(Python3_FOUND AND NOT CMAKE_CROSSCOMPILING)
add_test(NAME vk_pipeline_check_bary_fallback COMMAND "${Python3_EXECUTABLE}" -I
"${CMAKE_CURRENT_SOURCE_DIR}/test/check_vk_pipeline_bary_fallback.py"
--ctest --exe "$<TARGET_FILE:vk_pipeline_check>")
set_tests_properties(vk_pipeline_check_bary_fallback PROPERTIES SKIP_RETURN_CODE 77)
endif()
add_test(NAME depth24_convert COMMAND depth24_convert_test)
set_tests_properties(depth24_convert PROPERTIES SKIP_RETURN_CODE 77)
# Self-checking Vulkan tests with no shaders or game data; each exits 77 without a Vulkan device (TEST-514).
Expand Down Expand Up @@ -3303,6 +3311,14 @@ if(TARGET Vulkan::Headers)
add_test(NAME d3d12_f16_halves COMMAND d3d12_f16_halves_test)
set_tests_properties(d3d12_f16_halves PROPERTIES SKIP_RETURN_CODE 77)

# Per-pipeline --bary-fallback GS synthesis (RT-BARY-FALLBACK-PSO): perspective barycentrics + ClipDistance + f16 halves.
add_executable(d3d12_bary_fallback_test test/d3d12_bary_fallback_test.cpp shims/d3d12_xs_device.cpp)
target_link_libraries(d3d12_bary_fallback_test PRIVATE gta6rt_d3d12_com d3d12 dxgi)
target_compile_definitions(d3d12_bary_fallback_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(d3d12_bary_fallback_test PRIVATE /W4 /permissive- /utf-8)
add_test(NAME d3d12_bary_fallback COMMAND d3d12_bary_fallback_test)
set_tests_properties(d3d12_bary_fallback PROPERTIES SKIP_RETURN_CODE 77)

# PresentX and the frame events on the Vulkan device (shims/xbox_present.h), checked on swapchain pixels.
add_executable(xbox_present_test test/xbox_present_test.cpp)
target_link_libraries(xbox_present_test PRIVATE gta6rt_d3d12_com user32)
Expand Down
100 changes: 90 additions & 10 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
#include "../src/title_texture_refresh.h"
#include "title_gpu_placements.h"
#include "../src/spirv_f16_halves.h"
#include "../src/spirv_bary_fallback.h"
#include "../src/spirv_stores.h"
#include "../src/title_va_alias.h"
#include "../src/console_heap_vsharp_slots.h"
Expand Down Expand Up @@ -2336,6 +2337,7 @@ struct DeviceState {
// Cached physical-device no-attachment sample counts for D3D12_OPTIONS19.
UINT no_output_sample_counts = 0;
std::uint32_t f16_half_top_location = 29;
std::uint32_t bary_f16_half_top_location = 29;
float max_sampler_lod_bias = 0;
D3D12_CONSERVATIVE_RASTERIZATION_TIER conservative_raster_tier = D3D12_CONSERVATIVE_RASTERIZATION_TIER_NOT_SUPPORTED;
bool conservative_post_depth_coverage = false;
Expand Down Expand Up @@ -10981,19 +10983,46 @@ class PipelineState final
Stage& vs_stage = stages[0];
Stage& gs_stage = stages[1];
Stage& ps_stage = stages[2];
const bool bary_fallback = ps_stage.entry && ps_stage.entry->bary_fallback_location.has_value();
if (bary_fallback) {
const int bary_loc = int(*ps_stage.entry->bary_fallback_location);
if (mesh) {
return stream_unsupported(
"--bary-fallback pixel shader needs a geometry stage, which a mesh PSO cannot include; location",
bary_loc);
}
if (gs_stage.entry) {
return stream_unsupported(
"--bary-fallback pixel shader cannot pair with an explicit geometry shader; location", bary_loc);
}
if (rect_list) {
return stream_unsupported(
"--bary-fallback pixel shader cannot pair with a rectangle-list geometry stage; location",
bary_loc);
}
if (!device_.info.features.geometryShader) {
return stream_unsupported(
"--bary-fallback pixel shaders need the Vulkan geometryShader feature; location", bary_loc);
}
if (desc.PrimitiveTopologyType != D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE) {
log_once("a --bary-fallback geometry stage takes triangles; topology type", desc.PrimitiveTopologyType);
return E_INVALIDARG;
}
}
if (vs_stage.entry && ps_stage.entry && !ps_stage.entry->f16_halves.empty() && gs_stage.entry) {
log_once("--f16-interp-fallback halves are linked VS->PS only; this PSO has a geometry shader", 0);
}
if (vs_stage.entry && ps_stage.entry && !gs_stage.entry &&
(!ps_stage.entry->f16_halves.empty() || !vs_stage.entry->f16_halves.empty())) {
std::string link_error;
if (!link_f16_halves(vs_stage.words, *vs_stage.entry, ps_stage.words, *ps_stage.entry,
&link_error)) {
&link_error, bary_fallback)) {
std::fprintf(stderr, "[rt] d3d12: %s\n", link_error.c_str());
return E_FAIL;
}
f16_linked_ = true;
}
bary_fallback_ = bary_fallback;
// A lazy pipeline keeps where each stage's words come from, not the words or a module: build() makes them
// again, the same way, and drops them once the pipeline exists.
lazy_ = lazy_graphics_pipelines();
Expand Down Expand Up @@ -11048,8 +11077,8 @@ class PipelineState final
module = shader_module(stage.words);
if (!module) return E_FAIL;
}
if (stage.bit == VK_SHADER_STAGE_VERTEX_BIT && !geometry && device_.info.features.geometryShader &&
topology_type == D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE) {
if (stage.bit == VK_SHADER_STAGE_VERTEX_BIT && !geometry && !bary_fallback &&
device_.info.features.geometryShader && topology_type == D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE) {
rectangle_capable_ = true;
if (!lazy_) rectangle_vertex_ = modules_.back();
}
Expand Down Expand Up @@ -11082,6 +11111,40 @@ class PipelineState final
info.pName = "main";
stage_infos.push_back(info);
}
if (bary_fallback) { // the stage that supplies (1,0,0)/(0,1,0)/(0,0,1) at bary_fallback_input.location
std::vector<std::uint32_t> words;
leonida::BaryFallbackGsInfo bary_info{};
std::string bary_error;
if (!leonida::build_bary_fallback_gs(vs_stage.words, ps_stage.words,
*ps_stage.entry->bary_fallback_location, words, &bary_info,
&bary_error)) {
std::fprintf(stderr, "[rt] d3d12: %s\n", bary_error.c_str());
return E_FAIL;
}
if (bary_info.clip_count && !device_.info.features.shaderClipDistance) {
return stream_unsupported(
"--bary-fallback geometry stage forwards ClipDistance, which needs shaderClipDistance; count",
int(bary_info.clip_count));
}
if (bary_info.cull_count && !device_.info.features.shaderCullDistance) {
return stream_unsupported(
"--bary-fallback geometry stage forwards CullDistance, which needs shaderCullDistance; count",
int(bary_info.cull_count));
}
if (trace_pipeline_compile()) stage_bytes_ += words.size() * sizeof(std::uint32_t);
VkShaderModule module = VK_NULL_HANDLE;
if (lazy_) {
sources_.push_back({StageSource::bary_fallback});
} else {
module = shader_module(words);
if (!module) return E_FAIL;
}
VkPipelineShaderStageCreateInfo info{VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO};
info.stage = VK_SHADER_STAGE_GEOMETRY_BIT;
info.module = module;
info.pName = "main";
stage_infos.push_back(info);
}
if (depth24_fragment && !ps_stage.entry) { // no pixel shader: a built-in one writes the depth
const std::vector<std::uint32_t> words = leonida::depth24_depth_only_fragment(depth24_bias);
if (words.empty()) return E_FAIL;
Expand Down Expand Up @@ -12102,9 +12165,10 @@ class PipelineState final

bool link_f16_halves(std::vector<std::uint32_t>& vs_words, const leonida::ShaderEntry& vs_entry,
std::vector<std::uint32_t>& ps_words, const leonida::ShaderEntry& ps_entry,
std::string* error) const {
return leonida::link_f16_halves(vs_words, vs_entry, ps_words, ps_entry, error,
device_.f16_half_top_location);
std::string* error, bool bary_fallback = false) const {
const std::uint32_t top_loc =
bary_fallback ? device_.bary_f16_half_top_location : device_.f16_half_top_location;
return leonida::link_f16_halves(vs_words, vs_entry, ps_words, ps_entry, error, top_loc);
}

public:
Expand Down Expand Up @@ -12137,7 +12201,8 @@ class PipelineState final
}
if (f16_linked_) {
if (vs == SIZE_MAX || ps == SIZE_MAX ||
!link_f16_halves(words[vs], *sources_[vs].entry, words[ps], *sources_[ps].entry, &why))
!link_f16_halves(words[vs], *sources_[vs].entry, words[ps], *sources_[ps].entry, &why,
bary_fallback_))
return false;
}
for (std::size_t i = 0; i < sources_.size(); ++i) {
Expand All @@ -12150,6 +12215,12 @@ class PipelineState final
if (sources_[i].kind == StageSource::rect_list) {
if (vs == SIZE_MAX) return false;
words[i] = leonida::rect_list_geometry(words[vs], why);
} else if (sources_[i].kind == StageSource::bary_fallback) {
if (vs == SIZE_MAX || ps == SIZE_MAX || !sources_[ps].entry->bary_fallback_location ||
!leonida::build_bary_fallback_gs(words[vs], words[ps],
*sources_[ps].entry->bary_fallback_location, words[i], nullptr,
&why))
return false;
} else if (sources_[i].kind == StageSource::depth_only) {
words[i] = leonida::depth24_depth_only_fragment(depth24_bias_);
} else {
Expand Down Expand Up @@ -12181,7 +12252,7 @@ class PipelineState final

// Where a lazy graphics pipeline's stage modules come from, in recipe_.stages order (stage_words).
struct StageSource {
enum Kind : std::uint8_t { library, rect_list, depth_only } kind = library;
enum Kind : std::uint8_t { library, rect_list, bary_fallback, depth_only } kind = library;
std::uint8_t slot = 0; // library: the create_graphics stage table entry (0 VS or MS, 1 GS, 2 PS)
bool depth24 = false; // library: the D24 fragment patch applies
const leonida::ShaderEntry* entry = nullptr; // library: the translated shader
Expand All @@ -12194,8 +12265,9 @@ class PipelineState final
const leonida::ShaderEntry* compute_entry_ = nullptr; // compute: the translated shader build_compute reads
bool compute_failed_ = false; // compute: could not be built; not retried
std::atomic<bool> skip_logged_{false}; // compute: the skipped program's first dispatch was logged
bool lazy_ = false; // graphics: built at the first draw (lazy_graphics_pipelines)
bool f16_linked_ = false; // graphics: the VS and PS words had their f16 halves linked
bool lazy_ = false; // graphics: built at the first draw (lazy_graphics_pipelines)
bool f16_linked_ = false; // graphics: the VS and PS words had their f16 halves linked
bool bary_fallback_ = false; // graphics: synthesized --bary-fallback geometry stage
leonida::Depth24Bias depth24_bias_{};
unsigned rgba4_mask_ = 0; // graphics: the B4G4R4A4 targets stored as RGBA8 that the pixel shader is quantised for
bool dual_source_ = false; // graphics: a SRC1 blend factor; the pixel shader's SV_Target1 is Location 0, Index 1
Expand Down Expand Up @@ -13507,6 +13579,14 @@ class Device final
? info.f16_half_top_location
: leonida::f16_half_top_location(
props.limits.maxVertexOutputComponents, props.limits.maxFragmentInputComponents);
state_.bary_f16_half_top_location = state_.f16_half_top_location;
if (info.f16_half_top_location == 0 &&
props.limits.maxGeometryInputComponents && props.limits.maxGeometryOutputComponents) {
state_.bary_f16_half_top_location = std::min(
state_.f16_half_top_location,
leonida::f16_half_top_location(props.limits.maxGeometryInputComponents,
props.limits.maxGeometryOutputComponents));
}
}
if (const auto get_props2 = reinterpret_cast<PFN_vkGetPhysicalDeviceProperties2>(
info.get_instance_proc(info.instance, "vkGetPhysicalDeviceProperties2"))) {
Expand Down
10 changes: 10 additions & 0 deletions runtime/src/shader_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <fstream>
#include <iterator>
#include <map>
#include <optional>
#include <span>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -873,6 +874,8 @@ struct ShaderEntry {
std::vector<std::uint32_t> outputs; // pixel/vertex shaders: output locations written
// --f16-interp-fallback: param/attr index -> {low_half_location, high_half_location} (SHADER-378).
std::map<std::uint32_t, std::array<std::uint32_t, 2>> f16_halves;
// --bary-fallback: input location where a pass-through GS supplies one-hot barycentrics (RT-BARY-FALLBACK-PSO).
std::optional<std::uint32_t> bary_fallback_location;
// The S#s the shader builds from immediates (the index's literal_samplers): no heap slot names them, so a
// pipeline that uses the shader registers them in the bindless set itself (docs/design.md, Literal samplers).
std::vector<std::array<std::uint32_t, 4>> literal_samplers;
Expand Down Expand Up @@ -1301,6 +1304,13 @@ class ShaderLibrary {
e.f16_halves[std::uint32_t(param)] = {u32(&val.array[0]), u32(&val.array[1])};
}
}
if (const Json* bf = meta.find("bary_fallback_input"); bf && bf->kind == Json::Kind::object) {
if (const Json* loc = bf->find("location"); loc && loc->kind == Json::Kind::number &&
loc->number >= 0.0 && loc->number <= 4294967295.0 &&
double(std::uint32_t(loc->number)) == loc->number) {
e.bary_fallback_location = std::uint32_t(loc->number);
}
}
if (const Json* l = meta.find("literal_samplers"); l && l->kind == Json::Kind::array) {
for (const Json& item : l->array) {
if (item.kind != Json::Kind::array || item.array.size() != 4) continue;
Expand Down
Loading
Loading