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"
33 struct anv_render_pass anv_meta_dummy_renderpass
= {0};
36 build_nir_vertex_shader(bool attr_flat
)
40 const struct glsl_type
*vertex_type
= glsl_vec4_type();
42 nir_builder_init_simple_shader(&b
, MESA_SHADER_VERTEX
);
44 nir_variable
*pos_in
= nir_variable_create(b
.shader
, nir_var_shader_in
,
45 vertex_type
, "a_pos");
46 pos_in
->data
.location
= VERT_ATTRIB_GENERIC0
;
47 nir_variable
*pos_out
= nir_variable_create(b
.shader
, nir_var_shader_out
,
48 vertex_type
, "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
, nir_var_shader_in
,
58 attr_in
->data
.location
= VERT_ATTRIB_GENERIC1
;
59 nir_variable
*attr_out
= nir_variable_create(b
.shader
, nir_var_shader_out
,
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
, nir_var_shader_in
,
79 color_type
, "v_attr");
80 color_in
->data
.location
= VARYING_SLOT_VAR0
;
81 color_in
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
82 nir_variable
*color_out
= nir_variable_create(b
.shader
, nir_var_shader_out
,
83 color_type
, "f_color");
84 color_out
->data
.location
= FRAG_RESULT_DATA0
;
85 nir_copy_var(&b
, color_out
, color_in
);
91 build_nir_copy_fragment_shader(enum glsl_sampler_dim tex_dim
)
95 nir_builder_init_simple_shader(&b
, MESA_SHADER_FRAGMENT
);
97 const struct glsl_type
*color_type
= glsl_vec4_type();
99 nir_variable
*tex_pos_in
= nir_variable_create(b
.shader
, nir_var_shader_in
,
100 glsl_vec4_type(), "v_attr");
101 tex_pos_in
->data
.location
= VARYING_SLOT_VAR0
;
103 const struct glsl_type
*sampler_type
=
104 glsl_sampler_type(tex_dim
, false, false, glsl_get_base_type(color_type
));
105 nir_variable
*sampler
= nir_variable_create(b
.shader
, nir_var_uniform
,
106 sampler_type
, "s_tex");
107 sampler
->data
.descriptor_set
= 0;
108 sampler
->data
.binding
= 0;
110 nir_tex_instr
*tex
= nir_tex_instr_create(b
.shader
, 1);
111 tex
->sampler_dim
= tex_dim
;
112 tex
->op
= nir_texop_tex
;
113 tex
->src
[0].src_type
= nir_tex_src_coord
;
114 tex
->src
[0].src
= nir_src_for_ssa(nir_load_var(&b
, tex_pos_in
));
115 tex
->dest_type
= nir_type_float
; /* TODO */
118 case GLSL_SAMPLER_DIM_2D
:
119 tex
->coord_components
= 2;
121 case GLSL_SAMPLER_DIM_3D
:
122 tex
->coord_components
= 3;
125 assert(!"Unsupported texture dimension");
128 tex
->sampler
= nir_deref_var_create(tex
, sampler
);
130 nir_ssa_dest_init(&tex
->instr
, &tex
->dest
, 4, "tex");
131 nir_builder_instr_insert(&b
, &tex
->instr
);
133 nir_variable
*color_out
= nir_variable_create(b
.shader
, nir_var_shader_out
,
134 color_type
, "f_color");
135 color_out
->data
.location
= FRAG_RESULT_DATA0
;
136 nir_store_var(&b
, color_out
, &tex
->dest
.ssa
);
142 anv_device_init_meta_clear_state(struct anv_device
*device
)
144 struct anv_shader_module vsm
= {
145 .nir
= build_nir_vertex_shader(true),
148 struct anv_shader_module fsm
= {
149 .nir
= build_nir_clear_fragment_shader(),
153 anv_CreateShader(anv_device_to_handle(device
),
154 &(VkShaderCreateInfo
) {
155 .sType
= VK_STRUCTURE_TYPE_SHADER_CREATE_INFO
,
156 .module
= anv_shader_module_to_handle(&vsm
),
161 anv_CreateShader(anv_device_to_handle(device
),
162 &(VkShaderCreateInfo
) {
163 .sType
= VK_STRUCTURE_TYPE_SHADER_CREATE_INFO
,
164 .module
= anv_shader_module_to_handle(&fsm
),
168 /* We use instanced rendering to clear multiple render targets. We have two
169 * vertex buffers: the first vertex buffer holds per-vertex data and
170 * provides the vertices for the clear rectangle. The second one holds
171 * per-instance data, which consists of the VUE header (which selects the
172 * layer) and the color (Vulkan supports per-RT clear colors).
174 VkPipelineVertexInputStateCreateInfo vi_create_info
= {
175 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
177 .pVertexBindingDescriptions
= (VkVertexInputBindingDescription
[]) {
181 .stepRate
= VK_VERTEX_INPUT_STEP_RATE_VERTEX
186 .stepRate
= VK_VERTEX_INPUT_STEP_RATE_INSTANCE
190 .pVertexAttributeDescriptions
= (VkVertexInputAttributeDescription
[]) {
195 .format
= VK_FORMAT_R32G32B32A32_UINT
,
202 .format
= VK_FORMAT_R32G32B32_SFLOAT
,
209 .format
= VK_FORMAT_R32G32B32A32_SFLOAT
,
215 anv_graphics_pipeline_create(anv_device_to_handle(device
),
216 &(VkGraphicsPipelineCreateInfo
) {
217 .sType
= VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
,
220 .pStages
= (VkPipelineShaderStageCreateInfo
[]) {
222 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
223 .stage
= VK_SHADER_STAGE_VERTEX
,
225 .pSpecializationInfo
= NULL
227 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
228 .stage
= VK_SHADER_STAGE_FRAGMENT
,
230 .pSpecializationInfo
= NULL
,
233 .pVertexInputState
= &vi_create_info
,
234 .pInputAssemblyState
= &(VkPipelineInputAssemblyStateCreateInfo
) {
235 .sType
= VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO
,
236 .topology
= VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
,
237 .primitiveRestartEnable
= false,
239 .pViewportState
= &(VkPipelineViewportStateCreateInfo
) {
240 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO
,
244 .pRasterState
= &(VkPipelineRasterStateCreateInfo
) {
245 .sType
= VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO
,
246 .depthClipEnable
= true,
247 .rasterizerDiscardEnable
= false,
248 .fillMode
= VK_FILL_MODE_SOLID
,
249 .cullMode
= VK_CULL_MODE_NONE
,
250 .frontFace
= VK_FRONT_FACE_CCW
252 .pMultisampleState
= &(VkPipelineMultisampleStateCreateInfo
) {
253 .sType
= VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO
,
255 .sampleShadingEnable
= false,
256 .pSampleMask
= (VkSampleMask
[]) { UINT32_MAX
},
258 .pDepthStencilState
= &(VkPipelineDepthStencilStateCreateInfo
) {
259 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO
,
260 .depthTestEnable
= true,
261 .depthWriteEnable
= true,
262 .depthCompareOp
= VK_COMPARE_OP_ALWAYS
,
263 .depthBoundsTestEnable
= false,
264 .stencilTestEnable
= true,
265 .front
= (VkStencilOpState
) {
266 .stencilPassOp
= VK_STENCIL_OP_REPLACE
,
267 .stencilCompareOp
= VK_COMPARE_OP_ALWAYS
,
269 .back
= (VkStencilOpState
) {
270 .stencilPassOp
= VK_STENCIL_OP_REPLACE
,
271 .stencilCompareOp
= VK_COMPARE_OP_ALWAYS
,
274 .pColorBlendState
= &(VkPipelineColorBlendStateCreateInfo
) {
275 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
276 .attachmentCount
= 1,
277 .pAttachments
= (VkPipelineColorBlendAttachmentState
[]) {
278 { .channelWriteMask
= VK_CHANNEL_A_BIT
|
279 VK_CHANNEL_R_BIT
| VK_CHANNEL_G_BIT
| VK_CHANNEL_B_BIT
},
282 .pDynamicState
= &(VkPipelineDynamicStateCreateInfo
) {
283 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO
,
284 .dynamicStateCount
= 9,
285 .pDynamicStates
= (VkDynamicState
[]) {
286 VK_DYNAMIC_STATE_VIEWPORT
,
287 VK_DYNAMIC_STATE_SCISSOR
,
288 VK_DYNAMIC_STATE_LINE_WIDTH
,
289 VK_DYNAMIC_STATE_DEPTH_BIAS
,
290 VK_DYNAMIC_STATE_BLEND_CONSTANTS
,
291 VK_DYNAMIC_STATE_DEPTH_BOUNDS
,
292 VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
,
293 VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
,
294 VK_DYNAMIC_STATE_STENCIL_REFERENCE
,
298 .renderPass
= anv_render_pass_to_handle(&anv_meta_dummy_renderpass
),
301 &(struct anv_graphics_pipeline_create_info
) {
302 .use_repclear
= true,
303 .disable_viewport
= true,
307 &device
->meta_state
.clear
.pipeline
);
309 anv_DestroyShader(anv_device_to_handle(device
), vs
);
310 anv_DestroyShader(anv_device_to_handle(device
), fs
);
311 ralloc_free(vsm
.nir
);
312 ralloc_free(fsm
.nir
);
315 #define NUM_VB_USED 2
316 struct anv_saved_state
{
317 struct anv_vertex_binding old_vertex_bindings
[NUM_VB_USED
];
318 struct anv_descriptor_set
*old_descriptor_set0
;
319 struct anv_pipeline
*old_pipeline
;
320 uint32_t dynamic_flags
;
321 struct anv_dynamic_state dynamic
;
325 anv_cmd_buffer_save(struct anv_cmd_buffer
*cmd_buffer
,
326 struct anv_saved_state
*state
,
327 uint32_t dynamic_state
)
329 state
->old_pipeline
= cmd_buffer
->state
.pipeline
;
330 state
->old_descriptor_set0
= cmd_buffer
->state
.descriptors
[0];
331 memcpy(state
->old_vertex_bindings
, cmd_buffer
->state
.vertex_bindings
,
332 sizeof(state
->old_vertex_bindings
));
333 state
->dynamic_flags
= dynamic_state
;
334 anv_dynamic_state_copy(&state
->dynamic
, &cmd_buffer
->state
.dynamic
,
339 anv_cmd_buffer_restore(struct anv_cmd_buffer
*cmd_buffer
,
340 const struct anv_saved_state
*state
)
342 cmd_buffer
->state
.pipeline
= state
->old_pipeline
;
343 cmd_buffer
->state
.descriptors
[0] = state
->old_descriptor_set0
;
344 memcpy(cmd_buffer
->state
.vertex_bindings
, state
->old_vertex_bindings
,
345 sizeof(state
->old_vertex_bindings
));
347 cmd_buffer
->state
.vb_dirty
|= (1 << NUM_VB_USED
) - 1;
348 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_PIPELINE
;
349 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_VERTEX_BIT
;
351 anv_dynamic_state_copy(&cmd_buffer
->state
.dynamic
, &state
->dynamic
,
352 state
->dynamic_flags
);
353 cmd_buffer
->state
.dirty
|= state
->dynamic_flags
;
359 uint32_t ViewportIndex
;
363 struct clear_instance_data
{
364 struct vue_header vue_header
;
365 VkClearColorValue color
;
369 meta_emit_clear(struct anv_cmd_buffer
*cmd_buffer
,
371 struct clear_instance_data
*instance_data
,
372 VkClearDepthStencilValue ds_clear_value
)
374 struct anv_device
*device
= cmd_buffer
->device
;
375 struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
376 struct anv_state state
;
379 const float vertex_data
[] = {
380 /* Rect-list coordinates */
381 0.0, 0.0, ds_clear_value
.depth
,
382 fb
->width
, 0.0, ds_clear_value
.depth
,
383 fb
->width
, fb
->height
, ds_clear_value
.depth
,
385 /* Align to 16 bytes */
389 size
= sizeof(vertex_data
) + num_instances
* sizeof(*instance_data
);
390 state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, size
, 16);
392 /* Copy in the vertex and instance data */
393 memcpy(state
.map
, vertex_data
, sizeof(vertex_data
));
394 memcpy(state
.map
+ sizeof(vertex_data
), instance_data
,
395 num_instances
* sizeof(*instance_data
));
397 struct anv_buffer vertex_buffer
= {
398 .device
= cmd_buffer
->device
,
400 .bo
= &device
->dynamic_state_block_pool
.bo
,
401 .offset
= state
.offset
404 anv_CmdBindVertexBuffers(anv_cmd_buffer_to_handle(cmd_buffer
), 0, 2,
406 anv_buffer_to_handle(&vertex_buffer
),
407 anv_buffer_to_handle(&vertex_buffer
)
414 if (cmd_buffer
->state
.pipeline
!= anv_pipeline_from_handle(device
->meta_state
.clear
.pipeline
))
415 anv_CmdBindPipeline(anv_cmd_buffer_to_handle(cmd_buffer
),
416 VK_PIPELINE_BIND_POINT_GRAPHICS
,
417 device
->meta_state
.clear
.pipeline
);
419 ANV_CALL(CmdDraw
)(anv_cmd_buffer_to_handle(cmd_buffer
),
420 3, num_instances
, 0, 0);
424 anv_cmd_buffer_clear_attachments(struct anv_cmd_buffer
*cmd_buffer
,
425 struct anv_render_pass
*pass
,
426 const VkClearValue
*clear_values
)
428 struct anv_saved_state saved_state
;
430 if (pass
->has_stencil_clear_attachment
)
431 anv_finishme("stencil clear");
433 /* FINISHME: Rethink how we count clear attachments in light of
434 * 0.138.2 -> 0.170.2 diff.
436 if (pass
->num_color_clear_attachments
== 0 &&
437 !pass
->has_depth_clear_attachment
)
440 struct clear_instance_data instance_data
[pass
->num_color_clear_attachments
];
441 uint32_t color_attachments
[pass
->num_color_clear_attachments
];
442 uint32_t ds_attachment
= VK_ATTACHMENT_UNUSED
;
443 VkClearDepthStencilValue ds_clear_value
= {0};
446 for (uint32_t i
= 0; i
< pass
->attachment_count
; i
++) {
447 const struct anv_render_pass_attachment
*att
= &pass
->attachments
[i
];
449 if (anv_format_is_color(att
->format
)) {
450 if (att
->load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
451 instance_data
[layer
] = (struct clear_instance_data
) {
457 .color
= clear_values
[i
].color
,
459 color_attachments
[layer
] = i
;
463 if (att
->format
->depth_format
&&
464 att
->load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
465 assert(ds_attachment
== VK_ATTACHMENT_UNUSED
);
467 ds_clear_value
= clear_values
[ds_attachment
].depthStencil
;
470 if (att
->format
->has_stencil
&&
471 att
->stencil_load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
472 anv_finishme("stencil clear");
477 anv_cmd_buffer_save(cmd_buffer
, &saved_state
,
478 (1 << VK_DYNAMIC_STATE_VIEWPORT
));
479 cmd_buffer
->state
.dynamic
.viewport
.count
= 0;
481 struct anv_subpass subpass
= {
483 .color_count
= pass
->num_color_clear_attachments
,
484 .color_attachments
= color_attachments
,
485 .depth_stencil_attachment
= ds_attachment
,
488 anv_cmd_buffer_begin_subpass(cmd_buffer
, &subpass
);
490 meta_emit_clear(cmd_buffer
, pass
->num_color_clear_attachments
,
491 instance_data
, ds_clear_value
);
493 /* Restore API state */
494 anv_cmd_buffer_restore(cmd_buffer
, &saved_state
);
497 static VkImageViewType
498 meta_blit_get_src_image_view_type(const struct anv_image
*src_image
)
500 switch (src_image
->type
) {
501 case VK_IMAGE_TYPE_1D
:
502 return VK_IMAGE_VIEW_TYPE_1D
;
503 case VK_IMAGE_TYPE_2D
:
504 return VK_IMAGE_VIEW_TYPE_2D
;
505 case VK_IMAGE_TYPE_3D
:
506 return VK_IMAGE_VIEW_TYPE_3D
;
508 assert(!"bad VkImageType");
514 meta_blit_get_dest_view_base_array_slice(const struct anv_image
*dest_image
,
515 const VkImageSubresourceCopy
*dest_subresource
,
516 const VkOffset3D
*dest_offset
)
518 switch (dest_image
->type
) {
519 case VK_IMAGE_TYPE_1D
:
520 case VK_IMAGE_TYPE_2D
:
521 return dest_subresource
->arrayLayer
;
522 case VK_IMAGE_TYPE_3D
:
523 /* HACK: Vulkan does not allow attaching a 3D image to a framebuffer,
524 * but meta does it anyway. When doing so, we translate the
525 * destination's z offset into an array offset.
527 return dest_offset
->z
;
529 assert(!"bad VkImageType");
535 anv_device_init_meta_blit_state(struct anv_device
*device
)
537 anv_CreateRenderPass(anv_device_to_handle(device
),
538 &(VkRenderPassCreateInfo
) {
539 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
540 .attachmentCount
= 1,
541 .pAttachments
= &(VkAttachmentDescription
) {
542 .sType
= VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION
,
543 .format
= VK_FORMAT_UNDEFINED
, /* Our shaders don't care */
544 .loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
545 .storeOp
= VK_ATTACHMENT_STORE_OP_STORE
,
546 .initialLayout
= VK_IMAGE_LAYOUT_GENERAL
,
547 .finalLayout
= VK_IMAGE_LAYOUT_GENERAL
,
550 .pSubpasses
= &(VkSubpassDescription
) {
551 .sType
= VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION
,
552 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
555 .pColorAttachments
= &(VkAttachmentReference
) {
557 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
559 .pResolveAttachments
= NULL
,
560 .depthStencilAttachment
= (VkAttachmentReference
) {
561 .attachment
= VK_ATTACHMENT_UNUSED
,
562 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
565 .pPreserveAttachments
= &(VkAttachmentReference
) {
567 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
570 .dependencyCount
= 0,
571 }, &device
->meta_state
.blit
.render_pass
);
573 /* We don't use a vertex shader for clearing, but instead build and pass
574 * the VUEs directly to the rasterization backend. However, we do need
575 * to provide GLSL source for the vertex shader so that the compiler
576 * does not dead-code our inputs.
578 struct anv_shader_module vsm
= {
579 .nir
= build_nir_vertex_shader(false),
582 struct anv_shader_module fsm_2d
= {
583 .nir
= build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_2D
),
586 struct anv_shader_module fsm_3d
= {
587 .nir
= build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_3D
),
591 anv_CreateShader(anv_device_to_handle(device
),
592 &(VkShaderCreateInfo
) {
593 .sType
= VK_STRUCTURE_TYPE_SHADER_CREATE_INFO
,
594 .module
= anv_shader_module_to_handle(&vsm
),
599 anv_CreateShader(anv_device_to_handle(device
),
600 &(VkShaderCreateInfo
) {
601 .sType
= VK_STRUCTURE_TYPE_SHADER_CREATE_INFO
,
602 .module
= anv_shader_module_to_handle(&fsm_2d
),
607 anv_CreateShader(anv_device_to_handle(device
),
608 &(VkShaderCreateInfo
) {
609 .sType
= VK_STRUCTURE_TYPE_SHADER_CREATE_INFO
,
610 .module
= anv_shader_module_to_handle(&fsm_3d
),
614 VkPipelineVertexInputStateCreateInfo vi_create_info
= {
615 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
617 .pVertexBindingDescriptions
= (VkVertexInputBindingDescription
[]) {
621 .stepRate
= VK_VERTEX_INPUT_STEP_RATE_VERTEX
625 .strideInBytes
= 5 * sizeof(float),
626 .stepRate
= VK_VERTEX_INPUT_STEP_RATE_VERTEX
630 .pVertexAttributeDescriptions
= (VkVertexInputAttributeDescription
[]) {
635 .format
= VK_FORMAT_R32G32B32A32_UINT
,
642 .format
= VK_FORMAT_R32G32_SFLOAT
,
646 /* Texture Coordinate */
649 .format
= VK_FORMAT_R32G32B32_SFLOAT
,
655 VkDescriptorSetLayoutCreateInfo ds_layout_info
= {
656 .sType
= VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
,
658 .pBinding
= (VkDescriptorSetLayoutBinding
[]) {
660 .descriptorType
= VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
,
662 .stageFlags
= VK_SHADER_STAGE_FRAGMENT_BIT
,
663 .pImmutableSamplers
= NULL
667 anv_CreateDescriptorSetLayout(anv_device_to_handle(device
), &ds_layout_info
,
668 &device
->meta_state
.blit
.ds_layout
);
670 anv_CreatePipelineLayout(anv_device_to_handle(device
),
671 &(VkPipelineLayoutCreateInfo
) {
672 .sType
= VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
,
673 .descriptorSetCount
= 1,
674 .pSetLayouts
= &device
->meta_state
.blit
.ds_layout
,
676 &device
->meta_state
.blit
.pipeline_layout
);
678 VkPipelineShaderStageCreateInfo pipeline_shader_stages
[] = {
680 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
681 .stage
= VK_SHADER_STAGE_VERTEX
,
683 .pSpecializationInfo
= NULL
685 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
686 .stage
= VK_SHADER_STAGE_FRAGMENT
,
687 .shader
= {0}, /* TEMPLATE VALUE! FILL ME IN! */
688 .pSpecializationInfo
= NULL
692 const VkGraphicsPipelineCreateInfo vk_pipeline_info
= {
693 .sType
= VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
,
694 .stageCount
= ARRAY_SIZE(pipeline_shader_stages
),
695 .pStages
= pipeline_shader_stages
,
696 .pVertexInputState
= &vi_create_info
,
697 .pInputAssemblyState
= &(VkPipelineInputAssemblyStateCreateInfo
) {
698 .sType
= VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO
,
699 .topology
= VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
,
700 .primitiveRestartEnable
= false,
702 .pViewportState
= &(VkPipelineViewportStateCreateInfo
) {
703 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO
,
707 .pRasterState
= &(VkPipelineRasterStateCreateInfo
) {
708 .sType
= VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO
,
709 .depthClipEnable
= true,
710 .rasterizerDiscardEnable
= false,
711 .fillMode
= VK_FILL_MODE_SOLID
,
712 .cullMode
= VK_CULL_MODE_NONE
,
713 .frontFace
= VK_FRONT_FACE_CCW
715 .pMultisampleState
= &(VkPipelineMultisampleStateCreateInfo
) {
716 .sType
= VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO
,
718 .sampleShadingEnable
= false,
719 .pSampleMask
= (VkSampleMask
[]) { UINT32_MAX
},
721 .pColorBlendState
= &(VkPipelineColorBlendStateCreateInfo
) {
722 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
723 .attachmentCount
= 1,
724 .pAttachments
= (VkPipelineColorBlendAttachmentState
[]) {
725 { .channelWriteMask
= VK_CHANNEL_A_BIT
|
726 VK_CHANNEL_R_BIT
| VK_CHANNEL_G_BIT
| VK_CHANNEL_B_BIT
},
729 .pDynamicState
= &(VkPipelineDynamicStateCreateInfo
) {
730 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO
,
731 .dynamicStateCount
= 9,
732 .pDynamicStates
= (VkDynamicState
[]) {
733 VK_DYNAMIC_STATE_VIEWPORT
,
734 VK_DYNAMIC_STATE_SCISSOR
,
735 VK_DYNAMIC_STATE_LINE_WIDTH
,
736 VK_DYNAMIC_STATE_DEPTH_BIAS
,
737 VK_DYNAMIC_STATE_BLEND_CONSTANTS
,
738 VK_DYNAMIC_STATE_DEPTH_BOUNDS
,
739 VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
,
740 VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
,
741 VK_DYNAMIC_STATE_STENCIL_REFERENCE
,
745 .layout
= device
->meta_state
.blit
.pipeline_layout
,
746 .renderPass
= device
->meta_state
.blit
.render_pass
,
750 const struct anv_graphics_pipeline_create_info anv_pipeline_info
= {
751 .use_repclear
= false,
752 .disable_viewport
= true,
753 .disable_scissor
= true,
758 pipeline_shader_stages
[1].shader
= fs_2d
;
759 anv_graphics_pipeline_create(anv_device_to_handle(device
),
760 &vk_pipeline_info
, &anv_pipeline_info
,
761 &device
->meta_state
.blit
.pipeline_2d_src
);
763 pipeline_shader_stages
[1].shader
= fs_3d
;
764 anv_graphics_pipeline_create(anv_device_to_handle(device
),
765 &vk_pipeline_info
, &anv_pipeline_info
,
766 &device
->meta_state
.blit
.pipeline_3d_src
);
768 anv_DestroyShader(anv_device_to_handle(device
), vs
);
769 anv_DestroyShader(anv_device_to_handle(device
), fs_2d
);
770 anv_DestroyShader(anv_device_to_handle(device
), fs_3d
);
771 ralloc_free(vsm
.nir
);
772 ralloc_free(fsm_2d
.nir
);
773 ralloc_free(fsm_3d
.nir
);
777 meta_prepare_blit(struct anv_cmd_buffer
*cmd_buffer
,
778 struct anv_saved_state
*saved_state
)
780 anv_cmd_buffer_save(cmd_buffer
, saved_state
,
781 (1 << VK_DYNAMIC_STATE_VIEWPORT
));
785 VkOffset3D src_offset
;
786 VkExtent3D src_extent
;
787 VkOffset3D dest_offset
;
788 VkExtent3D dest_extent
;
792 meta_emit_blit(struct anv_cmd_buffer
*cmd_buffer
,
793 struct anv_image
*src_image
,
794 struct anv_image_view
*src_iview
,
795 VkOffset3D src_offset
,
796 VkExtent3D src_extent
,
797 struct anv_image
*dest_image
,
798 struct anv_image_view
*dest_iview
,
799 VkOffset3D dest_offset
,
800 VkExtent3D dest_extent
)
802 struct anv_device
*device
= cmd_buffer
->device
;
803 VkDescriptorPool dummy_desc_pool
= { .handle
= 1 };
805 struct blit_vb_data
{
810 unsigned vb_size
= sizeof(struct vue_header
) + 3 * sizeof(*vb_data
);
812 struct anv_state vb_state
=
813 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, vb_size
, 16);
814 memset(vb_state
.map
, 0, sizeof(struct vue_header
));
815 vb_data
= vb_state
.map
+ sizeof(struct vue_header
);
817 vb_data
[0] = (struct blit_vb_data
) {
819 dest_offset
.x
+ dest_extent
.width
,
820 dest_offset
.y
+ dest_extent
.height
,
823 (float)(src_offset
.x
+ src_extent
.width
) / (float)src_iview
->extent
.width
,
824 (float)(src_offset
.y
+ src_extent
.height
) / (float)src_iview
->extent
.height
,
825 (float)(src_offset
.z
+ src_extent
.depth
) / (float)src_iview
->extent
.depth
,
829 vb_data
[1] = (struct blit_vb_data
) {
832 dest_offset
.y
+ dest_extent
.height
,
835 (float)src_offset
.x
/ (float)src_iview
->extent
.width
,
836 (float)(src_offset
.y
+ src_extent
.height
) / (float)src_iview
->extent
.height
,
837 (float)(src_offset
.z
+ src_extent
.depth
) / (float)src_iview
->extent
.depth
,
841 vb_data
[2] = (struct blit_vb_data
) {
847 (float)src_offset
.x
/ (float)src_iview
->extent
.width
,
848 (float)src_offset
.y
/ (float)src_iview
->extent
.height
,
849 (float)src_offset
.z
/ (float)src_iview
->extent
.depth
,
853 struct anv_buffer vertex_buffer
= {
856 .bo
= &device
->dynamic_state_block_pool
.bo
,
857 .offset
= vb_state
.offset
,
860 anv_CmdBindVertexBuffers(anv_cmd_buffer_to_handle(cmd_buffer
), 0, 2,
862 anv_buffer_to_handle(&vertex_buffer
),
863 anv_buffer_to_handle(&vertex_buffer
)
867 sizeof(struct vue_header
),
871 anv_AllocDescriptorSets(anv_device_to_handle(device
), dummy_desc_pool
,
872 VK_DESCRIPTOR_SET_USAGE_ONE_SHOT
,
873 1, &device
->meta_state
.blit
.ds_layout
, &set
);
874 anv_UpdateDescriptorSets(anv_device_to_handle(device
),
876 (VkWriteDescriptorSet
[]) {
878 .sType
= VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET
,
881 .destArrayElement
= 0,
883 .descriptorType
= VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
,
884 .pDescriptors
= (VkDescriptorInfo
[]) {
886 .imageView
= anv_image_view_to_handle(src_iview
),
887 .imageLayout
= VK_IMAGE_LAYOUT_GENERAL
894 anv_CreateFramebuffer(anv_device_to_handle(device
),
895 &(VkFramebufferCreateInfo
) {
896 .sType
= VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
,
897 .attachmentCount
= 1,
898 .pAttachments
= (VkImageView
[]) {
899 anv_image_view_to_handle(dest_iview
),
901 .width
= dest_iview
->extent
.width
,
902 .height
= dest_iview
->extent
.height
,
906 ANV_CALL(CmdBeginRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
),
907 &(VkRenderPassBeginInfo
) {
908 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO
,
909 .renderPass
= device
->meta_state
.blit
.render_pass
,
912 .offset
= { dest_offset
.x
, dest_offset
.y
},
913 .extent
= { dest_extent
.width
, dest_extent
.height
},
915 .clearValueCount
= 0,
916 .pClearValues
= NULL
,
917 }, VK_RENDER_PASS_CONTENTS_INLINE
);
921 switch (src_image
->type
) {
922 case VK_IMAGE_TYPE_1D
:
923 anv_finishme("VK_IMAGE_TYPE_1D");
924 pipeline
= device
->meta_state
.blit
.pipeline_2d_src
;
926 case VK_IMAGE_TYPE_2D
:
927 pipeline
= device
->meta_state
.blit
.pipeline_2d_src
;
929 case VK_IMAGE_TYPE_3D
:
930 pipeline
= device
->meta_state
.blit
.pipeline_3d_src
;
933 unreachable(!"bad VkImageType");
936 if (cmd_buffer
->state
.pipeline
!= anv_pipeline_from_handle(pipeline
)) {
937 anv_CmdBindPipeline(anv_cmd_buffer_to_handle(cmd_buffer
),
938 VK_PIPELINE_BIND_POINT_GRAPHICS
, pipeline
);
941 anv_CmdSetViewport(anv_cmd_buffer_to_handle(cmd_buffer
), 1,
945 .width
= dest_iview
->extent
.width
,
946 .height
= dest_iview
->extent
.height
,
951 anv_CmdBindDescriptorSets(anv_cmd_buffer_to_handle(cmd_buffer
),
952 VK_PIPELINE_BIND_POINT_GRAPHICS
,
953 device
->meta_state
.blit
.pipeline_layout
, 0, 1,
956 ANV_CALL(CmdDraw
)(anv_cmd_buffer_to_handle(cmd_buffer
), 3, 1, 0, 0);
958 ANV_CALL(CmdEndRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
));
960 /* At the point where we emit the draw call, all data from the
961 * descriptor sets, etc. has been used. We are free to delete it.
963 anv_descriptor_set_destroy(device
, anv_descriptor_set_from_handle(set
));
964 anv_DestroyFramebuffer(anv_device_to_handle(device
), fb
);
968 meta_finish_blit(struct anv_cmd_buffer
*cmd_buffer
,
969 const struct anv_saved_state
*saved_state
)
971 anv_cmd_buffer_restore(cmd_buffer
, saved_state
);
975 vk_format_for_cpp(int cpp
)
978 case 1: return VK_FORMAT_R8_UINT
;
979 case 2: return VK_FORMAT_R8G8_UINT
;
980 case 3: return VK_FORMAT_R8G8B8_UINT
;
981 case 4: return VK_FORMAT_R8G8B8A8_UINT
;
982 case 6: return VK_FORMAT_R16G16B16_UINT
;
983 case 8: return VK_FORMAT_R16G16B16A16_UINT
;
984 case 12: return VK_FORMAT_R32G32B32_UINT
;
985 case 16: return VK_FORMAT_R32G32B32A32_UINT
;
987 unreachable("Invalid format cpp");
992 do_buffer_copy(struct anv_cmd_buffer
*cmd_buffer
,
993 struct anv_bo
*src
, uint64_t src_offset
,
994 struct anv_bo
*dest
, uint64_t dest_offset
,
995 int width
, int height
, VkFormat copy_format
)
997 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
999 VkImageCreateInfo image_info
= {
1000 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
1001 .imageType
= VK_IMAGE_TYPE_2D
,
1002 .format
= copy_format
,
1011 .tiling
= VK_IMAGE_TILING_LINEAR
,
1017 image_info
.usage
= VK_IMAGE_USAGE_SAMPLED_BIT
;
1018 anv_CreateImage(vk_device
, &image_info
, &src_image
);
1021 image_info
.usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
1022 anv_CreateImage(vk_device
, &image_info
, &dest_image
);
1024 /* We could use a vk call to bind memory, but that would require
1025 * creating a dummy memory object etc. so there's really no point.
1027 anv_image_from_handle(src_image
)->bo
= src
;
1028 anv_image_from_handle(src_image
)->offset
= src_offset
;
1029 anv_image_from_handle(dest_image
)->bo
= dest
;
1030 anv_image_from_handle(dest_image
)->offset
= dest_offset
;
1032 struct anv_image_view src_iview
;
1033 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1034 &(VkImageViewCreateInfo
) {
1035 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1037 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1038 .format
= copy_format
,
1040 VK_CHANNEL_SWIZZLE_R
,
1041 VK_CHANNEL_SWIZZLE_G
,
1042 VK_CHANNEL_SWIZZLE_B
,
1043 VK_CHANNEL_SWIZZLE_A
1045 .subresourceRange
= {
1046 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1049 .baseArrayLayer
= 0,
1055 struct anv_image_view dest_iview
;
1056 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1057 &(VkImageViewCreateInfo
) {
1058 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1059 .image
= dest_image
,
1060 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1061 .format
= copy_format
,
1063 .r
= VK_CHANNEL_SWIZZLE_R
,
1064 .g
= VK_CHANNEL_SWIZZLE_G
,
1065 .b
= VK_CHANNEL_SWIZZLE_B
,
1066 .a
= VK_CHANNEL_SWIZZLE_A
,
1068 .subresourceRange
= {
1069 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1072 .baseArrayLayer
= 0,
1078 meta_emit_blit(cmd_buffer
,
1079 anv_image_from_handle(src_image
),
1081 (VkOffset3D
) { 0, 0, 0 },
1082 (VkExtent3D
) { width
, height
, 1 },
1083 anv_image_from_handle(dest_image
),
1085 (VkOffset3D
) { 0, 0, 0 },
1086 (VkExtent3D
) { width
, height
, 1 });
1088 anv_DestroyImage(vk_device
, src_image
);
1089 anv_DestroyImage(vk_device
, dest_image
);
1092 void anv_CmdCopyBuffer(
1093 VkCmdBuffer cmdBuffer
,
1095 VkBuffer destBuffer
,
1096 uint32_t regionCount
,
1097 const VkBufferCopy
* pRegions
)
1099 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
1100 ANV_FROM_HANDLE(anv_buffer
, src_buffer
, srcBuffer
);
1101 ANV_FROM_HANDLE(anv_buffer
, dest_buffer
, destBuffer
);
1103 struct anv_saved_state saved_state
;
1105 meta_prepare_blit(cmd_buffer
, &saved_state
);
1107 for (unsigned r
= 0; r
< regionCount
; r
++) {
1108 uint64_t src_offset
= src_buffer
->offset
+ pRegions
[r
].srcOffset
;
1109 uint64_t dest_offset
= dest_buffer
->offset
+ pRegions
[r
].destOffset
;
1110 uint64_t copy_size
= pRegions
[r
].copySize
;
1112 /* First, we compute the biggest format that can be used with the
1113 * given offsets and size.
1117 int fs
= ffs(src_offset
) - 1;
1119 cpp
= MIN2(cpp
, 1 << fs
);
1120 assert(src_offset
% cpp
== 0);
1122 fs
= ffs(dest_offset
) - 1;
1124 cpp
= MIN2(cpp
, 1 << fs
);
1125 assert(dest_offset
% cpp
== 0);
1127 fs
= ffs(pRegions
[r
].copySize
) - 1;
1129 cpp
= MIN2(cpp
, 1 << fs
);
1130 assert(pRegions
[r
].copySize
% cpp
== 0);
1132 VkFormat copy_format
= vk_format_for_cpp(cpp
);
1134 /* This is maximum possible width/height our HW can handle */
1135 uint64_t max_surface_dim
= 1 << 14;
1137 /* First, we make a bunch of max-sized copies */
1138 uint64_t max_copy_size
= max_surface_dim
* max_surface_dim
* cpp
;
1139 while (copy_size
> max_copy_size
) {
1140 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
1141 dest_buffer
->bo
, dest_offset
,
1142 max_surface_dim
, max_surface_dim
, copy_format
);
1143 copy_size
-= max_copy_size
;
1144 src_offset
+= max_copy_size
;
1145 dest_offset
+= max_copy_size
;
1148 uint64_t height
= copy_size
/ (max_surface_dim
* cpp
);
1149 assert(height
< max_surface_dim
);
1151 uint64_t rect_copy_size
= height
* max_surface_dim
* cpp
;
1152 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
1153 dest_buffer
->bo
, dest_offset
,
1154 max_surface_dim
, height
, copy_format
);
1155 copy_size
-= rect_copy_size
;
1156 src_offset
+= rect_copy_size
;
1157 dest_offset
+= rect_copy_size
;
1160 if (copy_size
!= 0) {
1161 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
1162 dest_buffer
->bo
, dest_offset
,
1163 copy_size
/ cpp
, 1, copy_format
);
1167 meta_finish_blit(cmd_buffer
, &saved_state
);
1170 void anv_CmdCopyImage(
1171 VkCmdBuffer cmdBuffer
,
1173 VkImageLayout srcImageLayout
,
1175 VkImageLayout destImageLayout
,
1176 uint32_t regionCount
,
1177 const VkImageCopy
* pRegions
)
1179 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
1180 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
1181 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
1183 const VkImageViewType src_iview_type
=
1184 meta_blit_get_src_image_view_type(src_image
);
1186 struct anv_saved_state saved_state
;
1188 meta_prepare_blit(cmd_buffer
, &saved_state
);
1190 for (unsigned r
= 0; r
< regionCount
; r
++) {
1191 struct anv_image_view src_iview
;
1192 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1193 &(VkImageViewCreateInfo
) {
1194 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1196 .viewType
= src_iview_type
,
1197 .format
= src_image
->format
->vk_format
,
1199 VK_CHANNEL_SWIZZLE_R
,
1200 VK_CHANNEL_SWIZZLE_G
,
1201 VK_CHANNEL_SWIZZLE_B
,
1202 VK_CHANNEL_SWIZZLE_A
1204 .subresourceRange
= {
1205 .aspectMask
= 1 << pRegions
[r
].srcSubresource
.aspect
,
1206 .baseMipLevel
= pRegions
[r
].srcSubresource
.mipLevel
,
1208 .baseArrayLayer
= pRegions
[r
].srcSubresource
.arrayLayer
,
1214 const VkOffset3D dest_offset
= {
1215 .x
= pRegions
[r
].destOffset
.x
,
1216 .y
= pRegions
[r
].destOffset
.y
,
1220 const uint32_t dest_array_slice
=
1221 meta_blit_get_dest_view_base_array_slice(dest_image
,
1222 &pRegions
[r
].destSubresource
,
1223 &pRegions
[r
].destOffset
);
1225 if (pRegions
[r
].srcSubresource
.arraySize
> 1)
1226 anv_finishme("FINISHME: copy multiple array layers");
1228 if (pRegions
[r
].extent
.depth
> 1)
1229 anv_finishme("FINISHME: copy multiple depth layers");
1231 struct anv_image_view dest_iview
;
1232 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1233 &(VkImageViewCreateInfo
) {
1234 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1236 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1237 .format
= dest_image
->format
->vk_format
,
1239 VK_CHANNEL_SWIZZLE_R
,
1240 VK_CHANNEL_SWIZZLE_G
,
1241 VK_CHANNEL_SWIZZLE_B
,
1242 VK_CHANNEL_SWIZZLE_A
1244 .subresourceRange
= {
1245 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1246 .baseMipLevel
= pRegions
[r
].destSubresource
.mipLevel
,
1248 .baseArrayLayer
= dest_array_slice
,
1254 meta_emit_blit(cmd_buffer
,
1255 src_image
, &src_iview
,
1256 pRegions
[r
].srcOffset
,
1258 dest_image
, &dest_iview
,
1260 pRegions
[r
].extent
);
1263 meta_finish_blit(cmd_buffer
, &saved_state
);
1266 void anv_CmdBlitImage(
1267 VkCmdBuffer cmdBuffer
,
1269 VkImageLayout srcImageLayout
,
1271 VkImageLayout destImageLayout
,
1272 uint32_t regionCount
,
1273 const VkImageBlit
* pRegions
,
1277 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
1278 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
1279 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
1281 const VkImageViewType src_iview_type
=
1282 meta_blit_get_src_image_view_type(src_image
);
1284 struct anv_saved_state saved_state
;
1286 anv_finishme("respect VkTexFilter");
1288 meta_prepare_blit(cmd_buffer
, &saved_state
);
1290 for (unsigned r
= 0; r
< regionCount
; r
++) {
1291 struct anv_image_view src_iview
;
1292 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1293 &(VkImageViewCreateInfo
) {
1294 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1296 .viewType
= src_iview_type
,
1297 .format
= src_image
->format
->vk_format
,
1299 VK_CHANNEL_SWIZZLE_R
,
1300 VK_CHANNEL_SWIZZLE_G
,
1301 VK_CHANNEL_SWIZZLE_B
,
1302 VK_CHANNEL_SWIZZLE_A
1304 .subresourceRange
= {
1305 .aspectMask
= 1 << pRegions
[r
].srcSubresource
.aspect
,
1306 .baseMipLevel
= pRegions
[r
].srcSubresource
.mipLevel
,
1308 .baseArrayLayer
= pRegions
[r
].srcSubresource
.arrayLayer
,
1314 const VkOffset3D dest_offset
= {
1315 .x
= pRegions
[r
].destOffset
.x
,
1316 .y
= pRegions
[r
].destOffset
.y
,
1320 const uint32_t dest_array_slice
=
1321 meta_blit_get_dest_view_base_array_slice(dest_image
,
1322 &pRegions
[r
].destSubresource
,
1323 &pRegions
[r
].destOffset
);
1325 if (pRegions
[r
].srcSubresource
.arraySize
> 1)
1326 anv_finishme("FINISHME: copy multiple array layers");
1328 if (pRegions
[r
].destExtent
.depth
> 1)
1329 anv_finishme("FINISHME: copy multiple depth layers");
1331 struct anv_image_view dest_iview
;
1332 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1333 &(VkImageViewCreateInfo
) {
1334 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1336 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1337 .format
= dest_image
->format
->vk_format
,
1339 VK_CHANNEL_SWIZZLE_R
,
1340 VK_CHANNEL_SWIZZLE_G
,
1341 VK_CHANNEL_SWIZZLE_B
,
1342 VK_CHANNEL_SWIZZLE_A
1344 .subresourceRange
= {
1345 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1346 .baseMipLevel
= pRegions
[r
].destSubresource
.mipLevel
,
1348 .baseArrayLayer
= dest_array_slice
,
1354 meta_emit_blit(cmd_buffer
,
1355 src_image
, &src_iview
,
1356 pRegions
[r
].srcOffset
,
1357 pRegions
[r
].srcExtent
,
1358 dest_image
, &dest_iview
,
1360 pRegions
[r
].destExtent
);
1363 meta_finish_blit(cmd_buffer
, &saved_state
);
1367 make_image_for_buffer(VkDevice vk_device
, VkBuffer vk_buffer
, VkFormat format
,
1368 VkImageUsageFlags usage
,
1369 const VkBufferImageCopy
*copy
)
1371 ANV_FROM_HANDLE(anv_buffer
, buffer
, vk_buffer
);
1373 VkExtent3D extent
= copy
->imageExtent
;
1374 if (copy
->bufferRowLength
)
1375 extent
.width
= copy
->bufferRowLength
;
1376 if (copy
->bufferImageHeight
)
1377 extent
.height
= copy
->bufferImageHeight
;
1381 VkResult result
= anv_CreateImage(vk_device
,
1382 &(VkImageCreateInfo
) {
1383 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
1384 .imageType
= VK_IMAGE_TYPE_2D
,
1390 .tiling
= VK_IMAGE_TILING_LINEAR
,
1394 assert(result
== VK_SUCCESS
);
1396 ANV_FROM_HANDLE(anv_image
, image
, vk_image
);
1398 /* We could use a vk call to bind memory, but that would require
1399 * creating a dummy memory object etc. so there's really no point.
1401 image
->bo
= buffer
->bo
;
1402 image
->offset
= buffer
->offset
+ copy
->bufferOffset
;
1404 return anv_image_to_handle(image
);
1407 void anv_CmdCopyBufferToImage(
1408 VkCmdBuffer cmdBuffer
,
1411 VkImageLayout destImageLayout
,
1412 uint32_t regionCount
,
1413 const VkBufferImageCopy
* pRegions
)
1415 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
1416 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
1417 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
1418 const VkFormat orig_format
= dest_image
->format
->vk_format
;
1419 struct anv_saved_state saved_state
;
1421 meta_prepare_blit(cmd_buffer
, &saved_state
);
1423 for (unsigned r
= 0; r
< regionCount
; r
++) {
1424 VkFormat proxy_format
= orig_format
;
1425 VkImageAspect proxy_aspect
= pRegions
[r
].imageSubresource
.aspect
;
1427 if (orig_format
== VK_FORMAT_S8_UINT
) {
1428 proxy_format
= VK_FORMAT_R8_UINT
;
1429 proxy_aspect
= VK_IMAGE_ASPECT_COLOR
;
1432 VkImage srcImage
= make_image_for_buffer(vk_device
, srcBuffer
,
1433 proxy_format
, VK_IMAGE_USAGE_SAMPLED_BIT
, &pRegions
[r
]);
1435 struct anv_image_view src_iview
;
1436 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1437 &(VkImageViewCreateInfo
) {
1438 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1440 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1441 .format
= proxy_format
,
1443 VK_CHANNEL_SWIZZLE_R
,
1444 VK_CHANNEL_SWIZZLE_G
,
1445 VK_CHANNEL_SWIZZLE_B
,
1446 VK_CHANNEL_SWIZZLE_A
1448 .subresourceRange
= {
1449 .aspectMask
= 1 << proxy_aspect
,
1452 .baseArrayLayer
= 0,
1458 const VkOffset3D dest_offset
= {
1459 .x
= pRegions
[r
].imageOffset
.x
,
1460 .y
= pRegions
[r
].imageOffset
.y
,
1464 const uint32_t dest_array_slice
=
1465 meta_blit_get_dest_view_base_array_slice(dest_image
,
1466 &pRegions
[r
].imageSubresource
,
1467 &pRegions
[r
].imageOffset
);
1469 if (pRegions
[r
].imageExtent
.depth
> 1)
1470 anv_finishme("FINISHME: copy multiple depth layers");
1472 struct anv_image_view dest_iview
;
1473 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1474 &(VkImageViewCreateInfo
) {
1475 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1476 .image
= anv_image_to_handle(dest_image
),
1477 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1478 .format
= proxy_format
,
1480 VK_CHANNEL_SWIZZLE_R
,
1481 VK_CHANNEL_SWIZZLE_G
,
1482 VK_CHANNEL_SWIZZLE_B
,
1483 VK_CHANNEL_SWIZZLE_A
1485 .subresourceRange
= {
1486 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1487 .baseMipLevel
= pRegions
[r
].imageSubresource
.mipLevel
,
1489 .baseArrayLayer
= dest_array_slice
,
1495 meta_emit_blit(cmd_buffer
,
1496 anv_image_from_handle(srcImage
),
1498 (VkOffset3D
) { 0, 0, 0 },
1499 pRegions
[r
].imageExtent
,
1503 pRegions
[r
].imageExtent
);
1505 anv_DestroyImage(vk_device
, srcImage
);
1508 meta_finish_blit(cmd_buffer
, &saved_state
);
1511 void anv_CmdCopyImageToBuffer(
1512 VkCmdBuffer cmdBuffer
,
1514 VkImageLayout srcImageLayout
,
1515 VkBuffer destBuffer
,
1516 uint32_t regionCount
,
1517 const VkBufferImageCopy
* pRegions
)
1519 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
1520 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
1521 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
1522 struct anv_saved_state saved_state
;
1524 const VkImageViewType src_iview_type
=
1525 meta_blit_get_src_image_view_type(src_image
);
1527 meta_prepare_blit(cmd_buffer
, &saved_state
);
1529 for (unsigned r
= 0; r
< regionCount
; r
++) {
1530 if (pRegions
[r
].imageSubresource
.arraySize
> 1)
1531 anv_finishme("FINISHME: copy multiple array layers");
1533 if (pRegions
[r
].imageExtent
.depth
> 1)
1534 anv_finishme("FINISHME: copy multiple depth layers");
1536 struct anv_image_view src_iview
;
1537 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1538 &(VkImageViewCreateInfo
) {
1539 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1541 .viewType
= src_iview_type
,
1542 .format
= src_image
->format
->vk_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
= 1 << pRegions
[r
].imageSubresource
.aspect
,
1551 .baseMipLevel
= pRegions
[r
].imageSubresource
.mipLevel
,
1553 .baseArrayLayer
= pRegions
[r
].imageSubresource
.arrayLayer
,
1559 VkFormat dest_format
= src_image
->format
->vk_format
;
1560 if (dest_format
== VK_FORMAT_S8_UINT
) {
1561 dest_format
= VK_FORMAT_R8_UINT
;
1564 VkImage destImage
= make_image_for_buffer(vk_device
, destBuffer
,
1565 dest_format
, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
, &pRegions
[r
]);
1567 struct anv_image_view dest_iview
;
1568 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1569 &(VkImageViewCreateInfo
) {
1570 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1572 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1573 .format
= dest_format
,
1575 VK_CHANNEL_SWIZZLE_R
,
1576 VK_CHANNEL_SWIZZLE_G
,
1577 VK_CHANNEL_SWIZZLE_B
,
1578 VK_CHANNEL_SWIZZLE_A
1580 .subresourceRange
= {
1581 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1584 .baseArrayLayer
= 0,
1590 meta_emit_blit(cmd_buffer
,
1591 anv_image_from_handle(srcImage
),
1593 pRegions
[r
].imageOffset
,
1594 pRegions
[r
].imageExtent
,
1595 anv_image_from_handle(destImage
),
1597 (VkOffset3D
) { 0, 0, 0 },
1598 pRegions
[r
].imageExtent
);
1600 anv_DestroyImage(vk_device
, destImage
);
1603 meta_finish_blit(cmd_buffer
, &saved_state
);
1606 void anv_CmdUpdateBuffer(
1607 VkCmdBuffer cmdBuffer
,
1608 VkBuffer destBuffer
,
1609 VkDeviceSize destOffset
,
1610 VkDeviceSize dataSize
,
1611 const uint32_t* pData
)
1616 void anv_CmdFillBuffer(
1617 VkCmdBuffer cmdBuffer
,
1618 VkBuffer destBuffer
,
1619 VkDeviceSize destOffset
,
1620 VkDeviceSize fillSize
,
1626 void anv_CmdClearColorImage(
1627 VkCmdBuffer cmdBuffer
,
1629 VkImageLayout imageLayout
,
1630 const VkClearColorValue
* pColor
,
1631 uint32_t rangeCount
,
1632 const VkImageSubresourceRange
* pRanges
)
1634 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, cmdBuffer
);
1635 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1636 struct anv_saved_state saved_state
;
1638 anv_cmd_buffer_save(cmd_buffer
, &saved_state
,
1639 (1 << VK_DYNAMIC_STATE_VIEWPORT
));
1640 cmd_buffer
->state
.dynamic
.viewport
.count
= 0;
1642 for (uint32_t r
= 0; r
< rangeCount
; r
++) {
1643 for (uint32_t l
= 0; l
< pRanges
[r
].mipLevels
; l
++) {
1644 for (uint32_t s
= 0; s
< pRanges
[r
].arraySize
; s
++) {
1645 struct anv_image_view iview
;
1646 anv_image_view_init(&iview
, cmd_buffer
->device
,
1647 &(VkImageViewCreateInfo
) {
1648 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1650 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1651 .format
= image
->format
->vk_format
,
1653 VK_CHANNEL_SWIZZLE_R
,
1654 VK_CHANNEL_SWIZZLE_G
,
1655 VK_CHANNEL_SWIZZLE_B
,
1656 VK_CHANNEL_SWIZZLE_A
1658 .subresourceRange
= {
1659 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1660 .baseMipLevel
= pRanges
[r
].baseMipLevel
+ l
,
1662 .baseArrayLayer
= pRanges
[r
].baseArrayLayer
+ s
,
1669 anv_CreateFramebuffer(anv_device_to_handle(cmd_buffer
->device
),
1670 &(VkFramebufferCreateInfo
) {
1671 .sType
= VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
,
1672 .attachmentCount
= 1,
1673 .pAttachments
= (VkImageView
[]) {
1674 anv_image_view_to_handle(&iview
),
1676 .width
= iview
.extent
.width
,
1677 .height
= iview
.extent
.height
,
1682 anv_CreateRenderPass(anv_device_to_handle(cmd_buffer
->device
),
1683 &(VkRenderPassCreateInfo
) {
1684 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
1685 .attachmentCount
= 1,
1686 .pAttachments
= &(VkAttachmentDescription
) {
1687 .sType
= VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION
,
1688 .format
= iview
.format
->vk_format
,
1689 .loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
1690 .storeOp
= VK_ATTACHMENT_STORE_OP_STORE
,
1691 .initialLayout
= VK_IMAGE_LAYOUT_GENERAL
,
1692 .finalLayout
= VK_IMAGE_LAYOUT_GENERAL
,
1695 .pSubpasses
= &(VkSubpassDescription
) {
1696 .sType
= VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION
,
1697 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
1700 .pColorAttachments
= &(VkAttachmentReference
) {
1702 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
1704 .pResolveAttachments
= NULL
,
1705 .depthStencilAttachment
= (VkAttachmentReference
) {
1706 .attachment
= VK_ATTACHMENT_UNUSED
,
1707 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
1710 .pPreserveAttachments
= &(VkAttachmentReference
) {
1712 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
1715 .dependencyCount
= 0,
1718 ANV_CALL(CmdBeginRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
),
1719 &(VkRenderPassBeginInfo
) {
1720 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO
,
1722 .offset
= { 0, 0, },
1724 .width
= iview
.extent
.width
,
1725 .height
= iview
.extent
.height
,
1730 .clearValueCount
= 1,
1731 .pClearValues
= NULL
,
1732 }, VK_RENDER_PASS_CONTENTS_INLINE
);
1734 struct clear_instance_data instance_data
= {
1743 meta_emit_clear(cmd_buffer
, 1, &instance_data
,
1744 (VkClearDepthStencilValue
) {0});
1746 ANV_CALL(CmdEndRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
));
1751 /* Restore API state */
1752 anv_cmd_buffer_restore(cmd_buffer
, &saved_state
);
1755 void anv_CmdClearDepthStencilImage(
1756 VkCmdBuffer cmdBuffer
,
1758 VkImageLayout imageLayout
,
1759 const VkClearDepthStencilValue
* pDepthStencil
,
1760 uint32_t rangeCount
,
1761 const VkImageSubresourceRange
* pRanges
)
1766 void anv_CmdClearColorAttachment(
1767 VkCmdBuffer cmdBuffer
,
1768 uint32_t colorAttachment
,
1769 VkImageLayout imageLayout
,
1770 const VkClearColorValue
* pColor
,
1772 const VkRect3D
* pRects
)
1777 void anv_CmdClearDepthStencilAttachment(
1778 VkCmdBuffer cmdBuffer
,
1779 VkImageAspectFlags aspectMask
,
1780 VkImageLayout imageLayout
,
1781 const VkClearDepthStencilValue
* pDepthStencil
,
1783 const VkRect3D
* pRects
)
1788 void anv_CmdResolveImage(
1789 VkCmdBuffer cmdBuffer
,
1791 VkImageLayout srcImageLayout
,
1793 VkImageLayout destImageLayout
,
1794 uint32_t regionCount
,
1795 const VkImageResolve
* pRegions
)
1801 anv_device_init_meta(struct anv_device
*device
)
1803 anv_device_init_meta_clear_state(device
);
1804 anv_device_init_meta_blit_state(device
);
1808 anv_device_finish_meta(struct anv_device
*device
)
1811 anv_DestroyPipeline(anv_device_to_handle(device
),
1812 device
->meta_state
.clear
.pipeline
);
1815 anv_DestroyRenderPass(anv_device_to_handle(device
),
1816 device
->meta_state
.blit
.render_pass
);
1817 anv_DestroyPipeline(anv_device_to_handle(device
),
1818 device
->meta_state
.blit
.pipeline_2d_src
);
1819 anv_DestroyPipeline(anv_device_to_handle(device
),
1820 device
->meta_state
.blit
.pipeline_3d_src
);
1821 anv_DestroyPipelineLayout(anv_device_to_handle(device
),
1822 device
->meta_state
.blit
.pipeline_layout
);
1823 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device
),
1824 device
->meta_state
.blit
.ds_layout
);