2 * Copyright © 2016 Dave Airlie
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 #include "radv_meta.h"
29 #include "radv_private.h"
30 #include "nir/nir_builder.h"
32 #include "vk_format.h"
35 build_resolve_compute_shader(struct radv_device
*dev
, bool is_integer
, bool is_srgb
, int samples
)
39 const struct glsl_type
*sampler_type
= glsl_sampler_type(GLSL_SAMPLER_DIM_MS
,
43 const struct glsl_type
*img_type
= glsl_sampler_type(GLSL_SAMPLER_DIM_2D
,
47 snprintf(name
, 64, "meta_resolve_cs-%d-%s", samples
, is_integer
? "int" : (is_srgb
? "srgb" : "float"));
48 nir_builder_init_simple_shader(&b
, NULL
, MESA_SHADER_COMPUTE
, NULL
);
49 b
.shader
->info
->name
= ralloc_strdup(b
.shader
, name
);
50 b
.shader
->info
->cs
.local_size
[0] = 16;
51 b
.shader
->info
->cs
.local_size
[1] = 16;
52 b
.shader
->info
->cs
.local_size
[2] = 1;
54 nir_variable
*input_img
= nir_variable_create(b
.shader
, nir_var_uniform
,
55 sampler_type
, "s_tex");
56 input_img
->data
.descriptor_set
= 0;
57 input_img
->data
.binding
= 0;
59 nir_variable
*output_img
= nir_variable_create(b
.shader
, nir_var_uniform
,
61 output_img
->data
.descriptor_set
= 0;
62 output_img
->data
.binding
= 1;
63 nir_ssa_def
*invoc_id
= nir_load_system_value(&b
, nir_intrinsic_load_local_invocation_id
, 0);
64 nir_ssa_def
*wg_id
= nir_load_system_value(&b
, nir_intrinsic_load_work_group_id
, 0);
65 nir_ssa_def
*block_size
= nir_imm_ivec4(&b
,
66 b
.shader
->info
->cs
.local_size
[0],
67 b
.shader
->info
->cs
.local_size
[1],
68 b
.shader
->info
->cs
.local_size
[2], 0);
70 nir_ssa_def
*global_id
= nir_iadd(&b
, nir_imul(&b
, wg_id
, block_size
), invoc_id
);
72 nir_intrinsic_instr
*src_offset
= nir_intrinsic_instr_create(b
.shader
, nir_intrinsic_load_push_constant
);
73 src_offset
->src
[0] = nir_src_for_ssa(nir_imm_int(&b
, 0));
74 src_offset
->num_components
= 2;
75 nir_ssa_dest_init(&src_offset
->instr
, &src_offset
->dest
, 2, 32, "src_offset");
76 nir_builder_instr_insert(&b
, &src_offset
->instr
);
78 nir_intrinsic_instr
*dst_offset
= nir_intrinsic_instr_create(b
.shader
, nir_intrinsic_load_push_constant
);
79 dst_offset
->src
[0] = nir_src_for_ssa(nir_imm_int(&b
, 8));
80 dst_offset
->num_components
= 2;
81 nir_ssa_dest_init(&dst_offset
->instr
, &dst_offset
->dest
, 2, 32, "dst_offset");
82 nir_builder_instr_insert(&b
, &dst_offset
->instr
);
84 nir_ssa_def
*img_coord
= nir_channels(&b
, nir_iadd(&b
, global_id
, &src_offset
->dest
.ssa
), 0x3);
85 nir_variable
*color
= nir_local_variable_create(b
.impl
, glsl_vec4_type(), "color");
87 radv_meta_build_resolve_shader_core(&b
, is_integer
, is_srgb
, samples
,
88 input_img
, color
, img_coord
);
90 nir_ssa_def
*outval
= nir_load_var(&b
, color
);
91 nir_ssa_def
*coord
= nir_iadd(&b
, global_id
, &dst_offset
->dest
.ssa
);
92 nir_intrinsic_instr
*store
= nir_intrinsic_instr_create(b
.shader
, nir_intrinsic_image_store
);
93 store
->src
[0] = nir_src_for_ssa(coord
);
94 store
->src
[1] = nir_src_for_ssa(nir_ssa_undef(&b
, 1, 32));
95 store
->src
[2] = nir_src_for_ssa(outval
);
96 store
->variables
[0] = nir_deref_var_create(store
, output_img
);
97 nir_builder_instr_insert(&b
, &store
->instr
);
103 create_layout(struct radv_device
*device
)
107 * two descriptors one for the image being sampled
108 * one for the buffer being written.
110 VkDescriptorSetLayoutCreateInfo ds_create_info
= {
111 .sType
= VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
,
112 .flags
= VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
,
114 .pBindings
= (VkDescriptorSetLayoutBinding
[]) {
117 .descriptorType
= VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
,
118 .descriptorCount
= 1,
119 .stageFlags
= VK_SHADER_STAGE_COMPUTE_BIT
,
120 .pImmutableSamplers
= NULL
124 .descriptorType
= VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
,
125 .descriptorCount
= 1,
126 .stageFlags
= VK_SHADER_STAGE_COMPUTE_BIT
,
127 .pImmutableSamplers
= NULL
132 result
= radv_CreateDescriptorSetLayout(radv_device_to_handle(device
),
134 &device
->meta_state
.alloc
,
135 &device
->meta_state
.resolve_compute
.ds_layout
);
136 if (result
!= VK_SUCCESS
)
140 VkPipelineLayoutCreateInfo pl_create_info
= {
141 .sType
= VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
,
143 .pSetLayouts
= &device
->meta_state
.resolve_compute
.ds_layout
,
144 .pushConstantRangeCount
= 1,
145 .pPushConstantRanges
= &(VkPushConstantRange
){VK_SHADER_STAGE_COMPUTE_BIT
, 0, 16},
148 result
= radv_CreatePipelineLayout(radv_device_to_handle(device
),
150 &device
->meta_state
.alloc
,
151 &device
->meta_state
.resolve_compute
.p_layout
);
152 if (result
!= VK_SUCCESS
)
160 create_resolve_pipeline(struct radv_device
*device
,
164 VkPipeline
*pipeline
)
167 struct radv_shader_module cs
= { .nir
= NULL
};
169 cs
.nir
= build_resolve_compute_shader(device
, is_integer
, is_srgb
, samples
);
173 VkPipelineShaderStageCreateInfo pipeline_shader_stage
= {
174 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
175 .stage
= VK_SHADER_STAGE_COMPUTE_BIT
,
176 .module
= radv_shader_module_to_handle(&cs
),
178 .pSpecializationInfo
= NULL
,
181 VkComputePipelineCreateInfo vk_pipeline_info
= {
182 .sType
= VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO
,
183 .stage
= pipeline_shader_stage
,
185 .layout
= device
->meta_state
.resolve_compute
.p_layout
,
188 result
= radv_CreateComputePipelines(radv_device_to_handle(device
),
189 radv_pipeline_cache_to_handle(&device
->meta_state
.cache
),
190 1, &vk_pipeline_info
, NULL
,
192 if (result
!= VK_SUCCESS
)
203 radv_device_init_meta_resolve_compute_state(struct radv_device
*device
)
205 struct radv_meta_state
*state
= &device
->meta_state
;
207 memset(&device
->meta_state
.resolve_compute
, 0, sizeof(device
->meta_state
.resolve_compute
));
209 res
= create_layout(device
);
210 if (res
!= VK_SUCCESS
)
213 for (uint32_t i
= 0; i
< MAX_SAMPLES_LOG2
; ++i
) {
214 uint32_t samples
= 1 << i
;
216 res
= create_resolve_pipeline(device
, samples
, false, false,
217 &state
->resolve_compute
.rc
[i
].pipeline
);
219 res
= create_resolve_pipeline(device
, samples
, true, false,
220 &state
->resolve_compute
.rc
[i
].i_pipeline
);
222 res
= create_resolve_pipeline(device
, samples
, false, true,
223 &state
->resolve_compute
.rc
[i
].srgb_pipeline
);
231 radv_device_finish_meta_resolve_compute_state(struct radv_device
*device
)
233 struct radv_meta_state
*state
= &device
->meta_state
;
234 for (uint32_t i
= 0; i
< MAX_SAMPLES_LOG2
; ++i
) {
235 radv_DestroyPipeline(radv_device_to_handle(device
),
236 state
->resolve_compute
.rc
[i
].pipeline
,
239 radv_DestroyPipeline(radv_device_to_handle(device
),
240 state
->resolve_compute
.rc
[i
].i_pipeline
,
243 radv_DestroyPipeline(radv_device_to_handle(device
),
244 state
->resolve_compute
.rc
[i
].srgb_pipeline
,
248 radv_DestroyDescriptorSetLayout(radv_device_to_handle(device
),
249 state
->resolve_compute
.ds_layout
,
251 radv_DestroyPipelineLayout(radv_device_to_handle(device
),
252 state
->resolve_compute
.p_layout
,
257 emit_resolve(struct radv_cmd_buffer
*cmd_buffer
,
258 struct radv_image_view
*src_iview
,
259 struct radv_image_view
*dest_iview
,
260 const VkOffset2D
*src_offset
,
261 const VkOffset2D
*dest_offset
,
262 const VkExtent2D
*resolve_extent
)
264 struct radv_device
*device
= cmd_buffer
->device
;
265 const uint32_t samples
= src_iview
->image
->info
.samples
;
266 const uint32_t samples_log2
= ffs(samples
) - 1;
267 radv_meta_push_descriptor_set(cmd_buffer
,
268 VK_PIPELINE_BIND_POINT_COMPUTE
,
269 device
->meta_state
.resolve_compute
.p_layout
,
271 2, /* descriptorWriteCount */
272 (VkWriteDescriptorSet
[]) {
274 .sType
= VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET
,
276 .dstArrayElement
= 0,
277 .descriptorCount
= 1,
278 .descriptorType
= VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
,
279 .pImageInfo
= (VkDescriptorImageInfo
[]) {
281 .sampler
= VK_NULL_HANDLE
,
282 .imageView
= radv_image_view_to_handle(src_iview
),
283 .imageLayout
= VK_IMAGE_LAYOUT_GENERAL
},
287 .sType
= VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET
,
289 .dstArrayElement
= 0,
290 .descriptorCount
= 1,
291 .descriptorType
= VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
,
292 .pImageInfo
= (VkDescriptorImageInfo
[]) {
294 .sampler
= VK_NULL_HANDLE
,
295 .imageView
= radv_image_view_to_handle(dest_iview
),
296 .imageLayout
= VK_IMAGE_LAYOUT_GENERAL
,
303 if (vk_format_is_int(src_iview
->image
->vk_format
))
304 pipeline
= device
->meta_state
.resolve_compute
.rc
[samples_log2
].i_pipeline
;
305 else if (vk_format_is_srgb(src_iview
->image
->vk_format
))
306 pipeline
= device
->meta_state
.resolve_compute
.rc
[samples_log2
].srgb_pipeline
;
308 pipeline
= device
->meta_state
.resolve_compute
.rc
[samples_log2
].pipeline
;
309 if (cmd_buffer
->state
.compute_pipeline
!= radv_pipeline_from_handle(pipeline
)) {
310 radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer
),
311 VK_PIPELINE_BIND_POINT_COMPUTE
, pipeline
);
314 unsigned push_constants
[4] = {
320 radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer
),
321 device
->meta_state
.resolve_compute
.p_layout
,
322 VK_SHADER_STAGE_COMPUTE_BIT
, 0, 16,
324 radv_unaligned_dispatch(cmd_buffer
, resolve_extent
->width
, resolve_extent
->height
, 1);
328 void radv_meta_resolve_compute_image(struct radv_cmd_buffer
*cmd_buffer
,
329 struct radv_image
*src_image
,
330 VkImageLayout src_image_layout
,
331 struct radv_image
*dest_image
,
332 VkImageLayout dest_image_layout
,
333 uint32_t region_count
,
334 const VkImageResolve
*regions
)
336 struct radv_meta_saved_compute_state saved_state
;
338 for (uint32_t r
= 0; r
< region_count
; ++r
) {
339 const VkImageResolve
*region
= ®ions
[r
];
340 const uint32_t src_base_layer
=
341 radv_meta_get_iview_layer(src_image
, ®ion
->srcSubresource
,
343 VkImageSubresourceRange range
;
344 range
.aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
;
345 range
.baseMipLevel
= region
->srcSubresource
.mipLevel
;
346 range
.levelCount
= 1;
347 range
.baseArrayLayer
= src_base_layer
;
348 range
.layerCount
= region
->srcSubresource
.layerCount
;
349 radv_fast_clear_flush_image_inplace(cmd_buffer
, src_image
, &range
);
352 radv_meta_save_compute(&saved_state
, cmd_buffer
, 16);
354 for (uint32_t r
= 0; r
< region_count
; ++r
) {
355 const VkImageResolve
*region
= ®ions
[r
];
357 assert(region
->srcSubresource
.aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
358 assert(region
->dstSubresource
.aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
359 assert(region
->srcSubresource
.layerCount
== region
->dstSubresource
.layerCount
);
361 const uint32_t src_base_layer
=
362 radv_meta_get_iview_layer(src_image
, ®ion
->srcSubresource
,
365 const uint32_t dest_base_layer
=
366 radv_meta_get_iview_layer(dest_image
, ®ion
->dstSubresource
,
369 const struct VkExtent3D extent
=
370 radv_sanitize_image_extent(src_image
->type
, region
->extent
);
371 const struct VkOffset3D srcOffset
=
372 radv_sanitize_image_offset(src_image
->type
, region
->srcOffset
);
373 const struct VkOffset3D dstOffset
=
374 radv_sanitize_image_offset(dest_image
->type
, region
->dstOffset
);
376 for (uint32_t layer
= 0; layer
< region
->srcSubresource
.layerCount
;
379 struct radv_image_view src_iview
;
380 radv_image_view_init(&src_iview
, cmd_buffer
->device
,
381 &(VkImageViewCreateInfo
) {
382 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
383 .image
= radv_image_to_handle(src_image
),
384 .viewType
= radv_meta_get_view_type(src_image
),
385 .format
= src_image
->vk_format
,
386 .subresourceRange
= {
387 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
388 .baseMipLevel
= region
->srcSubresource
.mipLevel
,
390 .baseArrayLayer
= src_base_layer
+ layer
,
394 cmd_buffer
, VK_IMAGE_USAGE_SAMPLED_BIT
);
396 struct radv_image_view dest_iview
;
397 radv_image_view_init(&dest_iview
, cmd_buffer
->device
,
398 &(VkImageViewCreateInfo
) {
399 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
400 .image
= radv_image_to_handle(dest_image
),
401 .viewType
= radv_meta_get_view_type(dest_image
),
402 .format
= dest_image
->vk_format
,
403 .subresourceRange
= {
404 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
405 .baseMipLevel
= region
->dstSubresource
.mipLevel
,
407 .baseArrayLayer
= dest_base_layer
+ layer
,
411 cmd_buffer
, VK_IMAGE_USAGE_STORAGE_BIT
);
413 emit_resolve(cmd_buffer
,
416 &(VkOffset2D
) {srcOffset
.x
, srcOffset
.y
},
417 &(VkOffset2D
) {dstOffset
.x
, dstOffset
.y
},
418 &(VkExtent2D
) {extent
.width
, extent
.height
});
421 radv_meta_restore_compute(&saved_state
, cmd_buffer
, 16);