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