Commit b1c9726e authored by Ken Martin's avatar Ken Martin

convert depth peeling to use more texture objects

Change-Id: Ib626df6691d1871851edf853f1463bd954233201
parent 672a797b
......@@ -452,9 +452,6 @@ void vtkOpenGLPolyDataMapper::SetMapperShaderParameters(vtkgl::CellBO &cellBO,
cellBO.attributeUpdateTime.Modified();
}
vtkOpenGLRenderWindow *renWin = vtkOpenGLRenderWindow::SafeDownCast(ren->GetRenderWindow());
if (layout.TCoordComponents)
{
vtkTexture *texture = actor->GetTexture();
......
......@@ -39,6 +39,8 @@ PURPOSE. See the above copyright notice for more information.
#include "vtkRenderPass.h"
#include "vtkRenderState.h"
#include "vtkTextureObject.h"
#include "vtkOpenGLError.h"
#include <math.h>
......@@ -196,18 +198,41 @@ vtkOpenGLTexture *vtkOpenGLRendererCreateDepthPeelingTexture(
return result;
}
vtkTextureObject *vtkOpenGLRendererCreateDepthPeelingTextureObject(
vtkOpenGLRenderWindow *context, int width, int height, int numComponents, bool isDepth)
{
vtkTextureObject *result = vtkTextureObject::New();
result->SetContext(context);
if (isDepth == true)
{
result->AllocateDepth(width, height, vtkTextureObject::Float32);
}
else
{
result->Allocate2D(width, height, numComponents, VTK_UNSIGNED_CHAR);
}
result->SetMinificationFilter(vtkTextureObject::Nearest);
result->SetMagnificationFilter(vtkTextureObject::Nearest);
result->SetWrapS(vtkTextureObject::ClampToEdge);
result->SetWrapT(vtkTextureObject::ClampToEdge);
result->SetWrapR(vtkTextureObject::ClampToEdge);
return result;
}
// get the texture units for depth peeling
int vtkOpenGLRenderer::GetOpaqueRGBATextureUnit()
{
return vtkOpenGLRenderWindow::SafeDownCast(this->RenderWindow)->GetTextureUnitForTexture(this->OpaqueRGBATexture->GetTextureObject());
return this->OpaqueRGBATexture->GetTextureUnit();
}
int vtkOpenGLRenderer::GetOpaqueZTextureUnit()
{
return vtkOpenGLRenderWindow::SafeDownCast(this->RenderWindow)->GetTextureUnitForTexture(this->OpaqueZTexture->GetTextureObject());
return this->OpaqueZTexture->GetTextureUnit();
}
int vtkOpenGLRenderer::GetTranslucentRGBATextureUnit()
{
return vtkOpenGLRenderWindow::SafeDownCast(this->RenderWindow)->GetTextureUnitForTexture(this->TranslucentRGBATexture->GetTextureObject());
return this->TranslucentRGBATexture->GetTextureUnit();
}
int vtkOpenGLRenderer::GetTranslucentZTextureUnit()
{
......@@ -215,7 +240,7 @@ int vtkOpenGLRenderer::GetTranslucentZTextureUnit()
}
int vtkOpenGLRenderer::GetCurrentRGBATextureUnit()
{
return vtkOpenGLRenderWindow::SafeDownCast(this->RenderWindow)->GetTextureUnitForTexture(this->CurrentRGBATexture->GetTextureObject());
return this->CurrentRGBATexture->GetTextureUnit();
}
// ----------------------------------------------------------------------------
......@@ -228,10 +253,10 @@ void vtkOpenGLRenderer::DeviceRenderTranslucentPolygonalGeometry()
{
vtkOpenGLClearErrorMacro();
vtkOpenGLRenderWindow *context
= vtkOpenGLRenderWindow::SafeDownCast(this->RenderWindow);
if(this->UseDepthPeeling)
{
vtkOpenGLRenderWindow *context
= vtkOpenGLRenderWindow::SafeDownCast(this->RenderWindow);
if (!context)
{
vtkErrorMacro("OpenGL render window is required.")
......@@ -257,20 +282,9 @@ void vtkOpenGLRenderer::DeviceRenderTranslucentPolygonalGeometry()
this->GetTiledSizeAndOrigin(&this->ViewportWidth, &this->ViewportHeight,
&this->ViewportX, &this->ViewportY);
// create textures actors we need if not done already
if (this->OpaqueZTexture == NULL)
// create textures we need if not done already
if (this->DepthPeelingActor == NULL)
{
this->OpaqueZTexture = vtkOpenGLRendererCreateDepthPeelingTexture(
this->ViewportWidth, this->ViewportHeight, 1, true);
this->TranslucentZTexture = vtkOpenGLRendererCreateDepthPeelingTexture(
this->ViewportWidth, this->ViewportHeight, 1, true);
this->OpaqueRGBATexture = vtkOpenGLRendererCreateDepthPeelingTexture(
this->ViewportWidth, this->ViewportHeight, 4, false);
this->TranslucentRGBATexture = vtkOpenGLRendererCreateDepthPeelingTexture(
this->ViewportWidth, this->ViewportHeight, 4, false);
this->CurrentRGBATexture = vtkOpenGLRendererCreateDepthPeelingTexture(
this->ViewportWidth, this->ViewportHeight, 4, false);
this->DepthPeelingActor = vtkTexturedActor2D::New();
vtkNew<vtkPolyDataMapper2D> mapper;
vtkNew<vtkPolyData> polydata;
......@@ -302,31 +316,26 @@ void vtkOpenGLRenderer::DeviceRenderTranslucentPolygonalGeometry()
}
// has the size changed?
int *opaqueZTextureDims = this->OpaqueZTexture->GetImageDataInput(0)->GetDimensions();
if (opaqueZTextureDims[0] != this->ViewportWidth ||
opaqueZTextureDims[1] != this->ViewportHeight)
if (this->OpaqueRGBATexture && (
this->OpaqueRGBATexture->GetWidth() != this->ViewportWidth ||
this->OpaqueRGBATexture->GetHeight() != this->ViewportHeight))
{
this->OpaqueZTexture->GetImageDataInput(0)->SetExtent(
0,this->ViewportWidth-1, 0, this->ViewportHeight-1, 0,0);
this->OpaqueZTexture->GetImageDataInput(0)->AllocateScalars(VTK_FLOAT,1);
this->OpaqueZTexture->GetImageDataInput(0)->Modified();
this->TranslucentZTexture->GetImageDataInput(0)->SetExtent(
0,this->ViewportWidth-1, 0, this->ViewportHeight-1, 0,0);
this->TranslucentZTexture->GetImageDataInput(0)->AllocateScalars(VTK_FLOAT,1);
this->TranslucentZTexture->GetImageDataInput(0)->Modified();
this->OpaqueRGBATexture->GetImageDataInput(0)->SetExtent(
0,this->ViewportWidth-1, 0, this->ViewportHeight-1, 0,0);
this->OpaqueRGBATexture->GetImageDataInput(0)->AllocateScalars(VTK_UNSIGNED_CHAR,4);
this->OpaqueRGBATexture->GetImageDataInput(0)->Modified();
this->TranslucentRGBATexture->GetImageDataInput(0)->SetExtent(
0,this->ViewportWidth-1, 0, this->ViewportHeight-1, 0,0);
this->TranslucentRGBATexture->GetImageDataInput(0)->AllocateScalars(VTK_UNSIGNED_CHAR,4);
this->TranslucentRGBATexture->GetImageDataInput(0)->Modified();
this->CurrentRGBATexture->GetImageDataInput(0)->SetExtent(
0,this->ViewportWidth-1, 0, this->ViewportHeight-1, 0,0);
this->CurrentRGBATexture->GetImageDataInput(0)->AllocateScalars(VTK_UNSIGNED_CHAR,4);
this->CurrentRGBATexture->GetImageDataInput(0)->Modified();
this->OpaqueZTexture->UnRegister(this);
this->OpaqueZTexture = 0;
this->OpaqueRGBATexture->UnRegister(this);
this->OpaqueRGBATexture = 0;
this->TranslucentRGBATexture->UnRegister(this);
this->TranslucentRGBATexture = 0;
this->CurrentRGBATexture->UnRegister(this);
this->CurrentRGBATexture = 0;
vtkPolyData *polydata = vtkPolyDataMapper2D::SafeDownCast(this->DepthPeelingActor->GetMapper())->GetInput();
polydata->GetPoints()->SetPoint(1, this->ViewportWidth-1, 0, 0);
......@@ -335,6 +344,24 @@ void vtkOpenGLRenderer::DeviceRenderTranslucentPolygonalGeometry()
polydata->GetPoints()->Modified();
}
// create textures we need if not done already
if (this->OpaqueZTexture == NULL)
{
if (this->TranslucentZTexture == NULL)
{
this->TranslucentZTexture = vtkOpenGLRendererCreateDepthPeelingTexture(
this->ViewportWidth, this->ViewportHeight, 1, true);
}
this->OpaqueZTexture = vtkOpenGLRendererCreateDepthPeelingTextureObject(
context, this->ViewportWidth, this->ViewportHeight, 1, true);
this->OpaqueRGBATexture = vtkOpenGLRendererCreateDepthPeelingTextureObject(
context, this->ViewportWidth, this->ViewportHeight, 4, false);
this->TranslucentRGBATexture = vtkOpenGLRendererCreateDepthPeelingTextureObject(
context, this->ViewportWidth, this->ViewportHeight, 4, false);
this->CurrentRGBATexture = vtkOpenGLRendererCreateDepthPeelingTextureObject(
context, this->ViewportWidth, this->ViewportHeight, 4, false);
}
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
// glClearColor(0.0,0.0,0.0,0.0); // always clear to black
// glClearDepth(static_cast<GLclampf>(1.0));
......@@ -343,13 +370,14 @@ void vtkOpenGLRenderer::DeviceRenderTranslucentPolygonalGeometry()
#endif
glDisable(GL_BLEND);
// Get opaqueRGBA
this->OpaqueRGBATexture->Load(this);
this->OpaqueRGBATexture->CopyTexImage(this->ViewportX, this->ViewportY, this->ViewportWidth, this->ViewportHeight);
// Get opaqueZ
this->OpaqueZTexture->Load(this);
this->OpaqueZTexture->CopyTexImage(this->ViewportX, this->ViewportY, this->ViewportWidth, this->ViewportHeight);
// Get opaqueRGBA and opaqueZ
this->OpaqueRGBATexture->CopyFromFrameBuffer(this->ViewportX, this->ViewportY,
this->ViewportX, this->ViewportY,
this->ViewportWidth, this->ViewportHeight);
this->OpaqueRGBATexture->Deactivate(); // deactivate & unbind to save texture resources
this->OpaqueZTexture->CopyFromFrameBuffer(this->ViewportX, this->ViewportY,
this->ViewportX, this->ViewportY,
this->ViewportWidth, this->ViewportHeight);
// Initialize TranslucentZ to 0.0
vtkImageData *id = this->TranslucentZTexture->GetImageDataInput(0);
......@@ -361,12 +389,9 @@ void vtkOpenGLRenderer::DeviceRenderTranslucentPolygonalGeometry()
id->Modified();
this->TranslucentZTexture->Load(this);
// Initialize TranslucentRGBA and Current RGBA
this->TranslucentRGBATexture->Load(this);
this->CurrentRGBATexture->Load(this);
// Do render loop until complete
unsigned int threshold=static_cast<unsigned int>(this->ViewportWidth*this->ViewportHeight*OcclusionRatio);
unsigned int threshold=
static_cast<unsigned int>(this->ViewportWidth*this->ViewportHeight*OcclusionRatio);
#if GL_ES_VERSION_2_0 != 1
GLuint queryId;
......@@ -381,8 +406,7 @@ void vtkOpenGLRenderer::DeviceRenderTranslucentPolygonalGeometry()
{
// clear the zbuffer and color buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
this->OpaqueZTexture->Load(this);
this->TranslucentZTexture->Load(this);
this->OpaqueZTexture->Activate();
// render the translucent geometry
this->LastRenderingUsedDepthPeeling=1;
......@@ -408,36 +432,39 @@ void vtkOpenGLRenderer::DeviceRenderTranslucentPolygonalGeometry()
{
done = true;
}
//cerr << "Pass " << peelCount << " pixels Drawn " << nbPixels << "\n";
//cerr << "Pass " << peelCount << " pixels Drawn " << nbPixels << "\n";
// blend the last two peels together
if (peelCount > 1)
{
this->CurrentRGBATexture->CopyTexImage(this->ViewportX, this->ViewportY, this->ViewportWidth, this->ViewportHeight);
this->TranslucentRGBATexture->Load(this);
this->CurrentRGBATexture->Load(this);
this->CurrentRGBATexture->CopyFromFrameBuffer(this->ViewportX, this->ViewportY,
this->ViewportX, this->ViewportY,
this->ViewportWidth, this->ViewportHeight);
this->LastRenderingUsedDepthPeeling = 2;
// draw a full screen polygon that takes the TranslucentRGBA texture and blends it with the current frame buffer
// take the TranslucentRGBA texture and blend it with the current frame buffer
this->DepthPeelingActor->RenderOverlay(this);
}
// update translucent RGBA
this->TranslucentRGBATexture->CopyTexImage(this->ViewportX, this->ViewportY, this->ViewportWidth, this->ViewportHeight);
this->TranslucentRGBATexture->CopyFromFrameBuffer(this->ViewportX, this->ViewportY,
this->ViewportX, this->ViewportY,
this->ViewportWidth, this->ViewportHeight);
}
// unload the textures we are done with
this->CurrentRGBATexture->Deactivate();
this->OpaqueZTexture->Deactivate();
this->TranslucentZTexture->PostRender(this);
// blend in OpaqueRGBA
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
this->LastRenderingUsedDepthPeeling = 3;
this->OpaqueRGBATexture->Load(this);
this->TranslucentRGBATexture->Load(this);
this->OpaqueRGBATexture->Activate();
this->DepthPeelingActor->RenderOverlay(this);
// unload the textures
this->CurrentRGBATexture->PostRender(this);
this->TranslucentRGBATexture->PostRender(this);
this->OpaqueRGBATexture->PostRender(this);
this->TranslucentZTexture->PostRender(this);
this->OpaqueZTexture->PostRender(this);
// unload the last two textures
this->TranslucentRGBATexture->Deactivate();
this->OpaqueRGBATexture->Deactivate();
// done
this->LastRenderingUsedDepthPeeling=1;
......
......@@ -25,6 +25,7 @@
class vtkRenderPass;
class vtkOpenGLTexture;
class vtkTextureObject;
class vtkTexturedActor2D;
class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLRenderer : public vtkRenderer
......@@ -126,11 +127,11 @@ protected:
int DepthPeelingIsSupportedChecked;
vtkTexturedActor2D *DepthPeelingActor;
vtkOpenGLTexture *OpaqueZTexture;
vtkOpenGLTexture *OpaqueRGBATexture;
vtkOpenGLTexture *TranslucentRGBATexture;
vtkTextureObject *OpaqueZTexture;
vtkTextureObject *OpaqueRGBATexture;
vtkTextureObject *TranslucentRGBATexture;
vtkOpenGLTexture *TranslucentZTexture;
vtkOpenGLTexture *CurrentRGBATexture;
vtkTextureObject *CurrentRGBATexture;
// Description:
......
......@@ -314,6 +314,9 @@ vtkOpenGLRenderWindow* vtkTextureObject::GetContext()
//----------------------------------------------------------------------------
void vtkTextureObject::DestroyTexture()
{
// deactivate it first
this->Deactivate();
// because we don't hold a reference to the render
// context we don't have any control on when it is
// destroyed. In fact it may be destroyed before
......@@ -392,6 +395,7 @@ void vtkTextureObject::Deactivate()
{
if (this->Context)
{
this->Context->ActivateTexture(this);
this->UnBind();
this->Context->DeactivateTexture(this);
}
......@@ -406,7 +410,7 @@ void vtkTextureObject::Bind()
glBindTexture(this->Target, this->Handle);
vtkOpenGLCheckErrorMacro("failed at glBindTexture");
if (this->AutoParameters && (this->GetMTime()>this->SendParametersTime))
if (this->AutoParameters && (this->GetMTime() > this->SendParametersTime))
{
this->SendParameters();
}
......@@ -1764,7 +1768,6 @@ void vtkTextureObject::CopyFromFrameBuffer(int srcXmin,
this->Activate();
glCopyTexImage2D(this->Target,0,this->Format,srcXmin,srcYmin,width,height,0);
vtkOpenGLCheckErrorMacro("failed at glCopyTexImage2D");
this->Deactivate();
#else
......
......@@ -138,6 +138,9 @@ public:
// Description:
// Activate and Bind the texture
void Activate();
// Description:
// Deactivate and UnBind the texture
void Deactivate();
......@@ -208,8 +211,6 @@ public:
// vtkPixelBufferObject once it done with it.
vtkPixelBufferObject* Download();
#endif
// Description:
// Create a 2D depth texture using a PBO.
// \pre: valid_internalFormat: internalFormat>=0 && internalFormat<NumberOfDepthFormats
......@@ -218,6 +219,8 @@ public:
int internalFormat,
vtkPixelBufferObject *pbo);
#endif
// Description:
// Create a 2D depth texture but does not initialize its values.
bool AllocateDepth(unsigned int width,unsigned int height,
......@@ -244,8 +247,6 @@ public:
// Description:
// Create texture without uploading any data.
// To create a DEPTH_COMPONENT texture, vtktype must be set to VTK_VOID and
// numComps must be 1.
bool Create2D(unsigned int width, unsigned int height, int numComps,
int vtktype,
bool shaderSupportsTextureInt);
......
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