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