Replace _mesa_malloc, _mesa_calloc and _mesa_free with plain libc versions
[mesa.git] / src / mesa / state_tracker / st_cb_fbo.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 /**
30 * Framebuffer/renderbuffer functions.
31 *
32 * \author Brian Paul
33 */
34
35
36 #include "main/imports.h"
37 #include "main/context.h"
38 #include "main/fbobject.h"
39 #include "main/framebuffer.h"
40 #include "main/macros.h"
41 #include "main/renderbuffer.h"
42
43 #include "pipe/p_context.h"
44 #include "pipe/p_defines.h"
45 #include "pipe/p_screen.h"
46 #include "st_context.h"
47 #include "st_cb_fbo.h"
48 #include "st_format.h"
49 #include "st_public.h"
50 #include "st_texture.h"
51
52 #include "util/u_format.h"
53 #include "util/u_rect.h"
54 #include "util/u_inlines.h"
55
56
57 /**
58 * Compute the renderbuffer's Red/Green/EtcBit fields from the pipe format.
59 */
60 static int
61 init_renderbuffer_bits(struct st_renderbuffer *strb,
62 enum pipe_format pipeFormat)
63 {
64 struct pipe_format_info info;
65
66 if (!st_get_format_info( pipeFormat, &info )) {
67 assert( 0 );
68 }
69
70 strb->Base.Format = info.mesa_format;
71 strb->Base.DataType = st_format_datatype(pipeFormat);
72
73 return info.size;
74 }
75
76 /**
77 * gl_renderbuffer::AllocStorage()
78 * This is called to allocate the original drawing surface, and
79 * during window resize.
80 */
81 static GLboolean
82 st_renderbuffer_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
83 GLenum internalFormat,
84 GLuint width, GLuint height)
85 {
86 struct pipe_context *pipe = ctx->st->pipe;
87 struct st_renderbuffer *strb = st_renderbuffer(rb);
88 enum pipe_format format;
89
90 if (strb->format != PIPE_FORMAT_NONE)
91 format = strb->format;
92 else
93 format = st_choose_renderbuffer_format(pipe->screen, internalFormat);
94
95 /* init renderbuffer fields */
96 strb->Base.Width = width;
97 strb->Base.Height = height;
98 init_renderbuffer_bits(strb, format);
99
100 strb->defined = GL_FALSE; /* undefined contents now */
101
102 if(strb->software) {
103 size_t size;
104
105 free(strb->data);
106
107 assert(strb->format != PIPE_FORMAT_NONE);
108
109 strb->stride = util_format_get_stride(strb->format, width);
110 size = util_format_get_2d_size(strb->format, strb->stride, height);
111
112 strb->data = malloc(size);
113
114 return strb->data != NULL;
115 }
116 else {
117 struct pipe_texture template;
118 unsigned surface_usage;
119
120 /* Free the old surface and texture
121 */
122 pipe_surface_reference( &strb->surface, NULL );
123 pipe_texture_reference( &strb->texture, NULL );
124
125 /* Setup new texture template.
126 */
127 memset(&template, 0, sizeof(template));
128 template.target = PIPE_TEXTURE_2D;
129 template.format = format;
130 template.width0 = width;
131 template.height0 = height;
132 template.depth0 = 1;
133 template.last_level = 0;
134 template.nr_samples = rb->NumSamples;
135 if (util_format_is_depth_or_stencil(format)) {
136 template.tex_usage = PIPE_TEXTURE_USAGE_DEPTH_STENCIL;
137 }
138 else {
139 template.tex_usage = (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
140 PIPE_TEXTURE_USAGE_RENDER_TARGET);
141 }
142
143 /* Probably need dedicated flags for surface usage too:
144 */
145 surface_usage = (PIPE_BUFFER_USAGE_GPU_READ |
146 PIPE_BUFFER_USAGE_GPU_WRITE);
147 #if 0
148 PIPE_BUFFER_USAGE_CPU_READ |
149 PIPE_BUFFER_USAGE_CPU_WRITE);
150 #endif
151
152 strb->texture = pipe->screen->texture_create( pipe->screen,
153 &template );
154
155 if (!strb->texture)
156 return FALSE;
157
158 strb->surface = pipe->screen->get_tex_surface( pipe->screen,
159 strb->texture,
160 0, 0, 0,
161 surface_usage );
162 if (strb->surface) {
163 assert(strb->surface->texture);
164 assert(strb->surface->format);
165 assert(strb->surface->width == width);
166 assert(strb->surface->height == height);
167 }
168
169 return strb->surface != NULL;
170 }
171 }
172
173
174 /**
175 * gl_renderbuffer::Delete()
176 */
177 static void
178 st_renderbuffer_delete(struct gl_renderbuffer *rb)
179 {
180 struct st_renderbuffer *strb = st_renderbuffer(rb);
181 ASSERT(strb);
182 pipe_surface_reference(&strb->surface, NULL);
183 pipe_texture_reference(&strb->texture, NULL);
184 free(strb->data);
185 free(strb);
186 }
187
188
189 /**
190 * gl_renderbuffer::GetPointer()
191 */
192 static void *
193 null_get_pointer(GLcontext * ctx, struct gl_renderbuffer *rb,
194 GLint x, GLint y)
195 {
196 /* By returning NULL we force all software rendering to go through
197 * the span routines.
198 */
199 #if 0
200 assert(0); /* Should never get called with softpipe */
201 #endif
202 return NULL;
203 }
204
205
206 /**
207 * Called via ctx->Driver.NewFramebuffer()
208 */
209 static struct gl_framebuffer *
210 st_new_framebuffer(GLcontext *ctx, GLuint name)
211 {
212 /* XXX not sure we need to subclass gl_framebuffer for pipe */
213 return _mesa_new_framebuffer(ctx, name);
214 }
215
216
217 /**
218 * Called via ctx->Driver.NewRenderbuffer()
219 */
220 static struct gl_renderbuffer *
221 st_new_renderbuffer(GLcontext *ctx, GLuint name)
222 {
223 struct st_renderbuffer *strb = ST_CALLOC_STRUCT(st_renderbuffer);
224 if (strb) {
225 _mesa_init_renderbuffer(&strb->Base, name);
226 strb->Base.Delete = st_renderbuffer_delete;
227 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
228 strb->Base.GetPointer = null_get_pointer;
229 strb->format = PIPE_FORMAT_NONE;
230 return &strb->Base;
231 }
232 return NULL;
233 }
234
235
236 /**
237 * Allocate a renderbuffer for a an on-screen window (not a user-created
238 * renderbuffer). The window system code determines the format.
239 */
240 struct gl_renderbuffer *
241 st_new_renderbuffer_fb(enum pipe_format format, int samples, boolean sw)
242 {
243 struct st_renderbuffer *strb;
244
245 strb = ST_CALLOC_STRUCT(st_renderbuffer);
246 if (!strb) {
247 _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
248 return NULL;
249 }
250
251 _mesa_init_renderbuffer(&strb->Base, 0);
252 strb->Base.ClassID = 0x4242; /* just a unique value */
253 strb->Base.NumSamples = samples;
254 strb->format = format;
255 init_renderbuffer_bits(strb, format);
256 strb->software = sw;
257
258 switch (format) {
259 case PIPE_FORMAT_A8R8G8B8_UNORM:
260 case PIPE_FORMAT_B8G8R8A8_UNORM:
261 case PIPE_FORMAT_X8R8G8B8_UNORM:
262 case PIPE_FORMAT_B8G8R8X8_UNORM:
263 case PIPE_FORMAT_A1R5G5B5_UNORM:
264 case PIPE_FORMAT_A4R4G4B4_UNORM:
265 case PIPE_FORMAT_R5G6B5_UNORM:
266 strb->Base.InternalFormat = GL_RGBA;
267 break;
268 case PIPE_FORMAT_Z16_UNORM:
269 strb->Base.InternalFormat = GL_DEPTH_COMPONENT16;
270 break;
271 case PIPE_FORMAT_Z32_UNORM:
272 strb->Base.InternalFormat = GL_DEPTH_COMPONENT32;
273 break;
274 case PIPE_FORMAT_S8Z24_UNORM:
275 case PIPE_FORMAT_Z24S8_UNORM:
276 case PIPE_FORMAT_X8Z24_UNORM:
277 case PIPE_FORMAT_Z24X8_UNORM:
278 strb->Base.InternalFormat = GL_DEPTH24_STENCIL8_EXT;
279 break;
280 case PIPE_FORMAT_S8_UNORM:
281 strb->Base.InternalFormat = GL_STENCIL_INDEX8_EXT;
282 break;
283 case PIPE_FORMAT_R16G16B16A16_SNORM:
284 strb->Base.InternalFormat = GL_RGBA16;
285 break;
286 default:
287 _mesa_problem(NULL,
288 "Unexpected format in st_new_renderbuffer_fb");
289 free(strb);
290 return NULL;
291 }
292
293 /* st-specific methods */
294 strb->Base.Delete = st_renderbuffer_delete;
295 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
296 strb->Base.GetPointer = null_get_pointer;
297
298 /* surface is allocated in st_renderbuffer_alloc_storage() */
299 strb->surface = NULL;
300
301 return &strb->Base;
302 }
303
304
305
306
307 /**
308 * Called via ctx->Driver.BindFramebufferEXT().
309 */
310 static void
311 st_bind_framebuffer(GLcontext *ctx, GLenum target,
312 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
313 {
314
315 }
316
317 /**
318 * Called by ctx->Driver.FramebufferRenderbuffer
319 */
320 static void
321 st_framebuffer_renderbuffer(GLcontext *ctx,
322 struct gl_framebuffer *fb,
323 GLenum attachment,
324 struct gl_renderbuffer *rb)
325 {
326 /* XXX no need for derivation? */
327 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
328 }
329
330
331 /**
332 * Called by ctx->Driver.RenderTexture
333 */
334 static void
335 st_render_texture(GLcontext *ctx,
336 struct gl_framebuffer *fb,
337 struct gl_renderbuffer_attachment *att)
338 {
339 struct pipe_screen *screen = ctx->st->pipe->screen;
340 struct st_renderbuffer *strb;
341 struct gl_renderbuffer *rb;
342 struct pipe_texture *pt = st_get_texobj_texture(att->Texture);
343 struct st_texture_object *stObj;
344 const struct gl_texture_image *texImage;
345 GLint pt_level;
346
347 /* When would this fail? Perhaps assert? */
348 if (!pt)
349 return;
350
351 /* The first gallium texture level = Mesa BaseLevel */
352 pt_level = MAX2(0, (GLint) att->TextureLevel - att->Texture->BaseLevel);
353 texImage = att->Texture->Image[att->CubeMapFace][pt_level];
354
355 /* create new renderbuffer which wraps the texture image */
356 rb = st_new_renderbuffer(ctx, 0);
357 if (!rb) {
358 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
359 return;
360 }
361
362 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
363 assert(rb->RefCount == 1);
364 rb->AllocStorage = NULL; /* should not get called */
365 strb = st_renderbuffer(rb);
366
367 assert(strb->Base.RefCount > 0);
368
369 /* get the texture for the texture object */
370 stObj = st_texture_object(att->Texture);
371
372 /* point renderbuffer at texobject */
373 strb->rtt = stObj;
374 strb->rtt_level = pt_level;
375 strb->rtt_face = att->CubeMapFace;
376 strb->rtt_slice = att->Zoffset;
377
378 rb->Width = texImage->Width2;
379 rb->Height = texImage->Height2;
380 rb->_BaseFormat = texImage->_BaseFormat;
381 /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
382
383 /*printf("***** pipe texture %d x %d\n", pt->width0, pt->height0);*/
384
385 pipe_texture_reference( &strb->texture, pt );
386
387 pipe_surface_reference(&strb->surface, NULL);
388
389 assert(strb->rtt_level <= strb->texture->last_level);
390
391 /* new surface for rendering into the texture */
392 strb->surface = screen->get_tex_surface(screen,
393 strb->texture,
394 strb->rtt_face,
395 strb->rtt_level,
396 strb->rtt_slice,
397 PIPE_BUFFER_USAGE_GPU_READ |
398 PIPE_BUFFER_USAGE_GPU_WRITE);
399
400 init_renderbuffer_bits(strb, pt->format);
401
402 /*
403 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
404 att->Texture, pt, strb->surface, rb->Width, rb->Height);
405 */
406
407 /* Invalidate buffer state so that the pipe's framebuffer state
408 * gets updated.
409 * That's where the new renderbuffer (which we just created) gets
410 * passed to the pipe as a (color/depth) render target.
411 */
412 st_invalidate_state(ctx, _NEW_BUFFERS);
413 }
414
415
416 /**
417 * Called via ctx->Driver.FinishRenderTexture.
418 */
419 static void
420 st_finish_render_texture(GLcontext *ctx,
421 struct gl_renderbuffer_attachment *att)
422 {
423 struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer);
424
425 if (!strb)
426 return;
427
428 st_flush( ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL );
429
430 if (strb->surface)
431 pipe_surface_reference( &strb->surface, NULL );
432
433 strb->rtt = NULL;
434
435 /*
436 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
437 */
438
439 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
440
441 /* restore previous framebuffer state */
442 st_invalidate_state(ctx, _NEW_BUFFERS);
443 }
444
445
446 /**
447 * Validate a renderbuffer attachment for a particular usage.
448 */
449
450 static GLboolean
451 st_validate_attachment(struct pipe_screen *screen,
452 const struct gl_renderbuffer_attachment *att,
453 GLuint usage)
454 {
455 const struct st_texture_object *stObj =
456 st_texture_object(att->Texture);
457
458 /**
459 * Only validate texture attachments for now, since
460 * st_renderbuffer_alloc_storage makes sure that
461 * the format is supported.
462 */
463
464 if (att->Type != GL_TEXTURE)
465 return GL_TRUE;
466
467 if (!stObj)
468 return GL_FALSE;
469
470 return screen->is_format_supported(screen, stObj->pt->format,
471 PIPE_TEXTURE_2D,
472 usage, 0);
473 }
474
475 /**
476 * Check that the framebuffer configuration is valid in terms of what
477 * the driver can support.
478 *
479 * For Gallium we only supports combined Z+stencil, not separate buffers.
480 */
481 static void
482 st_validate_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
483 {
484 struct pipe_screen *screen = ctx->st->pipe->screen;
485 const struct gl_renderbuffer *depthRb =
486 fb->Attachment[BUFFER_DEPTH].Renderbuffer;
487 const struct gl_renderbuffer *stencilRb =
488 fb->Attachment[BUFFER_STENCIL].Renderbuffer;
489 GLuint i;
490
491 if (stencilRb && depthRb && stencilRb != depthRb) {
492 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
493 return;
494 }
495
496 if (!st_validate_attachment(screen,
497 &fb->Attachment[BUFFER_DEPTH],
498 PIPE_TEXTURE_USAGE_DEPTH_STENCIL)) {
499 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
500 return;
501 }
502 if (!st_validate_attachment(screen,
503 &fb->Attachment[BUFFER_STENCIL],
504 PIPE_TEXTURE_USAGE_DEPTH_STENCIL)) {
505 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
506 return;
507 }
508 for (i = 0; i < ctx->Const.MaxColorAttachments; i++) {
509 if (!st_validate_attachment(screen,
510 &fb->Attachment[BUFFER_COLOR0 + i],
511 PIPE_TEXTURE_USAGE_RENDER_TARGET)) {
512 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
513 return;
514 }
515 }
516 }
517
518
519 /**
520 * Copy back color buffer to front color buffer.
521 */
522 static void
523 copy_back_to_front(struct st_context *st,
524 struct gl_framebuffer *fb,
525 gl_buffer_index frontIndex,
526 gl_buffer_index backIndex)
527
528 {
529 struct st_framebuffer *stfb = (struct st_framebuffer *) fb;
530 struct pipe_surface *surf_front, *surf_back;
531
532 (void) st_get_framebuffer_surface(stfb, frontIndex, &surf_front);
533 (void) st_get_framebuffer_surface(stfb, backIndex, &surf_back);
534
535 if (surf_front && surf_back) {
536 if (st->pipe->surface_copy) {
537 st->pipe->surface_copy(st->pipe,
538 surf_front, 0, 0, /* dest */
539 surf_back, 0, 0, /* src */
540 fb->Width, fb->Height);
541 } else {
542 util_surface_copy(st->pipe, FALSE,
543 surf_front, 0, 0,
544 surf_back, 0, 0,
545 fb->Width, fb->Height);
546 }
547 }
548 }
549
550
551 /**
552 * Check if we're drawing into, or read from, a front color buffer. If the
553 * front buffer is missing, create it now.
554 *
555 * The back color buffer must exist since we'll use its format/samples info
556 * for creating the front buffer.
557 *
558 * \param frontIndex either BUFFER_FRONT_LEFT or BUFFER_FRONT_RIGHT
559 * \param backIndex either BUFFER_BACK_LEFT or BUFFER_BACK_RIGHT
560 */
561 static void
562 check_create_front_buffer(GLcontext *ctx, struct gl_framebuffer *fb,
563 gl_buffer_index frontIndex,
564 gl_buffer_index backIndex)
565 {
566 if (fb->Attachment[frontIndex].Renderbuffer == NULL) {
567 GLboolean create = GL_FALSE;
568
569 /* check if drawing to or reading from front buffer */
570 if (fb->_ColorReadBufferIndex == frontIndex) {
571 create = GL_TRUE;
572 }
573 else {
574 GLuint b;
575 for (b = 0; b < fb->_NumColorDrawBuffers; b++) {
576 if (fb->_ColorDrawBufferIndexes[b] == frontIndex) {
577 create = GL_TRUE;
578 break;
579 }
580 }
581 }
582
583 if (create) {
584 struct st_renderbuffer *back;
585 struct gl_renderbuffer *front;
586 enum pipe_format colorFormat;
587 uint samples;
588
589 if (0)
590 _mesa_debug(ctx, "Allocate new front buffer\n");
591
592 /* get back renderbuffer info */
593 back = st_renderbuffer(fb->Attachment[backIndex].Renderbuffer);
594 colorFormat = back->format;
595 samples = back->Base.NumSamples;
596
597 /* create front renderbuffer */
598 front = st_new_renderbuffer_fb(colorFormat, samples, FALSE);
599 _mesa_add_renderbuffer(fb, frontIndex, front);
600
601 /* alloc texture/surface for new front buffer */
602 front->AllocStorage(ctx, front, front->InternalFormat,
603 fb->Width, fb->Height);
604
605 /* initialize the front color buffer contents by copying
606 * the back buffer.
607 */
608 copy_back_to_front(ctx->st, fb, frontIndex, backIndex);
609 }
610 }
611 }
612
613
614 /**
615 * If front left/right color buffers are missing, create them now.
616 */
617 static void
618 check_create_front_buffers(GLcontext *ctx, struct gl_framebuffer *fb)
619 {
620 /* check if we need to create the front left buffer now */
621 check_create_front_buffer(ctx, fb, BUFFER_FRONT_LEFT, BUFFER_BACK_LEFT);
622
623 if (fb->Visual.stereoMode) {
624 check_create_front_buffer(ctx, fb, BUFFER_FRONT_RIGHT, BUFFER_BACK_RIGHT);
625 }
626
627 st_invalidate_state(ctx, _NEW_BUFFERS);
628 }
629
630
631 /**
632 * Called via glDrawBuffer.
633 */
634 static void
635 st_DrawBuffers(GLcontext *ctx, GLsizei count, const GLenum *buffers)
636 {
637 (void) count;
638 (void) buffers;
639 check_create_front_buffers(ctx, ctx->DrawBuffer);
640 }
641
642
643 /**
644 * Called via glReadBuffer.
645 */
646 static void
647 st_ReadBuffer(GLcontext *ctx, GLenum buffer)
648 {
649 (void) buffer;
650 check_create_front_buffers(ctx, ctx->ReadBuffer);
651 }
652
653
654 void st_init_fbo_functions(struct dd_function_table *functions)
655 {
656 functions->NewFramebuffer = st_new_framebuffer;
657 functions->NewRenderbuffer = st_new_renderbuffer;
658 functions->BindFramebuffer = st_bind_framebuffer;
659 functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer;
660 functions->RenderTexture = st_render_texture;
661 functions->FinishRenderTexture = st_finish_render_texture;
662 functions->ValidateFramebuffer = st_validate_framebuffer;
663 /* no longer needed by core Mesa, drivers handle resizes...
664 functions->ResizeBuffers = st_resize_buffers;
665 */
666
667 functions->DrawBuffers = st_DrawBuffers;
668 functions->ReadBuffer = st_ReadBuffer;
669 }