Merge pull request #137 from oneandonlydean/fix/pixel-inputs-at-parameter-slots

fix(recompiler): declare pixel shader inputs at their parameter slots
This commit is contained in:
boykopovar
2026-10-01 10:49:16 +03:00
committed by GitHub
10 changed files with 291 additions and 74 deletions
@@ -17,6 +17,7 @@
#include <map>
#include <optional>
#include <set>
#include <span>
#include <string>
#include <string_view>
#include <type_traits>
@@ -986,6 +987,7 @@ struct ModuleShape {
bool perVertex = false;
std::uint32_t perVertexLength = 3;
bool parameterOutput = false;
std::uint32_t parameterLocation = 0;
bool rectParameters = false;
bool secondTarget = false;
};
@@ -1023,7 +1025,7 @@ std::vector<std::uint32_t> makeModule(const ModuleShape& shape) {
if (shape.parameterOutput) {
const auto parameter = id();
emit(declarations, spv::OpVariable, {outputPointer, parameter, spv::StorageClassOutput});
emit(annotations, spv::OpDecorate, {parameter, spv::DecorationLocation, 0});
emit(annotations, spv::OpDecorate, {parameter, spv::DecorationLocation, shape.parameterLocation});
extraInterface.push_back(parameter);
}
if (shape.secondTarget) {
@@ -1201,6 +1203,95 @@ void rectListTests() {
expectFailure([&] { Draw(context, state, draw, {}); }, "incomplete rect-list");
}
ShaderRecompiler::RecompileResult recompilePixel(std::initializer_list<std::uint32_t> controls, std::span<const std::uint32_t> code) {
auto queue = makeState();
queue.context[0x1b3] = 0x2u;
queue.context[0x1b4] = 0x2u;
queue.context[0x1b6] = static_cast<std::uint32_t>(controls.size());
std::uint32_t index = 0;
for (const auto control : controls) queue.context[0x191 + index++] = control;
const auto pixel = AgcDriver::Graphics::DecodePixelStageInfo(queue.context, IdentityExports);
ShaderRecompiler::RecompileRequest request{};
request.shader = {ShaderRecompiler::ShaderStage::Fragment, 0x30000u, code, 0, {}};
request.context.waveSize = 64;
request.context.pixel = pixel;
request.target.vulkanVersion = 0x00401000u;
request.target.spirvVersion = 0x00010300u;
request.target.subgroupSize = 64;
request.target.fragmentShaderBarycentricEnabled = true;
request.layout.pushConstantSizeBytes = 128;
request.useCache = false;
return ShaderRecompiler::Recompile(request);
}
struct LocatedInput {
std::uint32_t location;
bool flat;
bool perVertex;
};
std::vector<LocatedInput> locatedInputs(std::span<const std::uint32_t> words) {
std::map<std::uint32_t, LocatedInput> decorated;
std::set<std::uint32_t> inputs;
for (std::size_t at = 5; at < words.size() && (words[at] >> 16u) != 0; at += words[at] >> 16u) {
const auto op = static_cast<spv::Op>(words[at] & 0xffffu);
if (op == spv::OpVariable && words[at + 3] == spv::StorageClassInput) inputs.insert(words[at + 2]);
if (op != spv::OpDecorate) continue;
auto& input = decorated[words[at + 1]];
if (words[at + 2] == spv::DecorationLocation) input.location = words[at + 3] + 1u;
if (words[at + 2] == spv::DecorationFlat) input.flat = true;
if (words[at + 2] == spv::DecorationPerVertexKHR) input.perVertex = true;
}
std::vector<LocatedInput> result;
for (const auto& [id, input] : decorated) {
if (input.location != 0 && inputs.contains(id)) result.push_back({input.location - 1u, input.flat, input.perVertex});
}
std::sort(result.begin(), result.end(), [](const auto& a, const auto& b) { return a.location < b.location; });
return result;
}
void pixelParameterSlotTests() {
using AgcDriver::Graphics::CompiledShader;
const VkPhysicalDeviceSubgroupProperties subgroup{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES};
auto state = AgcDriver::Graphics::DecodeState(makeState());
ShaderRecompiler::RecompileResult vertex;
vertex.spirv = makeModule({.parameterOutput = true});
const std::array<std::uint32_t, 8> mixed{0xc8100000u, 0xc8110001u, 0xc8160402u, 0xc81a0802u, 0xc81e0f02u, 0xf800180fu, 0x07060504u, 0xbf810000u};
auto pixel = recompilePixel({0x0u, 0x400u, 0x22u, 0x320u}, mixed);
auto inputs = locatedInputs(pixel.spirv.Words());
Require(inputs.size() == 1 && inputs[0].location == 0 && inputs[0].perVertex, "a slot read flat and interpolated did not become one per-vertex input");
std::array<CompiledShader, 2> shaders{{{ShaderRecompiler::ShaderStage::Vertex, &vertex, 0}, {ShaderRecompiler::ShaderStage::Fragment, &pixel, 0}}};
AgcDriver::Graphics::ValidateShaders(shaders, state, subgroup, true);
const std::array<std::uint32_t, 7> shared{0xc8100000u, 0xc8110001u, 0xc8140500u, 0xc8150501u, 0xf800180fu, 0x05040504u, 0xbf810000u};
pixel = recompilePixel({0x3u, 0x3u}, shared);
inputs = locatedInputs(pixel.spirv.Words());
Require(inputs.size() == 1 && inputs[0].location == 3 && !inputs[0].perVertex && !inputs[0].flat, "inputs reading one slot were not declared once at the slot");
pixel = recompilePixel({0x404u, 0x0u}, shared);
inputs = locatedInputs(pixel.spirv.Words());
Require(inputs.size() == 2 && inputs[0].location == 0 && !inputs[0].flat && inputs[1].location == 4 && inputs[1].flat, "flat and interpolated inputs of different slots moved");
pixel = recompilePixel({0x20u, 0x2320u}, shared);
Require(locatedInputs(pixel.spirv.Words()).empty(), "a defaulted input was declared as a parameter");
AgcDriver::Graphics::ValidateShaders(shaders, state, subgroup, true);
const std::array<std::uint32_t, 7> vertices{0xc8120002u, 0xc8160000u, 0xc81a0001u, 0xc81e0302u, 0xf800180fu, 0x07060504u, 0xbf810000u};
const auto subtracts = [](std::span<const std::uint32_t> words) {
std::size_t count = 0;
for (std::size_t at = 5; at < words.size() && (words[at] >> 16u) != 0; at += words[at] >> 16u) count += (words[at] & 0xffffu) == spv::OpFSub;
return count;
};
pixel = recompilePixel({0x423u}, vertices);
inputs = locatedInputs(pixel.spirv.Words());
Require(inputs.size() == 1 && inputs[0].location == 3 && inputs[0].perVertex, "a pass-through input (OFFSET bit 5 with FLAT_SHADE) was not read per vertex at its slot");
Require(subtracts(pixel.spirv.Words()) == 0, "v_interp_mov p10/p20 of a pass-through input subtracted vertex 0");
ShaderRecompiler::RecompileResult slotVertex;
slotVertex.spirv = makeModule({.parameterOutput = true, .parameterLocation = 3});
const std::array<CompiledShader, 2> slotShaders{{{ShaderRecompiler::ShaderStage::Vertex, &slotVertex, 0}, {ShaderRecompiler::ShaderStage::Fragment, &pixel, 0}}};
AgcDriver::Graphics::ValidateShaders(slotShaders, state, subgroup, true);
pixel = recompilePixel({0x403u}, vertices);
inputs = locatedInputs(pixel.spirv.Words());
Require(inputs.size() == 1 && inputs[0].location == 3 && inputs[0].perVertex && subtracts(pixel.spirv.Words()) == 2, "v_interp_mov p10/p20 of a flat input did not read differences to vertex 0");
expectFailure([&] { recompilePixel({0x423u, 0x3u}, shared); }, "passes its vertices through unchanged");
}
void validationTests() {
AgcDriver::Graphics::State state{};
state.stages.path = AgcDriver::Graphics::ShaderPath::Vertex;
@@ -1447,6 +1538,7 @@ int main() {
resourceTests();
debugBranchTests();
validationTests();
pixelParameterSlotTests();
rectListTests();
mock = MockVulkan{};
auto bdaContext = mockContext();
@@ -12,6 +12,7 @@
#include <spirv/unified1/spirv.hpp>
#include <algorithm>
#include <array>
#include <initializer_list>
#include <iostream>
#include <map>
#include <future>
@@ -20,6 +21,7 @@
#include <span>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
namespace {
@@ -525,6 +527,76 @@ void verifyPixelInputs() {
}
}
ShaderRecompiler::RecompileResult recompileSlots(std::initializer_list<std::uint32_t> controls, std::span<const std::uint32_t> code) {
using namespace ShaderRecompiler;
ShaderPixelStageInfo pixel{};
pixel.interpolatorCount = static_cast<std::uint32_t>(controls.size());
std::uint32_t index = 0;
for (const auto control : controls) pixel.interpolatorSettings[index++] = control;
pixel.inputAddr = PixelInputBit(PixelInput::PerspectiveCenter);
pixel.hasPerspectiveCenterVgpr = true;
pixel.targetOutputMode[0] = 9u;
pixel.targetExportMapping[0] = 0xe4u;
RecompileRequest request{};
request.shader = {ShaderStage::Fragment, 0x30000u, code, 0, {}};
request.context.waveSize = 64;
request.context.pixel = pixel;
request.target.vulkanVersion = 0x00401000u;
request.target.spirvVersion = 0x00010300u;
request.target.subgroupSize = 64;
request.target.fragmentShaderBarycentricEnabled = true;
request.layout.pushConstantSizeBytes = 128;
request.useCache = false;
return Recompile(request);
}
std::vector<std::pair<std::uint32_t, bool>> slotInputs(std::initializer_list<std::uint32_t> controls, std::span<const std::uint32_t> code) {
const auto result = recompileSlots(controls, code);
const auto& words = result.spirv.Words();
std::map<std::uint32_t, std::uint32_t> locations;
std::map<std::uint32_t, bool> perVertex;
std::vector<std::uint32_t> inputs;
for (std::size_t at = 5; at < words.size() && (words[at] >> 16u) != 0; at += words[at] >> 16u) {
const auto op = static_cast<spv::Op>(words[at] & 0xffffu);
if (op == spv::OpVariable && words[at + 3] == spv::StorageClassInput) inputs.push_back(words[at + 2]);
if (op == spv::OpDecorate && words[at + 2] == spv::DecorationLocation) locations[words[at + 1]] = words[at + 3];
if (op == spv::OpDecorate && words[at + 2] == spv::DecorationPerVertexKHR) perVertex[words[at + 1]] = true;
}
std::vector<std::pair<std::uint32_t, bool>> located;
for (const auto id : inputs) {
if (locations.contains(id)) located.emplace_back(locations.at(id), perVertex.contains(id));
}
std::sort(located.begin(), located.end());
return located;
}
void verifyPixelParameterSlots() {
static constexpr std::array<std::uint32_t, 7> shared{0xc8100000u, 0xc8110001u, 0xc8140500u, 0xc8150501u, 0xf800180fu, 0x05040504u, 0xbf810000u};
auto inputs = slotInputs({0x3u, 0x3u}, shared);
require(inputs.size() == 1u && inputs[0].first == 3u && !inputs[0].second, "inputs reading one slot were not declared once at the slot");
inputs = slotInputs({0x404u, 0x0u}, shared);
require(inputs.size() == 2u && inputs[0].first == 0u && inputs[1].first == 4u, "inputs of different slots moved");
require(slotInputs({0x20u, 0x2320u}, shared).empty(), "a defaulted input was declared as a parameter");
static constexpr std::array<std::uint32_t, 8> mixed{0xc8100000u, 0xc8110001u, 0xc8160402u, 0xc81a0802u, 0xc81e0f02u, 0xf800180fu, 0x07060504u, 0xbf810000u};
inputs = slotInputs({0x0u, 0x400u, 0x22u, 0x320u}, mixed);
require(inputs.size() == 1u && inputs[0].first == 0u && inputs[0].second, "a slot read flat and interpolated did not become one per-vertex input");
static constexpr std::array<std::uint32_t, 7> vertices{0xc8120002u, 0xc8160000u, 0xc81a0001u, 0xc81e0302u, 0xf800180fu, 0x07060504u, 0xbf810000u};
const auto subtracts = [](std::initializer_list<std::uint32_t> controls) {
const auto result = recompileSlots(controls, vertices);
const auto& words = result.spirv.Words();
std::size_t count = 0;
for (std::size_t at = 5; at < words.size() && (words[at] >> 16u) != 0; at += words[at] >> 16u) count += (words[at] & 0xffffu) == spv::OpFSub;
return count;
};
inputs = slotInputs({0x423u}, vertices);
require(inputs.size() == 1u && inputs[0].first == 3u && inputs[0].second, "a pass-through input (OFFSET bit 5 with FLAT_SHADE) was not read per vertex at its slot");
require(subtracts({0x423u}) == 0u, "v_interp_mov p10/p20 of a pass-through input subtracted vertex 0");
inputs = slotInputs({0x403u}, vertices);
require(inputs.size() == 1u && inputs[0].first == 3u && inputs[0].second && subtracts({0x403u}) == 2u, "v_interp_mov p10/p20 of a flat input did not read differences to vertex 0");
require(slotInputs({0x23u}, vertices).empty(), "a defaulted input (OFFSET bit 5 without FLAT_SHADE) was declared as a parameter");
expectFailure([] { static_cast<void>(recompileSlots({0x423u, 0x3u}, shared)); }, "passes its vertices through unchanged", "an interpolated pass-through input was accepted");
}
}
int main() {
@@ -536,6 +608,7 @@ int main() {
verifyProgramCounterRelativeData();
verifyMeshConfiguration();
verifyPixelInputs();
verifyPixelParameterSlots();
#if ANYPS5_ENABLE_SPIRV_TOOLS
const std::vector<std::uint32_t> minimalSpirv{
0x07230203u, 0x00010000u, 0u, 5u, 0u,
@@ -189,6 +189,28 @@ struct ShaderPixelInputInfo {
return psPosX || psPosY || psPosZ || psPosW;
}
[[nodiscard]] bool InputIsDefault(std::uint32_t input) const {
return input < inputNum && input < 32u && (interpolatorSettings[input] & 0x420u) == 0x20u;
}
[[nodiscard]] bool InputIsPassthrough(std::uint32_t input) const {
return input < inputNum && input < 32u && (interpolatorSettings[input] & 0x420u) == 0x420u;
}
[[nodiscard]] bool InputIsCustom(std::uint32_t input) const {
return input < 32u && ((customInterpolationMask & (1u << input)) != 0u || InputIsPassthrough(input));
}
[[nodiscard]] std::uint32_t InputSlot(std::uint32_t input) const {
return input < 32u ? interpolatorSettings[input] & 0x1fu : input;
}
[[nodiscard]] std::uint32_t InputDefaultBits(std::uint32_t input, std::uint32_t component) const {
const auto value = input < 32u ? (interpolatorSettings[input] >> 8u) & 0x3u : 0u;
const bool one = component == 3u ? (value & 0x1u) != 0u : (value & 0x2u) != 0u;
return one ? 0x3f800000u : 0u;
}
[[nodiscard]] bool InputIsLinear(std::uint32_t input, std::uint32_t linearInputs, std::uint32_t perspectiveInputs) const {
const auto bit = input < 32u ? 1u << input : 0u;
if ((linearInputs & perspectiveInputs & bit) != 0u) {
@@ -200,7 +200,7 @@ void CollectVertexInputs(const IrProgram& program, const ShaderVertexInputInfo*
}
bool IsPixelParameterCustom(const ShaderPixelInputInfo& pixel, std::uint32_t input) {
return input < 32u && (pixel.customInterpolationMask & (1u << input)) != 0u;
return pixel.InputIsCustom(input);
}
bool IsPixelParameterFlat(const ShaderPixelInputInfo& pixel, std::uint32_t input) {
@@ -215,28 +215,66 @@ void CollectPixelInputs(const IrProgram& program, const ShaderPixelInputInfo* pi
if (pixel->psFrontFace) {
AddInput(info, StageInputKind::FrontFacing, 0, 1, "gl_FrontFacing");
}
std::array<bool, 32> read {};
std::array<bool, 32> perVertex {};
std::array<bool, 32> interpolated {};
for (const auto& block : program.Blocks()) {
for (const IrValue* inst : block->Instructions()) {
if (inst->Opcode() == IrOpcode::GetAttribute) {
interpolated[inst->Argument(0)->Resolve()->ImmediateU32()] = true;
const auto input = inst->Argument(0)->Resolve()->ImmediateU32();
if (IsPixelParameterCustom(*pixel, input)) {
throw std::runtime_error("pixel input " + std::to_string(input) + " passes its vertices through unchanged but is read with v_interp_p1/p2");
}
read[input] = true;
interpolated[input] = true;
} else if (inst->Opcode() == IrOpcode::GetInterpolationParameter) {
const auto input = inst->Argument(0)->Resolve()->ImmediateU32();
const auto mode = inst->Argument(2)->Resolve()->ImmediateU32();
read[input] = true;
perVertex[input] = perVertex[input] || mode < 2u || !IsPixelParameterFlat(*pixel, input);
}
}
}
const auto& metadata = program.Metadata();
const auto linear = [&](std::uint32_t input) {
return pixel->InputIsLinear(input, metadata.pixelLinearInputs, metadata.pixelPerspectiveInputs);
};
const auto interpolation = [&](std::uint32_t input) {
return IsPixelParameterFlat(*pixel, input) ? 2u : linear(input) ? 1u : 0u;
};
constexpr std::uint32_t unassigned = std::numeric_limits<std::uint32_t>::max();
std::array<std::uint32_t, 32> slotInterpolation {};
slotInterpolation.fill(unassigned);
std::array<bool, 32> slotPerVertex {};
for (std::uint32_t input = 0; input < pixel->inputNum; input++) {
AddInput(info, StageInputKind::Parameter, input, 4, "in_param_" + std::to_string(input), perVertex[input]);
}
for (std::uint32_t input = 0; input < pixel->inputNum; input++) {
if (interpolated[input] && perVertex[input]) {
const auto kind = pixel->psNoPerspective ? StageInputKind::BaryCoordNoPerspective : StageInputKind::BaryCoordSmooth;
AddInput(info, kind, 0, 3, pixel->psNoPerspective ? "gl_BaryCoordNoPerspKHR" : "gl_BaryCoordKHR");
break;
if (!read[input] || pixel->InputIsDefault(input)) {
continue;
}
const auto slot = pixel->InputSlot(input);
if (slotInterpolation[slot] == unassigned) {
slotInterpolation[slot] = interpolation(input);
} else if (slotInterpolation[slot] != interpolation(input)) {
slotPerVertex[slot] = true;
}
slotPerVertex[slot] = slotPerVertex[slot] || perVertex[input];
}
bool smooth = false;
bool noPerspective = false;
for (std::uint32_t input = 0; input < pixel->inputNum; input++) {
if (!read[input] || pixel->InputIsDefault(input)) {
continue;
}
const bool vertexInput = slotPerVertex[pixel->InputSlot(input)];
AddInput(info, StageInputKind::Parameter, input, 4, "in_param_" + std::to_string(input), vertexInput);
if (vertexInput && interpolated[input] && !IsPixelParameterFlat(*pixel, input)) {
(linear(input) ? noPerspective : smooth) = true;
}
}
if (smooth) {
AddInput(info, StageInputKind::BaryCoordSmooth, 0, 3, "gl_BaryCoordKHR");
}
if (noPerspective) {
AddInput(info, StageInputKind::BaryCoordNoPerspective, 0, 3, "gl_BaryCoordNoPerspKHR");
}
}
@@ -36,6 +36,8 @@ std::uint32_t GlslStd450(SpirvEmitterState& state);
std::uint32_t PixelParameterLocation(const SpirvEmitterState& state, std::uint32_t attr);
bool PixelParameterIsFlat(const SpirvEmitterState& state, std::uint32_t attr);
bool PixelParameterIsCustom(const SpirvEmitterState& state, std::uint32_t attr);
bool PixelParameterIsDefault(const SpirvEmitterState& state, std::uint32_t attr);
bool PixelParameterIsLinear(const SpirvEmitterState& state, std::uint32_t attr);
VertexInputScalarKind VertexParameterScalarKind(const SpirvEmitterState& state, std::uint32_t location);
std::uint32_t VertexParameterComponentCount(const SpirvInputBinding& input);
std::uint32_t VertexParameterScalarType(SpirvEmitterState& state, VertexInputScalarKind kind);
@@ -13,6 +13,8 @@ std::vector<FragmentParameter> DescribeFragmentParameters(const IrProgram& progr
std::uint32_t PixelParameterLocation(const SpirvEmitterState& state, std::uint32_t attr);
bool PixelParameterIsFlat(const SpirvEmitterState& state, std::uint32_t attr);
bool PixelParameterIsCustom(const SpirvEmitterState& state, std::uint32_t attr);
bool PixelParameterIsDefault(const SpirvEmitterState& state, std::uint32_t attr);
bool PixelParameterIsLinear(const SpirvEmitterState& state, std::uint32_t attr);
VertexInputScalarKind VertexParameterScalarKind(const SpirvEmitterState& state, std::uint32_t location);
std::uint32_t VertexParameterComponentCount(const SpirvInputBinding& input);
std::uint32_t VertexParameterScalarType(SpirvEmitterState& state, VertexInputScalarKind kind);
@@ -21,6 +21,8 @@ const SpirvInputBinding* SpirvInputBindingForParameter(const SpirvEmitterState&
std::uint32_t PixelParameterLocation(const SpirvEmitterState& state, std::uint32_t attr);
bool PixelParameterIsFlat(const SpirvEmitterState& state, std::uint32_t attr);
bool PixelParameterIsCustom(const SpirvEmitterState& state, std::uint32_t attr);
bool PixelParameterIsDefault(const SpirvEmitterState& state, std::uint32_t attr);
bool PixelParameterIsLinear(const SpirvEmitterState& state, std::uint32_t attr);
VertexInputScalarKind VertexParameterScalarKind(const SpirvEmitterState& state, std::uint32_t location);
std::uint32_t VertexParameterComponentCount(const SpirvInputBinding& input);
@@ -190,7 +190,21 @@ void DefineInputs(SpirvEmitterState& state) {
addBuiltin(StageInputKind::WorkgroupId, 3u, "gl_WorkGroupID");
}
}
const bool pixelStage = state.program.Resources().stage == IrShaderStage::Pixel;
for (auto& input : state.inputs) {
if (pixelStage && input.kind == StageInputKind::Parameter) {
const auto location = PixelParameterLocation(state, input.location);
const auto shared = std::find_if(state.inputs.begin(), state.inputs.end(), [&](const SpirvInputBinding& other) {
return &other != &input && other.kind == StageInputKind::Parameter && other.variableId != 0u && PixelParameterLocation(state, other.location) == location;
});
if (shared != state.inputs.end()) {
if (shared->perVertex != input.perVertex) {
throw std::runtime_error("SPIR-V module emission failed: pixel inputs sharing parameter " + std::to_string(location) + " disagree on per-vertex access");
}
input.variableId = shared->variableId;
continue;
}
}
std::uint32_t type = TypeU32(state);
switch (input.kind) {
case StageInputKind::VertexIndex:
@@ -244,8 +258,7 @@ void DefineInputs(SpirvEmitterState& state) {
} else if (flat) {
state.module.AddAnnotation(spv::OpDecorate, input.variableId, spv::DecorationFlat);
}
const auto& metadata = state.program.Metadata();
if (state.program.Resources().stage == IrShaderStage::Pixel && !flat && !input.perVertex && PixelInfo(state).InputIsLinear(input.location, metadata.pixelLinearInputs, metadata.pixelPerspectiveInputs)) {
if (!flat && !input.perVertex && PixelParameterIsLinear(state, input.location)) {
state.module.AddAnnotation(spv::OpDecorate, input.variableId, spv::DecorationNoPerspective);
}
state.module.AddAnnotation(spv::OpDecorate, input.variableId, spv::DecorationLocation, PixelParameterLocation(state, input.location));
@@ -15,7 +15,6 @@ namespace ShaderRecompiler
{
namespace {
constexpr std::uint32_t PsInputOffsetMask = 0x0000001fu;
constexpr std::uint32_t PsInputFlatShade = 0x00000400u;
constexpr std::uint32_t PixelParameterLimit = 32u;
@@ -45,74 +44,35 @@ namespace ShaderRecompiler
return StageOf(state) == IrShaderStage::Vertex || StageOf(state) == IrShaderStage::Local;
}
std::uint32_t PixelMappedLocation(const ShaderPixelInputInfo& info, std::uint32_t input) {
if (input < info.inputNum) {
if (input >= PixelParameterLimit) {
FailEmit("pixel interpolator index is out of range");
}
return info.interpolatorSettings[input] & PsInputOffsetMask;
std::uint32_t PixelInputLocation(const ShaderPixelInputInfo& info, std::uint32_t input) {
if (input >= PixelParameterLimit || input >= info.inputNum) {
FailEmit("pixel interpolator index is out of range");
}
return input;
if (info.InputIsDefault(input)) {
FailEmit("pixel input " + std::to_string(input) + " reads a default value, not a parameter");
}
return info.InputSlot(input);
}
bool PixelInputIsCustom(const ShaderPixelInputInfo& info, std::uint32_t input) {
return input < PixelParameterLimit && (info.customInterpolationMask & (1u << input)) != 0u;
return info.InputIsCustom(input);
}
bool PixelInputIsFlat(const ShaderPixelInputInfo& info, std::uint32_t input) {
return input < info.inputNum && input < PixelParameterLimit && (info.interpolatorSettings[input] & PsInputFlatShade) != 0u && !PixelInputIsCustom(info, input);
}
std::uint32_t PixelInputLocation(const ShaderPixelInputInfo& info, std::span<const std::uint32_t> activeInputs, std::uint32_t input) {
std::array<bool, PixelParameterLimit> usedLocations{};
for (const auto activeInput : activeInputs) {
const auto mapped = PixelMappedLocation(info, activeInput);
if (mapped >= usedLocations.size()) {
FailEmit("pixel parameter location is out of range");
}
usedLocations[mapped] = true;
}
std::array<std::uint32_t, PixelParameterLimit * 2u> groupLocations;
groupLocations.fill(0xffffffffu);
for (const auto activeInput : activeInputs) {
const auto mapped = PixelMappedLocation(info, activeInput);
const auto group = mapped * 2u + (PixelInputIsFlat(info, activeInput) ? 1u : 0u);
auto& location = groupLocations[group];
if (location == 0xffffffffu) {
location = mapped;
if (groupLocations[group ^ 1u] != 0xffffffffu) {
location = 0;
while (location < usedLocations.size() && usedLocations[location]) {
location++;
}
if (location >= usedLocations.size()) {
FailEmit("no free pixel parameter location");
}
}
usedLocations[location] = true;
}
if (activeInput == input) {
return location;
}
}
return PixelMappedLocation(info, input);
}
}
std::vector<FragmentParameter> DescribeFragmentParameters(const IrProgram& program, const ShaderStageInputInfo& inputInfo) {
if (inputInfo.pixel == nullptr) FailEmit("pixel input info is missing");
const auto& pixel = *inputInfo.pixel;
std::vector<std::uint32_t> activeInputs;
for (const auto& input : program.Info().inputs) {
if (input.kind == StageInputKind::Parameter) activeInputs.push_back(input.location);
}
std::vector<FragmentParameter> result;
for (const auto& input : program.Info().inputs) {
if (input.kind != StageInputKind::Parameter) continue;
const auto location = PixelInputLocation(pixel, activeInputs, input.location);
const auto location = PixelInputLocation(pixel, input.location);
if (std::any_of(result.begin(), result.end(), [&](const auto& parameter) { return parameter.location == location; })) continue;
result.push_back({location, PixelMappedLocation(pixel, input.location), PixelInputIsFlat(pixel, input.location), input.perVertex});
result.push_back({location, location, PixelInputIsFlat(pixel, input.location), input.perVertex});
}
return result;
}
@@ -121,17 +81,7 @@ namespace ShaderRecompiler
if (StageOf(state) != IrShaderStage::Pixel) {
return attr;
}
std::array<std::uint32_t, PixelParameterLimit> activeInputs{};
std::uint32_t activeCount = 0;
for (const auto& input : state.inputs) {
if (input.kind == StageInputKind::Parameter) {
if (activeCount >= activeInputs.size()) {
FailEmit("too many pixel parameters");
}
activeInputs[activeCount++] = input.location;
}
}
return PixelInputLocation(PixelInfo(state), std::span<const std::uint32_t>(activeInputs.data(), activeCount), attr);
return PixelInputLocation(PixelInfo(state), attr);
}
bool PixelParameterIsFlat(const SpirvEmitterState& state, std::uint32_t attr) {
@@ -142,6 +92,15 @@ namespace ShaderRecompiler
return StageOf(state) == IrShaderStage::Pixel && PixelInputIsCustom(PixelInfo(state), attr);
}
bool PixelParameterIsDefault(const SpirvEmitterState& state, std::uint32_t attr) {
return StageOf(state) == IrShaderStage::Pixel && PixelInfo(state).InputIsDefault(attr);
}
bool PixelParameterIsLinear(const SpirvEmitterState& state, std::uint32_t attr) {
const auto& metadata = state.program.Metadata();
return StageOf(state) == IrShaderStage::Pixel && PixelInfo(state).InputIsLinear(attr, metadata.pixelLinearInputs, metadata.pixelPerspectiveInputs);
}
VertexInputScalarKind VertexParameterScalarKind(const SpirvEmitterState& state, std::uint32_t location) {
if (!IsVertexLikeStage(state)) {
return VertexInputScalarKind::Float;
@@ -100,6 +100,9 @@ std::uint32_t EmitBuiltinU32(SpirvEmitterState& state, StageInputKind kind, std:
}
std::uint32_t EmitAttributeValue(SpirvEmitterState& state, std::uint32_t attr, std::uint32_t chan) {
if (PixelParameterIsDefault(state, attr)) {
return ConstantU32(state, state.inputInfo.pixel->InputDefaultBits(attr, chan & 3u));
}
const auto* input = SpirvInputBindingForParameter(state, attr);
if (input == nullptr || input->variableId == 0) {
return ConstantU32(state, 0u);
@@ -119,8 +122,16 @@ std::uint32_t EmitAttributeValue(SpirvEmitterState& state, std::uint32_t attr, s
if (state.inputInfo.pixel == nullptr) {
throw std::runtime_error("pixel input info is missing for a per-vertex attribute");
}
const auto barycentricKind = state.inputInfo.pixel->psNoPerspective ? StageInputKind::BaryCoordNoPerspective : StageInputKind::BaryCoordSmooth;
if (PixelParameterIsFlat(state, attr)) {
const auto bits = state.module.AllocateId();
state.module.AddFunction(spv::OpBitcast, TypeU32(state), bits, loadPerVertex(0u));
return bits;
}
const auto barycentricKind = PixelParameterIsLinear(state, attr) ? StageInputKind::BaryCoordNoPerspective : StageInputKind::BaryCoordSmooth;
const auto barycentric = InputVariableForKind(state, barycentricKind);
if (barycentric == 0u) {
throw std::runtime_error("per-vertex attribute interpolation lacks its barycentric input");
}
std::uint32_t sum = 0;
for (std::uint32_t vertex = 0; vertex < 3u; vertex++) {
const auto pointer = state.module.AllocateId();
@@ -151,6 +162,9 @@ std::uint32_t EmitAttributeValue(SpirvEmitterState& state, std::uint32_t attr, s
}
std::uint32_t EmitInterpolationParameterValue(SpirvEmitterState& state, std::uint32_t attr, std::uint32_t chan, std::uint32_t mode) {
if (PixelParameterIsDefault(state, attr)) {
return mode == 2u ? EmitAttributeValue(state, attr, chan) : ConstantU32(state, 0u);
}
const auto* input = SpirvInputBindingForParameter(state, attr);
if (input == nullptr) {
throw std::runtime_error("interpolation parameter refers to an undefined attribute");