Path: blob/21.2-virgl/src/amd/vulkan/radv_meta_copy_vrs_htile.c
7405 views
/*1* Copyright © 2021 Valve 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#define AC_SURFACE_INCLUDE_NIR24#include "ac_surface.h"2526#include "radv_meta.h"27#include "radv_private.h"28#include "vk_format.h"2930void31radv_device_finish_meta_copy_vrs_htile_state(struct radv_device *device)32{33struct radv_meta_state *state = &device->meta_state;3435radv_DestroyPipeline(radv_device_to_handle(device), state->copy_vrs_htile_pipeline,36&state->alloc);37radv_DestroyPipelineLayout(radv_device_to_handle(device), state->copy_vrs_htile_p_layout,38&state->alloc);39radv_DestroyDescriptorSetLayout(radv_device_to_handle(device), state->copy_vrs_htile_ds_layout,40&state->alloc);41}4243static nir_shader *44build_copy_vrs_htile_shader(struct radv_device *device, struct radeon_surf *surf)45{46nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, NULL, "meta_copy_vrs_htile");47b.shader->info.workgroup_size[0] = 8;48b.shader->info.workgroup_size[1] = 8;49b.shader->info.workgroup_size[2] = 1;5051nir_ssa_def *invoc_id = nir_load_local_invocation_id(&b);52nir_ssa_def *wg_id = nir_load_workgroup_id(&b, 32);53nir_ssa_def *block_size =54nir_imm_ivec4(&b, b.shader->info.workgroup_size[0], b.shader->info.workgroup_size[1],55b.shader->info.workgroup_size[2], 0);5657/* Get coordinates. */58nir_ssa_def *global_id = nir_iadd(&b, nir_imul(&b, wg_id, block_size), invoc_id);59nir_ssa_def *coord = nir_channels(&b, global_id, 0x3);6061/* Multiply the coordinates by the HTILE block size. */62coord = nir_imul(&b, coord, nir_imm_ivec2(&b, 8, 8));6364/* Load constants. */65nir_ssa_def *constants = nir_load_push_constant(&b, 2, 32, nir_imm_int(&b, 0), .range = 8);66nir_ssa_def *htile_pitch = nir_channel(&b, constants, 0);67nir_ssa_def *htile_slice_size = nir_channel(&b, constants, 1);6869/* Get the HTILE addr from coordinates. */70nir_ssa_def *zero = nir_imm_int(&b, 0);71nir_ssa_def *htile_addr = ac_nir_htile_addr_from_coord(72&b, &device->physical_device->rad_info, &surf->u.gfx9.zs.htile_equation, htile_pitch,73htile_slice_size, nir_channel(&b, coord, 0), nir_channel(&b, coord, 1), zero, zero);7475/* Set up the input VRS image descriptor. */76const struct glsl_type *vrs_sampler_type =77glsl_sampler_type(GLSL_SAMPLER_DIM_2D, false, false, GLSL_TYPE_FLOAT);78nir_variable *input_vrs_img =79nir_variable_create(b.shader, nir_var_uniform, vrs_sampler_type, "input_vrs_image");80input_vrs_img->data.descriptor_set = 0;81input_vrs_img->data.binding = 0;8283nir_ssa_def *input_vrs_img_deref = &nir_build_deref_var(&b, input_vrs_img)->dest.ssa;8485/* Load the VRS rates from the 2D image. */86nir_tex_instr *tex = nir_tex_instr_create(b.shader, 3);87tex->sampler_dim = GLSL_SAMPLER_DIM_2D;88tex->op = nir_texop_txf;89tex->src[0].src_type = nir_tex_src_coord;90tex->src[0].src = nir_src_for_ssa(nir_channels(&b, global_id, 0x3));91tex->src[1].src_type = nir_tex_src_lod;92tex->src[1].src = nir_src_for_ssa(nir_imm_int(&b, 0));93tex->src[2].src_type = nir_tex_src_texture_deref;94tex->src[2].src = nir_src_for_ssa(input_vrs_img_deref);95tex->dest_type = nir_type_float32;96tex->is_array = false;97tex->coord_components = 2;9899nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, "tex");100nir_builder_instr_insert(&b, &tex->instr);101102/* Extract the X/Y rates and clamp them because the maximum supported VRS rate is 2x2 (1x1 in103* hardware).104*105* VRS rate X = min(value >> 2, 1)106* VRS rate Y = min(value & 3, 1)107*/108nir_ssa_def *x_rate = nir_ushr(&b, &tex->dest.ssa, nir_imm_int(&b, 2));109x_rate = nir_umin(&b, x_rate, nir_imm_int(&b, 1));110111nir_ssa_def *y_rate = nir_iand(&b, &tex->dest.ssa, nir_imm_int(&b, 3));112y_rate = nir_umin(&b, y_rate, nir_imm_int(&b, 1));113114/* Compute the final VRS rate. */115nir_ssa_def *vrs_rates = nir_ior(&b, nir_ishl(&b, y_rate, nir_imm_int(&b, 10)),116nir_ishl(&b, x_rate, nir_imm_int(&b, 6)));117118/* Load the HTILE buffer descriptor. */119nir_ssa_def *htile_buf = radv_meta_load_descriptor(&b, 0, 1);120121/* Load the existing HTILE 32-bit value for this 8x8 pixels area. */122nir_ssa_def *htile_value = nir_load_ssbo(&b, 1, 32, htile_buf, htile_addr, .align_mul = 4);123124/* Clear the 4-bit VRS rates. */125htile_value = nir_iand(&b, htile_value, nir_imm_int(&b, 0xfffff33f));126127/* Set the VRS rates loaded from the image. */128htile_value = nir_ior(&b, htile_value, vrs_rates);129130/* Store the updated HTILE 32-bit which contains the VRS rates. */131nir_store_ssbo(&b, htile_value, htile_buf, htile_addr, .write_mask = 0x1,132.access = ACCESS_NON_READABLE, .align_mul = 4);133134return b.shader;135}136137static VkResult138radv_device_init_meta_copy_vrs_htile_state(struct radv_device *device,139struct radeon_surf *surf)140{141struct radv_meta_state *state = &device->meta_state;142nir_shader *cs = build_copy_vrs_htile_shader(device, surf);143VkResult result;144145VkDescriptorSetLayoutCreateInfo ds_layout_info = {146.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,147.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR,148.bindingCount = 2,149.pBindings = (VkDescriptorSetLayoutBinding[]){150{.binding = 0,151.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,152.descriptorCount = 1,153.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,154.pImmutableSamplers = NULL},155{.binding = 1,156.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,157.descriptorCount = 1,158.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,159.pImmutableSamplers = NULL},160}};161162result = radv_CreateDescriptorSetLayout(radv_device_to_handle(device), &ds_layout_info,163&state->alloc, &state->copy_vrs_htile_ds_layout);164if (result != VK_SUCCESS)165goto fail;166167VkPipelineLayoutCreateInfo p_layout_info = {168.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,169.setLayoutCount = 1,170.pSetLayouts = &state->copy_vrs_htile_ds_layout,171.pushConstantRangeCount = 1,172.pPushConstantRanges =173&(VkPushConstantRange){174VK_SHADER_STAGE_COMPUTE_BIT,1750,1768,177},178};179180result = radv_CreatePipelineLayout(radv_device_to_handle(device), &p_layout_info, &state->alloc,181&state->copy_vrs_htile_p_layout);182if (result != VK_SUCCESS)183goto fail;184185VkPipelineShaderStageCreateInfo shader_stage = {186.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,187.stage = VK_SHADER_STAGE_COMPUTE_BIT,188.module = vk_shader_module_handle_from_nir(cs),189.pName = "main",190.pSpecializationInfo = NULL,191};192193VkComputePipelineCreateInfo pipeline_info = {194.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,195.stage = shader_stage,196.flags = 0,197.layout = state->copy_vrs_htile_p_layout,198};199200result = radv_CreateComputePipelines(radv_device_to_handle(device),201radv_pipeline_cache_to_handle(&state->cache), 1,202&pipeline_info, NULL, &state->copy_vrs_htile_pipeline);203fail:204ralloc_free(cs);205return result;206}207208void209radv_copy_vrs_htile(struct radv_cmd_buffer *cmd_buffer, struct radv_image *vrs_image,210VkExtent2D *extent, struct radv_image *dst_image)211{212struct radv_device *device = cmd_buffer->device;213struct radv_meta_state *state = &device->meta_state;214struct radv_meta_saved_state saved_state;215struct radv_image_view vrs_iview;216217assert(radv_image_has_htile(dst_image));218219if (!cmd_buffer->device->meta_state.copy_vrs_htile_pipeline) {220VkResult ret = radv_device_init_meta_copy_vrs_htile_state(cmd_buffer->device,221&dst_image->planes[0].surface);222if (ret != VK_SUCCESS) {223cmd_buffer->record_result = ret;224return;225}226}227228cmd_buffer->state.flush_bits |=229radv_src_access_flush(cmd_buffer, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, NULL) |230radv_dst_access_flush(cmd_buffer, VK_ACCESS_SHADER_READ_BIT, NULL);231232radv_meta_save(233&saved_state, cmd_buffer,234RADV_META_SAVE_COMPUTE_PIPELINE | RADV_META_SAVE_CONSTANTS | RADV_META_SAVE_DESCRIPTORS);235236radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer), VK_PIPELINE_BIND_POINT_COMPUTE,237state->copy_vrs_htile_pipeline);238239/* HTILE buffer */240uint64_t htile_offset = dst_image->offset + dst_image->planes[0].surface.meta_offset;241uint64_t htile_size = dst_image->planes[0].surface.meta_slice_size;242struct radv_buffer htile_buffer = {.bo = dst_image->bo,243.offset = htile_offset,244.size = htile_size};245246radv_image_view_init(&vrs_iview, cmd_buffer->device,247&(VkImageViewCreateInfo){248.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,249.image = radv_image_to_handle(vrs_image),250.viewType = VK_IMAGE_VIEW_TYPE_2D,251.format = vrs_image->vk_format,252.subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,253.baseMipLevel = 0,254.levelCount = 1,255.baseArrayLayer = 0,256.layerCount = 1},257},258NULL);259260radv_meta_push_descriptor_set(261cmd_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, state->copy_vrs_htile_p_layout, 0, /* set */2622, /* descriptorWriteCount */263(VkWriteDescriptorSet[]){264{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,265.dstBinding = 0,266.dstArrayElement = 0,267.descriptorCount = 1,268.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,269.pImageInfo =270(VkDescriptorImageInfo[]){271{272.sampler = VK_NULL_HANDLE,273.imageView = radv_image_view_to_handle(&vrs_iview),274.imageLayout = VK_IMAGE_LAYOUT_GENERAL,275},276}},277{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,278.dstBinding = 1,279.dstArrayElement = 0,280.descriptorCount = 1,281.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,282.pBufferInfo = &(VkDescriptorBufferInfo){.buffer = radv_buffer_to_handle(&htile_buffer),283.offset = 0,284.range = htile_size}}});285286const unsigned constants[2] = {287dst_image->planes[0].surface.meta_pitch, dst_image->planes[0].surface.meta_slice_size,288};289290radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer), state->copy_vrs_htile_p_layout,291VK_SHADER_STAGE_COMPUTE_BIT, 0, 8, constants);292293uint32_t width = DIV_ROUND_UP(extent->width, 8);294uint32_t height = DIV_ROUND_UP(extent->height, 8);295296radv_unaligned_dispatch(cmd_buffer, width, height, 1);297298radv_meta_restore(&saved_state, cmd_buffer);299300cmd_buffer->state.flush_bits |=301RADV_CMD_FLAG_CS_PARTIAL_FLUSH | RADV_CMD_FLAG_INV_VCACHE |302radv_src_access_flush(cmd_buffer, VK_ACCESS_SHADER_WRITE_BIT, NULL);303}304305306