1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Mesa 3-D graphics library 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Copyright (C) 1999-2007 Brian Paul All Rights Reserved. 5bf215546Sopenharmony_ci * 6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 8bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 9bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 11bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 12bf215546Sopenharmony_ci * 13bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included 14bf215546Sopenharmony_ci * in all copies or substantial portions of the Software. 15bf215546Sopenharmony_ci * 16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20bf215546Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22bf215546Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE. 23bf215546Sopenharmony_ci */ 24bf215546Sopenharmony_ci 25bf215546Sopenharmony_ci 26bf215546Sopenharmony_ci/** 27bf215546Sopenharmony_ci * \file clear.c 28bf215546Sopenharmony_ci * glClearColor, glClearIndex, glClear() functions. 29bf215546Sopenharmony_ci */ 30bf215546Sopenharmony_ci 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ci 33bf215546Sopenharmony_ci#include "glformats.h" 34bf215546Sopenharmony_ci#include "glheader.h" 35bf215546Sopenharmony_ci#include "context.h" 36bf215546Sopenharmony_ci#include "enums.h" 37bf215546Sopenharmony_ci#include "fbobject.h" 38bf215546Sopenharmony_ci#include "get.h" 39bf215546Sopenharmony_ci#include "macros.h" 40bf215546Sopenharmony_ci#include "mtypes.h" 41bf215546Sopenharmony_ci#include "state.h" 42bf215546Sopenharmony_ci#include "api_exec_decl.h" 43bf215546Sopenharmony_ci 44bf215546Sopenharmony_ci#include "state_tracker/st_cb_clear.h" 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_civoid GLAPIENTRY 47bf215546Sopenharmony_ci_mesa_ClearIndex( GLfloat c ) 48bf215546Sopenharmony_ci{ 49bf215546Sopenharmony_ci GET_CURRENT_CONTEXT(ctx); 50bf215546Sopenharmony_ci 51bf215546Sopenharmony_ci ctx->PopAttribState |= GL_COLOR_BUFFER_BIT; 52bf215546Sopenharmony_ci ctx->Color.ClearIndex = (GLuint) c; 53bf215546Sopenharmony_ci} 54bf215546Sopenharmony_ci 55bf215546Sopenharmony_ci 56bf215546Sopenharmony_ci/** 57bf215546Sopenharmony_ci * Specify the clear values for the color buffers. 58bf215546Sopenharmony_ci * 59bf215546Sopenharmony_ci * \param red red color component. 60bf215546Sopenharmony_ci * \param green green color component. 61bf215546Sopenharmony_ci * \param blue blue color component. 62bf215546Sopenharmony_ci * \param alpha alpha component. 63bf215546Sopenharmony_ci * 64bf215546Sopenharmony_ci * \sa glClearColor(). 65bf215546Sopenharmony_ci */ 66bf215546Sopenharmony_civoid GLAPIENTRY 67bf215546Sopenharmony_ci_mesa_ClearColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha ) 68bf215546Sopenharmony_ci{ 69bf215546Sopenharmony_ci GET_CURRENT_CONTEXT(ctx); 70bf215546Sopenharmony_ci 71bf215546Sopenharmony_ci ctx->PopAttribState |= GL_COLOR_BUFFER_BIT; 72bf215546Sopenharmony_ci ctx->Color.ClearColor.f[0] = red; 73bf215546Sopenharmony_ci ctx->Color.ClearColor.f[1] = green; 74bf215546Sopenharmony_ci ctx->Color.ClearColor.f[2] = blue; 75bf215546Sopenharmony_ci ctx->Color.ClearColor.f[3] = alpha; 76bf215546Sopenharmony_ci} 77bf215546Sopenharmony_ci 78bf215546Sopenharmony_ci 79bf215546Sopenharmony_ci/** 80bf215546Sopenharmony_ci * GL_EXT_texture_integer 81bf215546Sopenharmony_ci */ 82bf215546Sopenharmony_civoid GLAPIENTRY 83bf215546Sopenharmony_ci_mesa_ClearColorIiEXT(GLint r, GLint g, GLint b, GLint a) 84bf215546Sopenharmony_ci{ 85bf215546Sopenharmony_ci GET_CURRENT_CONTEXT(ctx); 86bf215546Sopenharmony_ci 87bf215546Sopenharmony_ci ctx->PopAttribState |= GL_COLOR_BUFFER_BIT; 88bf215546Sopenharmony_ci ctx->Color.ClearColor.i[0] = r; 89bf215546Sopenharmony_ci ctx->Color.ClearColor.i[1] = g; 90bf215546Sopenharmony_ci ctx->Color.ClearColor.i[2] = b; 91bf215546Sopenharmony_ci ctx->Color.ClearColor.i[3] = a; 92bf215546Sopenharmony_ci} 93bf215546Sopenharmony_ci 94bf215546Sopenharmony_ci 95bf215546Sopenharmony_ci/** 96bf215546Sopenharmony_ci * GL_EXT_texture_integer 97bf215546Sopenharmony_ci */ 98bf215546Sopenharmony_civoid GLAPIENTRY 99bf215546Sopenharmony_ci_mesa_ClearColorIuiEXT(GLuint r, GLuint g, GLuint b, GLuint a) 100bf215546Sopenharmony_ci{ 101bf215546Sopenharmony_ci GET_CURRENT_CONTEXT(ctx); 102bf215546Sopenharmony_ci 103bf215546Sopenharmony_ci ctx->PopAttribState |= GL_COLOR_BUFFER_BIT; 104bf215546Sopenharmony_ci ctx->Color.ClearColor.ui[0] = r; 105bf215546Sopenharmony_ci ctx->Color.ClearColor.ui[1] = g; 106bf215546Sopenharmony_ci ctx->Color.ClearColor.ui[2] = b; 107bf215546Sopenharmony_ci ctx->Color.ClearColor.ui[3] = a; 108bf215546Sopenharmony_ci} 109bf215546Sopenharmony_ci 110bf215546Sopenharmony_ci 111bf215546Sopenharmony_ci/** 112bf215546Sopenharmony_ci * Returns true if color writes are enabled for the given color attachment. 113bf215546Sopenharmony_ci * 114bf215546Sopenharmony_ci * Beyond checking ColorMask, this uses _mesa_format_has_color_component to 115bf215546Sopenharmony_ci * ignore components that don't actually exist in the format (such as X in 116bf215546Sopenharmony_ci * XRGB). 117bf215546Sopenharmony_ci */ 118bf215546Sopenharmony_cistatic bool 119bf215546Sopenharmony_cicolor_buffer_writes_enabled(const struct gl_context *ctx, unsigned idx) 120bf215546Sopenharmony_ci{ 121bf215546Sopenharmony_ci struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[idx]; 122bf215546Sopenharmony_ci GLuint c; 123bf215546Sopenharmony_ci 124bf215546Sopenharmony_ci if (rb) { 125bf215546Sopenharmony_ci for (c = 0; c < 4; c++) { 126bf215546Sopenharmony_ci if (GET_COLORMASK_BIT(ctx->Color.ColorMask, idx, c) && 127bf215546Sopenharmony_ci _mesa_format_has_color_component(rb->Format, c)) { 128bf215546Sopenharmony_ci return true; 129bf215546Sopenharmony_ci } 130bf215546Sopenharmony_ci } 131bf215546Sopenharmony_ci } 132bf215546Sopenharmony_ci 133bf215546Sopenharmony_ci return false; 134bf215546Sopenharmony_ci} 135bf215546Sopenharmony_ci 136bf215546Sopenharmony_ci 137bf215546Sopenharmony_ci/** 138bf215546Sopenharmony_ci * Clear buffers. 139bf215546Sopenharmony_ci * 140bf215546Sopenharmony_ci * \param mask bit-mask indicating the buffers to be cleared. 141bf215546Sopenharmony_ci * 142bf215546Sopenharmony_ci * Flushes the vertices and verifies the parameter. 143bf215546Sopenharmony_ci * If __struct gl_contextRec::NewState is set then calls _mesa_update_state() 144bf215546Sopenharmony_ci * to update gl_frame_buffer::_Xmin, etc. If the rasterization mode is set to 145bf215546Sopenharmony_ci * GL_RENDER then requests the driver to clear the buffers, via the 146bf215546Sopenharmony_ci * dd_function_table::Clear callback. 147bf215546Sopenharmony_ci */ 148bf215546Sopenharmony_cistatic ALWAYS_INLINE void 149bf215546Sopenharmony_ciclear(struct gl_context *ctx, GLbitfield mask, bool no_error) 150bf215546Sopenharmony_ci{ 151bf215546Sopenharmony_ci FLUSH_VERTICES(ctx, 0, 0); 152bf215546Sopenharmony_ci 153bf215546Sopenharmony_ci if (!no_error) { 154bf215546Sopenharmony_ci if (mask & ~(GL_COLOR_BUFFER_BIT | 155bf215546Sopenharmony_ci GL_DEPTH_BUFFER_BIT | 156bf215546Sopenharmony_ci GL_STENCIL_BUFFER_BIT | 157bf215546Sopenharmony_ci GL_ACCUM_BUFFER_BIT)) { 158bf215546Sopenharmony_ci _mesa_error( ctx, GL_INVALID_VALUE, "glClear(0x%x)", mask); 159bf215546Sopenharmony_ci return; 160bf215546Sopenharmony_ci } 161bf215546Sopenharmony_ci 162bf215546Sopenharmony_ci /* Accumulation buffers were removed in core contexts, and they never 163bf215546Sopenharmony_ci * existed in OpenGL ES. 164bf215546Sopenharmony_ci */ 165bf215546Sopenharmony_ci if ((mask & GL_ACCUM_BUFFER_BIT) != 0 166bf215546Sopenharmony_ci && (ctx->API == API_OPENGL_CORE || _mesa_is_gles(ctx))) { 167bf215546Sopenharmony_ci _mesa_error( ctx, GL_INVALID_VALUE, "glClear(GL_ACCUM_BUFFER_BIT)"); 168bf215546Sopenharmony_ci return; 169bf215546Sopenharmony_ci } 170bf215546Sopenharmony_ci } 171bf215546Sopenharmony_ci 172bf215546Sopenharmony_ci if (ctx->NewState) { 173bf215546Sopenharmony_ci _mesa_update_state( ctx ); /* update _Xmin, etc */ 174bf215546Sopenharmony_ci } 175bf215546Sopenharmony_ci 176bf215546Sopenharmony_ci if (!no_error && ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) { 177bf215546Sopenharmony_ci _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT, 178bf215546Sopenharmony_ci "glClear(incomplete framebuffer)"); 179bf215546Sopenharmony_ci return; 180bf215546Sopenharmony_ci } 181bf215546Sopenharmony_ci 182bf215546Sopenharmony_ci if (ctx->RasterDiscard) 183bf215546Sopenharmony_ci return; 184bf215546Sopenharmony_ci 185bf215546Sopenharmony_ci if (ctx->RenderMode == GL_RENDER) { 186bf215546Sopenharmony_ci GLbitfield bufferMask; 187bf215546Sopenharmony_ci 188bf215546Sopenharmony_ci /* don't clear depth buffer if depth writing disabled */ 189bf215546Sopenharmony_ci if (!ctx->Depth.Mask) 190bf215546Sopenharmony_ci mask &= ~GL_DEPTH_BUFFER_BIT; 191bf215546Sopenharmony_ci 192bf215546Sopenharmony_ci /* Build the bitmask to send to device driver's Clear function. 193bf215546Sopenharmony_ci * Note that the GL_COLOR_BUFFER_BIT flag will expand to 0, 1, 2 or 4 194bf215546Sopenharmony_ci * of the BUFFER_BIT_FRONT/BACK_LEFT/RIGHT flags, or one of the 195bf215546Sopenharmony_ci * BUFFER_BIT_COLORn flags. 196bf215546Sopenharmony_ci */ 197bf215546Sopenharmony_ci bufferMask = 0; 198bf215546Sopenharmony_ci if (mask & GL_COLOR_BUFFER_BIT) { 199bf215546Sopenharmony_ci GLuint i; 200bf215546Sopenharmony_ci for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) { 201bf215546Sopenharmony_ci gl_buffer_index buf = ctx->DrawBuffer->_ColorDrawBufferIndexes[i]; 202bf215546Sopenharmony_ci 203bf215546Sopenharmony_ci if (buf != BUFFER_NONE && color_buffer_writes_enabled(ctx, i)) { 204bf215546Sopenharmony_ci bufferMask |= 1 << buf; 205bf215546Sopenharmony_ci } 206bf215546Sopenharmony_ci } 207bf215546Sopenharmony_ci } 208bf215546Sopenharmony_ci 209bf215546Sopenharmony_ci if ((mask & GL_DEPTH_BUFFER_BIT) 210bf215546Sopenharmony_ci && ctx->DrawBuffer->Visual.depthBits > 0) { 211bf215546Sopenharmony_ci bufferMask |= BUFFER_BIT_DEPTH; 212bf215546Sopenharmony_ci } 213bf215546Sopenharmony_ci 214bf215546Sopenharmony_ci if ((mask & GL_STENCIL_BUFFER_BIT) 215bf215546Sopenharmony_ci && ctx->DrawBuffer->Visual.stencilBits > 0) { 216bf215546Sopenharmony_ci bufferMask |= BUFFER_BIT_STENCIL; 217bf215546Sopenharmony_ci } 218bf215546Sopenharmony_ci 219bf215546Sopenharmony_ci if ((mask & GL_ACCUM_BUFFER_BIT) 220bf215546Sopenharmony_ci && ctx->DrawBuffer->Visual.accumRedBits > 0) { 221bf215546Sopenharmony_ci bufferMask |= BUFFER_BIT_ACCUM; 222bf215546Sopenharmony_ci } 223bf215546Sopenharmony_ci 224bf215546Sopenharmony_ci st_Clear(ctx, bufferMask); 225bf215546Sopenharmony_ci } 226bf215546Sopenharmony_ci} 227bf215546Sopenharmony_ci 228bf215546Sopenharmony_ci 229bf215546Sopenharmony_civoid GLAPIENTRY 230bf215546Sopenharmony_ci_mesa_Clear_no_error(GLbitfield mask) 231bf215546Sopenharmony_ci{ 232bf215546Sopenharmony_ci GET_CURRENT_CONTEXT(ctx); 233bf215546Sopenharmony_ci clear(ctx, mask, true); 234bf215546Sopenharmony_ci} 235bf215546Sopenharmony_ci 236bf215546Sopenharmony_ci 237bf215546Sopenharmony_civoid GLAPIENTRY 238bf215546Sopenharmony_ci_mesa_Clear(GLbitfield mask) 239bf215546Sopenharmony_ci{ 240bf215546Sopenharmony_ci GET_CURRENT_CONTEXT(ctx); 241bf215546Sopenharmony_ci 242bf215546Sopenharmony_ci if (MESA_VERBOSE & VERBOSE_API) 243bf215546Sopenharmony_ci _mesa_debug(ctx, "glClear 0x%x\n", mask); 244bf215546Sopenharmony_ci 245bf215546Sopenharmony_ci clear(ctx, mask, false); 246bf215546Sopenharmony_ci} 247bf215546Sopenharmony_ci 248bf215546Sopenharmony_ci 249bf215546Sopenharmony_ci/** Returned by make_color_buffer_mask() for errors */ 250bf215546Sopenharmony_ci#define INVALID_MASK ~0x0U 251bf215546Sopenharmony_ci 252bf215546Sopenharmony_ci 253bf215546Sopenharmony_ci/** 254bf215546Sopenharmony_ci * Convert the glClearBuffer 'drawbuffer' parameter into a bitmask of 255bf215546Sopenharmony_ci * BUFFER_BIT_x values. 256bf215546Sopenharmony_ci * Return INVALID_MASK if the drawbuffer value is invalid. 257bf215546Sopenharmony_ci */ 258bf215546Sopenharmony_cistatic GLbitfield 259bf215546Sopenharmony_cimake_color_buffer_mask(struct gl_context *ctx, GLint drawbuffer) 260bf215546Sopenharmony_ci{ 261bf215546Sopenharmony_ci const struct gl_renderbuffer_attachment *att = ctx->DrawBuffer->Attachment; 262bf215546Sopenharmony_ci GLbitfield mask = 0x0; 263bf215546Sopenharmony_ci 264bf215546Sopenharmony_ci /* From the GL 4.0 specification: 265bf215546Sopenharmony_ci * If buffer is COLOR, a particular draw buffer DRAW_BUFFERi is 266bf215546Sopenharmony_ci * specified by passing i as the parameter drawbuffer, and value 267bf215546Sopenharmony_ci * points to a four-element vector specifying the R, G, B, and A 268bf215546Sopenharmony_ci * color to clear that draw buffer to. If the draw buffer is one 269bf215546Sopenharmony_ci * of FRONT, BACK, LEFT, RIGHT, or FRONT_AND_BACK, identifying 270bf215546Sopenharmony_ci * multiple buffers, each selected buffer is cleared to the same 271bf215546Sopenharmony_ci * value. 272bf215546Sopenharmony_ci * 273bf215546Sopenharmony_ci * Note that "drawbuffer" and "draw buffer" have different meaning. 274bf215546Sopenharmony_ci * "drawbuffer" specifies DRAW_BUFFERi, while "draw buffer" is what's 275bf215546Sopenharmony_ci * assigned to DRAW_BUFFERi. It could be COLOR_ATTACHMENT0, FRONT, BACK, 276bf215546Sopenharmony_ci * etc. 277bf215546Sopenharmony_ci */ 278bf215546Sopenharmony_ci if (drawbuffer < 0 || drawbuffer >= (GLint)ctx->Const.MaxDrawBuffers) { 279bf215546Sopenharmony_ci return INVALID_MASK; 280bf215546Sopenharmony_ci } 281bf215546Sopenharmony_ci 282bf215546Sopenharmony_ci switch (ctx->DrawBuffer->ColorDrawBuffer[drawbuffer]) { 283bf215546Sopenharmony_ci case GL_FRONT: 284bf215546Sopenharmony_ci if (att[BUFFER_FRONT_LEFT].Renderbuffer) 285bf215546Sopenharmony_ci mask |= BUFFER_BIT_FRONT_LEFT; 286bf215546Sopenharmony_ci if (att[BUFFER_FRONT_RIGHT].Renderbuffer) 287bf215546Sopenharmony_ci mask |= BUFFER_BIT_FRONT_RIGHT; 288bf215546Sopenharmony_ci break; 289bf215546Sopenharmony_ci case GL_BACK: 290bf215546Sopenharmony_ci /* For GLES contexts with a single buffered configuration, we actually 291bf215546Sopenharmony_ci * only have a front renderbuffer, so any clear calls to GL_BACK should 292bf215546Sopenharmony_ci * affect that buffer. See draw_buffer_enum_to_bitmask for details. 293bf215546Sopenharmony_ci */ 294bf215546Sopenharmony_ci if (_mesa_is_gles(ctx)) 295bf215546Sopenharmony_ci if (!ctx->DrawBuffer->Visual.doubleBufferMode) 296bf215546Sopenharmony_ci if (att[BUFFER_FRONT_LEFT].Renderbuffer) 297bf215546Sopenharmony_ci mask |= BUFFER_BIT_FRONT_LEFT; 298bf215546Sopenharmony_ci if (att[BUFFER_BACK_LEFT].Renderbuffer) 299bf215546Sopenharmony_ci mask |= BUFFER_BIT_BACK_LEFT; 300bf215546Sopenharmony_ci if (att[BUFFER_BACK_RIGHT].Renderbuffer) 301bf215546Sopenharmony_ci mask |= BUFFER_BIT_BACK_RIGHT; 302bf215546Sopenharmony_ci break; 303bf215546Sopenharmony_ci case GL_LEFT: 304bf215546Sopenharmony_ci if (att[BUFFER_FRONT_LEFT].Renderbuffer) 305bf215546Sopenharmony_ci mask |= BUFFER_BIT_FRONT_LEFT; 306bf215546Sopenharmony_ci if (att[BUFFER_BACK_LEFT].Renderbuffer) 307bf215546Sopenharmony_ci mask |= BUFFER_BIT_BACK_LEFT; 308bf215546Sopenharmony_ci break; 309bf215546Sopenharmony_ci case GL_RIGHT: 310bf215546Sopenharmony_ci if (att[BUFFER_FRONT_RIGHT].Renderbuffer) 311bf215546Sopenharmony_ci mask |= BUFFER_BIT_FRONT_RIGHT; 312bf215546Sopenharmony_ci if (att[BUFFER_BACK_RIGHT].Renderbuffer) 313bf215546Sopenharmony_ci mask |= BUFFER_BIT_BACK_RIGHT; 314bf215546Sopenharmony_ci break; 315bf215546Sopenharmony_ci case GL_FRONT_AND_BACK: 316bf215546Sopenharmony_ci if (att[BUFFER_FRONT_LEFT].Renderbuffer) 317bf215546Sopenharmony_ci mask |= BUFFER_BIT_FRONT_LEFT; 318bf215546Sopenharmony_ci if (att[BUFFER_BACK_LEFT].Renderbuffer) 319bf215546Sopenharmony_ci mask |= BUFFER_BIT_BACK_LEFT; 320bf215546Sopenharmony_ci if (att[BUFFER_FRONT_RIGHT].Renderbuffer) 321bf215546Sopenharmony_ci mask |= BUFFER_BIT_FRONT_RIGHT; 322bf215546Sopenharmony_ci if (att[BUFFER_BACK_RIGHT].Renderbuffer) 323bf215546Sopenharmony_ci mask |= BUFFER_BIT_BACK_RIGHT; 324bf215546Sopenharmony_ci break; 325bf215546Sopenharmony_ci default: 326bf215546Sopenharmony_ci { 327bf215546Sopenharmony_ci gl_buffer_index buf = 328bf215546Sopenharmony_ci ctx->DrawBuffer->_ColorDrawBufferIndexes[drawbuffer]; 329bf215546Sopenharmony_ci 330bf215546Sopenharmony_ci if (buf != BUFFER_NONE && att[buf].Renderbuffer) { 331bf215546Sopenharmony_ci mask |= 1 << buf; 332bf215546Sopenharmony_ci } 333bf215546Sopenharmony_ci } 334bf215546Sopenharmony_ci } 335bf215546Sopenharmony_ci 336bf215546Sopenharmony_ci return mask; 337bf215546Sopenharmony_ci} 338bf215546Sopenharmony_ci 339bf215546Sopenharmony_ci 340bf215546Sopenharmony_ci 341bf215546Sopenharmony_ci/** 342bf215546Sopenharmony_ci * New in GL 3.0 343bf215546Sopenharmony_ci * Clear signed integer color buffer or stencil buffer (not depth). 344bf215546Sopenharmony_ci */ 345bf215546Sopenharmony_cistatic ALWAYS_INLINE void 346bf215546Sopenharmony_ciclear_bufferiv(struct gl_context *ctx, GLenum buffer, GLint drawbuffer, 347bf215546Sopenharmony_ci const GLint *value, bool no_error) 348bf215546Sopenharmony_ci{ 349bf215546Sopenharmony_ci FLUSH_VERTICES(ctx, 0, 0); 350bf215546Sopenharmony_ci 351bf215546Sopenharmony_ci if (ctx->NewState) { 352bf215546Sopenharmony_ci _mesa_update_state( ctx ); 353bf215546Sopenharmony_ci } 354bf215546Sopenharmony_ci 355bf215546Sopenharmony_ci if (!no_error && ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) { 356bf215546Sopenharmony_ci _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT, 357bf215546Sopenharmony_ci "glClearBufferiv(incomplete framebuffer)"); 358bf215546Sopenharmony_ci return; 359bf215546Sopenharmony_ci } 360bf215546Sopenharmony_ci 361bf215546Sopenharmony_ci switch (buffer) { 362bf215546Sopenharmony_ci case GL_STENCIL: 363bf215546Sopenharmony_ci /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says: 364bf215546Sopenharmony_ci * 365bf215546Sopenharmony_ci * "ClearBuffer generates an INVALID VALUE error if buffer is 366bf215546Sopenharmony_ci * COLOR and drawbuffer is less than zero, or greater than the 367bf215546Sopenharmony_ci * value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH, 368bf215546Sopenharmony_ci * STENCIL, or DEPTH STENCIL and drawbuffer is not zero." 369bf215546Sopenharmony_ci */ 370bf215546Sopenharmony_ci if (!no_error && drawbuffer != 0) { 371bf215546Sopenharmony_ci _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferiv(drawbuffer=%d)", 372bf215546Sopenharmony_ci drawbuffer); 373bf215546Sopenharmony_ci return; 374bf215546Sopenharmony_ci } 375bf215546Sopenharmony_ci else if (ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer 376bf215546Sopenharmony_ci && !ctx->RasterDiscard) { 377bf215546Sopenharmony_ci /* Save current stencil clear value, set to 'value', do the 378bf215546Sopenharmony_ci * stencil clear and restore the clear value. 379bf215546Sopenharmony_ci * XXX in the future we may have a new st_ClearBuffer() 380bf215546Sopenharmony_ci * hook instead. 381bf215546Sopenharmony_ci */ 382bf215546Sopenharmony_ci const GLuint clearSave = ctx->Stencil.Clear; 383bf215546Sopenharmony_ci ctx->Stencil.Clear = *value; 384bf215546Sopenharmony_ci st_Clear(ctx, BUFFER_BIT_STENCIL); 385bf215546Sopenharmony_ci ctx->Stencil.Clear = clearSave; 386bf215546Sopenharmony_ci } 387bf215546Sopenharmony_ci break; 388bf215546Sopenharmony_ci case GL_COLOR: 389bf215546Sopenharmony_ci { 390bf215546Sopenharmony_ci const GLbitfield mask = make_color_buffer_mask(ctx, drawbuffer); 391bf215546Sopenharmony_ci if (!no_error && mask == INVALID_MASK) { 392bf215546Sopenharmony_ci _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferiv(drawbuffer=%d)", 393bf215546Sopenharmony_ci drawbuffer); 394bf215546Sopenharmony_ci return; 395bf215546Sopenharmony_ci } 396bf215546Sopenharmony_ci else if (mask && !ctx->RasterDiscard) { 397bf215546Sopenharmony_ci union gl_color_union clearSave; 398bf215546Sopenharmony_ci 399bf215546Sopenharmony_ci /* save color */ 400bf215546Sopenharmony_ci clearSave = ctx->Color.ClearColor; 401bf215546Sopenharmony_ci /* set color */ 402bf215546Sopenharmony_ci COPY_4V(ctx->Color.ClearColor.i, value); 403bf215546Sopenharmony_ci /* clear buffer(s) */ 404bf215546Sopenharmony_ci st_Clear(ctx, mask); 405bf215546Sopenharmony_ci /* restore color */ 406bf215546Sopenharmony_ci ctx->Color.ClearColor = clearSave; 407bf215546Sopenharmony_ci } 408bf215546Sopenharmony_ci } 409bf215546Sopenharmony_ci break; 410bf215546Sopenharmony_ci default: 411bf215546Sopenharmony_ci if (!no_error) { 412bf215546Sopenharmony_ci /* Page 498 of the PDF, section '17.4.3.1 Clearing Individual Buffers' 413bf215546Sopenharmony_ci * of the OpenGL 4.5 spec states: 414bf215546Sopenharmony_ci * 415bf215546Sopenharmony_ci * "An INVALID_ENUM error is generated by ClearBufferiv and 416bf215546Sopenharmony_ci * ClearNamedFramebufferiv if buffer is not COLOR or STENCIL." 417bf215546Sopenharmony_ci */ 418bf215546Sopenharmony_ci _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferiv(buffer=%s)", 419bf215546Sopenharmony_ci _mesa_enum_to_string(buffer)); 420bf215546Sopenharmony_ci } 421bf215546Sopenharmony_ci return; 422bf215546Sopenharmony_ci } 423bf215546Sopenharmony_ci} 424bf215546Sopenharmony_ci 425bf215546Sopenharmony_ci 426bf215546Sopenharmony_civoid GLAPIENTRY 427bf215546Sopenharmony_ci_mesa_ClearBufferiv_no_error(GLenum buffer, GLint drawbuffer, const GLint *value) 428bf215546Sopenharmony_ci{ 429bf215546Sopenharmony_ci GET_CURRENT_CONTEXT(ctx); 430bf215546Sopenharmony_ci clear_bufferiv(ctx, buffer, drawbuffer, value, true); 431bf215546Sopenharmony_ci} 432bf215546Sopenharmony_ci 433bf215546Sopenharmony_ci 434bf215546Sopenharmony_civoid GLAPIENTRY 435bf215546Sopenharmony_ci_mesa_ClearBufferiv(GLenum buffer, GLint drawbuffer, const GLint *value) 436bf215546Sopenharmony_ci{ 437bf215546Sopenharmony_ci GET_CURRENT_CONTEXT(ctx); 438bf215546Sopenharmony_ci clear_bufferiv(ctx, buffer, drawbuffer, value, false); 439bf215546Sopenharmony_ci} 440bf215546Sopenharmony_ci 441bf215546Sopenharmony_ci 442bf215546Sopenharmony_ci/** 443bf215546Sopenharmony_ci * The ClearBuffer framework is so complicated and so riddled with the 444bf215546Sopenharmony_ci * assumption that the framebuffer is bound that, for now, we will just fake 445bf215546Sopenharmony_ci * direct state access clearing for the user. 446bf215546Sopenharmony_ci */ 447bf215546Sopenharmony_civoid GLAPIENTRY 448bf215546Sopenharmony_ci_mesa_ClearNamedFramebufferiv(GLuint framebuffer, GLenum buffer, 449bf215546Sopenharmony_ci GLint drawbuffer, const GLint *value) 450bf215546Sopenharmony_ci{ 451bf215546Sopenharmony_ci GLint oldfb; 452bf215546Sopenharmony_ci 453bf215546Sopenharmony_ci _mesa_GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfb); 454bf215546Sopenharmony_ci _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer); 455bf215546Sopenharmony_ci _mesa_ClearBufferiv(buffer, drawbuffer, value); 456bf215546Sopenharmony_ci _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, (GLuint) oldfb); 457bf215546Sopenharmony_ci} 458bf215546Sopenharmony_ci 459bf215546Sopenharmony_ci 460bf215546Sopenharmony_ci/** 461bf215546Sopenharmony_ci * New in GL 3.0 462bf215546Sopenharmony_ci * Clear unsigned integer color buffer (not depth, not stencil). 463bf215546Sopenharmony_ci */ 464bf215546Sopenharmony_cistatic ALWAYS_INLINE void 465bf215546Sopenharmony_ciclear_bufferuiv(struct gl_context *ctx, GLenum buffer, GLint drawbuffer, 466bf215546Sopenharmony_ci const GLuint *value, bool no_error) 467bf215546Sopenharmony_ci{ 468bf215546Sopenharmony_ci FLUSH_VERTICES(ctx, 0, 0); 469bf215546Sopenharmony_ci 470bf215546Sopenharmony_ci if (ctx->NewState) { 471bf215546Sopenharmony_ci _mesa_update_state( ctx ); 472bf215546Sopenharmony_ci } 473bf215546Sopenharmony_ci 474bf215546Sopenharmony_ci if (!no_error && ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE) { 475bf215546Sopenharmony_ci _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION, 476bf215546Sopenharmony_ci "glClearBufferuiv(incomplete framebuffer)"); 477bf215546Sopenharmony_ci return; 478bf215546Sopenharmony_ci } 479bf215546Sopenharmony_ci 480bf215546Sopenharmony_ci switch (buffer) { 481bf215546Sopenharmony_ci case GL_COLOR: 482bf215546Sopenharmony_ci { 483bf215546Sopenharmony_ci const GLbitfield mask = make_color_buffer_mask(ctx, drawbuffer); 484bf215546Sopenharmony_ci if (!no_error && mask == INVALID_MASK) { 485bf215546Sopenharmony_ci _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferuiv(drawbuffer=%d)", 486bf215546Sopenharmony_ci drawbuffer); 487bf215546Sopenharmony_ci return; 488bf215546Sopenharmony_ci } 489bf215546Sopenharmony_ci else if (mask && !ctx->RasterDiscard) { 490bf215546Sopenharmony_ci union gl_color_union clearSave; 491bf215546Sopenharmony_ci 492bf215546Sopenharmony_ci /* save color */ 493bf215546Sopenharmony_ci clearSave = ctx->Color.ClearColor; 494bf215546Sopenharmony_ci /* set color */ 495bf215546Sopenharmony_ci COPY_4V(ctx->Color.ClearColor.ui, value); 496bf215546Sopenharmony_ci /* clear buffer(s) */ 497bf215546Sopenharmony_ci st_Clear(ctx, mask); 498bf215546Sopenharmony_ci /* restore color */ 499bf215546Sopenharmony_ci ctx->Color.ClearColor = clearSave; 500bf215546Sopenharmony_ci } 501bf215546Sopenharmony_ci } 502bf215546Sopenharmony_ci break; 503bf215546Sopenharmony_ci default: 504bf215546Sopenharmony_ci if (!no_error) { 505bf215546Sopenharmony_ci /* Page 498 of the PDF, section '17.4.3.1 Clearing Individual Buffers' 506bf215546Sopenharmony_ci * of the OpenGL 4.5 spec states: 507bf215546Sopenharmony_ci * 508bf215546Sopenharmony_ci * "An INVALID_ENUM error is generated by ClearBufferuiv and 509bf215546Sopenharmony_ci * ClearNamedFramebufferuiv if buffer is not COLOR." 510bf215546Sopenharmony_ci */ 511bf215546Sopenharmony_ci _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferuiv(buffer=%s)", 512bf215546Sopenharmony_ci _mesa_enum_to_string(buffer)); 513bf215546Sopenharmony_ci } 514bf215546Sopenharmony_ci return; 515bf215546Sopenharmony_ci } 516bf215546Sopenharmony_ci} 517bf215546Sopenharmony_ci 518bf215546Sopenharmony_ci 519bf215546Sopenharmony_civoid GLAPIENTRY 520bf215546Sopenharmony_ci_mesa_ClearBufferuiv_no_error(GLenum buffer, GLint drawbuffer, 521bf215546Sopenharmony_ci const GLuint *value) 522bf215546Sopenharmony_ci{ 523bf215546Sopenharmony_ci GET_CURRENT_CONTEXT(ctx); 524bf215546Sopenharmony_ci clear_bufferuiv(ctx, buffer, drawbuffer, value, true); 525bf215546Sopenharmony_ci} 526bf215546Sopenharmony_ci 527bf215546Sopenharmony_ci 528bf215546Sopenharmony_civoid GLAPIENTRY 529bf215546Sopenharmony_ci_mesa_ClearBufferuiv(GLenum buffer, GLint drawbuffer, const GLuint *value) 530bf215546Sopenharmony_ci{ 531bf215546Sopenharmony_ci GET_CURRENT_CONTEXT(ctx); 532bf215546Sopenharmony_ci clear_bufferuiv(ctx, buffer, drawbuffer, value, false); 533bf215546Sopenharmony_ci} 534bf215546Sopenharmony_ci 535bf215546Sopenharmony_ci 536bf215546Sopenharmony_ci/** 537bf215546Sopenharmony_ci * The ClearBuffer framework is so complicated and so riddled with the 538bf215546Sopenharmony_ci * assumption that the framebuffer is bound that, for now, we will just fake 539bf215546Sopenharmony_ci * direct state access clearing for the user. 540bf215546Sopenharmony_ci */ 541bf215546Sopenharmony_civoid GLAPIENTRY 542bf215546Sopenharmony_ci_mesa_ClearNamedFramebufferuiv(GLuint framebuffer, GLenum buffer, 543bf215546Sopenharmony_ci GLint drawbuffer, const GLuint *value) 544bf215546Sopenharmony_ci{ 545bf215546Sopenharmony_ci GLint oldfb; 546bf215546Sopenharmony_ci 547bf215546Sopenharmony_ci _mesa_GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfb); 548bf215546Sopenharmony_ci _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer); 549bf215546Sopenharmony_ci _mesa_ClearBufferuiv(buffer, drawbuffer, value); 550bf215546Sopenharmony_ci _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, (GLuint) oldfb); 551bf215546Sopenharmony_ci} 552bf215546Sopenharmony_ci 553bf215546Sopenharmony_ci 554bf215546Sopenharmony_ci/** 555bf215546Sopenharmony_ci * New in GL 3.0 556bf215546Sopenharmony_ci * Clear fixed-pt or float color buffer or depth buffer (not stencil). 557bf215546Sopenharmony_ci */ 558bf215546Sopenharmony_cistatic ALWAYS_INLINE void 559bf215546Sopenharmony_ciclear_bufferfv(struct gl_context *ctx, GLenum buffer, GLint drawbuffer, 560bf215546Sopenharmony_ci const GLfloat *value, bool no_error) 561bf215546Sopenharmony_ci{ 562bf215546Sopenharmony_ci FLUSH_VERTICES(ctx, 0, 0); 563bf215546Sopenharmony_ci 564bf215546Sopenharmony_ci if (ctx->NewState) { 565bf215546Sopenharmony_ci _mesa_update_state( ctx ); 566bf215546Sopenharmony_ci } 567bf215546Sopenharmony_ci 568bf215546Sopenharmony_ci if (!no_error && ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE) { 569bf215546Sopenharmony_ci _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION, 570bf215546Sopenharmony_ci "glClearBufferfv(incomplete framebuffer)"); 571bf215546Sopenharmony_ci return; 572bf215546Sopenharmony_ci } 573bf215546Sopenharmony_ci 574bf215546Sopenharmony_ci switch (buffer) { 575bf215546Sopenharmony_ci case GL_DEPTH: 576bf215546Sopenharmony_ci /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says: 577bf215546Sopenharmony_ci * 578bf215546Sopenharmony_ci * "ClearBuffer generates an INVALID VALUE error if buffer is 579bf215546Sopenharmony_ci * COLOR and drawbuffer is less than zero, or greater than the 580bf215546Sopenharmony_ci * value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH, 581bf215546Sopenharmony_ci * STENCIL, or DEPTH STENCIL and drawbuffer is not zero." 582bf215546Sopenharmony_ci */ 583bf215546Sopenharmony_ci if (!no_error && drawbuffer != 0) { 584bf215546Sopenharmony_ci _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferfv(drawbuffer=%d)", 585bf215546Sopenharmony_ci drawbuffer); 586bf215546Sopenharmony_ci return; 587bf215546Sopenharmony_ci } 588bf215546Sopenharmony_ci else if (ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer 589bf215546Sopenharmony_ci && !ctx->RasterDiscard) { 590bf215546Sopenharmony_ci /* Save current depth clear value, set to 'value', do the 591bf215546Sopenharmony_ci * depth clear and restore the clear value. 592bf215546Sopenharmony_ci * XXX in the future we may have a new st_ClearBuffer() 593bf215546Sopenharmony_ci * hook instead. 594bf215546Sopenharmony_ci */ 595bf215546Sopenharmony_ci const GLclampd clearSave = ctx->Depth.Clear; 596bf215546Sopenharmony_ci 597bf215546Sopenharmony_ci /* Page 263 (page 279 of the PDF) of the OpenGL 3.0 spec says: 598bf215546Sopenharmony_ci * 599bf215546Sopenharmony_ci * "If buffer is DEPTH, drawbuffer must be zero, and value points 600bf215546Sopenharmony_ci * to the single depth value to clear the depth buffer to. 601bf215546Sopenharmony_ci * Clamping and type conversion for fixed-point depth buffers are 602bf215546Sopenharmony_ci * performed in the same fashion as for ClearDepth." 603bf215546Sopenharmony_ci */ 604bf215546Sopenharmony_ci const struct gl_renderbuffer *rb = 605bf215546Sopenharmony_ci ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer; 606bf215546Sopenharmony_ci const bool is_float_depth = 607bf215546Sopenharmony_ci _mesa_has_depth_float_channel(rb->InternalFormat); 608bf215546Sopenharmony_ci ctx->Depth.Clear = is_float_depth ? *value : SATURATE(*value); 609bf215546Sopenharmony_ci 610bf215546Sopenharmony_ci st_Clear(ctx, BUFFER_BIT_DEPTH); 611bf215546Sopenharmony_ci ctx->Depth.Clear = clearSave; 612bf215546Sopenharmony_ci } 613bf215546Sopenharmony_ci /* clear depth buffer to value */ 614bf215546Sopenharmony_ci break; 615bf215546Sopenharmony_ci case GL_COLOR: 616bf215546Sopenharmony_ci { 617bf215546Sopenharmony_ci const GLbitfield mask = make_color_buffer_mask(ctx, drawbuffer); 618bf215546Sopenharmony_ci if (!no_error && mask == INVALID_MASK) { 619bf215546Sopenharmony_ci _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferfv(drawbuffer=%d)", 620bf215546Sopenharmony_ci drawbuffer); 621bf215546Sopenharmony_ci return; 622bf215546Sopenharmony_ci } 623bf215546Sopenharmony_ci else if (mask && !ctx->RasterDiscard) { 624bf215546Sopenharmony_ci union gl_color_union clearSave; 625bf215546Sopenharmony_ci 626bf215546Sopenharmony_ci /* save color */ 627bf215546Sopenharmony_ci clearSave = ctx->Color.ClearColor; 628bf215546Sopenharmony_ci /* set color */ 629bf215546Sopenharmony_ci COPY_4V(ctx->Color.ClearColor.f, value); 630bf215546Sopenharmony_ci /* clear buffer(s) */ 631bf215546Sopenharmony_ci st_Clear(ctx, mask); 632bf215546Sopenharmony_ci /* restore color */ 633bf215546Sopenharmony_ci ctx->Color.ClearColor = clearSave; 634bf215546Sopenharmony_ci } 635bf215546Sopenharmony_ci } 636bf215546Sopenharmony_ci break; 637bf215546Sopenharmony_ci default: 638bf215546Sopenharmony_ci if (!no_error) { 639bf215546Sopenharmony_ci /* Page 498 of the PDF, section '17.4.3.1 Clearing Individual Buffers' 640bf215546Sopenharmony_ci * of the OpenGL 4.5 spec states: 641bf215546Sopenharmony_ci * 642bf215546Sopenharmony_ci * "An INVALID_ENUM error is generated by ClearBufferfv and 643bf215546Sopenharmony_ci * ClearNamedFramebufferfv if buffer is not COLOR or DEPTH." 644bf215546Sopenharmony_ci */ 645bf215546Sopenharmony_ci _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferfv(buffer=%s)", 646bf215546Sopenharmony_ci _mesa_enum_to_string(buffer)); 647bf215546Sopenharmony_ci } 648bf215546Sopenharmony_ci return; 649bf215546Sopenharmony_ci } 650bf215546Sopenharmony_ci} 651bf215546Sopenharmony_ci 652bf215546Sopenharmony_ci 653bf215546Sopenharmony_civoid GLAPIENTRY 654bf215546Sopenharmony_ci_mesa_ClearBufferfv_no_error(GLenum buffer, GLint drawbuffer, 655bf215546Sopenharmony_ci const GLfloat *value) 656bf215546Sopenharmony_ci{ 657bf215546Sopenharmony_ci GET_CURRENT_CONTEXT(ctx); 658bf215546Sopenharmony_ci clear_bufferfv(ctx, buffer, drawbuffer, value, true); 659bf215546Sopenharmony_ci} 660bf215546Sopenharmony_ci 661bf215546Sopenharmony_ci 662bf215546Sopenharmony_civoid GLAPIENTRY 663bf215546Sopenharmony_ci_mesa_ClearBufferfv(GLenum buffer, GLint drawbuffer, const GLfloat *value) 664bf215546Sopenharmony_ci{ 665bf215546Sopenharmony_ci GET_CURRENT_CONTEXT(ctx); 666bf215546Sopenharmony_ci clear_bufferfv(ctx, buffer, drawbuffer, value, false); 667bf215546Sopenharmony_ci} 668bf215546Sopenharmony_ci 669bf215546Sopenharmony_ci 670bf215546Sopenharmony_ci/** 671bf215546Sopenharmony_ci * The ClearBuffer framework is so complicated and so riddled with the 672bf215546Sopenharmony_ci * assumption that the framebuffer is bound that, for now, we will just fake 673bf215546Sopenharmony_ci * direct state access clearing for the user. 674bf215546Sopenharmony_ci */ 675bf215546Sopenharmony_civoid GLAPIENTRY 676bf215546Sopenharmony_ci_mesa_ClearNamedFramebufferfv(GLuint framebuffer, GLenum buffer, 677bf215546Sopenharmony_ci GLint drawbuffer, const GLfloat *value) 678bf215546Sopenharmony_ci{ 679bf215546Sopenharmony_ci GLint oldfb; 680bf215546Sopenharmony_ci 681bf215546Sopenharmony_ci _mesa_GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfb); 682bf215546Sopenharmony_ci _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer); 683bf215546Sopenharmony_ci _mesa_ClearBufferfv(buffer, drawbuffer, value); 684bf215546Sopenharmony_ci _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, (GLuint) oldfb); 685bf215546Sopenharmony_ci} 686bf215546Sopenharmony_ci 687bf215546Sopenharmony_ci 688bf215546Sopenharmony_ci/** 689bf215546Sopenharmony_ci * New in GL 3.0 690bf215546Sopenharmony_ci * Clear depth/stencil buffer only. 691bf215546Sopenharmony_ci */ 692bf215546Sopenharmony_cistatic ALWAYS_INLINE void 693bf215546Sopenharmony_ciclear_bufferfi(struct gl_context *ctx, GLenum buffer, GLint drawbuffer, 694bf215546Sopenharmony_ci GLfloat depth, GLint stencil, bool no_error) 695bf215546Sopenharmony_ci{ 696bf215546Sopenharmony_ci GLbitfield mask = 0; 697bf215546Sopenharmony_ci 698bf215546Sopenharmony_ci FLUSH_VERTICES(ctx, 0, 0); 699bf215546Sopenharmony_ci 700bf215546Sopenharmony_ci if (!no_error) { 701bf215546Sopenharmony_ci if (buffer != GL_DEPTH_STENCIL) { 702bf215546Sopenharmony_ci _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferfi(buffer=%s)", 703bf215546Sopenharmony_ci _mesa_enum_to_string(buffer)); 704bf215546Sopenharmony_ci return; 705bf215546Sopenharmony_ci } 706bf215546Sopenharmony_ci 707bf215546Sopenharmony_ci /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says: 708bf215546Sopenharmony_ci * 709bf215546Sopenharmony_ci * "ClearBuffer generates an INVALID VALUE error if buffer is 710bf215546Sopenharmony_ci * COLOR and drawbuffer is less than zero, or greater than the 711bf215546Sopenharmony_ci * value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH, 712bf215546Sopenharmony_ci * STENCIL, or DEPTH STENCIL and drawbuffer is not zero." 713bf215546Sopenharmony_ci */ 714bf215546Sopenharmony_ci if (drawbuffer != 0) { 715bf215546Sopenharmony_ci _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferfi(drawbuffer=%d)", 716bf215546Sopenharmony_ci drawbuffer); 717bf215546Sopenharmony_ci return; 718bf215546Sopenharmony_ci } 719bf215546Sopenharmony_ci } 720bf215546Sopenharmony_ci 721bf215546Sopenharmony_ci if (ctx->RasterDiscard) 722bf215546Sopenharmony_ci return; 723bf215546Sopenharmony_ci 724bf215546Sopenharmony_ci if (ctx->NewState) { 725bf215546Sopenharmony_ci _mesa_update_state( ctx ); 726bf215546Sopenharmony_ci } 727bf215546Sopenharmony_ci 728bf215546Sopenharmony_ci if (!no_error && ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) { 729bf215546Sopenharmony_ci _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT, 730bf215546Sopenharmony_ci "glClearBufferfi(incomplete framebuffer)"); 731bf215546Sopenharmony_ci return; 732bf215546Sopenharmony_ci } 733bf215546Sopenharmony_ci 734bf215546Sopenharmony_ci if (ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer) 735bf215546Sopenharmony_ci mask |= BUFFER_BIT_DEPTH; 736bf215546Sopenharmony_ci if (ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer) 737bf215546Sopenharmony_ci mask |= BUFFER_BIT_STENCIL; 738bf215546Sopenharmony_ci 739bf215546Sopenharmony_ci if (mask) { 740bf215546Sopenharmony_ci /* save current clear values */ 741bf215546Sopenharmony_ci const GLclampd clearDepthSave = ctx->Depth.Clear; 742bf215546Sopenharmony_ci const GLuint clearStencilSave = ctx->Stencil.Clear; 743bf215546Sopenharmony_ci 744bf215546Sopenharmony_ci /* set new clear values 745bf215546Sopenharmony_ci * 746bf215546Sopenharmony_ci * Page 263 (page 279 of the PDF) of the OpenGL 3.0 spec says: 747bf215546Sopenharmony_ci * 748bf215546Sopenharmony_ci * "depth and stencil are the values to clear the depth and stencil 749bf215546Sopenharmony_ci * buffers to, respectively. Clamping and type conversion for 750bf215546Sopenharmony_ci * fixed-point depth buffers are performed in the same fashion as 751bf215546Sopenharmony_ci * for ClearDepth." 752bf215546Sopenharmony_ci */ 753bf215546Sopenharmony_ci const struct gl_renderbuffer *rb = 754bf215546Sopenharmony_ci ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer; 755bf215546Sopenharmony_ci const bool has_float_depth = rb && 756bf215546Sopenharmony_ci _mesa_has_depth_float_channel(rb->InternalFormat); 757bf215546Sopenharmony_ci ctx->Depth.Clear = has_float_depth ? depth : SATURATE(depth); 758bf215546Sopenharmony_ci ctx->Stencil.Clear = stencil; 759bf215546Sopenharmony_ci 760bf215546Sopenharmony_ci /* clear buffers */ 761bf215546Sopenharmony_ci st_Clear(ctx, mask); 762bf215546Sopenharmony_ci 763bf215546Sopenharmony_ci /* restore */ 764bf215546Sopenharmony_ci ctx->Depth.Clear = clearDepthSave; 765bf215546Sopenharmony_ci ctx->Stencil.Clear = clearStencilSave; 766bf215546Sopenharmony_ci } 767bf215546Sopenharmony_ci} 768bf215546Sopenharmony_ci 769bf215546Sopenharmony_ci 770bf215546Sopenharmony_civoid GLAPIENTRY 771bf215546Sopenharmony_ci_mesa_ClearBufferfi_no_error(GLenum buffer, GLint drawbuffer, 772bf215546Sopenharmony_ci GLfloat depth, GLint stencil) 773bf215546Sopenharmony_ci{ 774bf215546Sopenharmony_ci GET_CURRENT_CONTEXT(ctx); 775bf215546Sopenharmony_ci clear_bufferfi(ctx, buffer, drawbuffer, depth, stencil, true); 776bf215546Sopenharmony_ci} 777bf215546Sopenharmony_ci 778bf215546Sopenharmony_ci 779bf215546Sopenharmony_civoid GLAPIENTRY 780bf215546Sopenharmony_ci_mesa_ClearBufferfi(GLenum buffer, GLint drawbuffer, 781bf215546Sopenharmony_ci GLfloat depth, GLint stencil) 782bf215546Sopenharmony_ci{ 783bf215546Sopenharmony_ci GET_CURRENT_CONTEXT(ctx); 784bf215546Sopenharmony_ci clear_bufferfi(ctx, buffer, drawbuffer, depth, stencil, false); 785bf215546Sopenharmony_ci} 786bf215546Sopenharmony_ci 787bf215546Sopenharmony_ci 788bf215546Sopenharmony_ci/** 789bf215546Sopenharmony_ci * The ClearBuffer framework is so complicated and so riddled with the 790bf215546Sopenharmony_ci * assumption that the framebuffer is bound that, for now, we will just fake 791bf215546Sopenharmony_ci * direct state access clearing for the user. 792bf215546Sopenharmony_ci */ 793bf215546Sopenharmony_civoid GLAPIENTRY 794bf215546Sopenharmony_ci_mesa_ClearNamedFramebufferfi(GLuint framebuffer, GLenum buffer, 795bf215546Sopenharmony_ci GLint drawbuffer, GLfloat depth, GLint stencil) 796bf215546Sopenharmony_ci{ 797bf215546Sopenharmony_ci GLint oldfb; 798bf215546Sopenharmony_ci 799bf215546Sopenharmony_ci _mesa_GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfb); 800bf215546Sopenharmony_ci _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer); 801bf215546Sopenharmony_ci _mesa_ClearBufferfi(buffer, drawbuffer, depth, stencil); 802bf215546Sopenharmony_ci _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, (GLuint) oldfb); 803bf215546Sopenharmony_ci} 804