2 * Copyright © 2015 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
30 #include "anv_private.h"
31 #include "anv_nir_builder.h"
34 build_nir_vertex_shader(bool attr_flat
)
38 const struct glsl_type
*vertex_type
= glsl_vec4_type();
40 nir_builder_init_simple_shader(&b
, MESA_SHADER_VERTEX
);
42 nir_variable
*pos_in
= nir_variable_create(b
.shader
, nir_var_shader_in
,
43 vertex_type
, "a_pos");
44 pos_in
->data
.location
= VERT_ATTRIB_GENERIC0
;
45 nir_variable
*pos_out
= nir_variable_create(b
.shader
, nir_var_shader_out
,
46 vertex_type
, "gl_Position");
47 pos_in
->data
.location
= VARYING_SLOT_POS
;
48 nir_copy_var(&b
, pos_out
, pos_in
);
50 /* Add one more pass-through attribute. For clear shaders, this is used
51 * to store the color and for blit shaders it's the texture coordinate.
53 const struct glsl_type
*attr_type
= glsl_vec4_type();
54 nir_variable
*attr_in
= nir_variable_create(b
.shader
, nir_var_shader_in
,
56 attr_in
->data
.location
= VERT_ATTRIB_GENERIC1
;
57 nir_variable
*attr_out
= nir_variable_create(b
.shader
, nir_var_shader_out
,
59 attr_out
->data
.location
= VARYING_SLOT_VAR0
;
60 attr_out
->data
.interpolation
= attr_flat
? INTERP_QUALIFIER_FLAT
:
61 INTERP_QUALIFIER_SMOOTH
;
62 nir_copy_var(&b
, attr_out
, attr_in
);
68 build_nir_clear_fragment_shader(void)
72 const struct glsl_type
*color_type
= glsl_vec4_type();
74 nir_builder_init_simple_shader(&b
, MESA_SHADER_FRAGMENT
);
76 nir_variable
*color_in
= nir_variable_create(b
.shader
, nir_var_shader_in
,
77 color_type
, "v_attr");
78 color_in
->data
.location
= VARYING_SLOT_VAR0
;
79 color_in
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
80 nir_variable
*color_out
= nir_variable_create(b
.shader
, nir_var_shader_out
,
81 color_type
, "f_color");
82 color_out
->data
.location
= FRAG_RESULT_DATA0
;
83 nir_copy_var(&b
, color_out
, color_in
);
89 build_nir_copy_fragment_shader(enum glsl_sampler_dim tex_dim
)
93 nir_builder_init_simple_shader(&b
, MESA_SHADER_FRAGMENT
);
95 const struct glsl_type
*color_type
= glsl_vec4_type();
97 nir_variable
*tex_pos_in
= nir_variable_create(b
.shader
, nir_var_shader_in
,
98 glsl_vec4_type(), "v_attr");
99 tex_pos_in
->data
.location
= VARYING_SLOT_VAR0
;
101 const struct glsl_type
*sampler_type
=
102 glsl_sampler_type(tex_dim
, false, false, glsl_get_base_type(color_type
));
103 nir_variable
*sampler
= nir_variable_create(b
.shader
, nir_var_uniform
,
104 sampler_type
, "s_tex");
105 sampler
->data
.descriptor_set
= 0;
106 sampler
->data
.binding
= 0;
108 nir_tex_instr
*tex
= nir_tex_instr_create(b
.shader
, 1);
109 tex
->sampler_dim
= tex_dim
;
110 tex
->op
= nir_texop_tex
;
111 tex
->src
[0].src_type
= nir_tex_src_coord
;
112 tex
->src
[0].src
= nir_src_for_ssa(nir_load_var(&b
, tex_pos_in
));
113 tex
->dest_type
= nir_type_float
; /* TODO */
116 case GLSL_SAMPLER_DIM_2D
:
117 tex
->coord_components
= 2;
119 case GLSL_SAMPLER_DIM_3D
:
120 tex
->coord_components
= 3;
123 assert(!"Unsupported texture dimension");
126 tex
->sampler
= nir_deref_var_create(tex
, sampler
);
128 nir_ssa_dest_init(&tex
->instr
, &tex
->dest
, 4, "tex");
129 nir_builder_instr_insert(&b
, &tex
->instr
);
131 nir_variable
*color_out
= nir_variable_create(b
.shader
, nir_var_shader_out
,
132 color_type
, "f_color");
133 color_out
->data
.location
= FRAG_RESULT_DATA0
;
134 nir_store_var(&b
, color_out
, &tex
->dest
.ssa
);
140 anv_device_init_meta_clear_state(struct anv_device
*device
)
142 struct anv_shader_module vsm
= {
143 .nir
= build_nir_vertex_shader(true),
146 struct anv_shader_module fsm
= {
147 .nir
= build_nir_clear_fragment_shader(),
151 anv_CreateShader(anv_device_to_handle(device
),
152 &(VkShaderCreateInfo
) {
153 .sType
= VK_STRUCTURE_TYPE_SHADER_CREATE_INFO
,
154 .module
= anv_shader_module_to_handle(&vsm
),
159 anv_CreateShader(anv_device_to_handle(device
),
160 &(VkShaderCreateInfo
) {
161 .sType
= VK_STRUCTURE_TYPE_SHADER_CREATE_INFO
,
162 .module
= anv_shader_module_to_handle(&fsm
),
166 /* We use instanced rendering to clear multiple render targets. We have two
167 * vertex buffers: the first vertex buffer holds per-vertex data and
168 * provides the vertices for the clear rectangle. The second one holds
169 * per-instance data, which consists of the VUE header (which selects the
170 * layer) and the color (Vulkan supports per-RT clear colors).
172 VkPipelineVertexInputStateCreateInfo vi_create_info
= {
173 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
175 .pVertexBindingDescriptions
= (VkVertexInputBindingDescription
[]) {
179 .stepRate
= VK_VERTEX_INPUT_STEP_RATE_VERTEX
184 .stepRate
= VK_VERTEX_INPUT_STEP_RATE_INSTANCE
188 .pVertexAttributeDescriptions
= (VkVertexInputAttributeDescription
[]) {
193 .format
= VK_FORMAT_R32G32B32A32_UINT
,
200 .format
= VK_FORMAT_R32G32B32_SFLOAT
,
207 .format
= VK_FORMAT_R32G32B32A32_SFLOAT
,
213 anv_graphics_pipeline_create(anv_device_to_handle(device
),
214 &(VkGraphicsPipelineCreateInfo
) {
215 .sType
= VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
,
218 .pStages
= (VkPipelineShaderStageCreateInfo
[]) {
220 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
221 .stage
= VK_SHADER_STAGE_VERTEX
,
223 .pSpecializationInfo
= NULL
225 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
226 .stage
= VK_SHADER_STAGE_FRAGMENT
,
228 .pSpecializationInfo
= NULL
,
231 .pVertexInputState
= &vi_create_info
,
232 .pInputAssemblyState
= &(VkPipelineInputAssemblyStateCreateInfo
) {
233 .sType
= VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO
,
234 .topology
= VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
,
235 .primitiveRestartEnable
= false,
237 .pViewportState
= &(VkPipelineViewportStateCreateInfo
) {
238 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO
,
242 .pRasterState
= &(VkPipelineRasterStateCreateInfo
) {
243 .sType
= VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO
,
244 .depthClipEnable
= true,
245 .rasterizerDiscardEnable
= false,
246 .fillMode
= VK_FILL_MODE_SOLID
,
247 .cullMode
= VK_CULL_MODE_NONE
,
248 .frontFace
= VK_FRONT_FACE_CCW
250 .pMultisampleState
= &(VkPipelineMultisampleStateCreateInfo
) {
251 .sType
= VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO
,
253 .sampleShadingEnable
= false,
254 .pSampleMask
= (VkSampleMask
[]) { UINT32_MAX
},
256 .pDepthStencilState
= &(VkPipelineDepthStencilStateCreateInfo
) {
257 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO
,
258 .depthTestEnable
= true,
259 .depthWriteEnable
= true,
260 .depthCompareOp
= VK_COMPARE_OP_ALWAYS
,
261 .depthBoundsTestEnable
= false,
262 .stencilTestEnable
= true,
263 .front
= (VkStencilOpState
) {
264 .stencilPassOp
= VK_STENCIL_OP_REPLACE
,
265 .stencilCompareOp
= VK_COMPARE_OP_ALWAYS
,
267 .back
= (VkStencilOpState
) {
268 .stencilPassOp
= VK_STENCIL_OP_REPLACE
,
269 .stencilCompareOp
= VK_COMPARE_OP_ALWAYS
,
272 .pColorBlendState
= &(VkPipelineColorBlendStateCreateInfo
) {
273 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
274 .attachmentCount
= 1,
275 .pAttachments
= (VkPipelineColorBlendAttachmentState
[]) {
276 { .channelWriteMask
= VK_CHANNEL_A_BIT
|
277 VK_CHANNEL_R_BIT
| VK_CHANNEL_G_BIT
| VK_CHANNEL_B_BIT
},
280 .pDynamicState
= &(VkPipelineDynamicStateCreateInfo
) {
281 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO
,
282 .dynamicStateCount
= 9,
283 .pDynamicStates
= (VkDynamicState
[]) {
284 VK_DYNAMIC_STATE_VIEWPORT
,
285 VK_DYNAMIC_STATE_SCISSOR
,
286 VK_DYNAMIC_STATE_LINE_WIDTH
,
287 VK_DYNAMIC_STATE_DEPTH_BIAS
,
288 VK_DYNAMIC_STATE_BLEND_CONSTANTS
,
289 VK_DYNAMIC_STATE_DEPTH_BOUNDS
,
290 VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
,
291 VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
,
292 VK_DYNAMIC_STATE_STENCIL_REFERENCE
,
297 &(struct anv_graphics_pipeline_create_info
) {
298 .use_repclear
= true,
299 .disable_viewport
= true,
303 &device
->meta_state
.clear
.pipeline
);
305 anv_DestroyShader(anv_device_to_handle(device
), vs
);
306 anv_DestroyShader(anv_device_to_handle(device
), fs
);
307 ralloc_free(vsm
.nir
);
308 ralloc_free(fsm
.nir
);
311 #define NUM_VB_USED 2
312 struct anv_saved_state
{
313 struct anv_vertex_binding old_vertex_bindings
[NUM_VB_USED
];
314 struct anv_descriptor_set
*old_descriptor_set0
;
315 struct anv_pipeline
*old_pipeline
;
316 uint32_t dynamic_flags
;
317 struct anv_dynamic_state dynamic
;
321 anv_cmd_buffer_save(struct anv_cmd_buffer
*cmd_buffer
,
322 struct anv_saved_state
*state
,
323 uint32_t dynamic_state
)
325 state
->old_pipeline
= cmd_buffer
->state
.pipeline
;
326 state
->old_descriptor_set0
= cmd_buffer
->state
.descriptors
[0];
327 memcpy(state
->old_vertex_bindings
, cmd_buffer
->state
.vertex_bindings
,
328 sizeof(state
->old_vertex_bindings
));
329 state
->dynamic_flags
= dynamic_state
;
330 anv_dynamic_state_copy(&state
->dynamic
, &cmd_buffer
->state
.dynamic
,
335 anv_cmd_buffer_restore(struct anv_cmd_buffer
*cmd_buffer
,
336 const struct anv_saved_state
*state
)
338 cmd_buffer
->state
.pipeline
= state
->old_pipeline
;
339 cmd_buffer
->state
.descriptors
[0] = state
->old_descriptor_set0
;
340 memcpy(cmd_buffer
->state
.vertex_bindings
, state
->old_vertex_bindings
,
341 sizeof(state
->old_vertex_bindings
));
343 cmd_buffer
->state
.vb_dirty
|= (1 << NUM_VB_USED
) - 1;
344 cmd_buffer
->state
.dirty
|= ANV_CMD_BUFFER_PIPELINE_DIRTY
;
345 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_VERTEX_BIT
;
347 anv_dynamic_state_copy(&cmd_buffer
->state
.dynamic
, &state
->dynamic
,
348 state
->dynamic_flags
);
349 cmd_buffer
->state
.dirty
|= state
->dynamic_flags
;
355 uint32_t ViewportIndex
;
359 struct clear_instance_data
{
360 struct vue_header vue_header
;
361 VkClearColorValue color
;
365 meta_emit_clear(struct anv_cmd_buffer
*cmd_buffer
,
367 struct clear_instance_data
*instance_data
,
368 VkClearDepthStencilValue ds_clear_value
)
370 struct anv_device
*device
= cmd_buffer
->device
;
371 struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
372 struct anv_state state
;
375 const float vertex_data
[] = {
376 /* Rect-list coordinates */
377 0.0, 0.0, ds_clear_value
.depth
,
378 fb
->width
, 0.0, ds_clear_value
.depth
,
379 fb
->width
, fb
->height
, ds_clear_value
.depth
,
381 /* Align to 16 bytes */
385 size
= sizeof(vertex_data
) + num_instances
* sizeof(*instance_data
);
386 state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, size
, 16);
388 /* Copy in the vertex and instance data */
389 memcpy(state
.map
, vertex_data
, sizeof(vertex_data
));
390 memcpy(state
.map
+ sizeof(vertex_data
), instance_data
,
391 num_instances
* sizeof(*instance_data
));
393 struct anv_buffer vertex_buffer
= {
394 .device
= cmd_buffer
->device
,
396 .bo
= &device
->dynamic_state_block_pool
.bo
,
397 .offset
= state
.offset
400 anv_CmdBindVertexBuffers(anv_cmd_buffer_to_handle(cmd_buffer
), 0, 2,
402 anv_buffer_to_handle(&vertex_buffer
),
403 anv_buffer_to_handle(&vertex_buffer
)
410 if (cmd_buffer
->state
.pipeline
!= anv_pipeline_from_handle(device
->meta_state
.clear
.pipeline
))
411 anv_CmdBindPipeline(anv_cmd_buffer_to_handle(cmd_buffer
),
412 VK_PIPELINE_BIND_POINT_GRAPHICS
,
413 device
->meta_state
.clear
.pipeline
);
415 ANV_CALL(CmdDraw
)(anv_cmd_buffer_to_handle(cmd_buffer
),
416 3, num_instances
, 0, 0);
420 anv_cmd_buffer_clear_attachments(struct anv_cmd_buffer
*cmd_buffer
,
421 struct anv_render_pass
*pass
,
422 const VkClearValue
*clear_values
)
424 struct anv_saved_state saved_state
;
426 if (pass
->has_stencil_clear_attachment
)
427 anv_finishme("stencil clear");
429 /* FINISHME: Rethink how we count clear attachments in light of
430 * 0.138.2 -> 0.170.2 diff.
432 if (pass
->num_color_clear_attachments
== 0 &&
433 !pass
->has_depth_clear_attachment
)
436 struct clear_instance_data instance_data
[pass
->num_color_clear_attachments
];
437 uint32_t color_attachments
[pass
->num_color_clear_attachments
];
438 uint32_t ds_attachment
= VK_ATTACHMENT_UNUSED
;
439 VkClearDepthStencilValue ds_clear_value
= {0};
442 for (uint32_t i
= 0; i
< pass
->attachment_count
; i
++) {
443 const struct anv_render_pass_attachment
*att
= &pass
->attachments
[i
];
445 if (att
->load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
446 if (anv_format_is_color(att
->format
)) {
447 instance_data
[layer
] = (struct clear_instance_data
) {
453 .color
= clear_values
[i
].color
,
455 color_attachments
[layer
] = i
;
457 } else if (att
->format
->depth_format
) {
458 assert(ds_attachment
== VK_ATTACHMENT_UNUSED
);
460 ds_clear_value
= clear_values
[ds_attachment
].depthStencil
;
462 } else if (att
->stencil_load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
463 assert(att
->format
->has_stencil
);
464 anv_finishme("stencil clear");
468 anv_cmd_buffer_save(cmd_buffer
, &saved_state
,
469 (1 << VK_DYNAMIC_STATE_VIEWPORT
));
470 cmd_buffer
->state
.dynamic
.viewport
.count
= 0;
472 struct anv_subpass subpass
= {
474 .color_count
= pass
->num_color_clear_attachments
,
475 .color_attachments
= color_attachments
,
476 .depth_stencil_attachment
= ds_attachment
,
479 anv_cmd_buffer_begin_subpass(cmd_buffer
, &subpass
);
481 meta_emit_clear(cmd_buffer
, pass
->num_color_clear_attachments
,
482 instance_data
, ds_clear_value
);
484 /* Restore API state */
485 anv_cmd_buffer_restore(cmd_buffer
, &saved_state
);
488 static VkImageViewType
489 meta_blit_get_src_image_view_type(const struct anv_image
*src_image
)
491 switch (src_image
->type
) {
492 case VK_IMAGE_TYPE_1D
:
493 return VK_IMAGE_VIEW_TYPE_1D
;
494 case VK_IMAGE_TYPE_2D
:
495 return VK_IMAGE_VIEW_TYPE_2D
;
496 case VK_IMAGE_TYPE_3D
:
497 return VK_IMAGE_VIEW_TYPE_3D
;
499 assert(!"bad VkImageType");
505 meta_blit_get_dest_view_base_array_slice(const struct anv_image
*dest_image
,
506 const VkImageSubresourceCopy
*dest_subresource
,
507 const VkOffset3D
*dest_offset
)
509 switch (dest_image
->type
) {
510 case VK_IMAGE_TYPE_1D
:
511 case VK_IMAGE_TYPE_2D
:
512 return dest_subresource
->arrayLayer
;
513 case VK_IMAGE_TYPE_3D
:
514 /* HACK: Vulkan does not allow attaching a 3D image to a framebuffer,
515 * but meta does it anyway. When doing so, we translate the
516 * destination's z offset into an array offset.
518 return dest_offset
->z
;
520 assert(!"bad VkImageType");
526 anv_device_init_meta_blit_state(struct anv_device
*device
)
528 /* We don't use a vertex shader for clearing, but instead build and pass
529 * the VUEs directly to the rasterization backend. However, we do need
530 * to provide GLSL source for the vertex shader so that the compiler
531 * does not dead-code our inputs.
533 struct anv_shader_module vsm
= {
534 .nir
= build_nir_vertex_shader(false),
537 struct anv_shader_module fsm_2d
= {
538 .nir
= build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_2D
),
541 struct anv_shader_module fsm_3d
= {
542 .nir
= build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_3D
),
546 anv_CreateShader(anv_device_to_handle(device
),
547 &(VkShaderCreateInfo
) {
548 .sType
= VK_STRUCTURE_TYPE_SHADER_CREATE_INFO
,
549 .module
= anv_shader_module_to_handle(&vsm
),
554 anv_CreateShader(anv_device_to_handle(device
),
555 &(VkShaderCreateInfo
) {
556 .sType
= VK_STRUCTURE_TYPE_SHADER_CREATE_INFO
,
557 .module
= anv_shader_module_to_handle(&fsm_2d
),
562 anv_CreateShader(anv_device_to_handle(device
),
563 &(VkShaderCreateInfo
) {
564 .sType
= VK_STRUCTURE_TYPE_SHADER_CREATE_INFO
,
565 .module
= anv_shader_module_to_handle(&fsm_3d
),
569 VkPipelineVertexInputStateCreateInfo vi_create_info
= {
570 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
572 .pVertexBindingDescriptions
= (VkVertexInputBindingDescription
[]) {
576 .stepRate
= VK_VERTEX_INPUT_STEP_RATE_VERTEX
580 .strideInBytes
= 5 * sizeof(float),
581 .stepRate
= VK_VERTEX_INPUT_STEP_RATE_VERTEX
585 .pVertexAttributeDescriptions
= (VkVertexInputAttributeDescription
[]) {
590 .format
= VK_FORMAT_R32G32B32A32_UINT
,
597 .format
= VK_FORMAT_R32G32_SFLOAT
,
601 /* Texture Coordinate */
604 .format
= VK_FORMAT_R32G32B32_SFLOAT
,
610 VkDescriptorSetLayoutCreateInfo ds_layout_info
= {
611 .sType
= VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
,
613 .pBinding
= (VkDescriptorSetLayoutBinding
[]) {
615 .descriptorType
= VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
,
617 .stageFlags
= VK_SHADER_STAGE_FRAGMENT_BIT
,
618 .pImmutableSamplers
= NULL
622 anv_CreateDescriptorSetLayout(anv_device_to_handle(device
), &ds_layout_info
,
623 &device
->meta_state
.blit
.ds_layout
);
625 anv_CreatePipelineLayout(anv_device_to_handle(device
),
626 &(VkPipelineLayoutCreateInfo
) {
627 .sType
= VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
,
628 .descriptorSetCount
= 1,
629 .pSetLayouts
= &device
->meta_state
.blit
.ds_layout
,
631 &device
->meta_state
.blit
.pipeline_layout
);
633 VkPipelineShaderStageCreateInfo pipeline_shader_stages
[] = {
635 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
636 .stage
= VK_SHADER_STAGE_VERTEX
,
638 .pSpecializationInfo
= NULL
640 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
641 .stage
= VK_SHADER_STAGE_FRAGMENT
,
642 .shader
= {0}, /* TEMPLATE VALUE! FILL ME IN! */
643 .pSpecializationInfo
= NULL
647 const VkGraphicsPipelineCreateInfo vk_pipeline_info
= {
648 .sType
= VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
,
649 .stageCount
= ARRAY_SIZE(pipeline_shader_stages
),
650 .pStages
= pipeline_shader_stages
,
651 .pVertexInputState
= &vi_create_info
,
652 .pInputAssemblyState
= &(VkPipelineInputAssemblyStateCreateInfo
) {
653 .sType
= VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO
,
654 .topology
= VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
,
655 .primitiveRestartEnable
= false,
657 .pViewportState
= &(VkPipelineViewportStateCreateInfo
) {
658 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO
,
662 .pRasterState
= &(VkPipelineRasterStateCreateInfo
) {
663 .sType
= VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO
,
664 .depthClipEnable
= true,
665 .rasterizerDiscardEnable
= false,
666 .fillMode
= VK_FILL_MODE_SOLID
,
667 .cullMode
= VK_CULL_MODE_NONE
,
668 .frontFace
= VK_FRONT_FACE_CCW
670 .pMultisampleState
= &(VkPipelineMultisampleStateCreateInfo
) {
671 .sType
= VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO
,
673 .sampleShadingEnable
= false,
674 .pSampleMask
= (VkSampleMask
[]) { UINT32_MAX
},
676 .pColorBlendState
= &(VkPipelineColorBlendStateCreateInfo
) {
677 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
678 .attachmentCount
= 1,
679 .pAttachments
= (VkPipelineColorBlendAttachmentState
[]) {
680 { .channelWriteMask
= VK_CHANNEL_A_BIT
|
681 VK_CHANNEL_R_BIT
| VK_CHANNEL_G_BIT
| VK_CHANNEL_B_BIT
},
684 .pDynamicState
= &(VkPipelineDynamicStateCreateInfo
) {
685 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO
,
686 .dynamicStateCount
= 9,
687 .pDynamicStates
= (VkDynamicState
[]) {
688 VK_DYNAMIC_STATE_VIEWPORT
,
689 VK_DYNAMIC_STATE_SCISSOR
,
690 VK_DYNAMIC_STATE_LINE_WIDTH
,
691 VK_DYNAMIC_STATE_DEPTH_BIAS
,
692 VK_DYNAMIC_STATE_BLEND_CONSTANTS
,
693 VK_DYNAMIC_STATE_DEPTH_BOUNDS
,
694 VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
,
695 VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
,
696 VK_DYNAMIC_STATE_STENCIL_REFERENCE
,
700 .layout
= device
->meta_state
.blit
.pipeline_layout
,
703 const struct anv_graphics_pipeline_create_info anv_pipeline_info
= {
704 .use_repclear
= false,
705 .disable_viewport
= true,
706 .disable_scissor
= true,
711 pipeline_shader_stages
[1].shader
= fs_2d
;
712 anv_graphics_pipeline_create(anv_device_to_handle(device
),
713 &vk_pipeline_info
, &anv_pipeline_info
,
714 &device
->meta_state
.blit
.pipeline_2d_src
);
716 pipeline_shader_stages
[1].shader
= fs_3d
;
717 anv_graphics_pipeline_create(anv_device_to_handle(device
),
718 &vk_pipeline_info
, &anv_pipeline_info
,
719 &device
->meta_state
.blit
.pipeline_3d_src
);
721 anv_DestroyShader(anv_device_to_handle(device
), vs
);
722 anv_DestroyShader(anv_device_to_handle(device
), fs_2d
);
723 anv_DestroyShader(anv_device_to_handle(device
), fs_3d
);
724 ralloc_free(vsm
.nir
);
725 ralloc_free(fsm_2d
.nir
);
726 ralloc_free(fsm_3d
.nir
);
730 meta_prepare_blit(struct anv_cmd_buffer
*cmd_buffer
,
731 struct anv_saved_state
*saved_state
)
733 anv_cmd_buffer_save(cmd_buffer
, saved_state
,
734 (1 << VK_DYNAMIC_STATE_VIEWPORT
));
738 VkOffset3D src_offset
;
739 VkExtent3D src_extent
;
740 VkOffset3D dest_offset
;
741 VkExtent3D dest_extent
;
745 meta_emit_blit(struct anv_cmd_buffer
*cmd_buffer
,
746 struct anv_image
*src_image
,
747 struct anv_image_view
*src_iview
,
748 VkOffset3D src_offset
,
749 VkExtent3D src_extent
,
750 struct anv_image
*dest_image
,
751 struct anv_image_view
*dest_iview
,
752 VkOffset3D dest_offset
,
753 VkExtent3D dest_extent
)
755 struct anv_device
*device
= cmd_buffer
->device
;
756 VkDescriptorPool dummy_desc_pool
= { .handle
= 1 };
758 struct blit_vb_data
{
763 unsigned vb_size
= sizeof(struct vue_header
) + 3 * sizeof(*vb_data
);
765 struct anv_state vb_state
=
766 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, vb_size
, 16);
767 memset(vb_state
.map
, 0, sizeof(struct vue_header
));
768 vb_data
= vb_state
.map
+ sizeof(struct vue_header
);
770 vb_data
[0] = (struct blit_vb_data
) {
772 dest_offset
.x
+ dest_extent
.width
,
773 dest_offset
.y
+ dest_extent
.height
,
776 (float)(src_offset
.x
+ src_extent
.width
) / (float)src_iview
->extent
.width
,
777 (float)(src_offset
.y
+ src_extent
.height
) / (float)src_iview
->extent
.height
,
778 (float)(src_offset
.z
+ src_extent
.depth
) / (float)src_iview
->extent
.depth
,
782 vb_data
[1] = (struct blit_vb_data
) {
785 dest_offset
.y
+ dest_extent
.height
,
788 (float)src_offset
.x
/ (float)src_iview
->extent
.width
,
789 (float)(src_offset
.y
+ src_extent
.height
) / (float)src_iview
->extent
.height
,
790 (float)(src_offset
.z
+ src_extent
.depth
) / (float)src_iview
->extent
.depth
,
794 vb_data
[2] = (struct blit_vb_data
) {
800 (float)src_offset
.x
/ (float)src_iview
->extent
.width
,
801 (float)src_offset
.y
/ (float)src_iview
->extent
.height
,
802 (float)src_offset
.z
/ (float)src_iview
->extent
.depth
,
806 struct anv_buffer vertex_buffer
= {
809 .bo
= &device
->dynamic_state_block_pool
.bo
,
810 .offset
= vb_state
.offset
,
813 anv_CmdBindVertexBuffers(anv_cmd_buffer_to_handle(cmd_buffer
), 0, 2,
815 anv_buffer_to_handle(&vertex_buffer
),
816 anv_buffer_to_handle(&vertex_buffer
)
820 sizeof(struct vue_header
),
824 anv_AllocDescriptorSets(anv_device_to_handle(device
), dummy_desc_pool
,
825 VK_DESCRIPTOR_SET_USAGE_ONE_SHOT
,
826 1, &device
->meta_state
.blit
.ds_layout
, &set
);
827 anv_UpdateDescriptorSets(anv_device_to_handle(device
),
829 (VkWriteDescriptorSet
[]) {
831 .sType
= VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET
,
834 .destArrayElement
= 0,
836 .descriptorType
= VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
,
837 .pDescriptors
= (VkDescriptorInfo
[]) {
839 .imageView
= anv_image_view_to_handle(src_iview
),
840 .imageLayout
= VK_IMAGE_LAYOUT_GENERAL
847 anv_CreateFramebuffer(anv_device_to_handle(device
),
848 &(VkFramebufferCreateInfo
) {
849 .sType
= VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
,
850 .attachmentCount
= 1,
851 .pAttachments
= (VkImageView
[]) {
852 anv_image_view_to_handle(dest_iview
),
854 .width
= dest_iview
->extent
.width
,
855 .height
= dest_iview
->extent
.height
,
860 anv_CreateRenderPass(anv_device_to_handle(device
),
861 &(VkRenderPassCreateInfo
) {
862 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
863 .attachmentCount
= 1,
864 .pAttachments
= &(VkAttachmentDescription
) {
865 .sType
= VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION
,
866 .format
= dest_iview
->format
->vk_format
,
867 .loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
868 .storeOp
= VK_ATTACHMENT_STORE_OP_STORE
,
869 .initialLayout
= VK_IMAGE_LAYOUT_GENERAL
,
870 .finalLayout
= VK_IMAGE_LAYOUT_GENERAL
,
873 .pSubpasses
= &(VkSubpassDescription
) {
874 .sType
= VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION
,
875 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
878 .pColorAttachments
= &(VkAttachmentReference
) {
880 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
882 .pResolveAttachments
= NULL
,
883 .depthStencilAttachment
= (VkAttachmentReference
) {
884 .attachment
= VK_ATTACHMENT_UNUSED
,
885 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
888 .pPreserveAttachments
= &(VkAttachmentReference
) {
890 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
893 .dependencyCount
= 0,
896 ANV_CALL(CmdBeginRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
),
897 &(VkRenderPassBeginInfo
) {
898 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO
,
902 .offset
= { dest_offset
.x
, dest_offset
.y
},
903 .extent
= { dest_extent
.width
, dest_extent
.height
},
905 .clearValueCount
= 0,
906 .pClearValues
= NULL
,
907 }, VK_RENDER_PASS_CONTENTS_INLINE
);
911 switch (src_image
->type
) {
912 case VK_IMAGE_TYPE_1D
:
913 anv_finishme("VK_IMAGE_TYPE_1D");
914 pipeline
= device
->meta_state
.blit
.pipeline_2d_src
;
916 case VK_IMAGE_TYPE_2D
:
917 pipeline
= device
->meta_state
.blit
.pipeline_2d_src
;
919 case VK_IMAGE_TYPE_3D
:
920 pipeline
= device
->meta_state
.blit
.pipeline_3d_src
;
923 unreachable(!"bad VkImageType");
926 if (cmd_buffer
->state
.pipeline
!= anv_pipeline_from_handle(pipeline
)) {
927 anv_CmdBindPipeline(anv_cmd_buffer_to_handle(cmd_buffer
),
928 VK_PIPELINE_BIND_POINT_GRAPHICS
, pipeline
);
931 anv_CmdSetViewport(anv_cmd_buffer_to_handle(cmd_buffer
), 1,
935 .width
= dest_iview
->extent
.width
,
936 .height
= dest_iview
->extent
.height
,
941 anv_CmdBindDescriptorSets(anv_cmd_buffer_to_handle(cmd_buffer
),
942 VK_PIPELINE_BIND_POINT_GRAPHICS
,
943 device
->meta_state
.blit
.pipeline_layout
, 0, 1,
946 ANV_CALL(CmdDraw
)(anv_cmd_buffer_to_handle(cmd_buffer
), 3, 1, 0, 0);
948 ANV_CALL(CmdEndRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
));
950 /* At the point where we emit the draw call, all data from the
951 * descriptor sets, etc. has been used. We are free to delete it.
953 anv_descriptor_set_destroy(device
, anv_descriptor_set_from_handle(set
));
954 anv_DestroyFramebuffer(anv_device_to_handle(device
), fb
);
955 anv_DestroyRenderPass(anv_device_to_handle(device
), pass
);
959 meta_finish_blit(struct anv_cmd_buffer
*cmd_buffer
,
960 const struct anv_saved_state
*saved_state
)
962 anv_cmd_buffer_restore(cmd_buffer
, saved_state
);
966 vk_format_for_cpp(int cpp
)
969 case 1: return VK_FORMAT_R8_UINT
;
970 case 2: return VK_FORMAT_R8G8_UINT
;
971 case 3: return VK_FORMAT_R8G8B8_UINT
;
972 case 4: return VK_FORMAT_R8G8B8A8_UINT
;
973 case 6: return VK_FORMAT_R16G16B16_UINT
;
974 case 8: return VK_FORMAT_R16G16B16A16_UINT
;
975 case 12: return VK_FORMAT_R32G32B32_UINT
;
976 case 16: return VK_FORMAT_R32G32B32A32_UINT
;
978 unreachable("Invalid format cpp");
983 do_buffer_copy(struct anv_cmd_buffer
*cmd_buffer
,
984 struct anv_bo
*src
, uint64_t src_offset
,
985 struct anv_bo
*dest
, uint64_t dest_offset
,
986 int width
, int height
, VkFormat copy_format
)
988 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
990 VkImageCreateInfo image_info
= {
991 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
992 .imageType
= VK_IMAGE_TYPE_2D
,
993 .format
= copy_format
,
1002 .tiling
= VK_IMAGE_TILING_LINEAR
,
1008 image_info
.usage
= VK_IMAGE_USAGE_SAMPLED_BIT
;
1009 anv_CreateImage(vk_device
, &image_info
, &src_image
);
1012 image_info
.usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
1013 anv_CreateImage(vk_device
, &image_info
, &dest_image
);
1015 /* We could use a vk call to bind memory, but that would require
1016 * creating a dummy memory object etc. so there's really no point.
1018 anv_image_from_handle(src_image
)->bo
= src
;
1019 anv_image_from_handle(src_image
)->offset
= src_offset
;
1020 anv_image_from_handle(dest_image
)->bo
= dest
;
1021 anv_image_from_handle(dest_image
)->offset
= dest_offset
;
1023 struct anv_image_view src_iview
;
1024 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1025 &(VkImageViewCreateInfo
) {
1026 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1028 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1029 .format
= copy_format
,
1031 VK_CHANNEL_SWIZZLE_R
,
1032 VK_CHANNEL_SWIZZLE_G
,
1033 VK_CHANNEL_SWIZZLE_B
,
1034 VK_CHANNEL_SWIZZLE_A
1036 .subresourceRange
= {
1037 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1040 .baseArrayLayer
= 0,
1046 struct anv_image_view dest_iview
;
1047 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1048 &(VkImageViewCreateInfo
) {
1049 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1050 .image
= dest_image
,
1051 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1052 .format
= copy_format
,
1054 .r
= VK_CHANNEL_SWIZZLE_R
,
1055 .g
= VK_CHANNEL_SWIZZLE_G
,
1056 .b
= VK_CHANNEL_SWIZZLE_B
,
1057 .a
= VK_CHANNEL_SWIZZLE_A
,
1059 .subresourceRange
= {
1060 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1063 .baseArrayLayer
= 0,
1069 meta_emit_blit(cmd_buffer
,
1070 anv_image_from_handle(src_image
),
1072 (VkOffset3D
) { 0, 0, 0 },
1073 (VkExtent3D
) { width
, height
, 1 },
1074 anv_image_from_handle(dest_image
),
1076 (VkOffset3D
) { 0, 0, 0 },
1077 (VkExtent3D
) { width
, height
, 1 });
1079 anv_DestroyImage(vk_device
, src_image
);
1080 anv_DestroyImage(vk_device
, dest_image
);
1083 void anv_CmdCopyBuffer(
1084 VkCmdBuffer cmdBuffer
,
1086 VkBuffer destBuffer
,
1087 uint32_t regionCount
,
1088 const VkBufferCopy
* pRegions
)
1090 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
1091 ANV_FROM_HANDLE(anv_buffer
, src_buffer
, srcBuffer
);
1092 ANV_FROM_HANDLE(anv_buffer
, dest_buffer
, destBuffer
);
1094 struct anv_saved_state saved_state
;
1096 meta_prepare_blit(cmd_buffer
, &saved_state
);
1098 for (unsigned r
= 0; r
< regionCount
; r
++) {
1099 uint64_t src_offset
= src_buffer
->offset
+ pRegions
[r
].srcOffset
;
1100 uint64_t dest_offset
= dest_buffer
->offset
+ pRegions
[r
].destOffset
;
1101 uint64_t copy_size
= pRegions
[r
].copySize
;
1103 /* First, we compute the biggest format that can be used with the
1104 * given offsets and size.
1108 int fs
= ffs(src_offset
) - 1;
1110 cpp
= MIN2(cpp
, 1 << fs
);
1111 assert(src_offset
% cpp
== 0);
1113 fs
= ffs(dest_offset
) - 1;
1115 cpp
= MIN2(cpp
, 1 << fs
);
1116 assert(dest_offset
% cpp
== 0);
1118 fs
= ffs(pRegions
[r
].copySize
) - 1;
1120 cpp
= MIN2(cpp
, 1 << fs
);
1121 assert(pRegions
[r
].copySize
% cpp
== 0);
1123 VkFormat copy_format
= vk_format_for_cpp(cpp
);
1125 /* This is maximum possible width/height our HW can handle */
1126 uint64_t max_surface_dim
= 1 << 14;
1128 /* First, we make a bunch of max-sized copies */
1129 uint64_t max_copy_size
= max_surface_dim
* max_surface_dim
* cpp
;
1130 while (copy_size
> max_copy_size
) {
1131 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
1132 dest_buffer
->bo
, dest_offset
,
1133 max_surface_dim
, max_surface_dim
, copy_format
);
1134 copy_size
-= max_copy_size
;
1135 src_offset
+= max_copy_size
;
1136 dest_offset
+= max_copy_size
;
1139 uint64_t height
= copy_size
/ (max_surface_dim
* cpp
);
1140 assert(height
< max_surface_dim
);
1142 uint64_t rect_copy_size
= height
* max_surface_dim
* cpp
;
1143 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
1144 dest_buffer
->bo
, dest_offset
,
1145 max_surface_dim
, height
, copy_format
);
1146 copy_size
-= rect_copy_size
;
1147 src_offset
+= rect_copy_size
;
1148 dest_offset
+= rect_copy_size
;
1151 if (copy_size
!= 0) {
1152 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
1153 dest_buffer
->bo
, dest_offset
,
1154 copy_size
/ cpp
, 1, copy_format
);
1158 meta_finish_blit(cmd_buffer
, &saved_state
);
1161 void anv_CmdCopyImage(
1162 VkCmdBuffer cmdBuffer
,
1164 VkImageLayout srcImageLayout
,
1166 VkImageLayout destImageLayout
,
1167 uint32_t regionCount
,
1168 const VkImageCopy
* pRegions
)
1170 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
1171 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
1172 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
1174 const VkImageViewType src_iview_type
=
1175 meta_blit_get_src_image_view_type(src_image
);
1177 struct anv_saved_state saved_state
;
1179 meta_prepare_blit(cmd_buffer
, &saved_state
);
1181 for (unsigned r
= 0; r
< regionCount
; r
++) {
1182 struct anv_image_view src_iview
;
1183 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1184 &(VkImageViewCreateInfo
) {
1185 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1187 .viewType
= src_iview_type
,
1188 .format
= src_image
->format
->vk_format
,
1190 VK_CHANNEL_SWIZZLE_R
,
1191 VK_CHANNEL_SWIZZLE_G
,
1192 VK_CHANNEL_SWIZZLE_B
,
1193 VK_CHANNEL_SWIZZLE_A
1195 .subresourceRange
= {
1196 .aspectMask
= 1 << pRegions
[r
].srcSubresource
.aspect
,
1197 .baseMipLevel
= pRegions
[r
].srcSubresource
.mipLevel
,
1199 .baseArrayLayer
= pRegions
[r
].srcSubresource
.arrayLayer
,
1205 const VkOffset3D dest_offset
= {
1206 .x
= pRegions
[r
].destOffset
.x
,
1207 .y
= pRegions
[r
].destOffset
.y
,
1211 const uint32_t dest_array_slice
=
1212 meta_blit_get_dest_view_base_array_slice(dest_image
,
1213 &pRegions
[r
].destSubresource
,
1214 &pRegions
[r
].destOffset
);
1216 if (pRegions
[r
].srcSubresource
.arraySize
> 1)
1217 anv_finishme("FINISHME: copy multiple array layers");
1219 if (pRegions
[r
].extent
.depth
> 1)
1220 anv_finishme("FINISHME: copy multiple depth layers");
1222 struct anv_image_view dest_iview
;
1223 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1224 &(VkImageViewCreateInfo
) {
1225 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1227 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1228 .format
= dest_image
->format
->vk_format
,
1230 VK_CHANNEL_SWIZZLE_R
,
1231 VK_CHANNEL_SWIZZLE_G
,
1232 VK_CHANNEL_SWIZZLE_B
,
1233 VK_CHANNEL_SWIZZLE_A
1235 .subresourceRange
= {
1236 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1237 .baseMipLevel
= pRegions
[r
].destSubresource
.mipLevel
,
1239 .baseArrayLayer
= dest_array_slice
,
1245 meta_emit_blit(cmd_buffer
,
1246 src_image
, &src_iview
,
1247 pRegions
[r
].srcOffset
,
1249 dest_image
, &dest_iview
,
1251 pRegions
[r
].extent
);
1254 meta_finish_blit(cmd_buffer
, &saved_state
);
1257 void anv_CmdBlitImage(
1258 VkCmdBuffer cmdBuffer
,
1260 VkImageLayout srcImageLayout
,
1262 VkImageLayout destImageLayout
,
1263 uint32_t regionCount
,
1264 const VkImageBlit
* pRegions
,
1268 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
1269 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
1270 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
1272 const VkImageViewType src_iview_type
=
1273 meta_blit_get_src_image_view_type(src_image
);
1275 struct anv_saved_state saved_state
;
1277 anv_finishme("respect VkTexFilter");
1279 meta_prepare_blit(cmd_buffer
, &saved_state
);
1281 for (unsigned r
= 0; r
< regionCount
; r
++) {
1282 struct anv_image_view src_iview
;
1283 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1284 &(VkImageViewCreateInfo
) {
1285 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1287 .viewType
= src_iview_type
,
1288 .format
= src_image
->format
->vk_format
,
1290 VK_CHANNEL_SWIZZLE_R
,
1291 VK_CHANNEL_SWIZZLE_G
,
1292 VK_CHANNEL_SWIZZLE_B
,
1293 VK_CHANNEL_SWIZZLE_A
1295 .subresourceRange
= {
1296 .aspectMask
= 1 << pRegions
[r
].srcSubresource
.aspect
,
1297 .baseMipLevel
= pRegions
[r
].srcSubresource
.mipLevel
,
1299 .baseArrayLayer
= pRegions
[r
].srcSubresource
.arrayLayer
,
1305 const VkOffset3D dest_offset
= {
1306 .x
= pRegions
[r
].destOffset
.x
,
1307 .y
= pRegions
[r
].destOffset
.y
,
1311 const uint32_t dest_array_slice
=
1312 meta_blit_get_dest_view_base_array_slice(dest_image
,
1313 &pRegions
[r
].destSubresource
,
1314 &pRegions
[r
].destOffset
);
1316 if (pRegions
[r
].srcSubresource
.arraySize
> 1)
1317 anv_finishme("FINISHME: copy multiple array layers");
1319 if (pRegions
[r
].destExtent
.depth
> 1)
1320 anv_finishme("FINISHME: copy multiple depth layers");
1322 struct anv_image_view dest_iview
;
1323 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1324 &(VkImageViewCreateInfo
) {
1325 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1327 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1328 .format
= dest_image
->format
->vk_format
,
1330 VK_CHANNEL_SWIZZLE_R
,
1331 VK_CHANNEL_SWIZZLE_G
,
1332 VK_CHANNEL_SWIZZLE_B
,
1333 VK_CHANNEL_SWIZZLE_A
1335 .subresourceRange
= {
1336 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1337 .baseMipLevel
= pRegions
[r
].destSubresource
.mipLevel
,
1339 .baseArrayLayer
= dest_array_slice
,
1345 meta_emit_blit(cmd_buffer
,
1346 src_image
, &src_iview
,
1347 pRegions
[r
].srcOffset
,
1348 pRegions
[r
].srcExtent
,
1349 dest_image
, &dest_iview
,
1351 pRegions
[r
].destExtent
);
1354 meta_finish_blit(cmd_buffer
, &saved_state
);
1358 make_image_for_buffer(VkDevice vk_device
, VkBuffer vk_buffer
, VkFormat format
,
1359 VkImageUsageFlags usage
,
1360 const VkBufferImageCopy
*copy
)
1362 ANV_FROM_HANDLE(anv_buffer
, buffer
, vk_buffer
);
1364 VkExtent3D extent
= copy
->imageExtent
;
1365 if (copy
->bufferRowLength
)
1366 extent
.width
= copy
->bufferRowLength
;
1367 if (copy
->bufferImageHeight
)
1368 extent
.height
= copy
->bufferImageHeight
;
1372 VkResult result
= anv_CreateImage(vk_device
,
1373 &(VkImageCreateInfo
) {
1374 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
1375 .imageType
= VK_IMAGE_TYPE_2D
,
1381 .tiling
= VK_IMAGE_TILING_LINEAR
,
1385 assert(result
== VK_SUCCESS
);
1387 ANV_FROM_HANDLE(anv_image
, image
, vk_image
);
1389 /* We could use a vk call to bind memory, but that would require
1390 * creating a dummy memory object etc. so there's really no point.
1392 image
->bo
= buffer
->bo
;
1393 image
->offset
= buffer
->offset
+ copy
->bufferOffset
;
1395 return anv_image_to_handle(image
);
1398 void anv_CmdCopyBufferToImage(
1399 VkCmdBuffer cmdBuffer
,
1402 VkImageLayout destImageLayout
,
1403 uint32_t regionCount
,
1404 const VkBufferImageCopy
* pRegions
)
1406 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
1407 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
1408 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
1409 const VkFormat orig_format
= dest_image
->format
->vk_format
;
1410 struct anv_saved_state saved_state
;
1412 meta_prepare_blit(cmd_buffer
, &saved_state
);
1414 for (unsigned r
= 0; r
< regionCount
; r
++) {
1415 VkFormat proxy_format
= orig_format
;
1416 VkImageAspect proxy_aspect
= pRegions
[r
].imageSubresource
.aspect
;
1418 if (orig_format
== VK_FORMAT_S8_UINT
) {
1419 proxy_format
= VK_FORMAT_R8_UINT
;
1420 proxy_aspect
= VK_IMAGE_ASPECT_COLOR
;
1423 VkImage srcImage
= make_image_for_buffer(vk_device
, srcBuffer
,
1424 proxy_format
, VK_IMAGE_USAGE_SAMPLED_BIT
, &pRegions
[r
]);
1426 struct anv_image_view src_iview
;
1427 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1428 &(VkImageViewCreateInfo
) {
1429 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1431 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1432 .format
= proxy_format
,
1434 VK_CHANNEL_SWIZZLE_R
,
1435 VK_CHANNEL_SWIZZLE_G
,
1436 VK_CHANNEL_SWIZZLE_B
,
1437 VK_CHANNEL_SWIZZLE_A
1439 .subresourceRange
= {
1440 .aspectMask
= 1 << proxy_aspect
,
1443 .baseArrayLayer
= 0,
1449 const VkOffset3D dest_offset
= {
1450 .x
= pRegions
[r
].imageOffset
.x
,
1451 .y
= pRegions
[r
].imageOffset
.y
,
1455 const uint32_t dest_array_slice
=
1456 meta_blit_get_dest_view_base_array_slice(dest_image
,
1457 &pRegions
[r
].imageSubresource
,
1458 &pRegions
[r
].imageOffset
);
1460 if (pRegions
[r
].imageExtent
.depth
> 1)
1461 anv_finishme("FINISHME: copy multiple depth layers");
1463 struct anv_image_view dest_iview
;
1464 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1465 &(VkImageViewCreateInfo
) {
1466 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1467 .image
= anv_image_to_handle(dest_image
),
1468 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1469 .format
= proxy_format
,
1471 VK_CHANNEL_SWIZZLE_R
,
1472 VK_CHANNEL_SWIZZLE_G
,
1473 VK_CHANNEL_SWIZZLE_B
,
1474 VK_CHANNEL_SWIZZLE_A
1476 .subresourceRange
= {
1477 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1478 .baseMipLevel
= pRegions
[r
].imageSubresource
.mipLevel
,
1480 .baseArrayLayer
= dest_array_slice
,
1486 meta_emit_blit(cmd_buffer
,
1487 anv_image_from_handle(srcImage
),
1489 (VkOffset3D
) { 0, 0, 0 },
1490 pRegions
[r
].imageExtent
,
1494 pRegions
[r
].imageExtent
);
1496 anv_DestroyImage(vk_device
, srcImage
);
1499 meta_finish_blit(cmd_buffer
, &saved_state
);
1502 void anv_CmdCopyImageToBuffer(
1503 VkCmdBuffer cmdBuffer
,
1505 VkImageLayout srcImageLayout
,
1506 VkBuffer destBuffer
,
1507 uint32_t regionCount
,
1508 const VkBufferImageCopy
* pRegions
)
1510 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
1511 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
1512 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
1513 struct anv_saved_state saved_state
;
1515 const VkImageViewType src_iview_type
=
1516 meta_blit_get_src_image_view_type(src_image
);
1518 meta_prepare_blit(cmd_buffer
, &saved_state
);
1520 for (unsigned r
= 0; r
< regionCount
; r
++) {
1521 if (pRegions
[r
].imageSubresource
.arraySize
> 1)
1522 anv_finishme("FINISHME: copy multiple array layers");
1524 if (pRegions
[r
].imageExtent
.depth
> 1)
1525 anv_finishme("FINISHME: copy multiple depth layers");
1527 struct anv_image_view src_iview
;
1528 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1529 &(VkImageViewCreateInfo
) {
1530 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1532 .viewType
= src_iview_type
,
1533 .format
= src_image
->format
->vk_format
,
1535 VK_CHANNEL_SWIZZLE_R
,
1536 VK_CHANNEL_SWIZZLE_G
,
1537 VK_CHANNEL_SWIZZLE_B
,
1538 VK_CHANNEL_SWIZZLE_A
1540 .subresourceRange
= {
1541 .aspectMask
= 1 << pRegions
[r
].imageSubresource
.aspect
,
1542 .baseMipLevel
= pRegions
[r
].imageSubresource
.mipLevel
,
1544 .baseArrayLayer
= pRegions
[r
].imageSubresource
.arrayLayer
,
1550 VkFormat dest_format
= src_image
->format
->vk_format
;
1551 if (dest_format
== VK_FORMAT_S8_UINT
) {
1552 dest_format
= VK_FORMAT_R8_UINT
;
1555 VkImage destImage
= make_image_for_buffer(vk_device
, destBuffer
,
1556 dest_format
, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
, &pRegions
[r
]);
1558 struct anv_image_view dest_iview
;
1559 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1560 &(VkImageViewCreateInfo
) {
1561 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1563 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1564 .format
= dest_format
,
1566 VK_CHANNEL_SWIZZLE_R
,
1567 VK_CHANNEL_SWIZZLE_G
,
1568 VK_CHANNEL_SWIZZLE_B
,
1569 VK_CHANNEL_SWIZZLE_A
1571 .subresourceRange
= {
1572 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1575 .baseArrayLayer
= 0,
1581 meta_emit_blit(cmd_buffer
,
1582 anv_image_from_handle(srcImage
),
1584 pRegions
[r
].imageOffset
,
1585 pRegions
[r
].imageExtent
,
1586 anv_image_from_handle(destImage
),
1588 (VkOffset3D
) { 0, 0, 0 },
1589 pRegions
[r
].imageExtent
);
1591 anv_DestroyImage(vk_device
, destImage
);
1594 meta_finish_blit(cmd_buffer
, &saved_state
);
1597 void anv_CmdUpdateBuffer(
1598 VkCmdBuffer cmdBuffer
,
1599 VkBuffer destBuffer
,
1600 VkDeviceSize destOffset
,
1601 VkDeviceSize dataSize
,
1602 const uint32_t* pData
)
1607 void anv_CmdFillBuffer(
1608 VkCmdBuffer cmdBuffer
,
1609 VkBuffer destBuffer
,
1610 VkDeviceSize destOffset
,
1611 VkDeviceSize fillSize
,
1617 void anv_CmdClearColorImage(
1618 VkCmdBuffer cmdBuffer
,
1620 VkImageLayout imageLayout
,
1621 const VkClearColorValue
* pColor
,
1622 uint32_t rangeCount
,
1623 const VkImageSubresourceRange
* pRanges
)
1625 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
1626 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1627 struct anv_saved_state saved_state
;
1629 anv_cmd_buffer_save(cmd_buffer
, &saved_state
,
1630 (1 << VK_DYNAMIC_STATE_VIEWPORT
));
1631 cmd_buffer
->state
.dynamic
.viewport
.count
= 0;
1633 for (uint32_t r
= 0; r
< rangeCount
; r
++) {
1634 for (uint32_t l
= 0; l
< pRanges
[r
].mipLevels
; l
++) {
1635 for (uint32_t s
= 0; s
< pRanges
[r
].arraySize
; s
++) {
1636 struct anv_image_view iview
;
1637 anv_image_view_init(&iview
, cmd_buffer
->device
,
1638 &(VkImageViewCreateInfo
) {
1639 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1641 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1642 .format
= image
->format
->vk_format
,
1644 VK_CHANNEL_SWIZZLE_R
,
1645 VK_CHANNEL_SWIZZLE_G
,
1646 VK_CHANNEL_SWIZZLE_B
,
1647 VK_CHANNEL_SWIZZLE_A
1649 .subresourceRange
= {
1650 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1651 .baseMipLevel
= pRanges
[r
].baseMipLevel
+ l
,
1653 .baseArrayLayer
= pRanges
[r
].baseArrayLayer
+ s
,
1660 anv_CreateFramebuffer(anv_device_to_handle(cmd_buffer
->device
),
1661 &(VkFramebufferCreateInfo
) {
1662 .sType
= VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
,
1663 .attachmentCount
= 1,
1664 .pAttachments
= (VkImageView
[]) {
1665 anv_image_view_to_handle(&iview
),
1667 .width
= iview
.extent
.width
,
1668 .height
= iview
.extent
.height
,
1673 anv_CreateRenderPass(anv_device_to_handle(cmd_buffer
->device
),
1674 &(VkRenderPassCreateInfo
) {
1675 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
1676 .attachmentCount
= 1,
1677 .pAttachments
= &(VkAttachmentDescription
) {
1678 .sType
= VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION
,
1679 .format
= iview
.format
->vk_format
,
1680 .loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
1681 .storeOp
= VK_ATTACHMENT_STORE_OP_STORE
,
1682 .initialLayout
= VK_IMAGE_LAYOUT_GENERAL
,
1683 .finalLayout
= VK_IMAGE_LAYOUT_GENERAL
,
1686 .pSubpasses
= &(VkSubpassDescription
) {
1687 .sType
= VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION
,
1688 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
1691 .pColorAttachments
= &(VkAttachmentReference
) {
1693 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
1695 .pResolveAttachments
= NULL
,
1696 .depthStencilAttachment
= (VkAttachmentReference
) {
1697 .attachment
= VK_ATTACHMENT_UNUSED
,
1698 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
1701 .pPreserveAttachments
= &(VkAttachmentReference
) {
1703 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
1706 .dependencyCount
= 0,
1709 ANV_CALL(CmdBeginRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
),
1710 &(VkRenderPassBeginInfo
) {
1711 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO
,
1713 .offset
= { 0, 0, },
1715 .width
= iview
.extent
.width
,
1716 .height
= iview
.extent
.height
,
1721 .clearValueCount
= 1,
1722 .pClearValues
= NULL
,
1723 }, VK_RENDER_PASS_CONTENTS_INLINE
);
1725 struct clear_instance_data instance_data
= {
1734 meta_emit_clear(cmd_buffer
, 1, &instance_data
,
1735 (VkClearDepthStencilValue
) {0});
1737 ANV_CALL(CmdEndRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
));
1742 /* Restore API state */
1743 anv_cmd_buffer_restore(cmd_buffer
, &saved_state
);
1746 void anv_CmdClearDepthStencilImage(
1747 VkCmdBuffer cmdBuffer
,
1749 VkImageLayout imageLayout
,
1750 const VkClearDepthStencilValue
* pDepthStencil
,
1751 uint32_t rangeCount
,
1752 const VkImageSubresourceRange
* pRanges
)
1757 void anv_CmdClearColorAttachment(
1758 VkCmdBuffer cmdBuffer
,
1759 uint32_t colorAttachment
,
1760 VkImageLayout imageLayout
,
1761 const VkClearColorValue
* pColor
,
1763 const VkRect3D
* pRects
)
1768 void anv_CmdClearDepthStencilAttachment(
1769 VkCmdBuffer cmdBuffer
,
1770 VkImageAspectFlags aspectMask
,
1771 VkImageLayout imageLayout
,
1772 const VkClearDepthStencilValue
* pDepthStencil
,
1774 const VkRect3D
* pRects
)
1779 void anv_CmdResolveImage(
1780 VkCmdBuffer cmdBuffer
,
1782 VkImageLayout srcImageLayout
,
1784 VkImageLayout destImageLayout
,
1785 uint32_t regionCount
,
1786 const VkImageResolve
* pRegions
)
1792 anv_device_init_meta(struct anv_device
*device
)
1794 anv_device_init_meta_clear_state(device
);
1795 anv_device_init_meta_blit_state(device
);
1799 anv_device_finish_meta(struct anv_device
*device
)
1802 anv_DestroyPipeline(anv_device_to_handle(device
),
1803 device
->meta_state
.clear
.pipeline
);
1806 anv_DestroyPipeline(anv_device_to_handle(device
),
1807 device
->meta_state
.blit
.pipeline_2d_src
);
1808 anv_DestroyPipeline(anv_device_to_handle(device
),
1809 device
->meta_state
.blit
.pipeline_3d_src
);
1810 anv_DestroyPipelineLayout(anv_device_to_handle(device
),
1811 device
->meta_state
.blit
.pipeline_layout
);
1812 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device
),
1813 device
->meta_state
.blit
.ds_layout
);