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