diff --git a/core/shader/recompiler/CMakeLists.txt b/core/shader/recompiler/CMakeLists.txt index 31c6e21f4..0eedffef2 100644 --- a/core/shader/recompiler/CMakeLists.txt +++ b/core/shader/recompiler/CMakeLists.txt @@ -94,16 +94,25 @@ function(add_shader_recompiler target runtimeTarget) ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Optimization/src/SrtWalker/SrtPlanBuilder.cpp ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Optimization/src/SrtWalker/SrtRuntimeValidator.cpp ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Optimization/src/SrtWalker/SrtWalker.cpp + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/SpirvBackend/src/SpirvModule.cpp ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/SpirvBackend/src/SpirvAnalysis.cpp ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/SpirvBackend/src/SpirvAlu/SpirvAluEmitterRegisters.cpp ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/SpirvBackend/src/SpirvFlowEmitter.cpp ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/SpirvBackend/src/SpirvImageEmitter.cpp - ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/SpirvBackend/src/SpirvMemoryEmitter.cpp ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/SpirvBackend/src/SpirvModuleEmitter.cpp ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/SpirvBackend/src/SpirvEmitter.cpp ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/SpirvBackend/src/SpirvBufferFormat.cpp + + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/SpirvBackend/src/SpirvMemory/SpirvBufferAccess.cpp + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/SpirvBackend/src/SpirvMemory/SpirvConstants.cpp + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/SpirvBackend/src/SpirvMemory/SpirvDescriptors.cpp + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/SpirvBackend/src/SpirvMemory/SpirvFormatConvert.cpp + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/SpirvBackend/src/SpirvMemory/SpirvInputOutput.cpp + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/SpirvBackend/src/SpirvMemory/SpirvModuleSetup.cpp + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/SpirvBackend/src/SpirvMemory/SpirvSubgroup.cpp + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/SpirvBackend/src/SpirvMemory/SpirvTypes.cpp ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/tests/DummyShaders.cpp ) diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvBufferAccess.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvBufferAccess.cpp index 77d7f3720..4954e162c 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvBufferAccess.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvBufferAccess.cpp @@ -1 +1,183 @@ -#include "SpirvBackend/SpirvMemoryEmitter.hpp" +#include "SpirvBackend/SpirvMemory/SpirvBufferAccess.hpp" +#include "SpirvBackend/SpirvMemory/SpirvTypes.hpp" +#include "SpirvBackend/SpirvMemory/SpirvDescriptors.hpp" +#include +#include +#include +#include +#include + +namespace ShaderRecompiler +{ +namespace { + + constexpr std::uint32_t FunctionLdsDwords = 8192u; + + [[noreturn]] void FailEmit(const std::string& reason) { + throw std::runtime_error("SPIR-V module emission failed: " + reason); + } + + const ShaderWorkgroupInputInfo* ShaderWorkgroupInput(const SpirvEmitterState& state) { + switch (state.program.Resources().stage) { + case IrShaderStage::Compute: + if (state.inputInfo.compute == nullptr) { + FailEmit("compute input info is missing"); + } + return state.inputInfo.compute; + case IrShaderStage::Mesh: + if (state.inputInfo.vertex == nullptr) { + FailEmit("vertex input info is missing"); + } + return &state.inputInfo.vertex->mesh; + default: + return nullptr; + } + } + + void EnsureLdsStorage(SpirvEmitterState& state) { + if (state.ldsVariable != 0) { + return; + } + if (ShaderWorkgroupInput(state) == nullptr) { + FailEmit("function LDS was not prepared before function emission"); + } + state.ldsVariable = state.module.DefineGlobalVariable(TypeU32ArrayPointer(state, spv::StorageClassWorkgroup, LdsDwordCount(state)), spv::StorageClassWorkgroup); + state.module.AddName(state.ldsVariable, "lds_dwords"); + } + +} + +std::uint32_t EmitBinaryU32(SpirvEmitterState& state, std::uint32_t opcode, std::uint32_t lhs, std::uint32_t rhs) { + const auto result = state.module.AllocateId(); + state.module.AddFunction(opcode, TypeU32(state), result, lhs, rhs); + return result; +} + +std::uint32_t EmitShaderDataDwordLoad(SpirvEmitterState& state, std::uint32_t dwordIndex) { + const IrBindingLayout& layout = state.program.Metadata().bindings; + const auto pointer = state.module.AllocateId(); + const auto value = state.module.AllocateId(); + if (layout.UsesPushData()) { + state.module.AddFunction(spv::OpAccessChain, TypePushConstantElementPointer(state), pointer, state.pushConstantVariable, ConstantU32(state, 0), ConstantU32(state, dwordIndex + layout.pushDataStartDword)); + } else if (state.shaderDataStorageVariable != 0) { + state.module.AddFunction(spv::OpAccessChain, TypeStorageBufferElementPointer(state), pointer, state.shaderDataStorageVariable, ConstantU32(state, 0), ConstantU32(state, dwordIndex)); + } else { + FailEmit("shader data is neither in push constants nor in a storage buffer"); + } + state.module.AddFunction(spv::OpLoad, TypeU32(state), value, pointer); + return value; +} + +std::uint32_t StorageBufferPackedStride(const SpirvEmitterState& state, const MemoryInfo& mem) { + if (mem.resource >= state.program.Info().buffers.size()) { + ExitDescriptorBindingFailure(state, DescriptorBindingKind::Buffers, mem.resource, "buffer specialization is missing"); + } + return state.program.Info().buffers[mem.resource].packedStride; +} + +IrBufferFormat StorageBufferFormat(const SpirvEmitterState& state, const MemoryInfo& mem) { + if (mem.resource >= state.program.Info().buffers.size()) { + ExitDescriptorBindingFailure(state, DescriptorBindingKind::Buffers, mem.resource, "buffer specialization is missing"); + } + return state.program.Info().buffers[mem.resource].descriptorFormat; +} + +void EmitMemoryOffsets(SpirvEmitterState& state) { + const IrBindingLayout& layout = state.program.Metadata().bindings; + if (layout.memoryOffsetCount > state.memoryByteOffsets.size()) { + FailEmit("memory offset count exceeds the buffer limit"); + } + for (std::uint32_t i = 0; i < layout.memoryOffsetCount; i++) { + const auto word = EmitShaderDataDwordLoad(state, layout.memoryOffsetDword + i / 4u); + const auto shift = ConstantU32(state, (i % 4u) * 8u); + state.memoryByteOffsets[i] = EmitBinaryU32(state, spv::OpBitwiseAnd, EmitBinaryU32(state, spv::OpShiftRightLogical, word, shift), ConstantU32(state, 0xffu)); + } +} + +std::uint32_t LdsDwordCount(const SpirvEmitterState& state) { + const auto* workgroup = ShaderWorkgroupInput(state); + return workgroup != nullptr ? workgroup->ldsSizeDwords : FunctionLdsDwords; +} + +MemoryResourceAccess PrepareStorageBufferResourceAccess(SpirvEmitterState& state, const MemoryInfo& mem, std::uint32_t variable, std::uint32_t pointerType) { + if (variable == 0) { + ExitDescriptorBindingFailure(state, DescriptorBindingKind::Buffers, mem.resource, "storage buffer descriptor array was not emitted"); + } + const auto arrayIndex = ResourceForDescriptor(state, DescriptorBindingKind::Buffers, mem.resource); + MemoryResourceAccess access; + access.kind = mem.kind; + access.objectPointer = state.module.AllocateId(); + state.module.AddFunction(spv::OpAccessChain, pointerType, access.objectPointer, variable, ConstantU32(state, arrayIndex)); + access.byteOffset = state.memoryByteOffsets.at(arrayIndex); + access.length = state.module.AllocateId(); + state.module.AddFunction(spv::OpArrayLength, TypeU32(state), access.length, access.objectPointer, 0u); + return access; +} + +MemoryResourceAccess PrepareMemoryResourceAccess(SpirvEmitterState& state, const MemoryInfo& mem) { + MemoryResourceAccess access; + access.kind = mem.kind; + switch (mem.kind) { + case ResourceKind::Lds: + EnsureLdsStorage(state); + access.objectPointer = state.ldsVariable; + access.length = ConstantU32(state, LdsDwordCount(state)); + return access; + case ResourceKind::Gds: + if (state.gdsVariable == 0) { + ExitDescriptorBindingFailure(state, DescriptorBindingKind::Gds, mem.resource, "GDS binding was not emitted"); + } + if (state.gdsLength == 0) { + FailEmit("GDS length was not prepared at function entry"); + } + access.objectPointer = state.gdsVariable; + access.length = state.gdsLength; + return access; + case ResourceKind::Scratch: + if (state.scratchVariable.at(state.laneHalf) == 0) { + FailEmit("scratch storage was not prepared before function emission"); + } + access.objectPointer = state.scratchVariable.at(state.laneHalf); + access.length = ConstantU32(state, state.program.Info().scratchDwords); + return access; + case ResourceKind::ScalarAddress: + case ResourceKind::Flat: + case ResourceKind::Global: + FailEmit("physical address memory must use the BDA emitter"); + case ResourceKind::ScalarBuffer: + case ResourceKind::Buffer: + access = PrepareStorageBufferResourceAccess(state, mem, state.storageBufferVariable, TypeStorageBufferPointer(state)); + access.indexOffset = EmitBinaryU32(state, spv::OpShiftRightLogical, access.byteOffset, ConstantU32(state, 2u)); + access.addIndexOffset = true; + return access; + default: + FailEmit("unsupported memory resource kind " + std::to_string(static_cast(mem.kind))); + } +} + +std::uint32_t EmitMemoryElementIndex(SpirvEmitterState& state, const MemoryResourceAccess& access, std::uint32_t rawIndex) { + return access.addIndexOffset ? EmitAddU32(state, rawIndex, access.indexOffset) : rawIndex; +} + +std::uint32_t EmitMemoryElementInBounds(SpirvEmitterState& state, const MemoryResourceAccess& access, std::uint32_t index) { + const auto inBounds = state.module.AllocateId(); + state.module.AddFunction(spv::OpULessThan, TypeBool(state), inBounds, index, access.length); + return inBounds; +} + +std::uint32_t EmitMemoryElementPointer(SpirvEmitterState& state, const MemoryResourceAccess& access, std::uint32_t index) { + if (access.kind == ResourceKind::Lds || access.kind == ResourceKind::Scratch) { + const auto pointer = state.module.AllocateId(); + const std::uint32_t storageClass = access.kind == ResourceKind::Scratch ? spv::StorageClassFunction : ShaderWorkgroupInput(state) != nullptr ? spv::StorageClassWorkgroup : spv::StorageClassFunction; + state.module.AddFunction(spv::OpAccessChain, TypeU32ElementPointer(state, storageClass), pointer, access.objectPointer, index); + return pointer; + } + return EmitStorageBufferElementPointer(state, access, index, TypeStorageBufferElementPointer(state)); +} + +std::uint32_t EmitStorageBufferElementPointer(SpirvEmitterState& state, const MemoryResourceAccess& access, std::uint32_t index, std::uint32_t pointerType) { + const auto pointer = state.module.AllocateId(); + state.module.AddFunction(spv::OpAccessChain, pointerType, pointer, access.objectPointer, ConstantU32(state, 0), index); + return pointer; +} +} diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvConstants.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvConstants.cpp index 77d7f3720..cd5dfb0e2 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvConstants.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvConstants.cpp @@ -1 +1,48 @@ #include "SpirvBackend/SpirvMemoryEmitter.hpp" +#include + +namespace ShaderRecompiler +{ + +std::uint32_t ConstantU32(SpirvEmitterState& state, std::uint32_t value) { + return state.module.Constant(spv::OpConstant, TypeU32(state), value); +} + +std::uint32_t ConstantI32(SpirvEmitterState& state, std::int32_t value) { + return state.module.Constant(spv::OpConstant, TypeI32(state), static_cast(value)); +} + +std::uint32_t ConstantF32(SpirvEmitterState& state, std::uint32_t bits) { + return state.module.Constant(spv::OpConstant, TypeF32(state), bits); +} + +std::uint32_t FloatBits(float value) { + return std::bit_cast(value); +} + +std::uint32_t ConstantF32Value(SpirvEmitterState& state, float value) { + return ConstantF32(state, FloatBits(value)); +} + +std::uint32_t ConstantBool(SpirvEmitterState& state, bool value) { + return state.module.Constant(value ? spv::OpConstantTrue : spv::OpConstantFalse, TypeBool(state)); +} + +std::uint32_t ConstantU64(SpirvEmitterState& state, std::uint64_t value) { + return state.module.Constant(spv::OpConstantComposite, TypeU64(state), ConstantU32(state, static_cast(value)), ConstantU32(state, static_cast(value >> 32u))); +} + +std::uint32_t ConstantU32CompositeZero(SpirvEmitterState& state, std::uint32_t components) { + if (components < 2u || components > 4u) { + FailEmit("u32 composite component count is out of range"); + } + const auto zero = ConstantU32(state, 0); + const auto type = TypeU32Composite(state, components); + switch (components) { + case 2u: return state.module.Constant(spv::OpConstantComposite, type, zero, zero); + case 3u: return state.module.Constant(spv::OpConstantComposite, type, zero, zero, zero); + default: return state.module.Constant(spv::OpConstantComposite, type, zero, zero, zero, zero); + } +} + +} diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvDescriptors.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvDescriptors.cpp index 77d7f3720..6480feec1 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvDescriptors.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvDescriptors.cpp @@ -1 +1,185 @@ +#include +#include #include "SpirvBackend/SpirvMemoryEmitter.hpp" + +namespace ShaderRecompiler +{ + +[[noreturn]] void ExitDescriptorBindingFailure(const SpirvEmitterState& state, DescriptorBindingKind kind, std::uint32_t resource, const char* reason) { + throw std::runtime_error("shader binding resolution failed during SPIR-V emit: hash=" + std::to_string(state.program.Resources().shaderHash) + " stage=" + std::to_string(static_cast(StageOf(state))) + " resource=" + std::to_string(resource) + " binding_kind=" + std::to_string(static_cast(kind)) + " reason=" + reason); +} + +std::uint32_t ResourceForDescriptor(const SpirvEmitterState& state, DescriptorBindingKind kind, std::uint32_t resource) { + for (const auto& descriptor : state.program.Metadata().bindings.descriptors) { + if (descriptor.kind != kind) { + continue; + } + const auto found = std::find(descriptor.resources.begin(), descriptor.resources.end(), resource); + if (found == descriptor.resources.end()) { + ExitDescriptorBindingFailure(state, kind, resource, "resource is absent from descriptor group"); + } + return static_cast(found - descriptor.resources.begin()); + } + ExitDescriptorBindingFailure(state, kind, resource, "descriptor group was not allocated"); +} + +std::uint32_t DescriptorElementPointer(SpirvEmitterState& state, std::uint32_t resultPtrType, std::uint32_t variableId, std::uint32_t arrayIndex, DescriptorBindingKind kind, std::uint32_t resource, const char* variableName) { + if (variableId == 0) { + ExitDescriptorBindingFailure(state, kind, resource, variableName); + } + const auto pointer = state.module.AllocateId(); + state.module.AddFunction(spv::OpAccessChain, resultPtrType, pointer, variableId, ConstantU32(state, arrayIndex)); + return pointer; +} + +std::uint32_t ImageScalarType(SpirvEmitterState& state, IrTextureNumericClass numericClass) { + switch (numericClass) { + case IrTextureNumericClass::Float: return TypeF32(state); + case IrTextureNumericClass::Uint: return TypeU32(state); + case IrTextureNumericClass::Sint: return TypeI32(state); + case IrTextureNumericClass::Unsupported: break; + } + FailEmit("invalid image numeric class"); +} + +std::uint32_t ImageVectorType(SpirvEmitterState& state, IrTextureNumericClass numericClass, std::uint32_t components) { + return state.module.Type(spv::OpTypeVector, ImageScalarType(state, numericClass), components); +} + +std::uint32_t ImageType(SpirvEmitterState& state, const ImageResource& image) { + std::uint32_t sampled = 0; + std::uint32_t format = spv::ImageFormatUnknown; + if (image.resourceClass == ImageResourceClass::Sampled) { + if (image.atomic) { + FailEmit("sampled image cannot be atomic"); + } + sampled = 1; + } else if (image.resourceClass == ImageResourceClass::Storage) { + if (image.numericClass == IrTextureNumericClass::Sint || image.numericClass == IrTextureNumericClass::Unsupported) { + FailEmit("storage image numeric class is not supported"); + } + sampled = 2; + if (image.atomic) { + if (image.numericClass != IrTextureNumericClass::Uint) { + FailEmit("atomic storage image must be uint"); + } + format = spv::ImageFormatR32ui; + } + } else { + FailEmit("invalid image resource class"); + } + const auto& info = RdnaImageDimensionInfoFor(image.dimension); + return state.module.Type(spv::OpTypeImage, ImageScalarType(state, image.numericClass), info.spirvDimension, image.depthCompare ? 1u : 0u, info.arrayed, info.multisampled, sampled, format); +} + +std::uint32_t ImageViewSizeType(SpirvEmitterState& state, RdnaImageDimension dimension) { + switch (RdnaImageDimensionInfoFor(dimension).coordinateComponents) { + case 1u: return TypeU32(state); + case 2u: return TypeU32Vector(state, 2); + case 3u: return TypeU32Vector(state, 3); + default: FailEmit("image view size component count is invalid"); + } +} + +std::uint32_t LoadSampledImageDescriptor(SpirvEmitterState& state, std::uint32_t resource) { + const auto& imageResource = state.program.Info().images.at(resource); + if (imageResource.resourceClass != ImageResourceClass::Sampled) { + FailEmit("sampled image descriptor requested for a non-sampled image"); + } + const auto kind = DescriptorBindingForImage(imageResource); + const auto arrayIndex = ResourceForDescriptor(state, kind, resource); + const auto variable = state.imageVariables.at(ImageBindingIndex(kind)); + const auto imageType = ImageType(state, imageResource); + const auto pointerType = state.module.Type(spv::OpTypePointer, spv::StorageClassUniformConstant, imageType); + const auto pointer = DescriptorElementPointer(state, pointerType, variable, arrayIndex, kind, resource, "sampled image descriptor array was not emitted"); + const auto image = state.module.AllocateId(); + state.module.AddFunction(spv::OpLoad, imageType, image, pointer); + return image; +} + +std::uint32_t LoadSamplerDescriptor(SpirvEmitterState& state, std::uint32_t sampler) { + const auto arrayIndex = ResourceForDescriptor(state, DescriptorBindingKind::Samplers, sampler); + const auto samplerType = state.module.Type(spv::OpTypeSampler); + const auto pointerType = state.module.Type(spv::OpTypePointer, spv::StorageClassUniformConstant, samplerType); + const auto pointer = DescriptorElementPointer(state, pointerType, state.samplerVariable, arrayIndex, DescriptorBindingKind::Samplers, sampler, "sampler descriptor array was not emitted"); + const auto samplerId = state.module.AllocateId(); + state.module.AddFunction(spv::OpLoad, samplerType, samplerId, pointer); + return samplerId; +} + +std::uint32_t MakeSampledImage(SpirvEmitterState& state, std::uint32_t resource, std::uint32_t sampler) { + const auto& imageResource = state.program.Info().images.at(resource); + const auto image = LoadSampledImageDescriptor(state, resource); + const auto samplerId = LoadSamplerDescriptor(state, sampler); + const auto sampledImage = state.module.AllocateId(); + const auto sampledType = state.module.Type(spv::OpTypeSampledImage, ImageType(state, imageResource)); + state.module.AddFunction(spv::OpSampledImage, sampledType, sampledImage, image, samplerId); + return sampledImage; +} + +std::uint32_t StorageImageDescriptorPointer(SpirvEmitterState& state, std::uint32_t resource) { + const auto& image = state.program.Info().images.at(resource); + if (image.resourceClass != ImageResourceClass::Storage) { + FailEmit("storage image descriptor requested for a non-storage image"); + } + const auto kind = DescriptorBindingForImage(image); + const auto arrayIndex = ResourceForDescriptor(state, kind, resource); + const auto pointerType = state.module.Type(spv::OpTypePointer, spv::StorageClassUniformConstant, ImageType(state, image)); + const auto variable = state.imageVariables.at(ImageBindingIndex(kind)); + return DescriptorElementPointer(state, pointerType, variable, arrayIndex, kind, resource, "storage image descriptor array was not emitted"); +} + +void EmitStorageImageWrite(SpirvEmitterState& state, std::uint32_t resource, std::uint32_t mipLod, std::uint32_t coord, std::uint32_t texel) { + const auto& image = state.program.Info().images.at(resource); + if (image.resourceClass != ImageResourceClass::Storage) { + FailEmit("storage image write requested for a non-storage image"); + } + if (!image.atomic) { + state.module.EmitCapability(spv::CapabilityStorageImageWriteWithoutFormat); + } + const auto kind = DescriptorBindingForImage(image); + const auto arrayIndex = ResourceForDescriptor(state, kind, resource); + const auto imageType = ImageType(state, image); + const auto pointerType = state.module.Type(spv::OpTypePointer, spv::StorageClassUniformConstant, imageType); + const auto variable = state.imageVariables.at(ImageBindingIndex(kind)); + const auto loadAt = [&](std::uint32_t index) { + const auto pointer = DescriptorElementPointer(state, pointerType, variable, index, kind, resource, "storage image descriptor array was not emitted"); + const auto descriptor = state.module.AllocateId(); + state.module.AddFunction(spv::OpLoad, imageType, descriptor, pointer); + return descriptor; + }; + if (image.mipMode != ImageMipMode::DynamicStorage) { + state.module.AddFunction(spv::OpImageWrite, loadAt(arrayIndex), coord, texel); + return; + } + if (image.mipCount == 0u) { + ExitDescriptorBindingFailure(state, kind, resource, "dynamic storage image has no mip descriptors"); + } + const auto mergeLabel = state.module.AllocateId(); + std::vector labels(image.mipCount); + std::vector words{spv::OpSwitch, mipLod, mergeLabel}; + for (std::uint32_t mip = 0; mip < image.mipCount; mip++) { + labels[mip] = state.module.AllocateId(); + words.push_back(mip); + words.push_back(labels[mip]); + } + state.module.AddFunction(spv::OpSelectionMerge, mergeLabel, spv::SelectionControlMaskNone); + state.module.AddFunction(std::span(words)); + for (std::uint32_t mip = 0; mip < image.mipCount; mip++) { + EmitLabel(state, labels[mip]); + state.module.AddFunction(spv::OpImageWrite, loadAt(arrayIndex + mip), coord, texel); + state.module.AddFunction(spv::OpBranch, mergeLabel); + } + EmitLabel(state, mergeLabel); +} + +const RdnaImageDimensionInfo& RdnaImageDimensionInfoFor(RdnaImageDimension dimension) { + for (const auto& info : ImageDimensions) { + if (info.dimension == dimension) { + return info; + } + } + FailEmit("image dimension " + std::to_string(static_cast(dimension)) + " is invalid"); +} + +} diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvFormatConvert.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvFormatConvert.cpp index 77d7f3720..c98c29f60 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvFormatConvert.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvFormatConvert.cpp @@ -1 +1,174 @@ -#include "SpirvBackend/SpirvMemoryEmitter.hpp" +#include "SpirvBackend/SpirvMemory/SpirvFormatConvert.hpp" +#include "SpirvBackend/SpirvMemory/SpirvTypes.hpp" +#include "SpirvBackend/SpirvMemory/SpirvSubgroup.hpp" +#include +#include +#include +#include +#include +#include +#include + +namespace ShaderRecompiler +{ +namespace { + + [[noreturn]] void FailEmit(const std::string& reason) { + throw std::runtime_error("SPIR-V module emission failed: " + reason); + } + +} + +std::uint32_t EmitTBufferBitcastU32ToI32(SpirvEmitterState& state, std::uint32_t value) { + const auto result = state.module.AllocateId(); + state.module.AddFunction(spv::OpBitcast, TypeI32(state), result, value); + return result; +} + +bool IsSignedFormatComponent(SpirvFormatComponentType type) { + return type == SpirvFormatComponentType::Sint || type == SpirvFormatComponentType::Snorm || type == SpirvFormatComponentType::Sscaled; +} + +std::uint32_t EmitUFloatToF32Bits(SpirvEmitterState& state, std::uint32_t raw, std::uint32_t bits) { + const std::uint32_t mantissaBits = bits == 11u ? 6u : 5u; + const std::uint32_t mantissaMask = (1u << mantissaBits) - 1u; + const auto mantissa = EmitBinaryU32(state, spv::OpBitwiseAnd, raw, ConstantU32(state, mantissaMask)); + const auto exponent = EmitBinaryU32(state, spv::OpBitwiseAnd, EmitBinaryU32(state, spv::OpShiftRightLogical, raw, ConstantU32(state, mantissaBits)), ConstantU32(state, 0x1fu)); + const auto exponent32 = EmitAddU32(state, exponent, ConstantU32(state, 127u - 15u)); + const auto exponentBits = EmitBinaryU32(state, spv::OpShiftLeftLogical, exponent32, ConstantU32(state, 23)); + const auto mantissaBits32 = EmitBinaryU32(state, spv::OpShiftLeftLogical, mantissa, ConstantU32(state, 23u - mantissaBits)); + const auto normalBits = EmitBinaryU32(state, spv::OpBitwiseOr, exponentBits, mantissaBits32); + const auto normal = EmitBitcastU32ToF32(state, normalBits); + const auto specialBits = EmitBinaryU32(state, spv::OpBitwiseOr, ConstantU32(state, 0x7f800000u), mantissaBits32); + const auto special = EmitBitcastU32ToF32(state, specialBits); + const auto mantissaF32 = state.module.AllocateId(); + const auto subnormal = state.module.AllocateId(); + state.module.AddFunction(spv::OpConvertUToF, TypeF32(state), mantissaF32, mantissa); + state.module.AddFunction(spv::OpFMul, TypeF32(state), subnormal, mantissaF32, ConstantF32Value(state, std::ldexp(1.0f, 1 - 15 - static_cast(mantissaBits)))); + const auto zeroExponent = EmitCompareU32Constant(state, spv::OpIEqual, exponent, 0); + const auto specialExponent = EmitCompareU32Constant(state, spv::OpIEqual, exponent, 31); + const auto finite = EmitTBufferSelectF32(state, zeroExponent, subnormal, normal); + const auto result = EmitTBufferSelectF32(state, specialExponent, special, finite); + return EmitBitcastF32ToU32(state, result); +} + +std::uint32_t NormalizeFormatComponent(SpirvEmitterState& state, const SpirvBufferFormatInfo& info, std::uint32_t component, std::uint32_t raw) { + const auto bits = info.componentBits[component]; + switch (info.type) { + case SpirvFormatComponentType::Uint: + case SpirvFormatComponentType::Sint: + return raw; + case SpirvFormatComponentType::Uscaled: { + const auto value = state.module.AllocateId(); + state.module.AddFunction(spv::OpConvertUToF, TypeF32(state), value, raw); + return EmitBitcastF32ToU32(state, value); + } + case SpirvFormatComponentType::Sscaled: { + const auto signedRaw = EmitTBufferBitcastU32ToI32(state, raw); + const auto value = state.module.AllocateId(); + state.module.AddFunction(spv::OpConvertSToF, TypeF32(state), value, signedRaw); + return EmitBitcastF32ToU32(state, value); + } + case SpirvFormatComponentType::Unorm: { + const auto value = state.module.AllocateId(); + const auto normalized = state.module.AllocateId(); + const auto maxValue = static_cast((1u << bits) - 1u); + state.module.AddFunction(spv::OpConvertUToF, TypeF32(state), value, raw); + state.module.AddFunction(spv::OpFDiv, TypeF32(state), normalized, value, ConstantF32Value(state, maxValue)); + return EmitBitcastF32ToU32(state, normalized); + } + case SpirvFormatComponentType::Snorm: { + const auto signedRaw = EmitTBufferBitcastU32ToI32(state, raw); + const auto value = state.module.AllocateId(); + const auto normalized = state.module.AllocateId(); + const auto clamped = state.module.AllocateId(); + const auto maxValue = static_cast((1u << (bits - 1u)) - 1u); + state.module.AddFunction(spv::OpConvertSToF, TypeF32(state), value, signedRaw); + state.module.AddFunction(spv::OpFDiv, TypeF32(state), normalized, value, ConstantF32Value(state, maxValue)); + state.module.AddFunction(spv::OpExtInst, TypeF32(state), clamped, GlslStd450(state), GLSLstd450FMax, normalized, ConstantF32Value(state, -1.0f)); + return EmitBitcastF32ToU32(state, clamped); + } + case SpirvFormatComponentType::Float: + if (bits == 32u) { + return raw; + } + if (bits == 16u) { + return EmitBitcastF32ToU32(state, EmitF16BitsToF32(state, raw)); + } + return EmitUFloatToF32Bits(state, raw, bits); + default: + FailEmit("buffer format component type is not supported"); + } +} + +void EmitDeviceAtomicMemoryBarrier(SpirvEmitterState& state) { + const auto semantics = spv::MemorySemanticsAcquireReleaseMask | spv::MemorySemanticsUniformMemoryMask; + state.module.AddFunction(spv::OpMemoryBarrier, ConstantU32(state, spv::ScopeDevice), ConstantU32(state, semantics)); +} + +std::uint32_t EmitFloatAtomicReplacement(SpirvEmitterState& state, std::uint32_t old, std::uint32_t source, bool maxValue) { + struct OrderedBits { + std::uint32_t nan; + std::uint32_t zero; + std::uint32_t key; + }; + const auto classify = [&](std::uint32_t bits) { + const auto cls = EmitClassifyF32Bits(state, bits); + const auto negative = EmitCompareU32Constant(state, spv::OpINotEqual, EmitAndConstant(state, bits, 0x80000000u), 0u); + const auto negativeKey = state.module.AllocateId(); + state.module.AddFunction(spv::OpNot, TypeU32(state), negativeKey, bits); + const auto positiveKey = EmitBinaryU32(state, spv::OpBitwiseXor, bits, ConstantU32(state, 0x80000000u)); + return OrderedBits{cls.nan, cls.zero, EmitSelectValueU32(state, negative, negativeKey, positiveKey)}; + }; + const auto sourceClass = classify(source); + const auto oldClass = classify(old); + const auto unordered = EmitLogicalOrBool(state, EmitLogicalOrBool(state, sourceClass.nan, oldClass.nan), EmitLogicalAndBool(state, sourceClass.zero, oldClass.zero)); + const auto compare = state.module.AllocateId(); + state.module.AddFunction(maxValue ? spv::OpUGreaterThan : spv::OpULessThan, TypeBool(state), compare, sourceClass.key, oldClass.key); + return EmitSelectValueU32(state, EmitLogicalAndBool(state, EmitLogicalNotBool(state, unordered), compare), source, old); +} + +std::uint32_t EmitDsSwizzleTargetLane(SpirvEmitterState& state, std::uint32_t subid, std::uint32_t control) { + if ((control & 0xc000u) == 0xc000u) { + const std::uint32_t mask = control & 0x1fu; + const std::uint32_t rotate = (control >> 5u) & 0x1fu; + const std::uint32_t rotateDelta = (control & 0x400u) != 0u ? ((32u - rotate) & 0x1fu) : rotate; + const auto lane = EmitAndConstant(state, subid, 31); + const auto rotatedSum = EmitAddU32(state, lane, ConstantU32(state, rotateDelta)); + const auto rotated = EmitAndConstant(state, rotatedSum, 31); + const auto kept = EmitAndConstant(state, lane, mask); + const auto moved = EmitAndConstant(state, rotated, (~mask) & 31u); + const auto combined = EmitOrU32(state, kept, moved); + const auto base = EmitAndConstant(state, subid, 0xffffffe0u); + return EmitOrU32(state, base, combined); + } + if ((control & 0x8000u) != 0u) { + const auto lane2 = state.module.AllocateId(); + const auto shift = state.module.AllocateId(); + const auto perm0 = state.module.AllocateId(); + const auto perm = state.module.AllocateId(); + const auto base = state.module.AllocateId(); + const auto target = state.module.AllocateId(); + state.module.AddFunction(spv::OpBitwiseAnd, TypeU32(state), lane2, subid, ConstantU32(state, 3)); + state.module.AddFunction(spv::OpShiftLeftLogical, TypeU32(state), shift, lane2, ConstantU32(state, 1)); + state.module.AddFunction(spv::OpShiftRightLogical, TypeU32(state), perm0, ConstantU32(state, control), shift); + state.module.AddFunction(spv::OpBitwiseAnd, TypeU32(state), perm, perm0, ConstantU32(state, 3)); + state.module.AddFunction(spv::OpBitwiseAnd, TypeU32(state), base, subid, ConstantU32(state, 0xfffffffcu)); + state.module.AddFunction(spv::OpBitwiseOr, TypeU32(state), target, base, perm); + return target; + } + const auto lane = state.module.AllocateId(); + const auto masked = state.module.AllocateId(); + const auto ored = state.module.AllocateId(); + const auto xored = state.module.AllocateId(); + const auto base = state.module.AllocateId(); + const auto target = state.module.AllocateId(); + state.module.AddFunction(spv::OpBitwiseAnd, TypeU32(state), lane, subid, ConstantU32(state, 31)); + state.module.AddFunction(spv::OpBitwiseAnd, TypeU32(state), masked, lane, ConstantU32(state, control & 0x1fu)); + state.module.AddFunction(spv::OpBitwiseOr, TypeU32(state), ored, masked, ConstantU32(state, (control >> 5u) & 0x1fu)); + state.module.AddFunction(spv::OpBitwiseXor, TypeU32(state), xored, ored, ConstantU32(state, (control >> 10u) & 0x1fu)); + state.module.AddFunction(spv::OpBitwiseAnd, TypeU32(state), base, subid, ConstantU32(state, 0xffffffe0u)); + state.module.AddFunction(spv::OpBitwiseOr, TypeU32(state), target, base, xored); + return target; +} +} diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvInputOutput.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvInputOutput.cpp index 77d7f3720..f5ed11ea5 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvInputOutput.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvInputOutput.cpp @@ -1 +1,252 @@ -#include "SpirvBackend/SpirvMemoryEmitter.hpp" +#include "SpirvBackend/SpirvMemory/SpirvInputOutput.hpp" +#include "SpirvBackend/SpirvMemory/SpirvTypes.hpp" +#include "SpirvBackend/SpirvMemory/SpirvSubgroup.hpp" +#include +#include +#include +#include +#include +#include +#include + +namespace ShaderRecompiler +{ + namespace { + + constexpr std::uint32_t PsInputOffsetMask = 0x0000001fu; + constexpr std::uint32_t PsInputFlatShade = 0x00000400u; + constexpr std::uint32_t PixelParameterLimit = 32u; + + [[noreturn]] void FailEmit(const std::string& reason) { + throw std::runtime_error("SPIR-V module emission failed: " + reason); + } + + IrShaderStage StageOf(const SpirvEmitterState& state) { + return state.program.Resources().stage; + } + + const ShaderVertexInputInfo& VertexInfo(const SpirvEmitterState& state) { + if (state.inputInfo.vertex == nullptr) { + FailEmit("vertex input info is missing"); + } + return *state.inputInfo.vertex; + } + + const ShaderPixelInputInfo& PixelInfo(const SpirvEmitterState& state) { + if (state.inputInfo.pixel == nullptr) { + FailEmit("pixel input info is missing"); + } + return *state.inputInfo.pixel; + } + + bool IsVertexLikeStage(const SpirvEmitterState& state) { + 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; + } + return input; + } + + bool PixelInputIsCustom(const ShaderPixelInputInfo& info, std::uint32_t input) { + return input < PixelParameterLimit && (info.customInterpolationMask & (1u << input)) != 0u; + } + + 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 activeInputs, std::uint32_t input) { + std::array 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 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::uint32_t PixelParameterLocation(const SpirvEmitterState& state, std::uint32_t attr) { + if (StageOf(state) != IrShaderStage::Pixel) { + return attr; + } + std::array 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(activeInputs.data(), activeCount), attr); + } + + bool PixelParameterIsFlat(const SpirvEmitterState& state, std::uint32_t attr) { + return StageOf(state) == IrShaderStage::Pixel && PixelInputIsFlat(PixelInfo(state), attr); + } + + bool PixelParameterIsCustom(const SpirvEmitterState& state, std::uint32_t attr) { + return StageOf(state) == IrShaderStage::Pixel && PixelInputIsCustom(PixelInfo(state), attr); + } + + VertexInputScalarKind VertexParameterScalarKind(const SpirvEmitterState& state, std::uint32_t location) { + if (!IsVertexLikeStage(state)) { + return VertexInputScalarKind::Float; + } + const auto& vertex = VertexInfo(state); + if (location >= static_cast(ShaderVertexInputInfo::MaxResources) || location >= static_cast(vertex.resourcesNum)) { + return VertexInputScalarKind::Float; + } + switch (vertex.resources[location].Format()) { + case IrBufferFormat::Format8UInt: + case IrBufferFormat::Format16UInt: + case IrBufferFormat::Format8_8UInt: + case IrBufferFormat::Format32UInt: + case IrBufferFormat::Format16_16UInt: + case IrBufferFormat::Format8_8_8_8UInt: + case IrBufferFormat::Format32_32UInt: + case IrBufferFormat::Format16_16_16_16UInt: + case IrBufferFormat::Format32_32_32UInt: + case IrBufferFormat::Format32_32_32_32UInt: return VertexInputScalarKind::Uint; + case IrBufferFormat::Format8SInt: + case IrBufferFormat::Format16SInt: + case IrBufferFormat::Format8_8SInt: + case IrBufferFormat::Format32SInt: + case IrBufferFormat::Format16_16SInt: + case IrBufferFormat::Format8_8_8_8SInt: + case IrBufferFormat::Format32_32SInt: + case IrBufferFormat::Format16_16_16_16SInt: + case IrBufferFormat::Format32_32_32SInt: + case IrBufferFormat::Format32_32_32_32SInt: return VertexInputScalarKind::Sint; + default: return VertexInputScalarKind::Float; + } + } + + std::uint32_t VertexParameterComponentCount(const SpirvInputBinding& input) { + return std::clamp(input.componentCount, 1u, 4u); + } + + std::uint32_t VertexParameterScalarType(SpirvEmitterState& state, VertexInputScalarKind kind) { + switch (kind) { + case VertexInputScalarKind::Sint: return TypeI32(state); + case VertexInputScalarKind::Uint: return TypeU32(state); + case VertexInputScalarKind::Float: return TypeF32(state); + } + FailEmit("vertex input scalar kind is invalid"); + } + + std::uint32_t OutputVariableForExport(const SpirvEmitterState& state, const ExportInfo& exp) { + if (exp.kind == ExportTargetKind::Position && exp.index == 0) { + return state.perVertexVariable; + } + if (exp.kind == ExportTargetKind::MrtZ) { + return state.depthVariable; + } + const auto expectedKind = exp.kind == ExportTargetKind::Mrt ? StageOutputKind::Mrt : StageOutputKind::Parameter; + for (const auto& binding : state.outputs) { + if (binding.kind == expectedKind && binding.index == exp.index) { + return binding.variableId; + } + } + return 0; + } + + std::uint32_t InputVariableForKind(const SpirvEmitterState& state, StageInputKind kind) { + for (const auto& input : state.inputs) { + if (input.kind == kind) { + return input.variableId; + } + } + return 0; + } + + const SpirvInputBinding* SpirvInputBindingForParameter(const SpirvEmitterState& state, std::uint32_t location) { + for (const auto& input : state.inputs) { + if (input.kind == StageInputKind::Parameter && input.location == location) { + return &input; + } + } + return nullptr; + } + + std::uint32_t EmitVertexParameterComponentU32(SpirvEmitterState& state, const SpirvInputBinding& input, std::uint32_t component) { + const auto count = VertexParameterComponentCount(input); + const auto kind = VertexParameterScalarKind(state, input.location); + const auto scalarType = VertexParameterScalarType(state, kind); + const auto raw = state.module.AllocateId(); + if (count == 1u) { + state.module.AddFunction(spv::OpLoad, scalarType, raw, input.variableId); + } else { + const auto pointerType = TypePointer(state, spv::StorageClassInput, scalarType); + const auto pointer = state.module.AllocateId(); + state.module.AddFunction(spv::OpAccessChain, pointerType, pointer, input.variableId, ConstantU32(state, component)); + state.module.AddFunction(spv::OpLoad, scalarType, raw, pointer); + } + if (kind == VertexInputScalarKind::Uint) { + return raw; + } + const auto bits = state.module.AllocateId(); + state.module.AddFunction(spv::OpBitcast, TypeU32(state), bits, raw); + return bits; + } + + std::uint32_t EmitInputComponentU32(SpirvEmitterState& state, StageInputKind kind, std::uint32_t component) { + const auto variable = InputVariableForKind(state, kind); + if (variable == 0) { + FailEmit("stage input " + std::to_string(static_cast(kind)) + " was not declared"); + } + const auto pointer = state.module.AllocateId(); + const auto value = state.module.AllocateId(); + state.module.AddFunction(spv::OpAccessChain, TypePointer(state, spv::StorageClassInput, TypeU32(state)), pointer, variable, ConstantU32(state, component)); + state.module.AddFunction(spv::OpLoad, TypeU32(state), value, pointer); + return value; + } + + std::uint32_t EmitLocalInvocationIndex(SpirvEmitterState& state) { + const auto variable = InputVariableForKind(state, StageInputKind::LocalInvocationIndex); + if (variable == 0) { + FailEmit("LocalInvocationIndex was not declared"); + } + const auto value = state.module.AllocateId(); + state.module.AddFunction(spv::OpLoad, TypeU32(state), value, variable); + if (state.laneCount == 2) { + const auto waveBase = EmitBinaryU32(state, spv::OpBitwiseAnd, value, ConstantU32(state, ~31u)); + return EmitAddU32(state, EmitAddU32(state, value, waveBase), ConstantU32(state, state.laneHalf * 32)); + } + return value; + } +} diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvModuleSetup.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvModuleSetup.cpp index 77d7f3720..80221d4db 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvModuleSetup.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvModuleSetup.cpp @@ -1 +1,158 @@ #include "SpirvBackend/SpirvMemoryEmitter.hpp" +#include +#include + +namespace ShaderRecompiler +{ + +void EmitModuleHeader(SpirvModule& module, const IrProgram& program, const BindingAllocationResult& bindings) { + CheckBindings(program, bindings); + EmitBaseHeader(module, program); +} + +void EmitModuleHeader(SpirvEmitterState& state, const BindingAllocationResult& bindings) { + CheckBindings(state.program, bindings); + DefineModule(state); +} + +std::uint32_t ExecutionModelForStage(IrShaderStage stage) { + switch (stage) { + case IrShaderStage::Local: + case IrShaderStage::Vertex: return spv::ExecutionModelVertex; + case IrShaderStage::TessellationControl: return spv::ExecutionModelTessellationControl; + case IrShaderStage::TessellationEvaluation: return spv::ExecutionModelTessellationEvaluation; + case IrShaderStage::Mesh: return spv::ExecutionModelMeshEXT; + case IrShaderStage::Pixel: return spv::ExecutionModelFragment; + case IrShaderStage::Compute: return spv::ExecutionModelGLCompute; + default: FailEmit("shader stage has no execution model"); + } +} + +std::uint32_t DefineInterfaceVariable(SpirvEmitterState& state, std::uint32_t type, std::uint32_t storage, const char* name) { + const auto variable = state.module.DefineGlobalVariable(TypePointer(state, storage, type), storage); + state.interfaceVariables.push_back(variable); + state.module.AddName(variable, name); + return variable; +} + +void DefineModule(SpirvEmitterState& state) { + state.interfaceVariables.reserve(state.program.Info().inputs.size() + state.program.Info().outputs.size()); + DefineInputs(state); + DefineOutputs(state); + DefineTessellationInterfaces(state); + DefineDescriptors(state); + if (state.requirements.functionLds) { + state.ldsVariable = state.module.AllocateId(); + } + if (state.requirements.functionScratch) { + for (std::uint32_t half = 0; half < state.laneCount; half++) { + state.scratchVariable.at(half) = state.module.AllocateId(); + } + } + state.mainFunc = state.module.AllocateId(); + if (StageOf(state) == IrShaderStage::Mesh) { + const auto& mesh = VertexInfo(state).mesh; + state.meshGuestFunc = state.module.AllocateId(); + state.module.EmitCapability(spv::CapabilityMeshShadingEXT); + state.module.EmitExtension("SPV_EXT_mesh_shader"); + state.module.AddExecutionMode(state.mainFunc, spv::ExecutionModeOutputTrianglesEXT); + state.module.AddExecutionMode(state.mainFunc, spv::ExecutionModeOutputVertices, mesh.maxVertices); + state.module.AddExecutionMode(state.mainFunc, spv::ExecutionModeOutputPrimitivesEXT, mesh.maxPrimitives); + } + if (StageOf(state) == IrShaderStage::TessellationControl || StageOf(state) == IrShaderStage::TessellationEvaluation) { + DefineTessellationExecutionModes(state); + } + state.entryLabel = state.module.AllocateId(); + EmitBaseHeader(state.module, state.program); + for (const auto capability : state.requirements.capabilities) { + state.module.EmitCapability(capability); + } + for (const auto& extension : state.requirements.extensions) { + state.module.EmitExtension(extension); + } + if (state.requirements.bufferInt64Atomics) { + state.module.EmitCapability(spv::CapabilityInt64); + state.module.EmitCapability(spv::CapabilityInt64Atomics); + } + if (state.clipDistanceVariable != 0) { + state.module.EmitCapability(spv::CapabilityClipDistance); + } + if (state.cullDistanceVariable != 0) { + state.module.EmitCapability(spv::CapabilityCullDistance); + } + if (state.layerVariable != 0 || InputVariableForKind(state, StageInputKind::Layer) != 0) { + state.module.RequireVersion(0x00010500u); + state.module.EmitCapability(spv::CapabilityShaderLayer); + } + if (state.viewportIndexVariable != 0) { + state.module.RequireVersion(0x00010500u); + state.module.EmitCapability(spv::CapabilityShaderViewportIndex); + } + if (InputVariableForKind(state, StageInputKind::SampleId) != 0) { + state.module.EmitCapability(spv::CapabilitySampleRateShading); + } + if (state.requirements.imageGatherExtended) { + state.module.EmitCapability(spv::CapabilityImageGatherExtended); + } + if (state.laneCount == 2 || state.requirements.subgroupBallot || state.requirements.subgroupShuffle || state.requirements.subgroupLocalInvocationId) { + state.module.EmitCapability(spv::CapabilityGroupNonUniform); + } + if (state.laneCount == 2 || state.requirements.subgroupBallot) { + state.module.EmitCapability(spv::CapabilityGroupNonUniformBallot); + } + if (state.requirements.subgroupShuffle) { + state.module.EmitCapability(spv::CapabilityGroupNonUniformShuffle); + } + if (state.requirements.computeDerivatives && StageOf(state) == IrShaderStage::Compute) { + state.module.EmitCapability(spv::CapabilityComputeDerivativeGroupQuadsKHR); + state.module.EmitExtension("SPV_KHR_compute_shader_derivatives"); + } + const bool fragmentBarycentric = StageOf(state) == IrShaderStage::Pixel && std::any_of(state.inputs.begin(), state.inputs.end(), [](const SpirvInputBinding& input) { + return input.perVertex || input.kind == StageInputKind::BaryCoordSmooth || input.kind == StageInputKind::BaryCoordNoPerspective; + }); + if (fragmentBarycentric) { + state.module.EmitCapability(spv::CapabilityFragmentBarycentricKHR); + state.module.EmitExtension("SPV_KHR_fragment_shader_barycentric"); + } + state.module.AddExecutionMode(state.mainFunc, spv::ExecutionModeSignedZeroInfNanPreserve, 32u); + if (const auto* workgroup = ShaderWorkgroupInput(state)) { + const std::uint32_t derivativeDefault = state.requirements.computeDerivatives ? 2u : 1u; + std::uint32_t localX = workgroup->threadsNum[0] != 0u ? workgroup->threadsNum[0] : derivativeDefault; + std::uint32_t localY = workgroup->threadsNum[1] != 0u ? workgroup->threadsNum[1] : derivativeDefault; + std::uint32_t localZ = workgroup->threadsNum[2] != 0u ? workgroup->threadsNum[2] : 1u; + if (state.laneCount == 2) { + localX = ((localX * localY * localZ + 63u) / 64u) * 32u; + localY = 1u; + localZ = 1u; + if (state.requirements.computeDerivatives) { + localY = localX / 2u; + localX = 2u; + } + } + state.module.AddExecutionMode(state.mainFunc, spv::ExecutionModeLocalSize, localX, localY, localZ); + } + if (StageOf(state) == IrShaderStage::Pixel) { + const auto& pixel = PixelInfo(state); + state.module.AddExecutionMode(state.mainFunc, spv::ExecutionModeOriginUpperLeft); + if (state.depthVariable != 0) { + state.module.AddExecutionMode(state.mainFunc, spv::ExecutionModeDepthReplacing); + } + if (pixel.psEarlyZ && !pixel.psPixelKillEnable && !pixel.psDepthExportEnable && !pixel.psSampleMaskExportEnable) { + state.module.AddExecutionMode(state.mainFunc, spv::ExecutionModeEarlyFragmentTests); + } + } + if (state.requirements.computeDerivatives && StageOf(state) == IrShaderStage::Compute) { + state.module.AddExecutionMode(state.mainFunc, spv::ExecutionModeDerivativeGroupQuadsKHR); + } + state.module.AddName(state.mainFunc, "main"); + if (state.requirements.functionLds) { + state.module.AddName(state.ldsVariable, "lds_dwords"); + } + if (state.requirements.functionScratch) { + for (std::uint32_t half = 0; half < state.laneCount; half++) { + state.module.AddName(state.scratchVariable.at(half), "scratch_dwords"); + } + } +} + +} diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvSubgroup.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvSubgroup.cpp index 77d7f3720..314e04cd0 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvSubgroup.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvSubgroup.cpp @@ -1 +1,162 @@ -#include "SpirvBackend/SpirvMemoryEmitter.hpp" +#include "SpirvBackend/SpirvEmitter.hpp" +#include +#include +#include +#include +#include +#include +#include + +namespace ShaderRecompiler +{ + struct SpirvEmitterState; + + namespace { + + [[noreturn]] void FailEmit(const std::string& reason) { + throw std::runtime_error("SPIR-V module emission failed: " + reason); + } + + } + + std::uint32_t EmitSubgroupLocalInvocationId(SpirvEmitterState& state) { + if (state.subgroupLocalInvocationIdVariable == 0) { + FailEmit("SubgroupLocalInvocationId was not declared before function emission"); + } + const auto value = state.module.AllocateId(); + state.module.AddFunction(spv::OpLoad, TypeU32(state), value, state.subgroupLocalInvocationIdVariable); + return state.laneHalf == 0 ? value : EmitAddU32(state, value, ConstantU32(state, 32)); + } + + DppTargetLane EmitDppQuadPermTargetLane(SpirvEmitterState& state, std::uint32_t subid, std::uint32_t control) { + constexpr std::uint32_t laneBits = 2u; + constexpr std::uint32_t laneMask = (1u << laneBits) - 1u; + const auto groupBase = state.module.AllocateId(); + const auto lane = state.module.AllocateId(); + const auto shift = state.module.AllocateId(); + const auto selectedRaw = state.module.AllocateId(); + const auto selected = state.module.AllocateId(); + const auto target = state.module.AllocateId(); + state.module.AddFunction(spv::OpBitwiseAnd, TypeU32(state), groupBase, subid, ConstantU32(state, ~laneMask)); + state.module.AddFunction(spv::OpBitwiseAnd, TypeU32(state), lane, subid, ConstantU32(state, laneMask)); + state.module.AddFunction(spv::OpIMul, TypeU32(state), shift, lane, ConstantU32(state, laneBits)); + state.module.AddFunction(spv::OpShiftRightLogical, TypeU32(state), selectedRaw, ConstantU32(state, control), shift); + state.module.AddFunction(spv::OpBitwiseAnd, TypeU32(state), selected, selectedRaw, ConstantU32(state, laneMask)); + state.module.AddFunction(spv::OpBitwiseOr, TypeU32(state), target, groupBase, selected); + return {target, ConstantBool(state, true)}; + } + + DppTargetLane EmitDppRowShiftTargetLane(SpirvEmitterState& state, std::uint32_t subid, std::uint32_t amount, bool left) { + const auto row = state.module.AllocateId(); + const auto lane = state.module.AllocateId(); + const auto laneShifted = state.module.AllocateId(); + const auto target = state.module.AllocateId(); + const auto valid = state.module.AllocateId(); + state.module.AddFunction(spv::OpBitwiseAnd, TypeU32(state), row, subid, ConstantU32(state, 0xfffffff0u)); + state.module.AddFunction(spv::OpBitwiseAnd, TypeU32(state), lane, subid, ConstantU32(state, 15)); + if (left) { + state.module.AddFunction(spv::OpIAdd, TypeU32(state), laneShifted, lane, ConstantU32(state, amount)); + state.module.AddFunction(spv::OpULessThan, TypeBool(state), valid, lane, ConstantU32(state, 16u - amount)); + } else { + state.module.AddFunction(spv::OpISub, TypeU32(state), laneShifted, lane, ConstantU32(state, amount)); + state.module.AddFunction(spv::OpUGreaterThanEqual, TypeBool(state), valid, lane, ConstantU32(state, amount)); + } + state.module.AddFunction(spv::OpBitwiseOr, TypeU32(state), target, row, laneShifted); + return {target, valid}; + } + + DppTargetLane EmitDppRowRotateRightTargetLane(SpirvEmitterState& state, std::uint32_t subid, std::uint32_t amount) { + const auto row = state.module.AllocateId(); + const auto lane = state.module.AllocateId(); + const auto inHigh = state.module.AllocateId(); + const auto minus = state.module.AllocateId(); + const auto plus = state.module.AllocateId(); + const auto selected = state.module.AllocateId(); + const auto target = state.module.AllocateId(); + state.module.AddFunction(spv::OpBitwiseAnd, TypeU32(state), row, subid, ConstantU32(state, 0xfffffff0u)); + state.module.AddFunction(spv::OpBitwiseAnd, TypeU32(state), lane, subid, ConstantU32(state, 15)); + state.module.AddFunction(spv::OpUGreaterThanEqual, TypeBool(state), inHigh, lane, ConstantU32(state, amount)); + state.module.AddFunction(spv::OpISub, TypeU32(state), minus, lane, ConstantU32(state, amount)); + state.module.AddFunction(spv::OpIAdd, TypeU32(state), plus, lane, ConstantU32(state, 16u - amount)); + state.module.AddFunction(spv::OpSelect, TypeU32(state), selected, inHigh, minus, plus); + state.module.AddFunction(spv::OpBitwiseOr, TypeU32(state), target, row, selected); + return {target, ConstantBool(state, true)}; + } + + DppTargetLane EmitDppMirrorTargetLane(SpirvEmitterState& state, std::uint32_t subid, bool halfRow) { + const std::uint32_t baseMask = halfRow ? 0xfffffff8u : 0xfffffff0u; + const std::uint32_t laneMask = halfRow ? 7u : 15u; + const auto base = state.module.AllocateId(); + const auto lane = state.module.AllocateId(); + const auto mirrored = state.module.AllocateId(); + const auto target = state.module.AllocateId(); + state.module.AddFunction(spv::OpBitwiseAnd, TypeU32(state), base, subid, ConstantU32(state, baseMask)); + state.module.AddFunction(spv::OpBitwiseAnd, TypeU32(state), lane, subid, ConstantU32(state, laneMask)); + state.module.AddFunction(spv::OpISub, TypeU32(state), mirrored, ConstantU32(state, laneMask), lane); + state.module.AddFunction(spv::OpBitwiseOr, TypeU32(state), target, base, mirrored); + return {target, ConstantBool(state, true)}; + } + + DppTargetLane EmitDppTargetLane(SpirvEmitterState& state, std::uint32_t control) { + const auto subid = EmitSubgroupLocalInvocationId(state); + if (control <= 0xffu) { + return EmitDppQuadPermTargetLane(state, subid, control); + } + if (control >= 0x101u && control <= 0x10fu) { + return EmitDppRowShiftTargetLane(state, subid, control & 0xfu, true); + } + if (control >= 0x111u && control <= 0x11fu) { + return EmitDppRowShiftTargetLane(state, subid, control & 0xfu, false); + } + if (control >= 0x121u && control <= 0x12fu) { + return EmitDppRowRotateRightTargetLane(state, subid, control & 0xfu); + } + if (control == 0x140u) { + return EmitDppMirrorTargetLane(state, subid, false); + } + if (control == 0x141u) { + return EmitDppMirrorTargetLane(state, subid, true); + } + if (control >= 0x160u && control <= 0x16fu) { + const auto target = state.module.AllocateId(); + state.module.AddFunction(spv::OpBitwiseXor, TypeU32(state), target, subid, ConstantU32(state, control & 0xfu)); + return {target, ConstantBool(state, true)}; + } + FailEmit("DPP control " + std::to_string(control) + " is not supported"); + } + + std::uint32_t EmitBallotLaneActiveBool(SpirvEmitterState& state, std::uint32_t ballot, std::uint32_t lane) { + const auto waveSize = state.program.WaveSize(); + const auto low = state.module.AllocateId(); + state.module.AddFunction(spv::OpCompositeExtract, TypeU32(state), low, ballot, 0u); + std::uint32_t mask = low; + if (waveSize == 64u) { + const auto high = state.module.AllocateId(); + const auto inHigh = state.module.AllocateId(); + const auto selected = state.module.AllocateId(); + state.module.AddFunction(spv::OpCompositeExtract, TypeU32(state), high, ballot, 1u); + state.module.AddFunction(spv::OpUGreaterThanEqual, TypeBool(state), inHigh, lane, ConstantU32(state, 32)); + state.module.AddFunction(spv::OpSelect, TypeU32(state), selected, inHigh, high, low); + mask = selected; + } + const auto laneLow = state.module.AllocateId(); + const auto bit = state.module.AllocateId(); + state.module.AddFunction(spv::OpBitwiseAnd, TypeU32(state), laneLow, lane, ConstantU32(state, 31)); + state.module.AddFunction(spv::OpShiftLeftLogical, TypeU32(state), bit, ConstantU32(state, 1), laneLow); + const auto hit = state.module.AllocateId(); + const auto active = state.module.AllocateId(); + const auto inRange = state.module.AllocateId(); + const auto result = state.module.AllocateId(); + state.module.AddFunction(spv::OpBitwiseAnd, TypeU32(state), hit, mask, bit); + state.module.AddFunction(spv::OpINotEqual, TypeBool(state), active, hit, ConstantU32(state, 0)); + state.module.AddFunction(spv::OpULessThan, TypeBool(state), inRange, lane, ConstantU32(state, waveSize)); + state.module.AddFunction(spv::OpLogicalAnd, TypeBool(state), result, active, inRange); + return result; + } + + std::uint32_t EmitSubgroupLaneActiveBool(SpirvEmitterState& state, std::uint32_t lane) { + const auto activeBallot = state.module.AllocateId(); + state.module.AddFunction(spv::OpGroupNonUniformBallot, TypeU32Vector(state, 4), activeBallot, ConstantU32(state, spv::ScopeSubgroup), ConstantBool(state, true)); + return EmitBallotLaneActiveBool(state, activeBallot, lane); + } +} diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvTypes.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvTypes.cpp index 77d7f3720..1525d7036 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvTypes.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvTypes.cpp @@ -1 +1,135 @@ #include "SpirvBackend/SpirvMemoryEmitter.hpp" +#include +#include +#include +#include +#include + +namespace ShaderRecompiler +{ + +std::uint32_t TypeVoid(SpirvEmitterState& state) { + return state.module.Type(spv::OpTypeVoid); +} + +std::uint32_t TypeBool(SpirvEmitterState& state) { + return state.module.Type(spv::OpTypeBool); +} + +std::uint32_t TypeBoolVector(SpirvEmitterState& state, std::uint32_t components) { + return state.module.Type(spv::OpTypeVector, TypeBool(state), components); +} + +std::uint32_t TypeU32(SpirvEmitterState& state) { + return state.module.Type(spv::OpTypeInt, 32u, 0u); +} + +std::uint32_t TypeU64(SpirvEmitterState& state) { + return TypeU32Vector(state, 2); +} + +std::uint32_t TypeScalarU64(SpirvEmitterState& state) { + return state.module.Type(spv::OpTypeInt, 64u, 0u); +} + +std::uint32_t TypeU32Pair(SpirvEmitterState& state) { + const auto element = TypeU32(state); + return state.module.Type(spv::OpTypeStruct, element, element); +} + +std::uint32_t TypeI32(SpirvEmitterState& state) { + return state.module.Type(spv::OpTypeInt, 32u, 1u); +} + +std::uint32_t TypeI32Pair(SpirvEmitterState& state) { + const auto element = TypeI32(state); + return state.module.Type(spv::OpTypeStruct, element, element); +} + +std::uint32_t TypeF32(SpirvEmitterState& state) { + return state.module.Type(spv::OpTypeFloat, 32u); +} + +std::uint32_t TypeU32Vector(SpirvEmitterState& state, std::uint32_t components) { + return state.module.Type(spv::OpTypeVector, TypeU32(state), components); +} + +std::uint32_t TypeU32Composite(SpirvEmitterState& state, std::uint32_t components) { + if (components < 2u || components > 4u) { + FailEmit("u32 composite component count is out of range"); + } + return components == 2u ? TypeU32Pair(state) : TypeU32Vector(state, components); +} + +std::uint32_t TypeI32Vector(SpirvEmitterState& state, std::uint32_t components) { + return state.module.Type(spv::OpTypeVector, TypeI32(state), components); +} + +std::uint32_t TypeF32Vector(SpirvEmitterState& state, std::uint32_t components) { + return state.module.Type(spv::OpTypeVector, TypeF32(state), components); +} + +std::uint32_t TypePointer(SpirvEmitterState& state, std::uint32_t storageClass, std::uint32_t pointee) { + return state.module.Type(spv::OpTypePointer, storageClass, pointee); +} + +std::uint32_t TypeFunction(SpirvEmitterState& state) { + return state.module.Type(spv::OpTypeFunction, TypeVoid(state)); +} + +std::uint32_t TypeStorageBufferPointer(SpirvEmitterState& state) { + return TypePointer(state, spv::StorageClassStorageBuffer, StorageBufferType(state)); +} + +std::uint32_t TypeStorageBufferElementPointer(SpirvEmitterState& state) { + return TypePointer(state, spv::StorageClassStorageBuffer, TypeU32(state)); +} + +std::uint32_t TypeStorageBufferU64Pointer(SpirvEmitterState& state) { + return TypePointer(state, spv::StorageClassStorageBuffer, StorageBufferU64Type(state)); +} + +std::uint32_t TypeStorageBufferU64ElementPointer(SpirvEmitterState& state) { + return TypePointer(state, spv::StorageClassStorageBuffer, TypeScalarU64(state)); +} + +std::uint32_t TypePhysicalU32Pointer(SpirvEmitterState& state) { + return TypePointer(state, spv::StorageClassPhysicalStorageBuffer, TypeU32(state)); +} + +std::uint32_t TypePushConstantElementPointer(SpirvEmitterState& state) { + return TypePointer(state, spv::StorageClassPushConstant, TypeU32(state)); +} + +std::uint32_t TypeU32ArrayPointer(SpirvEmitterState& state, std::uint32_t storageClass, std::uint32_t dwords) { + const auto count = ConstantU32(state, std::max(dwords, 1u)); + const auto array = state.module.Type(spv::OpTypeArray, TypeU32(state), count); + return TypePointer(state, storageClass, array); +} + +std::uint32_t TypeU32ElementPointer(SpirvEmitterState& state, std::uint32_t storageClass) { + return TypePointer(state, storageClass, TypeU32(state)); +} + +std::uint32_t TypeId(SpirvEmitterState& state, IrType type) { + switch (type) { + case IrType::Bool: return TypeBool(state); + case IrType::U8: + case IrType::U16: + case IrType::U32: + case IrType::F16: return TypeU32(state); + case IrType::U64: return TypeU64(state); + case IrType::Vec2U32: return TypeU32Pair(state); + case IrType::F32: return TypeF32(state); + case IrType::Vec3U32: return TypeU32Vector(state, 3); + case IrType::Vec4U32: return TypeU32Vector(state, 4); + case IrType::Vec2F32: return TypeF32Vector(state, 2); + default: return 0; + } +} + +std::uint32_t GlslStd450(SpirvEmitterState& state) { + return state.module.Import("GLSL.std.450"); +} + +} diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvMemoryEmitter.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvMemoryEmitter.cpp deleted file mode 100644 index 51bf379ca..000000000 --- a/core/shader/recompiler/SpirvBackend/src/SpirvMemoryEmitter.cpp +++ /dev/null @@ -1,315 +0,0 @@ -#include "SpirvBackend/SpirvMemoryEmitter.hpp" -#include "SpirvBackend/SpirvEmitterInstructions.hpp" -#include - -namespace ShaderRecompiler { - -void EmitMemoryOperation(SpirvModule& module, const IrValue& value, std::uint32_t resultId) { - throw std::runtime_error("EmitMemoryOperation not implemented"); -} - -void EmitMemoryOperation(SpirvValueEmitContext& context, const IrValue& value, std::uint32_t resultId) { - throw std::runtime_error("EmitMemoryOperation not implemented"); -} - -void EmitLoadMemory(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitLoadMemory not implemented"); -} - -void EmitStoreMemory(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitStoreMemory not implemented"); -} - -std::uint32_t EmitAtomic32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitAtomic32 not implemented"); -} - -std::uint32_t EmitBufferAtomic64(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitBufferAtomic64 not implemented"); -} - -std::uint32_t EmitBufferFloatAtomic(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitBufferFloatAtomic not implemented"); -} - -void EmitSharedFloatAtomic(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitSharedFloatAtomic not implemented"); -} - -std::uint32_t EmitSharedIncDec(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitSharedIncDec not implemented"); -} - -std::uint32_t EmitAppendConsume(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitAppendConsume not implemented"); -} - -std::uint32_t EmitReadConst(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitReadConst not implemented"); -} - -void EmitReadConstBuffer(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitReadConstBuffer not implemented"); -} - -void EmitGetSrtResource(SpirvValueEmitContext& context) { - throw std::runtime_error("EmitGetSrtResource not implemented"); -} - -void EmitGetBufferResource(SpirvValueEmitContext& context) { - throw std::runtime_error("EmitGetBufferResource not implemented"); -} - -void EmitGetAddressResource(SpirvValueEmitContext& context) { - throw std::runtime_error("EmitGetAddressResource not implemented"); -} - -void EmitGetScratchResource(SpirvValueEmitContext& context) { - throw std::runtime_error("EmitGetScratchResource not implemented"); -} - -void EmitLoadAddressU8(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitLoadAddressU8 not implemented"); -} - -void EmitLoadAddressU16(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitLoadAddressU16 not implemented"); -} - -void EmitLoadAddressU32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitLoadAddressU32 not implemented"); -} - -void EmitStoreAddressU8(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitStoreAddressU8 not implemented"); -} - -void EmitStoreAddressU16(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitStoreAddressU16 not implemented"); -} - -void EmitStoreAddressU32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitStoreAddressU32 not implemented"); -} - -void EmitLoadBufferU8(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitLoadBufferU8 not implemented"); -} - -void EmitLoadBufferU16(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitLoadBufferU16 not implemented"); -} - -void EmitLoadBufferU32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitLoadBufferU32 not implemented"); -} - -void EmitLoadBufferU32x2(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitLoadBufferU32x2 not implemented"); -} - -void EmitLoadBufferU32x3(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitLoadBufferU32x3 not implemented"); -} - -void EmitLoadBufferU32x4(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitLoadBufferU32x4 not implemented"); -} - -void EmitStoreBufferU8(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitStoreBufferU8 not implemented"); -} - -void EmitStoreBufferU16(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitStoreBufferU16 not implemented"); -} - -void EmitStoreBufferU32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitStoreBufferU32 not implemented"); -} - -void EmitStoreBufferU32x2(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitStoreBufferU32x2 not implemented"); -} - -void EmitStoreBufferU32x3(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitStoreBufferU32x3 not implemented"); -} - -void EmitStoreBufferU32x4(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitStoreBufferU32x4 not implemented"); -} - -std::uint32_t EmitBufferAtomicSwap32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitBufferAtomicSwap32 not implemented"); -} - -std::uint32_t EmitBufferAtomicCmpSwap32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitBufferAtomicCmpSwap32 not implemented"); -} - -std::uint32_t EmitBufferAtomicSwap64(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitBufferAtomicSwap64 not implemented"); -} - -std::uint32_t EmitBufferAtomicIAdd32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitBufferAtomicIAdd32 not implemented"); -} - -std::uint32_t EmitBufferAtomicISub32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitBufferAtomicISub32 not implemented"); -} - -std::uint32_t EmitBufferAtomicSMin32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitBufferAtomicSMin32 not implemented"); -} - -std::uint32_t EmitBufferAtomicUMin32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitBufferAtomicUMin32 not implemented"); -} - -std::uint32_t EmitBufferAtomicSMax32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitBufferAtomicSMax32 not implemented"); -} - -std::uint32_t EmitBufferAtomicUMax32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitBufferAtomicUMax32 not implemented"); -} - -std::uint32_t EmitBufferAtomicAnd32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitBufferAtomicAnd32 not implemented"); -} - -std::uint32_t EmitBufferAtomicOr32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitBufferAtomicOr32 not implemented"); -} - -std::uint32_t EmitBufferAtomicOr64(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitBufferAtomicOr64 not implemented"); -} - -std::uint32_t EmitBufferAtomicXor32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitBufferAtomicXor32 not implemented"); -} - -std::uint32_t EmitBufferAtomicFMin32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitBufferAtomicFMin32 not implemented"); -} - -std::uint32_t EmitBufferAtomicFMax32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitBufferAtomicFMax32 not implemented"); -} - -void EmitLoadSharedU8(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitLoadSharedU8 not implemented"); -} - -void EmitLoadSharedU16(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitLoadSharedU16 not implemented"); -} - -void EmitLoadSharedU32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitLoadSharedU32 not implemented"); -} - -void EmitLoadSharedU32x2(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitLoadSharedU32x2 not implemented"); -} - -void EmitLoadSharedU32x3(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitLoadSharedU32x3 not implemented"); -} - -void EmitLoadSharedU32x4(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitLoadSharedU32x4 not implemented"); -} - -void EmitWriteSharedU8(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitWriteSharedU8 not implemented"); -} - -void EmitWriteSharedU16(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitWriteSharedU16 not implemented"); -} - -void EmitWriteSharedU32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitWriteSharedU32 not implemented"); -} - -void EmitWriteSharedU32x2(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitWriteSharedU32x2 not implemented"); -} - -void EmitWriteSharedU32x3(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitWriteSharedU32x3 not implemented"); -} - -void EmitWriteSharedU32x4(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitWriteSharedU32x4 not implemented"); -} - -void EmitSharedAtomicFMin32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitSharedAtomicFMin32 not implemented"); -} - -void EmitSharedAtomicFMax32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitSharedAtomicFMax32 not implemented"); -} - -std::uint32_t EmitSharedAtomicSwap32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitSharedAtomicSwap32 not implemented"); -} - -std::uint32_t EmitSharedAtomicIAdd32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitSharedAtomicIAdd32 not implemented"); -} - -std::uint32_t EmitSharedAtomicISub32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitSharedAtomicISub32 not implemented"); -} - -std::uint32_t EmitSharedAtomicInc32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitSharedAtomicInc32 not implemented"); -} - -std::uint32_t EmitSharedAtomicDec32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitSharedAtomicDec32 not implemented"); -} - -std::uint32_t EmitSharedAtomicSMin32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitSharedAtomicSMin32 not implemented"); -} - -std::uint32_t EmitSharedAtomicUMin32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitSharedAtomicUMin32 not implemented"); -} - -std::uint32_t EmitSharedAtomicSMax32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitSharedAtomicSMax32 not implemented"); -} - -std::uint32_t EmitSharedAtomicUMax32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitSharedAtomicUMax32 not implemented"); -} - -std::uint32_t EmitSharedAtomicAnd32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitSharedAtomicAnd32 not implemented"); -} - -std::uint32_t EmitSharedAtomicOr32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitSharedAtomicOr32 not implemented"); -} - -std::uint32_t EmitSharedAtomicXor32(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitSharedAtomicXor32 not implemented"); -} - -std::uint32_t EmitDataAppend(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitDataAppend not implemented"); -} - -std::uint32_t EmitDataConsume(SpirvValueEmitContext& ctx, const IrValue& inst) { - throw std::runtime_error("EmitDataConsume not implemented"); -} - -}