i965: Add missing close-parenthesis in error messages
[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 if (!brw->format_supported_as_render_target[image->format]) {
352 _mesa_error(ctx, GL_INVALID_OPERATION,
353 "glEGLImageTargetRenderbufferStorage(unsupported image format)");
354 return;
355 }
356
357 irb = intel_renderbuffer(rb);
358 intel_miptree_release(&irb->mt);
359
360 /* Disable creation of the miptree's aux buffers because the driver exposes
361 * no EGL API to manage them. That is, there is no API for resolving the aux
362 * buffer's content to the main buffer nor for invalidating the aux buffer's
363 * content.
364 */
365 irb->mt = intel_miptree_create_for_bo(brw,
366 image->bo,
367 image->format,
368 image->offset,
369 image->width,
370 image->height,
371 1,
372 image->pitch,
373 MIPTREE_LAYOUT_DISABLE_AUX);
374 if (!irb->mt)
375 return;
376
377 rb->InternalFormat = image->internal_format;
378 rb->Width = image->width;
379 rb->Height = image->height;
380 rb->Format = image->format;
381 rb->_BaseFormat = _mesa_get_format_base_format(image->format);
382 rb->NeedsFinishRenderTexture = true;
383 irb->layer_count = 1;
384 }
385
386 /**
387 * Called by _mesa_resize_framebuffer() for each hardware renderbuffer when a
388 * window system framebuffer is resized.
389 *
390 * Any actual buffer reallocations for hardware renderbuffers (which would
391 * have triggered _mesa_resize_framebuffer()) were done by
392 * intel_process_dri2_buffer().
393 */
394 static GLboolean
395 intel_alloc_window_storage(struct gl_context * ctx, struct gl_renderbuffer *rb,
396 GLenum internalFormat, GLuint width, GLuint height)
397 {
398 (void) ctx;
399 assert(rb->Name == 0);
400 rb->Width = width;
401 rb->Height = height;
402 rb->InternalFormat = internalFormat;
403
404 return true;
405 }
406
407 /** Dummy function for gl_renderbuffer::AllocStorage() */
408 static GLboolean
409 intel_nop_alloc_storage(struct gl_context * ctx, struct gl_renderbuffer *rb,
410 GLenum internalFormat, GLuint width, GLuint height)
411 {
412 (void) rb;
413 (void) internalFormat;
414 (void) width;
415 (void) height;
416 _mesa_problem(ctx, "intel_nop_alloc_storage should never be called.");
417 return false;
418 }
419
420 /**
421 * Create a new intel_renderbuffer which corresponds to an on-screen window,
422 * not a user-created renderbuffer.
423 *
424 * \param num_samples must be quantized.
425 */
426 struct intel_renderbuffer *
427 intel_create_renderbuffer(mesa_format format, unsigned num_samples)
428 {
429 struct intel_renderbuffer *irb;
430 struct gl_renderbuffer *rb;
431
432 GET_CURRENT_CONTEXT(ctx);
433
434 irb = CALLOC_STRUCT(intel_renderbuffer);
435 if (!irb) {
436 _mesa_error(ctx, GL_OUT_OF_MEMORY, "creating renderbuffer");
437 return NULL;
438 }
439
440 rb = &irb->Base.Base;
441 irb->layer_count = 1;
442
443 _mesa_init_renderbuffer(rb, 0);
444 rb->ClassID = INTEL_RB_CLASS;
445 rb->_BaseFormat = _mesa_get_format_base_format(format);
446 rb->Format = format;
447 rb->InternalFormat = rb->_BaseFormat;
448 rb->NumSamples = num_samples;
449
450 /* intel-specific methods */
451 rb->Delete = intel_delete_renderbuffer;
452 rb->AllocStorage = intel_alloc_window_storage;
453
454 return irb;
455 }
456
457 /**
458 * Private window-system buffers (as opposed to ones shared with the display
459 * server created with intel_create_renderbuffer()) are most similar in their
460 * handling to user-created renderbuffers, but they have a resize handler that
461 * may be called at intel_update_renderbuffers() time.
462 *
463 * \param num_samples must be quantized.
464 */
465 struct intel_renderbuffer *
466 intel_create_private_renderbuffer(mesa_format format, unsigned num_samples)
467 {
468 struct intel_renderbuffer *irb;
469
470 irb = intel_create_renderbuffer(format, num_samples);
471 irb->Base.Base.AllocStorage = intel_alloc_private_renderbuffer_storage;
472
473 return irb;
474 }
475
476 /**
477 * Create a new renderbuffer object.
478 * Typically called via glBindRenderbufferEXT().
479 */
480 static struct gl_renderbuffer *
481 intel_new_renderbuffer(struct gl_context * ctx, GLuint name)
482 {
483 struct intel_renderbuffer *irb;
484 struct gl_renderbuffer *rb;
485
486 irb = CALLOC_STRUCT(intel_renderbuffer);
487 if (!irb) {
488 _mesa_error(ctx, GL_OUT_OF_MEMORY, "creating renderbuffer");
489 return NULL;
490 }
491
492 rb = &irb->Base.Base;
493
494 _mesa_init_renderbuffer(rb, name);
495 rb->ClassID = INTEL_RB_CLASS;
496
497 /* intel-specific methods */
498 rb->Delete = intel_delete_renderbuffer;
499 rb->AllocStorage = intel_alloc_renderbuffer_storage;
500 /* span routines set in alloc_storage function */
501
502 return rb;
503 }
504
505 static bool
506 intel_renderbuffer_update_wrapper(struct brw_context *brw,
507 struct intel_renderbuffer *irb,
508 struct gl_texture_image *image,
509 uint32_t layer,
510 bool layered)
511 {
512 struct gl_renderbuffer *rb = &irb->Base.Base;
513 struct intel_texture_image *intel_image = intel_texture_image(image);
514 struct intel_mipmap_tree *mt = intel_image->mt;
515 int level = image->Level;
516
517 rb->AllocStorage = intel_nop_alloc_storage;
518
519 /* adjust for texture view parameters */
520 layer += image->TexObject->MinLayer;
521 level += image->TexObject->MinLevel;
522
523 intel_miptree_check_level_layer(mt, level, layer);
524 irb->mt_level = level;
525
526 int layer_multiplier;
527 switch (mt->msaa_layout) {
528 case INTEL_MSAA_LAYOUT_UMS:
529 case INTEL_MSAA_LAYOUT_CMS:
530 layer_multiplier = mt->num_samples;
531 break;
532
533 default:
534 layer_multiplier = 1;
535 }
536
537 irb->mt_layer = layer_multiplier * layer;
538
539 if (!layered) {
540 irb->layer_count = 1;
541 } else if (image->TexObject->NumLayers > 0) {
542 irb->layer_count = image->TexObject->NumLayers;
543 } else {
544 irb->layer_count = mt->level[level].depth / layer_multiplier;
545 }
546
547 intel_miptree_reference(&irb->mt, mt);
548
549 intel_renderbuffer_set_draw_offset(irb);
550
551 if (intel_miptree_wants_hiz_buffer(brw, mt)) {
552 intel_miptree_alloc_hiz(brw, mt);
553 if (!mt->hiz_buf)
554 return false;
555 }
556
557 return true;
558 }
559
560 void
561 intel_renderbuffer_set_draw_offset(struct intel_renderbuffer *irb)
562 {
563 unsigned int dst_x, dst_y;
564
565 /* compute offset of the particular 2D image within the texture region */
566 intel_miptree_get_image_offset(irb->mt,
567 irb->mt_level,
568 irb->mt_layer,
569 &dst_x, &dst_y);
570
571 irb->draw_x = dst_x;
572 irb->draw_y = dst_y;
573 }
574
575 /**
576 * Called by glFramebufferTexture[123]DEXT() (and other places) to
577 * prepare for rendering into texture memory. This might be called
578 * many times to choose different texture levels, cube faces, etc
579 * before intel_finish_render_texture() is ever called.
580 */
581 static void
582 intel_render_texture(struct gl_context * ctx,
583 struct gl_framebuffer *fb,
584 struct gl_renderbuffer_attachment *att)
585 {
586 struct brw_context *brw = brw_context(ctx);
587 struct gl_renderbuffer *rb = att->Renderbuffer;
588 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
589 struct gl_texture_image *image = rb->TexImage;
590 struct intel_texture_image *intel_image = intel_texture_image(image);
591 struct intel_mipmap_tree *mt = intel_image->mt;
592 int layer;
593
594 (void) fb;
595
596 if (att->CubeMapFace > 0) {
597 assert(att->Zoffset == 0);
598 layer = att->CubeMapFace;
599 } else {
600 layer = att->Zoffset;
601 }
602
603 if (!intel_image->mt) {
604 /* Fallback on drawing to a texture that doesn't have a miptree
605 * (has a border, width/height 0, etc.)
606 */
607 _swrast_render_texture(ctx, fb, att);
608 return;
609 }
610
611 intel_miptree_check_level_layer(mt, att->TextureLevel, layer);
612
613 if (!intel_renderbuffer_update_wrapper(brw, irb, image, layer, att->Layered)) {
614 _swrast_render_texture(ctx, fb, att);
615 return;
616 }
617
618 DBG("Begin render %s texture tex=%u w=%d h=%d d=%d refcount=%d\n",
619 _mesa_get_format_name(image->TexFormat),
620 att->Texture->Name, image->Width, image->Height, image->Depth,
621 rb->RefCount);
622 }
623
624
625 #define fbo_incomplete(fb, ...) do { \
626 static GLuint msg_id = 0; \
627 if (unlikely(ctx->Const.ContextFlags & GL_CONTEXT_FLAG_DEBUG_BIT)) { \
628 _mesa_gl_debug(ctx, &msg_id, \
629 MESA_DEBUG_SOURCE_API, \
630 MESA_DEBUG_TYPE_OTHER, \
631 MESA_DEBUG_SEVERITY_MEDIUM, \
632 __VA_ARGS__); \
633 } \
634 DBG(__VA_ARGS__); \
635 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED; \
636 } while (0)
637
638 /**
639 * Do additional "completeness" testing of a framebuffer object.
640 */
641 static void
642 intel_validate_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
643 {
644 struct brw_context *brw = brw_context(ctx);
645 struct intel_renderbuffer *depthRb =
646 intel_get_renderbuffer(fb, BUFFER_DEPTH);
647 struct intel_renderbuffer *stencilRb =
648 intel_get_renderbuffer(fb, BUFFER_STENCIL);
649 struct intel_mipmap_tree *depth_mt = NULL, *stencil_mt = NULL;
650 unsigned i;
651
652 DBG("%s() on fb %p (%s)\n", __func__,
653 fb, (fb == ctx->DrawBuffer ? "drawbuffer" :
654 (fb == ctx->ReadBuffer ? "readbuffer" : "other buffer")));
655
656 if (depthRb)
657 depth_mt = depthRb->mt;
658 if (stencilRb) {
659 stencil_mt = stencilRb->mt;
660 if (stencil_mt->stencil_mt)
661 stencil_mt = stencil_mt->stencil_mt;
662 }
663
664 if (depth_mt && stencil_mt) {
665 if (brw->gen >= 6) {
666 /* For gen >= 6, we are using the lod/minimum-array-element fields
667 * and supporting layered rendering. This means that we must restrict
668 * the depth & stencil attachments to match in various more retrictive
669 * ways. (width, height, depth, LOD and layer)
670 */
671 if (depth_mt->physical_width0 != stencil_mt->physical_width0 ||
672 depth_mt->physical_height0 != stencil_mt->physical_height0 ||
673 depth_mt->physical_depth0 != stencil_mt->physical_depth0 ||
674 depthRb->mt_level != stencilRb->mt_level ||
675 depthRb->mt_layer != stencilRb->mt_layer) {
676 fbo_incomplete(fb,
677 "FBO incomplete: depth and stencil must match in"
678 "width, height, depth, LOD and layer\n");
679 }
680 }
681 if (depth_mt == stencil_mt) {
682 /* For true packed depth/stencil (not faked on prefers-separate-stencil
683 * hardware) we need to be sure they're the same level/layer, since
684 * we'll be emitting a single packet describing the packed setup.
685 */
686 if (depthRb->mt_level != stencilRb->mt_level ||
687 depthRb->mt_layer != stencilRb->mt_layer) {
688 fbo_incomplete(fb,
689 "FBO incomplete: depth image level/layer %d/%d != "
690 "stencil image %d/%d\n",
691 depthRb->mt_level,
692 depthRb->mt_layer,
693 stencilRb->mt_level,
694 stencilRb->mt_layer);
695 }
696 } else {
697 if (!brw->has_separate_stencil) {
698 fbo_incomplete(fb, "FBO incomplete: separate stencil "
699 "unsupported\n");
700 }
701 if (stencil_mt->format != MESA_FORMAT_S_UINT8) {
702 fbo_incomplete(fb, "FBO incomplete: separate stencil is %s "
703 "instead of S8\n",
704 _mesa_get_format_name(stencil_mt->format));
705 }
706 if (brw->gen < 7 && !intel_renderbuffer_has_hiz(depthRb)) {
707 /* Before Gen7, separate depth and stencil buffers can be used
708 * only if HiZ is enabled. From the Sandybridge PRM, Volume 2,
709 * Part 1, Bit 3DSTATE_DEPTH_BUFFER.SeparateStencilBufferEnable:
710 * [DevSNB]: This field must be set to the same value (enabled
711 * or disabled) as Hierarchical Depth Buffer Enable.
712 */
713 fbo_incomplete(fb, "FBO incomplete: separate stencil "
714 "without HiZ\n");
715 }
716 }
717 }
718
719 for (i = 0; i < ARRAY_SIZE(fb->Attachment); i++) {
720 struct gl_renderbuffer *rb;
721 struct intel_renderbuffer *irb;
722
723 if (fb->Attachment[i].Type == GL_NONE)
724 continue;
725
726 /* A supported attachment will have a Renderbuffer set either
727 * from being a Renderbuffer or being a texture that got the
728 * intel_wrap_texture() treatment.
729 */
730 rb = fb->Attachment[i].Renderbuffer;
731 if (rb == NULL) {
732 fbo_incomplete(fb, "FBO incomplete: attachment without "
733 "renderbuffer\n");
734 continue;
735 }
736
737 if (fb->Attachment[i].Type == GL_TEXTURE) {
738 if (rb->TexImage->Border) {
739 fbo_incomplete(fb, "FBO incomplete: texture with border\n");
740 continue;
741 }
742 }
743
744 irb = intel_renderbuffer(rb);
745 if (irb == NULL) {
746 fbo_incomplete(fb, "FBO incomplete: software rendering "
747 "renderbuffer\n");
748 continue;
749 }
750
751 if (!brw_render_target_supported(brw, rb)) {
752 fbo_incomplete(fb, "FBO incomplete: Unsupported HW "
753 "texture/renderbuffer format attached: %s\n",
754 _mesa_get_format_name(intel_rb_format(irb)));
755 }
756 }
757 }
758
759 /**
760 * Try to do a glBlitFramebuffer using glCopyTexSubImage2D
761 * We can do this when the dst renderbuffer is actually a texture and
762 * there is no scaling, mirroring or scissoring.
763 *
764 * \return new buffer mask indicating the buffers left to blit using the
765 * normal path.
766 */
767 static GLbitfield
768 intel_blit_framebuffer_with_blitter(struct gl_context *ctx,
769 const struct gl_framebuffer *readFb,
770 const struct gl_framebuffer *drawFb,
771 GLint srcX0, GLint srcY0,
772 GLint srcX1, GLint srcY1,
773 GLint dstX0, GLint dstY0,
774 GLint dstX1, GLint dstY1,
775 GLbitfield mask)
776 {
777 struct brw_context *brw = brw_context(ctx);
778
779 /* Sync up the state of window system buffers. We need to do this before
780 * we go looking for the buffers.
781 */
782 intel_prepare_render(brw);
783
784 if (mask & GL_COLOR_BUFFER_BIT) {
785 unsigned i;
786 struct gl_renderbuffer *src_rb = readFb->_ColorReadBuffer;
787 struct intel_renderbuffer *src_irb = intel_renderbuffer(src_rb);
788
789 if (!src_irb) {
790 perf_debug("glBlitFramebuffer(): missing src renderbuffer. "
791 "Falling back to software rendering.\n");
792 return mask;
793 }
794
795 /* If the source and destination are the same size with no mirroring,
796 * the rectangles are within the size of the texture and there is no
797 * scissor, then we can probably use the blit engine.
798 */
799 if (!(srcX0 - srcX1 == dstX0 - dstX1 &&
800 srcY0 - srcY1 == dstY0 - dstY1 &&
801 srcX1 >= srcX0 &&
802 srcY1 >= srcY0 &&
803 srcX0 >= 0 && srcX1 <= readFb->Width &&
804 srcY0 >= 0 && srcY1 <= readFb->Height &&
805 dstX0 >= 0 && dstX1 <= drawFb->Width &&
806 dstY0 >= 0 && dstY1 <= drawFb->Height &&
807 !(ctx->Scissor.EnableFlags))) {
808 perf_debug("glBlitFramebuffer(): non-1:1 blit. "
809 "Falling back to software rendering.\n");
810 return mask;
811 }
812
813 /* Blit to all active draw buffers. We don't do any pre-checking,
814 * because we assume that copying to MRTs is rare, and failure midway
815 * through copying is even more rare. Even if it was to occur, it's
816 * safe to let meta start the copy over from scratch, because
817 * glBlitFramebuffer completely overwrites the destination pixels, and
818 * results are undefined if any destination pixels have a dependency on
819 * source pixels.
820 */
821 for (i = 0; i < drawFb->_NumColorDrawBuffers; i++) {
822 struct gl_renderbuffer *dst_rb = drawFb->_ColorDrawBuffers[i];
823 struct intel_renderbuffer *dst_irb = intel_renderbuffer(dst_rb);
824
825 if (!dst_irb) {
826 perf_debug("glBlitFramebuffer(): missing dst renderbuffer. "
827 "Falling back to software rendering.\n");
828 return mask;
829 }
830
831 if (!intel_miptree_blit(brw,
832 src_irb->mt,
833 src_irb->mt_level, src_irb->mt_layer,
834 srcX0, srcY0, src_rb->Name == 0,
835 dst_irb->mt,
836 dst_irb->mt_level, dst_irb->mt_layer,
837 dstX0, dstY0, dst_rb->Name == 0,
838 dstX1 - dstX0, dstY1 - dstY0, GL_COPY)) {
839 perf_debug("glBlitFramebuffer(): unknown blit failure. "
840 "Falling back to software rendering.\n");
841 return mask;
842 }
843 }
844
845 mask &= ~GL_COLOR_BUFFER_BIT;
846 }
847
848 return mask;
849 }
850
851 static void
852 intel_blit_framebuffer(struct gl_context *ctx,
853 struct gl_framebuffer *readFb,
854 struct gl_framebuffer *drawFb,
855 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
856 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
857 GLbitfield mask, GLenum filter)
858 {
859 struct brw_context *brw = brw_context(ctx);
860
861 /* Page 679 of OpenGL 4.4 spec says:
862 * "Added BlitFramebuffer to commands affected by conditional rendering in
863 * section 10.10 (Bug 9562)."
864 */
865 if (!_mesa_check_conditional_render(ctx))
866 return;
867
868 mask = brw_blorp_framebuffer(brw, readFb, drawFb,
869 srcX0, srcY0, srcX1, srcY1,
870 dstX0, dstY0, dstX1, dstY1,
871 mask, filter);
872 if (mask == 0x0)
873 return;
874
875 mask = _mesa_meta_BlitFramebuffer(ctx, readFb, drawFb,
876 srcX0, srcY0, srcX1, srcY1,
877 dstX0, dstY0, dstX1, dstY1,
878 mask, filter);
879 if (mask == 0x0)
880 return;
881
882 if (brw->gen >= 8 && (mask & GL_STENCIL_BUFFER_BIT)) {
883 brw_meta_fbo_stencil_blit(brw_context(ctx), readFb, drawFb,
884 srcX0, srcY0, srcX1, srcY1,
885 dstX0, dstY0, dstX1, dstY1);
886 mask &= ~GL_STENCIL_BUFFER_BIT;
887 if (mask == 0x0)
888 return;
889 }
890
891 /* Try using the BLT engine. */
892 mask = intel_blit_framebuffer_with_blitter(ctx, readFb, drawFb,
893 srcX0, srcY0, srcX1, srcY1,
894 dstX0, dstY0, dstX1, dstY1,
895 mask);
896 if (mask == 0x0)
897 return;
898
899 _swrast_BlitFramebuffer(ctx, readFb, drawFb,
900 srcX0, srcY0, srcX1, srcY1,
901 dstX0, dstY0, dstX1, dstY1,
902 mask, filter);
903 }
904
905 /**
906 * Gen4-5 implementation of glBlitFrameBuffer().
907 *
908 * Tries BLT, Meta, then swrast.
909 *
910 * Gen4-5 have a single ring for both 3D and BLT operations, so there's no
911 * inter-ring synchronization issues like on Gen6+. It is apparently faster
912 * than using the 3D pipeline. Original Gen4 also has to rebase and copy
913 * miptree slices in order to render to unaligned locations.
914 */
915 static void
916 gen4_blit_framebuffer(struct gl_context *ctx,
917 struct gl_framebuffer *readFb,
918 struct gl_framebuffer *drawFb,
919 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
920 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
921 GLbitfield mask, GLenum filter)
922 {
923 /* Page 679 of OpenGL 4.4 spec says:
924 * "Added BlitFramebuffer to commands affected by conditional rendering in
925 * section 10.10 (Bug 9562)."
926 */
927 if (!_mesa_check_conditional_render(ctx))
928 return;
929
930 mask = intel_blit_framebuffer_with_blitter(ctx, readFb, drawFb,
931 srcX0, srcY0, srcX1, srcY1,
932 dstX0, dstY0, dstX1, dstY1,
933 mask);
934 if (mask == 0x0)
935 return;
936
937 mask = _mesa_meta_BlitFramebuffer(ctx, readFb, drawFb,
938 srcX0, srcY0, srcX1, srcY1,
939 dstX0, dstY0, dstX1, dstY1,
940 mask, filter);
941 if (mask == 0x0)
942 return;
943
944 _swrast_BlitFramebuffer(ctx, readFb, drawFb,
945 srcX0, srcY0, srcX1, srcY1,
946 dstX0, dstY0, dstX1, dstY1,
947 mask, filter);
948 }
949
950 /**
951 * Does the renderbuffer have hiz enabled?
952 */
953 bool
954 intel_renderbuffer_has_hiz(struct intel_renderbuffer *irb)
955 {
956 return intel_miptree_level_has_hiz(irb->mt, irb->mt_level);
957 }
958
959 bool
960 intel_renderbuffer_resolve_hiz(struct brw_context *brw,
961 struct intel_renderbuffer *irb)
962 {
963 if (irb->mt)
964 return intel_miptree_slice_resolve_hiz(brw,
965 irb->mt,
966 irb->mt_level,
967 irb->mt_layer);
968
969 return false;
970 }
971
972 void
973 intel_renderbuffer_att_set_needs_depth_resolve(struct gl_renderbuffer_attachment *att)
974 {
975 struct intel_renderbuffer *irb = intel_renderbuffer(att->Renderbuffer);
976 if (irb->mt) {
977 if (att->Layered) {
978 intel_miptree_set_all_slices_need_depth_resolve(irb->mt, irb->mt_level);
979 } else {
980 intel_miptree_slice_set_needs_depth_resolve(irb->mt,
981 irb->mt_level,
982 irb->mt_layer);
983 }
984 }
985 }
986
987 bool
988 intel_renderbuffer_resolve_depth(struct brw_context *brw,
989 struct intel_renderbuffer *irb)
990 {
991 if (irb->mt)
992 return intel_miptree_slice_resolve_depth(brw,
993 irb->mt,
994 irb->mt_level,
995 irb->mt_layer);
996
997 return false;
998 }
999
1000 void
1001 intel_renderbuffer_move_to_temp(struct brw_context *brw,
1002 struct intel_renderbuffer *irb,
1003 bool invalidate)
1004 {
1005 struct gl_renderbuffer *rb =&irb->Base.Base;
1006 struct intel_texture_image *intel_image = intel_texture_image(rb->TexImage);
1007 struct intel_mipmap_tree *new_mt;
1008 int width, height, depth;
1009
1010 uint32_t layout_flags = MIPTREE_LAYOUT_ACCELERATED_UPLOAD |
1011 MIPTREE_LAYOUT_TILING_ANY;
1012
1013 intel_get_image_dims(rb->TexImage, &width, &height, &depth);
1014
1015 new_mt = intel_miptree_create(brw, rb->TexImage->TexObject->Target,
1016 intel_image->base.Base.TexFormat,
1017 intel_image->base.Base.Level,
1018 intel_image->base.Base.Level,
1019 width, height, depth,
1020 irb->mt->num_samples,
1021 layout_flags);
1022
1023 if (intel_miptree_wants_hiz_buffer(brw, new_mt)) {
1024 intel_miptree_alloc_hiz(brw, new_mt);
1025 }
1026
1027 intel_miptree_copy_teximage(brw, intel_image, new_mt, invalidate);
1028
1029 intel_miptree_reference(&irb->mt, intel_image->mt);
1030 intel_renderbuffer_set_draw_offset(irb);
1031 intel_miptree_release(&new_mt);
1032 }
1033
1034 void
1035 brw_render_cache_set_clear(struct brw_context *brw)
1036 {
1037 struct set_entry *entry;
1038
1039 set_foreach(brw->render_cache, entry) {
1040 _mesa_set_remove(brw->render_cache, entry);
1041 }
1042 }
1043
1044 void
1045 brw_render_cache_set_add_bo(struct brw_context *brw, drm_intel_bo *bo)
1046 {
1047 _mesa_set_add(brw->render_cache, bo);
1048 }
1049
1050 /**
1051 * Emits an appropriate flush for a BO if it has been rendered to within the
1052 * same batchbuffer as a read that's about to be emitted.
1053 *
1054 * The GPU has separate, incoherent caches for the render cache and the
1055 * sampler cache, along with other caches. Usually data in the different
1056 * caches don't interact (e.g. we don't render to our driver-generated
1057 * immediate constant data), but for render-to-texture in FBOs we definitely
1058 * do. When a batchbuffer is flushed, the kernel will ensure that everything
1059 * necessary is flushed before another use of that BO, but for reuse from
1060 * different caches within a batchbuffer, it's all our responsibility.
1061 */
1062 void
1063 brw_render_cache_set_check_flush(struct brw_context *brw, drm_intel_bo *bo)
1064 {
1065 if (!_mesa_set_search(brw->render_cache, bo))
1066 return;
1067
1068 brw_emit_mi_flush(brw);
1069 }
1070
1071 /**
1072 * Do one-time context initializations related to GL_EXT_framebuffer_object.
1073 * Hook in device driver functions.
1074 */
1075 void
1076 intel_fbo_init(struct brw_context *brw)
1077 {
1078 struct dd_function_table *dd = &brw->ctx.Driver;
1079 dd->NewRenderbuffer = intel_new_renderbuffer;
1080 dd->MapRenderbuffer = intel_map_renderbuffer;
1081 dd->UnmapRenderbuffer = intel_unmap_renderbuffer;
1082 dd->RenderTexture = intel_render_texture;
1083 dd->ValidateFramebuffer = intel_validate_framebuffer;
1084 if (brw->gen >= 6)
1085 dd->BlitFramebuffer = intel_blit_framebuffer;
1086 else
1087 dd->BlitFramebuffer = gen4_blit_framebuffer;
1088 dd->EGLImageTargetRenderbufferStorage =
1089 intel_image_target_renderbuffer_storage;
1090
1091 brw->render_cache = _mesa_set_create(brw, _mesa_hash_pointer,
1092 _mesa_key_pointer_equal);
1093 }