optimize case of reading GL_DEPTH_STENCIL pixels from GL_DEPTH_STENCIL renderbuffer
[mesa.git] / src / mesa / swrast / s_accum.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include "glheader.h"
27 #include "context.h"
28 #include "macros.h"
29 #include "imports.h"
30 #include "fbobject.h"
31
32 #include "s_accum.h"
33 #include "s_context.h"
34 #include "s_masking.h"
35 #include "s_span.h"
36
37
38 #define ACCUM_SCALE16 32767.0
39
40
41 /*
42 * Accumulation buffer notes
43 *
44 * Normally, accumulation buffer values are GLshorts with values in
45 * [-32767, 32767] which represent floating point colors in [-1, 1],
46 * as defined by the OpenGL specification.
47 *
48 * We optimize for the common case used for full-scene antialiasing:
49 * // start with accum buffer cleared to zero
50 * glAccum(GL_LOAD, w); // or GL_ACCUM the first image
51 * glAccum(GL_ACCUM, w);
52 * ...
53 * glAccum(GL_ACCUM, w);
54 * glAccum(GL_RETURN, 1.0);
55 * That is, we start with an empty accumulation buffer and accumulate
56 * n images, each with weight w = 1/n.
57 * In this scenario, we can simply store unscaled integer values in
58 * the accum buffer instead of scaled integers. We'll also keep track
59 * of the w value so when we do GL_RETURN we simply divide the accumulated
60 * values by n (n=1/w).
61 * This lets us avoid _many_ int->float->int conversions.
62 */
63
64
65 #if CHAN_BITS == 8
66 /* enable the optimization */
67 #define USE_OPTIMIZED_ACCUM 1
68 #else
69 #define USE_OPTIMIZED_ACCUM 0
70 #endif
71
72
73 /**
74 * This is called when we fall out of optimized/unscaled accum buffer mode.
75 * That is, we convert each unscaled accum buffer value into a scaled value
76 * representing the range[-1, 1].
77 */
78 static void
79 rescale_accum( GLcontext *ctx )
80 {
81 SWcontext *swrast = SWRAST_CONTEXT(ctx);
82 struct gl_renderbuffer *rb
83 = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
84 const GLfloat s = swrast->_IntegerAccumScaler * (32767.0F / CHAN_MAXF);
85
86 assert(rb);
87 assert(rb->_BaseFormat == GL_RGBA);
88 /* add other types in future? */
89 assert(rb->DataType == GL_SHORT || rb->DataType == GL_UNSIGNED_SHORT);
90 assert(swrast->_IntegerAccumMode);
91
92 if (rb->GetPointer(ctx, rb, 0, 0)) {
93 /* directly-addressable memory */
94 GLuint y;
95 for (y = 0; y < rb->Height; y++) {
96 GLuint i;
97 GLshort *acc = (GLshort *) rb->GetPointer(ctx, rb, 0, y);
98 for (i = 0; i < 4 * rb->Width; i++) {
99 acc[i] = (GLshort) (acc[i] * s);
100 }
101 }
102 }
103 else {
104 /* use get/put row funcs */
105 GLuint y;
106 for (y = 0; y < rb->Height; y++) {
107 GLshort accRow[MAX_WIDTH * 4];
108 GLuint i;
109 rb->GetRow(ctx, rb, rb->Width, 0, y, accRow);
110 for (i = 0; i < 4 * rb->Width; i++) {
111 accRow[i] = (GLshort) (accRow[i] * s);
112 }
113 rb->PutRow(ctx, rb, rb->Width, 0, y, accRow, NULL);
114 }
115 }
116
117 swrast->_IntegerAccumMode = GL_FALSE;
118 }
119
120
121
122 /**
123 * Clear the accumulation Buffer.
124 */
125 void
126 _swrast_clear_accum_buffer( GLcontext *ctx, struct gl_renderbuffer *rb )
127 {
128 SWcontext *swrast = SWRAST_CONTEXT(ctx);
129 GLuint x, y, width, height;
130
131 if (ctx->Visual.accumRedBits == 0) {
132 /* No accumulation buffer! Not an error. */
133 return;
134 }
135
136 assert(rb);
137 assert(rb->_BaseFormat == GL_RGBA);
138 /* add other types in future? */
139 assert(rb->DataType == GL_SHORT || rb->DataType == GL_UNSIGNED_SHORT);
140
141 /* bounds, with scissor */
142 x = ctx->DrawBuffer->_Xmin;
143 y = ctx->DrawBuffer->_Ymin;
144 width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
145 height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
146
147 if (rb->DataType == GL_SHORT || rb->DataType == GL_UNSIGNED_SHORT) {
148 const GLfloat accScale = 32767.0;
149 GLshort clearVal[4];
150 GLuint i;
151
152 clearVal[0] = (GLshort) (ctx->Accum.ClearColor[0] * accScale);
153 clearVal[1] = (GLshort) (ctx->Accum.ClearColor[1] * accScale);
154 clearVal[2] = (GLshort) (ctx->Accum.ClearColor[2] * accScale);
155 clearVal[3] = (GLshort) (ctx->Accum.ClearColor[3] * accScale);
156
157 for (i = 0; i < height; i++) {
158 rb->PutMonoRow(ctx, rb, width, x, y + i, clearVal, NULL);
159 }
160 }
161 else {
162 /* someday support other sizes */
163 }
164
165 /* update optimized accum state vars */
166 if (ctx->Accum.ClearColor[0] == 0.0 && ctx->Accum.ClearColor[1] == 0.0 &&
167 ctx->Accum.ClearColor[2] == 0.0 && ctx->Accum.ClearColor[3] == 0.0) {
168 #if USE_OPTIMIZED_ACCUM
169 swrast->_IntegerAccumMode = GL_TRUE;
170 #else
171 swrast->_IntegerAccumMode = GL_FALSE;
172 #endif
173 swrast->_IntegerAccumScaler = 0.0; /* denotes empty accum buffer */
174 }
175 else {
176 swrast->_IntegerAccumMode = GL_FALSE;
177 }
178 }
179
180
181 static void
182 accum_add(GLcontext *ctx, GLfloat value,
183 GLint xpos, GLint ypos, GLint width, GLint height )
184 {
185 SWcontext *swrast = SWRAST_CONTEXT(ctx);
186 struct gl_renderbuffer *rb
187 = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
188
189 assert(rb);
190
191 /* Leave optimized accum buffer mode */
192 if (swrast->_IntegerAccumMode)
193 rescale_accum(ctx);
194
195 if (rb->DataType == GL_SHORT || rb->DataType == GL_UNSIGNED_SHORT) {
196 const GLshort incr = (GLshort) (value * ACCUM_SCALE16);
197 if (rb->GetPointer(ctx, rb, 0, 0)) {
198 GLint i, j;
199 for (i = 0; i < height; i++) {
200 GLshort *acc = (GLshort *) rb->GetPointer(ctx, rb, xpos, ypos + i);
201 for (j = 0; j < 4 * width; j++) {
202 acc[j] += incr;
203 }
204 }
205 }
206 else {
207 GLint i, j;
208 for (i = 0; i < height; i++) {
209 GLshort accRow[4 * MAX_WIDTH];
210 rb->GetRow(ctx, rb, width, xpos, ypos + i, accRow);
211 for (j = 0; j < 4 * width; j++) {
212 accRow[j] += incr;
213 }
214 rb->PutRow(ctx, rb, width, xpos, ypos + i, accRow, NULL);
215 }
216 }
217 }
218 else {
219 /* other types someday */
220 }
221 }
222
223
224 static void
225 accum_mult(GLcontext *ctx, GLfloat mult,
226 GLint xpos, GLint ypos, GLint width, GLint height )
227 {
228 SWcontext *swrast = SWRAST_CONTEXT(ctx);
229 struct gl_renderbuffer *rb
230 = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
231
232 assert(rb);
233
234 /* Leave optimized accum buffer mode */
235 if (swrast->_IntegerAccumMode)
236 rescale_accum(ctx);
237
238 if (rb->DataType == GL_SHORT || rb->DataType == GL_UNSIGNED_SHORT) {
239 if (rb->GetPointer(ctx, rb, 0, 0)) {
240 GLint i, j;
241 for (i = 0; i < height; i++) {
242 GLshort *acc = (GLshort *) rb->GetPointer(ctx, rb, xpos, ypos + i);
243 for (j = 0; j < 4 * width; j++) {
244 acc[j] = (GLshort) (acc[j] * mult);
245 }
246 }
247 }
248 else {
249 GLint i, j;
250 for (i = 0; i < height; i++) {
251 GLshort accRow[4 * MAX_WIDTH];
252 rb->GetRow(ctx, rb, width, xpos, ypos + i, accRow);
253 for (j = 0; j < 4 * width; j++) {
254 accRow[j] = (GLshort) (accRow[j] * mult);
255 }
256 rb->PutRow(ctx, rb, width, xpos, ypos + i, accRow, NULL);
257 }
258 }
259 }
260 else {
261 /* other types someday */
262 }
263 }
264
265
266
267 static void
268 accum_accum(GLcontext *ctx, GLfloat value,
269 GLint xpos, GLint ypos, GLint width, GLint height )
270 {
271 SWcontext *swrast = SWRAST_CONTEXT(ctx);
272 struct gl_renderbuffer *rb
273 = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
274 const GLboolean directAccess = (rb->GetPointer(ctx, rb, 0, 0) != NULL);
275
276 assert(rb);
277
278 if (!ctx->ReadBuffer->_ColorReadBuffer) {
279 /* no read buffer - OK */
280 return;
281 }
282
283 /* May have to leave optimized accum buffer mode */
284 if (swrast->_IntegerAccumScaler == 0.0 && value > 0.0 && value <= 1.0)
285 swrast->_IntegerAccumScaler = value;
286 if (swrast->_IntegerAccumMode && value != swrast->_IntegerAccumScaler)
287 rescale_accum(ctx);
288
289 if (rb->DataType == GL_SHORT || rb->DataType == GL_UNSIGNED_SHORT) {
290 const GLfloat scale = value * ACCUM_SCALE16 / CHAN_MAXF;
291 GLshort accumRow[4 * MAX_WIDTH];
292 GLchan rgba[MAX_WIDTH][4];
293 GLint i;
294
295 for (i = 0; i < height; i++) {
296 GLshort *acc;
297 if (directAccess) {
298 acc = (GLshort *) rb->GetPointer(ctx, rb, xpos, ypos + i);
299 }
300 else {
301 rb->GetRow(ctx, rb, width, xpos, ypos + i, accumRow);
302 acc = accumRow;
303 }
304
305 /* read colors from color buffer */
306 _swrast_read_rgba_span(ctx, ctx->ReadBuffer->_ColorReadBuffer, width,
307 xpos, ypos + i, rgba);
308
309 /* do accumulation */
310 if (swrast->_IntegerAccumMode) {
311 /* simply add integer color values into accum buffer */
312 GLint j;
313 for (j = 0; j < width; j++) {
314 acc[j * 4 + 0] += rgba[j][RCOMP];
315 acc[j * 4 + 1] += rgba[j][GCOMP];
316 acc[j * 4 + 2] += rgba[j][BCOMP];
317 acc[j * 4 + 3] += rgba[j][ACOMP];
318 }
319 }
320 else {
321 /* scaled integer (or float) accum buffer */
322 GLint j;
323 for (j = 0; j < width; j++) {
324 acc[j * 4 + 0] += (GLshort) ((GLfloat) rgba[j][RCOMP] * scale);
325 acc[j * 4 + 1] += (GLshort) ((GLfloat) rgba[j][GCOMP] * scale);
326 acc[j * 4 + 2] += (GLshort) ((GLfloat) rgba[j][BCOMP] * scale);
327 acc[j * 4 + 3] += (GLshort) ((GLfloat) rgba[j][ACOMP] * scale);
328 }
329 }
330
331 if (!directAccess) {
332 rb->PutRow(ctx, rb, width, xpos, ypos + i, accumRow, NULL);
333 }
334 }
335 }
336 else {
337 /* other types someday */
338 }
339 }
340
341
342
343 static void
344 accum_load(GLcontext *ctx, GLfloat value,
345 GLint xpos, GLint ypos, GLint width, GLint height )
346 {
347 SWcontext *swrast = SWRAST_CONTEXT(ctx);
348 struct gl_renderbuffer *rb
349 = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
350 const GLboolean directAccess = (rb->GetPointer(ctx, rb, 0, 0) != NULL);
351
352 assert(rb);
353
354 if (!ctx->ReadBuffer->_ColorReadBuffer) {
355 /* no read buffer - OK */
356 return;
357 }
358
359 /* This is a change to go into optimized accum buffer mode */
360 if (value > 0.0 && value <= 1.0) {
361 #if USE_OPTIMIZED_ACCUM
362 swrast->_IntegerAccumMode = GL_TRUE;
363 #else
364 swrast->_IntegerAccumMode = GL_FALSE;
365 #endif
366 swrast->_IntegerAccumScaler = value;
367 }
368 else {
369 swrast->_IntegerAccumMode = GL_FALSE;
370 swrast->_IntegerAccumScaler = 0.0;
371 }
372
373 if (rb->DataType == GL_SHORT || rb->DataType == GL_UNSIGNED_SHORT) {
374 const GLfloat scale = value * ACCUM_SCALE16 / CHAN_MAXF;
375 GLshort accumRow[4 * MAX_WIDTH];
376 GLchan rgba[MAX_WIDTH][4];
377 GLint i;
378
379 for (i = 0; i < height; i++) {
380 GLshort *acc;
381 if (directAccess) {
382 acc = (GLshort *) rb->GetPointer(ctx, rb, xpos, ypos + i);
383 }
384 else {
385 rb->GetRow(ctx, rb, width, xpos, ypos + i, accumRow);
386 acc = accumRow;
387 }
388
389 /* read colors from color buffer */
390 _swrast_read_rgba_span(ctx, ctx->ReadBuffer->_ColorReadBuffer, width,
391 xpos, ypos + i, rgba);
392
393 /* do load */
394 if (swrast->_IntegerAccumMode) {
395 /* just copy values in */
396 GLint j;
397 assert(swrast->_IntegerAccumScaler > 0.0);
398 assert(swrast->_IntegerAccumScaler <= 1.0);
399 for (j = 0; j < width; j++) {
400 acc[j * 4 + 0] = rgba[j][RCOMP];
401 acc[j * 4 + 1] = rgba[j][GCOMP];
402 acc[j * 4 + 2] = rgba[j][BCOMP];
403 acc[j * 4 + 3] = rgba[j][ACOMP];
404 }
405 }
406 else {
407 /* scaled integer (or float) accum buffer */
408 GLint j;
409 for (j = 0; j < width; j++) {
410 acc[j * 4 + 0] = (GLshort) ((GLfloat) rgba[j][RCOMP] * scale);
411 acc[j * 4 + 1] = (GLshort) ((GLfloat) rgba[j][GCOMP] * scale);
412 acc[j * 4 + 2] = (GLshort) ((GLfloat) rgba[j][BCOMP] * scale);
413 acc[j * 4 + 3] = (GLshort) ((GLfloat) rgba[j][ACOMP] * scale);
414 }
415 }
416
417 if (!directAccess) {
418 rb->PutRow(ctx, rb, width, xpos, ypos + i, accumRow, NULL);
419 }
420 }
421 }
422 else {
423 /* other types someday */
424 }
425 }
426
427
428 static void
429 accum_return(GLcontext *ctx, GLfloat value,
430 GLint xpos, GLint ypos, GLint width, GLint height )
431 {
432 SWcontext *swrast = SWRAST_CONTEXT(ctx);
433 struct gl_framebuffer *fb = ctx->DrawBuffer;
434 struct gl_renderbuffer *accumRb = fb->Attachment[BUFFER_ACCUM].Renderbuffer;
435 const GLboolean directAccess
436 = (accumRb->GetPointer(ctx, accumRb, 0, 0) != NULL);
437 const GLboolean masking = (!ctx->Color.ColorMask[RCOMP] ||
438 !ctx->Color.ColorMask[GCOMP] ||
439 !ctx->Color.ColorMask[BCOMP] ||
440 !ctx->Color.ColorMask[ACOMP]);
441
442 static GLchan multTable[32768];
443 static GLfloat prevMult = 0.0;
444 const GLfloat mult = swrast->_IntegerAccumScaler;
445 const GLint max = MIN2((GLint) (256 / mult), 32767);
446
447 /* May have to leave optimized accum buffer mode */
448 if (swrast->_IntegerAccumMode && value != 1.0)
449 rescale_accum(ctx);
450
451 if (swrast->_IntegerAccumMode && swrast->_IntegerAccumScaler > 0) {
452 /* build lookup table to avoid many floating point multiplies */
453 GLint j;
454 assert(swrast->_IntegerAccumScaler <= 1.0);
455 if (mult != prevMult) {
456 for (j = 0; j < max; j++)
457 multTable[j] = IROUND((GLfloat) j * mult);
458 prevMult = mult;
459 }
460 }
461
462 if (accumRb->DataType == GL_SHORT ||
463 accumRb->DataType == GL_UNSIGNED_SHORT) {
464 const GLfloat scale = value * CHAN_MAXF / ACCUM_SCALE16;
465 GLuint buffer, i;
466
467 /* XXX maybe transpose the 'i' and 'buffer' loops??? */
468 for (i = 0; i < height; i++) {
469 GLchan rgba[MAX_WIDTH][4];
470 GLshort accumRow[4 * MAX_WIDTH];
471 GLshort *acc;
472
473 if (directAccess) {
474 acc = (GLshort *) accumRb->GetPointer(ctx, accumRb, xpos, ypos +i);
475 }
476 else {
477 accumRb->GetRow(ctx, accumRb, width, xpos, ypos + i, accumRow);
478 acc = accumRow;
479 }
480
481 /* get the colors to return */
482 if (swrast->_IntegerAccumMode) {
483 GLint j;
484 for (j = 0; j < width; j++) {
485 ASSERT(acc[j * 4 + 0] < max);
486 ASSERT(acc[j * 4 + 1] < max);
487 ASSERT(acc[j * 4 + 2] < max);
488 ASSERT(acc[j * 4 + 3] < max);
489 rgba[j][RCOMP] = multTable[acc[j * 4 + 0]];
490 rgba[j][GCOMP] = multTable[acc[j * 4 + 1]];
491 rgba[j][BCOMP] = multTable[acc[j * 4 + 2]];
492 rgba[j][ACOMP] = multTable[acc[j * 4 + 3]];
493 }
494 }
495 else {
496 /* scaled integer (or float) accum buffer */
497 GLint j;
498 for (j = 0; j < width; j++) {
499 GLint r = IROUND( (GLfloat) (acc[j * 4 + 0]) * scale );
500 GLint g = IROUND( (GLfloat) (acc[j * 4 + 1]) * scale );
501 GLint b = IROUND( (GLfloat) (acc[j * 4 + 2]) * scale );
502 GLint a = IROUND( (GLfloat) (acc[j * 4 + 3]) * scale );
503 rgba[j][RCOMP] = CLAMP( r, 0, CHAN_MAX );
504 rgba[j][GCOMP] = CLAMP( g, 0, CHAN_MAX );
505 rgba[j][BCOMP] = CLAMP( b, 0, CHAN_MAX );
506 rgba[j][ACOMP] = CLAMP( a, 0, CHAN_MAX );
507 }
508 }
509
510 /* store colors */
511 for (buffer = 0; buffer < fb->_NumColorDrawBuffers[0]; buffer++) {
512 struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[0][buffer];
513 if (masking) {
514 _swrast_mask_rgba_array(ctx, rb, width, xpos, ypos + i, rgba);
515 }
516 rb->PutRow(ctx, rb, width, xpos, ypos + i, rgba, NULL);
517 }
518 }
519 }
520 else {
521 /* other types someday */
522 }
523 }
524
525
526
527 /**
528 * Software fallback for glAccum.
529 */
530 void
531 _swrast_Accum( GLcontext *ctx, GLenum op, GLfloat value,
532 GLint xpos, GLint ypos,
533 GLint width, GLint height )
534
535 {
536 SWcontext *swrast = SWRAST_CONTEXT(ctx);
537
538 if (SWRAST_CONTEXT(ctx)->NewState)
539 _swrast_validate_derived( ctx );
540
541 if (!ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer) {
542 _mesa_warning(ctx, "Calling glAccum() without an accumulation buffer");
543 return;
544 }
545
546 RENDER_START(swrast, ctx);
547
548 switch (op) {
549 case GL_ADD:
550 if (value != 0.0F) {
551 accum_add(ctx, value, xpos, ypos, width, height);
552 }
553 break;
554 case GL_MULT:
555 if (value != 1.0F) {
556 accum_mult(ctx, value, xpos, ypos, width, height);
557 }
558 break;
559 case GL_ACCUM:
560 if (value != 0.0F) {
561 accum_accum(ctx, value, xpos, ypos, width, height);
562 }
563 break;
564 case GL_LOAD:
565 accum_load(ctx, value, xpos, ypos, width, height);
566 break;
567 case GL_RETURN:
568 accum_return(ctx, value, xpos, ypos, width, height);
569 break;
570 default:
571 _mesa_problem(ctx, "invalid mode in _swrast_Accum()");
572 break;
573 }
574
575 RENDER_FINISH(swrast, ctx);
576 }