blob: 4ce45feb9e64f28e9f6969dca23a50c5864bd8ab [file] [log] [blame]
Chad Versace16b2a482015-11-03 14:19:45 -08001/*
2 * Copyright © 2015 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24#include "anv_meta.h"
25#include "anv_meta_clear.h"
26#include "anv_nir_builder.h"
27#include "anv_private.h"
28
Chad Versacea9a30712015-11-03 14:55:58 -080029/** Vertex attributes for color clears. */
30struct color_clear_vattrs {
Chad Versace16b2a482015-11-03 14:19:45 -080031 struct anv_vue_header vue_header;
Chad Versacea9a30712015-11-03 14:55:58 -080032 float position[2]; /**< 3DPRIM_RECTLIST */
Chad Versace16b2a482015-11-03 14:19:45 -080033 VkClearColorValue color;
34};
35
Chad Versace16119ad2015-11-04 17:00:01 -080036/** Vertex attributes for depthstencil clears. */
37struct depthstencil_clear_vattrs {
Chad Versacea9a30712015-11-03 14:55:58 -080038 struct anv_vue_header vue_header;
39 float position[2]; /*<< 3DPRIM_RECTLIST */
40};
41
Chad Versace16b2a482015-11-03 14:19:45 -080042static void
Chad Versacea9a30712015-11-03 14:55:58 -080043meta_clear_begin(struct anv_meta_saved_state *saved_state,
44 struct anv_cmd_buffer *cmd_buffer)
Chad Versace16b2a482015-11-03 14:19:45 -080045{
Chad Versacea9a30712015-11-03 14:55:58 -080046 anv_meta_save(saved_state, cmd_buffer,
47 (1 << VK_DYNAMIC_STATE_VIEWPORT) |
Chad Versace16119ad2015-11-04 17:00:01 -080048 (1 << VK_DYNAMIC_STATE_SCISSOR) |
49 (1 << VK_DYNAMIC_STATE_STENCIL_REFERENCE));
Chad Versace16b2a482015-11-03 14:19:45 -080050
Chad Versace16b2a482015-11-03 14:19:45 -080051 cmd_buffer->state.dynamic.viewport.count = 0;
Chad Versacea9a30712015-11-03 14:55:58 -080052 cmd_buffer->state.dynamic.scissor.count = 0;
Chad Versace16b2a482015-11-03 14:19:45 -080053}
54
Chad Versacea9a30712015-11-03 14:55:58 -080055static void
56meta_clear_end(struct anv_meta_saved_state *saved_state,
57 struct anv_cmd_buffer *cmd_buffer)
Chad Versace16b2a482015-11-03 14:19:45 -080058{
Chad Versacea9a30712015-11-03 14:55:58 -080059 anv_meta_restore(saved_state, cmd_buffer);
Chad Versace16b2a482015-11-03 14:19:45 -080060}
61
Chad Versacea9a30712015-11-03 14:55:58 -080062static void
63build_color_shaders(struct nir_shader **out_vs,
64 struct nir_shader **out_fs)
Chad Versace16b2a482015-11-03 14:19:45 -080065{
Chad Versacea9a30712015-11-03 14:55:58 -080066 nir_builder vs_b;
67 nir_builder fs_b;
Chad Versace16b2a482015-11-03 14:19:45 -080068
Chad Versacea9a30712015-11-03 14:55:58 -080069 nir_builder_init_simple_shader(&vs_b, MESA_SHADER_VERTEX);
70 nir_builder_init_simple_shader(&fs_b, MESA_SHADER_FRAGMENT);
71
72 const struct glsl_type *position_type = glsl_vec4_type();
Chad Versace16b2a482015-11-03 14:19:45 -080073 const struct glsl_type *color_type = glsl_vec4_type();
74
Chad Versacea9a30712015-11-03 14:55:58 -080075 nir_variable *vs_in_pos =
76 nir_variable_create(vs_b.shader, nir_var_shader_in, position_type,
77 "a_position");
78 vs_in_pos->data.location = VERT_ATTRIB_GENERIC0;
Chad Versace16b2a482015-11-03 14:19:45 -080079
Chad Versacea9a30712015-11-03 14:55:58 -080080 nir_variable *vs_out_pos =
81 nir_variable_create(vs_b.shader, nir_var_shader_out, position_type,
82 "gl_Position");
83 vs_out_pos->data.location = VARYING_SLOT_POS;
Chad Versace16b2a482015-11-03 14:19:45 -080084
Chad Versacea9a30712015-11-03 14:55:58 -080085 nir_variable *vs_in_color =
86 nir_variable_create(vs_b.shader, nir_var_shader_in, color_type,
87 "a_color");
88 vs_in_color->data.location = VERT_ATTRIB_GENERIC1;
89
90 nir_variable *vs_out_color =
91 nir_variable_create(vs_b.shader, nir_var_shader_out, color_type,
92 "v_color");
93 vs_out_color->data.location = VARYING_SLOT_VAR0;
94 vs_out_color->data.interpolation = INTERP_QUALIFIER_FLAT;
95
96 nir_variable *fs_in_color =
97 nir_variable_create(fs_b.shader, nir_var_shader_in, color_type,
98 "v_color");
99 fs_in_color->data.location = vs_out_color->data.location;
100 fs_in_color->data.interpolation = vs_out_color->data.interpolation;
101
102 nir_variable *fs_out_color =
103 nir_variable_create(fs_b.shader, nir_var_shader_out, color_type,
104 "f_color");
105 fs_out_color->data.location = FRAG_RESULT_DATA0;
106
107 nir_copy_var(&vs_b, vs_out_pos, vs_in_pos);
108 nir_copy_var(&vs_b, vs_out_color, vs_in_color);
109 nir_copy_var(&fs_b, fs_out_color, fs_in_color);
110
111 *out_vs = vs_b.shader;
112 *out_fs = fs_b.shader;
Chad Versace16b2a482015-11-03 14:19:45 -0800113}
114
Chad Versacea9a30712015-11-03 14:55:58 -0800115static struct anv_pipeline *
116create_pipeline(struct anv_device *device,
117 struct nir_shader *vs_nir,
118 struct nir_shader *fs_nir,
119 const VkPipelineVertexInputStateCreateInfo *vi_state,
120 const VkPipelineDepthStencilStateCreateInfo *ds_state,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800121 const VkPipelineColorBlendStateCreateInfo *cb_state,
122 const VkAllocationCallbacks *alloc)
Chad Versace16b2a482015-11-03 14:19:45 -0800123{
Chad Versacea9a30712015-11-03 14:55:58 -0800124 VkDevice device_h = anv_device_to_handle(device);
Chad Versace16b2a482015-11-03 14:19:45 -0800125
Chad Versacea9a30712015-11-03 14:55:58 -0800126 struct anv_shader_module vs_m = { .nir = vs_nir };
127 struct anv_shader_module fs_m = { .nir = fs_nir };
Chad Versace16b2a482015-11-03 14:19:45 -0800128
Chad Versacea9a30712015-11-03 14:55:58 -0800129 VkPipeline pipeline_h;
130 anv_graphics_pipeline_create(device_h,
Chad Versace16b2a482015-11-03 14:19:45 -0800131 &(VkGraphicsPipelineCreateInfo) {
132 .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
Chad Versace16b2a482015-11-03 14:19:45 -0800133 .stageCount = 2,
134 .pStages = (VkPipelineShaderStageCreateInfo[]) {
135 {
136 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
Jason Ekstranda5f19f62015-12-02 16:08:13 -0800137 .stage = VK_SHADER_STAGE_VERTEX_BIT,
Jason Ekstrande10dc002015-12-02 14:35:07 -0800138 .module = anv_shader_module_to_handle(&vs_m),
139 .pName = "main",
Chad Versacea9a30712015-11-03 14:55:58 -0800140 },
141 {
Chad Versace16b2a482015-11-03 14:19:45 -0800142 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
Jason Ekstranda5f19f62015-12-02 16:08:13 -0800143 .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
Jason Ekstrande10dc002015-12-02 14:35:07 -0800144 .module = anv_shader_module_to_handle(&fs_m),
145 .pName = "main",
Chad Versacea9a30712015-11-03 14:55:58 -0800146 },
Chad Versace16b2a482015-11-03 14:19:45 -0800147 },
Chad Versacea9a30712015-11-03 14:55:58 -0800148 .pVertexInputState = vi_state,
Chad Versace16b2a482015-11-03 14:19:45 -0800149 .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo) {
150 .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
151 .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
152 .primitiveRestartEnable = false,
153 },
154 .pViewportState = &(VkPipelineViewportStateCreateInfo) {
155 .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
156 .viewportCount = 1,
Chad Versacea9a30712015-11-03 14:55:58 -0800157 .pViewports = NULL, /* dynamic */
Chad Versace16b2a482015-11-03 14:19:45 -0800158 .scissorCount = 1,
Chad Versacea9a30712015-11-03 14:55:58 -0800159 .pScissors = NULL, /* dynamic */
Chad Versace16b2a482015-11-03 14:19:45 -0800160 },
Jason Ekstrand7f228402015-11-30 18:05:00 -0800161 .pRasterizationState = &(VkPipelineRasterizationStateCreateInfo) {
162 .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
Chad Versace16b2a482015-11-03 14:19:45 -0800163 .rasterizerDiscardEnable = false,
Jason Ekstrand9b1cb8f2015-11-30 13:28:09 -0800164 .polygonMode = VK_POLYGON_MODE_FILL,
Chad Versace16b2a482015-11-03 14:19:45 -0800165 .cullMode = VK_CULL_MODE_NONE,
Jason Ekstrand9b1cb8f2015-11-30 13:28:09 -0800166 .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE,
Chad Versacea9a30712015-11-03 14:55:58 -0800167 .depthBiasEnable = false,
Chad Versace16b2a482015-11-03 14:19:45 -0800168 },
169 .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
170 .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
Jason Ekstrand7f228402015-11-30 18:05:00 -0800171 .rasterizationSamples = 1, /* FINISHME: Multisampling */
Chad Versace16b2a482015-11-03 14:19:45 -0800172 .sampleShadingEnable = false,
173 .pSampleMask = (VkSampleMask[]) { UINT32_MAX },
Jason Ekstrand9fa6e322015-11-30 17:20:49 -0800174 .alphaToCoverageEnable = false,
175 .alphaToOneEnable = false,
Chad Versace16b2a482015-11-03 14:19:45 -0800176 },
Chad Versacea9a30712015-11-03 14:55:58 -0800177 .pDepthStencilState = ds_state,
178 .pColorBlendState = cb_state,
Chad Versace16b2a482015-11-03 14:19:45 -0800179 .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
Chad Versacea9a30712015-11-03 14:55:58 -0800180 /* The meta clear pipeline declares all state as dynamic.
181 * As a consequence, vkCmdBindPipeline writes no dynamic state
182 * to the cmd buffer. Therefore, at the end of the meta clear,
183 * we need only restore dynamic state was vkCmdSet.
184 */
Chad Versace16b2a482015-11-03 14:19:45 -0800185 .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
186 .dynamicStateCount = 9,
187 .pDynamicStates = (VkDynamicState[]) {
188 VK_DYNAMIC_STATE_VIEWPORT,
189 VK_DYNAMIC_STATE_SCISSOR,
190 VK_DYNAMIC_STATE_LINE_WIDTH,
191 VK_DYNAMIC_STATE_DEPTH_BIAS,
192 VK_DYNAMIC_STATE_BLEND_CONSTANTS,
193 VK_DYNAMIC_STATE_DEPTH_BOUNDS,
194 VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK,
195 VK_DYNAMIC_STATE_STENCIL_WRITE_MASK,
196 VK_DYNAMIC_STATE_STENCIL_REFERENCE,
197 },
198 },
199 .flags = 0,
200 .renderPass = anv_render_pass_to_handle(&anv_meta_dummy_renderpass),
201 .subpass = 0,
202 },
203 &(struct anv_graphics_pipeline_create_info) {
204 .use_repclear = true,
205 .disable_viewport = true,
206 .disable_vs = true,
207 .use_rectlist = true
208 },
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800209 alloc,
Chad Versacea9a30712015-11-03 14:55:58 -0800210 &pipeline_h);
Chad Versace16b2a482015-11-03 14:19:45 -0800211
Chad Versacea9a30712015-11-03 14:55:58 -0800212 ralloc_free(vs_nir);
213 ralloc_free(fs_nir);
214
215 return anv_pipeline_from_handle(pipeline_h);
216}
217
218static void
219init_color_pipeline(struct anv_device *device)
220{
221 struct nir_shader *vs_nir;
222 struct nir_shader *fs_nir;
223 build_color_shaders(&vs_nir, &fs_nir);
224
225 const VkPipelineVertexInputStateCreateInfo vi_state = {
226 .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
Jason Ekstrande673d642015-11-30 17:00:30 -0800227 .vertexBindingDescriptionCount = 1,
Chad Versacea9a30712015-11-03 14:55:58 -0800228 .pVertexBindingDescriptions = (VkVertexInputBindingDescription[]) {
229 {
230 .binding = 0,
Jason Ekstrande673d642015-11-30 17:00:30 -0800231 .stride = sizeof(struct color_clear_vattrs),
Jason Ekstrand9b1cb8f2015-11-30 13:28:09 -0800232 .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
Chad Versacea9a30712015-11-03 14:55:58 -0800233 },
234 },
Jason Ekstrande673d642015-11-30 17:00:30 -0800235 .vertexAttributeDescriptionCount = 3,
Chad Versacea9a30712015-11-03 14:55:58 -0800236 .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[]) {
237 {
238 /* VUE Header */
239 .location = 0,
240 .binding = 0,
241 .format = VK_FORMAT_R32G32B32A32_UINT,
Jason Ekstrande673d642015-11-30 17:00:30 -0800242 .offset = offsetof(struct color_clear_vattrs, vue_header),
Chad Versacea9a30712015-11-03 14:55:58 -0800243 },
244 {
245 /* Position */
246 .location = 1,
247 .binding = 0,
248 .format = VK_FORMAT_R32G32_SFLOAT,
Jason Ekstrande673d642015-11-30 17:00:30 -0800249 .offset = offsetof(struct color_clear_vattrs, position),
Chad Versacea9a30712015-11-03 14:55:58 -0800250 },
251 {
252 /* Color */
253 .location = 2,
254 .binding = 0,
255 .format = VK_FORMAT_R32G32B32A32_SFLOAT,
Jason Ekstrande673d642015-11-30 17:00:30 -0800256 .offset = offsetof(struct color_clear_vattrs, color),
Chad Versacea9a30712015-11-03 14:55:58 -0800257 },
258 },
259 };
260
261 const VkPipelineDepthStencilStateCreateInfo ds_state = {
262 .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
263 .depthTestEnable = false,
264 .depthWriteEnable = false,
265 .depthBoundsTestEnable = false,
266 .stencilTestEnable = false,
267 };
268
269 const VkPipelineColorBlendStateCreateInfo cb_state = {
270 .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
Chad Versacea9a30712015-11-03 14:55:58 -0800271 .logicOpEnable = false,
272 .attachmentCount = 1,
273 .pAttachments = (VkPipelineColorBlendAttachmentState []) {
274 {
275 .blendEnable = false,
Jason Ekstrand4cf0b572015-11-30 18:12:55 -0800276 .colorWriteMask = VK_COLOR_COMPONENT_A_BIT |
277 VK_COLOR_COMPONENT_R_BIT |
278 VK_COLOR_COMPONENT_G_BIT |
279 VK_COLOR_COMPONENT_B_BIT,
Chad Versacea9a30712015-11-03 14:55:58 -0800280 },
281 },
282 };
283
284 device->meta_state.clear.color_pipeline =
285 create_pipeline(device, vs_nir, fs_nir, &vi_state, &ds_state,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800286 &cb_state, NULL);
Chad Versacea9a30712015-11-03 14:55:58 -0800287}
288
289static void
290emit_load_color_clear(struct anv_cmd_buffer *cmd_buffer,
291 uint32_t attachment,
292 VkClearColorValue clear_value)
293{
294 struct anv_device *device = cmd_buffer->device;
Jason Ekstranda89a4852015-11-30 11:48:08 -0800295 VkCommandBuffer cmd_buffer_h = anv_cmd_buffer_to_handle(cmd_buffer);
Chad Versacea9a30712015-11-03 14:55:58 -0800296 const struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
297 VkPipeline pipeline_h =
298 anv_pipeline_to_handle(device->meta_state.clear.color_pipeline);
299
300 const struct color_clear_vattrs vertex_data[3] = {
301 {
302 .vue_header = { 0 },
303 .position = { 0.0, 0.0 },
304 .color = clear_value,
305 },
306 {
307 .vue_header = { 0 },
308 .position = { fb->width, 0.0 },
309 .color = clear_value,
310 },
311 {
312 .vue_header = { 0 },
313 .position = { fb->width, fb->height },
314 .color = clear_value,
315 },
316 };
317
318 struct anv_state state =
Kristian Høgsberg77359202015-12-01 15:37:12 -0800319 anv_cmd_buffer_emit_dynamic(cmd_buffer, vertex_data, sizeof(vertex_data), 16);
Chad Versacea9a30712015-11-03 14:55:58 -0800320
321 struct anv_buffer vertex_buffer = {
322 .device = device,
323 .size = sizeof(vertex_data),
324 .bo = &device->dynamic_state_block_pool.bo,
325 .offset = state.offset,
326 };
327
328 anv_cmd_buffer_begin_subpass(cmd_buffer,
329 &(struct anv_subpass) {
330 .color_count = 1,
331 .color_attachments = (uint32_t[]) { attachment },
332 .depth_stencil_attachment = VK_ATTACHMENT_UNUSED,
333 });
334
335 ANV_CALL(CmdSetViewport)(cmd_buffer_h, 1,
336 (VkViewport[]) {
337 {
Jason Ekstrand5f348bd2015-11-30 17:26:32 -0800338 .x = 0,
339 .y = 0,
Chad Versacea9a30712015-11-03 14:55:58 -0800340 .width = fb->width,
341 .height = fb->height,
342 .minDepth = 0.0,
343 .maxDepth = 1.0,
344 },
345 });
346
347 ANV_CALL(CmdSetScissor)(cmd_buffer_h, 1,
348 (VkRect2D[]) {
349 {
350 .offset = { 0, 0 },
351 .extent = { fb->width, fb->height },
352 }
353 });
354
355 ANV_CALL(CmdBindVertexBuffers)(cmd_buffer_h, 0, 1,
356 (VkBuffer[]) { anv_buffer_to_handle(&vertex_buffer) },
357 (VkDeviceSize[]) { 0 });
358
359 if (cmd_buffer->state.pipeline != device->meta_state.clear.color_pipeline) {
360 ANV_CALL(CmdBindPipeline)(cmd_buffer_h, VK_PIPELINE_BIND_POINT_GRAPHICS,
361 pipeline_h);
362 }
363
364 ANV_CALL(CmdDraw)(cmd_buffer_h, 3, 1, 0, 0);
365}
366
367
368static void
369build_depthstencil_shaders(struct nir_shader **out_vs,
370 struct nir_shader **out_fs)
371{
372 nir_builder vs_b;
373 nir_builder fs_b;
374
375 nir_builder_init_simple_shader(&vs_b, MESA_SHADER_VERTEX);
376 nir_builder_init_simple_shader(&fs_b, MESA_SHADER_FRAGMENT);
377
378 const struct glsl_type *position_type = glsl_vec4_type();
379
380 nir_variable *vs_in_pos =
381 nir_variable_create(vs_b.shader, nir_var_shader_in, position_type,
382 "a_position");
383 vs_in_pos->data.location = VERT_ATTRIB_GENERIC0;
384
385 nir_variable *vs_out_pos =
386 nir_variable_create(vs_b.shader, nir_var_shader_out, position_type,
387 "gl_Position");
388 vs_out_pos->data.location = VARYING_SLOT_POS;
389
390 nir_copy_var(&vs_b, vs_out_pos, vs_in_pos);
391
392 *out_vs = vs_b.shader;
393 *out_fs = fs_b.shader;
394}
395
Chad Versace16119ad2015-11-04 17:00:01 -0800396static struct anv_pipeline *
397create_depthstencil_pipeline(struct anv_device *device,
398 VkImageAspectFlags aspects)
Chad Versacea9a30712015-11-03 14:55:58 -0800399{
400 struct nir_shader *vs_nir;
401 struct nir_shader *fs_nir;
Chad Versace16119ad2015-11-04 17:00:01 -0800402
Chad Versacea9a30712015-11-03 14:55:58 -0800403 build_depthstencil_shaders(&vs_nir, &fs_nir);
404
405 const VkPipelineVertexInputStateCreateInfo vi_state = {
406 .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
Jason Ekstrande673d642015-11-30 17:00:30 -0800407 .vertexBindingDescriptionCount = 1,
Chad Versacea9a30712015-11-03 14:55:58 -0800408 .pVertexBindingDescriptions = (VkVertexInputBindingDescription[]) {
409 {
410 .binding = 0,
Jason Ekstrande673d642015-11-30 17:00:30 -0800411 .stride = sizeof(struct depthstencil_clear_vattrs),
Jason Ekstrand9b1cb8f2015-11-30 13:28:09 -0800412 .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
Chad Versacea9a30712015-11-03 14:55:58 -0800413 },
414 },
Jason Ekstrande673d642015-11-30 17:00:30 -0800415 .vertexAttributeDescriptionCount = 2,
Chad Versacea9a30712015-11-03 14:55:58 -0800416 .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[]) {
417 {
418 /* VUE Header */
419 .location = 0,
420 .binding = 0,
421 .format = VK_FORMAT_R32G32B32A32_UINT,
Jason Ekstrande673d642015-11-30 17:00:30 -0800422 .offset = offsetof(struct depthstencil_clear_vattrs, vue_header),
Chad Versacea9a30712015-11-03 14:55:58 -0800423 },
424 {
425 /* Position */
426 .location = 1,
427 .binding = 0,
428 .format = VK_FORMAT_R32G32_SFLOAT,
Jason Ekstrande673d642015-11-30 17:00:30 -0800429 .offset = offsetof(struct depthstencil_clear_vattrs, position),
Chad Versacea9a30712015-11-03 14:55:58 -0800430 },
431 },
432 };
433
434 const VkPipelineDepthStencilStateCreateInfo ds_state = {
435 .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
Chad Versace16119ad2015-11-04 17:00:01 -0800436 .depthTestEnable = (aspects & VK_IMAGE_ASPECT_DEPTH_BIT),
Chad Versacea9a30712015-11-03 14:55:58 -0800437 .depthCompareOp = VK_COMPARE_OP_ALWAYS,
Chad Versace16119ad2015-11-04 17:00:01 -0800438 .depthWriteEnable = (aspects & VK_IMAGE_ASPECT_DEPTH_BIT),
Chad Versacea9a30712015-11-03 14:55:58 -0800439 .depthBoundsTestEnable = false,
Chad Versace16119ad2015-11-04 17:00:01 -0800440 .stencilTestEnable = (aspects & VK_IMAGE_ASPECT_STENCIL_BIT),
441 .front = {
Jason Ekstrand4ab93912015-11-30 14:19:41 -0800442 .passOp = VK_STENCIL_OP_REPLACE,
443 .compareOp = VK_COMPARE_OP_ALWAYS,
444 .writeMask = UINT32_MAX,
445 .reference = 0, /* dynamic */
Chad Versace16119ad2015-11-04 17:00:01 -0800446 },
447 .back = { 0 /* dont care */ },
Chad Versacea9a30712015-11-03 14:55:58 -0800448 };
449
450 const VkPipelineColorBlendStateCreateInfo cb_state = {
451 .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
Chad Versacea9a30712015-11-03 14:55:58 -0800452 .logicOpEnable = false,
453 .attachmentCount = 0,
454 .pAttachments = NULL,
455 };
456
Chad Versace16119ad2015-11-04 17:00:01 -0800457 return create_pipeline(device, vs_nir, fs_nir, &vi_state, &ds_state,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800458 &cb_state, NULL);
Chad Versacea9a30712015-11-03 14:55:58 -0800459}
460
461static void
Chad Versace16119ad2015-11-04 17:00:01 -0800462emit_load_depthstencil_clear(struct anv_cmd_buffer *cmd_buffer,
463 uint32_t attachment,
464 VkImageAspectFlags aspects,
465 VkClearDepthStencilValue clear_value)
Chad Versacea9a30712015-11-03 14:55:58 -0800466{
467 struct anv_device *device = cmd_buffer->device;
Jason Ekstranda89a4852015-11-30 11:48:08 -0800468 VkCommandBuffer cmd_buffer_h = anv_cmd_buffer_to_handle(cmd_buffer);
Chad Versacea9a30712015-11-03 14:55:58 -0800469 const struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
470
Chad Versace16119ad2015-11-04 17:00:01 -0800471 const struct depthstencil_clear_vattrs vertex_data[3] = {
Chad Versacea9a30712015-11-03 14:55:58 -0800472 {
473 .vue_header = { 0 },
474 .position = { 0.0, 0.0 },
475 },
476 {
477 .vue_header = { 0 },
478 .position = { fb->width, 0.0 },
479 },
480 {
481 .vue_header = { 0 },
482 .position = { fb->width, fb->height },
483 },
484 };
485
486 struct anv_state state =
Kristian Høgsberg77359202015-12-01 15:37:12 -0800487 anv_cmd_buffer_emit_dynamic(cmd_buffer, vertex_data, sizeof(vertex_data), 16);
Chad Versacea9a30712015-11-03 14:55:58 -0800488
489 struct anv_buffer vertex_buffer = {
490 .device = device,
491 .size = sizeof(vertex_data),
492 .bo = &device->dynamic_state_block_pool.bo,
493 .offset = state.offset,
494 };
495
496 anv_cmd_buffer_begin_subpass(cmd_buffer,
497 &(struct anv_subpass) {
498 .color_count = 0,
499 .depth_stencil_attachment = attachment,
500 });
501
502 ANV_CALL(CmdSetViewport)(cmd_buffer_h, 1,
503 (VkViewport[]) {
504 {
Jason Ekstrand5f348bd2015-11-30 17:26:32 -0800505 .x = 0,
506 .y = 0,
Chad Versacea9a30712015-11-03 14:55:58 -0800507 .width = fb->width,
508 .height = fb->height,
Chad Versace16119ad2015-11-04 17:00:01 -0800509
510 /* Ignored when clearing only stencil. */
511 .minDepth = clear_value.depth,
512 .maxDepth = clear_value.depth,
Chad Versacea9a30712015-11-03 14:55:58 -0800513 },
514 });
515
516 ANV_CALL(CmdSetScissor)(cmd_buffer_h, 1,
517 (VkRect2D[]) {
518 {
519 .offset = { 0, 0 },
520 .extent = { fb->width, fb->height },
521 }
522 });
523
Chad Versace16119ad2015-11-04 17:00:01 -0800524 if (aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
525 ANV_CALL(CmdSetStencilReference)(cmd_buffer_h, VK_STENCIL_FACE_FRONT_BIT,
526 clear_value.stencil);
527 }
528
Chad Versacea9a30712015-11-03 14:55:58 -0800529 ANV_CALL(CmdBindVertexBuffers)(cmd_buffer_h, 0, 1,
530 (VkBuffer[]) { anv_buffer_to_handle(&vertex_buffer) },
531 (VkDeviceSize[]) { 0 });
532
Chad Versace16119ad2015-11-04 17:00:01 -0800533 struct anv_pipeline *pipeline;
534 switch (aspects) {
535 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
536 pipeline = device->meta_state.clear.depthstencil_pipeline;
537 break;
538 case VK_IMAGE_ASPECT_DEPTH_BIT:
539 pipeline = device->meta_state.clear.depth_only_pipeline;
540 break;
541 case VK_IMAGE_ASPECT_STENCIL_BIT:
542 pipeline = device->meta_state.clear.stencil_only_pipeline;
543 break;
544 default:
545 unreachable("expected depth or stencil aspect");
546 }
547
548 if (cmd_buffer->state.pipeline != pipeline) {
Chad Versacea9a30712015-11-03 14:55:58 -0800549 ANV_CALL(CmdBindPipeline)(cmd_buffer_h, VK_PIPELINE_BIND_POINT_GRAPHICS,
Chad Versace16119ad2015-11-04 17:00:01 -0800550 anv_pipeline_to_handle(pipeline));
Chad Versacea9a30712015-11-03 14:55:58 -0800551 }
552
553 ANV_CALL(CmdDraw)(cmd_buffer_h, 3, 1, 0, 0);
554}
555
Chad Versace16119ad2015-11-04 17:00:01 -0800556static void
557init_depthstencil_pipelines(struct anv_device *device)
558{
559 device->meta_state.clear.depth_only_pipeline =
560 create_depthstencil_pipeline(device, VK_IMAGE_ASPECT_DEPTH_BIT);
561
562 device->meta_state.clear.stencil_only_pipeline =
563 create_depthstencil_pipeline(device, VK_IMAGE_ASPECT_STENCIL_BIT);
564
565 device->meta_state.clear.depthstencil_pipeline =
566 create_depthstencil_pipeline(device, VK_IMAGE_ASPECT_DEPTH_BIT |
567 VK_IMAGE_ASPECT_STENCIL_BIT);
568}
569
Chad Versacea9a30712015-11-03 14:55:58 -0800570void
571anv_device_init_meta_clear_state(struct anv_device *device)
572{
573 init_color_pipeline(device);
Chad Versace16119ad2015-11-04 17:00:01 -0800574 init_depthstencil_pipelines(device);
Chad Versacea9a30712015-11-03 14:55:58 -0800575}
576
577void
578anv_device_finish_meta_clear_state(struct anv_device *device)
579{
Chad Versace16119ad2015-11-04 17:00:01 -0800580 VkDevice device_h = anv_device_to_handle(device);
581
582 ANV_CALL(DestroyPipeline)(device_h,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800583 anv_pipeline_to_handle(device->meta_state.clear.color_pipeline),
584 NULL);
Chad Versace16119ad2015-11-04 17:00:01 -0800585 ANV_CALL(DestroyPipeline)(device_h,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800586 anv_pipeline_to_handle(device->meta_state.clear.depth_only_pipeline),
587 NULL);
Chad Versace16119ad2015-11-04 17:00:01 -0800588 ANV_CALL(DestroyPipeline)(device_h,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800589 anv_pipeline_to_handle(device->meta_state.clear.stencil_only_pipeline),
590 NULL);
Chad Versace16119ad2015-11-04 17:00:01 -0800591 ANV_CALL(DestroyPipeline)(device_h,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800592 anv_pipeline_to_handle(device->meta_state.clear.depthstencil_pipeline),
593 NULL);
Chad Versacea9a30712015-11-03 14:55:58 -0800594}
595
596void
597anv_cmd_buffer_clear_attachments(struct anv_cmd_buffer *cmd_buffer,
598 struct anv_render_pass *pass,
599 const VkClearValue *clear_values)
600{
601 struct anv_meta_saved_state saved_state;
602
Jason Ekstrande14b2c72015-11-21 11:39:12 -0800603 /* Figure out whether or not we actually need to clear anything to avoid
604 * trashing state when clearing is a no-op.
605 */
606 bool needs_clear = false;
607 for (uint32_t a = 0; a < pass->attachment_count; ++a) {
608 struct anv_render_pass_attachment *att = &pass->attachments[a];
609
610 if (anv_format_is_color(att->format)) {
611 if (att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
612 needs_clear = true;
613 break;
614 }
615 } else {
616 if ((att->format->depth_format &&
617 att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) ||
618 (att->format->has_stencil &&
619 att->stencil_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR)) {
620 needs_clear = true;
621 break;
622 }
623 }
624 }
625
626 if (!needs_clear)
627 return;
628
Chad Versacea9a30712015-11-03 14:55:58 -0800629 meta_clear_begin(&saved_state, cmd_buffer);
630
631 for (uint32_t a = 0; a < pass->attachment_count; ++a) {
632 struct anv_render_pass_attachment *att = &pass->attachments[a];
633
634 if (anv_format_is_color(att->format)) {
635 if (att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
636 emit_load_color_clear(cmd_buffer, a, clear_values[a].color);
637 }
638 } else {
Jason Ekstrand83c305f2015-11-21 00:04:57 -0800639 VkImageAspectFlags clear_aspects = 0;
Chad Versace16119ad2015-11-04 17:00:01 -0800640
Chad Versacea9a30712015-11-03 14:55:58 -0800641 if (att->format->depth_format &&
642 att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
Jason Ekstrand83c305f2015-11-21 00:04:57 -0800643 clear_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
Chad Versacea9a30712015-11-03 14:55:58 -0800644 }
645
646 if (att->format->has_stencil &&
647 att->stencil_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
Jason Ekstrand83c305f2015-11-21 00:04:57 -0800648 clear_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
Chad Versacea9a30712015-11-03 14:55:58 -0800649 }
Chad Versace16119ad2015-11-04 17:00:01 -0800650
Jason Ekstrand83c305f2015-11-21 00:04:57 -0800651 if (clear_aspects) {
652 emit_load_depthstencil_clear(cmd_buffer, a, clear_aspects,
653 clear_values[a].depthStencil);
654 }
Chad Versacea9a30712015-11-03 14:55:58 -0800655 }
656 }
657
658 meta_clear_end(&saved_state, cmd_buffer);
Chad Versace16b2a482015-11-03 14:19:45 -0800659}
660
661void anv_CmdClearColorImage(
Jason Ekstranda89a4852015-11-30 11:48:08 -0800662 VkCommandBuffer commandBuffer,
Chad Versace16b2a482015-11-03 14:19:45 -0800663 VkImage _image,
664 VkImageLayout imageLayout,
665 const VkClearColorValue* pColor,
666 uint32_t rangeCount,
667 const VkImageSubresourceRange* pRanges)
668{
Jason Ekstranda89a4852015-11-30 11:48:08 -0800669 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
Chad Versace16b2a482015-11-03 14:19:45 -0800670 ANV_FROM_HANDLE(anv_image, image, _image);
671 struct anv_meta_saved_state saved_state;
672
Chad Versacea9a30712015-11-03 14:55:58 -0800673 meta_clear_begin(&saved_state, cmd_buffer);
Chad Versace16b2a482015-11-03 14:19:45 -0800674
675 for (uint32_t r = 0; r < rangeCount; r++) {
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800676 for (uint32_t l = 0; l < pRanges[r].levelCount; l++) {
677 for (uint32_t s = 0; s < pRanges[r].layerCount; s++) {
Chad Versace16b2a482015-11-03 14:19:45 -0800678 struct anv_image_view iview;
679 anv_image_view_init(&iview, cmd_buffer->device,
680 &(VkImageViewCreateInfo) {
681 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
682 .image = _image,
Chad Versacef0d11d52015-12-09 17:03:14 -0800683 .viewType = anv_meta_get_view_type(image),
Chad Versace16b2a482015-11-03 14:19:45 -0800684 .format = image->format->vk_format,
Chad Versace16b2a482015-11-03 14:19:45 -0800685 .subresourceRange = {
686 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
687 .baseMipLevel = pRanges[r].baseMipLevel + l,
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800688 .levelCount = 1,
Chad Versace16b2a482015-11-03 14:19:45 -0800689 .baseArrayLayer = pRanges[r].baseArrayLayer + s,
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800690 .layerCount = 1
Chad Versace16b2a482015-11-03 14:19:45 -0800691 },
692 },
693 cmd_buffer);
694
695 VkFramebuffer fb;
696 anv_CreateFramebuffer(anv_device_to_handle(cmd_buffer->device),
697 &(VkFramebufferCreateInfo) {
698 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
699 .attachmentCount = 1,
700 .pAttachments = (VkImageView[]) {
701 anv_image_view_to_handle(&iview),
702 },
703 .width = iview.extent.width,
704 .height = iview.extent.height,
705 .layers = 1
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800706 }, &cmd_buffer->pool->alloc, &fb);
Chad Versace16b2a482015-11-03 14:19:45 -0800707
708 VkRenderPass pass;
709 anv_CreateRenderPass(anv_device_to_handle(cmd_buffer->device),
710 &(VkRenderPassCreateInfo) {
711 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
712 .attachmentCount = 1,
713 .pAttachments = &(VkAttachmentDescription) {
Chad Versace16b2a482015-11-03 14:19:45 -0800714 .format = iview.format->vk_format,
715 .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
716 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
717 .initialLayout = VK_IMAGE_LAYOUT_GENERAL,
718 .finalLayout = VK_IMAGE_LAYOUT_GENERAL,
719 },
720 .subpassCount = 1,
721 .pSubpasses = &(VkSubpassDescription) {
Chad Versace16b2a482015-11-03 14:19:45 -0800722 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
Jason Ekstrand43f3e922015-12-01 13:09:22 -0800723 .inputAttachmentCount = 0,
724 .colorAttachmentCount = 1,
Chad Versace16b2a482015-11-03 14:19:45 -0800725 .pColorAttachments = &(VkAttachmentReference) {
726 .attachment = 0,
727 .layout = VK_IMAGE_LAYOUT_GENERAL,
728 },
729 .pResolveAttachments = NULL,
Jason Ekstrand43f3e922015-12-01 13:09:22 -0800730 .pDepthStencilAttachment = &(VkAttachmentReference) {
Chad Versace16b2a482015-11-03 14:19:45 -0800731 .attachment = VK_ATTACHMENT_UNUSED,
732 .layout = VK_IMAGE_LAYOUT_GENERAL,
733 },
Jason Ekstrand43f3e922015-12-01 13:09:22 -0800734 .preserveAttachmentCount = 1,
Chad Versace16b2a482015-11-03 14:19:45 -0800735 .pPreserveAttachments = &(VkAttachmentReference) {
736 .attachment = 0,
737 .layout = VK_IMAGE_LAYOUT_GENERAL,
738 },
739 },
740 .dependencyCount = 0,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800741 }, &cmd_buffer->pool->alloc, &pass);
Chad Versace16b2a482015-11-03 14:19:45 -0800742
743 ANV_CALL(CmdBeginRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer),
744 &(VkRenderPassBeginInfo) {
745 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
746 .renderArea = {
747 .offset = { 0, 0, },
748 .extent = {
749 .width = iview.extent.width,
750 .height = iview.extent.height,
751 },
752 },
753 .renderPass = pass,
754 .framebuffer = fb,
755 .clearValueCount = 1,
Chad Versacea9a30712015-11-03 14:55:58 -0800756 .pClearValues = (VkClearValue[]) {
757 { .color = *pColor },
758 },
Jason Ekstranda89a4852015-11-30 11:48:08 -0800759 }, VK_SUBPASS_CONTENTS_INLINE);
Chad Versace16b2a482015-11-03 14:19:45 -0800760
Chad Versace16b2a482015-11-03 14:19:45 -0800761 ANV_CALL(CmdEndRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer));
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800762
763 /* XXX: We're leaking the render pass and framebuffer */
Chad Versace16b2a482015-11-03 14:19:45 -0800764 }
765 }
766 }
767
Chad Versacea9a30712015-11-03 14:55:58 -0800768 meta_clear_end(&saved_state, cmd_buffer);
Chad Versace16b2a482015-11-03 14:19:45 -0800769}
770
771void anv_CmdClearDepthStencilImage(
Jason Ekstranda89a4852015-11-30 11:48:08 -0800772 VkCommandBuffer commandBuffer,
Chad Versace16b2a482015-11-03 14:19:45 -0800773 VkImage image,
774 VkImageLayout imageLayout,
775 const VkClearDepthStencilValue* pDepthStencil,
776 uint32_t rangeCount,
777 const VkImageSubresourceRange* pRanges)
778{
779 stub();
780}
781
Jason Ekstrand569f70b2015-11-30 14:52:38 -0800782void anv_CmdClearAttachments(
Jason Ekstranda89a4852015-11-30 11:48:08 -0800783 VkCommandBuffer commandBuffer,
Jason Ekstrand569f70b2015-11-30 14:52:38 -0800784 uint32_t attachmentCount,
785 const VkClearAttachment* pAttachments,
Chad Versace16b2a482015-11-03 14:19:45 -0800786 uint32_t rectCount,
Jason Ekstrand569f70b2015-11-30 14:52:38 -0800787 const VkClearRect* pRects)
Chad Versace16b2a482015-11-03 14:19:45 -0800788{
789 stub();
790}