test(agc): validate pixel LDS and expose pipeline statistics

This commit is contained in:
Nicolas Abosch
2026-10-04 23:00:07 -03:00
parent ebf29f8d91
commit e08333825f
6 changed files with 249 additions and 0 deletions
@@ -191,6 +191,7 @@ struct VulkanDevice::State {
bool primitiveListRestart = false;
bool depthClipControl = false;
bool imageViewMinLod = false;
bool pipelineExecutableInfo = false;
bool maintenance8 = false;
bool depthClamp = false;
bool depthBounds = false;
@@ -957,6 +958,20 @@ VulkanDevice::VulkanDevice(const PresentationWindow* window) : state(std::make_u
} else {
std::fprintf(stderr, "[gpu] timeline semaphores unavailable or disabled; drains wait under the GPU mutex\n");
}
VkPhysicalDevicePipelineExecutablePropertiesFeaturesKHR pipelineFeatures{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_EXECUTABLE_PROPERTIES_FEATURES_KHR};
const auto pipelineStats = std::getenv("APS5_PIPELINE_STATS");
if (pipelineStats && std::string_view(pipelineStats) == "1") {
Graphics::Require(hasExtension(VK_KHR_PIPELINE_EXECUTABLE_PROPERTIES_EXTENSION_NAME), "APS5_PIPELINE_STATS requires VK_KHR_pipeline_executable_properties");
VkPhysicalDeviceFeatures2 features{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &pipelineFeatures};
state->InstanceFunction<PFN_vkGetPhysicalDeviceFeatures2>("vkGetPhysicalDeviceFeatures2")(selected, &features);
Graphics::Require(pipelineFeatures.pipelineExecutableInfo == VK_TRUE, "APS5_PIPELINE_STATS requires pipelineExecutableInfo");
state->pipelineExecutableInfo = true;
deviceExtensions.push_back(VK_KHR_PIPELINE_EXECUTABLE_PROPERTIES_EXTENSION_NAME);
deviceInfo.enabledExtensionCount = static_cast<std::uint32_t>(deviceExtensions.size());
deviceInfo.ppEnabledExtensionNames = deviceExtensions.data();
pipelineFeatures.pNext = byteFeatures.pNext;
byteFeatures.pNext = &pipelineFeatures;
}
bdaFeatures.pNext = &byteFeatures;
deviceInfo.pNext = &bdaFeatures;
check(state->InstanceFunction<PFN_vkCreateDevice>("vkCreateDevice")(selected, &deviceInfo, nullptr, &state->device), "vkCreateDevice");
@@ -2305,6 +2320,7 @@ Graphics::Context VulkanDevice::buildContext() const {
context.descriptorIndexing = state->descriptorIndexing;
context.primitiveListRestart = state->primitiveListRestart;
context.imageViewMinLod = state->imageViewMinLod;
context.pipelineExecutableInfo = state->pipelineExecutableInfo;
return context;
}
@@ -3309,6 +3325,7 @@ VulkanDevice::IndirectOutcome VulkanDevice::dispatch(const ShaderRecompiler::Rec
layoutInfo.pPushConstantRanges = pushStages != 0 ? &push : nullptr;
check(state->DeviceFunction<PFN_vkCreatePipelineLayout>("vkCreatePipelineLayout")(state->device, &layoutInfo, nullptr, &objects->layout), "vkCreatePipelineLayout");
VkComputePipelineCreateInfo pipelineInfo{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO};
pipelineInfo.flags = context.pipelineExecutableInfo ? VK_PIPELINE_CREATE_CAPTURE_STATISTICS_BIT_KHR : 0;
pipelineInfo.stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
pipelineInfo.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT;
pipelineInfo.stage.module = objects->module;
@@ -3319,6 +3336,7 @@ VulkanDevice::IndirectOutcome VulkanDevice::dispatch(const ShaderRecompiler::Rec
pipelineInfo.layout = objects->layout;
if (profile && shader.spirv.size() > 100000) std::fprintf(stderr, "[dispatch] creating a pipeline for %zu SPIR-V words (program 0x%llx)\n", shader.spirv.size(), static_cast<unsigned long long>(programAddress));
check(state->DeviceFunction<PFN_vkCreateComputePipelines>("vkCreateComputePipelines")(state->device, context.pipelineCache, 1, &pipelineInfo, nullptr, &objects->pipeline), "vkCreateComputePipelines");
Graphics::LogPipelineStatistics_nid_no_patch(context, objects->pipeline);
timing.Mark("pipeline_create");
if (pipelineKey != 0) {
// Find-or-insert: objects another dispatch of the variant mapped meanwhile serve this
@@ -135,6 +135,7 @@ struct Context {
bool descriptorIndexing = false;
bool primitiveListRestart = false;
bool imageViewMinLod = false;
bool pipelineExecutableInfo = false;
template<typename TFunction>
TFunction Function(const char* name) const {
@@ -8,6 +8,8 @@ namespace AgcDriver::Graphics {
struct VertexInputLayout;
void LogPipelineStatistics_nid_no_patch(const Context& context, VkPipeline pipeline);
// The attachments of one render pass instance. Work recorded with it references the handle until
// the batch completes, so a recorded draw keeps the object (Recorder::Keep) like its pipeline.
class Framebuffer {
@@ -14,6 +14,51 @@
namespace AgcDriver::Graphics {
void LogPipelineStatistics_nid_no_patch(const Context& context, VkPipeline pipeline) {
if (!context.pipelineExecutableInfo) return;
const auto getProperties = context.Function<PFN_vkGetPipelineExecutablePropertiesKHR>("vkGetPipelineExecutablePropertiesKHR");
const auto getStatistics = context.Function<PFN_vkGetPipelineExecutableStatisticsKHR>("vkGetPipelineExecutableStatisticsKHR");
VkPipelineInfoKHR pipelineInfo{VK_STRUCTURE_TYPE_PIPELINE_INFO_KHR};
pipelineInfo.pipeline = pipeline;
std::uint32_t count = 0;
Check(getProperties(context.device, &pipelineInfo, &count, nullptr), "vkGetPipelineExecutablePropertiesKHR count");
std::vector<VkPipelineExecutablePropertiesKHR> properties(count, {VK_STRUCTURE_TYPE_PIPELINE_EXECUTABLE_PROPERTIES_KHR});
Check(getProperties(context.device, &pipelineInfo, &count, properties.data()), "vkGetPipelineExecutablePropertiesKHR");
properties.resize(count);
for (std::uint32_t index = 0; index < properties.size(); ++index) {
VkPipelineExecutableInfoKHR executable{VK_STRUCTURE_TYPE_PIPELINE_EXECUTABLE_INFO_KHR};
executable.pipeline = pipeline;
executable.executableIndex = index;
count = 0;
Check(getStatistics(context.device, &executable, &count, nullptr), "vkGetPipelineExecutableStatisticsKHR count");
std::vector<VkPipelineExecutableStatisticKHR> statistics(count, {VK_STRUCTURE_TYPE_PIPELINE_EXECUTABLE_STATISTIC_KHR});
Check(getStatistics(context.device, &executable, &count, statistics.data()), "vkGetPipelineExecutableStatisticsKHR");
statistics.resize(count);
const auto& property = properties[index];
std::fprintf(stderr, "[pipeline-stats] executable=%u stages=0x%x subgroup=%u name=%s\n", index, property.stages, property.subgroupSize, property.name);
for (const auto& statistic : statistics) {
std::fprintf(stderr, "[pipeline-stats] executable=%u %s=", index, statistic.name);
switch (statistic.format) {
case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_BOOL32_KHR:
std::fprintf(stderr, "%s", statistic.value.b32 ? "true" : "false");
break;
case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_INT64_KHR:
std::fprintf(stderr, "%lld", static_cast<long long>(statistic.value.i64));
break;
case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR:
std::fprintf(stderr, "%llu", static_cast<unsigned long long>(statistic.value.u64));
break;
case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_FLOAT64_KHR:
std::fprintf(stderr, "%.17g", statistic.value.f64);
break;
default:
throw std::runtime_error("unsupported pipeline statistic format");
}
std::fprintf(stderr, " (%s)\n", statistic.description);
}
}
}
Framebuffer::Framebuffer(const Context& context, VkRenderPass renderPass, std::span<const VkImageView> targets, VkExtent2D extent) : context(context) {
// Cached objects outlive their device's teardown; they must not keep its buffer pool alive past it.
this->context.bufferPool.reset();
@@ -177,6 +222,7 @@ Pipeline::Pipeline(const Context& context, const State& state, const VertexInput
blend.pAttachments = state.blends.empty() ? nullptr : state.blends.data();
std::copy(state.blendConstants.begin(), state.blendConstants.end(), blend.blendConstants);
VkGraphicsPipelineCreateInfo pipelineInfo{VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO};
pipelineInfo.flags = context.pipelineExecutableInfo ? VK_PIPELINE_CREATE_CAPTURE_STATISTICS_BIT_KHR : 0;
pipelineInfo.stageCount = static_cast<std::uint32_t>(stages.size());
pipelineInfo.pStages = stages.data();
VkPipelineTessellationStateCreateInfo tessellation{VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO};
@@ -195,6 +241,7 @@ Pipeline::Pipeline(const Context& context, const State& state, const VertexInput
pipelineInfo.layout = layout;
pipelineInfo.renderPass = renderPass;
Check(context.Function<PFN_vkCreateGraphicsPipelines>("vkCreateGraphicsPipelines")(context.device, context.pipelineCache, 1, &pipelineInfo, nullptr, &pipeline), "vkCreateGraphicsPipelines");
LogPipelineStatistics_nid_no_patch(context, pipeline);
} catch (...) {
release();
throw;
@@ -0,0 +1,177 @@
#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp"
#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp"
#include "Recompiler.hpp"
#include "VulkanTestDevice.hpp"
#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
namespace {
using AgcDriver::Graphics::Require;
using ShaderRecompiler::ShaderStage;
constexpr std::uint32_t Sentinel = 0xdeadbeefu;
constexpr std::uint32_t Width = 64;
constexpr std::uint32_t Height = 32;
alignas(256) std::array<std::byte, Width * Height * 4> Pixels{};
alignas(256) std::array<std::uint32_t, Width * Height * 4> Results{};
alignas(256) constexpr std::array<std::uint32_t, 6> VertexCode{
0xe0382000, 0x80000005, 0xbf8c3f70, 0xf80008cf, 0x03020100, 0xbf810000,
};
std::vector<std::uint32_t> PixelCode(bool bounded) {
std::vector<std::uint32_t> code{0x7e040f00, 0x7e060f01, 0xd5430006, 0x0409ff03, Width};
const auto vop2 = [&](std::uint32_t opcode, std::uint32_t destination, std::uint32_t source, std::uint32_t vector) {
code.push_back(opcode | (destination << 17u) | (vector << 9u) | source);
};
vop2(0x34000000, 8, 0x88, 6);
for (std::uint32_t index = 0; index < 32; ++index) {
vop2(0x4a000000, 4, 0x80 + index, 2);
vop2(0x36000000, 9, 0x9f, 4);
vop2(0x34000000, 13, 0x82, 9);
vop2(0x4a000000, 5, 0x80 + index, 8);
for (std::uint32_t bank = 0; bank < 3; ++bank) {
if (bank != 0) vop2(0x4a000000, 5, 0xa0, 5);
code.push_back(0xd8340000 | (bank * 256u));
code.push_back(0x0000050d);
}
}
code.push_back(0xbf8cc07f);
if (bounded) vop2(0x36000000, 14, 0x9f, 3);
vop2(0x34000000, 15, 0x82, bounded ? 14 : 3);
for (std::uint32_t bank = 0; bank < 3; ++bank) {
code.push_back(0xd8d80000 | (bank * 256u));
code.push_back(((10u + bank) << 24u) | 15u);
}
code.push_back(0xbf8cc07f);
for (std::uint32_t bank = 0; bank < 3; ++bank) {
code.push_back(0xe0702000 | (bank * 4u));
code.push_back(0x80010006 | ((10u + bank) << 8u));
}
code.insert(code.end(), {0x7e0e02f2, 0xf800180f, 0x07070707, 0xbf810000});
return code;
}
std::array<std::uint32_t, 4> BufferDescriptor(const void* data, std::uint32_t stride, std::uint32_t count) {
const auto address = reinterpret_cast<std::uintptr_t>(data);
return {static_cast<std::uint32_t>(address), static_cast<std::uint32_t>((address >> 32u) & 0xffffu) | (stride << 16u), count, 0x01016facu};
}
constexpr std::array<std::array<float, 4>, 3> Triangles{{
{-1.0f, -1.0f, 0.5f, 1.0f}, {3.0f, -1.0f, 0.5f, 1.0f}, {-1.0f, 3.0f, 0.5f, 1.0f}
}};
void Draw(AgcDriver::VulkanDevice& device, std::uint32_t waveSize, bool bounded) {
const auto& triangles = Triangles;
const auto pixelCode = PixelCode(bounded);
Pixels.fill(std::byte{0});
Results.fill(Sentinel);
const auto target = device.Target();
std::vector<std::uint32_t> vertexUserData(4, 0u);
const auto vertexBuffer = BufferDescriptor(triangles.data(), 16u, static_cast<std::uint32_t>(triangles.size()));
std::copy(vertexBuffer.begin(), vertexBuffer.end(), vertexUserData.begin());
const std::array<ShaderRecompiler::MemoryRegion, 1> vertexMemory{{{reinterpret_cast<std::uintptr_t>(VertexCode.data()), std::as_bytes(std::span(VertexCode))}}};
ShaderRecompiler::RecompileRequest vertex{
{ShaderStage::Vertex, reinterpret_cast<std::uintptr_t>(VertexCode.data()), VertexCode, 0, {}},
{waveSize, 0, vertexUserData, std::nullopt, std::nullopt, ShaderRecompiler::ShaderVertexStageInfo{}, vertexMemory},
target,
{0, 0, 0, 64}
};
vertex.useCache = false;
const auto vertexResult = ShaderRecompiler::Recompile(vertex);
const auto vertexPush = static_cast<std::uint32_t>(vertexResult.pushConstants.size());
ShaderRecompiler::ShaderPixelStageInfo pixel{};
pixel.wave32 = waveSize == 32u;
pixel.inputAddr = ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::PositionX) | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::PositionY);
pixel.posX = true;
pixel.posY = true;
pixel.targetOutputMode[0] = 9;
pixel.targetExportMapping.fill(0xe4u);
std::vector<std::uint32_t> pixelUserData(8, 0u);
const auto results = BufferDescriptor(Results.data(), 16u, Width * Height);
std::copy(results.begin(), results.end(), pixelUserData.begin() + 4);
const std::array<ShaderRecompiler::MemoryRegion, 1> pixelMemory{{{reinterpret_cast<std::uintptr_t>(pixelCode.data()), std::as_bytes(std::span(pixelCode))}}};
ShaderRecompiler::RecompileRequest fragment{
{ShaderStage::Fragment, reinterpret_cast<std::uintptr_t>(pixelCode.data()), pixelCode, 0, {}},
{waveSize, 0, pixelUserData, std::nullopt, pixel, std::nullopt, pixelMemory},
target,
{0, 0, vertexPush, 128 - vertexPush}
};
fragment.useCache = false;
const auto pixelResult = ShaderRecompiler::Recompile(fragment);
const std::array<AgcDriver::Graphics::CompiledShader, 2> shaders{{
{ShaderStage::Vertex, &vertexResult, 0},
{ShaderStage::Fragment, &pixelResult, vertexPush}
}};
AgcDriver::Graphics::State state{};
state.stages = {AgcDriver::Graphics::ShaderPath::Vertex, 0u, waveSize, waveSize, std::nullopt, std::nullopt};
state.color = {reinterpret_cast<std::uintptr_t>(Pixels.data()), {Width, Height}, VK_FORMAT_R8G8B8A8_UNORM, Pixels.size(), 0xe4u};
state.colors = {state.color};
state.hasColorTarget = true;
state.renderExtent = {Width, Height};
state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
state.viewport = {0, static_cast<float>(Height), static_cast<float>(Width), -static_cast<float>(Height), 0, 1};
state.negativeOneToOne = false;
state.scissor = {{0, 0}, {Width, Height}};
state.cullMode = VK_CULL_MODE_NONE;
state.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
state.blend.colorWriteMask = 15;
state.blends = {state.blend};
state.blendConstants = {};
const AgcDriver::Pm4::DrawParameters draw{0, static_cast<std::uint32_t>(triangles.size()), 0, 1, 0, false};
device.Draw(state, draw, shaders);
device.WaitIdle();
}
void Check(std::uint32_t waveSize, bool bounded) {
const auto what = "pixel LDS wave" + std::to_string(waveSize) + (bounded ? " bounded" : " unbounded");
for (std::uint32_t pixel = 0; pixel < Width * Height; ++pixel) {
for (std::uint32_t channel = 0; channel < 4; ++channel) {
Require(Pixels[pixel * 4u + channel] == std::byte{255}, what + ": missing color at pixel " + std::to_string(pixel));
}
const auto x = pixel % Width;
const auto y = pixel / Width;
const auto index = (y + 32u - x) & 31u;
for (std::uint32_t bank = 0; bank < 3; ++bank) {
const auto expected = pixel * 256u + index + bank * 32u;
const auto actual = Results[pixel * 4u + bank];
Require(actual == expected, what + ": pixel " + std::to_string(pixel) + " bank " + std::to_string(bank) + " expected " + std::to_string(expected) + " got " + std::to_string(actual));
}
Require(Results[pixel * 4u + 3u] == Sentinel, what + ": output stride guard overwritten");
}
}
}
int main() {
try {
const auto device = OpenVulkanTestDevice();
if (!device) return VulkanTestSkipped;
if (device->Target().subgroupSize < 32u) {
std::printf("skipped, subgroup size %u cannot hold a wave32\n", device->Target().subgroupSize);
return VulkanTestSkipped;
}
for (const auto waveSize : {64u, 32u}) {
for (const auto bounded : {true, false}) {
std::fprintf(stderr, "pixel LDS wave%u %s\n", waveSize, bounded ? "bounded" : "unbounded");
for (std::uint32_t pass = 0; pass < 3; ++pass) {
Draw(*device, waveSize, bounded);
Check(waveSize, bounded);
}
}
}
std::puts("pixel LDS tests passed: 12 draws, 24576 pixels, 73728 LDS results");
return 0;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}
}
+4
View File
@@ -46,6 +46,10 @@ Build configuration parameters:
SDL and FreeType settings forced by the root `CMakeLists.txt` cannot be overridden with `-D`.
## Pipeline statistics
Set `APS5_PIPELINE_STATS=1` to capture and print driver statistics for each newly created graphics or compute pipeline. This requires `VK_KHR_pipeline_executable_properties` and `pipelineExecutableInfo`; an unsupported device fails with an error. Statistic names and units are driver-specific. Capturing statistics can increase pipeline compilation cost. The setting is disabled by default.
## Shader recompiler
The shader recompilation logic in [core/shader/recompiler](../../core/shader/recompiler) is isolated from the rest of the project and is a pure function of its input data, designed for integration into any other project. The current CMake target also includes cache support and links a supplied runtime target, glslang, and optionally SPIRV-Tools.