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