vulkan/wsi: Do image creation in common code
[mesa.git] / src / amd / vulkan / radv_meta_fast_clear.c
1 /*
2 * Copyright © 2016 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 <assert.h>
25 #include <stdbool.h>
26
27 #include "radv_meta.h"
28 #include "radv_private.h"
29 #include "sid.h"
30
31 static VkResult
32 create_pass(struct radv_device *device)
33 {
34 VkResult result;
35 VkDevice device_h = radv_device_to_handle(device);
36 const VkAllocationCallbacks *alloc = &device->meta_state.alloc;
37 VkAttachmentDescription attachment;
38
39 attachment.format = VK_FORMAT_UNDEFINED;
40 attachment.samples = 1;
41 attachment.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
42 attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
43 attachment.initialLayout = VK_IMAGE_LAYOUT_GENERAL;
44 attachment.finalLayout = VK_IMAGE_LAYOUT_GENERAL;
45
46 result = radv_CreateRenderPass(device_h,
47 &(VkRenderPassCreateInfo) {
48 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
49 .attachmentCount = 1,
50 .pAttachments = &attachment,
51 .subpassCount = 1,
52 .pSubpasses = &(VkSubpassDescription) {
53 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
54 .inputAttachmentCount = 0,
55 .colorAttachmentCount = 1,
56 .pColorAttachments = (VkAttachmentReference[]) {
57 {
58 .attachment = 0,
59 .layout = VK_IMAGE_LAYOUT_GENERAL,
60 },
61 },
62 .pResolveAttachments = NULL,
63 .pDepthStencilAttachment = &(VkAttachmentReference) {
64 .attachment = VK_ATTACHMENT_UNUSED,
65 },
66 .preserveAttachmentCount = 0,
67 .pPreserveAttachments = NULL,
68 },
69 .dependencyCount = 0,
70 },
71 alloc,
72 &device->meta_state.fast_clear_flush.pass);
73
74 return result;
75 }
76
77 static VkResult
78 create_pipeline(struct radv_device *device,
79 VkShaderModule vs_module_h)
80 {
81 VkResult result;
82 VkDevice device_h = radv_device_to_handle(device);
83
84 struct radv_shader_module fs_module = {
85 .nir = radv_meta_build_nir_fs_noop(),
86 };
87
88 if (!fs_module.nir) {
89 /* XXX: Need more accurate error */
90 result = VK_ERROR_OUT_OF_HOST_MEMORY;
91 goto cleanup;
92 }
93
94 const VkPipelineShaderStageCreateInfo stages[2] = {
95 {
96 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
97 .stage = VK_SHADER_STAGE_VERTEX_BIT,
98 .module = vs_module_h,
99 .pName = "main",
100 },
101 {
102 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
103 .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
104 .module = radv_shader_module_to_handle(&fs_module),
105 .pName = "main",
106 },
107 };
108
109 const VkPipelineVertexInputStateCreateInfo vi_state = {
110 .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
111 .vertexBindingDescriptionCount = 0,
112 .vertexAttributeDescriptionCount = 0,
113 };
114
115 const VkPipelineInputAssemblyStateCreateInfo ia_state = {
116 .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
117 .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
118 .primitiveRestartEnable = false,
119 };
120
121 const VkPipelineColorBlendStateCreateInfo blend_state = {
122 .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
123 .logicOpEnable = false,
124 .attachmentCount = 1,
125 .pAttachments = (VkPipelineColorBlendAttachmentState []) {
126 {
127 .colorWriteMask = VK_COLOR_COMPONENT_R_BIT |
128 VK_COLOR_COMPONENT_G_BIT |
129 VK_COLOR_COMPONENT_B_BIT |
130 VK_COLOR_COMPONENT_A_BIT,
131 },
132 }
133 };
134 const VkPipelineRasterizationStateCreateInfo rs_state = {
135 .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
136 .depthClampEnable = false,
137 .rasterizerDiscardEnable = false,
138 .polygonMode = VK_POLYGON_MODE_FILL,
139 .cullMode = VK_CULL_MODE_NONE,
140 .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE,
141 };
142
143 result = radv_graphics_pipeline_create(device_h,
144 radv_pipeline_cache_to_handle(&device->meta_state.cache),
145 &(VkGraphicsPipelineCreateInfo) {
146 .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
147 .stageCount = 2,
148 .pStages = stages,
149
150 .pVertexInputState = &vi_state,
151 .pInputAssemblyState = &ia_state,
152
153 .pViewportState = &(VkPipelineViewportStateCreateInfo) {
154 .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
155 .viewportCount = 1,
156 .scissorCount = 1,
157 },
158 .pRasterizationState = &rs_state,
159 .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
160 .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
161 .rasterizationSamples = 1,
162 .sampleShadingEnable = false,
163 .pSampleMask = NULL,
164 .alphaToCoverageEnable = false,
165 .alphaToOneEnable = false,
166 },
167 .pColorBlendState = &blend_state,
168 .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
169 .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
170 .dynamicStateCount = 2,
171 .pDynamicStates = (VkDynamicState[]) {
172 VK_DYNAMIC_STATE_VIEWPORT,
173 VK_DYNAMIC_STATE_SCISSOR,
174 },
175 },
176 .renderPass = device->meta_state.fast_clear_flush.pass,
177 .subpass = 0,
178 },
179 &(struct radv_graphics_pipeline_create_info) {
180 .use_rectlist = true,
181 .custom_blend_mode = V_028808_CB_ELIMINATE_FAST_CLEAR,
182 },
183 &device->meta_state.alloc,
184 &device->meta_state.fast_clear_flush.cmask_eliminate_pipeline);
185 if (result != VK_SUCCESS)
186 goto cleanup;
187
188 result = radv_graphics_pipeline_create(device_h,
189 radv_pipeline_cache_to_handle(&device->meta_state.cache),
190 &(VkGraphicsPipelineCreateInfo) {
191 .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
192 .stageCount = 2,
193 .pStages = stages,
194
195 .pVertexInputState = &vi_state,
196 .pInputAssemblyState = &ia_state,
197
198 .pViewportState = &(VkPipelineViewportStateCreateInfo) {
199 .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
200 .viewportCount = 1,
201 .scissorCount = 1,
202 },
203 .pRasterizationState = &rs_state,
204 .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
205 .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
206 .rasterizationSamples = 1,
207 .sampleShadingEnable = false,
208 .pSampleMask = NULL,
209 .alphaToCoverageEnable = false,
210 .alphaToOneEnable = false,
211 },
212 .pColorBlendState = &blend_state,
213 .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
214 .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
215 .dynamicStateCount = 2,
216 .pDynamicStates = (VkDynamicState[]) {
217 VK_DYNAMIC_STATE_VIEWPORT,
218 VK_DYNAMIC_STATE_SCISSOR,
219 },
220 },
221 .renderPass = device->meta_state.fast_clear_flush.pass,
222 .subpass = 0,
223 },
224 &(struct radv_graphics_pipeline_create_info) {
225 .use_rectlist = true,
226 .custom_blend_mode = V_028808_CB_FMASK_DECOMPRESS,
227 },
228 &device->meta_state.alloc,
229 &device->meta_state.fast_clear_flush.fmask_decompress_pipeline);
230 if (result != VK_SUCCESS)
231 goto cleanup_cmask;
232
233 goto cleanup;
234 cleanup_cmask:
235 radv_DestroyPipeline(device_h, device->meta_state.fast_clear_flush.cmask_eliminate_pipeline, &device->meta_state.alloc);
236 cleanup:
237 ralloc_free(fs_module.nir);
238 return result;
239 }
240
241 void
242 radv_device_finish_meta_fast_clear_flush_state(struct radv_device *device)
243 {
244 struct radv_meta_state *state = &device->meta_state;
245
246 radv_DestroyRenderPass(radv_device_to_handle(device),
247 state->fast_clear_flush.pass, &state->alloc);
248 radv_DestroyPipeline(radv_device_to_handle(device),
249 state->fast_clear_flush.cmask_eliminate_pipeline,
250 &state->alloc);
251 radv_DestroyPipeline(radv_device_to_handle(device),
252 state->fast_clear_flush.fmask_decompress_pipeline,
253 &state->alloc);
254 }
255
256 VkResult
257 radv_device_init_meta_fast_clear_flush_state(struct radv_device *device)
258 {
259 VkResult res = VK_SUCCESS;
260
261 struct radv_shader_module vs_module = { .nir = radv_meta_build_nir_vs_generate_vertices() };
262 if (!vs_module.nir) {
263 /* XXX: Need more accurate error */
264 res = VK_ERROR_OUT_OF_HOST_MEMORY;
265 goto fail;
266 }
267
268 res = create_pass(device);
269 if (res != VK_SUCCESS)
270 goto fail;
271
272 VkShaderModule vs_module_h = radv_shader_module_to_handle(&vs_module);
273 res = create_pipeline(device, vs_module_h);
274 if (res != VK_SUCCESS)
275 goto fail;
276
277 goto cleanup;
278
279 fail:
280 radv_device_finish_meta_fast_clear_flush_state(device);
281
282 cleanup:
283 ralloc_free(vs_module.nir);
284
285 return res;
286 }
287
288 static void
289 emit_fast_clear_flush(struct radv_cmd_buffer *cmd_buffer,
290 const VkExtent2D *resolve_extent,
291 VkPipeline pipeline)
292 {
293 VkCommandBuffer cmd_buffer_h = radv_cmd_buffer_to_handle(cmd_buffer);
294
295 radv_CmdBindPipeline(cmd_buffer_h, VK_PIPELINE_BIND_POINT_GRAPHICS,
296 pipeline);
297
298 radv_CmdSetViewport(radv_cmd_buffer_to_handle(cmd_buffer), 0, 1, &(VkViewport) {
299 .x = 0,
300 .y = 0,
301 .width = resolve_extent->width,
302 .height = resolve_extent->height,
303 .minDepth = 0.0f,
304 .maxDepth = 1.0f
305 });
306
307 radv_CmdSetScissor(radv_cmd_buffer_to_handle(cmd_buffer), 0, 1, &(VkRect2D) {
308 .offset = (VkOffset2D) { 0, 0 },
309 .extent = (VkExtent2D) { resolve_extent->width, resolve_extent->height },
310 });
311
312 radv_CmdDraw(cmd_buffer_h, 3, 1, 0, 0);
313 cmd_buffer->state.flush_bits |= (RADV_CMD_FLAG_FLUSH_AND_INV_CB |
314 RADV_CMD_FLAG_FLUSH_AND_INV_CB_META);
315 }
316
317 static void
318 radv_emit_set_predication_state_from_image(struct radv_cmd_buffer *cmd_buffer,
319 struct radv_image *image, bool value)
320 {
321 uint64_t va = 0;
322
323 if (value) {
324 va = radv_buffer_get_va(image->bo) + image->offset;
325 va += image->dcc_pred_offset;
326 }
327
328 si_emit_set_predication_state(cmd_buffer, va);
329 }
330
331 /**
332 */
333 void
334 radv_fast_clear_flush_image_inplace(struct radv_cmd_buffer *cmd_buffer,
335 struct radv_image *image,
336 const VkImageSubresourceRange *subresourceRange)
337 {
338 struct radv_meta_saved_state saved_state;
339 VkDevice device_h = radv_device_to_handle(cmd_buffer->device);
340 VkCommandBuffer cmd_buffer_h = radv_cmd_buffer_to_handle(cmd_buffer);
341 uint32_t layer_count = radv_get_layerCount(image, subresourceRange);
342 VkPipeline pipeline;
343
344 assert(cmd_buffer->queue_family_index == RADV_QUEUE_GENERAL);
345
346 radv_meta_save(&saved_state, cmd_buffer,
347 RADV_META_SAVE_GRAPHICS_PIPELINE |
348 RADV_META_SAVE_PASS);
349
350 if (image->fmask.size > 0) {
351 pipeline = cmd_buffer->device->meta_state.fast_clear_flush.fmask_decompress_pipeline;
352 } else {
353 pipeline = cmd_buffer->device->meta_state.fast_clear_flush.cmask_eliminate_pipeline;
354 }
355
356 if (image->surface.dcc_size) {
357 radv_emit_set_predication_state_from_image(cmd_buffer, image, true);
358 cmd_buffer->state.predicating = true;
359 }
360 for (uint32_t layer = 0; layer < layer_count; ++layer) {
361 struct radv_image_view iview;
362
363 radv_image_view_init(&iview, cmd_buffer->device,
364 &(VkImageViewCreateInfo) {
365 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
366 .image = radv_image_to_handle(image),
367 .viewType = radv_meta_get_view_type(image),
368 .format = image->vk_format,
369 .subresourceRange = {
370 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
371 .baseMipLevel = 0,
372 .levelCount = 1,
373 .baseArrayLayer = subresourceRange->baseArrayLayer + layer,
374 .layerCount = 1,
375 },
376 });
377
378 VkFramebuffer fb_h;
379 radv_CreateFramebuffer(device_h,
380 &(VkFramebufferCreateInfo) {
381 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
382 .attachmentCount = 1,
383 .pAttachments = (VkImageView[]) {
384 radv_image_view_to_handle(&iview)
385 },
386 .width = image->info.width,
387 .height = image->info.height,
388 .layers = 1
389 },
390 &cmd_buffer->pool->alloc,
391 &fb_h);
392
393 radv_CmdBeginRenderPass(cmd_buffer_h,
394 &(VkRenderPassBeginInfo) {
395 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
396 .renderPass = cmd_buffer->device->meta_state.fast_clear_flush.pass,
397 .framebuffer = fb_h,
398 .renderArea = {
399 .offset = {
400 0,
401 0,
402 },
403 .extent = {
404 image->info.width,
405 image->info.height,
406 }
407 },
408 .clearValueCount = 0,
409 .pClearValues = NULL,
410 },
411 VK_SUBPASS_CONTENTS_INLINE);
412
413 emit_fast_clear_flush(cmd_buffer,
414 &(VkExtent2D) { image->info.width, image->info.height },
415 pipeline);
416 radv_CmdEndRenderPass(cmd_buffer_h);
417
418 radv_DestroyFramebuffer(device_h, fb_h,
419 &cmd_buffer->pool->alloc);
420
421 }
422 if (image->surface.dcc_size) {
423 cmd_buffer->state.predicating = false;
424 radv_emit_set_predication_state_from_image(cmd_buffer, image, false);
425 }
426 radv_meta_restore(&saved_state, cmd_buffer);
427 }