Path: blob/21.2-virgl/src/intel/blorp/blorp_clear.c
7219 views
/*1* Copyright © 2013 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "util/ralloc.h"2425#include "main/macros.h" /* Needed for MAX3 and MAX2 for format_rgb9e5 */26#include "util/format_rgb9e5.h"27#include "util/format_srgb.h"2829#include "blorp_priv.h"30#include "compiler/brw_eu_defines.h"3132#include "blorp_nir_builder.h"3334#define FILE_DEBUG_FLAG DEBUG_BLORP3536#pragma pack(push, 1)37struct brw_blorp_const_color_prog_key38{39struct brw_blorp_base_key base;40bool use_simd16_replicated_data;41bool clear_rgb_as_red;42};43#pragma pack(pop)4445static bool46blorp_params_get_clear_kernel(struct blorp_batch *batch,47struct blorp_params *params,48bool use_replicated_data,49bool clear_rgb_as_red)50{51struct blorp_context *blorp = batch->blorp;5253const struct brw_blorp_const_color_prog_key blorp_key = {54.base = BRW_BLORP_BASE_KEY_INIT(BLORP_SHADER_TYPE_CLEAR),55.use_simd16_replicated_data = use_replicated_data,56.clear_rgb_as_red = clear_rgb_as_red,57};5859if (blorp->lookup_shader(batch, &blorp_key, sizeof(blorp_key),60¶ms->wm_prog_kernel, ¶ms->wm_prog_data))61return true;6263void *mem_ctx = ralloc_context(NULL);6465nir_builder b;66blorp_nir_init_shader(&b, mem_ctx, MESA_SHADER_FRAGMENT,67blorp_shader_type_to_name(blorp_key.base.shader_type));6869nir_variable *v_color =70BLORP_CREATE_NIR_INPUT(b.shader, clear_color, glsl_vec4_type());71nir_ssa_def *color = nir_load_var(&b, v_color);7273if (clear_rgb_as_red) {74nir_ssa_def *pos = nir_f2i32(&b, nir_load_frag_coord(&b));75nir_ssa_def *comp = nir_umod(&b, nir_channel(&b, pos, 0),76nir_imm_int(&b, 3));77nir_ssa_def *color_component =78nir_bcsel(&b, nir_ieq_imm(&b, comp, 0),79nir_channel(&b, color, 0),80nir_bcsel(&b, nir_ieq_imm(&b, comp, 1),81nir_channel(&b, color, 1),82nir_channel(&b, color, 2)));8384nir_ssa_def *u = nir_ssa_undef(&b, 1, 32);85color = nir_vec4(&b, color_component, u, u, u);86}8788nir_variable *frag_color = nir_variable_create(b.shader, nir_var_shader_out,89glsl_vec4_type(),90"gl_FragColor");91frag_color->data.location = FRAG_RESULT_COLOR;92nir_store_var(&b, frag_color, color, 0xf);9394struct brw_wm_prog_key wm_key;95brw_blorp_init_wm_prog_key(&wm_key);9697struct brw_wm_prog_data prog_data;98const unsigned *program =99blorp_compile_fs(blorp, mem_ctx, b.shader, &wm_key, use_replicated_data,100&prog_data);101102bool result =103blorp->upload_shader(batch, MESA_SHADER_FRAGMENT,104&blorp_key, sizeof(blorp_key),105program, prog_data.base.program_size,106&prog_data.base, sizeof(prog_data),107¶ms->wm_prog_kernel, ¶ms->wm_prog_data);108109ralloc_free(mem_ctx);110return result;111}112113#pragma pack(push, 1)114struct layer_offset_vs_key {115struct brw_blorp_base_key base;116unsigned num_inputs;117};118#pragma pack(pop)119120/* In the case of doing attachment clears, we are using a surface state that121* 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 adding123* the base array layer to the instance id. Unfortunately, our hardware has124* no real concept of "base instance", so we have to do it manually in a125* vertex shader.126*/127static bool128blorp_params_get_layer_offset_vs(struct blorp_batch *batch,129struct blorp_params *params)130{131struct blorp_context *blorp = batch->blorp;132struct layer_offset_vs_key blorp_key = {133.base = BRW_BLORP_BASE_KEY_INIT(BLORP_SHADER_TYPE_LAYER_OFFSET_VS),134};135136if (params->wm_prog_data)137blorp_key.num_inputs = params->wm_prog_data->num_varying_inputs;138139if (blorp->lookup_shader(batch, &blorp_key, sizeof(blorp_key),140¶ms->vs_prog_kernel, ¶ms->vs_prog_data))141return true;142143void *mem_ctx = ralloc_context(NULL);144145nir_builder b;146blorp_nir_init_shader(&b, mem_ctx, MESA_SHADER_VERTEX,147blorp_shader_type_to_name(blorp_key.base.shader_type));148149const struct glsl_type *uvec4_type = glsl_vector_type(GLSL_TYPE_UINT, 4);150151/* First we deal with the header which has instance and base instance */152nir_variable *a_header = nir_variable_create(b.shader, nir_var_shader_in,153uvec4_type, "header");154a_header->data.location = VERT_ATTRIB_GENERIC0;155156nir_variable *v_layer = nir_variable_create(b.shader, nir_var_shader_out,157glsl_int_type(), "layer_id");158v_layer->data.location = VARYING_SLOT_LAYER;159160/* Compute the layer id */161nir_ssa_def *header = nir_load_var(&b, a_header);162nir_ssa_def *base_layer = nir_channel(&b, header, 0);163nir_ssa_def *instance = nir_channel(&b, header, 1);164nir_store_var(&b, v_layer, nir_iadd(&b, instance, base_layer), 0x1);165166/* Then we copy the vertex from the next slot to VARYING_SLOT_POS */167nir_variable *a_vertex = nir_variable_create(b.shader, nir_var_shader_in,168glsl_vec4_type(), "a_vertex");169a_vertex->data.location = VERT_ATTRIB_GENERIC1;170171nir_variable *v_pos = nir_variable_create(b.shader, nir_var_shader_out,172glsl_vec4_type(), "v_pos");173v_pos->data.location = VARYING_SLOT_POS;174175nir_copy_var(&b, v_pos, a_vertex);176177/* Then we copy everything else */178for (unsigned i = 0; i < blorp_key.num_inputs; i++) {179nir_variable *a_in = nir_variable_create(b.shader, nir_var_shader_in,180uvec4_type, "input");181a_in->data.location = VERT_ATTRIB_GENERIC2 + i;182183nir_variable *v_out = nir_variable_create(b.shader, nir_var_shader_out,184uvec4_type, "output");185v_out->data.location = VARYING_SLOT_VAR0 + i;186187nir_copy_var(&b, v_out, a_in);188}189190struct brw_vs_prog_data vs_prog_data;191memset(&vs_prog_data, 0, sizeof(vs_prog_data));192193const unsigned *program =194blorp_compile_vs(blorp, mem_ctx, b.shader, &vs_prog_data);195196bool result =197blorp->upload_shader(batch, MESA_SHADER_VERTEX,198&blorp_key, sizeof(blorp_key),199program, vs_prog_data.base.base.program_size,200&vs_prog_data.base.base, sizeof(vs_prog_data),201¶ms->vs_prog_kernel, ¶ms->vs_prog_data);202203ralloc_free(mem_ctx);204return result;205}206207/* The x0, y0, x1, and y1 parameters must already be populated with the render208* area of the framebuffer to be cleared.209*/210static void211get_fast_clear_rect(const struct isl_device *dev,212const struct isl_surf *aux_surf,213unsigned *x0, unsigned *y0,214unsigned *x1, unsigned *y1)215{216unsigned int x_align, y_align;217unsigned int x_scaledown, y_scaledown;218219/* Only single sampled surfaces need to (and actually can) be resolved. */220if (aux_surf->usage == ISL_SURF_USAGE_CCS_BIT) {221/* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render222* Target(s)", beneath the "Fast Color Clear" bullet (p327):223*224* Clear pass must have a clear rectangle that must follow225* alignment rules in terms of pixels and lines as shown in the226* table below. Further, the clear-rectangle height and width227* must be multiple of the following dimensions. If the height228* and width of the render target being cleared do not meet these229* requirements, an MCS buffer can be created such that it230* follows the requirement and covers the RT.231*232* The alignment size in the table that follows is related to the233* alignment size that is baked into the CCS surface format but with X234* alignment multiplied by 16 and Y alignment multiplied by 32.235*/236x_align = isl_format_get_layout(aux_surf->format)->bw;237y_align = isl_format_get_layout(aux_surf->format)->bh;238239x_align *= 16;240241/* The line alignment requirement for Y-tiled is halved at SKL and again242* at TGL.243*/244if (dev->info->ver >= 12)245y_align *= 8;246else if (dev->info->ver >= 9)247y_align *= 16;248else249y_align *= 32;250251/* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render252* Target(s)", beneath the "Fast Color Clear" bullet (p327):253*254* In order to optimize the performance MCS buffer (when bound to255* 1X RT) clear similarly to MCS buffer clear for MSRT case,256* clear rect is required to be scaled by the following factors257* in the horizontal and vertical directions:258*259* The X and Y scale down factors in the table that follows are each260* equal to half the alignment value computed above.261*/262x_scaledown = x_align / 2;263y_scaledown = y_align / 2;264265if (ISL_DEV_IS_HASWELL(dev)) {266/* From BSpec: 3D-Media-GPGPU Engine > 3D Pipeline > Pixel > Pixel267* Backend > MCS Buffer for Render Target(s) [DevIVB+] > Table "Color268* Clear of Non-MultiSampled Render Target Restrictions":269*270* Clear rectangle must be aligned to two times the number of271* pixels in the table shown below due to 16x16 hashing across the272* slice.273*274* This restriction is only documented to exist on HSW GT3 but275* empirical evidence suggests that it's also needed GT2.276*/277x_align *= 2;278y_align *= 2;279}280} else {281assert(aux_surf->usage == ISL_SURF_USAGE_MCS_BIT);282283/* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render284* Target(s)", beneath the "MSAA Compression" bullet (p326):285*286* Clear pass for this case requires that scaled down primitive287* is sent down with upper left co-ordinate to coincide with288* actual rectangle being cleared. For MSAA, clear rectangle’s289* height and width need to as show in the following table in290* terms of (width,height) of the RT.291*292* MSAA Width of Clear Rect Height of Clear Rect293* 2X Ceil(1/8*width) Ceil(1/2*height)294* 4X Ceil(1/8*width) Ceil(1/2*height)295* 8X Ceil(1/2*width) Ceil(1/2*height)296* 16X width Ceil(1/2*height)297*298* The text "with upper left co-ordinate to coincide with actual299* rectangle being cleared" is a little confusing--it seems to imply300* that to clear a rectangle from (x,y) to (x+w,y+h), one needs to301* feed the pipeline using the rectangle (x,y) to302* (x+Ceil(w/N),y+Ceil(h/2)), where N is either 2 or 8 depending on303* the number of samples. Experiments indicate that this is not304* quite correct; actually, what the hardware appears to do is to305* align whatever rectangle is sent down the pipeline to the nearest306* multiple of 2x2 blocks, and then scale it up by a factor of N307* horizontally and 2 vertically. So the resulting alignment is 4308* vertically and either 4 or 16 horizontally, and the scaledown309* factor is 2 vertically and either 2 or 8 horizontally.310*/311switch (aux_surf->format) {312case ISL_FORMAT_MCS_2X:313case ISL_FORMAT_MCS_4X:314x_scaledown = 8;315break;316case ISL_FORMAT_MCS_8X:317x_scaledown = 2;318break;319case ISL_FORMAT_MCS_16X:320x_scaledown = 1;321break;322default:323unreachable("Unexpected MCS format for fast clear");324}325y_scaledown = 2;326x_align = x_scaledown * 2;327y_align = y_scaledown * 2;328}329330*x0 = ROUND_DOWN_TO(*x0, x_align) / x_scaledown;331*y0 = ROUND_DOWN_TO(*y0, y_align) / y_scaledown;332*x1 = ALIGN(*x1, x_align) / x_scaledown;333*y1 = ALIGN(*y1, y_align) / y_scaledown;334}335336void337blorp_fast_clear(struct blorp_batch *batch,338const struct blorp_surf *surf,339enum isl_format format, struct isl_swizzle swizzle,340uint32_t level, uint32_t start_layer, uint32_t num_layers,341uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1)342{343struct blorp_params params;344blorp_params_init(¶ms);345params.num_layers = num_layers;346347params.x0 = x0;348params.y0 = y0;349params.x1 = x1;350params.y1 = y1;351352memset(¶ms.wm_inputs.clear_color, 0xff, 4*sizeof(float));353params.fast_clear_op = ISL_AUX_OP_FAST_CLEAR;354355get_fast_clear_rect(batch->blorp->isl_dev, surf->aux_surf,356¶ms.x0, ¶ms.y0, ¶ms.x1, ¶ms.y1);357358if (!blorp_params_get_clear_kernel(batch, ¶ms, true, false))359return;360361brw_blorp_surface_info_init(batch->blorp, ¶ms.dst, surf, level,362start_layer, format, true);363params.num_samples = params.dst.surf.samples;364365assert(params.num_samples != 0);366if (params.num_samples == 1)367params.snapshot_type = INTEL_SNAPSHOT_CCS_COLOR_CLEAR;368else369params.snapshot_type = INTEL_SNAPSHOT_MCS_COLOR_CLEAR;370371/* If a swizzle was provided, we need to swizzle the clear color so that372* the hardware color format conversion will work properly.373*/374params.dst.clear_color =375isl_color_value_swizzle_inv(params.dst.clear_color, swizzle);376377batch->blorp->exec(batch, ¶ms);378}379380void381blorp_clear(struct blorp_batch *batch,382const struct blorp_surf *surf,383enum isl_format format, struct isl_swizzle swizzle,384uint32_t level, uint32_t start_layer, uint32_t num_layers,385uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1,386union isl_color_value clear_color,387const bool color_write_disable[4])388{389struct blorp_params params;390blorp_params_init(¶ms);391params.snapshot_type = INTEL_SNAPSHOT_SLOW_COLOR_CLEAR;392393/* Manually apply the clear destination swizzle. This way swizzled clears394* will work for swizzles which we can't normally use for rendering and it395* also ensures that they work on pre-Haswell hardware which can't swizlle396* at all.397*/398clear_color = isl_color_value_swizzle_inv(clear_color, swizzle);399swizzle = ISL_SWIZZLE_IDENTITY;400401bool clear_rgb_as_red = false;402if (format == ISL_FORMAT_R9G9B9E5_SHAREDEXP) {403clear_color.u32[0] = float3_to_rgb9e5(clear_color.f32);404format = ISL_FORMAT_R32_UINT;405} else if (format == ISL_FORMAT_L8_UNORM_SRGB) {406clear_color.f32[0] = util_format_linear_to_srgb_float(clear_color.f32[0]);407format = ISL_FORMAT_R8_UNORM;408} else if (format == ISL_FORMAT_A4B4G4R4_UNORM) {409/* Broadwell and earlier cannot render to this format so we need to work410* around it by swapping the colors around and using B4G4R4A4 instead.411*/412const struct isl_swizzle ARGB = ISL_SWIZZLE(ALPHA, RED, GREEN, BLUE);413clear_color = isl_color_value_swizzle_inv(clear_color, ARGB);414format = ISL_FORMAT_B4G4R4A4_UNORM;415} else if (isl_format_get_layout(format)->bpb % 3 == 0) {416clear_rgb_as_red = true;417if (format == ISL_FORMAT_R8G8B8_UNORM_SRGB) {418clear_color.f32[0] = util_format_linear_to_srgb_float(clear_color.f32[0]);419clear_color.f32[1] = util_format_linear_to_srgb_float(clear_color.f32[1]);420clear_color.f32[2] = util_format_linear_to_srgb_float(clear_color.f32[2]);421}422}423424memcpy(¶ms.wm_inputs.clear_color, clear_color.f32, sizeof(float) * 4);425426bool use_simd16_replicated_data = true;427428/* From the SNB PRM (Vol4_Part1):429*430* "Replicated data (Message Type = 111) is only supported when431* accessing tiled memory. Using this Message Type to access linear432* (untiled) memory is UNDEFINED."433*/434if (surf->surf->tiling == ISL_TILING_LINEAR)435use_simd16_replicated_data = false;436437/* Replicated clears don't work yet before gfx6 */438if (batch->blorp->isl_dev->info->ver < 6)439use_simd16_replicated_data = false;440441/* Constant color writes ignore everyting in blend and color calculator442* state. This is not documented.443*/444if (color_write_disable) {445for (unsigned i = 0; i < 4; i++) {446params.color_write_disable[i] = color_write_disable[i];447if (color_write_disable[i])448use_simd16_replicated_data = false;449}450}451452if (!blorp_params_get_clear_kernel(batch, ¶ms,453use_simd16_replicated_data,454clear_rgb_as_red))455return;456457if (!blorp_ensure_sf_program(batch, ¶ms))458return;459460while (num_layers > 0) {461brw_blorp_surface_info_init(batch->blorp, ¶ms.dst, surf, level,462start_layer, format, true);463params.dst.view.swizzle = swizzle;464465params.x0 = x0;466params.y0 = y0;467params.x1 = x1;468params.y1 = y1;469470if (params.dst.tile_x_sa || params.dst.tile_y_sa) {471assert(params.dst.surf.samples == 1);472assert(num_layers == 1);473params.x0 += params.dst.tile_x_sa;474params.y0 += params.dst.tile_y_sa;475params.x1 += params.dst.tile_x_sa;476params.y1 += params.dst.tile_y_sa;477}478479/* The MinLOD and MinimumArrayElement don't work properly for cube maps.480* Convert them to a single slice on gfx4.481*/482if (batch->blorp->isl_dev->info->ver == 4 &&483(params.dst.surf.usage & ISL_SURF_USAGE_CUBE_BIT)) {484blorp_surf_convert_to_single_slice(batch->blorp->isl_dev, ¶ms.dst);485}486487if (clear_rgb_as_red) {488surf_fake_rgb_with_red(batch->blorp->isl_dev, ¶ms.dst);489params.x0 *= 3;490params.x1 *= 3;491}492493if (isl_format_is_compressed(params.dst.surf.format)) {494blorp_surf_convert_to_uncompressed(batch->blorp->isl_dev, ¶ms.dst,495NULL, NULL, NULL, NULL);496//&dst_x, &dst_y, &dst_w, &dst_h);497}498499if (params.dst.tile_x_sa || params.dst.tile_y_sa) {500/* Either we're on gfx4 where there is no multisampling or the501* surface is compressed which also implies no multisampling.502* Therefore, sa == px and we don't need to do a conversion.503*/504assert(params.dst.surf.samples == 1);505params.x0 += params.dst.tile_x_sa;506params.y0 += params.dst.tile_y_sa;507params.x1 += params.dst.tile_x_sa;508params.y1 += params.dst.tile_y_sa;509}510511params.num_samples = params.dst.surf.samples;512513/* We may be restricted on the number of layers we can bind at any one514* time. In particular, Sandy Bridge has a maximum number of layers of515* 512 but a maximum 3D texture size is much larger.516*/517params.num_layers = MIN2(params.dst.view.array_len, num_layers);518519const unsigned max_image_width = 16 * 1024;520if (params.dst.surf.logical_level0_px.width > max_image_width) {521/* Clearing an RGB image as red multiplies the surface width by 3522* so it may now be too wide for the hardware surface limits. We523* have to break the clear up into pieces in order to clear wide524* images.525*/526assert(clear_rgb_as_red);527assert(params.dst.surf.dim == ISL_SURF_DIM_2D);528assert(params.dst.surf.tiling == ISL_TILING_LINEAR);529assert(params.dst.surf.logical_level0_px.depth == 1);530assert(params.dst.surf.logical_level0_px.array_len == 1);531assert(params.dst.surf.levels == 1);532assert(params.dst.surf.samples == 1);533assert(params.dst.tile_x_sa == 0 || params.dst.tile_y_sa == 0);534assert(params.dst.aux_usage == ISL_AUX_USAGE_NONE);535536/* max_image_width rounded down to a multiple of 3 */537const unsigned max_fake_rgb_width = (max_image_width / 3) * 3;538const unsigned cpp =539isl_format_get_layout(params.dst.surf.format)->bpb / 8;540541params.dst.surf.logical_level0_px.width = max_fake_rgb_width;542params.dst.surf.phys_level0_sa.width = max_fake_rgb_width;543544uint32_t orig_x0 = params.x0, orig_x1 = params.x1;545uint64_t orig_offset = params.dst.addr.offset;546for (uint32_t x = orig_x0; x < orig_x1; x += max_fake_rgb_width) {547/* Offset to the surface. It's easy because we're linear */548params.dst.addr.offset = orig_offset + x * cpp;549550params.x0 = 0;551params.x1 = MIN2(orig_x1 - x, max_image_width);552553batch->blorp->exec(batch, ¶ms);554}555} else {556batch->blorp->exec(batch, ¶ms);557}558559start_layer += params.num_layers;560num_layers -= params.num_layers;561}562}563564static bool565blorp_clear_stencil_as_rgba(struct blorp_batch *batch,566const struct blorp_surf *surf,567uint32_t level, uint32_t start_layer,568uint32_t num_layers,569uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1,570uint8_t stencil_mask, uint8_t stencil_value)571{572/* We only support separate W-tiled stencil for now */573if (surf->surf->format != ISL_FORMAT_R8_UINT ||574surf->surf->tiling != ISL_TILING_W)575return false;576577/* Stencil mask support would require piles of shader magic */578if (stencil_mask != 0xff)579return false;580581if (surf->surf->samples > 1) {582/* Adjust x0, y0, x1, and y1 to be in units of samples */583assert(surf->surf->msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED);584struct isl_extent2d msaa_px_size_sa =585isl_get_interleaved_msaa_px_size_sa(surf->surf->samples);586587x0 *= msaa_px_size_sa.w;588y0 *= msaa_px_size_sa.h;589x1 *= msaa_px_size_sa.w;590y1 *= msaa_px_size_sa.h;591}592593/* W-tiles and Y-tiles have the same layout as far as cache lines are594* concerned: both are 8x8 cache lines laid out Y-major. The difference is595* entirely in how the data is arranged withing the cache line. W-tiling596* is 8x8 pixels in a swizzled pattern while Y-tiling is 16B by 4 rows597* regardless of image format size. As long as everything is aligned to 8,598* we can just treat the W-tiled image as Y-tiled, ignore the layout599* difference within a cache line, and blast out data.600*/601if (x0 % 8 != 0 || y0 % 8 != 0 || x1 % 8 != 0 || y1 % 8 != 0)602return false;603604struct blorp_params params;605blorp_params_init(¶ms);606params.snapshot_type = INTEL_SNAPSHOT_SLOW_DEPTH_CLEAR;607608if (!blorp_params_get_clear_kernel(batch, ¶ms, true, false))609return false;610611memset(¶ms.wm_inputs.clear_color, stencil_value,612sizeof(params.wm_inputs.clear_color));613614/* The Sandy Bridge PRM Vol. 4 Pt. 2, section 2.11.2.1.1 has the615* following footnote to the format table:616*617* 128 BPE Formats cannot be Tiled Y when used as render targets618*619* We have to use RGBA16_UINT on SNB.620*/621enum isl_format wide_format;622if (ISL_GFX_VER(batch->blorp->isl_dev) <= 6) {623wide_format = ISL_FORMAT_R16G16B16A16_UINT;624625/* For RGBA16_UINT, we need to mask the stencil value otherwise, we risk626* clamping giving us the wrong values627*/628for (unsigned i = 0; i < 4; i++)629params.wm_inputs.clear_color[i] &= 0xffff;630} else {631wide_format = ISL_FORMAT_R32G32B32A32_UINT;632}633634for (uint32_t a = 0; a < num_layers; a++) {635uint32_t layer = start_layer + a;636637brw_blorp_surface_info_init(batch->blorp, ¶ms.dst, surf, level,638layer, ISL_FORMAT_UNSUPPORTED, true);639640if (surf->surf->samples > 1)641blorp_surf_fake_interleaved_msaa(batch->blorp->isl_dev, ¶ms.dst);642643/* Make it Y-tiled */644blorp_surf_retile_w_to_y(batch->blorp->isl_dev, ¶ms.dst);645646unsigned wide_Bpp =647isl_format_get_layout(wide_format)->bpb / 8;648649params.dst.view.format = params.dst.surf.format = wide_format;650assert(params.dst.surf.logical_level0_px.width % wide_Bpp == 0);651params.dst.surf.logical_level0_px.width /= wide_Bpp;652assert(params.dst.tile_x_sa % wide_Bpp == 0);653params.dst.tile_x_sa /= wide_Bpp;654655params.x0 = params.dst.tile_x_sa + x0 / (wide_Bpp / 2);656params.y0 = params.dst.tile_y_sa + y0 / 2;657params.x1 = params.dst.tile_x_sa + x1 / (wide_Bpp / 2);658params.y1 = params.dst.tile_y_sa + y1 / 2;659660batch->blorp->exec(batch, ¶ms);661}662663return true;664}665666void667blorp_clear_depth_stencil(struct blorp_batch *batch,668const struct blorp_surf *depth,669const struct blorp_surf *stencil,670uint32_t level, uint32_t start_layer,671uint32_t num_layers,672uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1,673bool clear_depth, float depth_value,674uint8_t stencil_mask, uint8_t stencil_value)675{676if (!clear_depth && blorp_clear_stencil_as_rgba(batch, stencil, level,677start_layer, num_layers,678x0, y0, x1, y1,679stencil_mask,680stencil_value))681return;682683struct blorp_params params;684blorp_params_init(¶ms);685params.snapshot_type = INTEL_SNAPSHOT_SLOW_DEPTH_CLEAR;686687params.x0 = x0;688params.y0 = y0;689params.x1 = x1;690params.y1 = y1;691692if (ISL_GFX_VER(batch->blorp->isl_dev) == 6) {693/* For some reason, Sandy Bridge gets occlusion queries wrong if we694* don't have a shader. In particular, it records samples even though695* we disable statistics in 3DSTATE_WM. Give it the usual clear shader696* to work around the issue.697*/698if (!blorp_params_get_clear_kernel(batch, ¶ms, false, false))699return;700}701702while (num_layers > 0) {703params.num_layers = num_layers;704705if (stencil_mask) {706brw_blorp_surface_info_init(batch->blorp, ¶ms.stencil, stencil,707level, start_layer,708ISL_FORMAT_UNSUPPORTED, true);709params.stencil_mask = stencil_mask;710params.stencil_ref = stencil_value;711712params.dst.surf.samples = params.stencil.surf.samples;713params.dst.surf.logical_level0_px =714params.stencil.surf.logical_level0_px;715params.dst.view = params.stencil.view;716717params.num_samples = params.stencil.surf.samples;718719/* We may be restricted on the number of layers we can bind at any720* one time. In particular, Sandy Bridge has a maximum number of721* layers of 512 but a maximum 3D texture size is much larger.722*/723if (params.stencil.view.array_len < params.num_layers)724params.num_layers = params.stencil.view.array_len;725}726727if (clear_depth) {728brw_blorp_surface_info_init(batch->blorp, ¶ms.depth, depth,729level, start_layer,730ISL_FORMAT_UNSUPPORTED, true);731params.z = depth_value;732params.depth_format =733isl_format_get_depth_format(depth->surf->format, false);734735params.dst.surf.samples = params.depth.surf.samples;736params.dst.surf.logical_level0_px =737params.depth.surf.logical_level0_px;738params.dst.view = params.depth.view;739740params.num_samples = params.depth.surf.samples;741742/* We may be restricted on the number of layers we can bind at any743* one time. In particular, Sandy Bridge has a maximum number of744* layers of 512 but a maximum 3D texture size is much larger.745*/746if (params.depth.view.array_len < params.num_layers)747params.num_layers = params.depth.view.array_len;748}749750batch->blorp->exec(batch, ¶ms);751752start_layer += params.num_layers;753num_layers -= params.num_layers;754}755}756757bool758blorp_can_hiz_clear_depth(const struct intel_device_info *devinfo,759const struct isl_surf *surf,760enum isl_aux_usage aux_usage,761uint32_t level, uint32_t layer,762uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1)763{764/* This function currently doesn't support any gen prior to gfx8 */765assert(devinfo->ver >= 8);766767if (devinfo->ver == 8 && surf->format == ISL_FORMAT_R16_UNORM) {768/* Apply the D16 alignment restrictions. On BDW, HiZ has an 8x4 sample769* block with the following property: as the number of samples increases,770* the number of pixels representable by this block decreases by a factor771* of the sample dimensions. Sample dimensions scale following the MSAA772* interleaved pattern.773*774* Sample|Sample|Pixel775* Count |Dim |Dim776* ===================777* 1 | 1x1 | 8x4778* 2 | 2x1 | 4x4779* 4 | 2x2 | 4x2780* 8 | 4x2 | 2x2781* 16 | 4x4 | 2x1782*783* Table: Pixel Dimensions in a HiZ Sample Block Pre-SKL784*/785const struct isl_extent2d sa_block_dim =786isl_get_interleaved_msaa_px_size_sa(surf->samples);787const uint8_t align_px_w = 8 / sa_block_dim.w;788const uint8_t align_px_h = 4 / sa_block_dim.h;789790/* Fast depth clears clear an entire sample block at a time. As a result,791* the rectangle must be aligned to the dimensions of the encompassing792* pixel block for a successful operation.793*794* Fast clears can still work if the upper-left corner is aligned and the795* bottom-rigtht corner touches the edge of a depth buffer whose extent796* is unaligned. This is because each miplevel in the depth buffer is797* padded by the Pixel Dim (similar to a standard compressed texture).798* In this case, the clear rectangle could be padded by to match the full799* depth buffer extent but to support multiple clearing techniques, we800* chose to be unaware of the depth buffer's extent and thus don't handle801* this case.802*/803if (x0 % align_px_w || y0 % align_px_h ||804x1 % align_px_w || y1 % align_px_h)805return false;806} else if (aux_usage == ISL_AUX_USAGE_HIZ_CCS_WT) {807/* We have to set the WM_HZ_OP::FullSurfaceDepthandStencilClear bit808* whenever we clear an uninitialized HIZ buffer (as some drivers809* currently do). However, this bit seems liable to clear 16x8 pixels in810* the ZCS on Gfx12 - greater than the slice alignments for depth811* buffers.812*/813assert(surf->image_alignment_el.w % 16 != 0 ||814surf->image_alignment_el.h % 8 != 0);815816/* This is the hypothesis behind some corruption that was seen with the817* amd_vertex_shader_layer-layered-depth-texture-render piglit test.818*819* From the Compressed Depth Buffers section of the Bspec, under the820* Gfx12 texture performant and ZCS columns:821*822* Update with clear at either 16x8 or 8x4 granularity, based on823* fs_clr or otherwise.824*825* There are a number of ways to avoid full surface CCS clears that826* overlap other slices, but for now we choose to disable fast-clears827* when an initializing clear could hit another miplevel.828*829* NOTE: Because the CCS compresses the depth buffer and not a version830* of it that has been rearranged with different alignments (like Gfx8+831* HIZ), we have to make sure that the x0 and y0 are at least 16x8832* aligned in the context of the entire surface.833*/834uint32_t slice_x0, slice_y0, slice_z0, slice_a0;835isl_surf_get_image_offset_el(surf, level,836surf->dim == ISL_SURF_DIM_3D ? 0 : layer,837surf->dim == ISL_SURF_DIM_3D ? layer: 0,838&slice_x0, &slice_y0, &slice_z0, &slice_a0);839assert(slice_z0 == 0 && slice_a0 == 0);840const bool max_x1_y1 =841x1 == minify(surf->logical_level0_px.width, level) &&842y1 == minify(surf->logical_level0_px.height, level);843const uint32_t haligned_x1 = ALIGN(x1, surf->image_alignment_el.w);844const uint32_t valigned_y1 = ALIGN(y1, surf->image_alignment_el.h);845const bool unaligned = (slice_x0 + x0) % 16 || (slice_y0 + y0) % 8 ||846(max_x1_y1 ? haligned_x1 % 16 || valigned_y1 % 8 :847x1 % 16 || y1 % 8);848const bool partial_clear = x0 > 0 || y0 > 0 || !max_x1_y1;849const bool multislice_surf = surf->levels > 1 ||850surf->logical_level0_px.depth > 1 ||851surf->logical_level0_px.array_len > 1;852853if (unaligned && (partial_clear || multislice_surf))854return false;855}856857return isl_aux_usage_has_hiz(aux_usage);858}859860static bool861blorp_can_clear_full_surface(const struct blorp_surf *depth,862const struct blorp_surf *stencil,863uint32_t level,864uint32_t x0, uint32_t y0,865uint32_t x1, uint32_t y1,866bool clear_depth,867bool clear_stencil)868{869uint32_t width = 0, height = 0;870if (clear_stencil) {871width = minify(stencil->surf->logical_level0_px.width, level);872height = minify(stencil->surf->logical_level0_px.height, level);873}874875if (clear_depth && !(width || height)) {876width = minify(depth->surf->logical_level0_px.width, level);877height = minify(depth->surf->logical_level0_px.height, level);878}879880return x0 == 0 && y0 == 0 && width == x1 && height == y1;881}882883void884blorp_hiz_clear_depth_stencil(struct blorp_batch *batch,885const struct blorp_surf *depth,886const struct blorp_surf *stencil,887uint32_t level,888uint32_t start_layer, uint32_t num_layers,889uint32_t x0, uint32_t y0,890uint32_t x1, uint32_t y1,891bool clear_depth, float depth_value,892bool clear_stencil, uint8_t stencil_value)893{894struct blorp_params params;895blorp_params_init(¶ms);896params.snapshot_type = INTEL_SNAPSHOT_HIZ_CLEAR;897898/* This requires WM_HZ_OP which only exists on gfx8+ */899assert(ISL_GFX_VER(batch->blorp->isl_dev) >= 8);900901params.hiz_op = ISL_AUX_OP_FAST_CLEAR;902/* From BSpec: 3DSTATE_WM_HZ_OP_BODY >> Full Surface Depth and Stencil Clear903*904* "Software must set this only when the APP requires the entire Depth905* surface to be cleared."906*/907params.full_surface_hiz_op =908blorp_can_clear_full_surface(depth, stencil, level, x0, y0, x1, y1,909clear_depth, clear_stencil);910params.num_layers = 1;911912params.x0 = x0;913params.y0 = y0;914params.x1 = x1;915params.y1 = y1;916917for (uint32_t l = 0; l < num_layers; l++) {918const uint32_t layer = start_layer + l;919if (clear_stencil) {920brw_blorp_surface_info_init(batch->blorp, ¶ms.stencil, stencil,921level, layer,922ISL_FORMAT_UNSUPPORTED, true);923params.stencil_mask = 0xff;924params.stencil_ref = stencil_value;925params.num_samples = params.stencil.surf.samples;926}927928if (clear_depth) {929/* If we're clearing depth, we must have HiZ */930assert(depth && isl_aux_usage_has_hiz(depth->aux_usage));931932brw_blorp_surface_info_init(batch->blorp, ¶ms.depth, depth,933level, layer,934ISL_FORMAT_UNSUPPORTED, true);935params.depth.clear_color.f32[0] = depth_value;936params.depth_format =937isl_format_get_depth_format(depth->surf->format, false);938params.num_samples = params.depth.surf.samples;939}940941batch->blorp->exec(batch, ¶ms);942}943}944945/* Given a depth stencil attachment, this function performs a fast depth clear946* on a depth portion and a regular clear on the stencil portion. When947* performing a fast depth clear on the depth portion, the HiZ buffer is simply948* tagged as cleared so the depth clear value is not actually needed.949*/950void951blorp_gfx8_hiz_clear_attachments(struct blorp_batch *batch,952uint32_t num_samples,953uint32_t x0, uint32_t y0,954uint32_t x1, uint32_t y1,955bool clear_depth, bool clear_stencil,956uint8_t stencil_value)957{958assert(batch->flags & BLORP_BATCH_NO_EMIT_DEPTH_STENCIL);959960struct blorp_params params;961blorp_params_init(¶ms);962params.snapshot_type = INTEL_SNAPSHOT_HIZ_CLEAR;963params.num_layers = 1;964params.hiz_op = ISL_AUX_OP_FAST_CLEAR;965params.x0 = x0;966params.y0 = y0;967params.x1 = x1;968params.y1 = y1;969params.num_samples = num_samples;970params.depth.enabled = clear_depth;971params.stencil.enabled = clear_stencil;972params.stencil_ref = stencil_value;973batch->blorp->exec(batch, ¶ms);974}975976/** Clear active color/depth/stencili attachments977*978* This function performs a clear operation on the currently bound979* color/depth/stencil attachments. It is assumed that any information passed980* in here is valid, consistent, and in-bounds relative to the currently981* attached depth/stencil. The binding_table_offset parameter is the 32-bit982* offset relative to surface state base address where pre-baked binding table983* that we are to use lives. If clear_color is false, binding_table_offset984* must point to a binding table with one entry which is a valid null surface985* that matches the currently bound depth and stencil.986*/987void988blorp_clear_attachments(struct blorp_batch *batch,989uint32_t binding_table_offset,990enum isl_format depth_format,991uint32_t num_samples,992uint32_t start_layer, uint32_t num_layers,993uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1,994bool clear_color, union isl_color_value color_value,995bool clear_depth, float depth_value,996uint8_t stencil_mask, uint8_t stencil_value)997{998struct blorp_params params;999blorp_params_init(¶ms);10001001assert(batch->flags & BLORP_BATCH_NO_EMIT_DEPTH_STENCIL);10021003params.x0 = x0;1004params.y0 = y0;1005params.x1 = x1;1006params.y1 = y1;10071008params.use_pre_baked_binding_table = true;1009params.pre_baked_binding_table_offset = binding_table_offset;10101011params.num_layers = num_layers;1012params.num_samples = num_samples;10131014if (clear_color) {1015params.dst.enabled = true;1016params.snapshot_type = INTEL_SNAPSHOT_SLOW_COLOR_CLEAR;10171018memcpy(¶ms.wm_inputs.clear_color, color_value.f32, sizeof(float) * 4);10191020/* Unfortunately, without knowing whether or not our destination surface1021* is tiled or not, we have to assume it may be linear. This means no1022* SIMD16_REPDATA for us. :-(1023*/1024if (!blorp_params_get_clear_kernel(batch, ¶ms, false, false))1025return;1026}10271028if (clear_depth) {1029params.depth.enabled = true;1030params.snapshot_type = INTEL_SNAPSHOT_SLOW_DEPTH_CLEAR;10311032params.z = depth_value;1033params.depth_format = isl_format_get_depth_format(depth_format, false);1034}10351036if (stencil_mask) {1037params.stencil.enabled = true;1038params.snapshot_type = INTEL_SNAPSHOT_SLOW_DEPTH_CLEAR;10391040params.stencil_mask = stencil_mask;1041params.stencil_ref = stencil_value;1042}10431044if (!blorp_params_get_layer_offset_vs(batch, ¶ms))1045return;10461047params.vs_inputs.base_layer = start_layer;10481049batch->blorp->exec(batch, ¶ms);1050}10511052void1053blorp_ccs_resolve(struct blorp_batch *batch,1054struct blorp_surf *surf, uint32_t level,1055uint32_t start_layer, uint32_t num_layers,1056enum isl_format format,1057enum isl_aux_op resolve_op)1058{1059struct blorp_params params;10601061blorp_params_init(¶ms);1062switch(resolve_op) {1063case ISL_AUX_OP_AMBIGUATE:1064params.snapshot_type = INTEL_SNAPSHOT_CCS_AMBIGUATE;1065break;1066case ISL_AUX_OP_FULL_RESOLVE:1067params.snapshot_type = INTEL_SNAPSHOT_CCS_RESOLVE;1068break;1069case ISL_AUX_OP_PARTIAL_RESOLVE:1070params.snapshot_type = INTEL_SNAPSHOT_CCS_PARTIAL_RESOLVE;1071break;1072default:1073assert(false);1074}1075brw_blorp_surface_info_init(batch->blorp, ¶ms.dst, surf,1076level, start_layer, format, true);10771078/* From the Ivy Bridge PRM, Vol2 Part1 11.9 "Render Target Resolve":1079*1080* A rectangle primitive must be scaled down by the following factors1081* with respect to render target being resolved.1082*1083* The scaledown factors in the table that follows are related to the block1084* size of the CCS format. For IVB and HSW, we divide by two, for BDW we1085* multiply by 8 and 16. On Sky Lake, we multiply by 8.1086*/1087const struct isl_format_layout *aux_fmtl =1088isl_format_get_layout(params.dst.aux_surf.format);1089assert(aux_fmtl->txc == ISL_TXC_CCS);10901091unsigned x_scaledown, y_scaledown;1092if (ISL_GFX_VER(batch->blorp->isl_dev) >= 12) {1093x_scaledown = aux_fmtl->bw * 8;1094y_scaledown = aux_fmtl->bh * 4;1095} else if (ISL_GFX_VER(batch->blorp->isl_dev) >= 9) {1096x_scaledown = aux_fmtl->bw * 8;1097y_scaledown = aux_fmtl->bh * 8;1098} else if (ISL_GFX_VER(batch->blorp->isl_dev) >= 8) {1099x_scaledown = aux_fmtl->bw * 8;1100y_scaledown = aux_fmtl->bh * 16;1101} else {1102x_scaledown = aux_fmtl->bw / 2;1103y_scaledown = aux_fmtl->bh / 2;1104}1105params.x0 = params.y0 = 0;1106params.x1 = minify(params.dst.surf.logical_level0_px.width, level);1107params.y1 = minify(params.dst.surf.logical_level0_px.height, level);1108params.x1 = ALIGN(params.x1, x_scaledown) / x_scaledown;1109params.y1 = ALIGN(params.y1, y_scaledown) / y_scaledown;11101111if (batch->blorp->isl_dev->info->ver >= 10) {1112assert(resolve_op == ISL_AUX_OP_FULL_RESOLVE ||1113resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE ||1114resolve_op == ISL_AUX_OP_AMBIGUATE);1115} else if (batch->blorp->isl_dev->info->ver >= 9) {1116assert(resolve_op == ISL_AUX_OP_FULL_RESOLVE ||1117resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE);1118} else {1119/* Broadwell and earlier do not have a partial resolve */1120assert(resolve_op == ISL_AUX_OP_FULL_RESOLVE);1121}1122params.fast_clear_op = resolve_op;1123params.num_layers = num_layers;11241125/* Note: there is no need to initialize push constants because it doesn't1126* matter what data gets dispatched to the render target. However, we must1127* ensure that the fragment shader delivers the data using the "replicated1128* color" message.1129*/11301131if (!blorp_params_get_clear_kernel(batch, ¶ms, true, false))1132return;11331134batch->blorp->exec(batch, ¶ms);1135}11361137static nir_ssa_def *1138blorp_nir_bit(nir_builder *b, nir_ssa_def *src, unsigned bit)1139{1140return nir_iand(b, nir_ushr(b, src, nir_imm_int(b, bit)),1141nir_imm_int(b, 1));1142}11431144#pragma pack(push, 1)1145struct blorp_mcs_partial_resolve_key1146{1147struct brw_blorp_base_key base;1148bool indirect_clear_color;1149bool int_format;1150uint32_t num_samples;1151};1152#pragma pack(pop)11531154static bool1155blorp_params_get_mcs_partial_resolve_kernel(struct blorp_batch *batch,1156struct blorp_params *params)1157{1158struct blorp_context *blorp = batch->blorp;1159const struct blorp_mcs_partial_resolve_key blorp_key = {1160.base = BRW_BLORP_BASE_KEY_INIT(BLORP_SHADER_TYPE_MCS_PARTIAL_RESOLVE),1161.indirect_clear_color = params->dst.clear_color_addr.buffer != NULL,1162.int_format = isl_format_has_int_channel(params->dst.view.format),1163.num_samples = params->num_samples,1164};11651166if (blorp->lookup_shader(batch, &blorp_key, sizeof(blorp_key),1167¶ms->wm_prog_kernel, ¶ms->wm_prog_data))1168return true;11691170void *mem_ctx = ralloc_context(NULL);11711172nir_builder b;1173blorp_nir_init_shader(&b, mem_ctx, MESA_SHADER_FRAGMENT,1174blorp_shader_type_to_name(blorp_key.base.shader_type));11751176nir_variable *v_color =1177BLORP_CREATE_NIR_INPUT(b.shader, clear_color, glsl_vec4_type());11781179nir_variable *frag_color =1180nir_variable_create(b.shader, nir_var_shader_out,1181glsl_vec4_type(), "gl_FragColor");1182frag_color->data.location = FRAG_RESULT_COLOR;11831184/* Do an MCS fetch and check if it is equal to the magic clear value */1185nir_ssa_def *mcs =1186blorp_nir_txf_ms_mcs(&b, nir_f2i32(&b, nir_load_frag_coord(&b)),1187nir_load_layer_id(&b));1188nir_ssa_def *is_clear =1189blorp_nir_mcs_is_clear_color(&b, mcs, blorp_key.num_samples);11901191/* If we aren't the clear value, discard. */1192nir_discard_if(&b, nir_inot(&b, is_clear));11931194nir_ssa_def *clear_color = nir_load_var(&b, v_color);1195if (blorp_key.indirect_clear_color && blorp->isl_dev->info->ver <= 8) {1196/* Gfx7-8 clear colors are stored as single 0/1 bits */1197clear_color = nir_vec4(&b, blorp_nir_bit(&b, clear_color, 31),1198blorp_nir_bit(&b, clear_color, 30),1199blorp_nir_bit(&b, clear_color, 29),1200blorp_nir_bit(&b, clear_color, 28));12011202if (!blorp_key.int_format)1203clear_color = nir_i2f32(&b, clear_color);1204}1205nir_store_var(&b, frag_color, clear_color, 0xf);12061207struct brw_wm_prog_key wm_key;1208brw_blorp_init_wm_prog_key(&wm_key);1209wm_key.base.tex.compressed_multisample_layout_mask = 1;1210wm_key.base.tex.msaa_16 = blorp_key.num_samples == 16;1211wm_key.multisample_fbo = true;12121213struct brw_wm_prog_data prog_data;1214const unsigned *program =1215blorp_compile_fs(blorp, mem_ctx, b.shader, &wm_key, false,1216&prog_data);12171218bool result =1219blorp->upload_shader(batch, MESA_SHADER_FRAGMENT,1220&blorp_key, sizeof(blorp_key),1221program, prog_data.base.program_size,1222&prog_data.base, sizeof(prog_data),1223¶ms->wm_prog_kernel, ¶ms->wm_prog_data);12241225ralloc_free(mem_ctx);1226return result;1227}12281229void1230blorp_mcs_partial_resolve(struct blorp_batch *batch,1231struct blorp_surf *surf,1232enum isl_format format,1233uint32_t start_layer, uint32_t num_layers)1234{1235struct blorp_params params;1236blorp_params_init(¶ms);1237params.snapshot_type = INTEL_SNAPSHOT_MCS_PARTIAL_RESOLVE;12381239assert(batch->blorp->isl_dev->info->ver >= 7);12401241params.x0 = 0;1242params.y0 = 0;1243params.x1 = surf->surf->logical_level0_px.width;1244params.y1 = surf->surf->logical_level0_px.height;12451246brw_blorp_surface_info_init(batch->blorp, ¶ms.src, surf, 0,1247start_layer, format, false);1248brw_blorp_surface_info_init(batch->blorp, ¶ms.dst, surf, 0,1249start_layer, format, true);12501251params.num_samples = params.dst.surf.samples;1252params.num_layers = num_layers;1253params.dst_clear_color_as_input = surf->clear_color_addr.buffer != NULL;12541255memcpy(¶ms.wm_inputs.clear_color,1256surf->clear_color.f32, sizeof(float) * 4);12571258if (!blorp_params_get_mcs_partial_resolve_kernel(batch, ¶ms))1259return;12601261batch->blorp->exec(batch, ¶ms);1262}12631264/** Clear a CCS to the "uncompressed" state1265*1266* This pass is the CCS equivalent of a "HiZ resolve". It sets the CCS values1267* for a given layer/level of a surface to 0x0 which is the "uncompressed"1268* state which tells the sampler to go look at the main surface.1269*/1270void1271blorp_ccs_ambiguate(struct blorp_batch *batch,1272struct blorp_surf *surf,1273uint32_t level, uint32_t layer)1274{1275if (ISL_GFX_VER(batch->blorp->isl_dev) >= 10) {1276/* On gfx10 and above, we have a hardware resolve op for this */1277return blorp_ccs_resolve(batch, surf, level, layer, 1,1278surf->surf->format, ISL_AUX_OP_AMBIGUATE);1279}12801281struct blorp_params params;1282blorp_params_init(¶ms);1283params.snapshot_type = INTEL_SNAPSHOT_CCS_AMBIGUATE;12841285assert(ISL_GFX_VER(batch->blorp->isl_dev) >= 7);12861287const struct isl_format_layout *aux_fmtl =1288isl_format_get_layout(surf->aux_surf->format);1289assert(aux_fmtl->txc == ISL_TXC_CCS);12901291params.dst = (struct brw_blorp_surface_info) {1292.enabled = true,1293.addr = surf->aux_addr,1294.view = {1295.usage = ISL_SURF_USAGE_RENDER_TARGET_BIT,1296.format = ISL_FORMAT_R32G32B32A32_UINT,1297.base_level = 0,1298.base_array_layer = 0,1299.levels = 1,1300.array_len = 1,1301.swizzle = ISL_SWIZZLE_IDENTITY,1302},1303};13041305uint32_t z = 0;1306if (surf->surf->dim == ISL_SURF_DIM_3D) {1307z = layer;1308layer = 0;1309}13101311uint32_t offset_B, x_offset_el, y_offset_el;1312isl_surf_get_image_offset_B_tile_el(surf->aux_surf, level, layer, z,1313&offset_B, &x_offset_el, &y_offset_el);1314params.dst.addr.offset += offset_B;13151316const uint32_t width_px =1317minify(surf->aux_surf->logical_level0_px.width, level);1318const uint32_t height_px =1319minify(surf->aux_surf->logical_level0_px.height, level);1320const uint32_t width_el = DIV_ROUND_UP(width_px, aux_fmtl->bw);1321const uint32_t height_el = DIV_ROUND_UP(height_px, aux_fmtl->bh);13221323struct isl_tile_info ccs_tile_info;1324isl_surf_get_tile_info(surf->aux_surf, &ccs_tile_info);13251326/* We're going to map it as a regular RGBA32_UINT surface. We need to1327* downscale a good deal. We start by computing the area on the CCS to1328* clear in units of Y-tiled cache lines.1329*/1330uint32_t x_offset_cl, y_offset_cl, width_cl, height_cl;1331if (ISL_GFX_VER(batch->blorp->isl_dev) >= 8) {1332/* From the Sky Lake PRM Vol. 12 in the section on planes:1333*1334* "The Color Control Surface (CCS) contains the compression status1335* of the cache-line pairs. The compression state of the cache-line1336* pair is specified by 2 bits in the CCS. Each CCS cache-line1337* represents an area on the main surface of 16x16 sets of 128 byte1338* Y-tiled cache-line-pairs. CCS is always Y tiled."1339*1340* Each 2-bit surface element in the CCS corresponds to a single1341* cache-line pair in the main surface. This means that 16x16 el block1342* in the CCS maps to a Y-tiled cache line. Fortunately, CCS layouts1343* are calculated with a very large alignment so we can round up to a1344* whole cache line without worrying about overdraw.1345*/13461347/* On Broadwell and above, a CCS tile is the same as a Y tile when1348* viewed at the cache-line granularity. Fortunately, the horizontal1349* and vertical alignment requirements of the CCS are such that we can1350* align to an entire cache line without worrying about crossing over1351* from one LOD to another.1352*/1353const uint32_t x_el_per_cl = ccs_tile_info.logical_extent_el.w / 8;1354const uint32_t y_el_per_cl = ccs_tile_info.logical_extent_el.h / 8;1355assert(surf->aux_surf->image_alignment_el.w % x_el_per_cl == 0);1356assert(surf->aux_surf->image_alignment_el.h % y_el_per_cl == 0);13571358assert(x_offset_el % x_el_per_cl == 0);1359assert(y_offset_el % y_el_per_cl == 0);1360x_offset_cl = x_offset_el / x_el_per_cl;1361y_offset_cl = y_offset_el / y_el_per_cl;1362width_cl = DIV_ROUND_UP(width_el, x_el_per_cl);1363height_cl = DIV_ROUND_UP(height_el, y_el_per_cl);1364} else {1365/* On gfx7, the CCS tiling is not so nice. However, there we are1366* guaranteed that we only have a single level and slice so we don't1367* have to worry about it and can just align to a whole tile.1368*/1369assert(surf->aux_surf->logical_level0_px.depth == 1);1370assert(surf->aux_surf->logical_level0_px.array_len == 1);1371assert(x_offset_el == 0 && y_offset_el == 0);1372const uint32_t width_tl =1373DIV_ROUND_UP(width_el, ccs_tile_info.logical_extent_el.w);1374const uint32_t height_tl =1375DIV_ROUND_UP(height_el, ccs_tile_info.logical_extent_el.h);1376x_offset_cl = 0;1377y_offset_cl = 0;1378width_cl = width_tl * 8;1379height_cl = height_tl * 8;1380}13811382/* We're going to use a RGBA32 format so as to write data as quickly as1383* possible. A y-tiled cache line will then be 1x4 px.1384*/1385const uint32_t x_offset_rgba_px = x_offset_cl;1386const uint32_t y_offset_rgba_px = y_offset_cl * 4;1387const uint32_t width_rgba_px = width_cl;1388const uint32_t height_rgba_px = height_cl * 4;13891390ASSERTED bool ok =1391isl_surf_init(batch->blorp->isl_dev, ¶ms.dst.surf,1392.dim = ISL_SURF_DIM_2D,1393.format = ISL_FORMAT_R32G32B32A32_UINT,1394.width = width_rgba_px + x_offset_rgba_px,1395.height = height_rgba_px + y_offset_rgba_px,1396.depth = 1,1397.levels = 1,1398.array_len = 1,1399.samples = 1,1400.row_pitch_B = surf->aux_surf->row_pitch_B,1401.usage = ISL_SURF_USAGE_RENDER_TARGET_BIT,1402.tiling_flags = ISL_TILING_Y0_BIT);1403assert(ok);14041405params.x0 = x_offset_rgba_px;1406params.y0 = y_offset_rgba_px;1407params.x1 = x_offset_rgba_px + width_rgba_px;1408params.y1 = y_offset_rgba_px + height_rgba_px;14091410/* A CCS value of 0 means "uncompressed." */1411memset(¶ms.wm_inputs.clear_color, 0,1412sizeof(params.wm_inputs.clear_color));14131414if (!blorp_params_get_clear_kernel(batch, ¶ms, true, false))1415return;14161417batch->blorp->exec(batch, ¶ms);1418}141914201421