Merge branch 'mesa_7_5_branch'
[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/renderbuffer.h"
41
42 #include "pipe/p_context.h"
43 #include "pipe/p_defines.h"
44 #include "pipe/p_screen.h"
45 #include "st_context.h"
46 #include "st_cb_fbo.h"
47 #include "st_cb_texture.h"
48 #include "st_format.h"
49 #include "st_public.h"
50 #include "st_texture.h"
51
52
53
54 /**
55 * Compute the renderbuffer's Red/Green/EtcBit fields from the pipe format.
56 */
57 static int
58 init_renderbuffer_bits(struct st_renderbuffer *strb,
59 enum pipe_format pipeFormat)
60 {
61 struct pipe_format_info info;
62
63 if (!st_get_format_info( pipeFormat, &info )) {
64 assert( 0 );
65 }
66
67 strb->Base._ActualFormat = info.base_format;
68 strb->Base.RedBits = info.red_bits;
69 strb->Base.GreenBits = info.green_bits;
70 strb->Base.BlueBits = info.blue_bits;
71 strb->Base.AlphaBits = info.alpha_bits;
72 strb->Base.DepthBits = info.depth_bits;
73 strb->Base.StencilBits = info.stencil_bits;
74 strb->Base.DataType = st_format_datatype(pipeFormat);
75
76 return info.size;
77 }
78
79 /**
80 * gl_renderbuffer::AllocStorage()
81 * This is called to allocate the original drawing surface, and
82 * during window resize.
83 */
84 static GLboolean
85 st_renderbuffer_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
86 GLenum internalFormat,
87 GLuint width, GLuint height)
88 {
89 struct pipe_context *pipe = ctx->st->pipe;
90 struct st_renderbuffer *strb = st_renderbuffer(rb);
91 struct pipe_texture template;
92 unsigned surface_usage;
93
94 /* Free the old surface and texture
95 */
96 pipe_surface_reference( &strb->surface, NULL );
97 pipe_texture_reference( &strb->texture, NULL );
98
99 /* Setup new texture template.
100 */
101 memset(&template, 0, sizeof(template));
102 template.target = PIPE_TEXTURE_2D;
103 if (strb->format != PIPE_FORMAT_NONE) {
104 template.format = strb->format;
105 }
106 else {
107 template.format = st_choose_renderbuffer_format(pipe, internalFormat);
108 }
109 pf_get_block(template.format, &template.block);
110 template.width[0] = width;
111 template.height[0] = height;
112 template.depth[0] = 1;
113 template.last_level = 0;
114 template.nr_samples = rb->NumSamples;
115 if (pf_is_depth_stencil(template.format)) {
116 template.tex_usage = PIPE_TEXTURE_USAGE_DEPTH_STENCIL;
117 }
118 else {
119 template.tex_usage = (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
120 PIPE_TEXTURE_USAGE_RENDER_TARGET);
121 }
122
123 /* init renderbuffer fields */
124 strb->Base.Width = width;
125 strb->Base.Height = height;
126 init_renderbuffer_bits(strb, template.format);
127
128 strb->defined = GL_FALSE; /* undefined contents now */
129
130 /* Probably need dedicated flags for surface usage too:
131 */
132 surface_usage = (PIPE_BUFFER_USAGE_GPU_READ |
133 PIPE_BUFFER_USAGE_GPU_WRITE);
134 #if 0
135 PIPE_BUFFER_USAGE_CPU_READ |
136 PIPE_BUFFER_USAGE_CPU_WRITE);
137 #endif
138
139 strb->texture = pipe->screen->texture_create( pipe->screen,
140 &template );
141
142 /* Special path for accum buffers.
143 *
144 * Try a different surface format. Since accum buffers are s/w
145 * only for now, the surface pixel format doesn't really matter,
146 * only that the buffer is large enough.
147 */
148 if (!strb->texture && template.format == DEFAULT_ACCUM_PIPE_FORMAT)
149 {
150 /* Actually, just setting this usage value should be sufficient
151 * to tell the driver to go ahead and allocate the buffer, even
152 * if HW doesn't support the format.
153 */
154 template.tex_usage = 0;
155 surface_usage = (PIPE_BUFFER_USAGE_CPU_READ |
156 PIPE_BUFFER_USAGE_CPU_WRITE);
157
158 strb->texture = pipe->screen->texture_create( pipe->screen,
159 &template );
160
161 }
162
163 if (!strb->texture)
164 return FALSE;
165
166 strb->surface = pipe->screen->get_tex_surface( pipe->screen,
167 strb->texture,
168 0, 0, 0,
169 surface_usage );
170
171 assert(strb->surface->texture);
172 assert(strb->surface->format);
173 assert(strb->surface->width == width);
174 assert(strb->surface->height == height);
175
176
177 return strb->surface != NULL;
178 }
179
180
181 /**
182 * gl_renderbuffer::Delete()
183 */
184 static void
185 st_renderbuffer_delete(struct gl_renderbuffer *rb)
186 {
187 struct st_renderbuffer *strb = st_renderbuffer(rb);
188 ASSERT(strb);
189 pipe_surface_reference(&strb->surface, NULL);
190 pipe_texture_reference(&strb->texture, NULL);
191 _mesa_free(strb);
192 }
193
194
195 /**
196 * gl_renderbuffer::GetPointer()
197 */
198 static void *
199 null_get_pointer(GLcontext * ctx, struct gl_renderbuffer *rb,
200 GLint x, GLint y)
201 {
202 /* By returning NULL we force all software rendering to go through
203 * the span routines.
204 */
205 #if 0
206 assert(0); /* Should never get called with softpipe */
207 #endif
208 return NULL;
209 }
210
211
212 /**
213 * Called via ctx->Driver.NewFramebuffer()
214 */
215 static struct gl_framebuffer *
216 st_new_framebuffer(GLcontext *ctx, GLuint name)
217 {
218 /* XXX not sure we need to subclass gl_framebuffer for pipe */
219 return _mesa_new_framebuffer(ctx, name);
220 }
221
222
223 /**
224 * Called via ctx->Driver.NewRenderbuffer()
225 */
226 static struct gl_renderbuffer *
227 st_new_renderbuffer(GLcontext *ctx, GLuint name)
228 {
229 struct st_renderbuffer *strb = ST_CALLOC_STRUCT(st_renderbuffer);
230 if (strb) {
231 _mesa_init_renderbuffer(&strb->Base, name);
232 strb->Base.Delete = st_renderbuffer_delete;
233 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
234 strb->Base.GetPointer = null_get_pointer;
235 strb->format = PIPE_FORMAT_NONE;
236 return &strb->Base;
237 }
238 return NULL;
239 }
240
241
242 /**
243 * Allocate a renderbuffer for a an on-screen window (not a user-created
244 * renderbuffer). The window system code determines the format.
245 */
246 struct gl_renderbuffer *
247 st_new_renderbuffer_fb(enum pipe_format format, int samples)
248 {
249 struct st_renderbuffer *strb;
250
251 strb = ST_CALLOC_STRUCT(st_renderbuffer);
252 if (!strb) {
253 _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
254 return NULL;
255 }
256
257 _mesa_init_renderbuffer(&strb->Base, 0);
258 strb->Base.ClassID = 0x4242; /* just a unique value */
259 strb->Base.NumSamples = samples;
260 strb->format = format;
261
262 switch (format) {
263 case PIPE_FORMAT_A8R8G8B8_UNORM:
264 case PIPE_FORMAT_B8G8R8A8_UNORM:
265 case PIPE_FORMAT_X8R8G8B8_UNORM:
266 case PIPE_FORMAT_B8G8R8X8_UNORM:
267 case PIPE_FORMAT_A1R5G5B5_UNORM:
268 case PIPE_FORMAT_A4R4G4B4_UNORM:
269 case PIPE_FORMAT_R5G6B5_UNORM:
270 strb->Base.InternalFormat = GL_RGBA;
271 strb->Base._BaseFormat = GL_RGBA;
272 break;
273 case PIPE_FORMAT_Z16_UNORM:
274 strb->Base.InternalFormat = GL_DEPTH_COMPONENT16;
275 strb->Base._BaseFormat = GL_DEPTH_COMPONENT;
276 break;
277 case PIPE_FORMAT_Z32_UNORM:
278 strb->Base.InternalFormat = GL_DEPTH_COMPONENT32;
279 strb->Base._BaseFormat = GL_DEPTH_COMPONENT;
280 break;
281 case PIPE_FORMAT_S8Z24_UNORM:
282 case PIPE_FORMAT_Z24S8_UNORM:
283 case PIPE_FORMAT_X8Z24_UNORM:
284 case PIPE_FORMAT_Z24X8_UNORM:
285 strb->Base.InternalFormat = GL_DEPTH24_STENCIL8_EXT;
286 strb->Base._BaseFormat = GL_DEPTH_STENCIL_EXT;
287 break;
288 case PIPE_FORMAT_S8_UNORM:
289 strb->Base.InternalFormat = GL_STENCIL_INDEX8_EXT;
290 strb->Base._BaseFormat = GL_STENCIL_INDEX;
291 break;
292 case DEFAULT_ACCUM_PIPE_FORMAT: /*PIPE_FORMAT_R16G16B16A16_SNORM*/
293 strb->Base.InternalFormat = GL_RGBA16;
294 strb->Base._BaseFormat = GL_RGBA;
295 break;
296 default:
297 _mesa_problem(NULL,
298 "Unexpected format in st_new_renderbuffer_fb");
299 return NULL;
300 }
301
302 /* st-specific methods */
303 strb->Base.Delete = st_renderbuffer_delete;
304 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
305 strb->Base.GetPointer = null_get_pointer;
306
307 /* surface is allocated in st_renderbuffer_alloc_storage() */
308 strb->surface = NULL;
309
310 return &strb->Base;
311 }
312
313
314
315
316 /**
317 * Called via ctx->Driver.BindFramebufferEXT().
318 */
319 static void
320 st_bind_framebuffer(GLcontext *ctx, GLenum target,
321 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
322 {
323
324 }
325
326 /**
327 * Called by ctx->Driver.FramebufferRenderbuffer
328 */
329 static void
330 st_framebuffer_renderbuffer(GLcontext *ctx,
331 struct gl_framebuffer *fb,
332 GLenum attachment,
333 struct gl_renderbuffer *rb)
334 {
335 /* XXX no need for derivation? */
336 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
337 }
338
339
340 /**
341 * Called by ctx->Driver.RenderTexture
342 */
343 static void
344 st_render_texture(GLcontext *ctx,
345 struct gl_framebuffer *fb,
346 struct gl_renderbuffer_attachment *att)
347 {
348 struct pipe_screen *screen = ctx->st->pipe->screen;
349 struct st_renderbuffer *strb;
350 struct gl_renderbuffer *rb;
351 struct pipe_texture *pt = st_get_texobj_texture(att->Texture);
352 struct st_texture_object *stObj;
353 const struct gl_texture_image *texImage =
354 att->Texture->Image[att->CubeMapFace][att->TextureLevel];
355
356 if (!pt)
357 return;
358
359 /* create new renderbuffer which wraps the texture image */
360 rb = st_new_renderbuffer(ctx, 0);
361 if (!rb) {
362 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
363 return;
364 }
365
366 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
367 assert(rb->RefCount == 1);
368 rb->AllocStorage = NULL; /* should not get called */
369 strb = st_renderbuffer(rb);
370
371 assert(strb->Base.RefCount > 0);
372
373 /* get the texture for the texture object */
374 stObj = st_texture_object(att->Texture);
375
376 /* point renderbuffer at texobject */
377 strb->rtt = stObj;
378 strb->rtt_level = att->TextureLevel;
379 strb->rtt_face = att->CubeMapFace;
380 strb->rtt_slice = att->Zoffset;
381
382 rb->Width = texImage->Width2;
383 rb->Height = texImage->Height2;
384 /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
385
386 /*printf("***** pipe texture %d x %d\n", pt->width[0], pt->height[0]);*/
387
388 pipe_texture_reference( &strb->texture, pt );
389
390 pipe_surface_reference(&strb->surface, NULL);
391
392 /* new surface for rendering into the texture */
393 strb->surface = screen->get_tex_surface(screen,
394 strb->texture,
395 strb->rtt_face,
396 strb->rtt_level,
397 strb->rtt_slice,
398 PIPE_BUFFER_USAGE_GPU_READ |
399 PIPE_BUFFER_USAGE_GPU_WRITE);
400
401 init_renderbuffer_bits(strb, pt->format);
402
403 /*
404 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
405 att->Texture, pt, strb->surface, rb->Width, rb->Height);
406 */
407
408 /* Invalidate buffer state so that the pipe's framebuffer state
409 * gets updated.
410 * That's where the new renderbuffer (which we just created) gets
411 * passed to the pipe as a (color/depth) render target.
412 */
413 st_invalidate_state(ctx, _NEW_BUFFERS);
414 }
415
416
417 /**
418 * Called via ctx->Driver.FinishRenderTexture.
419 */
420 static void
421 st_finish_render_texture(GLcontext *ctx,
422 struct gl_renderbuffer_attachment *att)
423 {
424 struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer);
425
426 if (!strb)
427 return;
428
429 st_flush( ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL );
430
431 if (strb->surface)
432 pipe_surface_reference( &strb->surface, NULL );
433
434 strb->rtt = NULL;
435
436 /*
437 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
438 */
439
440 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
441
442 /* restore previous framebuffer state */
443 st_invalidate_state(ctx, _NEW_BUFFERS);
444 }
445
446
447 /**
448 * Check that the framebuffer configuration is valid in terms of what
449 * the driver can support.
450 *
451 * For Gallium we only supports combined Z+stencil, not separate buffers.
452 */
453 static void
454 st_validate_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
455 {
456 const struct gl_renderbuffer *depthRb =
457 fb->Attachment[BUFFER_DEPTH].Renderbuffer;
458 const struct gl_renderbuffer *stencilRb =
459 fb->Attachment[BUFFER_STENCIL].Renderbuffer;
460
461 if (stencilRb && depthRb && stencilRb != depthRb) {
462 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
463 }
464 }
465
466
467 /**
468 * Copy back color buffer to front color buffer.
469 */
470 static void
471 copy_back_to_front(struct st_context *st,
472 struct gl_framebuffer *fb,
473 gl_buffer_index frontIndex,
474 gl_buffer_index backIndex)
475
476 {
477 struct st_framebuffer *stfb = (struct st_framebuffer *) fb;
478 struct pipe_surface *surf_front, *surf_back;
479
480 (void) st_get_framebuffer_surface(stfb, frontIndex, &surf_front);
481 (void) st_get_framebuffer_surface(stfb, backIndex, &surf_back);
482
483 if (surf_front && surf_back) {
484 st->pipe->surface_copy(st->pipe,
485 surf_front, 0, 0, /* dest */
486 surf_back, 0, 0, /* src */
487 fb->Width, fb->Height);
488 }
489 }
490
491
492 /**
493 * Check if we're drawing into, or read from, a front color buffer. If the
494 * front buffer is missing, create it now.
495 *
496 * The back color buffer must exist since we'll use its format/samples info
497 * for creating the front buffer.
498 *
499 * \param frontIndex either BUFFER_FRONT_LEFT or BUFFER_FRONT_RIGHT
500 * \param backIndex either BUFFER_BACK_LEFT or BUFFER_BACK_RIGHT
501 */
502 static void
503 check_create_front_buffer(GLcontext *ctx, struct gl_framebuffer *fb,
504 gl_buffer_index frontIndex,
505 gl_buffer_index backIndex)
506 {
507 if (fb->Attachment[frontIndex].Renderbuffer == NULL) {
508 GLboolean create = GL_FALSE;
509
510 /* check if drawing to or reading from front buffer */
511 if (fb->_ColorReadBufferIndex == frontIndex) {
512 create = GL_TRUE;
513 }
514 else {
515 GLuint b;
516 for (b = 0; b < fb->_NumColorDrawBuffers; b++) {
517 if (fb->_ColorDrawBufferIndexes[b] == frontIndex) {
518 create = GL_TRUE;
519 break;
520 }
521 }
522 }
523
524 if (create) {
525 struct st_renderbuffer *back;
526 struct gl_renderbuffer *front;
527 enum pipe_format colorFormat;
528 uint samples;
529
530 if (0)
531 _mesa_debug(ctx, "Allocate new front buffer\n");
532
533 /* get back renderbuffer info */
534 back = st_renderbuffer(fb->Attachment[backIndex].Renderbuffer);
535 colorFormat = back->format;
536 samples = back->Base.NumSamples;
537
538 /* create front renderbuffer */
539 front = st_new_renderbuffer_fb(colorFormat, samples);
540 _mesa_add_renderbuffer(fb, frontIndex, front);
541
542 /* alloc texture/surface for new front buffer */
543 front->AllocStorage(ctx, front, front->InternalFormat,
544 fb->Width, fb->Height);
545
546 /* initialize the front color buffer contents by copying
547 * the back buffer.
548 */
549 copy_back_to_front(ctx->st, fb, frontIndex, backIndex);
550 }
551 }
552 }
553
554
555 /**
556 * If front left/right color buffers are missing, create them now.
557 */
558 static void
559 check_create_front_buffers(GLcontext *ctx, struct gl_framebuffer *fb)
560 {
561 /* check if we need to create the front left buffer now */
562 check_create_front_buffer(ctx, fb, BUFFER_FRONT_LEFT, BUFFER_BACK_LEFT);
563
564 if (fb->Visual.stereoMode) {
565 check_create_front_buffer(ctx, fb, BUFFER_FRONT_RIGHT, BUFFER_BACK_RIGHT);
566 }
567
568 st_invalidate_state(ctx, _NEW_BUFFERS);
569 }
570
571
572 /**
573 * Called via glDrawBuffer.
574 */
575 static void
576 st_DrawBuffers(GLcontext *ctx, GLsizei count, const GLenum *buffers)
577 {
578 (void) count;
579 (void) buffers;
580 check_create_front_buffers(ctx, ctx->DrawBuffer);
581 }
582
583
584 /**
585 * Called via glReadBuffer.
586 */
587 static void
588 st_ReadBuffer(GLcontext *ctx, GLenum buffer)
589 {
590 (void) buffer;
591 check_create_front_buffers(ctx, ctx->ReadBuffer);
592 }
593
594
595 void st_init_fbo_functions(struct dd_function_table *functions)
596 {
597 functions->NewFramebuffer = st_new_framebuffer;
598 functions->NewRenderbuffer = st_new_renderbuffer;
599 functions->BindFramebuffer = st_bind_framebuffer;
600 functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer;
601 functions->RenderTexture = st_render_texture;
602 functions->FinishRenderTexture = st_finish_render_texture;
603 functions->ValidateFramebuffer = st_validate_framebuffer;
604 /* no longer needed by core Mesa, drivers handle resizes...
605 functions->ResizeBuffers = st_resize_buffers;
606 */
607
608 functions->DrawBuffers = st_DrawBuffers;
609 functions->ReadBuffer = st_ReadBuffer;
610 }