X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fmesa%2Fmain%2Fclear.c;h=a1bb36efe241e0508b6fd09dd917f43501713082;hb=b9b88516f8d3efc902696f1092519e298ceb7cdb;hp=4a3c11165863645262ca3c14999c34d986336c9e;hpb=080c40ab32b2abd6d8381b4a0cc143d36a1652b2;p=mesa.git diff --git a/src/mesa/main/clear.c b/src/mesa/main/clear.c index 4a3c1116586..a1bb36efe24 100644 --- a/src/mesa/main/clear.c +++ b/src/mesa/main/clear.c @@ -1,6 +1,5 @@ /* * Mesa 3-D graphics library - * Version: 7.1 * * Copyright (C) 1999-2007 Brian Paul All Rights Reserved. * @@ -17,9 +16,10 @@ * 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. + * THE AUTHORS OR COPYRIGHT HOLDERS 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. */ @@ -33,31 +33,22 @@ #include "glheader.h" #include "clear.h" #include "context.h" -#include "colormac.h" #include "enums.h" +#include "fbobject.h" +#include "get.h" +#include "macros.h" +#include "mtypes.h" #include "state.h" -#if _HAVE_FULL_GL void GLAPIENTRY _mesa_ClearIndex( GLfloat c ) { GET_CURRENT_CONTEXT(ctx); - ASSERT_OUTSIDE_BEGIN_END(ctx); - if (ctx->Color.ClearIndex == (GLuint) c) - return; - - FLUSH_VERTICES(ctx, _NEW_COLOR); ctx->Color.ClearIndex = (GLuint) c; - - if (!ctx->Visual.rgbMode && ctx->Driver.ClearIndex) { - /* it's OK to call glClearIndex in RGBA mode but it should be a NOP */ - (*ctx->Driver.ClearIndex)( ctx, ctx->Color.ClearIndex ); - } } -#endif /** @@ -69,51 +60,90 @@ _mesa_ClearIndex( GLfloat c ) * \param alpha alpha component. * * \sa glClearColor(). - * - * Clamps the parameters and updates gl_colorbuffer_attrib::ClearColor. On a - * change, flushes the vertices and notifies the driver via the - * dd_function_table::ClearColor callback. */ void GLAPIENTRY _mesa_ClearColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha ) { - GLfloat tmp[4]; GET_CURRENT_CONTEXT(ctx); - ASSERT_OUTSIDE_BEGIN_END(ctx); - tmp[0] = CLAMP(red, 0.0F, 1.0F); - tmp[1] = CLAMP(green, 0.0F, 1.0F); - tmp[2] = CLAMP(blue, 0.0F, 1.0F); - tmp[3] = CLAMP(alpha, 0.0F, 1.0F); + ctx->Color.ClearColor.f[0] = red; + ctx->Color.ClearColor.f[1] = green; + ctx->Color.ClearColor.f[2] = blue; + ctx->Color.ClearColor.f[3] = alpha; +} - if (TEST_EQ_4V(tmp, ctx->Color.ClearColor)) - return; /* no change */ - FLUSH_VERTICES(ctx, _NEW_COLOR); - COPY_4V(ctx->Color.ClearColor, tmp); +/** + * GL_EXT_texture_integer + */ +void GLAPIENTRY +_mesa_ClearColorIiEXT(GLint r, GLint g, GLint b, GLint a) +{ + GET_CURRENT_CONTEXT(ctx); + + ctx->Color.ClearColor.i[0] = r; + ctx->Color.ClearColor.i[1] = g; + ctx->Color.ClearColor.i[2] = b; + ctx->Color.ClearColor.i[3] = a; +} - if (ctx->Visual.rgbMode && ctx->Driver.ClearColor) { - /* it's OK to call glClearColor in CI mode but it should be a NOP */ - (*ctx->Driver.ClearColor)(ctx, ctx->Color.ClearColor); + +/** + * GL_EXT_texture_integer + */ +void GLAPIENTRY +_mesa_ClearColorIuiEXT(GLuint r, GLuint g, GLuint b, GLuint a) +{ + GET_CURRENT_CONTEXT(ctx); + + ctx->Color.ClearColor.ui[0] = r; + ctx->Color.ClearColor.ui[1] = g; + ctx->Color.ClearColor.ui[2] = b; + ctx->Color.ClearColor.ui[3] = a; +} + + +/** + * Returns true if color writes are enabled for the given color attachment. + * + * Beyond checking ColorMask, this uses _mesa_format_has_color_component to + * ignore components that don't actually exist in the format (such as X in + * XRGB). + */ +static bool +color_buffer_writes_enabled(const struct gl_context *ctx, unsigned idx) +{ + struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[idx]; + GLuint c; + GLubyte colorMask = 0; + + if (rb) { + for (c = 0; c < 4; c++) { + if (_mesa_format_has_color_component(rb->Format, c)) + colorMask |= ctx->Color.ColorMask[idx][c]; + } } + + return colorMask != 0; } /** * Clear buffers. - * + * * \param mask bit-mask indicating the buffers to be cleared. * - * Flushes the vertices and verifies the parameter. If __GLcontextRec::NewState - * is set then calls _mesa_update_state() to update gl_frame_buffer::_Xmin, - * etc. If the rasterization mode is set to GL_RENDER then requests the driver - * to clear the buffers, via the dd_function_table::Clear callback. - */ + * Flushes the vertices and verifies the parameter. + * If __struct gl_contextRec::NewState is set then calls _mesa_update_state() + * to update gl_frame_buffer::_Xmin, etc. If the rasterization mode is set to + * GL_RENDER then requests the driver to clear the buffers, via the + * dd_function_table::Clear callback. + */ void GLAPIENTRY _mesa_Clear( GLbitfield mask ) { GET_CURRENT_CONTEXT(ctx); - ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); + FLUSH_VERTICES(ctx, 0); FLUSH_CURRENT(ctx, 0); @@ -129,6 +159,15 @@ _mesa_Clear( GLbitfield mask ) return; } + /* Accumulation buffers were removed in core contexts, and they never + * existed in OpenGL ES. + */ + if ((mask & GL_ACCUM_BUFFER_BIT) != 0 + && (ctx->API == API_OPENGL_CORE || _mesa_is_gles(ctx))) { + _mesa_error( ctx, GL_INVALID_VALUE, "glClear(GL_ACCUM_BUFFER_BIT)"); + return; + } + if (ctx->NewState) { _mesa_update_state( ctx ); /* update _Xmin, etc */ } @@ -139,9 +178,7 @@ _mesa_Clear( GLbitfield mask ) return; } - if (ctx->DrawBuffer->Width == 0 || ctx->DrawBuffer->Height == 0 || - ctx->DrawBuffer->_Xmin >= ctx->DrawBuffer->_Xmax || - ctx->DrawBuffer->_Ymin >= ctx->DrawBuffer->_Ymax) + if (ctx->RasterDiscard) return; if (ctx->RenderMode == GL_RENDER) { @@ -160,7 +197,11 @@ _mesa_Clear( GLbitfield mask ) if (mask & GL_COLOR_BUFFER_BIT) { GLuint i; for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) { - bufferMask |= (1 << ctx->DrawBuffer->_ColorDrawBufferIndexes[i]); + GLint buf = ctx->DrawBuffer->_ColorDrawBufferIndexes[i]; + + if (buf >= 0 && color_buffer_writes_enabled(ctx, i)) { + bufferMask |= 1 << buf; + } } } @@ -179,14 +220,14 @@ _mesa_Clear( GLbitfield mask ) bufferMask |= BUFFER_BIT_ACCUM; } - ASSERT(ctx->Driver.Clear); + assert(ctx->Driver.Clear); ctx->Driver.Clear(ctx, bufferMask); } } /** Returned by make_color_buffer_mask() for errors */ -#define INVALID_MASK ~0x0 +#define INVALID_MASK ~0x0U /** @@ -195,12 +236,30 @@ _mesa_Clear( GLbitfield mask ) * Return INVALID_MASK if the drawbuffer value is invalid. */ static GLbitfield -make_color_buffer_mask(GLcontext *ctx, GLint drawbuffer) +make_color_buffer_mask(struct gl_context *ctx, GLint drawbuffer) { const struct gl_renderbuffer_attachment *att = ctx->DrawBuffer->Attachment; GLbitfield mask = 0x0; - switch (drawbuffer) { + /* From the GL 4.0 specification: + * If buffer is COLOR, a particular draw buffer DRAW_BUFFERi is + * specified by passing i as the parameter drawbuffer, and value + * points to a four-element vector specifying the R, G, B, and A + * color to clear that draw buffer to. If the draw buffer is one + * of FRONT, BACK, LEFT, RIGHT, or FRONT_AND_BACK, identifying + * multiple buffers, each selected buffer is cleared to the same + * value. + * + * Note that "drawbuffer" and "draw buffer" have different meaning. + * "drawbuffer" specifies DRAW_BUFFERi, while "draw buffer" is what's + * assigned to DRAW_BUFFERi. It could be COLOR_ATTACHMENT0, FRONT, BACK, + * etc. + */ + if (drawbuffer < 0 || drawbuffer >= (GLint)ctx->Const.MaxDrawBuffers) { + return INVALID_MASK; + } + + switch (ctx->DrawBuffer->ColorDrawBuffer[drawbuffer]) { case GL_FRONT: if (att[BUFFER_FRONT_LEFT].Renderbuffer) mask |= BUFFER_BIT_FRONT_LEFT; @@ -208,6 +267,14 @@ make_color_buffer_mask(GLcontext *ctx, GLint drawbuffer) mask |= BUFFER_BIT_FRONT_RIGHT; break; case GL_BACK: + /* For GLES contexts with a single buffered configuration, we actually + * only have a front renderbuffer, so any clear calls to GL_BACK should + * affect that buffer. See draw_buffer_enum_to_bitmask for details. + */ + if (_mesa_is_gles(ctx)) + if (!ctx->DrawBuffer->Visual.doubleBufferMode) + if (att[BUFFER_FRONT_LEFT].Renderbuffer) + mask |= BUFFER_BIT_FRONT_LEFT; if (att[BUFFER_BACK_LEFT].Renderbuffer) mask |= BUFFER_BIT_BACK_LEFT; if (att[BUFFER_BACK_RIGHT].Renderbuffer) @@ -236,11 +303,12 @@ make_color_buffer_mask(GLcontext *ctx, GLint drawbuffer) mask |= BUFFER_BIT_BACK_RIGHT; break; default: - if (drawbuffer < 0 || drawbuffer >= ctx->Const.MaxDrawBuffers) { - mask = INVALID_MASK; - } - else if (att[BUFFER_COLOR0 + drawbuffer].Renderbuffer) { - mask |= (BUFFER_BIT_COLOR0 << drawbuffer); + { + GLint buf = ctx->DrawBuffer->_ColorDrawBufferIndexes[drawbuffer]; + + if (buf >= 0 && att[buf].Renderbuffer) { + mask |= 1 << buf; + } } } @@ -257,27 +325,30 @@ void GLAPIENTRY _mesa_ClearBufferiv(GLenum buffer, GLint drawbuffer, const GLint *value) { GET_CURRENT_CONTEXT(ctx); - ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); + FLUSH_VERTICES(ctx, 0); FLUSH_CURRENT(ctx, 0); - if (!ctx->DrawBuffer->Visual.rgbMode) { - _mesa_error(ctx, GL_INVALID_OPERATION, "glClearBufferiv()"); - return; - } - if (ctx->NewState) { _mesa_update_state( ctx ); } switch (buffer) { case GL_STENCIL: + /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says: + * + * "ClearBuffer generates an INVALID VALUE error if buffer is + * COLOR and drawbuffer is less than zero, or greater than the + * value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH, + * STENCIL, or DEPTH STENCIL and drawbuffer is not zero." + */ if (drawbuffer != 0) { _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferiv(drawbuffer=%d)", drawbuffer); return; } - else { + else if (ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer + && !ctx->RasterDiscard) { /* Save current stencil clear value, set to 'value', do the * stencil clear and restore the clear value. * XXX in the future we may have a new ctx->Driver.ClearBuffer() @@ -285,12 +356,8 @@ _mesa_ClearBufferiv(GLenum buffer, GLint drawbuffer, const GLint *value) */ const GLuint clearSave = ctx->Stencil.Clear; ctx->Stencil.Clear = *value; - if (ctx->Driver.ClearStencil) - ctx->Driver.ClearStencil(ctx, *value); ctx->Driver.Clear(ctx, BUFFER_BIT_STENCIL); ctx->Stencil.Clear = clearSave; - if (ctx->Driver.ClearStencil) - ctx->Driver.ClearStencil(ctx, clearSave); } break; case GL_COLOR: @@ -301,35 +368,52 @@ _mesa_ClearBufferiv(GLenum buffer, GLint drawbuffer, const GLint *value) drawbuffer); return; } - else if (mask) { - /* XXX note: we're putting the integer clear values into the - * floating point state var. This will not always work. We'll - * need a new ctx->Driver.ClearBuffer() hook.... - */ - GLfloat clearSave[4]; + else if (mask && !ctx->RasterDiscard) { + union gl_color_union clearSave; + /* save color */ - COPY_4V(clearSave, ctx->Color.ClearColor); + clearSave = ctx->Color.ClearColor; /* set color */ - COPY_4V(ctx->Color.ClearColor, value); - if (ctx->Driver.ClearColor) - ctx->Driver.ClearColor(ctx, ctx->Color.ClearColor); + COPY_4V(ctx->Color.ClearColor.i, value); /* clear buffer(s) */ ctx->Driver.Clear(ctx, mask); /* restore color */ - COPY_4V(ctx->Color.ClearColor, clearSave); - if (ctx->Driver.ClearColor) - ctx->Driver.ClearColor(ctx, clearSave); + ctx->Color.ClearColor = clearSave; } } break; default: + /* Page 498 of the PDF, section '17.4.3.1 Clearing Individual Buffers' + * of the OpenGL 4.5 spec states: + * + * "An INVALID_ENUM error is generated by ClearBufferiv and + * ClearNamedFramebufferiv if buffer is not COLOR or STENCIL." + */ _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferiv(buffer=%s)", - _mesa_lookup_enum_by_nr(buffer)); + _mesa_enum_to_string(buffer)); return; } } +/** + * The ClearBuffer framework is so complicated and so riddled with the + * assumption that the framebuffer is bound that, for now, we will just fake + * direct state access clearing for the user. + */ +void GLAPIENTRY +_mesa_ClearNamedFramebufferiv(GLuint framebuffer, GLenum buffer, + GLint drawbuffer, const GLint *value) +{ + GLint oldfb; + + _mesa_GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfb); + _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer); + _mesa_ClearBufferiv(buffer, drawbuffer, value); + _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, (GLuint) oldfb); +} + + /** * New in GL 3.0 * Clear unsigned integer color buffer (not depth, not stencil). @@ -338,15 +422,10 @@ void GLAPIENTRY _mesa_ClearBufferuiv(GLenum buffer, GLint drawbuffer, const GLuint *value) { GET_CURRENT_CONTEXT(ctx); - ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); + FLUSH_VERTICES(ctx, 0); FLUSH_CURRENT(ctx, 0); - if (!ctx->DrawBuffer->Visual.rgbMode) { - _mesa_error(ctx, GL_INVALID_OPERATION, "glClearBufferuiv()"); - return; - } - if (ctx->NewState) { _mesa_update_state( ctx ); } @@ -356,39 +435,56 @@ _mesa_ClearBufferuiv(GLenum buffer, GLint drawbuffer, const GLuint *value) { const GLbitfield mask = make_color_buffer_mask(ctx, drawbuffer); if (mask == INVALID_MASK) { - _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferiv(drawbuffer=%d)", + _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferuiv(drawbuffer=%d)", drawbuffer); return; } - else if (mask) { - /* XXX note: we're putting the uint clear values into the - * floating point state var. This will not always work. We'll - * need a new ctx->Driver.ClearBuffer() hook.... - */ - GLfloat clearSave[4]; + else if (mask && !ctx->RasterDiscard) { + union gl_color_union clearSave; + /* save color */ - COPY_4V(clearSave, ctx->Color.ClearColor); + clearSave = ctx->Color.ClearColor; /* set color */ - COPY_4V(ctx->Color.ClearColor, value); - if (ctx->Driver.ClearColor) - ctx->Driver.ClearColor(ctx, ctx->Color.ClearColor); + COPY_4V(ctx->Color.ClearColor.ui, value); /* clear buffer(s) */ ctx->Driver.Clear(ctx, mask); /* restore color */ - COPY_4V(ctx->Color.ClearColor, clearSave); - if (ctx->Driver.ClearColor) - ctx->Driver.ClearColor(ctx, clearSave); + ctx->Color.ClearColor = clearSave; } } break; default: + /* Page 498 of the PDF, section '17.4.3.1 Clearing Individual Buffers' + * of the OpenGL 4.5 spec states: + * + * "An INVALID_ENUM error is generated by ClearBufferuiv and + * ClearNamedFramebufferuiv if buffer is not COLOR." + */ _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferuiv(buffer=%s)", - _mesa_lookup_enum_by_nr(buffer)); + _mesa_enum_to_string(buffer)); return; } } +/** + * The ClearBuffer framework is so complicated and so riddled with the + * assumption that the framebuffer is bound that, for now, we will just fake + * direct state access clearing for the user. + */ +void GLAPIENTRY +_mesa_ClearNamedFramebufferuiv(GLuint framebuffer, GLenum buffer, + GLint drawbuffer, const GLuint *value) +{ + GLint oldfb; + + _mesa_GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfb); + _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer); + _mesa_ClearBufferuiv(buffer, drawbuffer, value); + _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, (GLuint) oldfb); +} + + /** * New in GL 3.0 * Clear fixed-pt or float color buffer or depth buffer (not stencil). @@ -397,40 +493,39 @@ void GLAPIENTRY _mesa_ClearBufferfv(GLenum buffer, GLint drawbuffer, const GLfloat *value) { GET_CURRENT_CONTEXT(ctx); - ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); + FLUSH_VERTICES(ctx, 0); FLUSH_CURRENT(ctx, 0); - if (!ctx->DrawBuffer->Visual.rgbMode) { - _mesa_error(ctx, GL_INVALID_OPERATION, "glClearBufferfv()"); - return; - } - if (ctx->NewState) { _mesa_update_state( ctx ); } switch (buffer) { case GL_DEPTH: + /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says: + * + * "ClearBuffer generates an INVALID VALUE error if buffer is + * COLOR and drawbuffer is less than zero, or greater than the + * value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH, + * STENCIL, or DEPTH STENCIL and drawbuffer is not zero." + */ if (drawbuffer != 0) { _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferfv(drawbuffer=%d)", drawbuffer); return; } - else { + else if (ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer + && !ctx->RasterDiscard) { /* Save current depth clear value, set to 'value', do the * depth clear and restore the clear value. * XXX in the future we may have a new ctx->Driver.ClearBuffer() * hook instead. */ - const GLfloat clearSave = ctx->Depth.Clear; + const GLclampd clearSave = ctx->Depth.Clear; ctx->Depth.Clear = *value; - if (ctx->Driver.ClearDepth) - ctx->Driver.ClearDepth(ctx, *value); ctx->Driver.Clear(ctx, BUFFER_BIT_DEPTH); ctx->Depth.Clear = clearSave; - if (ctx->Driver.ClearDepth) - ctx->Driver.ClearDepth(ctx, clearSave); } /* clear depth buffer to value */ break; @@ -442,31 +537,52 @@ _mesa_ClearBufferfv(GLenum buffer, GLint drawbuffer, const GLfloat *value) drawbuffer); return; } - else if (mask) { - GLfloat clearSave[4]; + else if (mask && !ctx->RasterDiscard) { + union gl_color_union clearSave; + /* save color */ - COPY_4V(clearSave, ctx->Color.ClearColor); + clearSave = ctx->Color.ClearColor; /* set color */ - COPY_4V(ctx->Color.ClearColor, value); - if (ctx->Driver.ClearColor) - ctx->Driver.ClearColor(ctx, ctx->Color.ClearColor); + COPY_4V(ctx->Color.ClearColor.f, value); /* clear buffer(s) */ ctx->Driver.Clear(ctx, mask); /* restore color */ - COPY_4V(ctx->Color.ClearColor, clearSave); - if (ctx->Driver.ClearColor) - ctx->Driver.ClearColor(ctx, clearSave); + ctx->Color.ClearColor = clearSave; } } break; default: + /* Page 498 of the PDF, section '17.4.3.1 Clearing Individual Buffers' + * of the OpenGL 4.5 spec states: + * + * "An INVALID_ENUM error is generated by ClearBufferfv and + * ClearNamedFramebufferfv if buffer is not COLOR or DEPTH." + */ _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferfv(buffer=%s)", - _mesa_lookup_enum_by_nr(buffer)); + _mesa_enum_to_string(buffer)); return; } } +/** + * The ClearBuffer framework is so complicated and so riddled with the + * assumption that the framebuffer is bound that, for now, we will just fake + * direct state access clearing for the user. + */ +void GLAPIENTRY +_mesa_ClearNamedFramebufferfv(GLuint framebuffer, GLenum buffer, + GLint drawbuffer, const GLfloat *value) +{ + GLint oldfb; + + _mesa_GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfb); + _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer); + _mesa_ClearBufferfv(buffer, drawbuffer, value); + _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, (GLuint) oldfb); +} + + /** * New in GL 3.0 * Clear depth/stencil buffer only. @@ -476,53 +592,74 @@ _mesa_ClearBufferfi(GLenum buffer, GLint drawbuffer, GLfloat depth, GLint stencil) { GET_CURRENT_CONTEXT(ctx); - ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); + GLbitfield mask = 0; + FLUSH_VERTICES(ctx, 0); FLUSH_CURRENT(ctx, 0); - if (!ctx->DrawBuffer->Visual.rgbMode) { - _mesa_error(ctx, GL_INVALID_OPERATION, "glClearBufferfi()"); - return; - } - if (buffer != GL_DEPTH_STENCIL) { _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferfi(buffer=%s)", - _mesa_lookup_enum_by_nr(buffer)); + _mesa_enum_to_string(buffer)); return; } + /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says: + * + * "ClearBuffer generates an INVALID VALUE error if buffer is + * COLOR and drawbuffer is less than zero, or greater than the + * value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH, + * STENCIL, or DEPTH STENCIL and drawbuffer is not zero." + */ if (drawbuffer != 0) { _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferfi(drawbuffer=%d)", drawbuffer); return; } + if (ctx->RasterDiscard) + return; + if (ctx->NewState) { _mesa_update_state( ctx ); } - { + if (ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer) + mask |= BUFFER_BIT_DEPTH; + if (ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer) + mask |= BUFFER_BIT_STENCIL; + + if (mask) { /* save current clear values */ - const GLfloat clearDepthSave = ctx->Depth.Clear; + const GLclampd clearDepthSave = ctx->Depth.Clear; const GLuint clearStencilSave = ctx->Stencil.Clear; /* set new clear values */ ctx->Depth.Clear = depth; ctx->Stencil.Clear = stencil; - if (ctx->Driver.ClearDepth) - ctx->Driver.ClearDepth(ctx, depth); - if (ctx->Driver.ClearStencil) - ctx->Driver.ClearStencil(ctx, stencil); /* clear buffers */ - ctx->Driver.Clear(ctx, BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL); + ctx->Driver.Clear(ctx, mask); /* restore */ ctx->Depth.Clear = clearDepthSave; ctx->Stencil.Clear = clearStencilSave; - if (ctx->Driver.ClearDepth) - ctx->Driver.ClearDepth(ctx, clearDepthSave); - if (ctx->Driver.ClearStencil) - ctx->Driver.ClearStencil(ctx, clearStencilSave); } } + + +/** + * The ClearBuffer framework is so complicated and so riddled with the + * assumption that the framebuffer is bound that, for now, we will just fake + * direct state access clearing for the user. + */ +void GLAPIENTRY +_mesa_ClearNamedFramebufferfi(GLuint framebuffer, GLenum buffer, + GLint drawbuffer, GLfloat depth, GLint stencil) +{ + GLint oldfb; + + _mesa_GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfb); + _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer); + _mesa_ClearBufferfi(buffer, drawbuffer, depth, stencil); + _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, (GLuint) oldfb); +}