Navigation Menu

Skip to content

Commit

Permalink
hlsl: Emit attributes for POSITION1 and above.
Browse files Browse the repository at this point in the history
Also, add an assert for the case where a buggy VS is missing outputs its PS
needs (the shader will fail to compile anyway and produce a mystery crash
later, so this is an improvement)
  • Loading branch information
kg committed Jun 16, 2021
1 parent c9037d9 commit ae7c0f6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
11 changes: 11 additions & 0 deletions mojoshader_d3d11.c
Expand Up @@ -452,11 +452,13 @@ static char *rewritePixelShader(MOJOSHADER_d3d11Shader *vshader,
const char *pvarname, *vvarname;
for (i = 0; i < ppd->attribute_count; i++)
{
int found_matching_vs_output_for_ps_input = 0;
for (j = 0; j < vpd->output_count; j++)
{
if (ppd->attributes[i].usage == vpd->outputs[j].usage &&
ppd->attributes[i].index == vpd->outputs[j].index)
{
found_matching_vs_output_for_ps_input = 1;
pvarname = ppd->attributes[i].name;
vvarname = vpd->outputs[j].name;
if (strcmp(pvarname, vvarname) != 0)
Expand All @@ -466,6 +468,7 @@ static char *rewritePixelShader(MOJOSHADER_d3d11Shader *vshader,
vpd->outputs[j].usage == MOJOSHADER_USAGE_POSITION &&
vpd->outputs[j].index == 0)
{
found_matching_vs_output_for_ps_input = 1;
pvarname = ppd->attributes[i].name;
vvarname = vpd->outputs[j].name;
if (strcmp(pvarname, vvarname) != 0)
Expand All @@ -475,6 +478,14 @@ static char *rewritePixelShader(MOJOSHADER_d3d11Shader *vshader,

if (strcmp(ppd->attributes[i].name, "vFace") == 0)
vfaceidx = i;

// A vertex shader that doesn't properly initialize all its outputs
// can produce a situation where vpd->outputs is missing a matching
// entry for the PS's inputs, even though fxc will happily compile
// both shaders together as a technique in FX mode
// I don't know how to fix this yet, but a workaround is to
// correct your shader to zero-initialize all its outputs -kg
assert(found_matching_vs_output_for_ps_input);
} // for

// Special handling for VFACE
Expand Down
40 changes: 24 additions & 16 deletions profiles/mojoshader_profile_hlsl.c
Expand Up @@ -1048,23 +1048,31 @@ void emit_HLSL_attribute(Context *ctx, RegisterType regtype, int regnum,

else
{
if (usage == MOJOSHADER_USAGE_TEXCOORD)
switch (usage)
{
// ps_1_1 does a different hack for this attribute.
// Refer to emit_HLSL_global()'s REG_TYPE_ADDRESS code.
if (!shader_version_atleast(ctx, 1, 4))
skipreference = 1;
output_line(ctx, "float4 m_%s : TEXCOORD%d;", var, index);
} // if

else if (usage == MOJOSHADER_USAGE_COLOR)
output_line(ctx, "float4 m_%s : COLOR%d;", var, index);

else if (usage == MOJOSHADER_USAGE_FOG)
output_line(ctx, "float m_%s : FOG;", var);

else if (usage == MOJOSHADER_USAGE_NORMAL)
output_line(ctx, "float4 m_%s : NORMAL;", var);
case MOJOSHADER_USAGE_TEXCOORD:
// ps_1_1 does a different hack for this attribute.
// Refer to emit_HLSL_global()'s REG_TYPE_ADDRESS code.
if (!shader_version_atleast(ctx, 1, 4))
skipreference = 1;
output_line(ctx, "float4 m_%s : TEXCOORD%d;", var, index);
break;
case MOJOSHADER_USAGE_COLOR:
output_line(ctx, "float4 m_%s : COLOR%d;", var, index);
break;
case MOJOSHADER_USAGE_FOG:
output_line(ctx, "float m_%s : FOG;", var);
break;
case MOJOSHADER_USAGE_NORMAL:
output_line(ctx, "float4 m_%s : NORMAL;", var);
break;
case MOJOSHADER_USAGE_POSITION:
output_line(ctx, "float4 m_%s : POSITION%d;", var, index);
break;
default:
fail(ctx, "BUG: unhandled pixel shader input");
break;
} // switch
} // else

pop_output(ctx);
Expand Down

0 comments on commit ae7c0f6

Please sign in to comment.