anv/meta: Clear color attribute is always flat
[mesa.git] / src / vulkan / anv_meta_clear.c
1 /*
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
29 struct clear_instance_data {
30 struct anv_vue_header vue_header;
31 VkClearColorValue color;
32 };
33
34 static void
35 meta_emit_clear(struct anv_cmd_buffer *cmd_buffer,
36 int num_instances,
37 struct clear_instance_data *instance_data,
38 VkClearDepthStencilValue ds_clear_value)
39 {
40 struct anv_device *device = cmd_buffer->device;
41 struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
42 struct anv_state state;
43 uint32_t size;
44
45 const float vertex_data[] = {
46 /* Rect-list coordinates */
47 0.0, 0.0, ds_clear_value.depth,
48 fb->width, 0.0, ds_clear_value.depth,
49 fb->width, fb->height, ds_clear_value.depth,
50
51 /* Align to 16 bytes */
52 0.0, 0.0, 0.0,
53 };
54
55 size = sizeof(vertex_data) + num_instances * sizeof(*instance_data);
56 state = anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, size, 16);
57
58 /* Copy in the vertex and instance data */
59 memcpy(state.map, vertex_data, sizeof(vertex_data));
60 memcpy(state.map + sizeof(vertex_data), instance_data,
61 num_instances * sizeof(*instance_data));
62
63 struct anv_buffer vertex_buffer = {
64 .device = cmd_buffer->device,
65 .size = size,
66 .bo = &device->dynamic_state_block_pool.bo,
67 .offset = state.offset
68 };
69
70 anv_CmdBindVertexBuffers(anv_cmd_buffer_to_handle(cmd_buffer), 0, 2,
71 (VkBuffer[]) {
72 anv_buffer_to_handle(&vertex_buffer),
73 anv_buffer_to_handle(&vertex_buffer)
74 },
75 (VkDeviceSize[]) {
76 0,
77 sizeof(vertex_data)
78 });
79
80 if (cmd_buffer->state.pipeline != anv_pipeline_from_handle(device->meta_state.clear.pipeline))
81 anv_CmdBindPipeline(anv_cmd_buffer_to_handle(cmd_buffer),
82 VK_PIPELINE_BIND_POINT_GRAPHICS,
83 device->meta_state.clear.pipeline);
84
85 ANV_CALL(CmdDraw)(anv_cmd_buffer_to_handle(cmd_buffer),
86 3, num_instances, 0, 0);
87 }
88
89 void
90 anv_cmd_buffer_clear_attachments(struct anv_cmd_buffer *cmd_buffer,
91 struct anv_render_pass *pass,
92 const VkClearValue *clear_values)
93 {
94 struct anv_meta_saved_state saved_state;
95
96 if (pass->has_stencil_clear_attachment)
97 anv_finishme("stencil clear");
98
99 /* FINISHME: Rethink how we count clear attachments in light of
100 * 0.138.2 -> 0.170.2 diff.
101 */
102 if (pass->num_color_clear_attachments == 0 &&
103 !pass->has_depth_clear_attachment)
104 return;
105
106 struct clear_instance_data instance_data[pass->num_color_clear_attachments];
107 uint32_t color_attachments[pass->num_color_clear_attachments];
108 uint32_t ds_attachment = VK_ATTACHMENT_UNUSED;
109 VkClearDepthStencilValue ds_clear_value = {0};
110
111 int layer = 0;
112 for (uint32_t i = 0; i < pass->attachment_count; i++) {
113 const struct anv_render_pass_attachment *att = &pass->attachments[i];
114
115 if (anv_format_is_color(att->format)) {
116 if (att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
117 instance_data[layer] = (struct clear_instance_data) {
118 .vue_header = {
119 .RTAIndex = i,
120 .ViewportIndex = 0,
121 .PointWidth = 0.0
122 },
123 .color = clear_values[i].color,
124 };
125 color_attachments[layer] = i;
126 layer++;
127 }
128 } else {
129 if (att->format->depth_format &&
130 att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
131 assert(ds_attachment == VK_ATTACHMENT_UNUSED);
132 ds_attachment = i;
133 ds_clear_value = clear_values[ds_attachment].depthStencil;
134 }
135
136 if (att->format->has_stencil &&
137 att->stencil_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
138 anv_finishme("stencil clear");
139 }
140 }
141 }
142
143 anv_meta_save(&saved_state, cmd_buffer,
144 (1 << VK_DYNAMIC_STATE_VIEWPORT));
145 cmd_buffer->state.dynamic.viewport.count = 0;
146
147 struct anv_subpass subpass = {
148 .input_count = 0,
149 .color_count = pass->num_color_clear_attachments,
150 .color_attachments = color_attachments,
151 .depth_stencil_attachment = ds_attachment,
152 };
153
154 anv_cmd_buffer_begin_subpass(cmd_buffer, &subpass);
155
156 meta_emit_clear(cmd_buffer, pass->num_color_clear_attachments,
157 instance_data, ds_clear_value);
158
159 anv_meta_restore(&saved_state, cmd_buffer);
160 }
161
162 static nir_shader *
163 build_nir_vertex_shader(void)
164 {
165 nir_builder b;
166
167 const struct glsl_type *vertex_type = glsl_vec4_type();
168
169 nir_builder_init_simple_shader(&b, MESA_SHADER_VERTEX);
170
171 nir_variable *pos_in = nir_variable_create(b.shader, nir_var_shader_in,
172 vertex_type, "a_pos");
173 pos_in->data.location = VERT_ATTRIB_GENERIC0;
174 nir_variable *pos_out = nir_variable_create(b.shader, nir_var_shader_out,
175 vertex_type, "gl_Position");
176 pos_in->data.location = VARYING_SLOT_POS;
177 nir_copy_var(&b, pos_out, pos_in);
178
179 /* Add one more pass-through attribute. For clear shaders, this is used
180 * to store the color and for blit shaders it's the texture coordinate.
181 */
182 const struct glsl_type *attr_type = glsl_vec4_type();
183 nir_variable *attr_in = nir_variable_create(b.shader, nir_var_shader_in,
184 attr_type, "a_attr");
185 attr_in->data.location = VERT_ATTRIB_GENERIC1;
186 nir_variable *attr_out = nir_variable_create(b.shader, nir_var_shader_out,
187 attr_type, "v_attr");
188 attr_out->data.location = VARYING_SLOT_VAR0;
189 attr_out->data.interpolation = INTERP_QUALIFIER_FLAT;
190 nir_copy_var(&b, attr_out, attr_in);
191
192 return b.shader;
193 }
194
195 static nir_shader *
196 build_nir_clear_fragment_shader(void)
197 {
198 nir_builder b;
199
200 const struct glsl_type *color_type = glsl_vec4_type();
201
202 nir_builder_init_simple_shader(&b, MESA_SHADER_FRAGMENT);
203
204 nir_variable *color_in = nir_variable_create(b.shader, nir_var_shader_in,
205 color_type, "v_attr");
206 color_in->data.location = VARYING_SLOT_VAR0;
207 color_in->data.interpolation = INTERP_QUALIFIER_FLAT;
208 nir_variable *color_out = nir_variable_create(b.shader, nir_var_shader_out,
209 color_type, "f_color");
210 color_out->data.location = FRAG_RESULT_DATA0;
211 nir_copy_var(&b, color_out, color_in);
212
213 return b.shader;
214 }
215
216 void
217 anv_device_init_meta_clear_state(struct anv_device *device)
218 {
219 struct anv_shader_module vsm = {
220 .nir = build_nir_vertex_shader(),
221 };
222
223 struct anv_shader_module fsm = {
224 .nir = build_nir_clear_fragment_shader(),
225 };
226
227 VkShader vs;
228 anv_CreateShader(anv_device_to_handle(device),
229 &(VkShaderCreateInfo) {
230 .sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO,
231 .module = anv_shader_module_to_handle(&vsm),
232 .pName = "main",
233 }, &vs);
234
235 VkShader fs;
236 anv_CreateShader(anv_device_to_handle(device),
237 &(VkShaderCreateInfo) {
238 .sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO,
239 .module = anv_shader_module_to_handle(&fsm),
240 .pName = "main",
241 }, &fs);
242
243 /* We use instanced rendering to clear multiple render targets. We have two
244 * vertex buffers: the first vertex buffer holds per-vertex data and
245 * provides the vertices for the clear rectangle. The second one holds
246 * per-instance data, which consists of the VUE header (which selects the
247 * layer) and the color (Vulkan supports per-RT clear colors).
248 */
249 VkPipelineVertexInputStateCreateInfo vi_create_info = {
250 .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
251 .bindingCount = 2,
252 .pVertexBindingDescriptions = (VkVertexInputBindingDescription[]) {
253 {
254 .binding = 0,
255 .strideInBytes = 12,
256 .stepRate = VK_VERTEX_INPUT_STEP_RATE_VERTEX
257 },
258 {
259 .binding = 1,
260 .strideInBytes = 32,
261 .stepRate = VK_VERTEX_INPUT_STEP_RATE_INSTANCE
262 },
263 },
264 .attributeCount = 3,
265 .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[]) {
266 {
267 /* VUE Header */
268 .location = 0,
269 .binding = 1,
270 .format = VK_FORMAT_R32G32B32A32_UINT,
271 .offsetInBytes = 0
272 },
273 {
274 /* Position */
275 .location = 1,
276 .binding = 0,
277 .format = VK_FORMAT_R32G32B32_SFLOAT,
278 .offsetInBytes = 0
279 },
280 {
281 /* Color */
282 .location = 2,
283 .binding = 1,
284 .format = VK_FORMAT_R32G32B32A32_SFLOAT,
285 .offsetInBytes = 16
286 }
287 }
288 };
289
290 anv_graphics_pipeline_create(anv_device_to_handle(device),
291 &(VkGraphicsPipelineCreateInfo) {
292 .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
293
294 .stageCount = 2,
295 .pStages = (VkPipelineShaderStageCreateInfo[]) {
296 {
297 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
298 .stage = VK_SHADER_STAGE_VERTEX,
299 .shader = vs,
300 .pSpecializationInfo = NULL
301 }, {
302 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
303 .stage = VK_SHADER_STAGE_FRAGMENT,
304 .shader = fs,
305 .pSpecializationInfo = NULL,
306 }
307 },
308 .pVertexInputState = &vi_create_info,
309 .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo) {
310 .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
311 .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
312 .primitiveRestartEnable = false,
313 },
314 .pViewportState = &(VkPipelineViewportStateCreateInfo) {
315 .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
316 .viewportCount = 1,
317 .scissorCount = 1,
318 },
319 .pRasterState = &(VkPipelineRasterStateCreateInfo) {
320 .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO,
321 .depthClipEnable = true,
322 .rasterizerDiscardEnable = false,
323 .fillMode = VK_FILL_MODE_SOLID,
324 .cullMode = VK_CULL_MODE_NONE,
325 .frontFace = VK_FRONT_FACE_CCW
326 },
327 .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
328 .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
329 .rasterSamples = 1,
330 .sampleShadingEnable = false,
331 .pSampleMask = (VkSampleMask[]) { UINT32_MAX },
332 },
333 .pDepthStencilState = &(VkPipelineDepthStencilStateCreateInfo) {
334 .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
335 .depthTestEnable = true,
336 .depthWriteEnable = true,
337 .depthCompareOp = VK_COMPARE_OP_ALWAYS,
338 .depthBoundsTestEnable = false,
339 .stencilTestEnable = true,
340 .front = (VkStencilOpState) {
341 .stencilPassOp = VK_STENCIL_OP_REPLACE,
342 .stencilCompareOp = VK_COMPARE_OP_ALWAYS,
343 },
344 .back = (VkStencilOpState) {
345 .stencilPassOp = VK_STENCIL_OP_REPLACE,
346 .stencilCompareOp = VK_COMPARE_OP_ALWAYS,
347 },
348 },
349 .pColorBlendState = &(VkPipelineColorBlendStateCreateInfo) {
350 .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
351 .attachmentCount = 1,
352 .pAttachments = (VkPipelineColorBlendAttachmentState []) {
353 { .channelWriteMask = VK_CHANNEL_A_BIT |
354 VK_CHANNEL_R_BIT | VK_CHANNEL_G_BIT | VK_CHANNEL_B_BIT },
355 }
356 },
357 .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
358 .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
359 .dynamicStateCount = 9,
360 .pDynamicStates = (VkDynamicState[]) {
361 VK_DYNAMIC_STATE_VIEWPORT,
362 VK_DYNAMIC_STATE_SCISSOR,
363 VK_DYNAMIC_STATE_LINE_WIDTH,
364 VK_DYNAMIC_STATE_DEPTH_BIAS,
365 VK_DYNAMIC_STATE_BLEND_CONSTANTS,
366 VK_DYNAMIC_STATE_DEPTH_BOUNDS,
367 VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK,
368 VK_DYNAMIC_STATE_STENCIL_WRITE_MASK,
369 VK_DYNAMIC_STATE_STENCIL_REFERENCE,
370 },
371 },
372 .flags = 0,
373 .renderPass = anv_render_pass_to_handle(&anv_meta_dummy_renderpass),
374 .subpass = 0,
375 },
376 &(struct anv_graphics_pipeline_create_info) {
377 .use_repclear = true,
378 .disable_viewport = true,
379 .disable_vs = true,
380 .use_rectlist = true
381 },
382 &device->meta_state.clear.pipeline);
383
384 anv_DestroyShader(anv_device_to_handle(device), vs);
385 anv_DestroyShader(anv_device_to_handle(device), fs);
386 ralloc_free(vsm.nir);
387 ralloc_free(fsm.nir);
388 }
389
390 void anv_CmdClearColorImage(
391 VkCmdBuffer cmdBuffer,
392 VkImage _image,
393 VkImageLayout imageLayout,
394 const VkClearColorValue* pColor,
395 uint32_t rangeCount,
396 const VkImageSubresourceRange* pRanges)
397 {
398 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, cmdBuffer);
399 ANV_FROM_HANDLE(anv_image, image, _image);
400 struct anv_meta_saved_state saved_state;
401
402 anv_meta_save(&saved_state, cmd_buffer,
403 (1 << VK_DYNAMIC_STATE_VIEWPORT));
404 cmd_buffer->state.dynamic.viewport.count = 0;
405
406 for (uint32_t r = 0; r < rangeCount; r++) {
407 for (uint32_t l = 0; l < pRanges[r].mipLevels; l++) {
408 for (uint32_t s = 0; s < pRanges[r].arraySize; s++) {
409 struct anv_image_view iview;
410 anv_image_view_init(&iview, cmd_buffer->device,
411 &(VkImageViewCreateInfo) {
412 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
413 .image = _image,
414 .viewType = VK_IMAGE_VIEW_TYPE_2D,
415 .format = image->format->vk_format,
416 .channels = {
417 VK_CHANNEL_SWIZZLE_R,
418 VK_CHANNEL_SWIZZLE_G,
419 VK_CHANNEL_SWIZZLE_B,
420 VK_CHANNEL_SWIZZLE_A
421 },
422 .subresourceRange = {
423 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
424 .baseMipLevel = pRanges[r].baseMipLevel + l,
425 .mipLevels = 1,
426 .baseArrayLayer = pRanges[r].baseArrayLayer + s,
427 .arraySize = 1
428 },
429 },
430 cmd_buffer);
431
432 VkFramebuffer fb;
433 anv_CreateFramebuffer(anv_device_to_handle(cmd_buffer->device),
434 &(VkFramebufferCreateInfo) {
435 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
436 .attachmentCount = 1,
437 .pAttachments = (VkImageView[]) {
438 anv_image_view_to_handle(&iview),
439 },
440 .width = iview.extent.width,
441 .height = iview.extent.height,
442 .layers = 1
443 }, &fb);
444
445 VkRenderPass pass;
446 anv_CreateRenderPass(anv_device_to_handle(cmd_buffer->device),
447 &(VkRenderPassCreateInfo) {
448 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
449 .attachmentCount = 1,
450 .pAttachments = &(VkAttachmentDescription) {
451 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
452 .format = iview.format->vk_format,
453 .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
454 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
455 .initialLayout = VK_IMAGE_LAYOUT_GENERAL,
456 .finalLayout = VK_IMAGE_LAYOUT_GENERAL,
457 },
458 .subpassCount = 1,
459 .pSubpasses = &(VkSubpassDescription) {
460 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
461 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
462 .inputCount = 0,
463 .colorCount = 1,
464 .pColorAttachments = &(VkAttachmentReference) {
465 .attachment = 0,
466 .layout = VK_IMAGE_LAYOUT_GENERAL,
467 },
468 .pResolveAttachments = NULL,
469 .depthStencilAttachment = (VkAttachmentReference) {
470 .attachment = VK_ATTACHMENT_UNUSED,
471 .layout = VK_IMAGE_LAYOUT_GENERAL,
472 },
473 .preserveCount = 1,
474 .pPreserveAttachments = &(VkAttachmentReference) {
475 .attachment = 0,
476 .layout = VK_IMAGE_LAYOUT_GENERAL,
477 },
478 },
479 .dependencyCount = 0,
480 }, &pass);
481
482 ANV_CALL(CmdBeginRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer),
483 &(VkRenderPassBeginInfo) {
484 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
485 .renderArea = {
486 .offset = { 0, 0, },
487 .extent = {
488 .width = iview.extent.width,
489 .height = iview.extent.height,
490 },
491 },
492 .renderPass = pass,
493 .framebuffer = fb,
494 .clearValueCount = 1,
495 .pClearValues = NULL,
496 }, VK_RENDER_PASS_CONTENTS_INLINE);
497
498 struct clear_instance_data instance_data = {
499 .vue_header = {
500 .RTAIndex = 0,
501 .ViewportIndex = 0,
502 .PointWidth = 0.0
503 },
504 .color = *pColor,
505 };
506
507 meta_emit_clear(cmd_buffer, 1, &instance_data,
508 (VkClearDepthStencilValue) {0});
509
510 ANV_CALL(CmdEndRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer));
511 }
512 }
513 }
514
515 anv_meta_restore(&saved_state, cmd_buffer);
516 }
517
518 void anv_CmdClearDepthStencilImage(
519 VkCmdBuffer cmdBuffer,
520 VkImage image,
521 VkImageLayout imageLayout,
522 const VkClearDepthStencilValue* pDepthStencil,
523 uint32_t rangeCount,
524 const VkImageSubresourceRange* pRanges)
525 {
526 stub();
527 }
528
529 void anv_CmdClearColorAttachment(
530 VkCmdBuffer cmdBuffer,
531 uint32_t colorAttachment,
532 VkImageLayout imageLayout,
533 const VkClearColorValue* pColor,
534 uint32_t rectCount,
535 const VkRect3D* pRects)
536 {
537 stub();
538 }
539
540 void anv_CmdClearDepthStencilAttachment(
541 VkCmdBuffer cmdBuffer,
542 VkImageAspectFlags aspectMask,
543 VkImageLayout imageLayout,
544 const VkClearDepthStencilValue* pDepthStencil,
545 uint32_t rectCount,
546 const VkRect3D* pRects)
547 {
548 stub();
549 }