Path: blob/21.2-virgl/src/amd/vulkan/radv_meta_fmask_expand.c
7417 views
/*1* Copyright © 2019 Valve Corporation2* Copyright © 2018 Red Hat3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS21* IN THE SOFTWARE.22*/2324#include "radv_meta.h"25#include "radv_private.h"26#include "vk_format.h"2728static nir_shader *29build_fmask_expand_compute_shader(struct radv_device *device, int samples)30{31const struct glsl_type *type =32glsl_sampler_type(GLSL_SAMPLER_DIM_MS, false, true, GLSL_TYPE_FLOAT);33const struct glsl_type *img_type = glsl_image_type(GLSL_SAMPLER_DIM_MS, true, GLSL_TYPE_FLOAT);3435nir_builder b =36nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, NULL, "meta_fmask_expand_cs-%d", samples);37b.shader->info.workgroup_size[0] = 8;38b.shader->info.workgroup_size[1] = 8;39b.shader->info.workgroup_size[2] = 1;4041nir_variable *input_img = nir_variable_create(b.shader, nir_var_uniform, type, "s_tex");42input_img->data.descriptor_set = 0;43input_img->data.binding = 0;4445nir_variable *output_img = nir_variable_create(b.shader, nir_var_uniform, img_type, "out_img");46output_img->data.descriptor_set = 0;47output_img->data.binding = 1;48output_img->data.access = ACCESS_NON_READABLE;4950nir_ssa_def *invoc_id = nir_load_local_invocation_id(&b);51nir_ssa_def *wg_id = nir_load_workgroup_id(&b, 32);52nir_ssa_def *block_size =53nir_imm_ivec4(&b, b.shader->info.workgroup_size[0], b.shader->info.workgroup_size[1],54b.shader->info.workgroup_size[2], 0);5556nir_ssa_def *global_id = nir_iadd(&b, nir_imul(&b, wg_id, block_size), invoc_id);57nir_ssa_def *layer_id = nir_channel(&b, wg_id, 2);5859nir_ssa_def *input_img_deref = &nir_build_deref_var(&b, input_img)->dest.ssa;60nir_ssa_def *output_img_deref = &nir_build_deref_var(&b, output_img)->dest.ssa;6162nir_ssa_def *tex_coord =63nir_vec3(&b, nir_channel(&b, global_id, 0), nir_channel(&b, global_id, 1), layer_id);6465nir_tex_instr *tex_instr[8];66for (uint32_t i = 0; i < samples; i++) {67tex_instr[i] = nir_tex_instr_create(b.shader, 3);6869nir_tex_instr *tex = tex_instr[i];70tex->sampler_dim = GLSL_SAMPLER_DIM_MS;71tex->op = nir_texop_txf_ms;72tex->src[0].src_type = nir_tex_src_coord;73tex->src[0].src = nir_src_for_ssa(tex_coord);74tex->src[1].src_type = nir_tex_src_ms_index;75tex->src[1].src = nir_src_for_ssa(nir_imm_int(&b, i));76tex->src[2].src_type = nir_tex_src_texture_deref;77tex->src[2].src = nir_src_for_ssa(input_img_deref);78tex->dest_type = nir_type_float32;79tex->is_array = true;80tex->coord_components = 3;8182nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, "tex");83nir_builder_instr_insert(&b, &tex->instr);84}8586nir_ssa_def *img_coord =87nir_vec4(&b, nir_channel(&b, tex_coord, 0), nir_channel(&b, tex_coord, 1),88nir_channel(&b, tex_coord, 2), nir_imm_int(&b, 0));8990for (uint32_t i = 0; i < samples; i++) {91nir_ssa_def *outval = &tex_instr[i]->dest.ssa;9293nir_image_deref_store(&b, output_img_deref, img_coord, nir_imm_int(&b, i), outval,94nir_imm_int(&b, 0));95}9697return b.shader;98}99100void101radv_expand_fmask_image_inplace(struct radv_cmd_buffer *cmd_buffer, struct radv_image *image,102const VkImageSubresourceRange *subresourceRange)103{104struct radv_device *device = cmd_buffer->device;105struct radv_meta_saved_state saved_state;106const uint32_t samples = image->info.samples;107const uint32_t samples_log2 = ffs(samples) - 1;108unsigned layer_count = radv_get_layerCount(image, subresourceRange);109struct radv_image_view iview;110111radv_meta_save(&saved_state, cmd_buffer,112RADV_META_SAVE_COMPUTE_PIPELINE | RADV_META_SAVE_DESCRIPTORS);113114VkPipeline pipeline = device->meta_state.fmask_expand.pipeline[samples_log2];115116radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer), VK_PIPELINE_BIND_POINT_COMPUTE,117pipeline);118119cmd_buffer->state.flush_bits |= radv_dst_access_flush(120cmd_buffer, VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT, image);121122radv_image_view_init(&iview, device,123&(VkImageViewCreateInfo){124.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,125.image = radv_image_to_handle(image),126.viewType = radv_meta_get_view_type(image),127.format = vk_format_no_srgb(image->vk_format),128.subresourceRange =129{130.aspectMask = subresourceRange->aspectMask,131.baseMipLevel = 0,132.levelCount = 1,133.baseArrayLayer = subresourceRange->baseArrayLayer,134.layerCount = layer_count,135},136},137NULL);138139radv_meta_push_descriptor_set(140cmd_buffer, VK_PIPELINE_BIND_POINT_COMPUTE,141cmd_buffer->device->meta_state.fmask_expand.p_layout, 0, /* set */1422, /* descriptorWriteCount */143(VkWriteDescriptorSet[]){{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,144.dstBinding = 0,145.dstArrayElement = 0,146.descriptorCount = 1,147.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,148.pImageInfo =149(VkDescriptorImageInfo[]){150{.sampler = VK_NULL_HANDLE,151.imageView = radv_image_view_to_handle(&iview),152.imageLayout = VK_IMAGE_LAYOUT_GENERAL},153}},154{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,155.dstBinding = 1,156.dstArrayElement = 0,157.descriptorCount = 1,158.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,159.pImageInfo = (VkDescriptorImageInfo[]){160{.sampler = VK_NULL_HANDLE,161.imageView = radv_image_view_to_handle(&iview),162.imageLayout = VK_IMAGE_LAYOUT_GENERAL},163}}});164165radv_unaligned_dispatch(cmd_buffer, image->info.width, image->info.height, layer_count);166167radv_meta_restore(&saved_state, cmd_buffer);168169cmd_buffer->state.flush_bits |=170RADV_CMD_FLAG_CS_PARTIAL_FLUSH |171radv_src_access_flush(cmd_buffer, VK_ACCESS_SHADER_WRITE_BIT, image);172173/* Re-initialize FMASK in fully expanded mode. */174cmd_buffer->state.flush_bits |= radv_init_fmask(cmd_buffer, image, subresourceRange);175}176177void178radv_device_finish_meta_fmask_expand_state(struct radv_device *device)179{180struct radv_meta_state *state = &device->meta_state;181182for (uint32_t i = 0; i < MAX_SAMPLES_LOG2; ++i) {183radv_DestroyPipeline(radv_device_to_handle(device), state->fmask_expand.pipeline[i],184&state->alloc);185}186radv_DestroyPipelineLayout(radv_device_to_handle(device), state->fmask_expand.p_layout,187&state->alloc);188189radv_DestroyDescriptorSetLayout(radv_device_to_handle(device), state->fmask_expand.ds_layout,190&state->alloc);191}192193static VkResult194create_fmask_expand_pipeline(struct radv_device *device, int samples, VkPipeline *pipeline)195{196struct radv_meta_state *state = &device->meta_state;197VkResult result;198nir_shader *cs = build_fmask_expand_compute_shader(device, samples);199;200201VkPipelineShaderStageCreateInfo pipeline_shader_stage = {202.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,203.stage = VK_SHADER_STAGE_COMPUTE_BIT,204.module = vk_shader_module_handle_from_nir(cs),205.pName = "main",206.pSpecializationInfo = NULL,207};208209VkComputePipelineCreateInfo vk_pipeline_info = {210.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,211.stage = pipeline_shader_stage,212.flags = 0,213.layout = state->fmask_expand.p_layout,214};215216result = radv_CreateComputePipelines(radv_device_to_handle(device),217radv_pipeline_cache_to_handle(&state->cache), 1,218&vk_pipeline_info, NULL, pipeline);219220ralloc_free(cs);221return result;222}223224VkResult225radv_device_init_meta_fmask_expand_state(struct radv_device *device)226{227struct radv_meta_state *state = &device->meta_state;228VkResult result;229230VkDescriptorSetLayoutCreateInfo ds_create_info = {231.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,232.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR,233.bindingCount = 2,234.pBindings = (VkDescriptorSetLayoutBinding[]){235{.binding = 0,236.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,237.descriptorCount = 1,238.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,239.pImmutableSamplers = NULL},240{.binding = 1,241.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,242.descriptorCount = 1,243.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,244.pImmutableSamplers = NULL},245}};246247result = radv_CreateDescriptorSetLayout(radv_device_to_handle(device), &ds_create_info,248&state->alloc, &state->fmask_expand.ds_layout);249if (result != VK_SUCCESS)250goto fail;251252VkPipelineLayoutCreateInfo color_create_info = {253.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,254.setLayoutCount = 1,255.pSetLayouts = &state->fmask_expand.ds_layout,256.pushConstantRangeCount = 0,257.pPushConstantRanges = NULL,258};259260result = radv_CreatePipelineLayout(radv_device_to_handle(device), &color_create_info,261&state->alloc, &state->fmask_expand.p_layout);262if (result != VK_SUCCESS)263goto fail;264265for (uint32_t i = 0; i < MAX_SAMPLES_LOG2; i++) {266uint32_t samples = 1 << i;267result = create_fmask_expand_pipeline(device, samples, &state->fmask_expand.pipeline[i]);268if (result != VK_SUCCESS)269goto fail;270}271272return VK_SUCCESS;273fail:274radv_device_finish_meta_fmask_expand_state(device);275return result;276}277278279