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