i965/blorp: Take a layer range in intel_hiz_exec
[mesa.git] / src / mesa / drivers / dri / i965 / brw_blorp.c
1 /*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "main/context.h"
25 #include "main/teximage.h"
26 #include "main/blend.h"
27 #include "main/fbobject.h"
28 #include "main/renderbuffer.h"
29 #include "main/glformats.h"
30
31 #include "brw_blorp.h"
32 #include "brw_context.h"
33 #include "brw_defines.h"
34 #include "brw_meta_util.h"
35 #include "brw_state.h"
36 #include "intel_fbo.h"
37 #include "common/gen_debug.h"
38
39 #define FILE_DEBUG_FLAG DEBUG_BLORP
40
41 static bool
42 brw_blorp_lookup_shader(struct blorp_context *blorp,
43 const void *key, uint32_t key_size,
44 uint32_t *kernel_out, void *prog_data_out)
45 {
46 struct brw_context *brw = blorp->driver_ctx;
47 return brw_search_cache(&brw->cache, BRW_CACHE_BLORP_PROG,
48 key, key_size, kernel_out, prog_data_out);
49 }
50
51 static bool
52 brw_blorp_upload_shader(struct blorp_context *blorp,
53 const void *key, uint32_t key_size,
54 const void *kernel, uint32_t kernel_size,
55 const struct brw_stage_prog_data *prog_data,
56 uint32_t prog_data_size,
57 uint32_t *kernel_out, void *prog_data_out)
58 {
59 struct brw_context *brw = blorp->driver_ctx;
60 brw_upload_cache(&brw->cache, BRW_CACHE_BLORP_PROG, key, key_size,
61 kernel, kernel_size, prog_data, prog_data_size,
62 kernel_out, prog_data_out);
63 return true;
64 }
65
66 void
67 brw_blorp_init(struct brw_context *brw)
68 {
69 blorp_init(&brw->blorp, brw, &brw->isl_dev);
70
71 brw->blorp.compiler = brw->screen->compiler;
72
73 switch (brw->gen) {
74 case 4:
75 if (brw->is_g4x) {
76 brw->blorp.exec = gen45_blorp_exec;
77 } else {
78 brw->blorp.exec = gen4_blorp_exec;
79 }
80 break;
81 case 5:
82 brw->blorp.exec = gen5_blorp_exec;
83 break;
84 case 6:
85 brw->blorp.mocs.tex = 0;
86 brw->blorp.mocs.rb = 0;
87 brw->blorp.mocs.vb = 0;
88 brw->blorp.exec = gen6_blorp_exec;
89 break;
90 case 7:
91 brw->blorp.mocs.tex = GEN7_MOCS_L3;
92 brw->blorp.mocs.rb = GEN7_MOCS_L3;
93 brw->blorp.mocs.vb = GEN7_MOCS_L3;
94 if (brw->is_haswell) {
95 brw->blorp.exec = gen75_blorp_exec;
96 } else {
97 brw->blorp.exec = gen7_blorp_exec;
98 }
99 break;
100 case 8:
101 brw->blorp.mocs.tex = BDW_MOCS_WB;
102 brw->blorp.mocs.rb = BDW_MOCS_PTE;
103 brw->blorp.mocs.vb = BDW_MOCS_WB;
104 brw->blorp.exec = gen8_blorp_exec;
105 break;
106 case 9:
107 brw->blorp.mocs.tex = SKL_MOCS_WB;
108 brw->blorp.mocs.rb = SKL_MOCS_PTE;
109 brw->blorp.mocs.vb = SKL_MOCS_WB;
110 brw->blorp.exec = gen9_blorp_exec;
111 break;
112 default:
113 unreachable("Invalid gen");
114 }
115
116 brw->blorp.lookup_shader = brw_blorp_lookup_shader;
117 brw->blorp.upload_shader = brw_blorp_upload_shader;
118 }
119
120 static void
121 blorp_surf_for_miptree(struct brw_context *brw,
122 struct blorp_surf *surf,
123 struct intel_mipmap_tree *mt,
124 bool is_render_target,
125 uint32_t safe_aux_usage,
126 unsigned *level,
127 unsigned start_layer, unsigned num_layers,
128 struct isl_surf tmp_surfs[2])
129 {
130 if (mt->msaa_layout == INTEL_MSAA_LAYOUT_UMS ||
131 mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS) {
132 const unsigned num_samples = MAX2(1, mt->num_samples);
133 for (unsigned i = 0; i < num_layers; i++) {
134 for (unsigned s = 0; s < num_samples; s++) {
135 const unsigned phys_layer = (start_layer + i) * num_samples + s;
136 intel_miptree_check_level_layer(mt, *level, phys_layer);
137 }
138 }
139 } else {
140 for (unsigned i = 0; i < num_layers; i++)
141 intel_miptree_check_level_layer(mt, *level, start_layer + i);
142 }
143
144 intel_miptree_get_isl_surf(brw, mt, &tmp_surfs[0]);
145 surf->surf = &tmp_surfs[0];
146 surf->addr = (struct blorp_address) {
147 .buffer = mt->bo,
148 .offset = mt->offset,
149 .read_domains = is_render_target ? I915_GEM_DOMAIN_RENDER :
150 I915_GEM_DOMAIN_SAMPLER,
151 .write_domain = is_render_target ? I915_GEM_DOMAIN_RENDER : 0,
152 };
153
154 struct isl_surf *aux_surf = &tmp_surfs[1];
155 intel_miptree_get_aux_isl_surf(brw, mt, aux_surf, &surf->aux_usage);
156
157 if (surf->aux_usage != ISL_AUX_USAGE_NONE) {
158 if (surf->aux_usage == ISL_AUX_USAGE_HIZ) {
159 /* If we're not going to use it as a depth buffer, resolve HiZ */
160 if (!(safe_aux_usage & (1 << ISL_AUX_USAGE_HIZ))) {
161 for (unsigned i = 0; i < num_layers; i++) {
162 intel_miptree_slice_resolve_depth(brw, mt, *level,
163 start_layer + i);
164
165 /* If we're rendering to it then we'll need a HiZ resolve once
166 * we're done before we can use it with HiZ again.
167 */
168 if (is_render_target)
169 intel_miptree_slice_set_needs_hiz_resolve(mt, *level,
170 start_layer + i);
171 }
172 surf->aux_usage = ISL_AUX_USAGE_NONE;
173 }
174 } else if (!(safe_aux_usage & (1 << surf->aux_usage))) {
175 uint32_t flags = 0;
176 if (safe_aux_usage & (1 << ISL_AUX_USAGE_CCS_E))
177 flags |= INTEL_MIPTREE_IGNORE_CCS_E;
178
179 intel_miptree_resolve_color(brw, mt,
180 *level, start_layer, num_layers, flags);
181
182 assert(!intel_miptree_has_color_unresolved(mt, *level, 1,
183 start_layer, num_layers));
184 surf->aux_usage = ISL_AUX_USAGE_NONE;
185 }
186 }
187
188 if (is_render_target)
189 intel_miptree_used_for_rendering(brw, mt, *level,
190 start_layer, num_layers);
191
192 if (surf->aux_usage != ISL_AUX_USAGE_NONE) {
193 /* We only really need a clear color if we also have an auxiliary
194 * surface. Without one, it does nothing.
195 */
196 surf->clear_color = mt->fast_clear_color;
197
198 surf->aux_surf = aux_surf;
199 surf->aux_addr = (struct blorp_address) {
200 .read_domains = is_render_target ? I915_GEM_DOMAIN_RENDER :
201 I915_GEM_DOMAIN_SAMPLER,
202 .write_domain = is_render_target ? I915_GEM_DOMAIN_RENDER : 0,
203 };
204
205 if (mt->mcs_buf) {
206 surf->aux_addr.buffer = mt->mcs_buf->bo;
207 surf->aux_addr.offset = mt->mcs_buf->offset;
208 } else {
209 assert(surf->aux_usage == ISL_AUX_USAGE_HIZ);
210
211 surf->aux_addr.buffer = mt->hiz_buf->aux_base.bo;
212 surf->aux_addr.offset = mt->hiz_buf->aux_base.offset;
213 }
214 } else {
215 surf->aux_addr = (struct blorp_address) {
216 .buffer = NULL,
217 };
218 memset(&surf->clear_color, 0, sizeof(surf->clear_color));
219 }
220 assert((surf->aux_usage == ISL_AUX_USAGE_NONE) ==
221 (surf->aux_addr.buffer == NULL));
222
223 /* ISL wants real levels, not offset ones. */
224 *level -= mt->first_level;
225 }
226
227 static enum isl_format
228 brw_blorp_to_isl_format(struct brw_context *brw, mesa_format format,
229 bool is_render_target)
230 {
231 switch (format) {
232 case MESA_FORMAT_NONE:
233 return ISL_FORMAT_UNSUPPORTED;
234 case MESA_FORMAT_S_UINT8:
235 return ISL_FORMAT_R8_UINT;
236 case MESA_FORMAT_Z24_UNORM_X8_UINT:
237 case MESA_FORMAT_Z24_UNORM_S8_UINT:
238 return ISL_FORMAT_R24_UNORM_X8_TYPELESS;
239 case MESA_FORMAT_Z_FLOAT32:
240 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
241 return ISL_FORMAT_R32_FLOAT;
242 case MESA_FORMAT_Z_UNORM16:
243 return ISL_FORMAT_R16_UNORM;
244 default: {
245 if (is_render_target) {
246 assert(brw->format_supported_as_render_target[format]);
247 return brw->render_target_format[format];
248 } else {
249 return brw_isl_format_for_mesa_format(format);
250 }
251 break;
252 }
253 }
254 }
255
256 /**
257 * Convert an swizzle enumeration (i.e. SWIZZLE_X) to one of the Gen7.5+
258 * "Shader Channel Select" enumerations (i.e. HSW_SCS_RED). The mappings are
259 *
260 * SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_ZERO, SWIZZLE_ONE
261 * 0 1 2 3 4 5
262 * 4 5 6 7 0 1
263 * SCS_RED, SCS_GREEN, SCS_BLUE, SCS_ALPHA, SCS_ZERO, SCS_ONE
264 *
265 * which is simply adding 4 then modding by 8 (or anding with 7).
266 *
267 * We then may need to apply workarounds for textureGather hardware bugs.
268 */
269 static enum isl_channel_select
270 swizzle_to_scs(GLenum swizzle)
271 {
272 return (enum isl_channel_select)((swizzle + 4) & 7);
273 }
274
275 static unsigned
276 physical_to_logical_layer(struct intel_mipmap_tree *mt,
277 unsigned physical_layer)
278 {
279 if (mt->num_samples > 1 &&
280 (mt->msaa_layout == INTEL_MSAA_LAYOUT_UMS ||
281 mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS)) {
282 assert(physical_layer % mt->num_samples == 0);
283 return physical_layer / mt->num_samples;
284 } else {
285 return physical_layer;
286 }
287 }
288
289 /**
290 * Note: if the src (or dst) is a 2D multisample array texture on Gen7+ using
291 * INTEL_MSAA_LAYOUT_UMS or INTEL_MSAA_LAYOUT_CMS, src_layer (dst_layer) is
292 * the physical layer holding sample 0. So, for example, if
293 * src_mt->num_samples == 4, then logical layer n corresponds to src_layer ==
294 * 4*n.
295 */
296 void
297 brw_blorp_blit_miptrees(struct brw_context *brw,
298 struct intel_mipmap_tree *src_mt,
299 unsigned src_level, unsigned src_layer,
300 mesa_format src_format, int src_swizzle,
301 struct intel_mipmap_tree *dst_mt,
302 unsigned dst_level, unsigned dst_layer,
303 mesa_format dst_format,
304 float src_x0, float src_y0,
305 float src_x1, float src_y1,
306 float dst_x0, float dst_y0,
307 float dst_x1, float dst_y1,
308 GLenum filter, bool mirror_x, bool mirror_y,
309 bool decode_srgb, bool encode_srgb)
310 {
311 /* Blorp operates in logical layers */
312 src_layer = physical_to_logical_layer(src_mt, src_layer);
313 dst_layer = physical_to_logical_layer(dst_mt, dst_layer);
314
315 DBG("%s from %dx %s mt %p %d %d (%f,%f) (%f,%f)"
316 "to %dx %s mt %p %d %d (%f,%f) (%f,%f) (flip %d,%d)\n",
317 __func__,
318 src_mt->num_samples, _mesa_get_format_name(src_mt->format), src_mt,
319 src_level, src_layer, src_x0, src_y0, src_x1, src_y1,
320 dst_mt->num_samples, _mesa_get_format_name(dst_mt->format), dst_mt,
321 dst_level, dst_layer, dst_x0, dst_y0, dst_x1, dst_y1,
322 mirror_x, mirror_y);
323
324 if (!decode_srgb && _mesa_get_format_color_encoding(src_format) == GL_SRGB)
325 src_format = _mesa_get_srgb_format_linear(src_format);
326
327 if (!encode_srgb && _mesa_get_format_color_encoding(dst_format) == GL_SRGB)
328 dst_format = _mesa_get_srgb_format_linear(dst_format);
329
330 /* When doing a multisample resolve of a GL_LUMINANCE32F or GL_INTENSITY32F
331 * texture, the above code configures the source format for L32_FLOAT or
332 * I32_FLOAT, and the destination format for R32_FLOAT. On Sandy Bridge,
333 * the SAMPLE message appears to handle multisampled L32_FLOAT and
334 * I32_FLOAT textures incorrectly, resulting in blocky artifacts. So work
335 * around the problem by using a source format of R32_FLOAT. This
336 * shouldn't affect rendering correctness, since the destination format is
337 * R32_FLOAT, so only the contents of the red channel matters.
338 */
339 if (brw->gen == 6 &&
340 src_mt->num_samples > 1 && dst_mt->num_samples <= 1 &&
341 src_mt->format == dst_mt->format &&
342 (dst_format == MESA_FORMAT_L_FLOAT32 ||
343 dst_format == MESA_FORMAT_I_FLOAT32)) {
344 src_format = dst_format = MESA_FORMAT_R_FLOAT32;
345 }
346
347 uint32_t src_usage_flags = (1 << ISL_AUX_USAGE_MCS);
348 if (src_format == src_mt->format)
349 src_usage_flags |= (1 << ISL_AUX_USAGE_CCS_E);
350
351 uint32_t dst_usage_flags = (1 << ISL_AUX_USAGE_MCS);
352 if (dst_format == dst_mt->format) {
353 dst_usage_flags |= (1 << ISL_AUX_USAGE_CCS_E) |
354 (1 << ISL_AUX_USAGE_CCS_D);
355 }
356
357 struct isl_surf tmp_surfs[4];
358 struct blorp_surf src_surf, dst_surf;
359 blorp_surf_for_miptree(brw, &src_surf, src_mt, false, src_usage_flags,
360 &src_level, src_layer, 1, &tmp_surfs[0]);
361 blorp_surf_for_miptree(brw, &dst_surf, dst_mt, true, dst_usage_flags,
362 &dst_level, dst_layer, 1, &tmp_surfs[2]);
363
364 struct isl_swizzle src_isl_swizzle = {
365 .r = swizzle_to_scs(GET_SWZ(src_swizzle, 0)),
366 .g = swizzle_to_scs(GET_SWZ(src_swizzle, 1)),
367 .b = swizzle_to_scs(GET_SWZ(src_swizzle, 2)),
368 .a = swizzle_to_scs(GET_SWZ(src_swizzle, 3)),
369 };
370
371 struct blorp_batch batch;
372 blorp_batch_init(&brw->blorp, &batch, brw, 0);
373 blorp_blit(&batch, &src_surf, src_level, src_layer,
374 brw_blorp_to_isl_format(brw, src_format, false), src_isl_swizzle,
375 &dst_surf, dst_level, dst_layer,
376 brw_blorp_to_isl_format(brw, dst_format, true),
377 ISL_SWIZZLE_IDENTITY,
378 src_x0, src_y0, src_x1, src_y1,
379 dst_x0, dst_y0, dst_x1, dst_y1,
380 filter, mirror_x, mirror_y);
381 blorp_batch_finish(&batch);
382 }
383
384 void
385 brw_blorp_copy_miptrees(struct brw_context *brw,
386 struct intel_mipmap_tree *src_mt,
387 unsigned src_level, unsigned src_layer,
388 struct intel_mipmap_tree *dst_mt,
389 unsigned dst_level, unsigned dst_layer,
390 unsigned src_x, unsigned src_y,
391 unsigned dst_x, unsigned dst_y,
392 unsigned src_width, unsigned src_height)
393 {
394 DBG("%s from %dx %s mt %p %d %d (%d,%d) %dx%d"
395 "to %dx %s mt %p %d %d (%d,%d)\n",
396 __func__,
397 src_mt->num_samples, _mesa_get_format_name(src_mt->format), src_mt,
398 src_level, src_layer, src_x, src_y, src_width, src_height,
399 dst_mt->num_samples, _mesa_get_format_name(dst_mt->format), dst_mt,
400 dst_level, dst_layer, dst_x, dst_y);
401
402 struct isl_surf tmp_surfs[4];
403 struct blorp_surf src_surf, dst_surf;
404 blorp_surf_for_miptree(brw, &src_surf, src_mt, false,
405 (1 << ISL_AUX_USAGE_MCS) |
406 (1 << ISL_AUX_USAGE_CCS_E),
407 &src_level, src_layer, 1, &tmp_surfs[0]);
408 blorp_surf_for_miptree(brw, &dst_surf, dst_mt, true,
409 (1 << ISL_AUX_USAGE_MCS) |
410 (1 << ISL_AUX_USAGE_CCS_E),
411 &dst_level, dst_layer, 1, &tmp_surfs[2]);
412
413 struct blorp_batch batch;
414 blorp_batch_init(&brw->blorp, &batch, brw, 0);
415 blorp_copy(&batch, &src_surf, src_level, src_layer,
416 &dst_surf, dst_level, dst_layer,
417 src_x, src_y, dst_x, dst_y, src_width, src_height);
418 blorp_batch_finish(&batch);
419 }
420
421 static struct intel_mipmap_tree *
422 find_miptree(GLbitfield buffer_bit, struct intel_renderbuffer *irb)
423 {
424 struct intel_mipmap_tree *mt = irb->mt;
425 if (buffer_bit == GL_STENCIL_BUFFER_BIT && mt->stencil_mt)
426 mt = mt->stencil_mt;
427 return mt;
428 }
429
430 static int
431 blorp_get_texture_swizzle(const struct intel_renderbuffer *irb)
432 {
433 return irb->Base.Base._BaseFormat == GL_RGB ?
434 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE) :
435 SWIZZLE_XYZW;
436 }
437
438 static void
439 do_blorp_blit(struct brw_context *brw, GLbitfield buffer_bit,
440 struct intel_renderbuffer *src_irb, mesa_format src_format,
441 struct intel_renderbuffer *dst_irb, mesa_format dst_format,
442 GLfloat srcX0, GLfloat srcY0, GLfloat srcX1, GLfloat srcY1,
443 GLfloat dstX0, GLfloat dstY0, GLfloat dstX1, GLfloat dstY1,
444 GLenum filter, bool mirror_x, bool mirror_y)
445 {
446 const struct gl_context *ctx = &brw->ctx;
447
448 /* Find source/dst miptrees */
449 struct intel_mipmap_tree *src_mt = find_miptree(buffer_bit, src_irb);
450 struct intel_mipmap_tree *dst_mt = find_miptree(buffer_bit, dst_irb);
451
452 const bool do_srgb = ctx->Color.sRGBEnabled;
453
454 /* Do the blit */
455 brw_blorp_blit_miptrees(brw,
456 src_mt, src_irb->mt_level, src_irb->mt_layer,
457 src_format, blorp_get_texture_swizzle(src_irb),
458 dst_mt, dst_irb->mt_level, dst_irb->mt_layer,
459 dst_format,
460 srcX0, srcY0, srcX1, srcY1,
461 dstX0, dstY0, dstX1, dstY1,
462 filter, mirror_x, mirror_y,
463 do_srgb, do_srgb);
464
465 dst_irb->need_downsample = true;
466 }
467
468 static bool
469 try_blorp_blit(struct brw_context *brw,
470 const struct gl_framebuffer *read_fb,
471 const struct gl_framebuffer *draw_fb,
472 GLfloat srcX0, GLfloat srcY0, GLfloat srcX1, GLfloat srcY1,
473 GLfloat dstX0, GLfloat dstY0, GLfloat dstX1, GLfloat dstY1,
474 GLenum filter, GLbitfield buffer_bit)
475 {
476 struct gl_context *ctx = &brw->ctx;
477
478 /* Sync up the state of window system buffers. We need to do this before
479 * we go looking for the buffers.
480 */
481 intel_prepare_render(brw);
482
483 bool mirror_x, mirror_y;
484 if (brw_meta_mirror_clip_and_scissor(ctx, read_fb, draw_fb,
485 &srcX0, &srcY0, &srcX1, &srcY1,
486 &dstX0, &dstY0, &dstX1, &dstY1,
487 &mirror_x, &mirror_y))
488 return true;
489
490 /* Find buffers */
491 struct intel_renderbuffer *src_irb;
492 struct intel_renderbuffer *dst_irb;
493 struct intel_mipmap_tree *src_mt;
494 struct intel_mipmap_tree *dst_mt;
495 switch (buffer_bit) {
496 case GL_COLOR_BUFFER_BIT:
497 src_irb = intel_renderbuffer(read_fb->_ColorReadBuffer);
498 for (unsigned i = 0; i < draw_fb->_NumColorDrawBuffers; ++i) {
499 dst_irb = intel_renderbuffer(draw_fb->_ColorDrawBuffers[i]);
500 if (dst_irb)
501 do_blorp_blit(brw, buffer_bit,
502 src_irb, src_irb->Base.Base.Format,
503 dst_irb, dst_irb->Base.Base.Format,
504 srcX0, srcY0, srcX1, srcY1,
505 dstX0, dstY0, dstX1, dstY1,
506 filter, mirror_x, mirror_y);
507 }
508 break;
509 case GL_DEPTH_BUFFER_BIT:
510 src_irb =
511 intel_renderbuffer(read_fb->Attachment[BUFFER_DEPTH].Renderbuffer);
512 dst_irb =
513 intel_renderbuffer(draw_fb->Attachment[BUFFER_DEPTH].Renderbuffer);
514 src_mt = find_miptree(buffer_bit, src_irb);
515 dst_mt = find_miptree(buffer_bit, dst_irb);
516
517 /* We can't handle format conversions between Z24 and other formats
518 * since we have to lie about the surface format. See the comments in
519 * brw_blorp_surface_info::set().
520 */
521 if ((src_mt->format == MESA_FORMAT_Z24_UNORM_X8_UINT) !=
522 (dst_mt->format == MESA_FORMAT_Z24_UNORM_X8_UINT))
523 return false;
524
525 /* We also can't handle any combined depth-stencil formats because we
526 * have to reinterpret as a color format.
527 */
528 if (_mesa_get_format_base_format(src_mt->format) == GL_DEPTH_STENCIL ||
529 _mesa_get_format_base_format(dst_mt->format) == GL_DEPTH_STENCIL)
530 return false;
531
532 do_blorp_blit(brw, buffer_bit, src_irb, MESA_FORMAT_NONE,
533 dst_irb, MESA_FORMAT_NONE, srcX0, srcY0,
534 srcX1, srcY1, dstX0, dstY0, dstX1, dstY1,
535 filter, mirror_x, mirror_y);
536 break;
537 case GL_STENCIL_BUFFER_BIT:
538 /* Blorp doesn't support combined depth stencil which is all we have
539 * prior to gen6.
540 */
541 if (brw->gen < 6)
542 return false;
543
544 src_irb =
545 intel_renderbuffer(read_fb->Attachment[BUFFER_STENCIL].Renderbuffer);
546 dst_irb =
547 intel_renderbuffer(draw_fb->Attachment[BUFFER_STENCIL].Renderbuffer);
548 do_blorp_blit(brw, buffer_bit, src_irb, MESA_FORMAT_NONE,
549 dst_irb, MESA_FORMAT_NONE, srcX0, srcY0,
550 srcX1, srcY1, dstX0, dstY0, dstX1, dstY1,
551 filter, mirror_x, mirror_y);
552 break;
553 default:
554 unreachable("not reached");
555 }
556
557 return true;
558 }
559
560 bool
561 brw_blorp_copytexsubimage(struct brw_context *brw,
562 struct gl_renderbuffer *src_rb,
563 struct gl_texture_image *dst_image,
564 int slice,
565 int srcX0, int srcY0,
566 int dstX0, int dstY0,
567 int width, int height)
568 {
569 struct gl_context *ctx = &brw->ctx;
570 struct intel_renderbuffer *src_irb = intel_renderbuffer(src_rb);
571 struct intel_texture_image *intel_image = intel_texture_image(dst_image);
572
573 /* No pixel transfer operations (zoom, bias, mapping), just a blit */
574 if (brw->ctx._ImageTransferState)
575 return false;
576
577 /* Sync up the state of window system buffers. We need to do this before
578 * we go looking at the src renderbuffer's miptree.
579 */
580 intel_prepare_render(brw);
581
582 struct intel_mipmap_tree *src_mt = src_irb->mt;
583 struct intel_mipmap_tree *dst_mt = intel_image->mt;
584
585 /* There is support for only up to eight samples. */
586 if (src_mt->num_samples > 8 || dst_mt->num_samples > 8)
587 return false;
588
589 if (_mesa_get_format_base_format(src_rb->Format) !=
590 _mesa_get_format_base_format(dst_image->TexFormat)) {
591 return false;
592 }
593
594 /* We can't handle format conversions between Z24 and other formats since
595 * we have to lie about the surface format. See the comments in
596 * brw_blorp_surface_info::set().
597 */
598 if ((src_mt->format == MESA_FORMAT_Z24_UNORM_X8_UINT) !=
599 (dst_mt->format == MESA_FORMAT_Z24_UNORM_X8_UINT)) {
600 return false;
601 }
602
603 /* We also can't handle any combined depth-stencil formats because we
604 * have to reinterpret as a color format.
605 */
606 if (_mesa_get_format_base_format(src_mt->format) == GL_DEPTH_STENCIL ||
607 _mesa_get_format_base_format(dst_mt->format) == GL_DEPTH_STENCIL)
608 return false;
609
610 if (!brw->format_supported_as_render_target[dst_image->TexFormat])
611 return false;
612
613 /* Source clipping shouldn't be necessary, since copytexsubimage (in
614 * src/mesa/main/teximage.c) calls _mesa_clip_copytexsubimage() which
615 * takes care of it.
616 *
617 * Destination clipping shouldn't be necessary since the restrictions on
618 * glCopyTexSubImage prevent the user from specifying a destination rectangle
619 * that falls outside the bounds of the destination texture.
620 * See error_check_subtexture_dimensions().
621 */
622
623 int srcY1 = srcY0 + height;
624 int srcX1 = srcX0 + width;
625 int dstX1 = dstX0 + width;
626 int dstY1 = dstY0 + height;
627
628 /* Account for the fact that in the system framebuffer, the origin is at
629 * the lower left.
630 */
631 bool mirror_y = false;
632 if (_mesa_is_winsys_fbo(ctx->ReadBuffer)) {
633 GLint tmp = src_rb->Height - srcY0;
634 srcY0 = src_rb->Height - srcY1;
635 srcY1 = tmp;
636 mirror_y = true;
637 }
638
639 /* Account for face selection and texture view MinLayer */
640 int dst_slice = slice + dst_image->TexObject->MinLayer + dst_image->Face;
641 int dst_level = dst_image->Level + dst_image->TexObject->MinLevel;
642
643 brw_blorp_blit_miptrees(brw,
644 src_mt, src_irb->mt_level, src_irb->mt_layer,
645 src_rb->Format, blorp_get_texture_swizzle(src_irb),
646 dst_mt, dst_level, dst_slice,
647 dst_image->TexFormat,
648 srcX0, srcY0, srcX1, srcY1,
649 dstX0, dstY0, dstX1, dstY1,
650 GL_NEAREST, false, mirror_y,
651 false, false);
652
653 /* If we're copying to a packed depth stencil texture and the source
654 * framebuffer has separate stencil, we need to also copy the stencil data
655 * over.
656 */
657 src_rb = ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
658 if (_mesa_get_format_bits(dst_image->TexFormat, GL_STENCIL_BITS) > 0 &&
659 src_rb != NULL) {
660 src_irb = intel_renderbuffer(src_rb);
661 src_mt = src_irb->mt;
662
663 if (src_mt->stencil_mt)
664 src_mt = src_mt->stencil_mt;
665 if (dst_mt->stencil_mt)
666 dst_mt = dst_mt->stencil_mt;
667
668 if (src_mt != dst_mt) {
669 brw_blorp_blit_miptrees(brw,
670 src_mt, src_irb->mt_level, src_irb->mt_layer,
671 src_mt->format,
672 blorp_get_texture_swizzle(src_irb),
673 dst_mt, dst_level, dst_slice,
674 dst_mt->format,
675 srcX0, srcY0, srcX1, srcY1,
676 dstX0, dstY0, dstX1, dstY1,
677 GL_NEAREST, false, mirror_y,
678 false, false);
679 }
680 }
681
682 return true;
683 }
684
685
686 GLbitfield
687 brw_blorp_framebuffer(struct brw_context *brw,
688 struct gl_framebuffer *readFb,
689 struct gl_framebuffer *drawFb,
690 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
691 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
692 GLbitfield mask, GLenum filter)
693 {
694 static GLbitfield buffer_bits[] = {
695 GL_COLOR_BUFFER_BIT,
696 GL_DEPTH_BUFFER_BIT,
697 GL_STENCIL_BUFFER_BIT,
698 };
699
700 for (unsigned int i = 0; i < ARRAY_SIZE(buffer_bits); ++i) {
701 if ((mask & buffer_bits[i]) &&
702 try_blorp_blit(brw, readFb, drawFb,
703 srcX0, srcY0, srcX1, srcY1,
704 dstX0, dstY0, dstX1, dstY1,
705 filter, buffer_bits[i])) {
706 mask &= ~buffer_bits[i];
707 }
708 }
709
710 return mask;
711 }
712
713 static bool
714 set_write_disables(const struct intel_renderbuffer *irb,
715 const GLubyte *color_mask, bool *color_write_disable)
716 {
717 /* Format information in the renderbuffer represents the requirements
718 * given by the client. There are cases where the backing miptree uses,
719 * for example, RGBA to represent RGBX. Since the client is only expecting
720 * RGB we can treat alpha as not used and write whatever we like into it.
721 */
722 const GLenum base_format = irb->Base.Base._BaseFormat;
723 const int components = _mesa_base_format_component_count(base_format);
724 bool disables = false;
725
726 assert(components > 0);
727
728 for (int i = 0; i < components; i++) {
729 color_write_disable[i] = !color_mask[i];
730 disables = disables || !color_mask[i];
731 }
732
733 return disables;
734 }
735
736 static unsigned
737 irb_logical_mt_layer(struct intel_renderbuffer *irb)
738 {
739 return physical_to_logical_layer(irb->mt, irb->mt_layer);
740 }
741
742 static bool
743 do_single_blorp_clear(struct brw_context *brw, struct gl_framebuffer *fb,
744 struct gl_renderbuffer *rb, unsigned buf,
745 bool partial_clear, bool encode_srgb)
746 {
747 struct gl_context *ctx = &brw->ctx;
748 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
749 mesa_format format = irb->mt->format;
750 uint32_t x0, x1, y0, y1;
751
752 if (!encode_srgb && _mesa_get_format_color_encoding(format) == GL_SRGB)
753 format = _mesa_get_srgb_format_linear(format);
754
755 x0 = fb->_Xmin;
756 x1 = fb->_Xmax;
757 if (rb->Name != 0) {
758 y0 = fb->_Ymin;
759 y1 = fb->_Ymax;
760 } else {
761 y0 = rb->Height - fb->_Ymax;
762 y1 = rb->Height - fb->_Ymin;
763 }
764
765 /* If the clear region is empty, just return. */
766 if (x0 == x1 || y0 == y1)
767 return true;
768
769 bool can_fast_clear = !partial_clear;
770
771 bool color_write_disable[4] = { false, false, false, false };
772 if (set_write_disables(irb, ctx->Color.ColorMask[buf], color_write_disable))
773 can_fast_clear = false;
774
775 if (irb->mt->aux_disable & INTEL_AUX_DISABLE_CCS ||
776 !brw_is_color_fast_clear_compatible(brw, irb->mt, &ctx->Color.ClearColor))
777 can_fast_clear = false;
778
779 const unsigned logical_layer = irb_logical_mt_layer(irb);
780 const enum intel_fast_clear_state fast_clear_state =
781 intel_miptree_get_fast_clear_state(irb->mt, irb->mt_level,
782 logical_layer);
783
784 /* Surface state can only record one fast clear color value. Therefore
785 * unless different levels/layers agree on the color it can be used to
786 * represent only single level/layer. Here it will be reserved for the
787 * first slice (level 0, layer 0).
788 */
789 if (irb->layer_count > 1 || irb->mt_level || irb->mt_layer)
790 can_fast_clear = false;
791
792 if (can_fast_clear) {
793 union isl_color_value clear_color =
794 brw_meta_convert_fast_clear_color(brw, irb->mt,
795 &ctx->Color.ClearColor);
796
797 /* If the buffer is already in INTEL_FAST_CLEAR_STATE_CLEAR, the clear
798 * is redundant and can be skipped.
799 */
800 if (fast_clear_state == INTEL_FAST_CLEAR_STATE_CLEAR &&
801 memcmp(&irb->mt->fast_clear_color,
802 &clear_color, sizeof(clear_color)) == 0)
803 return true;
804
805 irb->mt->fast_clear_color = clear_color;
806
807 /* If the MCS buffer hasn't been allocated yet, we need to allocate
808 * it now.
809 */
810 if (!irb->mt->mcs_buf) {
811 assert(!intel_miptree_is_lossless_compressed(brw, irb->mt));
812 if (!intel_miptree_alloc_non_msrt_mcs(brw, irb->mt, false)) {
813 /* MCS allocation failed--probably this will only happen in
814 * out-of-memory conditions. But in any case, try to recover
815 * by falling back to a non-blorp clear technique.
816 */
817 return false;
818 }
819 }
820 }
821
822 const unsigned num_layers = fb->MaxNumLayers ? irb->layer_count : 1;
823
824 /* We can't setup the blorp_surf until we've allocated the MCS above */
825 struct isl_surf isl_tmp[2];
826 struct blorp_surf surf;
827 unsigned level = irb->mt_level;
828 blorp_surf_for_miptree(brw, &surf, irb->mt, true,
829 (1 << ISL_AUX_USAGE_MCS) |
830 (1 << ISL_AUX_USAGE_CCS_E) |
831 (1 << ISL_AUX_USAGE_CCS_D),
832 &level, logical_layer, num_layers, isl_tmp);
833
834 if (can_fast_clear) {
835 DBG("%s (fast) to mt %p level %d layers %d+%d\n", __FUNCTION__,
836 irb->mt, irb->mt_level, irb->mt_layer, num_layers);
837
838 /* Ivybrigde PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
839 *
840 * "Any transition from any value in {Clear, Render, Resolve} to a
841 * different value in {Clear, Render, Resolve} requires end of pipe
842 * synchronization."
843 *
844 * In other words, fast clear ops are not properly synchronized with
845 * other drawing. We need to use a PIPE_CONTROL to ensure that the
846 * contents of the previous draw hit the render target before we resolve
847 * and again afterwards to ensure that the resolve is complete before we
848 * do any more regular drawing.
849 */
850 brw_emit_pipe_control_flush(brw,
851 PIPE_CONTROL_RENDER_TARGET_FLUSH |
852 PIPE_CONTROL_CS_STALL);
853
854 struct blorp_batch batch;
855 blorp_batch_init(&brw->blorp, &batch, brw, 0);
856 blorp_fast_clear(&batch, &surf,
857 brw->render_target_format[format],
858 level, logical_layer, num_layers,
859 x0, y0, x1, y1);
860 blorp_batch_finish(&batch);
861
862 brw_emit_pipe_control_flush(brw,
863 PIPE_CONTROL_RENDER_TARGET_FLUSH |
864 PIPE_CONTROL_CS_STALL);
865
866 /* Now that the fast clear has occurred, put the buffer in
867 * INTEL_FAST_CLEAR_STATE_CLEAR so that we won't waste time doing
868 * redundant clears.
869 */
870 intel_miptree_set_fast_clear_state(brw, irb->mt, irb->mt_level,
871 logical_layer, num_layers,
872 INTEL_FAST_CLEAR_STATE_CLEAR);
873 } else {
874 DBG("%s (slow) to mt %p level %d layer %d+%d\n", __FUNCTION__,
875 irb->mt, irb->mt_level, irb->mt_layer, num_layers);
876
877 union isl_color_value clear_color;
878 memcpy(clear_color.f32, ctx->Color.ClearColor.f, sizeof(float) * 4);
879
880 struct blorp_batch batch;
881 blorp_batch_init(&brw->blorp, &batch, brw, 0);
882 blorp_clear(&batch, &surf,
883 brw->render_target_format[format],
884 ISL_SWIZZLE_IDENTITY,
885 level, irb_logical_mt_layer(irb), num_layers,
886 x0, y0, x1, y1,
887 clear_color, color_write_disable);
888 blorp_batch_finish(&batch);
889 }
890
891 return true;
892 }
893
894 bool
895 brw_blorp_clear_color(struct brw_context *brw, struct gl_framebuffer *fb,
896 GLbitfield mask, bool partial_clear, bool encode_srgb)
897 {
898 for (unsigned buf = 0; buf < fb->_NumColorDrawBuffers; buf++) {
899 struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[buf];
900 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
901
902 /* Only clear the buffers present in the provided mask */
903 if (((1 << fb->_ColorDrawBufferIndexes[buf]) & mask) == 0)
904 continue;
905
906 /* If this is an ES2 context or GL_ARB_ES2_compatibility is supported,
907 * the framebuffer can be complete with some attachments missing. In
908 * this case the _ColorDrawBuffers pointer will be NULL.
909 */
910 if (rb == NULL)
911 continue;
912
913 if (!do_single_blorp_clear(brw, fb, rb, buf, partial_clear,
914 encode_srgb)) {
915 return false;
916 }
917
918 irb->need_downsample = true;
919 }
920
921 return true;
922 }
923
924 void
925 brw_blorp_resolve_color(struct brw_context *brw, struct intel_mipmap_tree *mt,
926 unsigned level, unsigned layer)
927 {
928 DBG("%s to mt %p level %u layer %u\n", __FUNCTION__, mt, level, layer);
929
930 const mesa_format format = _mesa_get_srgb_format_linear(mt->format);
931
932 struct isl_surf isl_tmp[2];
933 struct blorp_surf surf;
934 blorp_surf_for_miptree(brw, &surf, mt, true,
935 (1 << ISL_AUX_USAGE_CCS_E) |
936 (1 << ISL_AUX_USAGE_CCS_D),
937 &level, layer, 1 /* num_layers */,
938 isl_tmp);
939
940 enum blorp_fast_clear_op resolve_op;
941 if (brw->gen >= 9) {
942 if (surf.aux_usage == ISL_AUX_USAGE_CCS_E)
943 resolve_op = BLORP_FAST_CLEAR_OP_RESOLVE_FULL;
944 else
945 resolve_op = BLORP_FAST_CLEAR_OP_RESOLVE_PARTIAL;
946 } else {
947 assert(surf.aux_usage == ISL_AUX_USAGE_CCS_D);
948 /* Broadwell and earlier do not have a partial resolve */
949 resolve_op = BLORP_FAST_CLEAR_OP_RESOLVE_FULL;
950 }
951
952 /* Ivybrigde PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
953 *
954 * "Any transition from any value in {Clear, Render, Resolve} to a
955 * different value in {Clear, Render, Resolve} requires end of pipe
956 * synchronization."
957 *
958 * In other words, fast clear ops are not properly synchronized with
959 * other drawing. We need to use a PIPE_CONTROL to ensure that the
960 * contents of the previous draw hit the render target before we resolve
961 * and again afterwards to ensure that the resolve is complete before we
962 * do any more regular drawing.
963 */
964 brw_emit_pipe_control_flush(brw,
965 PIPE_CONTROL_RENDER_TARGET_FLUSH |
966 PIPE_CONTROL_CS_STALL);
967
968
969 struct blorp_batch batch;
970 blorp_batch_init(&brw->blorp, &batch, brw, 0);
971 blorp_ccs_resolve(&batch, &surf, level, layer,
972 brw_blorp_to_isl_format(brw, format, true),
973 resolve_op);
974 blorp_batch_finish(&batch);
975
976 /* See comment above */
977 brw_emit_pipe_control_flush(brw,
978 PIPE_CONTROL_RENDER_TARGET_FLUSH |
979 PIPE_CONTROL_CS_STALL);
980 }
981
982 static void
983 gen6_blorp_hiz_exec(struct brw_context *brw, struct intel_mipmap_tree *mt,
984 unsigned int level, unsigned int layer, enum blorp_hiz_op op)
985 {
986 assert(intel_miptree_level_has_hiz(mt, level));
987
988 struct isl_surf isl_tmp[2];
989 struct blorp_surf surf;
990 blorp_surf_for_miptree(brw, &surf, mt, true, (1 << ISL_AUX_USAGE_HIZ),
991 &level, layer, 1, isl_tmp);
992
993 struct blorp_batch batch;
994 blorp_batch_init(&brw->blorp, &batch, brw, 0);
995 blorp_gen6_hiz_op(&batch, &surf, level, layer, op);
996 blorp_batch_finish(&batch);
997 }
998
999 /**
1000 * Perform a HiZ or depth resolve operation.
1001 *
1002 * For an overview of HiZ ops, see the following sections of the Sandy Bridge
1003 * PRM, Volume 1, Part 2:
1004 * - 7.5.3.1 Depth Buffer Clear
1005 * - 7.5.3.2 Depth Buffer Resolve
1006 * - 7.5.3.3 Hierarchical Depth Buffer Resolve
1007 */
1008 void
1009 intel_hiz_exec(struct brw_context *brw, struct intel_mipmap_tree *mt,
1010 unsigned int level, unsigned int start_layer,
1011 unsigned int num_layers, enum blorp_hiz_op op)
1012 {
1013 const char *opname = NULL;
1014
1015 switch (op) {
1016 case BLORP_HIZ_OP_DEPTH_RESOLVE:
1017 opname = "depth resolve";
1018 break;
1019 case BLORP_HIZ_OP_HIZ_RESOLVE:
1020 opname = "hiz ambiguate";
1021 break;
1022 case BLORP_HIZ_OP_DEPTH_CLEAR:
1023 opname = "depth clear";
1024 break;
1025 case BLORP_HIZ_OP_NONE:
1026 opname = "noop?";
1027 break;
1028 }
1029
1030 DBG("%s %s to mt %p level %d layers %d-%d\n",
1031 __func__, opname, mt, level, start_layer, start_layer + num_layers - 1);
1032
1033 if (brw->gen >= 8) {
1034 for (unsigned a = 0; a < num_layers; a++)
1035 gen8_hiz_exec(brw, mt, level, start_layer + a, op);
1036 } else {
1037 for (unsigned a = 0; a < num_layers; a++)
1038 gen6_blorp_hiz_exec(brw, mt, level, start_layer + a, op);
1039 }
1040 }