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