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
32 #define GLSL(src) "#version 330\n" #src
35 anv_device_init_meta_clear_state(struct anv_device
*device
)
37 VkPipelineIaStateCreateInfo ia_create_info
= {
38 .sType
= VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO
,
39 .topology
= VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
,
40 .disableVertexReuse
= false,
41 .primitiveRestartEnable
= false,
42 .primitiveRestartIndex
= 0
45 /* We don't use a vertex shader for clearing, but instead build and pass
46 * the VUEs directly to the rasterization backend.
48 static const char fs_source
[] = GLSL(
57 vkCreateShader((VkDevice
) device
,
58 &(VkShaderCreateInfo
) {
59 .sType
= VK_STRUCTURE_TYPE_SHADER_CREATE_INFO
,
60 .codeSize
= sizeof(fs_source
),
66 VkPipelineShaderStageCreateInfo fs_create_info
= {
67 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
68 .pNext
= &ia_create_info
,
70 .stage
= VK_SHADER_STAGE_FRAGMENT
,
72 .linkConstBufferCount
= 0,
73 .pLinkConstBufferInfo
= NULL
,
74 .pSpecializationInfo
= NULL
78 /* We use instanced rendering to clear multiple render targets. We have two
79 * vertex buffers: the first vertex buffer holds per-vertex data and
80 * provides the vertices for the clear rectangle. The second one holds
81 * per-instance data, which consists of the VUE header (which selects the
82 * layer) and the color (Vulkan supports per-RT clear colors).
84 VkPipelineVertexInputCreateInfo vi_create_info
= {
85 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO
,
86 .pNext
= &fs_create_info
,
88 .pVertexBindingDescriptions
= (VkVertexInputBindingDescription
[]) {
92 .stepRate
= VK_VERTEX_INPUT_STEP_RATE_VERTEX
97 .stepRate
= VK_VERTEX_INPUT_STEP_RATE_INSTANCE
101 .pVertexAttributeDescriptions
= (VkVertexInputAttributeDescription
[]) {
106 .format
= VK_FORMAT_R32G32B32A32_UINT
,
113 .format
= VK_FORMAT_R32G32_SFLOAT
,
120 .format
= VK_FORMAT_R32G32B32A32_SFLOAT
,
126 VkPipelineRsStateCreateInfo rs_create_info
= {
127 .sType
= VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO
,
128 .pNext
= &vi_create_info
,
129 .depthClipEnable
= true,
130 .rasterizerDiscardEnable
= false,
131 .fillMode
= VK_FILL_MODE_SOLID
,
132 .cullMode
= VK_CULL_MODE_NONE
,
133 .frontFace
= VK_FRONT_FACE_CCW
136 anv_pipeline_create((VkDevice
) device
,
137 &(VkGraphicsPipelineCreateInfo
) {
138 .sType
= VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
,
139 .pNext
= &rs_create_info
,
143 &(struct anv_pipeline_create_info
) {
144 .use_repclear
= true,
145 .disable_viewport
= true,
148 &device
->clear_state
.pipeline
);
150 vkDestroyObject((VkDevice
) device
, VK_OBJECT_TYPE_SHADER
, fs
);
152 vkCreateDynamicRasterState((VkDevice
) device
,
153 &(VkDynamicRsStateCreateInfo
) {
154 .sType
= VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO
,
156 &device
->clear_state
.rs_state
);
159 struct anv_saved_state
{
161 struct anv_buffer
*buffer
;
164 struct anv_descriptor_set
*dsets
[1];
165 struct anv_pipeline
*pipeline
;
169 anv_cmd_buffer_save(struct anv_cmd_buffer
*cmd_buffer
,
170 struct anv_saved_state
*state
)
172 memcpy(state
->vb
, cmd_buffer
->vb
, sizeof(state
->vb
));
173 memcpy(state
->dsets
, cmd_buffer
->descriptor_sets
, sizeof(state
->dsets
));
174 state
->pipeline
= cmd_buffer
->pipeline
;
178 anv_cmd_buffer_restore(struct anv_cmd_buffer
*cmd_buffer
,
179 const struct anv_saved_state
*state
)
181 memcpy(cmd_buffer
->vb
, state
->vb
, sizeof(state
->vb
));
182 memcpy(cmd_buffer
->descriptor_sets
, state
->dsets
, sizeof(state
->dsets
));
183 cmd_buffer
->pipeline
= state
->pipeline
;
185 cmd_buffer
->vb_dirty
|= (1 << ARRAY_SIZE(state
->vb
)) - 1;
186 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_PIPELINE_DIRTY
|
187 ANV_CMD_BUFFER_DESCRIPTOR_SET_DIRTY
;
191 anv_cmd_buffer_clear(struct anv_cmd_buffer
*cmd_buffer
,
192 struct anv_render_pass
*pass
)
194 struct anv_device
*device
= cmd_buffer
->device
;
195 struct anv_framebuffer
*fb
= cmd_buffer
->framebuffer
;
196 struct anv_saved_state saved_state
;
197 struct anv_state state
;
200 struct instance_data
{
204 uint32_t ViewportIndex
;
210 const float vertex_data
[] = {
211 /* Rect-list coordinates */
214 fb
->width
, fb
->height
,
216 /* Align to 16 bytes */
220 size
= sizeof(vertex_data
) + pass
->num_clear_layers
* sizeof(instance_data
[0]);
221 state
= anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
, size
, 16);
223 memcpy(state
.map
, vertex_data
, sizeof(vertex_data
));
224 instance_data
= state
.map
+ sizeof(vertex_data
);
226 for (uint32_t i
= 0; i
< pass
->num_layers
; i
++) {
227 if (pass
->layers
[i
].color_load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
228 *instance_data
++ = (struct instance_data
) {
235 pass
->layers
[i
].clear_color
.color
.floatColor
[0],
236 pass
->layers
[i
].clear_color
.color
.floatColor
[1],
237 pass
->layers
[i
].clear_color
.color
.floatColor
[2],
238 pass
->layers
[i
].clear_color
.color
.floatColor
[3],
244 struct anv_buffer vertex_buffer
= {
245 .device
= cmd_buffer
->device
,
247 .bo
= &device
->surface_state_block_pool
.bo
,
248 .offset
= state
.offset
251 anv_cmd_buffer_save(cmd_buffer
, &saved_state
);
253 vkCmdBindVertexBuffers((VkCmdBuffer
) cmd_buffer
, 0, 2,
255 (VkBuffer
) &vertex_buffer
,
256 (VkBuffer
) &vertex_buffer
263 if ((VkPipeline
) cmd_buffer
->pipeline
!= device
->clear_state
.pipeline
)
264 vkCmdBindPipeline((VkCmdBuffer
) cmd_buffer
,
265 VK_PIPELINE_BIND_POINT_GRAPHICS
, device
->clear_state
.pipeline
);
267 /* We don't need anything here, only set if not already set. */
268 if (cmd_buffer
->rs_state
== NULL
)
269 vkCmdBindDynamicStateObject((VkCmdBuffer
) cmd_buffer
,
270 VK_STATE_BIND_POINT_RASTER
,
271 device
->clear_state
.rs_state
);
273 if (cmd_buffer
->vp_state
== NULL
)
274 vkCmdBindDynamicStateObject((VkCmdBuffer
) cmd_buffer
,
275 VK_STATE_BIND_POINT_VIEWPORT
,
276 cmd_buffer
->framebuffer
->vp_state
);
278 vkCmdDraw((VkCmdBuffer
) cmd_buffer
, 0, 3, 0, pass
->num_clear_layers
);
280 /* Restore API state */
281 anv_cmd_buffer_restore(cmd_buffer
, &saved_state
);
285 void VKAPI
vkCmdCopyBuffer(
286 VkCmdBuffer cmdBuffer
,
289 uint32_t regionCount
,
290 const VkBufferCopy
* pRegions
)
295 void VKAPI
vkCmdCopyImage(
296 VkCmdBuffer cmdBuffer
,
298 VkImageLayout srcImageLayout
,
300 VkImageLayout destImageLayout
,
301 uint32_t regionCount
,
302 const VkImageCopy
* pRegions
)
307 void VKAPI
vkCmdBlitImage(
308 VkCmdBuffer cmdBuffer
,
310 VkImageLayout srcImageLayout
,
312 VkImageLayout destImageLayout
,
313 uint32_t regionCount
,
314 const VkImageBlit
* pRegions
)
319 void VKAPI
vkCmdCopyBufferToImage(
320 VkCmdBuffer cmdBuffer
,
323 VkImageLayout destImageLayout
,
324 uint32_t regionCount
,
325 const VkBufferImageCopy
* pRegions
)
330 void VKAPI
vkCmdCopyImageToBuffer(
331 VkCmdBuffer cmdBuffer
,
333 VkImageLayout srcImageLayout
,
335 uint32_t regionCount
,
336 const VkBufferImageCopy
* pRegions
)
341 void VKAPI
vkCmdCloneImageData(
342 VkCmdBuffer cmdBuffer
,
344 VkImageLayout srcImageLayout
,
346 VkImageLayout destImageLayout
)
351 void VKAPI
vkCmdUpdateBuffer(
352 VkCmdBuffer cmdBuffer
,
354 VkDeviceSize destOffset
,
355 VkDeviceSize dataSize
,
356 const uint32_t* pData
)
361 void VKAPI
vkCmdFillBuffer(
362 VkCmdBuffer cmdBuffer
,
364 VkDeviceSize destOffset
,
365 VkDeviceSize fillSize
,
371 void VKAPI
vkCmdClearColorImage(
372 VkCmdBuffer cmdBuffer
,
374 VkImageLayout imageLayout
,
375 const VkClearColor
* color
,
377 const VkImageSubresourceRange
* pRanges
)
382 void VKAPI
vkCmdClearDepthStencil(
383 VkCmdBuffer cmdBuffer
,
385 VkImageLayout imageLayout
,
389 const VkImageSubresourceRange
* pRanges
)
394 void VKAPI
vkCmdResolveImage(
395 VkCmdBuffer cmdBuffer
,
397 VkImageLayout srcImageLayout
,
399 VkImageLayout destImageLayout
,
400 uint32_t regionCount
,
401 const VkImageResolve
* pRegions
)
407 anv_device_init_meta(struct anv_device
*device
)
409 anv_device_init_meta_clear_state(device
);