Skip to content

Commit

Permalink
Fix the broken FIS sampling, since it relies on the environment map b…
Browse files Browse the repository at this point in the history
…eing non-pre-convolved.
  • Loading branch information
ApoorvaJ committed Sep 18, 2023
1 parent fa7d436 commit fb08a15
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
26 changes: 26 additions & 0 deletions source/MaterialXRender/LightHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,30 @@ class MX_RENDER_API LightHandler
return _envRadianceMap;
}

/// Set the environment radiance map for the pre-convolved environment lighting model.
void setEnvRadianceMapPreConvolved(ImagePtr map)
{
_envRadianceMapPreConvolved = map;
}

/// Return the environment radiance map for the pre-convolved environment lighting model.
ImagePtr getEnvRadianceMapPreConvolved() const
{
return _envRadianceMapPreConvolved;
}

/// Set whether to use the pre-convolved environment lighting model.
void setUsePreConvolvedEnvLighting(bool val)
{
_usePreConvolvedEnvLighting = val;
}

/// Return whether to use the pre-convolved environment lighting model.
bool getUsePreConvolvedEnvLighting()
{
return _usePreConvolvedEnvLighting;
}

/// Set the environment irradiance map
void setEnvIrradianceMap(ImagePtr map)
{
Expand Down Expand Up @@ -216,8 +240,10 @@ class MX_RENDER_API LightHandler
Matrix44 _lightTransform;
bool _directLighting;
bool _indirectLighting;
bool _usePreConvolvedEnvLighting;

ImagePtr _envRadianceMap;
ImagePtr _envRadianceMapPreConvolved;
ImagePtr _envIrradianceMap;
int _envSampleCount;

Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXRenderMsl/MslPipelineStateObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ int GetStrideOfMetalType(MTLDataType type)
// Bind environment lights.
ImageMap envLights =
{
{ HW::ENV_RADIANCE, lightHandler->getEnvRadianceMap() },
{ HW::ENV_RADIANCE, lightHandler->getUsePreConvolvedEnvLighting() ? lightHandler->getEnvRadianceMapPreConvolved() : lightHandler->getEnvRadianceMap() },
{ HW::ENV_IRRADIANCE, lightHandler->getEnvIrradianceMap() }
};
for (const auto& env : envLights)
Expand Down
4 changes: 4 additions & 0 deletions source/MaterialXView/RenderPipelineMetal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,16 @@
material->bindShader();
material->getProgram()->bindUniform(mx::HW::CONVOLUTION_MIP_LEVEL, mx::Value::createValue(i));

bool prevValue = lightHandler->getUsePreConvolvedEnvLighting();
lightHandler->setUsePreConvolvedEnvLighting(false);
material->getProgram()->prepareUsedResources(
MTL(renderCmdEncoder),
_viewer->_identityCamera,
nullptr,
imageHandler,
lightHandler);
lightHandler->setUsePreConvolvedEnvLighting(prevValue);

_viewer->renderScreenSpaceQuad(material);

MTL(endCommandBuffer());
Expand Down
10 changes: 6 additions & 4 deletions source/MaterialXView/Viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,13 +503,13 @@ void Viewer::loadEnvironmentLight()
}
}

_lightHandler->setEnvRadianceMap(envRadianceMap);
mx::ImagePtr mippedEnvRadianceMap = _renderPipeline->convolveEnvironment();
_imageHandler->releaseRenderResources(envRadianceMap);
// Release any existing environment maps and store the new ones.
_imageHandler->releaseRenderResources(_lightHandler->getEnvRadianceMap());
_imageHandler->releaseRenderResources(_lightHandler->getEnvIrradianceMap());
_lightHandler->setEnvRadianceMap(mippedEnvRadianceMap);

_lightHandler->setEnvRadianceMap(envRadianceMap);
mx::ImagePtr preConvolvedEnvRadianceMap = _renderPipeline->convolveEnvironment();
_lightHandler->setEnvRadianceMapPreConvolved(preConvolvedEnvRadianceMap);
_lightHandler->setEnvIrradianceMap(envIrradianceMap);

// Look for a light rig using an expected filename convention.
Expand Down Expand Up @@ -769,12 +769,14 @@ void Viewer::createAdvancedSettings(Widget* parent)

ng::CheckBox* importanceSampleBox = new ng::CheckBox(advancedPopup, "Environment FIS");
importanceSampleBox->set_checked(_genContext.getOptions().hwSpecularEnvironmentMethod == mx::SPECULAR_ENVIRONMENT_FIS);
_lightHandler->setUsePreConvolvedEnvLighting(_genContext.getOptions().hwSpecularEnvironmentMethod != mx::SPECULAR_ENVIRONMENT_FIS);
importanceSampleBox->set_callback([this](bool enable)
{
_genContext.getOptions().hwSpecularEnvironmentMethod = enable ? mx::SPECULAR_ENVIRONMENT_FIS : mx::SPECULAR_ENVIRONMENT_PREFILTER;
#ifndef MATERIALXVIEW_METAL_BACKEND
_genContextEssl.getOptions().hwSpecularEnvironmentMethod = _genContext.getOptions().hwSpecularEnvironmentMethod;
#endif
_lightHandler->setUsePreConvolvedEnvLighting(!enable);
reloadShaders();
});

Expand Down

0 comments on commit fb08a15

Please sign in to comment.