intel: Make renderbuffer tiling choice match texture tiling choice.
[mesa.git] / src / mesa / drivers / dri / intel / intel_fbo.c
1 /**************************************************************************
2 *
3 * Copyright 2006 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 #include "main/imports.h"
30 #include "main/macros.h"
31 #include "main/mtypes.h"
32 #include "main/fbobject.h"
33 #include "main/framebuffer.h"
34 #include "main/renderbuffer.h"
35 #include "main/context.h"
36 #include "main/texrender.h"
37 #include "drivers/common/meta.h"
38
39 #include "intel_context.h"
40 #include "intel_batchbuffer.h"
41 #include "intel_buffers.h"
42 #include "intel_fbo.h"
43 #include "intel_mipmap_tree.h"
44 #include "intel_regions.h"
45 #include "intel_tex.h"
46 #include "intel_span.h"
47
48 #define FILE_DEBUG_FLAG DEBUG_FBO
49
50
51 /**
52 * Create a new framebuffer object.
53 */
54 static struct gl_framebuffer *
55 intel_new_framebuffer(struct gl_context * ctx, GLuint name)
56 {
57 /* Only drawable state in intel_framebuffer at this time, just use Mesa's
58 * class
59 */
60 return _mesa_new_framebuffer(ctx, name);
61 }
62
63
64 /** Called by gl_renderbuffer::Delete() */
65 static void
66 intel_delete_renderbuffer(struct gl_renderbuffer *rb)
67 {
68 GET_CURRENT_CONTEXT(ctx);
69 struct intel_context *intel = intel_context(ctx);
70 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
71
72 ASSERT(irb);
73
74 if (intel && irb->region) {
75 intel_region_release(&irb->region);
76 }
77
78 free(irb);
79 }
80
81
82 /**
83 * Return a pointer to a specific pixel in a renderbuffer.
84 */
85 static void *
86 intel_get_pointer(struct gl_context * ctx, struct gl_renderbuffer *rb,
87 GLint x, GLint y)
88 {
89 /* By returning NULL we force all software rendering to go through
90 * the span routines.
91 */
92 return NULL;
93 }
94
95
96 /**
97 * Called via glRenderbufferStorageEXT() to set the format and allocate
98 * storage for a user-created renderbuffer.
99 */
100 static GLboolean
101 intel_alloc_renderbuffer_storage(struct gl_context * ctx, struct gl_renderbuffer *rb,
102 GLenum internalFormat,
103 GLuint width, GLuint height)
104 {
105 struct intel_context *intel = intel_context(ctx);
106 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
107 int cpp, tiling;
108
109 ASSERT(rb->Name != 0);
110
111 switch (internalFormat) {
112 default:
113 /* Use the same format-choice logic as for textures.
114 * Renderbuffers aren't any different from textures for us,
115 * except they're less useful because you can't texture with
116 * them.
117 */
118 rb->Format = intel->ctx.Driver.ChooseTextureFormat(ctx, internalFormat,
119 GL_NONE, GL_NONE);
120 break;
121 case GL_STENCIL_INDEX:
122 case GL_STENCIL_INDEX1_EXT:
123 case GL_STENCIL_INDEX4_EXT:
124 case GL_STENCIL_INDEX8_EXT:
125 case GL_STENCIL_INDEX16_EXT:
126 /* These aren't actual texture formats, so force them here. */
127 rb->Format = MESA_FORMAT_S8_Z24;
128 break;
129 }
130
131 rb->_BaseFormat = _mesa_base_fbo_format(ctx, internalFormat);
132 rb->DataType = intel_mesa_format_to_rb_datatype(rb->Format);
133 cpp = _mesa_get_format_bytes(rb->Format);
134
135 intel_flush(ctx);
136
137 /* free old region */
138 if (irb->region) {
139 intel_region_release(&irb->region);
140 }
141
142 /* allocate new memory region/renderbuffer */
143
144 /* alloc hardware renderbuffer */
145 DBG("Allocating %d x %d Intel RBO\n", width, height);
146
147 tiling = I915_TILING_NONE;
148 if (intel->use_texture_tiling) {
149 GLenum base_format = _mesa_get_format_base_format(rb->Format);
150
151 if (intel->gen >= 4 && (base_format == GL_DEPTH_COMPONENT ||
152 base_format == GL_DEPTH_STENCIL))
153 tiling = I915_TILING_Y;
154 else
155 tiling = I915_TILING_X;
156 }
157
158 irb->region = intel_region_alloc(intel->intelScreen, tiling, cpp,
159 width, height, GL_TRUE);
160 if (!irb->region)
161 return GL_FALSE; /* out of memory? */
162
163 ASSERT(irb->region->buffer);
164
165 rb->Width = width;
166 rb->Height = height;
167
168 return GL_TRUE;
169 }
170
171
172 #if FEATURE_OES_EGL_image
173 static void
174 intel_image_target_renderbuffer_storage(struct gl_context *ctx,
175 struct gl_renderbuffer *rb,
176 void *image_handle)
177 {
178 struct intel_context *intel = intel_context(ctx);
179 struct intel_renderbuffer *irb;
180 __DRIscreen *screen;
181 __DRIimage *image;
182
183 screen = intel->intelScreen->driScrnPriv;
184 image = screen->dri2.image->lookupEGLImage(screen, image_handle,
185 screen->loaderPrivate);
186 if (image == NULL)
187 return;
188
189 irb = intel_renderbuffer(rb);
190 if (irb->region)
191 intel_region_release(&irb->region);
192 intel_region_reference(&irb->region, image->region);
193
194 rb->InternalFormat = image->internal_format;
195 rb->Width = image->region->width;
196 rb->Height = image->region->height;
197 rb->Format = image->format;
198 rb->DataType = image->data_type;
199 rb->_BaseFormat = _mesa_base_fbo_format(&intel->ctx,
200 image->internal_format);
201 }
202 #endif
203
204 /**
205 * Called for each hardware renderbuffer when a _window_ is resized.
206 * Just update fields.
207 * Not used for user-created renderbuffers!
208 */
209 static GLboolean
210 intel_alloc_window_storage(struct gl_context * ctx, struct gl_renderbuffer *rb,
211 GLenum internalFormat, GLuint width, GLuint height)
212 {
213 ASSERT(rb->Name == 0);
214 rb->Width = width;
215 rb->Height = height;
216 rb->InternalFormat = internalFormat;
217
218 return GL_TRUE;
219 }
220
221
222 static void
223 intel_resize_buffers(struct gl_context *ctx, struct gl_framebuffer *fb,
224 GLuint width, GLuint height)
225 {
226 int i;
227
228 _mesa_resize_framebuffer(ctx, fb, width, height);
229
230 fb->Initialized = GL_TRUE; /* XXX remove someday */
231
232 if (fb->Name != 0) {
233 return;
234 }
235
236
237 /* Make sure all window system renderbuffers are up to date */
238 for (i = BUFFER_FRONT_LEFT; i <= BUFFER_BACK_RIGHT; i++) {
239 struct gl_renderbuffer *rb = fb->Attachment[i].Renderbuffer;
240
241 /* only resize if size is changing */
242 if (rb && (rb->Width != width || rb->Height != height)) {
243 rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height);
244 }
245 }
246 }
247
248
249 /** Dummy function for gl_renderbuffer::AllocStorage() */
250 static GLboolean
251 intel_nop_alloc_storage(struct gl_context * ctx, struct gl_renderbuffer *rb,
252 GLenum internalFormat, GLuint width, GLuint height)
253 {
254 _mesa_problem(ctx, "intel_op_alloc_storage should never be called.");
255 return GL_FALSE;
256 }
257
258
259 void
260 intel_renderbuffer_set_region(struct intel_context *intel,
261 struct intel_renderbuffer *rb,
262 struct intel_region *region)
263 {
264 struct intel_region *old;
265
266 old = rb->region;
267 rb->region = NULL;
268 intel_region_reference(&rb->region, region);
269 intel_region_release(&old);
270 }
271
272
273 /**
274 * Create a new intel_renderbuffer which corresponds to an on-screen window,
275 * not a user-created renderbuffer.
276 */
277 struct intel_renderbuffer *
278 intel_create_renderbuffer(gl_format format)
279 {
280 GET_CURRENT_CONTEXT(ctx);
281
282 struct intel_renderbuffer *irb;
283
284 irb = CALLOC_STRUCT(intel_renderbuffer);
285 if (!irb) {
286 _mesa_error(ctx, GL_OUT_OF_MEMORY, "creating renderbuffer");
287 return NULL;
288 }
289
290 _mesa_init_renderbuffer(&irb->Base, 0);
291 irb->Base.ClassID = INTEL_RB_CLASS;
292 irb->Base._BaseFormat = _mesa_get_format_base_format(format);
293 irb->Base.Format = format;
294 irb->Base.InternalFormat = irb->Base._BaseFormat;
295 irb->Base.DataType = intel_mesa_format_to_rb_datatype(format);
296
297 /* intel-specific methods */
298 irb->Base.Delete = intel_delete_renderbuffer;
299 irb->Base.AllocStorage = intel_alloc_window_storage;
300 irb->Base.GetPointer = intel_get_pointer;
301
302 return irb;
303 }
304
305
306 /**
307 * Create a new renderbuffer object.
308 * Typically called via glBindRenderbufferEXT().
309 */
310 static struct gl_renderbuffer *
311 intel_new_renderbuffer(struct gl_context * ctx, GLuint name)
312 {
313 /*struct intel_context *intel = intel_context(ctx); */
314 struct intel_renderbuffer *irb;
315
316 irb = CALLOC_STRUCT(intel_renderbuffer);
317 if (!irb) {
318 _mesa_error(ctx, GL_OUT_OF_MEMORY, "creating renderbuffer");
319 return NULL;
320 }
321
322 _mesa_init_renderbuffer(&irb->Base, name);
323 irb->Base.ClassID = INTEL_RB_CLASS;
324
325 /* intel-specific methods */
326 irb->Base.Delete = intel_delete_renderbuffer;
327 irb->Base.AllocStorage = intel_alloc_renderbuffer_storage;
328 irb->Base.GetPointer = intel_get_pointer;
329 /* span routines set in alloc_storage function */
330
331 return &irb->Base;
332 }
333
334
335 /**
336 * Called via glBindFramebufferEXT().
337 */
338 static void
339 intel_bind_framebuffer(struct gl_context * ctx, GLenum target,
340 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
341 {
342 if (target == GL_FRAMEBUFFER_EXT || target == GL_DRAW_FRAMEBUFFER_EXT) {
343 intel_draw_buffer(ctx, fb);
344 }
345 else {
346 /* don't need to do anything if target == GL_READ_FRAMEBUFFER_EXT */
347 }
348 }
349
350
351 /**
352 * Called via glFramebufferRenderbufferEXT().
353 */
354 static void
355 intel_framebuffer_renderbuffer(struct gl_context * ctx,
356 struct gl_framebuffer *fb,
357 GLenum attachment, struct gl_renderbuffer *rb)
358 {
359 DBG("Intel FramebufferRenderbuffer %u %u\n", fb->Name, rb ? rb->Name : 0);
360
361 intel_flush(ctx);
362
363 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
364 intel_draw_buffer(ctx, fb);
365 }
366
367
368 static GLboolean
369 intel_update_wrapper(struct gl_context *ctx, struct intel_renderbuffer *irb,
370 struct gl_texture_image *texImage)
371 {
372 if (!intel_span_supports_format(texImage->TexFormat)) {
373 DBG("Render to texture BAD FORMAT %s\n",
374 _mesa_get_format_name(texImage->TexFormat));
375 return GL_FALSE;
376 } else {
377 DBG("Render to texture %s\n", _mesa_get_format_name(texImage->TexFormat));
378 }
379
380 irb->Base.Format = texImage->TexFormat;
381 irb->Base.DataType = intel_mesa_format_to_rb_datatype(texImage->TexFormat);
382 irb->Base.InternalFormat = texImage->InternalFormat;
383 irb->Base._BaseFormat = _mesa_base_fbo_format(ctx, irb->Base.InternalFormat);
384 irb->Base.Width = texImage->Width;
385 irb->Base.Height = texImage->Height;
386
387 irb->Base.Delete = intel_delete_renderbuffer;
388 irb->Base.AllocStorage = intel_nop_alloc_storage;
389
390 return GL_TRUE;
391 }
392
393
394 /**
395 * When glFramebufferTexture[123]D is called this function sets up the
396 * gl_renderbuffer wrapper around the texture image.
397 * This will have the region info needed for hardware rendering.
398 */
399 static struct intel_renderbuffer *
400 intel_wrap_texture(struct gl_context * ctx, struct gl_texture_image *texImage)
401 {
402 const GLuint name = ~0; /* not significant, but distinct for debugging */
403 struct intel_renderbuffer *irb;
404
405 /* make an intel_renderbuffer to wrap the texture image */
406 irb = CALLOC_STRUCT(intel_renderbuffer);
407 if (!irb) {
408 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture");
409 return NULL;
410 }
411
412 _mesa_init_renderbuffer(&irb->Base, name);
413 irb->Base.ClassID = INTEL_RB_CLASS;
414
415 if (!intel_update_wrapper(ctx, irb, texImage)) {
416 free(irb);
417 return NULL;
418 }
419
420 return irb;
421 }
422
423
424 /**
425 * Called by glFramebufferTexture[123]DEXT() (and other places) to
426 * prepare for rendering into texture memory. This might be called
427 * many times to choose different texture levels, cube faces, etc
428 * before intel_finish_render_texture() is ever called.
429 */
430 static void
431 intel_render_texture(struct gl_context * ctx,
432 struct gl_framebuffer *fb,
433 struct gl_renderbuffer_attachment *att)
434 {
435 struct gl_texture_image *newImage
436 = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
437 struct intel_renderbuffer *irb = intel_renderbuffer(att->Renderbuffer);
438 struct intel_texture_image *intel_image;
439 GLuint dst_x, dst_y;
440
441 (void) fb;
442
443 ASSERT(newImage);
444
445 intel_image = intel_texture_image(newImage);
446 if (!intel_image->mt) {
447 /* Fallback on drawing to a texture that doesn't have a miptree
448 * (has a border, width/height 0, etc.)
449 */
450 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
451 _mesa_render_texture(ctx, fb, att);
452 return;
453 }
454 else if (!irb) {
455 irb = intel_wrap_texture(ctx, newImage);
456 if (irb) {
457 /* bind the wrapper to the attachment point */
458 _mesa_reference_renderbuffer(&att->Renderbuffer, &irb->Base);
459 }
460 else {
461 /* fallback to software rendering */
462 _mesa_render_texture(ctx, fb, att);
463 return;
464 }
465 }
466
467 if (!intel_update_wrapper(ctx, irb, newImage)) {
468 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
469 _mesa_render_texture(ctx, fb, att);
470 return;
471 }
472
473 DBG("Begin render texture tid %lx tex=%u w=%d h=%d refcount=%d\n",
474 _glthread_GetID(),
475 att->Texture->Name, newImage->Width, newImage->Height,
476 irb->Base.RefCount);
477
478 /* point the renderbufer's region to the texture image region */
479 if (irb->region != intel_image->mt->region) {
480 if (irb->region)
481 intel_region_release(&irb->region);
482 intel_region_reference(&irb->region, intel_image->mt->region);
483 }
484
485 /* compute offset of the particular 2D image within the texture region */
486 intel_miptree_get_image_offset(intel_image->mt,
487 att->TextureLevel,
488 att->CubeMapFace,
489 att->Zoffset,
490 &dst_x, &dst_y);
491
492 intel_image->mt->region->draw_offset = (dst_y * intel_image->mt->region->pitch +
493 dst_x) * intel_image->mt->cpp;
494 intel_image->mt->region->draw_x = dst_x;
495 intel_image->mt->region->draw_y = dst_y;
496 intel_image->used_as_render_target = GL_TRUE;
497
498 /* update drawing region, etc */
499 intel_draw_buffer(ctx, fb);
500 }
501
502
503 /**
504 * Called by Mesa when rendering to a texture is done.
505 */
506 static void
507 intel_finish_render_texture(struct gl_context * ctx,
508 struct gl_renderbuffer_attachment *att)
509 {
510 struct intel_context *intel = intel_context(ctx);
511 struct gl_texture_object *tex_obj = att->Texture;
512 struct gl_texture_image *image =
513 tex_obj->Image[att->CubeMapFace][att->TextureLevel];
514 struct intel_texture_image *intel_image = intel_texture_image(image);
515
516 DBG("Finish render texture tid %lx tex=%u\n",
517 _glthread_GetID(), att->Texture->Name);
518
519 /* Flag that this image may now be validated into the object's miptree. */
520 if (intel_image)
521 intel_image->used_as_render_target = GL_FALSE;
522
523 /* Since we've (probably) rendered to the texture and will (likely) use
524 * it in the texture domain later on in this batchbuffer, flush the
525 * batch. Once again, we wish for a domain tracker in libdrm to cover
526 * usage inside of a batchbuffer like GEM does in the kernel.
527 */
528 intel_batchbuffer_emit_mi_flush(intel->batch);
529 }
530
531 /**
532 * Do additional "completeness" testing of a framebuffer object.
533 */
534 static void
535 intel_validate_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
536 {
537 struct intel_context *intel = intel_context(ctx);
538 const struct intel_renderbuffer *depthRb =
539 intel_get_renderbuffer(fb, BUFFER_DEPTH);
540 const struct intel_renderbuffer *stencilRb =
541 intel_get_renderbuffer(fb, BUFFER_STENCIL);
542 int i;
543
544 if (depthRb && stencilRb && stencilRb != depthRb) {
545 if (fb->Attachment[BUFFER_DEPTH].Type == GL_TEXTURE &&
546 fb->Attachment[BUFFER_STENCIL].Type == GL_TEXTURE &&
547 (fb->Attachment[BUFFER_DEPTH].Texture->Name ==
548 fb->Attachment[BUFFER_STENCIL].Texture->Name)) {
549 /* OK */
550 } else {
551 /* we only support combined depth/stencil buffers, not separate
552 * stencil buffers.
553 */
554 DBG("Only supports combined depth/stencil (found %s, %s)\n",
555 depthRb ? _mesa_get_format_name(depthRb->Base.Format): "NULL",
556 stencilRb ? _mesa_get_format_name(stencilRb->Base.Format): "NULL");
557 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
558 }
559 }
560
561 for (i = 0; i < Elements(fb->Attachment); i++) {
562 struct gl_renderbuffer *rb;
563 struct intel_renderbuffer *irb;
564
565 if (fb->Attachment[i].Type == GL_NONE)
566 continue;
567
568 /* A supported attachment will have a Renderbuffer set either
569 * from being a Renderbuffer or being a texture that got the
570 * intel_wrap_texture() treatment.
571 */
572 rb = fb->Attachment[i].Renderbuffer;
573 if (rb == NULL) {
574 DBG("attachment without renderbuffer\n");
575 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
576 continue;
577 }
578
579 irb = intel_renderbuffer(rb);
580 if (irb == NULL) {
581 DBG("software rendering renderbuffer\n");
582 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
583 continue;
584 }
585
586 if (!intel_span_supports_format(irb->Base.Format) ||
587 !intel->vtbl.render_target_supported(irb->Base.Format)) {
588 DBG("Unsupported texture/renderbuffer format attached: %s\n",
589 _mesa_get_format_name(irb->Base.Format));
590 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
591 }
592 }
593 }
594
595
596 /**
597 * Do one-time context initializations related to GL_EXT_framebuffer_object.
598 * Hook in device driver functions.
599 */
600 void
601 intel_fbo_init(struct intel_context *intel)
602 {
603 intel->ctx.Driver.NewFramebuffer = intel_new_framebuffer;
604 intel->ctx.Driver.NewRenderbuffer = intel_new_renderbuffer;
605 intel->ctx.Driver.BindFramebuffer = intel_bind_framebuffer;
606 intel->ctx.Driver.FramebufferRenderbuffer = intel_framebuffer_renderbuffer;
607 intel->ctx.Driver.RenderTexture = intel_render_texture;
608 intel->ctx.Driver.FinishRenderTexture = intel_finish_render_texture;
609 intel->ctx.Driver.ResizeBuffers = intel_resize_buffers;
610 intel->ctx.Driver.ValidateFramebuffer = intel_validate_framebuffer;
611 intel->ctx.Driver.BlitFramebuffer = _mesa_meta_BlitFramebuffer;
612
613 #if FEATURE_OES_EGL_image
614 intel->ctx.Driver.EGLImageTargetRenderbufferStorage =
615 intel_image_target_renderbuffer_storage;
616 #endif
617 }