From 453cd25da57e4088826cb6df48b5b6856affe109 Mon Sep 17 00:00:00 2001
From: ameerj <52414509+ameerj@users.noreply.github.com>
Date: Fri, 28 May 2021 13:55:07 -0400
Subject: [PATCH] glsl: SSBO access fixes and wip SampleExplicitLod
 implementation.

---
 .../backend/glsl/emit_glsl_image.cpp              | 15 ++++++++++++++-
 .../backend/glsl/emit_glsl_memory.cpp             |  8 +++++---
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_image.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_image.cpp
index c070fba0e8..1a34fe9b35 100644
--- a/src/shader_recompiler/backend/glsl/emit_glsl_image.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_glsl_image.cpp
@@ -46,7 +46,20 @@ void EmitImageSampleExplicitLod([[maybe_unused]] EmitContext& ctx, [[maybe_unuse
                                 [[maybe_unused]] std::string_view coords,
                                 [[maybe_unused]] std::string_view lod_lc,
                                 [[maybe_unused]] const IR::Value& offset) {
-    throw NotImplementedException("GLSL Instruction");
+    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)};
+    if (!offset.IsEmpty()) {
+        ctx.AddF32x4("{}=textureLodOffset({},{},{},ivec2({}));", inst, texture, coords, lod_lc,
+                     ctx.reg_alloc.Consume(offset));
+    } else {
+        ctx.AddF32x4("{}=textureLod({},{},{});", inst, texture, coords, lod_lc);
+    }
 }
 
 void EmitImageSampleDrefImplicitLod([[maybe_unused]] EmitContext& ctx,
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_memory.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_memory.cpp
index 6168c4e063..708c9685ba 100644
--- a/src/shader_recompiler/backend/glsl/emit_glsl_memory.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_glsl_memory.cpp
@@ -81,13 +81,15 @@ void EmitWriteStorageS16([[maybe_unused]] EmitContext& ctx,
 
 void EmitWriteStorage32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset,
                         std::string_view value) {
-    ctx.Add("ssbo{}[{}]={};", binding.U32(), offset.U32(), value);
+    const auto offset_var{ctx.reg_alloc.Consume(offset)};
+    ctx.Add("ssbo{}[{}]={};", binding.U32(), offset_var, value);
 }
 
 void EmitWriteStorage64(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset,
                         std::string_view value) {
-    ctx.Add("ssbo{}[{}]={}.x;", binding.U32(), offset.U32(), value);
-    ctx.Add("ssbo{}[{}]={}.y;", binding.U32(), offset.U32() + 1, value);
+    const auto offset_var{ctx.reg_alloc.Consume(offset)};
+    ctx.Add("ssbo{}[{}]={}.x;", binding.U32(), offset_var, value);
+    ctx.Add("ssbo{}[{}+1]={}.y;", binding.U32(), offset_var, value);
 }
 
 void EmitWriteStorage128([[maybe_unused]] EmitContext& ctx,