forked from jherico/VulkanExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexturearray.cpp
More file actions
322 lines (262 loc) · 12.1 KB
/
Copy pathtexturearray.cpp
File metadata and controls
322 lines (262 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* Vulkan Example - Texture arrays and instanced rendering
*
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
*
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/
#include "vulkanExampleBase.h"
// Vertex layout for this example
struct Vertex {
float pos[3];
float uv[2];
};
class VulkanExample : public vkx::ExampleBase {
public:
// Number of array layers in texture array
// Also used as instance count
vkx::Texture textureArray;
struct {
vk::PipelineVertexInputStateCreateInfo inputState;
std::vector<vk::VertexInputBindingDescription> bindingDescriptions;
std::vector<vk::VertexInputAttributeDescription> attributeDescriptions;
} vertices;
struct {
vkx::MeshBuffer quad;
} meshes;
struct {
vkx::UniformData vertexShader;
} uniformData;
struct UboInstanceData {
// Model matrix
glm::mat4 model;
// Texture array index
// Vec4 due to padding
glm::vec4 arrayIndex;
};
struct {
// Global matrices
struct {
glm::mat4 projection;
glm::mat4 view;
} matrices;
// Seperate data for each instance
UboInstanceData instance[8];
} uboVS;
struct {
vk::Pipeline solid;
} pipelines;
vk::PipelineLayout pipelineLayout;
vk::DescriptorSet descriptorSet;
vk::DescriptorSetLayout descriptorSetLayout;
VulkanExample() : vkx::ExampleBase(ENABLE_VALIDATION) {
camera.setZoom(-15.0f);
rotationSpeed = 0.25f;
camera.setRotation({ -15.0f, 35.0f, 0.0f });
title = "Vulkan Example - Texture arrays";
srand(time(NULL));
}
~VulkanExample() {
// Clean up used Vulkan resources
// Note : Inherited destructor cleans up resources stored in base class
device.destroyPipeline(pipelines.solid);
device.destroyPipelineLayout(pipelineLayout);
device.destroyDescriptorSetLayout(descriptorSetLayout);
meshes.quad.destroy();
uniformData.vertexShader.destroy();
// Clean up texture resources
textureArray.destroy();
}
void loadTextures() {
textureArray = textureLoader->loadTextureArray(getAssetPath() + "textures/texturearray_bc3.ktx", vk::Format::eBc3UnormBlock);
}
void updateDrawCommandBuffer(const vk::CommandBuffer& cmdBuffer) {
cmdBuffer.setViewport(0, vkx::viewport(size));
cmdBuffer.setScissor(0, vkx::rect2D(size));
cmdBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipelineLayout, 0, descriptorSet, nullptr);
cmdBuffer.bindVertexBuffers(VERTEX_BUFFER_BIND_ID, meshes.quad.vertices.buffer, { 0 });
cmdBuffer.bindIndexBuffer(meshes.quad.indices.buffer, 0, vk::IndexType::eUint32);
cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipelines.solid);
cmdBuffer.drawIndexed(meshes.quad.indexCount, textureArray.layerCount, 0, 0, 0);
}
// Setup vertices for a single uv-mapped quad
void generateQuad() {
#define dim 2.5f
std::vector<Vertex> vertexBuffer =
{
{ { dim, dim, 0.0f }, { 1.0f, 1.0f } },
{ { -dim, dim, 0.0f }, { 0.0f, 1.0f } },
{ { -dim, -dim, 0.0f }, { 0.0f, 0.0f } },
{ { dim, -dim, 0.0f }, { 1.0f, 0.0f } }
};
#undef dim
meshes.quad.vertices = stageToDeviceBuffer(vk::BufferUsageFlagBits::eVertexBuffer, vertexBuffer);
// Setup indices
std::vector<uint32_t> indexBuffer = { 0,1,2, 2,3,0 };
meshes.quad.indexCount = indexBuffer.size();
meshes.quad.indices = stageToDeviceBuffer(vk::BufferUsageFlagBits::eVertexBuffer, indexBuffer);
}
void setupVertexDescriptions() {
// Binding description
vertices.bindingDescriptions.resize(1);
vertices.bindingDescriptions[0] =
vkx::vertexInputBindingDescription(VERTEX_BUFFER_BIND_ID, sizeof(Vertex), vk::VertexInputRate::eVertex);
// Attribute descriptions
// Describes memory layout and shader positions
vertices.attributeDescriptions.resize(2);
// Location 0 : Position
vertices.attributeDescriptions[0] =
vkx::vertexInputAttributeDescription(VERTEX_BUFFER_BIND_ID, 0, vk::Format::eR32G32B32Sfloat, 0);
// Location 1 : Texture coordinates
vertices.attributeDescriptions[1] =
vkx::vertexInputAttributeDescription(VERTEX_BUFFER_BIND_ID, 1, vk::Format::eR32G32Sfloat, sizeof(float) * 3);
vertices.inputState = vk::PipelineVertexInputStateCreateInfo();
vertices.inputState.vertexBindingDescriptionCount = vertices.bindingDescriptions.size();
vertices.inputState.pVertexBindingDescriptions = vertices.bindingDescriptions.data();
vertices.inputState.vertexAttributeDescriptionCount = vertices.attributeDescriptions.size();
vertices.inputState.pVertexAttributeDescriptions = vertices.attributeDescriptions.data();
}
void setupDescriptorPool() {
std::vector<vk::DescriptorPoolSize> poolSizes =
{
vkx::descriptorPoolSize(vk::DescriptorType::eUniformBuffer, 1),
vkx::descriptorPoolSize(vk::DescriptorType::eCombinedImageSampler, 1)
};
vk::DescriptorPoolCreateInfo descriptorPoolInfo =
vkx::descriptorPoolCreateInfo(poolSizes.size(), poolSizes.data(), 2);
descriptorPool = device.createDescriptorPool(descriptorPoolInfo);
}
void setupDescriptorSetLayout() {
std::vector<vk::DescriptorSetLayoutBinding> setLayoutBindings =
{
// Binding 0 : Vertex shader uniform buffer
vkx::descriptorSetLayoutBinding(
vk::DescriptorType::eUniformBuffer,
vk::ShaderStageFlagBits::eVertex,
0),
// Binding 1 : Fragment shader image sampler (texture array)
vkx::descriptorSetLayoutBinding(
vk::DescriptorType::eCombinedImageSampler,
vk::ShaderStageFlagBits::eFragment,
1)
};
vk::DescriptorSetLayoutCreateInfo descriptorLayout =
vkx::descriptorSetLayoutCreateInfo(setLayoutBindings.data(), setLayoutBindings.size());
descriptorSetLayout = device.createDescriptorSetLayout(descriptorLayout);
vk::PipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
vkx::pipelineLayoutCreateInfo(&descriptorSetLayout, 1);
pipelineLayout = device.createPipelineLayout(pPipelineLayoutCreateInfo);
}
void setupDescriptorSet() {
vk::DescriptorSetAllocateInfo allocInfo =
vkx::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1);
descriptorSet = device.allocateDescriptorSets(allocInfo)[0];
// vk::Image descriptor for the texture array
vk::DescriptorImageInfo texArrayDescriptor =
vkx::descriptorImageInfo(textureArray.sampler, textureArray.view, vk::ImageLayout::eGeneral);
std::vector<vk::WriteDescriptorSet> writeDescriptorSets =
{
// Binding 0 : Vertex shader uniform buffer
vkx::writeDescriptorSet(
descriptorSet,
vk::DescriptorType::eUniformBuffer,
0,
&uniformData.vertexShader.descriptor),
// Binding 1 : Fragment shader cubemap sampler
vkx::writeDescriptorSet(
descriptorSet,
vk::DescriptorType::eCombinedImageSampler,
1,
&texArrayDescriptor)
};
device.updateDescriptorSets(writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL);
}
void preparePipelines() {
vk::PipelineInputAssemblyStateCreateInfo inputAssemblyState =
vkx::pipelineInputAssemblyStateCreateInfo(vk::PrimitiveTopology::eTriangleList, vk::PipelineInputAssemblyStateCreateFlags(), VK_FALSE);
vk::PipelineRasterizationStateCreateInfo rasterizationState =
vkx::pipelineRasterizationStateCreateInfo(vk::PolygonMode::eFill, vk::CullModeFlagBits::eNone, vk::FrontFace::eCounterClockwise);
vk::PipelineColorBlendAttachmentState blendAttachmentState =
vkx::pipelineColorBlendAttachmentState();
vk::PipelineColorBlendStateCreateInfo colorBlendState =
vkx::pipelineColorBlendStateCreateInfo(1, &blendAttachmentState);
vk::PipelineDepthStencilStateCreateInfo depthStencilState =
vkx::pipelineDepthStencilStateCreateInfo(VK_TRUE, VK_TRUE, vk::CompareOp::eLessOrEqual);
vk::PipelineViewportStateCreateInfo viewportState =
vkx::pipelineViewportStateCreateInfo(1, 1);
vk::PipelineMultisampleStateCreateInfo multisampleState =
vkx::pipelineMultisampleStateCreateInfo(vk::SampleCountFlagBits::e1);
std::vector<vk::DynamicState> dynamicStateEnables = {
vk::DynamicState::eViewport,
vk::DynamicState::eScissor
};
vk::PipelineDynamicStateCreateInfo dynamicState =
vkx::pipelineDynamicStateCreateInfo(dynamicStateEnables.data(), dynamicStateEnables.size());
// Instacing pipeline
// Load shaders
std::array<vk::PipelineShaderStageCreateInfo, 2> shaderStages;
shaderStages[0] = loadShader(getAssetPath() + "shaders/texturearray/instancing.vert.spv", vk::ShaderStageFlagBits::eVertex);
shaderStages[1] = loadShader(getAssetPath() + "shaders/texturearray/instancing.frag.spv", vk::ShaderStageFlagBits::eFragment);
vk::GraphicsPipelineCreateInfo pipelineCreateInfo =
vkx::pipelineCreateInfo(pipelineLayout, renderPass);
pipelineCreateInfo.pVertexInputState = &vertices.inputState;
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
pipelineCreateInfo.pRasterizationState = &rasterizationState;
pipelineCreateInfo.pColorBlendState = &colorBlendState;
pipelineCreateInfo.pMultisampleState = &multisampleState;
pipelineCreateInfo.pViewportState = &viewportState;
pipelineCreateInfo.pDepthStencilState = &depthStencilState;
pipelineCreateInfo.pDynamicState = &dynamicState;
pipelineCreateInfo.stageCount = shaderStages.size();
pipelineCreateInfo.pStages = shaderStages.data();
pipelines.solid = device.createGraphicsPipelines(pipelineCache, pipelineCreateInfo, nullptr)[0];
}
void prepareUniformBuffers() {
// Vertex shader uniform buffer block
uniformData.vertexShader = createUniformBuffer(uboVS);
// Array indices and model matrices are fixed
float offset = -1.5f;
float center = (textureArray.layerCount*offset) / 2;
for (uint32_t i = 0; i < textureArray.layerCount; i++) {
// Instance model matrix
uboVS.instance[i].model = glm::translate(glm::mat4(), glm::vec3(0.0f, i * offset - center, 0.0f)) *
glm::mat4_cast(glm::angleAxis(glm::radians(60.0f), glm::vec3(1.0f, 0.0f, 0.0f)));
// Instance texture array index
uboVS.instance[i].arrayIndex.x = i;
}
// Update instanced part of the uniform buffer
uniformData.vertexShader.copy(uboVS);
updateUniformBufferMatrices();
}
void updateUniformBufferMatrices() {
// Only updates the uniform buffer block part containing the global matrices
// Projection
uboVS.matrices.projection = camera.matrices.perspective;
// View
uboVS.matrices.view = camera.matrices.view;
// Only update the matrices part of the uniform buffer
uniformData.vertexShader.copy(uboVS.matrices);
}
void prepare() {
ExampleBase::prepare();
setupVertexDescriptions();
loadTextures();
generateQuad();
prepareUniformBuffers();
setupDescriptorSetLayout();
preparePipelines();
setupDescriptorPool();
setupDescriptorSet();
updateDrawCommandBuffers();
prepared = true;
}
virtual void render() {
if (!prepared)
return;
draw();
}
virtual void viewChanged() {
updateUniformBufferMatrices();
}
};
RUN_EXAMPLE(VulkanExample)