Commit 28839f4a authored by David C. Lonie's avatar David C. Lonie
Browse files

Fix issue with coincident geometry + MSAA. See #16899.

101f9eeb changed the polydata
fragment shaders to always write out gl_FragDepth explicitly to help
with the depth peeling abstractions. However, this causes issues when
multisampling, as the multisampled depths are not handled correctly when
gl_FragDepth is not the default value. [1] has some more detailed info.

After this patch, gl_FragDepth is only set when it is needed.

[1] http://www.gamedev.net/topic/544920-gl_fragdepth-and-anti-aliasing/#entry4514289
parent 6cf5847e
...@@ -564,6 +564,13 @@ bool vtkDepthPeelingPass::ReplaceShaderValues(std::string &, ...@@ -564,6 +564,13 @@ bool vtkDepthPeelingPass::ReplaceShaderValues(std::string &,
"uniform sampler2D translucentZTexture;\n" "uniform sampler2D translucentZTexture;\n"
); );
// Set gl_FragDepth if it isn't set already. It may have already been replaced
// by the mapper, in which case the substitution will fail and the previously
// set depth value will be used.
vtkShaderProgram::Substitute(
fragmentShader, "//VTK::Depth::Impl",
"gl_FragDepth = gl_FragCoord.z;");
// the .0000001 below is an epsilon. It turns out that // the .0000001 below is an epsilon. It turns out that
// graphics cards can render the same polygon two times // graphics cards can render the same polygon two times
// in a row with different z values. I suspect it has to // in a row with different z values. I suspect it has to
......
...@@ -115,6 +115,12 @@ bool vtkDualDepthPeelingPass::ReplaceShaderValues(std::string &, ...@@ -115,6 +115,12 @@ bool vtkDualDepthPeelingPass::ReplaceShaderValues(std::string &,
switch (this->CurrentStage) switch (this->CurrentStage)
{ {
case vtkDualDepthPeelingPass::InitializingDepth: case vtkDualDepthPeelingPass::InitializingDepth:
// Set gl_FragDepth if it isn't set already. It may have already been
// replaced by the mapper, in which case the substitution will fail and
// the previously set depth value will be used.
vtkShaderProgram::Substitute(
fragmentShader, "//VTK::Depth::Impl",
"gl_FragDepth = gl_FragCoord.z;");
vtkShaderProgram::Substitute( vtkShaderProgram::Substitute(
fragmentShader, "//VTK::DepthPeeling::Dec", fragmentShader, "//VTK::DepthPeeling::Dec",
"uniform sampler2D opaqueDepth;\n"); "uniform sampler2D opaqueDepth;\n");
...@@ -136,6 +142,12 @@ bool vtkDualDepthPeelingPass::ReplaceShaderValues(std::string &, ...@@ -136,6 +142,12 @@ bool vtkDualDepthPeelingPass::ReplaceShaderValues(std::string &,
break; break;
case vtkDualDepthPeelingPass::Peeling: case vtkDualDepthPeelingPass::Peeling:
// Set gl_FragDepth if it isn't set already. It may have already been
// replaced by the mapper, in which case the substitution will fail and
// the previously set depth value will be used.
vtkShaderProgram::Substitute(
fragmentShader, "//VTK::Depth::Impl",
"gl_FragDepth = gl_FragCoord.z;");
vtkShaderProgram::Substitute( vtkShaderProgram::Substitute(
fragmentShader, "//VTK::DepthPeeling::Dec", fragmentShader, "//VTK::DepthPeeling::Dec",
"uniform sampler2D lastFrontPeel;\n" "uniform sampler2D lastFrontPeel;\n"
...@@ -239,6 +251,12 @@ bool vtkDualDepthPeelingPass::ReplaceShaderValues(std::string &, ...@@ -239,6 +251,12 @@ bool vtkDualDepthPeelingPass::ReplaceShaderValues(std::string &,
break; break;
case vtkDualDepthPeelingPass::AlphaBlending: case vtkDualDepthPeelingPass::AlphaBlending:
// Set gl_FragDepth if it isn't set already. It may have already been
// replaced by the mapper, in which case the substitution will fail and
// the previously set depth value will be used.
vtkShaderProgram::Substitute(
fragmentShader, "//VTK::Depth::Impl",
"gl_FragDepth = gl_FragCoord.z;");
vtkShaderProgram::Substitute( vtkShaderProgram::Substitute(
fragmentShader, "//VTK::DepthPeeling::Dec", fragmentShader, "//VTK::DepthPeeling::Dec",
"uniform sampler2D lastDepthPeel;\n"); "uniform sampler2D lastDepthPeel;\n");
......
...@@ -1559,17 +1559,28 @@ void vtkOpenGLPolyDataMapper::ReplaceShaderDepth( ...@@ -1559,17 +1559,28 @@ void vtkOpenGLPolyDataMapper::ReplaceShaderDepth(
std::map<vtkShader::Type, vtkShader *> shaders, std::map<vtkShader::Type, vtkShader *> shaders,
vtkRenderer *, vtkActor *) vtkRenderer *, vtkActor *)
{ {
std::string FSSource = shaders[vtkShader::Fragment]->GetSource(); // If MSAA is enabled, don't write to gl_FragDepth unless we absolutely have
vtkShaderProgram::Substitute(FSSource,"//VTK::Depth::Impl", // to. See VTK issue 16899.
"gl_FragDepth = gl_FragCoord.z;"); #if GL_ES_VERSION_3_0 != 1
shaders[vtkShader::Fragment]->SetSource(FSSource); bool multisampling = glIsEnabled(GL_MULTISAMPLE) == GL_TRUE;
#else
bool multisample = false;
#endif
if (!multisampling)
{
std::string FSSource = shaders[vtkShader::Fragment]->GetSource();
vtkShaderProgram::Substitute(FSSource,
"//VTK::Depth::Impl",
"gl_FragDepth = gl_FragCoord.z;");
shaders[vtkShader::Fragment]->SetSource(FSSource);
}
} }
void vtkOpenGLPolyDataMapper::ReplaceShaderValues( void vtkOpenGLPolyDataMapper::ReplaceShaderValues(
std::map<vtkShader::Type, vtkShader *> shaders, std::map<vtkShader::Type, vtkShader *> shaders,
vtkRenderer *ren, vtkActor *actor) vtkRenderer *ren, vtkActor *actor)
{ {
this->ReplaceShaderRenderPass(shaders, ren, actor);
this->ReplaceShaderColor(shaders, ren, actor); this->ReplaceShaderColor(shaders, ren, actor);
this->ReplaceShaderNormal(shaders, ren, actor); this->ReplaceShaderNormal(shaders, ren, actor);
this->ReplaceShaderLight(shaders, ren, actor); this->ReplaceShaderLight(shaders, ren, actor);
...@@ -1580,6 +1591,7 @@ void vtkOpenGLPolyDataMapper::ReplaceShaderValues( ...@@ -1580,6 +1591,7 @@ void vtkOpenGLPolyDataMapper::ReplaceShaderValues(
this->ReplaceShaderPositionVC(shaders, ren, actor); this->ReplaceShaderPositionVC(shaders, ren, actor);
this->ReplaceShaderCoincidentOffset(shaders, ren, actor); this->ReplaceShaderCoincidentOffset(shaders, ren, actor);
this->ReplaceShaderDepth(shaders, ren, actor); this->ReplaceShaderDepth(shaders, ren, actor);
this->ReplaceShaderRenderPass(shaders, ren, actor);
//cout << "VS: " << shaders[vtkShader::Vertex]->GetSource() << endl; //cout << "VS: " << shaders[vtkShader::Vertex]->GetSource() << endl;
//cout << "GS: " << shaders[vtkShader::Geometry]->GetSource() << endl; //cout << "GS: " << shaders[vtkShader::Geometry]->GetSource() << endl;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment