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