feat(agc): primitive restart with an all-ones index for strips, and for lists with VK_EXT_primitive_topology_list_restart

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ButHENCbXXt8h4EZuWUabJ
This commit is contained in:
Dean Galvin
2026-09-28 19:15:10 +13:00
parent fe1935ef68
commit 181457a0a6
8 changed files with 55 additions and 5 deletions
@@ -154,6 +154,7 @@ public:
void* Window() const;
void Resize(std::uint32_t width, std::uint32_t height);
bool Presentable() const;
bool PrimitiveListRestart() const;
// A presentation is a few steps so the presenter holds GuestMemory::GpuMutex only while it
// touches the queue. Presentations are slots (FlipInFlight() + 1, each with its own command
// buffer, fence, kept resident image and dump buffer): RetirePresents(keep) (no mutex) retires
@@ -3866,8 +3866,21 @@ private:
if (drawParameters.indexed) {
const auto restart = queue.userConfig.find(0x24b);
if (restart != queue.userConfig.end() && restart->second != 0) {
rejected = "AGC graphics: primitive restart (GE_MULTI_PRIM_IB_RESET_EN) is unsupported for indexed draws";
return DrawVerdict::Rejected;
const auto resetIndex = queue.context.find(0x103);
const auto primitive = queue.userConfig.find(0x242);
const std::uint32_t allOnes = drawParameters.indexSize == 2 ? 0xffffu : 0xffffffffu;
const auto type = primitive == queue.userConfig.end() ? 0u : primitive->second & 0x3fu;
const bool strip = type == 3 || type == 5 || type == 6;
const bool list = type == 1 || type == 2 || type == 4;
const auto restartDevice = device.load();
if (!strip && !(list && restartDevice != nullptr && restartDevice->PrimitiveListRestart())) {
rejected = "AGC graphics: primitive restart is only supported for strips, and for lists with VK_EXT_primitive_topology_list_restart";
return DrawVerdict::Rejected;
}
if (resetIndex == queue.context.end() || (resetIndex->second & allOnes) != allOnes) {
rejected = "AGC graphics: primitive restart index other than all ones is unsupported";
return DrawVerdict::Rejected;
}
}
}
if (DrawPrecheck()) {
@@ -185,6 +185,7 @@ struct VulkanDevice::State {
// VK_EXT_descriptor_indexing with non-uniform image array indexing (bindless image tables in
// graphics stages, and compute workgroups wider than a wave).
bool descriptorIndexing = false;
bool primitiveListRestart = false;
bool depthClipControl = false;
bool depthClamp = false;
VkDeviceSize hostImportAlignment = 0;
@@ -728,6 +729,15 @@ VulkanDevice::VulkanDevice(const PresentationWindow* window) : state(std::make_u
#endif
deviceExtensions.push_back(VK_EXT_EXTERNAL_MEMORY_HOST_EXTENSION_NAME);
}
VkPhysicalDevicePrimitiveTopologyListRestartFeaturesEXT listRestartFeatures{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRIMITIVE_TOPOLOGY_LIST_RESTART_FEATURES_EXT};
if (hasExtension(VK_EXT_PRIMITIVE_TOPOLOGY_LIST_RESTART_EXTENSION_NAME)) {
VkPhysicalDeviceFeatures2 features{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &listRestartFeatures};
state->InstanceFunction<PFN_vkGetPhysicalDeviceFeatures2>("vkGetPhysicalDeviceFeatures2")(selected, &features);
state->primitiveListRestart = listRestartFeatures.primitiveTopologyListRestart == VK_TRUE;
if (state->primitiveListRestart) deviceExtensions.push_back(VK_EXT_PRIMITIVE_TOPOLOGY_LIST_RESTART_EXTENSION_NAME);
}
listRestartFeatures = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRIMITIVE_TOPOLOGY_LIST_RESTART_FEATURES_EXT};
listRestartFeatures.primitiveTopologyListRestart = state->primitiveListRestart ? VK_TRUE : VK_FALSE;
VkPhysicalDeviceDepthClipControlFeaturesEXT depthClipFeatures{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_CLIP_CONTROL_FEATURES_EXT};
if (hasExtension(VK_EXT_DEPTH_CLIP_CONTROL_EXTENSION_NAME)) {
VkPhysicalDeviceFeatures2 features{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &depthClipFeatures};
@@ -815,6 +825,10 @@ VulkanDevice::VulkanDevice(const PresentationWindow* window) : state(std::make_u
depthClipFeatures.pNext = const_cast<void*>(deviceInfo.pNext);
deviceInfo.pNext = &depthClipFeatures;
}
if (state->primitiveListRestart) {
listRestartFeatures.pNext = const_cast<void*>(deviceInfo.pNext);
deviceInfo.pNext = &listRestartFeatures;
}
byteFeatures.pNext = const_cast<void*>(deviceInfo.pNext);
if (state->fragmentShaderBarycentric) {
barycentricFeatures.pNext = byteFeatures.pNext;
@@ -2108,6 +2122,10 @@ ShaderRecompiler::SpirvTarget VulkanDevice::Target() const {
return target;
}
bool VulkanDevice::PrimitiveListRestart() const {
return state->primitiveListRestart;
}
Graphics::Context VulkanDevice::graphicsContext() const {
static const bool noCache = std::getenv("APS5_NO_CONTEXT_CACHE") != nullptr;
if (state->contextReady && !noCache) return state->context;
@@ -2152,6 +2170,7 @@ Graphics::Context VulkanDevice::buildContext() const {
context.copiedWriters = state->copiedWriters.get();
context.functions = state->functionsReady ? &state->deviceFunctions : nullptr;
context.descriptorIndexing = state->descriptorIndexing;
context.primitiveListRestart = state->primitiveListRestart;
return context;
}
@@ -125,6 +125,7 @@ struct Context {
// VK_EXT_descriptor_indexing with non-uniform sampled/storage image array indexing enabled
// (bindless image tables in graphics stages).
bool descriptorIndexing = false;
bool primitiveListRestart = false;
template<typename TFunction>
TFunction Function(const char* name) const {
@@ -52,6 +52,7 @@ struct State {
bool rectList = false;
VkExtent2D renderExtent;
VkPrimitiveTopology topology;
bool primitiveRestart = false;
VkViewport viewport;
bool negativeOneToOne;
bool depthClamp = false;
@@ -118,6 +118,8 @@ Pipeline::Pipeline(const Context& context, const State& state, const VertexInput
input.pVertexAttributeDescriptions = vertexInput.attributes.data();
VkPipelineInputAssemblyStateCreateInfo assembly{VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO};
assembly.topology = state.topology;
const bool listTopology = state.topology == VK_PRIMITIVE_TOPOLOGY_POINT_LIST || state.topology == VK_PRIMITIVE_TOPOLOGY_LINE_LIST || state.topology == VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
assembly.primitiveRestartEnable = state.primitiveRestart && (!listTopology || context.primitiveListRestart) ? VK_TRUE : VK_FALSE;
// Viewport and scissor are set per draw (Begin), so they do not multiply pipelines; the depth
// clip control stays baked in.
VkPipelineViewportStateCreateInfo viewports{VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO};
@@ -313,6 +315,7 @@ std::vector<std::byte> pipelineKey(const Context& context, const State& state, c
append(key, state.hasColorTarget);
append(key, state.rectList);
append(key, state.topology);
append(key, state.primitiveRestart);
append(key, state.cullMode);
append(key, state.frontFace);
append(key, state.negativeOneToOne);
@@ -293,7 +293,7 @@ State DecodeState(const QueueState& queue) {
default: throw std::runtime_error("AGC graphics: unsupported primitive type " + std::to_string(primitive));
}
APS5_LOG_OUT_DEBUG("Topology=%u", static_cast<unsigned>(result.topology));
(void)read(queue.userConfig, 0x24b, RegisterBank::UserConfig);
result.primitiveRestart = read(queue.userConfig, 0x24b, RegisterBank::UserConfig) != 0 && !result.rectList && result.topology != VK_PRIMITIVE_TOPOLOGY_PATCH_LIST;
if ((read(cx, 0x207) & LayerExports) != 0) {
static bool reported = false;
if (!reported) {
@@ -514,7 +514,12 @@ std::string DrawRejection(const QueueState& queue, bool indexed) {
const auto require = [&](bool condition, const char* reason) {
return condition ? std::string() : "AGC graphics: " + std::string(reason);
};
if (auto reason = indexed ? nonzero(queue.userConfig, 0x24b, ~0u, "primitive restart (GE_MULTI_PRIM_IB_RESET_EN)") : std::string(); !reason.empty()) return reason;
if (indexed && value(queue.userConfig, 0x24b, word) && word != 0) {
std::uint32_t primitive = 0;
std::uint32_t resetIndex = 0;
if (value(queue.userConfig, 0x242, primitive) && (primitive & 0x3fu) != 1 && (primitive & 0x3fu) != 2 && (primitive & 0x3fu) != 3 && (primitive & 0x3fu) != 4 && (primitive & 0x3fu) != 5 && (primitive & 0x3fu) != 6) return "AGC graphics: primitive restart is only supported for point, line and triangle topologies";
if (value(cx, 0x103, resetIndex) && (resetIndex & 0xffffu) != 0xffffu) return "AGC graphics: primitive restart index other than all ones is unsupported";
}
if (auto reason = nonzero(cx, 0x207, ~LayerExports, "clip distances, layer, viewport or auxiliary vertex exports"); !reason.empty()) return reason;
if (value(cx, 0x200, word)) {
if (!depthPassThrough(word) && !IgnoreDepthTest() && (word & DepthControlMask) != 0) return zeroMessage(0x200, word, "depth, stencil or conditional color writes");
@@ -89,7 +89,14 @@ void stateTests() {
queue.context[0x1b4] = 2;
(void)AgcDriver::Graphics::DecodeState(queue);
Require(AgcDriver::Graphics::DrawRejection(queue, false).empty(), "primitive restart rejected a non-indexed draw");
Require(AgcDriver::Graphics::DrawRejection(queue, true).find("GE_MULTI_PRIM_IB_RESET_EN") != std::string::npos, "primitive restart was accepted for an indexed draw");
queue.userConfig[0x242] = 9;
Require(AgcDriver::Graphics::DrawRejection(queue, true).find("point, line and triangle") != std::string::npos, "primitive restart was accepted for patches");
queue.userConfig[0x242] = 6;
queue.context[0x103] = 0xffffffffu;
Require(AgcDriver::Graphics::DrawRejection(queue, true).empty(), "primitive restart was rejected for an indexed strip");
Require(AgcDriver::Graphics::DecodeState(queue).primitiveRestart, "primitive restart was not decoded for a strip");
queue.context[0x103] = 5;
Require(AgcDriver::Graphics::DrawRejection(queue, true).find("all ones") != std::string::npos, "a restart index other than all ones was accepted");
queue = makeState();
queue.userConfig.erase(0x24b);
queue.context[0x2a5] = 0;