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
, "a_pos",
45 pos_in
->data
.location
= VERT_ATTRIB_GENERIC0
;
46 nir_variable
*pos_out
= nir_variable_create(b
.shader
, "gl_Position",
49 pos_in
->data
.location
= VARYING_SLOT_POS
;
50 nir_copy_var(&b
, pos_out
, pos_in
);
52 /* Add one more pass-through attribute. For clear shaders, this is used
53 * to store the color and for blit shaders it's the texture coordinate.
55 const struct glsl_type
*attr_type
= glsl_vec4_type();
56 nir_variable
*attr_in
= nir_variable_create(b
.shader
, "a_attr", attr_type
,
58 attr_in
->data
.location
= VERT_ATTRIB_GENERIC1
;
59 nir_variable
*attr_out
= nir_variable_create(b
.shader
, "v_attr", attr_type
,
61 attr_out
->data
.location
= VARYING_SLOT_VAR0
;
62 attr_out
->data
.interpolation
= attr_flat
? INTERP_QUALIFIER_FLAT
:
63 INTERP_QUALIFIER_SMOOTH
;
64 nir_copy_var(&b
, attr_out
, attr_in
);
70 build_nir_clear_fragment_shader(void)
74 const struct glsl_type
*color_type
= glsl_vec4_type();
76 nir_builder_init_simple_shader(&b
, MESA_SHADER_FRAGMENT
);
78 nir_variable
*color_in
= nir_variable_create(b
.shader
, "v_attr",
81 color_in
->data
.location
= VARYING_SLOT_VAR0
;
82 color_in
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
83 nir_variable
*color_out
= nir_variable_create(b
.shader
, "f_color",
86 color_out
->data
.location
= FRAG_RESULT_DATA0
;
87 nir_copy_var(&b
, color_out
, color_in
);
93 build_nir_copy_fragment_shader(enum glsl_sampler_dim tex_dim
)
97 nir_builder_init_simple_shader(&b
, MESA_SHADER_FRAGMENT
);
99 const struct glsl_type
*color_type
= glsl_vec4_type();
101 nir_variable
*tex_pos_in
= nir_variable_create(b
.shader
, "v_attr",
104 tex_pos_in
->data
.location
= VARYING_SLOT_VAR0
;
106 const struct glsl_type
*sampler_type
=
107 glsl_sampler_type(tex_dim
, false, false, glsl_get_base_type(color_type
));
108 nir_variable
*sampler
= nir_variable_create(b
.shader
, "s_tex", sampler_type
,
110 sampler
->data
.descriptor_set
= 0;
111 sampler
->data
.binding
= 0;
113 nir_tex_instr
*tex
= nir_tex_instr_create(b
.shader
, 1);
114 tex
->sampler_dim
= tex_dim
;
115 tex
->op
= nir_texop_tex
;
116 tex
->src
[0].src_type
= nir_tex_src_coord
;
117 tex
->src
[0].src
= nir_src_for_ssa(nir_load_var(&b
, tex_pos_in
));
118 tex
->dest_type
= nir_type_float
; /* TODO */
121 case GLSL_SAMPLER_DIM_2D
:
122 tex
->coord_components
= 2;
124 case GLSL_SAMPLER_DIM_3D
:
125 tex
->coord_components
= 3;
128 assert(!"Unsupported texture dimension");
131 tex
->sampler
= nir_deref_var_create(tex
, sampler
);
133 nir_ssa_dest_init(&tex
->instr
, &tex
->dest
, 4, "tex");
134 nir_builder_instr_insert(&b
, &tex
->instr
);
136 nir_variable
*color_out
= nir_variable_create(b
.shader
, "f_color",
139 color_out
->data
.location
= FRAG_RESULT_DATA0
;
140 nir_store_var(&b
, color_out
, &tex
->dest
.ssa
);
146 anv_device_init_meta_clear_state(struct anv_device
*device
)
148 struct anv_shader_module vsm
= {
149 .nir
= build_nir_vertex_shader(true),
152 struct anv_shader_module fsm
= {
153 .nir
= build_nir_clear_fragment_shader(),
157 anv_CreateShader(anv_device_to_handle(device
),
158 &(VkShaderCreateInfo
) {
159 .sType
= VK_STRUCTURE_TYPE_SHADER_CREATE_INFO
,
160 .module
= anv_shader_module_to_handle(&vsm
),
165 anv_CreateShader(anv_device_to_handle(device
),
166 &(VkShaderCreateInfo
) {
167 .sType
= VK_STRUCTURE_TYPE_SHADER_CREATE_INFO
,
168 .module
= anv_shader_module_to_handle(&fsm
),
172 /* We use instanced rendering to clear multiple render targets. We have two
173 * vertex buffers: the first vertex buffer holds per-vertex data and
174 * provides the vertices for the clear rectangle. The second one holds
175 * per-instance data, which consists of the VUE header (which selects the
176 * layer) and the color (Vulkan supports per-RT clear colors).
178 VkPipelineVertexInputStateCreateInfo vi_create_info
= {
179 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
181 .pVertexBindingDescriptions
= (VkVertexInputBindingDescription
[]) {
185 .stepRate
= VK_VERTEX_INPUT_STEP_RATE_VERTEX
190 .stepRate
= VK_VERTEX_INPUT_STEP_RATE_INSTANCE
194 .pVertexAttributeDescriptions
= (VkVertexInputAttributeDescription
[]) {
199 .format
= VK_FORMAT_R32G32B32A32_UINT
,
206 .format
= VK_FORMAT_R32G32B32_SFLOAT
,
213 .format
= VK_FORMAT_R32G32B32A32_SFLOAT
,
219 anv_graphics_pipeline_create(anv_device_to_handle(device
),
220 &(VkGraphicsPipelineCreateInfo
) {
221 .sType
= VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
,
224 .pStages
= (VkPipelineShaderStageCreateInfo
[]) {
226 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
227 .stage
= VK_SHADER_STAGE_VERTEX
,
229 .pSpecializationInfo
= NULL
231 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
232 .stage
= VK_SHADER_STAGE_FRAGMENT
,
234 .pSpecializationInfo
= NULL
,
237 .pVertexInputState
= &vi_create_info
,
238 .pInputAssemblyState
= &(VkPipelineInputAssemblyStateCreateInfo
) {
239 .sType
= VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO
,
240 .topology
= VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
,
241 .primitiveRestartEnable
= false,
243 .pRasterState
= &(VkPipelineRasterStateCreateInfo
) {
244 .sType
= VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO
,
245 .depthClipEnable
= true,
246 .rasterizerDiscardEnable
= false,
247 .fillMode
= VK_FILL_MODE_SOLID
,
248 .cullMode
= VK_CULL_MODE_NONE
,
249 .frontFace
= VK_FRONT_FACE_CCW
251 .pDepthStencilState
= &(VkPipelineDepthStencilStateCreateInfo
) {
252 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO
,
253 .depthTestEnable
= true,
254 .depthWriteEnable
= true,
255 .depthCompareOp
= VK_COMPARE_OP_ALWAYS
,
256 .depthBoundsTestEnable
= false,
257 .stencilTestEnable
= true,
258 .front
= (VkStencilOpState
) {
259 .stencilPassOp
= VK_STENCIL_OP_REPLACE
,
260 .stencilCompareOp
= VK_COMPARE_OP_ALWAYS
,
262 .back
= (VkStencilOpState
) {
263 .stencilPassOp
= VK_STENCIL_OP_REPLACE
,
264 .stencilCompareOp
= VK_COMPARE_OP_ALWAYS
,
267 .pColorBlendState
= &(VkPipelineColorBlendStateCreateInfo
) {
268 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
269 .attachmentCount
= 1,
270 .pAttachments
= (VkPipelineColorBlendAttachmentState
[]) {
271 { .channelWriteMask
= VK_CHANNEL_A_BIT
|
272 VK_CHANNEL_R_BIT
| VK_CHANNEL_G_BIT
| VK_CHANNEL_B_BIT
},
277 &(struct anv_graphics_pipeline_create_info
) {
278 .use_repclear
= true,
279 .disable_viewport
= true,
283 &device
->meta_state
.clear
.pipeline
);
285 anv_DestroyShader(anv_device_to_handle(device
), vs
);
286 anv_DestroyShader(anv_device_to_handle(device
), fs
);
287 ralloc_free(vsm
.nir
);
288 ralloc_free(fsm
.nir
);
291 #define NUM_VB_USED 2
292 struct anv_saved_state
{
293 struct anv_vertex_binding old_vertex_bindings
[NUM_VB_USED
];
294 struct anv_descriptor_set
*old_descriptor_set0
;
295 struct anv_pipeline
*old_pipeline
;
296 struct anv_dynamic_ds_state
*old_ds_state
;
297 struct anv_dynamic_cb_state
*old_cb_state
;
301 anv_cmd_buffer_save(struct anv_cmd_buffer
*cmd_buffer
,
302 struct anv_saved_state
*state
)
304 state
->old_pipeline
= cmd_buffer
->state
.pipeline
;
305 state
->old_descriptor_set0
= cmd_buffer
->state
.descriptors
[0].set
;
306 memcpy(state
->old_vertex_bindings
, cmd_buffer
->state
.vertex_bindings
,
307 sizeof(state
->old_vertex_bindings
));
308 state
->old_ds_state
= cmd_buffer
->state
.ds_state
;
309 state
->old_cb_state
= cmd_buffer
->state
.cb_state
;
313 anv_cmd_buffer_restore(struct anv_cmd_buffer
*cmd_buffer
,
314 const struct anv_saved_state
*state
)
316 cmd_buffer
->state
.pipeline
= state
->old_pipeline
;
317 cmd_buffer
->state
.descriptors
[0].set
= state
->old_descriptor_set0
;
318 memcpy(cmd_buffer
->state
.vertex_bindings
, state
->old_vertex_bindings
,
319 sizeof(state
->old_vertex_bindings
));
321 cmd_buffer
->state
.vb_dirty
|= (1 << NUM_VB_USED
) - 1;
322 cmd_buffer
->state
.dirty
|= ANV_CMD_BUFFER_PIPELINE_DIRTY
;
323 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_VERTEX_BIT
;
325 if (cmd_buffer
->state
.ds_state
!= state
->old_ds_state
) {
326 cmd_buffer
->state
.ds_state
= state
->old_ds_state
;
327 cmd_buffer
->state
.dirty
|= ANV_CMD_BUFFER_DS_DIRTY
;
330 if (cmd_buffer
->state
.cb_state
!= state
->old_cb_state
) {
331 cmd_buffer
->state
.cb_state
= state
->old_cb_state
;
332 cmd_buffer
->state
.dirty
|= ANV_CMD_BUFFER_CB_DIRTY
;
339 uint32_t ViewportIndex
;
343 struct clear_instance_data
{
344 struct vue_header vue_header
;
345 VkClearColorValue color
;
349 meta_emit_clear(struct anv_cmd_buffer
*cmd_buffer
,
351 struct clear_instance_data
*instance_data
,
352 VkClearDepthStencilValue ds_clear_value
)
354 struct anv_device
*device
= cmd_buffer
->device
;
355 struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
356 struct anv_state state
;
359 const float vertex_data
[] = {
360 /* Rect-list coordinates */
361 0.0, 0.0, ds_clear_value
.depth
,
362 fb
->width
, 0.0, ds_clear_value
.depth
,
363 fb
->width
, fb
->height
, ds_clear_value
.depth
,
365 /* Align to 16 bytes */
369 size
= sizeof(vertex_data
) + num_instances
* sizeof(*instance_data
);
370 state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, size
, 16);
372 /* Copy in the vertex and instance data */
373 memcpy(state
.map
, vertex_data
, sizeof(vertex_data
));
374 memcpy(state
.map
+ sizeof(vertex_data
), instance_data
,
375 num_instances
* sizeof(*instance_data
));
377 struct anv_buffer vertex_buffer
= {
378 .device
= cmd_buffer
->device
,
380 .bo
= &device
->dynamic_state_block_pool
.bo
,
381 .offset
= state
.offset
384 anv_CmdBindVertexBuffers(anv_cmd_buffer_to_handle(cmd_buffer
), 0, 2,
386 anv_buffer_to_handle(&vertex_buffer
),
387 anv_buffer_to_handle(&vertex_buffer
)
394 if (cmd_buffer
->state
.pipeline
!= anv_pipeline_from_handle(device
->meta_state
.clear
.pipeline
))
395 anv_CmdBindPipeline(anv_cmd_buffer_to_handle(cmd_buffer
),
396 VK_PIPELINE_BIND_POINT_GRAPHICS
,
397 device
->meta_state
.clear
.pipeline
);
399 /* We don't need anything here, only set if not already set. */
400 if (cmd_buffer
->state
.rs_state
== NULL
)
401 anv_CmdBindDynamicRasterState(anv_cmd_buffer_to_handle(cmd_buffer
),
402 device
->meta_state
.shared
.rs_state
);
404 if (cmd_buffer
->state
.vp_state
== NULL
)
405 anv_CmdBindDynamicViewportState(anv_cmd_buffer_to_handle(cmd_buffer
),
406 cmd_buffer
->state
.framebuffer
->vp_state
);
408 if (cmd_buffer
->state
.ds_state
== NULL
)
409 anv_CmdBindDynamicDepthStencilState(anv_cmd_buffer_to_handle(cmd_buffer
),
410 device
->meta_state
.shared
.ds_state
);
412 if (cmd_buffer
->state
.cb_state
== NULL
)
413 anv_CmdBindDynamicColorBlendState(anv_cmd_buffer_to_handle(cmd_buffer
),
414 device
->meta_state
.shared
.cb_state
);
416 ANV_CALL(CmdDraw
)(anv_cmd_buffer_to_handle(cmd_buffer
),
417 3, num_instances
, 0, 0);
421 anv_cmd_buffer_clear_attachments(struct anv_cmd_buffer
*cmd_buffer
,
422 struct anv_render_pass
*pass
,
423 const VkClearValue
*clear_values
)
425 struct anv_saved_state saved_state
;
427 if (pass
->has_stencil_clear_attachment
)
428 anv_finishme("stencil clear");
430 /* FINISHME: Rethink how we count clear attachments in light of
431 * 0.138.2 -> 0.170.2 diff.
433 if (pass
->num_color_clear_attachments
== 0 &&
434 !pass
->has_depth_clear_attachment
)
437 struct clear_instance_data instance_data
[pass
->num_color_clear_attachments
];
438 uint32_t color_attachments
[pass
->num_color_clear_attachments
];
439 uint32_t ds_attachment
= VK_ATTACHMENT_UNUSED
;
440 VkClearDepthStencilValue ds_clear_value
= {0};
443 for (uint32_t i
= 0; i
< pass
->attachment_count
; i
++) {
444 const struct anv_render_pass_attachment
*att
= &pass
->attachments
[i
];
446 if (att
->load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
447 if (anv_format_is_color(att
->format
)) {
448 instance_data
[layer
] = (struct clear_instance_data
) {
454 .color
= clear_values
[i
].color
,
456 color_attachments
[layer
] = i
;
458 } else if (att
->format
->depth_format
) {
459 assert(ds_attachment
== VK_ATTACHMENT_UNUSED
);
461 ds_clear_value
= clear_values
[ds_attachment
].depthStencil
;
463 } else if (att
->stencil_load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
464 assert(att
->format
->has_stencil
);
465 anv_finishme("stencil clear");
469 anv_cmd_buffer_save(cmd_buffer
, &saved_state
);
471 struct anv_subpass subpass
= {
473 .color_count
= pass
->num_color_clear_attachments
,
474 .color_attachments
= color_attachments
,
475 .depth_stencil_attachment
= ds_attachment
,
478 anv_cmd_buffer_begin_subpass(cmd_buffer
, &subpass
);
480 meta_emit_clear(cmd_buffer
, pass
->num_color_clear_attachments
,
481 instance_data
, ds_clear_value
);
483 /* Restore API state */
484 anv_cmd_buffer_restore(cmd_buffer
, &saved_state
);
487 static VkImageViewType
488 meta_blit_get_src_image_view_type(const struct anv_image
*src_image
)
490 switch (src_image
->type
) {
491 case VK_IMAGE_TYPE_1D
:
492 return VK_IMAGE_VIEW_TYPE_1D
;
493 case VK_IMAGE_TYPE_2D
:
494 return VK_IMAGE_VIEW_TYPE_2D
;
495 case VK_IMAGE_TYPE_3D
:
496 return VK_IMAGE_VIEW_TYPE_3D
;
498 assert(!"bad VkImageType");
504 meta_blit_get_dest_view_base_array_slice(const struct anv_image
*dest_image
,
505 const VkImageSubresourceCopy
*dest_subresource
,
506 const VkOffset3D
*dest_offset
)
508 switch (dest_image
->type
) {
509 case VK_IMAGE_TYPE_1D
:
510 case VK_IMAGE_TYPE_2D
:
511 return dest_subresource
->arrayLayer
;
512 case VK_IMAGE_TYPE_3D
:
513 /* HACK: Vulkan does not allow attaching a 3D image to a framebuffer,
514 * but meta does it anyway. When doing so, we translate the
515 * destination's z offset into an array offset.
517 return dest_offset
->z
;
519 assert(!"bad VkImageType");
525 anv_device_init_meta_blit_state(struct anv_device
*device
)
527 /* We don't use a vertex shader for clearing, but instead build and pass
528 * the VUEs directly to the rasterization backend. However, we do need
529 * to provide GLSL source for the vertex shader so that the compiler
530 * does not dead-code our inputs.
532 struct anv_shader_module vsm
= {
533 .nir
= build_nir_vertex_shader(false),
536 struct anv_shader_module fsm_2d
= {
537 .nir
= build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_2D
),
540 struct anv_shader_module fsm_3d
= {
541 .nir
= build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_3D
),
545 anv_CreateShader(anv_device_to_handle(device
),
546 &(VkShaderCreateInfo
) {
547 .sType
= VK_STRUCTURE_TYPE_SHADER_CREATE_INFO
,
548 .module
= anv_shader_module_to_handle(&vsm
),
553 anv_CreateShader(anv_device_to_handle(device
),
554 &(VkShaderCreateInfo
) {
555 .sType
= VK_STRUCTURE_TYPE_SHADER_CREATE_INFO
,
556 .module
= anv_shader_module_to_handle(&fsm_2d
),
561 anv_CreateShader(anv_device_to_handle(device
),
562 &(VkShaderCreateInfo
) {
563 .sType
= VK_STRUCTURE_TYPE_SHADER_CREATE_INFO
,
564 .module
= anv_shader_module_to_handle(&fsm_3d
),
568 VkPipelineVertexInputStateCreateInfo vi_create_info
= {
569 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
571 .pVertexBindingDescriptions
= (VkVertexInputBindingDescription
[]) {
575 .stepRate
= VK_VERTEX_INPUT_STEP_RATE_VERTEX
579 .strideInBytes
= 5 * sizeof(float),
580 .stepRate
= VK_VERTEX_INPUT_STEP_RATE_VERTEX
584 .pVertexAttributeDescriptions
= (VkVertexInputAttributeDescription
[]) {
589 .format
= VK_FORMAT_R32G32B32A32_UINT
,
596 .format
= VK_FORMAT_R32G32_SFLOAT
,
600 /* Texture Coordinate */
603 .format
= VK_FORMAT_R32G32B32_SFLOAT
,
609 VkDescriptorSetLayoutCreateInfo ds_layout_info
= {
610 .sType
= VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
,
612 .pBinding
= (VkDescriptorSetLayoutBinding
[]) {
614 .descriptorType
= VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
,
616 .stageFlags
= VK_SHADER_STAGE_FRAGMENT_BIT
,
617 .pImmutableSamplers
= NULL
621 anv_CreateDescriptorSetLayout(anv_device_to_handle(device
), &ds_layout_info
,
622 &device
->meta_state
.blit
.ds_layout
);
624 anv_CreatePipelineLayout(anv_device_to_handle(device
),
625 &(VkPipelineLayoutCreateInfo
) {
626 .sType
= VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
,
627 .descriptorSetCount
= 1,
628 .pSetLayouts
= &device
->meta_state
.blit
.ds_layout
,
630 &device
->meta_state
.blit
.pipeline_layout
);
632 VkPipelineShaderStageCreateInfo pipeline_shader_stages
[] = {
634 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
635 .stage
= VK_SHADER_STAGE_VERTEX
,
637 .pSpecializationInfo
= NULL
639 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
640 .stage
= VK_SHADER_STAGE_FRAGMENT
,
641 .shader
= {0}, /* TEMPLATE VALUE! FILL ME IN! */
642 .pSpecializationInfo
= NULL
646 const VkGraphicsPipelineCreateInfo vk_pipeline_info
= {
647 .sType
= VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
,
648 .stageCount
= ARRAY_SIZE(pipeline_shader_stages
),
649 .pStages
= pipeline_shader_stages
,
650 .pVertexInputState
= &vi_create_info
,
651 .pInputAssemblyState
= &(VkPipelineInputAssemblyStateCreateInfo
) {
652 .sType
= VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO
,
653 .topology
= VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
,
654 .primitiveRestartEnable
= false,
656 .pRasterState
= &(VkPipelineRasterStateCreateInfo
) {
657 .sType
= VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO
,
658 .depthClipEnable
= true,
659 .rasterizerDiscardEnable
= false,
660 .fillMode
= VK_FILL_MODE_SOLID
,
661 .cullMode
= VK_CULL_MODE_NONE
,
662 .frontFace
= VK_FRONT_FACE_CCW
664 .pColorBlendState
= &(VkPipelineColorBlendStateCreateInfo
) {
665 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
666 .attachmentCount
= 1,
667 .pAttachments
= (VkPipelineColorBlendAttachmentState
[]) {
668 { .channelWriteMask
= VK_CHANNEL_A_BIT
|
669 VK_CHANNEL_R_BIT
| VK_CHANNEL_G_BIT
| VK_CHANNEL_B_BIT
},
673 .layout
= device
->meta_state
.blit
.pipeline_layout
,
676 const struct anv_graphics_pipeline_create_info anv_pipeline_info
= {
677 .use_repclear
= false,
678 .disable_viewport
= true,
679 .disable_scissor
= true,
684 pipeline_shader_stages
[1].shader
= fs_2d
;
685 anv_graphics_pipeline_create(anv_device_to_handle(device
),
686 &vk_pipeline_info
, &anv_pipeline_info
,
687 &device
->meta_state
.blit
.pipeline_2d_src
);
689 pipeline_shader_stages
[1].shader
= fs_3d
;
690 anv_graphics_pipeline_create(anv_device_to_handle(device
),
691 &vk_pipeline_info
, &anv_pipeline_info
,
692 &device
->meta_state
.blit
.pipeline_3d_src
);
694 anv_DestroyShader(anv_device_to_handle(device
), vs
);
695 anv_DestroyShader(anv_device_to_handle(device
), fs_2d
);
696 anv_DestroyShader(anv_device_to_handle(device
), fs_3d
);
697 ralloc_free(vsm
.nir
);
698 ralloc_free(fsm_2d
.nir
);
699 ralloc_free(fsm_3d
.nir
);
703 meta_prepare_blit(struct anv_cmd_buffer
*cmd_buffer
,
704 struct anv_saved_state
*saved_state
)
706 struct anv_device
*device
= cmd_buffer
->device
;
708 anv_cmd_buffer_save(cmd_buffer
, saved_state
);
710 /* We don't need anything here, only set if not already set. */
711 if (cmd_buffer
->state
.rs_state
== NULL
)
712 anv_CmdBindDynamicRasterState(anv_cmd_buffer_to_handle(cmd_buffer
),
713 device
->meta_state
.shared
.rs_state
);
714 if (cmd_buffer
->state
.ds_state
== NULL
)
715 anv_CmdBindDynamicDepthStencilState(anv_cmd_buffer_to_handle(cmd_buffer
),
716 device
->meta_state
.shared
.ds_state
);
718 anv_CmdBindDynamicColorBlendState(anv_cmd_buffer_to_handle(cmd_buffer
),
719 device
->meta_state
.shared
.cb_state
);
723 VkOffset3D src_offset
;
724 VkExtent3D src_extent
;
725 VkOffset3D dest_offset
;
726 VkExtent3D dest_extent
;
730 meta_emit_blit(struct anv_cmd_buffer
*cmd_buffer
,
731 struct anv_image
*src_image
,
732 struct anv_image_view
*src_iview
,
733 VkOffset3D src_offset
,
734 VkExtent3D src_extent
,
735 struct anv_image
*dest_image
,
736 struct anv_image_view
*dest_iview
,
737 VkOffset3D dest_offset
,
738 VkExtent3D dest_extent
)
740 struct anv_device
*device
= cmd_buffer
->device
;
741 VkDescriptorPool dummy_desc_pool
= { .handle
= 1 };
743 struct blit_vb_data
{
748 unsigned vb_size
= sizeof(struct vue_header
) + 3 * sizeof(*vb_data
);
750 struct anv_state vb_state
=
751 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, vb_size
, 16);
752 memset(vb_state
.map
, 0, sizeof(struct vue_header
));
753 vb_data
= vb_state
.map
+ sizeof(struct vue_header
);
755 vb_data
[0] = (struct blit_vb_data
) {
757 dest_offset
.x
+ dest_extent
.width
,
758 dest_offset
.y
+ dest_extent
.height
,
761 (float)(src_offset
.x
+ src_extent
.width
) / (float)src_iview
->extent
.width
,
762 (float)(src_offset
.y
+ src_extent
.height
) / (float)src_iview
->extent
.height
,
763 (float)(src_offset
.z
+ src_extent
.depth
) / (float)src_iview
->extent
.depth
,
767 vb_data
[1] = (struct blit_vb_data
) {
770 dest_offset
.y
+ dest_extent
.height
,
773 (float)src_offset
.x
/ (float)src_iview
->extent
.width
,
774 (float)(src_offset
.y
+ src_extent
.height
) / (float)src_iview
->extent
.height
,
775 (float)(src_offset
.z
+ src_extent
.depth
) / (float)src_iview
->extent
.depth
,
779 vb_data
[2] = (struct blit_vb_data
) {
785 (float)src_offset
.x
/ (float)src_iview
->extent
.width
,
786 (float)src_offset
.y
/ (float)src_iview
->extent
.height
,
787 (float)src_offset
.z
/ (float)src_iview
->extent
.depth
,
791 struct anv_buffer vertex_buffer
= {
794 .bo
= &device
->dynamic_state_block_pool
.bo
,
795 .offset
= vb_state
.offset
,
798 anv_CmdBindVertexBuffers(anv_cmd_buffer_to_handle(cmd_buffer
), 0, 2,
800 anv_buffer_to_handle(&vertex_buffer
),
801 anv_buffer_to_handle(&vertex_buffer
)
805 sizeof(struct vue_header
),
809 anv_AllocDescriptorSets(anv_device_to_handle(device
), dummy_desc_pool
,
810 VK_DESCRIPTOR_SET_USAGE_ONE_SHOT
,
811 1, &device
->meta_state
.blit
.ds_layout
, &set
);
812 anv_UpdateDescriptorSets(anv_device_to_handle(device
),
814 (VkWriteDescriptorSet
[]) {
816 .sType
= VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET
,
819 .destArrayElement
= 0,
821 .descriptorType
= VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
,
822 .pDescriptors
= (VkDescriptorInfo
[]) {
824 .imageView
= anv_image_view_to_handle(src_iview
),
825 .imageLayout
= VK_IMAGE_LAYOUT_GENERAL
832 anv_CreateFramebuffer(anv_device_to_handle(device
),
833 &(VkFramebufferCreateInfo
) {
834 .sType
= VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
,
835 .attachmentCount
= 1,
836 .pAttachments
= (VkImageView
[]) {
837 anv_image_view_to_handle(dest_iview
),
839 .width
= dest_iview
->extent
.width
,
840 .height
= dest_iview
->extent
.height
,
845 anv_CreateRenderPass(anv_device_to_handle(device
),
846 &(VkRenderPassCreateInfo
) {
847 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
848 .attachmentCount
= 1,
849 .pAttachments
= &(VkAttachmentDescription
) {
850 .sType
= VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION
,
851 .format
= dest_iview
->format
->vk_format
,
852 .loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
853 .storeOp
= VK_ATTACHMENT_STORE_OP_STORE
,
854 .initialLayout
= VK_IMAGE_LAYOUT_GENERAL
,
855 .finalLayout
= VK_IMAGE_LAYOUT_GENERAL
,
858 .pSubpasses
= &(VkSubpassDescription
) {
859 .sType
= VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION
,
860 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
863 .pColorAttachments
= &(VkAttachmentReference
) {
865 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
867 .pResolveAttachments
= NULL
,
868 .depthStencilAttachment
= (VkAttachmentReference
) {
869 .attachment
= VK_ATTACHMENT_UNUSED
,
870 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
873 .pPreserveAttachments
= &(VkAttachmentReference
) {
875 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
878 .dependencyCount
= 0,
881 ANV_CALL(CmdBeginRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
),
882 &(VkRenderPassBeginInfo
) {
883 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO
,
887 .offset
= { dest_offset
.x
, dest_offset
.y
},
888 .extent
= { dest_extent
.width
, dest_extent
.height
},
890 .clearValueCount
= 0,
891 .pClearValues
= NULL
,
892 }, VK_RENDER_PASS_CONTENTS_INLINE
);
896 switch (src_image
->type
) {
897 case VK_IMAGE_TYPE_1D
:
898 anv_finishme("VK_IMAGE_TYPE_1D");
899 pipeline
= device
->meta_state
.blit
.pipeline_2d_src
;
901 case VK_IMAGE_TYPE_2D
:
902 pipeline
= device
->meta_state
.blit
.pipeline_2d_src
;
904 case VK_IMAGE_TYPE_3D
:
905 pipeline
= device
->meta_state
.blit
.pipeline_3d_src
;
908 unreachable(!"bad VkImageType");
911 if (cmd_buffer
->state
.pipeline
!= anv_pipeline_from_handle(pipeline
)) {
912 anv_CmdBindPipeline(anv_cmd_buffer_to_handle(cmd_buffer
),
913 VK_PIPELINE_BIND_POINT_GRAPHICS
, pipeline
);
916 anv_CmdBindDynamicViewportState(anv_cmd_buffer_to_handle(cmd_buffer
),
917 anv_framebuffer_from_handle(fb
)->vp_state
);
919 anv_CmdBindDescriptorSets(anv_cmd_buffer_to_handle(cmd_buffer
),
920 VK_PIPELINE_BIND_POINT_GRAPHICS
,
921 device
->meta_state
.blit
.pipeline_layout
, 0, 1,
924 ANV_CALL(CmdDraw
)(anv_cmd_buffer_to_handle(cmd_buffer
), 3, 1, 0, 0);
926 ANV_CALL(CmdEndRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
));
928 /* At the point where we emit the draw call, all data from the
929 * descriptor sets, etc. has been used. We are free to delete it.
931 anv_descriptor_set_destroy(device
, anv_descriptor_set_from_handle(set
));
932 anv_DestroyFramebuffer(anv_device_to_handle(device
), fb
);
933 anv_DestroyRenderPass(anv_device_to_handle(device
), pass
);
937 meta_finish_blit(struct anv_cmd_buffer
*cmd_buffer
,
938 const struct anv_saved_state
*saved_state
)
940 anv_cmd_buffer_restore(cmd_buffer
, saved_state
);
944 vk_format_for_cpp(int cpp
)
947 case 1: return VK_FORMAT_R8_UINT
;
948 case 2: return VK_FORMAT_R8G8_UINT
;
949 case 3: return VK_FORMAT_R8G8B8_UINT
;
950 case 4: return VK_FORMAT_R8G8B8A8_UINT
;
951 case 6: return VK_FORMAT_R16G16B16_UINT
;
952 case 8: return VK_FORMAT_R16G16B16A16_UINT
;
953 case 12: return VK_FORMAT_R32G32B32_UINT
;
954 case 16: return VK_FORMAT_R32G32B32A32_UINT
;
956 unreachable("Invalid format cpp");
961 do_buffer_copy(struct anv_cmd_buffer
*cmd_buffer
,
962 struct anv_bo
*src
, uint64_t src_offset
,
963 struct anv_bo
*dest
, uint64_t dest_offset
,
964 int width
, int height
, VkFormat copy_format
)
966 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
968 VkImageCreateInfo image_info
= {
969 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
970 .imageType
= VK_IMAGE_TYPE_2D
,
971 .format
= copy_format
,
980 .tiling
= VK_IMAGE_TILING_LINEAR
,
986 image_info
.usage
= VK_IMAGE_USAGE_SAMPLED_BIT
;
987 anv_CreateImage(vk_device
, &image_info
, &src_image
);
990 image_info
.usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
991 anv_CreateImage(vk_device
, &image_info
, &dest_image
);
993 /* We could use a vk call to bind memory, but that would require
994 * creating a dummy memory object etc. so there's really no point.
996 anv_image_from_handle(src_image
)->bo
= src
;
997 anv_image_from_handle(src_image
)->offset
= src_offset
;
998 anv_image_from_handle(dest_image
)->bo
= dest
;
999 anv_image_from_handle(dest_image
)->offset
= dest_offset
;
1001 struct anv_image_view src_iview
;
1002 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1003 &(VkImageViewCreateInfo
) {
1004 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1006 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1007 .format
= copy_format
,
1009 VK_CHANNEL_SWIZZLE_R
,
1010 VK_CHANNEL_SWIZZLE_G
,
1011 VK_CHANNEL_SWIZZLE_B
,
1012 VK_CHANNEL_SWIZZLE_A
1014 .subresourceRange
= {
1015 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1018 .baseArrayLayer
= 0,
1024 struct anv_image_view dest_iview
;
1025 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1026 &(VkImageViewCreateInfo
) {
1027 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1028 .image
= dest_image
,
1029 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1030 .format
= copy_format
,
1032 .r
= VK_CHANNEL_SWIZZLE_R
,
1033 .g
= VK_CHANNEL_SWIZZLE_G
,
1034 .b
= VK_CHANNEL_SWIZZLE_B
,
1035 .a
= VK_CHANNEL_SWIZZLE_A
,
1037 .subresourceRange
= {
1038 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1041 .baseArrayLayer
= 0,
1047 meta_emit_blit(cmd_buffer
,
1048 anv_image_from_handle(src_image
),
1050 (VkOffset3D
) { 0, 0, 0 },
1051 (VkExtent3D
) { width
, height
, 1 },
1052 anv_image_from_handle(dest_image
),
1054 (VkOffset3D
) { 0, 0, 0 },
1055 (VkExtent3D
) { width
, height
, 1 });
1057 anv_DestroyImage(vk_device
, src_image
);
1058 anv_DestroyImage(vk_device
, dest_image
);
1061 void anv_CmdCopyBuffer(
1062 VkCmdBuffer cmdBuffer
,
1064 VkBuffer destBuffer
,
1065 uint32_t regionCount
,
1066 const VkBufferCopy
* pRegions
)
1068 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
1069 ANV_FROM_HANDLE(anv_buffer
, src_buffer
, srcBuffer
);
1070 ANV_FROM_HANDLE(anv_buffer
, dest_buffer
, destBuffer
);
1072 struct anv_saved_state saved_state
;
1074 meta_prepare_blit(cmd_buffer
, &saved_state
);
1076 for (unsigned r
= 0; r
< regionCount
; r
++) {
1077 uint64_t src_offset
= src_buffer
->offset
+ pRegions
[r
].srcOffset
;
1078 uint64_t dest_offset
= dest_buffer
->offset
+ pRegions
[r
].destOffset
;
1079 uint64_t copy_size
= pRegions
[r
].copySize
;
1081 /* First, we compute the biggest format that can be used with the
1082 * given offsets and size.
1086 int fs
= ffs(src_offset
) - 1;
1088 cpp
= MIN2(cpp
, 1 << fs
);
1089 assert(src_offset
% cpp
== 0);
1091 fs
= ffs(dest_offset
) - 1;
1093 cpp
= MIN2(cpp
, 1 << fs
);
1094 assert(dest_offset
% cpp
== 0);
1096 fs
= ffs(pRegions
[r
].copySize
) - 1;
1098 cpp
= MIN2(cpp
, 1 << fs
);
1099 assert(pRegions
[r
].copySize
% cpp
== 0);
1101 VkFormat copy_format
= vk_format_for_cpp(cpp
);
1103 /* This is maximum possible width/height our HW can handle */
1104 uint64_t max_surface_dim
= 1 << 14;
1106 /* First, we make a bunch of max-sized copies */
1107 uint64_t max_copy_size
= max_surface_dim
* max_surface_dim
* cpp
;
1108 while (copy_size
> max_copy_size
) {
1109 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
1110 dest_buffer
->bo
, dest_offset
,
1111 max_surface_dim
, max_surface_dim
, copy_format
);
1112 copy_size
-= max_copy_size
;
1113 src_offset
+= max_copy_size
;
1114 dest_offset
+= max_copy_size
;
1117 uint64_t height
= copy_size
/ (max_surface_dim
* cpp
);
1118 assert(height
< max_surface_dim
);
1120 uint64_t rect_copy_size
= height
* max_surface_dim
* cpp
;
1121 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
1122 dest_buffer
->bo
, dest_offset
,
1123 max_surface_dim
, height
, copy_format
);
1124 copy_size
-= rect_copy_size
;
1125 src_offset
+= rect_copy_size
;
1126 dest_offset
+= rect_copy_size
;
1129 if (copy_size
!= 0) {
1130 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
1131 dest_buffer
->bo
, dest_offset
,
1132 copy_size
/ cpp
, 1, copy_format
);
1136 meta_finish_blit(cmd_buffer
, &saved_state
);
1139 void anv_CmdCopyImage(
1140 VkCmdBuffer cmdBuffer
,
1142 VkImageLayout srcImageLayout
,
1144 VkImageLayout destImageLayout
,
1145 uint32_t regionCount
,
1146 const VkImageCopy
* pRegions
)
1148 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
1149 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
1150 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
1152 const VkImageViewType src_iview_type
=
1153 meta_blit_get_src_image_view_type(src_image
);
1155 struct anv_saved_state saved_state
;
1157 meta_prepare_blit(cmd_buffer
, &saved_state
);
1159 for (unsigned r
= 0; r
< regionCount
; r
++) {
1160 struct anv_image_view src_iview
;
1161 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1162 &(VkImageViewCreateInfo
) {
1163 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1165 .viewType
= src_iview_type
,
1166 .format
= src_image
->format
->vk_format
,
1168 VK_CHANNEL_SWIZZLE_R
,
1169 VK_CHANNEL_SWIZZLE_G
,
1170 VK_CHANNEL_SWIZZLE_B
,
1171 VK_CHANNEL_SWIZZLE_A
1173 .subresourceRange
= {
1174 .aspectMask
= 1 << pRegions
[r
].srcSubresource
.aspect
,
1175 .baseMipLevel
= pRegions
[r
].srcSubresource
.mipLevel
,
1177 .baseArrayLayer
= pRegions
[r
].srcSubresource
.arrayLayer
,
1183 const VkOffset3D dest_offset
= {
1184 .x
= pRegions
[r
].destOffset
.x
,
1185 .y
= pRegions
[r
].destOffset
.y
,
1189 const uint32_t dest_array_slice
=
1190 meta_blit_get_dest_view_base_array_slice(dest_image
,
1191 &pRegions
[r
].destSubresource
,
1192 &pRegions
[r
].destOffset
);
1194 if (pRegions
[r
].srcSubresource
.arraySize
> 1)
1195 anv_finishme("FINISHME: copy multiple array layers");
1197 if (pRegions
[r
].extent
.depth
> 1)
1198 anv_finishme("FINISHME: copy multiple depth layers");
1200 struct anv_image_view dest_iview
;
1201 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1202 &(VkImageViewCreateInfo
) {
1203 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1205 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1206 .format
= dest_image
->format
->vk_format
,
1208 VK_CHANNEL_SWIZZLE_R
,
1209 VK_CHANNEL_SWIZZLE_G
,
1210 VK_CHANNEL_SWIZZLE_B
,
1211 VK_CHANNEL_SWIZZLE_A
1213 .subresourceRange
= {
1214 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1215 .baseMipLevel
= pRegions
[r
].destSubresource
.mipLevel
,
1217 .baseArrayLayer
= dest_array_slice
,
1223 meta_emit_blit(cmd_buffer
,
1224 src_image
, &src_iview
,
1225 pRegions
[r
].srcOffset
,
1227 dest_image
, &dest_iview
,
1229 pRegions
[r
].extent
);
1232 meta_finish_blit(cmd_buffer
, &saved_state
);
1235 void anv_CmdBlitImage(
1236 VkCmdBuffer cmdBuffer
,
1238 VkImageLayout srcImageLayout
,
1240 VkImageLayout destImageLayout
,
1241 uint32_t regionCount
,
1242 const VkImageBlit
* pRegions
,
1246 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
1247 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
1248 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
1250 const VkImageViewType src_iview_type
=
1251 meta_blit_get_src_image_view_type(src_image
);
1253 struct anv_saved_state saved_state
;
1255 anv_finishme("respect VkTexFilter");
1257 meta_prepare_blit(cmd_buffer
, &saved_state
);
1259 for (unsigned r
= 0; r
< regionCount
; r
++) {
1260 struct anv_image_view src_iview
;
1261 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1262 &(VkImageViewCreateInfo
) {
1263 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1265 .viewType
= src_iview_type
,
1266 .format
= src_image
->format
->vk_format
,
1268 VK_CHANNEL_SWIZZLE_R
,
1269 VK_CHANNEL_SWIZZLE_G
,
1270 VK_CHANNEL_SWIZZLE_B
,
1271 VK_CHANNEL_SWIZZLE_A
1273 .subresourceRange
= {
1274 .aspectMask
= 1 << pRegions
[r
].srcSubresource
.aspect
,
1275 .baseMipLevel
= pRegions
[r
].srcSubresource
.mipLevel
,
1277 .baseArrayLayer
= pRegions
[r
].srcSubresource
.arrayLayer
,
1283 const VkOffset3D dest_offset
= {
1284 .x
= pRegions
[r
].destOffset
.x
,
1285 .y
= pRegions
[r
].destOffset
.y
,
1289 const uint32_t dest_array_slice
=
1290 meta_blit_get_dest_view_base_array_slice(dest_image
,
1291 &pRegions
[r
].destSubresource
,
1292 &pRegions
[r
].destOffset
);
1294 if (pRegions
[r
].srcSubresource
.arraySize
> 1)
1295 anv_finishme("FINISHME: copy multiple array layers");
1297 if (pRegions
[r
].destExtent
.depth
> 1)
1298 anv_finishme("FINISHME: copy multiple depth layers");
1300 struct anv_image_view dest_iview
;
1301 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1302 &(VkImageViewCreateInfo
) {
1303 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1305 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1306 .format
= dest_image
->format
->vk_format
,
1308 VK_CHANNEL_SWIZZLE_R
,
1309 VK_CHANNEL_SWIZZLE_G
,
1310 VK_CHANNEL_SWIZZLE_B
,
1311 VK_CHANNEL_SWIZZLE_A
1313 .subresourceRange
= {
1314 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1315 .baseMipLevel
= pRegions
[r
].destSubresource
.mipLevel
,
1317 .baseArrayLayer
= dest_array_slice
,
1323 meta_emit_blit(cmd_buffer
,
1324 src_image
, &src_iview
,
1325 pRegions
[r
].srcOffset
,
1326 pRegions
[r
].srcExtent
,
1327 dest_image
, &dest_iview
,
1329 pRegions
[r
].destExtent
);
1332 meta_finish_blit(cmd_buffer
, &saved_state
);
1336 make_image_for_buffer(VkDevice vk_device
, VkBuffer vk_buffer
, VkFormat format
,
1337 VkImageUsageFlags usage
,
1338 const VkBufferImageCopy
*copy
)
1340 ANV_FROM_HANDLE(anv_buffer
, buffer
, vk_buffer
);
1342 VkExtent3D extent
= copy
->imageExtent
;
1343 if (copy
->bufferRowLength
)
1344 extent
.width
= copy
->bufferRowLength
;
1345 if (copy
->bufferImageHeight
)
1346 extent
.height
= copy
->bufferImageHeight
;
1350 VkResult result
= anv_CreateImage(vk_device
,
1351 &(VkImageCreateInfo
) {
1352 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
1353 .imageType
= VK_IMAGE_TYPE_2D
,
1359 .tiling
= VK_IMAGE_TILING_LINEAR
,
1363 assert(result
== VK_SUCCESS
);
1365 ANV_FROM_HANDLE(anv_image
, image
, vk_image
);
1367 /* We could use a vk call to bind memory, but that would require
1368 * creating a dummy memory object etc. so there's really no point.
1370 image
->bo
= buffer
->bo
;
1371 image
->offset
= buffer
->offset
+ copy
->bufferOffset
;
1373 return anv_image_to_handle(image
);
1376 void anv_CmdCopyBufferToImage(
1377 VkCmdBuffer cmdBuffer
,
1380 VkImageLayout destImageLayout
,
1381 uint32_t regionCount
,
1382 const VkBufferImageCopy
* pRegions
)
1384 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
1385 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
1386 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
1387 const VkFormat orig_format
= dest_image
->format
->vk_format
;
1388 struct anv_saved_state saved_state
;
1390 meta_prepare_blit(cmd_buffer
, &saved_state
);
1392 for (unsigned r
= 0; r
< regionCount
; r
++) {
1393 VkFormat proxy_format
= orig_format
;
1394 VkImageAspect proxy_aspect
= pRegions
[r
].imageSubresource
.aspect
;
1396 if (orig_format
== VK_FORMAT_S8_UINT
) {
1397 proxy_format
= VK_FORMAT_R8_UINT
;
1398 proxy_aspect
= VK_IMAGE_ASPECT_COLOR
;
1401 VkImage srcImage
= make_image_for_buffer(vk_device
, srcBuffer
,
1402 proxy_format
, VK_IMAGE_USAGE_SAMPLED_BIT
, &pRegions
[r
]);
1404 struct anv_image_view src_iview
;
1405 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1406 &(VkImageViewCreateInfo
) {
1407 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1409 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1410 .format
= proxy_format
,
1412 VK_CHANNEL_SWIZZLE_R
,
1413 VK_CHANNEL_SWIZZLE_G
,
1414 VK_CHANNEL_SWIZZLE_B
,
1415 VK_CHANNEL_SWIZZLE_A
1417 .subresourceRange
= {
1418 .aspectMask
= 1 << proxy_aspect
,
1421 .baseArrayLayer
= 0,
1427 const VkOffset3D dest_offset
= {
1428 .x
= pRegions
[r
].imageOffset
.x
,
1429 .y
= pRegions
[r
].imageOffset
.y
,
1433 const uint32_t dest_array_slice
=
1434 meta_blit_get_dest_view_base_array_slice(dest_image
,
1435 &pRegions
[r
].imageSubresource
,
1436 &pRegions
[r
].imageOffset
);
1438 if (pRegions
[r
].imageExtent
.depth
> 1)
1439 anv_finishme("FINISHME: copy multiple depth layers");
1441 struct anv_image_view dest_iview
;
1442 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1443 &(VkImageViewCreateInfo
) {
1444 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1445 .image
= anv_image_to_handle(dest_image
),
1446 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1447 .format
= proxy_format
,
1449 VK_CHANNEL_SWIZZLE_R
,
1450 VK_CHANNEL_SWIZZLE_G
,
1451 VK_CHANNEL_SWIZZLE_B
,
1452 VK_CHANNEL_SWIZZLE_A
1454 .subresourceRange
= {
1455 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1456 .baseMipLevel
= pRegions
[r
].imageSubresource
.mipLevel
,
1458 .baseArrayLayer
= dest_array_slice
,
1464 meta_emit_blit(cmd_buffer
,
1465 anv_image_from_handle(srcImage
),
1467 (VkOffset3D
) { 0, 0, 0 },
1468 pRegions
[r
].imageExtent
,
1472 pRegions
[r
].imageExtent
);
1474 anv_DestroyImage(vk_device
, srcImage
);
1477 meta_finish_blit(cmd_buffer
, &saved_state
);
1480 void anv_CmdCopyImageToBuffer(
1481 VkCmdBuffer cmdBuffer
,
1483 VkImageLayout srcImageLayout
,
1484 VkBuffer destBuffer
,
1485 uint32_t regionCount
,
1486 const VkBufferImageCopy
* pRegions
)
1488 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
1489 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
1490 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
1491 struct anv_saved_state saved_state
;
1493 const VkImageViewType src_iview_type
=
1494 meta_blit_get_src_image_view_type(src_image
);
1496 meta_prepare_blit(cmd_buffer
, &saved_state
);
1498 for (unsigned r
= 0; r
< regionCount
; r
++) {
1499 if (pRegions
[r
].imageSubresource
.arraySize
> 1)
1500 anv_finishme("FINISHME: copy multiple array layers");
1502 if (pRegions
[r
].imageExtent
.depth
> 1)
1503 anv_finishme("FINISHME: copy multiple depth layers");
1505 struct anv_image_view src_iview
;
1506 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1507 &(VkImageViewCreateInfo
) {
1508 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1510 .viewType
= src_iview_type
,
1511 .format
= src_image
->format
->vk_format
,
1513 VK_CHANNEL_SWIZZLE_R
,
1514 VK_CHANNEL_SWIZZLE_G
,
1515 VK_CHANNEL_SWIZZLE_B
,
1516 VK_CHANNEL_SWIZZLE_A
1518 .subresourceRange
= {
1519 .aspectMask
= 1 << pRegions
[r
].imageSubresource
.aspect
,
1520 .baseMipLevel
= pRegions
[r
].imageSubresource
.mipLevel
,
1522 .baseArrayLayer
= pRegions
[r
].imageSubresource
.arrayLayer
,
1528 VkFormat dest_format
= src_image
->format
->vk_format
;
1529 if (dest_format
== VK_FORMAT_S8_UINT
) {
1530 dest_format
= VK_FORMAT_R8_UINT
;
1533 VkImage destImage
= make_image_for_buffer(vk_device
, destBuffer
,
1534 dest_format
, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
, &pRegions
[r
]);
1536 struct anv_image_view dest_iview
;
1537 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1538 &(VkImageViewCreateInfo
) {
1539 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1541 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1542 .format
= dest_format
,
1544 VK_CHANNEL_SWIZZLE_R
,
1545 VK_CHANNEL_SWIZZLE_G
,
1546 VK_CHANNEL_SWIZZLE_B
,
1547 VK_CHANNEL_SWIZZLE_A
1549 .subresourceRange
= {
1550 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1553 .baseArrayLayer
= 0,
1559 meta_emit_blit(cmd_buffer
,
1560 anv_image_from_handle(srcImage
),
1562 pRegions
[r
].imageOffset
,
1563 pRegions
[r
].imageExtent
,
1564 anv_image_from_handle(destImage
),
1566 (VkOffset3D
) { 0, 0, 0 },
1567 pRegions
[r
].imageExtent
);
1569 anv_DestroyImage(vk_device
, destImage
);
1572 meta_finish_blit(cmd_buffer
, &saved_state
);
1575 void anv_CmdUpdateBuffer(
1576 VkCmdBuffer cmdBuffer
,
1577 VkBuffer destBuffer
,
1578 VkDeviceSize destOffset
,
1579 VkDeviceSize dataSize
,
1580 const uint32_t* pData
)
1585 void anv_CmdFillBuffer(
1586 VkCmdBuffer cmdBuffer
,
1587 VkBuffer destBuffer
,
1588 VkDeviceSize destOffset
,
1589 VkDeviceSize fillSize
,
1595 void anv_CmdClearColorImage(
1596 VkCmdBuffer cmdBuffer
,
1598 VkImageLayout imageLayout
,
1599 const VkClearColorValue
* pColor
,
1600 uint32_t rangeCount
,
1601 const VkImageSubresourceRange
* pRanges
)
1603 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
1604 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1605 struct anv_saved_state saved_state
;
1607 anv_cmd_buffer_save(cmd_buffer
, &saved_state
);
1609 for (uint32_t r
= 0; r
< rangeCount
; r
++) {
1610 for (uint32_t l
= 0; l
< pRanges
[r
].mipLevels
; l
++) {
1611 for (uint32_t s
= 0; s
< pRanges
[r
].arraySize
; s
++) {
1612 struct anv_image_view iview
;
1613 anv_image_view_init(&iview
, cmd_buffer
->device
,
1614 &(VkImageViewCreateInfo
) {
1615 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1617 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1618 .format
= image
->format
->vk_format
,
1620 VK_CHANNEL_SWIZZLE_R
,
1621 VK_CHANNEL_SWIZZLE_G
,
1622 VK_CHANNEL_SWIZZLE_B
,
1623 VK_CHANNEL_SWIZZLE_A
1625 .subresourceRange
= {
1626 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1627 .baseMipLevel
= pRanges
[r
].baseMipLevel
+ l
,
1629 .baseArrayLayer
= pRanges
[r
].baseArrayLayer
+ s
,
1636 anv_CreateFramebuffer(anv_device_to_handle(cmd_buffer
->device
),
1637 &(VkFramebufferCreateInfo
) {
1638 .sType
= VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
,
1639 .attachmentCount
= 1,
1640 .pAttachments
= (VkImageView
[]) {
1641 anv_image_view_to_handle(&iview
),
1643 .width
= iview
.extent
.width
,
1644 .height
= iview
.extent
.height
,
1649 anv_CreateRenderPass(anv_device_to_handle(cmd_buffer
->device
),
1650 &(VkRenderPassCreateInfo
) {
1651 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
1652 .attachmentCount
= 1,
1653 .pAttachments
= &(VkAttachmentDescription
) {
1654 .sType
= VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION
,
1655 .format
= iview
.format
->vk_format
,
1656 .loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
1657 .storeOp
= VK_ATTACHMENT_STORE_OP_STORE
,
1658 .initialLayout
= VK_IMAGE_LAYOUT_GENERAL
,
1659 .finalLayout
= VK_IMAGE_LAYOUT_GENERAL
,
1662 .pSubpasses
= &(VkSubpassDescription
) {
1663 .sType
= VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION
,
1664 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
1667 .pColorAttachments
= &(VkAttachmentReference
) {
1669 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
1671 .pResolveAttachments
= NULL
,
1672 .depthStencilAttachment
= (VkAttachmentReference
) {
1673 .attachment
= VK_ATTACHMENT_UNUSED
,
1674 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
1677 .pPreserveAttachments
= &(VkAttachmentReference
) {
1679 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
1682 .dependencyCount
= 0,
1685 ANV_CALL(CmdBeginRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
),
1686 &(VkRenderPassBeginInfo
) {
1687 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO
,
1689 .offset
= { 0, 0, },
1691 .width
= iview
.extent
.width
,
1692 .height
= iview
.extent
.height
,
1697 .clearValueCount
= 1,
1698 .pClearValues
= NULL
,
1699 }, VK_RENDER_PASS_CONTENTS_INLINE
);
1701 struct clear_instance_data instance_data
= {
1710 meta_emit_clear(cmd_buffer
, 1, &instance_data
,
1711 (VkClearDepthStencilValue
) {0});
1713 ANV_CALL(CmdEndRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
));
1718 /* Restore API state */
1719 anv_cmd_buffer_restore(cmd_buffer
, &saved_state
);
1722 void anv_CmdClearDepthStencilImage(
1723 VkCmdBuffer cmdBuffer
,
1725 VkImageLayout imageLayout
,
1726 const VkClearDepthStencilValue
* pDepthStencil
,
1727 uint32_t rangeCount
,
1728 const VkImageSubresourceRange
* pRanges
)
1733 void anv_CmdClearColorAttachment(
1734 VkCmdBuffer cmdBuffer
,
1735 uint32_t colorAttachment
,
1736 VkImageLayout imageLayout
,
1737 const VkClearColorValue
* pColor
,
1739 const VkRect3D
* pRects
)
1744 void anv_CmdClearDepthStencilAttachment(
1745 VkCmdBuffer cmdBuffer
,
1746 VkImageAspectFlags aspectMask
,
1747 VkImageLayout imageLayout
,
1748 const VkClearDepthStencilValue
* pDepthStencil
,
1750 const VkRect3D
* pRects
)
1755 void anv_CmdResolveImage(
1756 VkCmdBuffer cmdBuffer
,
1758 VkImageLayout srcImageLayout
,
1760 VkImageLayout destImageLayout
,
1761 uint32_t regionCount
,
1762 const VkImageResolve
* pRegions
)
1768 anv_device_init_meta(struct anv_device
*device
)
1770 anv_device_init_meta_clear_state(device
);
1771 anv_device_init_meta_blit_state(device
);
1773 ANV_CALL(CreateDynamicRasterState
)(anv_device_to_handle(device
),
1774 &(VkDynamicRasterStateCreateInfo
) {
1775 .sType
= VK_STRUCTURE_TYPE_DYNAMIC_RASTER_STATE_CREATE_INFO
,
1777 &device
->meta_state
.shared
.rs_state
);
1779 ANV_CALL(CreateDynamicColorBlendState
)(anv_device_to_handle(device
),
1780 &(VkDynamicColorBlendStateCreateInfo
) {
1781 .sType
= VK_STRUCTURE_TYPE_DYNAMIC_COLOR_BLEND_STATE_CREATE_INFO
1783 &device
->meta_state
.shared
.cb_state
);
1785 ANV_CALL(CreateDynamicDepthStencilState
)(anv_device_to_handle(device
),
1786 &(VkDynamicDepthStencilStateCreateInfo
) {
1787 .sType
= VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO
1789 &device
->meta_state
.shared
.ds_state
);
1793 anv_device_finish_meta(struct anv_device
*device
)
1796 anv_DestroyPipeline(anv_device_to_handle(device
),
1797 device
->meta_state
.clear
.pipeline
);
1800 anv_DestroyPipeline(anv_device_to_handle(device
),
1801 device
->meta_state
.blit
.pipeline_2d_src
);
1802 anv_DestroyPipeline(anv_device_to_handle(device
),
1803 device
->meta_state
.blit
.pipeline_3d_src
);
1804 anv_DestroyPipelineLayout(anv_device_to_handle(device
),
1805 device
->meta_state
.blit
.pipeline_layout
);
1806 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device
),
1807 device
->meta_state
.blit
.ds_layout
);
1810 anv_DestroyDynamicRasterState(anv_device_to_handle(device
),
1811 device
->meta_state
.shared
.rs_state
);
1812 anv_DestroyDynamicColorBlendState(anv_device_to_handle(device
),
1813 device
->meta_state
.shared
.cb_state
);
1814 anv_DestroyDynamicDepthStencilState(anv_device_to_handle(device
),
1815 device
->meta_state
.shared
.ds_state
);