From 27548b2e4a3a6a04f5248daaf7d3d58c82e0cbe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kro=C5=A1l=C3=A1k?= Date: Thu, 23 Jul 2020 18:16:22 -0400 Subject: [PATCH] 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 | 10 ---------- profiles/mojoshader_profile_spirv.c | 29 +++++++++++++++++++++++++++-- profiles/mojoshader_profile_spirv.h | 5 +++++ 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/mojoshader.c b/mojoshader.c index a5c4f967..ffd842b8 100644 --- a/mojoshader.c +++ b/mojoshader.c @@ -1736,16 +1736,6 @@ static void state_texops(Context *ctx, const char *opcode, 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) diff --git a/profiles/mojoshader_profile_spirv.c b/profiles/mojoshader_profile_spirv.c index c88f26a2..c29bb5e7 100644 --- a/profiles/mojoshader_profile_spirv.c +++ b/profiles/mojoshader_profile_spirv.c @@ -1945,7 +1945,9 @@ void emit_SPIRV_global(Context *ctx, RegisterType regtype, int regnum) // 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 @@ void emit_SPIRV_finalize(Context *ctx) /* 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 @@ void emit_SPIRV_finalize(Context *ctx) // 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. diff --git a/profiles/mojoshader_profile_spirv.h b/profiles/mojoshader_profile_spirv.h index a04f7a25..67618f60 100644 --- a/profiles/mojoshader_profile_spirv.h +++ b/profiles/mojoshader_profile_spirv.h @@ -211,6 +211,11 @@ typedef struct SpirvContext 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;