Skip to content

Commit

Permalink
texelFetch and comparison fixes (issues #23 and #24)
Browse files Browse the repository at this point in the history
texelFetch and texelFetchOffset swizzle fix.
Comparison opcodes give an integer result since sm4 but floatBitsToInt
was still being applied.
Missing a space on samplerBuffer declarations to separate the type from
uniform name.
  • Loading branch information
James-Jones committed Jul 31, 2014
1 parent 1536284 commit ba22d62
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
3 changes: 1 addition & 2 deletions src/toGLSLDeclaration.c
Original file line number Diff line number Diff line change
Expand Up @@ -1951,8 +1951,7 @@ Would generate a vec2 and a vec3. We discard the second one making .z invalid!
{
case RESOURCE_DIMENSION_BUFFER:
{
bcatcstr(glsl, "uniform ");
bcatcstr(glsl, GetSamplerType(psContext,
bformata(glsl, "uniform %s ", GetSamplerType(psContext,
RESOURCE_DIMENSION_BUFFER,
psDecl->asOperands[0].ui32RegisterNumber));
TranslateOperand(psContext, &psDecl->asOperands[0], TO_FLAG_NONE);
Expand Down
63 changes: 33 additions & 30 deletions src/toGLSLInstruction.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,15 @@ static void AddComparision(HLSLCrossCompilerContext* psContext, Instruction* psI

SHADER_VARIABLE_TYPE eDestType = GetOperandDataType(psContext, &psInst->asOperands[0]);

int floatResult = 0;

minElemCount = s1ElemCount < minElemCount ? s1ElemCount : minElemCount;

if(psContext->psShader->ui32MajorVersion < 4)
{
floatResult = 1;
}

if(destElemCount > 1)
{
const char* glslOpcode [] = {
Expand All @@ -160,7 +167,14 @@ static void AddComparision(HLSLCrossCompilerContext* psContext, Instruction* psI
//Component-wise compare
AddIndentation(psContext);
TranslateOperand(psContext, &psInst->asOperands[0], TO_FLAG_DESTINATION);
AddAssignToDest(psContext, &psInst->asOperands[0], typeFlag);
if(floatResult)
{
AddAssignToDest(psContext, &psInst->asOperands[0], typeFlag);
}
else
{
AddAssignToDest(psContext, &psInst->asOperands[0], typeFlag | TO_FLAG_INTEGER);
}
bcatcstr(glsl, "(");

if(eDestType == SVT_UINT)
Expand All @@ -182,7 +196,7 @@ static void AddComparision(HLSLCrossCompilerContext* psContext, Instruction* psI
TranslateOperand(psContext, &psInst->asOperands[2], typeFlag);
bcatcstr(glsl, ")");
AddSwizzleUsingElementCount(psContext, minElemCount);
if(psContext->psShader->ui32MajorVersion < 4)
if(floatResult)
{
//Result is 1.0f or 0.0f
bcatcstr(glsl, "))");
Expand Down Expand Up @@ -213,7 +227,16 @@ static void AddComparision(HLSLCrossCompilerContext* psContext, Instruction* psI
//Scalar compare
AddIndentation(psContext);
TranslateOperand(psContext, &psInst->asOperands[0], TO_FLAG_DESTINATION);
AddAssignToDest(psContext, &psInst->asOperands[0], typeFlag);

if(floatResult)
{
AddAssignToDest(psContext, &psInst->asOperands[0], typeFlag);
}
else
{
AddAssignToDest(psContext, &psInst->asOperands[0], typeFlag | TO_FLAG_INTEGER);
}

bcatcstr(glsl, "(");

bcatcstr(glsl, "((");
Expand All @@ -226,7 +249,7 @@ static void AddComparision(HLSLCrossCompilerContext* psContext, Instruction* psI
bcatcstr(glsl, ")");
if(s1ElemCount > minElemCount)
AddSwizzleUsingElementCount(psContext, minElemCount);
if(psContext->psShader->ui32MajorVersion < 4)
if(floatResult)
{
bcatcstr(glsl, ") ? 1.0f : 1.0f");
}
Expand Down Expand Up @@ -716,9 +739,6 @@ static void TranslateTexelFetch(HLSLCrossCompilerContext* psContext,
ResourceBinding* psBinding,
bstring glsl)
{
const uint32_t ui32ResultElementCount = GetNumSwizzleElements(&psInst->asOperands[0]);
const uint32_t ui32ResourceSwizzleElementCount = GetNumSwizzleElements(&psInst->asOperands[2]);

AddIndentation(psContext);
TranslateOperand(psContext, &psInst->asOperands[0], TO_FLAG_DESTINATION);
AddAssignToDest(psContext, &psInst->asOperands[0], ResourceReturnTypeToFlag(psBinding->ui32ReturnType));
Expand All @@ -728,7 +748,6 @@ static void TranslateTexelFetch(HLSLCrossCompilerContext* psContext,
{
case REFLECT_RESOURCE_DIMENSION_TEXTURE1D:
{
//texelFetch(samplerBuffer, int coord, level)
TranslateOperand(psContext, &psInst->asOperands[2], TO_FLAG_NONE);
bcatcstr(glsl, ", int((");
TranslateOperand(psContext, &psInst->asOperands[1], TO_FLAG_INTEGER);
Expand All @@ -738,7 +757,6 @@ static void TranslateTexelFetch(HLSLCrossCompilerContext* psContext,
case REFLECT_RESOURCE_DIMENSION_TEXTURE2DARRAY:
case REFLECT_RESOURCE_DIMENSION_TEXTURE3D:
{
//texelFetch(samplerBuffer, ivec3 coord, level)
TranslateOperand(psContext, &psInst->asOperands[2], TO_FLAG_NONE);
bcatcstr(glsl, ", ivec3((");
TranslateOperand(psContext, &psInst->asOperands[1], TO_FLAG_INTEGER);
Expand All @@ -748,7 +766,6 @@ static void TranslateTexelFetch(HLSLCrossCompilerContext* psContext,
case REFLECT_RESOURCE_DIMENSION_TEXTURE2D:
case REFLECT_RESOURCE_DIMENSION_TEXTURE1DARRAY:
{
//texelFetch(samplerBuffer, ivec2 coord, level)
TranslateOperand(psContext, &psInst->asOperands[2], TO_FLAG_NONE);
bcatcstr(glsl, ", ivec2((");
TranslateOperand(psContext, &psInst->asOperands[1], TO_FLAG_INTEGER);
Expand All @@ -757,7 +774,6 @@ static void TranslateTexelFetch(HLSLCrossCompilerContext* psContext,
}
case REFLECT_RESOURCE_DIMENSION_BUFFER:
{
//texelFetch(samplerBuffer, scalar integer coord)
TranslateOperand(psContext, &psInst->asOperands[2], TO_FLAG_NONE);
bcatcstr(glsl, ", int((");
TranslateOperand(psContext, &psInst->asOperands[1], TO_FLAG_INTEGER);
Expand All @@ -766,8 +782,6 @@ static void TranslateTexelFetch(HLSLCrossCompilerContext* psContext,
}
case REFLECT_RESOURCE_DIMENSION_TEXTURE2DMS:
{
//texelFetch(samplerBuffer, ivec2 coord, sample)

ASSERT(psInst->eOpcode == OPCODE_LD_MS);
TranslateOperand(psContext, &psInst->asOperands[2], TO_FLAG_NONE);
bcatcstr(glsl, ", ivec2((");
Expand All @@ -779,8 +793,6 @@ static void TranslateTexelFetch(HLSLCrossCompilerContext* psContext,
}
case REFLECT_RESOURCE_DIMENSION_TEXTURE2DMSARRAY:
{
//texelFetch(samplerBuffer, ivec3 coord, sample)

ASSERT(psInst->eOpcode == OPCODE_LD_MS);
TranslateOperand(psContext, &psInst->asOperands[2], TO_FLAG_NONE);
bcatcstr(glsl, ", ivec3((");
Expand All @@ -801,22 +813,16 @@ static void TranslateTexelFetch(HLSLCrossCompilerContext* psContext,
}

TranslateOperandSwizzle(psContext, &psInst->asOperands[2]);
if(ui32ResourceSwizzleElementCount != ui32ResultElementCount)
{
AddSwizzleUsingElementCount(psContext, ui32ResultElementCount);
}

bcatcstr(glsl, ");\n");
bcatcstr(glsl, ")");
TranslateOperandSwizzle(psContext, &psInst->asOperands[0]);
bcatcstr(glsl, ";\n");
}

static void TranslateTexelFetchOffset(HLSLCrossCompilerContext* psContext,
Instruction* psInst,
ResourceBinding* psBinding,
bstring glsl)
{
const uint32_t ui32ResultElementCount = GetNumSwizzleElements(&psInst->asOperands[0]);
const uint32_t ui32ResourceSwizzleElementCount = GetNumSwizzleElements(&psInst->asOperands[2]);

AddIndentation(psContext);
TranslateOperand(psContext, &psInst->asOperands[0], TO_FLAG_DESTINATION);
AddAssignToDest(psContext, &psInst->asOperands[0], ResourceReturnTypeToFlag(psBinding->ui32ReturnType));
Expand Down Expand Up @@ -883,12 +889,9 @@ static void TranslateTexelFetchOffset(HLSLCrossCompilerContext* psContext,
}

TranslateOperandSwizzle(psContext, &psInst->asOperands[2]);
if(ui32ResourceSwizzleElementCount != ui32ResultElementCount)
{
AddSwizzleUsingElementCount(psContext, ui32ResultElementCount);
}

bcatcstr(glsl, ");\n");
bcatcstr(glsl, ")");
TranslateOperandSwizzle(psContext, &psInst->asOperands[0]);
bcatcstr(glsl, ";\n");
}


Expand Down

0 comments on commit ba22d62

Please sign in to comment.