mesa/st: enable GL_EXT_framebuffer_sRGB
[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/mfeatures.h"
42 #include "main/renderbuffer.h"
43
44 #include "pipe/p_context.h"
45 #include "pipe/p_defines.h"
46 #include "pipe/p_screen.h"
47 #include "st_context.h"
48 #include "st_cb_fbo.h"
49 #include "st_cb_flush.h"
50 #include "st_format.h"
51 #include "st_texture.h"
52 #include "st_manager.h"
53
54 #include "util/u_format.h"
55 #include "util/u_inlines.h"
56 #include "util/u_surface.h"
57
58
59 /**
60 * gl_renderbuffer::AllocStorage()
61 * This is called to allocate the original drawing surface, and
62 * during window resize.
63 */
64 static GLboolean
65 st_renderbuffer_alloc_storage(struct gl_context * ctx,
66 struct gl_renderbuffer *rb,
67 GLenum internalFormat,
68 GLuint width, GLuint height)
69 {
70 struct st_context *st = st_context(ctx);
71 struct pipe_context *pipe = st->pipe;
72 struct pipe_screen *screen = st->pipe->screen;
73 struct st_renderbuffer *strb = st_renderbuffer(rb);
74 enum pipe_format format;
75 struct pipe_surface surf_tmpl;
76
77 if (strb->format != PIPE_FORMAT_NONE)
78 format = strb->format;
79 else
80 format = st_choose_renderbuffer_format(screen, internalFormat,
81 rb->NumSamples);
82
83 /* init renderbuffer fields */
84 strb->Base.Width = width;
85 strb->Base.Height = height;
86 strb->Base.Format = st_pipe_format_to_mesa_format(format);
87 strb->Base.DataType = st_format_datatype(format);
88
89 strb->defined = GL_FALSE; /* undefined contents now */
90
91 if (strb->software) {
92 size_t size;
93
94 free(strb->data);
95
96 assert(strb->format != PIPE_FORMAT_NONE);
97
98 strb->stride = util_format_get_stride(strb->format, width);
99 size = util_format_get_2d_size(strb->format, strb->stride, height);
100
101 strb->data = malloc(size);
102
103 return strb->data != NULL;
104 }
105 else {
106 struct pipe_resource template;
107
108 /* Free the old surface and texture
109 */
110 pipe_surface_reference( &strb->surface, NULL );
111 pipe_resource_reference( &strb->texture, NULL );
112 pipe_sampler_view_reference(&strb->sampler_view, NULL);
113
114 /* Setup new texture template.
115 */
116 memset(&template, 0, sizeof(template));
117 template.target = st->internal_target;
118 template.format = format;
119 template.width0 = width;
120 template.height0 = height;
121 template.depth0 = 1;
122 template.array_size = 1;
123 template.last_level = 0;
124 template.nr_samples = rb->NumSamples;
125 if (util_format_is_depth_or_stencil(format)) {
126 template.bind = PIPE_BIND_DEPTH_STENCIL;
127 }
128 else {
129 template.bind = (PIPE_BIND_DISPLAY_TARGET |
130 PIPE_BIND_RENDER_TARGET);
131 }
132
133 strb->texture = screen->resource_create(screen, &template);
134
135 if (!strb->texture)
136 return FALSE;
137
138 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
139 u_surface_default_template(&surf_tmpl, strb->texture, template.bind);
140 strb->surface = pipe->create_surface(pipe,
141 strb->texture,
142 &surf_tmpl);
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_resource_reference(&strb->texture, NULL);
165 pipe_sampler_view_reference(&strb->sampler_view, NULL);
166 free(strb->data);
167 free(strb);
168 }
169
170
171 /**
172 * gl_renderbuffer::GetPointer()
173 */
174 static void *
175 null_get_pointer(struct gl_context * 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(struct gl_context *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(struct gl_context *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_R8G8B8A8_UNORM:
243 case PIPE_FORMAT_B8G8R8A8_UNORM:
244 case PIPE_FORMAT_A8R8G8B8_UNORM:
245 case PIPE_FORMAT_R8G8B8X8_UNORM:
246 case PIPE_FORMAT_B8G8R8X8_UNORM:
247 case PIPE_FORMAT_X8R8G8B8_UNORM:
248 case PIPE_FORMAT_B5G5R5A1_UNORM:
249 case PIPE_FORMAT_B4G4R4A4_UNORM:
250 case PIPE_FORMAT_B5G6R5_UNORM:
251 strb->Base.InternalFormat = GL_RGBA;
252 break;
253 case PIPE_FORMAT_Z16_UNORM:
254 strb->Base.InternalFormat = GL_DEPTH_COMPONENT16;
255 break;
256 case PIPE_FORMAT_Z32_UNORM:
257 strb->Base.InternalFormat = GL_DEPTH_COMPONENT32;
258 break;
259 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
260 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
261 case PIPE_FORMAT_Z24X8_UNORM:
262 case PIPE_FORMAT_X8Z24_UNORM:
263 strb->Base.InternalFormat = GL_DEPTH24_STENCIL8_EXT;
264 break;
265 case PIPE_FORMAT_S8_USCALED:
266 strb->Base.InternalFormat = GL_STENCIL_INDEX8_EXT;
267 break;
268 case PIPE_FORMAT_R16G16B16A16_SNORM:
269 strb->Base.InternalFormat = GL_RGBA16;
270 break;
271 case PIPE_FORMAT_R8_UNORM:
272 strb->Base.InternalFormat = GL_R8;
273 break;
274 case PIPE_FORMAT_R8G8_UNORM:
275 strb->Base.InternalFormat = GL_RG8;
276 break;
277 case PIPE_FORMAT_R16_UNORM:
278 strb->Base.InternalFormat = GL_R16;
279 break;
280 case PIPE_FORMAT_R16G16_UNORM:
281 strb->Base.InternalFormat = GL_RG16;
282 break;
283 default:
284 _mesa_problem(NULL,
285 "Unexpected format in st_new_renderbuffer_fb");
286 free(strb);
287 return NULL;
288 }
289
290 /* st-specific methods */
291 strb->Base.Delete = st_renderbuffer_delete;
292 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
293 strb->Base.GetPointer = null_get_pointer;
294
295 /* surface is allocated in st_renderbuffer_alloc_storage() */
296 strb->surface = NULL;
297
298 return &strb->Base;
299 }
300
301
302
303
304 /**
305 * Called via ctx->Driver.BindFramebufferEXT().
306 */
307 static void
308 st_bind_framebuffer(struct gl_context *ctx, GLenum target,
309 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
310 {
311
312 }
313
314 /**
315 * Called by ctx->Driver.FramebufferRenderbuffer
316 */
317 static void
318 st_framebuffer_renderbuffer(struct gl_context *ctx,
319 struct gl_framebuffer *fb,
320 GLenum attachment,
321 struct gl_renderbuffer *rb)
322 {
323 /* XXX no need for derivation? */
324 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
325 }
326
327
328 /**
329 * Called by ctx->Driver.RenderTexture
330 */
331 static void
332 st_render_texture(struct gl_context *ctx,
333 struct gl_framebuffer *fb,
334 struct gl_renderbuffer_attachment *att)
335 {
336 struct st_context *st = st_context(ctx);
337 struct pipe_context *pipe = st->pipe;
338 struct st_renderbuffer *strb;
339 struct gl_renderbuffer *rb;
340 struct pipe_resource *pt = st_get_texobj_resource(att->Texture);
341 struct st_texture_object *stObj;
342 const struct gl_texture_image *texImage;
343 struct pipe_surface surf_tmpl;
344
345 /* When would this fail? Perhaps assert? */
346 if (!pt)
347 return;
348
349 /* get pointer to texture image we're rendeing to */
350 texImage = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
351
352 /* create new renderbuffer which wraps the texture image */
353 rb = st_new_renderbuffer(ctx, 0);
354 if (!rb) {
355 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
356 return;
357 }
358
359 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
360 assert(rb->RefCount == 1);
361 rb->AllocStorage = NULL; /* should not get called */
362 strb = st_renderbuffer(rb);
363
364 assert(strb->Base.RefCount > 0);
365
366 /* get the texture for the texture object */
367 stObj = st_texture_object(att->Texture);
368
369 /* point renderbuffer at texobject */
370 strb->rtt = stObj;
371 strb->rtt_level = att->TextureLevel;
372 strb->rtt_face = att->CubeMapFace;
373 strb->rtt_slice = att->Zoffset;
374
375 rb->Width = texImage->Width2;
376 rb->Height = texImage->Height2;
377 rb->_BaseFormat = texImage->_BaseFormat;
378 /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
379
380 /*printf("***** pipe texture %d x %d\n", pt->width0, pt->height0);*/
381
382 pipe_resource_reference( &strb->texture, pt );
383
384 pipe_surface_reference(&strb->surface, NULL);
385
386 pipe_sampler_view_reference(&strb->sampler_view,
387 st_get_texture_sampler_view(stObj, pipe));
388
389 assert(strb->rtt_level <= strb->texture->last_level);
390
391 /* new surface for rendering into the texture */
392 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
393 surf_tmpl.format = ctx->Color.sRGBEnabled ? strb->texture->format : util_format_linear(strb->texture->format);
394 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
395 surf_tmpl.u.tex.level = strb->rtt_level;
396 surf_tmpl.u.tex.first_layer = strb->rtt_face + strb->rtt_slice;
397 surf_tmpl.u.tex.last_layer = strb->rtt_face + strb->rtt_slice;
398 strb->surface = pipe->create_surface(pipe,
399 strb->texture,
400 &surf_tmpl);
401
402 strb->format = pt->format;
403
404 strb->Base.Format = st_pipe_format_to_mesa_format(pt->format);
405 strb->Base.DataType = st_format_datatype(pt->format);
406
407 /*
408 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
409 att->Texture, pt, strb->surface, rb->Width, rb->Height);
410 */
411
412 /* Invalidate buffer state so that the pipe's framebuffer state
413 * gets updated.
414 * That's where the new renderbuffer (which we just created) gets
415 * passed to the pipe as a (color/depth) render target.
416 */
417 st_invalidate_state(ctx, _NEW_BUFFERS);
418 }
419
420
421 /**
422 * Called via ctx->Driver.FinishRenderTexture.
423 */
424 static void
425 st_finish_render_texture(struct gl_context *ctx,
426 struct gl_renderbuffer_attachment *att)
427 {
428 struct st_context *st = st_context(ctx);
429 struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer);
430
431 if (!strb)
432 return;
433
434 st_flush(st, PIPE_FLUSH_RENDER_CACHE, NULL);
435
436 strb->rtt = NULL;
437
438 /*
439 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
440 */
441
442 /* restore previous framebuffer state */
443 st_invalidate_state(ctx, _NEW_BUFFERS);
444 }
445
446
447 /**
448 * Validate a renderbuffer attachment for a particular set of bindings.
449 */
450 static GLboolean
451 st_validate_attachment(struct pipe_screen *screen,
452 const struct gl_renderbuffer_attachment *att,
453 unsigned bindings)
454 {
455 const struct st_texture_object *stObj = st_texture_object(att->Texture);
456
457 /* Only validate texture attachments for now, since
458 * st_renderbuffer_alloc_storage makes sure that
459 * the format is supported.
460 */
461 if (att->Type != GL_TEXTURE)
462 return GL_TRUE;
463
464 if (!stObj)
465 return GL_FALSE;
466
467 return screen->is_format_supported(screen, stObj->pt->format,
468 PIPE_TEXTURE_2D,
469 stObj->pt->nr_samples, bindings, 0);
470 }
471
472
473 /**
474 * Check if two renderbuffer attachments name a combined depth/stencil
475 * renderbuffer.
476 */
477 GLboolean
478 st_is_depth_stencil_combined(const struct gl_renderbuffer_attachment *depth,
479 const struct gl_renderbuffer_attachment *stencil)
480 {
481 assert(depth && stencil);
482
483 if (depth->Type == stencil->Type) {
484 if (depth->Type == GL_RENDERBUFFER_EXT &&
485 depth->Renderbuffer == stencil->Renderbuffer)
486 return GL_TRUE;
487
488 if (depth->Type == GL_TEXTURE &&
489 depth->Texture == stencil->Texture)
490 return GL_TRUE;
491 }
492
493 return GL_FALSE;
494 }
495
496
497 /**
498 * Check that the framebuffer configuration is valid in terms of what
499 * the driver can support.
500 *
501 * For Gallium we only supports combined Z+stencil, not separate buffers.
502 */
503 static void
504 st_validate_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
505 {
506 struct st_context *st = st_context(ctx);
507 struct pipe_screen *screen = st->pipe->screen;
508 const struct gl_renderbuffer_attachment *depth =
509 &fb->Attachment[BUFFER_DEPTH];
510 const struct gl_renderbuffer_attachment *stencil =
511 &fb->Attachment[BUFFER_STENCIL];
512 GLuint i;
513
514 if (depth->Type && stencil->Type && depth->Type != stencil->Type) {
515 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
516 return;
517 }
518 if (depth->Type == GL_RENDERBUFFER_EXT &&
519 stencil->Type == GL_RENDERBUFFER_EXT &&
520 depth->Renderbuffer != stencil->Renderbuffer) {
521 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
522 return;
523 }
524 if (depth->Type == GL_TEXTURE &&
525 stencil->Type == GL_TEXTURE &&
526 depth->Texture != stencil->Texture) {
527 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
528 return;
529 }
530
531 if (!st_validate_attachment(screen,
532 depth,
533 PIPE_BIND_DEPTH_STENCIL)) {
534 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
535 return;
536 }
537 if (!st_validate_attachment(screen,
538 stencil,
539 PIPE_BIND_DEPTH_STENCIL)) {
540 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
541 return;
542 }
543 for (i = 0; i < ctx->Const.MaxColorAttachments; i++) {
544 if (!st_validate_attachment(screen,
545 &fb->Attachment[BUFFER_COLOR0 + i],
546 PIPE_BIND_RENDER_TARGET)) {
547 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
548 return;
549 }
550 }
551 }
552
553
554 /**
555 * Called via glDrawBuffer.
556 */
557 static void
558 st_DrawBuffers(struct gl_context *ctx, GLsizei count, const GLenum *buffers)
559 {
560 struct st_context *st = st_context(ctx);
561 struct gl_framebuffer *fb = ctx->DrawBuffer;
562 GLuint i;
563
564 (void) count;
565 (void) buffers;
566
567 /* add the renderbuffers on demand */
568 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
569 gl_buffer_index idx = fb->_ColorDrawBufferIndexes[i];
570 st_manager_add_color_renderbuffer(st, fb, idx);
571 }
572 }
573
574
575 /**
576 * Called via glReadBuffer.
577 */
578 static void
579 st_ReadBuffer(struct gl_context *ctx, GLenum buffer)
580 {
581 struct st_context *st = st_context(ctx);
582 struct gl_framebuffer *fb = ctx->ReadBuffer;
583
584 (void) buffer;
585
586 /* add the renderbuffer on demand */
587 st_manager_add_color_renderbuffer(st, fb, fb->_ColorReadBufferIndex);
588 }
589
590
591 void st_init_fbo_functions(struct dd_function_table *functions)
592 {
593 #if FEATURE_EXT_framebuffer_object
594 functions->NewFramebuffer = st_new_framebuffer;
595 functions->NewRenderbuffer = st_new_renderbuffer;
596 functions->BindFramebuffer = st_bind_framebuffer;
597 functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer;
598 functions->RenderTexture = st_render_texture;
599 functions->FinishRenderTexture = st_finish_render_texture;
600 functions->ValidateFramebuffer = st_validate_framebuffer;
601 #endif
602 /* no longer needed by core Mesa, drivers handle resizes...
603 functions->ResizeBuffers = st_resize_buffers;
604 */
605
606 functions->DrawBuffers = st_DrawBuffers;
607 functions->ReadBuffer = st_ReadBuffer;
608 }
609
610 /* XXX unused ? */
611 struct pipe_sampler_view *
612 st_get_renderbuffer_sampler_view(struct st_renderbuffer *rb,
613 struct pipe_context *pipe)
614 {
615 if (!rb->sampler_view) {
616 rb->sampler_view = st_create_texture_sampler_view(pipe, rb->texture);
617 }
618
619 return rb->sampler_view;
620 }