2 * Copyright © 2016 Intel Corporation
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
27 #include "radv_meta.h"
28 #include "radv_private.h"
32 create_pass(struct radv_device
*device
,
37 VkDevice device_h
= radv_device_to_handle(device
);
38 const VkAllocationCallbacks
*alloc
= &device
->meta_state
.alloc
;
39 VkAttachmentDescription attachment
;
42 attachment
.format
= VK_FORMAT_D32_SFLOAT_S8_UINT
;
43 attachment
.samples
= samples
;
44 attachment
.loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
;
45 attachment
.storeOp
= VK_ATTACHMENT_STORE_OP_STORE
;
46 attachment
.stencilLoadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
;
47 attachment
.stencilStoreOp
= VK_ATTACHMENT_STORE_OP_STORE
;
48 attachment
.initialLayout
= VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
;
49 attachment
.finalLayout
= VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
;
51 result
= radv_CreateRenderPass(device_h
,
52 &(VkRenderPassCreateInfo
) {
53 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
55 .pAttachments
= &attachment
,
57 .pSubpasses
= &(VkSubpassDescription
) {
58 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
59 .inputAttachmentCount
= 0,
60 .colorAttachmentCount
= 0,
61 .pColorAttachments
= NULL
,
62 .pResolveAttachments
= NULL
,
63 .pDepthStencilAttachment
= &(VkAttachmentReference
) {
65 .layout
= VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
,
67 .preserveAttachmentCount
= 0,
68 .pPreserveAttachments
= NULL
,
79 create_pipeline_layout(struct radv_device
*device
, VkPipelineLayout
*layout
)
81 VkPipelineLayoutCreateInfo pl_create_info
= {
82 .sType
= VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
,
85 .pushConstantRangeCount
= 0,
86 .pPushConstantRanges
= NULL
,
89 return radv_CreatePipelineLayout(radv_device_to_handle(device
),
91 &device
->meta_state
.alloc
,
96 create_pipeline(struct radv_device
*device
,
97 VkShaderModule vs_module_h
,
100 VkPipelineLayout layout
,
101 VkPipeline
*decompress_pipeline
,
102 VkPipeline
*resummarize_pipeline
)
105 VkDevice device_h
= radv_device_to_handle(device
);
106 struct radv_shader_module vs_module
= {0};
108 mtx_lock(&device
->meta_state
.mtx
);
109 if (*decompress_pipeline
) {
110 mtx_unlock(&device
->meta_state
.mtx
);
115 vs_module
.nir
= radv_meta_build_nir_vs_generate_vertices();
116 vs_module_h
= radv_shader_module_to_handle(&vs_module
);
119 struct radv_shader_module fs_module
= {
120 .nir
= radv_meta_build_nir_fs_noop(),
123 if (!fs_module
.nir
) {
124 /* XXX: Need more accurate error */
125 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
129 const VkPipelineSampleLocationsStateCreateInfoEXT sample_locs_create_info
= {
130 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SAMPLE_LOCATIONS_STATE_CREATE_INFO_EXT
,
131 .sampleLocationsEnable
= false,
134 const VkGraphicsPipelineCreateInfo pipeline_create_info
= {
135 .sType
= VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
,
137 .pStages
= (VkPipelineShaderStageCreateInfo
[]) {
139 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
140 .stage
= VK_SHADER_STAGE_VERTEX_BIT
,
141 .module
= vs_module_h
,
145 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
146 .stage
= VK_SHADER_STAGE_FRAGMENT_BIT
,
147 .module
= radv_shader_module_to_handle(&fs_module
),
151 .pVertexInputState
= &(VkPipelineVertexInputStateCreateInfo
) {
152 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
153 .vertexBindingDescriptionCount
= 0,
154 .vertexAttributeDescriptionCount
= 0,
156 .pInputAssemblyState
= &(VkPipelineInputAssemblyStateCreateInfo
) {
157 .sType
= VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO
,
158 .topology
= VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
,
159 .primitiveRestartEnable
= false,
161 .pViewportState
= &(VkPipelineViewportStateCreateInfo
) {
162 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO
,
166 .pRasterizationState
= &(VkPipelineRasterizationStateCreateInfo
) {
167 .sType
= VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO
,
168 .depthClampEnable
= false,
169 .rasterizerDiscardEnable
= false,
170 .polygonMode
= VK_POLYGON_MODE_FILL
,
171 .cullMode
= VK_CULL_MODE_NONE
,
172 .frontFace
= VK_FRONT_FACE_COUNTER_CLOCKWISE
,
174 .pMultisampleState
= &(VkPipelineMultisampleStateCreateInfo
) {
175 .sType
= VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO
,
176 .pNext
= &sample_locs_create_info
,
177 .rasterizationSamples
= samples
,
178 .sampleShadingEnable
= false,
180 .alphaToCoverageEnable
= false,
181 .alphaToOneEnable
= false,
183 .pColorBlendState
= &(VkPipelineColorBlendStateCreateInfo
) {
184 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
185 .logicOpEnable
= false,
186 .attachmentCount
= 0,
187 .pAttachments
= NULL
,
189 .pDepthStencilState
= &(VkPipelineDepthStencilStateCreateInfo
) {
190 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO
,
191 .depthTestEnable
= false,
192 .depthWriteEnable
= false,
193 .depthBoundsTestEnable
= false,
194 .stencilTestEnable
= false,
196 .pDynamicState
= &(VkPipelineDynamicStateCreateInfo
) {
197 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO
,
198 .dynamicStateCount
= 3,
199 .pDynamicStates
= (VkDynamicState
[]) {
200 VK_DYNAMIC_STATE_VIEWPORT
,
201 VK_DYNAMIC_STATE_SCISSOR
,
202 VK_DYNAMIC_STATE_SAMPLE_LOCATIONS_EXT
,
210 result
= radv_graphics_pipeline_create(device_h
,
211 radv_pipeline_cache_to_handle(&device
->meta_state
.cache
),
212 &pipeline_create_info
,
213 &(struct radv_graphics_pipeline_create_info
) {
214 .use_rectlist
= true,
215 .db_flush_depth_inplace
= true,
216 .db_flush_stencil_inplace
= true,
218 &device
->meta_state
.alloc
,
219 decompress_pipeline
);
220 if (result
!= VK_SUCCESS
)
223 result
= radv_graphics_pipeline_create(device_h
,
224 radv_pipeline_cache_to_handle(&device
->meta_state
.cache
),
225 &pipeline_create_info
,
226 &(struct radv_graphics_pipeline_create_info
) {
227 .use_rectlist
= true,
228 .db_flush_depth_inplace
= true,
229 .db_flush_stencil_inplace
= true,
230 .db_resummarize
= true,
232 &device
->meta_state
.alloc
,
233 resummarize_pipeline
);
234 if (result
!= VK_SUCCESS
)
240 ralloc_free(fs_module
.nir
);
242 ralloc_free(vs_module
.nir
);
243 mtx_unlock(&device
->meta_state
.mtx
);
248 radv_device_finish_meta_depth_decomp_state(struct radv_device
*device
)
250 struct radv_meta_state
*state
= &device
->meta_state
;
252 for (uint32_t i
= 0; i
< ARRAY_SIZE(state
->depth_decomp
); ++i
) {
253 radv_DestroyRenderPass(radv_device_to_handle(device
),
254 state
->depth_decomp
[i
].pass
,
256 radv_DestroyPipelineLayout(radv_device_to_handle(device
),
257 state
->depth_decomp
[i
].p_layout
,
259 radv_DestroyPipeline(radv_device_to_handle(device
),
260 state
->depth_decomp
[i
].decompress_pipeline
,
262 radv_DestroyPipeline(radv_device_to_handle(device
),
263 state
->depth_decomp
[i
].resummarize_pipeline
,
269 radv_device_init_meta_depth_decomp_state(struct radv_device
*device
, bool on_demand
)
271 struct radv_meta_state
*state
= &device
->meta_state
;
272 VkResult res
= VK_SUCCESS
;
274 struct radv_shader_module vs_module
= { .nir
= radv_meta_build_nir_vs_generate_vertices() };
275 if (!vs_module
.nir
) {
276 /* XXX: Need more accurate error */
277 res
= VK_ERROR_OUT_OF_HOST_MEMORY
;
281 VkShaderModule vs_module_h
= radv_shader_module_to_handle(&vs_module
);
283 for (uint32_t i
= 0; i
< ARRAY_SIZE(state
->depth_decomp
); ++i
) {
284 uint32_t samples
= 1 << i
;
286 res
= create_pass(device
, samples
, &state
->depth_decomp
[i
].pass
);
287 if (res
!= VK_SUCCESS
)
290 res
= create_pipeline_layout(device
,
291 &state
->depth_decomp
[i
].p_layout
);
292 if (res
!= VK_SUCCESS
)
298 res
= create_pipeline(device
, vs_module_h
, samples
,
299 state
->depth_decomp
[i
].pass
,
300 state
->depth_decomp
[i
].p_layout
,
301 &state
->depth_decomp
[i
].decompress_pipeline
,
302 &state
->depth_decomp
[i
].resummarize_pipeline
);
303 if (res
!= VK_SUCCESS
)
310 radv_device_finish_meta_depth_decomp_state(device
);
313 ralloc_free(vs_module
.nir
);
323 static void radv_process_depth_image_inplace(struct radv_cmd_buffer
*cmd_buffer
,
324 struct radv_image
*image
,
325 VkImageSubresourceRange
*subresourceRange
,
326 enum radv_depth_op op
)
328 struct radv_meta_saved_state saved_state
;
329 VkDevice device_h
= radv_device_to_handle(cmd_buffer
->device
);
330 VkCommandBuffer cmd_buffer_h
= radv_cmd_buffer_to_handle(cmd_buffer
);
331 uint32_t width
= radv_minify(image
->info
.width
,
332 subresourceRange
->baseMipLevel
);
333 uint32_t height
= radv_minify(image
->info
.height
,
334 subresourceRange
->baseMipLevel
);
335 uint32_t samples
= image
->info
.samples
;
336 uint32_t samples_log2
= ffs(samples
) - 1;
337 struct radv_meta_state
*meta_state
= &cmd_buffer
->device
->meta_state
;
338 VkPipeline pipeline_h
;
340 if (!radv_image_has_htile(image
))
343 if (!meta_state
->depth_decomp
[samples_log2
].decompress_pipeline
) {
344 VkResult ret
= create_pipeline(cmd_buffer
->device
, VK_NULL_HANDLE
, samples
,
345 meta_state
->depth_decomp
[samples_log2
].pass
,
346 meta_state
->depth_decomp
[samples_log2
].p_layout
,
347 &meta_state
->depth_decomp
[samples_log2
].decompress_pipeline
,
348 &meta_state
->depth_decomp
[samples_log2
].resummarize_pipeline
);
349 if (ret
!= VK_SUCCESS
) {
350 cmd_buffer
->record_result
= ret
;
355 radv_meta_save(&saved_state
, cmd_buffer
,
356 RADV_META_SAVE_GRAPHICS_PIPELINE
|
357 RADV_META_SAVE_PASS
);
360 case DEPTH_DECOMPRESS
:
361 pipeline_h
= meta_state
->depth_decomp
[samples_log2
].decompress_pipeline
;
363 case DEPTH_RESUMMARIZE
:
364 pipeline_h
= meta_state
->depth_decomp
[samples_log2
].resummarize_pipeline
;
367 unreachable("unknown operation");
370 radv_CmdBindPipeline(cmd_buffer_h
, VK_PIPELINE_BIND_POINT_GRAPHICS
,
373 radv_CmdSetViewport(cmd_buffer_h
, 0, 1, &(VkViewport
) {
382 radv_CmdSetScissor(cmd_buffer_h
, 0, 1, &(VkRect2D
) {
384 .extent
= { width
, height
},
387 for (uint32_t layer
= 0; layer
< radv_get_layerCount(image
, subresourceRange
); layer
++) {
388 struct radv_image_view iview
;
390 radv_image_view_init(&iview
, cmd_buffer
->device
,
391 &(VkImageViewCreateInfo
) {
392 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
393 .image
= radv_image_to_handle(image
),
394 .viewType
= radv_meta_get_view_type(image
),
395 .format
= image
->vk_format
,
396 .subresourceRange
= {
397 .aspectMask
= VK_IMAGE_ASPECT_DEPTH_BIT
,
398 .baseMipLevel
= subresourceRange
->baseMipLevel
,
400 .baseArrayLayer
= subresourceRange
->baseArrayLayer
+ layer
,
407 radv_CreateFramebuffer(device_h
,
408 &(VkFramebufferCreateInfo
) {
409 .sType
= VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
,
410 .attachmentCount
= 1,
411 .pAttachments
= (VkImageView
[]) {
412 radv_image_view_to_handle(&iview
)
418 &cmd_buffer
->pool
->alloc
,
421 radv_CmdBeginRenderPass(cmd_buffer_h
,
422 &(VkRenderPassBeginInfo
) {
423 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO
,
424 .renderPass
= meta_state
->depth_decomp
[samples_log2
].pass
,
436 .clearValueCount
= 0,
437 .pClearValues
= NULL
,
439 VK_SUBPASS_CONTENTS_INLINE
);
441 radv_CmdDraw(cmd_buffer_h
, 3, 1, 0, 0);
442 radv_CmdEndRenderPass(cmd_buffer_h
);
444 radv_DestroyFramebuffer(device_h
, fb_h
,
445 &cmd_buffer
->pool
->alloc
);
447 radv_meta_restore(&saved_state
, cmd_buffer
);
450 void radv_decompress_depth_image_inplace(struct radv_cmd_buffer
*cmd_buffer
,
451 struct radv_image
*image
,
452 VkImageSubresourceRange
*subresourceRange
)
454 assert(cmd_buffer
->queue_family_index
== RADV_QUEUE_GENERAL
);
455 radv_process_depth_image_inplace(cmd_buffer
, image
, subresourceRange
, DEPTH_DECOMPRESS
);
458 void radv_resummarize_depth_image_inplace(struct radv_cmd_buffer
*cmd_buffer
,
459 struct radv_image
*image
,
460 VkImageSubresourceRange
*subresourceRange
)
462 assert(cmd_buffer
->queue_family_index
== RADV_QUEUE_GENERAL
);
463 radv_process_depth_image_inplace(cmd_buffer
, image
, subresourceRange
, DEPTH_RESUMMARIZE
);