From 55e0211a5e520482246273f2cc64388c4b4eff1c Mon Sep 17 00:00:00 2001
From: ameerj <52414509+ameerj@users.noreply.github.com>
Date: Sat, 29 May 2021 01:06:29 -0400
Subject: [PATCH] glsl: Implement TEX ImageSample functions

---
 .../backend/glsl/emit_context.cpp             |  9 +++
 .../backend/glsl/emit_glsl_convert.cpp        |  2 +-
 .../backend/glsl/emit_glsl_image.cpp          | 71 ++++++++++++++++---
 3 files changed, 71 insertions(+), 11 deletions(-)

diff --git a/src/shader_recompiler/backend/glsl/emit_context.cpp b/src/shader_recompiler/backend/glsl/emit_context.cpp
index db62ba73b..eb1d8266f 100644
--- a/src/shader_recompiler/backend/glsl/emit_context.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_context.cpp
@@ -23,6 +23,10 @@ std::string_view InterpDecorator(Interpolation interp) {
 
 std::string_view SamplerType(TextureType type) {
     switch (type) {
+    case TextureType::Color1D:
+        return "sampler1D";
+    case TextureType::ColorArray1D:
+        return "sampler1DArray";
     case TextureType::Color2D:
         return "sampler2D";
     case TextureType::ColorArray2D:
@@ -31,6 +35,10 @@ std::string_view SamplerType(TextureType type) {
         return "sampler3D";
     case TextureType::ColorCube:
         return "samplerCube";
+    case TextureType::ColorArrayCube:
+        return "samplerCubeArray";
+    case TextureType::Buffer:
+        return "samplerBuffer";
     default:
         fmt::print("Texture type: {}", type);
         throw NotImplementedException("Texture type: {}", type);
@@ -101,6 +109,7 @@ EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile
 
 void EmitContext::SetupExtensions(std::string&) {
     header += "#extension GL_ARB_separate_shader_objects : enable\n";
+    header += "#extension GL_ARB_sparse_texture2 : enable\n";
     // header += "#extension GL_ARB_texture_cube_map_array : enable\n";
     if (info.uses_int64) {
         header += "#extension GL_ARB_gpu_shader_int64 : enable\n";
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_convert.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_convert.cpp
index fa1b02af1..087eaea8f 100644
--- a/src/shader_recompiler/backend/glsl/emit_glsl_convert.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_glsl_convert.cpp
@@ -195,7 +195,7 @@ void EmitConvertF32U8([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::In
 
 void EmitConvertF32U16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                        [[maybe_unused]] std::string_view value) {
-    ctx.AddF32("{}=float(uint({}));", inst, value);
+    ctx.AddF32("{}=float(uint({}&0xffff));", inst, value);
 }
 
 void EmitConvertF32U32([[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.cpp
index 1a34fe9b3..71eb3ac2b 100644
--- a/src/shader_recompiler/backend/glsl/emit_glsl_image.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_glsl_image.cpp
@@ -10,7 +10,7 @@
 
 namespace Shader::Backend::GLSL {
 namespace {
-std::string Texture(EmitContext& ctx, IR::TextureInstInfo info,
+std::string Texture(EmitContext& ctx, const IR::TextureInstInfo& info,
                     [[maybe_unused]] const IR::Value& index) {
     if (info.type == TextureType::Buffer) {
         throw NotImplementedException("TextureType::Buffer");
@@ -18,6 +18,32 @@ std::string Texture(EmitContext& ctx, IR::TextureInstInfo info,
         return fmt::format("tex{}", ctx.texture_bindings.at(info.descriptor_index));
     }
 }
+
+std::string CastToIntVec(std::string_view value, const IR::TextureInstInfo& info) {
+    switch (info.type) {
+    case TextureType::Color1D:
+        return fmt::format("int({})", value);
+    case TextureType::ColorArray1D:
+    case TextureType::Color2D:
+        return fmt::format("ivec2({})", value);
+    case TextureType::ColorArray2D:
+    case TextureType::Color3D:
+    case TextureType::ColorCube:
+        return fmt::format("ivec3({})", value);
+    case TextureType::ColorArrayCube:
+        return fmt::format("ivec4({})", value);
+    default:
+        throw NotImplementedException("Offset type {}", info.type.Value());
+    }
+}
+
+IR::Inst* PrepareSparse(IR::Inst& inst) {
+    const auto sparse_inst{inst.GetAssociatedPseudoOperation(IR::Opcode::GetSparseFromOp)};
+    if (sparse_inst) {
+        sparse_inst->Invalidate();
+    }
+    return sparse_inst;
+}
 } // namespace
 
 void EmitImageSampleImplicitLod([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
@@ -26,18 +52,30 @@ void EmitImageSampleImplicitLod([[maybe_unused]] EmitContext& ctx, [[maybe_unuse
                                 [[maybe_unused]] std::string_view bias_lc,
                                 [[maybe_unused]] const IR::Value& offset) {
     const auto info{inst.Flags<IR::TextureInstInfo>()};
-    if (info.has_bias) {
-        throw NotImplementedException("Bias texture samples");
-    }
     if (info.has_lod_clamp) {
         throw NotImplementedException("Lod clamp samples");
     }
     const auto texture{Texture(ctx, info, index)};
+    const auto bias{info.has_bias ? fmt::format(",{}", bias_lc) : ""};
+    const auto texel{ctx.reg_alloc.Define(inst, Type::F32x4)};
+    const auto sparse_inst{PrepareSparse(inst)};
+    if (!sparse_inst) {
+        if (!offset.IsEmpty()) {
+            ctx.Add("{}=textureOffset({},{},{}{});", texel, texture, coords,
+                    CastToIntVec(ctx.reg_alloc.Consume(offset), info), bias);
+        } else {
+            ctx.Add("{}=texture({},{}{});", texel, texture, coords, bias);
+        }
+        return;
+    }
+    // TODO: Query sparseTexels extension support
     if (!offset.IsEmpty()) {
-        ctx.AddF32x4("{}=textureOffset({},{},ivec2({}));", inst, texture, coords,
-                     ctx.reg_alloc.Consume(offset));
+        ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureOffsetARB({},{},{},{}{}));",
+                  *sparse_inst, texture, coords, CastToIntVec(ctx.reg_alloc.Consume(offset), info),
+                  texel, bias);
     } else {
-        ctx.AddF32x4("{}=texture({},{});", inst, texture, coords);
+        ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureARB({},{},{}{}));", *sparse_inst,
+                  texture, coords, texel, bias);
     }
 }
 
@@ -54,11 +92,24 @@ void EmitImageSampleExplicitLod([[maybe_unused]] EmitContext& ctx, [[maybe_unuse
         throw NotImplementedException("Lod clamp samples");
     }
     const auto texture{Texture(ctx, info, index)};
+    const auto texel{ctx.reg_alloc.Define(inst, Type::F32x4)};
+    const auto sparse_inst{PrepareSparse(inst)};
+    if (!sparse_inst) {
+        if (!offset.IsEmpty()) {
+            ctx.Add("{}=textureLodOffset({},{},{},{});", texel, texture, coords, lod_lc,
+                    CastToIntVec(ctx.reg_alloc.Consume(offset), info));
+        } else {
+            ctx.Add("{}=textureLod({},{},{});", texel, texture, coords, lod_lc);
+        }
+        return;
+    }
     if (!offset.IsEmpty()) {
-        ctx.AddF32x4("{}=textureLodOffset({},{},{},ivec2({}));", inst, texture, coords, lod_lc,
-                     ctx.reg_alloc.Consume(offset));
+        ctx.AddU1("{}=sparseTexelsResidentARB(sparseTexelFetchOffsetARB({},{},int({}),{},{}));",
+                  *sparse_inst, texture, CastToIntVec(coords, info), lod_lc,
+                  CastToIntVec(ctx.reg_alloc.Consume(offset), info), texel);
     } else {
-        ctx.AddF32x4("{}=textureLod({},{},{});", inst, texture, coords, lod_lc);
+        ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureLodARB({},{},{},{}));", *sparse_inst,
+                  texture, coords, lod_lc, texel);
     }
 }