📄 fbobject.c
字号:
/*
* Mesa 3-D graphics library
* Version: 6.3
*
* Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* Authors:
* Brian Paul
*/
#include "context.h"
#include "fbobject.h"
#include "framebuffer.h"
#include "hash.h"
#include "renderbuffer.h"
#include "teximage.h"
#include "texstore.h"
/**
* Notes:
*
* None of the GL_EXT_framebuffer_object functions are compiled into
* display lists.
*/
/*
* When glGenRender/FramebuffersEXT() is called we insert pointers to
* these placeholder objects into the hash table.
* Later, when the object ID is first bound, we replace the placeholder
* with the real frame/renderbuffer.
*/
static struct gl_framebuffer DummyFramebuffer;
static struct gl_renderbuffer DummyRenderbuffer;
#define IS_CUBE_FACE(TARGET) \
((TARGET) >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && \
(TARGET) <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
/**
* Helper routine for getting a gl_renderbuffer.
*/
static struct gl_renderbuffer *
lookup_renderbuffer(GLcontext *ctx, GLuint id)
{
struct gl_renderbuffer *rb;
if (id == 0)
return NULL;
rb = (struct gl_renderbuffer *)
_mesa_HashLookup(ctx->Shared->RenderBuffers, id);
return rb;
}
/**
* Helper routine for getting a gl_framebuffer.
*/
static struct gl_framebuffer *
lookup_framebuffer(GLcontext *ctx, GLuint id)
{
struct gl_framebuffer *fb;
if (id == 0)
return NULL;
fb = (struct gl_framebuffer *)
_mesa_HashLookup(ctx->Shared->FrameBuffers, id);
return fb;
}
/**
* Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
* gl_renderbuffer_attachment object.
*/
static struct gl_renderbuffer_attachment *
get_attachment(GLcontext *ctx, struct gl_framebuffer *fb, GLenum attachment)
{
GLuint i;
switch (attachment) {
case GL_COLOR_ATTACHMENT0_EXT:
case GL_COLOR_ATTACHMENT1_EXT:
case GL_COLOR_ATTACHMENT2_EXT:
case GL_COLOR_ATTACHMENT3_EXT:
case GL_COLOR_ATTACHMENT4_EXT:
case GL_COLOR_ATTACHMENT5_EXT:
case GL_COLOR_ATTACHMENT6_EXT:
case GL_COLOR_ATTACHMENT7_EXT:
case GL_COLOR_ATTACHMENT8_EXT:
case GL_COLOR_ATTACHMENT9_EXT:
case GL_COLOR_ATTACHMENT10_EXT:
case GL_COLOR_ATTACHMENT11_EXT:
case GL_COLOR_ATTACHMENT12_EXT:
case GL_COLOR_ATTACHMENT13_EXT:
case GL_COLOR_ATTACHMENT14_EXT:
case GL_COLOR_ATTACHMENT15_EXT:
i = attachment - GL_COLOR_ATTACHMENT0_EXT;
if (i >= ctx->Const.MaxColorAttachments) {
return NULL;
}
return &fb->Attachment[BUFFER_COLOR0 + i];
case GL_DEPTH_ATTACHMENT_EXT:
return &fb->Attachment[BUFFER_DEPTH];
case GL_STENCIL_ATTACHMENT_EXT:
return &fb->Attachment[BUFFER_STENCIL];
default:
return NULL;
}
}
/**
* Remove any texture or renderbuffer attached to the given attachment
* point. Update reference counts, etc.
*/
void
_mesa_remove_attachment(GLcontext *ctx, struct gl_renderbuffer_attachment *att)
{
if (att->Type == GL_TEXTURE) {
ASSERT(att->Texture);
if (att->Renderbuffer) {
/* delete/remove the 'wrapper' renderbuffer */
/* XXX do we really want to do this??? */
att->Renderbuffer->Delete(att->Renderbuffer);
att->Renderbuffer = NULL;
}
att->Texture->RefCount--;
if (att->Texture->RefCount == 0) {
ctx->Driver.DeleteTexture(ctx, att->Texture);
}
att->Texture = NULL;
}
else if (att->Type == GL_RENDERBUFFER_EXT) {
ASSERT(att->Renderbuffer);
ASSERT(!att->Texture);
att->Renderbuffer->RefCount--;
if (att->Renderbuffer->RefCount == 0) {
att->Renderbuffer->Delete(att->Renderbuffer);
}
att->Renderbuffer = NULL;
}
att->Type = GL_NONE;
att->Complete = GL_TRUE;
}
/**
* Bind a texture object to an attachment point.
* The previous binding, if any, will be removed first.
*/
void
_mesa_set_texture_attachment(GLcontext *ctx,
struct gl_renderbuffer_attachment *att,
struct gl_texture_object *texObj,
GLenum texTarget, GLuint level, GLuint zoffset)
{
_mesa_remove_attachment(ctx, att);
att->Type = GL_TEXTURE;
att->Texture = texObj;
att->TextureLevel = level;
if (IS_CUBE_FACE(texTarget)) {
att->CubeMapFace = texTarget - GL_TEXTURE_CUBE_MAP_POSITIVE_X;
}
else {
att->CubeMapFace = 0;
}
att->Zoffset = zoffset;
att->Complete = GL_FALSE;
texObj->RefCount++;
/* XXX when we attach to a texture, we should probably set the
* att->Renderbuffer pointer to a "wrapper renderbuffer" which
* makes the texture image look like renderbuffer.
*/
}
/**
* Bind a renderbuffer to an attachment point.
* The previous binding, if any, will be removed first.
*/
void
_mesa_set_renderbuffer_attachment(GLcontext *ctx,
struct gl_renderbuffer_attachment *att,
struct gl_renderbuffer *rb)
{
_mesa_remove_attachment(ctx, att);
att->Type = GL_RENDERBUFFER_EXT;
att->Renderbuffer = rb;
att->Texture = NULL; /* just to be safe */
att->Complete = GL_FALSE;
rb->RefCount++;
}
/**
* Fallback for ctx->Driver.FramebufferRenderbuffer()
* Sets a framebuffer attachment to a particular renderbuffer.
* The framebuffer in question is ctx->DrawBuffer.
* \sa _mesa_renderbuffer_texture
*/
void
_mesa_framebuffer_renderbuffer(GLcontext *ctx,
struct gl_renderbuffer_attachment *att,
struct gl_renderbuffer *rb)
{
if (rb) {
_mesa_set_renderbuffer_attachment(ctx, att, rb);
}
else {
_mesa_remove_attachment(ctx, att);
}
}
/**
* Test if an attachment point is complete and update its Complete field.
* \param format if GL_COLOR, this is a color attachment point,
* if GL_DEPTH, this is a depth component attachment point,
* if GL_STENCIL, this is a stencil component attachment point.
*/
static void
test_attachment_completeness(const GLcontext *ctx, GLenum format,
struct gl_renderbuffer_attachment *att)
{
assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
/* assume complete */
att->Complete = GL_TRUE;
/* Look for reasons why the attachment might be incomplete */
if (att->Type == GL_TEXTURE) {
const struct gl_texture_object *texObj = att->Texture;
struct gl_texture_image *texImage;
if (!texObj) {
att->Complete = GL_FALSE;
return;
}
texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
if (!texImage) {
att->Complete = GL_FALSE;
return;
}
if (texImage->Width < 1 || texImage->Height < 1) {
att->Complete = GL_FALSE;
return;
}
if (texObj->Target == GL_TEXTURE_3D && att->Zoffset >= texImage->Depth) {
att->Complete = GL_FALSE;
return;
}
if (format == GL_COLOR) {
if (texImage->TexFormat->BaseFormat != GL_RGB &&
texImage->TexFormat->BaseFormat != GL_RGBA) {
att->Complete = GL_FALSE;
return;
}
}
else if (format == GL_DEPTH) {
if (texImage->TexFormat->BaseFormat != GL_DEPTH_COMPONENT) {
att->Complete = GL_FALSE;
return;
}
}
else {
/* no such thing as stencil textures */
att->Complete = GL_FALSE;
return;
}
}
else if (att->Type == GL_RENDERBUFFER_EXT) {
if (att->Renderbuffer->Width < 1 || att->Renderbuffer->Height < 1) {
att->Complete = GL_FALSE;
return;
}
if (format == GL_COLOR) {
if (att->Renderbuffer->_BaseFormat != GL_RGB &&
att->Renderbuffer->_BaseFormat != GL_RGBA) {
att->Complete = GL_FALSE;
return;
}
}
else if (format == GL_DEPTH) {
if (att->Renderbuffer->_BaseFormat != GL_DEPTH_COMPONENT) {
att->Complete = GL_FALSE;
return;
}
}
else {
assert(format == GL_STENCIL);
if (att->Renderbuffer->_BaseFormat != GL_STENCIL_INDEX) {
att->Complete = GL_FALSE;
return;
}
}
}
else {
ASSERT(att->Type == GL_NONE);
/* complete */
return;
}
}
/**
* Test if the given framebuffer object is complete and update its
* Status field with the results.
* Also update the framebuffer's Width and Height fields if the
* framebuffer is complete.
*/
void
_mesa_test_framebuffer_completeness(GLcontext *ctx, struct gl_framebuffer *fb)
{
GLuint numImages, width = 0, height = 0;
GLenum intFormat = GL_NONE;
GLuint w = 0, h = 0;
GLint i;
assert(fb->Name != 0);
numImages = 0;
fb->Width = 0;
fb->Height = 0;
/* Start at -2 to more easily loop over all attachment points */
for (i = -2; i < (GLint) ctx->Const.MaxColorAttachments; i++) {
struct gl_renderbuffer_attachment *att;
GLenum f;
if (i == -2) {
att = &fb->Attachment[BUFFER_DEPTH];
test_attachment_completeness(ctx, GL_DEPTH, att);
if (!att->Complete) {
fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
return;
}
}
else if (i == -1) {
att = &fb->Attachment[BUFFER_STENCIL];
test_attachment_completeness(ctx, GL_STENCIL, att);
if (!att->Complete) {
fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
return;
}
}
else {
att = &fb->Attachment[BUFFER_COLOR0 + i];
test_attachment_completeness(ctx, GL_COLOR, att);
if (!att->Complete) {
fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
return;
}
}
if (att->Type == GL_TEXTURE) {
w = att->Texture->Image[att->CubeMapFace][att->TextureLevel]->Width;
h = att->Texture->Image[att->CubeMapFace][att->TextureLevel]->Height;
f = att->Texture->Image[att->CubeMapFace][att->TextureLevel]->Format;
numImages++;
if (f != GL_RGB && f != GL_RGBA && f != GL_DEPTH_COMPONENT) {
fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
return;
}
}
else if (att->Type == GL_RENDERBUFFER_EXT) {
w = att->Renderbuffer->Width;
h = att->Renderbuffer->Height;
f = att->Renderbuffer->InternalFormat;
numImages++;
}
else {
assert(att->Type == GL_NONE);
continue;
}
if (numImages == 1) {
/* set required width, height and format */
width = w;
height = h;
if (i >= 0)
intFormat = f;
}
else {
/* check that width, height, format are same */
if (w != width || h != height) {
fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
return;
}
if (intFormat != GL_NONE && f != intFormat) {
fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
return;
}
}
}
/* Check that all DrawBuffers are present */
for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
if (fb->ColorDrawBuffer[i] != GL_NONE) {
const struct gl_renderbuffer_attachment *att
= get_attachment(ctx, fb, fb->ColorDrawBuffer[i]);
assert(att);
if (att->Type == GL_NONE) {
fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
return;
}
}
}
/* Check that the ReadBuffer is present */
if (fb->ColorReadBuffer != GL_NONE) {
const struct gl_renderbuffer_attachment *att
= get_attachment(ctx, fb, fb->ColorReadBuffer);
assert(att);
if (att->Type == GL_NONE) {
fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
return;
}
}
/* Check if any renderbuffer is attached more than once */
for (i = 0; i < BUFFER_COUNT - 1; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -