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"
29 #include "nir/nir_builder.h"
32 * Vertex attributes used by all pipelines.
35 float position
[2]; /**< 3DPRIM_RECTLIST */
38 /* passthrough vertex shader */
42 const struct glsl_type
*vec4
= glsl_vec4_type();
45 nir_variable
*a_position
;
46 nir_variable
*v_position
;
48 nir_builder_init_simple_shader(&b
, NULL
, MESA_SHADER_VERTEX
, NULL
);
49 b
.shader
->info
->name
= ralloc_strdup(b
.shader
, "meta_fast_clear_vs");
51 a_position
= nir_variable_create(b
.shader
, nir_var_shader_in
, vec4
,
53 a_position
->data
.location
= VERT_ATTRIB_GENERIC0
;
55 v_position
= nir_variable_create(b
.shader
, nir_var_shader_out
, vec4
,
57 v_position
->data
.location
= VARYING_SLOT_POS
;
59 nir_copy_var(&b
, v_position
, a_position
);
64 /* simple passthrough shader */
70 nir_builder_init_simple_shader(&b
, NULL
, MESA_SHADER_FRAGMENT
, NULL
);
71 b
.shader
->info
->name
= ralloc_asprintf(b
.shader
,
72 "meta_fast_clear_noop_fs");
78 create_pass(struct radv_device
*device
)
81 VkDevice device_h
= radv_device_to_handle(device
);
82 const VkAllocationCallbacks
*alloc
= &device
->meta_state
.alloc
;
83 VkAttachmentDescription attachment
;
85 attachment
.format
= VK_FORMAT_UNDEFINED
;
86 attachment
.samples
= 1;
87 attachment
.loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
;
88 attachment
.storeOp
= VK_ATTACHMENT_STORE_OP_STORE
;
89 attachment
.initialLayout
= VK_IMAGE_LAYOUT_GENERAL
;
90 attachment
.finalLayout
= VK_IMAGE_LAYOUT_GENERAL
;
92 result
= radv_CreateRenderPass(device_h
,
93 &(VkRenderPassCreateInfo
) {
94 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
96 .pAttachments
= &attachment
,
98 .pSubpasses
= &(VkSubpassDescription
) {
99 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
100 .inputAttachmentCount
= 0,
101 .colorAttachmentCount
= 1,
102 .pColorAttachments
= (VkAttachmentReference
[]) {
105 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
108 .pResolveAttachments
= NULL
,
109 .pDepthStencilAttachment
= &(VkAttachmentReference
) {
110 .attachment
= VK_ATTACHMENT_UNUSED
,
112 .preserveAttachmentCount
= 0,
113 .pPreserveAttachments
= NULL
,
115 .dependencyCount
= 0,
118 &device
->meta_state
.fast_clear_flush
.pass
);
124 create_pipeline(struct radv_device
*device
,
125 VkShaderModule vs_module_h
)
128 VkDevice device_h
= radv_device_to_handle(device
);
130 struct radv_shader_module fs_module
= {
131 .nir
= build_nir_fs(),
134 if (!fs_module
.nir
) {
135 /* XXX: Need more accurate error */
136 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
140 const VkPipelineShaderStageCreateInfo stages
[2] = {
142 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
143 .stage
= VK_SHADER_STAGE_VERTEX_BIT
,
144 .module
= vs_module_h
,
148 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
149 .stage
= VK_SHADER_STAGE_FRAGMENT_BIT
,
150 .module
= radv_shader_module_to_handle(&fs_module
),
155 const VkPipelineVertexInputStateCreateInfo vi_state
= {
156 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
157 .vertexBindingDescriptionCount
= 1,
158 .pVertexBindingDescriptions
= (VkVertexInputBindingDescription
[]) {
161 .stride
= sizeof(struct vertex_attrs
),
162 .inputRate
= VK_VERTEX_INPUT_RATE_VERTEX
165 .vertexAttributeDescriptionCount
= 1,
166 .pVertexAttributeDescriptions
= (VkVertexInputAttributeDescription
[]) {
171 .format
= VK_FORMAT_R32G32_SFLOAT
,
172 .offset
= offsetof(struct vertex_attrs
, position
),
177 const VkPipelineInputAssemblyStateCreateInfo ia_state
= {
178 .sType
= VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO
,
179 .topology
= VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
,
180 .primitiveRestartEnable
= false,
183 const VkPipelineColorBlendStateCreateInfo blend_state
= {
184 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
185 .logicOpEnable
= false,
186 .attachmentCount
= 1,
187 .pAttachments
= (VkPipelineColorBlendAttachmentState
[]) {
189 .colorWriteMask
= VK_COLOR_COMPONENT_R_BIT
|
190 VK_COLOR_COMPONENT_G_BIT
|
191 VK_COLOR_COMPONENT_B_BIT
|
192 VK_COLOR_COMPONENT_A_BIT
,
196 const VkPipelineRasterizationStateCreateInfo rs_state
= {
197 .sType
= VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO
,
198 .depthClampEnable
= false,
199 .rasterizerDiscardEnable
= false,
200 .polygonMode
= VK_POLYGON_MODE_FILL
,
201 .cullMode
= VK_CULL_MODE_NONE
,
202 .frontFace
= VK_FRONT_FACE_COUNTER_CLOCKWISE
,
205 result
= radv_graphics_pipeline_create(device_h
,
206 radv_pipeline_cache_to_handle(&device
->meta_state
.cache
),
207 &(VkGraphicsPipelineCreateInfo
) {
208 .sType
= VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
,
212 .pVertexInputState
= &vi_state
,
213 .pInputAssemblyState
= &ia_state
,
215 .pViewportState
= &(VkPipelineViewportStateCreateInfo
) {
216 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO
,
220 .pRasterizationState
= &rs_state
,
221 .pMultisampleState
= &(VkPipelineMultisampleStateCreateInfo
) {
222 .sType
= VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO
,
223 .rasterizationSamples
= 1,
224 .sampleShadingEnable
= false,
226 .alphaToCoverageEnable
= false,
227 .alphaToOneEnable
= false,
229 .pColorBlendState
= &blend_state
,
230 .pDynamicState
= &(VkPipelineDynamicStateCreateInfo
) {
231 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO
,
232 .dynamicStateCount
= 2,
233 .pDynamicStates
= (VkDynamicState
[]) {
234 VK_DYNAMIC_STATE_VIEWPORT
,
235 VK_DYNAMIC_STATE_SCISSOR
,
238 .renderPass
= device
->meta_state
.fast_clear_flush
.pass
,
241 &(struct radv_graphics_pipeline_create_info
) {
242 .use_rectlist
= true,
243 .custom_blend_mode
= V_028808_CB_ELIMINATE_FAST_CLEAR
,
245 &device
->meta_state
.alloc
,
246 &device
->meta_state
.fast_clear_flush
.cmask_eliminate_pipeline
);
247 if (result
!= VK_SUCCESS
)
250 result
= radv_graphics_pipeline_create(device_h
,
251 radv_pipeline_cache_to_handle(&device
->meta_state
.cache
),
252 &(VkGraphicsPipelineCreateInfo
) {
253 .sType
= VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
,
257 .pVertexInputState
= &vi_state
,
258 .pInputAssemblyState
= &ia_state
,
260 .pViewportState
= &(VkPipelineViewportStateCreateInfo
) {
261 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO
,
265 .pRasterizationState
= &rs_state
,
266 .pMultisampleState
= &(VkPipelineMultisampleStateCreateInfo
) {
267 .sType
= VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO
,
268 .rasterizationSamples
= 1,
269 .sampleShadingEnable
= false,
271 .alphaToCoverageEnable
= false,
272 .alphaToOneEnable
= false,
274 .pColorBlendState
= &blend_state
,
275 .pDynamicState
= &(VkPipelineDynamicStateCreateInfo
) {
276 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO
,
277 .dynamicStateCount
= 2,
278 .pDynamicStates
= (VkDynamicState
[]) {
279 VK_DYNAMIC_STATE_VIEWPORT
,
280 VK_DYNAMIC_STATE_SCISSOR
,
283 .renderPass
= device
->meta_state
.fast_clear_flush
.pass
,
286 &(struct radv_graphics_pipeline_create_info
) {
287 .use_rectlist
= true,
288 .custom_blend_mode
= V_028808_CB_FMASK_DECOMPRESS
,
290 &device
->meta_state
.alloc
,
291 &device
->meta_state
.fast_clear_flush
.fmask_decompress_pipeline
);
292 if (result
!= VK_SUCCESS
)
297 radv_DestroyPipeline(device_h
, device
->meta_state
.fast_clear_flush
.cmask_eliminate_pipeline
, &device
->meta_state
.alloc
);
299 ralloc_free(fs_module
.nir
);
304 radv_device_finish_meta_fast_clear_flush_state(struct radv_device
*device
)
306 struct radv_meta_state
*state
= &device
->meta_state
;
307 VkDevice device_h
= radv_device_to_handle(device
);
308 VkRenderPass pass_h
= device
->meta_state
.fast_clear_flush
.pass
;
309 const VkAllocationCallbacks
*alloc
= &device
->meta_state
.alloc
;
312 radv_DestroyRenderPass(device_h
, pass_h
,
313 &device
->meta_state
.alloc
);
315 VkPipeline pipeline_h
= state
->fast_clear_flush
.cmask_eliminate_pipeline
;
317 radv_DestroyPipeline(device_h
, pipeline_h
, alloc
);
320 pipeline_h
= state
->fast_clear_flush
.fmask_decompress_pipeline
;
322 radv_DestroyPipeline(device_h
, pipeline_h
, alloc
);
327 radv_device_init_meta_fast_clear_flush_state(struct radv_device
*device
)
329 VkResult res
= VK_SUCCESS
;
331 zero(device
->meta_state
.fast_clear_flush
);
333 struct radv_shader_module vs_module
= { .nir
= build_nir_vs() };
334 if (!vs_module
.nir
) {
335 /* XXX: Need more accurate error */
336 res
= VK_ERROR_OUT_OF_HOST_MEMORY
;
340 res
= create_pass(device
);
341 if (res
!= VK_SUCCESS
)
344 VkShaderModule vs_module_h
= radv_shader_module_to_handle(&vs_module
);
345 res
= create_pipeline(device
, vs_module_h
);
346 if (res
!= VK_SUCCESS
)
352 radv_device_finish_meta_fast_clear_flush_state(device
);
355 ralloc_free(vs_module
.nir
);
361 emit_fast_clear_flush(struct radv_cmd_buffer
*cmd_buffer
,
362 const VkExtent2D
*resolve_extent
,
363 bool fmask_decompress
)
365 struct radv_device
*device
= cmd_buffer
->device
;
366 VkCommandBuffer cmd_buffer_h
= radv_cmd_buffer_to_handle(cmd_buffer
);
368 const struct vertex_attrs vertex_data
[3] = {
389 radv_cmd_buffer_upload_data(cmd_buffer
, sizeof(vertex_data
), 16, vertex_data
, &offset
);
390 struct radv_buffer vertex_buffer
= {
392 .size
= sizeof(vertex_data
),
393 .bo
= cmd_buffer
->upload
.upload_bo
,
397 VkBuffer vertex_buffer_h
= radv_buffer_to_handle(&vertex_buffer
);
399 radv_CmdBindVertexBuffers(cmd_buffer_h
,
402 (VkBuffer
[]) { vertex_buffer_h
},
403 (VkDeviceSize
[]) { 0 });
405 VkPipeline pipeline_h
;
406 if (fmask_decompress
)
407 pipeline_h
= device
->meta_state
.fast_clear_flush
.fmask_decompress_pipeline
;
409 pipeline_h
= device
->meta_state
.fast_clear_flush
.cmask_eliminate_pipeline
;
410 RADV_FROM_HANDLE(radv_pipeline
, pipeline
, pipeline_h
);
412 if (cmd_buffer
->state
.pipeline
!= pipeline
) {
413 radv_CmdBindPipeline(cmd_buffer_h
, VK_PIPELINE_BIND_POINT_GRAPHICS
,
417 radv_CmdSetViewport(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &(VkViewport
) {
420 .width
= resolve_extent
->width
,
421 .height
= resolve_extent
->height
,
426 radv_CmdSetScissor(radv_cmd_buffer_to_handle(cmd_buffer
), 0, 1, &(VkRect2D
) {
427 .offset
= (VkOffset2D
) { 0, 0 },
428 .extent
= (VkExtent2D
) { resolve_extent
->width
, resolve_extent
->height
},
431 radv_CmdDraw(cmd_buffer_h
, 3, 1, 0, 0);
432 cmd_buffer
->state
.flush_bits
|= (RADV_CMD_FLAG_FLUSH_AND_INV_CB
|
433 RADV_CMD_FLAG_FLUSH_AND_INV_CB_META
);
439 radv_fast_clear_flush_image_inplace(struct radv_cmd_buffer
*cmd_buffer
,
440 struct radv_image
*image
,
441 const VkImageSubresourceRange
*subresourceRange
)
443 struct radv_meta_saved_state saved_state
;
444 struct radv_meta_saved_pass_state saved_pass_state
;
445 VkDevice device_h
= radv_device_to_handle(cmd_buffer
->device
);
446 VkCommandBuffer cmd_buffer_h
= radv_cmd_buffer_to_handle(cmd_buffer
);
447 uint32_t layer_count
= radv_get_layerCount(image
, subresourceRange
);
449 assert(cmd_buffer
->queue_family_index
== RADV_QUEUE_GENERAL
);
450 radv_meta_save_pass(&saved_pass_state
, cmd_buffer
);
451 radv_meta_save_graphics_reset_vport_scissor(&saved_state
, cmd_buffer
);
453 for (uint32_t layer
= 0; layer
< layer_count
; ++layer
) {
454 struct radv_image_view iview
;
456 radv_image_view_init(&iview
, cmd_buffer
->device
,
457 &(VkImageViewCreateInfo
) {
458 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
459 .image
= radv_image_to_handle(image
),
460 .viewType
= radv_meta_get_view_type(image
),
461 .format
= image
->vk_format
,
462 .subresourceRange
= {
463 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
466 .baseArrayLayer
= subresourceRange
->baseArrayLayer
+ layer
,
470 cmd_buffer
, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
);
473 radv_CreateFramebuffer(device_h
,
474 &(VkFramebufferCreateInfo
) {
475 .sType
= VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
,
476 .attachmentCount
= 1,
477 .pAttachments
= (VkImageView
[]) {
478 radv_image_view_to_handle(&iview
)
480 .width
= image
->extent
.width
,
481 .height
= image
->extent
.height
,
484 &cmd_buffer
->pool
->alloc
,
487 radv_CmdBeginRenderPass(cmd_buffer_h
,
488 &(VkRenderPassBeginInfo
) {
489 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO
,
490 .renderPass
= cmd_buffer
->device
->meta_state
.fast_clear_flush
.pass
,
499 image
->extent
.height
,
502 .clearValueCount
= 0,
503 .pClearValues
= NULL
,
505 VK_SUBPASS_CONTENTS_INLINE
);
507 emit_fast_clear_flush(cmd_buffer
,
508 &(VkExtent2D
) { image
->extent
.width
, image
->extent
.height
},
509 image
->fmask
.size
> 0);
510 radv_CmdEndRenderPass(cmd_buffer_h
);
512 radv_DestroyFramebuffer(device_h
, fb_h
,
513 &cmd_buffer
->pool
->alloc
);
516 radv_meta_restore(&saved_state
, cmd_buffer
);
517 radv_meta_restore_pass(&saved_pass_state
, cmd_buffer
);