Skip to content

Commit

Permalink
Fix Chiang hair model for Metal Shading language (AcademySoftwareFoun…
Browse files Browse the repository at this point in the history
…dation#2057)

The current approach for converting GLSL code to MSL doesn't work for the recent Chiang hair PR AcademySoftwareFoundation#1968.
  • Loading branch information
ld-kerley authored Oct 10, 2024
1 parent 5101013 commit 0f2438e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
13 changes: 11 additions & 2 deletions libraries/pbrlib/genglsl/mx_hair_bsdf.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,21 @@ vec3 mx_chiang_hair_bsdf(
float alpha = cuticle_angle * M_PI - (M_PI / 2.0); // remap [0, 1] to [-PI/2, PI/2]
mx_hair_alpha_angles(alpha, sinThetaI, cosThetaI, angles);

vec3 tint[4] = vec3[](tint_R, tint_TT, tint_TRT, tint_TRT);
vec3 tint[4];
tint[0] = tint_R;
tint[1] = tint_TT;
tint[2] = tint_TRT;
tint[3] = tint_TRT;

roughness_R = clamp(roughness_R, 0.001, 1.0);
roughness_TT = clamp(roughness_TT, 0.001, 1.0);
roughness_TRT = clamp(roughness_TRT, 0.001, 1.0);
vec2 vs[4] = vec2[](roughness_R, roughness_TT, roughness_TRT, roughness_TRT);

vec2 vs[4];
vs[0] = roughness_R;
vs[1] = roughness_TT;
vs[2] = roughness_TRT;
vs[3] = roughness_TRT;

// R, TT, TRT, TRRT+
vec3 F = vec3(0.0);
Expand Down
24 changes: 23 additions & 1 deletion source/MaterialXGenMsl/MslShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,29 @@ void MslShaderGenerator::MetalizeGeneratedShader(ShaderStage& shaderStage) const
}
size_t typename_end = pos;
std::string typeName = sourceCode.substr(typename_beg, typename_end - typename_beg);
sourceCode.replace(beg, typename_end - beg, "thread " + typeName + "&");

while (std::isspace(sourceCode[pos]))
{
++pos;
}
size_t varname_beg = pos;
while (!std::isspace(sourceCode[pos]) && sourceCode[pos] != '\n' && sourceCode[pos] != ',' && sourceCode[pos] != ')' )
{
++pos;
}
size_t varname_end = pos;
std::string varName = sourceCode.substr(varname_beg, varname_end - varname_beg);

if (varName.find('[') != std::string::npos)
{
// if the variable is an array then we don't need to declare it as a reference,
// we will effectively just be passing the pointer to the array
sourceCode.replace(beg, typename_end - beg, "thread " + typeName);
}
else
{
sourceCode.replace(beg, typename_end - beg, "thread " + typeName + "&");
}
}
pos = sourceCode.find(keyword, pos);
}
Expand Down

0 comments on commit 0f2438e

Please sign in to comment.