Skip to content

Commit

Permalink
Clarify data generation shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
jstone-lucasfilm committed Dec 30, 2023
1 parent eae062a commit 38606ad
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 21 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,17 @@ vec3 mx_latlong_map_projection_inverse(vec2 uv)
return vec3(x, y, z);
}

vec3 mx_prefilter_environment()
vec3 mx_generate_prefilter_env()
{
vec2 uv = gl_FragCoord.xy * pow(2.0, $envPrefilterMip) / vec2(2048.0, 1024.0);
float alpha = mx_latlong_lod_to_alpha(float($envPrefilterMip));
if ($envPrefilterMip == 0)
{
return textureLod($envRadiance, uv, 0).rgb;
}

// Compute world normal and transform.
vec3 worldN = mx_latlong_map_projection_inverse(uv);
mat3 tangentToWorld = mx_orthonormal_basis(worldN);

// Local normal and view vectors are constant and aligned.
// The tangent view vector is aligned with the normal.
vec3 V = vec3(0.0, 0.0, 1.0);
float NdotV = 1.0;

// Compute derived properties.
float alpha = mx_latlong_lod_to_alpha(float($envPrefilterMip));
vec2 outputUv = gl_FragCoord.xy * pow(2.0, $envPrefilterMip) / vec2(textureSize($envRadiance, 0));
vec3 worldN = mx_latlong_map_projection_inverse(outputUv);
mat3 tangentToWorld = mx_orthonormal_basis(worldN);
float G1V = mx_ggx_smith_G1(NdotV, alpha);

// Integrate the LD term for the given environment and alpha.
Expand Down
5 changes: 3 additions & 2 deletions libraries/pbrlib/genglsl/lib/mx_microfacet_specular.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ vec3 mx_latlong_map_lookup(vec3 dir, mat4 transform, float lod, sampler2D envSam
return textureLod(envSampler, uv, lod).rgb;
}

// Return the mip level with the appropriate coverage for a filtered importance sample.
// https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch20.html
// Section 20.4 Equation 13
float mx_latlong_compute_lod(vec3 dir, float pdf, float maxMipLevel, int envSamples)
Expand All @@ -604,16 +605,16 @@ float mx_latlong_compute_lod(vec3 dir, float pdf, float maxMipLevel, int envSamp
return max(effectiveMaxMipLevel - 0.5 * log2(float(envSamples) * pdf * distortion), 0.0);
}

// Return the mip level associated with the given alpha in a prefiltered environment.
float mx_latlong_alpha_to_lod(float alpha)
{
// Return the mip level associated with the given alpha in a prefiltered environment.
float lodBias = (alpha < 0.25) ? sqrt(alpha) : 0.5 * alpha + 0.375;
return lodBias * float($envRadianceMips - 1);
}

// Return the alpha associated with the given mip level in a prefiltered environment.
float mx_latlong_lod_to_alpha(float lod)
{
// Return the alpha associated with the given mip level in a prefiltered environment.
float lodBias = lod / float($envRadianceMips - 1);
return (lodBias < 0.5) ? mx_square(lodBias) : 2.0 * (lodBias - 0.375);
}
6 changes: 3 additions & 3 deletions source/MaterialXGenGlsl/GlslShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,14 +587,14 @@ void GlslShaderGenerator::emitPixelStage(const ShaderGraph& graph, GenContext& c
// Emit directional albedo table code.
if (context.getOptions().hwWriteAlbedoTable)
{
emitLibraryInclude("pbrlib/genglsl/lib/mx_table.glsl", context, stage);
emitLibraryInclude("pbrlib/genglsl/lib/mx_generate_albedo_table.glsl", context, stage);
emitLineBreak(stage);
}

// Emit environment prefiltering code
if (context.getOptions().hwWriteEnvPrefilter)
{
emitLibraryInclude("pbrlib/genglsl/lib/mx_prefilter_environment.glsl", context, stage);
emitLibraryInclude("pbrlib/genglsl/lib/mx_generate_prefilter_env.glsl", context, stage);
emitLineBreak(stage);
}

Expand Down Expand Up @@ -645,7 +645,7 @@ void GlslShaderGenerator::emitPixelStage(const ShaderGraph& graph, GenContext& c
}
else if (context.getOptions().hwWriteEnvPrefilter)
{
emitLine(outputSocket->getVariable() + " = vec4(mx_prefilter_environment(), 1.0)", stage);
emitLine(outputSocket->getVariable() + " = vec4(mx_generate_prefilter_env(), 1.0)", stage);
}
else
{
Expand Down
6 changes: 3 additions & 3 deletions source/MaterialXGenMsl/MslShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1055,14 +1055,14 @@ void MslShaderGenerator::emitPixelStage(const ShaderGraph& graph, GenContext& co
// Emit directional albedo table code.
if (context.getOptions().hwWriteAlbedoTable)
{
emitLibraryInclude("pbrlib/genglsl/lib/mx_table.glsl", context, stage);
emitLibraryInclude("pbrlib/genglsl/lib/mx_generate_albedo_table.glsl", context, stage);
emitLineBreak(stage);
}

// Emit environment prefiltering code
if (context.getOptions().hwWriteEnvPrefilter)
{
emitLibraryInclude("pbrlib/genglsl/lib/mx_prefilter_environment.glsl", context, stage);
emitLibraryInclude("pbrlib/genglsl/lib/mx_generate_prefilter_env.glsl", context, stage);
emitLineBreak(stage);
}

Expand Down Expand Up @@ -1113,7 +1113,7 @@ void MslShaderGenerator::emitPixelStage(const ShaderGraph& graph, GenContext& co
}
else if (context.getOptions().hwWriteEnvPrefilter)
{
emitLine(outputSocket->getVariable() + " = float4(mx_prefilter_environment(), 1.0)", stage);
emitLine(outputSocket->getVariable() + " = float4(mx_generate_prefilter_env(), 1.0)", stage);
}
else
{
Expand Down

0 comments on commit 38606ad

Please sign in to comment.