forked from SaschaWillems/Vulkan
-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathparallaxmapping.cpp
258 lines (211 loc) · 10.3 KB
/
parallaxmapping.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
/*
* Vulkan Example - Parallax Mapping
*
* 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_UV,
vks::model::Component::VERTEX_COMPONENT_NORMAL,
vks::model::Component::VERTEX_COMPONENT_TANGENT,
vks::model::Component::VERTEX_COMPONENT_BITANGENT,
} };
class VulkanExample : public vkx::ExampleBase {
public:
bool splitScreen = true;
struct {
vks::texture::Texture2D colorMap;
// Normals and height are combined in one texture (height = alpha channel)
vks::texture::Texture2D normalHeightMap;
} textures;
struct {
vks::model::Model quad;
} meshes;
struct {
vks::Buffer vertexShader;
vks::Buffer fragmentShader;
} uniformData;
struct {
struct {
glm::mat4 projection;
glm::mat4 model;
glm::mat4 normal;
glm::vec4 lightPos;
glm::vec4 cameraPos;
} vertexShader;
struct FragmentShader {
// Scale and bias control the parallax offset effect
// They need to be tweaked for each material
// Getting them wrong destroys the depth effect
float scale = 0.06f;
float bias = -0.04f;
float lightRadius = 1.0f;
int32_t usePom = 1;
int32_t displayNormalMap = 0;
} fragmentShader;
} ubos;
struct {
vk::Pipeline parallaxMapping;
vk::Pipeline normalMapping;
} pipelines;
vk::PipelineLayout pipelineLayout;
vk::DescriptorSet descriptorSet;
vk::DescriptorSetLayout descriptorSetLayout;
VulkanExample() {
camera.setRotation({ 0.0, 15.0, 0.0 });
camera.setRotation({ 0.1, 0.1, -2.5 });
camera.dolly(-2.25f);
rotationSpeed = 0.25f;
paused = true;
title = "Vulkan Example - Parallax Mapping";
}
~VulkanExample() {
// Clean up used Vulkan resources
// Note : Inherited destructor cleans up resources stored in base class
device.destroyPipeline(pipelines.parallaxMapping);
device.destroyPipeline(pipelines.normalMapping);
device.destroyPipelineLayout(pipelineLayout);
device.destroyDescriptorSetLayout(descriptorSetLayout);
meshes.quad.destroy();
uniformData.vertexShader.destroy();
uniformData.fragmentShader.destroy();
textures.colorMap.destroy();
textures.normalHeightMap.destroy();
}
void loadTextures() {
textures.colorMap.loadFromFile(context, getAssetPath() + "textures/rocks_color_bc3.dds", vk::Format::eBc3UnormBlock);
textures.normalHeightMap.loadFromFile(context, getAssetPath() + "textures/rocks_normal_height_rgba.dds", vk::Format::eR8G8B8A8Unorm);
}
void updateDrawCommandBuffer(const vk::CommandBuffer& cmdBuffer) override {
vk::Viewport viewport = vks::util::viewport((splitScreen) ? (float)size.width / 2.0f : (float)size.width, (float)size.height, 0.0f, 1.0f);
cmdBuffer.setViewport(0, viewport);
cmdBuffer.setScissor(0, vks::util::rect2D(size));
cmdBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipelineLayout, 0, descriptorSet, nullptr);
vk::DeviceSize offsets = 0;
cmdBuffer.bindVertexBuffers(0, meshes.quad.vertices.buffer, offsets);
cmdBuffer.bindIndexBuffer(meshes.quad.indices.buffer, 0, vk::IndexType::eUint32);
// Parallax enabled
cmdBuffer.setViewport(0, viewport);
cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipelines.parallaxMapping);
cmdBuffer.drawIndexed(meshes.quad.indexCount, 1, 0, 0, 1);
// Normal mapping
if (splitScreen) {
viewport.x = viewport.width;
cmdBuffer.setViewport(0, viewport);
cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipelines.normalMapping);
cmdBuffer.drawIndexed(meshes.quad.indexCount, 1, 0, 0, 1);
}
}
void loadMeshes() { meshes.quad.loadFromFile(context, getAssetPath() + "models/plane_z.obj", vertexLayout, 0.1f); }
void setupDescriptorPool() {
// Example uses two ubos and two image sampler
std::vector<vk::DescriptorPoolSize> poolSizes{ { vk::DescriptorType::eUniformBuffer, 2 }, { vk::DescriptorType::eCombinedImageSampler, 2 } };
descriptorPool = device.createDescriptorPool({ {}, 4, (uint32_t)poolSizes.size(), poolSizes.data() });
}
void setupDescriptorSetLayout() {
std::vector<vk::DescriptorSetLayoutBinding> setLayoutBindings{
// Binding 0 : Vertex shader uniform buffer
vk::DescriptorSetLayoutBinding{ 0, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex },
// Binding 1 : Fragment shader color map image sampler
vk::DescriptorSetLayoutBinding{ 1, vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment },
// Binding 2 : Fragment combined normal and heightmap
vk::DescriptorSetLayoutBinding{ 2, vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment },
// Binding 3 : Fragment shader uniform buffer
vk::DescriptorSetLayoutBinding{ 3, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eFragment },
};
descriptorSetLayout = device.createDescriptorSetLayout({ {}, (uint32_t)setLayoutBindings.size(), setLayoutBindings.data() });
pipelineLayout = device.createPipelineLayout({ {}, 1, &descriptorSetLayout });
}
void setupDescriptorSet() {
descriptorSet = device.allocateDescriptorSets({ descriptorPool, 1, &descriptorSetLayout })[0];
// Color map image descriptor
vk::DescriptorImageInfo texDescriptorColorMap{ textures.colorMap.sampler, textures.colorMap.view, vk::ImageLayout::eGeneral };
vk::DescriptorImageInfo texDescriptorNormalHeightMap{ textures.normalHeightMap.sampler, textures.normalHeightMap.view, vk::ImageLayout::eGeneral };
std::vector<vk::WriteDescriptorSet> writeDescriptorSets{
// Binding 0 : Vertex shader uniform buffer
vk::WriteDescriptorSet{ descriptorSet, 0, 0, 1, vk::DescriptorType::eUniformBuffer, nullptr, &uniformData.vertexShader.descriptor },
// Binding 1 : Fragment shader image sampler
vk::WriteDescriptorSet{ descriptorSet, 1, 0, 1, vk::DescriptorType::eCombinedImageSampler, &texDescriptorColorMap },
// Binding 2 : Combined normal and heightmap
vk::WriteDescriptorSet{ descriptorSet, 2, 0, 1, vk::DescriptorType::eCombinedImageSampler, &texDescriptorNormalHeightMap },
// Binding 3 : Fragment shader uniform buffer
vk::WriteDescriptorSet{ descriptorSet, 3, 0, 1, vk::DescriptorType::eUniformBuffer, nullptr, &uniformData.fragmentShader.descriptor },
};
device.updateDescriptorSets(writeDescriptorSets, nullptr);
}
void preparePipelines() {
// Parallax mapping pipeline
vks::pipelines::GraphicsPipelineBuilder pipelineCreator{ device, pipelineLayout, renderPass };
pipelineCreator.rasterizationState.frontFace = vk::FrontFace::eClockwise;
pipelineCreator.vertexInputState.appendVertexLayout(vertexLayout);
pipelineCreator.loadShader(getAssetPath() + "shaders/parallaxmapping/parallax.vert.spv", vk::ShaderStageFlagBits::eVertex);
pipelineCreator.loadShader(getAssetPath() + "shaders/parallaxmapping/parallax.frag.spv", vk::ShaderStageFlagBits::eFragment);
pipelines.parallaxMapping = pipelineCreator.create(context.pipelineCache);
pipelineCreator.destroyShaderModules();
// Normal mapping (no parallax effect)
pipelineCreator.loadShader(getAssetPath() + "shaders/parallaxmapping/normalmap.vert.spv", vk::ShaderStageFlagBits::eVertex);
pipelineCreator.loadShader(getAssetPath() + "shaders/parallaxmapping/normalmap.frag.spv", vk::ShaderStageFlagBits::eFragment);
pipelines.normalMapping = pipelineCreator.create(context.pipelineCache);
}
void prepareUniformBuffers() {
// Vertex shader ubo
uniformData.vertexShader = context.createUniformBuffer(ubos.vertexShader);
// Fragment shader ubo
uniformData.fragmentShader = context.createUniformBuffer(ubos.fragmentShader);
updateUniformBuffers();
}
void updateUniformBuffers() {
// Vertex shader
ubos.vertexShader.projection =
glm::perspective(glm::radians(45.0f), (float)(size.width * ((splitScreen) ? 0.5f : 1.0f)) / (float)size.height, 0.001f, 256.0f);
ubos.vertexShader.model = camera.matrices.view;
ubos.vertexShader.normal = glm::inverseTranspose(ubos.vertexShader.model);
if (!paused) {
ubos.vertexShader.lightPos.x = sinf(glm::radians(timer * 360.0f)) * 0.5f;
ubos.vertexShader.lightPos.y = cosf(glm::radians(timer * 360.0f)) * 0.5f;
}
ubos.vertexShader.cameraPos = glm::vec4(0.0, 0.0, camera.position.z, 0.0);
uniformData.vertexShader.copy(ubos.vertexShader);
// Fragment shader
uniformData.fragmentShader.copy(ubos.fragmentShader);
}
void prepare() override {
ExampleBase::prepare();
loadTextures();
loadMeshes();
prepareUniformBuffers();
setupDescriptorSetLayout();
preparePipelines();
setupDescriptorPool();
setupDescriptorSet();
buildCommandBuffers();
prepared = true;
}
void render() override {
if (!prepared)
return;
draw();
if (!paused) {
updateUniformBuffers();
}
}
void viewChanged() override { updateUniformBuffers(); }
void toggleParallaxOffset() {
ubos.fragmentShader.usePom = !ubos.fragmentShader.usePom;
updateUniformBuffers();
}
void toggleNormalMapDisplay() {
ubos.fragmentShader.displayNormalMap = !ubos.fragmentShader.displayNormalMap;
updateUniformBuffers();
}
void toggleSplitScreen() {
splitScreen = !splitScreen;
updateUniformBuffers();
buildCommandBuffers();
}
};
RUN_EXAMPLE(VulkanExample)