Skip to content

Commit

Permalink
Refactor|libgui|GLTarget: Allow querying the attached depth texture
Browse files Browse the repository at this point in the history
The view may be rendered to any target, so there must be a way to
access the currently used depth texture.
  • Loading branch information
skyjake committed Nov 29, 2013
1 parent 9ae5d68 commit 6b523f7
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 22 deletions.
13 changes: 6 additions & 7 deletions doomsday/client/src/render/fx/lensflares.cpp
Expand Up @@ -477,22 +477,21 @@ void fx::LensFlares::beginFrame()
void LensFlares::draw()
{
viewdata_t const *viewData = R_ViewData(console());
/*
d->uEyePos = Vector3f(vOrigin[VX], vOrigin[VY], vOrigin[VZ]);
d->uEyeFront = Vector3f(viewData->frontVec);
*/

d->eyeFront = Vector3f(viewData->frontVec);

Rectanglef const rect = viewRect();
float const aspect = rect.height() / rect.width();
float const aspect = rect.height() / rect.width();

Canvas &canvas = ClientWindow::main().canvas();

d->uViewUnit = Vector2f(aspect, 1.f);
d->uPixelAsUv = Vector2f(1.f / canvas.width(), 1.f / canvas.height());
d->uMvpMatrix = GL_GetProjectionMatrix() * Rend_GetModelViewMatrix(console());
d->uDepthBuf = canvas.depthBufferTexture();

// Depth information is required for occlusion.
GLTexture *depthTex = GLState::top().target().attachedTexture(GLTarget::Depth);
/// @todo Handle the situation if depth information is not available in the target.
d->uDepthBuf = depthTex;

GLState::push()
.setCull(gl::None)
Expand Down
8 changes: 8 additions & 0 deletions doomsday/libgui/include/de/gui/gltarget.h
Expand Up @@ -175,6 +175,14 @@ class LIBGUI_PUBLIC GLTarget : public Asset
*/
void resize(Size const &size);

/**
* Returns the texture being used for a particular attachment in this target.
*
* @param attachment Which attachment.
* @return
*/
GLTexture *attachedTexture(Flags const &attachment) const;

/**
* Sets the subregion inside the render target where scissor and viewport
* will be scaled into. Scissor and viewport can still be defined as if the
Expand Down
82 changes: 67 additions & 15 deletions doomsday/libgui/src/gltarget.cpp
Expand Up @@ -32,18 +32,42 @@ static Vector2ui const nullSize;
DENG2_PIMPL(GLTarget),
DENG2_OBSERVES(Asset, Deletion)
{
enum RenderBufId {
enum AttachmentId {
ColorBuffer,
DepthBuffer,
StencilBuffer,
MAX_BUFFERS
MAX_ATTACHMENTS
};

static AttachmentId attachmentToId(GLenum atc)
{
switch(atc)
{
case GL_COLOR_ATTACHMENT0:
return ColorBuffer;

case GL_DEPTH_ATTACHMENT:
return DepthBuffer;

case GL_STENCIL_ATTACHMENT:
return StencilBuffer;

case GL_DEPTH_STENCIL_ATTACHMENT:
return DepthBuffer;

default:
DENG2_ASSERT(false);
break;
}
return ColorBuffer; // should not be reached
}

GLuint fbo;
GLuint renderBufs[MAX_BUFFERS];
GLuint renderBufs[MAX_ATTACHMENTS];
GLTexture *bufTextures[MAX_ATTACHMENTS];
Flags flags;
Flags textureAttachment; ///< Where to attach @a texture.
GLTexture *texture;
GLTexture *texture;
Vector2ui size;
Vector4f clearColor;
Rectangleui activeRect; ///< Initially null.
Expand All @@ -54,6 +78,7 @@ DENG2_OBSERVES(Asset, Deletion)
texture(0)
{
zap(renderBufs);
zap(bufTextures);
}

Instance(Public *i, Flags const &texAttachment, GLTexture &colorTexture, Flags const &otherAtm)
Expand All @@ -62,6 +87,7 @@ DENG2_OBSERVES(Asset, Deletion)
texture(&colorTexture), size(colorTexture.size())
{
zap(renderBufs);
zap(bufTextures);
}

Instance(Public *i, Vector2ui const &targetSize, Flags const &fboFlags)
Expand All @@ -70,6 +96,7 @@ DENG2_OBSERVES(Asset, Deletion)
texture(0), size(targetSize)
{
zap(renderBufs);
zap(bufTextures);
}

~Instance()
Expand All @@ -82,7 +109,24 @@ DENG2_OBSERVES(Asset, Deletion)
return !texture && size == nullSize;
}

void attachRenderbuffer(RenderBufId id, GLenum type, GLenum attachment)
GLTexture *bufferTexture(Flags const &flags) const
{
if(flags == Color)
{
return bufTextures[ColorBuffer];
}
if(flags == DepthStencil || flags == Depth)
{
return bufTextures[DepthBuffer];
}
if(flags == Stencil)
{
return bufTextures[StencilBuffer];
}
return 0;
}

void attachRenderbuffer(AttachmentId id, GLenum type, GLenum attachment)
{
DENG2_ASSERT(size != Vector2ui(0, 0));

Expand All @@ -104,13 +148,17 @@ DENG2_OBSERVES(Asset, Deletion)
LOG_DEBUG("Creating FBO %i") << fbo;
}

void attachTexture(GLuint texName, GLenum attachment, int level = 0)
void attachTexture(GLTexture &tex, GLenum attachment, int level = 0)
{
LOG_DEBUG("glTex %i (level %i) => FBO attachment 0x%x") << texName << level << attachment;
DENG2_ASSERT(tex.isReady());

glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, texName, level);
LOG_DEBUG("glTex %i (level %i) => FBO attachment %i (0x%x)")
<< tex.glName() << level << attachmentToId(attachment) << attachment;

glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, tex.glName(), level);
LIBGUI_ASSERT_GL_OK();

bufTextures[attachmentToId(attachment)] = &tex;
}

void alloc()
Expand All @@ -119,16 +167,13 @@ DENG2_OBSERVES(Asset, Deletion)

if(texture)
{
// Target renders to texture.
DENG2_ASSERT(texture->isReady());

// The texture's attachment point must be unambiguously defined.
DENG2_ASSERT(textureAttachment == Color ||
textureAttachment == Depth ||
textureAttachment == Stencil ||
textureAttachment == DepthStencil);

attachTexture(texture->glName(),
attachTexture(*texture,
textureAttachment == Color? GL_COLOR_ATTACHMENT0 :
textureAttachment == Depth? GL_DEPTH_ATTACHMENT :
textureAttachment == Stencil? GL_STENCIL_ATTACHMENT :
Expand Down Expand Up @@ -181,8 +226,9 @@ DENG2_OBSERVES(Asset, Deletion)

void releaseRenderBuffers()
{
glDeleteRenderbuffers(MAX_BUFFERS, renderBufs);
glDeleteRenderbuffers(MAX_ATTACHMENTS, renderBufs);
zap(renderBufs);
zap(bufTextures);
}

void release()
Expand All @@ -194,6 +240,7 @@ DENG2_OBSERVES(Asset, Deletion)
glDeleteFramebuffers(1, &fbo);
fbo = 0;
}
zap(bufTextures);
texture = 0;
size = Vector2ui(0, 0);
}
Expand Down Expand Up @@ -304,7 +351,7 @@ void GLTarget::configure(GLTexture *colorTex, GLTexture *depthStencilTex)
{
DENG2_ASSERT(colorTex->isReady());
DENG2_ASSERT(d->size == colorTex->size());
d->attachTexture(colorTex->glName(), GL_COLOR_ATTACHMENT0);
d->attachTexture(*colorTex, GL_COLOR_ATTACHMENT0);
}
else
{
Expand All @@ -316,7 +363,7 @@ void GLTarget::configure(GLTexture *colorTex, GLTexture *depthStencilTex)
{
DENG2_ASSERT(depthStencilTex->isReady());
DENG2_ASSERT(d->size == depthStencilTex->size());
d->attachTexture(depthStencilTex->glName(), GL_DEPTH_STENCIL_ATTACHMENT);
d->attachTexture(*depthStencilTex, GL_DEPTH_STENCIL_ATTACHMENT);
}
else
{
Expand Down Expand Up @@ -410,6 +457,11 @@ void GLTarget::resize(Size const &size)
GLState::top().target().glBind();
}

GLTexture *GLTarget::attachedTexture(Flags const &attachment) const
{
return d->bufferTexture(attachment);
}

GLuint GLTarget::glName() const
{
return d->fbo;
Expand Down

0 comments on commit 6b523f7

Please sign in to comment.