st/mesa: allow rendering to sRGB textures if EXT_fb_srgb is unsupported
[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 gl_context *ctx,
452 struct pipe_screen *screen,
453 const struct gl_renderbuffer_attachment *att,
454 unsigned bindings)
455 {
456 const struct st_texture_object *stObj = st_texture_object(att->Texture);
457 enum pipe_format format;
458 gl_format texFormat;
459
460 /* Only validate texture attachments for now, since
461 * st_renderbuffer_alloc_storage makes sure that
462 * the format is supported.
463 */
464 if (att->Type != GL_TEXTURE)
465 return GL_TRUE;
466
467 if (!stObj)
468 return GL_FALSE;
469
470 format = stObj->pt->format;
471 texFormat =
472 stObj->base.Image[att->CubeMapFace][att->TextureLevel]->TexFormat;
473
474 /* If the encoding is sRGB and sRGB rendering cannot be enabled,
475 * check for linear format support instead.
476 * Later when we create a surface, we change the format to a linear one. */
477 if (!ctx->Const.sRGBCapable &&
478 _mesa_get_format_color_encoding(texFormat) == GL_SRGB) {
479 const gl_format linearFormat = _mesa_get_srgb_format_linear(texFormat);
480 format = st_mesa_format_to_pipe_format(linearFormat);
481 }
482
483 return screen->is_format_supported(screen, format,
484 PIPE_TEXTURE_2D,
485 stObj->pt->nr_samples, bindings, 0);
486 }
487
488
489 /**
490 * Check if two renderbuffer attachments name a combined depth/stencil
491 * renderbuffer.
492 */
493 GLboolean
494 st_is_depth_stencil_combined(const struct gl_renderbuffer_attachment *depth,
495 const struct gl_renderbuffer_attachment *stencil)
496 {
497 assert(depth && stencil);
498
499 if (depth->Type == stencil->Type) {
500 if (depth->Type == GL_RENDERBUFFER_EXT &&
501 depth->Renderbuffer == stencil->Renderbuffer)
502 return GL_TRUE;
503
504 if (depth->Type == GL_TEXTURE &&
505 depth->Texture == stencil->Texture)
506 return GL_TRUE;
507 }
508
509 return GL_FALSE;
510 }
511
512
513 /**
514 * Check that the framebuffer configuration is valid in terms of what
515 * the driver can support.
516 *
517 * For Gallium we only supports combined Z+stencil, not separate buffers.
518 */
519 static void
520 st_validate_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
521 {
522 struct st_context *st = st_context(ctx);
523 struct pipe_screen *screen = st->pipe->screen;
524 const struct gl_renderbuffer_attachment *depth =
525 &fb->Attachment[BUFFER_DEPTH];
526 const struct gl_renderbuffer_attachment *stencil =
527 &fb->Attachment[BUFFER_STENCIL];
528 GLuint i;
529
530 if (depth->Type && stencil->Type && depth->Type != stencil->Type) {
531 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
532 return;
533 }
534 if (depth->Type == GL_RENDERBUFFER_EXT &&
535 stencil->Type == GL_RENDERBUFFER_EXT &&
536 depth->Renderbuffer != stencil->Renderbuffer) {
537 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
538 return;
539 }
540 if (depth->Type == GL_TEXTURE &&
541 stencil->Type == GL_TEXTURE &&
542 depth->Texture != stencil->Texture) {
543 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
544 return;
545 }
546
547 if (!st_validate_attachment(ctx,
548 screen,
549 depth,
550 PIPE_BIND_DEPTH_STENCIL)) {
551 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
552 return;
553 }
554 if (!st_validate_attachment(ctx,
555 screen,
556 stencil,
557 PIPE_BIND_DEPTH_STENCIL)) {
558 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
559 return;
560 }
561 for (i = 0; i < ctx->Const.MaxColorAttachments; i++) {
562 if (!st_validate_attachment(ctx,
563 screen,
564 &fb->Attachment[BUFFER_COLOR0 + i],
565 PIPE_BIND_RENDER_TARGET)) {
566 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
567 return;
568 }
569 }
570 }
571
572
573 /**
574 * Called via glDrawBuffer.
575 */
576 static void
577 st_DrawBuffers(struct gl_context *ctx, GLsizei count, const GLenum *buffers)
578 {
579 struct st_context *st = st_context(ctx);
580 struct gl_framebuffer *fb = ctx->DrawBuffer;
581 GLuint i;
582
583 (void) count;
584 (void) buffers;
585
586 /* add the renderbuffers on demand */
587 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
588 gl_buffer_index idx = fb->_ColorDrawBufferIndexes[i];
589 st_manager_add_color_renderbuffer(st, fb, idx);
590 }
591 }
592
593
594 /**
595 * Called via glReadBuffer.
596 */
597 static void
598 st_ReadBuffer(struct gl_context *ctx, GLenum buffer)
599 {
600 struct st_context *st = st_context(ctx);
601 struct gl_framebuffer *fb = ctx->ReadBuffer;
602
603 (void) buffer;
604
605 /* add the renderbuffer on demand */
606 st_manager_add_color_renderbuffer(st, fb, fb->_ColorReadBufferIndex);
607 }
608
609
610 void st_init_fbo_functions(struct dd_function_table *functions)
611 {
612 #if FEATURE_EXT_framebuffer_object
613 functions->NewFramebuffer = st_new_framebuffer;
614 functions->NewRenderbuffer = st_new_renderbuffer;
615 functions->BindFramebuffer = st_bind_framebuffer;
616 functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer;
617 functions->RenderTexture = st_render_texture;
618 functions->FinishRenderTexture = st_finish_render_texture;
619 functions->ValidateFramebuffer = st_validate_framebuffer;
620 #endif
621 /* no longer needed by core Mesa, drivers handle resizes...
622 functions->ResizeBuffers = st_resize_buffers;
623 */
624
625 functions->DrawBuffers = st_DrawBuffers;
626 functions->ReadBuffer = st_ReadBuffer;
627 }
628
629 /* XXX unused ? */
630 struct pipe_sampler_view *
631 st_get_renderbuffer_sampler_view(struct st_renderbuffer *rb,
632 struct pipe_context *pipe)
633 {
634 if (!rb->sampler_view) {
635 rb->sampler_view = st_create_texture_sampler_view(pipe, rb->texture);
636 }
637
638 return rb->sampler_view;
639 }