intel/blorp/gen4: Drop cube map flag for single face copy
[mesa.git] / src / intel / blorp / blorp_clear.c
1 /*
2 * Copyright © 2013 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 "util/ralloc.h"
25
26 #include "main/macros.h" /* Needed for MAX3 and MAX2 for format_rgb9e5 */
27 #include "util/format_rgb9e5.h"
28
29 #include "blorp_priv.h"
30 #include "compiler/brw_eu_defines.h"
31
32 #include "compiler/nir/nir_builder.h"
33
34 #define FILE_DEBUG_FLAG DEBUG_BLORP
35
36 struct brw_blorp_const_color_prog_key
37 {
38 enum blorp_shader_type shader_type; /* Must be BLORP_SHADER_TYPE_CLEAR */
39 bool use_simd16_replicated_data;
40 bool pad[3];
41 };
42
43 static bool
44 blorp_params_get_clear_kernel(struct blorp_context *blorp,
45 struct blorp_params *params,
46 bool use_replicated_data)
47 {
48 const struct brw_blorp_const_color_prog_key blorp_key = {
49 .shader_type = BLORP_SHADER_TYPE_CLEAR,
50 .use_simd16_replicated_data = use_replicated_data,
51 };
52
53 if (blorp->lookup_shader(blorp, &blorp_key, sizeof(blorp_key),
54 &params->wm_prog_kernel, &params->wm_prog_data))
55 return true;
56
57 void *mem_ctx = ralloc_context(NULL);
58
59 nir_builder b;
60 nir_builder_init_simple_shader(&b, mem_ctx, MESA_SHADER_FRAGMENT, NULL);
61 b.shader->info.name = ralloc_strdup(b.shader, "BLORP-clear");
62
63 nir_variable *v_color =
64 BLORP_CREATE_NIR_INPUT(b.shader, clear_color, glsl_vec4_type());
65
66 nir_variable *frag_color = nir_variable_create(b.shader, nir_var_shader_out,
67 glsl_vec4_type(),
68 "gl_FragColor");
69 frag_color->data.location = FRAG_RESULT_COLOR;
70
71 nir_copy_var(&b, frag_color, v_color);
72
73 struct brw_wm_prog_key wm_key;
74 brw_blorp_init_wm_prog_key(&wm_key);
75
76 struct brw_wm_prog_data prog_data;
77 unsigned program_size;
78 const unsigned *program =
79 blorp_compile_fs(blorp, mem_ctx, b.shader, &wm_key, use_replicated_data,
80 &prog_data, &program_size);
81
82 bool result =
83 blorp->upload_shader(blorp, &blorp_key, sizeof(blorp_key),
84 program, program_size,
85 &prog_data.base, sizeof(prog_data),
86 &params->wm_prog_kernel, &params->wm_prog_data);
87
88 ralloc_free(mem_ctx);
89 return result;
90 }
91
92 struct layer_offset_vs_key {
93 enum blorp_shader_type shader_type;
94 unsigned num_inputs;
95 };
96
97 /* In the case of doing attachment clears, we are using a surface state that
98 * is handed to us so we can't set (and don't even know) the base array layer.
99 * In order to do a layered clear in this scenario, we need some way of adding
100 * the base array layer to the instance id. Unfortunately, our hardware has
101 * no real concept of "base instance", so we have to do it manually in a
102 * vertex shader.
103 */
104 static bool
105 blorp_params_get_layer_offset_vs(struct blorp_context *blorp,
106 struct blorp_params *params)
107 {
108 struct layer_offset_vs_key blorp_key = {
109 .shader_type = BLORP_SHADER_TYPE_LAYER_OFFSET_VS,
110 };
111
112 if (params->wm_prog_data)
113 blorp_key.num_inputs = params->wm_prog_data->num_varying_inputs;
114
115 if (blorp->lookup_shader(blorp, &blorp_key, sizeof(blorp_key),
116 &params->vs_prog_kernel, &params->vs_prog_data))
117 return true;
118
119 void *mem_ctx = ralloc_context(NULL);
120
121 nir_builder b;
122 nir_builder_init_simple_shader(&b, mem_ctx, MESA_SHADER_VERTEX, NULL);
123 b.shader->info.name = ralloc_strdup(b.shader, "BLORP-layer-offset-vs");
124
125 const struct glsl_type *uvec4_type = glsl_vector_type(GLSL_TYPE_UINT, 4);
126
127 /* First we deal with the header which has instance and base instance */
128 nir_variable *a_header = nir_variable_create(b.shader, nir_var_shader_in,
129 uvec4_type, "header");
130 a_header->data.location = VERT_ATTRIB_GENERIC0;
131
132 nir_variable *v_layer = nir_variable_create(b.shader, nir_var_shader_out,
133 glsl_int_type(), "layer_id");
134 v_layer->data.location = VARYING_SLOT_LAYER;
135
136 /* Compute the layer id */
137 nir_ssa_def *header = nir_load_var(&b, a_header);
138 nir_ssa_def *base_layer = nir_channel(&b, header, 0);
139 nir_ssa_def *instance = nir_channel(&b, header, 1);
140 nir_store_var(&b, v_layer, nir_iadd(&b, instance, base_layer), 0x1);
141
142 /* Then we copy the vertex from the next slot to VARYING_SLOT_POS */
143 nir_variable *a_vertex = nir_variable_create(b.shader, nir_var_shader_in,
144 glsl_vec4_type(), "a_vertex");
145 a_vertex->data.location = VERT_ATTRIB_GENERIC1;
146
147 nir_variable *v_pos = nir_variable_create(b.shader, nir_var_shader_out,
148 glsl_vec4_type(), "v_pos");
149 v_pos->data.location = VARYING_SLOT_POS;
150
151 nir_copy_var(&b, v_pos, a_vertex);
152
153 /* Then we copy everything else */
154 for (unsigned i = 0; i < blorp_key.num_inputs; i++) {
155 nir_variable *a_in = nir_variable_create(b.shader, nir_var_shader_in,
156 uvec4_type, "input");
157 a_in->data.location = VERT_ATTRIB_GENERIC2 + i;
158
159 nir_variable *v_out = nir_variable_create(b.shader, nir_var_shader_out,
160 uvec4_type, "output");
161 v_out->data.location = VARYING_SLOT_VAR0 + i;
162
163 nir_copy_var(&b, v_out, a_in);
164 }
165
166 struct brw_vs_prog_data vs_prog_data;
167 memset(&vs_prog_data, 0, sizeof(vs_prog_data));
168
169 unsigned program_size;
170 const unsigned *program =
171 blorp_compile_vs(blorp, mem_ctx, b.shader, &vs_prog_data, &program_size);
172
173 bool result =
174 blorp->upload_shader(blorp, &blorp_key, sizeof(blorp_key),
175 program, program_size,
176 &vs_prog_data.base.base, sizeof(vs_prog_data),
177 &params->vs_prog_kernel, &params->vs_prog_data);
178
179 ralloc_free(mem_ctx);
180 return result;
181 }
182
183 /* The x0, y0, x1, and y1 parameters must already be populated with the render
184 * area of the framebuffer to be cleared.
185 */
186 static void
187 get_fast_clear_rect(const struct isl_device *dev,
188 const struct isl_surf *aux_surf,
189 unsigned *x0, unsigned *y0,
190 unsigned *x1, unsigned *y1)
191 {
192 unsigned int x_align, y_align;
193 unsigned int x_scaledown, y_scaledown;
194
195 /* Only single sampled surfaces need to (and actually can) be resolved. */
196 if (aux_surf->usage == ISL_SURF_USAGE_CCS_BIT) {
197 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
198 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
199 *
200 * Clear pass must have a clear rectangle that must follow
201 * alignment rules in terms of pixels and lines as shown in the
202 * table below. Further, the clear-rectangle height and width
203 * must be multiple of the following dimensions. If the height
204 * and width of the render target being cleared do not meet these
205 * requirements, an MCS buffer can be created such that it
206 * follows the requirement and covers the RT.
207 *
208 * The alignment size in the table that follows is related to the
209 * alignment size that is baked into the CCS surface format but with X
210 * alignment multiplied by 16 and Y alignment multiplied by 32.
211 */
212 x_align = isl_format_get_layout(aux_surf->format)->bw;
213 y_align = isl_format_get_layout(aux_surf->format)->bh;
214
215 x_align *= 16;
216
217 /* SKL+ line alignment requirement for Y-tiled are half those of the prior
218 * generations.
219 */
220 if (dev->info->gen >= 9)
221 y_align *= 16;
222 else
223 y_align *= 32;
224
225 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
226 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
227 *
228 * In order to optimize the performance MCS buffer (when bound to
229 * 1X RT) clear similarly to MCS buffer clear for MSRT case,
230 * clear rect is required to be scaled by the following factors
231 * in the horizontal and vertical directions:
232 *
233 * The X and Y scale down factors in the table that follows are each
234 * equal to half the alignment value computed above.
235 */
236 x_scaledown = x_align / 2;
237 y_scaledown = y_align / 2;
238
239 /* From BSpec: 3D-Media-GPGPU Engine > 3D Pipeline > Pixel > Pixel
240 * Backend > MCS Buffer for Render Target(s) [DevIVB+] > Table "Color
241 * Clear of Non-MultiSampled Render Target Restrictions":
242 *
243 * Clear rectangle must be aligned to two times the number of
244 * pixels in the table shown below due to 16x16 hashing across the
245 * slice.
246 */
247 x_align *= 2;
248 y_align *= 2;
249 } else {
250 assert(aux_surf->usage == ISL_SURF_USAGE_MCS_BIT);
251
252 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
253 * Target(s)", beneath the "MSAA Compression" bullet (p326):
254 *
255 * Clear pass for this case requires that scaled down primitive
256 * is sent down with upper left co-ordinate to coincide with
257 * actual rectangle being cleared. For MSAA, clear rectangle’s
258 * height and width need to as show in the following table in
259 * terms of (width,height) of the RT.
260 *
261 * MSAA Width of Clear Rect Height of Clear Rect
262 * 2X Ceil(1/8*width) Ceil(1/2*height)
263 * 4X Ceil(1/8*width) Ceil(1/2*height)
264 * 8X Ceil(1/2*width) Ceil(1/2*height)
265 * 16X width Ceil(1/2*height)
266 *
267 * The text "with upper left co-ordinate to coincide with actual
268 * rectangle being cleared" is a little confusing--it seems to imply
269 * that to clear a rectangle from (x,y) to (x+w,y+h), one needs to
270 * feed the pipeline using the rectangle (x,y) to
271 * (x+Ceil(w/N),y+Ceil(h/2)), where N is either 2 or 8 depending on
272 * the number of samples. Experiments indicate that this is not
273 * quite correct; actually, what the hardware appears to do is to
274 * align whatever rectangle is sent down the pipeline to the nearest
275 * multiple of 2x2 blocks, and then scale it up by a factor of N
276 * horizontally and 2 vertically. So the resulting alignment is 4
277 * vertically and either 4 or 16 horizontally, and the scaledown
278 * factor is 2 vertically and either 2 or 8 horizontally.
279 */
280 switch (aux_surf->format) {
281 case ISL_FORMAT_MCS_2X:
282 case ISL_FORMAT_MCS_4X:
283 x_scaledown = 8;
284 break;
285 case ISL_FORMAT_MCS_8X:
286 x_scaledown = 2;
287 break;
288 case ISL_FORMAT_MCS_16X:
289 x_scaledown = 1;
290 break;
291 default:
292 unreachable("Unexpected MCS format for fast clear");
293 }
294 y_scaledown = 2;
295 x_align = x_scaledown * 2;
296 y_align = y_scaledown * 2;
297 }
298
299 *x0 = ROUND_DOWN_TO(*x0, x_align) / x_scaledown;
300 *y0 = ROUND_DOWN_TO(*y0, y_align) / y_scaledown;
301 *x1 = ALIGN(*x1, x_align) / x_scaledown;
302 *y1 = ALIGN(*y1, y_align) / y_scaledown;
303 }
304
305 void
306 blorp_fast_clear(struct blorp_batch *batch,
307 const struct blorp_surf *surf, enum isl_format format,
308 uint32_t level, uint32_t start_layer, uint32_t num_layers,
309 uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1)
310 {
311 /* Ensure that all layers undergoing the clear have an auxiliary buffer. */
312 assert(start_layer + num_layers <=
313 MAX2(surf->aux_surf->logical_level0_px.depth >> level,
314 surf->aux_surf->logical_level0_px.array_len));
315
316 struct blorp_params params;
317 blorp_params_init(&params);
318 params.num_layers = num_layers;
319
320 params.x0 = x0;
321 params.y0 = y0;
322 params.x1 = x1;
323 params.y1 = y1;
324
325 memset(&params.wm_inputs.clear_color, 0xff, 4*sizeof(float));
326 params.fast_clear_op = BLORP_FAST_CLEAR_OP_CLEAR;
327
328 get_fast_clear_rect(batch->blorp->isl_dev, surf->aux_surf,
329 &params.x0, &params.y0, &params.x1, &params.y1);
330
331 if (!blorp_params_get_clear_kernel(batch->blorp, &params, true))
332 return;
333
334 brw_blorp_surface_info_init(batch->blorp, &params.dst, surf, level,
335 start_layer, format, true);
336 params.num_samples = params.dst.surf.samples;
337
338 batch->blorp->exec(batch, &params);
339 }
340
341 static union isl_color_value
342 swizzle_color_value(union isl_color_value src, struct isl_swizzle swizzle)
343 {
344 union isl_color_value dst = { .u32 = { 0, } };
345
346 /* We assign colors in ABGR order so that the first one will be taken in
347 * RGBA precedence order. According to the PRM docs for shader channel
348 * select, this matches Haswell hardware behavior.
349 */
350 if ((unsigned)(swizzle.a - ISL_CHANNEL_SELECT_RED) < 4)
351 dst.u32[swizzle.a - ISL_CHANNEL_SELECT_RED] = src.u32[3];
352 if ((unsigned)(swizzle.b - ISL_CHANNEL_SELECT_RED) < 4)
353 dst.u32[swizzle.b - ISL_CHANNEL_SELECT_RED] = src.u32[2];
354 if ((unsigned)(swizzle.g - ISL_CHANNEL_SELECT_RED) < 4)
355 dst.u32[swizzle.g - ISL_CHANNEL_SELECT_RED] = src.u32[1];
356 if ((unsigned)(swizzle.r - ISL_CHANNEL_SELECT_RED) < 4)
357 dst.u32[swizzle.r - ISL_CHANNEL_SELECT_RED] = src.u32[0];
358
359 return dst;
360 }
361
362 void
363 blorp_clear(struct blorp_batch *batch,
364 const struct blorp_surf *surf,
365 enum isl_format format, struct isl_swizzle swizzle,
366 uint32_t level, uint32_t start_layer, uint32_t num_layers,
367 uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1,
368 union isl_color_value clear_color,
369 const bool color_write_disable[4])
370 {
371 struct blorp_params params;
372 blorp_params_init(&params);
373
374 /* Manually apply the clear destination swizzle. This way swizzled clears
375 * will work for swizzles which we can't normally use for rendering and it
376 * also ensures that they work on pre-Haswell hardware which can't swizlle
377 * at all.
378 */
379 clear_color = swizzle_color_value(clear_color, swizzle);
380 swizzle = ISL_SWIZZLE_IDENTITY;
381
382 if (format == ISL_FORMAT_R9G9B9E5_SHAREDEXP) {
383 clear_color.u32[0] = float3_to_rgb9e5(clear_color.f32);
384 format = ISL_FORMAT_R32_UINT;
385 } else if (format == ISL_FORMAT_A4B4G4R4_UNORM) {
386 /* Broadwell and earlier cannot render to this format so we need to work
387 * around it by swapping the colors around and using B4G4R4A4 instead.
388 */
389 const struct isl_swizzle ARGB = ISL_SWIZZLE(ALPHA, RED, GREEN, BLUE);
390 clear_color = swizzle_color_value(clear_color, ARGB);
391 format = ISL_FORMAT_B4G4R4A4_UNORM;
392 }
393
394 memcpy(&params.wm_inputs.clear_color, clear_color.f32, sizeof(float) * 4);
395
396 bool use_simd16_replicated_data = true;
397
398 /* From the SNB PRM (Vol4_Part1):
399 *
400 * "Replicated data (Message Type = 111) is only supported when
401 * accessing tiled memory. Using this Message Type to access linear
402 * (untiled) memory is UNDEFINED."
403 */
404 if (surf->surf->tiling == ISL_TILING_LINEAR)
405 use_simd16_replicated_data = false;
406
407 /* Replicated clears don't work yet before gen6 */
408 if (batch->blorp->isl_dev->info->gen < 6)
409 use_simd16_replicated_data = false;
410
411 /* Constant color writes ignore everyting in blend and color calculator
412 * state. This is not documented.
413 */
414 if (color_write_disable) {
415 for (unsigned i = 0; i < 4; i++) {
416 params.color_write_disable[i] = color_write_disable[i];
417 if (color_write_disable[i])
418 use_simd16_replicated_data = false;
419 }
420 }
421
422 if (!blorp_params_get_clear_kernel(batch->blorp, &params,
423 use_simd16_replicated_data))
424 return;
425
426 if (!blorp_ensure_sf_program(batch->blorp, &params))
427 return;
428
429 while (num_layers > 0) {
430 brw_blorp_surface_info_init(batch->blorp, &params.dst, surf, level,
431 start_layer, format, true);
432 params.dst.view.swizzle = swizzle;
433
434 params.x0 = x0;
435 params.y0 = y0;
436 params.x1 = x1;
437 params.y1 = y1;
438
439 /* The MinLOD and MinimumArrayElement don't work properly for cube maps.
440 * Convert them to a single slice on gen4.
441 */
442 if (batch->blorp->isl_dev->info->gen == 4 &&
443 (params.dst.surf.usage & ISL_SURF_USAGE_CUBE_BIT)) {
444 blorp_surf_convert_to_single_slice(batch->blorp->isl_dev, &params.dst);
445
446 if (params.dst.tile_x_sa || params.dst.tile_y_sa) {
447 /* This is gen4 so there is no multisampling and sa == px. */
448 params.x0 += params.dst.tile_x_sa;
449 params.y0 += params.dst.tile_y_sa;
450 params.x1 += params.dst.tile_x_sa;
451 params.y1 += params.dst.tile_y_sa;
452 }
453 }
454
455 params.num_samples = params.dst.surf.samples;
456
457 /* We may be restricted on the number of layers we can bind at any one
458 * time. In particular, Sandy Bridge has a maximum number of layers of
459 * 512 but a maximum 3D texture size is much larger.
460 */
461 params.num_layers = MIN2(params.dst.view.array_len, num_layers);
462 batch->blorp->exec(batch, &params);
463
464 start_layer += params.num_layers;
465 num_layers -= params.num_layers;
466 }
467 }
468
469 void
470 blorp_clear_depth_stencil(struct blorp_batch *batch,
471 const struct blorp_surf *depth,
472 const struct blorp_surf *stencil,
473 uint32_t level, uint32_t start_layer,
474 uint32_t num_layers,
475 uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1,
476 bool clear_depth, float depth_value,
477 uint8_t stencil_mask, uint8_t stencil_value)
478 {
479 struct blorp_params params;
480 blorp_params_init(&params);
481
482 params.x0 = x0;
483 params.y0 = y0;
484 params.x1 = x1;
485 params.y1 = y1;
486
487 if (ISL_DEV_GEN(batch->blorp->isl_dev) == 6) {
488 /* For some reason, Sandy Bridge gets occlusion queries wrong if we
489 * don't have a shader. In particular, it records samples even though
490 * we disable statistics in 3DSTATE_WM. Give it the usual clear shader
491 * to work around the issue.
492 */
493 if (!blorp_params_get_clear_kernel(batch->blorp, &params, false))
494 return;
495 }
496
497 while (num_layers > 0) {
498 params.num_layers = num_layers;
499
500 if (stencil_mask) {
501 brw_blorp_surface_info_init(batch->blorp, &params.stencil, stencil,
502 level, start_layer,
503 ISL_FORMAT_UNSUPPORTED, true);
504 params.stencil_mask = stencil_mask;
505 params.stencil_ref = stencil_value;
506
507 params.dst.surf.samples = params.stencil.surf.samples;
508 params.dst.surf.logical_level0_px =
509 params.stencil.surf.logical_level0_px;
510 params.dst.view = params.depth.view;
511
512 params.num_samples = params.stencil.surf.samples;
513
514 /* We may be restricted on the number of layers we can bind at any
515 * one time. In particular, Sandy Bridge has a maximum number of
516 * layers of 512 but a maximum 3D texture size is much larger.
517 */
518 if (params.stencil.view.array_len < params.num_layers)
519 params.num_layers = params.stencil.view.array_len;
520 }
521
522 if (clear_depth) {
523 brw_blorp_surface_info_init(batch->blorp, &params.depth, depth,
524 level, start_layer,
525 ISL_FORMAT_UNSUPPORTED, true);
526 params.z = depth_value;
527 params.depth_format =
528 isl_format_get_depth_format(depth->surf->format, false);
529
530 params.dst.surf.samples = params.depth.surf.samples;
531 params.dst.surf.logical_level0_px =
532 params.depth.surf.logical_level0_px;
533 params.dst.view = params.depth.view;
534
535 params.num_samples = params.depth.surf.samples;
536
537 /* We may be restricted on the number of layers we can bind at any
538 * one time. In particular, Sandy Bridge has a maximum number of
539 * layers of 512 but a maximum 3D texture size is much larger.
540 */
541 if (params.depth.view.array_len < params.num_layers)
542 params.num_layers = params.depth.view.array_len;
543 }
544
545 batch->blorp->exec(batch, &params);
546
547 start_layer += params.num_layers;
548 num_layers -= params.num_layers;
549 }
550 }
551
552 bool
553 blorp_can_hiz_clear_depth(uint8_t gen, enum isl_format format,
554 uint32_t num_samples,
555 uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1)
556 {
557 /* This function currently doesn't support any gen prior to gen8 */
558 assert(gen >= 8);
559
560 if (gen == 8 && format == ISL_FORMAT_R16_UNORM) {
561 /* Apply the D16 alignment restrictions. On BDW, HiZ has an 8x4 sample
562 * block with the following property: as the number of samples increases,
563 * the number of pixels representable by this block decreases by a factor
564 * of the sample dimensions. Sample dimensions scale following the MSAA
565 * interleaved pattern.
566 *
567 * Sample|Sample|Pixel
568 * Count |Dim |Dim
569 * ===================
570 * 1 | 1x1 | 8x4
571 * 2 | 2x1 | 4x4
572 * 4 | 2x2 | 4x2
573 * 8 | 4x2 | 2x2
574 * 16 | 4x4 | 2x1
575 *
576 * Table: Pixel Dimensions in a HiZ Sample Block Pre-SKL
577 */
578 const struct isl_extent2d sa_block_dim =
579 isl_get_interleaved_msaa_px_size_sa(num_samples);
580 const uint8_t align_px_w = 8 / sa_block_dim.w;
581 const uint8_t align_px_h = 4 / sa_block_dim.h;
582
583 /* Fast depth clears clear an entire sample block at a time. As a result,
584 * the rectangle must be aligned to the dimensions of the encompassing
585 * pixel block for a successful operation.
586 *
587 * Fast clears can still work if the upper-left corner is aligned and the
588 * bottom-rigtht corner touches the edge of a depth buffer whose extent
589 * is unaligned. This is because each miplevel in the depth buffer is
590 * padded by the Pixel Dim (similar to a standard compressed texture).
591 * In this case, the clear rectangle could be padded by to match the full
592 * depth buffer extent but to support multiple clearing techniques, we
593 * chose to be unaware of the depth buffer's extent and thus don't handle
594 * this case.
595 */
596 if (x0 % align_px_w || y0 % align_px_h ||
597 x1 % align_px_w || y1 % align_px_h)
598 return false;
599 }
600 return true;
601 }
602
603 /* Given a depth stencil attachment, this function performs a fast depth clear
604 * on a depth portion and a regular clear on the stencil portion. When
605 * performing a fast depth clear on the depth portion, the HiZ buffer is simply
606 * tagged as cleared so the depth clear value is not actually needed.
607 */
608 void
609 blorp_gen8_hiz_clear_attachments(struct blorp_batch *batch,
610 uint32_t num_samples,
611 uint32_t x0, uint32_t y0,
612 uint32_t x1, uint32_t y1,
613 bool clear_depth, bool clear_stencil,
614 uint8_t stencil_value)
615 {
616 assert(batch->flags & BLORP_BATCH_NO_EMIT_DEPTH_STENCIL);
617
618 struct blorp_params params;
619 blorp_params_init(&params);
620 params.num_layers = 1;
621 params.hiz_op = BLORP_HIZ_OP_DEPTH_CLEAR;
622 params.x0 = x0;
623 params.y0 = y0;
624 params.x1 = x1;
625 params.y1 = y1;
626 params.num_samples = num_samples;
627 params.depth.enabled = clear_depth;
628 params.stencil.enabled = clear_stencil;
629 params.stencil_ref = stencil_value;
630 batch->blorp->exec(batch, &params);
631 }
632
633 /** Clear active color/depth/stencili attachments
634 *
635 * This function performs a clear operation on the currently bound
636 * color/depth/stencil attachments. It is assumed that any information passed
637 * in here is valid, consistent, and in-bounds relative to the currently
638 * attached depth/stencil. The binding_table_offset parameter is the 32-bit
639 * offset relative to surface state base address where pre-baked binding table
640 * that we are to use lives. If clear_color is false, binding_table_offset
641 * must point to a binding table with one entry which is a valid null surface
642 * that matches the currently bound depth and stencil.
643 */
644 void
645 blorp_clear_attachments(struct blorp_batch *batch,
646 uint32_t binding_table_offset,
647 enum isl_format depth_format,
648 uint32_t num_samples,
649 uint32_t start_layer, uint32_t num_layers,
650 uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1,
651 bool clear_color, union isl_color_value color_value,
652 bool clear_depth, float depth_value,
653 uint8_t stencil_mask, uint8_t stencil_value)
654 {
655 struct blorp_params params;
656 blorp_params_init(&params);
657
658 assert(batch->flags & BLORP_BATCH_NO_EMIT_DEPTH_STENCIL);
659
660 params.x0 = x0;
661 params.y0 = y0;
662 params.x1 = x1;
663 params.y1 = y1;
664
665 params.use_pre_baked_binding_table = true;
666 params.pre_baked_binding_table_offset = binding_table_offset;
667
668 params.num_layers = num_layers;
669 params.num_samples = num_samples;
670
671 if (clear_color) {
672 params.dst.enabled = true;
673
674 memcpy(&params.wm_inputs.clear_color, color_value.f32, sizeof(float) * 4);
675
676 /* Unfortunately, without knowing whether or not our destination surface
677 * is tiled or not, we have to assume it may be linear. This means no
678 * SIMD16_REPDATA for us. :-(
679 */
680 if (!blorp_params_get_clear_kernel(batch->blorp, &params, false))
681 return;
682 }
683
684 if (clear_depth) {
685 params.depth.enabled = true;
686
687 params.z = depth_value;
688 params.depth_format = isl_format_get_depth_format(depth_format, false);
689 }
690
691 if (stencil_mask) {
692 params.stencil.enabled = true;
693
694 params.stencil_mask = stencil_mask;
695 params.stencil_ref = stencil_value;
696 }
697
698 if (!blorp_params_get_layer_offset_vs(batch->blorp, &params))
699 return;
700
701 params.vs_inputs.base_layer = start_layer;
702
703 batch->blorp->exec(batch, &params);
704 }
705
706 static void
707 prepare_ccs_resolve(struct blorp_batch * const batch,
708 struct blorp_params * const params,
709 const struct blorp_surf * const surf,
710 const uint32_t level, const uint32_t layer,
711 const enum isl_format format,
712 const enum blorp_fast_clear_op resolve_op)
713 {
714 blorp_params_init(params);
715 brw_blorp_surface_info_init(batch->blorp, &params->dst, surf,
716 level, layer, format, true);
717
718 /* From the Ivy Bridge PRM, Vol2 Part1 11.9 "Render Target Resolve":
719 *
720 * A rectangle primitive must be scaled down by the following factors
721 * with respect to render target being resolved.
722 *
723 * The scaledown factors in the table that follows are related to the block
724 * size of the CCS format. For IVB and HSW, we divide by two, for BDW we
725 * multiply by 8 and 16. On Sky Lake, we multiply by 8.
726 */
727 const struct isl_format_layout *aux_fmtl =
728 isl_format_get_layout(params->dst.aux_surf.format);
729 assert(aux_fmtl->txc == ISL_TXC_CCS);
730
731 unsigned x_scaledown, y_scaledown;
732 if (ISL_DEV_GEN(batch->blorp->isl_dev) >= 9) {
733 x_scaledown = aux_fmtl->bw * 8;
734 y_scaledown = aux_fmtl->bh * 8;
735 } else if (ISL_DEV_GEN(batch->blorp->isl_dev) >= 8) {
736 x_scaledown = aux_fmtl->bw * 8;
737 y_scaledown = aux_fmtl->bh * 16;
738 } else {
739 x_scaledown = aux_fmtl->bw / 2;
740 y_scaledown = aux_fmtl->bh / 2;
741 }
742 params->x0 = params->y0 = 0;
743 params->x1 = minify(params->dst.aux_surf.logical_level0_px.width, level);
744 params->y1 = minify(params->dst.aux_surf.logical_level0_px.height, level);
745 params->x1 = ALIGN(params->x1, x_scaledown) / x_scaledown;
746 params->y1 = ALIGN(params->y1, y_scaledown) / y_scaledown;
747
748 if (batch->blorp->isl_dev->info->gen >= 9) {
749 assert(resolve_op == BLORP_FAST_CLEAR_OP_RESOLVE_FULL ||
750 resolve_op == BLORP_FAST_CLEAR_OP_RESOLVE_PARTIAL);
751 } else {
752 /* Broadwell and earlier do not have a partial resolve */
753 assert(resolve_op == BLORP_FAST_CLEAR_OP_RESOLVE_FULL);
754 }
755 params->fast_clear_op = resolve_op;
756
757 /* Note: there is no need to initialize push constants because it doesn't
758 * matter what data gets dispatched to the render target. However, we must
759 * ensure that the fragment shader delivers the data using the "replicated
760 * color" message.
761 */
762
763 if (!blorp_params_get_clear_kernel(batch->blorp, params, true))
764 return;
765 }
766
767 void
768 blorp_ccs_resolve(struct blorp_batch *batch,
769 struct blorp_surf *surf, uint32_t level, uint32_t layer,
770 enum isl_format format,
771 enum blorp_fast_clear_op resolve_op)
772 {
773 struct blorp_params params;
774
775 prepare_ccs_resolve(batch, &params, surf, level, layer, format, resolve_op);
776
777 batch->blorp->exec(batch, &params);
778 }
779
780 void
781 blorp_ccs_resolve_attachment(struct blorp_batch *batch,
782 const uint32_t binding_table_offset,
783 struct blorp_surf * const surf,
784 const uint32_t level, const uint32_t num_layers,
785 const enum isl_format format,
786 const enum blorp_fast_clear_op resolve_op)
787 {
788 struct blorp_params params;
789
790 prepare_ccs_resolve(batch, &params, surf, level, 0, format, resolve_op);
791 params.use_pre_baked_binding_table = true;
792 params.pre_baked_binding_table_offset = binding_table_offset;
793 params.num_layers = num_layers;
794
795 batch->blorp->exec(batch, &params);
796 }