Fix SM1 shaders on SPIR-V after TEX* opcodes changes:
authorMartin Krošlák <kroslakma@gmail.com>
Thu, 23 Jul 2020 18:16:22 -0400
changeset 1299 02d36217591b
parent 1298 b70ccc2c7a51
child 1300 f1cdc5187d53
Fix SM1 shaders on SPIR-V after TEX* opcodes changes: - Fix implicit input attribute on SPIR-V profile - Remove texcoord attribs from other TEX* opcodes
mojoshader.c
profiles/mojoshader_profile_spirv.c
profiles/mojoshader_profile_spirv.h
--- a/mojoshader.c	Thu Jul 23 05:12:09 2020 -0400
+++ b/mojoshader.c	Thu Jul 23 18:16:22 2020 -0400
@@ -1736,16 +1736,6 @@
         TextureType ttyp = (dims == 2) ? TEXTURE_TYPE_2D : TEXTURE_TYPE_CUBE;
         add_sampler(ctx, dst->regnum, ttyp, texbem);
     } // if
-
-    add_attribute_register(ctx, REG_TYPE_TEXTURE, dst->regnum,
-                           MOJOSHADER_USAGE_TEXCOORD, dst->regnum, 0xF, 0);
-
-    // Strictly speaking, there should be a TEX opcode prior to this call that
-    //  should fill in this metadata, but I'm not sure that's required for the
-    //  shader to assemble in D3D, so we'll do this so we don't fail with a
-    //  cryptic error message even if the developer didn't do the TEX.
-    add_attribute_register(ctx, REG_TYPE_TEXTURE, src->regnum,
-                           MOJOSHADER_USAGE_TEXCOORD, src->regnum, 0xF, 0);
 } // state_texops
 
 static void state_texbem(Context *ctx, const char *opcode)
--- a/profiles/mojoshader_profile_spirv.c	Thu Jul 23 05:12:09 2020 -0400
+++ b/profiles/mojoshader_profile_spirv.c	Thu Jul 23 18:16:22 2020 -0400
@@ -1945,7 +1945,9 @@
 
                     // Overwrite Private variable with Input variable, so emit_SPIRV_finalize outputs
                     // OpEntryPoint with correct references to Input and Output variables.
+                    ctx->spirv.id_implicit_input[regnum] = id_input_var;
                     r->spirv.iddecl = id_input_var;
+                    spv_output_regname(ctx, id_input_var, regtype, regnum);
                     return;
                 } // if
                 tid = spv_get_type(ctx, STI_PTR_VEC4_P);
@@ -2481,8 +2483,21 @@
     /* 3 is for opcode + exec. model + idmain */
     uint32 inoutcount = ctx->spirv.inoutcount;
 
-    if (shader_is_pixel(ctx) && !shader_version_atleast(ctx, 2, 0))
-        inoutcount += 1;
+    uint32 implicit_input_count = sizeof(ctx->spirv.id_implicit_input) / sizeof(uint32);
+    if (shader_is_pixel(ctx))
+    {
+        if (!shader_version_atleast(ctx, 1, 4))
+        {
+            for (uint32 i = 0; i < implicit_input_count; i++)
+            {
+                if (ctx->spirv.id_implicit_input[i])
+                    inoutcount += 1;
+            } // for
+        } // if
+
+        if (!shader_version_atleast(ctx, 2, 0))
+            inoutcount += 1;
+    } // if
 
     spv_emit_part(ctx, 3 + spv_strlen(ctx->mainfn) + inoutcount, 3, SpvOpEntryPoint,
         model, ctx->spirv.idmain
@@ -2520,6 +2535,16 @@
     // only applies to pixel shaders
     if (shader_is_pixel(ctx))
     {
+        if (!shader_version_atleast(ctx, 1, 4))
+        {
+            for (uint32 i = 0; i < implicit_input_count; i++)
+            {
+                uint32 id = ctx->spirv.id_implicit_input[i];
+                if (id)
+                    spv_emit_word(ctx, id);
+            } // for
+        } // if
+
         if (!shader_version_atleast(ctx, 2, 0))
         {
             // r0 is used as color output.
--- a/profiles/mojoshader_profile_spirv.h	Thu Jul 23 05:12:09 2020 -0400
+++ b/profiles/mojoshader_profile_spirv.h	Thu Jul 23 18:16:22 2020 -0400
@@ -211,6 +211,11 @@
         uint32 idtexbeml;
     } sampler_extras[4];
 
+    // TEX opcode in ps_1_3 and below has one implicit texcoord input attribute for each texture
+    // register. We use this array to hold SSA id of this input attribute (see emit_SPIRV_global
+    // for details).
+    uint32 id_implicit_input[4];
+
     int loop_stack_idx;
     SpirvLoopInfo loop_stack[32];
 } SpirvContext;