Skip to content

Commit

Permalink
Implement per-mip logic for the convolution shader
Browse files Browse the repository at this point in the history
  • Loading branch information
ApoorvaJ committed Sep 18, 2023
1 parent 76d314d commit fb312d3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
29 changes: 27 additions & 2 deletions libraries/pbrlib/genglsl/lib/mx_pre_convolve_environment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,33 @@

vec3 mx_pre_convolve_environment()
{
vec2 uv = gl_FragCoord.xy / vec2(2048.0, 1024.0);
vec2 uv = gl_FragCoord.xy * pow(2.0, (float)$convolutionMipLevel) / vec2(2048.0, 1024.0);
// vec2 ggxDirAlbedo = mx_ggx_dir_albedo(uv.x, uv.y, vec3(1, 0, 0), vec3(0, 1, 0)).xy;
return textureLod($envRadiance, uv, 0).rgb * vec3(uv, 0.0);
vec3 col = vec3(0, 0, 0);
switch ($convolutionMipLevel)
{
case 0:
col = vec3(1.0, 0.0, 0.0);
break;
case 1:
col = vec3(0.0, 1.0, 0.0);
break;
case 2:
col = vec3(0.0, 0.0, 1.0);
break;
case 3:
col = vec3(1.0, 0.0, 1.0);
break;
case 4:
col = vec3(1.0, 1.0, 0.0);
break;
case 5:
col = vec3(0.0, 1.0, 1.0);
break;
case 6:
col = vec3(1.0, 1.0, 1.0);
break;
}
return textureLod($envRadiance, uv, 0).rgb * col;
// return vec3(uv, 0.0);
}
4 changes: 4 additions & 0 deletions source/MaterialXGenShader/HwShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const string T_SHADOW_MAP = "$shadowMap";
const string T_SHADOW_MATRIX = "$shadowMatrix";
const string T_VERTEX_DATA_INSTANCE = "$vd";
const string T_LIGHT_DATA_INSTANCE = "$lightData";
const string T_CONVOLUTION_MIP_LEVEL = "$convolutionMipLevel";

const string IN_POSITION = "i_position";
const string IN_NORMAL = "i_normal";
Expand Down Expand Up @@ -123,6 +124,7 @@ const string SHADOW_MATRIX = "u_shadowMatrix";
const string VERTEX_DATA_INSTANCE = "vd";
const string LIGHT_DATA_INSTANCE = "u_lightData";
const string LIGHT_DATA_MAX_LIGHT_SOURCES = "MAX_LIGHT_SOURCES";
const string CONVOLUTION_MIP_LEVEL = "u_convolutionMipLevel";

const string VERTEX_INPUTS = "VertexInputs";
const string VERTEX_DATA = "VertexData";
Expand Down Expand Up @@ -222,6 +224,7 @@ HwShaderGenerator::HwShaderGenerator(SyntaxPtr syntax) :
_tokenSubstitutions[HW::T_AMB_OCC_GAIN] = HW::AMB_OCC_GAIN;
_tokenSubstitutions[HW::T_VERTEX_DATA_INSTANCE] = HW::VERTEX_DATA_INSTANCE;
_tokenSubstitutions[HW::T_LIGHT_DATA_INSTANCE] = HW::LIGHT_DATA_INSTANCE;
_tokenSubstitutions[HW::T_CONVOLUTION_MIP_LEVEL] = HW::CONVOLUTION_MIP_LEVEL;

// Setup closure contexts for defining closure functions
//
Expand Down Expand Up @@ -363,6 +366,7 @@ ShaderPtr HwShaderGenerator::createShader(const string& name, ElementPtr element
if (context.getOptions().hwWriteEnvPreConvolution)
{
psPrivateUniforms->add(Type::FILENAME, HW::T_ENV_RADIANCE);
psPrivateUniforms->add(Type::INTEGER, HW::T_CONVOLUTION_MIP_LEVEL, Value::createValue<int>(1));
}

// Create uniforms for the published graph interface
Expand Down
2 changes: 2 additions & 0 deletions source/MaterialXGenShader/HwShaderGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ extern MX_GENSHADER_API const string T_SHADOW_MAP;
extern MX_GENSHADER_API const string T_SHADOW_MATRIX;
extern MX_GENSHADER_API const string T_VERTEX_DATA_INSTANCE;
extern MX_GENSHADER_API const string T_LIGHT_DATA_INSTANCE;
extern MX_GENSHADER_API const string T_CONVOLUTION_MIP_LEVEL;

/// Default names for identifiers.
/// Replacing above tokens in final code.
Expand Down Expand Up @@ -192,6 +193,7 @@ extern MX_GENSHADER_API const string SHADOW_MATRIX;
extern MX_GENSHADER_API const string VERTEX_DATA_INSTANCE;
extern MX_GENSHADER_API const string LIGHT_DATA_INSTANCE;
extern MX_GENSHADER_API const string LIGHT_DATA_MAX_LIGHT_SOURCES;
extern MX_GENSHADER_API const string CONVOLUTION_MIP_LEVEL;

/// Variable blocks names.
extern MX_GENSHADER_API const string VERTEX_INPUTS; // Geometric inputs for vertex stage.
Expand Down
1 change: 1 addition & 0 deletions source/MaterialXView/RenderPipelineMetal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@

framebuffer->bind(desc);
material->bindShader();
material->getProgram()->bindUniform(mx::HW::CONVOLUTION_MIP_LEVEL, mx::Value::createValue(i));

material->getProgram()->prepareUsedResources(
MTL(renderCmdEncoder),
Expand Down

0 comments on commit fb312d3

Please sign in to comment.