1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2019 Valve Corporation 3bf215546Sopenharmony_ci * Copyright © 2018 Red Hat 4bf215546Sopenharmony_ci * 5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 8bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 10bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 11bf215546Sopenharmony_ci * 12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 13bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 14bf215546Sopenharmony_ci * Software. 15bf215546Sopenharmony_ci * 16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22bf215546Sopenharmony_ci * IN THE SOFTWARE. 23bf215546Sopenharmony_ci */ 24bf215546Sopenharmony_ci 25bf215546Sopenharmony_ci#include "radv_meta.h" 26bf215546Sopenharmony_ci#include "radv_private.h" 27bf215546Sopenharmony_ci#include "vk_format.h" 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_cistatic nir_shader * 30bf215546Sopenharmony_cibuild_fmask_expand_compute_shader(struct radv_device *device, int samples) 31bf215546Sopenharmony_ci{ 32bf215546Sopenharmony_ci const struct glsl_type *type = 33bf215546Sopenharmony_ci glsl_sampler_type(GLSL_SAMPLER_DIM_MS, false, true, GLSL_TYPE_FLOAT); 34bf215546Sopenharmony_ci const struct glsl_type *img_type = glsl_image_type(GLSL_SAMPLER_DIM_MS, true, GLSL_TYPE_FLOAT); 35bf215546Sopenharmony_ci 36bf215546Sopenharmony_ci nir_builder b = 37bf215546Sopenharmony_ci radv_meta_init_shader(device, MESA_SHADER_COMPUTE, "meta_fmask_expand_cs-%d", samples); 38bf215546Sopenharmony_ci b.shader->info.workgroup_size[0] = 8; 39bf215546Sopenharmony_ci b.shader->info.workgroup_size[1] = 8; 40bf215546Sopenharmony_ci 41bf215546Sopenharmony_ci nir_variable *input_img = nir_variable_create(b.shader, nir_var_uniform, type, "s_tex"); 42bf215546Sopenharmony_ci input_img->data.descriptor_set = 0; 43bf215546Sopenharmony_ci input_img->data.binding = 0; 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_ci nir_variable *output_img = nir_variable_create(b.shader, nir_var_image, img_type, "out_img"); 46bf215546Sopenharmony_ci output_img->data.descriptor_set = 0; 47bf215546Sopenharmony_ci output_img->data.binding = 1; 48bf215546Sopenharmony_ci output_img->data.access = ACCESS_NON_READABLE; 49bf215546Sopenharmony_ci 50bf215546Sopenharmony_ci nir_ssa_def *input_img_deref = &nir_build_deref_var(&b, input_img)->dest.ssa; 51bf215546Sopenharmony_ci nir_ssa_def *output_img_deref = &nir_build_deref_var(&b, output_img)->dest.ssa; 52bf215546Sopenharmony_ci 53bf215546Sopenharmony_ci nir_ssa_def *tex_coord = get_global_ids(&b, 3); 54bf215546Sopenharmony_ci 55bf215546Sopenharmony_ci nir_tex_instr *tex_instr[8]; 56bf215546Sopenharmony_ci for (uint32_t i = 0; i < samples; i++) { 57bf215546Sopenharmony_ci tex_instr[i] = nir_tex_instr_create(b.shader, 3); 58bf215546Sopenharmony_ci 59bf215546Sopenharmony_ci nir_tex_instr *tex = tex_instr[i]; 60bf215546Sopenharmony_ci tex->sampler_dim = GLSL_SAMPLER_DIM_MS; 61bf215546Sopenharmony_ci tex->op = nir_texop_txf_ms; 62bf215546Sopenharmony_ci tex->src[0].src_type = nir_tex_src_coord; 63bf215546Sopenharmony_ci tex->src[0].src = nir_src_for_ssa(tex_coord); 64bf215546Sopenharmony_ci tex->src[1].src_type = nir_tex_src_ms_index; 65bf215546Sopenharmony_ci tex->src[1].src = nir_src_for_ssa(nir_imm_int(&b, i)); 66bf215546Sopenharmony_ci tex->src[2].src_type = nir_tex_src_texture_deref; 67bf215546Sopenharmony_ci tex->src[2].src = nir_src_for_ssa(input_img_deref); 68bf215546Sopenharmony_ci tex->dest_type = nir_type_float32; 69bf215546Sopenharmony_ci tex->is_array = true; 70bf215546Sopenharmony_ci tex->coord_components = 3; 71bf215546Sopenharmony_ci 72bf215546Sopenharmony_ci nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, "tex"); 73bf215546Sopenharmony_ci nir_builder_instr_insert(&b, &tex->instr); 74bf215546Sopenharmony_ci } 75bf215546Sopenharmony_ci 76bf215546Sopenharmony_ci nir_ssa_def *img_coord = 77bf215546Sopenharmony_ci nir_vec4(&b, nir_channel(&b, tex_coord, 0), nir_channel(&b, tex_coord, 1), 78bf215546Sopenharmony_ci nir_channel(&b, tex_coord, 2), nir_ssa_undef(&b, 1, 32)); 79bf215546Sopenharmony_ci 80bf215546Sopenharmony_ci for (uint32_t i = 0; i < samples; i++) { 81bf215546Sopenharmony_ci nir_ssa_def *outval = &tex_instr[i]->dest.ssa; 82bf215546Sopenharmony_ci 83bf215546Sopenharmony_ci nir_image_deref_store(&b, output_img_deref, img_coord, nir_imm_int(&b, i), outval, 84bf215546Sopenharmony_ci nir_imm_int(&b, 0), .image_dim = GLSL_SAMPLER_DIM_MS, .image_array = true); 85bf215546Sopenharmony_ci } 86bf215546Sopenharmony_ci 87bf215546Sopenharmony_ci return b.shader; 88bf215546Sopenharmony_ci} 89bf215546Sopenharmony_ci 90bf215546Sopenharmony_civoid 91bf215546Sopenharmony_ciradv_expand_fmask_image_inplace(struct radv_cmd_buffer *cmd_buffer, struct radv_image *image, 92bf215546Sopenharmony_ci const VkImageSubresourceRange *subresourceRange) 93bf215546Sopenharmony_ci{ 94bf215546Sopenharmony_ci struct radv_device *device = cmd_buffer->device; 95bf215546Sopenharmony_ci struct radv_meta_saved_state saved_state; 96bf215546Sopenharmony_ci const uint32_t samples = image->info.samples; 97bf215546Sopenharmony_ci const uint32_t samples_log2 = ffs(samples) - 1; 98bf215546Sopenharmony_ci unsigned layer_count = radv_get_layerCount(image, subresourceRange); 99bf215546Sopenharmony_ci struct radv_image_view iview; 100bf215546Sopenharmony_ci 101bf215546Sopenharmony_ci radv_meta_save(&saved_state, cmd_buffer, 102bf215546Sopenharmony_ci RADV_META_SAVE_COMPUTE_PIPELINE | RADV_META_SAVE_DESCRIPTORS); 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_ci VkPipeline pipeline = device->meta_state.fmask_expand.pipeline[samples_log2]; 105bf215546Sopenharmony_ci 106bf215546Sopenharmony_ci radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer), VK_PIPELINE_BIND_POINT_COMPUTE, 107bf215546Sopenharmony_ci pipeline); 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci cmd_buffer->state.flush_bits |= radv_dst_access_flush( 110bf215546Sopenharmony_ci cmd_buffer, VK_ACCESS_2_SHADER_READ_BIT | VK_ACCESS_2_SHADER_WRITE_BIT, image); 111bf215546Sopenharmony_ci 112bf215546Sopenharmony_ci radv_image_view_init(&iview, device, 113bf215546Sopenharmony_ci &(VkImageViewCreateInfo){ 114bf215546Sopenharmony_ci .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, 115bf215546Sopenharmony_ci .image = radv_image_to_handle(image), 116bf215546Sopenharmony_ci .viewType = radv_meta_get_view_type(image), 117bf215546Sopenharmony_ci .format = vk_format_no_srgb(image->vk.format), 118bf215546Sopenharmony_ci .subresourceRange = 119bf215546Sopenharmony_ci { 120bf215546Sopenharmony_ci .aspectMask = subresourceRange->aspectMask, 121bf215546Sopenharmony_ci .baseMipLevel = 0, 122bf215546Sopenharmony_ci .levelCount = 1, 123bf215546Sopenharmony_ci .baseArrayLayer = subresourceRange->baseArrayLayer, 124bf215546Sopenharmony_ci .layerCount = layer_count, 125bf215546Sopenharmony_ci }, 126bf215546Sopenharmony_ci }, 127bf215546Sopenharmony_ci 0, NULL); 128bf215546Sopenharmony_ci 129bf215546Sopenharmony_ci radv_meta_push_descriptor_set( 130bf215546Sopenharmony_ci cmd_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, 131bf215546Sopenharmony_ci cmd_buffer->device->meta_state.fmask_expand.p_layout, 0, /* set */ 132bf215546Sopenharmony_ci 2, /* descriptorWriteCount */ 133bf215546Sopenharmony_ci (VkWriteDescriptorSet[]){{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, 134bf215546Sopenharmony_ci .dstBinding = 0, 135bf215546Sopenharmony_ci .dstArrayElement = 0, 136bf215546Sopenharmony_ci .descriptorCount = 1, 137bf215546Sopenharmony_ci .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 138bf215546Sopenharmony_ci .pImageInfo = 139bf215546Sopenharmony_ci (VkDescriptorImageInfo[]){ 140bf215546Sopenharmony_ci {.sampler = VK_NULL_HANDLE, 141bf215546Sopenharmony_ci .imageView = radv_image_view_to_handle(&iview), 142bf215546Sopenharmony_ci .imageLayout = VK_IMAGE_LAYOUT_GENERAL}, 143bf215546Sopenharmony_ci }}, 144bf215546Sopenharmony_ci {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, 145bf215546Sopenharmony_ci .dstBinding = 1, 146bf215546Sopenharmony_ci .dstArrayElement = 0, 147bf215546Sopenharmony_ci .descriptorCount = 1, 148bf215546Sopenharmony_ci .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 149bf215546Sopenharmony_ci .pImageInfo = (VkDescriptorImageInfo[]){ 150bf215546Sopenharmony_ci {.sampler = VK_NULL_HANDLE, 151bf215546Sopenharmony_ci .imageView = radv_image_view_to_handle(&iview), 152bf215546Sopenharmony_ci .imageLayout = VK_IMAGE_LAYOUT_GENERAL}, 153bf215546Sopenharmony_ci }}}); 154bf215546Sopenharmony_ci 155bf215546Sopenharmony_ci radv_unaligned_dispatch(cmd_buffer, image->info.width, image->info.height, layer_count); 156bf215546Sopenharmony_ci 157bf215546Sopenharmony_ci radv_image_view_finish(&iview); 158bf215546Sopenharmony_ci 159bf215546Sopenharmony_ci radv_meta_restore(&saved_state, cmd_buffer); 160bf215546Sopenharmony_ci 161bf215546Sopenharmony_ci cmd_buffer->state.flush_bits |= 162bf215546Sopenharmony_ci RADV_CMD_FLAG_CS_PARTIAL_FLUSH | 163bf215546Sopenharmony_ci radv_src_access_flush(cmd_buffer, VK_ACCESS_2_SHADER_WRITE_BIT, image); 164bf215546Sopenharmony_ci 165bf215546Sopenharmony_ci /* Re-initialize FMASK in fully expanded mode. */ 166bf215546Sopenharmony_ci cmd_buffer->state.flush_bits |= radv_init_fmask(cmd_buffer, image, subresourceRange); 167bf215546Sopenharmony_ci} 168bf215546Sopenharmony_ci 169bf215546Sopenharmony_civoid 170bf215546Sopenharmony_ciradv_device_finish_meta_fmask_expand_state(struct radv_device *device) 171bf215546Sopenharmony_ci{ 172bf215546Sopenharmony_ci struct radv_meta_state *state = &device->meta_state; 173bf215546Sopenharmony_ci 174bf215546Sopenharmony_ci for (uint32_t i = 0; i < MAX_SAMPLES_LOG2; ++i) { 175bf215546Sopenharmony_ci radv_DestroyPipeline(radv_device_to_handle(device), state->fmask_expand.pipeline[i], 176bf215546Sopenharmony_ci &state->alloc); 177bf215546Sopenharmony_ci } 178bf215546Sopenharmony_ci radv_DestroyPipelineLayout(radv_device_to_handle(device), state->fmask_expand.p_layout, 179bf215546Sopenharmony_ci &state->alloc); 180bf215546Sopenharmony_ci 181bf215546Sopenharmony_ci device->vk.dispatch_table.DestroyDescriptorSetLayout( 182bf215546Sopenharmony_ci radv_device_to_handle(device), state->fmask_expand.ds_layout, &state->alloc); 183bf215546Sopenharmony_ci} 184bf215546Sopenharmony_ci 185bf215546Sopenharmony_cistatic VkResult 186bf215546Sopenharmony_cicreate_fmask_expand_pipeline(struct radv_device *device, int samples, VkPipeline *pipeline) 187bf215546Sopenharmony_ci{ 188bf215546Sopenharmony_ci struct radv_meta_state *state = &device->meta_state; 189bf215546Sopenharmony_ci VkResult result; 190bf215546Sopenharmony_ci nir_shader *cs = build_fmask_expand_compute_shader(device, samples); 191bf215546Sopenharmony_ci ; 192bf215546Sopenharmony_ci 193bf215546Sopenharmony_ci VkPipelineShaderStageCreateInfo pipeline_shader_stage = { 194bf215546Sopenharmony_ci .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, 195bf215546Sopenharmony_ci .stage = VK_SHADER_STAGE_COMPUTE_BIT, 196bf215546Sopenharmony_ci .module = vk_shader_module_handle_from_nir(cs), 197bf215546Sopenharmony_ci .pName = "main", 198bf215546Sopenharmony_ci .pSpecializationInfo = NULL, 199bf215546Sopenharmony_ci }; 200bf215546Sopenharmony_ci 201bf215546Sopenharmony_ci VkComputePipelineCreateInfo vk_pipeline_info = { 202bf215546Sopenharmony_ci .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, 203bf215546Sopenharmony_ci .stage = pipeline_shader_stage, 204bf215546Sopenharmony_ci .flags = 0, 205bf215546Sopenharmony_ci .layout = state->fmask_expand.p_layout, 206bf215546Sopenharmony_ci }; 207bf215546Sopenharmony_ci 208bf215546Sopenharmony_ci result = radv_CreateComputePipelines(radv_device_to_handle(device), 209bf215546Sopenharmony_ci radv_pipeline_cache_to_handle(&state->cache), 1, 210bf215546Sopenharmony_ci &vk_pipeline_info, NULL, pipeline); 211bf215546Sopenharmony_ci 212bf215546Sopenharmony_ci ralloc_free(cs); 213bf215546Sopenharmony_ci return result; 214bf215546Sopenharmony_ci} 215bf215546Sopenharmony_ci 216bf215546Sopenharmony_ciVkResult 217bf215546Sopenharmony_ciradv_device_init_meta_fmask_expand_state(struct radv_device *device) 218bf215546Sopenharmony_ci{ 219bf215546Sopenharmony_ci struct radv_meta_state *state = &device->meta_state; 220bf215546Sopenharmony_ci VkResult result; 221bf215546Sopenharmony_ci 222bf215546Sopenharmony_ci VkDescriptorSetLayoutCreateInfo ds_create_info = { 223bf215546Sopenharmony_ci .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, 224bf215546Sopenharmony_ci .flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR, 225bf215546Sopenharmony_ci .bindingCount = 2, 226bf215546Sopenharmony_ci .pBindings = (VkDescriptorSetLayoutBinding[]){ 227bf215546Sopenharmony_ci {.binding = 0, 228bf215546Sopenharmony_ci .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 229bf215546Sopenharmony_ci .descriptorCount = 1, 230bf215546Sopenharmony_ci .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT, 231bf215546Sopenharmony_ci .pImmutableSamplers = NULL}, 232bf215546Sopenharmony_ci {.binding = 1, 233bf215546Sopenharmony_ci .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 234bf215546Sopenharmony_ci .descriptorCount = 1, 235bf215546Sopenharmony_ci .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT, 236bf215546Sopenharmony_ci .pImmutableSamplers = NULL}, 237bf215546Sopenharmony_ci }}; 238bf215546Sopenharmony_ci 239bf215546Sopenharmony_ci result = radv_CreateDescriptorSetLayout(radv_device_to_handle(device), &ds_create_info, 240bf215546Sopenharmony_ci &state->alloc, &state->fmask_expand.ds_layout); 241bf215546Sopenharmony_ci if (result != VK_SUCCESS) 242bf215546Sopenharmony_ci return result; 243bf215546Sopenharmony_ci 244bf215546Sopenharmony_ci VkPipelineLayoutCreateInfo color_create_info = { 245bf215546Sopenharmony_ci .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, 246bf215546Sopenharmony_ci .setLayoutCount = 1, 247bf215546Sopenharmony_ci .pSetLayouts = &state->fmask_expand.ds_layout, 248bf215546Sopenharmony_ci .pushConstantRangeCount = 0, 249bf215546Sopenharmony_ci .pPushConstantRanges = NULL, 250bf215546Sopenharmony_ci }; 251bf215546Sopenharmony_ci 252bf215546Sopenharmony_ci result = radv_CreatePipelineLayout(radv_device_to_handle(device), &color_create_info, 253bf215546Sopenharmony_ci &state->alloc, &state->fmask_expand.p_layout); 254bf215546Sopenharmony_ci if (result != VK_SUCCESS) 255bf215546Sopenharmony_ci return result; 256bf215546Sopenharmony_ci 257bf215546Sopenharmony_ci for (uint32_t i = 0; i < MAX_SAMPLES_LOG2; i++) { 258bf215546Sopenharmony_ci uint32_t samples = 1 << i; 259bf215546Sopenharmony_ci result = create_fmask_expand_pipeline(device, samples, &state->fmask_expand.pipeline[i]); 260bf215546Sopenharmony_ci if (result != VK_SUCCESS) 261bf215546Sopenharmony_ci return result; 262bf215546Sopenharmony_ci } 263bf215546Sopenharmony_ci 264bf215546Sopenharmony_ci return VK_SUCCESS; 265bf215546Sopenharmony_ci} 266