Merge remote-tracking branch 'origin/master' into vulkan
[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 /* This file contains utility functions for help debugging. They can be
27 * called from GDB or similar to help inspect images and buffers.
28 */
29
30 void
31 anv_dump_image_to_ppm(struct anv_device *device,
32 struct anv_image *image, unsigned miplevel,
33 unsigned array_layer, const char *filename)
34 {
35 VkDevice vk_device = anv_device_to_handle(device);
36 VkResult result;
37
38 VkExtent2D extent = { image->extent.width, image->extent.height };
39 for (unsigned i = 0; i < miplevel; i++) {
40 extent.width = MAX2(1, extent.width / 2);
41 extent.height = MAX2(1, extent.height / 2);
42 }
43
44 VkImage copy_image;
45 result = anv_CreateImage(vk_device,
46 &(VkImageCreateInfo) {
47 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
48 .imageType = VK_IMAGE_TYPE_2D,
49 .format = VK_FORMAT_R8G8B8A8_UNORM,
50 .extent = (VkExtent3D) { extent.width, extent.height, 1 },
51 .mipLevels = 1,
52 .arrayLayers = 1,
53 .samples = 1,
54 .tiling = VK_IMAGE_TILING_LINEAR,
55 .usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT,
56 .flags = 0,
57 }, NULL, &copy_image);
58 assert(result == VK_SUCCESS);
59
60 VkMemoryRequirements reqs;
61 anv_GetImageMemoryRequirements(vk_device, copy_image, &reqs);
62
63 VkDeviceMemory memory;
64 result = anv_AllocateMemory(vk_device,
65 &(VkMemoryAllocateInfo) {
66 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
67 .allocationSize = reqs.size,
68 .memoryTypeIndex = 0,
69 }, NULL, &memory);
70 assert(result == VK_SUCCESS);
71
72 result = anv_BindImageMemory(vk_device, copy_image, memory, 0);
73 assert(result == VK_SUCCESS);
74
75 VkCommandPool commandPool;
76 result = anv_CreateCommandPool(vk_device,
77 &(VkCommandPoolCreateInfo) {
78 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
79 .queueFamilyIndex = 0,
80 .flags = 0,
81 }, NULL, &commandPool);
82 assert(result == VK_SUCCESS);
83
84 VkCommandBuffer cmd;
85 result = anv_AllocateCommandBuffers(vk_device,
86 &(VkCommandBufferAllocateInfo) {
87 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
88 .commandPool = commandPool,
89 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
90 .commandBufferCount = 1,
91 }, &cmd);
92 assert(result == VK_SUCCESS);
93
94 result = anv_BeginCommandBuffer(cmd,
95 &(VkCommandBufferBeginInfo) {
96 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
97 .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
98 });
99 assert(result == VK_SUCCESS);
100
101 anv_CmdBlitImage(cmd,
102 anv_image_to_handle(image), VK_IMAGE_LAYOUT_GENERAL,
103 copy_image, VK_IMAGE_LAYOUT_GENERAL, 1,
104 &(VkImageBlit) {
105 .srcSubresource = {
106 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
107 .mipLevel = miplevel,
108 .baseArrayLayer = array_layer,
109 .layerCount = 1,
110 },
111 .srcOffsets = {
112 { 0, 0, 0 },
113 { extent.width, extent.height, 1 },
114 },
115 .dstSubresource = {
116 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
117 .mipLevel = 0,
118 .baseArrayLayer = 0,
119 .layerCount = 1,
120 },
121 .dstOffsets = {
122 { 0, 0, 0 },
123 { extent.width, extent.height, 1 },
124 },
125 }, VK_FILTER_NEAREST);
126
127 ANV_CALL(CmdPipelineBarrier)(cmd,
128 VK_PIPELINE_STAGE_TRANSFER_BIT,
129 VK_PIPELINE_STAGE_TRANSFER_BIT,
130 true, 0, NULL, 0, NULL, 1,
131 &(VkImageMemoryBarrier) {
132 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
133 .srcAccessMask = VK_ACCESS_HOST_READ_BIT,
134 .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
135 .oldLayout = VK_IMAGE_LAYOUT_GENERAL,
136 .newLayout = VK_IMAGE_LAYOUT_GENERAL,
137 .srcQueueFamilyIndex = 0,
138 .dstQueueFamilyIndex = 0,
139 .image = copy_image,
140 .subresourceRange = (VkImageSubresourceRange) {
141 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
142 .baseMipLevel = 0,
143 .levelCount = 1,
144 .baseArrayLayer = 0,
145 .layerCount = 1,
146 },
147 });
148
149 result = anv_EndCommandBuffer(cmd);
150 assert(result == VK_SUCCESS);
151
152 VkFence fence;
153 result = anv_CreateFence(vk_device,
154 &(VkFenceCreateInfo) {
155 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
156 .flags = 0,
157 }, NULL, &fence);
158 assert(result == VK_SUCCESS);
159
160 result = anv_QueueSubmit(anv_queue_to_handle(&device->queue), 1,
161 &(VkSubmitInfo) {
162 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
163 .commandBufferCount = 1,
164 .pCommandBuffers = &cmd,
165 }, fence);
166 assert(result == VK_SUCCESS);
167
168 result = anv_WaitForFences(vk_device, 1, &fence, true, UINT64_MAX);
169 assert(result == VK_SUCCESS);
170
171 anv_DestroyFence(vk_device, fence, NULL);
172 anv_DestroyCommandPool(vk_device, commandPool, NULL);
173
174 uint8_t *map;
175 result = anv_MapMemory(vk_device, memory, 0, reqs.size, 0, (void **)&map);
176 assert(result == VK_SUCCESS);
177
178 VkSubresourceLayout layout;
179 anv_GetImageSubresourceLayout(vk_device, copy_image,
180 &(VkImageSubresource) {
181 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
182 .mipLevel = 0,
183 .arrayLayer = 0,
184 }, &layout);
185
186 map += layout.offset;
187
188 /* Now we can finally write the PPM file */
189 FILE *file = fopen(filename, "wb");
190 assert(file);
191
192 fprintf(file, "P6\n%d %d\n255\n", extent.width, extent.height);
193 for (unsigned y = 0; y < extent.height; y++) {
194 uint8_t row[extent.width * 3];
195 for (unsigned x = 0; x < extent.width; x++) {
196 row[x * 3 + 0] = map[x * 4 + 0];
197 row[x * 3 + 1] = map[x * 4 + 1];
198 row[x * 3 + 2] = map[x * 4 + 2];
199 }
200 fwrite(row, 3, extent.width, file);
201
202 map += layout.rowPitch;
203 }
204 fclose(file);
205
206 anv_UnmapMemory(vk_device, memory);
207 anv_DestroyImage(vk_device, copy_image, NULL);
208 anv_FreeMemory(vk_device, memory, NULL);
209 }