forked from SaschaWillems/Vulkan
-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathtexturecubemap.cpp
More file actions
224 lines (186 loc) · 8.48 KB
/
texturecubemap.cpp
File metadata and controls
224 lines (186 loc) · 8.48 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
/*
* Vulkan Example - Cube map texture loading and displaying
*
* 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
vks::model::VertexLayout vertexLayout{ {
vks::model::Component::VERTEX_COMPONENT_POSITION,
vks::model::Component::VERTEX_COMPONENT_NORMAL,
vks::model::Component::VERTEX_COMPONENT_UV,
} };
class VulkanExample : public vkx::ExampleBase {
public:
vks::texture::TextureCubeMap cubeMap;
struct {
vks::model::Model skybox, object;
} meshes;
struct {
vks::Buffer objectVS;
vks::Buffer skyboxVS;
} uniformData;
struct {
glm::mat4 projection;
glm::mat4 model;
} uboVS;
struct {
vk::Pipeline skybox;
vk::Pipeline reflect;
} pipelines;
struct {
vk::DescriptorSet object;
vk::DescriptorSet skybox;
} descriptorSets;
vk::PipelineLayout pipelineLayout;
vk::DescriptorSetLayout descriptorSetLayout;
VulkanExample() {
camera.dolly(-4.0f);
camera.setRotation({ -2.25f, -35.0f, 0.0f });
rotationSpeed = 0.25f;
title = "Vulkan Example - Cube map";
}
~VulkanExample() {
// Clean up used Vulkan resources
// Note : Inherited destructor cleans up resources stored in base class
// Clean up texture resources
cubeMap.destroy();
device.destroyPipeline(pipelines.skybox);
device.destroyPipeline(pipelines.reflect);
device.destroyPipelineLayout(pipelineLayout);
device.destroyDescriptorSetLayout(descriptorSetLayout);
meshes.object.destroy();
meshes.skybox.destroy();
uniformData.objectVS.destroy();
uniformData.skyboxVS.destroy();
}
void updateDrawCommandBuffer(const vk::CommandBuffer& cmdBuffer) {
cmdBuffer.setViewport(0, vks::util::viewport(size, 0.0f, 1.0f));
cmdBuffer.setScissor(0, vks::util::rect2D(size));
vk::DeviceSize offsets = 0;
// Skybox
cmdBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipelineLayout, 0, descriptorSets.skybox, nullptr);
cmdBuffer.bindVertexBuffers(0, meshes.skybox.vertices.buffer, offsets);
cmdBuffer.bindIndexBuffer(meshes.skybox.indices.buffer, 0, vk::IndexType::eUint32);
cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipelines.skybox);
cmdBuffer.drawIndexed(meshes.skybox.indexCount, 1, 0, 0, 0);
// 3D object
cmdBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipelineLayout, 0, descriptorSets.object, nullptr);
cmdBuffer.bindVertexBuffers(0, meshes.object.vertices.buffer, offsets);
cmdBuffer.bindIndexBuffer(meshes.object.indices.buffer, 0, vk::IndexType::eUint32);
cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipelines.reflect);
cmdBuffer.drawIndexed(meshes.object.indexCount, 1, 0, 0, 0);
}
void loadMeshes() {
meshes.object.loadFromFile(context, getAssetPath() + "models/sphere.obj", vertexLayout, 0.05f);
meshes.skybox.loadFromFile(context, getAssetPath() + "models/cube.obj", vertexLayout, 0.05f);
}
void setupDescriptorPool() {
std::vector<vk::DescriptorPoolSize> poolSizes{
{ vk::DescriptorType::eUniformBuffer, 2 },
{ vk::DescriptorType::eCombinedImageSampler, 2 },
};
descriptorPool = device.createDescriptorPool({ {}, 2, (uint32_t)poolSizes.size(), poolSizes.data() });
}
void setupDescriptorSetLayout() {
std::vector<vk::DescriptorSetLayoutBinding> setLayoutBindings{
// Binding 0 : Vertex shader uniform buffer
{ 0, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex },
// Binding 1 : Fragment shader image sampler
{ 1, vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment },
};
descriptorSetLayout = device.createDescriptorSetLayout({ {}, (uint32_t)setLayoutBindings.size(), setLayoutBindings.data() });
pipelineLayout = device.createPipelineLayout({ {}, 1, &descriptorSetLayout });
}
void setupDescriptorSets() {
// vk::Image descriptor for the cube map texture
vk::DescriptorImageInfo cubeMapDescriptor{ cubeMap.sampler, cubeMap.view, vk::ImageLayout::eGeneral };
vk::DescriptorSetAllocateInfo allocInfo{ descriptorPool, 1, &descriptorSetLayout };
// 3D object descriptor set
descriptorSets.object = device.allocateDescriptorSets(allocInfo)[0];
device.updateDescriptorSets(
{
{ descriptorSets.object, 0, 0, 1, vk::DescriptorType::eUniformBuffer, nullptr, &uniformData.objectVS.descriptor },
{ descriptorSets.object, 1, 0, 1, vk::DescriptorType::eCombinedImageSampler, &cubeMapDescriptor },
},
nullptr);
// Sky box descriptor set
descriptorSets.skybox = device.allocateDescriptorSets(allocInfo)[0];
device.updateDescriptorSets(
{
{ descriptorSets.skybox, 0, 0, 1, vk::DescriptorType::eUniformBuffer, nullptr, &uniformData.skyboxVS.descriptor },
{ descriptorSets.skybox, 1, 0, 1, vk::DescriptorType::eCombinedImageSampler, &cubeMapDescriptor },
},
nullptr);
}
void preparePipelines() {
vks::pipelines::GraphicsPipelineBuilder pipelineBuilder{ device, pipelineLayout, renderPass };
pipelineBuilder.vertexInputState.appendVertexLayout(vertexLayout);
pipelineBuilder.rasterizationState.cullMode = vk::CullModeFlagBits::eNone;
pipelineBuilder.loadShader(getAssetPath() + "shaders/texturecubemap/reflect.vert.spv", vk::ShaderStageFlagBits::eVertex);
pipelineBuilder.loadShader(getAssetPath() + "shaders/texturecubemap/reflect.frag.spv", vk::ShaderStageFlagBits::eFragment);
pipelines.reflect = pipelineBuilder.create(context.pipelineCache);
pipelineBuilder.destroyShaderModules();
pipelineBuilder.loadShader(getAssetPath() + "shaders/texturecubemap/skybox.vert.spv", vk::ShaderStageFlagBits::eVertex);
pipelineBuilder.loadShader(getAssetPath() + "shaders/texturecubemap/skybox.frag.spv", vk::ShaderStageFlagBits::eFragment);
pipelineBuilder.depthStencilState.depthWriteEnable = VK_FALSE;
pipelines.skybox = pipelineBuilder.create(context.pipelineCache);
}
// Prepare and initialize uniform buffer containing shader uniforms
void prepareUniformBuffers() {
// 3D objact
uniformData.objectVS = context.createUniformBuffer(uboVS);
// Skybox
uniformData.skyboxVS = context.createUniformBuffer(uboVS);
}
void updateUniformBuffers() {
// Common projection
uboVS.projection = camera.matrices.perspective;
// Skysphere, rotation only
uboVS.model = camera.matrices.skyboxView;
uniformData.skyboxVS.copy(uboVS);
// 3D object, translation combined with the rotation
uboVS.model = camera.matrices.view;
uniformData.objectVS.copy(uboVS);
}
void loadTextures() {
vk::Format format;
std::string filename;
const auto& deviceFeatures = context.deviceFeatures;
if (deviceFeatures.textureCompressionBC) {
filename = "cubemap_yokohama_bc3_unorm.ktx";
format = vk::Format::eBc3UnormBlock;
} else if (deviceFeatures.textureCompressionASTC_LDR) {
filename = "cubemap_yokohama_astc_8x8_unorm.ktx";
format = vk::Format::eAstc8x8UnormBlock;
} else if (deviceFeatures.textureCompressionETC2) {
filename = "cubemap_yokohama_etc2_unorm.ktx";
format = vk::Format::eEtc2R8G8B8UnormBlock;
} else {
throw std::runtime_error("Device does not support any compressed texture format!");
}
cubeMap.loadFromFile(context, getAssetPath() + "textures/" + filename, format);
}
void prepare() {
ExampleBase::prepare();
loadMeshes();
prepareUniformBuffers();
loadTextures();
setupDescriptorSetLayout();
preparePipelines();
setupDescriptorPool();
setupDescriptorSets();
buildCommandBuffers();
prepared = true;
}
virtual void render() {
if (!prepared)
return;
draw();
updateUniformBuffers();
}
virtual void viewChanged() { updateUniformBuffers(); }
};
RUN_EXAMPLE(VulkanExample)