Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Mesa 3-D graphics library
  3.  *
  4.  * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the "Software"),
  8.  * to deal in the Software without restriction, including without limitation
  9.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10.  * and/or sell copies of the Software, and to permit persons to whom the
  11.  * Software is furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included
  14.  * in all copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  19.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22.  * OTHER DEALINGS IN THE SOFTWARE.
  23.  */
  24.  
  25.  
  26. /**
  27.  * \file clear.c
  28.  * glClearColor, glClearIndex, glClear() functions.
  29.  */
  30.  
  31.  
  32.  
  33. #include "glheader.h"
  34. #include "clear.h"
  35. #include "context.h"
  36. #include "colormac.h"
  37. #include "enums.h"
  38. #include "macros.h"
  39. #include "mtypes.h"
  40. #include "state.h"
  41.  
  42.  
  43.  
  44. void GLAPIENTRY
  45. _mesa_ClearIndex( GLfloat c )
  46. {
  47.    GET_CURRENT_CONTEXT(ctx);
  48.  
  49.    ctx->Color.ClearIndex = (GLuint) c;
  50. }
  51.  
  52.  
  53. /**
  54.  * Specify the clear values for the color buffers.
  55.  *
  56.  * \param red red color component.
  57.  * \param green green color component.
  58.  * \param blue blue color component.
  59.  * \param alpha alpha component.
  60.  *
  61.  * \sa glClearColor().
  62.  *
  63.  * Clamps the parameters and updates gl_colorbuffer_attrib::ClearColor.  On a
  64.  * change, flushes the vertices and notifies the driver via the
  65.  * dd_function_table::ClearColor callback.
  66.  */
  67. void GLAPIENTRY
  68. _mesa_ClearColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
  69. {
  70.    GET_CURRENT_CONTEXT(ctx);
  71.  
  72.    ctx->Color.ClearColor.f[0] = red;
  73.    ctx->Color.ClearColor.f[1] = green;
  74.    ctx->Color.ClearColor.f[2] = blue;
  75.    ctx->Color.ClearColor.f[3] = alpha;
  76. }
  77.  
  78.  
  79. /**
  80.  * GL_EXT_texture_integer
  81.  */
  82. void GLAPIENTRY
  83. _mesa_ClearColorIiEXT(GLint r, GLint g, GLint b, GLint a)
  84. {
  85.    GET_CURRENT_CONTEXT(ctx);
  86.  
  87.    ctx->Color.ClearColor.i[0] = r;
  88.    ctx->Color.ClearColor.i[1] = g;
  89.    ctx->Color.ClearColor.i[2] = b;
  90.    ctx->Color.ClearColor.i[3] = a;
  91. }
  92.  
  93.  
  94. /**
  95.  * GL_EXT_texture_integer
  96.  */
  97. void GLAPIENTRY
  98. _mesa_ClearColorIuiEXT(GLuint r, GLuint g, GLuint b, GLuint a)
  99. {
  100.    GET_CURRENT_CONTEXT(ctx);
  101.  
  102.    ctx->Color.ClearColor.ui[0] = r;
  103.    ctx->Color.ClearColor.ui[1] = g;
  104.    ctx->Color.ClearColor.ui[2] = b;
  105.    ctx->Color.ClearColor.ui[3] = a;
  106. }
  107.  
  108.  
  109. /**
  110.  * Clear buffers.
  111.  *
  112.  * \param mask bit-mask indicating the buffers to be cleared.
  113.  *
  114.  * Flushes the vertices and verifies the parameter. If __struct gl_contextRec::NewState
  115.  * is set then calls _mesa_update_state() to update gl_frame_buffer::_Xmin,
  116.  * etc. If the rasterization mode is set to GL_RENDER then requests the driver
  117.  * to clear the buffers, via the dd_function_table::Clear callback.
  118.  */
  119. void GLAPIENTRY
  120. _mesa_Clear( GLbitfield mask )
  121. {
  122.    GET_CURRENT_CONTEXT(ctx);
  123.    FLUSH_VERTICES(ctx, 0);
  124.  
  125.    FLUSH_CURRENT(ctx, 0);
  126.  
  127.    if (MESA_VERBOSE & VERBOSE_API)
  128.       _mesa_debug(ctx, "glClear 0x%x\n", mask);
  129.  
  130.    if (mask & ~(GL_COLOR_BUFFER_BIT |
  131.                 GL_DEPTH_BUFFER_BIT |
  132.                 GL_STENCIL_BUFFER_BIT |
  133.                 GL_ACCUM_BUFFER_BIT)) {
  134.       /* invalid bit set */
  135.       _mesa_error( ctx, GL_INVALID_VALUE, "glClear(0x%x)", mask);
  136.       return;
  137.    }
  138.  
  139.    /* Accumulation buffers were removed in core contexts, and they never
  140.     * existed in OpenGL ES.
  141.     */
  142.    if ((mask & GL_ACCUM_BUFFER_BIT) != 0
  143.        && (ctx->API == API_OPENGL_CORE || _mesa_is_gles(ctx))) {
  144.       _mesa_error( ctx, GL_INVALID_VALUE, "glClear(GL_ACCUM_BUFFER_BIT)");
  145.       return;
  146.    }
  147.  
  148.    if (ctx->NewState) {
  149.       _mesa_update_state( ctx );        /* update _Xmin, etc */
  150.    }
  151.  
  152.    if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
  153.       _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
  154.                   "glClear(incomplete framebuffer)");
  155.       return;
  156.    }
  157.  
  158.    if (ctx->DrawBuffer->Width == 0 || ctx->DrawBuffer->Height == 0 ||
  159.        ctx->DrawBuffer->_Xmin >= ctx->DrawBuffer->_Xmax ||
  160.        ctx->DrawBuffer->_Ymin >= ctx->DrawBuffer->_Ymax)
  161.       return;
  162.  
  163.    if (ctx->RasterDiscard)
  164.       return;
  165.  
  166.    if (ctx->RenderMode == GL_RENDER) {
  167.       GLbitfield bufferMask;
  168.  
  169.       /* don't clear depth buffer if depth writing disabled */
  170.       if (!ctx->Depth.Mask)
  171.          mask &= ~GL_DEPTH_BUFFER_BIT;
  172.  
  173.       /* Build the bitmask to send to device driver's Clear function.
  174.        * Note that the GL_COLOR_BUFFER_BIT flag will expand to 0, 1, 2 or 4
  175.        * of the BUFFER_BIT_FRONT/BACK_LEFT/RIGHT flags, or one of the
  176.        * BUFFER_BIT_COLORn flags.
  177.        */
  178.       bufferMask = 0;
  179.       if (mask & GL_COLOR_BUFFER_BIT) {
  180.          GLuint i;
  181.          for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
  182.             bufferMask |= (1 << ctx->DrawBuffer->_ColorDrawBufferIndexes[i]);
  183.          }
  184.       }
  185.  
  186.       if ((mask & GL_DEPTH_BUFFER_BIT)
  187.           && ctx->DrawBuffer->Visual.haveDepthBuffer) {
  188.          bufferMask |= BUFFER_BIT_DEPTH;
  189.       }
  190.  
  191.       if ((mask & GL_STENCIL_BUFFER_BIT)
  192.           && ctx->DrawBuffer->Visual.haveStencilBuffer) {
  193.          bufferMask |= BUFFER_BIT_STENCIL;
  194.       }
  195.  
  196.       if ((mask & GL_ACCUM_BUFFER_BIT)
  197.           && ctx->DrawBuffer->Visual.haveAccumBuffer) {
  198.          bufferMask |= BUFFER_BIT_ACCUM;
  199.       }
  200.  
  201.       ASSERT(ctx->Driver.Clear);
  202.       ctx->Driver.Clear(ctx, bufferMask);
  203.    }
  204.     LEAVE();
  205.  
  206. }
  207.  
  208.  
  209. /** Returned by make_color_buffer_mask() for errors */
  210. #define INVALID_MASK ~0x0
  211.  
  212.  
  213. /**
  214.  * Convert the glClearBuffer 'drawbuffer' parameter into a bitmask of
  215.  * BUFFER_BIT_x values.
  216.  * Return INVALID_MASK if the drawbuffer value is invalid.
  217.  */
  218. static GLbitfield
  219. make_color_buffer_mask(struct gl_context *ctx, GLint drawbuffer)
  220. {
  221.    const struct gl_renderbuffer_attachment *att = ctx->DrawBuffer->Attachment;
  222.    GLbitfield mask = 0x0;
  223.  
  224.    switch (drawbuffer) {
  225.    case GL_FRONT:
  226.       if (att[BUFFER_FRONT_LEFT].Renderbuffer)
  227.          mask |= BUFFER_BIT_FRONT_LEFT;
  228.       if (att[BUFFER_FRONT_RIGHT].Renderbuffer)
  229.          mask |= BUFFER_BIT_FRONT_RIGHT;
  230.       break;
  231.    case GL_BACK:
  232.       if (att[BUFFER_BACK_LEFT].Renderbuffer)
  233.          mask |= BUFFER_BIT_BACK_LEFT;
  234.       if (att[BUFFER_BACK_RIGHT].Renderbuffer)
  235.          mask |= BUFFER_BIT_BACK_RIGHT;
  236.       break;
  237.    case GL_LEFT:
  238.       if (att[BUFFER_FRONT_LEFT].Renderbuffer)
  239.          mask |= BUFFER_BIT_FRONT_LEFT;
  240.       if (att[BUFFER_BACK_LEFT].Renderbuffer)
  241.          mask |= BUFFER_BIT_BACK_LEFT;
  242.       break;
  243.    case GL_RIGHT:
  244.       if (att[BUFFER_FRONT_RIGHT].Renderbuffer)
  245.          mask |= BUFFER_BIT_FRONT_RIGHT;
  246.       if (att[BUFFER_BACK_RIGHT].Renderbuffer)
  247.          mask |= BUFFER_BIT_BACK_RIGHT;
  248.       break;
  249.    case GL_FRONT_AND_BACK:
  250.       if (att[BUFFER_FRONT_LEFT].Renderbuffer)
  251.          mask |= BUFFER_BIT_FRONT_LEFT;
  252.       if (att[BUFFER_BACK_LEFT].Renderbuffer)
  253.          mask |= BUFFER_BIT_BACK_LEFT;
  254.       if (att[BUFFER_FRONT_RIGHT].Renderbuffer)
  255.          mask |= BUFFER_BIT_FRONT_RIGHT;
  256.       if (att[BUFFER_BACK_RIGHT].Renderbuffer)
  257.          mask |= BUFFER_BIT_BACK_RIGHT;
  258.       break;
  259.    default:
  260.       if (drawbuffer < 0 || drawbuffer >= (GLint)ctx->Const.MaxDrawBuffers) {
  261.          mask = INVALID_MASK;
  262.       }
  263.       else if (att[BUFFER_COLOR0 + drawbuffer].Renderbuffer) {
  264.          mask |= (BUFFER_BIT_COLOR0 << drawbuffer);
  265.       }
  266.    }
  267.  
  268.    return mask;
  269. }
  270.  
  271.  
  272.  
  273. /**
  274.  * New in GL 3.0
  275.  * Clear signed integer color buffer or stencil buffer (not depth).
  276.  */
  277. void GLAPIENTRY
  278. _mesa_ClearBufferiv(GLenum buffer, GLint drawbuffer, const GLint *value)
  279. {
  280.    GET_CURRENT_CONTEXT(ctx);
  281.    FLUSH_VERTICES(ctx, 0);
  282.  
  283.    FLUSH_CURRENT(ctx, 0);
  284.  
  285.    if (ctx->NewState) {
  286.       _mesa_update_state( ctx );
  287.    }
  288.  
  289.    switch (buffer) {
  290.    case GL_STENCIL:
  291.       /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
  292.        *
  293.        *     "ClearBuffer generates an INVALID VALUE error if buffer is
  294.        *     COLOR and drawbuffer is less than zero, or greater than the
  295.        *     value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH,
  296.        *     STENCIL, or DEPTH STENCIL and drawbuffer is not zero."
  297.        */
  298.       if (drawbuffer != 0) {
  299.          _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferiv(drawbuffer=%d)",
  300.                      drawbuffer);
  301.          return;
  302.       }
  303.       else if (ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer && !ctx->RasterDiscard) {
  304.          /* Save current stencil clear value, set to 'value', do the
  305.           * stencil clear and restore the clear value.
  306.           * XXX in the future we may have a new ctx->Driver.ClearBuffer()
  307.           * hook instead.
  308.           */
  309.          const GLuint clearSave = ctx->Stencil.Clear;
  310.          ctx->Stencil.Clear = *value;
  311.          ctx->Driver.Clear(ctx, BUFFER_BIT_STENCIL);
  312.          ctx->Stencil.Clear = clearSave;
  313.       }
  314.       break;
  315.    case GL_COLOR:
  316.       {
  317.          const GLbitfield mask = make_color_buffer_mask(ctx, drawbuffer);
  318.          if (mask == INVALID_MASK) {
  319.             _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferiv(drawbuffer=%d)",
  320.                         drawbuffer);
  321.             return;
  322.          }
  323.          else if (mask && !ctx->RasterDiscard) {
  324.             union gl_color_union clearSave;
  325.  
  326.             /* save color */
  327.             clearSave = ctx->Color.ClearColor;
  328.             /* set color */
  329.             COPY_4V(ctx->Color.ClearColor.i, value);
  330.             /* clear buffer(s) */
  331.             ctx->Driver.Clear(ctx, mask);
  332.             /* restore color */
  333.             ctx->Color.ClearColor = clearSave;
  334.          }
  335.       }
  336.       break;
  337.    case GL_DEPTH:
  338.       /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
  339.        *
  340.        *     "The result of ClearBuffer is undefined if no conversion between
  341.        *     the type of the specified value and the type of the buffer being
  342.        *     cleared is defined (for example, if ClearBufferiv is called for a
  343.        *     fixed- or floating-point buffer, or if ClearBufferfv is called
  344.        *     for a signed or unsigned integer buffer). This is not an error."
  345.        *
  346.        * In this case we take "undefined" and "not an error" to mean "ignore."
  347.        * Note that we still need to generate an error for the invalid
  348.        * drawbuffer case (see the GL_STENCIL case above).
  349.        */
  350.       if (drawbuffer != 0) {
  351.          _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferiv(drawbuffer=%d)",
  352.                      drawbuffer);
  353.          return;
  354.       }
  355.       return;
  356.    default:
  357.       _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferiv(buffer=%s)",
  358.                   _mesa_lookup_enum_by_nr(buffer));
  359.       return;
  360.    }
  361. }
  362.  
  363.  
  364. /**
  365.  * New in GL 3.0
  366.  * Clear unsigned integer color buffer (not depth, not stencil).
  367.  */
  368. void GLAPIENTRY
  369. _mesa_ClearBufferuiv(GLenum buffer, GLint drawbuffer, const GLuint *value)
  370. {
  371.    GET_CURRENT_CONTEXT(ctx);
  372.  
  373.    FLUSH_VERTICES(ctx, 0);
  374.    FLUSH_CURRENT(ctx, 0);
  375.  
  376.    if (ctx->NewState) {
  377.       _mesa_update_state( ctx );
  378.    }
  379.  
  380.    switch (buffer) {
  381.    case GL_COLOR:
  382.       {
  383.          const GLbitfield mask = make_color_buffer_mask(ctx, drawbuffer);
  384.          if (mask == INVALID_MASK) {
  385.             _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferuiv(drawbuffer=%d)",
  386.                         drawbuffer);
  387.             return;
  388.          }
  389.          else if (mask && !ctx->RasterDiscard) {
  390.             union gl_color_union clearSave;
  391.  
  392.             /* save color */
  393.             clearSave = ctx->Color.ClearColor;
  394.             /* set color */
  395.             COPY_4V(ctx->Color.ClearColor.ui, value);
  396.             /* clear buffer(s) */
  397.             ctx->Driver.Clear(ctx, mask);
  398.             /* restore color */
  399.             ctx->Color.ClearColor = clearSave;
  400.          }
  401.       }
  402.       break;
  403.    case GL_DEPTH:
  404.    case GL_STENCIL:
  405.       /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
  406.        *
  407.        *     "The result of ClearBuffer is undefined if no conversion between
  408.        *     the type of the specified value and the type of the buffer being
  409.        *     cleared is defined (for example, if ClearBufferiv is called for a
  410.        *     fixed- or floating-point buffer, or if ClearBufferfv is called
  411.        *     for a signed or unsigned integer buffer). This is not an error."
  412.        *
  413.        * In this case we take "undefined" and "not an error" to mean "ignore."
  414.        * Even though we could do something sensible for GL_STENCIL, page 263
  415.        * (page 279 of the PDF) says:
  416.        *
  417.        *     "Only ClearBufferiv should be used to clear stencil buffers."
  418.        *
  419.        * Note that we still need to generate an error for the invalid
  420.        * drawbuffer case (see the GL_STENCIL case in _mesa_ClearBufferiv).
  421.        */
  422.       if (drawbuffer != 0) {
  423.          _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferuiv(drawbuffer=%d)",
  424.                      drawbuffer);
  425.          return;
  426.       }
  427.       return;
  428.    default:
  429.       _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferuiv(buffer=%s)",
  430.                   _mesa_lookup_enum_by_nr(buffer));
  431.       return;
  432.    }
  433. }
  434.  
  435.  
  436. /**
  437.  * New in GL 3.0
  438.  * Clear fixed-pt or float color buffer or depth buffer (not stencil).
  439.  */
  440. void GLAPIENTRY
  441. _mesa_ClearBufferfv(GLenum buffer, GLint drawbuffer, const GLfloat *value)
  442. {
  443.    GET_CURRENT_CONTEXT(ctx);
  444.  
  445.    FLUSH_VERTICES(ctx, 0);
  446.    FLUSH_CURRENT(ctx, 0);
  447.  
  448.    if (ctx->NewState) {
  449.       _mesa_update_state( ctx );
  450.    }
  451.  
  452.    switch (buffer) {
  453.    case GL_DEPTH:
  454.       /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
  455.        *
  456.        *     "ClearBuffer generates an INVALID VALUE error if buffer is
  457.        *     COLOR and drawbuffer is less than zero, or greater than the
  458.        *     value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH,
  459.        *     STENCIL, or DEPTH STENCIL and drawbuffer is not zero."
  460.        */
  461.       if (drawbuffer != 0) {
  462.          _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferfv(drawbuffer=%d)",
  463.                      drawbuffer);
  464.          return;
  465.       }
  466.       else if (ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer && !ctx->RasterDiscard) {
  467.          /* Save current depth clear value, set to 'value', do the
  468.           * depth clear and restore the clear value.
  469.           * XXX in the future we may have a new ctx->Driver.ClearBuffer()
  470.           * hook instead.
  471.           */
  472.          const GLclampd clearSave = ctx->Depth.Clear;
  473.          ctx->Depth.Clear = *value;
  474.          ctx->Driver.Clear(ctx, BUFFER_BIT_DEPTH);
  475.          ctx->Depth.Clear = clearSave;
  476.       }
  477.       /* clear depth buffer to value */
  478.       break;
  479.    case GL_COLOR:
  480.       {
  481.          const GLbitfield mask = make_color_buffer_mask(ctx, drawbuffer);
  482.          if (mask == INVALID_MASK) {
  483.             _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferfv(drawbuffer=%d)",
  484.                         drawbuffer);
  485.             return;
  486.          }
  487.          else if (mask && !ctx->RasterDiscard) {
  488.             union gl_color_union clearSave;
  489.  
  490.             /* save color */
  491.             clearSave = ctx->Color.ClearColor;
  492.             /* set color */
  493.             COPY_4V(ctx->Color.ClearColor.f, value);
  494.             /* clear buffer(s) */
  495.             ctx->Driver.Clear(ctx, mask);
  496.             /* restore color */
  497.             ctx->Color.ClearColor = clearSave;
  498.          }
  499.       }
  500.       break;
  501.    case GL_STENCIL:
  502.       /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
  503.        *
  504.        *     "The result of ClearBuffer is undefined if no conversion between
  505.        *     the type of the specified value and the type of the buffer being
  506.        *     cleared is defined (for example, if ClearBufferiv is called for a
  507.        *     fixed- or floating-point buffer, or if ClearBufferfv is called
  508.        *     for a signed or unsigned integer buffer). This is not an error."
  509.        *
  510.        * In this case we take "undefined" and "not an error" to mean "ignore."
  511.        * Note that we still need to generate an error for the invalid
  512.        * drawbuffer case (see the GL_DEPTH case above).
  513.        */
  514.       if (drawbuffer != 0) {
  515.          _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferfv(drawbuffer=%d)",
  516.                      drawbuffer);
  517.          return;
  518.       }
  519.       return;
  520.    default:
  521.       _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferfv(buffer=%s)",
  522.                   _mesa_lookup_enum_by_nr(buffer));
  523.       return;
  524.    }
  525. }
  526.  
  527.  
  528. /**
  529.  * New in GL 3.0
  530.  * Clear depth/stencil buffer only.
  531.  */
  532. void GLAPIENTRY
  533. _mesa_ClearBufferfi(GLenum buffer, GLint drawbuffer,
  534.                     GLfloat depth, GLint stencil)
  535. {
  536.    GET_CURRENT_CONTEXT(ctx);
  537.    GLbitfield mask = 0;
  538.  
  539.    FLUSH_VERTICES(ctx, 0);
  540.    FLUSH_CURRENT(ctx, 0);
  541.  
  542.    if (buffer != GL_DEPTH_STENCIL) {
  543.       _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferfi(buffer=%s)",
  544.                   _mesa_lookup_enum_by_nr(buffer));
  545.       return;
  546.    }
  547.  
  548.    /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
  549.     *
  550.     *     "ClearBuffer generates an INVALID VALUE error if buffer is
  551.     *     COLOR and drawbuffer is less than zero, or greater than the
  552.     *     value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH,
  553.     *     STENCIL, or DEPTH STENCIL and drawbuffer is not zero."
  554.     */
  555.    if (drawbuffer != 0) {
  556.       _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferfi(drawbuffer=%d)",
  557.                   drawbuffer);
  558.       return;
  559.    }
  560.  
  561.    if (ctx->RasterDiscard)
  562.       return;
  563.  
  564.    if (ctx->NewState) {
  565.       _mesa_update_state( ctx );
  566.    }
  567.  
  568.    if (ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer)
  569.       mask |= BUFFER_BIT_DEPTH;
  570.    if (ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer)
  571.       mask |= BUFFER_BIT_STENCIL;
  572.  
  573.    if (mask) {
  574.       /* save current clear values */
  575.       const GLclampd clearDepthSave = ctx->Depth.Clear;
  576.       const GLuint clearStencilSave = ctx->Stencil.Clear;
  577.  
  578.       /* set new clear values */
  579.       ctx->Depth.Clear = depth;
  580.       ctx->Stencil.Clear = stencil;
  581.  
  582.       /* clear buffers */
  583.       ctx->Driver.Clear(ctx, mask);
  584.  
  585.       /* restore */
  586.       ctx->Depth.Clear = clearDepthSave;
  587.       ctx->Stencil.Clear = clearStencilSave;
  588.    }
  589. }
  590.