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(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_pipeline
*pipeline
;
168 anv_cmd_buffer_save(struct anv_cmd_buffer
*cmd_buffer
, struct anv_saved_state
*state
)
170 memcpy(state
->vb
, cmd_buffer
->vb
, sizeof(state
->vb
));
171 state
->pipeline
= cmd_buffer
->pipeline
;
175 anv_cmd_buffer_restore(struct anv_cmd_buffer
*cmd_buffer
, struct anv_saved_state
*state
)
177 memcpy(cmd_buffer
->vb
, state
->vb
, sizeof(state
->vb
));
178 cmd_buffer
->pipeline
= state
->pipeline
;
180 cmd_buffer
->vb_dirty
|= (1 << ARRAY_SIZE(state
->vb
)) - 1;
181 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_PIPELINE_DIRTY
;
185 anv_cmd_buffer_clear(struct anv_cmd_buffer
*cmd_buffer
,
186 struct anv_render_pass
*pass
)
188 struct anv_device
*device
= cmd_buffer
->device
;
189 struct anv_framebuffer
*fb
= cmd_buffer
->framebuffer
;
190 struct anv_saved_state saved_state
;
191 struct anv_state state
;
194 struct instance_data
{
198 uint32_t ViewportIndex
;
204 const float vertex_data
[] = {
205 /* Rect-list coordinates */
208 fb
->width
, fb
->height
,
210 /* Align to 16 bytes */
214 size
= sizeof(vertex_data
) + pass
->num_clear_layers
* sizeof(instance_data
[0]);
215 state
= anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
, size
, 16);
217 memcpy(state
.map
, vertex_data
, sizeof(vertex_data
));
218 instance_data
= state
.map
+ sizeof(vertex_data
);
220 for (uint32_t i
= 0; i
< pass
->num_layers
; i
++) {
221 if (pass
->layers
[i
].color_load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
222 *instance_data
++ = (struct instance_data
) {
229 pass
->layers
[i
].clear_color
.color
.floatColor
[0],
230 pass
->layers
[i
].clear_color
.color
.floatColor
[1],
231 pass
->layers
[i
].clear_color
.color
.floatColor
[2],
232 pass
->layers
[i
].clear_color
.color
.floatColor
[3],
238 struct anv_buffer vertex_buffer
= {
239 .device
= cmd_buffer
->device
,
241 .bo
= &device
->surface_state_block_pool
.bo
,
242 .offset
= state
.offset
245 anv_cmd_buffer_save(cmd_buffer
, &saved_state
);
247 vkCmdBindVertexBuffers((VkCmdBuffer
) cmd_buffer
, 0, 2,
249 (VkBuffer
) &vertex_buffer
,
250 (VkBuffer
) &vertex_buffer
257 if ((VkPipeline
) cmd_buffer
->pipeline
!= device
->clear_state
.pipeline
)
258 vkCmdBindPipeline((VkCmdBuffer
) cmd_buffer
,
259 VK_PIPELINE_BIND_POINT_GRAPHICS
, device
->clear_state
.pipeline
);
261 /* We don't need anything here, only set if not already set. */
262 if (cmd_buffer
->rs_state
== NULL
)
263 vkCmdBindDynamicStateObject((VkCmdBuffer
) cmd_buffer
,
264 VK_STATE_BIND_POINT_RASTER
,
265 device
->clear_state
.rs_state
);
267 if (cmd_buffer
->vp_state
== NULL
)
268 vkCmdBindDynamicStateObject((VkCmdBuffer
) cmd_buffer
,
269 VK_STATE_BIND_POINT_VIEWPORT
,
270 cmd_buffer
->framebuffer
->vp_state
);
272 vkCmdDraw((VkCmdBuffer
) cmd_buffer
, 0, 3, 0, pass
->num_clear_layers
);
274 /* Restore API state */
275 anv_cmd_buffer_restore(cmd_buffer
, &saved_state
);
279 void VKAPI
vkCmdCopyBuffer(
280 VkCmdBuffer cmdBuffer
,
283 uint32_t regionCount
,
284 const VkBufferCopy
* pRegions
)
288 void VKAPI
vkCmdCopyImage(
289 VkCmdBuffer cmdBuffer
,
291 VkImageLayout srcImageLayout
,
293 VkImageLayout destImageLayout
,
294 uint32_t regionCount
,
295 const VkImageCopy
* pRegions
)
299 void VKAPI
vkCmdBlitImage(
300 VkCmdBuffer cmdBuffer
,
302 VkImageLayout srcImageLayout
,
304 VkImageLayout destImageLayout
,
305 uint32_t regionCount
,
306 const VkImageBlit
* pRegions
)
310 void VKAPI
vkCmdCopyBufferToImage(
311 VkCmdBuffer cmdBuffer
,
314 VkImageLayout destImageLayout
,
315 uint32_t regionCount
,
316 const VkBufferImageCopy
* pRegions
)
320 void VKAPI
vkCmdCopyImageToBuffer(
321 VkCmdBuffer cmdBuffer
,
323 VkImageLayout srcImageLayout
,
325 uint32_t regionCount
,
326 const VkBufferImageCopy
* pRegions
)
330 void VKAPI
vkCmdCloneImageData(
331 VkCmdBuffer cmdBuffer
,
333 VkImageLayout srcImageLayout
,
335 VkImageLayout destImageLayout
)
339 void VKAPI
vkCmdUpdateBuffer(
340 VkCmdBuffer cmdBuffer
,
342 VkDeviceSize destOffset
,
343 VkDeviceSize dataSize
,
344 const uint32_t* pData
)
348 void VKAPI
vkCmdFillBuffer(
349 VkCmdBuffer cmdBuffer
,
351 VkDeviceSize destOffset
,
352 VkDeviceSize fillSize
,
357 void VKAPI
vkCmdClearColorImage(
358 VkCmdBuffer cmdBuffer
,
360 VkImageLayout imageLayout
,
361 const VkClearColor
* color
,
363 const VkImageSubresourceRange
* pRanges
)
367 void VKAPI
vkCmdClearDepthStencil(
368 VkCmdBuffer cmdBuffer
,
370 VkImageLayout imageLayout
,
374 const VkImageSubresourceRange
* pRanges
)
378 void VKAPI
vkCmdResolveImage(
379 VkCmdBuffer cmdBuffer
,
381 VkImageLayout srcImageLayout
,
383 VkImageLayout destImageLayout
,
384 uint32_t regionCount
,
385 const VkImageResolve
* pRegions
)