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
25 #include "anv_private.h"
26 #include "nir/nir_builder.h"
28 /** Vertex attributes for color clears. */
29 struct color_clear_vattrs
{
30 struct anv_vue_header vue_header
;
31 float position
[2]; /**< 3DPRIM_RECTLIST */
32 VkClearColorValue color
;
35 /** Vertex attributes for depthstencil clears. */
36 struct depthstencil_clear_vattrs
{
37 struct anv_vue_header vue_header
;
38 float position
[2]; /*<< 3DPRIM_RECTLIST */
42 meta_clear_begin(struct anv_meta_saved_state
*saved_state
,
43 struct anv_cmd_buffer
*cmd_buffer
)
45 anv_meta_save(saved_state
, cmd_buffer
,
46 (1 << VK_DYNAMIC_STATE_VIEWPORT
) |
47 (1 << VK_DYNAMIC_STATE_STENCIL_REFERENCE
) |
48 (1 << VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
));
50 /* Avoid uploading more viewport states than necessary */
51 cmd_buffer
->state
.dynamic
.viewport
.count
= 0;
55 meta_clear_end(struct anv_meta_saved_state
*saved_state
,
56 struct anv_cmd_buffer
*cmd_buffer
)
58 anv_meta_restore(saved_state
, cmd_buffer
);
62 build_color_shaders(struct nir_shader
**out_vs
,
63 struct nir_shader
**out_fs
,
69 nir_builder_init_simple_shader(&vs_b
, NULL
, MESA_SHADER_VERTEX
, NULL
);
70 nir_builder_init_simple_shader(&fs_b
, NULL
, MESA_SHADER_FRAGMENT
, NULL
);
72 vs_b
.shader
->info
.name
= ralloc_strdup(vs_b
.shader
, "meta_clear_color_vs");
73 fs_b
.shader
->info
.name
= ralloc_strdup(fs_b
.shader
, "meta_clear_color_fs");
75 const struct glsl_type
*position_type
= glsl_vec4_type();
76 const struct glsl_type
*color_type
= glsl_vec4_type();
78 nir_variable
*vs_in_pos
=
79 nir_variable_create(vs_b
.shader
, nir_var_shader_in
, position_type
,
81 vs_in_pos
->data
.location
= VERT_ATTRIB_GENERIC0
;
83 nir_variable
*vs_out_pos
=
84 nir_variable_create(vs_b
.shader
, nir_var_shader_out
, position_type
,
86 vs_out_pos
->data
.location
= VARYING_SLOT_POS
;
88 nir_variable
*vs_in_color
=
89 nir_variable_create(vs_b
.shader
, nir_var_shader_in
, color_type
,
91 vs_in_color
->data
.location
= VERT_ATTRIB_GENERIC1
;
93 nir_variable
*vs_out_color
=
94 nir_variable_create(vs_b
.shader
, nir_var_shader_out
, color_type
,
96 vs_out_color
->data
.location
= VARYING_SLOT_VAR0
;
97 vs_out_color
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
99 nir_variable
*fs_in_color
=
100 nir_variable_create(fs_b
.shader
, nir_var_shader_in
, color_type
,
102 fs_in_color
->data
.location
= vs_out_color
->data
.location
;
103 fs_in_color
->data
.interpolation
= vs_out_color
->data
.interpolation
;
105 nir_variable
*fs_out_color
=
106 nir_variable_create(fs_b
.shader
, nir_var_shader_out
, color_type
,
108 fs_out_color
->data
.location
= FRAG_RESULT_DATA0
+ frag_output
;
110 nir_copy_var(&vs_b
, vs_out_pos
, vs_in_pos
);
111 nir_copy_var(&vs_b
, vs_out_color
, vs_in_color
);
112 nir_copy_var(&fs_b
, fs_out_color
, fs_in_color
);
114 *out_vs
= vs_b
.shader
;
115 *out_fs
= fs_b
.shader
;
119 create_pipeline(struct anv_device
*device
,
121 struct nir_shader
*vs_nir
,
122 struct nir_shader
*fs_nir
,
123 const VkPipelineVertexInputStateCreateInfo
*vi_state
,
124 const VkPipelineDepthStencilStateCreateInfo
*ds_state
,
125 const VkPipelineColorBlendStateCreateInfo
*cb_state
,
126 const VkAllocationCallbacks
*alloc
,
128 struct anv_pipeline
**pipeline
)
130 VkDevice device_h
= anv_device_to_handle(device
);
133 struct anv_shader_module vs_m
= { .nir
= vs_nir
};
134 struct anv_shader_module fs_m
= { .nir
= fs_nir
};
136 VkPipeline pipeline_h
= VK_NULL_HANDLE
;
137 result
= anv_graphics_pipeline_create(device_h
,
139 &(VkGraphicsPipelineCreateInfo
) {
140 .sType
= VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
,
141 .stageCount
= fs_nir
? 2 : 1,
142 .pStages
= (VkPipelineShaderStageCreateInfo
[]) {
144 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
145 .stage
= VK_SHADER_STAGE_VERTEX_BIT
,
146 .module
= anv_shader_module_to_handle(&vs_m
),
150 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
151 .stage
= VK_SHADER_STAGE_FRAGMENT_BIT
,
152 .module
= anv_shader_module_to_handle(&fs_m
),
156 .pVertexInputState
= vi_state
,
157 .pInputAssemblyState
= &(VkPipelineInputAssemblyStateCreateInfo
) {
158 .sType
= VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO
,
159 .topology
= VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
,
160 .primitiveRestartEnable
= false,
162 .pViewportState
= &(VkPipelineViewportStateCreateInfo
) {
163 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO
,
165 .pViewports
= NULL
, /* dynamic */
167 .pScissors
= NULL
, /* dynamic */
169 .pRasterizationState
= &(VkPipelineRasterizationStateCreateInfo
) {
170 .sType
= VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO
,
171 .rasterizerDiscardEnable
= false,
172 .polygonMode
= VK_POLYGON_MODE_FILL
,
173 .cullMode
= VK_CULL_MODE_NONE
,
174 .frontFace
= VK_FRONT_FACE_COUNTER_CLOCKWISE
,
175 .depthBiasEnable
= false,
177 .pMultisampleState
= &(VkPipelineMultisampleStateCreateInfo
) {
178 .sType
= VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO
,
179 .rasterizationSamples
= samples
,
180 .sampleShadingEnable
= false,
181 .pSampleMask
= (VkSampleMask
[]) { ~0 },
182 .alphaToCoverageEnable
= false,
183 .alphaToOneEnable
= false,
185 .pDepthStencilState
= ds_state
,
186 .pColorBlendState
= cb_state
,
187 .pDynamicState
= &(VkPipelineDynamicStateCreateInfo
) {
188 /* The meta clear pipeline declares all state as dynamic.
189 * As a consequence, vkCmdBindPipeline writes no dynamic state
190 * to the cmd buffer. Therefore, at the end of the meta clear,
191 * we need only restore dynamic state was vkCmdSet.
193 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO
,
194 .dynamicStateCount
= 8,
195 .pDynamicStates
= (VkDynamicState
[]) {
196 /* Everything except stencil write mask */
197 VK_DYNAMIC_STATE_VIEWPORT
,
198 VK_DYNAMIC_STATE_SCISSOR
,
199 VK_DYNAMIC_STATE_LINE_WIDTH
,
200 VK_DYNAMIC_STATE_DEPTH_BIAS
,
201 VK_DYNAMIC_STATE_BLEND_CONSTANTS
,
202 VK_DYNAMIC_STATE_DEPTH_BOUNDS
,
203 VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
,
204 VK_DYNAMIC_STATE_STENCIL_REFERENCE
,
208 .renderPass
= anv_render_pass_to_handle(&anv_meta_dummy_renderpass
),
211 &(struct anv_graphics_pipeline_create_info
) {
212 .color_attachment_count
= MAX_RTS
,
213 .use_repclear
= use_repclear
,
223 *pipeline
= anv_pipeline_from_handle(pipeline_h
);
229 create_color_pipeline(struct anv_device
*device
,
231 uint32_t frag_output
,
232 struct anv_pipeline
**pipeline
)
234 struct nir_shader
*vs_nir
;
235 struct nir_shader
*fs_nir
;
236 build_color_shaders(&vs_nir
, &fs_nir
, frag_output
);
238 const VkPipelineVertexInputStateCreateInfo vi_state
= {
239 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
240 .vertexBindingDescriptionCount
= 1,
241 .pVertexBindingDescriptions
= (VkVertexInputBindingDescription
[]) {
244 .stride
= sizeof(struct color_clear_vattrs
),
245 .inputRate
= VK_VERTEX_INPUT_RATE_VERTEX
248 .vertexAttributeDescriptionCount
= 3,
249 .pVertexAttributeDescriptions
= (VkVertexInputAttributeDescription
[]) {
254 .format
= VK_FORMAT_R32G32B32A32_UINT
,
255 .offset
= offsetof(struct color_clear_vattrs
, vue_header
),
261 .format
= VK_FORMAT_R32G32_SFLOAT
,
262 .offset
= offsetof(struct color_clear_vattrs
, position
),
268 .format
= VK_FORMAT_R32G32B32A32_SFLOAT
,
269 .offset
= offsetof(struct color_clear_vattrs
, color
),
274 const VkPipelineDepthStencilStateCreateInfo ds_state
= {
275 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO
,
276 .depthTestEnable
= false,
277 .depthWriteEnable
= false,
278 .depthBoundsTestEnable
= false,
279 .stencilTestEnable
= false,
282 VkPipelineColorBlendAttachmentState blend_attachment_state
[MAX_RTS
] = { 0 };
283 blend_attachment_state
[frag_output
] = (VkPipelineColorBlendAttachmentState
) {
284 .blendEnable
= false,
285 .colorWriteMask
= VK_COLOR_COMPONENT_A_BIT
|
286 VK_COLOR_COMPONENT_R_BIT
|
287 VK_COLOR_COMPONENT_G_BIT
|
288 VK_COLOR_COMPONENT_B_BIT
,
291 const VkPipelineColorBlendStateCreateInfo cb_state
= {
292 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
293 .logicOpEnable
= false,
294 .attachmentCount
= MAX_RTS
,
295 .pAttachments
= blend_attachment_state
298 /* Use the repclear shader. Since the NIR shader we are providing has
299 * exactly one output, that output will get compacted down to binding
300 * table entry 0. The hard-coded repclear shader is then exactly what
301 * we want regardless of what attachment we are actually clearing.
304 create_pipeline(device
, samples
, vs_nir
, fs_nir
, &vi_state
, &ds_state
,
305 &cb_state
, &device
->meta_state
.alloc
,
306 /*use_repclear*/ true, pipeline
);
310 destroy_pipeline(struct anv_device
*device
, struct anv_pipeline
*pipeline
)
315 ANV_CALL(DestroyPipeline
)(anv_device_to_handle(device
),
316 anv_pipeline_to_handle(pipeline
),
317 &device
->meta_state
.alloc
);
321 anv_device_finish_meta_clear_state(struct anv_device
*device
)
323 struct anv_meta_state
*state
= &device
->meta_state
;
325 for (uint32_t i
= 0; i
< ARRAY_SIZE(state
->clear
); ++i
) {
326 for (uint32_t j
= 0; j
< ARRAY_SIZE(state
->clear
[i
].color_pipelines
); ++j
) {
327 destroy_pipeline(device
, state
->clear
[i
].color_pipelines
[j
]);
330 destroy_pipeline(device
, state
->clear
[i
].depth_only_pipeline
);
331 destroy_pipeline(device
, state
->clear
[i
].stencil_only_pipeline
);
332 destroy_pipeline(device
, state
->clear
[i
].depthstencil_pipeline
);
337 emit_color_clear(struct anv_cmd_buffer
*cmd_buffer
,
338 const VkClearAttachment
*clear_att
,
339 const VkClearRect
*clear_rect
)
341 struct anv_device
*device
= cmd_buffer
->device
;
342 const struct anv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
343 const struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
344 const uint32_t subpass_att
= clear_att
->colorAttachment
;
345 const uint32_t pass_att
= subpass
->color_attachments
[subpass_att
];
346 const struct anv_image_view
*iview
= fb
->attachments
[pass_att
];
347 const uint32_t samples
= iview
->image
->samples
;
348 const uint32_t samples_log2
= ffs(samples
) - 1;
349 struct anv_pipeline
*pipeline
=
350 device
->meta_state
.clear
[samples_log2
].color_pipelines
[subpass_att
];
351 VkClearColorValue clear_value
= clear_att
->clearValue
.color
;
353 VkCommandBuffer cmd_buffer_h
= anv_cmd_buffer_to_handle(cmd_buffer
);
354 VkPipeline pipeline_h
= anv_pipeline_to_handle(pipeline
);
356 assert(samples_log2
< ARRAY_SIZE(device
->meta_state
.clear
));
357 assert(clear_att
->aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
358 assert(clear_att
->colorAttachment
< subpass
->color_count
);
360 const struct color_clear_vattrs vertex_data
[3] = {
364 clear_rect
->rect
.offset
.x
,
365 clear_rect
->rect
.offset
.y
,
367 .color
= clear_value
,
372 clear_rect
->rect
.offset
.x
+ clear_rect
->rect
.extent
.width
,
373 clear_rect
->rect
.offset
.y
,
375 .color
= clear_value
,
380 clear_rect
->rect
.offset
.x
+ clear_rect
->rect
.extent
.width
,
381 clear_rect
->rect
.offset
.y
+ clear_rect
->rect
.extent
.height
,
383 .color
= clear_value
,
387 struct anv_state state
=
388 anv_cmd_buffer_emit_dynamic(cmd_buffer
, vertex_data
, sizeof(vertex_data
), 16);
390 struct anv_buffer vertex_buffer
= {
392 .size
= sizeof(vertex_data
),
393 .bo
= &device
->dynamic_state_block_pool
.bo
,
394 .offset
= state
.offset
,
397 ANV_CALL(CmdBindVertexBuffers
)(cmd_buffer_h
, 0, 1,
398 (VkBuffer
[]) { anv_buffer_to_handle(&vertex_buffer
) },
399 (VkDeviceSize
[]) { 0 });
401 if (cmd_buffer
->state
.pipeline
!= pipeline
) {
402 ANV_CALL(CmdBindPipeline
)(cmd_buffer_h
, VK_PIPELINE_BIND_POINT_GRAPHICS
,
406 ANV_CALL(CmdDraw
)(cmd_buffer_h
, 3, 1, 0, 0);
411 build_depthstencil_shader(struct nir_shader
**out_vs
)
415 nir_builder_init_simple_shader(&vs_b
, NULL
, MESA_SHADER_VERTEX
, NULL
);
417 vs_b
.shader
->info
.name
= ralloc_strdup(vs_b
.shader
, "meta_clear_depthstencil_vs");
419 const struct glsl_type
*position_type
= glsl_vec4_type();
421 nir_variable
*vs_in_pos
=
422 nir_variable_create(vs_b
.shader
, nir_var_shader_in
, position_type
,
424 vs_in_pos
->data
.location
= VERT_ATTRIB_GENERIC0
;
426 nir_variable
*vs_out_pos
=
427 nir_variable_create(vs_b
.shader
, nir_var_shader_out
, position_type
,
429 vs_out_pos
->data
.location
= VARYING_SLOT_POS
;
431 nir_copy_var(&vs_b
, vs_out_pos
, vs_in_pos
);
433 *out_vs
= vs_b
.shader
;
437 create_depthstencil_pipeline(struct anv_device
*device
,
438 VkImageAspectFlags aspects
,
440 struct anv_pipeline
**pipeline
)
442 struct nir_shader
*vs_nir
;
444 build_depthstencil_shader(&vs_nir
);
446 const VkPipelineVertexInputStateCreateInfo vi_state
= {
447 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
448 .vertexBindingDescriptionCount
= 1,
449 .pVertexBindingDescriptions
= (VkVertexInputBindingDescription
[]) {
452 .stride
= sizeof(struct depthstencil_clear_vattrs
),
453 .inputRate
= VK_VERTEX_INPUT_RATE_VERTEX
456 .vertexAttributeDescriptionCount
= 2,
457 .pVertexAttributeDescriptions
= (VkVertexInputAttributeDescription
[]) {
462 .format
= VK_FORMAT_R32G32B32A32_UINT
,
463 .offset
= offsetof(struct depthstencil_clear_vattrs
, vue_header
),
469 .format
= VK_FORMAT_R32G32_SFLOAT
,
470 .offset
= offsetof(struct depthstencil_clear_vattrs
, position
),
475 const VkPipelineDepthStencilStateCreateInfo ds_state
= {
476 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO
,
477 .depthTestEnable
= (aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
),
478 .depthCompareOp
= VK_COMPARE_OP_ALWAYS
,
479 .depthWriteEnable
= (aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
),
480 .depthBoundsTestEnable
= false,
481 .stencilTestEnable
= (aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
),
483 .passOp
= VK_STENCIL_OP_REPLACE
,
484 .compareOp
= VK_COMPARE_OP_ALWAYS
,
485 .writeMask
= UINT32_MAX
,
486 .reference
= 0, /* dynamic */
488 .back
= { 0 /* dont care */ },
491 const VkPipelineColorBlendStateCreateInfo cb_state
= {
492 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
493 .logicOpEnable
= false,
494 .attachmentCount
= 0,
495 .pAttachments
= NULL
,
498 return create_pipeline(device
, samples
, vs_nir
, NULL
, &vi_state
, &ds_state
,
499 &cb_state
, &device
->meta_state
.alloc
,
500 /*use_repclear*/ true, pipeline
);
504 emit_depthstencil_clear(struct anv_cmd_buffer
*cmd_buffer
,
505 const VkClearAttachment
*clear_att
,
506 const VkClearRect
*clear_rect
)
508 struct anv_device
*device
= cmd_buffer
->device
;
509 struct anv_meta_state
*meta_state
= &device
->meta_state
;
510 const struct anv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
511 const struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
512 const uint32_t pass_att
= subpass
->depth_stencil_attachment
;
513 const struct anv_image_view
*iview
= fb
->attachments
[pass_att
];
514 const uint32_t samples
= iview
->image
->samples
;
515 const uint32_t samples_log2
= ffs(samples
) - 1;
516 VkClearDepthStencilValue clear_value
= clear_att
->clearValue
.depthStencil
;
517 VkImageAspectFlags aspects
= clear_att
->aspectMask
;
519 VkCommandBuffer cmd_buffer_h
= anv_cmd_buffer_to_handle(cmd_buffer
);
521 assert(samples_log2
< ARRAY_SIZE(meta_state
->clear
));
522 assert(aspects
== VK_IMAGE_ASPECT_DEPTH_BIT
||
523 aspects
== VK_IMAGE_ASPECT_STENCIL_BIT
||
524 aspects
== (VK_IMAGE_ASPECT_DEPTH_BIT
|
525 VK_IMAGE_ASPECT_STENCIL_BIT
));
526 assert(pass_att
!= VK_ATTACHMENT_UNUSED
);
528 const struct depthstencil_clear_vattrs vertex_data
[3] = {
532 clear_rect
->rect
.offset
.x
,
533 clear_rect
->rect
.offset
.y
,
539 clear_rect
->rect
.offset
.x
+ clear_rect
->rect
.extent
.width
,
540 clear_rect
->rect
.offset
.y
,
546 clear_rect
->rect
.offset
.x
+ clear_rect
->rect
.extent
.width
,
547 clear_rect
->rect
.offset
.y
+ clear_rect
->rect
.extent
.height
,
552 struct anv_state state
=
553 anv_cmd_buffer_emit_dynamic(cmd_buffer
, vertex_data
, sizeof(vertex_data
), 16);
555 struct anv_buffer vertex_buffer
= {
557 .size
= sizeof(vertex_data
),
558 .bo
= &device
->dynamic_state_block_pool
.bo
,
559 .offset
= state
.offset
,
562 ANV_CALL(CmdSetViewport
)(cmd_buffer_h
, 0, 1,
568 .height
= fb
->height
,
570 /* Ignored when clearing only stencil. */
571 .minDepth
= clear_value
.depth
,
572 .maxDepth
= clear_value
.depth
,
576 if (aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
577 ANV_CALL(CmdSetStencilReference
)(cmd_buffer_h
, VK_STENCIL_FACE_FRONT_BIT
,
578 clear_value
.stencil
);
581 ANV_CALL(CmdBindVertexBuffers
)(cmd_buffer_h
, 0, 1,
582 (VkBuffer
[]) { anv_buffer_to_handle(&vertex_buffer
) },
583 (VkDeviceSize
[]) { 0 });
585 struct anv_pipeline
*pipeline
;
587 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
588 pipeline
= meta_state
->clear
[samples_log2
].depthstencil_pipeline
;
590 case VK_IMAGE_ASPECT_DEPTH_BIT
:
591 pipeline
= meta_state
->clear
[samples_log2
].depth_only_pipeline
;
593 case VK_IMAGE_ASPECT_STENCIL_BIT
:
594 pipeline
= meta_state
->clear
[samples_log2
].stencil_only_pipeline
;
597 unreachable("expected depth or stencil aspect");
600 if (cmd_buffer
->state
.pipeline
!= pipeline
) {
601 ANV_CALL(CmdBindPipeline
)(cmd_buffer_h
, VK_PIPELINE_BIND_POINT_GRAPHICS
,
602 anv_pipeline_to_handle(pipeline
));
605 ANV_CALL(CmdDraw
)(cmd_buffer_h
, 3, 1, 0, 0);
609 anv_device_init_meta_clear_state(struct anv_device
*device
)
612 struct anv_meta_state
*state
= &device
->meta_state
;
614 zero(device
->meta_state
.clear
);
616 for (uint32_t i
= 0; i
< ARRAY_SIZE(state
->clear
); ++i
) {
617 uint32_t samples
= 1 << i
;
619 for (uint32_t j
= 0; j
< ARRAY_SIZE(state
->clear
[i
].color_pipelines
); ++j
) {
620 res
= create_color_pipeline(device
, samples
, /* frag_output */ j
,
621 &state
->clear
[i
].color_pipelines
[j
]);
622 if (res
!= VK_SUCCESS
)
626 res
= create_depthstencil_pipeline(device
,
627 VK_IMAGE_ASPECT_DEPTH_BIT
, samples
,
628 &state
->clear
[i
].depth_only_pipeline
);
629 if (res
!= VK_SUCCESS
)
632 res
= create_depthstencil_pipeline(device
,
633 VK_IMAGE_ASPECT_STENCIL_BIT
, samples
,
634 &state
->clear
[i
].stencil_only_pipeline
);
635 if (res
!= VK_SUCCESS
)
638 res
= create_depthstencil_pipeline(device
,
639 VK_IMAGE_ASPECT_DEPTH_BIT
|
640 VK_IMAGE_ASPECT_STENCIL_BIT
, samples
,
641 &state
->clear
[i
].depthstencil_pipeline
);
642 if (res
!= VK_SUCCESS
)
649 anv_device_finish_meta_clear_state(device
);
654 * The parameters mean that same as those in vkCmdClearAttachments.
657 emit_clear(struct anv_cmd_buffer
*cmd_buffer
,
658 const VkClearAttachment
*clear_att
,
659 const VkClearRect
*clear_rect
)
661 if (clear_att
->aspectMask
& VK_IMAGE_ASPECT_COLOR_BIT
) {
662 emit_color_clear(cmd_buffer
, clear_att
, clear_rect
);
664 assert(clear_att
->aspectMask
& (VK_IMAGE_ASPECT_DEPTH_BIT
|
665 VK_IMAGE_ASPECT_STENCIL_BIT
));
666 emit_depthstencil_clear(cmd_buffer
, clear_att
, clear_rect
);
671 subpass_needs_clear(const struct anv_cmd_buffer
*cmd_buffer
)
673 const struct anv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
674 uint32_t ds
= cmd_state
->subpass
->depth_stencil_attachment
;
676 for (uint32_t i
= 0; i
< cmd_state
->subpass
->color_count
; ++i
) {
677 uint32_t a
= cmd_state
->subpass
->color_attachments
[i
];
678 if (cmd_state
->attachments
[a
].pending_clear_aspects
) {
683 if (ds
!= VK_ATTACHMENT_UNUSED
&&
684 cmd_state
->attachments
[ds
].pending_clear_aspects
) {
692 * Emit any pending attachment clears for the current subpass.
694 * @see anv_attachment_state::pending_clear_aspects
697 anv_cmd_buffer_clear_subpass(struct anv_cmd_buffer
*cmd_buffer
)
699 struct anv_cmd_state
*cmd_state
= &cmd_buffer
->state
;
700 struct anv_meta_saved_state saved_state
;
702 if (!subpass_needs_clear(cmd_buffer
))
705 meta_clear_begin(&saved_state
, cmd_buffer
);
707 if (cmd_state
->framebuffer
->layers
> 1)
708 anv_finishme("clearing multi-layer framebuffer");
710 VkClearRect clear_rect
= {
711 .rect
= cmd_state
->render_area
,
713 .layerCount
= 1, /* FINISHME: clear multi-layer framebuffer */
716 for (uint32_t i
= 0; i
< cmd_state
->subpass
->color_count
; ++i
) {
717 uint32_t a
= cmd_state
->subpass
->color_attachments
[i
];
719 if (!cmd_state
->attachments
[a
].pending_clear_aspects
)
722 assert(cmd_state
->attachments
[a
].pending_clear_aspects
==
723 VK_IMAGE_ASPECT_COLOR_BIT
);
725 VkClearAttachment clear_att
= {
726 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
727 .colorAttachment
= i
, /* Use attachment index relative to subpass */
728 .clearValue
= cmd_state
->attachments
[a
].clear_value
,
731 emit_clear(cmd_buffer
, &clear_att
, &clear_rect
);
732 cmd_state
->attachments
[a
].pending_clear_aspects
= 0;
735 uint32_t ds
= cmd_state
->subpass
->depth_stencil_attachment
;
737 if (ds
!= VK_ATTACHMENT_UNUSED
&&
738 cmd_state
->attachments
[ds
].pending_clear_aspects
) {
740 VkClearAttachment clear_att
= {
741 .aspectMask
= cmd_state
->attachments
[ds
].pending_clear_aspects
,
742 .clearValue
= cmd_state
->attachments
[ds
].clear_value
,
745 emit_clear(cmd_buffer
, &clear_att
, &clear_rect
);
746 cmd_state
->attachments
[ds
].pending_clear_aspects
= 0;
749 meta_clear_end(&saved_state
, cmd_buffer
);
753 anv_cmd_clear_image(struct anv_cmd_buffer
*cmd_buffer
,
754 struct anv_image
*image
,
755 VkImageLayout image_layout
,
756 const VkClearValue
*clear_value
,
757 uint32_t range_count
,
758 const VkImageSubresourceRange
*ranges
)
760 VkDevice device_h
= anv_device_to_handle(cmd_buffer
->device
);
762 for (uint32_t r
= 0; r
< range_count
; r
++) {
763 const VkImageSubresourceRange
*range
= &ranges
[r
];
764 for (uint32_t l
= 0; l
< anv_get_levelCount(image
, range
); ++l
) {
765 const uint32_t layer_count
= image
->type
== VK_IMAGE_TYPE_3D
?
766 anv_minify(image
->extent
.depth
, l
) :
767 anv_get_layerCount(image
, range
);
768 for (uint32_t s
= 0; s
< layer_count
; ++s
) {
769 struct anv_image_view iview
;
770 anv_image_view_init(&iview
, cmd_buffer
->device
,
771 &(VkImageViewCreateInfo
) {
772 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
773 .image
= anv_image_to_handle(image
),
774 .viewType
= anv_meta_get_view_type(image
),
775 .format
= image
->vk_format
,
776 .subresourceRange
= {
777 .aspectMask
= range
->aspectMask
,
778 .baseMipLevel
= range
->baseMipLevel
+ l
,
780 .baseArrayLayer
= range
->baseArrayLayer
+ s
,
784 cmd_buffer
, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
);
787 anv_CreateFramebuffer(device_h
,
788 &(VkFramebufferCreateInfo
) {
789 .sType
= VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
,
790 .attachmentCount
= 1,
791 .pAttachments
= (VkImageView
[]) {
792 anv_image_view_to_handle(&iview
),
794 .width
= iview
.extent
.width
,
795 .height
= iview
.extent
.height
,
798 &cmd_buffer
->pool
->alloc
,
801 VkAttachmentDescription att_desc
= {
802 .format
= iview
.vk_format
,
803 .loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
804 .storeOp
= VK_ATTACHMENT_STORE_OP_STORE
,
805 .stencilLoadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
806 .stencilStoreOp
= VK_ATTACHMENT_STORE_OP_STORE
,
807 .initialLayout
= image_layout
,
808 .finalLayout
= image_layout
,
811 VkSubpassDescription subpass_desc
= {
812 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
813 .inputAttachmentCount
= 0,
814 .colorAttachmentCount
= 0,
815 .pColorAttachments
= NULL
,
816 .pResolveAttachments
= NULL
,
817 .pDepthStencilAttachment
= NULL
,
818 .preserveAttachmentCount
= 0,
819 .pPreserveAttachments
= NULL
,
822 const VkAttachmentReference att_ref
= {
824 .layout
= image_layout
,
827 if (range
->aspectMask
& VK_IMAGE_ASPECT_COLOR_BIT
) {
828 subpass_desc
.colorAttachmentCount
= 1;
829 subpass_desc
.pColorAttachments
= &att_ref
;
831 subpass_desc
.pDepthStencilAttachment
= &att_ref
;
835 anv_CreateRenderPass(device_h
,
836 &(VkRenderPassCreateInfo
) {
837 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
838 .attachmentCount
= 1,
839 .pAttachments
= &att_desc
,
841 .pSubpasses
= &subpass_desc
,
843 &cmd_buffer
->pool
->alloc
,
846 ANV_CALL(CmdBeginRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
),
847 &(VkRenderPassBeginInfo
) {
848 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO
,
852 .width
= iview
.extent
.width
,
853 .height
= iview
.extent
.height
,
858 .clearValueCount
= 0,
859 .pClearValues
= NULL
,
861 VK_SUBPASS_CONTENTS_INLINE
);
863 VkClearAttachment clear_att
= {
864 .aspectMask
= range
->aspectMask
,
865 .colorAttachment
= 0,
866 .clearValue
= *clear_value
,
869 VkClearRect clear_rect
= {
872 .extent
= { iview
.extent
.width
, iview
.extent
.height
},
874 .baseArrayLayer
= range
->baseArrayLayer
,
875 .layerCount
= 1, /* FINISHME: clear multi-layer framebuffer */
878 emit_clear(cmd_buffer
, &clear_att
, &clear_rect
);
880 ANV_CALL(CmdEndRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
));
881 ANV_CALL(DestroyRenderPass
)(device_h
, pass
,
882 &cmd_buffer
->pool
->alloc
);
883 ANV_CALL(DestroyFramebuffer
)(device_h
, fb
,
884 &cmd_buffer
->pool
->alloc
);
890 void anv_CmdClearColorImage(
891 VkCommandBuffer commandBuffer
,
893 VkImageLayout imageLayout
,
894 const VkClearColorValue
* pColor
,
896 const VkImageSubresourceRange
* pRanges
)
898 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
899 ANV_FROM_HANDLE(anv_image
, image
, image_h
);
900 struct anv_meta_saved_state saved_state
;
902 meta_clear_begin(&saved_state
, cmd_buffer
);
904 anv_cmd_clear_image(cmd_buffer
, image
, imageLayout
,
905 (const VkClearValue
*) pColor
,
906 rangeCount
, pRanges
);
908 meta_clear_end(&saved_state
, cmd_buffer
);
911 void anv_CmdClearDepthStencilImage(
912 VkCommandBuffer commandBuffer
,
914 VkImageLayout imageLayout
,
915 const VkClearDepthStencilValue
* pDepthStencil
,
917 const VkImageSubresourceRange
* pRanges
)
919 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
920 ANV_FROM_HANDLE(anv_image
, image
, image_h
);
921 struct anv_meta_saved_state saved_state
;
923 meta_clear_begin(&saved_state
, cmd_buffer
);
925 anv_cmd_clear_image(cmd_buffer
, image
, imageLayout
,
926 (const VkClearValue
*) pDepthStencil
,
927 rangeCount
, pRanges
);
929 meta_clear_end(&saved_state
, cmd_buffer
);
932 void anv_CmdClearAttachments(
933 VkCommandBuffer commandBuffer
,
934 uint32_t attachmentCount
,
935 const VkClearAttachment
* pAttachments
,
937 const VkClearRect
* pRects
)
939 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
940 struct anv_meta_saved_state saved_state
;
942 meta_clear_begin(&saved_state
, cmd_buffer
);
944 /* FINISHME: We can do better than this dumb loop. It thrashes too much
947 for (uint32_t a
= 0; a
< attachmentCount
; ++a
) {
948 for (uint32_t r
= 0; r
< rectCount
; ++r
) {
949 emit_clear(cmd_buffer
, &pAttachments
[a
], &pRects
[r
]);
953 meta_clear_end(&saved_state
, cmd_buffer
);
957 do_buffer_fill(struct anv_cmd_buffer
*cmd_buffer
,
958 struct anv_bo
*dest
, uint64_t dest_offset
,
959 int width
, int height
, VkFormat fill_format
, uint32_t data
)
961 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
963 VkImageCreateInfo image_info
= {
964 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
965 .imageType
= VK_IMAGE_TYPE_2D
,
966 .format
= fill_format
,
975 .tiling
= VK_IMAGE_TILING_LINEAR
,
976 .usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
,
981 image_info
.usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
982 anv_CreateImage(vk_device
, &image_info
,
983 &cmd_buffer
->pool
->alloc
, &dest_image
);
985 /* We could use a vk call to bind memory, but that would require
986 * creating a dummy memory object etc. so there's really no point.
988 anv_image_from_handle(dest_image
)->bo
= dest
;
989 anv_image_from_handle(dest_image
)->offset
= dest_offset
;
991 const VkClearValue clear_value
= {
993 .uint32
= { data
, data
, data
, data
}
997 const VkImageSubresourceRange range
= {
998 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1001 .baseArrayLayer
= 0,
1005 anv_cmd_clear_image(cmd_buffer
, anv_image_from_handle(dest_image
),
1006 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
,
1007 &clear_value
, 1, &range
);
1010 void anv_CmdFillBuffer(
1011 VkCommandBuffer commandBuffer
,
1013 VkDeviceSize dstOffset
,
1014 VkDeviceSize fillSize
,
1017 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1018 ANV_FROM_HANDLE(anv_buffer
, dst_buffer
, dstBuffer
);
1019 struct anv_meta_saved_state saved_state
;
1021 meta_clear_begin(&saved_state
, cmd_buffer
);
1025 if ((fillSize
& 15) == 0 && (dstOffset
& 15) == 0) {
1026 format
= VK_FORMAT_R32G32B32A32_UINT
;
1028 } else if ((fillSize
& 7) == 0 && (dstOffset
& 15) == 0) {
1029 format
= VK_FORMAT_R32G32_UINT
;
1032 assert((fillSize
& 3) == 0 && (dstOffset
& 3) == 0);
1033 format
= VK_FORMAT_R32_UINT
;
1037 /* This is maximum possible width/height our HW can handle */
1038 const uint64_t max_surface_dim
= 1 << 14;
1040 /* First, we make a bunch of max-sized copies */
1041 const uint64_t max_fill_size
= max_surface_dim
* max_surface_dim
* bs
;
1042 while (fillSize
> max_fill_size
) {
1043 do_buffer_fill(cmd_buffer
, dst_buffer
->bo
,
1044 dst_buffer
->offset
+ dstOffset
,
1045 max_surface_dim
, max_surface_dim
, format
, data
);
1046 fillSize
-= max_fill_size
;
1047 dstOffset
+= max_fill_size
;
1050 uint64_t height
= fillSize
/ (max_surface_dim
* bs
);
1051 assert(height
< max_surface_dim
);
1053 const uint64_t rect_fill_size
= height
* max_surface_dim
* bs
;
1054 do_buffer_fill(cmd_buffer
, dst_buffer
->bo
,
1055 dst_buffer
->offset
+ dstOffset
,
1056 max_surface_dim
, height
, format
, data
);
1057 fillSize
-= rect_fill_size
;
1058 dstOffset
+= rect_fill_size
;
1061 if (fillSize
!= 0) {
1062 do_buffer_fill(cmd_buffer
, dst_buffer
->bo
,
1063 dst_buffer
->offset
+ dstOffset
,
1064 fillSize
/ bs
, 1, format
, data
);
1067 meta_clear_end(&saved_state
, cmd_buffer
);