i965: Add blorp support for gen4-5
[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 do_blorp_blit(brw, buffer_bit, src_irb, MESA_FORMAT_NONE,
588 dst_irb, MESA_FORMAT_NONE, srcX0, srcY0,
589 srcX1, srcY1, dstX0, dstY0, dstX1, dstY1,
590 filter, mirror_x, mirror_y);
591 break;
592 case GL_STENCIL_BUFFER_BIT:
593 src_irb =
594 intel_renderbuffer(read_fb->Attachment[BUFFER_STENCIL].Renderbuffer);
595 dst_irb =
596 intel_renderbuffer(draw_fb->Attachment[BUFFER_STENCIL].Renderbuffer);
597 do_blorp_blit(brw, buffer_bit, src_irb, MESA_FORMAT_NONE,
598 dst_irb, MESA_FORMAT_NONE, srcX0, srcY0,
599 srcX1, srcY1, dstX0, dstY0, dstX1, dstY1,
600 filter, mirror_x, mirror_y);
601 break;
602 default:
603 unreachable("not reached");
604 }
605
606 return true;
607 }
608
609 bool
610 brw_blorp_copytexsubimage(struct brw_context *brw,
611 struct gl_renderbuffer *src_rb,
612 struct gl_texture_image *dst_image,
613 int slice,
614 int srcX0, int srcY0,
615 int dstX0, int dstY0,
616 int width, int height)
617 {
618 struct gl_context *ctx = &brw->ctx;
619 struct intel_renderbuffer *src_irb = intel_renderbuffer(src_rb);
620 struct intel_texture_image *intel_image = intel_texture_image(dst_image);
621
622 /* No pixel transfer operations (zoom, bias, mapping), just a blit */
623 if (brw->ctx._ImageTransferState)
624 return false;
625
626 /* Sync up the state of window system buffers. We need to do this before
627 * we go looking at the src renderbuffer's miptree.
628 */
629 intel_prepare_render(brw);
630
631 struct intel_mipmap_tree *src_mt = src_irb->mt;
632 struct intel_mipmap_tree *dst_mt = intel_image->mt;
633
634 /* There is support for only up to eight samples. */
635 if (src_mt->num_samples > 8 || dst_mt->num_samples > 8)
636 return false;
637
638 /* BLORP is only supported from Gen6 onwards. */
639 if (brw->gen < 6)
640 return false;
641
642 if (_mesa_get_format_base_format(src_rb->Format) !=
643 _mesa_get_format_base_format(dst_image->TexFormat)) {
644 return false;
645 }
646
647 /* We can't handle format conversions between Z24 and other formats since
648 * we have to lie about the surface format. See the comments in
649 * brw_blorp_surface_info::set().
650 */
651 if ((src_mt->format == MESA_FORMAT_Z24_UNORM_X8_UINT) !=
652 (dst_mt->format == MESA_FORMAT_Z24_UNORM_X8_UINT)) {
653 return false;
654 }
655
656 if (!brw->format_supported_as_render_target[dst_image->TexFormat])
657 return false;
658
659 /* Source clipping shouldn't be necessary, since copytexsubimage (in
660 * src/mesa/main/teximage.c) calls _mesa_clip_copytexsubimage() which
661 * takes care of it.
662 *
663 * Destination clipping shouldn't be necessary since the restrictions on
664 * glCopyTexSubImage prevent the user from specifying a destination rectangle
665 * that falls outside the bounds of the destination texture.
666 * See error_check_subtexture_dimensions().
667 */
668
669 int srcY1 = srcY0 + height;
670 int srcX1 = srcX0 + width;
671 int dstX1 = dstX0 + width;
672 int dstY1 = dstY0 + height;
673
674 /* Account for the fact that in the system framebuffer, the origin is at
675 * the lower left.
676 */
677 bool mirror_y = false;
678 if (_mesa_is_winsys_fbo(ctx->ReadBuffer)) {
679 GLint tmp = src_rb->Height - srcY0;
680 srcY0 = src_rb->Height - srcY1;
681 srcY1 = tmp;
682 mirror_y = true;
683 }
684
685 /* Account for face selection and texture view MinLayer */
686 int dst_slice = slice + dst_image->TexObject->MinLayer + dst_image->Face;
687 int dst_level = dst_image->Level + dst_image->TexObject->MinLevel;
688
689 brw_blorp_blit_miptrees(brw,
690 src_mt, src_irb->mt_level, src_irb->mt_layer,
691 src_rb->Format, blorp_get_texture_swizzle(src_irb),
692 dst_mt, dst_level, dst_slice,
693 dst_image->TexFormat,
694 srcX0, srcY0, srcX1, srcY1,
695 dstX0, dstY0, dstX1, dstY1,
696 GL_NEAREST, false, mirror_y,
697 false, false);
698
699 /* If we're copying to a packed depth stencil texture and the source
700 * framebuffer has separate stencil, we need to also copy the stencil data
701 * over.
702 */
703 src_rb = ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
704 if (_mesa_get_format_bits(dst_image->TexFormat, GL_STENCIL_BITS) > 0 &&
705 src_rb != NULL) {
706 src_irb = intel_renderbuffer(src_rb);
707 src_mt = src_irb->mt;
708
709 if (src_mt->stencil_mt)
710 src_mt = src_mt->stencil_mt;
711 if (dst_mt->stencil_mt)
712 dst_mt = dst_mt->stencil_mt;
713
714 if (src_mt != dst_mt) {
715 brw_blorp_blit_miptrees(brw,
716 src_mt, src_irb->mt_level, src_irb->mt_layer,
717 src_mt->format,
718 blorp_get_texture_swizzle(src_irb),
719 dst_mt, dst_level, dst_slice,
720 dst_mt->format,
721 srcX0, srcY0, srcX1, srcY1,
722 dstX0, dstY0, dstX1, dstY1,
723 GL_NEAREST, false, mirror_y,
724 false, false);
725 }
726 }
727
728 return true;
729 }
730
731
732 GLbitfield
733 brw_blorp_framebuffer(struct brw_context *brw,
734 struct gl_framebuffer *readFb,
735 struct gl_framebuffer *drawFb,
736 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
737 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
738 GLbitfield mask, GLenum filter)
739 {
740 /* BLORP is not supported before Gen6. */
741 if (brw->gen < 6)
742 return mask;
743
744 static GLbitfield buffer_bits[] = {
745 GL_COLOR_BUFFER_BIT,
746 GL_DEPTH_BUFFER_BIT,
747 GL_STENCIL_BUFFER_BIT,
748 };
749
750 for (unsigned int i = 0; i < ARRAY_SIZE(buffer_bits); ++i) {
751 if ((mask & buffer_bits[i]) &&
752 try_blorp_blit(brw, readFb, drawFb,
753 srcX0, srcY0, srcX1, srcY1,
754 dstX0, dstY0, dstX1, dstY1,
755 filter, buffer_bits[i])) {
756 mask &= ~buffer_bits[i];
757 }
758 }
759
760 return mask;
761 }
762
763 static bool
764 set_write_disables(const struct intel_renderbuffer *irb,
765 const GLubyte *color_mask, bool *color_write_disable)
766 {
767 /* Format information in the renderbuffer represents the requirements
768 * given by the client. There are cases where the backing miptree uses,
769 * for example, RGBA to represent RGBX. Since the client is only expecting
770 * RGB we can treat alpha as not used and write whatever we like into it.
771 */
772 const GLenum base_format = irb->Base.Base._BaseFormat;
773 const int components = _mesa_base_format_component_count(base_format);
774 bool disables = false;
775
776 assert(components > 0);
777
778 for (int i = 0; i < components; i++) {
779 color_write_disable[i] = !color_mask[i];
780 disables = disables || !color_mask[i];
781 }
782
783 return disables;
784 }
785
786 static unsigned
787 irb_logical_mt_layer(struct intel_renderbuffer *irb)
788 {
789 return physical_to_logical_layer(irb->mt, irb->mt_layer);
790 }
791
792 static bool
793 do_single_blorp_clear(struct brw_context *brw, struct gl_framebuffer *fb,
794 struct gl_renderbuffer *rb, unsigned buf,
795 bool partial_clear, bool encode_srgb)
796 {
797 struct gl_context *ctx = &brw->ctx;
798 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
799 mesa_format format = irb->mt->format;
800 uint32_t x0, x1, y0, y1;
801
802 if (!encode_srgb && _mesa_get_format_color_encoding(format) == GL_SRGB)
803 format = _mesa_get_srgb_format_linear(format);
804
805 x0 = fb->_Xmin;
806 x1 = fb->_Xmax;
807 if (rb->Name != 0) {
808 y0 = fb->_Ymin;
809 y1 = fb->_Ymax;
810 } else {
811 y0 = rb->Height - fb->_Ymax;
812 y1 = rb->Height - fb->_Ymin;
813 }
814
815 /* If the clear region is empty, just return. */
816 if (x0 == x1 || y0 == y1)
817 return true;
818
819 bool can_fast_clear = !partial_clear;
820
821 bool color_write_disable[4] = { false, false, false, false };
822 if (set_write_disables(irb, ctx->Color.ColorMask[buf], color_write_disable))
823 can_fast_clear = false;
824
825 if (irb->mt->aux_disable & INTEL_AUX_DISABLE_CCS ||
826 !brw_is_color_fast_clear_compatible(brw, irb->mt, &ctx->Color.ClearColor))
827 can_fast_clear = false;
828
829 const unsigned logical_layer = irb_logical_mt_layer(irb);
830 const enum intel_fast_clear_state fast_clear_state =
831 intel_miptree_get_fast_clear_state(irb->mt, irb->mt_level,
832 logical_layer);
833
834 /* Surface state can only record one fast clear color value. Therefore
835 * unless different levels/layers agree on the color it can be used to
836 * represent only single level/layer. Here it will be reserved for the
837 * first slice (level 0, layer 0).
838 */
839 if (irb->layer_count > 1 || irb->mt_level || irb->mt_layer)
840 can_fast_clear = false;
841
842 if (can_fast_clear) {
843 union gl_color_union override_color =
844 brw_meta_convert_fast_clear_color(brw, irb->mt,
845 &ctx->Color.ClearColor);
846
847 /* Record the clear color in the miptree so that it will be
848 * programmed in SURFACE_STATE by later rendering and resolve
849 * operations.
850 */
851 const bool color_updated = brw_meta_set_fast_clear_color(
852 brw, &irb->mt->gen9_fast_clear_color,
853 &override_color);
854
855 /* If the buffer is already in INTEL_FAST_CLEAR_STATE_CLEAR, the clear
856 * is redundant and can be skipped.
857 */
858 if (!color_updated && fast_clear_state == INTEL_FAST_CLEAR_STATE_CLEAR)
859 return true;
860
861 /* If the MCS buffer hasn't been allocated yet, we need to allocate
862 * it now.
863 */
864 if (!irb->mt->mcs_buf) {
865 assert(!intel_miptree_is_lossless_compressed(brw, irb->mt));
866 if (!intel_miptree_alloc_non_msrt_mcs(brw, irb->mt, false)) {
867 /* MCS allocation failed--probably this will only happen in
868 * out-of-memory conditions. But in any case, try to recover
869 * by falling back to a non-blorp clear technique.
870 */
871 return false;
872 }
873 }
874 }
875
876 const unsigned num_layers = fb->MaxNumLayers ? irb->layer_count : 1;
877
878 /* We can't setup the blorp_surf until we've allocated the MCS above */
879 struct isl_surf isl_tmp[2];
880 struct blorp_surf surf;
881 unsigned level = irb->mt_level;
882 blorp_surf_for_miptree(brw, &surf, irb->mt, true,
883 (1 << ISL_AUX_USAGE_MCS) |
884 (1 << ISL_AUX_USAGE_CCS_E) |
885 (1 << ISL_AUX_USAGE_CCS_D),
886 &level, logical_layer, num_layers, isl_tmp);
887
888 if (can_fast_clear) {
889 DBG("%s (fast) to mt %p level %d layers %d+%d\n", __FUNCTION__,
890 irb->mt, irb->mt_level, irb->mt_layer, num_layers);
891
892 /* Ivybrigde PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
893 *
894 * "Any transition from any value in {Clear, Render, Resolve} to a
895 * different value in {Clear, Render, Resolve} requires end of pipe
896 * synchronization."
897 *
898 * In other words, fast clear ops are not properly synchronized with
899 * other drawing. We need to use a PIPE_CONTROL to ensure that the
900 * contents of the previous draw hit the render target before we resolve
901 * and again afterwards to ensure that the resolve is complete before we
902 * do any more regular drawing.
903 */
904 brw_emit_pipe_control_flush(brw,
905 PIPE_CONTROL_RENDER_TARGET_FLUSH |
906 PIPE_CONTROL_CS_STALL);
907
908 struct blorp_batch batch;
909 blorp_batch_init(&brw->blorp, &batch, brw, 0);
910 blorp_fast_clear(&batch, &surf,
911 (enum isl_format)brw->render_target_format[format],
912 level, logical_layer, num_layers,
913 x0, y0, x1, y1);
914 blorp_batch_finish(&batch);
915
916 brw_emit_pipe_control_flush(brw,
917 PIPE_CONTROL_RENDER_TARGET_FLUSH |
918 PIPE_CONTROL_CS_STALL);
919
920 /* Now that the fast clear has occurred, put the buffer in
921 * INTEL_FAST_CLEAR_STATE_CLEAR so that we won't waste time doing
922 * redundant clears.
923 */
924 intel_miptree_set_fast_clear_state(brw, irb->mt, irb->mt_level,
925 logical_layer, num_layers,
926 INTEL_FAST_CLEAR_STATE_CLEAR);
927 } else {
928 DBG("%s (slow) to mt %p level %d layer %d+%d\n", __FUNCTION__,
929 irb->mt, irb->mt_level, irb->mt_layer, num_layers);
930
931 union isl_color_value clear_color;
932 memcpy(clear_color.f32, ctx->Color.ClearColor.f, sizeof(float) * 4);
933
934 struct blorp_batch batch;
935 blorp_batch_init(&brw->blorp, &batch, brw, 0);
936 blorp_clear(&batch, &surf,
937 (enum isl_format)brw->render_target_format[format],
938 ISL_SWIZZLE_IDENTITY,
939 level, irb_logical_mt_layer(irb), num_layers,
940 x0, y0, x1, y1,
941 clear_color, color_write_disable);
942 blorp_batch_finish(&batch);
943 }
944
945 return true;
946 }
947
948 bool
949 brw_blorp_clear_color(struct brw_context *brw, struct gl_framebuffer *fb,
950 GLbitfield mask, bool partial_clear, bool encode_srgb)
951 {
952 for (unsigned buf = 0; buf < fb->_NumColorDrawBuffers; buf++) {
953 struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[buf];
954 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
955
956 /* Only clear the buffers present in the provided mask */
957 if (((1 << fb->_ColorDrawBufferIndexes[buf]) & mask) == 0)
958 continue;
959
960 /* If this is an ES2 context or GL_ARB_ES2_compatibility is supported,
961 * the framebuffer can be complete with some attachments missing. In
962 * this case the _ColorDrawBuffers pointer will be NULL.
963 */
964 if (rb == NULL)
965 continue;
966
967 if (!do_single_blorp_clear(brw, fb, rb, buf, partial_clear,
968 encode_srgb)) {
969 return false;
970 }
971
972 irb->need_downsample = true;
973 }
974
975 return true;
976 }
977
978 void
979 brw_blorp_resolve_color(struct brw_context *brw, struct intel_mipmap_tree *mt,
980 unsigned level, unsigned layer)
981 {
982 DBG("%s to mt %p level %u layer %u\n", __FUNCTION__, mt, level, layer);
983
984 const mesa_format format = _mesa_get_srgb_format_linear(mt->format);
985
986 struct isl_surf isl_tmp[2];
987 struct blorp_surf surf;
988 blorp_surf_for_miptree(brw, &surf, mt, true,
989 (1 << ISL_AUX_USAGE_CCS_E) |
990 (1 << ISL_AUX_USAGE_CCS_D),
991 &level, layer, 1 /* num_layers */,
992 isl_tmp);
993
994 enum blorp_fast_clear_op resolve_op;
995 if (brw->gen >= 9) {
996 if (surf.aux_usage == ISL_AUX_USAGE_CCS_E)
997 resolve_op = BLORP_FAST_CLEAR_OP_RESOLVE_FULL;
998 else
999 resolve_op = BLORP_FAST_CLEAR_OP_RESOLVE_PARTIAL;
1000 } else {
1001 assert(surf.aux_usage == ISL_AUX_USAGE_CCS_D);
1002 /* Broadwell and earlier do not have a partial resolve */
1003 resolve_op = BLORP_FAST_CLEAR_OP_RESOLVE_FULL;
1004 }
1005
1006 /* Ivybrigde PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
1007 *
1008 * "Any transition from any value in {Clear, Render, Resolve} to a
1009 * different value in {Clear, Render, Resolve} requires end of pipe
1010 * synchronization."
1011 *
1012 * In other words, fast clear ops are not properly synchronized with
1013 * other drawing. We need to use a PIPE_CONTROL to ensure that the
1014 * contents of the previous draw hit the render target before we resolve
1015 * and again afterwards to ensure that the resolve is complete before we
1016 * do any more regular drawing.
1017 */
1018 brw_emit_pipe_control_flush(brw,
1019 PIPE_CONTROL_RENDER_TARGET_FLUSH |
1020 PIPE_CONTROL_CS_STALL);
1021
1022
1023 struct blorp_batch batch;
1024 blorp_batch_init(&brw->blorp, &batch, brw, 0);
1025 blorp_ccs_resolve(&batch, &surf, level, layer,
1026 brw_blorp_to_isl_format(brw, format, true),
1027 resolve_op);
1028 blorp_batch_finish(&batch);
1029
1030 /* See comment above */
1031 brw_emit_pipe_control_flush(brw,
1032 PIPE_CONTROL_RENDER_TARGET_FLUSH |
1033 PIPE_CONTROL_CS_STALL);
1034 }
1035
1036 static void
1037 gen6_blorp_hiz_exec(struct brw_context *brw, struct intel_mipmap_tree *mt,
1038 unsigned int level, unsigned int layer, enum blorp_hiz_op op)
1039 {
1040 assert(intel_miptree_level_has_hiz(mt, level));
1041
1042 struct isl_surf isl_tmp[2];
1043 struct blorp_surf surf;
1044 blorp_surf_for_miptree(brw, &surf, mt, true, (1 << ISL_AUX_USAGE_HIZ),
1045 &level, layer, 1, isl_tmp);
1046
1047 struct blorp_batch batch;
1048 blorp_batch_init(&brw->blorp, &batch, brw, 0);
1049 blorp_gen6_hiz_op(&batch, &surf, level, layer, op);
1050 blorp_batch_finish(&batch);
1051 }
1052
1053 /**
1054 * Perform a HiZ or depth resolve operation.
1055 *
1056 * For an overview of HiZ ops, see the following sections of the Sandy Bridge
1057 * PRM, Volume 1, Part 2:
1058 * - 7.5.3.1 Depth Buffer Clear
1059 * - 7.5.3.2 Depth Buffer Resolve
1060 * - 7.5.3.3 Hierarchical Depth Buffer Resolve
1061 */
1062 void
1063 intel_hiz_exec(struct brw_context *brw, struct intel_mipmap_tree *mt,
1064 unsigned int level, unsigned int layer, enum blorp_hiz_op op)
1065 {
1066 const char *opname = NULL;
1067
1068 switch (op) {
1069 case BLORP_HIZ_OP_DEPTH_RESOLVE:
1070 opname = "depth resolve";
1071 break;
1072 case BLORP_HIZ_OP_HIZ_RESOLVE:
1073 opname = "hiz ambiguate";
1074 break;
1075 case BLORP_HIZ_OP_DEPTH_CLEAR:
1076 opname = "depth clear";
1077 break;
1078 case BLORP_HIZ_OP_NONE:
1079 opname = "noop?";
1080 break;
1081 }
1082
1083 DBG("%s %s to mt %p level %d layer %d\n",
1084 __func__, opname, mt, level, layer);
1085
1086 if (brw->gen >= 8) {
1087 gen8_hiz_exec(brw, mt, level, layer, op);
1088 } else {
1089 gen6_blorp_hiz_exec(brw, mt, level, layer, op);
1090 }
1091 }