98247f208b7725a926062b811c93508459e11045
[mesa.git] / src / mesa / drivers / dri / i965 / intel_fbo.c
1 /**************************************************************************
2 *
3 * Copyright 2006 VMware, Inc.
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 VMWARE 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/enums.h"
30 #include "main/imports.h"
31 #include "main/macros.h"
32 #include "main/mtypes.h"
33 #include "main/fbobject.h"
34 #include "main/framebuffer.h"
35 #include "main/renderbuffer.h"
36 #include "main/context.h"
37 #include "main/teximage.h"
38 #include "main/image.h"
39 #include "main/hash_table.h"
40 #include "main/set.h"
41
42 #include "swrast/swrast.h"
43 #include "drivers/common/meta.h"
44
45 #include "intel_batchbuffer.h"
46 #include "intel_buffers.h"
47 #include "intel_blit.h"
48 #include "intel_fbo.h"
49 #include "intel_mipmap_tree.h"
50 #include "intel_regions.h"
51 #include "intel_screen.h"
52 #include "intel_tex.h"
53 #include "brw_context.h"
54
55 #define FILE_DEBUG_FLAG DEBUG_FBO
56
57 /**
58 * Create a new framebuffer object.
59 */
60 static struct gl_framebuffer *
61 intel_new_framebuffer(struct gl_context * ctx, GLuint name)
62 {
63 /* Only drawable state in intel_framebuffer at this time, just use Mesa's
64 * class
65 */
66 return _mesa_new_framebuffer(ctx, name);
67 }
68
69
70 /** Called by gl_renderbuffer::Delete() */
71 static void
72 intel_delete_renderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb)
73 {
74 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
75
76 ASSERT(irb);
77
78 intel_miptree_release(&irb->mt);
79 intel_miptree_release(&irb->singlesample_mt);
80
81 _mesa_delete_renderbuffer(ctx, rb);
82 }
83
84 /**
85 * \brief Downsample a winsys renderbuffer from mt to singlesample_mt.
86 *
87 * If the miptree needs no downsample, then skip.
88 */
89 void
90 intel_renderbuffer_downsample(struct brw_context *brw,
91 struct intel_renderbuffer *irb)
92 {
93 if (!irb->need_downsample)
94 return;
95 intel_miptree_updownsample(brw, irb->mt, irb->singlesample_mt);
96 irb->need_downsample = false;
97 }
98
99 /**
100 * \brief Upsample a winsys renderbuffer from singlesample_mt to mt.
101 *
102 * The upsample is done unconditionally.
103 */
104 void
105 intel_renderbuffer_upsample(struct brw_context *brw,
106 struct intel_renderbuffer *irb)
107 {
108 assert(!irb->need_downsample);
109
110 intel_miptree_updownsample(brw, irb->singlesample_mt, irb->mt);
111 }
112
113 /**
114 * \see dd_function_table::MapRenderbuffer
115 */
116 static void
117 intel_map_renderbuffer(struct gl_context *ctx,
118 struct gl_renderbuffer *rb,
119 GLuint x, GLuint y, GLuint w, GLuint h,
120 GLbitfield mode,
121 GLubyte **out_map,
122 GLint *out_stride)
123 {
124 struct brw_context *brw = brw_context(ctx);
125 struct swrast_renderbuffer *srb = (struct swrast_renderbuffer *)rb;
126 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
127 struct intel_mipmap_tree *mt;
128 void *map;
129 int stride;
130
131 if (srb->Buffer) {
132 /* this is a malloc'd renderbuffer (accum buffer), not an irb */
133 GLint bpp = _mesa_get_format_bytes(rb->Format);
134 GLint rowStride = srb->RowStride;
135 *out_map = (GLubyte *) srb->Buffer + y * rowStride + x * bpp;
136 *out_stride = rowStride;
137 return;
138 }
139
140 intel_prepare_render(brw);
141
142 /* The MapRenderbuffer API should always return a single-sampled mapping.
143 * The case we are asked to map multisampled RBs is in glReadPixels() (or
144 * swrast paths like glCopyTexImage()) from a window-system MSAA buffer,
145 * and GL expects an automatic resolve to happen.
146 *
147 * If it's a color miptree, there is a ->singlesample_mt which wraps the
148 * actual window system renderbuffer (which we may resolve to at any time),
149 * while the miptree itself is our driver-private allocation. If it's a
150 * depth or stencil miptree, we have a private MSAA buffer and no shared
151 * singlesample buffer, and since we don't expect anybody to ever actually
152 * resolve it, we just make a temporary singlesample buffer now when we
153 * have to.
154 */
155 if (rb->NumSamples > 1) {
156 if (!irb->singlesample_mt) {
157 irb->singlesample_mt =
158 intel_miptree_create_for_renderbuffer(brw, irb->mt->format,
159 rb->Width, rb->Height,
160 0 /*num_samples*/);
161 if (!irb->singlesample_mt)
162 goto fail;
163 irb->singlesample_mt_is_tmp = true;
164 irb->need_downsample = true;
165 }
166
167 intel_renderbuffer_downsample(brw, irb);
168 mt = irb->singlesample_mt;
169
170 irb->need_map_upsample = mode & GL_MAP_WRITE_BIT;
171 } else {
172 mt = irb->mt;
173 }
174
175 /* For a window-system renderbuffer, we need to flip the mapping we receive
176 * upside-down. So we need to ask for a rectangle on flipped vertically, and
177 * we then return a pointer to the bottom of it with a negative stride.
178 */
179 if (rb->Name == 0) {
180 y = rb->Height - y - h;
181 }
182
183 intel_miptree_map(brw, mt, irb->mt_level, irb->mt_layer,
184 x, y, w, h, mode, &map, &stride);
185
186 if (rb->Name == 0) {
187 map += (h - 1) * stride;
188 stride = -stride;
189 }
190
191 DBG("%s: rb %d (%s) mt mapped: (%d, %d) (%dx%d) -> %p/%d\n",
192 __FUNCTION__, rb->Name, _mesa_get_format_name(rb->Format),
193 x, y, w, h, map, stride);
194
195 *out_map = map;
196 *out_stride = stride;
197 return;
198
199 fail:
200 *out_map = NULL;
201 *out_stride = 0;
202 }
203
204 /**
205 * \see dd_function_table::UnmapRenderbuffer
206 */
207 static void
208 intel_unmap_renderbuffer(struct gl_context *ctx,
209 struct gl_renderbuffer *rb)
210 {
211 struct brw_context *brw = brw_context(ctx);
212 struct swrast_renderbuffer *srb = (struct swrast_renderbuffer *)rb;
213 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
214 struct intel_mipmap_tree *mt;
215
216 DBG("%s: rb %d (%s)\n", __FUNCTION__,
217 rb->Name, _mesa_get_format_name(rb->Format));
218
219 if (srb->Buffer) {
220 /* this is a malloc'd renderbuffer (accum buffer) */
221 /* nothing to do */
222 return;
223 }
224
225 if (rb->NumSamples > 1) {
226 mt = irb->singlesample_mt;
227 } else {
228 mt = irb->mt;
229 }
230
231 intel_miptree_unmap(brw, mt, irb->mt_level, irb->mt_layer);
232
233 if (irb->need_map_upsample) {
234 intel_renderbuffer_upsample(brw, irb);
235 irb->need_map_upsample = false;
236 }
237
238 if (irb->singlesample_mt_is_tmp)
239 intel_miptree_release(&irb->singlesample_mt);
240 }
241
242
243 /**
244 * Round up the requested multisample count to the next supported sample size.
245 */
246 unsigned
247 intel_quantize_num_samples(struct intel_screen *intel, unsigned num_samples)
248 {
249 const int *msaa_modes = intel_supported_msaa_modes(intel);
250 int quantized_samples = 0;
251
252 for (int i = 0; msaa_modes[i] != -1; ++i) {
253 if (msaa_modes[i] >= num_samples)
254 quantized_samples = msaa_modes[i];
255 else
256 break;
257 }
258
259 return quantized_samples;
260 }
261
262
263 /**
264 * Called via glRenderbufferStorageEXT() to set the format and allocate
265 * storage for a user-created renderbuffer.
266 */
267 static GLboolean
268 intel_alloc_renderbuffer_storage(struct gl_context * ctx, struct gl_renderbuffer *rb,
269 GLenum internalFormat,
270 GLuint width, GLuint height)
271 {
272 struct brw_context *brw = brw_context(ctx);
273 struct intel_screen *screen = brw->intelScreen;
274 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
275 rb->NumSamples = intel_quantize_num_samples(screen, rb->NumSamples);
276
277 switch (internalFormat) {
278 default:
279 /* Use the same format-choice logic as for textures.
280 * Renderbuffers aren't any different from textures for us,
281 * except they're less useful because you can't texture with
282 * them.
283 */
284 rb->Format = ctx->Driver.ChooseTextureFormat(ctx, GL_TEXTURE_2D,
285 internalFormat,
286 GL_NONE, GL_NONE);
287 break;
288 case GL_STENCIL_INDEX:
289 case GL_STENCIL_INDEX1_EXT:
290 case GL_STENCIL_INDEX4_EXT:
291 case GL_STENCIL_INDEX8_EXT:
292 case GL_STENCIL_INDEX16_EXT:
293 /* These aren't actual texture formats, so force them here. */
294 if (brw->has_separate_stencil) {
295 rb->Format = MESA_FORMAT_S_UINT8;
296 } else {
297 assert(!brw->must_use_separate_stencil);
298 rb->Format = MESA_FORMAT_Z24_UNORM_S8_UINT;
299 }
300 break;
301 }
302
303 rb->Width = width;
304 rb->Height = height;
305 rb->_BaseFormat = _mesa_base_fbo_format(ctx, internalFormat);
306
307 intel_miptree_release(&irb->mt);
308
309 DBG("%s: %s: %s (%dx%d)\n", __FUNCTION__,
310 _mesa_lookup_enum_by_nr(internalFormat),
311 _mesa_get_format_name(rb->Format), width, height);
312
313 if (width == 0 || height == 0)
314 return true;
315
316 irb->mt = intel_miptree_create_for_renderbuffer(brw, rb->Format,
317 width, height,
318 rb->NumSamples);
319 if (!irb->mt)
320 return false;
321
322 return true;
323 }
324
325
326 static void
327 intel_image_target_renderbuffer_storage(struct gl_context *ctx,
328 struct gl_renderbuffer *rb,
329 void *image_handle)
330 {
331 struct brw_context *brw = brw_context(ctx);
332 struct intel_renderbuffer *irb;
333 __DRIscreen *screen;
334 __DRIimage *image;
335
336 screen = brw->intelScreen->driScrnPriv;
337 image = screen->dri2.image->lookupEGLImage(screen, image_handle,
338 screen->loaderPrivate);
339 if (image == NULL)
340 return;
341
342 if (image->planar_format && image->planar_format->nplanes > 1) {
343 _mesa_error(ctx, GL_INVALID_OPERATION,
344 "glEGLImageTargetRenderbufferStorage(planar buffers are not "
345 "supported as render targets.");
346 return;
347 }
348
349 /* Buffers originating from outside are for read-only. */
350 if (image->dma_buf_imported) {
351 _mesa_error(ctx, GL_INVALID_OPERATION,
352 "glEGLImageTargetRenderbufferStorage(dma buffers are read-only)");
353 return;
354 }
355
356 /* __DRIimage is opaque to the core so it has to be checked here */
357 switch (image->format) {
358 case MESA_FORMAT_R8G8B8A8_UNORM:
359 _mesa_error(ctx, GL_INVALID_OPERATION,
360 "glEGLImageTargetRenderbufferStorage(unsupported image format");
361 return;
362 break;
363 default:
364 break;
365 }
366
367 irb = intel_renderbuffer(rb);
368 intel_miptree_release(&irb->mt);
369 irb->mt = intel_miptree_create_for_bo(brw,
370 image->region->bo,
371 image->format,
372 image->offset,
373 image->region->width,
374 image->region->height,
375 image->region->pitch,
376 image->region->tiling);
377 if (!irb->mt)
378 return;
379
380 rb->InternalFormat = image->internal_format;
381 rb->Width = image->region->width;
382 rb->Height = image->region->height;
383 rb->Format = image->format;
384 rb->_BaseFormat = _mesa_base_fbo_format(ctx, image->internal_format);
385 rb->NeedsFinishRenderTexture = true;
386 }
387
388 /**
389 * Called by _mesa_resize_framebuffer() for each hardware renderbuffer when a
390 * window system framebuffer is resized.
391 *
392 * Any actual buffer reallocations for hardware renderbuffers (which would
393 * have triggered _mesa_resize_framebuffer()) were done by
394 * intel_process_dri2_buffer().
395 */
396 static GLboolean
397 intel_alloc_window_storage(struct gl_context * ctx, struct gl_renderbuffer *rb,
398 GLenum internalFormat, GLuint width, GLuint height)
399 {
400 ASSERT(rb->Name == 0);
401 rb->Width = width;
402 rb->Height = height;
403 rb->InternalFormat = internalFormat;
404
405 return true;
406 }
407
408 /** Dummy function for gl_renderbuffer::AllocStorage() */
409 static GLboolean
410 intel_nop_alloc_storage(struct gl_context * ctx, struct gl_renderbuffer *rb,
411 GLenum internalFormat, GLuint width, GLuint height)
412 {
413 _mesa_problem(ctx, "intel_op_alloc_storage should never be called.");
414 return false;
415 }
416
417 /**
418 * Create a new intel_renderbuffer which corresponds to an on-screen window,
419 * not a user-created renderbuffer.
420 *
421 * \param num_samples must be quantized.
422 */
423 struct intel_renderbuffer *
424 intel_create_renderbuffer(mesa_format format, unsigned num_samples)
425 {
426 struct intel_renderbuffer *irb;
427 struct gl_renderbuffer *rb;
428
429 GET_CURRENT_CONTEXT(ctx);
430
431 irb = CALLOC_STRUCT(intel_renderbuffer);
432 if (!irb) {
433 _mesa_error(ctx, GL_OUT_OF_MEMORY, "creating renderbuffer");
434 return NULL;
435 }
436
437 rb = &irb->Base.Base;
438
439 _mesa_init_renderbuffer(rb, 0);
440 rb->ClassID = INTEL_RB_CLASS;
441 rb->_BaseFormat = _mesa_get_format_base_format(format);
442 rb->Format = format;
443 rb->InternalFormat = rb->_BaseFormat;
444 rb->NumSamples = num_samples;
445
446 /* intel-specific methods */
447 rb->Delete = intel_delete_renderbuffer;
448 rb->AllocStorage = intel_alloc_window_storage;
449
450 return irb;
451 }
452
453 /**
454 * Private window-system buffers (as opposed to ones shared with the display
455 * server created with intel_create_renderbuffer()) are most similar in their
456 * handling to user-created renderbuffers, but they have a resize handler that
457 * may be called at intel_update_renderbuffers() time.
458 *
459 * \param num_samples must be quantized.
460 */
461 struct intel_renderbuffer *
462 intel_create_private_renderbuffer(mesa_format format, unsigned num_samples)
463 {
464 struct intel_renderbuffer *irb;
465
466 irb = intel_create_renderbuffer(format, num_samples);
467 irb->Base.Base.AllocStorage = intel_alloc_renderbuffer_storage;
468
469 return irb;
470 }
471
472 /**
473 * Create a new renderbuffer object.
474 * Typically called via glBindRenderbufferEXT().
475 */
476 static struct gl_renderbuffer *
477 intel_new_renderbuffer(struct gl_context * ctx, GLuint name)
478 {
479 struct intel_renderbuffer *irb;
480 struct gl_renderbuffer *rb;
481
482 irb = CALLOC_STRUCT(intel_renderbuffer);
483 if (!irb) {
484 _mesa_error(ctx, GL_OUT_OF_MEMORY, "creating renderbuffer");
485 return NULL;
486 }
487
488 rb = &irb->Base.Base;
489
490 _mesa_init_renderbuffer(rb, name);
491 rb->ClassID = INTEL_RB_CLASS;
492
493 /* intel-specific methods */
494 rb->Delete = intel_delete_renderbuffer;
495 rb->AllocStorage = intel_alloc_renderbuffer_storage;
496 /* span routines set in alloc_storage function */
497
498 return rb;
499 }
500
501 static bool
502 intel_renderbuffer_update_wrapper(struct brw_context *brw,
503 struct intel_renderbuffer *irb,
504 struct gl_texture_image *image,
505 uint32_t layer)
506 {
507 struct gl_renderbuffer *rb = &irb->Base.Base;
508 struct intel_texture_image *intel_image = intel_texture_image(image);
509 struct intel_mipmap_tree *mt = intel_image->mt;
510 int level = image->Level;
511
512 rb->AllocStorage = intel_nop_alloc_storage;
513
514 /* adjust for texture view parameters */
515 layer += image->TexObject->MinLayer;
516 level += image->TexObject->MinLevel;
517
518 intel_miptree_check_level_layer(mt, level, layer);
519 irb->mt_level = level;
520
521 switch (mt->msaa_layout) {
522 case INTEL_MSAA_LAYOUT_UMS:
523 case INTEL_MSAA_LAYOUT_CMS:
524 irb->mt_layer = layer * mt->num_samples;
525 break;
526
527 default:
528 irb->mt_layer = layer;
529 }
530
531 intel_miptree_reference(&irb->mt, mt);
532
533 intel_renderbuffer_set_draw_offset(irb);
534
535 if (mt->hiz_mt == NULL && brw_is_hiz_depth_format(brw, rb->Format)) {
536 intel_miptree_alloc_hiz(brw, mt);
537 if (!mt->hiz_mt)
538 return false;
539 }
540
541 return true;
542 }
543
544 void
545 intel_renderbuffer_set_draw_offset(struct intel_renderbuffer *irb)
546 {
547 unsigned int dst_x, dst_y;
548
549 /* compute offset of the particular 2D image within the texture region */
550 intel_miptree_get_image_offset(irb->mt,
551 irb->mt_level,
552 irb->mt_layer,
553 &dst_x, &dst_y);
554
555 irb->draw_x = dst_x;
556 irb->draw_y = dst_y;
557 }
558
559 /**
560 * Called by glFramebufferTexture[123]DEXT() (and other places) to
561 * prepare for rendering into texture memory. This might be called
562 * many times to choose different texture levels, cube faces, etc
563 * before intel_finish_render_texture() is ever called.
564 */
565 static void
566 intel_render_texture(struct gl_context * ctx,
567 struct gl_framebuffer *fb,
568 struct gl_renderbuffer_attachment *att)
569 {
570 struct brw_context *brw = brw_context(ctx);
571 struct gl_renderbuffer *rb = att->Renderbuffer;
572 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
573 struct gl_texture_image *image = rb->TexImage;
574 struct intel_texture_image *intel_image = intel_texture_image(image);
575 struct intel_mipmap_tree *mt = intel_image->mt;
576 int layer;
577
578 (void) fb;
579
580 if (att->CubeMapFace > 0) {
581 assert(att->Zoffset == 0);
582 layer = att->CubeMapFace;
583 } else {
584 layer = att->Zoffset;
585 }
586
587 if (!intel_image->mt) {
588 /* Fallback on drawing to a texture that doesn't have a miptree
589 * (has a border, width/height 0, etc.)
590 */
591 _swrast_render_texture(ctx, fb, att);
592 return;
593 }
594
595 intel_miptree_check_level_layer(mt, att->TextureLevel, layer);
596
597 if (!intel_renderbuffer_update_wrapper(brw, irb, image, layer)) {
598 _swrast_render_texture(ctx, fb, att);
599 return;
600 }
601
602 DBG("Begin render %s texture tex=%u w=%d h=%d d=%d refcount=%d\n",
603 _mesa_get_format_name(image->TexFormat),
604 att->Texture->Name, image->Width, image->Height, image->Depth,
605 rb->RefCount);
606 }
607
608
609 #define fbo_incomplete(fb, ...) do { \
610 static GLuint msg_id = 0; \
611 if (unlikely(ctx->Const.ContextFlags & GL_CONTEXT_FLAG_DEBUG_BIT)) { \
612 _mesa_gl_debug(ctx, &msg_id, \
613 MESA_DEBUG_TYPE_OTHER, \
614 MESA_DEBUG_SEVERITY_MEDIUM, \
615 __VA_ARGS__); \
616 } \
617 DBG(__VA_ARGS__); \
618 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED; \
619 } while (0)
620
621 /**
622 * Do additional "completeness" testing of a framebuffer object.
623 */
624 static void
625 intel_validate_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
626 {
627 struct brw_context *brw = brw_context(ctx);
628 struct intel_renderbuffer *depthRb =
629 intel_get_renderbuffer(fb, BUFFER_DEPTH);
630 struct intel_renderbuffer *stencilRb =
631 intel_get_renderbuffer(fb, BUFFER_STENCIL);
632 struct intel_mipmap_tree *depth_mt = NULL, *stencil_mt = NULL;
633 int i;
634
635 DBG("%s() on fb %p (%s)\n", __FUNCTION__,
636 fb, (fb == ctx->DrawBuffer ? "drawbuffer" :
637 (fb == ctx->ReadBuffer ? "readbuffer" : "other buffer")));
638
639 if (depthRb)
640 depth_mt = depthRb->mt;
641 if (stencilRb) {
642 stencil_mt = stencilRb->mt;
643 if (stencil_mt->stencil_mt)
644 stencil_mt = stencil_mt->stencil_mt;
645 }
646
647 if (depth_mt && stencil_mt) {
648 if (brw->gen >= 7) {
649 /* For gen >= 7, we are using the lod/minimum-array-element fields
650 * and supportting layered rendering. This means that we must restrict
651 * the depth & stencil attachments to match in various more retrictive
652 * ways. (width, height, depth, LOD and layer)
653 */
654 if (depth_mt->physical_width0 != stencil_mt->physical_width0 ||
655 depth_mt->physical_height0 != stencil_mt->physical_height0 ||
656 depth_mt->physical_depth0 != stencil_mt->physical_depth0 ||
657 depthRb->mt_level != stencilRb->mt_level ||
658 depthRb->mt_layer != stencilRb->mt_layer) {
659 fbo_incomplete(fb,
660 "FBO incomplete: depth and stencil must match in"
661 "width, height, depth, LOD and layer\n");
662 }
663 }
664 if (depth_mt == stencil_mt) {
665 /* For true packed depth/stencil (not faked on prefers-separate-stencil
666 * hardware) we need to be sure they're the same level/layer, since
667 * we'll be emitting a single packet describing the packed setup.
668 */
669 if (depthRb->mt_level != stencilRb->mt_level ||
670 depthRb->mt_layer != stencilRb->mt_layer) {
671 fbo_incomplete(fb,
672 "FBO incomplete: depth image level/layer %d/%d != "
673 "stencil image %d/%d\n",
674 depthRb->mt_level,
675 depthRb->mt_layer,
676 stencilRb->mt_level,
677 stencilRb->mt_layer);
678 }
679 } else {
680 if (!brw->has_separate_stencil) {
681 fbo_incomplete(fb, "FBO incomplete: separate stencil "
682 "unsupported\n");
683 }
684 if (stencil_mt->format != MESA_FORMAT_S_UINT8) {
685 fbo_incomplete(fb, "FBO incomplete: separate stencil is %s "
686 "instead of S8\n",
687 _mesa_get_format_name(stencil_mt->format));
688 }
689 if (brw->gen < 7 && !intel_renderbuffer_has_hiz(depthRb)) {
690 /* Before Gen7, separate depth and stencil buffers can be used
691 * only if HiZ is enabled. From the Sandybridge PRM, Volume 2,
692 * Part 1, Bit 3DSTATE_DEPTH_BUFFER.SeparateStencilBufferEnable:
693 * [DevSNB]: This field must be set to the same value (enabled
694 * or disabled) as Hierarchical Depth Buffer Enable.
695 */
696 fbo_incomplete(fb, "FBO incomplete: separate stencil "
697 "without HiZ\n");
698 }
699 }
700 }
701
702 for (i = 0; i < Elements(fb->Attachment); i++) {
703 struct gl_renderbuffer *rb;
704 struct intel_renderbuffer *irb;
705
706 if (fb->Attachment[i].Type == GL_NONE)
707 continue;
708
709 /* A supported attachment will have a Renderbuffer set either
710 * from being a Renderbuffer or being a texture that got the
711 * intel_wrap_texture() treatment.
712 */
713 rb = fb->Attachment[i].Renderbuffer;
714 if (rb == NULL) {
715 fbo_incomplete(fb, "FBO incomplete: attachment without "
716 "renderbuffer\n");
717 continue;
718 }
719
720 if (fb->Attachment[i].Type == GL_TEXTURE) {
721 if (rb->TexImage->Border) {
722 fbo_incomplete(fb, "FBO incomplete: texture with border\n");
723 continue;
724 }
725 }
726
727 irb = intel_renderbuffer(rb);
728 if (irb == NULL) {
729 fbo_incomplete(fb, "FBO incomplete: software rendering "
730 "renderbuffer\n");
731 continue;
732 }
733
734 if (!brw_render_target_supported(brw, rb)) {
735 fbo_incomplete(fb, "FBO incomplete: Unsupported HW "
736 "texture/renderbuffer format attached: %s\n",
737 _mesa_get_format_name(intel_rb_format(irb)));
738 }
739 }
740 }
741
742 /**
743 * Try to do a glBlitFramebuffer using glCopyTexSubImage2D
744 * We can do this when the dst renderbuffer is actually a texture and
745 * there is no scaling, mirroring or scissoring.
746 *
747 * \return new buffer mask indicating the buffers left to blit using the
748 * normal path.
749 */
750 static GLbitfield
751 intel_blit_framebuffer_with_blitter(struct gl_context *ctx,
752 GLint srcX0, GLint srcY0,
753 GLint srcX1, GLint srcY1,
754 GLint dstX0, GLint dstY0,
755 GLint dstX1, GLint dstY1,
756 GLbitfield mask, GLenum filter)
757 {
758 struct brw_context *brw = brw_context(ctx);
759
760 /* Sync up the state of window system buffers. We need to do this before
761 * we go looking for the buffers.
762 */
763 intel_prepare_render(brw);
764
765 if (mask & GL_COLOR_BUFFER_BIT) {
766 GLint i;
767 const struct gl_framebuffer *drawFb = ctx->DrawBuffer;
768 const struct gl_framebuffer *readFb = ctx->ReadBuffer;
769 struct gl_renderbuffer *src_rb = readFb->_ColorReadBuffer;
770 struct intel_renderbuffer *src_irb = intel_renderbuffer(src_rb);
771
772 if (!src_irb) {
773 perf_debug("glBlitFramebuffer(): missing src renderbuffer. "
774 "Falling back to software rendering.\n");
775 return mask;
776 }
777
778 /* If the source and destination are the same size with no mirroring,
779 * the rectangles are within the size of the texture and there is no
780 * scissor, then we can probably use the blit engine.
781 */
782 if (!(srcX0 - srcX1 == dstX0 - dstX1 &&
783 srcY0 - srcY1 == dstY0 - dstY1 &&
784 srcX1 >= srcX0 &&
785 srcY1 >= srcY0 &&
786 srcX0 >= 0 && srcX1 <= readFb->Width &&
787 srcY0 >= 0 && srcY1 <= readFb->Height &&
788 dstX0 >= 0 && dstX1 <= drawFb->Width &&
789 dstY0 >= 0 && dstY1 <= drawFb->Height &&
790 !(ctx->Scissor.EnableFlags))) {
791 perf_debug("glBlitFramebuffer(): non-1:1 blit. "
792 "Falling back to software rendering.\n");
793 return mask;
794 }
795
796 /* Blit to all active draw buffers. We don't do any pre-checking,
797 * because we assume that copying to MRTs is rare, and failure midway
798 * through copying is even more rare. Even if it was to occur, it's
799 * safe to let meta start the copy over from scratch, because
800 * glBlitFramebuffer completely overwrites the destination pixels, and
801 * results are undefined if any destination pixels have a dependency on
802 * source pixels.
803 */
804 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
805 struct gl_renderbuffer *dst_rb = ctx->DrawBuffer->_ColorDrawBuffers[i];
806 struct intel_renderbuffer *dst_irb = intel_renderbuffer(dst_rb);
807
808 if (!dst_irb) {
809 perf_debug("glBlitFramebuffer(): missing dst renderbuffer. "
810 "Falling back to software rendering.\n");
811 return mask;
812 }
813
814 if (!intel_miptree_blit(brw,
815 src_irb->mt,
816 src_irb->mt_level, src_irb->mt_layer,
817 srcX0, srcY0, src_rb->Name == 0,
818 dst_irb->mt,
819 dst_irb->mt_level, dst_irb->mt_layer,
820 dstX0, dstY0, dst_rb->Name == 0,
821 dstX1 - dstX0, dstY1 - dstY0, GL_COPY)) {
822 perf_debug("glBlitFramebuffer(): unknown blit failure. "
823 "Falling back to software rendering.\n");
824 return mask;
825 }
826 }
827
828 mask &= ~GL_COLOR_BUFFER_BIT;
829 }
830
831 return mask;
832 }
833
834 static void
835 intel_blit_framebuffer(struct gl_context *ctx,
836 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
837 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
838 GLbitfield mask, GLenum filter)
839 {
840 mask = brw_blorp_framebuffer(brw_context(ctx),
841 srcX0, srcY0, srcX1, srcY1,
842 dstX0, dstY0, dstX1, dstY1,
843 mask, filter);
844 if (mask == 0x0)
845 return;
846
847 /* Try using the BLT engine. */
848 mask = intel_blit_framebuffer_with_blitter(ctx,
849 srcX0, srcY0, srcX1, srcY1,
850 dstX0, dstY0, dstX1, dstY1,
851 mask, filter);
852 if (mask == 0x0)
853 return;
854
855
856 _mesa_meta_BlitFramebuffer(ctx,
857 srcX0, srcY0, srcX1, srcY1,
858 dstX0, dstY0, dstX1, dstY1,
859 mask, filter);
860 }
861
862 /**
863 * Does the renderbuffer have hiz enabled?
864 */
865 bool
866 intel_renderbuffer_has_hiz(struct intel_renderbuffer *irb)
867 {
868 return intel_miptree_slice_has_hiz(irb->mt, irb->mt_level, irb->mt_layer);
869 }
870
871 bool
872 intel_renderbuffer_resolve_hiz(struct brw_context *brw,
873 struct intel_renderbuffer *irb)
874 {
875 if (irb->mt)
876 return intel_miptree_slice_resolve_hiz(brw,
877 irb->mt,
878 irb->mt_level,
879 irb->mt_layer);
880
881 return false;
882 }
883
884 void
885 intel_renderbuffer_att_set_needs_depth_resolve(struct gl_renderbuffer_attachment *att)
886 {
887 struct intel_renderbuffer *irb = intel_renderbuffer(att->Renderbuffer);
888 if (irb->mt) {
889 if (att->Layered) {
890 intel_miptree_set_all_slices_need_depth_resolve(irb->mt, irb->mt_level);
891 } else {
892 intel_miptree_slice_set_needs_depth_resolve(irb->mt,
893 irb->mt_level,
894 irb->mt_layer);
895 }
896 }
897 }
898
899 bool
900 intel_renderbuffer_resolve_depth(struct brw_context *brw,
901 struct intel_renderbuffer *irb)
902 {
903 if (irb->mt)
904 return intel_miptree_slice_resolve_depth(brw,
905 irb->mt,
906 irb->mt_level,
907 irb->mt_layer);
908
909 return false;
910 }
911
912 void
913 intel_renderbuffer_move_to_temp(struct brw_context *brw,
914 struct intel_renderbuffer *irb,
915 bool invalidate)
916 {
917 struct gl_renderbuffer *rb =&irb->Base.Base;
918 struct intel_texture_image *intel_image = intel_texture_image(rb->TexImage);
919 struct intel_mipmap_tree *new_mt;
920 int width, height, depth;
921
922 intel_miptree_get_dimensions_for_image(rb->TexImage, &width, &height, &depth);
923
924 new_mt = intel_miptree_create(brw, rb->TexImage->TexObject->Target,
925 intel_image->base.Base.TexFormat,
926 intel_image->base.Base.Level,
927 intel_image->base.Base.Level,
928 width, height, depth,
929 true,
930 irb->mt->num_samples,
931 INTEL_MIPTREE_TILING_ANY);
932
933 if (brw_is_hiz_depth_format(brw, new_mt->format)) {
934 intel_miptree_alloc_hiz(brw, new_mt);
935 }
936
937 intel_miptree_copy_teximage(brw, intel_image, new_mt, invalidate);
938
939 intel_miptree_reference(&irb->mt, intel_image->mt);
940 intel_renderbuffer_set_draw_offset(irb);
941 intel_miptree_release(&new_mt);
942 }
943
944 void
945 brw_render_cache_set_clear(struct brw_context *brw)
946 {
947 struct set_entry *entry;
948
949 set_foreach(brw->render_cache, entry) {
950 _mesa_set_remove(brw->render_cache, entry);
951 }
952 }
953
954 void
955 brw_render_cache_set_add_bo(struct brw_context *brw, drm_intel_bo *bo)
956 {
957 _mesa_set_add(brw->render_cache, _mesa_hash_pointer(bo), bo);
958 }
959
960 /**
961 * Emits an appropriate flush for a BO if it has been rendered to within the
962 * same batchbuffer as a read that's about to be emitted.
963 *
964 * The GPU has separate, incoherent caches for the render cache and the
965 * sampler cache, along with other caches. Usually data in the different
966 * caches don't interact (e.g. we don't render to our driver-generated
967 * immediate constant data), but for render-to-texture in FBOs we definitely
968 * do. When a batchbuffer is flushed, the kernel will ensure that everything
969 * necessary is flushed before another use of that BO, but for reuse from
970 * different caches within a batchbuffer, it's all our responsibility.
971 */
972 void
973 brw_render_cache_set_check_flush(struct brw_context *brw, drm_intel_bo *bo)
974 {
975 if (!_mesa_set_search(brw->render_cache, _mesa_hash_pointer(bo), bo))
976 return;
977
978 intel_batchbuffer_emit_mi_flush(brw);
979 }
980
981 /**
982 * Do one-time context initializations related to GL_EXT_framebuffer_object.
983 * Hook in device driver functions.
984 */
985 void
986 intel_fbo_init(struct brw_context *brw)
987 {
988 struct dd_function_table *dd = &brw->ctx.Driver;
989 dd->NewFramebuffer = intel_new_framebuffer;
990 dd->NewRenderbuffer = intel_new_renderbuffer;
991 dd->MapRenderbuffer = intel_map_renderbuffer;
992 dd->UnmapRenderbuffer = intel_unmap_renderbuffer;
993 dd->RenderTexture = intel_render_texture;
994 dd->ValidateFramebuffer = intel_validate_framebuffer;
995 dd->BlitFramebuffer = intel_blit_framebuffer;
996 dd->EGLImageTargetRenderbufferStorage =
997 intel_image_target_renderbuffer_storage;
998
999 brw->render_cache = _mesa_set_create(brw, _mesa_key_pointer_equal);
1000 }