Skip to content
Navigation Menu
{{ message }}
forked from LunarG/VulkanSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_texture.cpp
More file actions
300 lines (256 loc) · 12.1 KB
/
Copy pathinit_texture.cpp
File metadata and controls
300 lines (256 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* Vulkan Samples
*
* Copyright (C) 2015-2016 Valve Corporation
* Copyright (C) 2015-2016 LunarG, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
VULKAN_SAMPLE_SHORT_DESCRIPTION
Inititalize Texture
*/
/* This is part of the draw cube progression */
#include <sys/stat.h>
#include <util_init.hpp>
#include <assert.h>
#include <cstdlib>
int sample_main(int argc, char *argv[]) {
VkResult U_ASSERT_ONLY res;
bool U_ASSERT_ONLY pass;
struct sample_info info = {};
char sample_title[] = "Texture Initialization Sample";
init_global_layer_properties(info);
init_instance_extension_names(info);
init_device_extension_names(info);
init_instance(info, sample_title);
init_enumerate_device(info);
init_connection(info);
init_window_size(info, 50, 50);
init_window(info);
init_swapchain_extension(info);
init_device(info);
init_command_pool(info);
init_command_buffer(info);
execute_begin_command_buffer(info);
init_device_queue(info);
/* VULKAN_KEY_START */
/*
* Set up textures:
* - Create a linear tiled image
* - Map it and write the texture data into it
* - If linear images cannot be used as textures, create an optimally
* tiled image and blit from the linearly tiled image to the optimally
* tiled image
* -
* -
* -
*/
struct texture_object texObj;
std::string filename = get_base_data_dir();
filename.append("lunarg.ppm");
#ifndef __ANDROID__
struct stat statstruct;
if (stat(filename.c_str(), &statstruct)) {
filename = "../../API-Samples/data/lunarg.ppm";
}
#endif
if (!read_ppm(filename.c_str(), texObj.tex_width, texObj.tex_height, 0, NULL)) {
std::cout << "Could not read texture file lunarg.ppm\n";
exit(-1);
}
VkFormatProperties formatProps;
vkGetPhysicalDeviceFormatProperties(info.gpus[0], VK_FORMAT_R8G8B8A8_UNORM, &formatProps);
/* See if we can use a linear tiled image for a texture, if not, we will
* need a staging buffer for the texture data */
texObj.needs_staging = (!(formatProps.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT));
VkImageCreateInfo image_create_info = {};
image_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
image_create_info.pNext = NULL;
image_create_info.imageType = VK_IMAGE_TYPE_2D;
image_create_info.format = VK_FORMAT_R8G8B8A8_UNORM;
image_create_info.extent.width = texObj.tex_width;
image_create_info.extent.height = texObj.tex_height;
image_create_info.extent.depth = 1;
image_create_info.mipLevels = 1;
image_create_info.arrayLayers = 1;
image_create_info.samples = NUM_SAMPLES;
image_create_info.tiling = texObj.needs_staging ? VK_IMAGE_TILING_OPTIMAL : VK_IMAGE_TILING_LINEAR;
image_create_info.initialLayout = texObj.needs_staging ? VK_IMAGE_LAYOUT_UNDEFINED : VK_IMAGE_LAYOUT_PREINITIALIZED;
image_create_info.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
if (texObj.needs_staging) {
image_create_info.usage |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
}
image_create_info.queueFamilyIndexCount = 0;
image_create_info.pQueueFamilyIndices = NULL;
image_create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
image_create_info.flags = 0;
VkMemoryAllocateInfo mem_alloc = {};
mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
mem_alloc.pNext = NULL;
mem_alloc.allocationSize = 0;
mem_alloc.memoryTypeIndex = 0;
VkMemoryRequirements mem_reqs;
res = vkCreateImage(info.device, &image_create_info, NULL, &texObj.image);
assert(res == VK_SUCCESS);
vkGetImageMemoryRequirements(info.device, texObj.image, &mem_reqs);
mem_alloc.allocationSize = mem_reqs.size;
VkFlags requirements = texObj.needs_staging ? 0 : (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
pass = memory_type_from_properties(info, mem_reqs.memoryTypeBits, requirements, &mem_alloc.memoryTypeIndex);
assert(pass && "No mappable, coherent memory");
/* allocate memory */
res = vkAllocateMemory(info.device, &mem_alloc, NULL, &(texObj.image_memory));
assert(res == VK_SUCCESS);
/* bind memory */
res = vkBindImageMemory(info.device, texObj.image, texObj.image_memory, 0);
assert(res == VK_SUCCESS);
VkImageSubresource subres = {};
subres.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
subres.mipLevel = 0;
subres.arrayLayer = 0;
VkSubresourceLayout layout = {};
void *data;
if (texObj.needs_staging) {
/* Need a staging buffer to map and copy texture into */
VkBufferCreateInfo buffer_create_info = {};
buffer_create_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
buffer_create_info.pNext = NULL;
buffer_create_info.flags = 0;
buffer_create_info.size = texObj.tex_width * texObj.tex_height * 4;
buffer_create_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
buffer_create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
buffer_create_info.queueFamilyIndexCount = 0;
buffer_create_info.pQueueFamilyIndices = NULL;
res = vkCreateBuffer(info.device, &buffer_create_info, NULL, &texObj.buffer);
assert(res == VK_SUCCESS);
VkMemoryAllocateInfo buf_mem_alloc = {};
buf_mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
buf_mem_alloc.pNext = NULL;
buf_mem_alloc.allocationSize = 0;
buf_mem_alloc.memoryTypeIndex = 0;
vkGetBufferMemoryRequirements(info.device, texObj.buffer, &mem_reqs);
buf_mem_alloc.allocationSize = mem_reqs.size;
requirements = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
pass = memory_type_from_properties(info, mem_reqs.memoryTypeBits, requirements, &buf_mem_alloc.memoryTypeIndex);
assert(pass && "No mappable, coherent memory");
/* allocate memory */
res = vkAllocateMemory(info.device, &buf_mem_alloc, NULL, &(texObj.buffer_memory));
assert(res == VK_SUCCESS);
/* bind memory */
res = vkBindBufferMemory(info.device, texObj.buffer, texObj.buffer_memory, 0);
assert(res == VK_SUCCESS);
} else {
texObj.buffer = VK_NULL_HANDLE;
texObj.buffer_memory = VK_NULL_HANDLE;
/* Get the subresource layout so we know what the row pitch is */
vkGetImageSubresourceLayout(info.device, texObj.image, &subres, &layout);
}
VkDeviceMemory mapped_memory = texObj.needs_staging ? texObj.buffer_memory : texObj.image_memory;
res = vkMapMemory(info.device, mapped_memory, 0, mem_reqs.size, 0, &data);
assert(res == VK_SUCCESS);
/* Read the ppm file into the mappable image's memory */
if (!read_ppm(filename.c_str(), texObj.tex_width, texObj.tex_height,
texObj.needs_staging ? (texObj.tex_width * 4) : layout.rowPitch, (unsigned char *)data)) {
std::cout << "Could not load texture file lunarg.ppm\n";
exit(-1);
}
vkUnmapMemory(info.device, mapped_memory);
if (!texObj.needs_staging) {
/* If we can use the linear tiled image as a texture, just do it */
texObj.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
set_image_layout(info, texObj.image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED, texObj.imageLayout,
VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
} else {
/* Since we're going to blit to the texture image, set its layout to
* DESTINATION_OPTIMAL */
set_image_layout(info, texObj.image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
VkBufferImageCopy copy_region;
copy_region.bufferOffset = 0;
copy_region.bufferRowLength = texObj.tex_width;
copy_region.bufferImageHeight = texObj.tex_height;
copy_region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
copy_region.imageSubresource.mipLevel = 0;
copy_region.imageSubresource.baseArrayLayer = 0;
copy_region.imageSubresource.layerCount = 1;
copy_region.imageOffset.x = 0;
copy_region.imageOffset.y = 0;
copy_region.imageOffset.z = 0;
copy_region.imageExtent.width = texObj.tex_width;
copy_region.imageExtent.height = texObj.tex_height;
copy_region.imageExtent.depth = 1;
/* Put the copy command into the command buffer */
vkCmdCopyBufferToImage(info.cmd, texObj.buffer, texObj.image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©_region);
/* Set the layout for the texture image from DESTINATION_OPTIMAL to
* SHADER_READ_ONLY */
texObj.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
set_image_layout(info, texObj.image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, texObj.imageLayout,
VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
}
execute_end_command_buffer(info);
execute_queue_command_buffer(info);
VkSamplerCreateInfo samplerCreateInfo = {};
samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
samplerCreateInfo.magFilter = VK_FILTER_NEAREST;
samplerCreateInfo.minFilter = VK_FILTER_NEAREST;
samplerCreateInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;
samplerCreateInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
samplerCreateInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
samplerCreateInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
samplerCreateInfo.mipLodBias = 0.0;
samplerCreateInfo.anisotropyEnable = VK_FALSE;
samplerCreateInfo.maxAnisotropy = 1;
samplerCreateInfo.compareEnable = VK_FALSE;
samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
samplerCreateInfo.minLod = 0.0;
samplerCreateInfo.maxLod = 0.0;
samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
/* create sampler */
res = vkCreateSampler(info.device, &samplerCreateInfo, NULL, &texObj.sampler);
assert(res == VK_SUCCESS);
VkImageViewCreateInfo view_info = {};
view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
view_info.pNext = NULL;
view_info.image = VK_NULL_HANDLE;
view_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
view_info.format = VK_FORMAT_R8G8B8A8_UNORM;
view_info.components.r = VK_COMPONENT_SWIZZLE_R;
view_info.components.g = VK_COMPONENT_SWIZZLE_G;
view_info.components.b = VK_COMPONENT_SWIZZLE_B;
view_info.components.a = VK_COMPONENT_SWIZZLE_A;
view_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
view_info.subresourceRange.baseMipLevel = 0;
view_info.subresourceRange.levelCount = 1;
view_info.subresourceRange.baseArrayLayer = 0;
view_info.subresourceRange.layerCount = 1;
/* create image view */
view_info.image = texObj.image;
res = vkCreateImageView(info.device, &view_info, NULL, &texObj.view);
assert(res == VK_SUCCESS);
info.textures.push_back(texObj);
/* VULKAN_KEY_END */
/* Clean Up */
vkDestroySampler(info.device, texObj.sampler, NULL);
vkDestroyImageView(info.device, texObj.view, NULL);
vkDestroyImage(info.device, texObj.image, NULL);
vkFreeMemory(info.device, texObj.image_memory, NULL);
/* Release the resources for the staging image */
vkFreeMemory(info.device, texObj.buffer_memory, NULL);
vkDestroyBuffer(info.device, texObj.buffer, NULL);
destroy_command_buffer(info);
destroy_command_pool(info);
destroy_device(info);
destroy_window(info);
destroy_instance(info);
return 0;
}
You can’t perform that action at this time.
