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