Merge branch 'primitive-restart-cleanup'
[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_cb_flush.h"
49 #include "st_format.h"
50 #include "st_texture.h"
51 #include "st_manager.h"
52
53 #include "util/u_format.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(struct gl_context * ctx, struct gl_renderbuffer *rb,
64 GLenum internalFormat,
65 GLuint width, GLuint height)
66 {
67 struct st_context *st = st_context(ctx);
68 struct pipe_screen *screen = 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, rb->NumSamples);
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_resource template;
101
102 /* Free the old surface and texture
103 */
104 pipe_surface_reference( &strb->surface, NULL );
105 pipe_resource_reference( &strb->texture, NULL );
106 pipe_sampler_view_reference(&strb->sampler_view, NULL);
107
108 /* Setup new texture template.
109 */
110 memset(&template, 0, sizeof(template));
111 template.target = st->internal_target;
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.bind = PIPE_BIND_DEPTH_STENCIL;
120 }
121 else {
122 template.bind = (PIPE_BIND_DISPLAY_TARGET |
123 PIPE_BIND_RENDER_TARGET);
124 }
125
126 strb->texture = screen->resource_create(screen, &template);
127
128 if (!strb->texture)
129 return FALSE;
130
131 strb->surface = screen->get_tex_surface(screen,
132 strb->texture,
133 0, 0, 0,
134 template.bind);
135 if (strb->surface) {
136 assert(strb->surface->texture);
137 assert(strb->surface->format);
138 assert(strb->surface->width == width);
139 assert(strb->surface->height == height);
140 }
141
142 return strb->surface != NULL;
143 }
144 }
145
146
147 /**
148 * gl_renderbuffer::Delete()
149 */
150 static void
151 st_renderbuffer_delete(struct gl_renderbuffer *rb)
152 {
153 struct st_renderbuffer *strb = st_renderbuffer(rb);
154 ASSERT(strb);
155 pipe_surface_reference(&strb->surface, NULL);
156 pipe_resource_reference(&strb->texture, NULL);
157 pipe_sampler_view_reference(&strb->sampler_view, NULL);
158 free(strb->data);
159 free(strb);
160 }
161
162
163 /**
164 * gl_renderbuffer::GetPointer()
165 */
166 static void *
167 null_get_pointer(struct gl_context * ctx, struct gl_renderbuffer *rb,
168 GLint x, GLint y)
169 {
170 /* By returning NULL we force all software rendering to go through
171 * the span routines.
172 */
173 #if 0
174 assert(0); /* Should never get called with softpipe */
175 #endif
176 return NULL;
177 }
178
179
180 /**
181 * Called via ctx->Driver.NewFramebuffer()
182 */
183 static struct gl_framebuffer *
184 st_new_framebuffer(struct gl_context *ctx, GLuint name)
185 {
186 /* XXX not sure we need to subclass gl_framebuffer for pipe */
187 return _mesa_new_framebuffer(ctx, name);
188 }
189
190
191 /**
192 * Called via ctx->Driver.NewRenderbuffer()
193 */
194 static struct gl_renderbuffer *
195 st_new_renderbuffer(struct gl_context *ctx, GLuint name)
196 {
197 struct st_renderbuffer *strb = ST_CALLOC_STRUCT(st_renderbuffer);
198 if (strb) {
199 _mesa_init_renderbuffer(&strb->Base, name);
200 strb->Base.Delete = st_renderbuffer_delete;
201 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
202 strb->Base.GetPointer = null_get_pointer;
203 strb->format = PIPE_FORMAT_NONE;
204 return &strb->Base;
205 }
206 return NULL;
207 }
208
209
210 /**
211 * Allocate a renderbuffer for a an on-screen window (not a user-created
212 * renderbuffer). The window system code determines the format.
213 */
214 struct gl_renderbuffer *
215 st_new_renderbuffer_fb(enum pipe_format format, int samples, boolean sw)
216 {
217 struct st_renderbuffer *strb;
218
219 strb = ST_CALLOC_STRUCT(st_renderbuffer);
220 if (!strb) {
221 _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
222 return NULL;
223 }
224
225 _mesa_init_renderbuffer(&strb->Base, 0);
226 strb->Base.ClassID = 0x4242; /* just a unique value */
227 strb->Base.NumSamples = samples;
228 strb->Base.Format = st_pipe_format_to_mesa_format(format);
229 strb->Base.DataType = st_format_datatype(format);
230 strb->format = format;
231 strb->software = sw;
232
233 switch (format) {
234 case PIPE_FORMAT_R8G8B8A8_UNORM:
235 case PIPE_FORMAT_B8G8R8A8_UNORM:
236 case PIPE_FORMAT_A8R8G8B8_UNORM:
237 case PIPE_FORMAT_R8G8B8X8_UNORM:
238 case PIPE_FORMAT_B8G8R8X8_UNORM:
239 case PIPE_FORMAT_X8R8G8B8_UNORM:
240 case PIPE_FORMAT_B5G5R5A1_UNORM:
241 case PIPE_FORMAT_B4G4R4A4_UNORM:
242 case PIPE_FORMAT_B5G6R5_UNORM:
243 strb->Base.InternalFormat = GL_RGBA;
244 break;
245 case PIPE_FORMAT_Z16_UNORM:
246 strb->Base.InternalFormat = GL_DEPTH_COMPONENT16;
247 break;
248 case PIPE_FORMAT_Z32_UNORM:
249 strb->Base.InternalFormat = GL_DEPTH_COMPONENT32;
250 break;
251 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
252 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
253 case PIPE_FORMAT_Z24X8_UNORM:
254 case PIPE_FORMAT_X8Z24_UNORM:
255 strb->Base.InternalFormat = GL_DEPTH24_STENCIL8_EXT;
256 break;
257 case PIPE_FORMAT_S8_USCALED:
258 strb->Base.InternalFormat = GL_STENCIL_INDEX8_EXT;
259 break;
260 case PIPE_FORMAT_R16G16B16A16_SNORM:
261 strb->Base.InternalFormat = GL_RGBA16;
262 break;
263 case PIPE_FORMAT_R8_UNORM:
264 strb->Base.InternalFormat = GL_R8;
265 break;
266 case PIPE_FORMAT_R8G8_UNORM:
267 strb->Base.InternalFormat = GL_RG8;
268 break;
269 case PIPE_FORMAT_R16_UNORM:
270 strb->Base.InternalFormat = GL_R16;
271 break;
272 case PIPE_FORMAT_R16G16_UNORM:
273 strb->Base.InternalFormat = GL_RG16;
274 break;
275 default:
276 _mesa_problem(NULL,
277 "Unexpected format in st_new_renderbuffer_fb");
278 free(strb);
279 return NULL;
280 }
281
282 /* st-specific methods */
283 strb->Base.Delete = st_renderbuffer_delete;
284 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
285 strb->Base.GetPointer = null_get_pointer;
286
287 /* surface is allocated in st_renderbuffer_alloc_storage() */
288 strb->surface = NULL;
289
290 return &strb->Base;
291 }
292
293
294
295
296 /**
297 * Called via ctx->Driver.BindFramebufferEXT().
298 */
299 static void
300 st_bind_framebuffer(struct gl_context *ctx, GLenum target,
301 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
302 {
303
304 }
305
306 /**
307 * Called by ctx->Driver.FramebufferRenderbuffer
308 */
309 static void
310 st_framebuffer_renderbuffer(struct gl_context *ctx,
311 struct gl_framebuffer *fb,
312 GLenum attachment,
313 struct gl_renderbuffer *rb)
314 {
315 /* XXX no need for derivation? */
316 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
317 }
318
319
320 /**
321 * Called by ctx->Driver.RenderTexture
322 */
323 static void
324 st_render_texture(struct gl_context *ctx,
325 struct gl_framebuffer *fb,
326 struct gl_renderbuffer_attachment *att)
327 {
328 struct st_context *st = st_context(ctx);
329 struct pipe_context *pipe = st->pipe;
330 struct pipe_screen *screen = pipe->screen;
331 struct st_renderbuffer *strb;
332 struct gl_renderbuffer *rb;
333 struct pipe_resource *pt = st_get_texobj_resource(att->Texture);
334 struct st_texture_object *stObj;
335 const struct gl_texture_image *texImage;
336
337 /* When would this fail? Perhaps assert? */
338 if (!pt)
339 return;
340
341 /* get pointer to texture image we're rendeing to */
342 texImage = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
343
344 /* create new renderbuffer which wraps the texture image */
345 rb = st_new_renderbuffer(ctx, 0);
346 if (!rb) {
347 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
348 return;
349 }
350
351 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
352 assert(rb->RefCount == 1);
353 rb->AllocStorage = NULL; /* should not get called */
354 strb = st_renderbuffer(rb);
355
356 assert(strb->Base.RefCount > 0);
357
358 /* get the texture for the texture object */
359 stObj = st_texture_object(att->Texture);
360
361 /* point renderbuffer at texobject */
362 strb->rtt = stObj;
363 strb->rtt_level = att->TextureLevel;
364 strb->rtt_face = att->CubeMapFace;
365 strb->rtt_slice = att->Zoffset;
366
367 rb->Width = texImage->Width2;
368 rb->Height = texImage->Height2;
369 rb->_BaseFormat = texImage->_BaseFormat;
370 /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
371
372 /*printf("***** pipe texture %d x %d\n", pt->width0, pt->height0);*/
373
374 pipe_resource_reference( &strb->texture, pt );
375
376 pipe_surface_reference(&strb->surface, NULL);
377
378 pipe_sampler_view_reference(&strb->sampler_view,
379 st_get_texture_sampler_view(stObj, pipe));
380
381 assert(strb->rtt_level <= strb->texture->last_level);
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_BIND_RENDER_TARGET);
390
391 strb->format = pt->format;
392
393 strb->Base.Format = st_pipe_format_to_mesa_format(pt->format);
394 strb->Base.DataType = st_format_datatype(pt->format);
395
396 /*
397 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
398 att->Texture, pt, strb->surface, rb->Width, rb->Height);
399 */
400
401 /* Invalidate buffer state so that the pipe's framebuffer state
402 * gets updated.
403 * That's where the new renderbuffer (which we just created) gets
404 * passed to the pipe as a (color/depth) render target.
405 */
406 st_invalidate_state(ctx, _NEW_BUFFERS);
407 }
408
409
410 /**
411 * Called via ctx->Driver.FinishRenderTexture.
412 */
413 static void
414 st_finish_render_texture(struct gl_context *ctx,
415 struct gl_renderbuffer_attachment *att)
416 {
417 struct st_context *st = st_context(ctx);
418 struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer);
419
420 if (!strb)
421 return;
422
423 st_flush(st, PIPE_FLUSH_RENDER_CACHE, NULL);
424
425 strb->rtt = NULL;
426
427 /*
428 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
429 */
430
431 /* restore previous framebuffer state */
432 st_invalidate_state(ctx, _NEW_BUFFERS);
433 }
434
435
436 /**
437 * Validate a renderbuffer attachment for a particular set of bindings.
438 */
439 static GLboolean
440 st_validate_attachment(struct pipe_screen *screen,
441 const struct gl_renderbuffer_attachment *att,
442 unsigned bindings)
443 {
444 const struct st_texture_object *stObj = st_texture_object(att->Texture);
445
446 /* Only validate texture attachments for now, since
447 * st_renderbuffer_alloc_storage makes sure that
448 * the format is supported.
449 */
450 if (att->Type != GL_TEXTURE)
451 return GL_TRUE;
452
453 if (!stObj)
454 return GL_FALSE;
455
456 return screen->is_format_supported(screen, stObj->pt->format,
457 PIPE_TEXTURE_2D,
458 stObj->pt->nr_samples, bindings, 0);
459 }
460
461
462 /**
463 * Check if two renderbuffer attachments name a combined depth/stencil
464 * renderbuffer.
465 */
466 GLboolean
467 st_is_depth_stencil_combined(const struct gl_renderbuffer_attachment *depth,
468 const struct gl_renderbuffer_attachment *stencil)
469 {
470 assert(depth && stencil);
471
472 if (depth->Type == stencil->Type) {
473 if (depth->Type == GL_RENDERBUFFER_EXT &&
474 depth->Renderbuffer == stencil->Renderbuffer)
475 return GL_TRUE;
476
477 if (depth->Type == GL_TEXTURE &&
478 depth->Texture == stencil->Texture)
479 return GL_TRUE;
480 }
481
482 return GL_FALSE;
483 }
484
485
486 /**
487 * Check that the framebuffer configuration is valid in terms of what
488 * the driver can support.
489 *
490 * For Gallium we only supports combined Z+stencil, not separate buffers.
491 */
492 static void
493 st_validate_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
494 {
495 struct st_context *st = st_context(ctx);
496 struct pipe_screen *screen = st->pipe->screen;
497 const struct gl_renderbuffer_attachment *depth =
498 &fb->Attachment[BUFFER_DEPTH];
499 const struct gl_renderbuffer_attachment *stencil =
500 &fb->Attachment[BUFFER_STENCIL];
501 GLuint i;
502
503 if (depth->Type && stencil->Type && depth->Type != stencil->Type) {
504 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
505 return;
506 }
507 if (depth->Type == GL_RENDERBUFFER_EXT &&
508 stencil->Type == GL_RENDERBUFFER_EXT &&
509 depth->Renderbuffer != stencil->Renderbuffer) {
510 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
511 return;
512 }
513 if (depth->Type == GL_TEXTURE &&
514 stencil->Type == GL_TEXTURE &&
515 depth->Texture != stencil->Texture) {
516 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
517 return;
518 }
519
520 if (!st_validate_attachment(screen,
521 depth,
522 PIPE_BIND_DEPTH_STENCIL)) {
523 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
524 return;
525 }
526 if (!st_validate_attachment(screen,
527 stencil,
528 PIPE_BIND_DEPTH_STENCIL)) {
529 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
530 return;
531 }
532 for (i = 0; i < ctx->Const.MaxColorAttachments; i++) {
533 if (!st_validate_attachment(screen,
534 &fb->Attachment[BUFFER_COLOR0 + i],
535 PIPE_BIND_RENDER_TARGET)) {
536 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
537 return;
538 }
539 }
540 }
541
542
543 /**
544 * Called via glDrawBuffer.
545 */
546 static void
547 st_DrawBuffers(struct gl_context *ctx, GLsizei count, const GLenum *buffers)
548 {
549 struct st_context *st = st_context(ctx);
550 struct gl_framebuffer *fb = ctx->DrawBuffer;
551 GLuint i;
552
553 (void) count;
554 (void) buffers;
555
556 /* add the renderbuffers on demand */
557 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
558 gl_buffer_index idx = fb->_ColorDrawBufferIndexes[i];
559 st_manager_add_color_renderbuffer(st, fb, idx);
560 }
561 }
562
563
564 /**
565 * Called via glReadBuffer.
566 */
567 static void
568 st_ReadBuffer(struct gl_context *ctx, GLenum buffer)
569 {
570 struct st_context *st = st_context(ctx);
571 struct gl_framebuffer *fb = ctx->ReadBuffer;
572
573 (void) buffer;
574
575 /* add the renderbuffer on demand */
576 st_manager_add_color_renderbuffer(st, fb, fb->_ColorReadBufferIndex);
577 }
578
579
580 void st_init_fbo_functions(struct dd_function_table *functions)
581 {
582 #if FEATURE_EXT_framebuffer_object
583 functions->NewFramebuffer = st_new_framebuffer;
584 functions->NewRenderbuffer = st_new_renderbuffer;
585 functions->BindFramebuffer = st_bind_framebuffer;
586 functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer;
587 functions->RenderTexture = st_render_texture;
588 functions->FinishRenderTexture = st_finish_render_texture;
589 functions->ValidateFramebuffer = st_validate_framebuffer;
590 #endif
591 /* no longer needed by core Mesa, drivers handle resizes...
592 functions->ResizeBuffers = st_resize_buffers;
593 */
594
595 functions->DrawBuffers = st_DrawBuffers;
596 functions->ReadBuffer = st_ReadBuffer;
597 }
598
599 /* XXX unused ? */
600 struct pipe_sampler_view *
601 st_get_renderbuffer_sampler_view(struct st_renderbuffer *rb,
602 struct pipe_context *pipe)
603 {
604 if (!rb->sampler_view) {
605 rb->sampler_view = st_create_texture_sampler_view(pipe, rb->texture);
606 }
607
608 return rb->sampler_view;
609 }