Added few more stubs so that control reaches to DestroyDevice().
[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/bufferobj.h"
28 #include "main/enums.h"
29 #include "main/fbobject.h"
30 #include "main/image.h"
31 #include "main/renderbuffer.h"
32 #include "main/glformats.h"
33
34 #include "brw_blorp.h"
35 #include "brw_context.h"
36 #include "brw_defines.h"
37 #include "brw_meta_util.h"
38 #include "brw_state.h"
39 #include "intel_buffer_objects.h"
40 #include "intel_fbo.h"
41 #include "dev/gen_debug.h"
42
43 #define FILE_DEBUG_FLAG DEBUG_BLORP
44
45 static bool
46 brw_blorp_lookup_shader(struct blorp_batch *batch,
47 const void *key, uint32_t key_size,
48 uint32_t *kernel_out, void *prog_data_out)
49 {
50 struct brw_context *brw = batch->driver_batch;
51 return brw_search_cache(&brw->cache, BRW_CACHE_BLORP_PROG, key, key_size,
52 kernel_out, prog_data_out, true);
53 }
54
55 static bool
56 brw_blorp_upload_shader(struct blorp_batch *batch, uint32_t stage,
57 const void *key, uint32_t key_size,
58 const void *kernel, uint32_t kernel_size,
59 const struct brw_stage_prog_data *prog_data,
60 uint32_t prog_data_size,
61 uint32_t *kernel_out, void *prog_data_out)
62 {
63 struct brw_context *brw = batch->driver_batch;
64 brw_upload_cache(&brw->cache, BRW_CACHE_BLORP_PROG, key, key_size,
65 kernel, kernel_size, prog_data, prog_data_size,
66 kernel_out, prog_data_out);
67 return true;
68 }
69
70 void
71 brw_blorp_init(struct brw_context *brw)
72 {
73 const struct gen_device_info *devinfo = &brw->screen->devinfo;
74
75 blorp_init(&brw->blorp, brw, &brw->isl_dev);
76
77 brw->blorp.compiler = brw->screen->compiler;
78
79 switch (devinfo->gen) {
80 case 4:
81 if (devinfo->is_g4x) {
82 brw->blorp.exec = gen45_blorp_exec;
83 } else {
84 brw->blorp.exec = gen4_blorp_exec;
85 }
86 break;
87 case 5:
88 brw->blorp.exec = gen5_blorp_exec;
89 break;
90 case 6:
91 brw->blorp.exec = gen6_blorp_exec;
92 break;
93 case 7:
94 if (devinfo->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.exec = gen8_blorp_exec;
102 break;
103 case 9:
104 brw->blorp.exec = gen9_blorp_exec;
105 break;
106 case 10:
107 brw->blorp.exec = gen10_blorp_exec;
108 break;
109 case 11:
110 brw->blorp.exec = gen11_blorp_exec;
111 break;
112
113 default:
114 unreachable("Invalid gen");
115 }
116
117 brw->blorp.lookup_shader = brw_blorp_lookup_shader;
118 brw->blorp.upload_shader = brw_blorp_upload_shader;
119 }
120
121 static void
122 blorp_surf_for_miptree(struct brw_context *brw,
123 struct blorp_surf *surf,
124 const struct intel_mipmap_tree *mt,
125 enum isl_aux_usage aux_usage,
126 bool is_render_target,
127 unsigned *level,
128 unsigned start_layer, unsigned num_layers)
129 {
130 const struct gen_device_info *devinfo = &brw->screen->devinfo;
131
132 if (mt->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY) {
133 const unsigned num_samples = mt->surf.samples;
134 for (unsigned i = 0; i < num_layers; i++) {
135 for (unsigned s = 0; s < num_samples; s++) {
136 const unsigned phys_layer = (start_layer + i) * num_samples + s;
137 intel_miptree_check_level_layer(mt, *level, phys_layer);
138 }
139 }
140 } else {
141 for (unsigned i = 0; i < num_layers; i++)
142 intel_miptree_check_level_layer(mt, *level, start_layer + i);
143 }
144
145 *surf = (struct blorp_surf) {
146 .surf = &mt->surf,
147 .addr = (struct blorp_address) {
148 .buffer = mt->bo,
149 .offset = mt->offset,
150 .reloc_flags = is_render_target ? EXEC_OBJECT_WRITE : 0,
151 .mocs = brw_get_bo_mocs(devinfo, mt->bo),
152 },
153 .aux_usage = aux_usage,
154 .tile_x_sa = mt->level[*level].level_x,
155 .tile_y_sa = mt->level[*level].level_y,
156 };
157
158 if (surf->aux_usage == ISL_AUX_USAGE_HIZ &&
159 !intel_miptree_level_has_hiz(mt, *level))
160 surf->aux_usage = ISL_AUX_USAGE_NONE;
161
162 if (surf->aux_usage != ISL_AUX_USAGE_NONE) {
163 /* We only really need a clear color if we also have an auxiliary
164 * surface. Without one, it does nothing.
165 */
166 surf->clear_color =
167 intel_miptree_get_clear_color(devinfo, mt, mt->surf.format,
168 !is_render_target, (struct brw_bo **)
169 &surf->clear_color_addr.buffer,
170 &surf->clear_color_addr.offset);
171
172 surf->aux_surf = &mt->aux_buf->surf;
173 surf->aux_addr = (struct blorp_address) {
174 .reloc_flags = is_render_target ? EXEC_OBJECT_WRITE : 0,
175 .mocs = surf->addr.mocs,
176 };
177
178 surf->aux_addr.buffer = mt->aux_buf->bo;
179 surf->aux_addr.offset = mt->aux_buf->offset;
180 } else {
181 surf->aux_addr = (struct blorp_address) {
182 .buffer = NULL,
183 };
184 memset(&surf->clear_color, 0, sizeof(surf->clear_color));
185 }
186 assert((surf->aux_usage == ISL_AUX_USAGE_NONE) ==
187 (surf->aux_addr.buffer == NULL));
188
189 if (!is_render_target && brw->screen->devinfo.gen == 9)
190 gen9_apply_single_tex_astc5x5_wa(brw, mt->format, surf->aux_usage);
191
192 /* ISL wants real levels, not offset ones. */
193 *level -= mt->first_level;
194 }
195
196 static bool
197 brw_blorp_supports_dst_format(struct brw_context *brw, mesa_format format)
198 {
199 /* If it's renderable, it's definitely supported. */
200 if (brw->mesa_format_supports_render[format])
201 return true;
202
203 /* BLORP can't compress anything */
204 if (_mesa_is_format_compressed(format))
205 return false;
206
207 /* No exotic formats such as GL_LUMINANCE_ALPHA */
208 if (_mesa_get_format_bits(format, GL_RED_BITS) == 0 &&
209 _mesa_get_format_bits(format, GL_DEPTH_BITS) == 0 &&
210 _mesa_get_format_bits(format, GL_STENCIL_BITS) == 0)
211 return false;
212
213 return true;
214 }
215
216 static enum isl_format
217 brw_blorp_to_isl_format(struct brw_context *brw, mesa_format format,
218 bool is_render_target)
219 {
220 switch (format) {
221 case MESA_FORMAT_NONE:
222 return ISL_FORMAT_UNSUPPORTED;
223 case MESA_FORMAT_S_UINT8:
224 return ISL_FORMAT_R8_UINT;
225 case MESA_FORMAT_Z24_UNORM_X8_UINT:
226 case MESA_FORMAT_Z24_UNORM_S8_UINT:
227 return ISL_FORMAT_R24_UNORM_X8_TYPELESS;
228 case MESA_FORMAT_Z_FLOAT32:
229 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
230 return ISL_FORMAT_R32_FLOAT;
231 case MESA_FORMAT_Z_UNORM16:
232 return ISL_FORMAT_R16_UNORM;
233 default:
234 if (is_render_target) {
235 assert(brw_blorp_supports_dst_format(brw, format));
236 if (brw->mesa_format_supports_render[format]) {
237 return brw->mesa_to_isl_render_format[format];
238 } else {
239 return brw_isl_format_for_mesa_format(format);
240 }
241 } else {
242 /* Some destinations (is_render_target == true) are supported by
243 * blorp even though we technically can't render to them.
244 */
245 return brw_isl_format_for_mesa_format(format);
246 }
247 }
248 }
249
250 /**
251 * Convert an swizzle enumeration (i.e. SWIZZLE_X) to one of the Gen7.5+
252 * "Shader Channel Select" enumerations (i.e. HSW_SCS_RED). The mappings are
253 *
254 * SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_ZERO, SWIZZLE_ONE
255 * 0 1 2 3 4 5
256 * 4 5 6 7 0 1
257 * SCS_RED, SCS_GREEN, SCS_BLUE, SCS_ALPHA, SCS_ZERO, SCS_ONE
258 *
259 * which is simply adding 4 then modding by 8 (or anding with 7).
260 *
261 * We then may need to apply workarounds for textureGather hardware bugs.
262 */
263 static enum isl_channel_select
264 swizzle_to_scs(GLenum swizzle)
265 {
266 return (enum isl_channel_select)((swizzle + 4) & 7);
267 }
268
269 /**
270 * Note: if the src (or dst) is a 2D multisample array texture on Gen7+ using
271 * INTEL_MSAA_LAYOUT_UMS or INTEL_MSAA_LAYOUT_CMS, src_layer (dst_layer) is
272 * the physical layer holding sample 0. So, for example, if
273 * src_mt->surf.samples == 4, then logical layer n corresponds to src_layer ==
274 * 4*n.
275 */
276 void
277 brw_blorp_blit_miptrees(struct brw_context *brw,
278 struct intel_mipmap_tree *src_mt,
279 unsigned src_level, unsigned src_layer,
280 mesa_format src_format, int src_swizzle,
281 struct intel_mipmap_tree *dst_mt,
282 unsigned dst_level, unsigned dst_layer,
283 mesa_format dst_format,
284 float src_x0, float src_y0,
285 float src_x1, float src_y1,
286 float dst_x0, float dst_y0,
287 float dst_x1, float dst_y1,
288 GLenum gl_filter, bool mirror_x, bool mirror_y,
289 bool decode_srgb, bool encode_srgb)
290 {
291 const struct gen_device_info *devinfo = &brw->screen->devinfo;
292
293 DBG("%s from %dx %s mt %p %d %d (%f,%f) (%f,%f) "
294 "to %dx %s mt %p %d %d (%f,%f) (%f,%f) (flip %d,%d)\n",
295 __func__,
296 src_mt->surf.samples, _mesa_get_format_name(src_mt->format), src_mt,
297 src_level, src_layer, src_x0, src_y0, src_x1, src_y1,
298 dst_mt->surf.samples, _mesa_get_format_name(dst_mt->format), dst_mt,
299 dst_level, dst_layer, dst_x0, dst_y0, dst_x1, dst_y1,
300 mirror_x, mirror_y);
301
302 if (src_format == MESA_FORMAT_NONE)
303 src_format = src_mt->format;
304
305 if (dst_format == MESA_FORMAT_NONE)
306 dst_format = dst_mt->format;
307
308 if (!decode_srgb)
309 src_format = _mesa_get_srgb_format_linear(src_format);
310
311 if (!encode_srgb)
312 dst_format = _mesa_get_srgb_format_linear(dst_format);
313
314 /* When doing a multisample resolve of a GL_LUMINANCE32F or GL_INTENSITY32F
315 * texture, the above code configures the source format for L32_FLOAT or
316 * I32_FLOAT, and the destination format for R32_FLOAT. On Sandy Bridge,
317 * the SAMPLE message appears to handle multisampled L32_FLOAT and
318 * I32_FLOAT textures incorrectly, resulting in blocky artifacts. So work
319 * around the problem by using a source format of R32_FLOAT. This
320 * shouldn't affect rendering correctness, since the destination format is
321 * R32_FLOAT, so only the contents of the red channel matters.
322 */
323 if (devinfo->gen == 6 &&
324 src_mt->surf.samples > 1 && dst_mt->surf.samples <= 1 &&
325 src_mt->format == dst_mt->format &&
326 (dst_format == MESA_FORMAT_L_FLOAT32 ||
327 dst_format == MESA_FORMAT_I_FLOAT32)) {
328 src_format = dst_format = MESA_FORMAT_R_FLOAT32;
329 }
330
331 enum blorp_filter blorp_filter;
332 if (fabsf(dst_x1 - dst_x0) == fabsf(src_x1 - src_x0) &&
333 fabsf(dst_y1 - dst_y0) == fabsf(src_y1 - src_y0)) {
334 if (src_mt->surf.samples > 1 && dst_mt->surf.samples <= 1) {
335 /* From the OpenGL ES 3.2 specification, section 16.2.1:
336 *
337 * "If the read framebuffer is multisampled (its effective value
338 * of SAMPLE_BUFFERS is one) and the draw framebuffer is not (its
339 * value of SAMPLE_BUFFERS is zero), the samples corresponding to
340 * each pixel location in the source are converted to a single
341 * sample before being written to the destination. The filter
342 * parameter is ignored. If the source formats are integer types
343 * or stencil values, a single sample’s value is selected for each
344 * pixel. If the source formats are floating-point or normalized
345 * types, the sample values for each pixel are resolved in an
346 * implementation-dependent manner. If the source formats are
347 * depth values, sample values are resolved in an implementation-
348 * dependent manner where the result will be between the minimum
349 * and maximum depth values in the pixel."
350 *
351 * For depth and stencil resolves, we choose to always use the value
352 * at sample 0.
353 */
354 GLenum base_format = _mesa_get_format_base_format(src_mt->format);
355 if (base_format == GL_DEPTH_COMPONENT ||
356 base_format == GL_STENCIL_INDEX ||
357 base_format == GL_DEPTH_STENCIL ||
358 _mesa_is_format_integer(src_mt->format)) {
359 /* The OpenGL ES 3.2 spec says:
360 *
361 * "If the source formats are integer types or stencil values,
362 * a single sample's value is selected for each pixel."
363 *
364 * Just take sample 0 in this case.
365 */
366 blorp_filter = BLORP_FILTER_SAMPLE_0;
367 } else {
368 blorp_filter = BLORP_FILTER_AVERAGE;
369 }
370 } else {
371 /* From the OpenGL 4.6 specification, section 18.3.1:
372 *
373 * "If the source and destination dimensions are identical, no
374 * filtering is applied."
375 *
376 * Using BLORP_FILTER_NONE will also handle the upsample case by
377 * replicating the one value in the source to all values in the
378 * destination.
379 */
380 blorp_filter = BLORP_FILTER_NONE;
381 }
382 } else if (gl_filter == GL_LINEAR ||
383 gl_filter == GL_SCALED_RESOLVE_FASTEST_EXT ||
384 gl_filter == GL_SCALED_RESOLVE_NICEST_EXT) {
385 blorp_filter = BLORP_FILTER_BILINEAR;
386 } else {
387 blorp_filter = BLORP_FILTER_NEAREST;
388 }
389
390 enum isl_format src_isl_format =
391 brw_blorp_to_isl_format(brw, src_format, false);
392 enum isl_aux_usage src_aux_usage =
393 intel_miptree_texture_aux_usage(brw, src_mt, src_isl_format,
394 0 /* The astc5x5 WA isn't needed */);
395 /* We do format workarounds for some depth formats so we can't reliably
396 * sample with HiZ. One of these days, we should fix that.
397 */
398 if (src_aux_usage == ISL_AUX_USAGE_HIZ && src_mt->format != src_format)
399 src_aux_usage = ISL_AUX_USAGE_NONE;
400 const bool src_clear_supported =
401 src_aux_usage != ISL_AUX_USAGE_NONE && src_mt->format == src_format;
402 intel_miptree_prepare_access(brw, src_mt, src_level, 1, src_layer, 1,
403 src_aux_usage, src_clear_supported);
404
405 enum isl_format dst_isl_format =
406 brw_blorp_to_isl_format(brw, dst_format, true);
407 enum isl_aux_usage dst_aux_usage =
408 intel_miptree_render_aux_usage(brw, dst_mt, dst_isl_format,
409 false, false);
410 const bool dst_clear_supported = dst_aux_usage != ISL_AUX_USAGE_NONE;
411 intel_miptree_prepare_access(brw, dst_mt, dst_level, 1, dst_layer, 1,
412 dst_aux_usage, dst_clear_supported);
413
414 struct blorp_surf src_surf, dst_surf;
415 blorp_surf_for_miptree(brw, &src_surf, src_mt, src_aux_usage, false,
416 &src_level, src_layer, 1);
417 blorp_surf_for_miptree(brw, &dst_surf, dst_mt, dst_aux_usage, true,
418 &dst_level, dst_layer, 1);
419
420 struct isl_swizzle src_isl_swizzle = {
421 .r = swizzle_to_scs(GET_SWZ(src_swizzle, 0)),
422 .g = swizzle_to_scs(GET_SWZ(src_swizzle, 1)),
423 .b = swizzle_to_scs(GET_SWZ(src_swizzle, 2)),
424 .a = swizzle_to_scs(GET_SWZ(src_swizzle, 3)),
425 };
426
427 struct blorp_batch batch;
428 blorp_batch_init(&brw->blorp, &batch, brw, 0);
429 blorp_blit(&batch, &src_surf, src_level, src_layer,
430 src_isl_format, src_isl_swizzle,
431 &dst_surf, dst_level, dst_layer,
432 dst_isl_format, ISL_SWIZZLE_IDENTITY,
433 src_x0, src_y0, src_x1, src_y1,
434 dst_x0, dst_y0, dst_x1, dst_y1,
435 blorp_filter, mirror_x, mirror_y);
436 blorp_batch_finish(&batch);
437
438 intel_miptree_finish_write(brw, dst_mt, dst_level, dst_layer, 1,
439 dst_aux_usage);
440 }
441
442 void
443 brw_blorp_copy_miptrees(struct brw_context *brw,
444 struct intel_mipmap_tree *src_mt,
445 unsigned src_level, unsigned src_layer,
446 struct intel_mipmap_tree *dst_mt,
447 unsigned dst_level, unsigned dst_layer,
448 unsigned src_x, unsigned src_y,
449 unsigned dst_x, unsigned dst_y,
450 unsigned src_width, unsigned src_height)
451 {
452 const struct gen_device_info *devinfo = &brw->screen->devinfo;
453
454 DBG("%s from %dx %s mt %p %d %d (%d,%d) %dx%d"
455 "to %dx %s mt %p %d %d (%d,%d)\n",
456 __func__,
457 src_mt->surf.samples, _mesa_get_format_name(src_mt->format), src_mt,
458 src_level, src_layer, src_x, src_y, src_width, src_height,
459 dst_mt->surf.samples, _mesa_get_format_name(dst_mt->format), dst_mt,
460 dst_level, dst_layer, dst_x, dst_y);
461
462 enum isl_aux_usage src_aux_usage, dst_aux_usage;
463 bool src_clear_supported, dst_clear_supported;
464
465 switch (src_mt->aux_usage) {
466 case ISL_AUX_USAGE_HIZ:
467 if (intel_miptree_sample_with_hiz(brw, src_mt)) {
468 src_aux_usage = src_mt->aux_usage;
469 src_clear_supported = true;
470 } else {
471 src_aux_usage = ISL_AUX_USAGE_NONE;
472 src_clear_supported = false;
473 }
474 break;
475 case ISL_AUX_USAGE_MCS:
476 case ISL_AUX_USAGE_CCS_E:
477 src_aux_usage = src_mt->aux_usage;
478 /* Prior to gen9, fast-clear only supported 0/1 clear colors. Since
479 * we're going to re-interpret the format as an integer format possibly
480 * with a different number of components, we can't handle clear colors
481 * until gen9.
482 */
483 src_clear_supported = devinfo->gen >= 9;
484 break;
485 default:
486 src_aux_usage = ISL_AUX_USAGE_NONE;
487 src_clear_supported = false;
488 break;
489 }
490
491 switch (dst_mt->aux_usage) {
492 case ISL_AUX_USAGE_MCS:
493 case ISL_AUX_USAGE_CCS_E:
494 dst_aux_usage = dst_mt->aux_usage;
495 /* Prior to gen9, fast-clear only supported 0/1 clear colors. Since
496 * we're going to re-interpret the format as an integer format possibly
497 * with a different number of components, we can't handle clear colors
498 * until gen9.
499 */
500 dst_clear_supported = devinfo->gen >= 9;
501 break;
502 default:
503 dst_aux_usage = ISL_AUX_USAGE_NONE;
504 dst_clear_supported = false;
505 break;
506 }
507
508 intel_miptree_prepare_access(brw, src_mt, src_level, 1, src_layer, 1,
509 src_aux_usage, src_clear_supported);
510 intel_miptree_prepare_access(brw, dst_mt, dst_level, 1, dst_layer, 1,
511 dst_aux_usage, dst_clear_supported);
512
513 struct blorp_surf src_surf, dst_surf;
514 blorp_surf_for_miptree(brw, &src_surf, src_mt, src_aux_usage, false,
515 &src_level, src_layer, 1);
516 blorp_surf_for_miptree(brw, &dst_surf, dst_mt, dst_aux_usage, true,
517 &dst_level, dst_layer, 1);
518
519 /* The hardware seems to have issues with having a two different format
520 * views of the same texture in the sampler cache at the same time. It's
521 * unclear exactly what the issue is but it hurts glCopyImageSubData
522 * particularly badly because it does a lot of format reinterprets. We
523 * badly need better understanding of the issue and a better fix but this
524 * works for now and fixes CTS tests.
525 *
526 * TODO: Remove this hack!
527 */
528 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_CS_STALL |
529 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
530
531 struct blorp_batch batch;
532 blorp_batch_init(&brw->blorp, &batch, brw, 0);
533 blorp_copy(&batch, &src_surf, src_level, src_layer,
534 &dst_surf, dst_level, dst_layer,
535 src_x, src_y, dst_x, dst_y, src_width, src_height);
536 blorp_batch_finish(&batch);
537
538 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_CS_STALL |
539 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
540
541 intel_miptree_finish_write(brw, dst_mt, dst_level, dst_layer, 1,
542 dst_aux_usage);
543 }
544
545 void
546 brw_blorp_copy_buffers(struct brw_context *brw,
547 struct brw_bo *src_bo,
548 unsigned src_offset,
549 struct brw_bo *dst_bo,
550 unsigned dst_offset,
551 unsigned size)
552 {
553 DBG("%s %d bytes from %p[%d] to %p[%d]",
554 __func__, size, src_bo, src_offset, dst_bo, dst_offset);
555
556 struct blorp_batch batch;
557 struct blorp_address src = { .buffer = src_bo, .offset = src_offset };
558 struct blorp_address dst = { .buffer = dst_bo, .offset = dst_offset };
559
560 blorp_batch_init(&brw->blorp, &batch, brw, 0);
561 blorp_buffer_copy(&batch, src, dst, size);
562 blorp_batch_finish(&batch);
563 }
564
565
566 static struct intel_mipmap_tree *
567 find_miptree(GLbitfield buffer_bit, struct intel_renderbuffer *irb)
568 {
569 struct intel_mipmap_tree *mt = irb->mt;
570 if (buffer_bit == GL_STENCIL_BUFFER_BIT && mt->stencil_mt)
571 mt = mt->stencil_mt;
572 return mt;
573 }
574
575 static int
576 blorp_get_texture_swizzle(const struct intel_renderbuffer *irb)
577 {
578 return irb->Base.Base._BaseFormat == GL_RGB ?
579 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE) :
580 SWIZZLE_XYZW;
581 }
582
583 static void
584 do_blorp_blit(struct brw_context *brw, GLbitfield buffer_bit,
585 struct intel_renderbuffer *src_irb, mesa_format src_format,
586 struct intel_renderbuffer *dst_irb, mesa_format dst_format,
587 GLfloat srcX0, GLfloat srcY0, GLfloat srcX1, GLfloat srcY1,
588 GLfloat dstX0, GLfloat dstY0, GLfloat dstX1, GLfloat dstY1,
589 GLenum filter, bool mirror_x, bool mirror_y)
590 {
591 const struct gl_context *ctx = &brw->ctx;
592
593 /* Find source/dst miptrees */
594 struct intel_mipmap_tree *src_mt = find_miptree(buffer_bit, src_irb);
595 struct intel_mipmap_tree *dst_mt = find_miptree(buffer_bit, dst_irb);
596
597 const bool do_srgb = ctx->Color.sRGBEnabled;
598
599 /* Do the blit */
600 brw_blorp_blit_miptrees(brw,
601 src_mt, src_irb->mt_level, src_irb->mt_layer,
602 src_format, blorp_get_texture_swizzle(src_irb),
603 dst_mt, dst_irb->mt_level, dst_irb->mt_layer,
604 dst_format,
605 srcX0, srcY0, srcX1, srcY1,
606 dstX0, dstY0, dstX1, dstY1,
607 filter, mirror_x, mirror_y,
608 do_srgb, do_srgb);
609
610 dst_irb->need_downsample = true;
611 }
612
613 static bool
614 try_blorp_blit(struct brw_context *brw,
615 const struct gl_framebuffer *read_fb,
616 const struct gl_framebuffer *draw_fb,
617 GLfloat srcX0, GLfloat srcY0, GLfloat srcX1, GLfloat srcY1,
618 GLfloat dstX0, GLfloat dstY0, GLfloat dstX1, GLfloat dstY1,
619 GLenum filter, GLbitfield buffer_bit)
620 {
621 const struct gen_device_info *devinfo = &brw->screen->devinfo;
622 struct gl_context *ctx = &brw->ctx;
623
624 /* Sync up the state of window system buffers. We need to do this before
625 * we go looking for the buffers.
626 */
627 intel_prepare_render(brw);
628
629 bool mirror_x, mirror_y;
630 if (brw_meta_mirror_clip_and_scissor(ctx, read_fb, draw_fb,
631 &srcX0, &srcY0, &srcX1, &srcY1,
632 &dstX0, &dstY0, &dstX1, &dstY1,
633 &mirror_x, &mirror_y))
634 return true;
635
636 /* Find buffers */
637 struct intel_renderbuffer *src_irb;
638 struct intel_renderbuffer *dst_irb;
639 struct intel_mipmap_tree *src_mt;
640 struct intel_mipmap_tree *dst_mt;
641 switch (buffer_bit) {
642 case GL_COLOR_BUFFER_BIT:
643 src_irb = intel_renderbuffer(read_fb->_ColorReadBuffer);
644 for (unsigned i = 0; i < draw_fb->_NumColorDrawBuffers; ++i) {
645 dst_irb = intel_renderbuffer(draw_fb->_ColorDrawBuffers[i]);
646 if (dst_irb)
647 do_blorp_blit(brw, buffer_bit,
648 src_irb, src_irb->Base.Base.Format,
649 dst_irb, dst_irb->Base.Base.Format,
650 srcX0, srcY0, srcX1, srcY1,
651 dstX0, dstY0, dstX1, dstY1,
652 filter, mirror_x, mirror_y);
653 }
654 break;
655 case GL_DEPTH_BUFFER_BIT:
656 src_irb =
657 intel_renderbuffer(read_fb->Attachment[BUFFER_DEPTH].Renderbuffer);
658 dst_irb =
659 intel_renderbuffer(draw_fb->Attachment[BUFFER_DEPTH].Renderbuffer);
660 src_mt = find_miptree(buffer_bit, src_irb);
661 dst_mt = find_miptree(buffer_bit, dst_irb);
662
663 /* We also can't handle any combined depth-stencil formats because we
664 * have to reinterpret as a color format.
665 */
666 if (_mesa_get_format_base_format(src_mt->format) == GL_DEPTH_STENCIL ||
667 _mesa_get_format_base_format(dst_mt->format) == GL_DEPTH_STENCIL)
668 return false;
669
670 do_blorp_blit(brw, buffer_bit, src_irb, MESA_FORMAT_NONE,
671 dst_irb, MESA_FORMAT_NONE, srcX0, srcY0,
672 srcX1, srcY1, dstX0, dstY0, dstX1, dstY1,
673 filter, mirror_x, mirror_y);
674 break;
675 case GL_STENCIL_BUFFER_BIT:
676 /* Blorp doesn't support combined depth stencil which is all we have
677 * prior to gen6.
678 */
679 if (devinfo->gen < 6)
680 return false;
681
682 src_irb =
683 intel_renderbuffer(read_fb->Attachment[BUFFER_STENCIL].Renderbuffer);
684 dst_irb =
685 intel_renderbuffer(draw_fb->Attachment[BUFFER_STENCIL].Renderbuffer);
686 do_blorp_blit(brw, buffer_bit, src_irb, MESA_FORMAT_NONE,
687 dst_irb, MESA_FORMAT_NONE, srcX0, srcY0,
688 srcX1, srcY1, dstX0, dstY0, dstX1, dstY1,
689 filter, mirror_x, mirror_y);
690 break;
691 default:
692 unreachable("not reached");
693 }
694
695 return true;
696 }
697
698 static void
699 apply_y_flip(int *y0, int *y1, int height)
700 {
701 int tmp = height - *y0;
702 *y0 = height - *y1;
703 *y1 = tmp;
704 }
705
706 bool
707 brw_blorp_copytexsubimage(struct brw_context *brw,
708 struct gl_renderbuffer *src_rb,
709 struct gl_texture_image *dst_image,
710 int slice,
711 int srcX0, int srcY0,
712 int dstX0, int dstY0,
713 int width, int height)
714 {
715 struct gl_context *ctx = &brw->ctx;
716 struct intel_renderbuffer *src_irb = intel_renderbuffer(src_rb);
717 struct intel_texture_image *intel_image = intel_texture_image(dst_image);
718
719 /* No pixel transfer operations (zoom, bias, mapping), just a blit */
720 if (brw->ctx._ImageTransferState)
721 return false;
722
723 /* Sync up the state of window system buffers. We need to do this before
724 * we go looking at the src renderbuffer's miptree.
725 */
726 intel_prepare_render(brw);
727
728 struct intel_mipmap_tree *src_mt = src_irb->mt;
729 struct intel_mipmap_tree *dst_mt = intel_image->mt;
730
731 /* We can't handle any combined depth-stencil formats because we have to
732 * reinterpret as a color format.
733 */
734 if (_mesa_get_format_base_format(src_mt->format) == GL_DEPTH_STENCIL ||
735 _mesa_get_format_base_format(dst_mt->format) == GL_DEPTH_STENCIL)
736 return false;
737
738 if (!brw_blorp_supports_dst_format(brw, dst_image->TexFormat))
739 return false;
740
741 /* Source clipping shouldn't be necessary, since copytexsubimage (in
742 * src/mesa/main/teximage.c) calls _mesa_clip_copytexsubimage() which
743 * takes care of it.
744 *
745 * Destination clipping shouldn't be necessary since the restrictions on
746 * glCopyTexSubImage prevent the user from specifying a destination rectangle
747 * that falls outside the bounds of the destination texture.
748 * See error_check_subtexture_dimensions().
749 */
750
751 int srcY1 = srcY0 + height;
752 int srcX1 = srcX0 + width;
753 int dstX1 = dstX0 + width;
754 int dstY1 = dstY0 + height;
755
756 /* Account for the fact that in the system framebuffer, the origin is at
757 * the lower left.
758 */
759 bool mirror_y = ctx->ReadBuffer->FlipY;
760 if (mirror_y)
761 apply_y_flip(&srcY0, &srcY1, src_rb->Height);
762
763 /* Account for face selection and texture view MinLayer */
764 int dst_slice = slice + dst_image->TexObject->MinLayer + dst_image->Face;
765 int dst_level = dst_image->Level + dst_image->TexObject->MinLevel;
766
767 brw_blorp_blit_miptrees(brw,
768 src_mt, src_irb->mt_level, src_irb->mt_layer,
769 src_rb->Format, blorp_get_texture_swizzle(src_irb),
770 dst_mt, dst_level, dst_slice,
771 dst_image->TexFormat,
772 srcX0, srcY0, srcX1, srcY1,
773 dstX0, dstY0, dstX1, dstY1,
774 GL_NEAREST, false, mirror_y,
775 false, false);
776
777 /* If we're copying to a packed depth stencil texture and the source
778 * framebuffer has separate stencil, we need to also copy the stencil data
779 * over.
780 */
781 src_rb = ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
782 if (_mesa_get_format_bits(dst_image->TexFormat, GL_STENCIL_BITS) > 0 &&
783 src_rb != NULL) {
784 src_irb = intel_renderbuffer(src_rb);
785 src_mt = src_irb->mt;
786
787 if (src_mt->stencil_mt)
788 src_mt = src_mt->stencil_mt;
789 if (dst_mt->stencil_mt)
790 dst_mt = dst_mt->stencil_mt;
791
792 if (src_mt != dst_mt) {
793 brw_blorp_blit_miptrees(brw,
794 src_mt, src_irb->mt_level, src_irb->mt_layer,
795 src_mt->format,
796 blorp_get_texture_swizzle(src_irb),
797 dst_mt, dst_level, dst_slice,
798 dst_mt->format,
799 srcX0, srcY0, srcX1, srcY1,
800 dstX0, dstY0, dstX1, dstY1,
801 GL_NEAREST, false, mirror_y,
802 false, false);
803 }
804 }
805
806 return true;
807 }
808
809
810 GLbitfield
811 brw_blorp_framebuffer(struct brw_context *brw,
812 struct gl_framebuffer *readFb,
813 struct gl_framebuffer *drawFb,
814 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
815 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
816 GLbitfield mask, GLenum filter)
817 {
818 static GLbitfield buffer_bits[] = {
819 GL_COLOR_BUFFER_BIT,
820 GL_DEPTH_BUFFER_BIT,
821 GL_STENCIL_BUFFER_BIT,
822 };
823
824 for (unsigned int i = 0; i < ARRAY_SIZE(buffer_bits); ++i) {
825 if ((mask & buffer_bits[i]) &&
826 try_blorp_blit(brw, readFb, drawFb,
827 srcX0, srcY0, srcX1, srcY1,
828 dstX0, dstY0, dstX1, dstY1,
829 filter, buffer_bits[i])) {
830 mask &= ~buffer_bits[i];
831 }
832 }
833
834 /* try_blorp_blit should always be successful for color blits. */
835 assert(!(mask & GL_COLOR_BUFFER_BIT));
836 return mask;
837 }
838
839 static struct brw_bo *
840 blorp_get_client_bo(struct brw_context *brw,
841 unsigned w, unsigned h, unsigned d,
842 GLenum target, GLenum format, GLenum type,
843 const void *pixels,
844 const struct gl_pixelstore_attrib *packing,
845 uint32_t *offset_out, uint32_t *row_stride_out,
846 uint32_t *image_stride_out, bool read_only)
847 {
848 /* Account for SKIP_PIXELS, SKIP_ROWS, ALIGNMENT, and SKIP_IMAGES */
849 const GLuint dims = _mesa_get_texture_dimensions(target);
850 const uint32_t first_pixel = _mesa_image_offset(dims, packing, w, h,
851 format, type, 0, 0, 0);
852 const uint32_t last_pixel = _mesa_image_offset(dims, packing, w, h,
853 format, type,
854 d - 1, h - 1, w);
855 const uint32_t stride = _mesa_image_row_stride(packing, w, format, type);
856 const uint32_t size = last_pixel - first_pixel;
857
858 *row_stride_out = stride;
859 *image_stride_out = _mesa_image_image_stride(packing, w, h, format, type);
860
861 if (packing->BufferObj) {
862 const uint32_t offset = first_pixel + (intptr_t)pixels;
863
864 if (!read_only) {
865 const int32_t cpp = _mesa_bytes_per_pixel(format, type);
866 assert(cpp > 0);
867
868 if ((offset % cpp) || (stride % cpp)) {
869 perf_debug("Bad PBO alignment; fallback to CPU mapping\n");
870 return NULL;
871 }
872 }
873
874 /* This is a user-provided PBO. We just need to get the BO out */
875 struct intel_buffer_object *intel_pbo =
876 intel_buffer_object(packing->BufferObj);
877 struct brw_bo *bo =
878 intel_bufferobj_buffer(brw, intel_pbo, offset, size, !read_only);
879
880 /* We take a reference to the BO so that the caller can just always
881 * unref without having to worry about whether it's a user PBO or one
882 * we created.
883 */
884 brw_bo_reference(bo);
885
886 *offset_out = offset;
887 return bo;
888 } else {
889 /* Someone should have already checked that there is data to upload. */
890 assert(pixels);
891
892 /* Creating a temp buffer currently only works for upload */
893 assert(read_only);
894
895 /* This is not a user-provided PBO. Instead, pixels is a pointer to CPU
896 * data which we need to copy into a BO.
897 */
898 struct brw_bo *bo =
899 brw_bo_alloc(brw->bufmgr, "tmp_tex_subimage_src", size,
900 BRW_MEMZONE_OTHER);
901 if (bo == NULL) {
902 perf_debug("intel_texsubimage: temp bo creation failed: size = %u\n",
903 size);
904 return NULL;
905 }
906
907 if (brw_bo_subdata(bo, 0, size, pixels + first_pixel)) {
908 perf_debug("intel_texsubimage: temp bo upload failed\n");
909 brw_bo_unreference(bo);
910 return NULL;
911 }
912
913 *offset_out = 0;
914 return bo;
915 }
916 }
917
918 /* Consider all the restrictions and determine the format of the source. */
919 static mesa_format
920 blorp_get_client_format(struct brw_context *brw,
921 GLenum format, GLenum type,
922 const struct gl_pixelstore_attrib *packing)
923 {
924 if (brw->ctx._ImageTransferState)
925 return MESA_FORMAT_NONE;
926
927 if (packing->SwapBytes || packing->LsbFirst || packing->Invert) {
928 perf_debug("intel_texsubimage_blorp: unsupported gl_pixelstore_attrib\n");
929 return MESA_FORMAT_NONE;
930 }
931
932 if (format != GL_RED &&
933 format != GL_RG &&
934 format != GL_RGB &&
935 format != GL_BGR &&
936 format != GL_RGBA &&
937 format != GL_BGRA &&
938 format != GL_ALPHA &&
939 format != GL_RED_INTEGER &&
940 format != GL_RG_INTEGER &&
941 format != GL_RGB_INTEGER &&
942 format != GL_BGR_INTEGER &&
943 format != GL_RGBA_INTEGER &&
944 format != GL_BGRA_INTEGER) {
945 perf_debug("intel_texsubimage_blorp: %s not supported",
946 _mesa_enum_to_string(format));
947 return MESA_FORMAT_NONE;
948 }
949
950 return _mesa_tex_format_from_format_and_type(&brw->ctx, format, type);
951 }
952
953 bool
954 brw_blorp_upload_miptree(struct brw_context *brw,
955 struct intel_mipmap_tree *dst_mt,
956 mesa_format dst_format,
957 uint32_t level, uint32_t x, uint32_t y, uint32_t z,
958 uint32_t width, uint32_t height, uint32_t depth,
959 GLenum target, GLenum format, GLenum type,
960 const void *pixels,
961 const struct gl_pixelstore_attrib *packing)
962 {
963 const mesa_format src_format =
964 blorp_get_client_format(brw, format, type, packing);
965 if (src_format == MESA_FORMAT_NONE)
966 return false;
967
968 if (!brw->mesa_format_supports_render[dst_format]) {
969 perf_debug("intel_texsubimage: can't use %s as render target\n",
970 _mesa_get_format_name(dst_format));
971 return false;
972 }
973
974 uint32_t src_offset, src_row_stride, src_image_stride;
975 struct brw_bo *src_bo =
976 blorp_get_client_bo(brw, width, height, depth,
977 target, format, type, pixels, packing,
978 &src_offset, &src_row_stride,
979 &src_image_stride, true);
980 if (src_bo == NULL)
981 return false;
982
983 /* Now that source is offset to correct starting point, adjust the
984 * given dimensions to treat 1D arrays as 2D.
985 */
986 if (target == GL_TEXTURE_1D_ARRAY) {
987 assert(depth == 1);
988 assert(z == 0);
989 depth = height;
990 height = 1;
991 z = y;
992 y = 0;
993 src_image_stride = src_row_stride;
994 }
995
996 intel_miptree_check_level_layer(dst_mt, level, z + depth - 1);
997
998 bool result = false;
999
1000 /* Blit slice-by-slice creating a single-slice miptree for each layer. Even
1001 * in case of linear buffers hardware wants image arrays to be aligned by
1002 * four rows. This way hardware only gets one image at a time and any
1003 * source alignment will do.
1004 */
1005 for (unsigned i = 0; i < depth; ++i) {
1006 struct intel_mipmap_tree *src_mt = intel_miptree_create_for_bo(
1007 brw, src_bo, src_format,
1008 src_offset + i * src_image_stride,
1009 width, height, 1,
1010 src_row_stride,
1011 ISL_TILING_LINEAR, 0);
1012
1013 if (!src_mt) {
1014 perf_debug("intel_texsubimage: miptree creation for src failed\n");
1015 goto err;
1016 }
1017
1018 /* In case exact match is needed, copy using equivalent UINT formats
1019 * preventing hardware from changing presentation for SNORM -1.
1020 */
1021 if (src_mt->format == dst_format) {
1022 brw_blorp_copy_miptrees(brw, src_mt, 0, 0,
1023 dst_mt, level, z + i,
1024 0, 0, x, y, width, height);
1025 } else {
1026 brw_blorp_blit_miptrees(brw, src_mt, 0, 0,
1027 src_format, SWIZZLE_XYZW,
1028 dst_mt, level, z + i,
1029 dst_format,
1030 0, 0, width, height,
1031 x, y, x + width, y + height,
1032 GL_NEAREST, false, false, false, false);
1033 }
1034
1035 intel_miptree_release(&src_mt);
1036 }
1037
1038 result = true;
1039
1040 err:
1041 brw_bo_unreference(src_bo);
1042
1043 return result;
1044 }
1045
1046 bool
1047 brw_blorp_download_miptree(struct brw_context *brw,
1048 struct intel_mipmap_tree *src_mt,
1049 mesa_format src_format, uint32_t src_swizzle,
1050 uint32_t level, uint32_t x, uint32_t y, uint32_t z,
1051 uint32_t width, uint32_t height, uint32_t depth,
1052 GLenum target, GLenum format, GLenum type,
1053 bool y_flip, const void *pixels,
1054 const struct gl_pixelstore_attrib *packing)
1055 {
1056 const mesa_format dst_format =
1057 blorp_get_client_format(brw, format, type, packing);
1058 if (dst_format == MESA_FORMAT_NONE)
1059 return false;
1060
1061 if (!brw->mesa_format_supports_render[dst_format]) {
1062 perf_debug("intel_texsubimage: can't use %s as render target\n",
1063 _mesa_get_format_name(dst_format));
1064 return false;
1065 }
1066
1067 /* We can't fetch from LUMINANCE or intensity as that would require a
1068 * non-trivial swizzle.
1069 */
1070 switch (_mesa_get_format_base_format(src_format)) {
1071 case GL_LUMINANCE:
1072 case GL_LUMINANCE_ALPHA:
1073 case GL_INTENSITY:
1074 return false;
1075 default:
1076 break;
1077 }
1078
1079 /* This pass only works for PBOs */
1080 assert(packing->BufferObj);
1081
1082 uint32_t dst_offset, dst_row_stride, dst_image_stride;
1083 struct brw_bo *dst_bo =
1084 blorp_get_client_bo(brw, width, height, depth,
1085 target, format, type, pixels, packing,
1086 &dst_offset, &dst_row_stride,
1087 &dst_image_stride, false);
1088 if (dst_bo == NULL)
1089 return false;
1090
1091 /* Now that source is offset to correct starting point, adjust the
1092 * given dimensions to treat 1D arrays as 2D.
1093 */
1094 if (target == GL_TEXTURE_1D_ARRAY) {
1095 assert(depth == 1);
1096 assert(z == 0);
1097 depth = height;
1098 height = 1;
1099 z = y;
1100 y = 0;
1101 dst_image_stride = dst_row_stride;
1102 }
1103
1104 intel_miptree_check_level_layer(src_mt, level, z + depth - 1);
1105
1106 int y0 = y;
1107 int y1 = y + height;
1108 if (y_flip) {
1109 apply_y_flip(&y0, &y1, minify(src_mt->surf.phys_level0_sa.height,
1110 level - src_mt->first_level));
1111 }
1112
1113 bool result = false;
1114
1115 /* Blit slice-by-slice creating a single-slice miptree for each layer. Even
1116 * in case of linear buffers hardware wants image arrays to be aligned by
1117 * four rows. This way hardware only gets one image at a time and any
1118 * source alignment will do.
1119 */
1120 for (unsigned i = 0; i < depth; ++i) {
1121 struct intel_mipmap_tree *dst_mt = intel_miptree_create_for_bo(
1122 brw, dst_bo, dst_format,
1123 dst_offset + i * dst_image_stride,
1124 width, height, 1,
1125 dst_row_stride,
1126 ISL_TILING_LINEAR, 0);
1127
1128 if (!dst_mt) {
1129 perf_debug("intel_texsubimage: miptree creation for src failed\n");
1130 goto err;
1131 }
1132
1133 /* In case exact match is needed, copy using equivalent UINT formats
1134 * preventing hardware from changing presentation for SNORM -1.
1135 */
1136 if (dst_mt->format == src_format && !y_flip &&
1137 src_swizzle == SWIZZLE_XYZW) {
1138 brw_blorp_copy_miptrees(brw, src_mt, level, z + i,
1139 dst_mt, 0, 0,
1140 x, y, 0, 0, width, height);
1141 } else {
1142 brw_blorp_blit_miptrees(brw, src_mt, level, z + i,
1143 src_format, src_swizzle,
1144 dst_mt, 0, 0, dst_format,
1145 x, y0, x + width, y1,
1146 0, 0, width, height,
1147 GL_NEAREST, false, y_flip, false, false);
1148 }
1149
1150 intel_miptree_release(&dst_mt);
1151 }
1152
1153 result = true;
1154
1155 /* As we implement PBO transfers by binding the user-provided BO as a
1156 * fake framebuffer and rendering to it. This breaks the invariant of the
1157 * GL that nothing is able to render to a BO, causing nondeterministic
1158 * corruption issues because the render cache is not coherent with a
1159 * number of other caches that the BO could potentially be bound to
1160 * afterwards.
1161 *
1162 * This could be solved in the same way that we guarantee texture
1163 * coherency after a texture is attached to a framebuffer and
1164 * rendered to, but that would involve checking *all* BOs bound to
1165 * the pipeline for the case we need to emit a cache flush due to
1166 * previous rendering to any of them -- Including vertex, index,
1167 * uniform, atomic counter, shader image, transform feedback,
1168 * indirect draw buffers, etc.
1169 *
1170 * That would increase the per-draw call overhead even though it's
1171 * very unlikely that any of the BOs bound to the pipeline has been
1172 * rendered to via a PBO at any point, so it seems better to just
1173 * flush here unconditionally.
1174 */
1175 brw_emit_mi_flush(brw);
1176
1177 err:
1178 brw_bo_unreference(dst_bo);
1179
1180 return result;
1181 }
1182
1183 static bool
1184 set_write_disables(const struct intel_renderbuffer *irb,
1185 const unsigned color_mask, bool *color_write_disable)
1186 {
1187 /* Format information in the renderbuffer represents the requirements
1188 * given by the client. There are cases where the backing miptree uses,
1189 * for example, RGBA to represent RGBX. Since the client is only expecting
1190 * RGB we can treat alpha as not used and write whatever we like into it.
1191 */
1192 const GLenum base_format = irb->Base.Base._BaseFormat;
1193 const int components = _mesa_components_in_format(base_format);
1194 bool disables = false;
1195
1196 assert(components > 0);
1197
1198 for (int i = 0; i < components; i++) {
1199 color_write_disable[i] = !(color_mask & (1 << i));
1200 disables = disables || color_write_disable[i];
1201 }
1202
1203 return disables;
1204 }
1205
1206 static void
1207 do_single_blorp_clear(struct brw_context *brw, struct gl_framebuffer *fb,
1208 struct gl_renderbuffer *rb, unsigned buf,
1209 bool partial_clear, bool encode_srgb)
1210 {
1211 struct gl_context *ctx = &brw->ctx;
1212 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
1213 uint32_t x0, x1, y0, y1;
1214
1215 mesa_format format = irb->Base.Base.Format;
1216 if (!encode_srgb)
1217 format = _mesa_get_srgb_format_linear(format);
1218 enum isl_format isl_format = brw->mesa_to_isl_render_format[format];
1219
1220 x0 = fb->_Xmin;
1221 x1 = fb->_Xmax;
1222 if (fb->FlipY) {
1223 y0 = rb->Height - fb->_Ymax;
1224 y1 = rb->Height - fb->_Ymin;
1225 } else {
1226 y0 = fb->_Ymin;
1227 y1 = fb->_Ymax;
1228 }
1229
1230 /* If the clear region is empty, just return. */
1231 if (x0 == x1 || y0 == y1)
1232 return;
1233
1234 bool can_fast_clear = !partial_clear;
1235
1236 if (INTEL_DEBUG & DEBUG_NO_FAST_CLEAR)
1237 can_fast_clear = false;
1238
1239 bool color_write_disable[4] = { false, false, false, false };
1240 if (set_write_disables(irb, GET_COLORMASK(ctx->Color.ColorMask, buf),
1241 color_write_disable))
1242 can_fast_clear = false;
1243
1244 /* We store clear colors as floats or uints as needed. If there are
1245 * texture views in play, the formats will not properly be respected
1246 * during resolves because the resolve operations only know about the
1247 * miptree and not the renderbuffer.
1248 */
1249 if (irb->Base.Base.Format != irb->mt->format)
1250 can_fast_clear = false;
1251
1252 if (!irb->mt->supports_fast_clear ||
1253 !brw_is_color_fast_clear_compatible(brw, irb->mt, &ctx->Color.ClearColor))
1254 can_fast_clear = false;
1255
1256 /* Surface state can only record one fast clear color value. Therefore
1257 * unless different levels/layers agree on the color it can be used to
1258 * represent only single level/layer. Here it will be reserved for the
1259 * first slice (level 0, layer 0).
1260 */
1261 if (irb->layer_count > 1 || irb->mt_level || irb->mt_layer)
1262 can_fast_clear = false;
1263
1264 unsigned level = irb->mt_level;
1265 const unsigned num_layers = fb->MaxNumLayers ? irb->layer_count : 1;
1266
1267 /* If the MCS buffer hasn't been allocated yet, we need to allocate it now.
1268 */
1269 if (can_fast_clear && !irb->mt->aux_buf) {
1270 assert(irb->mt->aux_usage == ISL_AUX_USAGE_CCS_D);
1271 if (!intel_miptree_alloc_aux(brw, irb->mt)) {
1272 /* We're out of memory. Fall back to a non-fast clear. */
1273 can_fast_clear = false;
1274 }
1275 }
1276
1277 if (can_fast_clear) {
1278 const enum isl_aux_state aux_state =
1279 intel_miptree_get_aux_state(irb->mt, irb->mt_level, irb->mt_layer);
1280 union isl_color_value clear_color =
1281 brw_meta_convert_fast_clear_color(brw, irb->mt,
1282 &ctx->Color.ClearColor);
1283
1284 /* If the buffer is already in ISL_AUX_STATE_CLEAR and the clear color
1285 * hasn't changed, the clear is redundant and can be skipped.
1286 */
1287 if (!intel_miptree_set_clear_color(brw, irb->mt, clear_color) &&
1288 aux_state == ISL_AUX_STATE_CLEAR) {
1289 return;
1290 }
1291
1292 DBG("%s (fast) to mt %p level %d layers %d+%d\n", __FUNCTION__,
1293 irb->mt, irb->mt_level, irb->mt_layer, num_layers);
1294
1295 /* We can't setup the blorp_surf until we've allocated the MCS above */
1296 struct blorp_surf surf;
1297 blorp_surf_for_miptree(brw, &surf, irb->mt, irb->mt->aux_usage, true,
1298 &level, irb->mt_layer, num_layers);
1299
1300 /* Ivybrigde PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
1301 *
1302 * "Any transition from any value in {Clear, Render, Resolve} to a
1303 * different value in {Clear, Render, Resolve} requires end of pipe
1304 * synchronization."
1305 *
1306 * In other words, fast clear ops are not properly synchronized with
1307 * other drawing. We need to use a PIPE_CONTROL to ensure that the
1308 * contents of the previous draw hit the render target before we resolve
1309 * and again afterwards to ensure that the resolve is complete before we
1310 * do any more regular drawing.
1311 */
1312 brw_emit_end_of_pipe_sync(brw, PIPE_CONTROL_RENDER_TARGET_FLUSH);
1313
1314 struct blorp_batch batch;
1315 blorp_batch_init(&brw->blorp, &batch, brw, 0);
1316 blorp_fast_clear(&batch, &surf, isl_format_srgb_to_linear(isl_format),
1317 ISL_SWIZZLE_IDENTITY,
1318 level, irb->mt_layer, num_layers, x0, y0, x1, y1);
1319 blorp_batch_finish(&batch);
1320
1321 brw_emit_end_of_pipe_sync(brw, PIPE_CONTROL_RENDER_TARGET_FLUSH);
1322
1323 /* Now that the fast clear has occurred, put the buffer in
1324 * INTEL_FAST_CLEAR_STATE_CLEAR so that we won't waste time doing
1325 * redundant clears.
1326 */
1327 intel_miptree_set_aux_state(brw, irb->mt, irb->mt_level,
1328 irb->mt_layer, num_layers,
1329 ISL_AUX_STATE_CLEAR);
1330 } else {
1331 DBG("%s (slow) to mt %p level %d layer %d+%d\n", __FUNCTION__,
1332 irb->mt, irb->mt_level, irb->mt_layer, num_layers);
1333
1334 enum isl_aux_usage aux_usage =
1335 intel_miptree_render_aux_usage(brw, irb->mt, isl_format,
1336 false, false);
1337 intel_miptree_prepare_render(brw, irb->mt, level, irb->mt_layer,
1338 num_layers, aux_usage);
1339
1340 struct blorp_surf surf;
1341 blorp_surf_for_miptree(brw, &surf, irb->mt, aux_usage, true,
1342 &level, irb->mt_layer, num_layers);
1343
1344 union isl_color_value clear_color;
1345 memcpy(clear_color.f32, ctx->Color.ClearColor.f, sizeof(float) * 4);
1346
1347 struct blorp_batch batch;
1348 blorp_batch_init(&brw->blorp, &batch, brw, 0);
1349 blorp_clear(&batch, &surf, isl_format, ISL_SWIZZLE_IDENTITY,
1350 level, irb->mt_layer, num_layers,
1351 x0, y0, x1, y1,
1352 clear_color, color_write_disable);
1353 blorp_batch_finish(&batch);
1354
1355 intel_miptree_finish_render(brw, irb->mt, level, irb->mt_layer,
1356 num_layers, aux_usage);
1357 }
1358
1359 return;
1360 }
1361
1362 void
1363 brw_blorp_clear_color(struct brw_context *brw, struct gl_framebuffer *fb,
1364 GLbitfield mask, bool partial_clear, bool encode_srgb)
1365 {
1366 for (unsigned buf = 0; buf < fb->_NumColorDrawBuffers; buf++) {
1367 struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[buf];
1368 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
1369
1370 /* Only clear the buffers present in the provided mask */
1371 if (((1 << fb->_ColorDrawBufferIndexes[buf]) & mask) == 0)
1372 continue;
1373
1374 /* If this is an ES2 context or GL_ARB_ES2_compatibility is supported,
1375 * the framebuffer can be complete with some attachments missing. In
1376 * this case the _ColorDrawBuffers pointer will be NULL.
1377 */
1378 if (rb == NULL)
1379 continue;
1380
1381 do_single_blorp_clear(brw, fb, rb, buf, partial_clear, encode_srgb);
1382 irb->need_downsample = true;
1383 }
1384
1385 return;
1386 }
1387
1388 void
1389 brw_blorp_clear_depth_stencil(struct brw_context *brw,
1390 struct gl_framebuffer *fb,
1391 GLbitfield mask, bool partial_clear)
1392 {
1393 const struct gl_context *ctx = &brw->ctx;
1394 struct gl_renderbuffer *depth_rb =
1395 fb->Attachment[BUFFER_DEPTH].Renderbuffer;
1396 struct gl_renderbuffer *stencil_rb =
1397 fb->Attachment[BUFFER_STENCIL].Renderbuffer;
1398
1399 if (!depth_rb || ctx->Depth.Mask == GL_FALSE)
1400 mask &= ~BUFFER_BIT_DEPTH;
1401
1402 if (!stencil_rb || (ctx->Stencil.WriteMask[0] & 0xff) == 0)
1403 mask &= ~BUFFER_BIT_STENCIL;
1404
1405 if (!(mask & (BUFFER_BITS_DEPTH_STENCIL)))
1406 return;
1407
1408 uint32_t x0, x1, y0, y1, rb_height;
1409 if (depth_rb) {
1410 rb_height = depth_rb->Height;
1411 if (stencil_rb) {
1412 assert(depth_rb->Width == stencil_rb->Width);
1413 assert(depth_rb->Height == stencil_rb->Height);
1414 }
1415 } else {
1416 assert(stencil_rb);
1417 rb_height = stencil_rb->Height;
1418 }
1419
1420 x0 = fb->_Xmin;
1421 x1 = fb->_Xmax;
1422 if (fb->FlipY) {
1423 y0 = rb_height - fb->_Ymax;
1424 y1 = rb_height - fb->_Ymin;
1425 } else {
1426 y0 = fb->_Ymin;
1427 y1 = fb->_Ymax;
1428 }
1429
1430 /* If the clear region is empty, just return. */
1431 if (x0 == x1 || y0 == y1)
1432 return;
1433
1434 uint32_t level = 0, start_layer = 0, num_layers;
1435 struct blorp_surf depth_surf, stencil_surf;
1436
1437 struct intel_mipmap_tree *depth_mt = NULL;
1438 if (mask & BUFFER_BIT_DEPTH) {
1439 struct intel_renderbuffer *irb = intel_renderbuffer(depth_rb);
1440 depth_mt = find_miptree(GL_DEPTH_BUFFER_BIT, irb);
1441
1442 level = irb->mt_level;
1443 start_layer = irb->mt_layer;
1444 num_layers = fb->MaxNumLayers ? irb->layer_count : 1;
1445
1446 intel_miptree_prepare_depth(brw, depth_mt, level,
1447 start_layer, num_layers);
1448
1449 unsigned depth_level = level;
1450 blorp_surf_for_miptree(brw, &depth_surf, depth_mt, depth_mt->aux_usage,
1451 true, &depth_level, start_layer, num_layers);
1452 assert(depth_level == level);
1453 }
1454
1455 uint8_t stencil_mask = 0;
1456 struct intel_mipmap_tree *stencil_mt = NULL;
1457 if (mask & BUFFER_BIT_STENCIL) {
1458 struct intel_renderbuffer *irb = intel_renderbuffer(stencil_rb);
1459 stencil_mt = find_miptree(GL_STENCIL_BUFFER_BIT, irb);
1460
1461 if (mask & BUFFER_BIT_DEPTH) {
1462 assert(level == irb->mt_level);
1463 assert(start_layer == irb->mt_layer);
1464 assert(num_layers == fb->MaxNumLayers ? irb->layer_count : 1);
1465 }
1466
1467 level = irb->mt_level;
1468 start_layer = irb->mt_layer;
1469 num_layers = fb->MaxNumLayers ? irb->layer_count : 1;
1470
1471 stencil_mask = ctx->Stencil.WriteMask[0] & 0xff;
1472
1473 intel_miptree_prepare_access(brw, stencil_mt, level, 1,
1474 start_layer, num_layers,
1475 ISL_AUX_USAGE_NONE, false);
1476
1477 unsigned stencil_level = level;
1478 blorp_surf_for_miptree(brw, &stencil_surf, stencil_mt,
1479 ISL_AUX_USAGE_NONE, true,
1480 &stencil_level, start_layer, num_layers);
1481 }
1482
1483 assert((mask & BUFFER_BIT_DEPTH) || stencil_mask);
1484
1485 struct blorp_batch batch;
1486 blorp_batch_init(&brw->blorp, &batch, brw, 0);
1487 blorp_clear_depth_stencil(&batch, &depth_surf, &stencil_surf,
1488 level, start_layer, num_layers,
1489 x0, y0, x1, y1,
1490 (mask & BUFFER_BIT_DEPTH), ctx->Depth.Clear,
1491 stencil_mask, ctx->Stencil.Clear);
1492 blorp_batch_finish(&batch);
1493
1494 if (mask & BUFFER_BIT_DEPTH) {
1495 intel_miptree_finish_depth(brw, depth_mt, level,
1496 start_layer, num_layers, true);
1497 }
1498
1499 if (stencil_mask) {
1500 intel_miptree_finish_write(brw, stencil_mt, level,
1501 start_layer, num_layers,
1502 ISL_AUX_USAGE_NONE);
1503 }
1504 }
1505
1506 void
1507 brw_blorp_resolve_color(struct brw_context *brw, struct intel_mipmap_tree *mt,
1508 unsigned level, unsigned layer,
1509 enum isl_aux_op resolve_op)
1510 {
1511 DBG("%s to mt %p level %u layer %u\n", __FUNCTION__, mt, level, layer);
1512
1513 const mesa_format format = _mesa_get_srgb_format_linear(mt->format);
1514
1515 struct blorp_surf surf;
1516 blorp_surf_for_miptree(brw, &surf, mt, mt->aux_usage, true,
1517 &level, layer, 1 /* num_layers */);
1518
1519 /* Ivybrigde PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
1520 *
1521 * "Any transition from any value in {Clear, Render, Resolve} to a
1522 * different value in {Clear, Render, Resolve} requires end of pipe
1523 * synchronization."
1524 *
1525 * In other words, fast clear ops are not properly synchronized with
1526 * other drawing. We need to use a PIPE_CONTROL to ensure that the
1527 * contents of the previous draw hit the render target before we resolve
1528 * and again afterwards to ensure that the resolve is complete before we
1529 * do any more regular drawing.
1530 */
1531 brw_emit_end_of_pipe_sync(brw, PIPE_CONTROL_RENDER_TARGET_FLUSH);
1532
1533
1534 struct blorp_batch batch;
1535 blorp_batch_init(&brw->blorp, &batch, brw, 0);
1536 blorp_ccs_resolve(&batch, &surf, level, layer, 1,
1537 brw_blorp_to_isl_format(brw, format, true),
1538 resolve_op);
1539 blorp_batch_finish(&batch);
1540
1541 /* See comment above */
1542 brw_emit_end_of_pipe_sync(brw, PIPE_CONTROL_RENDER_TARGET_FLUSH);
1543 }
1544
1545 void
1546 brw_blorp_mcs_partial_resolve(struct brw_context *brw,
1547 struct intel_mipmap_tree *mt,
1548 uint32_t start_layer, uint32_t num_layers)
1549 {
1550 DBG("%s to mt %p layers %u-%u\n", __FUNCTION__, mt,
1551 start_layer, start_layer + num_layers - 1);
1552
1553 assert(mt->aux_usage == ISL_AUX_USAGE_MCS);
1554
1555 const mesa_format format = _mesa_get_srgb_format_linear(mt->format);
1556 enum isl_format isl_format = brw_blorp_to_isl_format(brw, format, true);
1557
1558 struct blorp_surf surf;
1559 uint32_t level = 0;
1560 blorp_surf_for_miptree(brw, &surf, mt, ISL_AUX_USAGE_MCS, true,
1561 &level, start_layer, num_layers);
1562
1563 struct blorp_batch batch;
1564 blorp_batch_init(&brw->blorp, &batch, brw, 0);
1565 blorp_mcs_partial_resolve(&batch, &surf, isl_format,
1566 start_layer, num_layers);
1567 blorp_batch_finish(&batch);
1568 }
1569
1570 /**
1571 * Perform a HiZ or depth resolve operation.
1572 *
1573 * For an overview of HiZ ops, see the following sections of the Sandy Bridge
1574 * PRM, Volume 1, Part 2:
1575 * - 7.5.3.1 Depth Buffer Clear
1576 * - 7.5.3.2 Depth Buffer Resolve
1577 * - 7.5.3.3 Hierarchical Depth Buffer Resolve
1578 */
1579 void
1580 intel_hiz_exec(struct brw_context *brw, struct intel_mipmap_tree *mt,
1581 unsigned int level, unsigned int start_layer,
1582 unsigned int num_layers, enum isl_aux_op op)
1583 {
1584 assert(intel_miptree_level_has_hiz(mt, level));
1585 assert(op != ISL_AUX_OP_NONE);
1586 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1587 const char *opname = NULL;
1588
1589 switch (op) {
1590 case ISL_AUX_OP_FULL_RESOLVE:
1591 opname = "depth resolve";
1592 break;
1593 case ISL_AUX_OP_AMBIGUATE:
1594 opname = "hiz ambiguate";
1595 break;
1596 case ISL_AUX_OP_FAST_CLEAR:
1597 opname = "depth clear";
1598 break;
1599 case ISL_AUX_OP_PARTIAL_RESOLVE:
1600 case ISL_AUX_OP_NONE:
1601 unreachable("Invalid HiZ op");
1602 }
1603
1604 DBG("%s %s to mt %p level %d layers %d-%d\n",
1605 __func__, opname, mt, level, start_layer, start_layer + num_layers - 1);
1606
1607 /* The following stalls and flushes are only documented to be required for
1608 * HiZ clear operations. However, they also seem to be required for
1609 * resolve operations.
1610 */
1611 if (devinfo->gen == 6) {
1612 /* From the Sandy Bridge PRM, volume 2 part 1, page 313:
1613 *
1614 * "If other rendering operations have preceded this clear, a
1615 * PIPE_CONTROL with write cache flush enabled and Z-inhibit
1616 * disabled must be issued before the rectangle primitive used for
1617 * the depth buffer clear operation.
1618 */
1619 brw_emit_pipe_control_flush(brw,
1620 PIPE_CONTROL_RENDER_TARGET_FLUSH |
1621 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
1622 PIPE_CONTROL_CS_STALL);
1623 } else if (devinfo->gen >= 7) {
1624 /*
1625 * From the Ivybridge PRM, volume 2, "Depth Buffer Clear":
1626 *
1627 * If other rendering operations have preceded this clear, a
1628 * PIPE_CONTROL with depth cache flush enabled, Depth Stall bit
1629 * enabled must be issued before the rectangle primitive used for
1630 * the depth buffer clear operation.
1631 *
1632 * Same applies for Gen8 and Gen9.
1633 *
1634 * In addition, from the Ivybridge PRM, volume 2, 1.10.4.1
1635 * PIPE_CONTROL, Depth Cache Flush Enable:
1636 *
1637 * This bit must not be set when Depth Stall Enable bit is set in
1638 * this packet.
1639 *
1640 * This is confirmed to hold for real, HSW gets immediate gpu hangs.
1641 *
1642 * Therefore issue two pipe control flushes, one for cache flush and
1643 * another for depth stall.
1644 */
1645 brw_emit_pipe_control_flush(brw,
1646 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
1647 PIPE_CONTROL_CS_STALL);
1648
1649 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_DEPTH_STALL);
1650 }
1651
1652 assert(mt->aux_usage == ISL_AUX_USAGE_HIZ && mt->aux_buf);
1653
1654 struct blorp_surf surf;
1655 blorp_surf_for_miptree(brw, &surf, mt, ISL_AUX_USAGE_HIZ, true,
1656 &level, start_layer, num_layers);
1657
1658 struct blorp_batch batch;
1659 blorp_batch_init(&brw->blorp, &batch, brw,
1660 BLORP_BATCH_NO_UPDATE_CLEAR_COLOR);
1661 blorp_hiz_op(&batch, &surf, level, start_layer, num_layers, op);
1662 blorp_batch_finish(&batch);
1663
1664 /* The following stalls and flushes are only documented to be required for
1665 * HiZ clear operations. However, they also seem to be required for
1666 * resolve operations.
1667 */
1668 if (devinfo->gen == 6) {
1669 /* From the Sandy Bridge PRM, volume 2 part 1, page 314:
1670 *
1671 * "DevSNB, DevSNB-B{W/A}]: Depth buffer clear pass must be
1672 * followed by a PIPE_CONTROL command with DEPTH_STALL bit set
1673 * and Then followed by Depth FLUSH'
1674 */
1675 brw_emit_pipe_control_flush(brw,
1676 PIPE_CONTROL_DEPTH_STALL);
1677
1678 brw_emit_pipe_control_flush(brw,
1679 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
1680 PIPE_CONTROL_CS_STALL);
1681 } else if (devinfo->gen >= 8) {
1682 /*
1683 * From the Broadwell PRM, volume 7, "Depth Buffer Clear":
1684 *
1685 * "Depth buffer clear pass using any of the methods (WM_STATE,
1686 * 3DSTATE_WM or 3DSTATE_WM_HZ_OP) must be followed by a
1687 * PIPE_CONTROL command with DEPTH_STALL bit and Depth FLUSH bits
1688 * "set" before starting to render. DepthStall and DepthFlush are
1689 * not needed between consecutive depth clear passes nor is it
1690 * required if the depth clear pass was done with
1691 * 'full_surf_clear' bit set in the 3DSTATE_WM_HZ_OP."
1692 *
1693 * TODO: Such as the spec says, this could be conditional.
1694 */
1695 brw_emit_pipe_control_flush(brw,
1696 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
1697 PIPE_CONTROL_DEPTH_STALL);
1698
1699 }
1700 }