VERY experimental Nintendo Switch 2 emulator
C++
glsl: Implement derivatives and YDirection
ameerjb96d76fa7eb5
diff --git a/src/shader_recompiler/backend/glsl/emit_context.cpp b/src/shader_recompiler/backend/glsl/emit_context.cppindex 2bf0def827..0ddc0443b0 100644--- a/src/shader_recompiler/backend/glsl/emit_context.cpp+++ b/src/shader_recompiler/backend/glsl/emit_context.cpp@@ -22,9 +22,10 @@ std::string_view InterpDecorator(Interpolation interp) {}} // namespace-EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile& profile_)- : info{program.info}, profile{profile_} {- std::string header = "#version 450\n";+EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile& profile_,+ const RuntimeInfo& runtime_info_)+ : info{program.info}, profile{profile_}, runtime_info{runtime_info_} {+ std::string header = "";SetupExtensions(header);stage = program.stage;switch (program.stage) {diff --git a/src/shader_recompiler/backend/glsl/emit_context.h b/src/shader_recompiler/backend/glsl/emit_context.hindex 66f70d3550..07dad6e578 100644--- a/src/shader_recompiler/backend/glsl/emit_context.h+++ b/src/shader_recompiler/backend/glsl/emit_context.h@@ -16,6 +16,7 @@namespace Shader {struct Info;struct Profile;+struct RuntimeInfo;} // namespace Shadernamespace Shader::Backend {@@ -31,7 +32,8 @@ namespace Shader::Backend::GLSL {class EmitContext {public:- explicit EmitContext(IR::Program& program, Bindings& bindings, const Profile& profile_);+ explicit EmitContext(IR::Program& program, Bindings& bindings, const Profile& profile_,+ const RuntimeInfo& runtime_info_);template <Type type, typename... Args>void Add(const char* format_str, IR::Inst& inst, Args&&... args) {@@ -121,6 +123,7 @@ public:RegAlloc reg_alloc;const Info& info;const Profile& profile;+ const RuntimeInfo& runtime_info;Stage stage{};std::string_view stage_name = "invalid";@@ -131,6 +134,8 @@ public:std::vector<u32> texture_bindings;std::vector<u32> image_bindings;+ bool uses_y_direction{};+private:void SetupExtensions(std::string& header);void DefineConstantBuffers();diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_composite.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_composite.cppindex aa966a304f..22e489aa94 100644--- a/src/shader_recompiler/backend/glsl/emit_glsl_composite.cpp+++ b/src/shader_recompiler/backend/glsl/emit_glsl_composite.cpp@@ -42,25 +42,19 @@ void EmitCompositeExtractU32x4(EmitContext& ctx, IR::Inst& inst, std::string_viectx.AddU32("{}={}.{};", inst, composite, SWIZZLE[index]);}-void EmitCompositeInsertU32x2([[maybe_unused]] EmitContext& ctx,- [[maybe_unused]] std::string_view composite,- [[maybe_unused]] std::string_view object,- [[maybe_unused]] u32 index) {- throw NotImplementedException("GLSL Instruction");+void EmitCompositeInsertU32x2(EmitContext& ctx, std::string_view composite, std::string_view object,+ u32 index) {+ ctx.Add("{}.{}={};", composite, SWIZZLE[index], object);}-void EmitCompositeInsertU32x3([[maybe_unused]] EmitContext& ctx,- [[maybe_unused]] std::string_view composite,- [[maybe_unused]] std::string_view object,- [[maybe_unused]] u32 index) {- throw NotImplementedException("GLSL Instruction");+void EmitCompositeInsertU32x3(EmitContext& ctx, std::string_view composite, std::string_view object,+ u32 index) {+ ctx.Add("{}.{}={};", composite, SWIZZLE[index], object);}-void EmitCompositeInsertU32x4([[maybe_unused]] EmitContext& ctx,- [[maybe_unused]] std::string_view composite,- [[maybe_unused]] std::string_view object,- [[maybe_unused]] u32 index) {- throw NotImplementedException("GLSL Instruction");+void EmitCompositeInsertU32x4(EmitContext& ctx, std::string_view composite, std::string_view object,+ u32 index) {+ ctx.Add("{}.{}={};", composite, SWIZZLE[index], object);}void EmitCompositeConstructF16x2([[maybe_unused]] EmitContext& ctx,@@ -153,25 +147,19 @@ void EmitCompositeExtractF32x4(EmitContext& ctx, IR::Inst& inst, std::string_viectx.AddF32("{}={}.{};", inst, composite, SWIZZLE[index]);}-void EmitCompositeInsertF32x2([[maybe_unused]] EmitContext& ctx,- [[maybe_unused]] std::string_view composite,- [[maybe_unused]] std::string_view object,- [[maybe_unused]] u32 index) {- throw NotImplementedException("GLSL Instruction");+void EmitCompositeInsertF32x2(EmitContext& ctx, std::string_view composite, std::string_view object,+ u32 index) {+ ctx.Add("{}.{}={};", composite, SWIZZLE[index], object);}-void EmitCompositeInsertF32x3([[maybe_unused]] EmitContext& ctx,- [[maybe_unused]] std::string_view composite,- [[maybe_unused]] std::string_view object,- [[maybe_unused]] u32 index) {- throw NotImplementedException("GLSL Instruction");+void EmitCompositeInsertF32x3(EmitContext& ctx, std::string_view composite, std::string_view object,+ u32 index) {+ ctx.Add("{}.{}={};", composite, SWIZZLE[index], object);}-void EmitCompositeInsertF32x4([[maybe_unused]] EmitContext& ctx,- [[maybe_unused]] std::string_view composite,- [[maybe_unused]] std::string_view object,- [[maybe_unused]] u32 index) {- throw NotImplementedException("GLSL Instruction");+void EmitCompositeInsertF32x4(EmitContext& ctx, std::string_view composite, std::string_view object,+ u32 index) {+ ctx.Add("{}.{}={};", composite, SWIZZLE[index], object);}void EmitCompositeConstructF64x2([[maybe_unused]] EmitContext& ctx) {@@ -198,24 +186,18 @@ void EmitCompositeExtractF64x4([[maybe_unused]] EmitContext& ctx) {throw NotImplementedException("GLSL Instruction");}-void EmitCompositeInsertF64x2([[maybe_unused]] EmitContext& ctx,- [[maybe_unused]] std::string_view composite,- [[maybe_unused]] std::string_view object,- [[maybe_unused]] u32 index) {- throw NotImplementedException("GLSL Instruction");+void EmitCompositeInsertF64x2(EmitContext& ctx, std::string_view composite, std::string_view object,+ u32 index) {+ ctx.Add("{}.{}={};", composite, SWIZZLE[index], object);}-void EmitCompositeInsertF64x3([[maybe_unused]] EmitContext& ctx,- [[maybe_unused]] std::string_view composite,- [[maybe_unused]] std::string_view object,- [[maybe_unused]] u32 index) {- throw NotImplementedException("GLSL Instruction");+void EmitCompositeInsertF64x3(EmitContext& ctx, std::string_view composite, std::string_view object,+ u32 index) {+ ctx.Add("{}.{}={};", composite, SWIZZLE[index], object);}-void EmitCompositeInsertF64x4([[maybe_unused]] EmitContext& ctx,- [[maybe_unused]] std::string_view composite,- [[maybe_unused]] std::string_view object,- [[maybe_unused]] u32 index) {- throw NotImplementedException("GLSL Instruction");+void EmitCompositeInsertF64x4(EmitContext& ctx, std::string_view composite, std::string_view object,+ u32 index) {+ ctx.Add("{}.{}={};", composite, SWIZZLE[index], object);}} // namespace Shader::Backend::GLSLdiff --git a/src/shader_recompiler/backend/glsl/emit_glsl_floating_point.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_floating_point.cppindex 33aba9fef0..6a9b95ecab 100644--- a/src/shader_recompiler/backend/glsl/emit_glsl_floating_point.cpp+++ b/src/shader_recompiler/backend/glsl/emit_glsl_floating_point.cpp@@ -124,16 +124,16 @@ void EmitFPLog2(EmitContext& ctx, IR::Inst& inst, std::string_view value) {}void EmitFPRecip32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {- ctx.AddF32("{}=1/{};", inst, value);+ ctx.AddF32("{}=(1.0f)/{};", inst, value);}void EmitFPRecip64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {- ctx.AddF64("{}=1/{};", inst, value);+ ctx.AddF64("{}=1.0/{};", inst, value);}void EmitFPRecipSqrt32([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,[[maybe_unused]] std::string_view value) {- ctx.AddF32("{}=1/sqrt({});", inst, value);+ ctx.AddF32("{}=(1.0f)/sqrt({});", inst, value);}void EmitFPRecipSqrt64([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_image.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_image.cppindex cc5afc048d..1a348b1172 100644--- a/src/shader_recompiler/backend/glsl/emit_glsl_image.cpp+++ b/src/shader_recompiler/backend/glsl/emit_glsl_image.cpp@@ -37,6 +37,9 @@ void EmitImageSampleImplicitLod([[maybe_unused]] EmitContext& ctx, [[maybe_unuseif (!offset.IsEmpty()) {throw NotImplementedException("Offset");}+ if (info.type != TextureType::Color2D) {+ throw NotImplementedException("Texture type: {}", info.type.Value());+ }const auto texture{Texture(ctx, info, index)};ctx.AddF32x4("{}=texture({},{});", inst, texture, coords);}diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_instructions.h b/src/shader_recompiler/backend/glsl/emit_glsl_instructions.hindex 295b7a8c43..f08ed0ece7 100644--- a/src/shader_recompiler/backend/glsl/emit_glsl_instructions.h+++ b/src/shader_recompiler/backend/glsl/emit_glsl_instructions.h@@ -87,7 +87,7 @@ void EmitLocalInvocationId(EmitContext& ctx);void EmitInvocationId(EmitContext& ctx);void EmitSampleId(EmitContext& ctx);void EmitIsHelperInvocation(EmitContext& ctx);-void EmitYDirection(EmitContext& ctx);+void EmitYDirection(EmitContext& ctx, IR::Inst& inst);void EmitLoadLocal(EmitContext& ctx, std::string_view word_offset);void EmitWriteLocal(EmitContext& ctx, std::string_view word_offset, std::string_view value);void EmitUndefU1(EmitContext& ctx, IR::Inst& inst);@@ -692,11 +692,11 @@ void EmitShuffleDown(EmitContext& ctx, IR::Inst& inst, std::string_view value,void EmitShuffleButterfly(EmitContext& ctx, IR::Inst& inst, std::string_view value,std::string_view index, std::string_view clamp,std::string_view segmentation_mask);-void EmitFSwizzleAdd(EmitContext& ctx, std::string_view op_a, std::string_view op_b,+void EmitFSwizzleAdd(EmitContext& ctx, IR::Inst& inst, std::string_view op_a, std::string_view op_b,std::string_view swizzle);-void EmitDPdxFine(EmitContext& ctx, std::string_view op_a);-void EmitDPdyFine(EmitContext& ctx, std::string_view op_a);-void EmitDPdxCoarse(EmitContext& ctx, std::string_view op_a);-void EmitDPdyCoarse(EmitContext& ctx, std::string_view op_a);+void EmitDPdxFine(EmitContext& ctx, IR::Inst& inst, std::string_view op_a);+void EmitDPdyFine(EmitContext& ctx, IR::Inst& inst, std::string_view op_a);+void EmitDPdxCoarse(EmitContext& ctx, IR::Inst& inst, std::string_view op_a);+void EmitDPdyCoarse(EmitContext& ctx, IR::Inst& inst, std::string_view op_a);} // namespace Shader::Backend::GLSLdiff --git a/src/shader_recompiler/backend/glsl/emit_glsl_not_implemented.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_not_implemented.cppindex 761145a5d2..4dfc0c8960 100644--- a/src/shader_recompiler/backend/glsl/emit_glsl_not_implemented.cpp+++ b/src/shader_recompiler/backend/glsl/emit_glsl_not_implemented.cpp@@ -224,8 +224,9 @@ void EmitIsHelperInvocation(EmitContext& ctx) {NotImplemented();}-void EmitYDirection(EmitContext& ctx) {- NotImplemented();+void EmitYDirection(EmitContext& ctx, IR::Inst& inst) {+ ctx.uses_y_direction = true;+ ctx.AddF32("{}=gl_FrontMaterial.ambient.a;", inst);}void EmitLoadLocal(EmitContext& ctx, std::string_view word_offset) {@@ -650,25 +651,4 @@ void EmitShuffleButterfly(EmitContext& ctx, IR::Inst& inst, std::string_view valNotImplemented();}-void EmitFSwizzleAdd(EmitContext& ctx, std::string_view op_a, std::string_view op_b,- std::string_view swizzle) {- NotImplemented();-}--void EmitDPdxFine(EmitContext& ctx, std::string_view op_a) {- NotImplemented();-}--void EmitDPdyFine(EmitContext& ctx, std::string_view op_a) {- NotImplemented();-}--void EmitDPdxCoarse(EmitContext& ctx, std::string_view op_a) {- NotImplemented();-}--void EmitDPdyCoarse(EmitContext& ctx, std::string_view op_a) {- NotImplemented();-}-} // namespace Shader::Backend::GLSLdiff --git a/src/shader_recompiler/backend/glsl/emit_glsl_warp.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_warp.cppindex e69de29bb2..1876778785 100644--- a/src/shader_recompiler/backend/glsl/emit_glsl_warp.cpp+++ b/src/shader_recompiler/backend/glsl/emit_glsl_warp.cpp@@ -0,0 +1,35 @@+// Copyright 2021 yuzu Emulator Project+// Licensed under GPLv2 or any later version+// Refer to the license.txt file included.++#include <string_view>++#include "shader_recompiler/backend/glsl/emit_context.h"+#include "shader_recompiler/backend/glsl/emit_glsl_instructions.h"+#include "shader_recompiler/frontend/ir/modifiers.h"+#include "shader_recompiler/frontend/ir/value.h"+#include "shader_recompiler/profile.h"++namespace Shader::Backend::GLSL {+void EmitFSwizzleAdd([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,+ [[maybe_unused]] std::string_view op_a, [[maybe_unused]] std::string_view op_b,+ [[maybe_unused]] std::string_view swizzle) {+ throw NotImplementedException("GLSL Instruction");+}++void EmitDPdxFine(EmitContext& ctx, IR::Inst& inst, std::string_view op_a) {+ ctx.AddF32("{}=dFdxFine({});", inst, op_a);+}++void EmitDPdyFine(EmitContext& ctx, IR::Inst& inst, std::string_view op_a) {+ ctx.AddF32("{}=dFdyFine({});", inst, op_a);+}++void EmitDPdxCoarse(EmitContext& ctx, IR::Inst& inst, std::string_view op_a) {+ ctx.AddF32("{}=dFdxCoarse({});", inst, op_a);+}++void EmitDPdyCoarse(EmitContext& ctx, IR::Inst& inst, std::string_view op_a) {+ ctx.AddF32("{}=dFdyCoarse({});", inst, op_a);+}+} // namespace Shader::Backend::GLSL