Path: blob/21.2-virgl/src/amd/vulkan/radv_meta_bufimage.c
7202 views
/*1* Copyright © 2016 Red Hat.2* Copyright © 2016 Bas Nieuwenhuizen3*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*/23#include "nir/nir_builder.h"24#include "radv_meta.h"2526/*27* GFX queue: Compute shader implementation of image->buffer copy28* Compute queue: implementation also of buffer->image, image->image, and image clear.29*/3031/* GFX9 needs to use a 3D sampler to access 3D resources, so the shader has the options32* for that.33*/34static nir_shader *35build_nir_itob_compute_shader(struct radv_device *dev, bool is_3d)36{37enum glsl_sampler_dim dim = is_3d ? GLSL_SAMPLER_DIM_3D : GLSL_SAMPLER_DIM_2D;38const struct glsl_type *sampler_type = glsl_sampler_type(dim, false, false, GLSL_TYPE_FLOAT);39const struct glsl_type *img_type = glsl_image_type(GLSL_SAMPLER_DIM_BUF, false, GLSL_TYPE_FLOAT);40nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, NULL,41is_3d ? "meta_itob_cs_3d" : "meta_itob_cs");42b.shader->info.workgroup_size[0] = 8;43b.shader->info.workgroup_size[1] = 8;44b.shader->info.workgroup_size[2] = 1;45nir_variable *input_img = nir_variable_create(b.shader, nir_var_uniform, sampler_type, "s_tex");46input_img->data.descriptor_set = 0;47input_img->data.binding = 0;4849nir_variable *output_img = nir_variable_create(b.shader, nir_var_uniform, img_type, "out_img");50output_img->data.descriptor_set = 0;51output_img->data.binding = 1;5253nir_ssa_def *invoc_id = nir_load_local_invocation_id(&b);54nir_ssa_def *wg_id = nir_load_workgroup_id(&b, 32);55nir_ssa_def *block_size =56nir_imm_ivec4(&b, b.shader->info.workgroup_size[0], b.shader->info.workgroup_size[1],57b.shader->info.workgroup_size[2], 0);5859nir_ssa_def *global_id = nir_iadd(&b, nir_imul(&b, wg_id, block_size), invoc_id);6061nir_ssa_def *offset =62nir_load_push_constant(&b, is_3d ? 3 : 2, 32, nir_imm_int(&b, 0), .range = 16);63nir_ssa_def *stride = nir_load_push_constant(&b, 1, 32, nir_imm_int(&b, 12), .range = 16);6465nir_ssa_def *img_coord = nir_iadd(&b, global_id, offset);66nir_ssa_def *input_img_deref = &nir_build_deref_var(&b, input_img)->dest.ssa;6768nir_tex_instr *tex = nir_tex_instr_create(b.shader, 3);69tex->sampler_dim = dim;70tex->op = nir_texop_txf;71tex->src[0].src_type = nir_tex_src_coord;72tex->src[0].src = nir_src_for_ssa(nir_channels(&b, img_coord, is_3d ? 0x7 : 0x3));73tex->src[1].src_type = nir_tex_src_lod;74tex->src[1].src = nir_src_for_ssa(nir_imm_int(&b, 0));75tex->src[2].src_type = nir_tex_src_texture_deref;76tex->src[2].src = nir_src_for_ssa(input_img_deref);77tex->dest_type = nir_type_float32;78tex->is_array = false;79tex->coord_components = is_3d ? 3 : 2;8081nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, "tex");82nir_builder_instr_insert(&b, &tex->instr);8384nir_ssa_def *pos_x = nir_channel(&b, global_id, 0);85nir_ssa_def *pos_y = nir_channel(&b, global_id, 1);8687nir_ssa_def *tmp = nir_imul(&b, pos_y, stride);88tmp = nir_iadd(&b, tmp, pos_x);8990nir_ssa_def *coord = nir_vec4(&b, tmp, tmp, tmp, tmp);9192nir_ssa_def *outval = &tex->dest.ssa;93nir_image_deref_store(&b, &nir_build_deref_var(&b, output_img)->dest.ssa, coord,94nir_ssa_undef(&b, 1, 32), outval, nir_imm_int(&b, 0));9596return b.shader;97}9899/* Image to buffer - don't write use image accessors */100static VkResult101radv_device_init_meta_itob_state(struct radv_device *device)102{103VkResult result;104nir_shader *cs = build_nir_itob_compute_shader(device, false);105nir_shader *cs_3d = NULL;106107if (device->physical_device->rad_info.chip_class >= GFX9)108cs_3d = build_nir_itob_compute_shader(device, true);109110/*111* two descriptors one for the image being sampled112* one for the buffer being written.113*/114VkDescriptorSetLayoutCreateInfo ds_create_info = {115.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,116.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR,117.bindingCount = 2,118.pBindings = (VkDescriptorSetLayoutBinding[]){119{.binding = 0,120.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,121.descriptorCount = 1,122.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,123.pImmutableSamplers = NULL},124{.binding = 1,125.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER,126.descriptorCount = 1,127.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,128.pImmutableSamplers = NULL},129}};130131result = radv_CreateDescriptorSetLayout(radv_device_to_handle(device), &ds_create_info,132&device->meta_state.alloc,133&device->meta_state.itob.img_ds_layout);134if (result != VK_SUCCESS)135goto fail;136137VkPipelineLayoutCreateInfo pl_create_info = {138.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,139.setLayoutCount = 1,140.pSetLayouts = &device->meta_state.itob.img_ds_layout,141.pushConstantRangeCount = 1,142.pPushConstantRanges = &(VkPushConstantRange){VK_SHADER_STAGE_COMPUTE_BIT, 0, 16},143};144145result =146radv_CreatePipelineLayout(radv_device_to_handle(device), &pl_create_info,147&device->meta_state.alloc, &device->meta_state.itob.img_p_layout);148if (result != VK_SUCCESS)149goto fail;150151/* compute shader */152153VkPipelineShaderStageCreateInfo pipeline_shader_stage = {154.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,155.stage = VK_SHADER_STAGE_COMPUTE_BIT,156.module = vk_shader_module_handle_from_nir(cs),157.pName = "main",158.pSpecializationInfo = NULL,159};160161VkComputePipelineCreateInfo vk_pipeline_info = {162.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,163.stage = pipeline_shader_stage,164.flags = 0,165.layout = device->meta_state.itob.img_p_layout,166};167168result = radv_CreateComputePipelines(radv_device_to_handle(device),169radv_pipeline_cache_to_handle(&device->meta_state.cache), 1,170&vk_pipeline_info, NULL, &device->meta_state.itob.pipeline);171if (result != VK_SUCCESS)172goto fail;173174if (device->physical_device->rad_info.chip_class >= GFX9) {175VkPipelineShaderStageCreateInfo pipeline_shader_stage_3d = {176.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,177.stage = VK_SHADER_STAGE_COMPUTE_BIT,178.module = vk_shader_module_handle_from_nir(cs_3d),179.pName = "main",180.pSpecializationInfo = NULL,181};182183VkComputePipelineCreateInfo vk_pipeline_info_3d = {184.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,185.stage = pipeline_shader_stage_3d,186.flags = 0,187.layout = device->meta_state.itob.img_p_layout,188};189190result = radv_CreateComputePipelines(191radv_device_to_handle(device), radv_pipeline_cache_to_handle(&device->meta_state.cache), 1,192&vk_pipeline_info_3d, NULL, &device->meta_state.itob.pipeline_3d);193if (result != VK_SUCCESS)194goto fail;195ralloc_free(cs_3d);196}197ralloc_free(cs);198199return VK_SUCCESS;200fail:201ralloc_free(cs);202ralloc_free(cs_3d);203return result;204}205206static void207radv_device_finish_meta_itob_state(struct radv_device *device)208{209struct radv_meta_state *state = &device->meta_state;210211radv_DestroyPipelineLayout(radv_device_to_handle(device), state->itob.img_p_layout,212&state->alloc);213radv_DestroyDescriptorSetLayout(radv_device_to_handle(device), state->itob.img_ds_layout,214&state->alloc);215radv_DestroyPipeline(radv_device_to_handle(device), state->itob.pipeline, &state->alloc);216if (device->physical_device->rad_info.chip_class >= GFX9)217radv_DestroyPipeline(radv_device_to_handle(device), state->itob.pipeline_3d, &state->alloc);218}219220static nir_shader *221build_nir_btoi_compute_shader(struct radv_device *dev, bool is_3d)222{223enum glsl_sampler_dim dim = is_3d ? GLSL_SAMPLER_DIM_3D : GLSL_SAMPLER_DIM_2D;224const struct glsl_type *buf_type =225glsl_sampler_type(GLSL_SAMPLER_DIM_BUF, false, false, GLSL_TYPE_FLOAT);226const struct glsl_type *img_type = glsl_image_type(dim, false, GLSL_TYPE_FLOAT);227nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, NULL,228is_3d ? "meta_btoi_cs_3d" : "meta_btoi_cs");229b.shader->info.workgroup_size[0] = 8;230b.shader->info.workgroup_size[1] = 8;231b.shader->info.workgroup_size[2] = 1;232nir_variable *input_img = nir_variable_create(b.shader, nir_var_uniform, buf_type, "s_tex");233input_img->data.descriptor_set = 0;234input_img->data.binding = 0;235236nir_variable *output_img = nir_variable_create(b.shader, nir_var_uniform, img_type, "out_img");237output_img->data.descriptor_set = 0;238output_img->data.binding = 1;239240nir_ssa_def *invoc_id = nir_load_local_invocation_id(&b);241nir_ssa_def *wg_id = nir_load_workgroup_id(&b, 32);242nir_ssa_def *block_size =243nir_imm_ivec4(&b, b.shader->info.workgroup_size[0], b.shader->info.workgroup_size[1],244b.shader->info.workgroup_size[2], 0);245246nir_ssa_def *global_id = nir_iadd(&b, nir_imul(&b, wg_id, block_size), invoc_id);247248nir_ssa_def *offset =249nir_load_push_constant(&b, is_3d ? 3 : 2, 32, nir_imm_int(&b, 0), .range = 16);250nir_ssa_def *stride = nir_load_push_constant(&b, 1, 32, nir_imm_int(&b, 12), .range = 16);251252nir_ssa_def *pos_x = nir_channel(&b, global_id, 0);253nir_ssa_def *pos_y = nir_channel(&b, global_id, 1);254255nir_ssa_def *tmp = nir_imul(&b, pos_y, stride);256tmp = nir_iadd(&b, tmp, pos_x);257258nir_ssa_def *buf_coord = nir_vec4(&b, tmp, tmp, tmp, tmp);259260nir_ssa_def *img_coord = nir_iadd(&b, global_id, offset);261nir_ssa_def *input_img_deref = &nir_build_deref_var(&b, input_img)->dest.ssa;262263nir_tex_instr *tex = nir_tex_instr_create(b.shader, 3);264tex->sampler_dim = GLSL_SAMPLER_DIM_BUF;265tex->op = nir_texop_txf;266tex->src[0].src_type = nir_tex_src_coord;267tex->src[0].src = nir_src_for_ssa(nir_channels(&b, buf_coord, 1));268tex->src[1].src_type = nir_tex_src_lod;269tex->src[1].src = nir_src_for_ssa(nir_imm_int(&b, 0));270tex->src[2].src_type = nir_tex_src_texture_deref;271tex->src[2].src = nir_src_for_ssa(input_img_deref);272tex->dest_type = nir_type_float32;273tex->is_array = false;274tex->coord_components = 1;275276nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, "tex");277nir_builder_instr_insert(&b, &tex->instr);278279nir_ssa_def *outval = &tex->dest.ssa;280nir_image_deref_store(&b, &nir_build_deref_var(&b, output_img)->dest.ssa, img_coord,281nir_ssa_undef(&b, 1, 32), outval, nir_imm_int(&b, 0));282283return b.shader;284}285286/* Buffer to image - don't write use image accessors */287static VkResult288radv_device_init_meta_btoi_state(struct radv_device *device)289{290VkResult result;291nir_shader *cs = build_nir_btoi_compute_shader(device, false);292nir_shader *cs_3d = NULL;293if (device->physical_device->rad_info.chip_class >= GFX9)294cs_3d = build_nir_btoi_compute_shader(device, true);295/*296* two descriptors one for the image being sampled297* one for the buffer being written.298*/299VkDescriptorSetLayoutCreateInfo ds_create_info = {300.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,301.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR,302.bindingCount = 2,303.pBindings = (VkDescriptorSetLayoutBinding[]){304{.binding = 0,305.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER,306.descriptorCount = 1,307.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,308.pImmutableSamplers = NULL},309{.binding = 1,310.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,311.descriptorCount = 1,312.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,313.pImmutableSamplers = NULL},314}};315316result = radv_CreateDescriptorSetLayout(radv_device_to_handle(device), &ds_create_info,317&device->meta_state.alloc,318&device->meta_state.btoi.img_ds_layout);319if (result != VK_SUCCESS)320goto fail;321322VkPipelineLayoutCreateInfo pl_create_info = {323.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,324.setLayoutCount = 1,325.pSetLayouts = &device->meta_state.btoi.img_ds_layout,326.pushConstantRangeCount = 1,327.pPushConstantRanges = &(VkPushConstantRange){VK_SHADER_STAGE_COMPUTE_BIT, 0, 16},328};329330result =331radv_CreatePipelineLayout(radv_device_to_handle(device), &pl_create_info,332&device->meta_state.alloc, &device->meta_state.btoi.img_p_layout);333if (result != VK_SUCCESS)334goto fail;335336/* compute shader */337338VkPipelineShaderStageCreateInfo pipeline_shader_stage = {339.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,340.stage = VK_SHADER_STAGE_COMPUTE_BIT,341.module = vk_shader_module_handle_from_nir(cs),342.pName = "main",343.pSpecializationInfo = NULL,344};345346VkComputePipelineCreateInfo vk_pipeline_info = {347.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,348.stage = pipeline_shader_stage,349.flags = 0,350.layout = device->meta_state.btoi.img_p_layout,351};352353result = radv_CreateComputePipelines(radv_device_to_handle(device),354radv_pipeline_cache_to_handle(&device->meta_state.cache), 1,355&vk_pipeline_info, NULL, &device->meta_state.btoi.pipeline);356if (result != VK_SUCCESS)357goto fail;358359if (device->physical_device->rad_info.chip_class >= GFX9) {360VkPipelineShaderStageCreateInfo pipeline_shader_stage_3d = {361.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,362.stage = VK_SHADER_STAGE_COMPUTE_BIT,363.module = vk_shader_module_handle_from_nir(cs_3d),364.pName = "main",365.pSpecializationInfo = NULL,366};367368VkComputePipelineCreateInfo vk_pipeline_info_3d = {369.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,370.stage = pipeline_shader_stage_3d,371.flags = 0,372.layout = device->meta_state.btoi.img_p_layout,373};374375result = radv_CreateComputePipelines(376radv_device_to_handle(device), radv_pipeline_cache_to_handle(&device->meta_state.cache), 1,377&vk_pipeline_info_3d, NULL, &device->meta_state.btoi.pipeline_3d);378ralloc_free(cs_3d);379}380ralloc_free(cs);381382return VK_SUCCESS;383fail:384ralloc_free(cs_3d);385ralloc_free(cs);386return result;387}388389static void390radv_device_finish_meta_btoi_state(struct radv_device *device)391{392struct radv_meta_state *state = &device->meta_state;393394radv_DestroyPipelineLayout(radv_device_to_handle(device), state->btoi.img_p_layout,395&state->alloc);396radv_DestroyDescriptorSetLayout(radv_device_to_handle(device), state->btoi.img_ds_layout,397&state->alloc);398radv_DestroyPipeline(radv_device_to_handle(device), state->btoi.pipeline, &state->alloc);399radv_DestroyPipeline(radv_device_to_handle(device), state->btoi.pipeline_3d, &state->alloc);400}401402/* Buffer to image - special path for R32G32B32 */403static nir_shader *404build_nir_btoi_r32g32b32_compute_shader(struct radv_device *dev)405{406const struct glsl_type *buf_type =407glsl_sampler_type(GLSL_SAMPLER_DIM_BUF, false, false, GLSL_TYPE_FLOAT);408const struct glsl_type *img_type = glsl_image_type(GLSL_SAMPLER_DIM_BUF, false, GLSL_TYPE_FLOAT);409nir_builder b =410nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, NULL, "meta_btoi_r32g32b32_cs");411b.shader->info.workgroup_size[0] = 8;412b.shader->info.workgroup_size[1] = 8;413b.shader->info.workgroup_size[2] = 1;414nir_variable *input_img = nir_variable_create(b.shader, nir_var_uniform, buf_type, "s_tex");415input_img->data.descriptor_set = 0;416input_img->data.binding = 0;417418nir_variable *output_img = nir_variable_create(b.shader, nir_var_uniform, img_type, "out_img");419output_img->data.descriptor_set = 0;420output_img->data.binding = 1;421422nir_ssa_def *invoc_id = nir_load_local_invocation_id(&b);423nir_ssa_def *wg_id = nir_load_workgroup_id(&b, 32);424nir_ssa_def *block_size =425nir_imm_ivec4(&b, b.shader->info.workgroup_size[0], b.shader->info.workgroup_size[1],426b.shader->info.workgroup_size[2], 0);427428nir_ssa_def *global_id = nir_iadd(&b, nir_imul(&b, wg_id, block_size), invoc_id);429430nir_ssa_def *offset = nir_load_push_constant(&b, 2, 32, nir_imm_int(&b, 0), .range = 16);431nir_ssa_def *pitch = nir_load_push_constant(&b, 1, 32, nir_imm_int(&b, 8), .range = 16);432nir_ssa_def *stride = nir_load_push_constant(&b, 1, 32, nir_imm_int(&b, 12), .range = 16);433434nir_ssa_def *pos_x = nir_channel(&b, global_id, 0);435nir_ssa_def *pos_y = nir_channel(&b, global_id, 1);436437nir_ssa_def *tmp = nir_imul(&b, pos_y, stride);438tmp = nir_iadd(&b, tmp, pos_x);439440nir_ssa_def *buf_coord = nir_vec4(&b, tmp, tmp, tmp, tmp);441442nir_ssa_def *img_coord = nir_iadd(&b, global_id, offset);443444nir_ssa_def *global_pos =445nir_iadd(&b, nir_imul(&b, nir_channel(&b, img_coord, 1), pitch),446nir_imul(&b, nir_channel(&b, img_coord, 0), nir_imm_int(&b, 3)));447448nir_ssa_def *input_img_deref = &nir_build_deref_var(&b, input_img)->dest.ssa;449450nir_tex_instr *tex = nir_tex_instr_create(b.shader, 3);451tex->sampler_dim = GLSL_SAMPLER_DIM_BUF;452tex->op = nir_texop_txf;453tex->src[0].src_type = nir_tex_src_coord;454tex->src[0].src = nir_src_for_ssa(nir_channels(&b, buf_coord, 1));455tex->src[1].src_type = nir_tex_src_lod;456tex->src[1].src = nir_src_for_ssa(nir_imm_int(&b, 0));457tex->src[2].src_type = nir_tex_src_texture_deref;458tex->src[2].src = nir_src_for_ssa(input_img_deref);459tex->dest_type = nir_type_float32;460tex->is_array = false;461tex->coord_components = 1;462nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, "tex");463nir_builder_instr_insert(&b, &tex->instr);464465nir_ssa_def *outval = &tex->dest.ssa;466467for (int chan = 0; chan < 3; chan++) {468nir_ssa_def *local_pos = nir_iadd(&b, global_pos, nir_imm_int(&b, chan));469470nir_ssa_def *coord = nir_vec4(&b, local_pos, local_pos, local_pos, local_pos);471472nir_image_deref_store(&b, &nir_build_deref_var(&b, output_img)->dest.ssa, coord,473nir_ssa_undef(&b, 1, 32), nir_channel(&b, outval, chan),474nir_imm_int(&b, 0));475}476477return b.shader;478}479480static VkResult481radv_device_init_meta_btoi_r32g32b32_state(struct radv_device *device)482{483VkResult result;484nir_shader *cs = build_nir_btoi_r32g32b32_compute_shader(device);485486VkDescriptorSetLayoutCreateInfo ds_create_info = {487.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,488.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR,489.bindingCount = 2,490.pBindings = (VkDescriptorSetLayoutBinding[]){491{.binding = 0,492.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER,493.descriptorCount = 1,494.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,495.pImmutableSamplers = NULL},496{.binding = 1,497.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER,498.descriptorCount = 1,499.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,500.pImmutableSamplers = NULL},501}};502503result = radv_CreateDescriptorSetLayout(radv_device_to_handle(device), &ds_create_info,504&device->meta_state.alloc,505&device->meta_state.btoi_r32g32b32.img_ds_layout);506if (result != VK_SUCCESS)507goto fail;508509VkPipelineLayoutCreateInfo pl_create_info = {510.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,511.setLayoutCount = 1,512.pSetLayouts = &device->meta_state.btoi_r32g32b32.img_ds_layout,513.pushConstantRangeCount = 1,514.pPushConstantRanges = &(VkPushConstantRange){VK_SHADER_STAGE_COMPUTE_BIT, 0, 16},515};516517result = radv_CreatePipelineLayout(radv_device_to_handle(device), &pl_create_info,518&device->meta_state.alloc,519&device->meta_state.btoi_r32g32b32.img_p_layout);520if (result != VK_SUCCESS)521goto fail;522523/* compute shader */524525VkPipelineShaderStageCreateInfo pipeline_shader_stage = {526.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,527.stage = VK_SHADER_STAGE_COMPUTE_BIT,528.module = vk_shader_module_handle_from_nir(cs),529.pName = "main",530.pSpecializationInfo = NULL,531};532533VkComputePipelineCreateInfo vk_pipeline_info = {534.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,535.stage = pipeline_shader_stage,536.flags = 0,537.layout = device->meta_state.btoi_r32g32b32.img_p_layout,538};539540result = radv_CreateComputePipelines(541radv_device_to_handle(device), radv_pipeline_cache_to_handle(&device->meta_state.cache), 1,542&vk_pipeline_info, NULL, &device->meta_state.btoi_r32g32b32.pipeline);543544fail:545ralloc_free(cs);546return result;547}548549static void550radv_device_finish_meta_btoi_r32g32b32_state(struct radv_device *device)551{552struct radv_meta_state *state = &device->meta_state;553554radv_DestroyPipelineLayout(radv_device_to_handle(device), state->btoi_r32g32b32.img_p_layout,555&state->alloc);556radv_DestroyDescriptorSetLayout(radv_device_to_handle(device),557state->btoi_r32g32b32.img_ds_layout, &state->alloc);558radv_DestroyPipeline(radv_device_to_handle(device), state->btoi_r32g32b32.pipeline,559&state->alloc);560}561562static nir_shader *563build_nir_itoi_compute_shader(struct radv_device *dev, bool is_3d, int samples)564{565bool is_multisampled = samples > 1;566enum glsl_sampler_dim dim = is_3d ? GLSL_SAMPLER_DIM_3D567: is_multisampled ? GLSL_SAMPLER_DIM_MS568: GLSL_SAMPLER_DIM_2D;569const struct glsl_type *buf_type = glsl_sampler_type(dim, false, false, GLSL_TYPE_FLOAT);570const struct glsl_type *img_type = glsl_image_type(dim, false, GLSL_TYPE_FLOAT);571nir_builder b = nir_builder_init_simple_shader(572MESA_SHADER_COMPUTE, NULL, is_3d ? "meta_itoi_cs_3d-%d" : "meta_itoi_cs-%d", samples);573b.shader->info.workgroup_size[0] = 8;574b.shader->info.workgroup_size[1] = 8;575b.shader->info.workgroup_size[2] = 1;576nir_variable *input_img = nir_variable_create(b.shader, nir_var_uniform, buf_type, "s_tex");577input_img->data.descriptor_set = 0;578input_img->data.binding = 0;579580nir_variable *output_img = nir_variable_create(b.shader, nir_var_uniform, img_type, "out_img");581output_img->data.descriptor_set = 0;582output_img->data.binding = 1;583584nir_ssa_def *invoc_id = nir_load_local_invocation_id(&b);585nir_ssa_def *wg_id = nir_load_workgroup_id(&b, 32);586nir_ssa_def *block_size =587nir_imm_ivec4(&b, b.shader->info.workgroup_size[0], b.shader->info.workgroup_size[1],588b.shader->info.workgroup_size[2], 0);589590nir_ssa_def *global_id = nir_iadd(&b, nir_imul(&b, wg_id, block_size), invoc_id);591592nir_ssa_def *src_offset =593nir_load_push_constant(&b, is_3d ? 3 : 2, 32, nir_imm_int(&b, 0), .range = 24);594nir_ssa_def *dst_offset =595nir_load_push_constant(&b, is_3d ? 3 : 2, 32, nir_imm_int(&b, 12), .range = 24);596597nir_ssa_def *src_coord = nir_iadd(&b, global_id, src_offset);598nir_ssa_def *input_img_deref = &nir_build_deref_var(&b, input_img)->dest.ssa;599600nir_ssa_def *dst_coord = nir_iadd(&b, global_id, dst_offset);601602nir_tex_instr *tex_instr[8];603for (uint32_t i = 0; i < samples; i++) {604tex_instr[i] = nir_tex_instr_create(b.shader, is_multisampled ? 4 : 3);605606nir_tex_instr *tex = tex_instr[i];607tex->sampler_dim = dim;608tex->op = is_multisampled ? nir_texop_txf_ms : nir_texop_txf;609tex->src[0].src_type = nir_tex_src_coord;610tex->src[0].src = nir_src_for_ssa(nir_channels(&b, src_coord, is_3d ? 0x7 : 0x3));611tex->src[1].src_type = nir_tex_src_lod;612tex->src[1].src = nir_src_for_ssa(nir_imm_int(&b, 0));613tex->src[2].src_type = nir_tex_src_texture_deref;614tex->src[2].src = nir_src_for_ssa(input_img_deref);615if (is_multisampled) {616tex->src[3].src_type = nir_tex_src_ms_index;617tex->src[3].src = nir_src_for_ssa(nir_imm_int(&b, i));618}619tex->dest_type = nir_type_float32;620tex->is_array = false;621tex->coord_components = is_3d ? 3 : 2;622623nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, "tex");624nir_builder_instr_insert(&b, &tex->instr);625}626627for (uint32_t i = 0; i < samples; i++) {628nir_ssa_def *outval = &tex_instr[i]->dest.ssa;629nir_image_deref_store(&b, &nir_build_deref_var(&b, output_img)->dest.ssa, dst_coord,630nir_imm_int(&b, i), outval, nir_imm_int(&b, 0));631}632633return b.shader;634}635636static VkResult637create_itoi_pipeline(struct radv_device *device, int samples, VkPipeline *pipeline)638{639struct radv_meta_state *state = &device->meta_state;640nir_shader *cs = build_nir_itoi_compute_shader(device, false, samples);641VkResult result;642643VkPipelineShaderStageCreateInfo pipeline_shader_stage = {644.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,645.stage = VK_SHADER_STAGE_COMPUTE_BIT,646.module = vk_shader_module_handle_from_nir(cs),647.pName = "main",648.pSpecializationInfo = NULL,649};650651VkComputePipelineCreateInfo vk_pipeline_info = {652.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,653.stage = pipeline_shader_stage,654.flags = 0,655.layout = state->itoi.img_p_layout,656};657658result = radv_CreateComputePipelines(radv_device_to_handle(device),659radv_pipeline_cache_to_handle(&state->cache), 1,660&vk_pipeline_info, NULL, pipeline);661ralloc_free(cs);662return result;663}664665/* image to image - don't write use image accessors */666static VkResult667radv_device_init_meta_itoi_state(struct radv_device *device)668{669VkResult result;670671/*672* two descriptors one for the image being sampled673* one for the buffer being written.674*/675VkDescriptorSetLayoutCreateInfo ds_create_info = {676.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,677.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR,678.bindingCount = 2,679.pBindings = (VkDescriptorSetLayoutBinding[]){680{.binding = 0,681.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,682.descriptorCount = 1,683.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,684.pImmutableSamplers = NULL},685{.binding = 1,686.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,687.descriptorCount = 1,688.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,689.pImmutableSamplers = NULL},690}};691692result = radv_CreateDescriptorSetLayout(radv_device_to_handle(device), &ds_create_info,693&device->meta_state.alloc,694&device->meta_state.itoi.img_ds_layout);695if (result != VK_SUCCESS)696goto fail;697698VkPipelineLayoutCreateInfo pl_create_info = {699.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,700.setLayoutCount = 1,701.pSetLayouts = &device->meta_state.itoi.img_ds_layout,702.pushConstantRangeCount = 1,703.pPushConstantRanges = &(VkPushConstantRange){VK_SHADER_STAGE_COMPUTE_BIT, 0, 24},704};705706result =707radv_CreatePipelineLayout(radv_device_to_handle(device), &pl_create_info,708&device->meta_state.alloc, &device->meta_state.itoi.img_p_layout);709if (result != VK_SUCCESS)710goto fail;711712for (uint32_t i = 0; i < MAX_SAMPLES_LOG2; i++) {713uint32_t samples = 1 << i;714result = create_itoi_pipeline(device, samples, &device->meta_state.itoi.pipeline[i]);715if (result != VK_SUCCESS)716goto fail;717}718719if (device->physical_device->rad_info.chip_class >= GFX9) {720nir_shader *cs_3d = build_nir_itoi_compute_shader(device, true, 1);721722VkPipelineShaderStageCreateInfo pipeline_shader_stage_3d = {723.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,724.stage = VK_SHADER_STAGE_COMPUTE_BIT,725.module = vk_shader_module_handle_from_nir(cs_3d),726.pName = "main",727.pSpecializationInfo = NULL,728};729730VkComputePipelineCreateInfo vk_pipeline_info_3d = {731.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,732.stage = pipeline_shader_stage_3d,733.flags = 0,734.layout = device->meta_state.itoi.img_p_layout,735};736737result = radv_CreateComputePipelines(738radv_device_to_handle(device), radv_pipeline_cache_to_handle(&device->meta_state.cache), 1,739&vk_pipeline_info_3d, NULL, &device->meta_state.itoi.pipeline_3d);740ralloc_free(cs_3d);741}742743return VK_SUCCESS;744fail:745return result;746}747748static void749radv_device_finish_meta_itoi_state(struct radv_device *device)750{751struct radv_meta_state *state = &device->meta_state;752753radv_DestroyPipelineLayout(radv_device_to_handle(device), state->itoi.img_p_layout,754&state->alloc);755radv_DestroyDescriptorSetLayout(radv_device_to_handle(device), state->itoi.img_ds_layout,756&state->alloc);757758for (uint32_t i = 0; i < MAX_SAMPLES_LOG2; ++i) {759radv_DestroyPipeline(radv_device_to_handle(device), state->itoi.pipeline[i], &state->alloc);760}761762if (device->physical_device->rad_info.chip_class >= GFX9)763radv_DestroyPipeline(radv_device_to_handle(device), state->itoi.pipeline_3d, &state->alloc);764}765766static nir_shader *767build_nir_itoi_r32g32b32_compute_shader(struct radv_device *dev)768{769const struct glsl_type *type =770glsl_sampler_type(GLSL_SAMPLER_DIM_BUF, false, false, GLSL_TYPE_FLOAT);771const struct glsl_type *img_type = glsl_image_type(GLSL_SAMPLER_DIM_BUF, false, GLSL_TYPE_FLOAT);772nir_builder b =773nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, NULL, "meta_itoi_r32g32b32_cs");774b.shader->info.workgroup_size[0] = 8;775b.shader->info.workgroup_size[1] = 8;776b.shader->info.workgroup_size[2] = 1;777nir_variable *input_img = nir_variable_create(b.shader, nir_var_uniform, type, "input_img");778input_img->data.descriptor_set = 0;779input_img->data.binding = 0;780781nir_variable *output_img =782nir_variable_create(b.shader, nir_var_uniform, img_type, "output_img");783output_img->data.descriptor_set = 0;784output_img->data.binding = 1;785786nir_ssa_def *invoc_id = nir_load_local_invocation_id(&b);787nir_ssa_def *wg_id = nir_load_workgroup_id(&b, 32);788nir_ssa_def *block_size =789nir_imm_ivec4(&b, b.shader->info.workgroup_size[0], b.shader->info.workgroup_size[1],790b.shader->info.workgroup_size[2], 0);791792nir_ssa_def *global_id = nir_iadd(&b, nir_imul(&b, wg_id, block_size), invoc_id);793794nir_ssa_def *src_offset = nir_load_push_constant(&b, 3, 32, nir_imm_int(&b, 0), .range = 24);795nir_ssa_def *dst_offset = nir_load_push_constant(&b, 3, 32, nir_imm_int(&b, 12), .range = 24);796797nir_ssa_def *src_stride = nir_channel(&b, src_offset, 2);798nir_ssa_def *dst_stride = nir_channel(&b, dst_offset, 2);799800nir_ssa_def *src_img_coord = nir_iadd(&b, global_id, src_offset);801nir_ssa_def *dst_img_coord = nir_iadd(&b, global_id, dst_offset);802803nir_ssa_def *src_global_pos =804nir_iadd(&b, nir_imul(&b, nir_channel(&b, src_img_coord, 1), src_stride),805nir_imul(&b, nir_channel(&b, src_img_coord, 0), nir_imm_int(&b, 3)));806807nir_ssa_def *dst_global_pos =808nir_iadd(&b, nir_imul(&b, nir_channel(&b, dst_img_coord, 1), dst_stride),809nir_imul(&b, nir_channel(&b, dst_img_coord, 0), nir_imm_int(&b, 3)));810811for (int chan = 0; chan < 3; chan++) {812/* src */813nir_ssa_def *src_local_pos = nir_iadd(&b, src_global_pos, nir_imm_int(&b, chan));814815nir_ssa_def *src_coord =816nir_vec4(&b, src_local_pos, src_local_pos, src_local_pos, src_local_pos);817818nir_ssa_def *input_img_deref = &nir_build_deref_var(&b, input_img)->dest.ssa;819820nir_tex_instr *tex = nir_tex_instr_create(b.shader, 3);821tex->sampler_dim = GLSL_SAMPLER_DIM_BUF;822tex->op = nir_texop_txf;823tex->src[0].src_type = nir_tex_src_coord;824tex->src[0].src = nir_src_for_ssa(nir_channels(&b, src_coord, 1));825tex->src[1].src_type = nir_tex_src_lod;826tex->src[1].src = nir_src_for_ssa(nir_imm_int(&b, 0));827tex->src[2].src_type = nir_tex_src_texture_deref;828tex->src[2].src = nir_src_for_ssa(input_img_deref);829tex->dest_type = nir_type_float32;830tex->is_array = false;831tex->coord_components = 1;832nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, "tex");833nir_builder_instr_insert(&b, &tex->instr);834835nir_ssa_def *outval = &tex->dest.ssa;836837/* dst */838nir_ssa_def *dst_local_pos = nir_iadd(&b, dst_global_pos, nir_imm_int(&b, chan));839840nir_ssa_def *dst_coord =841nir_vec4(&b, dst_local_pos, dst_local_pos, dst_local_pos, dst_local_pos);842843nir_image_deref_store(&b, &nir_build_deref_var(&b, output_img)->dest.ssa, dst_coord,844nir_ssa_undef(&b, 1, 32), nir_channel(&b, outval, 0),845nir_imm_int(&b, 0));846}847848return b.shader;849}850851/* Image to image - special path for R32G32B32 */852static VkResult853radv_device_init_meta_itoi_r32g32b32_state(struct radv_device *device)854{855VkResult result;856nir_shader *cs = build_nir_itoi_r32g32b32_compute_shader(device);857858VkDescriptorSetLayoutCreateInfo ds_create_info = {859.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,860.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR,861.bindingCount = 2,862.pBindings = (VkDescriptorSetLayoutBinding[]){863{.binding = 0,864.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER,865.descriptorCount = 1,866.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,867.pImmutableSamplers = NULL},868{.binding = 1,869.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER,870.descriptorCount = 1,871.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,872.pImmutableSamplers = NULL},873}};874875result = radv_CreateDescriptorSetLayout(radv_device_to_handle(device), &ds_create_info,876&device->meta_state.alloc,877&device->meta_state.itoi_r32g32b32.img_ds_layout);878if (result != VK_SUCCESS)879goto fail;880881VkPipelineLayoutCreateInfo pl_create_info = {882.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,883.setLayoutCount = 1,884.pSetLayouts = &device->meta_state.itoi_r32g32b32.img_ds_layout,885.pushConstantRangeCount = 1,886.pPushConstantRanges = &(VkPushConstantRange){VK_SHADER_STAGE_COMPUTE_BIT, 0, 24},887};888889result = radv_CreatePipelineLayout(radv_device_to_handle(device), &pl_create_info,890&device->meta_state.alloc,891&device->meta_state.itoi_r32g32b32.img_p_layout);892if (result != VK_SUCCESS)893goto fail;894895/* compute shader */896897VkPipelineShaderStageCreateInfo pipeline_shader_stage = {898.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,899.stage = VK_SHADER_STAGE_COMPUTE_BIT,900.module = vk_shader_module_handle_from_nir(cs),901.pName = "main",902.pSpecializationInfo = NULL,903};904905VkComputePipelineCreateInfo vk_pipeline_info = {906.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,907.stage = pipeline_shader_stage,908.flags = 0,909.layout = device->meta_state.itoi_r32g32b32.img_p_layout,910};911912result = radv_CreateComputePipelines(913radv_device_to_handle(device), radv_pipeline_cache_to_handle(&device->meta_state.cache), 1,914&vk_pipeline_info, NULL, &device->meta_state.itoi_r32g32b32.pipeline);915916fail:917ralloc_free(cs);918return result;919}920921static void922radv_device_finish_meta_itoi_r32g32b32_state(struct radv_device *device)923{924struct radv_meta_state *state = &device->meta_state;925926radv_DestroyPipelineLayout(radv_device_to_handle(device), state->itoi_r32g32b32.img_p_layout,927&state->alloc);928radv_DestroyDescriptorSetLayout(radv_device_to_handle(device),929state->itoi_r32g32b32.img_ds_layout, &state->alloc);930radv_DestroyPipeline(radv_device_to_handle(device), state->itoi_r32g32b32.pipeline,931&state->alloc);932}933934static nir_shader *935build_nir_cleari_compute_shader(struct radv_device *dev, bool is_3d, int samples)936{937bool is_multisampled = samples > 1;938enum glsl_sampler_dim dim = is_3d ? GLSL_SAMPLER_DIM_3D939: is_multisampled ? GLSL_SAMPLER_DIM_MS940: GLSL_SAMPLER_DIM_2D;941const struct glsl_type *img_type = glsl_image_type(dim, false, GLSL_TYPE_FLOAT);942nir_builder b = nir_builder_init_simple_shader(943MESA_SHADER_COMPUTE, NULL, is_3d ? "meta_cleari_cs_3d-%d" : "meta_cleari_cs-%d", samples);944b.shader->info.workgroup_size[0] = 8;945b.shader->info.workgroup_size[1] = 8;946b.shader->info.workgroup_size[2] = 1;947948nir_variable *output_img = nir_variable_create(b.shader, nir_var_uniform, img_type, "out_img");949output_img->data.descriptor_set = 0;950output_img->data.binding = 0;951952nir_ssa_def *invoc_id = nir_load_local_invocation_id(&b);953nir_ssa_def *wg_id = nir_load_workgroup_id(&b, 32);954nir_ssa_def *block_size =955nir_imm_ivec4(&b, b.shader->info.workgroup_size[0], b.shader->info.workgroup_size[1],956b.shader->info.workgroup_size[2], 0);957958nir_ssa_def *global_id = nir_iadd(&b, nir_imul(&b, wg_id, block_size), invoc_id);959960nir_ssa_def *clear_val = nir_load_push_constant(&b, 4, 32, nir_imm_int(&b, 0), .range = 20);961nir_ssa_def *layer = nir_load_push_constant(&b, 1, 32, nir_imm_int(&b, 16), .range = 20);962963nir_ssa_def *global_z = nir_iadd(&b, nir_channel(&b, global_id, 2), layer);964965nir_ssa_def *comps[4];966comps[0] = nir_channel(&b, global_id, 0);967comps[1] = nir_channel(&b, global_id, 1);968comps[2] = global_z;969comps[3] = nir_imm_int(&b, 0);970global_id = nir_vec(&b, comps, 4);971972for (uint32_t i = 0; i < samples; i++) {973nir_image_deref_store(&b, &nir_build_deref_var(&b, output_img)->dest.ssa, global_id,974nir_imm_int(&b, i), clear_val, nir_imm_int(&b, 0));975}976977return b.shader;978}979980static VkResult981create_cleari_pipeline(struct radv_device *device, int samples, VkPipeline *pipeline)982{983nir_shader *cs = build_nir_cleari_compute_shader(device, false, samples);984VkResult result;985986VkPipelineShaderStageCreateInfo pipeline_shader_stage = {987.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,988.stage = VK_SHADER_STAGE_COMPUTE_BIT,989.module = vk_shader_module_handle_from_nir(cs),990.pName = "main",991.pSpecializationInfo = NULL,992};993994VkComputePipelineCreateInfo vk_pipeline_info = {995.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,996.stage = pipeline_shader_stage,997.flags = 0,998.layout = device->meta_state.cleari.img_p_layout,999};10001001result = radv_CreateComputePipelines(radv_device_to_handle(device),1002radv_pipeline_cache_to_handle(&device->meta_state.cache), 1,1003&vk_pipeline_info, NULL, pipeline);1004ralloc_free(cs);1005return result;1006}10071008static VkResult1009radv_device_init_meta_cleari_state(struct radv_device *device)1010{1011VkResult result;10121013/*1014* two descriptors one for the image being sampled1015* one for the buffer being written.1016*/1017VkDescriptorSetLayoutCreateInfo ds_create_info = {1018.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,1019.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR,1020.bindingCount = 1,1021.pBindings = (VkDescriptorSetLayoutBinding[]){1022{.binding = 0,1023.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,1024.descriptorCount = 1,1025.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,1026.pImmutableSamplers = NULL},1027}};10281029result = radv_CreateDescriptorSetLayout(radv_device_to_handle(device), &ds_create_info,1030&device->meta_state.alloc,1031&device->meta_state.cleari.img_ds_layout);1032if (result != VK_SUCCESS)1033goto fail;10341035VkPipelineLayoutCreateInfo pl_create_info = {1036.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,1037.setLayoutCount = 1,1038.pSetLayouts = &device->meta_state.cleari.img_ds_layout,1039.pushConstantRangeCount = 1,1040.pPushConstantRanges = &(VkPushConstantRange){VK_SHADER_STAGE_COMPUTE_BIT, 0, 20},1041};10421043result =1044radv_CreatePipelineLayout(radv_device_to_handle(device), &pl_create_info,1045&device->meta_state.alloc, &device->meta_state.cleari.img_p_layout);1046if (result != VK_SUCCESS)1047goto fail;10481049for (uint32_t i = 0; i < MAX_SAMPLES_LOG2; i++) {1050uint32_t samples = 1 << i;1051result = create_cleari_pipeline(device, samples, &device->meta_state.cleari.pipeline[i]);1052if (result != VK_SUCCESS)1053goto fail;1054}10551056if (device->physical_device->rad_info.chip_class >= GFX9) {1057nir_shader *cs_3d = build_nir_cleari_compute_shader(device, true, 1);10581059/* compute shader */1060VkPipelineShaderStageCreateInfo pipeline_shader_stage_3d = {1061.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,1062.stage = VK_SHADER_STAGE_COMPUTE_BIT,1063.module = vk_shader_module_handle_from_nir(cs_3d),1064.pName = "main",1065.pSpecializationInfo = NULL,1066};10671068VkComputePipelineCreateInfo vk_pipeline_info_3d = {1069.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,1070.stage = pipeline_shader_stage_3d,1071.flags = 0,1072.layout = device->meta_state.cleari.img_p_layout,1073};10741075result = radv_CreateComputePipelines(1076radv_device_to_handle(device), radv_pipeline_cache_to_handle(&device->meta_state.cache), 1,1077&vk_pipeline_info_3d, NULL, &device->meta_state.cleari.pipeline_3d);1078ralloc_free(cs_3d);1079}10801081return VK_SUCCESS;1082fail:1083return result;1084}10851086static void1087radv_device_finish_meta_cleari_state(struct radv_device *device)1088{1089struct radv_meta_state *state = &device->meta_state;10901091radv_DestroyPipelineLayout(radv_device_to_handle(device), state->cleari.img_p_layout,1092&state->alloc);1093radv_DestroyDescriptorSetLayout(radv_device_to_handle(device), state->cleari.img_ds_layout,1094&state->alloc);10951096for (uint32_t i = 0; i < MAX_SAMPLES_LOG2; ++i) {1097radv_DestroyPipeline(radv_device_to_handle(device), state->cleari.pipeline[i], &state->alloc);1098}10991100radv_DestroyPipeline(radv_device_to_handle(device), state->cleari.pipeline_3d, &state->alloc);1101}11021103/* Special path for clearing R32G32B32 images using a compute shader. */1104static nir_shader *1105build_nir_cleari_r32g32b32_compute_shader(struct radv_device *dev)1106{1107const struct glsl_type *img_type = glsl_image_type(GLSL_SAMPLER_DIM_BUF, false, GLSL_TYPE_FLOAT);1108nir_builder b =1109nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, NULL, "meta_cleari_r32g32b32_cs");1110b.shader->info.workgroup_size[0] = 8;1111b.shader->info.workgroup_size[1] = 8;1112b.shader->info.workgroup_size[2] = 1;11131114nir_variable *output_img = nir_variable_create(b.shader, nir_var_uniform, img_type, "out_img");1115output_img->data.descriptor_set = 0;1116output_img->data.binding = 0;11171118nir_ssa_def *invoc_id = nir_load_local_invocation_id(&b);1119nir_ssa_def *wg_id = nir_load_workgroup_id(&b, 32);1120nir_ssa_def *block_size =1121nir_imm_ivec4(&b, b.shader->info.workgroup_size[0], b.shader->info.workgroup_size[1],1122b.shader->info.workgroup_size[2], 0);11231124nir_ssa_def *global_id = nir_iadd(&b, nir_imul(&b, wg_id, block_size), invoc_id);11251126nir_ssa_def *clear_val = nir_load_push_constant(&b, 3, 32, nir_imm_int(&b, 0), .range = 16);1127nir_ssa_def *stride = nir_load_push_constant(&b, 1, 32, nir_imm_int(&b, 12), .range = 16);11281129nir_ssa_def *global_x = nir_channel(&b, global_id, 0);1130nir_ssa_def *global_y = nir_channel(&b, global_id, 1);11311132nir_ssa_def *global_pos =1133nir_iadd(&b, nir_imul(&b, global_y, stride), nir_imul(&b, global_x, nir_imm_int(&b, 3)));11341135for (unsigned chan = 0; chan < 3; chan++) {1136nir_ssa_def *local_pos = nir_iadd(&b, global_pos, nir_imm_int(&b, chan));11371138nir_ssa_def *coord = nir_vec4(&b, local_pos, local_pos, local_pos, local_pos);11391140nir_image_deref_store(&b, &nir_build_deref_var(&b, output_img)->dest.ssa, coord,1141nir_ssa_undef(&b, 1, 32), nir_channel(&b, clear_val, chan),1142nir_imm_int(&b, 0));1143}11441145return b.shader;1146}11471148static VkResult1149radv_device_init_meta_cleari_r32g32b32_state(struct radv_device *device)1150{1151VkResult result;1152nir_shader *cs = build_nir_cleari_r32g32b32_compute_shader(device);11531154VkDescriptorSetLayoutCreateInfo ds_create_info = {1155.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,1156.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR,1157.bindingCount = 1,1158.pBindings = (VkDescriptorSetLayoutBinding[]){1159{.binding = 0,1160.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER,1161.descriptorCount = 1,1162.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,1163.pImmutableSamplers = NULL},1164}};11651166result = radv_CreateDescriptorSetLayout(radv_device_to_handle(device), &ds_create_info,1167&device->meta_state.alloc,1168&device->meta_state.cleari_r32g32b32.img_ds_layout);1169if (result != VK_SUCCESS)1170goto fail;11711172VkPipelineLayoutCreateInfo pl_create_info = {1173.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,1174.setLayoutCount = 1,1175.pSetLayouts = &device->meta_state.cleari_r32g32b32.img_ds_layout,1176.pushConstantRangeCount = 1,1177.pPushConstantRanges = &(VkPushConstantRange){VK_SHADER_STAGE_COMPUTE_BIT, 0, 16},1178};11791180result = radv_CreatePipelineLayout(radv_device_to_handle(device), &pl_create_info,1181&device->meta_state.alloc,1182&device->meta_state.cleari_r32g32b32.img_p_layout);1183if (result != VK_SUCCESS)1184goto fail;11851186/* compute shader */1187VkPipelineShaderStageCreateInfo pipeline_shader_stage = {1188.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,1189.stage = VK_SHADER_STAGE_COMPUTE_BIT,1190.module = vk_shader_module_handle_from_nir(cs),1191.pName = "main",1192.pSpecializationInfo = NULL,1193};11941195VkComputePipelineCreateInfo vk_pipeline_info = {1196.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,1197.stage = pipeline_shader_stage,1198.flags = 0,1199.layout = device->meta_state.cleari_r32g32b32.img_p_layout,1200};12011202result = radv_CreateComputePipelines(1203radv_device_to_handle(device), radv_pipeline_cache_to_handle(&device->meta_state.cache), 1,1204&vk_pipeline_info, NULL, &device->meta_state.cleari_r32g32b32.pipeline);12051206fail:1207ralloc_free(cs);1208return result;1209}12101211static void1212radv_device_finish_meta_cleari_r32g32b32_state(struct radv_device *device)1213{1214struct radv_meta_state *state = &device->meta_state;12151216radv_DestroyPipelineLayout(radv_device_to_handle(device), state->cleari_r32g32b32.img_p_layout,1217&state->alloc);1218radv_DestroyDescriptorSetLayout(radv_device_to_handle(device),1219state->cleari_r32g32b32.img_ds_layout, &state->alloc);1220radv_DestroyPipeline(radv_device_to_handle(device), state->cleari_r32g32b32.pipeline,1221&state->alloc);1222}12231224void1225radv_device_finish_meta_bufimage_state(struct radv_device *device)1226{1227radv_device_finish_meta_itob_state(device);1228radv_device_finish_meta_btoi_state(device);1229radv_device_finish_meta_btoi_r32g32b32_state(device);1230radv_device_finish_meta_itoi_state(device);1231radv_device_finish_meta_itoi_r32g32b32_state(device);1232radv_device_finish_meta_cleari_state(device);1233radv_device_finish_meta_cleari_r32g32b32_state(device);1234}12351236VkResult1237radv_device_init_meta_bufimage_state(struct radv_device *device)1238{1239VkResult result;12401241result = radv_device_init_meta_itob_state(device);1242if (result != VK_SUCCESS)1243goto fail_itob;12441245result = radv_device_init_meta_btoi_state(device);1246if (result != VK_SUCCESS)1247goto fail_btoi;12481249result = radv_device_init_meta_btoi_r32g32b32_state(device);1250if (result != VK_SUCCESS)1251goto fail_btoi_r32g32b32;12521253result = radv_device_init_meta_itoi_state(device);1254if (result != VK_SUCCESS)1255goto fail_itoi;12561257result = radv_device_init_meta_itoi_r32g32b32_state(device);1258if (result != VK_SUCCESS)1259goto fail_itoi_r32g32b32;12601261result = radv_device_init_meta_cleari_state(device);1262if (result != VK_SUCCESS)1263goto fail_cleari;12641265result = radv_device_init_meta_cleari_r32g32b32_state(device);1266if (result != VK_SUCCESS)1267goto fail_cleari_r32g32b32;12681269return VK_SUCCESS;1270fail_cleari_r32g32b32:1271radv_device_finish_meta_cleari_r32g32b32_state(device);1272fail_cleari:1273radv_device_finish_meta_cleari_state(device);1274fail_itoi_r32g32b32:1275radv_device_finish_meta_itoi_r32g32b32_state(device);1276fail_itoi:1277radv_device_finish_meta_itoi_state(device);1278fail_btoi_r32g32b32:1279radv_device_finish_meta_btoi_r32g32b32_state(device);1280fail_btoi:1281radv_device_finish_meta_btoi_state(device);1282fail_itob:1283radv_device_finish_meta_itob_state(device);1284return result;1285}12861287static void1288create_iview(struct radv_cmd_buffer *cmd_buffer, struct radv_meta_blit2d_surf *surf,1289struct radv_image_view *iview)1290{1291VkImageViewType view_type = cmd_buffer->device->physical_device->rad_info.chip_class < GFX91292? VK_IMAGE_VIEW_TYPE_2D1293: radv_meta_get_view_type(surf->image);1294radv_image_view_init(iview, cmd_buffer->device,1295&(VkImageViewCreateInfo){1296.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,1297.image = radv_image_to_handle(surf->image),1298.viewType = view_type,1299.format = surf->format,1300.subresourceRange = {.aspectMask = surf->aspect_mask,1301.baseMipLevel = surf->level,1302.levelCount = 1,1303.baseArrayLayer = surf->layer,1304.layerCount = 1},1305},1306&(struct radv_image_view_extra_create_info){1307.disable_compression = surf->disable_compression,1308});1309}13101311static void1312create_bview(struct radv_cmd_buffer *cmd_buffer, struct radv_buffer *buffer, unsigned offset,1313VkFormat format, struct radv_buffer_view *bview)1314{1315radv_buffer_view_init(bview, cmd_buffer->device,1316&(VkBufferViewCreateInfo){1317.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO,1318.flags = 0,1319.buffer = radv_buffer_to_handle(buffer),1320.format = format,1321.offset = offset,1322.range = VK_WHOLE_SIZE,1323});1324}13251326static void1327create_buffer_from_image(struct radv_cmd_buffer *cmd_buffer, struct radv_meta_blit2d_surf *surf,1328VkBufferUsageFlagBits usage, VkBuffer *buffer)1329{1330struct radv_device *device = cmd_buffer->device;1331struct radv_device_memory mem = {.bo = surf->image->bo};13321333radv_CreateBuffer(radv_device_to_handle(device),1334&(VkBufferCreateInfo){1335.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,1336.flags = 0,1337.size = surf->image->size,1338.usage = usage,1339.sharingMode = VK_SHARING_MODE_EXCLUSIVE,1340},1341NULL, buffer);13421343radv_BindBufferMemory2(radv_device_to_handle(device), 1,1344(VkBindBufferMemoryInfo[]){{1345.sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO,1346.buffer = *buffer,1347.memory = radv_device_memory_to_handle(&mem),1348.memoryOffset = surf->image->offset,1349}});1350}13511352static void1353create_bview_for_r32g32b32(struct radv_cmd_buffer *cmd_buffer, struct radv_buffer *buffer,1354unsigned offset, VkFormat src_format, struct radv_buffer_view *bview)1355{1356VkFormat format;13571358switch (src_format) {1359case VK_FORMAT_R32G32B32_UINT:1360format = VK_FORMAT_R32_UINT;1361break;1362case VK_FORMAT_R32G32B32_SINT:1363format = VK_FORMAT_R32_SINT;1364break;1365case VK_FORMAT_R32G32B32_SFLOAT:1366format = VK_FORMAT_R32_SFLOAT;1367break;1368default:1369unreachable("invalid R32G32B32 format");1370}13711372radv_buffer_view_init(bview, cmd_buffer->device,1373&(VkBufferViewCreateInfo){1374.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO,1375.flags = 0,1376.buffer = radv_buffer_to_handle(buffer),1377.format = format,1378.offset = offset,1379.range = VK_WHOLE_SIZE,1380});1381}13821383static unsigned1384get_image_stride_for_r32g32b32(struct radv_cmd_buffer *cmd_buffer,1385struct radv_meta_blit2d_surf *surf)1386{1387unsigned stride;13881389if (cmd_buffer->device->physical_device->rad_info.chip_class >= GFX9) {1390stride = surf->image->planes[0].surface.u.gfx9.surf_pitch;1391} else {1392stride = surf->image->planes[0].surface.u.legacy.level[0].nblk_x * 3;1393}13941395return stride;1396}13971398static void1399itob_bind_descriptors(struct radv_cmd_buffer *cmd_buffer, struct radv_image_view *src,1400struct radv_buffer_view *dst)1401{1402struct radv_device *device = cmd_buffer->device;14031404radv_meta_push_descriptor_set(1405cmd_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, device->meta_state.itob.img_p_layout, 0, /* set */14062, /* descriptorWriteCount */1407(VkWriteDescriptorSet[]){1408{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,1409.dstBinding = 0,1410.dstArrayElement = 0,1411.descriptorCount = 1,1412.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,1413.pImageInfo =1414(VkDescriptorImageInfo[]){1415{1416.sampler = VK_NULL_HANDLE,1417.imageView = radv_image_view_to_handle(src),1418.imageLayout = VK_IMAGE_LAYOUT_GENERAL,1419},1420}},1421{1422.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,1423.dstBinding = 1,1424.dstArrayElement = 0,1425.descriptorCount = 1,1426.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER,1427.pTexelBufferView = (VkBufferView[]){radv_buffer_view_to_handle(dst)},1428}});1429}14301431void1432radv_meta_image_to_buffer(struct radv_cmd_buffer *cmd_buffer, struct radv_meta_blit2d_surf *src,1433struct radv_meta_blit2d_buffer *dst, unsigned num_rects,1434struct radv_meta_blit2d_rect *rects)1435{1436VkPipeline pipeline = cmd_buffer->device->meta_state.itob.pipeline;1437struct radv_device *device = cmd_buffer->device;1438struct radv_image_view src_view;1439struct radv_buffer_view dst_view;14401441create_iview(cmd_buffer, src, &src_view);1442create_bview(cmd_buffer, dst->buffer, dst->offset, dst->format, &dst_view);1443itob_bind_descriptors(cmd_buffer, &src_view, &dst_view);14441445if (device->physical_device->rad_info.chip_class >= GFX9 && src->image->type == VK_IMAGE_TYPE_3D)1446pipeline = cmd_buffer->device->meta_state.itob.pipeline_3d;14471448radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer), VK_PIPELINE_BIND_POINT_COMPUTE,1449pipeline);14501451for (unsigned r = 0; r < num_rects; ++r) {1452unsigned push_constants[4] = {rects[r].src_x, rects[r].src_y, src->layer, dst->pitch};1453radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer),1454device->meta_state.itob.img_p_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0,145516, push_constants);14561457radv_unaligned_dispatch(cmd_buffer, rects[r].width, rects[r].height, 1);1458}1459}14601461static void1462btoi_r32g32b32_bind_descriptors(struct radv_cmd_buffer *cmd_buffer, struct radv_buffer_view *src,1463struct radv_buffer_view *dst)1464{1465struct radv_device *device = cmd_buffer->device;14661467radv_meta_push_descriptor_set(1468cmd_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, device->meta_state.btoi_r32g32b32.img_p_layout,14690, /* set */14702, /* descriptorWriteCount */1471(VkWriteDescriptorSet[]){1472{1473.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,1474.dstBinding = 0,1475.dstArrayElement = 0,1476.descriptorCount = 1,1477.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER,1478.pTexelBufferView = (VkBufferView[]){radv_buffer_view_to_handle(src)},1479},1480{1481.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,1482.dstBinding = 1,1483.dstArrayElement = 0,1484.descriptorCount = 1,1485.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER,1486.pTexelBufferView = (VkBufferView[]){radv_buffer_view_to_handle(dst)},1487}});1488}14891490static void1491radv_meta_buffer_to_image_cs_r32g32b32(struct radv_cmd_buffer *cmd_buffer,1492struct radv_meta_blit2d_buffer *src,1493struct radv_meta_blit2d_surf *dst, unsigned num_rects,1494struct radv_meta_blit2d_rect *rects)1495{1496VkPipeline pipeline = cmd_buffer->device->meta_state.btoi_r32g32b32.pipeline;1497struct radv_device *device = cmd_buffer->device;1498struct radv_buffer_view src_view, dst_view;1499unsigned dst_offset = 0;1500unsigned stride;1501VkBuffer buffer;15021503/* This special btoi path for R32G32B32 formats will write the linear1504* image as a buffer with the same underlying memory. The compute1505* shader will copy all components separately using a R32 format.1506*/1507create_buffer_from_image(cmd_buffer, dst, VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT, &buffer);15081509create_bview(cmd_buffer, src->buffer, src->offset, src->format, &src_view);1510create_bview_for_r32g32b32(cmd_buffer, radv_buffer_from_handle(buffer), dst_offset, dst->format,1511&dst_view);1512btoi_r32g32b32_bind_descriptors(cmd_buffer, &src_view, &dst_view);15131514radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer), VK_PIPELINE_BIND_POINT_COMPUTE,1515pipeline);15161517stride = get_image_stride_for_r32g32b32(cmd_buffer, dst);15181519for (unsigned r = 0; r < num_rects; ++r) {1520unsigned push_constants[4] = {1521rects[r].dst_x,1522rects[r].dst_y,1523stride,1524src->pitch,1525};15261527radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer),1528device->meta_state.btoi_r32g32b32.img_p_layout,1529VK_SHADER_STAGE_COMPUTE_BIT, 0, 16, push_constants);15301531radv_unaligned_dispatch(cmd_buffer, rects[r].width, rects[r].height, 1);1532}15331534radv_DestroyBuffer(radv_device_to_handle(device), buffer, NULL);1535}15361537static void1538btoi_bind_descriptors(struct radv_cmd_buffer *cmd_buffer, struct radv_buffer_view *src,1539struct radv_image_view *dst)1540{1541struct radv_device *device = cmd_buffer->device;15421543radv_meta_push_descriptor_set(1544cmd_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, device->meta_state.btoi.img_p_layout, 0, /* set */15452, /* descriptorWriteCount */1546(VkWriteDescriptorSet[]){1547{1548.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,1549.dstBinding = 0,1550.dstArrayElement = 0,1551.descriptorCount = 1,1552.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER,1553.pTexelBufferView = (VkBufferView[]){radv_buffer_view_to_handle(src)},1554},1555{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,1556.dstBinding = 1,1557.dstArrayElement = 0,1558.descriptorCount = 1,1559.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,1560.pImageInfo = (VkDescriptorImageInfo[]){1561{1562.sampler = VK_NULL_HANDLE,1563.imageView = radv_image_view_to_handle(dst),1564.imageLayout = VK_IMAGE_LAYOUT_GENERAL,1565},1566}}});1567}15681569void1570radv_meta_buffer_to_image_cs(struct radv_cmd_buffer *cmd_buffer,1571struct radv_meta_blit2d_buffer *src, struct radv_meta_blit2d_surf *dst,1572unsigned num_rects, struct radv_meta_blit2d_rect *rects)1573{1574VkPipeline pipeline = cmd_buffer->device->meta_state.btoi.pipeline;1575struct radv_device *device = cmd_buffer->device;1576struct radv_buffer_view src_view;1577struct radv_image_view dst_view;15781579if (dst->image->vk_format == VK_FORMAT_R32G32B32_UINT ||1580dst->image->vk_format == VK_FORMAT_R32G32B32_SINT ||1581dst->image->vk_format == VK_FORMAT_R32G32B32_SFLOAT) {1582radv_meta_buffer_to_image_cs_r32g32b32(cmd_buffer, src, dst, num_rects, rects);1583return;1584}15851586create_bview(cmd_buffer, src->buffer, src->offset, src->format, &src_view);1587create_iview(cmd_buffer, dst, &dst_view);1588btoi_bind_descriptors(cmd_buffer, &src_view, &dst_view);15891590if (device->physical_device->rad_info.chip_class >= GFX9 && dst->image->type == VK_IMAGE_TYPE_3D)1591pipeline = cmd_buffer->device->meta_state.btoi.pipeline_3d;1592radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer), VK_PIPELINE_BIND_POINT_COMPUTE,1593pipeline);15941595for (unsigned r = 0; r < num_rects; ++r) {1596unsigned push_constants[4] = {1597rects[r].dst_x,1598rects[r].dst_y,1599dst->layer,1600src->pitch,1601};1602radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer),1603device->meta_state.btoi.img_p_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0,160416, push_constants);16051606radv_unaligned_dispatch(cmd_buffer, rects[r].width, rects[r].height, 1);1607}1608}16091610static void1611itoi_r32g32b32_bind_descriptors(struct radv_cmd_buffer *cmd_buffer, struct radv_buffer_view *src,1612struct radv_buffer_view *dst)1613{1614struct radv_device *device = cmd_buffer->device;16151616radv_meta_push_descriptor_set(1617cmd_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, device->meta_state.itoi_r32g32b32.img_p_layout,16180, /* set */16192, /* descriptorWriteCount */1620(VkWriteDescriptorSet[]){1621{1622.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,1623.dstBinding = 0,1624.dstArrayElement = 0,1625.descriptorCount = 1,1626.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER,1627.pTexelBufferView = (VkBufferView[]){radv_buffer_view_to_handle(src)},1628},1629{1630.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,1631.dstBinding = 1,1632.dstArrayElement = 0,1633.descriptorCount = 1,1634.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER,1635.pTexelBufferView = (VkBufferView[]){radv_buffer_view_to_handle(dst)},1636}});1637}16381639static void1640radv_meta_image_to_image_cs_r32g32b32(struct radv_cmd_buffer *cmd_buffer,1641struct radv_meta_blit2d_surf *src,1642struct radv_meta_blit2d_surf *dst, unsigned num_rects,1643struct radv_meta_blit2d_rect *rects)1644{1645VkPipeline pipeline = cmd_buffer->device->meta_state.itoi_r32g32b32.pipeline;1646struct radv_device *device = cmd_buffer->device;1647struct radv_buffer_view src_view, dst_view;1648unsigned src_offset = 0, dst_offset = 0;1649unsigned src_stride, dst_stride;1650VkBuffer src_buffer, dst_buffer;16511652/* 96-bit formats are only compatible to themselves. */1653assert(dst->format == VK_FORMAT_R32G32B32_UINT || dst->format == VK_FORMAT_R32G32B32_SINT ||1654dst->format == VK_FORMAT_R32G32B32_SFLOAT);16551656/* This special itoi path for R32G32B32 formats will write the linear1657* image as a buffer with the same underlying memory. The compute1658* shader will copy all components separately using a R32 format.1659*/1660create_buffer_from_image(cmd_buffer, src, VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT, &src_buffer);1661create_buffer_from_image(cmd_buffer, dst, VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT, &dst_buffer);16621663create_bview_for_r32g32b32(cmd_buffer, radv_buffer_from_handle(src_buffer), src_offset,1664src->format, &src_view);1665create_bview_for_r32g32b32(cmd_buffer, radv_buffer_from_handle(dst_buffer), dst_offset,1666dst->format, &dst_view);1667itoi_r32g32b32_bind_descriptors(cmd_buffer, &src_view, &dst_view);16681669radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer), VK_PIPELINE_BIND_POINT_COMPUTE,1670pipeline);16711672src_stride = get_image_stride_for_r32g32b32(cmd_buffer, src);1673dst_stride = get_image_stride_for_r32g32b32(cmd_buffer, dst);16741675for (unsigned r = 0; r < num_rects; ++r) {1676unsigned push_constants[6] = {1677rects[r].src_x, rects[r].src_y, src_stride, rects[r].dst_x, rects[r].dst_y, dst_stride,1678};1679radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer),1680device->meta_state.itoi_r32g32b32.img_p_layout,1681VK_SHADER_STAGE_COMPUTE_BIT, 0, 24, push_constants);16821683radv_unaligned_dispatch(cmd_buffer, rects[r].width, rects[r].height, 1);1684}16851686radv_DestroyBuffer(radv_device_to_handle(device), src_buffer, NULL);1687radv_DestroyBuffer(radv_device_to_handle(device), dst_buffer, NULL);1688}16891690static void1691itoi_bind_descriptors(struct radv_cmd_buffer *cmd_buffer, struct radv_image_view *src,1692struct radv_image_view *dst)1693{1694struct radv_device *device = cmd_buffer->device;16951696radv_meta_push_descriptor_set(1697cmd_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, device->meta_state.itoi.img_p_layout, 0, /* set */16982, /* descriptorWriteCount */1699(VkWriteDescriptorSet[]){{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,1700.dstBinding = 0,1701.dstArrayElement = 0,1702.descriptorCount = 1,1703.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,1704.pImageInfo =1705(VkDescriptorImageInfo[]){1706{1707.sampler = VK_NULL_HANDLE,1708.imageView = radv_image_view_to_handle(src),1709.imageLayout = VK_IMAGE_LAYOUT_GENERAL,1710},1711}},1712{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,1713.dstBinding = 1,1714.dstArrayElement = 0,1715.descriptorCount = 1,1716.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,1717.pImageInfo = (VkDescriptorImageInfo[]){1718{1719.sampler = VK_NULL_HANDLE,1720.imageView = radv_image_view_to_handle(dst),1721.imageLayout = VK_IMAGE_LAYOUT_GENERAL,1722},1723}}});1724}17251726void1727radv_meta_image_to_image_cs(struct radv_cmd_buffer *cmd_buffer, struct radv_meta_blit2d_surf *src,1728struct radv_meta_blit2d_surf *dst, unsigned num_rects,1729struct radv_meta_blit2d_rect *rects)1730{1731struct radv_device *device = cmd_buffer->device;1732struct radv_image_view src_view, dst_view;1733uint32_t samples = src->image->info.samples;1734uint32_t samples_log2 = ffs(samples) - 1;17351736if (src->format == VK_FORMAT_R32G32B32_UINT || src->format == VK_FORMAT_R32G32B32_SINT ||1737src->format == VK_FORMAT_R32G32B32_SFLOAT) {1738radv_meta_image_to_image_cs_r32g32b32(cmd_buffer, src, dst, num_rects, rects);1739return;1740}17411742create_iview(cmd_buffer, src, &src_view);1743create_iview(cmd_buffer, dst, &dst_view);17441745itoi_bind_descriptors(cmd_buffer, &src_view, &dst_view);17461747VkPipeline pipeline = cmd_buffer->device->meta_state.itoi.pipeline[samples_log2];1748if (device->physical_device->rad_info.chip_class >= GFX9 &&1749(src->image->type == VK_IMAGE_TYPE_3D || dst->image->type == VK_IMAGE_TYPE_3D))1750pipeline = cmd_buffer->device->meta_state.itoi.pipeline_3d;1751radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer), VK_PIPELINE_BIND_POINT_COMPUTE,1752pipeline);17531754for (unsigned r = 0; r < num_rects; ++r) {1755unsigned push_constants[6] = {1756rects[r].src_x, rects[r].src_y, src->layer, rects[r].dst_x, rects[r].dst_y, dst->layer,1757};1758radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer),1759device->meta_state.itoi.img_p_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0,176024, push_constants);17611762radv_unaligned_dispatch(cmd_buffer, rects[r].width, rects[r].height, 1);1763}1764}17651766static void1767cleari_r32g32b32_bind_descriptors(struct radv_cmd_buffer *cmd_buffer, struct radv_buffer_view *view)1768{1769struct radv_device *device = cmd_buffer->device;17701771radv_meta_push_descriptor_set(1772cmd_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, device->meta_state.cleari_r32g32b32.img_p_layout,17730, /* set */17741, /* descriptorWriteCount */1775(VkWriteDescriptorSet[]){{1776.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,1777.dstBinding = 0,1778.dstArrayElement = 0,1779.descriptorCount = 1,1780.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER,1781.pTexelBufferView = (VkBufferView[]){radv_buffer_view_to_handle(view)},1782}});1783}17841785static void1786radv_meta_clear_image_cs_r32g32b32(struct radv_cmd_buffer *cmd_buffer,1787struct radv_meta_blit2d_surf *dst,1788const VkClearColorValue *clear_color)1789{1790VkPipeline pipeline = cmd_buffer->device->meta_state.cleari_r32g32b32.pipeline;1791struct radv_device *device = cmd_buffer->device;1792struct radv_buffer_view dst_view;1793unsigned stride;1794VkBuffer buffer;17951796/* This special clear path for R32G32B32 formats will write the linear1797* image as a buffer with the same underlying memory. The compute1798* shader will clear all components separately using a R32 format.1799*/1800create_buffer_from_image(cmd_buffer, dst, VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT, &buffer);18011802create_bview_for_r32g32b32(cmd_buffer, radv_buffer_from_handle(buffer), 0, dst->format,1803&dst_view);1804cleari_r32g32b32_bind_descriptors(cmd_buffer, &dst_view);18051806radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer), VK_PIPELINE_BIND_POINT_COMPUTE,1807pipeline);18081809stride = get_image_stride_for_r32g32b32(cmd_buffer, dst);18101811unsigned push_constants[4] = {1812clear_color->uint32[0],1813clear_color->uint32[1],1814clear_color->uint32[2],1815stride,1816};18171818radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer),1819device->meta_state.cleari_r32g32b32.img_p_layout,1820VK_SHADER_STAGE_COMPUTE_BIT, 0, 16, push_constants);18211822radv_unaligned_dispatch(cmd_buffer, dst->image->info.width, dst->image->info.height, 1);18231824radv_DestroyBuffer(radv_device_to_handle(device), buffer, NULL);1825}18261827static void1828cleari_bind_descriptors(struct radv_cmd_buffer *cmd_buffer, struct radv_image_view *dst_iview)1829{1830struct radv_device *device = cmd_buffer->device;18311832radv_meta_push_descriptor_set(cmd_buffer, VK_PIPELINE_BIND_POINT_COMPUTE,1833device->meta_state.cleari.img_p_layout, 0, /* set */18341, /* descriptorWriteCount */1835(VkWriteDescriptorSet[]){1836{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,1837.dstBinding = 0,1838.dstArrayElement = 0,1839.descriptorCount = 1,1840.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,1841.pImageInfo =1842(VkDescriptorImageInfo[]){1843{1844.sampler = VK_NULL_HANDLE,1845.imageView = radv_image_view_to_handle(dst_iview),1846.imageLayout = VK_IMAGE_LAYOUT_GENERAL,1847},1848}},1849});1850}18511852void1853radv_meta_clear_image_cs(struct radv_cmd_buffer *cmd_buffer, struct radv_meta_blit2d_surf *dst,1854const VkClearColorValue *clear_color)1855{1856struct radv_device *device = cmd_buffer->device;1857struct radv_image_view dst_iview;1858uint32_t samples = dst->image->info.samples;1859uint32_t samples_log2 = ffs(samples) - 1;18601861if (dst->format == VK_FORMAT_R32G32B32_UINT || dst->format == VK_FORMAT_R32G32B32_SINT ||1862dst->format == VK_FORMAT_R32G32B32_SFLOAT) {1863radv_meta_clear_image_cs_r32g32b32(cmd_buffer, dst, clear_color);1864return;1865}18661867create_iview(cmd_buffer, dst, &dst_iview);1868cleari_bind_descriptors(cmd_buffer, &dst_iview);18691870VkPipeline pipeline = cmd_buffer->device->meta_state.cleari.pipeline[samples_log2];1871if (device->physical_device->rad_info.chip_class >= GFX9 && dst->image->type == VK_IMAGE_TYPE_3D)1872pipeline = cmd_buffer->device->meta_state.cleari.pipeline_3d;18731874radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer), VK_PIPELINE_BIND_POINT_COMPUTE,1875pipeline);18761877unsigned push_constants[5] = {1878clear_color->uint32[0],1879clear_color->uint32[1],1880clear_color->uint32[2],1881clear_color->uint32[3],1882dst->layer,1883};18841885radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer),1886device->meta_state.cleari.img_p_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, 20,1887push_constants);18881889radv_unaligned_dispatch(cmd_buffer, dst->image->info.width, dst->image->info.height, 1);1890}189118921893