vk/0.210.0: Switch to the new-style handle declarations
[mesa.git] / src / 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 .arraySize = 1,
53 .samples = 1,
54 .tiling = VK_IMAGE_TILING_LINEAR,
55 .usage = VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT,
56 .flags = 0,
57 }, &copy_image);
58 assert(result == VK_SUCCESS);
59
60 VkMemoryRequirements reqs;
61 result = anv_GetImageMemoryRequirements(vk_device, copy_image, &reqs);
62
63 VkDeviceMemory memory;
64 result = anv_AllocMemory(vk_device,
65 &(VkMemoryAllocInfo) {
66 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
67 .allocationSize = reqs.size,
68 .memoryTypeIndex = 0,
69 }, &memory);
70 assert(result == VK_SUCCESS);
71
72 result = anv_BindImageMemory(vk_device, copy_image, memory, 0);
73 assert(result == VK_SUCCESS);
74
75 VkCmdPool cmdPool;
76 result = anv_CreateCommandPool(vk_device,
77 &(VkCmdPoolCreateInfo) {
78 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
79 .queueFamilyIndex = 0,
80 .flags = 0,
81 }, &cmdPool);
82 assert(result == VK_SUCCESS);
83
84 VkCmdBuffer cmd;
85 result = anv_CreateCommandBuffer(vk_device,
86 &(VkCmdBufferCreateInfo) {
87 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
88 .cmdPool = cmdPool,
89 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
90 .flags = 0,
91 }, &cmd);
92 assert(result == VK_SUCCESS);
93
94 result = anv_BeginCommandBuffer(cmd,
95 &(VkCmdBufferBeginInfo) {
96 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
97 .flags = VK_CMD_BUFFER_OPTIMIZE_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 .aspect = VK_IMAGE_ASPECT_COLOR,
107 .mipLevel = miplevel,
108 .arrayLayer = array_layer,
109 .arraySize = 1,
110 },
111 .srcOffset = (VkOffset3D) { 0, 0, 0 },
112 .srcExtent = (VkExtent3D) {
113 extent.width,
114 extent.height,
115 1
116 },
117 .destSubresource = {
118 .aspect = VK_IMAGE_ASPECT_COLOR,
119 .mipLevel = 0,
120 .arrayLayer = 0,
121 .arraySize = 1,
122 },
123 .destOffset = (VkOffset3D) { 0, 0, 0 },
124 .destExtent = (VkExtent3D) {
125 extent.width,
126 extent.height,
127 1
128 },
129 }, VK_TEX_FILTER_NEAREST);
130
131 ANV_CALL(CmdPipelineBarrier)(cmd,
132 VK_PIPELINE_STAGE_TRANSFER_BIT,
133 VK_PIPELINE_STAGE_TRANSFER_BIT,
134 true, 1,
135 (const void * []) { &(VkImageMemoryBarrier) {
136 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
137 .outputMask = VK_MEMORY_OUTPUT_TRANSFER_BIT,
138 .inputMask = VK_MEMORY_INPUT_HOST_READ_BIT,
139 .oldLayout = VK_IMAGE_LAYOUT_GENERAL,
140 .newLayout = VK_IMAGE_LAYOUT_GENERAL,
141 .srcQueueFamilyIndex = 0,
142 .destQueueFamilyIndex = 0,
143 .image = copy_image,
144 .subresourceRange = (VkImageSubresourceRange) {
145 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
146 .baseMipLevel = 0,
147 .mipLevels = 1,
148 .baseArrayLayer = 0,
149 .arraySize = 1,
150 },
151 }});
152
153 result = anv_EndCommandBuffer(cmd);
154 assert(result == VK_SUCCESS);
155
156 VkFence fence;
157 result = anv_CreateFence(vk_device,
158 &(VkFenceCreateInfo) {
159 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
160 .flags = 0,
161 }, &fence);
162 assert(result == VK_SUCCESS);
163
164 result = anv_QueueSubmit(anv_queue_to_handle(&device->queue),
165 1, &cmd, 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);
172 anv_DestroyCommandPool(vk_device, cmdPool);
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 result = anv_GetImageSubresourceLayout(vk_device, copy_image,
180 &(VkImageSubresource) {
181 .aspect = VK_IMAGE_ASPECT_COLOR,
182 .mipLevel = 0,
183 .arrayLayer = 0,
184 }, &layout);
185 assert(result == VK_SUCCESS);
186
187 map += layout.offset;
188
189 /* Now we can finally write the PPM file */
190 FILE *file = fopen(filename, "wb");
191 assert(file);
192
193 fprintf(file, "P6\n%d %d\n255\n", extent.width, extent.height);
194 for (unsigned y = 0; y < extent.height; y++) {
195 uint8_t row[extent.width * 3];
196 for (unsigned x = 0; x < extent.width; x++) {
197 row[x * 3 + 0] = map[x * 4 + 0];
198 row[x * 3 + 1] = map[x * 4 + 1];
199 row[x * 3 + 2] = map[x * 4 + 2];
200 }
201 fwrite(row, 3, extent.width, file);
202
203 map += layout.rowPitch;
204 }
205 fclose(file);
206
207 anv_UnmapMemory(vk_device, memory);
208 anv_DestroyImage(vk_device, copy_image);
209 anv_FreeMemory(vk_device, memory);
210 }