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
)
35 VkDevice device_h
= radv_device_to_handle(device
);
36 const VkAllocationCallbacks
*alloc
= &device
->meta_state
.alloc
;
37 VkAttachmentDescription attachment
;
39 attachment
.format
= VK_FORMAT_D32_SFLOAT_S8_UINT
;
40 attachment
.samples
= 1;
41 attachment
.loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
;
42 attachment
.storeOp
= VK_ATTACHMENT_STORE_OP_STORE
;
43 attachment
.initialLayout
= VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
;
44 attachment
.finalLayout
= VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
;
46 result
= radv_CreateRenderPass(device_h
,
47 &(VkRenderPassCreateInfo
) {
48 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
50 .pAttachments
= &attachment
,
52 .pSubpasses
= &(VkSubpassDescription
) {
53 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
54 .inputAttachmentCount
= 0,
55 .colorAttachmentCount
= 0,
56 .pColorAttachments
= NULL
,
57 .pResolveAttachments
= NULL
,
58 .pDepthStencilAttachment
= &(VkAttachmentReference
) {
60 .layout
= VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
,
62 .preserveAttachmentCount
= 0,
63 .pPreserveAttachments
= NULL
,
68 &device
->meta_state
.depth_decomp
.pass
);
74 create_pipeline(struct radv_device
*device
,
75 VkShaderModule vs_module_h
)
78 VkDevice device_h
= radv_device_to_handle(device
);
80 struct radv_shader_module fs_module
= {
81 .nir
= radv_meta_build_nir_fs_noop(),
85 /* XXX: Need more accurate error */
86 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
90 const VkGraphicsPipelineCreateInfo pipeline_create_info
= {
91 .sType
= VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
,
93 .pStages
= (VkPipelineShaderStageCreateInfo
[]) {
95 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
96 .stage
= VK_SHADER_STAGE_VERTEX_BIT
,
97 .module
= vs_module_h
,
101 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
102 .stage
= VK_SHADER_STAGE_FRAGMENT_BIT
,
103 .module
= radv_shader_module_to_handle(&fs_module
),
107 .pVertexInputState
= &(VkPipelineVertexInputStateCreateInfo
) {
108 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
109 .vertexBindingDescriptionCount
= 0,
110 .vertexAttributeDescriptionCount
= 0,
112 .pInputAssemblyState
= &(VkPipelineInputAssemblyStateCreateInfo
) {
113 .sType
= VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO
,
114 .topology
= VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
,
115 .primitiveRestartEnable
= false,
117 .pViewportState
= &(VkPipelineViewportStateCreateInfo
) {
118 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO
,
122 .pRasterizationState
= &(VkPipelineRasterizationStateCreateInfo
) {
123 .sType
= VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO
,
124 .depthClampEnable
= false,
125 .rasterizerDiscardEnable
= false,
126 .polygonMode
= VK_POLYGON_MODE_FILL
,
127 .cullMode
= VK_CULL_MODE_NONE
,
128 .frontFace
= VK_FRONT_FACE_COUNTER_CLOCKWISE
,
130 .pMultisampleState
= &(VkPipelineMultisampleStateCreateInfo
) {
131 .sType
= VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO
,
132 .rasterizationSamples
= 1,
133 .sampleShadingEnable
= false,
135 .alphaToCoverageEnable
= false,
136 .alphaToOneEnable
= false,
138 .pColorBlendState
= &(VkPipelineColorBlendStateCreateInfo
) {
139 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
140 .logicOpEnable
= false,
141 .attachmentCount
= 0,
142 .pAttachments
= NULL
,
144 .pDepthStencilState
= &(VkPipelineDepthStencilStateCreateInfo
) {
145 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO
,
146 .depthTestEnable
= false,
147 .depthWriteEnable
= false,
148 .depthBoundsTestEnable
= false,
149 .stencilTestEnable
= false,
151 .pDynamicState
= &(VkPipelineDynamicStateCreateInfo
) {
152 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO
,
153 .dynamicStateCount
= 2,
154 .pDynamicStates
= (VkDynamicState
[]) {
155 VK_DYNAMIC_STATE_VIEWPORT
,
156 VK_DYNAMIC_STATE_SCISSOR
,
159 .renderPass
= device
->meta_state
.depth_decomp
.pass
,
163 result
= radv_graphics_pipeline_create(device_h
,
164 radv_pipeline_cache_to_handle(&device
->meta_state
.cache
),
165 &pipeline_create_info
,
166 &(struct radv_graphics_pipeline_create_info
) {
167 .use_rectlist
= true,
168 .db_flush_depth_inplace
= true,
169 .db_flush_stencil_inplace
= true,
171 &device
->meta_state
.alloc
,
172 &device
->meta_state
.depth_decomp
.decompress_pipeline
);
173 if (result
!= VK_SUCCESS
)
176 result
= radv_graphics_pipeline_create(device_h
,
177 radv_pipeline_cache_to_handle(&device
->meta_state
.cache
),
178 &pipeline_create_info
,
179 &(struct radv_graphics_pipeline_create_info
) {
180 .use_rectlist
= true,
181 .db_flush_depth_inplace
= true,
182 .db_flush_stencil_inplace
= true,
183 .db_resummarize
= true,
185 &device
->meta_state
.alloc
,
186 &device
->meta_state
.depth_decomp
.resummarize_pipeline
);
187 if (result
!= VK_SUCCESS
)
193 ralloc_free(fs_module
.nir
);
198 radv_device_finish_meta_depth_decomp_state(struct radv_device
*device
)
200 struct radv_meta_state
*state
= &device
->meta_state
;
201 VkDevice device_h
= radv_device_to_handle(device
);
202 VkRenderPass pass_h
= device
->meta_state
.depth_decomp
.pass
;
203 const VkAllocationCallbacks
*alloc
= &device
->meta_state
.alloc
;
206 radv_DestroyRenderPass(device_h
, pass_h
,
207 &device
->meta_state
.alloc
);
209 VkPipeline pipeline_h
= state
->depth_decomp
.decompress_pipeline
;
211 radv_DestroyPipeline(device_h
, pipeline_h
, alloc
);
213 pipeline_h
= state
->depth_decomp
.resummarize_pipeline
;
215 radv_DestroyPipeline(device_h
, pipeline_h
, alloc
);
220 radv_device_init_meta_depth_decomp_state(struct radv_device
*device
)
222 VkResult res
= VK_SUCCESS
;
224 zero(device
->meta_state
.depth_decomp
);
226 struct radv_shader_module vs_module
= { .nir
= radv_meta_build_nir_vs_generate_vertices() };
227 if (!vs_module
.nir
) {
228 /* XXX: Need more accurate error */
229 res
= VK_ERROR_OUT_OF_HOST_MEMORY
;
233 res
= create_pass(device
);
234 if (res
!= VK_SUCCESS
)
237 VkShaderModule vs_module_h
= radv_shader_module_to_handle(&vs_module
);
238 res
= create_pipeline(device
, vs_module_h
);
239 if (res
!= VK_SUCCESS
)
245 radv_device_finish_meta_depth_decomp_state(device
);
248 ralloc_free(vs_module
.nir
);
254 emit_depth_decomp(struct radv_cmd_buffer
*cmd_buffer
,
255 const VkOffset2D
*dest_offset
,
256 const VkExtent2D
*depth_decomp_extent
,
257 VkPipeline pipeline_h
)
259 VkCommandBuffer cmd_buffer_h
= radv_cmd_buffer_to_handle(cmd_buffer
);
261 RADV_FROM_HANDLE(radv_pipeline
, pipeline
, pipeline_h
);
263 if (cmd_buffer
->state
.pipeline
!= pipeline
) {
264 radv_CmdBindPipeline(cmd_buffer_h
, VK_PIPELINE_BIND_POINT_GRAPHICS
,
268 radv_CmdSetViewport(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &(VkViewport
) {
271 .width
= depth_decomp_extent
->width
,
272 .height
= depth_decomp_extent
->height
,
277 radv_CmdSetScissor(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &(VkRect2D
) {
278 .offset
= *dest_offset
,
279 .extent
= *depth_decomp_extent
,
282 radv_CmdDraw(cmd_buffer_h
, 3, 1, 0, 0);
286 static void radv_process_depth_image_inplace(struct radv_cmd_buffer
*cmd_buffer
,
287 struct radv_image
*image
,
288 VkImageSubresourceRange
*subresourceRange
,
289 VkPipeline pipeline_h
)
291 struct radv_meta_saved_state saved_state
;
292 struct radv_meta_saved_pass_state saved_pass_state
;
293 VkDevice device_h
= radv_device_to_handle(cmd_buffer
->device
);
294 VkCommandBuffer cmd_buffer_h
= radv_cmd_buffer_to_handle(cmd_buffer
);
295 uint32_t width
= radv_minify(image
->info
.width
,
296 subresourceRange
->baseMipLevel
);
297 uint32_t height
= radv_minify(image
->info
.height
,
298 subresourceRange
->baseMipLevel
);
300 if (!image
->surface
.htile_size
)
302 radv_meta_save_pass(&saved_pass_state
, cmd_buffer
);
304 radv_meta_save_graphics_reset_vport_scissor_novertex(&saved_state
, cmd_buffer
);
306 for (uint32_t layer
= 0; layer
< radv_get_layerCount(image
, subresourceRange
); layer
++) {
307 struct radv_image_view iview
;
309 radv_image_view_init(&iview
, cmd_buffer
->device
,
310 &(VkImageViewCreateInfo
) {
311 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
312 .image
= radv_image_to_handle(image
),
313 .format
= image
->vk_format
,
314 .subresourceRange
= {
315 .aspectMask
= VK_IMAGE_ASPECT_DEPTH_BIT
,
316 .baseMipLevel
= subresourceRange
->baseMipLevel
,
318 .baseArrayLayer
= subresourceRange
->baseArrayLayer
+ layer
,
325 radv_CreateFramebuffer(device_h
,
326 &(VkFramebufferCreateInfo
) {
327 .sType
= VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
,
328 .attachmentCount
= 1,
329 .pAttachments
= (VkImageView
[]) {
330 radv_image_view_to_handle(&iview
)
336 &cmd_buffer
->pool
->alloc
,
339 radv_CmdBeginRenderPass(cmd_buffer_h
,
340 &(VkRenderPassBeginInfo
) {
341 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO
,
342 .renderPass
= cmd_buffer
->device
->meta_state
.depth_decomp
.pass
,
354 .clearValueCount
= 0,
355 .pClearValues
= NULL
,
357 VK_SUBPASS_CONTENTS_INLINE
);
359 emit_depth_decomp(cmd_buffer
, &(VkOffset2D
){0, 0 }, &(VkExtent2D
){width
, height
}, pipeline_h
);
360 radv_CmdEndRenderPass(cmd_buffer_h
);
362 radv_DestroyFramebuffer(device_h
, fb_h
,
363 &cmd_buffer
->pool
->alloc
);
365 radv_meta_restore(&saved_state
, cmd_buffer
);
366 radv_meta_restore_pass(&saved_pass_state
, cmd_buffer
);
369 void radv_decompress_depth_image_inplace(struct radv_cmd_buffer
*cmd_buffer
,
370 struct radv_image
*image
,
371 VkImageSubresourceRange
*subresourceRange
)
373 assert(cmd_buffer
->queue_family_index
== RADV_QUEUE_GENERAL
);
374 radv_process_depth_image_inplace(cmd_buffer
, image
, subresourceRange
,
375 cmd_buffer
->device
->meta_state
.depth_decomp
.decompress_pipeline
);
378 void radv_resummarize_depth_image_inplace(struct radv_cmd_buffer
*cmd_buffer
,
379 struct radv_image
*image
,
380 VkImageSubresourceRange
*subresourceRange
)
382 assert(cmd_buffer
->queue_family_index
== RADV_QUEUE_GENERAL
);
383 radv_process_depth_image_inplace(cmd_buffer
, image
, subresourceRange
,
384 cmd_buffer
->device
->meta_state
.depth_decomp
.resummarize_pipeline
);