From 73cb17f41bf019df504d2d2af4ebdf45aa3201c6 Mon Sep 17 00:00:00 2001
From: FernandoS27 <fsahmkow27@gmail.com>
Date: Sun, 4 Apr 2021 09:38:15 +0200
Subject: shader: Implement indexed Position and ClipDistances

---
 .../backend/spirv/emit_context.cpp                 | 40 ++++++++++++++++++++++
 1 file changed, 40 insertions(+)

(limited to 'src/shader_recompiler/backend/spirv/emit_context.cpp')

diff --git a/src/shader_recompiler/backend/spirv/emit_context.cpp b/src/shader_recompiler/backend/spirv/emit_context.cpp
index eadecb0641..e22bb5371e 100644
--- a/src/shader_recompiler/backend/spirv/emit_context.cpp
+++ b/src/shader_recompiler/backend/spirv/emit_context.cpp
@@ -327,6 +327,10 @@ void EmitContext::DefineAttributeMemAccess(const Info& info) {
         const Id compare_index{OpShiftRightLogical(U32[1], base_index, Constant(U32[1], 2U))};
         std::vector<Sirit::Literal> literals;
         std::vector<Id> labels;
+        if (info.loads_position) {
+            literals.push_back(static_cast<u32>(IR::Attribute::PositionX) >> 2);
+            labels.push_back(OpLabel());
+        }
         const u32 base_attribute_value = static_cast<u32>(IR::Attribute::Generic0X) >> 2;
         for (u32 i = 0; i < info.input_generics.size(); i++) {
             if (!info.input_generics[i].used) {
@@ -340,6 +344,12 @@ void EmitContext::DefineAttributeMemAccess(const Info& info) {
         AddLabel(default_label);
         OpReturnValue(Constant(F32[1], 0.0f));
         size_t label_index = 0;
+        if (info.loads_position) {
+            AddLabel(labels[label_index]);
+            const Id result{OpLoad(F32[1], OpAccessChain(input_f32, input_position, masked_index))};
+            OpReturnValue(result);
+            label_index++;
+        }
         for (u32 i = 0; i < info.input_generics.size(); i++) {
             if (!info.input_generics[i].used) {
                 continue;
@@ -377,6 +387,10 @@ void EmitContext::DefineAttributeMemAccess(const Info& info) {
         const Id compare_index{OpShiftRightLogical(U32[1], base_index, Constant(U32[1], 2U))};
         std::vector<Sirit::Literal> literals;
         std::vector<Id> labels;
+        if (info.stores_position) {
+            literals.push_back(static_cast<u32>(IR::Attribute::PositionX) >> 2);
+            labels.push_back(OpLabel());
+        }
         const u32 base_attribute_value = static_cast<u32>(IR::Attribute::Generic0X) >> 2;
         for (u32 i = 0; i < info.stores_generics.size(); i++) {
             if (!info.stores_generics[i]) {
@@ -385,11 +399,24 @@ void EmitContext::DefineAttributeMemAccess(const Info& info) {
             literals.push_back(base_attribute_value + i);
             labels.push_back(OpLabel());
         }
+        if (info.stores_clip_distance) {
+            literals.push_back(static_cast<u32>(IR::Attribute::ClipDistance0) >> 2);
+            labels.push_back(OpLabel());
+            literals.push_back(static_cast<u32>(IR::Attribute::ClipDistance4) >> 2);
+            labels.push_back(OpLabel());
+        }
         OpSelectionMerge(end_block, spv::SelectionControlMask::MaskNone);
         OpSwitch(compare_index, default_label, literals, labels);
         AddLabel(default_label);
         OpReturn();
         size_t label_index = 0;
+        if (info.stores_position) {
+            AddLabel(labels[label_index]);
+            const Id pointer{OpAccessChain(output_f32, output_position, masked_index)};
+            OpStore(pointer, store_value);
+            OpReturn();
+            label_index++;
+        }
         for (u32 i = 0; i < info.stores_generics.size(); i++) {
             if (!info.stores_generics[i]) {
                 continue;
@@ -401,6 +428,19 @@ void EmitContext::DefineAttributeMemAccess(const Info& info) {
             OpReturn();
             label_index++;
         }
+        if (info.stores_clip_distance) {
+            AddLabel(labels[label_index]);
+            const Id pointer{OpAccessChain(output_f32, clip_distances, masked_index)};
+            OpStore(pointer, store_value);
+            OpReturn();
+            label_index++;
+            AddLabel(labels[label_index]);
+            const Id fixed_index{OpIAdd(U32[1], masked_index, Constant(U32[1], 4))};
+            const Id pointer2{OpAccessChain(output_f32, clip_distances, fixed_index)};
+            OpStore(pointer2, store_value);
+            OpReturn();
+            label_index++;
+        }
         AddLabel(end_block);
         OpUnreachable();
         OpFunctionEnd();
-- 
cgit v1.2.3-70-g09d2