Skip to content

Commit

Permalink
Clean up assembler writemask parsing a little.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Jul 20, 2020
1 parent 22f2853 commit e4d4550
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions mojoshader_assembler.c
Expand Up @@ -128,6 +128,19 @@ static inline int isfail(const Context *ctx)
return ctx->isfail; return ctx->isfail;
} // isfail } // isfail


static int vecsize_from_writemask(const uint8 m)
{
return (m & 1) + ((m >> 1) & 1) + ((m >> 2) & 1) + ((m >> 3) & 1);
} // vecsize_from_writemask

static void set_dstarg_writemask(DestArgInfo *dst, const uint8 mask)
{
dst->writemask = mask;
dst->writemask0 = ((mask >> 0) & 1);
dst->writemask1 = ((mask >> 1) & 1);
dst->writemask2 = ((mask >> 2) & 1);
dst->writemask3 = ((mask >> 3) & 1);
} // set_dstarg_writemask


// Shader model version magic... // Shader model version magic...


Expand Down Expand Up @@ -553,12 +566,8 @@ static int parse_destination_token(Context *ctx)
int invalid_writemask = 0; int invalid_writemask = 0;
if (nexttoken(ctx) != ((Token) '.')) if (nexttoken(ctx) != ((Token) '.'))
{ {
info->writemask = ctx->default_writemask; set_dstarg_writemask(info, ctx->default_writemask);
info->writemask0 = ((info->writemask >> 0) & 0x1); pushback(ctx); // no explicit writemask; do default mask.
info->writemask1 = ((info->writemask >> 1) & 0x1);
info->writemask2 = ((info->writemask >> 2) & 0x1);
info->writemask3 = ((info->writemask >> 3) & 0x1);
pushback(ctx); // no explicit writemask; do full mask.
} // if } // if
else if (nexttoken(ctx) != TOKEN_IDENTIFIER) else if (nexttoken(ctx) != TOKEN_IDENTIFIER)
{ {
Expand All @@ -570,33 +579,29 @@ static int parse_destination_token(Context *ctx)
const unsigned int tokenlen = ctx->tokenlen; const unsigned int tokenlen = ctx->tokenlen;
memcpy(tokenbytes, ctx->token, ((tokenlen < 4) ? tokenlen : 4)); memcpy(tokenbytes, ctx->token, ((tokenlen < 4) ? tokenlen : 4));
char *ptr = tokenbytes; char *ptr = tokenbytes;

uint8 writemask = 0;
if ((*ptr == 'r') || (*ptr == 'x')) { info->writemask0 = 1; ptr++; } if ((*ptr == 'r') || (*ptr == 'x')) { writemask |= (1<<0); ptr++; }
if ((*ptr == 'g') || (*ptr == 'y')) { info->writemask1 = 1; ptr++; } if ((*ptr == 'g') || (*ptr == 'y')) { writemask |= (1<<1); ptr++; }
if ((*ptr == 'b') || (*ptr == 'z')) { info->writemask2 = 1; ptr++; } if ((*ptr == 'b') || (*ptr == 'z')) { writemask |= (1<<2); ptr++; }
if ((*ptr == 'a') || (*ptr == 'w')) { info->writemask3 = 1; ptr++; } if ((*ptr == 'a') || (*ptr == 'w')) { writemask |= (1<<3); ptr++; }


if (*ptr != '\0') if (*ptr != '\0')
invalid_writemask = 1; invalid_writemask = 1;


info->writemask = ( ((info->writemask0 & 0x1) << 0) |
((info->writemask1 & 0x1) << 1) |
((info->writemask2 & 0x1) << 2) |
((info->writemask3 & 0x1) << 3) );

// Cg generates code with oDepth.z, and Microsoft's tools accept // Cg generates code with oDepth.z, and Microsoft's tools accept
// oFog.x and probably others. For safety's sake, we'll allow // oFog.x and probably others. For safety's sake, we'll allow
// any single channel to be specified and will just wipe out the // any single channel to be specified and will just wipe out the
// writemask as if it wasn't specified at all. More than one // writemask as if it wasn't specified at all. More than one
// channel will be a fail, though. // channel will be a fail, though.
if (!invalid_writemask && scalar_register(ctx->shader_type, info->regtype, info->regnum)) if (!invalid_writemask && scalar_register(ctx->shader_type, info->regtype, info->regnum))
{ {
const int numchans = info->writemask0 + info->writemask1 + info->writemask2 + info->writemask3; const int numchans = vecsize_from_writemask(writemask);
if (numchans != 1) if (numchans != 1)
fail(ctx, "Non-scalar writemask specified for scalar register"); fail(ctx, "Non-scalar writemask specified for scalar register");
info->writemask = 0xF; writemask = 0xF;
info->writemask0 = info->writemask1 = info->writemask2 = info->writemask3 = 1;
} // if } // if

set_dstarg_writemask(info, writemask);
} // else } // else


if (invalid_writemask) if (invalid_writemask)
Expand Down

0 comments on commit e4d4550

Please sign in to comment.