blob: 9221c7287a33a52e2314bbe06882319d5ea3a561 [file] [log] [blame]
Brian Paul5ab1d0a2008-06-09 15:01:02 -06001/*
2 * Mesa 3-D graphics library
Brian Paul5ab1d0a2008-06-09 15:01:02 -06003 *
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
Kenneth Graunke3d8d5b22013-04-21 13:46:48 -070019 * 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.
Brian Paul5ab1d0a2008-06-09 15:01:02 -060023 */
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"
Brian Paul2b5ece52009-12-30 09:25:24 -070036#include "enums.h"
Laura Ekstrand6236c472015-02-05 13:24:43 -080037#include "fbobject.h"
38#include "get.h"
Vinson Lee3fdd9fa2010-07-30 00:41:08 -070039#include "macros.h"
Vinson Lee0117da42011-01-05 23:11:54 -080040#include "mtypes.h"
Brian Paul5ab1d0a2008-06-09 15:01:02 -060041#include "state.h"
42
43
44
Brian Paul5ab1d0a2008-06-09 15:01:02 -060045void GLAPIENTRY
46_mesa_ClearIndex( GLfloat c )
47{
48 GET_CURRENT_CONTEXT(ctx);
Brian Paul5ab1d0a2008-06-09 15:01:02 -060049
Brian Paul5ab1d0a2008-06-09 15:01:02 -060050 ctx->Color.ClearIndex = (GLuint) c;
Brian Paul5ab1d0a2008-06-09 15:01:02 -060051}
Brian Paul5ab1d0a2008-06-09 15:01:02 -060052
53
54/**
55 * Specify the clear values for the color buffers.
56 *
57 * \param red red color component.
58 * \param green green color component.
59 * \param blue blue color component.
60 * \param alpha alpha component.
61 *
62 * \sa glClearColor().
Brian Paul5ab1d0a2008-06-09 15:01:02 -060063 */
64void GLAPIENTRY
65_mesa_ClearColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
66{
Brian Paul5ab1d0a2008-06-09 15:01:02 -060067 GET_CURRENT_CONTEXT(ctx);
Brian Paul5ab1d0a2008-06-09 15:01:02 -060068
Marek Olšák3140d132013-04-15 03:41:43 +020069 ctx->Color.ClearColor.f[0] = red;
70 ctx->Color.ClearColor.f[1] = green;
71 ctx->Color.ClearColor.f[2] = blue;
72 ctx->Color.ClearColor.f[3] = alpha;
Brian Paul5ab1d0a2008-06-09 15:01:02 -060073}
74
75
76/**
Brian Paula0bc8ee2010-10-23 09:26:10 -060077 * GL_EXT_texture_integer
78 */
79void GLAPIENTRY
80_mesa_ClearColorIiEXT(GLint r, GLint g, GLint b, GLint a)
81{
Brian Paula0bc8ee2010-10-23 09:26:10 -060082 GET_CURRENT_CONTEXT(ctx);
Brian Paula0bc8ee2010-10-23 09:26:10 -060083
Marek Olšák3140d132013-04-15 03:41:43 +020084 ctx->Color.ClearColor.i[0] = r;
85 ctx->Color.ClearColor.i[1] = g;
86 ctx->Color.ClearColor.i[2] = b;
87 ctx->Color.ClearColor.i[3] = a;
Brian Paula0bc8ee2010-10-23 09:26:10 -060088}
89
90
91/**
92 * GL_EXT_texture_integer
93 */
94void GLAPIENTRY
95_mesa_ClearColorIuiEXT(GLuint r, GLuint g, GLuint b, GLuint a)
96{
Brian Paula0bc8ee2010-10-23 09:26:10 -060097 GET_CURRENT_CONTEXT(ctx);
Brian Paula0bc8ee2010-10-23 09:26:10 -060098
Marek Olšák3140d132013-04-15 03:41:43 +020099 ctx->Color.ClearColor.ui[0] = r;
100 ctx->Color.ClearColor.ui[1] = g;
101 ctx->Color.ClearColor.ui[2] = b;
102 ctx->Color.ClearColor.ui[3] = a;
Brian Paula0bc8ee2010-10-23 09:26:10 -0600103}
104
105
106/**
Kenneth Graunke630bf282014-03-21 15:58:02 -0700107 * Returns true if color writes are enabled for the given color attachment.
108 *
109 * Beyond checking ColorMask, this uses _mesa_format_has_color_component to
110 * ignore components that don't actually exist in the format (such as X in
111 * XRGB).
112 */
113static bool
114color_buffer_writes_enabled(const struct gl_context *ctx, unsigned idx)
115{
116 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[idx];
117 GLuint c;
Kenneth Graunke630bf282014-03-21 15:58:02 -0700118
119 if (rb) {
120 for (c = 0; c < 4; c++) {
Marek Olšákaf3685d2018-01-31 03:03:25 +0100121 if (GET_COLORMASK_BIT(ctx->Color.ColorMask, idx, c) &&
Brian Paul6b601532017-04-27 08:52:30 -0600122 _mesa_format_has_color_component(rb->Format, c)) {
123 return true;
124 }
Kenneth Graunke630bf282014-03-21 15:58:02 -0700125 }
126 }
127
Brian Paul6b601532017-04-27 08:52:30 -0600128 return false;
Kenneth Graunke630bf282014-03-21 15:58:02 -0700129}
130
131
132/**
Brian Paul5ab1d0a2008-06-09 15:01:02 -0600133 * Clear buffers.
Brian Paule725dc02014-12-12 16:45:33 -0700134 *
Brian Paul5ab1d0a2008-06-09 15:01:02 -0600135 * \param mask bit-mask indicating the buffers to be cleared.
136 *
Brian Paule725dc02014-12-12 16:45:33 -0700137 * Flushes the vertices and verifies the parameter.
Jakob Bornecrantz09171702018-09-19 15:21:26 +0100138 * If __struct gl_contextRec::NewState is set then calls _mesa_update_state()
139 * to update gl_frame_buffer::_Xmin, etc. If the rasterization mode is set to
140 * GL_RENDER then requests the driver to clear the buffers, via the
141 * dd_function_table::Clear callback.
Brian Paule725dc02014-12-12 16:45:33 -0700142 */
Samuel Pitoiset34e8d0e2017-06-26 17:44:44 +0200143static ALWAYS_INLINE void
144clear(struct gl_context *ctx, GLbitfield mask, bool no_error)
Brian Paul5ab1d0a2008-06-09 15:01:02 -0600145{
Eric Anholta9754792013-01-16 16:20:38 -0800146 FLUSH_VERTICES(ctx, 0);
Brian Paul5ab1d0a2008-06-09 15:01:02 -0600147 FLUSH_CURRENT(ctx, 0);
148
Samuel Pitoiset34e8d0e2017-06-26 17:44:44 +0200149 if (!no_error) {
150 if (mask & ~(GL_COLOR_BUFFER_BIT |
151 GL_DEPTH_BUFFER_BIT |
152 GL_STENCIL_BUFFER_BIT |
153 GL_ACCUM_BUFFER_BIT)) {
154 _mesa_error( ctx, GL_INVALID_VALUE, "glClear(0x%x)", mask);
155 return;
156 }
Brian Paul5ab1d0a2008-06-09 15:01:02 -0600157
Samuel Pitoiset34e8d0e2017-06-26 17:44:44 +0200158 /* Accumulation buffers were removed in core contexts, and they never
159 * existed in OpenGL ES.
160 */
161 if ((mask & GL_ACCUM_BUFFER_BIT) != 0
162 && (ctx->API == API_OPENGL_CORE || _mesa_is_gles(ctx))) {
163 _mesa_error( ctx, GL_INVALID_VALUE, "glClear(GL_ACCUM_BUFFER_BIT)");
164 return;
165 }
Ian Romanicke58c19a2011-09-20 16:39:30 -0700166 }
167
Jakob Bornecrantz09171702018-09-19 15:21:26 +0100168 if (ctx->NewState) {
169 _mesa_update_state( ctx ); /* update _Xmin, etc */
Brian Paul5ab1d0a2008-06-09 15:01:02 -0600170 }
171
Samuel Pitoiset34e8d0e2017-06-26 17:44:44 +0200172 if (!no_error && ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
Brian Paul5ab1d0a2008-06-09 15:01:02 -0600173 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
174 "glClear(incomplete framebuffer)");
175 return;
176 }
177
Paul Berryaee96802011-12-20 16:18:39 -0800178 if (ctx->RasterDiscard)
Eric Anholte7c2b712011-09-29 23:13:44 -0700179 return;
180
Brian Paul5ab1d0a2008-06-09 15:01:02 -0600181 if (ctx->RenderMode == GL_RENDER) {
182 GLbitfield bufferMask;
183
184 /* don't clear depth buffer if depth writing disabled */
185 if (!ctx->Depth.Mask)
186 mask &= ~GL_DEPTH_BUFFER_BIT;
187
188 /* Build the bitmask to send to device driver's Clear function.
189 * Note that the GL_COLOR_BUFFER_BIT flag will expand to 0, 1, 2 or 4
190 * of the BUFFER_BIT_FRONT/BACK_LEFT/RIGHT flags, or one of the
191 * BUFFER_BIT_COLORn flags.
192 */
193 bufferMask = 0;
194 if (mask & GL_COLOR_BUFFER_BIT) {
195 GLuint i;
196 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
Brian Paulf8bae522017-11-08 22:18:40 -0700197 gl_buffer_index buf = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
Marek Olšák9bf95782014-01-08 01:23:43 +0100198
Brian Paulf8bae522017-11-08 22:18:40 -0700199 if (buf != BUFFER_NONE && color_buffer_writes_enabled(ctx, i)) {
Marek Olšák9bf95782014-01-08 01:23:43 +0100200 bufferMask |= 1 << buf;
201 }
Brian Paul5ab1d0a2008-06-09 15:01:02 -0600202 }
203 }
204
205 if ((mask & GL_DEPTH_BUFFER_BIT)
Adam Jackson78e0fa62019-09-06 11:51:23 -0400206 && ctx->DrawBuffer->Visual.depthBits > 0) {
Brian Paul5ab1d0a2008-06-09 15:01:02 -0600207 bufferMask |= BUFFER_BIT_DEPTH;
208 }
209
210 if ((mask & GL_STENCIL_BUFFER_BIT)
Adam Jackson78e0fa62019-09-06 11:51:23 -0400211 && ctx->DrawBuffer->Visual.stencilBits > 0) {
Brian Paul5ab1d0a2008-06-09 15:01:02 -0600212 bufferMask |= BUFFER_BIT_STENCIL;
213 }
214
215 if ((mask & GL_ACCUM_BUFFER_BIT)
Adam Jackson78e0fa62019-09-06 11:51:23 -0400216 && ctx->DrawBuffer->Visual.accumRedBits > 0) {
Brian Paul5ab1d0a2008-06-09 15:01:02 -0600217 bufferMask |= BUFFER_BIT_ACCUM;
218 }
219
Matt Turnerbfcdb842015-02-20 20:18:47 -0800220 assert(ctx->Driver.Clear);
Brian Paul5ab1d0a2008-06-09 15:01:02 -0600221 ctx->Driver.Clear(ctx, bufferMask);
222 }
223}
Brian Paul2b5ece52009-12-30 09:25:24 -0700224
225
Samuel Pitoiset34e8d0e2017-06-26 17:44:44 +0200226void GLAPIENTRY
Samuel Pitoisete529ade2017-06-26 17:46:39 +0200227_mesa_Clear_no_error(GLbitfield mask)
228{
229 GET_CURRENT_CONTEXT(ctx);
230 clear(ctx, mask, true);
231}
232
233
234void GLAPIENTRY
Samuel Pitoiset34e8d0e2017-06-26 17:44:44 +0200235_mesa_Clear(GLbitfield mask)
236{
237 GET_CURRENT_CONTEXT(ctx);
238
239 if (MESA_VERBOSE & VERBOSE_API)
240 _mesa_debug(ctx, "glClear 0x%x\n", mask);
241
242 clear(ctx, mask, false);
243}
244
245
Brian Paul2b5ece52009-12-30 09:25:24 -0700246/** Returned by make_color_buffer_mask() for errors */
Jan Vesely3cb10cc2015-01-15 13:41:04 -0500247#define INVALID_MASK ~0x0U
Brian Paul2b5ece52009-12-30 09:25:24 -0700248
249
250/**
251 * Convert the glClearBuffer 'drawbuffer' parameter into a bitmask of
252 * BUFFER_BIT_x values.
253 * Return INVALID_MASK if the drawbuffer value is invalid.
254 */
255static GLbitfield
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400256make_color_buffer_mask(struct gl_context *ctx, GLint drawbuffer)
Brian Paul2b5ece52009-12-30 09:25:24 -0700257{
258 const struct gl_renderbuffer_attachment *att = ctx->DrawBuffer->Attachment;
259 GLbitfield mask = 0x0;
260
Marek Olšák03d848e2013-12-04 00:27:20 +0100261 /* From the GL 4.0 specification:
262 * If buffer is COLOR, a particular draw buffer DRAW_BUFFERi is
263 * specified by passing i as the parameter drawbuffer, and value
264 * points to a four-element vector specifying the R, G, B, and A
265 * color to clear that draw buffer to. If the draw buffer is one
266 * of FRONT, BACK, LEFT, RIGHT, or FRONT_AND_BACK, identifying
267 * multiple buffers, each selected buffer is cleared to the same
268 * value.
269 *
270 * Note that "drawbuffer" and "draw buffer" have different meaning.
271 * "drawbuffer" specifies DRAW_BUFFERi, while "draw buffer" is what's
272 * assigned to DRAW_BUFFERi. It could be COLOR_ATTACHMENT0, FRONT, BACK,
273 * etc.
274 */
275 if (drawbuffer < 0 || drawbuffer >= (GLint)ctx->Const.MaxDrawBuffers) {
276 return INVALID_MASK;
277 }
278
279 switch (ctx->DrawBuffer->ColorDrawBuffer[drawbuffer]) {
Brian Paul2b5ece52009-12-30 09:25:24 -0700280 case GL_FRONT:
281 if (att[BUFFER_FRONT_LEFT].Renderbuffer)
282 mask |= BUFFER_BIT_FRONT_LEFT;
283 if (att[BUFFER_FRONT_RIGHT].Renderbuffer)
284 mask |= BUFFER_BIT_FRONT_RIGHT;
285 break;
286 case GL_BACK:
Gurchetan Singh2e6d3582016-06-30 15:20:34 -0700287 /* For GLES contexts with a single buffered configuration, we actually
288 * only have a front renderbuffer, so any clear calls to GL_BACK should
289 * affect that buffer. See draw_buffer_enum_to_bitmask for details.
290 */
291 if (_mesa_is_gles(ctx))
292 if (!ctx->DrawBuffer->Visual.doubleBufferMode)
293 if (att[BUFFER_FRONT_LEFT].Renderbuffer)
294 mask |= BUFFER_BIT_FRONT_LEFT;
Brian Paul2b5ece52009-12-30 09:25:24 -0700295 if (att[BUFFER_BACK_LEFT].Renderbuffer)
296 mask |= BUFFER_BIT_BACK_LEFT;
297 if (att[BUFFER_BACK_RIGHT].Renderbuffer)
298 mask |= BUFFER_BIT_BACK_RIGHT;
299 break;
300 case GL_LEFT:
301 if (att[BUFFER_FRONT_LEFT].Renderbuffer)
302 mask |= BUFFER_BIT_FRONT_LEFT;
303 if (att[BUFFER_BACK_LEFT].Renderbuffer)
304 mask |= BUFFER_BIT_BACK_LEFT;
305 break;
306 case GL_RIGHT:
307 if (att[BUFFER_FRONT_RIGHT].Renderbuffer)
308 mask |= BUFFER_BIT_FRONT_RIGHT;
309 if (att[BUFFER_BACK_RIGHT].Renderbuffer)
310 mask |= BUFFER_BIT_BACK_RIGHT;
311 break;
312 case GL_FRONT_AND_BACK:
313 if (att[BUFFER_FRONT_LEFT].Renderbuffer)
314 mask |= BUFFER_BIT_FRONT_LEFT;
315 if (att[BUFFER_BACK_LEFT].Renderbuffer)
316 mask |= BUFFER_BIT_BACK_LEFT;
317 if (att[BUFFER_FRONT_RIGHT].Renderbuffer)
318 mask |= BUFFER_BIT_FRONT_RIGHT;
319 if (att[BUFFER_BACK_RIGHT].Renderbuffer)
320 mask |= BUFFER_BIT_BACK_RIGHT;
321 break;
322 default:
Marek Olšák03d848e2013-12-04 00:27:20 +0100323 {
Brian Paulf8bae522017-11-08 22:18:40 -0700324 gl_buffer_index buf =
325 ctx->DrawBuffer->_ColorDrawBufferIndexes[drawbuffer];
Marek Olšák03d848e2013-12-04 00:27:20 +0100326
Brian Paulf8bae522017-11-08 22:18:40 -0700327 if (buf != BUFFER_NONE && att[buf].Renderbuffer) {
Marek Olšák03d848e2013-12-04 00:27:20 +0100328 mask |= 1 << buf;
329 }
Brian Paul2b5ece52009-12-30 09:25:24 -0700330 }
331 }
332
333 return mask;
334}
335
336
337
338/**
339 * New in GL 3.0
340 * Clear signed integer color buffer or stencil buffer (not depth).
341 */
Samuel Pitoiset54bd9a12017-07-21 14:27:23 +0200342static ALWAYS_INLINE void
343clear_bufferiv(struct gl_context *ctx, GLenum buffer, GLint drawbuffer,
344 const GLint *value, bool no_error)
Brian Paul2b5ece52009-12-30 09:25:24 -0700345{
Eric Anholta9754792013-01-16 16:20:38 -0800346 FLUSH_VERTICES(ctx, 0);
Brian Paul2b5ece52009-12-30 09:25:24 -0700347 FLUSH_CURRENT(ctx, 0);
348
Jakob Bornecrantz09171702018-09-19 15:21:26 +0100349 if (ctx->NewState) {
350 _mesa_update_state( ctx );
Brian Paul2b5ece52009-12-30 09:25:24 -0700351 }
352
353 switch (buffer) {
354 case GL_STENCIL:
Ian Romanick295e07e2011-11-01 15:11:12 -0700355 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
356 *
357 * "ClearBuffer generates an INVALID VALUE error if buffer is
358 * COLOR and drawbuffer is less than zero, or greater than the
359 * value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH,
360 * STENCIL, or DEPTH STENCIL and drawbuffer is not zero."
361 */
Samuel Pitoiset54bd9a12017-07-21 14:27:23 +0200362 if (!no_error && drawbuffer != 0) {
Brian Paul2b5ece52009-12-30 09:25:24 -0700363 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferiv(drawbuffer=%d)",
364 drawbuffer);
365 return;
366 }
Brian Paule725dc02014-12-12 16:45:33 -0700367 else if (ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer
368 && !ctx->RasterDiscard) {
Brian Paul2b5ece52009-12-30 09:25:24 -0700369 /* Save current stencil clear value, set to 'value', do the
370 * stencil clear and restore the clear value.
371 * XXX in the future we may have a new ctx->Driver.ClearBuffer()
372 * hook instead.
373 */
374 const GLuint clearSave = ctx->Stencil.Clear;
375 ctx->Stencil.Clear = *value;
Brian Paul2b5ece52009-12-30 09:25:24 -0700376 ctx->Driver.Clear(ctx, BUFFER_BIT_STENCIL);
377 ctx->Stencil.Clear = clearSave;
Brian Paul2b5ece52009-12-30 09:25:24 -0700378 }
379 break;
380 case GL_COLOR:
381 {
382 const GLbitfield mask = make_color_buffer_mask(ctx, drawbuffer);
Samuel Pitoiset54bd9a12017-07-21 14:27:23 +0200383 if (!no_error && mask == INVALID_MASK) {
Brian Paul2b5ece52009-12-30 09:25:24 -0700384 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferiv(drawbuffer=%d)",
385 drawbuffer);
386 return;
387 }
Paul Berryaee96802011-12-20 16:18:39 -0800388 else if (mask && !ctx->RasterDiscard) {
Dave Airlie093dc9e2011-09-12 10:57:40 +0100389 union gl_color_union clearSave;
390
Brian Paul2b5ece52009-12-30 09:25:24 -0700391 /* save color */
Dave Airlie093dc9e2011-09-12 10:57:40 +0100392 clearSave = ctx->Color.ClearColor;
Brian Paul2b5ece52009-12-30 09:25:24 -0700393 /* set color */
Dave Airlie093dc9e2011-09-12 10:57:40 +0100394 COPY_4V(ctx->Color.ClearColor.i, value);
Brian Paul2b5ece52009-12-30 09:25:24 -0700395 /* clear buffer(s) */
396 ctx->Driver.Clear(ctx, mask);
397 /* restore color */
Dave Airlie093dc9e2011-09-12 10:57:40 +0100398 ctx->Color.ClearColor = clearSave;
Brian Paul2b5ece52009-12-30 09:25:24 -0700399 }
400 }
401 break;
402 default:
Samuel Pitoiset54bd9a12017-07-21 14:27:23 +0200403 if (!no_error) {
404 /* Page 498 of the PDF, section '17.4.3.1 Clearing Individual Buffers'
405 * of the OpenGL 4.5 spec states:
406 *
407 * "An INVALID_ENUM error is generated by ClearBufferiv and
408 * ClearNamedFramebufferiv if buffer is not COLOR or STENCIL."
409 */
410 _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferiv(buffer=%s)",
411 _mesa_enum_to_string(buffer));
412 }
Brian Paul2b5ece52009-12-30 09:25:24 -0700413 return;
414 }
415}
416
417
Samuel Pitoiset54bd9a12017-07-21 14:27:23 +0200418void GLAPIENTRY
Samuel Pitoisetda0ecda2017-07-21 14:28:52 +0200419_mesa_ClearBufferiv_no_error(GLenum buffer, GLint drawbuffer, const GLint *value)
420{
421 GET_CURRENT_CONTEXT(ctx);
422 clear_bufferiv(ctx, buffer, drawbuffer, value, true);
423}
424
425
426void GLAPIENTRY
Samuel Pitoiset54bd9a12017-07-21 14:27:23 +0200427_mesa_ClearBufferiv(GLenum buffer, GLint drawbuffer, const GLint *value)
428{
429 GET_CURRENT_CONTEXT(ctx);
430 clear_bufferiv(ctx, buffer, drawbuffer, value, false);
431}
432
433
Brian Paul2b5ece52009-12-30 09:25:24 -0700434/**
Laura Ekstrand6236c472015-02-05 13:24:43 -0800435 * The ClearBuffer framework is so complicated and so riddled with the
436 * assumption that the framebuffer is bound that, for now, we will just fake
437 * direct state access clearing for the user.
438 */
439void GLAPIENTRY
440_mesa_ClearNamedFramebufferiv(GLuint framebuffer, GLenum buffer,
441 GLint drawbuffer, const GLint *value)
442{
443 GLint oldfb;
444
445 _mesa_GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfb);
446 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer);
447 _mesa_ClearBufferiv(buffer, drawbuffer, value);
448 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, (GLuint) oldfb);
449}
450
451
452/**
Brian Paul2b5ece52009-12-30 09:25:24 -0700453 * New in GL 3.0
454 * Clear unsigned integer color buffer (not depth, not stencil).
455 */
Samuel Pitoisetb18b1fa2017-07-21 14:23:35 +0200456static ALWAYS_INLINE void
457clear_bufferuiv(struct gl_context *ctx, GLenum buffer, GLint drawbuffer,
458 const GLuint *value, bool no_error)
Brian Paul2b5ece52009-12-30 09:25:24 -0700459{
Eric Anholta9754792013-01-16 16:20:38 -0800460 FLUSH_VERTICES(ctx, 0);
Brian Paul2b5ece52009-12-30 09:25:24 -0700461 FLUSH_CURRENT(ctx, 0);
462
Jakob Bornecrantz09171702018-09-19 15:21:26 +0100463 if (ctx->NewState) {
464 _mesa_update_state( ctx );
Brian Paul2b5ece52009-12-30 09:25:24 -0700465 }
466
467 switch (buffer) {
468 case GL_COLOR:
469 {
470 const GLbitfield mask = make_color_buffer_mask(ctx, drawbuffer);
Samuel Pitoisetb18b1fa2017-07-21 14:23:35 +0200471 if (!no_error && mask == INVALID_MASK) {
Brian Paul197b1d72010-11-19 14:34:07 -0700472 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferuiv(drawbuffer=%d)",
Brian Paul2b5ece52009-12-30 09:25:24 -0700473 drawbuffer);
474 return;
475 }
Paul Berryaee96802011-12-20 16:18:39 -0800476 else if (mask && !ctx->RasterDiscard) {
Dave Airlie093dc9e2011-09-12 10:57:40 +0100477 union gl_color_union clearSave;
478
Brian Paul2b5ece52009-12-30 09:25:24 -0700479 /* save color */
Dave Airlie093dc9e2011-09-12 10:57:40 +0100480 clearSave = ctx->Color.ClearColor;
Brian Paul2b5ece52009-12-30 09:25:24 -0700481 /* set color */
Dave Airlie093dc9e2011-09-12 10:57:40 +0100482 COPY_4V(ctx->Color.ClearColor.ui, value);
Brian Paul2b5ece52009-12-30 09:25:24 -0700483 /* clear buffer(s) */
484 ctx->Driver.Clear(ctx, mask);
485 /* restore color */
Dave Airlie093dc9e2011-09-12 10:57:40 +0100486 ctx->Color.ClearColor = clearSave;
Brian Paul2b5ece52009-12-30 09:25:24 -0700487 }
488 }
489 break;
490 default:
Samuel Pitoisetb18b1fa2017-07-21 14:23:35 +0200491 if (!no_error) {
492 /* Page 498 of the PDF, section '17.4.3.1 Clearing Individual Buffers'
493 * of the OpenGL 4.5 spec states:
494 *
495 * "An INVALID_ENUM error is generated by ClearBufferuiv and
496 * ClearNamedFramebufferuiv if buffer is not COLOR."
497 */
498 _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferuiv(buffer=%s)",
499 _mesa_enum_to_string(buffer));
500 }
Brian Paul2b5ece52009-12-30 09:25:24 -0700501 return;
502 }
503}
504
505
Samuel Pitoisetb18b1fa2017-07-21 14:23:35 +0200506void GLAPIENTRY
Samuel Pitoiset11e05422017-07-21 14:25:03 +0200507_mesa_ClearBufferuiv_no_error(GLenum buffer, GLint drawbuffer,
508 const GLuint *value)
509{
510 GET_CURRENT_CONTEXT(ctx);
511 clear_bufferuiv(ctx, buffer, drawbuffer, value, true);
512}
513
514
515void GLAPIENTRY
Samuel Pitoisetb18b1fa2017-07-21 14:23:35 +0200516_mesa_ClearBufferuiv(GLenum buffer, GLint drawbuffer, const GLuint *value)
517{
518 GET_CURRENT_CONTEXT(ctx);
519 clear_bufferuiv(ctx, buffer, drawbuffer, value, false);
520}
521
522
Brian Paul2b5ece52009-12-30 09:25:24 -0700523/**
Laura Ekstrand43db4b82015-02-05 13:30:50 -0800524 * The ClearBuffer framework is so complicated and so riddled with the
525 * assumption that the framebuffer is bound that, for now, we will just fake
526 * direct state access clearing for the user.
527 */
528void GLAPIENTRY
529_mesa_ClearNamedFramebufferuiv(GLuint framebuffer, GLenum buffer,
530 GLint drawbuffer, const GLuint *value)
531{
532 GLint oldfb;
533
534 _mesa_GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfb);
535 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer);
536 _mesa_ClearBufferuiv(buffer, drawbuffer, value);
537 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, (GLuint) oldfb);
538}
539
540
541/**
Brian Paul2b5ece52009-12-30 09:25:24 -0700542 * New in GL 3.0
543 * Clear fixed-pt or float color buffer or depth buffer (not stencil).
544 */
Samuel Pitoiset33b47302017-07-21 14:02:20 +0200545static ALWAYS_INLINE void
546clear_bufferfv(struct gl_context *ctx, GLenum buffer, GLint drawbuffer,
547 const GLfloat *value, bool no_error)
Brian Paul2b5ece52009-12-30 09:25:24 -0700548{
Eric Anholta9754792013-01-16 16:20:38 -0800549 FLUSH_VERTICES(ctx, 0);
Brian Paul2b5ece52009-12-30 09:25:24 -0700550 FLUSH_CURRENT(ctx, 0);
551
Jakob Bornecrantz09171702018-09-19 15:21:26 +0100552 if (ctx->NewState) {
553 _mesa_update_state( ctx );
Brian Paul2b5ece52009-12-30 09:25:24 -0700554 }
555
556 switch (buffer) {
557 case GL_DEPTH:
Ian Romanick295e07e2011-11-01 15:11:12 -0700558 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
559 *
560 * "ClearBuffer generates an INVALID VALUE error if buffer is
561 * COLOR and drawbuffer is less than zero, or greater than the
562 * value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH,
563 * STENCIL, or DEPTH STENCIL and drawbuffer is not zero."
564 */
Samuel Pitoiset33b47302017-07-21 14:02:20 +0200565 if (!no_error && drawbuffer != 0) {
Brian Paul2b5ece52009-12-30 09:25:24 -0700566 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferfv(drawbuffer=%d)",
567 drawbuffer);
568 return;
569 }
Brian Paule725dc02014-12-12 16:45:33 -0700570 else if (ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer
571 && !ctx->RasterDiscard) {
Brian Paul2b5ece52009-12-30 09:25:24 -0700572 /* Save current depth clear value, set to 'value', do the
573 * depth clear and restore the clear value.
574 * XXX in the future we may have a new ctx->Driver.ClearBuffer()
575 * hook instead.
576 */
Karl Schultzb30898f2010-02-13 17:31:58 -0700577 const GLclampd clearSave = ctx->Depth.Clear;
Brian Paul2b5ece52009-12-30 09:25:24 -0700578 ctx->Depth.Clear = *value;
Brian Paul2b5ece52009-12-30 09:25:24 -0700579 ctx->Driver.Clear(ctx, BUFFER_BIT_DEPTH);
580 ctx->Depth.Clear = clearSave;
Brian Paul2b5ece52009-12-30 09:25:24 -0700581 }
582 /* clear depth buffer to value */
583 break;
584 case GL_COLOR:
585 {
586 const GLbitfield mask = make_color_buffer_mask(ctx, drawbuffer);
Samuel Pitoiset33b47302017-07-21 14:02:20 +0200587 if (!no_error && mask == INVALID_MASK) {
Brian Paul2b5ece52009-12-30 09:25:24 -0700588 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferfv(drawbuffer=%d)",
589 drawbuffer);
590 return;
591 }
Paul Berryaee96802011-12-20 16:18:39 -0800592 else if (mask && !ctx->RasterDiscard) {
Dave Airlie093dc9e2011-09-12 10:57:40 +0100593 union gl_color_union clearSave;
594
Brian Paul2b5ece52009-12-30 09:25:24 -0700595 /* save color */
Dave Airlie093dc9e2011-09-12 10:57:40 +0100596 clearSave = ctx->Color.ClearColor;
Brian Paul2b5ece52009-12-30 09:25:24 -0700597 /* set color */
Brian Paulcf41d7c2012-08-25 06:43:37 -0600598 COPY_4V(ctx->Color.ClearColor.f, value);
Brian Paul2b5ece52009-12-30 09:25:24 -0700599 /* clear buffer(s) */
600 ctx->Driver.Clear(ctx, mask);
601 /* restore color */
Dave Airlie093dc9e2011-09-12 10:57:40 +0100602 ctx->Color.ClearColor = clearSave;
Brian Paul2b5ece52009-12-30 09:25:24 -0700603 }
604 }
605 break;
606 default:
Samuel Pitoiset33b47302017-07-21 14:02:20 +0200607 if (!no_error) {
608 /* Page 498 of the PDF, section '17.4.3.1 Clearing Individual Buffers'
609 * of the OpenGL 4.5 spec states:
610 *
611 * "An INVALID_ENUM error is generated by ClearBufferfv and
612 * ClearNamedFramebufferfv if buffer is not COLOR or DEPTH."
613 */
614 _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferfv(buffer=%s)",
615 _mesa_enum_to_string(buffer));
616 }
Brian Paul2b5ece52009-12-30 09:25:24 -0700617 return;
618 }
619}
620
621
Samuel Pitoiset33b47302017-07-21 14:02:20 +0200622void GLAPIENTRY
Samuel Pitoiset5e05e7d2017-07-21 14:04:15 +0200623_mesa_ClearBufferfv_no_error(GLenum buffer, GLint drawbuffer,
624 const GLfloat *value)
625{
626 GET_CURRENT_CONTEXT(ctx);
627 clear_bufferfv(ctx, buffer, drawbuffer, value, true);
628}
629
630
631void GLAPIENTRY
Samuel Pitoiset33b47302017-07-21 14:02:20 +0200632_mesa_ClearBufferfv(GLenum buffer, GLint drawbuffer, const GLfloat *value)
633{
634 GET_CURRENT_CONTEXT(ctx);
635 clear_bufferfv(ctx, buffer, drawbuffer, value, false);
636}
637
638
Brian Paul2b5ece52009-12-30 09:25:24 -0700639/**
Laura Ekstrandbbd9c552015-02-05 13:38:39 -0800640 * The ClearBuffer framework is so complicated and so riddled with the
641 * assumption that the framebuffer is bound that, for now, we will just fake
642 * direct state access clearing for the user.
643 */
644void GLAPIENTRY
645_mesa_ClearNamedFramebufferfv(GLuint framebuffer, GLenum buffer,
646 GLint drawbuffer, const GLfloat *value)
647{
648 GLint oldfb;
649
650 _mesa_GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfb);
651 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer);
652 _mesa_ClearBufferfv(buffer, drawbuffer, value);
653 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, (GLuint) oldfb);
654}
655
656
657/**
Brian Paul2b5ece52009-12-30 09:25:24 -0700658 * New in GL 3.0
659 * Clear depth/stencil buffer only.
660 */
Samuel Pitoiset1ed61e02017-07-21 14:19:27 +0200661static ALWAYS_INLINE void
662clear_bufferfi(struct gl_context *ctx, GLenum buffer, GLint drawbuffer,
663 GLfloat depth, GLint stencil, bool no_error)
Brian Paul2b5ece52009-12-30 09:25:24 -0700664{
Dave Airlie092cf9a2012-01-10 12:48:26 +0000665 GLbitfield mask = 0;
666
Eric Anholta9754792013-01-16 16:20:38 -0800667 FLUSH_VERTICES(ctx, 0);
Brian Paul2b5ece52009-12-30 09:25:24 -0700668 FLUSH_CURRENT(ctx, 0);
669
Samuel Pitoiset1ed61e02017-07-21 14:19:27 +0200670 if (!no_error) {
671 if (buffer != GL_DEPTH_STENCIL) {
672 _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferfi(buffer=%s)",
673 _mesa_enum_to_string(buffer));
674 return;
675 }
Brian Paul2b5ece52009-12-30 09:25:24 -0700676
Samuel Pitoiset1ed61e02017-07-21 14:19:27 +0200677 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
678 *
679 * "ClearBuffer generates an INVALID VALUE error if buffer is
680 * COLOR and drawbuffer is less than zero, or greater than the
681 * value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH,
682 * STENCIL, or DEPTH STENCIL and drawbuffer is not zero."
683 */
684 if (drawbuffer != 0) {
685 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferfi(drawbuffer=%d)",
686 drawbuffer);
687 return;
688 }
Brian Paul2b5ece52009-12-30 09:25:24 -0700689 }
690
Paul Berryaee96802011-12-20 16:18:39 -0800691 if (ctx->RasterDiscard)
Eric Anholte7c2b712011-09-29 23:13:44 -0700692 return;
693
Jakob Bornecrantz09171702018-09-19 15:21:26 +0100694 if (ctx->NewState) {
695 _mesa_update_state( ctx );
Brian Paul2b5ece52009-12-30 09:25:24 -0700696 }
697
Dave Airlie092cf9a2012-01-10 12:48:26 +0000698 if (ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer)
699 mask |= BUFFER_BIT_DEPTH;
700 if (ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer)
701 mask |= BUFFER_BIT_STENCIL;
702
703 if (mask) {
Brian Paul2b5ece52009-12-30 09:25:24 -0700704 /* save current clear values */
Karl Schultzb30898f2010-02-13 17:31:58 -0700705 const GLclampd clearDepthSave = ctx->Depth.Clear;
Brian Paul2b5ece52009-12-30 09:25:24 -0700706 const GLuint clearStencilSave = ctx->Stencil.Clear;
707
708 /* set new clear values */
709 ctx->Depth.Clear = depth;
710 ctx->Stencil.Clear = stencil;
Brian Paul2b5ece52009-12-30 09:25:24 -0700711
712 /* clear buffers */
Dave Airlie092cf9a2012-01-10 12:48:26 +0000713 ctx->Driver.Clear(ctx, mask);
Brian Paul2b5ece52009-12-30 09:25:24 -0700714
715 /* restore */
716 ctx->Depth.Clear = clearDepthSave;
717 ctx->Stencil.Clear = clearStencilSave;
Brian Paul2b5ece52009-12-30 09:25:24 -0700718 }
719}
Laura Ekstranda0329c72015-02-05 13:43:12 -0800720
721
Samuel Pitoiset1ed61e02017-07-21 14:19:27 +0200722void GLAPIENTRY
Samuel Pitoiset73c5e752017-07-21 14:21:10 +0200723_mesa_ClearBufferfi_no_error(GLenum buffer, GLint drawbuffer,
724 GLfloat depth, GLint stencil)
725{
726 GET_CURRENT_CONTEXT(ctx);
727 clear_bufferfi(ctx, buffer, drawbuffer, depth, stencil, true);
728}
729
730
731void GLAPIENTRY
Samuel Pitoiset1ed61e02017-07-21 14:19:27 +0200732_mesa_ClearBufferfi(GLenum buffer, GLint drawbuffer,
733 GLfloat depth, GLint stencil)
734{
735 GET_CURRENT_CONTEXT(ctx);
736 clear_bufferfi(ctx, buffer, drawbuffer, depth, stencil, false);
737}
738
739
Laura Ekstranda0329c72015-02-05 13:43:12 -0800740/**
741 * The ClearBuffer framework is so complicated and so riddled with the
742 * assumption that the framebuffer is bound that, for now, we will just fake
743 * direct state access clearing for the user.
744 */
745void GLAPIENTRY
746_mesa_ClearNamedFramebufferfi(GLuint framebuffer, GLenum buffer,
Ilia Mirkin7d7e0152016-06-09 23:45:22 -0400747 GLint drawbuffer, GLfloat depth, GLint stencil)
Laura Ekstranda0329c72015-02-05 13:43:12 -0800748{
749 GLint oldfb;
750
751 _mesa_GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfb);
752 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer);
Ilia Mirkin7d7e0152016-06-09 23:45:22 -0400753 _mesa_ClearBufferfi(buffer, drawbuffer, depth, stencil);
Laura Ekstranda0329c72015-02-05 13:43:12 -0800754 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, (GLuint) oldfb);
755}