Added few more stubs so that control reaches to DestroyDevice().
[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 #include "util/format_srgb.h"
29
30 #include "blorp_priv.h"
31 #include "compiler/brw_eu_defines.h"
32
33 #include "blorp_nir_builder.h"
34
35 #define FILE_DEBUG_FLAG DEBUG_BLORP
36
37 #pragma pack(push, 1)
38 struct brw_blorp_const_color_prog_key
39 {
40 enum blorp_shader_type shader_type; /* Must be BLORP_SHADER_TYPE_CLEAR */
41 bool use_simd16_replicated_data;
42 bool clear_rgb_as_red;
43 };
44 #pragma pack(pop)
45
46 static bool
47 blorp_params_get_clear_kernel(struct blorp_batch *batch,
48 struct blorp_params *params,
49 bool use_replicated_data,
50 bool clear_rgb_as_red)
51 {
52 struct blorp_context *blorp = batch->blorp;
53
54 const struct brw_blorp_const_color_prog_key blorp_key = {
55 .shader_type = BLORP_SHADER_TYPE_CLEAR,
56 .use_simd16_replicated_data = use_replicated_data,
57 .clear_rgb_as_red = clear_rgb_as_red,
58 };
59
60 if (blorp->lookup_shader(batch, &blorp_key, sizeof(blorp_key),
61 &params->wm_prog_kernel, &params->wm_prog_data))
62 return true;
63
64 void *mem_ctx = ralloc_context(NULL);
65
66 nir_builder b;
67 blorp_nir_init_shader(&b, mem_ctx, MESA_SHADER_FRAGMENT, "BLORP-clear");
68
69 nir_variable *v_color =
70 BLORP_CREATE_NIR_INPUT(b.shader, clear_color, glsl_vec4_type());
71 nir_ssa_def *color = nir_load_var(&b, v_color);
72
73 if (clear_rgb_as_red) {
74 nir_ssa_def *pos = nir_f2i32(&b, nir_load_frag_coord(&b));
75 nir_ssa_def *comp = nir_umod(&b, nir_channel(&b, pos, 0),
76 nir_imm_int(&b, 3));
77 nir_ssa_def *color_component =
78 nir_bcsel(&b, nir_ieq(&b, comp, nir_imm_int(&b, 0)),
79 nir_channel(&b, color, 0),
80 nir_bcsel(&b, nir_ieq(&b, comp, nir_imm_int(&b, 1)),
81 nir_channel(&b, color, 1),
82 nir_channel(&b, color, 2)));
83
84 nir_ssa_def *u = nir_ssa_undef(&b, 1, 32);
85 color = nir_vec4(&b, color_component, u, u, u);
86 }
87
88 nir_variable *frag_color = nir_variable_create(b.shader, nir_var_shader_out,
89 glsl_vec4_type(),
90 "gl_FragColor");
91 frag_color->data.location = FRAG_RESULT_COLOR;
92 nir_store_var(&b, frag_color, color, 0xf);
93
94 struct brw_wm_prog_key wm_key;
95 brw_blorp_init_wm_prog_key(&wm_key);
96
97 struct brw_wm_prog_data prog_data;
98 const unsigned *program =
99 blorp_compile_fs(blorp, mem_ctx, b.shader, &wm_key, use_replicated_data,
100 &prog_data);
101
102 bool result =
103 blorp->upload_shader(batch, MESA_SHADER_FRAGMENT,
104 &blorp_key, sizeof(blorp_key),
105 program, prog_data.base.program_size,
106 &prog_data.base, sizeof(prog_data),
107 &params->wm_prog_kernel, &params->wm_prog_data);
108
109 ralloc_free(mem_ctx);
110 return result;
111 }
112
113 #pragma pack(push, 1)
114 struct layer_offset_vs_key {
115 enum blorp_shader_type shader_type;
116 unsigned num_inputs;
117 };
118 #pragma pack(pop)
119
120 /* In the case of doing attachment clears, we are using a surface state that
121 * is handed to us so we can't set (and don't even know) the base array layer.
122 * In order to do a layered clear in this scenario, we need some way of adding
123 * the base array layer to the instance id. Unfortunately, our hardware has
124 * no real concept of "base instance", so we have to do it manually in a
125 * vertex shader.
126 */
127 static bool
128 blorp_params_get_layer_offset_vs(struct blorp_batch *batch,
129 struct blorp_params *params)
130 {
131 struct blorp_context *blorp = batch->blorp;
132 struct layer_offset_vs_key blorp_key = {
133 .shader_type = BLORP_SHADER_TYPE_LAYER_OFFSET_VS,
134 };
135
136 if (params->wm_prog_data)
137 blorp_key.num_inputs = params->wm_prog_data->num_varying_inputs;
138
139 if (blorp->lookup_shader(batch, &blorp_key, sizeof(blorp_key),
140 &params->vs_prog_kernel, &params->vs_prog_data))
141 return true;
142
143 void *mem_ctx = ralloc_context(NULL);
144
145 nir_builder b;
146 blorp_nir_init_shader(&b, mem_ctx, MESA_SHADER_VERTEX, "BLORP-layer-offset-vs");
147
148 const struct glsl_type *uvec4_type = glsl_vector_type(GLSL_TYPE_UINT, 4);
149
150 /* First we deal with the header which has instance and base instance */
151 nir_variable *a_header = nir_variable_create(b.shader, nir_var_shader_in,
152 uvec4_type, "header");
153 a_header->data.location = VERT_ATTRIB_GENERIC0;
154
155 nir_variable *v_layer = nir_variable_create(b.shader, nir_var_shader_out,
156 glsl_int_type(), "layer_id");
157 v_layer->data.location = VARYING_SLOT_LAYER;
158
159 /* Compute the layer id */
160 nir_ssa_def *header = nir_load_var(&b, a_header);
161 nir_ssa_def *base_layer = nir_channel(&b, header, 0);
162 nir_ssa_def *instance = nir_channel(&b, header, 1);
163 nir_store_var(&b, v_layer, nir_iadd(&b, instance, base_layer), 0x1);
164
165 /* Then we copy the vertex from the next slot to VARYING_SLOT_POS */
166 nir_variable *a_vertex = nir_variable_create(b.shader, nir_var_shader_in,
167 glsl_vec4_type(), "a_vertex");
168 a_vertex->data.location = VERT_ATTRIB_GENERIC1;
169
170 nir_variable *v_pos = nir_variable_create(b.shader, nir_var_shader_out,
171 glsl_vec4_type(), "v_pos");
172 v_pos->data.location = VARYING_SLOT_POS;
173
174 nir_copy_var(&b, v_pos, a_vertex);
175
176 /* Then we copy everything else */
177 for (unsigned i = 0; i < blorp_key.num_inputs; i++) {
178 nir_variable *a_in = nir_variable_create(b.shader, nir_var_shader_in,
179 uvec4_type, "input");
180 a_in->data.location = VERT_ATTRIB_GENERIC2 + i;
181
182 nir_variable *v_out = nir_variable_create(b.shader, nir_var_shader_out,
183 uvec4_type, "output");
184 v_out->data.location = VARYING_SLOT_VAR0 + i;
185
186 nir_copy_var(&b, v_out, a_in);
187 }
188
189 struct brw_vs_prog_data vs_prog_data;
190 memset(&vs_prog_data, 0, sizeof(vs_prog_data));
191
192 const unsigned *program =
193 blorp_compile_vs(blorp, mem_ctx, b.shader, &vs_prog_data);
194
195 bool result =
196 blorp->upload_shader(batch, MESA_SHADER_VERTEX,
197 &blorp_key, sizeof(blorp_key),
198 program, vs_prog_data.base.base.program_size,
199 &vs_prog_data.base.base, sizeof(vs_prog_data),
200 &params->vs_prog_kernel, &params->vs_prog_data);
201
202 ralloc_free(mem_ctx);
203 return result;
204 }
205
206 /* The x0, y0, x1, and y1 parameters must already be populated with the render
207 * area of the framebuffer to be cleared.
208 */
209 static void
210 get_fast_clear_rect(const struct isl_device *dev,
211 const struct isl_surf *aux_surf,
212 unsigned *x0, unsigned *y0,
213 unsigned *x1, unsigned *y1)
214 {
215 unsigned int x_align, y_align;
216 unsigned int x_scaledown, y_scaledown;
217
218 /* Only single sampled surfaces need to (and actually can) be resolved. */
219 if (aux_surf->usage == ISL_SURF_USAGE_CCS_BIT) {
220 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
221 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
222 *
223 * Clear pass must have a clear rectangle that must follow
224 * alignment rules in terms of pixels and lines as shown in the
225 * table below. Further, the clear-rectangle height and width
226 * must be multiple of the following dimensions. If the height
227 * and width of the render target being cleared do not meet these
228 * requirements, an MCS buffer can be created such that it
229 * follows the requirement and covers the RT.
230 *
231 * The alignment size in the table that follows is related to the
232 * alignment size that is baked into the CCS surface format but with X
233 * alignment multiplied by 16 and Y alignment multiplied by 32.
234 */
235 x_align = isl_format_get_layout(aux_surf->format)->bw;
236 y_align = isl_format_get_layout(aux_surf->format)->bh;
237
238 x_align *= 16;
239
240 /* The line alignment requirement for Y-tiled is halved at SKL and again
241 * at TGL.
242 */
243 if (dev->info->gen >= 12)
244 y_align *= 8;
245 else if (dev->info->gen >= 9)
246 y_align *= 16;
247 else
248 y_align *= 32;
249
250 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
251 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
252 *
253 * In order to optimize the performance MCS buffer (when bound to
254 * 1X RT) clear similarly to MCS buffer clear for MSRT case,
255 * clear rect is required to be scaled by the following factors
256 * in the horizontal and vertical directions:
257 *
258 * The X and Y scale down factors in the table that follows are each
259 * equal to half the alignment value computed above.
260 */
261 x_scaledown = x_align / 2;
262 y_scaledown = y_align / 2;
263
264 if (ISL_DEV_IS_HASWELL(dev)) {
265 /* From BSpec: 3D-Media-GPGPU Engine > 3D Pipeline > Pixel > Pixel
266 * Backend > MCS Buffer for Render Target(s) [DevIVB+] > Table "Color
267 * Clear of Non-MultiSampled Render Target Restrictions":
268 *
269 * Clear rectangle must be aligned to two times the number of
270 * pixels in the table shown below due to 16x16 hashing across the
271 * slice.
272 *
273 * This restriction is only documented to exist on HSW GT3 but
274 * empirical evidence suggests that it's also needed GT2.
275 */
276 x_align *= 2;
277 y_align *= 2;
278 }
279 } else {
280 assert(aux_surf->usage == ISL_SURF_USAGE_MCS_BIT);
281
282 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
283 * Target(s)", beneath the "MSAA Compression" bullet (p326):
284 *
285 * Clear pass for this case requires that scaled down primitive
286 * is sent down with upper left co-ordinate to coincide with
287 * actual rectangle being cleared. For MSAA, clear rectangle’s
288 * height and width need to as show in the following table in
289 * terms of (width,height) of the RT.
290 *
291 * MSAA Width of Clear Rect Height of Clear Rect
292 * 2X Ceil(1/8*width) Ceil(1/2*height)
293 * 4X Ceil(1/8*width) Ceil(1/2*height)
294 * 8X Ceil(1/2*width) Ceil(1/2*height)
295 * 16X width Ceil(1/2*height)
296 *
297 * The text "with upper left co-ordinate to coincide with actual
298 * rectangle being cleared" is a little confusing--it seems to imply
299 * that to clear a rectangle from (x,y) to (x+w,y+h), one needs to
300 * feed the pipeline using the rectangle (x,y) to
301 * (x+Ceil(w/N),y+Ceil(h/2)), where N is either 2 or 8 depending on
302 * the number of samples. Experiments indicate that this is not
303 * quite correct; actually, what the hardware appears to do is to
304 * align whatever rectangle is sent down the pipeline to the nearest
305 * multiple of 2x2 blocks, and then scale it up by a factor of N
306 * horizontally and 2 vertically. So the resulting alignment is 4
307 * vertically and either 4 or 16 horizontally, and the scaledown
308 * factor is 2 vertically and either 2 or 8 horizontally.
309 */
310 switch (aux_surf->format) {
311 case ISL_FORMAT_MCS_2X:
312 case ISL_FORMAT_MCS_4X:
313 x_scaledown = 8;
314 break;
315 case ISL_FORMAT_MCS_8X:
316 x_scaledown = 2;
317 break;
318 case ISL_FORMAT_MCS_16X:
319 x_scaledown = 1;
320 break;
321 default:
322 unreachable("Unexpected MCS format for fast clear");
323 }
324 y_scaledown = 2;
325 x_align = x_scaledown * 2;
326 y_align = y_scaledown * 2;
327 }
328
329 *x0 = ROUND_DOWN_TO(*x0, x_align) / x_scaledown;
330 *y0 = ROUND_DOWN_TO(*y0, y_align) / y_scaledown;
331 *x1 = ALIGN(*x1, x_align) / x_scaledown;
332 *y1 = ALIGN(*y1, y_align) / y_scaledown;
333 }
334
335 void
336 blorp_fast_clear(struct blorp_batch *batch,
337 const struct blorp_surf *surf,
338 enum isl_format format, struct isl_swizzle swizzle,
339 uint32_t level, uint32_t start_layer, uint32_t num_layers,
340 uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1)
341 {
342 struct blorp_params params;
343 blorp_params_init(&params);
344 params.num_layers = num_layers;
345
346 params.x0 = x0;
347 params.y0 = y0;
348 params.x1 = x1;
349 params.y1 = y1;
350
351 memset(&params.wm_inputs.clear_color, 0xff, 4*sizeof(float));
352 params.fast_clear_op = ISL_AUX_OP_FAST_CLEAR;
353
354 get_fast_clear_rect(batch->blorp->isl_dev, surf->aux_surf,
355 &params.x0, &params.y0, &params.x1, &params.y1);
356
357 if (!blorp_params_get_clear_kernel(batch, &params, true, false))
358 return;
359
360 brw_blorp_surface_info_init(batch->blorp, &params.dst, surf, level,
361 start_layer, format, true);
362 params.num_samples = params.dst.surf.samples;
363
364 /* If a swizzle was provided, we need to swizzle the clear color so that
365 * the hardware color format conversion will work properly.
366 */
367 params.dst.clear_color =
368 isl_color_value_swizzle_inv(params.dst.clear_color, swizzle);
369
370 batch->blorp->exec(batch, &params);
371 }
372
373 void
374 blorp_clear(struct blorp_batch *batch,
375 const struct blorp_surf *surf,
376 enum isl_format format, struct isl_swizzle swizzle,
377 uint32_t level, uint32_t start_layer, uint32_t num_layers,
378 uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1,
379 union isl_color_value clear_color,
380 const bool color_write_disable[4])
381 {
382 struct blorp_params params;
383 blorp_params_init(&params);
384
385 /* Manually apply the clear destination swizzle. This way swizzled clears
386 * will work for swizzles which we can't normally use for rendering and it
387 * also ensures that they work on pre-Haswell hardware which can't swizlle
388 * at all.
389 */
390 clear_color = isl_color_value_swizzle_inv(clear_color, swizzle);
391 swizzle = ISL_SWIZZLE_IDENTITY;
392
393 bool clear_rgb_as_red = false;
394 if (format == ISL_FORMAT_R9G9B9E5_SHAREDEXP) {
395 clear_color.u32[0] = float3_to_rgb9e5(clear_color.f32);
396 format = ISL_FORMAT_R32_UINT;
397 } else if (format == ISL_FORMAT_L8_UNORM_SRGB) {
398 clear_color.f32[0] = util_format_linear_to_srgb_float(clear_color.f32[0]);
399 format = ISL_FORMAT_R8_UNORM;
400 } else if (format == ISL_FORMAT_A4B4G4R4_UNORM) {
401 /* Broadwell and earlier cannot render to this format so we need to work
402 * around it by swapping the colors around and using B4G4R4A4 instead.
403 */
404 const struct isl_swizzle ARGB = ISL_SWIZZLE(ALPHA, RED, GREEN, BLUE);
405 clear_color = isl_color_value_swizzle_inv(clear_color, ARGB);
406 format = ISL_FORMAT_B4G4R4A4_UNORM;
407 } else if (isl_format_get_layout(format)->bpb % 3 == 0) {
408 clear_rgb_as_red = true;
409 if (format == ISL_FORMAT_R8G8B8_UNORM_SRGB) {
410 clear_color.f32[0] = util_format_linear_to_srgb_float(clear_color.f32[0]);
411 clear_color.f32[1] = util_format_linear_to_srgb_float(clear_color.f32[1]);
412 clear_color.f32[2] = util_format_linear_to_srgb_float(clear_color.f32[2]);
413 }
414 }
415
416 memcpy(&params.wm_inputs.clear_color, clear_color.f32, sizeof(float) * 4);
417
418 bool use_simd16_replicated_data = true;
419
420 /* From the SNB PRM (Vol4_Part1):
421 *
422 * "Replicated data (Message Type = 111) is only supported when
423 * accessing tiled memory. Using this Message Type to access linear
424 * (untiled) memory is UNDEFINED."
425 */
426 if (surf->surf->tiling == ISL_TILING_LINEAR)
427 use_simd16_replicated_data = false;
428
429 /* Replicated clears don't work yet before gen6 */
430 if (batch->blorp->isl_dev->info->gen < 6)
431 use_simd16_replicated_data = false;
432
433 /* Constant color writes ignore everyting in blend and color calculator
434 * state. This is not documented.
435 */
436 if (color_write_disable) {
437 for (unsigned i = 0; i < 4; i++) {
438 params.color_write_disable[i] = color_write_disable[i];
439 if (color_write_disable[i])
440 use_simd16_replicated_data = false;
441 }
442 }
443
444 if (!blorp_params_get_clear_kernel(batch, &params,
445 use_simd16_replicated_data,
446 clear_rgb_as_red))
447 return;
448
449 if (!blorp_ensure_sf_program(batch, &params))
450 return;
451
452 while (num_layers > 0) {
453 brw_blorp_surface_info_init(batch->blorp, &params.dst, surf, level,
454 start_layer, format, true);
455 params.dst.view.swizzle = swizzle;
456
457 params.x0 = x0;
458 params.y0 = y0;
459 params.x1 = x1;
460 params.y1 = y1;
461
462 if (params.dst.tile_x_sa || params.dst.tile_y_sa) {
463 assert(params.dst.surf.samples == 1);
464 assert(num_layers == 1);
465 params.x0 += params.dst.tile_x_sa;
466 params.y0 += params.dst.tile_y_sa;
467 params.x1 += params.dst.tile_x_sa;
468 params.y1 += params.dst.tile_y_sa;
469 }
470
471 /* The MinLOD and MinimumArrayElement don't work properly for cube maps.
472 * Convert them to a single slice on gen4.
473 */
474 if (batch->blorp->isl_dev->info->gen == 4 &&
475 (params.dst.surf.usage & ISL_SURF_USAGE_CUBE_BIT)) {
476 blorp_surf_convert_to_single_slice(batch->blorp->isl_dev, &params.dst);
477 }
478
479 if (clear_rgb_as_red) {
480 surf_fake_rgb_with_red(batch->blorp->isl_dev, &params.dst);
481 params.x0 *= 3;
482 params.x1 *= 3;
483 }
484
485 if (isl_format_is_compressed(params.dst.surf.format)) {
486 blorp_surf_convert_to_uncompressed(batch->blorp->isl_dev, &params.dst,
487 NULL, NULL, NULL, NULL);
488 //&dst_x, &dst_y, &dst_w, &dst_h);
489 }
490
491 if (params.dst.tile_x_sa || params.dst.tile_y_sa) {
492 /* Either we're on gen4 where there is no multisampling or the
493 * surface is compressed which also implies no multisampling.
494 * Therefore, sa == px and we don't need to do a conversion.
495 */
496 assert(params.dst.surf.samples == 1);
497 params.x0 += params.dst.tile_x_sa;
498 params.y0 += params.dst.tile_y_sa;
499 params.x1 += params.dst.tile_x_sa;
500 params.y1 += params.dst.tile_y_sa;
501 }
502
503 params.num_samples = params.dst.surf.samples;
504
505 /* We may be restricted on the number of layers we can bind at any one
506 * time. In particular, Sandy Bridge has a maximum number of layers of
507 * 512 but a maximum 3D texture size is much larger.
508 */
509 params.num_layers = MIN2(params.dst.view.array_len, num_layers);
510
511 const unsigned max_image_width = 16 * 1024;
512 if (params.dst.surf.logical_level0_px.width > max_image_width) {
513 /* Clearing an RGB image as red multiplies the surface width by 3
514 * so it may now be too wide for the hardware surface limits. We
515 * have to break the clear up into pieces in order to clear wide
516 * images.
517 */
518 assert(clear_rgb_as_red);
519 assert(params.dst.surf.dim == ISL_SURF_DIM_2D);
520 assert(params.dst.surf.tiling == ISL_TILING_LINEAR);
521 assert(params.dst.surf.logical_level0_px.depth == 1);
522 assert(params.dst.surf.logical_level0_px.array_len == 1);
523 assert(params.dst.surf.levels == 1);
524 assert(params.dst.surf.samples == 1);
525 assert(params.dst.tile_x_sa == 0 || params.dst.tile_y_sa == 0);
526 assert(params.dst.aux_usage == ISL_AUX_USAGE_NONE);
527
528 /* max_image_width rounded down to a multiple of 3 */
529 const unsigned max_fake_rgb_width = (max_image_width / 3) * 3;
530 const unsigned cpp =
531 isl_format_get_layout(params.dst.surf.format)->bpb / 8;
532
533 params.dst.surf.logical_level0_px.width = max_fake_rgb_width;
534 params.dst.surf.phys_level0_sa.width = max_fake_rgb_width;
535
536 uint32_t orig_x0 = params.x0, orig_x1 = params.x1;
537 uint64_t orig_offset = params.dst.addr.offset;
538 for (uint32_t x = orig_x0; x < orig_x1; x += max_fake_rgb_width) {
539 /* Offset to the surface. It's easy because we're linear */
540 params.dst.addr.offset = orig_offset + x * cpp;
541
542 params.x0 = 0;
543 params.x1 = MIN2(orig_x1 - x, max_image_width);
544
545 batch->blorp->exec(batch, &params);
546 }
547 } else {
548 batch->blorp->exec(batch, &params);
549 }
550
551 start_layer += params.num_layers;
552 num_layers -= params.num_layers;
553 }
554 }
555
556 static bool
557 blorp_clear_stencil_as_rgba(struct blorp_batch *batch,
558 const struct blorp_surf *surf,
559 uint32_t level, uint32_t start_layer,
560 uint32_t num_layers,
561 uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1,
562 uint8_t stencil_mask, uint8_t stencil_value)
563 {
564 /* We only support separate W-tiled stencil for now */
565 if (surf->surf->format != ISL_FORMAT_R8_UINT ||
566 surf->surf->tiling != ISL_TILING_W)
567 return false;
568
569 /* Stencil mask support would require piles of shader magic */
570 if (stencil_mask != 0xff)
571 return false;
572
573 if (surf->surf->samples > 1) {
574 /* Adjust x0, y0, x1, and y1 to be in units of samples */
575 assert(surf->surf->msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED);
576 struct isl_extent2d msaa_px_size_sa =
577 isl_get_interleaved_msaa_px_size_sa(surf->surf->samples);
578
579 x0 *= msaa_px_size_sa.w;
580 y0 *= msaa_px_size_sa.h;
581 x1 *= msaa_px_size_sa.w;
582 y1 *= msaa_px_size_sa.h;
583 }
584
585 /* W-tiles and Y-tiles have the same layout as far as cache lines are
586 * concerned: both are 8x8 cache lines laid out Y-major. The difference is
587 * entirely in how the data is arranged withing the cache line. W-tiling
588 * is 8x8 pixels in a swizzled pattern while Y-tiling is 16B by 4 rows
589 * regardless of image format size. As long as everything is aligned to 8,
590 * we can just treat the W-tiled image as Y-tiled, ignore the layout
591 * difference within a cache line, and blast out data.
592 */
593 if (x0 % 8 != 0 || y0 % 8 != 0 || x1 % 8 != 0 || y1 % 8 != 0)
594 return false;
595
596 struct blorp_params params;
597 blorp_params_init(&params);
598
599 if (!blorp_params_get_clear_kernel(batch, &params, true, false))
600 return false;
601
602 memset(&params.wm_inputs.clear_color, stencil_value,
603 sizeof(params.wm_inputs.clear_color));
604
605 /* The Sandy Bridge PRM Vol. 4 Pt. 2, section 2.11.2.1.1 has the
606 * following footnote to the format table:
607 *
608 * 128 BPE Formats cannot be Tiled Y when used as render targets
609 *
610 * We have to use RGBA16_UINT on SNB.
611 */
612 enum isl_format wide_format;
613 if (ISL_DEV_GEN(batch->blorp->isl_dev) <= 6) {
614 wide_format = ISL_FORMAT_R16G16B16A16_UINT;
615
616 /* For RGBA16_UINT, we need to mask the stencil value otherwise, we risk
617 * clamping giving us the wrong values
618 */
619 for (unsigned i = 0; i < 4; i++)
620 params.wm_inputs.clear_color[i] &= 0xffff;
621 } else {
622 wide_format = ISL_FORMAT_R32G32B32A32_UINT;
623 }
624
625 for (uint32_t a = 0; a < num_layers; a++) {
626 uint32_t layer = start_layer + a;
627
628 brw_blorp_surface_info_init(batch->blorp, &params.dst, surf, level,
629 layer, ISL_FORMAT_UNSUPPORTED, true);
630
631 if (surf->surf->samples > 1)
632 blorp_surf_fake_interleaved_msaa(batch->blorp->isl_dev, &params.dst);
633
634 /* Make it Y-tiled */
635 blorp_surf_retile_w_to_y(batch->blorp->isl_dev, &params.dst);
636
637 unsigned wide_Bpp =
638 isl_format_get_layout(wide_format)->bpb / 8;
639
640 params.dst.view.format = params.dst.surf.format = wide_format;
641 assert(params.dst.surf.logical_level0_px.width % wide_Bpp == 0);
642 params.dst.surf.logical_level0_px.width /= wide_Bpp;
643 assert(params.dst.tile_x_sa % wide_Bpp == 0);
644 params.dst.tile_x_sa /= wide_Bpp;
645
646 params.x0 = params.dst.tile_x_sa + x0 / (wide_Bpp / 2);
647 params.y0 = params.dst.tile_y_sa + y0 / 2;
648 params.x1 = params.dst.tile_x_sa + x1 / (wide_Bpp / 2);
649 params.y1 = params.dst.tile_y_sa + y1 / 2;
650
651 batch->blorp->exec(batch, &params);
652 }
653
654 return true;
655 }
656
657 void
658 blorp_clear_depth_stencil(struct blorp_batch *batch,
659 const struct blorp_surf *depth,
660 const struct blorp_surf *stencil,
661 uint32_t level, uint32_t start_layer,
662 uint32_t num_layers,
663 uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1,
664 bool clear_depth, float depth_value,
665 uint8_t stencil_mask, uint8_t stencil_value)
666 {
667 if (!clear_depth && blorp_clear_stencil_as_rgba(batch, stencil, level,
668 start_layer, num_layers,
669 x0, y0, x1, y1,
670 stencil_mask,
671 stencil_value))
672 return;
673
674 struct blorp_params params;
675 blorp_params_init(&params);
676
677 params.x0 = x0;
678 params.y0 = y0;
679 params.x1 = x1;
680 params.y1 = y1;
681
682 if (ISL_DEV_GEN(batch->blorp->isl_dev) == 6) {
683 /* For some reason, Sandy Bridge gets occlusion queries wrong if we
684 * don't have a shader. In particular, it records samples even though
685 * we disable statistics in 3DSTATE_WM. Give it the usual clear shader
686 * to work around the issue.
687 */
688 if (!blorp_params_get_clear_kernel(batch, &params, false, false))
689 return;
690 }
691
692 while (num_layers > 0) {
693 params.num_layers = num_layers;
694
695 if (stencil_mask) {
696 brw_blorp_surface_info_init(batch->blorp, &params.stencil, stencil,
697 level, start_layer,
698 ISL_FORMAT_UNSUPPORTED, true);
699 params.stencil_mask = stencil_mask;
700 params.stencil_ref = stencil_value;
701
702 params.dst.surf.samples = params.stencil.surf.samples;
703 params.dst.surf.logical_level0_px =
704 params.stencil.surf.logical_level0_px;
705 params.dst.view = params.stencil.view;
706
707 params.num_samples = params.stencil.surf.samples;
708
709 /* We may be restricted on the number of layers we can bind at any
710 * one time. In particular, Sandy Bridge has a maximum number of
711 * layers of 512 but a maximum 3D texture size is much larger.
712 */
713 if (params.stencil.view.array_len < params.num_layers)
714 params.num_layers = params.stencil.view.array_len;
715 }
716
717 if (clear_depth) {
718 brw_blorp_surface_info_init(batch->blorp, &params.depth, depth,
719 level, start_layer,
720 ISL_FORMAT_UNSUPPORTED, true);
721 params.z = depth_value;
722 params.depth_format =
723 isl_format_get_depth_format(depth->surf->format, false);
724
725 params.dst.surf.samples = params.depth.surf.samples;
726 params.dst.surf.logical_level0_px =
727 params.depth.surf.logical_level0_px;
728 params.dst.view = params.depth.view;
729
730 params.num_samples = params.depth.surf.samples;
731
732 /* We may be restricted on the number of layers we can bind at any
733 * one time. In particular, Sandy Bridge has a maximum number of
734 * layers of 512 but a maximum 3D texture size is much larger.
735 */
736 if (params.depth.view.array_len < params.num_layers)
737 params.num_layers = params.depth.view.array_len;
738 }
739
740 batch->blorp->exec(batch, &params);
741
742 start_layer += params.num_layers;
743 num_layers -= params.num_layers;
744 }
745 }
746
747 bool
748 blorp_can_hiz_clear_depth(const struct gen_device_info *devinfo,
749 const struct isl_surf *surf,
750 enum isl_aux_usage aux_usage,
751 uint32_t level, uint32_t layer,
752 uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1)
753 {
754 /* This function currently doesn't support any gen prior to gen8 */
755 assert(devinfo->gen >= 8);
756
757 if (devinfo->gen == 8 && surf->format == ISL_FORMAT_R16_UNORM) {
758 /* Apply the D16 alignment restrictions. On BDW, HiZ has an 8x4 sample
759 * block with the following property: as the number of samples increases,
760 * the number of pixels representable by this block decreases by a factor
761 * of the sample dimensions. Sample dimensions scale following the MSAA
762 * interleaved pattern.
763 *
764 * Sample|Sample|Pixel
765 * Count |Dim |Dim
766 * ===================
767 * 1 | 1x1 | 8x4
768 * 2 | 2x1 | 4x4
769 * 4 | 2x2 | 4x2
770 * 8 | 4x2 | 2x2
771 * 16 | 4x4 | 2x1
772 *
773 * Table: Pixel Dimensions in a HiZ Sample Block Pre-SKL
774 */
775 const struct isl_extent2d sa_block_dim =
776 isl_get_interleaved_msaa_px_size_sa(surf->samples);
777 const uint8_t align_px_w = 8 / sa_block_dim.w;
778 const uint8_t align_px_h = 4 / sa_block_dim.h;
779
780 /* Fast depth clears clear an entire sample block at a time. As a result,
781 * the rectangle must be aligned to the dimensions of the encompassing
782 * pixel block for a successful operation.
783 *
784 * Fast clears can still work if the upper-left corner is aligned and the
785 * bottom-rigtht corner touches the edge of a depth buffer whose extent
786 * is unaligned. This is because each miplevel in the depth buffer is
787 * padded by the Pixel Dim (similar to a standard compressed texture).
788 * In this case, the clear rectangle could be padded by to match the full
789 * depth buffer extent but to support multiple clearing techniques, we
790 * chose to be unaware of the depth buffer's extent and thus don't handle
791 * this case.
792 */
793 if (x0 % align_px_w || y0 % align_px_h ||
794 x1 % align_px_w || y1 % align_px_h)
795 return false;
796 } else if (aux_usage == ISL_AUX_USAGE_HIZ_CCS_WT) {
797 /* We have to set the WM_HZ_OP::FullSurfaceDepthandStencilClear bit
798 * whenever we clear an uninitialized HIZ buffer (as some drivers
799 * currently do). However, this bit seems liable to clear 16x8 pixels in
800 * the ZCS on Gen12 - greater than the slice alignments for depth
801 * buffers.
802 */
803 assert(surf->image_alignment_el.w % 16 != 0 ||
804 surf->image_alignment_el.h % 8 != 0);
805
806 /* This is the hypothesis behind some corruption that was seen with the
807 * amd_vertex_shader_layer-layered-depth-texture-render piglit test.
808 *
809 * From the Compressed Depth Buffers section of the Bspec, under the
810 * Gen12 texture performant and ZCS columns:
811 *
812 * Update with clear at either 16x8 or 8x4 granularity, based on
813 * fs_clr or otherwise.
814 *
815 * There are a number of ways to avoid full surface CCS clears that
816 * overlap other slices, but for now we choose to disable fast-clears
817 * when an initializing clear could hit another miplevel.
818 *
819 * NOTE: Because the CCS compresses the depth buffer and not a version
820 * of it that has been rearranged with different alignments (like Gen8+
821 * HIZ), we have to make sure that the x0 and y0 are at least 16x8
822 * aligned in the context of the entire surface.
823 */
824 uint32_t slice_x0, slice_y0;
825 isl_surf_get_image_offset_el(surf, level,
826 surf->dim == ISL_SURF_DIM_3D ? 0 : layer,
827 surf->dim == ISL_SURF_DIM_3D ? layer: 0,
828 &slice_x0, &slice_y0);
829 const bool max_x1_y1 =
830 x1 == minify(surf->logical_level0_px.width, level) &&
831 y1 == minify(surf->logical_level0_px.height, level);
832 const uint32_t haligned_x1 = ALIGN(x1, surf->image_alignment_el.w);
833 const uint32_t valigned_y1 = ALIGN(y1, surf->image_alignment_el.h);
834 const bool unaligned = (slice_x0 + x0) % 16 || (slice_y0 + y0) % 8 ||
835 max_x1_y1 ? haligned_x1 % 16 || valigned_y1 % 8 :
836 x1 % 16 || y1 % 8;
837 const bool alignment_used = surf->levels > 1 ||
838 surf->logical_level0_px.depth > 1 ||
839 surf->logical_level0_px.array_len > 1;
840
841 if (unaligned && alignment_used)
842 return false;
843 }
844
845 return isl_aux_usage_has_hiz(aux_usage);
846 }
847
848 void
849 blorp_hiz_clear_depth_stencil(struct blorp_batch *batch,
850 const struct blorp_surf *depth,
851 const struct blorp_surf *stencil,
852 uint32_t level,
853 uint32_t start_layer, uint32_t num_layers,
854 uint32_t x0, uint32_t y0,
855 uint32_t x1, uint32_t y1,
856 bool clear_depth, float depth_value,
857 bool clear_stencil, uint8_t stencil_value)
858 {
859 struct blorp_params params;
860 blorp_params_init(&params);
861
862 /* This requires WM_HZ_OP which only exists on gen8+ */
863 assert(ISL_DEV_GEN(batch->blorp->isl_dev) >= 8);
864
865 params.hiz_op = ISL_AUX_OP_FAST_CLEAR;
866 params.num_layers = 1;
867
868 params.x0 = x0;
869 params.y0 = y0;
870 params.x1 = x1;
871 params.y1 = y1;
872
873 for (uint32_t l = 0; l < num_layers; l++) {
874 const uint32_t layer = start_layer + l;
875 if (clear_stencil) {
876 brw_blorp_surface_info_init(batch->blorp, &params.stencil, stencil,
877 level, layer,
878 ISL_FORMAT_UNSUPPORTED, true);
879 params.stencil_mask = 0xff;
880 params.stencil_ref = stencil_value;
881 params.num_samples = params.stencil.surf.samples;
882 }
883
884 if (clear_depth) {
885 /* If we're clearing depth, we must have HiZ */
886 assert(depth && isl_aux_usage_has_hiz(depth->aux_usage));
887
888 brw_blorp_surface_info_init(batch->blorp, &params.depth, depth,
889 level, layer,
890 ISL_FORMAT_UNSUPPORTED, true);
891 params.depth.clear_color.f32[0] = depth_value;
892 params.depth_format =
893 isl_format_get_depth_format(depth->surf->format, false);
894 params.num_samples = params.depth.surf.samples;
895 }
896
897 batch->blorp->exec(batch, &params);
898 }
899 }
900
901 /* Given a depth stencil attachment, this function performs a fast depth clear
902 * on a depth portion and a regular clear on the stencil portion. When
903 * performing a fast depth clear on the depth portion, the HiZ buffer is simply
904 * tagged as cleared so the depth clear value is not actually needed.
905 */
906 void
907 blorp_gen8_hiz_clear_attachments(struct blorp_batch *batch,
908 uint32_t num_samples,
909 uint32_t x0, uint32_t y0,
910 uint32_t x1, uint32_t y1,
911 bool clear_depth, bool clear_stencil,
912 uint8_t stencil_value)
913 {
914 assert(batch->flags & BLORP_BATCH_NO_EMIT_DEPTH_STENCIL);
915
916 struct blorp_params params;
917 blorp_params_init(&params);
918 params.num_layers = 1;
919 params.hiz_op = ISL_AUX_OP_FAST_CLEAR;
920 params.x0 = x0;
921 params.y0 = y0;
922 params.x1 = x1;
923 params.y1 = y1;
924 params.num_samples = num_samples;
925 params.depth.enabled = clear_depth;
926 params.stencil.enabled = clear_stencil;
927 params.stencil_ref = stencil_value;
928 batch->blorp->exec(batch, &params);
929 }
930
931 /** Clear active color/depth/stencili attachments
932 *
933 * This function performs a clear operation on the currently bound
934 * color/depth/stencil attachments. It is assumed that any information passed
935 * in here is valid, consistent, and in-bounds relative to the currently
936 * attached depth/stencil. The binding_table_offset parameter is the 32-bit
937 * offset relative to surface state base address where pre-baked binding table
938 * that we are to use lives. If clear_color is false, binding_table_offset
939 * must point to a binding table with one entry which is a valid null surface
940 * that matches the currently bound depth and stencil.
941 */
942 void
943 blorp_clear_attachments(struct blorp_batch *batch,
944 uint32_t binding_table_offset,
945 enum isl_format depth_format,
946 uint32_t num_samples,
947 uint32_t start_layer, uint32_t num_layers,
948 uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1,
949 bool clear_color, union isl_color_value color_value,
950 bool clear_depth, float depth_value,
951 uint8_t stencil_mask, uint8_t stencil_value)
952 {
953 struct blorp_params params;
954 blorp_params_init(&params);
955
956 assert(batch->flags & BLORP_BATCH_NO_EMIT_DEPTH_STENCIL);
957
958 params.x0 = x0;
959 params.y0 = y0;
960 params.x1 = x1;
961 params.y1 = y1;
962
963 params.use_pre_baked_binding_table = true;
964 params.pre_baked_binding_table_offset = binding_table_offset;
965
966 params.num_layers = num_layers;
967 params.num_samples = num_samples;
968
969 if (clear_color) {
970 params.dst.enabled = true;
971
972 memcpy(&params.wm_inputs.clear_color, color_value.f32, sizeof(float) * 4);
973
974 /* Unfortunately, without knowing whether or not our destination surface
975 * is tiled or not, we have to assume it may be linear. This means no
976 * SIMD16_REPDATA for us. :-(
977 */
978 if (!blorp_params_get_clear_kernel(batch, &params, false, false))
979 return;
980 }
981
982 if (clear_depth) {
983 params.depth.enabled = true;
984
985 params.z = depth_value;
986 params.depth_format = isl_format_get_depth_format(depth_format, false);
987 }
988
989 if (stencil_mask) {
990 params.stencil.enabled = true;
991
992 params.stencil_mask = stencil_mask;
993 params.stencil_ref = stencil_value;
994 }
995
996 if (!blorp_params_get_layer_offset_vs(batch, &params))
997 return;
998
999 params.vs_inputs.base_layer = start_layer;
1000
1001 batch->blorp->exec(batch, &params);
1002 }
1003
1004 void
1005 blorp_ccs_resolve(struct blorp_batch *batch,
1006 struct blorp_surf *surf, uint32_t level,
1007 uint32_t start_layer, uint32_t num_layers,
1008 enum isl_format format,
1009 enum isl_aux_op resolve_op)
1010 {
1011 struct blorp_params params;
1012
1013 blorp_params_init(&params);
1014 brw_blorp_surface_info_init(batch->blorp, &params.dst, surf,
1015 level, start_layer, format, true);
1016
1017 /* From the Ivy Bridge PRM, Vol2 Part1 11.9 "Render Target Resolve":
1018 *
1019 * A rectangle primitive must be scaled down by the following factors
1020 * with respect to render target being resolved.
1021 *
1022 * The scaledown factors in the table that follows are related to the block
1023 * size of the CCS format. For IVB and HSW, we divide by two, for BDW we
1024 * multiply by 8 and 16. On Sky Lake, we multiply by 8.
1025 */
1026 const struct isl_format_layout *aux_fmtl =
1027 isl_format_get_layout(params.dst.aux_surf.format);
1028 assert(aux_fmtl->txc == ISL_TXC_CCS);
1029
1030 unsigned x_scaledown, y_scaledown;
1031 if (ISL_DEV_GEN(batch->blorp->isl_dev) >= 12) {
1032 x_scaledown = aux_fmtl->bw * 8;
1033 y_scaledown = aux_fmtl->bh * 4;
1034 } else if (ISL_DEV_GEN(batch->blorp->isl_dev) >= 9) {
1035 x_scaledown = aux_fmtl->bw * 8;
1036 y_scaledown = aux_fmtl->bh * 8;
1037 } else if (ISL_DEV_GEN(batch->blorp->isl_dev) >= 8) {
1038 x_scaledown = aux_fmtl->bw * 8;
1039 y_scaledown = aux_fmtl->bh * 16;
1040 } else {
1041 x_scaledown = aux_fmtl->bw / 2;
1042 y_scaledown = aux_fmtl->bh / 2;
1043 }
1044 params.x0 = params.y0 = 0;
1045 params.x1 = minify(params.dst.surf.logical_level0_px.width, level);
1046 params.y1 = minify(params.dst.surf.logical_level0_px.height, level);
1047 params.x1 = ALIGN(params.x1, x_scaledown) / x_scaledown;
1048 params.y1 = ALIGN(params.y1, y_scaledown) / y_scaledown;
1049
1050 if (batch->blorp->isl_dev->info->gen >= 10) {
1051 assert(resolve_op == ISL_AUX_OP_FULL_RESOLVE ||
1052 resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE ||
1053 resolve_op == ISL_AUX_OP_AMBIGUATE);
1054 } else if (batch->blorp->isl_dev->info->gen >= 9) {
1055 assert(resolve_op == ISL_AUX_OP_FULL_RESOLVE ||
1056 resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE);
1057 } else {
1058 /* Broadwell and earlier do not have a partial resolve */
1059 assert(resolve_op == ISL_AUX_OP_FULL_RESOLVE);
1060 }
1061 params.fast_clear_op = resolve_op;
1062 params.num_layers = num_layers;
1063
1064 /* Note: there is no need to initialize push constants because it doesn't
1065 * matter what data gets dispatched to the render target. However, we must
1066 * ensure that the fragment shader delivers the data using the "replicated
1067 * color" message.
1068 */
1069
1070 if (!blorp_params_get_clear_kernel(batch, &params, true, false))
1071 return;
1072
1073 batch->blorp->exec(batch, &params);
1074 }
1075
1076 static nir_ssa_def *
1077 blorp_nir_bit(nir_builder *b, nir_ssa_def *src, unsigned bit)
1078 {
1079 return nir_iand(b, nir_ushr(b, src, nir_imm_int(b, bit)),
1080 nir_imm_int(b, 1));
1081 }
1082
1083 #pragma pack(push, 1)
1084 struct blorp_mcs_partial_resolve_key
1085 {
1086 enum blorp_shader_type shader_type;
1087 bool indirect_clear_color;
1088 bool int_format;
1089 uint32_t num_samples;
1090 };
1091 #pragma pack(pop)
1092
1093 static bool
1094 blorp_params_get_mcs_partial_resolve_kernel(struct blorp_batch *batch,
1095 struct blorp_params *params)
1096 {
1097 struct blorp_context *blorp = batch->blorp;
1098 const struct blorp_mcs_partial_resolve_key blorp_key = {
1099 .shader_type = BLORP_SHADER_TYPE_MCS_PARTIAL_RESOLVE,
1100 .indirect_clear_color = params->dst.clear_color_addr.buffer != NULL,
1101 .int_format = isl_format_has_int_channel(params->dst.view.format),
1102 .num_samples = params->num_samples,
1103 };
1104
1105 if (blorp->lookup_shader(batch, &blorp_key, sizeof(blorp_key),
1106 &params->wm_prog_kernel, &params->wm_prog_data))
1107 return true;
1108
1109 void *mem_ctx = ralloc_context(NULL);
1110
1111 nir_builder b;
1112 blorp_nir_init_shader(&b, mem_ctx, MESA_SHADER_FRAGMENT,
1113 "BLORP-mcs-partial-resolve");
1114
1115 nir_variable *v_color =
1116 BLORP_CREATE_NIR_INPUT(b.shader, clear_color, glsl_vec4_type());
1117
1118 nir_variable *frag_color =
1119 nir_variable_create(b.shader, nir_var_shader_out,
1120 glsl_vec4_type(), "gl_FragColor");
1121 frag_color->data.location = FRAG_RESULT_COLOR;
1122
1123 /* Do an MCS fetch and check if it is equal to the magic clear value */
1124 nir_ssa_def *mcs =
1125 blorp_nir_txf_ms_mcs(&b, nir_f2i32(&b, nir_load_frag_coord(&b)),
1126 nir_load_layer_id(&b));
1127 nir_ssa_def *is_clear =
1128 blorp_nir_mcs_is_clear_color(&b, mcs, blorp_key.num_samples);
1129
1130 /* If we aren't the clear value, discard. */
1131 nir_intrinsic_instr *discard =
1132 nir_intrinsic_instr_create(b.shader, nir_intrinsic_discard_if);
1133 discard->src[0] = nir_src_for_ssa(nir_inot(&b, is_clear));
1134 nir_builder_instr_insert(&b, &discard->instr);
1135
1136 nir_ssa_def *clear_color = nir_load_var(&b, v_color);
1137 if (blorp_key.indirect_clear_color && blorp->isl_dev->info->gen <= 8) {
1138 /* Gen7-8 clear colors are stored as single 0/1 bits */
1139 clear_color = nir_vec4(&b, blorp_nir_bit(&b, clear_color, 31),
1140 blorp_nir_bit(&b, clear_color, 30),
1141 blorp_nir_bit(&b, clear_color, 29),
1142 blorp_nir_bit(&b, clear_color, 28));
1143
1144 if (!blorp_key.int_format)
1145 clear_color = nir_i2f32(&b, clear_color);
1146 }
1147 nir_store_var(&b, frag_color, clear_color, 0xf);
1148
1149 struct brw_wm_prog_key wm_key;
1150 brw_blorp_init_wm_prog_key(&wm_key);
1151 wm_key.base.tex.compressed_multisample_layout_mask = 1;
1152 wm_key.base.tex.msaa_16 = blorp_key.num_samples == 16;
1153 wm_key.multisample_fbo = true;
1154
1155 struct brw_wm_prog_data prog_data;
1156 const unsigned *program =
1157 blorp_compile_fs(blorp, mem_ctx, b.shader, &wm_key, false,
1158 &prog_data);
1159
1160 bool result =
1161 blorp->upload_shader(batch, MESA_SHADER_FRAGMENT,
1162 &blorp_key, sizeof(blorp_key),
1163 program, prog_data.base.program_size,
1164 &prog_data.base, sizeof(prog_data),
1165 &params->wm_prog_kernel, &params->wm_prog_data);
1166
1167 ralloc_free(mem_ctx);
1168 return result;
1169 }
1170
1171 void
1172 blorp_mcs_partial_resolve(struct blorp_batch *batch,
1173 struct blorp_surf *surf,
1174 enum isl_format format,
1175 uint32_t start_layer, uint32_t num_layers)
1176 {
1177 struct blorp_params params;
1178 blorp_params_init(&params);
1179
1180 assert(batch->blorp->isl_dev->info->gen >= 7);
1181
1182 params.x0 = 0;
1183 params.y0 = 0;
1184 params.x1 = surf->surf->logical_level0_px.width;
1185 params.y1 = surf->surf->logical_level0_px.height;
1186
1187 brw_blorp_surface_info_init(batch->blorp, &params.src, surf, 0,
1188 start_layer, format, false);
1189 brw_blorp_surface_info_init(batch->blorp, &params.dst, surf, 0,
1190 start_layer, format, true);
1191
1192 params.num_samples = params.dst.surf.samples;
1193 params.num_layers = num_layers;
1194 params.dst_clear_color_as_input = surf->clear_color_addr.buffer != NULL;
1195
1196 memcpy(&params.wm_inputs.clear_color,
1197 surf->clear_color.f32, sizeof(float) * 4);
1198
1199 if (!blorp_params_get_mcs_partial_resolve_kernel(batch, &params))
1200 return;
1201
1202 batch->blorp->exec(batch, &params);
1203 }
1204
1205 /** Clear a CCS to the "uncompressed" state
1206 *
1207 * This pass is the CCS equivalent of a "HiZ resolve". It sets the CCS values
1208 * for a given layer/level of a surface to 0x0 which is the "uncompressed"
1209 * state which tells the sampler to go look at the main surface.
1210 */
1211 void
1212 blorp_ccs_ambiguate(struct blorp_batch *batch,
1213 struct blorp_surf *surf,
1214 uint32_t level, uint32_t layer)
1215 {
1216 if (ISL_DEV_GEN(batch->blorp->isl_dev) >= 10) {
1217 /* On gen10 and above, we have a hardware resolve op for this */
1218 return blorp_ccs_resolve(batch, surf, level, layer, 1,
1219 surf->surf->format, ISL_AUX_OP_AMBIGUATE);
1220 }
1221
1222 struct blorp_params params;
1223 blorp_params_init(&params);
1224
1225 assert(ISL_DEV_GEN(batch->blorp->isl_dev) >= 7);
1226
1227 const struct isl_format_layout *aux_fmtl =
1228 isl_format_get_layout(surf->aux_surf->format);
1229 assert(aux_fmtl->txc == ISL_TXC_CCS);
1230
1231 params.dst = (struct brw_blorp_surface_info) {
1232 .enabled = true,
1233 .addr = surf->aux_addr,
1234 .view = {
1235 .usage = ISL_SURF_USAGE_RENDER_TARGET_BIT,
1236 .format = ISL_FORMAT_R32G32B32A32_UINT,
1237 .base_level = 0,
1238 .base_array_layer = 0,
1239 .levels = 1,
1240 .array_len = 1,
1241 .swizzle = ISL_SWIZZLE_IDENTITY,
1242 },
1243 };
1244
1245 uint32_t z = 0;
1246 if (surf->surf->dim == ISL_SURF_DIM_3D) {
1247 z = layer;
1248 layer = 0;
1249 }
1250
1251 uint32_t offset_B, x_offset_el, y_offset_el;
1252 isl_surf_get_image_offset_el(surf->aux_surf, level, layer, z,
1253 &x_offset_el, &y_offset_el);
1254 isl_tiling_get_intratile_offset_el(surf->aux_surf->tiling, aux_fmtl->bpb,
1255 surf->aux_surf->row_pitch_B,
1256 x_offset_el, y_offset_el,
1257 &offset_B, &x_offset_el, &y_offset_el);
1258 params.dst.addr.offset += offset_B;
1259
1260 const uint32_t width_px =
1261 minify(surf->aux_surf->logical_level0_px.width, level);
1262 const uint32_t height_px =
1263 minify(surf->aux_surf->logical_level0_px.height, level);
1264 const uint32_t width_el = DIV_ROUND_UP(width_px, aux_fmtl->bw);
1265 const uint32_t height_el = DIV_ROUND_UP(height_px, aux_fmtl->bh);
1266
1267 struct isl_tile_info ccs_tile_info;
1268 isl_surf_get_tile_info(surf->aux_surf, &ccs_tile_info);
1269
1270 /* We're going to map it as a regular RGBA32_UINT surface. We need to
1271 * downscale a good deal. We start by computing the area on the CCS to
1272 * clear in units of Y-tiled cache lines.
1273 */
1274 uint32_t x_offset_cl, y_offset_cl, width_cl, height_cl;
1275 if (ISL_DEV_GEN(batch->blorp->isl_dev) >= 8) {
1276 /* From the Sky Lake PRM Vol. 12 in the section on planes:
1277 *
1278 * "The Color Control Surface (CCS) contains the compression status
1279 * of the cache-line pairs. The compression state of the cache-line
1280 * pair is specified by 2 bits in the CCS. Each CCS cache-line
1281 * represents an area on the main surface of 16x16 sets of 128 byte
1282 * Y-tiled cache-line-pairs. CCS is always Y tiled."
1283 *
1284 * Each 2-bit surface element in the CCS corresponds to a single
1285 * cache-line pair in the main surface. This means that 16x16 el block
1286 * in the CCS maps to a Y-tiled cache line. Fortunately, CCS layouts
1287 * are calculated with a very large alignment so we can round up to a
1288 * whole cache line without worrying about overdraw.
1289 */
1290
1291 /* On Broadwell and above, a CCS tile is the same as a Y tile when
1292 * viewed at the cache-line granularity. Fortunately, the horizontal
1293 * and vertical alignment requirements of the CCS are such that we can
1294 * align to an entire cache line without worrying about crossing over
1295 * from one LOD to another.
1296 */
1297 const uint32_t x_el_per_cl = ccs_tile_info.logical_extent_el.w / 8;
1298 const uint32_t y_el_per_cl = ccs_tile_info.logical_extent_el.h / 8;
1299 assert(surf->aux_surf->image_alignment_el.w % x_el_per_cl == 0);
1300 assert(surf->aux_surf->image_alignment_el.h % y_el_per_cl == 0);
1301
1302 assert(x_offset_el % x_el_per_cl == 0);
1303 assert(y_offset_el % y_el_per_cl == 0);
1304 x_offset_cl = x_offset_el / x_el_per_cl;
1305 y_offset_cl = y_offset_el / y_el_per_cl;
1306 width_cl = DIV_ROUND_UP(width_el, x_el_per_cl);
1307 height_cl = DIV_ROUND_UP(height_el, y_el_per_cl);
1308 } else {
1309 /* On gen7, the CCS tiling is not so nice. However, there we are
1310 * guaranteed that we only have a single level and slice so we don't
1311 * have to worry about it and can just align to a whole tile.
1312 */
1313 assert(surf->aux_surf->logical_level0_px.depth == 1);
1314 assert(surf->aux_surf->logical_level0_px.array_len == 1);
1315 assert(x_offset_el == 0 && y_offset_el == 0);
1316 const uint32_t width_tl =
1317 DIV_ROUND_UP(width_el, ccs_tile_info.logical_extent_el.w);
1318 const uint32_t height_tl =
1319 DIV_ROUND_UP(height_el, ccs_tile_info.logical_extent_el.h);
1320 x_offset_cl = 0;
1321 y_offset_cl = 0;
1322 width_cl = width_tl * 8;
1323 height_cl = height_tl * 8;
1324 }
1325
1326 /* We're going to use a RGBA32 format so as to write data as quickly as
1327 * possible. A y-tiled cache line will then be 1x4 px.
1328 */
1329 const uint32_t x_offset_rgba_px = x_offset_cl;
1330 const uint32_t y_offset_rgba_px = y_offset_cl * 4;
1331 const uint32_t width_rgba_px = width_cl;
1332 const uint32_t height_rgba_px = height_cl * 4;
1333
1334 ASSERTED bool ok =
1335 isl_surf_init(batch->blorp->isl_dev, &params.dst.surf,
1336 .dim = ISL_SURF_DIM_2D,
1337 .format = ISL_FORMAT_R32G32B32A32_UINT,
1338 .width = width_rgba_px + x_offset_rgba_px,
1339 .height = height_rgba_px + y_offset_rgba_px,
1340 .depth = 1,
1341 .levels = 1,
1342 .array_len = 1,
1343 .samples = 1,
1344 .row_pitch_B = surf->aux_surf->row_pitch_B,
1345 .usage = ISL_SURF_USAGE_RENDER_TARGET_BIT,
1346 .tiling_flags = ISL_TILING_Y0_BIT);
1347 assert(ok);
1348
1349 params.x0 = x_offset_rgba_px;
1350 params.y0 = y_offset_rgba_px;
1351 params.x1 = x_offset_rgba_px + width_rgba_px;
1352 params.y1 = y_offset_rgba_px + height_rgba_px;
1353
1354 /* A CCS value of 0 means "uncompressed." */
1355 memset(&params.wm_inputs.clear_color, 0,
1356 sizeof(params.wm_inputs.clear_color));
1357
1358 if (!blorp_params_get_clear_kernel(batch, &params, true, false))
1359 return;
1360
1361 batch->blorp->exec(batch, &params);
1362 }