mesa: rename gl_renderbuffer::Data to Buffer
[mesa.git] / src / mesa / swrast / s_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 software-based renderbuffers.
28 * Also, routines for reading/writing software-based renderbuffer data as
29 * ubytes, ushorts, uints, etc.
30 */
31
32
33 #include "main/glheader.h"
34 #include "main/imports.h"
35 #include "main/context.h"
36 #include "main/fbobject.h"
37 #include "main/formats.h"
38 #include "main/mtypes.h"
39 #include "main/renderbuffer.h"
40 #include "swrast/s_renderbuffer.h"
41
42
43 /**
44 * This is a software fallback for the gl_renderbuffer->AllocStorage
45 * function.
46 * Device drivers will typically override this function for the buffers
47 * which it manages (typically color buffers, Z and stencil).
48 * Other buffers (like software accumulation and aux buffers) which the driver
49 * doesn't manage can be handled with this function.
50 *
51 * This one multi-purpose function can allocate stencil, depth, accum, color
52 * or color-index buffers!
53 */
54 static GLboolean
55 soft_renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
56 GLenum internalFormat,
57 GLuint width, GLuint height)
58 {
59 switch (internalFormat) {
60 case GL_RGB:
61 case GL_R3_G3_B2:
62 case GL_RGB4:
63 case GL_RGB5:
64 case GL_RGB8:
65 case GL_RGB10:
66 case GL_RGB12:
67 case GL_RGB16:
68 rb->Format = MESA_FORMAT_RGB888;
69 break;
70 case GL_RGBA:
71 case GL_RGBA2:
72 case GL_RGBA4:
73 case GL_RGB5_A1:
74 case GL_RGBA8:
75 #if 1
76 case GL_RGB10_A2:
77 case GL_RGBA12:
78 #endif
79 if (_mesa_little_endian())
80 rb->Format = MESA_FORMAT_RGBA8888_REV;
81 else
82 rb->Format = MESA_FORMAT_RGBA8888;
83 break;
84 case GL_RGBA16:
85 case GL_RGBA16_SNORM:
86 /* for accum buffer */
87 rb->Format = MESA_FORMAT_SIGNED_RGBA_16;
88 break;
89 case GL_STENCIL_INDEX:
90 case GL_STENCIL_INDEX1_EXT:
91 case GL_STENCIL_INDEX4_EXT:
92 case GL_STENCIL_INDEX8_EXT:
93 case GL_STENCIL_INDEX16_EXT:
94 rb->Format = MESA_FORMAT_S8;
95 break;
96 case GL_DEPTH_COMPONENT:
97 case GL_DEPTH_COMPONENT16:
98 rb->Format = MESA_FORMAT_Z16;
99 break;
100 case GL_DEPTH_COMPONENT24:
101 rb->Format = MESA_FORMAT_X8_Z24;
102 break;
103 case GL_DEPTH_COMPONENT32:
104 rb->Format = MESA_FORMAT_Z32;
105 break;
106 case GL_DEPTH_STENCIL_EXT:
107 case GL_DEPTH24_STENCIL8_EXT:
108 rb->Format = MESA_FORMAT_Z24_S8;
109 break;
110 default:
111 /* unsupported format */
112 return GL_FALSE;
113 }
114
115 /* free old buffer storage */
116 if (rb->Buffer) {
117 free(rb->Buffer);
118 rb->Buffer = NULL;
119 }
120
121 rb->RowStrideBytes = width * _mesa_get_format_bytes(rb->Format);
122
123 if (width > 0 && height > 0) {
124 /* allocate new buffer storage */
125 rb->Buffer = malloc(width * height * _mesa_get_format_bytes(rb->Format));
126
127 if (rb->Buffer == NULL) {
128 rb->Width = 0;
129 rb->Height = 0;
130 _mesa_error(ctx, GL_OUT_OF_MEMORY,
131 "software renderbuffer allocation (%d x %d x %d)",
132 width, height, _mesa_get_format_bytes(rb->Format));
133 return GL_FALSE;
134 }
135 }
136
137 rb->Width = width;
138 rb->Height = height;
139 rb->_BaseFormat = _mesa_base_fbo_format(ctx, internalFormat);
140
141 if (rb->Name == 0 &&
142 internalFormat == GL_RGBA16_SNORM &&
143 rb->_BaseFormat == 0) {
144 /* NOTE: This is a special case just for accumulation buffers.
145 * This is a very limited use case- there's no snorm texturing or
146 * rendering going on.
147 */
148 rb->_BaseFormat = GL_RGBA;
149 }
150 else {
151 /* the internalFormat should have been error checked long ago */
152 ASSERT(rb->_BaseFormat);
153 }
154
155 return GL_TRUE;
156 }
157
158
159 /**
160 * Called via gl_renderbuffer::Delete()
161 */
162 static void
163 soft_renderbuffer_delete(struct gl_renderbuffer *rb)
164 {
165 if (rb->Buffer) {
166 free(rb->Buffer);
167 rb->Buffer = NULL;
168 }
169 free(rb);
170 }
171
172
173 void
174 _swrast_map_soft_renderbuffer(struct gl_context *ctx,
175 struct gl_renderbuffer *rb,
176 GLuint x, GLuint y, GLuint w, GLuint h,
177 GLbitfield mode,
178 GLubyte **out_map,
179 GLint *out_stride)
180 {
181 GLubyte *map = rb->Buffer;
182 int cpp = _mesa_get_format_bytes(rb->Format);
183 int stride = rb->Width * cpp;
184
185 if (!map) {
186 *out_map = NULL;
187 *out_stride = 0;
188 }
189
190 map += y * stride;
191 map += x * cpp;
192
193 *out_map = map;
194 *out_stride = stride;
195 }
196
197
198 void
199 _swrast_unmap_soft_renderbuffer(struct gl_context *ctx,
200 struct gl_renderbuffer *rb)
201 {
202 }
203
204
205
206 /**
207 * Allocate a software-based renderbuffer. This is called via the
208 * ctx->Driver.NewRenderbuffer() function when the user creates a new
209 * renderbuffer.
210 * This would not be used for hardware-based renderbuffers.
211 */
212 struct gl_renderbuffer *
213 _swrast_new_soft_renderbuffer(struct gl_context *ctx, GLuint name)
214 {
215 struct gl_renderbuffer *rb = _mesa_new_renderbuffer(ctx, name);
216 if (rb) {
217 rb->AllocStorage = soft_renderbuffer_storage;
218 rb->Delete = soft_renderbuffer_delete;
219 }
220 return rb;
221 }
222
223
224 /**
225 * Add software-based color renderbuffers to the given framebuffer.
226 * This is a helper routine for device drivers when creating a
227 * window system framebuffer (not a user-created render/framebuffer).
228 * Once this function is called, you can basically forget about this
229 * renderbuffer; core Mesa will handle all the buffer management and
230 * rendering!
231 */
232 static GLboolean
233 add_color_renderbuffers(struct gl_context *ctx, struct gl_framebuffer *fb,
234 GLuint rgbBits, GLuint alphaBits,
235 GLboolean frontLeft, GLboolean backLeft,
236 GLboolean frontRight, GLboolean backRight)
237 {
238 gl_buffer_index b;
239
240 if (rgbBits > 16 || alphaBits > 16) {
241 _mesa_problem(ctx,
242 "Unsupported bit depth in add_color_renderbuffers");
243 return GL_FALSE;
244 }
245
246 assert(MAX_COLOR_ATTACHMENTS >= 4);
247
248 for (b = BUFFER_FRONT_LEFT; b <= BUFFER_BACK_RIGHT; b++) {
249 struct gl_renderbuffer *rb;
250
251 if (b == BUFFER_FRONT_LEFT && !frontLeft)
252 continue;
253 else if (b == BUFFER_BACK_LEFT && !backLeft)
254 continue;
255 else if (b == BUFFER_FRONT_RIGHT && !frontRight)
256 continue;
257 else if (b == BUFFER_BACK_RIGHT && !backRight)
258 continue;
259
260 assert(fb->Attachment[b].Renderbuffer == NULL);
261
262 rb = _mesa_new_renderbuffer(ctx, 0);
263 if (!rb) {
264 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating color buffer");
265 return GL_FALSE;
266 }
267
268 rb->InternalFormat = GL_RGBA;
269
270 rb->AllocStorage = soft_renderbuffer_storage;
271 _mesa_add_renderbuffer(fb, b, rb);
272 }
273
274 return GL_TRUE;
275 }
276
277
278 /**
279 * Add a software-based depth renderbuffer to the given framebuffer.
280 * This is a helper routine for device drivers when creating a
281 * window system framebuffer (not a user-created render/framebuffer).
282 * Once this function is called, you can basically forget about this
283 * renderbuffer; core Mesa will handle all the buffer management and
284 * rendering!
285 */
286 static GLboolean
287 add_depth_renderbuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
288 GLuint depthBits)
289 {
290 struct gl_renderbuffer *rb;
291
292 if (depthBits > 32) {
293 _mesa_problem(ctx,
294 "Unsupported depthBits in add_depth_renderbuffer");
295 return GL_FALSE;
296 }
297
298 assert(fb->Attachment[BUFFER_DEPTH].Renderbuffer == NULL);
299
300 rb = _mesa_new_renderbuffer(ctx, 0);
301 if (!rb) {
302 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating depth buffer");
303 return GL_FALSE;
304 }
305
306 if (depthBits <= 16) {
307 rb->InternalFormat = GL_DEPTH_COMPONENT16;
308 }
309 else if (depthBits <= 24) {
310 rb->InternalFormat = GL_DEPTH_COMPONENT24;
311 }
312 else {
313 rb->InternalFormat = GL_DEPTH_COMPONENT32;
314 }
315
316 rb->AllocStorage = soft_renderbuffer_storage;
317 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, rb);
318
319 return GL_TRUE;
320 }
321
322
323 /**
324 * Add a software-based stencil renderbuffer to the given framebuffer.
325 * This is a helper routine for device drivers when creating a
326 * window system framebuffer (not a user-created render/framebuffer).
327 * Once this function is called, you can basically forget about this
328 * renderbuffer; core Mesa will handle all the buffer management and
329 * rendering!
330 */
331 static GLboolean
332 add_stencil_renderbuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
333 GLuint stencilBits)
334 {
335 struct gl_renderbuffer *rb;
336
337 if (stencilBits > 16) {
338 _mesa_problem(ctx,
339 "Unsupported stencilBits in add_stencil_renderbuffer");
340 return GL_FALSE;
341 }
342
343 assert(fb->Attachment[BUFFER_STENCIL].Renderbuffer == NULL);
344
345 rb = _mesa_new_renderbuffer(ctx, 0);
346 if (!rb) {
347 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating stencil buffer");
348 return GL_FALSE;
349 }
350
351 assert(stencilBits <= 8);
352 rb->InternalFormat = GL_STENCIL_INDEX8;
353
354 rb->AllocStorage = soft_renderbuffer_storage;
355 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, rb);
356
357 return GL_TRUE;
358 }
359
360
361 static GLboolean
362 add_depth_stencil_renderbuffer(struct gl_context *ctx,
363 struct gl_framebuffer *fb)
364 {
365 struct gl_renderbuffer *rb;
366
367 assert(fb->Attachment[BUFFER_DEPTH].Renderbuffer == NULL);
368 assert(fb->Attachment[BUFFER_STENCIL].Renderbuffer == NULL);
369
370 rb = _mesa_new_renderbuffer(ctx, 0);
371 if (!rb) {
372 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating depth+stencil buffer");
373 return GL_FALSE;
374 }
375
376 rb->InternalFormat = GL_DEPTH_STENCIL;
377
378 rb->AllocStorage = soft_renderbuffer_storage;
379 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, rb);
380 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, rb);
381
382 return GL_TRUE;
383 }
384
385
386 /**
387 * Add a software-based accumulation renderbuffer to the given framebuffer.
388 * This is a helper routine for device drivers when creating a
389 * window system framebuffer (not a user-created render/framebuffer).
390 * Once this function is called, you can basically forget about this
391 * renderbuffer; core Mesa will handle all the buffer management and
392 * rendering!
393 */
394 static GLboolean
395 add_accum_renderbuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
396 GLuint redBits, GLuint greenBits,
397 GLuint blueBits, GLuint alphaBits)
398 {
399 struct gl_renderbuffer *rb;
400
401 if (redBits > 16 || greenBits > 16 || blueBits > 16 || alphaBits > 16) {
402 _mesa_problem(ctx,
403 "Unsupported accumBits in add_accum_renderbuffer");
404 return GL_FALSE;
405 }
406
407 assert(fb->Attachment[BUFFER_ACCUM].Renderbuffer == NULL);
408
409 rb = _mesa_new_renderbuffer(ctx, 0);
410 if (!rb) {
411 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating accum buffer");
412 return GL_FALSE;
413 }
414
415 rb->InternalFormat = GL_RGBA16_SNORM;
416 rb->AllocStorage = soft_renderbuffer_storage;
417 _mesa_add_renderbuffer(fb, BUFFER_ACCUM, rb);
418
419 return GL_TRUE;
420 }
421
422
423
424 /**
425 * Add a software-based aux renderbuffer to the given framebuffer.
426 * This is a helper routine for device drivers when creating a
427 * window system framebuffer (not a user-created render/framebuffer).
428 * Once this function is called, you can basically forget about this
429 * renderbuffer; core Mesa will handle all the buffer management and
430 * rendering!
431 *
432 * NOTE: color-index aux buffers not supported.
433 */
434 static GLboolean
435 add_aux_renderbuffers(struct gl_context *ctx, struct gl_framebuffer *fb,
436 GLuint colorBits, GLuint numBuffers)
437 {
438 GLuint i;
439
440 if (colorBits > 16) {
441 _mesa_problem(ctx,
442 "Unsupported colorBits in add_aux_renderbuffers");
443 return GL_FALSE;
444 }
445
446 assert(numBuffers <= MAX_AUX_BUFFERS);
447
448 for (i = 0; i < numBuffers; i++) {
449 struct gl_renderbuffer *rb = _mesa_new_renderbuffer(ctx, 0);
450
451 assert(fb->Attachment[BUFFER_AUX0 + i].Renderbuffer == NULL);
452
453 if (!rb) {
454 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating aux buffer");
455 return GL_FALSE;
456 }
457
458 assert (colorBits <= 8);
459 rb->InternalFormat = GL_RGBA;
460
461 rb->AllocStorage = soft_renderbuffer_storage;
462 _mesa_add_renderbuffer(fb, BUFFER_AUX0 + i, rb);
463 }
464 return GL_TRUE;
465 }
466
467
468 /**
469 * Create/attach software-based renderbuffers to the given framebuffer.
470 * This is a helper routine for device drivers. Drivers can just as well
471 * call the individual _mesa_add_*_renderbuffer() routines directly.
472 */
473 void
474 _swrast_add_soft_renderbuffers(struct gl_framebuffer *fb,
475 GLboolean color,
476 GLboolean depth,
477 GLboolean stencil,
478 GLboolean accum,
479 GLboolean alpha,
480 GLboolean aux)
481 {
482 GLboolean frontLeft = GL_TRUE;
483 GLboolean backLeft = fb->Visual.doubleBufferMode;
484 GLboolean frontRight = fb->Visual.stereoMode;
485 GLboolean backRight = fb->Visual.stereoMode && fb->Visual.doubleBufferMode;
486
487 if (color) {
488 assert(fb->Visual.redBits == fb->Visual.greenBits);
489 assert(fb->Visual.redBits == fb->Visual.blueBits);
490 add_color_renderbuffers(NULL, fb,
491 fb->Visual.redBits,
492 fb->Visual.alphaBits,
493 frontLeft, backLeft,
494 frontRight, backRight);
495 }
496
497 #if 0
498 /* This is pretty much for debugging purposes only since there's a perf
499 * hit for using combined depth/stencil in swrast.
500 */
501 if (depth && fb->Visual.depthBits == 24 &&
502 stencil && fb->Visual.stencilBits == 8) {
503 /* use combined depth/stencil buffer */
504 add_depth_stencil_renderbuffer(NULL, fb);
505 }
506 else
507 #else
508 (void) add_depth_stencil_renderbuffer;
509 #endif
510 {
511 if (depth) {
512 assert(fb->Visual.depthBits > 0);
513 add_depth_renderbuffer(NULL, fb, fb->Visual.depthBits);
514 }
515
516 if (stencil) {
517 assert(fb->Visual.stencilBits > 0);
518 add_stencil_renderbuffer(NULL, fb, fb->Visual.stencilBits);
519 }
520 }
521
522 if (accum) {
523 assert(fb->Visual.accumRedBits > 0);
524 assert(fb->Visual.accumGreenBits > 0);
525 assert(fb->Visual.accumBlueBits > 0);
526 add_accum_renderbuffer(NULL, fb,
527 fb->Visual.accumRedBits,
528 fb->Visual.accumGreenBits,
529 fb->Visual.accumBlueBits,
530 fb->Visual.accumAlphaBits);
531 }
532
533 if (aux) {
534 assert(fb->Visual.numAuxBuffers > 0);
535 add_aux_renderbuffers(NULL, fb, fb->Visual.redBits,
536 fb->Visual.numAuxBuffers);
537 }
538
539 #if 0
540 if (multisample) {
541 /* maybe someday */
542 }
543 #endif
544 }