From 3f31a547e04b9899f1e572faa7a830aca107033e Mon Sep 17 00:00:00 2001
From: ameerj <52414509+ameerj@users.noreply.github.com>
Date: Tue, 8 Jun 2021 21:22:50 -0400
Subject: [PATCH] glsl: Implement more attribute getters and setters

---
 .../backend/glsl/emit_context.cpp             |  4 ++
 .../glsl/emit_glsl_context_get_set.cpp        | 68 +++++++++++++++----
 2 files changed, 60 insertions(+), 12 deletions(-)

diff --git a/src/shader_recompiler/backend/glsl/emit_context.cpp b/src/shader_recompiler/backend/glsl/emit_context.cpp
index 6c2828644..e0d678554 100644
--- a/src/shader_recompiler/backend/glsl/emit_context.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_context.cpp
@@ -239,6 +239,7 @@ bool UsesTyplessImage(const Info& info) {
 EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile& profile_,
                          const RuntimeInfo& runtime_info_)
     : info{program.info}, profile{profile_}, runtime_info{runtime_info_} {
+    header += "#pragma optionNV(fastmath off)\n";
     SetupExtensions(header);
     stage = program.stage;
     switch (program.stage) {
@@ -351,6 +352,9 @@ void EmitContext::SetupExtensions(std::string&) {
     if (info.uses_sparse_residency) {
         header += "#extension GL_ARB_sparse_texture2 : enable\n";
     }
+    if (info.stores_viewport_mask && profile.support_viewport_mask) {
+        header += "#extension GL_NV_viewport_array2 : enable\n";
+    }
     if (UsesTyplessImage(info)) {
         header += "#extension GL_EXT_shader_image_load_formatted : enable\n";
     }
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_context_get_set.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_context_get_set.cpp
index d09187ea7..c9a2ceb3d 100644
--- a/src/shader_recompiler/backend/glsl/emit_glsl_context_get_set.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_glsl_context_get_set.cpp
@@ -203,6 +203,9 @@ void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr,
         return;
     }
     switch (attr) {
+    case IR::Attribute::PrimitiveId:
+        ctx.AddF32("{}=itof(gl_PrimitiveID);", inst);
+        break;
     case IR::Attribute::PositionX:
     case IR::Attribute::PositionY:
     case IR::Attribute::PositionZ:
@@ -212,10 +215,20 @@ void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr,
         ctx.AddF32("{}={}{}.{};", inst, input_decorator, ctx.position_name, swizzle);
         break;
     }
+    case IR::Attribute::ColorFrontDiffuseR:
+    case IR::Attribute::ColorFrontDiffuseG:
+    case IR::Attribute::ColorFrontDiffuseB:
+    case IR::Attribute::ColorFrontDiffuseA:
+        ctx.AddF32("{}=gl_FrontMaterial.diffuse.{};", inst, swizzle);
+        break;
     case IR::Attribute::PointSpriteS:
     case IR::Attribute::PointSpriteT:
         ctx.AddF32("{}=gl_PointCoord.{};", inst, swizzle);
         break;
+    case IR::Attribute::TessellationEvaluationPointU:
+    case IR::Attribute::TessellationEvaluationPointV:
+        ctx.AddF32("{}=gl_TessCoord.{};", inst, swizzle);
+        break;
     case IR::Attribute::InstanceId:
         ctx.AddF32("{}=itof(gl_InstanceID);", inst);
         break;
@@ -225,10 +238,6 @@ void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr,
     case IR::Attribute::FrontFace:
         ctx.AddF32("{}=itof(gl_FrontFacing?-1:0);", inst);
         break;
-    case IR::Attribute::TessellationEvaluationPointU:
-    case IR::Attribute::TessellationEvaluationPointV:
-        ctx.AddF32("{}=gl_TessCoord.{};", inst, swizzle);
-        break;
     default:
         fmt::print("Get attribute {}", attr);
         throw NotImplementedException("Get attribute {}", attr);
@@ -262,6 +271,23 @@ void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, std::string_view val
         }
         ctx.Add("gl_Layer=ftoi({});", value);
         break;
+    case IR::Attribute::ViewportIndex:
+        if (ctx.stage != Stage::Geometry &&
+            !ctx.profile.support_viewport_index_layer_non_geometry) {
+            // LOG_WARNING(..., "Shader stores viewport index but device does not support viewport
+            // layer extension");
+            break;
+        }
+        ctx.Add("gl_ViewportIndex=ftoi({});", value);
+        break;
+    case IR::Attribute::ViewportMask:
+        if (ctx.stage != Stage::Geometry && !ctx.profile.support_viewport_mask) {
+            // LOG_WARNING(..., "Shader stores viewport mask but device does not support viewport
+            // mask extension");
+            break;
+        }
+        ctx.Add("gl_ViewportMask[0]=ftoi({});", value);
+        break;
     case IR::Attribute::PointSize:
         ctx.Add("gl_PointSize={};", value);
         break;
@@ -271,14 +297,32 @@ void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, std::string_view val
     case IR::Attribute::PositionW:
         ctx.Add("gl_Position.{}={};", swizzle, value);
         break;
-    case IR::Attribute::ViewportIndex:
-        if (ctx.stage != Stage::Geometry &&
-            !ctx.profile.support_viewport_index_layer_non_geometry) {
-            // LOG_WARNING(..., "Shader stores viewport index but device does not support viewport
-            // layer extension");
-            break;
-        }
-        ctx.Add("gl_ViewportIndex=ftoi({});", value);
+    case IR::Attribute::ColorFrontDiffuseR:
+    case IR::Attribute::ColorFrontDiffuseG:
+    case IR::Attribute::ColorFrontDiffuseB:
+    case IR::Attribute::ColorFrontDiffuseA:
+        ctx.Add("gl_FrontMaterial.diffuse.{}={};", swizzle, value);
+        break;
+    case IR::Attribute::ColorFrontSpecularR:
+    case IR::Attribute::ColorFrontSpecularG:
+    case IR::Attribute::ColorFrontSpecularB:
+    case IR::Attribute::ColorFrontSpecularA:
+        ctx.Add("gl_FrontMaterial.specular.{}={};", swizzle, value);
+        break;
+    case IR::Attribute::ColorBackDiffuseR:
+    case IR::Attribute::ColorBackDiffuseG:
+    case IR::Attribute::ColorBackDiffuseB:
+    case IR::Attribute::ColorBackDiffuseA:
+        ctx.Add("gl_BackMaterial.diffuse.{}={};", swizzle, value);
+        break;
+    case IR::Attribute::ColorBackSpecularR:
+    case IR::Attribute::ColorBackSpecularG:
+    case IR::Attribute::ColorBackSpecularB:
+    case IR::Attribute::ColorBackSpecularA:
+        ctx.Add("gl_BackMaterial.specular.{}={};", swizzle, value);
+        break;
+    case IR::Attribute::FogCoordinate:
+        ctx.Add("gl_FragCoord.x={};", value);
         break;
     case IR::Attribute::ClipDistance0:
     case IR::Attribute::ClipDistance1: