mojoshader_common.c
changeset 1277 da61410edbc9
parent 1239 98ccda2125b1
child 1278 be0f548f321f
--- a/mojoshader_common.c	Mon Jul 06 16:23:06 2020 -0400
+++ b/mojoshader_common.c	Tue Jul 07 17:19:45 2020 -0400
@@ -1055,5 +1055,68 @@
     return (text - textstart);
 } // MOJOSHADER_printFloat
 
+#if SUPPORT_PROFILE_SPIRV
+#include "spirv/spirv.h"
+#include "spirv/GLSL.std.450.h"
+void MOJOSHADER_spirv_link_attributes(const MOJOSHADER_parseData *vertex,
+                                      const MOJOSHADER_parseData *pixel)
+{
+    int i;
+    uint32 attr_loc = 1; // 0 is reserved for COLOR0
+    uint32 vOffset, pOffset;
+    int vDataLen = vertex->output_len - sizeof(SpirvPatchTable);
+    int pDataLen = pixel->output_len - sizeof(SpirvPatchTable);
+    SpirvPatchTable *vTable = (SpirvPatchTable *) &vertex->output[vDataLen];
+    SpirvPatchTable *pTable = (SpirvPatchTable *) &pixel->output[pDataLen];
+    const uint32 texcoord0Loc = pTable->attrib_offsets[MOJOSHADER_USAGE_TEXCOORD][0];
+
+    for (i = 0; i < pixel->attribute_count; i++)
+    {
+        const MOJOSHADER_attribute *pAttr = &pixel->attributes[i];
+        if (pAttr->usage == MOJOSHADER_USAGE_COLOR && pAttr->index == 0)
+            continue;
+
+        // The input may not exist in the output list!
+        pOffset = pTable->attrib_offsets[pAttr->usage][pAttr->index];
+        vOffset = vTable->attrib_offsets[pAttr->usage][pAttr->index];
+        ((uint32 *) pixel->output)[pOffset] = attr_loc;
+        if (vOffset)
+            ((uint32 *) vertex->output)[vOffset] = attr_loc;
+        attr_loc++;
+    } // for
+
+    // There may be outputs not present in the input list!
+    for (i = 0; i < vertex->output_count; i++)
+    {
+        const MOJOSHADER_attribute *vAttr = &vertex->outputs[i];
+        if (vAttr->usage == MOJOSHADER_USAGE_POSITION && vAttr->index == 0)
+            continue;
+        if (vAttr->usage == MOJOSHADER_USAGE_COLOR && vAttr->index == 0)
+            continue;
+
+        if (!pTable->attrib_offsets[vAttr->usage][vAttr->index])
+        {
+            vOffset = vTable->attrib_offsets[vAttr->usage][vAttr->index];
+            ((uint32 *) vertex->output)[vOffset] = attr_loc++;
+        } // if
+    } // while
+
+    // gl_PointCoord support
+    if (texcoord0Loc)
+    {
+        if (vTable->attrib_offsets[MOJOSHADER_USAGE_POINTSIZE][0] > 0)
+        {
+            ((uint32 *) pixel->output)[texcoord0Loc - 1] = SpvDecorationBuiltIn;
+            ((uint32 *) pixel->output)[texcoord0Loc] = SpvBuiltInPointCoord;
+        } // if
+        else
+        {
+            // texcoord0Loc should already have attr_loc from the above work!
+            ((uint32 *) pixel->output)[texcoord0Loc - 1] = SpvDecorationLocation;
+        } // else
+    } // if
+} // MOJOSHADER_spirv_link_attributes
+#endif
+
 // end of mojoshader_common.c ...