VERY experimental Nintendo Switch 2 emulator
C++
shader: Implement TMML partially
FernandoS27d523cd563ce7
diff --git a/src/shader_recompiler/CMakeLists.txt b/src/shader_recompiler/CMakeLists.txtindex d3afd7d316..d9a2b9cb45 100644--- a/src/shader_recompiler/CMakeLists.txt+++ b/src/shader_recompiler/CMakeLists.txt@@ -135,6 +135,7 @@ add_library(shader_recompiler STATICfrontend/maxwell/translate/impl/texture_gather.cppfrontend/maxwell/translate/impl/texture_load_swizzled.cppfrontend/maxwell/translate/impl/texture_load.cpp+ frontend/maxwell/translate/impl/texture_mipmap_level.cppfrontend/maxwell/translate/impl/texture_query.cppfrontend/maxwell/translate/impl/video_helper.cppfrontend/maxwell/translate/impl/video_helper.hdiff --git a/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_image.cppindex 2cd6b38c46..9f8fe26031 100644--- a/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp+++ b/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp@@ -297,8 +297,8 @@ Id EmitImageQueryDimensions(EmitContext& ctx, IR::Inst* inst, const IR::Value& iId EmitImageQueryLod(EmitContext& ctx, IR::Inst*, const IR::Value& index, Id coords) {const Id zero{ctx.f32_zero_value};- const Id image{TextureImage(ctx, index)};- return ctx.OpCompositeConstruct(ctx.F32[4], ctx.OpImageQueryLod(ctx.F32[2], image, coords),+ const Id sampler{Texture(ctx, index)};+ return ctx.OpCompositeConstruct(ctx.F32[4], ctx.OpImageQueryLod(ctx.F32[2], sampler, coords),zero, zero);}diff --git a/src/shader_recompiler/frontend/ir/ir_emitter.cpp b/src/shader_recompiler/frontend/ir/ir_emitter.cppindex ba95917277..f6818ec8a7 100644--- a/src/shader_recompiler/frontend/ir/ir_emitter.cpp+++ b/src/shader_recompiler/frontend/ir/ir_emitter.cpp@@ -1567,10 +1567,10 @@ Value IREmitter::ImageQueryDimension(const Value& handle, const IR::U32& lod) {return Inst(op, handle, lod);}-Value IREmitter::ImageQueryLod(const Value& handle, const Value& coords) {+Value IREmitter::ImageQueryLod(const Value& handle, const Value& coords, TextureInstInfo info) {const Opcode op{handle.IsImmediate() ? Opcode::BoundImageQueryLod: Opcode::BindlessImageQueryLod};- return Inst(op, handle, coords);+ return Inst(op, Flags{info}, handle, coords);}U1 IREmitter::VoteAll(const U1& value) {diff --git a/src/shader_recompiler/frontend/ir/ir_emitter.h b/src/shader_recompiler/frontend/ir/ir_emitter.hindex 9e752b2083..2beeace8f2 100644--- a/src/shader_recompiler/frontend/ir/ir_emitter.h+++ b/src/shader_recompiler/frontend/ir/ir_emitter.h@@ -255,7 +255,8 @@ public:TextureInstInfo info);[[nodiscard]] Value ImageQueryDimension(const Value& handle, const IR::U32& lod);- [[nodiscard]] Value ImageQueryLod(const Value& handle, const Value& coords);+ [[nodiscard]] Value ImageQueryLod(const Value& handle, const Value& coords,+ TextureInstInfo info);[[nodiscard]] Value ImageGather(const Value& handle, const Value& coords, const Value& offset,const Value& offset2, TextureInstInfo info);diff --git a/src/shader_recompiler/frontend/maxwell/translate/impl/not_implemented.cpp b/src/shader_recompiler/frontend/maxwell/translate/impl/not_implemented.cppindex 7e1ad63e11..9f5ea77751 100644--- a/src/shader_recompiler/frontend/maxwell/translate/impl/not_implemented.cpp+++ b/src/shader_recompiler/frontend/maxwell/translate/impl/not_implemented.cpp@@ -313,14 +313,6 @@ void TranslatorVisitor::SYNC(u64) {ThrowNotImplemented(Opcode::SYNC);}-void TranslatorVisitor::TMML(u64) {- ThrowNotImplemented(Opcode::TMML);-}--void TranslatorVisitor::TMML_b(u64) {- ThrowNotImplemented(Opcode::TMML_b);-}-void TranslatorVisitor::TXA(u64) {ThrowNotImplemented(Opcode::TXA);}diff --git a/src/shader_recompiler/frontend/maxwell/translate/impl/texture_mipmap_level.cpp b/src/shader_recompiler/frontend/maxwell/translate/impl/texture_mipmap_level.cppnew file mode 100644index 0000000000..ee13ede30a--- /dev/null+++ b/src/shader_recompiler/frontend/maxwell/translate/impl/texture_mipmap_level.cpp@@ -0,0 +1,130 @@+// Copyright 2021 yuzu Emulator Project+// Licensed under GPLv2 or any later version+// Refer to the license.txt file included.++#include <optional>++#include "common/bit_field.h"+#include "common/common_types.h"+#include "shader_recompiler/frontend/ir/modifiers.h"+#include "shader_recompiler/frontend/maxwell/translate/impl/impl.h"++namespace Shader::Maxwell {+namespace {++enum class TextureType : u64 {+ _1D,+ ARRAY_1D,+ _2D,+ ARRAY_2D,+ _3D,+ ARRAY_3D,+ CUBE,+ ARRAY_CUBE,+};++Shader::TextureType GetType(TextureType type, bool dc) {+ switch (type) {+ case TextureType::_1D:+ return dc ? Shader::TextureType::Shadow1D : Shader::TextureType::Color1D;+ case TextureType::ARRAY_1D:+ return dc ? Shader::TextureType::ShadowArray1D : Shader::TextureType::ColorArray1D;+ case TextureType::_2D:+ return dc ? Shader::TextureType::Shadow2D : Shader::TextureType::Color2D;+ case TextureType::ARRAY_2D:+ return dc ? Shader::TextureType::ShadowArray2D : Shader::TextureType::ColorArray2D;+ case TextureType::_3D:+ return dc ? Shader::TextureType::Shadow3D : Shader::TextureType::Color3D;+ case TextureType::ARRAY_3D:+ throw NotImplementedException("3D array texture type");+ case TextureType::CUBE:+ return dc ? Shader::TextureType::ShadowCube : Shader::TextureType::ColorCube;+ case TextureType::ARRAY_CUBE:+ return dc ? Shader::TextureType::ShadowArrayCube : Shader::TextureType::ColorArrayCube;+ }+ throw NotImplementedException("Invalid texture type {}", type);+}++IR::Value MakeCoords(TranslatorVisitor& v, IR::Reg reg, TextureType type) {+ const auto read_array{[&]() -> IR::F32 { return v.ir.ConvertUToF(32, 16, v.X(reg)); }};+ switch (type) {+ case TextureType::_1D:+ return v.F(reg);+ case TextureType::ARRAY_1D:+ return v.ir.CompositeConstruct(v.F(reg + 1), read_array());+ case TextureType::_2D:+ return v.ir.CompositeConstruct(v.F(reg), v.F(reg + 1));+ case TextureType::ARRAY_2D:+ return v.ir.CompositeConstruct(v.F(reg + 1), v.F(reg + 2), read_array());+ case TextureType::_3D:+ return v.ir.CompositeConstruct(v.F(reg), v.F(reg + 1), v.F(reg + 2));+ case TextureType::ARRAY_3D:+ throw NotImplementedException("3D array texture type");+ case TextureType::CUBE:+ return v.ir.CompositeConstruct(v.F(reg), v.F(reg + 1), v.F(reg + 2));+ case TextureType::ARRAY_CUBE:+ return v.ir.CompositeConstruct(v.F(reg + 1), v.F(reg + 2), v.F(reg + 3), read_array());+ }+ throw NotImplementedException("Invalid texture type {}", type);+}++void Impl(TranslatorVisitor& v, u64 insn, bool is_bindless) {+ union {+ u64 raw;+ BitField<49, 1, u64> nodep;+ BitField<35, 1, u64> ndv;+ BitField<0, 8, IR::Reg> dest_reg;+ BitField<8, 8, IR::Reg> coord_reg;+ BitField<20, 8, IR::Reg> meta_reg;+ BitField<28, 3, TextureType> type;+ BitField<31, 4, u64> mask;+ BitField<36, 13, u64> cbuf_offset;+ } const tmml{insn};++ if ((tmml.mask & 0xC) != 0) {+ throw NotImplementedException("TMML BA results are not implmented");+ }++ IR::F32 transform_constant = v.ir.Imm32(256.0f);++ const IR::Value coords{MakeCoords(v, tmml.coord_reg, tmml.type)};++ IR::U32 handle;+ IR::Reg meta_reg{tmml.meta_reg};+ if (!is_bindless) {+ handle = v.ir.Imm32(static_cast<u32>(tmml.cbuf_offset.Value() * 4));+ } else {+ handle = v.X(meta_reg++);+ }+ IR::TextureInstInfo info{};+ info.type.Assign(GetType(tmml.type, false));+ const IR::Value sample{+ [&]() -> IR::Value { return v.ir.ImageQueryLod(handle, coords, info); }()};++ const IR::FpControl fp_control{+ .no_contraction{false},+ .rounding{IR::FpRounding::RP},+ .fmz_mode{IR::FmzMode::FTZ},+ };+ IR::Reg dest_reg{tmml.dest_reg};+ for (size_t element = 0; element < 4; ++element) {+ if (((tmml.mask >> element) & 1) == 0) {+ continue;+ }+ IR::F32 value = IR::F32{v.ir.CompositeExtract(sample, element)};+ v.F(dest_reg,+ element < 2 ? IR::F32{v.ir.FPMul(value, transform_constant, fp_control)} : value);+ ++dest_reg;+ }+}+} // Anonymous namespace++void TranslatorVisitor::TMML(u64 insn) {+ Impl(*this, insn, false);+}++void TranslatorVisitor::TMML_b(u64 insn) {+ Impl(*this, insn, true);+}++} // namespace Shader::Maxwell