Skip to content

Commit

Permalink
[HdSt] Improved GLSL and MSL shader compatibility
Browse files Browse the repository at this point in the history
Updated curve basis coefficient and array initialization to
use braced initializer list syntax since that is supported
for both GLSL and MSL.

Also, added some floating point literal suffixes to help
with overload resolution for MSL.

Contribution: Thor Hjalmarsson

Fixes #2027

(Internal change: 2268985)
  • Loading branch information
davidgyu authored and pixar-oss committed Mar 31, 2023
1 parent c4d17de commit af04493
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
10 changes: 5 additions & 5 deletions pxr/imaging/hdSt/codeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3662,7 +3662,7 @@ static void _EmitFVarAccessor(
str << " ivec2 fvarPatchParam = HdGet_fvarPatchParam"
<< fvarChannel << "();\n"
<< " OsdPatchParam param = OsdPatchParamInit(fvarPatchParam.x,"
<< " fvarPatchParam.y, 0);\n"
<< " fvarPatchParam.y, 0.0f);\n"
<< " float wP[20], wDu[20], wDv[20], wDuu[20], wDuv[20], "
<< "wDvv[20];\n"
<< " OsdEvaluatePatchBasisNormalized(patchType, param,"
Expand Down Expand Up @@ -3714,9 +3714,9 @@ static void _EmitFVarAccessor(
str << " ivec2 fvarPatchParam = HdGet_fvarPatchParam"
<< fvarChannel << "();\n"
<< " OsdPatchParam param = OsdPatchParamInit(fvarPatchParam.x,"
<< " fvarPatchParam.y, 0);\n"
<< " fvarPatchParam.y, 0.0f);\n"
<< " vec2 unnormalized = GetPatchCoord(localIndex).xy;\n"
<< " float uv[2] = float[2](unnormalized.x, unnormalized.y);\n"
<< " float uv[2] = { unnormalized.x, unnormalized.y };\n"
<< " OsdPatchParamNormalize(param, uv);\n"
<< " vec2 localST = vec2(uv[0], uv[1]);\n";
break;
Expand All @@ -3727,9 +3727,9 @@ static void _EmitFVarAccessor(
str << " ivec2 fvarPatchParam = HdGet_fvarPatchParam"
<< fvarChannel << "();\n"
<< " OsdPatchParam param = OsdPatchParamInit(fvarPatchParam.x,"
<< " fvarPatchParam.y, 0);\n"
<< " fvarPatchParam.y, 0.0f);\n"
<< " vec2 unnormalized = GetPatchCoord(localIndex).xy;\n"
<< " float uv[2] = float[2](unnormalized.x, unnormalized.y);\n"
<< " float uv[2] = { unnormalized.x, unnormalized.y };\n"
<< " OsdPatchParamNormalizeTriangle(param, uv);\n"
<< " vec2 localST = vec2(uv[0], uv[1]);\n";
break;
Expand Down
36 changes: 24 additions & 12 deletions pxr/imaging/hdSt/shaders/basisCurves.glslfx
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ vec2 projectToScreen(MAT4 projMat, vec4 vertex, vec2 screen_size)
{
vec4 res = vec4(projMat * vertex);
res /= res.w;
return (clamp(res.xy, -1.3, 1.3) + 1) * (screen_size * 0.5);
return (clamp(res.xy, -1.3f, 1.3f) + 1.0f) * (screen_size * 0.5f);
}

--- --------------------------------------------------------------------------
Expand Down Expand Up @@ -250,7 +250,9 @@ void determineLODSettings()

float maxWidthScreenSpace = max(length(v_w0), length(v_w1));

float level_w = clamp(maxWidthScreenSpace / GetPixelToTessRatio() / widthDecimation, 1, maxTess);
float level_w = clamp(
maxWidthScreenSpace / GetPixelToTessRatio() / widthDecimation,
1.0f, maxTess);

gl_TessLevelOuter[0] = 1;
gl_TessLevelOuter[1] = level_w;
Expand Down Expand Up @@ -305,7 +307,7 @@ void determineLODSettings()

// Need to handle off screen
float dist = distance(v0, v1) + distance(v1, v2) + distance(v2, v3);
float level = clamp(dist / GetPixelToTessRatio(), 0, maxTess);
float level = clamp(dist / GetPixelToTessRatio(), 0.0f, maxTess);

gl_TessLevelOuter[0] = 1;
gl_TessLevelOuter[1] = level;
Expand Down Expand Up @@ -361,7 +363,7 @@ void determineLODSettings()

// Need to handle off screen
float dist = distance(v0, v1) + distance(v1, v2) + distance(v2, v3);
float level = clamp(dist / GetPixelToTessRatio(), 0, maxTess);
float level = clamp(dist / GetPixelToTessRatio(), 0.0f, maxTess);

gl_TessLevelOuter[0] = level;
gl_TessLevelOuter[1] = 1;
Expand Down Expand Up @@ -409,12 +411,14 @@ void determineLODSettings()

// Need to handle off screen
float dist = distance(v0, v1) + distance(v1, v2) + distance(v2, v3);
float level = clamp(dist / GetPixelToTessRatio(), 0, maxTess);
float level = clamp(dist / GetPixelToTessRatio(), 0.0f, maxTess);

float maxWidthScreenSpace =
max(max(max(length(v_w0), length(v_w1)), length(v_w2)), length(v_w3));

float level_w = clamp(maxWidthScreenSpace / GetPixelToTessRatio() / widthDecimation, 1, maxTess);
float level_w = clamp(
maxWidthScreenSpace / GetPixelToTessRatio() / widthDecimation,
1.0f, maxTess);

gl_TessLevelOuter[0] = level;
gl_TessLevelOuter[1] = level_w;
Expand Down Expand Up @@ -598,7 +602,7 @@ void main()
vec3 evaluateNormal(float u)
{
// XXX: This clamp is a hack to mask some odd orientation flipping issues
u = clamp(u, 1e-3, 1.0 - 1e-3);
u = clamp(u, 1e-3f, 1.0f - 1e-3f);
return mix(inData[1].Neye, inData[0].Neye, u);
}

Expand Down Expand Up @@ -711,7 +715,7 @@ vec3 evaluateNormal(vec4 basis, float u)
{
// XXX: This clamp is a hack to mask some odd orientation flipping issues
// for oriented bezier curves.
u = clamp(u, 1e-3, 1.0 - 1e-3);
u = clamp(u, 1e-3f, 1.0f - 1e-3f);
return mix(inData[2].Neye, inData[1].Neye, u);
}

Expand Down Expand Up @@ -826,7 +830,9 @@ Coeffs evaluateBasis(float u, float u2, float u3)
tangent_basis[1] = -9.0*u2 + 6.0*u;
tangent_basis[2] = 9.0*u2 - 12.0*u + 3.0;
tangent_basis[3] = -3.0*u2 + 6.0*u - 3.0;
return Coeffs(basis, tangent_basis);

Coeffs coeffs = { basis, tangent_basis };
return coeffs;
}

--- --------------------------------------------------------------------------
Expand All @@ -844,7 +850,9 @@ Coeffs evaluateBasis(float u, float u2, float u3)
tangent_basis[1] = -1;
tangent_basis[2] = 0;
tangent_basis[3] = 0;
return Coeffs(basis, tangent_basis);

Coeffs coeffs = { basis, tangent_basis };
return coeffs;
}

--- --------------------------------------------------------------------------
Expand All @@ -862,7 +870,9 @@ Coeffs evaluateBasis(float u, float u2, float u3)
tangent_basis[1] = -4.5*u2 + 4.0*u + 0.5;
tangent_basis[2] = 4.5*u2 - 5.0*u;
tangent_basis[3] = -1.5*u2 + 2.0*u - 0.5;
return Coeffs(basis, tangent_basis);

Coeffs coeffs = { basis, tangent_basis };
return coeffs;
}

--- --------------------------------------------------------------------------
Expand All @@ -880,7 +890,9 @@ Coeffs evaluateBasis(float u, float u2, float u3)
tangent_basis[1] = -1.5*u2 + u + 0.5;
tangent_basis[2] = 1.5*u2 - 2.0*u;
tangent_basis[3] = -0.5*u2 + u - 0.5;
return Coeffs(basis, tangent_basis);

Coeffs coeffs = { basis, tangent_basis };
return coeffs;
}

--- --------------------------------------------------------------------------
Expand Down

0 comments on commit af04493

Please sign in to comment.