-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path07-texture-array.cpp
More file actions
266 lines (247 loc) · 9.66 KB
/
Copy path07-texture-array.cpp
File metadata and controls
266 lines (247 loc) · 9.66 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
#include <fstream>
#include "../framework/vulkanApp.h"
#include "../framework/utilities.h"
#include "quadric/include/cube.h"
// Use PgUp/PgDown to select texture lod
class TextureArrayApp : public VulkanApp
{
struct alignas(16) TexParameters
{
float lod;
};
struct DescriptorSetTable
{
magma::descriptor::UniformBuffer worldViewProj = 0;
magma::descriptor::UniformBuffer texParameters = 1;
magma::descriptor::CombinedImageSampler imageArray = 2;
} setTable;
std::unique_ptr<quadric::Cube> mesh;
std::unique_ptr<magma::ImageView> imageArrayView;
std::unique_ptr<magma::Sampler> anisotropicSampler;
std::unique_ptr<magma::UniformBuffer<rapid::matrix>> uniformWorldViewProj;
std::unique_ptr<magma::UniformBuffer<TexParameters>> uniformTexParameters;
std::unique_ptr<magma::DescriptorSet> descriptorSet;
std::unique_ptr<magma::GraphicsPipeline> graphicsPipeline;
rapid::matrix viewProj;
float lod = 0.f;
public:
TextureArrayApp(const AppEntry& entry):
VulkanApp(entry, TEXT("07 - Texture array"), 512, 512, true)
{
initialize();
setupView();
createMesh();
loadTextureArray({"dice1.dds" , "dice2.dds", "dice3.dds", "dice4.dds", "dice5.dds", "dice6.dds"});
createSampler();
createUniformBuffers();
setupDescriptorSet();
setupPipeline();
for (uint32_t i = 0; i < (uint32_t)commandBuffers.size(); ++i)
recordCommandBuffer(i);
timer->run();
}
void render(uint32_t bufferIndex) override
{
updatePerspectiveTransform();
submitCommandBuffer(bufferIndex);
}
void onKeyDown(char key, int repeat, uint32_t flags) override
{
switch (key)
{
case AppKey::PgUp:
if (lod < imageArrayView->getImage()->getMipLevels() - 1)
{
lod += 1.f;
updateLod();
}
break;
case AppKey::PgDn:
if (lod > 0.f)
{
lod -= 1.f;
updateLod();
}
break;
}
VulkanApp::onKeyDown(key, repeat, flags);
}
void onResize(uint32_t width, uint32_t height) override
{
VulkanApp::onResize(width, height);
setupView();
for (uint32_t i = 0; i < (uint32_t)commandBuffers.size(); ++i)
recordCommandBuffer(i);
}
void setupView()
{
const rapid::vector3 eye(0.f, 0.f, 7.f);
const rapid::vector3 center(0.f, 0.f, 0.f);
const rapid::vector3 up(0.f, 1.f, 0.f);
constexpr float fov = rapid::radians(60.f);
const float aspect = width/(float)height;
constexpr float zn = 1.f, zf = 100.f;
const rapid::matrix view = rapid::lookAtRH(eye, center, up);
const rapid::matrix proj = rapid::perspectiveFovRH(fov, aspect, zn, zf);
viewProj = view * proj;
}
void updatePerspectiveTransform()
{
constexpr float speed = 0.1f;
static float angle = 0.f;
angle += timer->millisecondsElapsed() * speed;
const float radians = rapid::radians(angle);
const rapid::matrix pitch = rapid::rotationX(radians);
const rapid::matrix yaw = rapid::rotationY(radians);
const rapid::matrix roll = rapid::rotationZ(radians);
const rapid::matrix world = pitch * yaw * roll;
magma::map(uniformWorldViewProj,
[this, &world](auto *worldViewProj)
{
*worldViewProj = world * viewProj;
});
}
void updateLod()
{
magma::map<TexParameters>(uniformTexParameters,
[this](auto *block)
{
block->lod = lod;
});
std::cout << "Texture LOD: " << lod << "\n";
}
void createMesh()
{
mesh = std::make_unique<quadric::Cube>(cmdBufferCopy);
}
void loadTextureArray(const std::initializer_list<std::string>& filenames)
{
std::list<std::ifstream> files;
std::streamoff totalSize = 0;
for (const std::string& filename: filenames)
{ // Open binary stream
files.emplace_back("textures/" + filename, std::ios::in | std::ios::binary | std::ios::ate);
if (!files.back().is_open())
throw std::runtime_error("failed to open file \"" + filename + "\"");
totalSize += files.back().tellg();
}
std::list<gliml::context> ctxArray;
auto buffer = std::make_unique<magma::SrcTransferBuffer>(device, totalSize);
VkDeviceSize baseMipOffset = 0ull;
magma::map<uint8_t>(buffer, [&](uint8_t *data)
{ // Read all data to single buffer
for (auto& file: files)
{
const std::streamoff size = file.tellg();
file.seekg(0, std::ios::beg);
file.read(reinterpret_cast<char *>(data), size);
file.close();
ctxArray.emplace_back();
gliml::context& ctx = ctxArray.back();
ctx.enable_dxt(true);
if (!ctx.load(data, static_cast<unsigned>(size)))
throw std::runtime_error("failed to load DDS texture");
// Skip DDS header of the first file
if (0ull == baseMipOffset)
baseMipOffset = reinterpret_cast<const uint8_t *>(ctx.image_data(0, 0)) - data;
data += size;
}
});
const VkFormat format = utilities::getBlockCompressedFormat(ctxArray.front());
const VkExtent2D extent = {
(uint32_t)ctxArray.front().image_width(0, 0),
(uint32_t)ctxArray.front().image_height(0, 0)
};
// Assert that all files have the same format and dimensions
for (const auto& ctx: ctxArray)
{
MAGMA_ASSERT(utilities::getBlockCompressedFormat(ctx) == format);
MAGMA_ASSERT(static_cast<uint32_t>(ctx.image_width(0, 0)) == extent.width);
MAGMA_ASSERT(static_cast<uint32_t>(ctx.image_height(0, 0)) == extent.height);
}
// Setup texture array data description
const uint8_t *frontImageFirstMipData = (const uint8_t *)ctxArray.front().image_data(0, 0);
magma::Mipmap mipMap;
for (const auto& ctx: ctxArray)
{
for (int level = 0; level < ctx.num_mipmaps(0); ++level)
{
const ptrdiff_t offset = (const uint8_t *)ctx.image_data(0, level) - frontImageFirstMipData;
MAGMA_ASSERT(offset < (VkDeviceSize)totalSize);
mipMap.emplace_back(
ctx.image_width(0, level),
ctx.image_height(0, level),
(VkDeviceSize)offset);
}
}
// Upload texture array data from buffer
cmdImageCopy->begin(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT);
const magma::Image::CopyLayout bufferLayout{baseMipOffset, 0, 0};
auto imageArray = std::make_unique<magma::Image2DArray>(cmdImageCopy,
format, magma::core::countof(ctxArray), buffer, mipMap, bufferLayout);
cmdImageCopy->end();
submitCopyImageCommands();
// Create image view for fragment shader
imageArrayView = std::make_unique<magma::UniqueImageView>(std::move(imageArray));
}
void createSampler()
{
anisotropicSampler = std::make_unique<magma::Sampler>(device, magma::sampler::magMinLinearMipAnisotropicClampToEdge8x);
}
void createUniformBuffers()
{
uniformWorldViewProj = std::make_unique<magma::UniformBuffer<rapid::matrix>>(device);
uniformTexParameters = std::make_unique<magma::UniformBuffer<TexParameters>>(device);
updateLod();
}
void setupDescriptorSet()
{
setTable.worldViewProj = uniformWorldViewProj;
setTable.texParameters = uniformTexParameters;
setTable.imageArray = {imageArrayView, anisotropicSampler};
descriptorSet = std::make_unique<magma::DescriptorSet>(descriptorPool,
setTable, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
nullptr, 0, shaderReflectionFactory, "textureArray");
}
void setupPipeline()
{
auto layout = std::make_unique<magma::PipelineLayout>(descriptorSet->getLayout());
graphicsPipeline = std::make_unique<GraphicsPipeline>(device,
"transform", "textureArray",
mesh->getVertexInput(),
magma::renderstate::triangleList,
negateViewport ? magma::renderstate::fillCullBackCcw
: magma::renderstate::fillCullBackCw,
magma::renderstate::dontMultisample,
magma::renderstate::depthLess,
magma::renderstate::dontBlendRgb,
std::move(layout),
renderPass, 0,
pipelineCache);
}
void recordCommandBuffer(uint32_t index)
{
auto& cmdBuffer = commandBuffers[index];
cmdBuffer->begin();
{
cmdBuffer->beginRenderPass(renderPass, framebuffers[index],
{
magma::clear::gray,
magma::clear::depthOne
});
{
cmdBuffer->setViewport(0, 0, width, negateViewport ? -int32_t(height) : height);
cmdBuffer->setScissor(0, 0, width, height);
cmdBuffer->bindDescriptorSet(graphicsPipeline, 0, descriptorSet);
cmdBuffer->bindPipeline(graphicsPipeline);
mesh->draw(cmdBuffer);
}
cmdBuffer->endRenderPass();
}
cmdBuffer->end();
}
};
std::unique_ptr<IApplication> appFactory(const AppEntry& entry)
{
return std::unique_ptr<TextureArrayApp>(new TextureArrayApp(entry));
}