mesa: split bind_buffer_range_uniform_buffer() in two
[mesa.git] / src / mesa / main / accum.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2005 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 #include "glheader.h"
26 #include "accum.h"
27 #include "condrender.h"
28 #include "context.h"
29 #include "format_unpack.h"
30 #include "format_pack.h"
31 #include "imports.h"
32 #include "macros.h"
33 #include "state.h"
34 #include "mtypes.h"
35 #include "main/dispatch.h"
36
37
38 void GLAPIENTRY
39 _mesa_ClearAccum( GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha )
40 {
41 GLfloat tmp[4];
42 GET_CURRENT_CONTEXT(ctx);
43
44 tmp[0] = CLAMP( red, -1.0F, 1.0F );
45 tmp[1] = CLAMP( green, -1.0F, 1.0F );
46 tmp[2] = CLAMP( blue, -1.0F, 1.0F );
47 tmp[3] = CLAMP( alpha, -1.0F, 1.0F );
48
49 if (TEST_EQ_4V(tmp, ctx->Accum.ClearColor))
50 return;
51
52 COPY_4FV( ctx->Accum.ClearColor, tmp );
53 }
54
55
56 /**
57 * Clear the accumulation buffer by mapping the renderbuffer and
58 * writing the clear color to it. Called by the driver's implementation
59 * of the glClear function.
60 */
61 void
62 _mesa_clear_accum_buffer(struct gl_context *ctx)
63 {
64 GLuint x, y, width, height;
65 GLubyte *accMap;
66 GLint accRowStride;
67 struct gl_renderbuffer *accRb;
68
69 if (!ctx->DrawBuffer)
70 return;
71
72 accRb = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
73 if (!accRb)
74 return; /* missing accum buffer, not an error */
75
76 /* bounds, with scissor */
77 x = ctx->DrawBuffer->_Xmin;
78 y = ctx->DrawBuffer->_Ymin;
79 width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
80 height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
81
82 ctx->Driver.MapRenderbuffer(ctx, accRb, x, y, width, height,
83 GL_MAP_WRITE_BIT, &accMap, &accRowStride);
84
85 if (!accMap) {
86 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
87 return;
88 }
89
90 if (accRb->Format == MESA_FORMAT_RGBA_SNORM16) {
91 const GLshort clearR = FLOAT_TO_SHORT(ctx->Accum.ClearColor[0]);
92 const GLshort clearG = FLOAT_TO_SHORT(ctx->Accum.ClearColor[1]);
93 const GLshort clearB = FLOAT_TO_SHORT(ctx->Accum.ClearColor[2]);
94 const GLshort clearA = FLOAT_TO_SHORT(ctx->Accum.ClearColor[3]);
95 GLuint i, j;
96
97 for (j = 0; j < height; j++) {
98 GLshort *row = (GLshort *) accMap;
99
100 for (i = 0; i < width; i++) {
101 row[i * 4 + 0] = clearR;
102 row[i * 4 + 1] = clearG;
103 row[i * 4 + 2] = clearB;
104 row[i * 4 + 3] = clearA;
105 }
106 accMap += accRowStride;
107 }
108 }
109 else {
110 /* other types someday? */
111 _mesa_warning(ctx, "unexpected accum buffer type");
112 }
113
114 ctx->Driver.UnmapRenderbuffer(ctx, accRb);
115 }
116
117
118 /**
119 * if (bias)
120 * Accum += value
121 * else
122 * Accum *= value
123 */
124 static void
125 accum_scale_or_bias(struct gl_context *ctx, GLfloat value,
126 GLint xpos, GLint ypos, GLint width, GLint height,
127 GLboolean bias)
128 {
129 struct gl_renderbuffer *accRb =
130 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
131 GLubyte *accMap;
132 GLint accRowStride;
133
134 assert(accRb);
135
136 ctx->Driver.MapRenderbuffer(ctx, accRb, xpos, ypos, width, height,
137 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
138 &accMap, &accRowStride);
139
140 if (!accMap) {
141 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
142 return;
143 }
144
145 if (accRb->Format == MESA_FORMAT_RGBA_SNORM16) {
146 const GLshort incr = (GLshort) (value * 32767.0f);
147 GLint i, j;
148 if (bias) {
149 for (j = 0; j < height; j++) {
150 GLshort *acc = (GLshort *) accMap;
151 for (i = 0; i < 4 * width; i++) {
152 acc[i] += incr;
153 }
154 accMap += accRowStride;
155 }
156 }
157 else {
158 /* scale */
159 for (j = 0; j < height; j++) {
160 GLshort *acc = (GLshort *) accMap;
161 for (i = 0; i < 4 * width; i++) {
162 acc[i] = (GLshort) (acc[i] * value);
163 }
164 accMap += accRowStride;
165 }
166 }
167 }
168 else {
169 /* other types someday? */
170 }
171
172 ctx->Driver.UnmapRenderbuffer(ctx, accRb);
173 }
174
175
176 /**
177 * if (load)
178 * Accum = ColorBuf * value
179 * else
180 * Accum += ColorBuf * value
181 */
182 static void
183 accum_or_load(struct gl_context *ctx, GLfloat value,
184 GLint xpos, GLint ypos, GLint width, GLint height,
185 GLboolean load)
186 {
187 struct gl_renderbuffer *accRb =
188 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
189 struct gl_renderbuffer *colorRb = ctx->ReadBuffer->_ColorReadBuffer;
190 GLubyte *accMap, *colorMap;
191 GLint accRowStride, colorRowStride;
192 GLbitfield mappingFlags;
193
194 if (!colorRb) {
195 /* no read buffer - OK */
196 return;
197 }
198
199 assert(accRb);
200
201 mappingFlags = GL_MAP_WRITE_BIT;
202 if (!load) /* if we're accumulating */
203 mappingFlags |= GL_MAP_READ_BIT;
204
205 /* Map accum buffer */
206 ctx->Driver.MapRenderbuffer(ctx, accRb, xpos, ypos, width, height,
207 mappingFlags, &accMap, &accRowStride);
208 if (!accMap) {
209 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
210 return;
211 }
212
213 /* Map color buffer */
214 ctx->Driver.MapRenderbuffer(ctx, colorRb, xpos, ypos, width, height,
215 GL_MAP_READ_BIT,
216 &colorMap, &colorRowStride);
217 if (!colorMap) {
218 ctx->Driver.UnmapRenderbuffer(ctx, accRb);
219 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
220 return;
221 }
222
223 if (accRb->Format == MESA_FORMAT_RGBA_SNORM16) {
224 const GLfloat scale = value * 32767.0f;
225 GLint i, j;
226 GLfloat (*rgba)[4];
227
228 rgba = malloc(width * 4 * sizeof(GLfloat));
229 if (rgba) {
230 for (j = 0; j < height; j++) {
231 GLshort *acc = (GLshort *) accMap;
232
233 /* read colors from source color buffer */
234 _mesa_unpack_rgba_row(colorRb->Format, width, colorMap, rgba);
235
236 if (load) {
237 for (i = 0; i < width; i++) {
238 acc[i * 4 + 0] = (GLshort) (rgba[i][RCOMP] * scale);
239 acc[i * 4 + 1] = (GLshort) (rgba[i][GCOMP] * scale);
240 acc[i * 4 + 2] = (GLshort) (rgba[i][BCOMP] * scale);
241 acc[i * 4 + 3] = (GLshort) (rgba[i][ACOMP] * scale);
242 }
243 }
244 else {
245 /* accumulate */
246 for (i = 0; i < width; i++) {
247 acc[i * 4 + 0] += (GLshort) (rgba[i][RCOMP] * scale);
248 acc[i * 4 + 1] += (GLshort) (rgba[i][GCOMP] * scale);
249 acc[i * 4 + 2] += (GLshort) (rgba[i][BCOMP] * scale);
250 acc[i * 4 + 3] += (GLshort) (rgba[i][ACOMP] * scale);
251 }
252 }
253
254 colorMap += colorRowStride;
255 accMap += accRowStride;
256 }
257
258 free(rgba);
259 }
260 else {
261 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
262 }
263 }
264 else {
265 /* other types someday? */
266 }
267
268 ctx->Driver.UnmapRenderbuffer(ctx, accRb);
269 ctx->Driver.UnmapRenderbuffer(ctx, colorRb);
270 }
271
272
273 /**
274 * ColorBuffer = Accum * value
275 */
276 static void
277 accum_return(struct gl_context *ctx, GLfloat value,
278 GLint xpos, GLint ypos, GLint width, GLint height)
279 {
280 struct gl_framebuffer *fb = ctx->DrawBuffer;
281 struct gl_renderbuffer *accRb = fb->Attachment[BUFFER_ACCUM].Renderbuffer;
282 GLubyte *accMap, *colorMap;
283 GLint accRowStride, colorRowStride;
284 GLuint buffer;
285
286 /* Map accum buffer */
287 ctx->Driver.MapRenderbuffer(ctx, accRb, xpos, ypos, width, height,
288 GL_MAP_READ_BIT,
289 &accMap, &accRowStride);
290 if (!accMap) {
291 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
292 return;
293 }
294
295 /* Loop over destination buffers */
296 for (buffer = 0; buffer < fb->_NumColorDrawBuffers; buffer++) {
297 struct gl_renderbuffer *colorRb = fb->_ColorDrawBuffers[buffer];
298 const GLboolean masking = (!ctx->Color.ColorMask[buffer][RCOMP] ||
299 !ctx->Color.ColorMask[buffer][GCOMP] ||
300 !ctx->Color.ColorMask[buffer][BCOMP] ||
301 !ctx->Color.ColorMask[buffer][ACOMP]);
302 GLbitfield mappingFlags = GL_MAP_WRITE_BIT;
303
304 if (masking)
305 mappingFlags |= GL_MAP_READ_BIT;
306
307 /* Map color buffer */
308 ctx->Driver.MapRenderbuffer(ctx, colorRb, xpos, ypos, width, height,
309 mappingFlags, &colorMap, &colorRowStride);
310 if (!colorMap) {
311 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
312 continue;
313 }
314
315 if (accRb->Format == MESA_FORMAT_RGBA_SNORM16) {
316 const GLfloat scale = value / 32767.0f;
317 GLint i, j;
318 GLfloat (*rgba)[4], (*dest)[4];
319
320 rgba = malloc(width * 4 * sizeof(GLfloat));
321 dest = malloc(width * 4 * sizeof(GLfloat));
322
323 if (rgba && dest) {
324 for (j = 0; j < height; j++) {
325 GLshort *acc = (GLshort *) accMap;
326
327 for (i = 0; i < width; i++) {
328 rgba[i][0] = acc[i * 4 + 0] * scale;
329 rgba[i][1] = acc[i * 4 + 1] * scale;
330 rgba[i][2] = acc[i * 4 + 2] * scale;
331 rgba[i][3] = acc[i * 4 + 3] * scale;
332 }
333
334 if (masking) {
335
336 /* get existing colors from dest buffer */
337 _mesa_unpack_rgba_row(colorRb->Format, width, colorMap, dest);
338
339 /* use the dest colors where mask[channel] = 0 */
340 if (ctx->Color.ColorMask[buffer][RCOMP] == 0) {
341 for (i = 0; i < width; i++)
342 rgba[i][RCOMP] = dest[i][RCOMP];
343 }
344 if (ctx->Color.ColorMask[buffer][GCOMP] == 0) {
345 for (i = 0; i < width; i++)
346 rgba[i][GCOMP] = dest[i][GCOMP];
347 }
348 if (ctx->Color.ColorMask[buffer][BCOMP] == 0) {
349 for (i = 0; i < width; i++)
350 rgba[i][BCOMP] = dest[i][BCOMP];
351 }
352 if (ctx->Color.ColorMask[buffer][ACOMP] == 0) {
353 for (i = 0; i < width; i++)
354 rgba[i][ACOMP] = dest[i][ACOMP];
355 }
356 }
357
358 _mesa_pack_float_rgba_row(colorRb->Format, width,
359 (const GLfloat (*)[4]) rgba, colorMap);
360
361 accMap += accRowStride;
362 colorMap += colorRowStride;
363 }
364 }
365 else {
366 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
367 }
368 free(rgba);
369 free(dest);
370 }
371 else {
372 /* other types someday? */
373 }
374
375 ctx->Driver.UnmapRenderbuffer(ctx, colorRb);
376 }
377
378 ctx->Driver.UnmapRenderbuffer(ctx, accRb);
379 }
380
381
382
383 /**
384 * Software fallback for glAccum. A hardware driver that supports
385 * signed 16-bit color channels could implement hardware accumulation
386 * operations, but no driver does so at this time.
387 */
388 static void
389 accum(struct gl_context *ctx, GLenum op, GLfloat value)
390 {
391 GLint xpos, ypos, width, height;
392
393 if (!ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer) {
394 _mesa_warning(ctx, "Calling glAccum() without an accumulation buffer");
395 return;
396 }
397
398 if (!_mesa_check_conditional_render(ctx))
399 return;
400
401 xpos = ctx->DrawBuffer->_Xmin;
402 ypos = ctx->DrawBuffer->_Ymin;
403 width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
404 height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
405
406 switch (op) {
407 case GL_ADD:
408 if (value != 0.0F) {
409 accum_scale_or_bias(ctx, value, xpos, ypos, width, height, GL_TRUE);
410 }
411 break;
412 case GL_MULT:
413 if (value != 1.0F) {
414 accum_scale_or_bias(ctx, value, xpos, ypos, width, height, GL_FALSE);
415 }
416 break;
417 case GL_ACCUM:
418 if (value != 0.0F) {
419 accum_or_load(ctx, value, xpos, ypos, width, height, GL_FALSE);
420 }
421 break;
422 case GL_LOAD:
423 accum_or_load(ctx, value, xpos, ypos, width, height, GL_TRUE);
424 break;
425 case GL_RETURN:
426 accum_return(ctx, value, xpos, ypos, width, height);
427 break;
428 default:
429 unreachable("invalid mode in _mesa_Accum()");
430 break;
431 }
432 }
433
434
435 void
436 _mesa_init_accum( struct gl_context *ctx )
437 {
438 /* Accumulate buffer group */
439 ASSIGN_4V( ctx->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 );
440 }
441
442
443 void GLAPIENTRY
444 _mesa_Accum( GLenum op, GLfloat value )
445 {
446 GET_CURRENT_CONTEXT(ctx);
447 FLUSH_VERTICES(ctx, 0);
448
449 switch (op) {
450 case GL_ADD:
451 case GL_MULT:
452 case GL_ACCUM:
453 case GL_LOAD:
454 case GL_RETURN:
455 /* OK */
456 break;
457 default:
458 _mesa_error(ctx, GL_INVALID_ENUM, "glAccum(op)");
459 return;
460 }
461
462 if (ctx->DrawBuffer->Visual.haveAccumBuffer == 0) {
463 _mesa_error(ctx, GL_INVALID_OPERATION, "glAccum(no accum buffer)");
464 return;
465 }
466
467 if (ctx->DrawBuffer != ctx->ReadBuffer) {
468 /* See GLX_SGI_make_current_read or WGL_ARB_make_current_read,
469 * or GL_EXT_framebuffer_blit.
470 */
471 _mesa_error(ctx, GL_INVALID_OPERATION,
472 "glAccum(different read/draw buffers)");
473 return;
474 }
475
476 if (ctx->NewState)
477 _mesa_update_state(ctx);
478
479 if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
480 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
481 "glAccum(incomplete framebuffer)");
482 return;
483 }
484
485 if (ctx->RasterDiscard)
486 return;
487
488 if (ctx->RenderMode == GL_RENDER) {
489 accum(ctx, op, value);
490 }
491 }