2021-03-08 21:31:53 +00:00
|
|
|
// Copyright 2021 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <boost/container/static_vector.hpp>
|
|
|
|
|
|
|
|
#include "shader_recompiler/backend/spirv/emit_spirv.h"
|
|
|
|
#include "shader_recompiler/frontend/ir/modifiers.h"
|
|
|
|
|
|
|
|
namespace Shader::Backend::SPIRV {
|
|
|
|
namespace {
|
|
|
|
class ImageOperands {
|
|
|
|
public:
|
|
|
|
explicit ImageOperands(EmitContext& ctx, bool has_bias, bool has_lod, bool has_lod_clamp,
|
|
|
|
Id lod, Id offset) {
|
|
|
|
if (has_bias) {
|
|
|
|
const Id bias{has_lod_clamp ? ctx.OpCompositeExtract(ctx.F32[1], lod, 0) : lod};
|
|
|
|
Add(spv::ImageOperandsMask::Bias, bias);
|
|
|
|
}
|
|
|
|
if (has_lod) {
|
|
|
|
const Id lod_value{has_lod_clamp ? ctx.OpCompositeExtract(ctx.F32[1], lod, 0) : lod};
|
|
|
|
Add(spv::ImageOperandsMask::Lod, lod_value);
|
|
|
|
}
|
|
|
|
if (Sirit::ValidId(offset)) {
|
|
|
|
Add(spv::ImageOperandsMask::Offset, offset);
|
|
|
|
}
|
|
|
|
if (has_lod_clamp) {
|
|
|
|
const Id lod_clamp{has_bias ? ctx.OpCompositeExtract(ctx.F32[1], lod, 1) : lod};
|
|
|
|
Add(spv::ImageOperandsMask::MinLod, lod_clamp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 15:02:04 +00:00
|
|
|
explicit ImageOperands([[maybe_unused]] EmitContext& ctx, Id offset, Id offset2) {
|
2021-03-24 22:41:55 +00:00
|
|
|
if (Sirit::ValidId(offset)) {
|
|
|
|
Add(spv::ImageOperandsMask::Offset, offset);
|
|
|
|
}
|
2021-03-26 15:02:04 +00:00
|
|
|
if (Sirit::ValidId(offset2)) {
|
|
|
|
Add(spv::ImageOperandsMask::ConstOffsets, offset2);
|
|
|
|
}
|
2021-03-24 22:41:55 +00:00
|
|
|
}
|
|
|
|
|
2021-03-26 18:24:50 +00:00
|
|
|
explicit ImageOperands([[maybe_unused]] EmitContext& ctx, Id offset, Id lod, Id ms) {
|
|
|
|
if (Sirit::ValidId(lod)) {
|
|
|
|
Add(spv::ImageOperandsMask::Lod, lod);
|
|
|
|
}
|
|
|
|
if (Sirit::ValidId(offset)) {
|
|
|
|
Add(spv::ImageOperandsMask::Offset, offset);
|
|
|
|
}
|
|
|
|
if (Sirit::ValidId(ms)) {
|
|
|
|
Add(spv::ImageOperandsMask::Sample, ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 21:31:53 +00:00
|
|
|
void Add(spv::ImageOperandsMask new_mask, Id value) {
|
|
|
|
mask = static_cast<spv::ImageOperandsMask>(static_cast<unsigned>(mask) |
|
|
|
|
static_cast<unsigned>(new_mask));
|
|
|
|
operands.push_back(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::span<const Id> Span() const noexcept {
|
|
|
|
return std::span{operands.data(), operands.size()};
|
|
|
|
}
|
|
|
|
|
|
|
|
spv::ImageOperandsMask Mask() const noexcept {
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
boost::container::static_vector<Id, 3> operands;
|
|
|
|
spv::ImageOperandsMask mask{};
|
|
|
|
};
|
|
|
|
|
|
|
|
Id Texture(EmitContext& ctx, const IR::Value& index) {
|
|
|
|
if (index.IsImmediate()) {
|
|
|
|
const TextureDefinition def{ctx.textures.at(index.U32())};
|
|
|
|
return ctx.OpLoad(def.type, def.id);
|
|
|
|
}
|
|
|
|
throw NotImplementedException("Indirect texture sample");
|
|
|
|
}
|
|
|
|
|
2021-03-15 07:54:43 +00:00
|
|
|
Id Decorate(EmitContext& ctx, IR::Inst* inst, Id sample) {
|
|
|
|
const auto info{inst->Flags<IR::TextureInstInfo>()};
|
|
|
|
if (info.relaxed_precision != 0) {
|
|
|
|
ctx.Decorate(sample, spv::Decoration::RelaxedPrecision);
|
|
|
|
}
|
|
|
|
return sample;
|
|
|
|
}
|
|
|
|
|
2021-03-08 21:31:53 +00:00
|
|
|
template <typename MethodPtrType, typename... Args>
|
|
|
|
Id Emit(MethodPtrType sparse_ptr, MethodPtrType non_sparse_ptr, EmitContext& ctx, IR::Inst* inst,
|
|
|
|
Id result_type, Args&&... args) {
|
|
|
|
IR::Inst* const sparse{inst->GetAssociatedPseudoOperation(IR::Opcode::GetSparseFromOp)};
|
|
|
|
if (!sparse) {
|
2021-03-15 07:54:43 +00:00
|
|
|
return Decorate(ctx, inst, (ctx.*non_sparse_ptr)(result_type, std::forward<Args>(args)...));
|
2021-03-08 21:31:53 +00:00
|
|
|
}
|
|
|
|
const Id struct_type{ctx.TypeStruct(ctx.U32[1], result_type)};
|
|
|
|
const Id sample{(ctx.*sparse_ptr)(struct_type, std::forward<Args>(args)...)};
|
|
|
|
const Id resident_code{ctx.OpCompositeExtract(ctx.U32[1], sample, 0U)};
|
|
|
|
sparse->SetDefinition(ctx.OpImageSparseTexelsResident(ctx.U1, resident_code));
|
|
|
|
sparse->Invalidate();
|
2021-03-15 07:54:43 +00:00
|
|
|
Decorate(ctx, inst, sample);
|
2021-03-08 21:31:53 +00:00
|
|
|
return ctx.OpCompositeExtract(result_type, sample, 1U);
|
|
|
|
}
|
|
|
|
} // Anonymous namespace
|
|
|
|
|
|
|
|
Id EmitBindlessImageSampleImplicitLod(EmitContext&) {
|
|
|
|
throw LogicError("Unreachable instruction");
|
|
|
|
}
|
|
|
|
|
|
|
|
Id EmitBindlessImageSampleExplicitLod(EmitContext&) {
|
|
|
|
throw LogicError("Unreachable instruction");
|
|
|
|
}
|
|
|
|
|
|
|
|
Id EmitBindlessImageSampleDrefImplicitLod(EmitContext&) {
|
|
|
|
throw LogicError("Unreachable instruction");
|
|
|
|
}
|
|
|
|
|
|
|
|
Id EmitBindlessImageSampleDrefExplicitLod(EmitContext&) {
|
|
|
|
throw LogicError("Unreachable instruction");
|
|
|
|
}
|
|
|
|
|
2021-03-24 22:41:55 +00:00
|
|
|
Id EmitBindlessImageGather(EmitContext&) {
|
|
|
|
throw LogicError("Unreachable instruction");
|
|
|
|
}
|
|
|
|
|
|
|
|
Id EmitBindlessImageGatherDref(EmitContext&) {
|
|
|
|
throw LogicError("Unreachable instruction");
|
|
|
|
}
|
|
|
|
|
2021-03-26 18:24:50 +00:00
|
|
|
Id EmitBindlessImageFetch(EmitContext&) {
|
|
|
|
throw LogicError("Unreachable instruction");
|
|
|
|
}
|
|
|
|
|
2021-03-08 21:31:53 +00:00
|
|
|
Id EmitBoundImageSampleImplicitLod(EmitContext&) {
|
|
|
|
throw LogicError("Unreachable instruction");
|
|
|
|
}
|
|
|
|
|
|
|
|
Id EmitBoundImageSampleExplicitLod(EmitContext&) {
|
|
|
|
throw LogicError("Unreachable instruction");
|
|
|
|
}
|
|
|
|
|
|
|
|
Id EmitBoundImageSampleDrefImplicitLod(EmitContext&) {
|
|
|
|
throw LogicError("Unreachable instruction");
|
|
|
|
}
|
|
|
|
|
|
|
|
Id EmitBoundImageSampleDrefExplicitLod(EmitContext&) {
|
|
|
|
throw LogicError("Unreachable instruction");
|
|
|
|
}
|
|
|
|
|
2021-03-24 22:41:55 +00:00
|
|
|
Id EmitBoundImageGather(EmitContext&) {
|
|
|
|
throw LogicError("Unreachable instruction");
|
|
|
|
}
|
|
|
|
|
|
|
|
Id EmitBoundImageGatherDref(EmitContext&) {
|
|
|
|
throw LogicError("Unreachable instruction");
|
|
|
|
}
|
|
|
|
|
2021-03-26 18:24:50 +00:00
|
|
|
Id EmitBoundImageFetch(EmitContext&) {
|
|
|
|
throw LogicError("Unreachable instruction");
|
|
|
|
}
|
|
|
|
|
2021-03-08 21:31:53 +00:00
|
|
|
Id EmitImageSampleImplicitLod(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords,
|
|
|
|
Id bias_lc, Id offset) {
|
|
|
|
const auto info{inst->Flags<IR::TextureInstInfo>()};
|
|
|
|
const ImageOperands operands(ctx, info.has_bias != 0, false, info.has_lod_clamp != 0, bias_lc,
|
|
|
|
offset);
|
|
|
|
return Emit(&EmitContext::OpImageSparseSampleImplicitLod,
|
|
|
|
&EmitContext::OpImageSampleImplicitLod, ctx, inst, ctx.F32[4], Texture(ctx, index),
|
|
|
|
coords, operands.Mask(), operands.Span());
|
|
|
|
}
|
|
|
|
|
|
|
|
Id EmitImageSampleExplicitLod(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords,
|
|
|
|
Id lod_lc, Id offset) {
|
|
|
|
const auto info{inst->Flags<IR::TextureInstInfo>()};
|
|
|
|
const ImageOperands operands(ctx, false, true, info.has_lod_clamp != 0, lod_lc, offset);
|
|
|
|
return Emit(&EmitContext::OpImageSparseSampleExplicitLod,
|
|
|
|
&EmitContext::OpImageSampleExplicitLod, ctx, inst, ctx.F32[4], Texture(ctx, index),
|
|
|
|
coords, operands.Mask(), operands.Span());
|
|
|
|
}
|
|
|
|
|
|
|
|
Id EmitImageSampleDrefImplicitLod(EmitContext& ctx, IR::Inst* inst, const IR::Value& index,
|
|
|
|
Id coords, Id dref, Id bias_lc, Id offset) {
|
|
|
|
const auto info{inst->Flags<IR::TextureInstInfo>()};
|
|
|
|
const ImageOperands operands(ctx, info.has_bias != 0, false, info.has_lod_clamp != 0, bias_lc,
|
|
|
|
offset);
|
|
|
|
return Emit(&EmitContext::OpImageSparseSampleDrefImplicitLod,
|
|
|
|
&EmitContext::OpImageSampleDrefImplicitLod, ctx, inst, ctx.F32[1],
|
|
|
|
Texture(ctx, index), coords, dref, operands.Mask(), operands.Span());
|
|
|
|
}
|
|
|
|
|
|
|
|
Id EmitImageSampleDrefExplicitLod(EmitContext& ctx, IR::Inst* inst, const IR::Value& index,
|
|
|
|
Id coords, Id dref, Id lod_lc, Id offset) {
|
|
|
|
const auto info{inst->Flags<IR::TextureInstInfo>()};
|
|
|
|
const ImageOperands operands(ctx, false, true, info.has_lod_clamp != 0, lod_lc, offset);
|
|
|
|
return Emit(&EmitContext::OpImageSparseSampleDrefExplicitLod,
|
|
|
|
&EmitContext::OpImageSampleDrefExplicitLod, ctx, inst, ctx.F32[1],
|
|
|
|
Texture(ctx, index), coords, dref, operands.Mask(), operands.Span());
|
|
|
|
}
|
|
|
|
|
2021-03-24 22:41:55 +00:00
|
|
|
Id EmitImageGather(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, Id offset,
|
2021-03-26 18:24:50 +00:00
|
|
|
Id offset2) {
|
2021-03-24 22:41:55 +00:00
|
|
|
const auto info{inst->Flags<IR::TextureInstInfo>()};
|
2021-03-26 15:02:04 +00:00
|
|
|
const ImageOperands operands(ctx, offset, offset2);
|
2021-03-24 22:41:55 +00:00
|
|
|
return Emit(&EmitContext::OpImageSparseGather, &EmitContext::OpImageGather, ctx, inst,
|
|
|
|
ctx.F32[4], Texture(ctx, index), coords,
|
|
|
|
ctx.Constant(ctx.U32[1], info.gather_component.Value()), operands.Mask(),
|
|
|
|
operands.Span());
|
|
|
|
}
|
|
|
|
|
|
|
|
Id EmitImageGatherDref(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords,
|
2021-03-26 18:24:50 +00:00
|
|
|
Id offset, Id offset2, Id dref) {
|
2021-03-24 22:41:55 +00:00
|
|
|
const auto info{inst->Flags<IR::TextureInstInfo>()};
|
2021-03-26 15:02:04 +00:00
|
|
|
const ImageOperands operands(ctx, offset, offset2);
|
2021-03-24 22:41:55 +00:00
|
|
|
return Emit(&EmitContext::OpImageSparseDrefGather, &EmitContext::OpImageDrefGather, ctx, inst,
|
|
|
|
ctx.F32[4], Texture(ctx, index), coords, dref, operands.Mask(), operands.Span());
|
|
|
|
}
|
|
|
|
|
2021-03-26 18:24:50 +00:00
|
|
|
Id EmitImageFetch(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, Id offset,
|
|
|
|
Id lod, Id ms) {
|
|
|
|
const auto info{inst->Flags<IR::TextureInstInfo>()};
|
|
|
|
const ImageOperands operands(ctx, offset, lod, ms);
|
|
|
|
return Emit(&EmitContext::OpImageSparseFetch, &EmitContext::OpImageFetch, ctx, inst, ctx.F32[4],
|
|
|
|
Texture(ctx, index), coords, operands.Mask(), operands.Span());
|
|
|
|
}
|
|
|
|
|
2021-03-08 21:31:53 +00:00
|
|
|
} // namespace Shader::Backend::SPIRV
|