anv/dump: Fix vkCmdPipelineBarrier flags
[mesa.git] / src / intel / vulkan / anv_dump.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_private.h"
25
26 #include "util/list.h"
27 #include "util/ralloc.h"
28
29 /* This file contains utility functions for help debugging. They can be
30 * called from GDB or similar to help inspect images and buffers.
31 *
32 * To dump the framebuffers of an application after each render pass, all you
33 * have to do is the following
34 *
35 * 1) Start the application in GDB
36 * 2) Run until you get to the point where the rendering errors occur
37 * 3) Pause in GDB and set a breakpoint in anv_QueuePresentKHR
38 * 4) Continue until it reaches anv_QueuePresentKHR
39 * 5) Call anv_dump_start(queue->device, ANV_DUMP_FRAMEBUFFERS_BIT)
40 * 6) Continue until the next anv_QueuePresentKHR call
41 * 7) Call anv_dump_finish() to complete the dump and write files
42 *
43 * While it's a bit manual, the process does allow you to do some very
44 * valuable debugging by dumping every render target at the end of every
45 * render pass. It's worth noting that this assumes that the application
46 * creates all of the command buffers more-or-less in-order and between the
47 * two anv_QueuePresentKHR calls.
48 */
49
50 struct dump_image {
51 struct list_head link;
52
53 const char *filename;
54
55 VkExtent2D extent;
56 VkImage image;
57 VkDeviceMemory memory;
58 };
59
60 static void
61 dump_image_init(struct anv_device *device, struct dump_image *image,
62 uint32_t width, uint32_t height, const char *filename)
63 {
64 VkDevice vk_device = anv_device_to_handle(device);
65 MAYBE_UNUSED VkResult result;
66
67 image->filename = filename;
68 image->extent = (VkExtent2D) { width, height };
69
70 result = anv_CreateImage(vk_device,
71 &(VkImageCreateInfo) {
72 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
73 .imageType = VK_IMAGE_TYPE_2D,
74 .format = VK_FORMAT_R8G8B8A8_UNORM,
75 .extent = (VkExtent3D) { width, height, 1 },
76 .mipLevels = 1,
77 .arrayLayers = 1,
78 .samples = 1,
79 .tiling = VK_IMAGE_TILING_LINEAR,
80 .usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT,
81 .flags = 0,
82 }, NULL, &image->image);
83 assert(result == VK_SUCCESS);
84
85 VkMemoryRequirements reqs;
86 anv_GetImageMemoryRequirements(vk_device, image->image, &reqs);
87
88 result = anv_AllocateMemory(vk_device,
89 &(VkMemoryAllocateInfo) {
90 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
91 .allocationSize = reqs.size,
92 .memoryTypeIndex = 0,
93 }, NULL, &image->memory);
94 assert(result == VK_SUCCESS);
95
96 result = anv_BindImageMemory(vk_device, image->image, image->memory, 0);
97 assert(result == VK_SUCCESS);
98 }
99
100 static void
101 dump_image_finish(struct anv_device *device, struct dump_image *image)
102 {
103 VkDevice vk_device = anv_device_to_handle(device);
104
105 anv_DestroyImage(vk_device, image->image, NULL);
106 anv_FreeMemory(vk_device, image->memory, NULL);
107 }
108
109 static void
110 dump_image_do_blit(struct anv_device *device, struct dump_image *image,
111 struct anv_cmd_buffer *cmd_buffer, struct anv_image *src,
112 VkImageAspectFlagBits aspect,
113 unsigned miplevel, unsigned array_layer)
114 {
115 ANV_CALL(CmdPipelineBarrier)(anv_cmd_buffer_to_handle(cmd_buffer),
116 VK_PIPELINE_STAGE_TRANSFER_BIT,
117 VK_PIPELINE_STAGE_TRANSFER_BIT,
118 0, 0, NULL, 0, NULL, 1,
119 &(VkImageMemoryBarrier) {
120 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
121 .srcAccessMask = ~0,
122 .dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT,
123 .oldLayout = VK_IMAGE_LAYOUT_GENERAL,
124 .newLayout = VK_IMAGE_LAYOUT_GENERAL,
125 .srcQueueFamilyIndex = 0,
126 .dstQueueFamilyIndex = 0,
127 .image = anv_image_to_handle(src),
128 .subresourceRange = (VkImageSubresourceRange) {
129 .aspectMask = aspect,
130 .baseMipLevel = miplevel,
131 .levelCount = 1,
132 .baseArrayLayer = array_layer,
133 .layerCount = 1,
134 },
135 });
136
137 /* We need to do a blit so the image needs to be declared as sampled. The
138 * only thing these are used for is making sure we create the correct
139 * views, so it should be find to just stomp it and set it back.
140 */
141 VkImageUsageFlags old_usage = src->usage;
142 src->usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
143
144 anv_CmdBlitImage(anv_cmd_buffer_to_handle(cmd_buffer),
145 anv_image_to_handle(src), VK_IMAGE_LAYOUT_GENERAL,
146 image->image, VK_IMAGE_LAYOUT_GENERAL, 1,
147 &(VkImageBlit) {
148 .srcSubresource = {
149 .aspectMask = aspect,
150 .mipLevel = miplevel,
151 .baseArrayLayer = array_layer,
152 .layerCount = 1,
153 },
154 .srcOffsets = {
155 { 0, 0, 0 },
156 { image->extent.width, image->extent.height, 1 },
157 },
158 .dstSubresource = {
159 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
160 .mipLevel = 0,
161 .baseArrayLayer = 0,
162 .layerCount = 1,
163 },
164 .dstOffsets = {
165 { 0, 0, 0 },
166 { image->extent.width, image->extent.height, 1 },
167 },
168 }, VK_FILTER_NEAREST);
169
170 src->usage = old_usage;
171
172 ANV_CALL(CmdPipelineBarrier)(anv_cmd_buffer_to_handle(cmd_buffer),
173 VK_PIPELINE_STAGE_TRANSFER_BIT,
174 VK_PIPELINE_STAGE_TRANSFER_BIT,
175 0, 0, NULL, 0, NULL, 1,
176 &(VkImageMemoryBarrier) {
177 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
178 .srcAccessMask = VK_ACCESS_HOST_READ_BIT,
179 .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
180 .oldLayout = VK_IMAGE_LAYOUT_GENERAL,
181 .newLayout = VK_IMAGE_LAYOUT_GENERAL,
182 .srcQueueFamilyIndex = 0,
183 .dstQueueFamilyIndex = 0,
184 .image = image->image,
185 .subresourceRange = (VkImageSubresourceRange) {
186 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
187 .baseMipLevel = 0,
188 .levelCount = 1,
189 .baseArrayLayer = 0,
190 .layerCount = 1,
191 },
192 });
193 }
194
195 static void
196 dump_image_write_to_ppm(struct anv_device *device, struct dump_image *image)
197 {
198 VkDevice vk_device = anv_device_to_handle(device);
199 MAYBE_UNUSED VkResult result;
200
201 VkMemoryRequirements reqs;
202 anv_GetImageMemoryRequirements(vk_device, image->image, &reqs);
203
204 uint8_t *map;
205 result = anv_MapMemory(vk_device, image->memory, 0, reqs.size, 0, (void **)&map);
206 assert(result == VK_SUCCESS);
207
208 VkSubresourceLayout layout;
209 anv_GetImageSubresourceLayout(vk_device, image->image,
210 &(VkImageSubresource) {
211 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
212 .mipLevel = 0,
213 .arrayLayer = 0,
214 }, &layout);
215
216 map += layout.offset;
217
218 FILE *file = fopen(image->filename, "wb");
219 assert(file);
220
221 uint8_t *row = malloc(image->extent.width * 3);
222 assert(row);
223
224 fprintf(file, "P6\n%d %d\n255\n", image->extent.width, image->extent.height);
225 for (unsigned y = 0; y < image->extent.height; y++) {
226 for (unsigned x = 0; x < image->extent.width; x++) {
227 row[x * 3 + 0] = map[x * 4 + 0];
228 row[x * 3 + 1] = map[x * 4 + 1];
229 row[x * 3 + 2] = map[x * 4 + 2];
230 }
231 fwrite(row, 3, image->extent.width, file);
232
233 map += layout.rowPitch;
234 }
235 free(row);
236 fclose(file);
237
238 anv_UnmapMemory(vk_device, image->memory);
239 }
240
241 void
242 anv_dump_image_to_ppm(struct anv_device *device,
243 struct anv_image *image, unsigned miplevel,
244 unsigned array_layer, VkImageAspectFlagBits aspect,
245 const char *filename)
246 {
247 VkDevice vk_device = anv_device_to_handle(device);
248 MAYBE_UNUSED VkResult result;
249
250 const uint32_t width = anv_minify(image->extent.width, miplevel);
251 const uint32_t height = anv_minify(image->extent.height, miplevel);
252
253 struct dump_image dump;
254 dump_image_init(device, &dump, width, height, filename);
255
256 VkCommandPool commandPool;
257 result = anv_CreateCommandPool(vk_device,
258 &(VkCommandPoolCreateInfo) {
259 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
260 .queueFamilyIndex = 0,
261 .flags = 0,
262 }, NULL, &commandPool);
263 assert(result == VK_SUCCESS);
264
265 VkCommandBuffer cmd;
266 result = anv_AllocateCommandBuffers(vk_device,
267 &(VkCommandBufferAllocateInfo) {
268 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
269 .commandPool = commandPool,
270 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
271 .commandBufferCount = 1,
272 }, &cmd);
273 assert(result == VK_SUCCESS);
274
275 result = anv_BeginCommandBuffer(cmd,
276 &(VkCommandBufferBeginInfo) {
277 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
278 .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
279 });
280 assert(result == VK_SUCCESS);
281
282 dump_image_do_blit(device, &dump, anv_cmd_buffer_from_handle(cmd), image,
283 aspect, miplevel, array_layer);
284
285 result = anv_EndCommandBuffer(cmd);
286 assert(result == VK_SUCCESS);
287
288 VkFence fence;
289 result = anv_CreateFence(vk_device,
290 &(VkFenceCreateInfo) {
291 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
292 .flags = 0,
293 }, NULL, &fence);
294 assert(result == VK_SUCCESS);
295
296 result = anv_QueueSubmit(anv_queue_to_handle(&device->queue), 1,
297 &(VkSubmitInfo) {
298 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
299 .commandBufferCount = 1,
300 .pCommandBuffers = &cmd,
301 }, fence);
302 assert(result == VK_SUCCESS);
303
304 result = anv_WaitForFences(vk_device, 1, &fence, true, UINT64_MAX);
305 assert(result == VK_SUCCESS);
306
307 anv_DestroyFence(vk_device, fence, NULL);
308 anv_DestroyCommandPool(vk_device, commandPool, NULL);
309
310 dump_image_write_to_ppm(device, &dump);
311 dump_image_finish(device, &dump);
312 }
313
314 static pthread_mutex_t dump_mutex = PTHREAD_MUTEX_INITIALIZER;
315
316 static enum anv_dump_action dump_actions = 0;
317
318 /* Used to prevent recursive dumping */
319 static enum anv_dump_action dump_old_actions;
320
321 struct list_head dump_list;
322 static void *dump_ctx;
323 static struct anv_device *dump_device;
324 static unsigned dump_count;
325
326 void
327 anv_dump_start(struct anv_device *device, enum anv_dump_action actions)
328 {
329 pthread_mutex_lock(&dump_mutex);
330
331 dump_device = device;
332 dump_actions = actions;
333 list_inithead(&dump_list);
334 dump_ctx = ralloc_context(NULL);
335 dump_count = 0;
336
337 pthread_mutex_unlock(&dump_mutex);
338 }
339
340 void
341 anv_dump_finish()
342 {
343 anv_DeviceWaitIdle(anv_device_to_handle(dump_device));
344
345 pthread_mutex_lock(&dump_mutex);
346
347 list_for_each_entry(struct dump_image, dump, &dump_list, link) {
348 dump_image_write_to_ppm(dump_device, dump);
349 dump_image_finish(dump_device, dump);
350 }
351
352 dump_actions = 0;
353 dump_device = NULL;
354 list_inithead(&dump_list);
355
356 ralloc_free(dump_ctx);
357 dump_ctx = NULL;
358
359 pthread_mutex_unlock(&dump_mutex);
360 }
361
362 static bool
363 dump_lock(enum anv_dump_action action)
364 {
365 if (likely((dump_actions & action) == 0))
366 return false;
367
368 pthread_mutex_lock(&dump_mutex);
369
370 /* Prevent recursive dumping */
371 dump_old_actions = dump_actions;
372 dump_actions = 0;
373
374 return true;
375 }
376
377 static void
378 dump_unlock()
379 {
380 dump_actions = dump_old_actions;
381 pthread_mutex_unlock(&dump_mutex);
382 }
383
384 static void
385 dump_add_image(struct anv_cmd_buffer *cmd_buffer, struct anv_image *image,
386 VkImageAspectFlagBits aspect,
387 unsigned miplevel, unsigned array_layer, const char *filename)
388 {
389 const uint32_t width = anv_minify(image->extent.width, miplevel);
390 const uint32_t height = anv_minify(image->extent.height, miplevel);
391
392 struct dump_image *dump = ralloc(dump_ctx, struct dump_image);
393
394 dump_image_init(cmd_buffer->device, dump, width, height, filename);
395 dump_image_do_blit(cmd_buffer->device, dump, cmd_buffer, image,
396 aspect, miplevel, array_layer);
397
398 list_addtail(&dump->link, &dump_list);
399 }
400
401 void
402 anv_dump_add_framebuffer(struct anv_cmd_buffer *cmd_buffer,
403 struct anv_framebuffer *fb)
404 {
405 if (!dump_lock(ANV_DUMP_FRAMEBUFFERS_BIT))
406 return;
407
408 unsigned dump_idx = dump_count++;
409
410 for (unsigned i = 0; i < fb->attachment_count; i++) {
411 struct anv_image_view *iview = fb->attachments[i];
412
413 uint32_t b;
414 for_each_bit(b, iview->image->aspects) {
415 VkImageAspectFlagBits aspect = (1 << b);
416 char suffix;
417 switch (aspect) {
418 case VK_IMAGE_ASPECT_COLOR_BIT: suffix = 'c'; break;
419 case VK_IMAGE_ASPECT_DEPTH_BIT: suffix = 'd'; break;
420 case VK_IMAGE_ASPECT_STENCIL_BIT: suffix = 's'; break;
421 default:
422 unreachable("Invalid aspect");
423 }
424
425 char *filename = ralloc_asprintf(dump_ctx, "framebuffer%04d-%d%c.ppm",
426 dump_idx, i, suffix);
427
428 dump_add_image(cmd_buffer, (struct anv_image *)iview->image, aspect,
429 iview->base_mip, iview->base_layer, filename);
430 }
431 }
432
433 dump_unlock();
434 }