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