forked from SaschaWillems/Vulkan
-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathsphericalenvmapping.cpp
192 lines (161 loc) · 7.09 KB
/
sphericalenvmapping.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
/*
* Vulkan Example - Spherical Environment Mapping, using different mat caps
*
* Use +/-/space toggle through different material captures
*
* Based on https://www.clicktorelease.com/blog/creating-spherical-environment-mapping-shader
*
* 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>
class VulkanExample : public vkx::ExampleBase {
public:
vks::model::VertexLayout vertexLayout{ {
vks::model::Component::VERTEX_COMPONENT_POSITION,
vks::model::Component::VERTEX_COMPONENT_NORMAL,
vks::model::Component::VERTEX_COMPONENT_UV,
vks::model::Component::VERTEX_COMPONENT_COLOR,
} };
struct {
vks::model::Model object;
} meshes;
struct {
vks::texture::Texture2DArray matCapArray;
} textures;
struct {
vks::Buffer vertexShader;
} uniformData;
struct UboVS {
glm::mat4 projection;
glm::mat4 model;
glm::mat4 normal;
glm::mat4 view;
uint32_t texIndex{ 0 };
} uboVS;
struct {
vk::Pipeline sem;
} pipelines;
vk::PipelineLayout pipelineLayout;
vk::DescriptorSet descriptorSet;
vk::DescriptorSetLayout descriptorSetLayout;
VulkanExample() {
rotationSpeed = 0.75f;
zoomSpeed = 0.25f;
camera.dolly(-0.9f);
camera.setRotation({ -25.0f, 23.75f, 0.0f });
title = "Vulkan Example - Spherical Environment Mapping";
}
~VulkanExample() {
// Clean up used Vulkan resources
// Note : Inherited destructor cleans up resources stored in base class
device.destroyPipeline(pipelines.sem);
device.destroyPipelineLayout(pipelineLayout);
device.destroyDescriptorSetLayout(descriptorSetLayout);
meshes.object.destroy();
uniformData.vertexShader.destroy();
textures.matCapArray.destroy();
}
void loadAssets() override {
// Several mat caps are stored in a single texture array
// so they can easily be switched inside the shader
// just by updating the index in a uniform buffer
textures.matCapArray.loadFromFile(context, getAssetPath() + "textures/matcap_array_rgba.ktx", vk::Format::eR8G8B8A8Unorm);
meshes.object.loadFromFile(context, getAssetPath() + "models/chinesedragon.dae", vertexLayout, 0.05f);
}
void updateDrawCommandBuffer(const vk::CommandBuffer& cmdBuffer) override {
cmdBuffer.setViewport(0, vks::util::viewport(size));
cmdBuffer.setScissor(0, vks::util::rect2D(size));
cmdBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipelineLayout, 0, descriptorSet, nullptr);
cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipelines.sem);
cmdBuffer.bindVertexBuffers(0, meshes.object.vertices.buffer, { 0 });
cmdBuffer.bindIndexBuffer(meshes.object.indices.buffer, 0, vk::IndexType::eUint32);
cmdBuffer.drawIndexed(meshes.object.indexCount, 1, 0, 0, 0);
}
void setupDescriptorPool() {
// Example uses one ubo and one image sampler
std::vector<vk::DescriptorPoolSize> poolSizes = { vk::DescriptorPoolSize(vk::DescriptorType::eUniformBuffer, 1),
vk::DescriptorPoolSize(vk::DescriptorType::eCombinedImageSampler, 1) };
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 color map 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 setupDescriptorSet() {
vk::DescriptorSetAllocateInfo allocInfo{ descriptorPool, 1, &descriptorSetLayout };
descriptorSet = device.allocateDescriptorSets(allocInfo)[0];
// Color map image descriptor
vk::DescriptorImageInfo texDescriptorColorMap{ textures.matCapArray.sampler, textures.matCapArray.view, vk::ImageLayout::eGeneral };
std::vector<vk::WriteDescriptorSet> writeDescriptorSets{
// Binding 0 : Vertex shader uniform buffer
{ descriptorSet, 0, 0, 1, vk::DescriptorType::eUniformBuffer, nullptr, &uniformData.vertexShader.descriptor },
// Binding 1 : Fragment shader image sampler
{ descriptorSet, 1, 0, 1, vk::DescriptorType::eCombinedImageSampler, &texDescriptorColorMap },
};
device.updateDescriptorSets(writeDescriptorSets, nullptr);
}
void preparePipelines() {
// Spherical environment rendering pipeline
vks::pipelines::GraphicsPipelineBuilder pipelineBuilder{ device, pipelineLayout, renderPass };
pipelineBuilder.vertexInputState.appendVertexLayout(vertexLayout);
pipelineBuilder.rasterizationState.frontFace = vk::FrontFace::eClockwise;
// Load shaders
pipelineBuilder.loadShader(getAssetPath() + "shaders/sphericalenvmapping/sem.vert.spv", vk::ShaderStageFlagBits::eVertex);
pipelineBuilder.loadShader(getAssetPath() + "shaders/sphericalenvmapping/sem.frag.spv", vk::ShaderStageFlagBits::eFragment);
pipelines.sem = pipelineBuilder.create(context.pipelineCache);
}
void prepareUniformBuffers() {
// Vertex shader uniform buffer block
uniformData.vertexShader = context.createUniformBuffer(uboVS);
updateUniformBuffers();
}
void updateUniformBuffers() {
uboVS.projection = camera.matrices.perspective;
uboVS.view = camera.matrices.view;
uboVS.normal = glm::inverseTranspose(uboVS.view * uboVS.model);
uniformData.vertexShader.copy(uboVS);
}
void prepare() override {
ExampleBase::prepare();
prepareUniformBuffers();
setupDescriptorSetLayout();
preparePipelines();
setupDescriptorPool();
setupDescriptorSet();
buildCommandBuffers();
prepared = true;
}
void render() override {
if (!prepared)
return;
draw();
}
void viewChanged() override { updateUniformBuffers(); }
void keyPressed(uint32_t keyCode) override {
switch (keyCode) {
case KEY_KPADD:
case KEY_SPACE:
case GAMEPAD_BUTTON_A:
changeMatCapIndex(1);
break;
case KEY_KPSUB:
case GAMEPAD_BUTTON_X:
changeMatCapIndex(-1);
break;
}
}
void changeMatCapIndex(uint32_t delta) {
uboVS.texIndex += delta;
uboVS.texIndex %= textures.matCapArray.layerCount;
updateUniformBuffers();
}
};
RUN_EXAMPLE(VulkanExample)