Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
krolli committed Jul 23, 2020
1 parent 98891de commit 27548b2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
10 changes: 0 additions & 10 deletions mojoshader.c
Expand Up @@ -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)
Expand Down
29 changes: 27 additions & 2 deletions profiles/mojoshader_profile_spirv.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions profiles/mojoshader_profile_spirv.h
Expand Up @@ -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;
Expand Down

0 comments on commit 27548b2

Please sign in to comment.