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