-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgvk-getting-started-03-texture-mapping.cpp
399 lines (348 loc) · 19.4 KB
/
gvk-getting-started-03-texture-mapping.cpp
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*******************************************************************************
MIT License
Copyright (c) Intel Corporation
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************/
#include "gvk-sample-utilities.hpp"
#include "stb/stb_image.h"
#include <array>
#include <cassert>
#include <iostream>
#include <vector>
VkResult create_image_and_view(const gvk::Context& context, gvk::ImageView* pImageView)
{
assert(pImageView);
// We're declaring pImageData outside of our gvk_result_scope because we're
// responsible for cleaning it up and we need to make sure that we handle
// cases where an error has caused us to break out of the gvk_result_scope...
stbi_uc* pImageData = nullptr;
gvk_result_scope_begin(VK_ERROR_INITIALIZATION_FAILED) {
// We're using stb (https://github.com/nothings/stb) to load image data. Stb
// will give us the pixel data, width, height, and channel count of the loaded
// image. We're passing 4 in for the number of channels we want stb to give
// us...if we passed 0, stb would provide the number of channels provided by
// the loaded image.
int width = 0;
int height = 0;
int channels = 0;
#if 1
pImageData = stbi_load_from_memory(gvk_sample_get_png().data(), (int)gvk_sample_get_png().size(), &width, &height, &channels, 4);
#else
pImageData = stbi_load("path/to/image.png", &width, &height, &channels, 4);
#endif
gvk_result(pImageData ? VK_SUCCESS : VK_ERROR_INITIALIZATION_FAILED);
// Create a gvk::Image with the width and height returned from stb. We're
// setting VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT
// because we're going to use the gvk::Image as a trasfer destination when we
// copy data to it and we're going to sample from it during rendering...
auto imageCreateInfo = gvk::get_default<VkImageCreateInfo>();
imageCreateInfo.extent.width = width;
imageCreateInfo.extent.height = height;
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
imageCreateInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
imageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imageCreateInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
VmaAllocationCreateInfo allocationCreateInfo{ };
allocationCreateInfo.usage = VMA_MEMORY_USAGE_AUTO;
gvk::Image image;
gvk_result(gvk::Image::create(context.get<gvk::Devices>()[0], &imageCreateInfo, &allocationCreateInfo, &image));
// Get the gvk::Device object's VmaAllocator, then get the gvk::Image
// object's VmaAllocationInfo...we'll use this to get the required
// size to create a gvk::Buffer to use as a staging resource for
// uploading the image data...
VmaAllocationInfo allocationInfo{ };
auto vmaAllocator = context.get<gvk::Devices>()[0].get<VmaAllocator>();
vmaGetAllocationInfo(vmaAllocator, image.get<VmaAllocation>(), &allocationInfo);
// Create the staging gvk::Buffer.
// Because we're using VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT
// we should only perform sequential writes like memcpy(). See VMA's
// documentation for more info...
// https://gpuopen-librariesandsdks.github.io/VulkanMemoryAllocator/html/choosing_memory_type.html
auto bufferCreateInfo = gvk::get_default<VkBufferCreateInfo>();
bufferCreateInfo.size = allocationInfo.size;
bufferCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
allocationCreateInfo.usage = VMA_MEMORY_USAGE_AUTO;
allocationCreateInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
gvk::Buffer stagingBuffer;
gvk_result(gvk::Buffer::create(context.get<gvk::Devices>()[0], &bufferCreateInfo, &allocationCreateInfo, &stagingBuffer));
// Copy the image data that we got from stb into the gvk::Buffer...
uint8_t* pStagingData = nullptr;
gvk_result(vmaMapMemory(vmaAllocator, stagingBuffer.get<VmaAllocation>(), (void**)&pStagingData));
memcpy(pStagingData, pImageData, width * height * 4 * sizeof(uint8_t));
vmaUnmapMemory(vmaAllocator, stagingBuffer.get<VmaAllocation>());
// We need to record cmds to execute the transfer. gvk::execute_immediately()
// will prepare a given VkCommandBuffer (gvk handles implicitly convert to and
// from their underlying Vulkan type) for one time submission, execute the
// given callback, then submit the VkCommandBuffer on the given VkQueue. If a
// VkFence isn't provided, gvk::execute_immediately() will block the calling
// thread until execution of the given VkCommandBuffer has completed...
gvk_result(gvk::execute_immediately(
context.get<gvk::Devices>()[0],
gvk::get_queue_family(context.get<gvk::Devices>()[0], 0).queues[0],
context.get<gvk::CommandBuffers>()[0],
VK_NULL_HANDLE,
[&](auto)
{
// We start by recording a VkImageMemoryBarrier that will transition the
// gvk::Image to VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL and make it available at
// VK_PIPELINE_STAGE_TRANSFER_BIT...
auto imageMemoryBarrier = gvk::get_default<VkImageMemoryBarrier>();
imageMemoryBarrier.srcAccessMask = 0;
imageMemoryBarrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
imageMemoryBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
imageMemoryBarrier.image = image;
vkCmdPipelineBarrier(
context.get<gvk::CommandBuffers>()[0],
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT,
0,
0, nullptr,
0, nullptr,
1, &imageMemoryBarrier
);
// Copy from the staging resource to the gvk::Image...
auto bufferImageCopy = gvk::get_default<VkBufferImageCopy>();
bufferImageCopy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
bufferImageCopy.imageSubresource.layerCount = 1;
bufferImageCopy.imageExtent = imageCreateInfo.extent;
vkCmdCopyBufferToImage(context.get<gvk::CommandBuffers>()[0], stagingBuffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &bufferImageCopy);
// Then transition the gvk::Image to VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
// after VK_PIPELINE_STAGE_TRANSFER_BIT and make it available at
// VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT since it will be sampled from during
// fragment shading...
imageMemoryBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
imageMemoryBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
imageMemoryBarrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
imageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
vkCmdPipelineBarrier(
context.get<gvk::CommandBuffers>()[0],
VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
0,
0, nullptr,
0, nullptr,
1, &imageMemoryBarrier
);
}
));
// Create gvk::ImageView...
auto imageViewCreateInfo = gvk::get_default<VkImageViewCreateInfo>();
imageViewCreateInfo.image = image;
imageViewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
imageViewCreateInfo.format = imageCreateInfo.format;
gvk_result(gvk::ImageView::create(context.get<gvk::Devices>()[0], &imageViewCreateInfo, nullptr, pImageView));
} gvk_result_scope_end;
// If we got image data from stb, we need to free it...
if (pImageData) {
stbi_image_free(pImageData);
}
return gvkResult;
}
VkResult create_mesh(const gvk::Context& context, const glm::vec2& extent, gvk::Mesh* pMesh)
{
assert(pMesh);
gvk_result_scope_begin(VK_ERROR_INITIALIZATION_FAILED) {
float a = 1.0f / std::max(extent[0], extent[1]);
auto w = extent[0] * a * 0.5f;
auto h = extent[1] * a * 0.5f;
std::array<VertexPositionTexcoord, 4> vertices {
VertexPositionTexcoord {{ -w, 0, -h }, { 0, 0 }},
VertexPositionTexcoord {{ w, 0, -h }, { 1, 0 }},
VertexPositionTexcoord {{ w, 0, h }, { 1, 1 }},
VertexPositionTexcoord {{ -w, 0, h }, { 0, 1 }},
};
std::array<uint16_t, 6> indices {
0, 1, 2,
2, 3, 0,
};
gvk_result(pMesh->write(
context.get<gvk::Devices>()[0],
gvk::get_queue_family(context.get<gvk::Devices>()[0], 0).queues[0],
context.get<gvk::CommandBuffers>()[0],
VK_NULL_HANDLE,
(uint32_t)vertices.size(),
vertices.data(),
(uint32_t)indices.size(),
indices.data()
));
} gvk_result_scope_end;
return gvkResult;
}
int main(int, const char*[])
{
gvk_result_scope_begin(VK_ERROR_INITIALIZATION_FAILED) {
GvkSampleContext context;
gvk_result(GvkSampleContext::create("Intel(R) GPA Utilities for Vulkan* - Getting Started - 03 - Texture Mapping", &context));
gvk::system::Surface systemSurface;
gvk_result(gvk_sample_create_sys_surface(context, &systemSurface));
gvk::wsi::Context wsiContext;
gvk_result(gvk_sample_create_wsi_context(context, systemSurface, &wsiContext));
gvk::spirv::ShaderInfo vertexShaderInfo{ };
vertexShaderInfo.language = gvk::spirv::ShadingLanguage::Glsl;
vertexShaderInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
vertexShaderInfo.lineOffset = __LINE__;
vertexShaderInfo.source = R"(
#version 450
layout(set = 0, binding = 0)
uniform UniformBuffer
{
mat4 world;
mat4 view;
mat4 projection;
} ubo;
layout(location = 0) in vec3 vsPosition;
layout(location = 1) in vec2 vsTexcoord;
layout(location = 0) out vec2 fsTexcoord;
out gl_PerVertex
{
vec4 gl_Position;
};
void main()
{
gl_Position = ubo.projection * ubo.view * ubo.world * vec4(vsPosition, 1);
fsTexcoord = vsTexcoord;
}
)";
gvk::spirv::ShaderInfo fragmentShaderInfo{ };
fragmentShaderInfo.language = gvk::spirv::ShadingLanguage::Glsl;
fragmentShaderInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
fragmentShaderInfo.lineOffset = __LINE__;
fragmentShaderInfo.source = R"(
#version 450
/**
This provides access to VkImage data referenced by the VkImageView referenced by
the descriptor at binding 1 of the VkDescriptorSet bound by the call to
vkCmdBindDescriptorSets() during VkCommandBuffer recording.
*/
layout(set = 0, binding = 1) uniform sampler2D image;
layout(location = 0) in vec2 fsTexcoord;
layout(location = 0) out vec4 fragColor;
void main()
{
fragColor = texture(image, fsTexcoord);
}
)";
gvk::Pipeline pipeline;
gvk_result(gvk_sample_create_pipeline<VertexPositionTexcoord>(
wsiContext.get<gvk::RenderPass>(),
VK_CULL_MODE_BACK_BIT,
vertexShaderInfo,
fragmentShaderInfo,
&pipeline
));
// Create gvk::ImageView...
gvk::ImageView imageView;
gvk_result(create_image_and_view(context, &imageView));
auto imageViewExtent = imageView.get<gvk::Image>().get<VkImageCreateInfo>().extent;
// Create gvk::Sampler...
gvk::Sampler sampler;
gvk_result(gvk::Sampler::create(context.get<gvk::Devices>()[0], &gvk::get_default<VkSamplerCreateInfo>(), nullptr, &sampler));
gvk::Mesh mesh;
gvk_result(create_mesh(context, { (float)imageViewExtent.width, (float)imageViewExtent.height }, &mesh));
gvk::Buffer uniformBuffer;
gvk_result(gvk_sample_create_uniform_buffer<Uniforms>(context, &uniformBuffer));
VmaAllocationInfo uniformBufferAllocationInfo{ };
vmaGetAllocationInfo(context.get<gvk::Devices>()[0].get<VmaAllocator>(), uniformBuffer.get<VmaAllocation>(), &uniformBufferAllocationInfo);
std::vector<gvk::DescriptorSet> descriptorSets;
gvk_result(gvk_sample_allocate_descriptor_sets(pipeline, descriptorSets));
assert(descriptorSets.size() == 1);
auto descriptorSet = descriptorSets[0];
auto descriptorBufferInfo = gvk::get_default<VkDescriptorBufferInfo>();
descriptorBufferInfo.buffer = uniformBuffer;
descriptorBufferInfo.range = VK_WHOLE_SIZE;
// Prepare a VkDescriptorImageInfo with our gvk::Sampler and gvk::ImageView...
auto descriptorImageInfo = gvk::get_default<VkDescriptorImageInfo>();
descriptorImageInfo.sampler = sampler;
descriptorImageInfo.imageView = imageView;
descriptorImageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
// Update the gvk::DescriptorSet with gvk::Buffer and gvk::Image descriptors...
std::array<VkWriteDescriptorSet, 2> writeDescriptorSets {
VkWriteDescriptorSet {
/* .sType = */ gvk::get_stype<VkWriteDescriptorSet>(),
/* .pNext = */ nullptr,
/* .dstSet = */ descriptorSet,
/* .dstBinding = */ 0,
/* .dstArrayElement = */ 0,
/* .descriptorCount = */ 1,
/* .descriptorType = */ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
/* .pImageInfo = */ nullptr,
/* .pBufferInfo = */ &descriptorBufferInfo,
/* .pTexelBufferView = */ nullptr,
},
VkWriteDescriptorSet {
/* .sType = */ gvk::get_stype<VkWriteDescriptorSet>(),
/* .pNext = */ nullptr,
/* .dstSet = */ descriptorSet,
/* .dstBinding = */ 1,
/* .dstArrayElement = */ 0,
/* .descriptorCount = */ 1,
/* .descriptorType = */ VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
/* .pImageInfo = */ &descriptorImageInfo,
/* .pBufferInfo = */ nullptr,
/* .pTexelBufferView = */ nullptr,
}
};
vkUpdateDescriptorSets(context.get<gvk::Devices>()[0], (uint32_t)writeDescriptorSets.size(), writeDescriptorSets.data(), 0, nullptr);
gvk::system::Clock clock;
gvk::math::Camera camera;
camera.transform.translation = { 0, 1.25f, 1.25f };
gvk::math::Transform quadTransform;
while (
!(systemSurface.get<gvk::system::Input>().keyboard.down(gvk::system::Key::Escape)) &&
!(systemSurface.get<gvk::system::Surface::StatusFlags>() & gvk::system::Surface::CloseRequested)) {
gvk::system::Surface::update();
clock.update();
auto rotation = 90.0f * clock.elapsed<gvk::system::Seconds<float>>();
quadTransform.rotation *= glm::angleAxis(glm::radians(rotation), glm::vec3 { 0, 1, 0 });
Uniforms uniforms{ };
uniforms.object.world = quadTransform.world_from_local();
uniforms.camera.view = camera.view(quadTransform.translation);
uniforms.camera.projection = camera.projection();
memcpy(uniformBufferAllocationInfo.pMappedData, &uniforms, sizeof(Uniforms));
gvk::wsi::AcquiredImageInfo acquiredImageInfo{ };
gvk::RenderTarget acquiredImageRenderTarget = VK_NULL_HANDLE;
auto wsiStatus = wsiContext.acquire_next_image(UINT64_MAX, VK_NULL_HANDLE, &acquiredImageInfo, &acquiredImageRenderTarget);
if (wsiStatus == VK_SUCCESS || wsiStatus == VK_SUBOPTIMAL_KHR) {
const auto& device = context.get<gvk::Devices>()[0];
auto extent = wsiContext.get<gvk::SwapchainKHR>().get<VkSwapchainCreateInfoKHR>().imageExtent;
camera.set_aspect_ratio(extent.width, extent.height);
gvk_result(vkBeginCommandBuffer(acquiredImageInfo.commandBuffer, &gvk::get_default<VkCommandBufferBeginInfo>()));
const auto& renderPassBeginInfo = acquiredImageRenderTarget.get<VkRenderPassBeginInfo>();
vkCmdBeginRenderPass(acquiredImageInfo.commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
VkRect2D scissor { { }, renderPassBeginInfo.renderArea.extent };
vkCmdSetScissor(acquiredImageInfo.commandBuffer, 0, 1, &scissor);
VkViewport viewport { 0, 0, (float)scissor.extent.width, (float)scissor.extent.height, 0, 1 };
vkCmdSetViewport(acquiredImageInfo.commandBuffer, 0, 1, &viewport);
vkCmdBindPipeline(acquiredImageInfo.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
vkCmdBindDescriptorSets(acquiredImageInfo.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.get<gvk::PipelineLayout>(), 0, 1, &descriptorSet.get<VkDescriptorSet>(), 0, nullptr);
mesh.record_cmds(acquiredImageInfo.commandBuffer);
vkCmdEndRenderPass(acquiredImageInfo.commandBuffer);
gvk_result(vkEndCommandBuffer(acquiredImageInfo.commandBuffer));
const auto& queue = gvk::get_queue_family(device, 0).queues[0];
gvk_result(vkQueueSubmit(queue, 1, &wsiContext.get<VkSubmitInfo>(acquiredImageInfo), acquiredImageInfo.fence));
wsiStatus = wsiContext.queue_present(queue, &acquiredImageInfo);
gvk_result((wsiStatus == VK_SUBOPTIMAL_KHR || wsiStatus == VK_ERROR_OUT_OF_DATE_KHR) ? VK_SUCCESS : wsiStatus);
}
}
gvk_result(vkDeviceWaitIdle(context.get<gvk::Devices>()[0]));
} gvk_result_scope_end;
if (gvkResult) {
std::cerr << gvk::to_string(gvkResult) << std::endl;
}
return (int)gvkResult;
}