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