mesa: Add a gl_renderbuffer.RowStride field like textures have.
[mesa.git] / src / mesa / main / renderbuffer.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2006 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 /**
27 * Functions for allocating/managing renderbuffers.
28 * Also, routines for reading/writing software-based renderbuffer data as
29 * ubytes, ushorts, uints, etc.
30 *
31 * The 'alpha8' renderbuffer is interesting. It's used to add a software-based
32 * alpha channel to RGB renderbuffers. This is done by wrapping the RGB
33 * renderbuffer with the alpha renderbuffer. We can do this because of the
34 * OO-nature of renderbuffers.
35 *
36 * Down the road we'll use this for run-time support of 8, 16 and 32-bit
37 * color channels. For example, Mesa may use 32-bit/float color channels
38 * internally (swrast) and use wrapper renderbuffers to convert 32-bit
39 * values down to 16 or 8-bit values for whatever kind of framebuffer we have.
40 */
41
42
43 #include "glheader.h"
44 #include "imports.h"
45 #include "context.h"
46 #include "fbobject.h"
47 #include "formats.h"
48 #include "mtypes.h"
49 #include "renderbuffer.h"
50
51
52 /*
53 * Routines for get/put values in common buffer formats follow.
54 */
55
56 /**********************************************************************
57 * Functions for buffers of 1 X GLubyte values.
58 * Typically stencil.
59 */
60
61 static void *
62 get_pointer_ubyte(struct gl_context *ctx, struct gl_renderbuffer *rb,
63 GLint x, GLint y)
64 {
65 if (!rb->Data)
66 return NULL;
67 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
68 /* Can't assert rb->Format since these funcs may be used for serveral
69 * different formats (GL_ALPHA8, GL_STENCIL_INDEX8, etc).
70 */
71 return (GLubyte *) rb->Data + y * rb->RowStride + x;
72 }
73
74
75 static void
76 get_row_ubyte(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
77 GLint x, GLint y, void *values)
78 {
79 const GLubyte *src = (const GLubyte *) rb->Data + y * rb->RowStride + x;
80 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
81 memcpy(values, src, count * sizeof(GLubyte));
82 }
83
84
85 static void
86 get_values_ubyte(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
87 const GLint x[], const GLint y[], void *values)
88 {
89 GLubyte *dst = (GLubyte *) values;
90 GLuint i;
91 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
92 for (i = 0; i < count; i++) {
93 const GLubyte *src = (GLubyte *) rb->Data + y[i] * rb->RowStride + x[i];
94 dst[i] = *src;
95 }
96 }
97
98
99 static void
100 put_row_ubyte(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
101 GLint x, GLint y, const void *values, const GLubyte *mask)
102 {
103 const GLubyte *src = (const GLubyte *) values;
104 GLubyte *dst = (GLubyte *) rb->Data + y * rb->RowStride + x;
105 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
106 if (mask) {
107 GLuint i;
108 for (i = 0; i < count; i++) {
109 if (mask[i]) {
110 dst[i] = src[i];
111 }
112 }
113 }
114 else {
115 memcpy(dst, values, count * sizeof(GLubyte));
116 }
117 }
118
119
120 static void
121 put_mono_row_ubyte(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
122 GLint x, GLint y, const void *value, const GLubyte *mask)
123 {
124 const GLubyte val = *((const GLubyte *) value);
125 GLubyte *dst = (GLubyte *) rb->Data + y * rb->RowStride + x;
126 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
127 if (mask) {
128 GLuint i;
129 for (i = 0; i < count; i++) {
130 if (mask[i]) {
131 dst[i] = val;
132 }
133 }
134 }
135 else {
136 GLuint i;
137 for (i = 0; i < count; i++) {
138 dst[i] = val;
139 }
140 }
141 }
142
143
144 static void
145 put_values_ubyte(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
146 const GLint x[], const GLint y[],
147 const void *values, const GLubyte *mask)
148 {
149 const GLubyte *src = (const GLubyte *) values;
150 GLuint i;
151 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
152 for (i = 0; i < count; i++) {
153 if (!mask || mask[i]) {
154 GLubyte *dst = (GLubyte *) rb->Data + y[i] * rb->RowStride + x[i];
155 *dst = src[i];
156 }
157 }
158 }
159
160
161 static void
162 put_mono_values_ubyte(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
163 const GLint x[], const GLint y[],
164 const void *value, const GLubyte *mask)
165 {
166 const GLubyte val = *((const GLubyte *) value);
167 GLuint i;
168 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
169 for (i = 0; i < count; i++) {
170 if (!mask || mask[i]) {
171 GLubyte *dst = (GLubyte *) rb->Data + y[i] * rb->RowStride + x[i];
172 *dst = val;
173 }
174 }
175 }
176
177
178 /**********************************************************************
179 * Functions for buffers of 1 X GLushort values.
180 * Typically depth/Z.
181 */
182
183 static void *
184 get_pointer_ushort(struct gl_context *ctx, struct gl_renderbuffer *rb,
185 GLint x, GLint y)
186 {
187 if (!rb->Data)
188 return NULL;
189 ASSERT(rb->DataType == GL_UNSIGNED_SHORT);
190 ASSERT(rb->Width > 0);
191 return (GLushort *) rb->Data + y * rb->RowStride + x;
192 }
193
194
195 static void
196 get_row_ushort(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
197 GLint x, GLint y, void *values)
198 {
199 const void *src = rb->GetPointer(ctx, rb, x, y);
200 ASSERT(rb->DataType == GL_UNSIGNED_SHORT);
201 memcpy(values, src, count * sizeof(GLushort));
202 }
203
204
205 static void
206 get_values_ushort(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
207 const GLint x[], const GLint y[], void *values)
208 {
209 GLushort *dst = (GLushort *) values;
210 GLuint i;
211 ASSERT(rb->DataType == GL_UNSIGNED_SHORT);
212 for (i = 0; i < count; i++) {
213 const GLushort *src = (GLushort *) rb->Data + y[i] * rb->RowStride + x[i];
214 dst[i] = *src;
215 }
216 }
217
218
219 static void
220 put_row_ushort(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
221 GLint x, GLint y, const void *values, const GLubyte *mask)
222 {
223 const GLushort *src = (const GLushort *) values;
224 GLushort *dst = (GLushort *) rb->Data + y * rb->RowStride + x;
225 ASSERT(rb->DataType == GL_UNSIGNED_SHORT);
226 if (mask) {
227 GLuint i;
228 for (i = 0; i < count; i++) {
229 if (mask[i]) {
230 dst[i] = src[i];
231 }
232 }
233 }
234 else {
235 memcpy(dst, src, count * sizeof(GLushort));
236 }
237 }
238
239
240 static void
241 put_mono_row_ushort(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
242 GLint x, GLint y, const void *value, const GLubyte *mask)
243 {
244 const GLushort val = *((const GLushort *) value);
245 GLushort *dst = (GLushort *) rb->Data + y * rb->RowStride + x;
246 ASSERT(rb->DataType == GL_UNSIGNED_SHORT);
247 if (mask) {
248 GLuint i;
249 for (i = 0; i < count; i++) {
250 if (mask[i]) {
251 dst[i] = val;
252 }
253 }
254 }
255 else {
256 GLuint i;
257 for (i = 0; i < count; i++) {
258 dst[i] = val;
259 }
260 }
261 }
262
263
264 static void
265 put_values_ushort(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
266 const GLint x[], const GLint y[], const void *values,
267 const GLubyte *mask)
268 {
269 const GLushort *src = (const GLushort *) values;
270 GLuint i;
271 ASSERT(rb->DataType == GL_UNSIGNED_SHORT);
272 for (i = 0; i < count; i++) {
273 if (!mask || mask[i]) {
274 GLushort *dst = (GLushort *) rb->Data + y[i] * rb->RowStride + x[i];
275 *dst = src[i];
276 }
277 }
278 }
279
280
281 static void
282 put_mono_values_ushort(struct gl_context *ctx, struct gl_renderbuffer *rb,
283 GLuint count, const GLint x[], const GLint y[],
284 const void *value, const GLubyte *mask)
285 {
286 const GLushort val = *((const GLushort *) value);
287 ASSERT(rb->DataType == GL_UNSIGNED_SHORT);
288 if (mask) {
289 GLuint i;
290 for (i = 0; i < count; i++) {
291 if (mask[i]) {
292 GLushort *dst = (GLushort *) rb->Data + y[i] * rb->RowStride + x[i];
293 *dst = val;
294 }
295 }
296 }
297 else {
298 GLuint i;
299 for (i = 0; i < count; i++) {
300 GLushort *dst = (GLushort *) rb->Data + y[i] * rb->RowStride + x[i];
301 *dst = val;
302 }
303 }
304 }
305
306
307 /**********************************************************************
308 * Functions for buffers of 1 X GLuint values.
309 * Typically depth/Z or color index.
310 */
311
312 static void *
313 get_pointer_uint(struct gl_context *ctx, struct gl_renderbuffer *rb,
314 GLint x, GLint y)
315 {
316 if (!rb->Data)
317 return NULL;
318 ASSERT(rb->DataType == GL_UNSIGNED_INT ||
319 rb->DataType == GL_UNSIGNED_INT_24_8_EXT);
320 return (GLuint *) rb->Data + y * rb->RowStride + x;
321 }
322
323
324 static void
325 get_row_uint(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
326 GLint x, GLint y, void *values)
327 {
328 const void *src = rb->GetPointer(ctx, rb, x, y);
329 ASSERT(rb->DataType == GL_UNSIGNED_INT ||
330 rb->DataType == GL_UNSIGNED_INT_24_8_EXT);
331 memcpy(values, src, count * sizeof(GLuint));
332 }
333
334
335 static void
336 get_values_uint(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
337 const GLint x[], const GLint y[], void *values)
338 {
339 GLuint *dst = (GLuint *) values;
340 GLuint i;
341 ASSERT(rb->DataType == GL_UNSIGNED_INT ||
342 rb->DataType == GL_UNSIGNED_INT_24_8_EXT);
343 for (i = 0; i < count; i++) {
344 const GLuint *src = (GLuint *) rb->Data + y[i] * rb->RowStride + x[i];
345 dst[i] = *src;
346 }
347 }
348
349
350 static void
351 put_row_uint(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
352 GLint x, GLint y, const void *values, const GLubyte *mask)
353 {
354 const GLuint *src = (const GLuint *) values;
355 GLuint *dst = (GLuint *) rb->Data + y * rb->RowStride + x;
356 ASSERT(rb->DataType == GL_UNSIGNED_INT ||
357 rb->DataType == GL_UNSIGNED_INT_24_8_EXT);
358 if (mask) {
359 GLuint i;
360 for (i = 0; i < count; i++) {
361 if (mask[i]) {
362 dst[i] = src[i];
363 }
364 }
365 }
366 else {
367 memcpy(dst, src, count * sizeof(GLuint));
368 }
369 }
370
371
372 static void
373 put_mono_row_uint(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
374 GLint x, GLint y, const void *value, const GLubyte *mask)
375 {
376 const GLuint val = *((const GLuint *) value);
377 GLuint *dst = (GLuint *) rb->Data + y * rb->RowStride + x;
378 ASSERT(rb->DataType == GL_UNSIGNED_INT ||
379 rb->DataType == GL_UNSIGNED_INT_24_8_EXT);
380 if (mask) {
381 GLuint i;
382 for (i = 0; i < count; i++) {
383 if (mask[i]) {
384 dst[i] = val;
385 }
386 }
387 }
388 else {
389 GLuint i;
390 for (i = 0; i < count; i++) {
391 dst[i] = val;
392 }
393 }
394 }
395
396
397 static void
398 put_values_uint(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
399 const GLint x[], const GLint y[], const void *values,
400 const GLubyte *mask)
401 {
402 const GLuint *src = (const GLuint *) values;
403 GLuint i;
404 ASSERT(rb->DataType == GL_UNSIGNED_INT ||
405 rb->DataType == GL_UNSIGNED_INT_24_8_EXT);
406 for (i = 0; i < count; i++) {
407 if (!mask || mask[i]) {
408 GLuint *dst = (GLuint *) rb->Data + y[i] * rb->RowStride + x[i];
409 *dst = src[i];
410 }
411 }
412 }
413
414
415 static void
416 put_mono_values_uint(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
417 const GLint x[], const GLint y[], const void *value,
418 const GLubyte *mask)
419 {
420 const GLuint val = *((const GLuint *) value);
421 GLuint i;
422 ASSERT(rb->DataType == GL_UNSIGNED_INT ||
423 rb->DataType == GL_UNSIGNED_INT_24_8_EXT);
424 for (i = 0; i < count; i++) {
425 if (!mask || mask[i]) {
426 GLuint *dst = (GLuint *) rb->Data + y[i] * rb->RowStride + x[i];
427 *dst = val;
428 }
429 }
430 }
431
432
433 /**********************************************************************
434 * Functions for buffers of 3 X GLubyte (or GLbyte) values.
435 * Typically color buffers.
436 * NOTE: the incoming and outgoing colors are RGBA! We ignore incoming
437 * alpha values and return 255 for outgoing alpha values.
438 */
439
440 static void *
441 get_pointer_ubyte3(struct gl_context *ctx, struct gl_renderbuffer *rb,
442 GLint x, GLint y)
443 {
444 ASSERT(rb->Format == MESA_FORMAT_RGB888);
445 /* No direct access since this buffer is RGB but caller will be
446 * treating it as if it were RGBA.
447 */
448 return NULL;
449 }
450
451
452 static void
453 get_row_ubyte3(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
454 GLint x, GLint y, void *values)
455 {
456 const GLubyte *src = (const GLubyte *) (rb->Data +
457 3 * (y * rb->RowStride + x));
458 GLubyte *dst = (GLubyte *) values;
459 GLuint i;
460 ASSERT(rb->Format == MESA_FORMAT_RGB888);
461 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
462 for (i = 0; i < count; i++) {
463 dst[i * 4 + 0] = src[i * 3 + 0];
464 dst[i * 4 + 1] = src[i * 3 + 1];
465 dst[i * 4 + 2] = src[i * 3 + 2];
466 dst[i * 4 + 3] = 255;
467 }
468 }
469
470
471 static void
472 get_values_ubyte3(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
473 const GLint x[], const GLint y[], void *values)
474 {
475 GLubyte *dst = (GLubyte *) values;
476 GLuint i;
477 ASSERT(rb->Format == MESA_FORMAT_RGB888);
478 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
479 for (i = 0; i < count; i++) {
480 const GLubyte *src
481 = (GLubyte *) rb->Data + 3 * (y[i] * rb->RowStride + x[i]);
482 dst[i * 4 + 0] = src[0];
483 dst[i * 4 + 1] = src[1];
484 dst[i * 4 + 2] = src[2];
485 dst[i * 4 + 3] = 255;
486 }
487 }
488
489
490 static void
491 put_row_ubyte3(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
492 GLint x, GLint y, const void *values, const GLubyte *mask)
493 {
494 /* note: incoming values are RGB+A! */
495 const GLubyte *src = (const GLubyte *) values;
496 GLubyte *dst = (GLubyte *) rb->Data + 3 * (y * rb->RowStride + x);
497 GLuint i;
498 ASSERT(rb->Format == MESA_FORMAT_RGB888);
499 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
500 for (i = 0; i < count; i++) {
501 if (!mask || mask[i]) {
502 dst[i * 3 + 0] = src[i * 4 + 0];
503 dst[i * 3 + 1] = src[i * 4 + 1];
504 dst[i * 3 + 2] = src[i * 4 + 2];
505 }
506 }
507 }
508
509
510 static void
511 put_row_rgb_ubyte3(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
512 GLint x, GLint y, const void *values, const GLubyte *mask)
513 {
514 /* note: incoming values are RGB+A! */
515 const GLubyte *src = (const GLubyte *) values;
516 GLubyte *dst = (GLubyte *) rb->Data + 3 * (y * rb->RowStride + x);
517 GLuint i;
518 ASSERT(rb->Format == MESA_FORMAT_RGB888);
519 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
520 for (i = 0; i < count; i++) {
521 if (!mask || mask[i]) {
522 dst[i * 3 + 0] = src[i * 3 + 0];
523 dst[i * 3 + 1] = src[i * 3 + 1];
524 dst[i * 3 + 2] = src[i * 3 + 2];
525 }
526 }
527 }
528
529
530 static void
531 put_mono_row_ubyte3(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
532 GLint x, GLint y, const void *value, const GLubyte *mask)
533 {
534 /* note: incoming value is RGB+A! */
535 const GLubyte val0 = ((const GLubyte *) value)[0];
536 const GLubyte val1 = ((const GLubyte *) value)[1];
537 const GLubyte val2 = ((const GLubyte *) value)[2];
538 GLubyte *dst = (GLubyte *) rb->Data + 3 * (y * rb->RowStride + x);
539 ASSERT(rb->Format == MESA_FORMAT_RGB888);
540 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
541 if (!mask && val0 == val1 && val1 == val2) {
542 /* optimized case */
543 memset(dst, val0, 3 * count);
544 }
545 else {
546 GLuint i;
547 for (i = 0; i < count; i++) {
548 if (!mask || mask[i]) {
549 dst[i * 3 + 0] = val0;
550 dst[i * 3 + 1] = val1;
551 dst[i * 3 + 2] = val2;
552 }
553 }
554 }
555 }
556
557
558 static void
559 put_values_ubyte3(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
560 const GLint x[], const GLint y[], const void *values,
561 const GLubyte *mask)
562 {
563 /* note: incoming values are RGB+A! */
564 const GLubyte *src = (const GLubyte *) values;
565 GLuint i;
566 ASSERT(rb->Format == MESA_FORMAT_RGB888);
567 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
568 for (i = 0; i < count; i++) {
569 if (!mask || mask[i]) {
570 GLubyte *dst = (GLubyte *) rb->Data + 3 * (y[i] * rb->RowStride + x[i]);
571 dst[0] = src[i * 4 + 0];
572 dst[1] = src[i * 4 + 1];
573 dst[2] = src[i * 4 + 2];
574 }
575 }
576 }
577
578
579 static void
580 put_mono_values_ubyte3(struct gl_context *ctx, struct gl_renderbuffer *rb,
581 GLuint count, const GLint x[], const GLint y[],
582 const void *value, const GLubyte *mask)
583 {
584 /* note: incoming value is RGB+A! */
585 const GLubyte val0 = ((const GLubyte *) value)[0];
586 const GLubyte val1 = ((const GLubyte *) value)[1];
587 const GLubyte val2 = ((const GLubyte *) value)[2];
588 GLuint i;
589 ASSERT(rb->Format == MESA_FORMAT_RGB888);
590 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
591 for (i = 0; i < count; i++) {
592 if (!mask || mask[i]) {
593 GLubyte *dst = (GLubyte *) (rb->Data +
594 3 * (y[i] * rb->RowStride + x[i]));
595 dst[0] = val0;
596 dst[1] = val1;
597 dst[2] = val2;
598 }
599 }
600 }
601
602
603 /**********************************************************************
604 * Functions for buffers of 4 X GLubyte (or GLbyte) values.
605 * Typically color buffers.
606 */
607
608 static void *
609 get_pointer_ubyte4(struct gl_context *ctx, struct gl_renderbuffer *rb,
610 GLint x, GLint y)
611 {
612 if (!rb->Data)
613 return NULL;
614 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
615 ASSERT(rb->Format == MESA_FORMAT_RGBA8888);
616 return (GLubyte *) rb->Data + 4 * (y * rb->RowStride + x);
617 }
618
619
620 static void
621 get_row_ubyte4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
622 GLint x, GLint y, void *values)
623 {
624 const GLubyte *src = (const GLubyte *) (rb->Data +
625 4 * (y * rb->RowStride + x));
626 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
627 ASSERT(rb->Format == MESA_FORMAT_RGBA8888);
628 memcpy(values, src, 4 * count * sizeof(GLubyte));
629 }
630
631
632 static void
633 get_values_ubyte4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
634 const GLint x[], const GLint y[], void *values)
635 {
636 /* treat 4*GLubyte as 1*GLuint */
637 GLuint *dst = (GLuint *) values;
638 GLuint i;
639 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
640 ASSERT(rb->Format == MESA_FORMAT_RGBA8888);
641 for (i = 0; i < count; i++) {
642 const GLuint *src = (GLuint *) rb->Data + (y[i] * rb->RowStride + x[i]);
643 dst[i] = *src;
644 }
645 }
646
647
648 static void
649 put_row_ubyte4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
650 GLint x, GLint y, const void *values, const GLubyte *mask)
651 {
652 /* treat 4*GLubyte as 1*GLuint */
653 const GLuint *src = (const GLuint *) values;
654 GLuint *dst = (GLuint *) rb->Data + (y * rb->RowStride + x);
655 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
656 ASSERT(rb->Format == MESA_FORMAT_RGBA8888);
657 if (mask) {
658 GLuint i;
659 for (i = 0; i < count; i++) {
660 if (mask[i]) {
661 dst[i] = src[i];
662 }
663 }
664 }
665 else {
666 memcpy(dst, src, 4 * count * sizeof(GLubyte));
667 }
668 }
669
670
671 static void
672 put_row_rgb_ubyte4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
673 GLint x, GLint y, const void *values, const GLubyte *mask)
674 {
675 /* Store RGB values in RGBA buffer */
676 const GLubyte *src = (const GLubyte *) values;
677 GLubyte *dst = (GLubyte *) rb->Data + 4 * (y * rb->RowStride + x);
678 GLuint i;
679 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
680 ASSERT(rb->Format == MESA_FORMAT_RGBA8888);
681 for (i = 0; i < count; i++) {
682 if (!mask || mask[i]) {
683 dst[i * 4 + 0] = src[i * 3 + 0];
684 dst[i * 4 + 1] = src[i * 3 + 1];
685 dst[i * 4 + 2] = src[i * 3 + 2];
686 dst[i * 4 + 3] = 0xff;
687 }
688 }
689 }
690
691
692 static void
693 put_mono_row_ubyte4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
694 GLint x, GLint y, const void *value, const GLubyte *mask)
695 {
696 /* treat 4*GLubyte as 1*GLuint */
697 const GLuint val = *((const GLuint *) value);
698 GLuint *dst = (GLuint *) rb->Data + (y * rb->RowStride + x);
699 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
700 ASSERT(rb->Format == MESA_FORMAT_RGBA8888);
701 if (!mask && val == 0) {
702 /* common case */
703 memset(dst, 0, count * 4 * sizeof(GLubyte));
704 }
705 else {
706 /* general case */
707 if (mask) {
708 GLuint i;
709 for (i = 0; i < count; i++) {
710 if (mask[i]) {
711 dst[i] = val;
712 }
713 }
714 }
715 else {
716 GLuint i;
717 for (i = 0; i < count; i++) {
718 dst[i] = val;
719 }
720 }
721 }
722 }
723
724
725 static void
726 put_values_ubyte4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
727 const GLint x[], const GLint y[], const void *values,
728 const GLubyte *mask)
729 {
730 /* treat 4*GLubyte as 1*GLuint */
731 const GLuint *src = (const GLuint *) values;
732 GLuint i;
733 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
734 ASSERT(rb->Format == MESA_FORMAT_RGBA8888);
735 for (i = 0; i < count; i++) {
736 if (!mask || mask[i]) {
737 GLuint *dst = (GLuint *) rb->Data + (y[i] * rb->RowStride + x[i]);
738 *dst = src[i];
739 }
740 }
741 }
742
743
744 static void
745 put_mono_values_ubyte4(struct gl_context *ctx, struct gl_renderbuffer *rb,
746 GLuint count, const GLint x[], const GLint y[],
747 const void *value, const GLubyte *mask)
748 {
749 /* treat 4*GLubyte as 1*GLuint */
750 const GLuint val = *((const GLuint *) value);
751 GLuint i;
752 ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
753 ASSERT(rb->Format == MESA_FORMAT_RGBA8888);
754 for (i = 0; i < count; i++) {
755 if (!mask || mask[i]) {
756 GLuint *dst = (GLuint *) rb->Data + (y[i] * rb->RowStride + x[i]);
757 *dst = val;
758 }
759 }
760 }
761
762
763 /**********************************************************************
764 * Functions for buffers of 4 X GLushort (or GLshort) values.
765 * Typically accum buffer.
766 */
767
768 static void *
769 get_pointer_ushort4(struct gl_context *ctx, struct gl_renderbuffer *rb,
770 GLint x, GLint y)
771 {
772 if (!rb->Data)
773 return NULL;
774 ASSERT(rb->DataType == GL_UNSIGNED_SHORT || rb->DataType == GL_SHORT);
775 return (GLushort *) rb->Data + 4 * (y * rb->RowStride + x);
776 }
777
778
779 static void
780 get_row_ushort4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
781 GLint x, GLint y, void *values)
782 {
783 const GLshort *src = (const GLshort *) (rb->Data +
784 4 * (y * rb->RowStride + x));
785 ASSERT(rb->DataType == GL_UNSIGNED_SHORT || rb->DataType == GL_SHORT);
786 memcpy(values, src, 4 * count * sizeof(GLshort));
787 }
788
789
790 static void
791 get_values_ushort4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
792 const GLint x[], const GLint y[], void *values)
793 {
794 GLushort *dst = (GLushort *) values;
795 GLuint i;
796 ASSERT(rb->DataType == GL_UNSIGNED_SHORT || rb->DataType == GL_SHORT);
797 for (i = 0; i < count; i++) {
798 const GLushort *src
799 = (GLushort *) rb->Data + 4 * (y[i] * rb->RowStride + x[i]);
800 dst[i] = *src;
801 }
802 }
803
804
805 static void
806 put_row_ushort4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
807 GLint x, GLint y, const void *values, const GLubyte *mask)
808 {
809 const GLushort *src = (const GLushort *) values;
810 GLushort *dst = (GLushort *) rb->Data + 4 * (y * rb->RowStride + x);
811 ASSERT(rb->DataType == GL_UNSIGNED_SHORT || rb->DataType == GL_SHORT);
812 if (mask) {
813 GLuint i;
814 for (i = 0; i < count; i++) {
815 if (mask[i]) {
816 dst[i * 4 + 0] = src[i * 4 + 0];
817 dst[i * 4 + 1] = src[i * 4 + 1];
818 dst[i * 4 + 2] = src[i * 4 + 2];
819 dst[i * 4 + 3] = src[i * 4 + 3];
820 }
821 }
822 }
823 else {
824 memcpy(dst, src, 4 * count * sizeof(GLushort));
825 }
826 }
827
828
829 static void
830 put_row_rgb_ushort4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
831 GLint x, GLint y, const void *values, const GLubyte *mask)
832 {
833 /* Put RGB values in RGBA buffer */
834 const GLushort *src = (const GLushort *) values;
835 GLushort *dst = (GLushort *) rb->Data + 4 * (y * rb->RowStride + x);
836 ASSERT(rb->DataType == GL_UNSIGNED_SHORT || rb->DataType == GL_SHORT);
837 if (mask) {
838 GLuint i;
839 for (i = 0; i < count; i++) {
840 if (mask[i]) {
841 dst[i * 4 + 0] = src[i * 3 + 0];
842 dst[i * 4 + 1] = src[i * 3 + 1];
843 dst[i * 4 + 2] = src[i * 3 + 2];
844 dst[i * 4 + 3] = 0xffff;
845 }
846 }
847 }
848 else {
849 memcpy(dst, src, 4 * count * sizeof(GLushort));
850 }
851 }
852
853
854 static void
855 put_mono_row_ushort4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
856 GLint x, GLint y, const void *value, const GLubyte *mask)
857 {
858 const GLushort val0 = ((const GLushort *) value)[0];
859 const GLushort val1 = ((const GLushort *) value)[1];
860 const GLushort val2 = ((const GLushort *) value)[2];
861 const GLushort val3 = ((const GLushort *) value)[3];
862 GLushort *dst = (GLushort *) rb->Data + 4 * (y * rb->RowStride + x);
863 ASSERT(rb->DataType == GL_UNSIGNED_SHORT || rb->DataType == GL_SHORT);
864 if (!mask && val0 == 0 && val1 == 0 && val2 == 0 && val3 == 0) {
865 /* common case for clearing accum buffer */
866 memset(dst, 0, count * 4 * sizeof(GLushort));
867 }
868 else {
869 GLuint i;
870 for (i = 0; i < count; i++) {
871 if (!mask || mask[i]) {
872 dst[i * 4 + 0] = val0;
873 dst[i * 4 + 1] = val1;
874 dst[i * 4 + 2] = val2;
875 dst[i * 4 + 3] = val3;
876 }
877 }
878 }
879 }
880
881
882 static void
883 put_values_ushort4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
884 const GLint x[], const GLint y[], const void *values,
885 const GLubyte *mask)
886 {
887 const GLushort *src = (const GLushort *) values;
888 GLuint i;
889 ASSERT(rb->DataType == GL_UNSIGNED_SHORT || rb->DataType == GL_SHORT);
890 for (i = 0; i < count; i++) {
891 if (!mask || mask[i]) {
892 GLushort *dst = (GLushort *) (rb->Data + 4 *
893 (y[i] * rb->RowStride + x[i]));
894 dst[0] = src[i * 4 + 0];
895 dst[1] = src[i * 4 + 1];
896 dst[2] = src[i * 4 + 2];
897 dst[3] = src[i * 4 + 3];
898 }
899 }
900 }
901
902
903 static void
904 put_mono_values_ushort4(struct gl_context *ctx, struct gl_renderbuffer *rb,
905 GLuint count, const GLint x[], const GLint y[],
906 const void *value, const GLubyte *mask)
907 {
908 const GLushort val0 = ((const GLushort *) value)[0];
909 const GLushort val1 = ((const GLushort *) value)[1];
910 const GLushort val2 = ((const GLushort *) value)[2];
911 const GLushort val3 = ((const GLushort *) value)[3];
912 GLuint i;
913 ASSERT(rb->DataType == GL_UNSIGNED_SHORT || rb->DataType == GL_SHORT);
914 for (i = 0; i < count; i++) {
915 if (!mask || mask[i]) {
916 GLushort *dst = (GLushort *) (rb->Data +
917 4 * (y[i] * rb->RowStride + x[i]));
918 dst[0] = val0;
919 dst[1] = val1;
920 dst[2] = val2;
921 dst[3] = val3;
922 }
923 }
924 }
925
926
927
928 /**
929 * This is a software fallback for the gl_renderbuffer->AllocStorage
930 * function.
931 * Device drivers will typically override this function for the buffers
932 * which it manages (typically color buffers, Z and stencil).
933 * Other buffers (like software accumulation and aux buffers) which the driver
934 * doesn't manage can be handled with this function.
935 *
936 * This one multi-purpose function can allocate stencil, depth, accum, color
937 * or color-index buffers!
938 *
939 * This function also plugs in the appropriate GetPointer, Get/PutRow and
940 * Get/PutValues functions.
941 */
942 GLboolean
943 _mesa_soft_renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
944 GLenum internalFormat,
945 GLuint width, GLuint height)
946 {
947 GLuint pixelSize;
948
949 switch (internalFormat) {
950 case GL_RGB:
951 case GL_R3_G3_B2:
952 case GL_RGB4:
953 case GL_RGB5:
954 case GL_RGB8:
955 case GL_RGB10:
956 case GL_RGB12:
957 case GL_RGB16:
958 rb->Format = MESA_FORMAT_RGB888;
959 rb->DataType = GL_UNSIGNED_BYTE;
960 rb->GetPointer = get_pointer_ubyte3;
961 rb->GetRow = get_row_ubyte3;
962 rb->GetValues = get_values_ubyte3;
963 rb->PutRow = put_row_ubyte3;
964 rb->PutRowRGB = put_row_rgb_ubyte3;
965 rb->PutMonoRow = put_mono_row_ubyte3;
966 rb->PutValues = put_values_ubyte3;
967 rb->PutMonoValues = put_mono_values_ubyte3;
968 pixelSize = 3 * sizeof(GLubyte);
969 break;
970 case GL_RGBA:
971 case GL_RGBA2:
972 case GL_RGBA4:
973 case GL_RGB5_A1:
974 case GL_RGBA8:
975 #if 1
976 case GL_RGB10_A2:
977 case GL_RGBA12:
978 #endif
979 rb->Format = MESA_FORMAT_RGBA8888;
980 rb->DataType = GL_UNSIGNED_BYTE;
981 rb->GetPointer = get_pointer_ubyte4;
982 rb->GetRow = get_row_ubyte4;
983 rb->GetValues = get_values_ubyte4;
984 rb->PutRow = put_row_ubyte4;
985 rb->PutRowRGB = put_row_rgb_ubyte4;
986 rb->PutMonoRow = put_mono_row_ubyte4;
987 rb->PutValues = put_values_ubyte4;
988 rb->PutMonoValues = put_mono_values_ubyte4;
989 pixelSize = 4 * sizeof(GLubyte);
990 break;
991 case GL_RGBA16:
992 case GL_RGBA16_SNORM:
993 /* for accum buffer */
994 rb->Format = MESA_FORMAT_SIGNED_RGBA_16;
995 rb->DataType = GL_SHORT;
996 rb->GetPointer = get_pointer_ushort4;
997 rb->GetRow = get_row_ushort4;
998 rb->GetValues = get_values_ushort4;
999 rb->PutRow = put_row_ushort4;
1000 rb->PutRowRGB = put_row_rgb_ushort4;
1001 rb->PutMonoRow = put_mono_row_ushort4;
1002 rb->PutValues = put_values_ushort4;
1003 rb->PutMonoValues = put_mono_values_ushort4;
1004 pixelSize = 4 * sizeof(GLushort);
1005 break;
1006 #if 0
1007 case GL_ALPHA8:
1008 rb->Format = MESA_FORMAT_A8;
1009 rb->DataType = GL_UNSIGNED_BYTE;
1010 rb->GetPointer = get_pointer_alpha8;
1011 rb->GetRow = get_row_alpha8;
1012 rb->GetValues = get_values_alpha8;
1013 rb->PutRow = put_row_alpha8;
1014 rb->PutRowRGB = NULL;
1015 rb->PutMonoRow = put_mono_row_alpha8;
1016 rb->PutValues = put_values_alpha8;
1017 rb->PutMonoValues = put_mono_values_alpha8;
1018 pixelSize = sizeof(GLubyte);
1019 break;
1020 #endif
1021 case GL_STENCIL_INDEX:
1022 case GL_STENCIL_INDEX1_EXT:
1023 case GL_STENCIL_INDEX4_EXT:
1024 case GL_STENCIL_INDEX8_EXT:
1025 case GL_STENCIL_INDEX16_EXT:
1026 rb->Format = MESA_FORMAT_S8;
1027 rb->DataType = GL_UNSIGNED_BYTE;
1028 rb->GetPointer = get_pointer_ubyte;
1029 rb->GetRow = get_row_ubyte;
1030 rb->GetValues = get_values_ubyte;
1031 rb->PutRow = put_row_ubyte;
1032 rb->PutRowRGB = NULL;
1033 rb->PutMonoRow = put_mono_row_ubyte;
1034 rb->PutValues = put_values_ubyte;
1035 rb->PutMonoValues = put_mono_values_ubyte;
1036 pixelSize = sizeof(GLubyte);
1037 break;
1038 case GL_DEPTH_COMPONENT:
1039 case GL_DEPTH_COMPONENT16:
1040 rb->Format = MESA_FORMAT_Z16;
1041 rb->DataType = GL_UNSIGNED_SHORT;
1042 rb->GetPointer = get_pointer_ushort;
1043 rb->GetRow = get_row_ushort;
1044 rb->GetValues = get_values_ushort;
1045 rb->PutRow = put_row_ushort;
1046 rb->PutRowRGB = NULL;
1047 rb->PutMonoRow = put_mono_row_ushort;
1048 rb->PutValues = put_values_ushort;
1049 rb->PutMonoValues = put_mono_values_ushort;
1050 pixelSize = sizeof(GLushort);
1051 break;
1052 case GL_DEPTH_COMPONENT24:
1053 rb->DataType = GL_UNSIGNED_INT;
1054 rb->GetPointer = get_pointer_uint;
1055 rb->GetRow = get_row_uint;
1056 rb->GetValues = get_values_uint;
1057 rb->PutRow = put_row_uint;
1058 rb->PutRowRGB = NULL;
1059 rb->PutMonoRow = put_mono_row_uint;
1060 rb->PutValues = put_values_uint;
1061 rb->PutMonoValues = put_mono_values_uint;
1062 rb->Format = MESA_FORMAT_X8_Z24;
1063 pixelSize = sizeof(GLuint);
1064 break;
1065 case GL_DEPTH_COMPONENT32:
1066 rb->DataType = GL_UNSIGNED_INT;
1067 rb->GetPointer = get_pointer_uint;
1068 rb->GetRow = get_row_uint;
1069 rb->GetValues = get_values_uint;
1070 rb->PutRow = put_row_uint;
1071 rb->PutRowRGB = NULL;
1072 rb->PutMonoRow = put_mono_row_uint;
1073 rb->PutValues = put_values_uint;
1074 rb->PutMonoValues = put_mono_values_uint;
1075 rb->Format = MESA_FORMAT_Z32;
1076 pixelSize = sizeof(GLuint);
1077 break;
1078 case GL_DEPTH_STENCIL_EXT:
1079 case GL_DEPTH24_STENCIL8_EXT:
1080 rb->Format = MESA_FORMAT_Z24_S8;
1081 rb->DataType = GL_UNSIGNED_INT_24_8_EXT;
1082 rb->GetPointer = get_pointer_uint;
1083 rb->GetRow = get_row_uint;
1084 rb->GetValues = get_values_uint;
1085 rb->PutRow = put_row_uint;
1086 rb->PutRowRGB = NULL;
1087 rb->PutMonoRow = put_mono_row_uint;
1088 rb->PutValues = put_values_uint;
1089 rb->PutMonoValues = put_mono_values_uint;
1090 pixelSize = sizeof(GLuint);
1091 break;
1092 default:
1093 /* unsupported format */
1094 return GL_FALSE;
1095 }
1096
1097 ASSERT(rb->DataType);
1098 ASSERT(rb->GetPointer);
1099 ASSERT(rb->GetRow);
1100 ASSERT(rb->GetValues);
1101 ASSERT(rb->PutRow);
1102 ASSERT(rb->PutMonoRow);
1103 ASSERT(rb->PutValues);
1104 ASSERT(rb->PutMonoValues);
1105
1106 /* free old buffer storage */
1107 if (rb->Data) {
1108 free(rb->Data);
1109 rb->Data = NULL;
1110 }
1111
1112 rb->RowStride = width;
1113
1114 if (width > 0 && height > 0) {
1115 /* allocate new buffer storage */
1116 rb->Data = malloc(width * height * pixelSize);
1117
1118 if (rb->Data == NULL) {
1119 rb->Width = 0;
1120 rb->Height = 0;
1121 rb->RowStride = 0;
1122 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1123 "software renderbuffer allocation (%d x %d x %d)",
1124 width, height, pixelSize);
1125 return GL_FALSE;
1126 }
1127 }
1128
1129 rb->Width = width;
1130 rb->Height = height;
1131 rb->_BaseFormat = _mesa_base_fbo_format(ctx, internalFormat);
1132
1133 if (rb->Name == 0 &&
1134 internalFormat == GL_RGBA16_SNORM &&
1135 rb->_BaseFormat == 0) {
1136 /* NOTE: This is a special case just for accumulation buffers.
1137 * This is a very limited use case- there's no snorm texturing or
1138 * rendering going on.
1139 */
1140 rb->_BaseFormat = GL_RGBA;
1141 }
1142 else {
1143 /* the internalFormat should have been error checked long ago */
1144 ASSERT(rb->_BaseFormat);
1145 }
1146
1147 return GL_TRUE;
1148 }
1149
1150
1151
1152 /**********************************************************************/
1153 /**********************************************************************/
1154 /**********************************************************************/
1155
1156
1157 /**
1158 * Here we utilize the gl_renderbuffer->Wrapper field to put an alpha
1159 * buffer wrapper around an existing RGB renderbuffer (hw or sw).
1160 *
1161 * When PutRow is called (for example), we store the alpha values in
1162 * this buffer, then pass on the PutRow call to the wrapped RGB
1163 * buffer.
1164 */
1165
1166
1167 static GLboolean
1168 alloc_storage_alpha8(struct gl_context *ctx, struct gl_renderbuffer *arb,
1169 GLenum internalFormat, GLuint width, GLuint height)
1170 {
1171 ASSERT(arb != arb->Wrapped);
1172 ASSERT(arb->Format == MESA_FORMAT_A8);
1173
1174 /* first, pass the call to the wrapped RGB buffer */
1175 if (!arb->Wrapped->AllocStorage(ctx, arb->Wrapped, internalFormat,
1176 width, height)) {
1177 return GL_FALSE;
1178 }
1179
1180 /* next, resize my alpha buffer */
1181 if (arb->Data) {
1182 free(arb->Data);
1183 }
1184
1185 arb->Data = malloc(width * height * sizeof(GLubyte));
1186 if (arb->Data == NULL) {
1187 arb->Width = 0;
1188 arb->Height = 0;
1189 _mesa_error(ctx, GL_OUT_OF_MEMORY, "software alpha buffer allocation");
1190 return GL_FALSE;
1191 }
1192
1193 arb->Width = width;
1194 arb->Height = height;
1195 arb->RowStride = width;
1196
1197 return GL_TRUE;
1198 }
1199
1200
1201 /**
1202 * Delete an alpha_renderbuffer object, as well as the wrapped RGB buffer.
1203 */
1204 static void
1205 delete_renderbuffer_alpha8(struct gl_renderbuffer *arb)
1206 {
1207 if (arb->Data) {
1208 free(arb->Data);
1209 }
1210 ASSERT(arb->Wrapped);
1211 ASSERT(arb != arb->Wrapped);
1212 arb->Wrapped->Delete(arb->Wrapped);
1213 arb->Wrapped = NULL;
1214 free(arb);
1215 }
1216
1217
1218 static void *
1219 get_pointer_alpha8(struct gl_context *ctx, struct gl_renderbuffer *arb,
1220 GLint x, GLint y)
1221 {
1222 return NULL; /* don't allow direct access! */
1223 }
1224
1225
1226 static void
1227 get_row_alpha8(struct gl_context *ctx, struct gl_renderbuffer *arb, GLuint count,
1228 GLint x, GLint y, void *values)
1229 {
1230 /* NOTE: 'values' is RGBA format! */
1231 const GLubyte *src = (const GLubyte *) arb->Data + y * arb->RowStride + x;
1232 GLubyte *dst = (GLubyte *) values;
1233 GLuint i;
1234 ASSERT(arb != arb->Wrapped);
1235 ASSERT(arb->DataType == GL_UNSIGNED_BYTE);
1236 /* first, pass the call to the wrapped RGB buffer */
1237 arb->Wrapped->GetRow(ctx, arb->Wrapped, count, x, y, values);
1238 /* second, fill in alpha values from this buffer! */
1239 for (i = 0; i < count; i++) {
1240 dst[i * 4 + 3] = src[i];
1241 }
1242 }
1243
1244
1245 static void
1246 get_values_alpha8(struct gl_context *ctx, struct gl_renderbuffer *arb, GLuint count,
1247 const GLint x[], const GLint y[], void *values)
1248 {
1249 GLubyte *dst = (GLubyte *) values;
1250 GLuint i;
1251 ASSERT(arb != arb->Wrapped);
1252 ASSERT(arb->DataType == GL_UNSIGNED_BYTE);
1253 /* first, pass the call to the wrapped RGB buffer */
1254 arb->Wrapped->GetValues(ctx, arb->Wrapped, count, x, y, values);
1255 /* second, fill in alpha values from this buffer! */
1256 for (i = 0; i < count; i++) {
1257 const GLubyte *src = (GLubyte *) arb->Data + y[i] * arb->RowStride + x[i];
1258 dst[i * 4 + 3] = *src;
1259 }
1260 }
1261
1262
1263 static void
1264 put_row_alpha8(struct gl_context *ctx, struct gl_renderbuffer *arb, GLuint count,
1265 GLint x, GLint y, const void *values, const GLubyte *mask)
1266 {
1267 const GLubyte *src = (const GLubyte *) values;
1268 GLubyte *dst = (GLubyte *) arb->Data + y * arb->RowStride + x;
1269 GLuint i;
1270 ASSERT(arb != arb->Wrapped);
1271 ASSERT(arb->DataType == GL_UNSIGNED_BYTE);
1272 /* first, pass the call to the wrapped RGB buffer */
1273 arb->Wrapped->PutRow(ctx, arb->Wrapped, count, x, y, values, mask);
1274 /* second, store alpha in our buffer */
1275 for (i = 0; i < count; i++) {
1276 if (!mask || mask[i]) {
1277 dst[i] = src[i * 4 + 3];
1278 }
1279 }
1280 }
1281
1282
1283 static void
1284 put_row_rgb_alpha8(struct gl_context *ctx, struct gl_renderbuffer *arb, GLuint count,
1285 GLint x, GLint y, const void *values, const GLubyte *mask)
1286 {
1287 const GLubyte *src = (const GLubyte *) values;
1288 GLubyte *dst = (GLubyte *) arb->Data + y * arb->RowStride + x;
1289 GLuint i;
1290 ASSERT(arb != arb->Wrapped);
1291 ASSERT(arb->DataType == GL_UNSIGNED_BYTE);
1292 /* first, pass the call to the wrapped RGB buffer */
1293 arb->Wrapped->PutRowRGB(ctx, arb->Wrapped, count, x, y, values, mask);
1294 /* second, store alpha in our buffer */
1295 for (i = 0; i < count; i++) {
1296 if (!mask || mask[i]) {
1297 dst[i] = src[i * 4 + 3];
1298 }
1299 }
1300 }
1301
1302
1303 static void
1304 put_mono_row_alpha8(struct gl_context *ctx, struct gl_renderbuffer *arb, GLuint count,
1305 GLint x, GLint y, const void *value, const GLubyte *mask)
1306 {
1307 const GLubyte val = ((const GLubyte *) value)[3];
1308 GLubyte *dst = (GLubyte *) arb->Data + y * arb->RowStride + x;
1309 ASSERT(arb != arb->Wrapped);
1310 ASSERT(arb->DataType == GL_UNSIGNED_BYTE);
1311 /* first, pass the call to the wrapped RGB buffer */
1312 arb->Wrapped->PutMonoRow(ctx, arb->Wrapped, count, x, y, value, mask);
1313 /* second, store alpha in our buffer */
1314 if (mask) {
1315 GLuint i;
1316 for (i = 0; i < count; i++) {
1317 if (mask[i]) {
1318 dst[i] = val;
1319 }
1320 }
1321 }
1322 else {
1323 memset(dst, val, count);
1324 }
1325 }
1326
1327
1328 static void
1329 put_values_alpha8(struct gl_context *ctx, struct gl_renderbuffer *arb, GLuint count,
1330 const GLint x[], const GLint y[],
1331 const void *values, const GLubyte *mask)
1332 {
1333 const GLubyte *src = (const GLubyte *) values;
1334 GLuint i;
1335 ASSERT(arb != arb->Wrapped);
1336 ASSERT(arb->DataType == GL_UNSIGNED_BYTE);
1337 /* first, pass the call to the wrapped RGB buffer */
1338 arb->Wrapped->PutValues(ctx, arb->Wrapped, count, x, y, values, mask);
1339 /* second, store alpha in our buffer */
1340 for (i = 0; i < count; i++) {
1341 if (!mask || mask[i]) {
1342 GLubyte *dst = (GLubyte *) arb->Data + y[i] * arb->RowStride + x[i];
1343 *dst = src[i * 4 + 3];
1344 }
1345 }
1346 }
1347
1348
1349 static void
1350 put_mono_values_alpha8(struct gl_context *ctx, struct gl_renderbuffer *arb,
1351 GLuint count, const GLint x[], const GLint y[],
1352 const void *value, const GLubyte *mask)
1353 {
1354 const GLubyte val = ((const GLubyte *) value)[3];
1355 GLuint i;
1356 ASSERT(arb != arb->Wrapped);
1357 ASSERT(arb->DataType == GL_UNSIGNED_BYTE);
1358 /* first, pass the call to the wrapped RGB buffer */
1359 arb->Wrapped->PutValues(ctx, arb->Wrapped, count, x, y, value, mask);
1360 /* second, store alpha in our buffer */
1361 for (i = 0; i < count; i++) {
1362 if (!mask || mask[i]) {
1363 GLubyte *dst = (GLubyte *) arb->Data + y[i] * arb->RowStride + x[i];
1364 *dst = val;
1365 }
1366 }
1367 }
1368
1369
1370 static void
1371 copy_buffer_alpha8(struct gl_renderbuffer* dst, struct gl_renderbuffer* src)
1372 {
1373 ASSERT(dst->Format == MESA_FORMAT_A8);
1374 ASSERT(src->Format == MESA_FORMAT_A8);
1375 ASSERT(dst->Width == src->Width);
1376 ASSERT(dst->Height == src->Height);
1377 ASSERT(dst->RowStride == src->RowStride);
1378
1379 memcpy(dst->Data, src->Data, dst->RowStride * dst->Height * sizeof(GLubyte));
1380 }
1381
1382
1383 /**********************************************************************/
1384 /**********************************************************************/
1385 /**********************************************************************/
1386
1387
1388 /**
1389 * Default GetPointer routine. Always return NULL to indicate that
1390 * direct buffer access is not supported.
1391 */
1392 static void *
1393 nop_get_pointer(struct gl_context *ctx, struct gl_renderbuffer *rb, GLint x, GLint y)
1394 {
1395 return NULL;
1396 }
1397
1398
1399 /**
1400 * Initialize the fields of a gl_renderbuffer to default values.
1401 */
1402 void
1403 _mesa_init_renderbuffer(struct gl_renderbuffer *rb, GLuint name)
1404 {
1405 _glthread_INIT_MUTEX(rb->Mutex);
1406
1407 rb->ClassID = 0;
1408 rb->Name = name;
1409 rb->RefCount = 0;
1410 rb->Delete = _mesa_delete_renderbuffer;
1411
1412 /* The rest of these should be set later by the caller of this function or
1413 * the AllocStorage method:
1414 */
1415 rb->AllocStorage = NULL;
1416
1417 rb->Width = 0;
1418 rb->Height = 0;
1419 rb->InternalFormat = GL_NONE;
1420 rb->Format = MESA_FORMAT_NONE;
1421
1422 rb->DataType = GL_NONE;
1423 rb->Data = NULL;
1424
1425 /* Point back to ourself so that we don't have to check for Wrapped==NULL
1426 * all over the drivers.
1427 */
1428 rb->Wrapped = rb;
1429
1430 rb->GetPointer = nop_get_pointer;
1431 rb->GetRow = NULL;
1432 rb->GetValues = NULL;
1433 rb->PutRow = NULL;
1434 rb->PutRowRGB = NULL;
1435 rb->PutMonoRow = NULL;
1436 rb->PutValues = NULL;
1437 rb->PutMonoValues = NULL;
1438 }
1439
1440
1441 /**
1442 * Allocate a new gl_renderbuffer object. This can be used for user-created
1443 * renderbuffers or window-system renderbuffers.
1444 */
1445 struct gl_renderbuffer *
1446 _mesa_new_renderbuffer(struct gl_context *ctx, GLuint name)
1447 {
1448 struct gl_renderbuffer *rb = CALLOC_STRUCT(gl_renderbuffer);
1449 if (rb) {
1450 _mesa_init_renderbuffer(rb, name);
1451 }
1452 return rb;
1453 }
1454
1455
1456 /**
1457 * Delete a gl_framebuffer.
1458 * This is the default function for renderbuffer->Delete().
1459 */
1460 void
1461 _mesa_delete_renderbuffer(struct gl_renderbuffer *rb)
1462 {
1463 if (rb->Data) {
1464 free(rb->Data);
1465 }
1466 free(rb);
1467 }
1468
1469
1470 /**
1471 * Allocate a software-based renderbuffer. This is called via the
1472 * ctx->Driver.NewRenderbuffer() function when the user creates a new
1473 * renderbuffer.
1474 * This would not be used for hardware-based renderbuffers.
1475 */
1476 struct gl_renderbuffer *
1477 _mesa_new_soft_renderbuffer(struct gl_context *ctx, GLuint name)
1478 {
1479 struct gl_renderbuffer *rb = _mesa_new_renderbuffer(ctx, name);
1480 if (rb) {
1481 rb->AllocStorage = _mesa_soft_renderbuffer_storage;
1482 /* Normally, one would setup the PutRow, GetRow, etc functions here.
1483 * But we're doing that in the _mesa_soft_renderbuffer_storage() function
1484 * instead.
1485 */
1486 }
1487 return rb;
1488 }
1489
1490
1491 /**
1492 * Add software-based color renderbuffers to the given framebuffer.
1493 * This is a helper routine for device drivers when creating a
1494 * window system framebuffer (not a user-created render/framebuffer).
1495 * Once this function is called, you can basically forget about this
1496 * renderbuffer; core Mesa will handle all the buffer management and
1497 * rendering!
1498 */
1499 GLboolean
1500 _mesa_add_color_renderbuffers(struct gl_context *ctx, struct gl_framebuffer *fb,
1501 GLuint rgbBits, GLuint alphaBits,
1502 GLboolean frontLeft, GLboolean backLeft,
1503 GLboolean frontRight, GLboolean backRight)
1504 {
1505 gl_buffer_index b;
1506
1507 if (rgbBits > 16 || alphaBits > 16) {
1508 _mesa_problem(ctx,
1509 "Unsupported bit depth in _mesa_add_color_renderbuffers");
1510 return GL_FALSE;
1511 }
1512
1513 assert(MAX_COLOR_ATTACHMENTS >= 4);
1514
1515 for (b = BUFFER_FRONT_LEFT; b <= BUFFER_BACK_RIGHT; b++) {
1516 struct gl_renderbuffer *rb;
1517
1518 if (b == BUFFER_FRONT_LEFT && !frontLeft)
1519 continue;
1520 else if (b == BUFFER_BACK_LEFT && !backLeft)
1521 continue;
1522 else if (b == BUFFER_FRONT_RIGHT && !frontRight)
1523 continue;
1524 else if (b == BUFFER_BACK_RIGHT && !backRight)
1525 continue;
1526
1527 assert(fb->Attachment[b].Renderbuffer == NULL);
1528
1529 rb = _mesa_new_renderbuffer(ctx, 0);
1530 if (!rb) {
1531 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating color buffer");
1532 return GL_FALSE;
1533 }
1534
1535 if (rgbBits <= 8) {
1536 if (alphaBits)
1537 rb->Format = MESA_FORMAT_RGBA8888;
1538 else
1539 rb->Format = MESA_FORMAT_RGB888;
1540 }
1541 else {
1542 assert(rgbBits <= 16);
1543 rb->Format = MESA_FORMAT_NONE; /*XXX RGBA16;*/
1544 }
1545 rb->InternalFormat = GL_RGBA;
1546
1547 rb->AllocStorage = _mesa_soft_renderbuffer_storage;
1548 _mesa_add_renderbuffer(fb, b, rb);
1549 }
1550
1551 return GL_TRUE;
1552 }
1553
1554
1555 /**
1556 * Add software-based alpha renderbuffers to the given framebuffer.
1557 * This is a helper routine for device drivers when creating a
1558 * window system framebuffer (not a user-created render/framebuffer).
1559 * Once this function is called, you can basically forget about this
1560 * renderbuffer; core Mesa will handle all the buffer management and
1561 * rendering!
1562 */
1563 GLboolean
1564 _mesa_add_alpha_renderbuffers(struct gl_context *ctx, struct gl_framebuffer *fb,
1565 GLuint alphaBits,
1566 GLboolean frontLeft, GLboolean backLeft,
1567 GLboolean frontRight, GLboolean backRight)
1568 {
1569 gl_buffer_index b;
1570
1571 /* for window system framebuffers only! */
1572 assert(fb->Name == 0);
1573
1574 if (alphaBits > 8) {
1575 _mesa_problem(ctx,
1576 "Unsupported bit depth in _mesa_add_alpha_renderbuffers");
1577 return GL_FALSE;
1578 }
1579
1580 assert(MAX_COLOR_ATTACHMENTS >= 4);
1581
1582 /* Wrap each of the RGB color buffers with an alpha renderbuffer.
1583 */
1584 for (b = BUFFER_FRONT_LEFT; b <= BUFFER_BACK_RIGHT; b++) {
1585 struct gl_renderbuffer *arb;
1586
1587 if (b == BUFFER_FRONT_LEFT && !frontLeft)
1588 continue;
1589 else if (b == BUFFER_BACK_LEFT && !backLeft)
1590 continue;
1591 else if (b == BUFFER_FRONT_RIGHT && !frontRight)
1592 continue;
1593 else if (b == BUFFER_BACK_RIGHT && !backRight)
1594 continue;
1595
1596 /* the RGB buffer to wrap must already exist!! */
1597 assert(fb->Attachment[b].Renderbuffer);
1598
1599 /* only GLubyte supported for now */
1600 assert(fb->Attachment[b].Renderbuffer->DataType == GL_UNSIGNED_BYTE);
1601
1602 /* allocate alpha renderbuffer */
1603 arb = _mesa_new_renderbuffer(ctx, 0);
1604 if (!arb) {
1605 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating alpha buffer");
1606 return GL_FALSE;
1607 }
1608
1609 /* wrap the alpha renderbuffer around the RGB renderbuffer */
1610 arb->Wrapped = fb->Attachment[b].Renderbuffer;
1611
1612 /* Set up my alphabuffer fields and plug in my functions.
1613 * The functions will put/get the alpha values from/to RGBA arrays
1614 * and then call the wrapped buffer's functions to handle the RGB
1615 * values.
1616 */
1617 arb->InternalFormat = arb->Wrapped->InternalFormat;
1618 arb->Format = MESA_FORMAT_A8;
1619 arb->DataType = arb->Wrapped->DataType;
1620 arb->AllocStorage = alloc_storage_alpha8;
1621 arb->Delete = delete_renderbuffer_alpha8;
1622 arb->GetPointer = get_pointer_alpha8;
1623 arb->GetRow = get_row_alpha8;
1624 arb->GetValues = get_values_alpha8;
1625 arb->PutRow = put_row_alpha8;
1626 arb->PutRowRGB = put_row_rgb_alpha8;
1627 arb->PutMonoRow = put_mono_row_alpha8;
1628 arb->PutValues = put_values_alpha8;
1629 arb->PutMonoValues = put_mono_values_alpha8;
1630
1631 /* clear the pointer to avoid assertion/sanity check failure later */
1632 fb->Attachment[b].Renderbuffer = NULL;
1633
1634 /* plug the alpha renderbuffer into the colorbuffer attachment */
1635 _mesa_add_renderbuffer(fb, b, arb);
1636 }
1637
1638 return GL_TRUE;
1639 }
1640
1641
1642 /**
1643 * For framebuffers that use a software alpha channel wrapper
1644 * created by _mesa_add_alpha_renderbuffer or _mesa_add_soft_renderbuffers,
1645 * copy the back buffer alpha channel into the front buffer alpha channel.
1646 */
1647 void
1648 _mesa_copy_soft_alpha_renderbuffers(struct gl_context *ctx, struct gl_framebuffer *fb)
1649 {
1650 if (fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer &&
1651 fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer)
1652 copy_buffer_alpha8(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer,
1653 fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
1654
1655
1656 if (fb->Attachment[BUFFER_FRONT_RIGHT].Renderbuffer &&
1657 fb->Attachment[BUFFER_BACK_RIGHT].Renderbuffer)
1658 copy_buffer_alpha8(fb->Attachment[BUFFER_FRONT_RIGHT].Renderbuffer,
1659 fb->Attachment[BUFFER_BACK_RIGHT].Renderbuffer);
1660 }
1661
1662
1663 /**
1664 * Add a software-based depth renderbuffer to the given framebuffer.
1665 * This is a helper routine for device drivers when creating a
1666 * window system framebuffer (not a user-created render/framebuffer).
1667 * Once this function is called, you can basically forget about this
1668 * renderbuffer; core Mesa will handle all the buffer management and
1669 * rendering!
1670 */
1671 GLboolean
1672 _mesa_add_depth_renderbuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
1673 GLuint depthBits)
1674 {
1675 struct gl_renderbuffer *rb;
1676
1677 if (depthBits > 32) {
1678 _mesa_problem(ctx,
1679 "Unsupported depthBits in _mesa_add_depth_renderbuffer");
1680 return GL_FALSE;
1681 }
1682
1683 assert(fb->Attachment[BUFFER_DEPTH].Renderbuffer == NULL);
1684
1685 rb = _mesa_new_renderbuffer(ctx, 0);
1686 if (!rb) {
1687 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating depth buffer");
1688 return GL_FALSE;
1689 }
1690
1691 if (depthBits <= 16) {
1692 rb->Format = MESA_FORMAT_Z16;
1693 rb->InternalFormat = GL_DEPTH_COMPONENT16;
1694 }
1695 else if (depthBits <= 24) {
1696 rb->Format = MESA_FORMAT_X8_Z24;
1697 rb->InternalFormat = GL_DEPTH_COMPONENT24;
1698 }
1699 else {
1700 rb->Format = MESA_FORMAT_Z32;
1701 rb->InternalFormat = GL_DEPTH_COMPONENT32;
1702 }
1703
1704 rb->AllocStorage = _mesa_soft_renderbuffer_storage;
1705 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, rb);
1706
1707 return GL_TRUE;
1708 }
1709
1710
1711 /**
1712 * Add a software-based stencil renderbuffer to the given framebuffer.
1713 * This is a helper routine for device drivers when creating a
1714 * window system framebuffer (not a user-created render/framebuffer).
1715 * Once this function is called, you can basically forget about this
1716 * renderbuffer; core Mesa will handle all the buffer management and
1717 * rendering!
1718 */
1719 GLboolean
1720 _mesa_add_stencil_renderbuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
1721 GLuint stencilBits)
1722 {
1723 struct gl_renderbuffer *rb;
1724
1725 if (stencilBits > 16) {
1726 _mesa_problem(ctx,
1727 "Unsupported stencilBits in _mesa_add_stencil_renderbuffer");
1728 return GL_FALSE;
1729 }
1730
1731 assert(fb->Attachment[BUFFER_STENCIL].Renderbuffer == NULL);
1732
1733 rb = _mesa_new_renderbuffer(ctx, 0);
1734 if (!rb) {
1735 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating stencil buffer");
1736 return GL_FALSE;
1737 }
1738
1739 assert(stencilBits <= 8);
1740 rb->Format = MESA_FORMAT_S8;
1741 rb->InternalFormat = GL_STENCIL_INDEX8;
1742
1743 rb->AllocStorage = _mesa_soft_renderbuffer_storage;
1744 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, rb);
1745
1746 return GL_TRUE;
1747 }
1748
1749
1750 /**
1751 * Add a software-based accumulation renderbuffer to the given framebuffer.
1752 * This is a helper routine for device drivers when creating a
1753 * window system framebuffer (not a user-created render/framebuffer).
1754 * Once this function is called, you can basically forget about this
1755 * renderbuffer; core Mesa will handle all the buffer management and
1756 * rendering!
1757 */
1758 GLboolean
1759 _mesa_add_accum_renderbuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
1760 GLuint redBits, GLuint greenBits,
1761 GLuint blueBits, GLuint alphaBits)
1762 {
1763 struct gl_renderbuffer *rb;
1764
1765 if (redBits > 16 || greenBits > 16 || blueBits > 16 || alphaBits > 16) {
1766 _mesa_problem(ctx,
1767 "Unsupported accumBits in _mesa_add_accum_renderbuffer");
1768 return GL_FALSE;
1769 }
1770
1771 assert(fb->Attachment[BUFFER_ACCUM].Renderbuffer == NULL);
1772
1773 rb = _mesa_new_renderbuffer(ctx, 0);
1774 if (!rb) {
1775 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating accum buffer");
1776 return GL_FALSE;
1777 }
1778
1779 rb->Format = MESA_FORMAT_SIGNED_RGBA_16;
1780 rb->InternalFormat = GL_RGBA16_SNORM;
1781 rb->AllocStorage = _mesa_soft_renderbuffer_storage;
1782 _mesa_add_renderbuffer(fb, BUFFER_ACCUM, rb);
1783
1784 return GL_TRUE;
1785 }
1786
1787
1788
1789 /**
1790 * Add a software-based aux renderbuffer to the given framebuffer.
1791 * This is a helper routine for device drivers when creating a
1792 * window system framebuffer (not a user-created render/framebuffer).
1793 * Once this function is called, you can basically forget about this
1794 * renderbuffer; core Mesa will handle all the buffer management and
1795 * rendering!
1796 *
1797 * NOTE: color-index aux buffers not supported.
1798 */
1799 GLboolean
1800 _mesa_add_aux_renderbuffers(struct gl_context *ctx, struct gl_framebuffer *fb,
1801 GLuint colorBits, GLuint numBuffers)
1802 {
1803 GLuint i;
1804
1805 if (colorBits > 16) {
1806 _mesa_problem(ctx,
1807 "Unsupported accumBits in _mesa_add_aux_renderbuffers");
1808 return GL_FALSE;
1809 }
1810
1811 assert(numBuffers <= MAX_AUX_BUFFERS);
1812
1813 for (i = 0; i < numBuffers; i++) {
1814 struct gl_renderbuffer *rb = _mesa_new_renderbuffer(ctx, 0);
1815
1816 assert(fb->Attachment[BUFFER_AUX0 + i].Renderbuffer == NULL);
1817
1818 if (!rb) {
1819 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating aux buffer");
1820 return GL_FALSE;
1821 }
1822
1823 assert (colorBits <= 8);
1824 rb->Format = MESA_FORMAT_RGBA8888;
1825 rb->InternalFormat = GL_RGBA;
1826
1827 rb->AllocStorage = _mesa_soft_renderbuffer_storage;
1828 _mesa_add_renderbuffer(fb, BUFFER_AUX0 + i, rb);
1829 }
1830 return GL_TRUE;
1831 }
1832
1833
1834 /**
1835 * Create/attach software-based renderbuffers to the given framebuffer.
1836 * This is a helper routine for device drivers. Drivers can just as well
1837 * call the individual _mesa_add_*_renderbuffer() routines directly.
1838 */
1839 void
1840 _mesa_add_soft_renderbuffers(struct gl_framebuffer *fb,
1841 GLboolean color,
1842 GLboolean depth,
1843 GLboolean stencil,
1844 GLboolean accum,
1845 GLboolean alpha,
1846 GLboolean aux)
1847 {
1848 GLboolean frontLeft = GL_TRUE;
1849 GLboolean backLeft = fb->Visual.doubleBufferMode;
1850 GLboolean frontRight = fb->Visual.stereoMode;
1851 GLboolean backRight = fb->Visual.stereoMode && fb->Visual.doubleBufferMode;
1852
1853 if (color) {
1854 assert(fb->Visual.redBits == fb->Visual.greenBits);
1855 assert(fb->Visual.redBits == fb->Visual.blueBits);
1856 _mesa_add_color_renderbuffers(NULL, fb,
1857 fb->Visual.redBits,
1858 fb->Visual.alphaBits,
1859 frontLeft, backLeft,
1860 frontRight, backRight);
1861 }
1862
1863 if (depth) {
1864 assert(fb->Visual.depthBits > 0);
1865 _mesa_add_depth_renderbuffer(NULL, fb, fb->Visual.depthBits);
1866 }
1867
1868 if (stencil) {
1869 assert(fb->Visual.stencilBits > 0);
1870 _mesa_add_stencil_renderbuffer(NULL, fb, fb->Visual.stencilBits);
1871 }
1872
1873 if (accum) {
1874 assert(fb->Visual.accumRedBits > 0);
1875 assert(fb->Visual.accumGreenBits > 0);
1876 assert(fb->Visual.accumBlueBits > 0);
1877 _mesa_add_accum_renderbuffer(NULL, fb,
1878 fb->Visual.accumRedBits,
1879 fb->Visual.accumGreenBits,
1880 fb->Visual.accumBlueBits,
1881 fb->Visual.accumAlphaBits);
1882 }
1883
1884 if (aux) {
1885 assert(fb->Visual.numAuxBuffers > 0);
1886 _mesa_add_aux_renderbuffers(NULL, fb, fb->Visual.redBits,
1887 fb->Visual.numAuxBuffers);
1888 }
1889
1890 if (alpha) {
1891 assert(fb->Visual.alphaBits > 0);
1892 _mesa_add_alpha_renderbuffers(NULL, fb, fb->Visual.alphaBits,
1893 frontLeft, backLeft,
1894 frontRight, backRight);
1895 }
1896
1897 #if 0
1898 if (multisample) {
1899 /* maybe someday */
1900 }
1901 #endif
1902 }
1903
1904
1905 /**
1906 * Attach a renderbuffer to a framebuffer.
1907 * \param bufferName one of the BUFFER_x tokens
1908 */
1909 void
1910 _mesa_add_renderbuffer(struct gl_framebuffer *fb,
1911 gl_buffer_index bufferName, struct gl_renderbuffer *rb)
1912 {
1913 assert(fb);
1914 assert(rb);
1915 assert(bufferName < BUFFER_COUNT);
1916
1917 /* There should be no previous renderbuffer on this attachment point,
1918 * with the exception of depth/stencil since the same renderbuffer may
1919 * be used for both.
1920 */
1921 assert(bufferName == BUFFER_DEPTH ||
1922 bufferName == BUFFER_STENCIL ||
1923 fb->Attachment[bufferName].Renderbuffer == NULL);
1924
1925 /* winsys vs. user-created buffer cross check */
1926 if (fb->Name) {
1927 assert(rb->Name);
1928 }
1929 else {
1930 assert(!rb->Name);
1931 }
1932
1933 fb->Attachment[bufferName].Type = GL_RENDERBUFFER_EXT;
1934 fb->Attachment[bufferName].Complete = GL_TRUE;
1935 _mesa_reference_renderbuffer(&fb->Attachment[bufferName].Renderbuffer, rb);
1936 }
1937
1938
1939 /**
1940 * Remove the named renderbuffer from the given framebuffer.
1941 * \param bufferName one of the BUFFER_x tokens
1942 */
1943 void
1944 _mesa_remove_renderbuffer(struct gl_framebuffer *fb,
1945 gl_buffer_index bufferName)
1946 {
1947 struct gl_renderbuffer *rb;
1948
1949 assert(bufferName < BUFFER_COUNT);
1950
1951 rb = fb->Attachment[bufferName].Renderbuffer;
1952 if (!rb)
1953 return;
1954
1955 _mesa_reference_renderbuffer(&rb, NULL);
1956
1957 fb->Attachment[bufferName].Renderbuffer = NULL;
1958 }
1959
1960
1961 /**
1962 * Set *ptr to point to rb. If *ptr points to another renderbuffer,
1963 * dereference that buffer first. The new renderbuffer's refcount will
1964 * be incremented. The old renderbuffer's refcount will be decremented.
1965 */
1966 void
1967 _mesa_reference_renderbuffer(struct gl_renderbuffer **ptr,
1968 struct gl_renderbuffer *rb)
1969 {
1970 assert(ptr);
1971 if (*ptr == rb) {
1972 /* no change */
1973 return;
1974 }
1975
1976 if (*ptr) {
1977 /* Unreference the old renderbuffer */
1978 GLboolean deleteFlag = GL_FALSE;
1979 struct gl_renderbuffer *oldRb = *ptr;
1980
1981 _glthread_LOCK_MUTEX(oldRb->Mutex);
1982 ASSERT(oldRb->RefCount > 0);
1983 oldRb->RefCount--;
1984 /*printf("RB DECR %p (%d) to %d\n", (void*) oldRb, oldRb->Name, oldRb->RefCount);*/
1985 deleteFlag = (oldRb->RefCount == 0);
1986 _glthread_UNLOCK_MUTEX(oldRb->Mutex);
1987
1988 if (deleteFlag) {
1989 oldRb->Delete(oldRb);
1990 }
1991
1992 *ptr = NULL;
1993 }
1994 assert(!*ptr);
1995
1996 if (rb) {
1997 /* reference new renderbuffer */
1998 _glthread_LOCK_MUTEX(rb->Mutex);
1999 rb->RefCount++;
2000 /*printf("RB INCR %p (%d) to %d\n", (void*) rb, rb->Name, rb->RefCount);*/
2001 _glthread_UNLOCK_MUTEX(rb->Mutex);
2002 *ptr = rb;
2003 }
2004 }
2005
2006
2007 /**
2008 * Create a new combined depth/stencil renderbuffer for implementing
2009 * the GL_EXT_packed_depth_stencil extension.
2010 * \return new depth/stencil renderbuffer
2011 */
2012 struct gl_renderbuffer *
2013 _mesa_new_depthstencil_renderbuffer(struct gl_context *ctx, GLuint name)
2014 {
2015 struct gl_renderbuffer *dsrb;
2016
2017 dsrb = _mesa_new_renderbuffer(ctx, name);
2018 if (!dsrb)
2019 return NULL;
2020
2021 /* init fields not covered by _mesa_new_renderbuffer() */
2022 dsrb->InternalFormat = GL_DEPTH24_STENCIL8_EXT;
2023 dsrb->Format = MESA_FORMAT_Z24_S8;
2024 dsrb->AllocStorage = _mesa_soft_renderbuffer_storage;
2025
2026 return dsrb;
2027 }