-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
375 lines (314 loc) · 12.5 KB
/
utils.cpp
File metadata and controls
375 lines (314 loc) · 12.5 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include "utils.h"
entt::entity Utils::createBoundingBox(Issam::Scene* scene, const vec3& point1, const vec3& point2) {
std::vector<Vertex> vertices(8);
//std::vector<uint16_t> indices = {
// 0, 1, 2, 2, 3, 0, // Front face
// 4, 5, 6, 6, 7, 4, // Back face
// 0, 1, 5, 5, 4, 0, // Bottom face
// 2, 3, 7, 7, 6, 2, // Top face
// 0, 3, 7, 7, 4, 0, // Left face
// 1, 2, 6, 6, 5, 1 // Right face
//};
std::vector<uint16_t> indices = {
0, 1, 1, 2, 2, 3, 3, 0, // Bottom face
4, 5, 5, 6, 6, 7, 7, 4, // Top face
0, 4, 1, 5, 2, 6, 3, 7 // Vertical edges
};
vec3 minPoint = glm::min(point1, point2);
vec3 maxPoint = glm::max(point1, point2);
vertices[0].position = vec3(minPoint.x, minPoint.y, minPoint.z);
vertices[1].position = vec3(maxPoint.x, minPoint.y, minPoint.z);
vertices[2].position = vec3(maxPoint.x, maxPoint.y, minPoint.z);
vertices[3].position = vec3(minPoint.x, maxPoint.y, minPoint.z);
vertices[4].position = vec3(minPoint.x, minPoint.y, maxPoint.z);
vertices[5].position = vec3(maxPoint.x, minPoint.y, maxPoint.z);
vertices[6].position = vec3(maxPoint.x, maxPoint.y, maxPoint.z);
vertices[7].position = vec3(minPoint.x, maxPoint.y, maxPoint.z);
MeshPtr cubeMesh = std::make_shared<Mesh>();
cubeMesh->setVertices(vertices);
cubeMesh->setIndices(indices);
entt::entity cubeEntity = scene->addEntity();
Issam::MeshRenderer meshRenderer;
meshRenderer.mesh = cubeMesh;
Material* material = new Material();
material->setAttribute("unlitMaterialModel", "colorFactor", glm::vec4(1.0, 0.0, 1.0, 1.0));
meshRenderer.material = material;
scene->addComponent<Issam::MeshRenderer>(cubeEntity, meshRenderer);
Issam::Filters filters;
filters.add("debug");
scene->addComponent<Issam::Filters>(cubeEntity, filters);
return cubeEntity;
}
entt::entity Utils::addLine(Issam::Scene* scene, glm::vec3 start, glm::vec3 end, glm::vec4 color) {
std::vector<Vertex> vertices;
vertices.push_back({ start });
vertices.push_back({ end });
std::vector<uint16_t> indices;
indices.push_back(0);
indices.push_back(1);
MeshPtr lineMesh = std::make_shared<Mesh>();
lineMesh->setVertices(vertices);
lineMesh->setIndices(indices);
Issam::MeshRenderer meshRenderer;
meshRenderer.mesh = lineMesh;
Material* material = new Material();
material->setAttribute("unlitMaterialModel", "colorFactor", color);
meshRenderer.material = material;
entt::entity lineEntity = scene->addEntity();
scene->addComponent<Issam::MeshRenderer>(lineEntity, meshRenderer);
Issam::Filters filters;
filters.add("debug");
scene->addComponent<Issam::Filters>(lineEntity, filters);
return lineEntity;
}
entt::entity Utils::addAxes(Issam::Scene* scene)
{
glm::vec4 red(1.0f, 0.0f, 0.0f, 1.0f);
glm::vec4 green(0.0f, 1.0f, 0.0f, 1.0f);
glm::vec4 blue(0.0f, 0.0f, 1.0f, 1.0f);
// Add X axis (red)
entt::entity x_axis = addLine(scene, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 0.0f, 0.0f), red);
// Add Y axis (green)
entt::entity y_axis = addLine(scene, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f), green);
// Add Z axis (blue)
entt::entity z_axis = addLine(scene, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f), blue);
entt::entity axes = scene->addEntity();
scene->addChild(axes, x_axis);
scene->addChild(axes, y_axis);
scene->addChild(axes, z_axis);
return axes;
}
wgpu::Texture Utils::CreateWhiteTexture(TextureView* pTextureView) {
wgpu::Extent3D textureSize = { 1, 1, 1 };
// Define the texture descriptor
wgpu::TextureDescriptor textureDesc = {};
textureDesc.size = textureSize;
textureDesc.format = wgpu::TextureFormat::RGBA8Unorm;
textureDesc.usage = wgpu::TextureUsage::CopyDst | wgpu::TextureUsage::TextureBinding;
textureDesc.dimension = wgpu::TextureDimension::e2D;
textureDesc.mipLevelCount = 1;
textureDesc.sampleCount = 1;
// Create the texture
Texture texture = Context::getInstance().getDevice().CreateTexture(&textureDesc);
// Define the white pixel data
uint8_t whitePixel[4] = { 255, 255, 255, 255 };
// Define the texture data layout
wgpu::TextureDataLayout textureDataLayout = {};
textureDataLayout.offset = 0;
textureDataLayout.bytesPerRow = 4;
textureDataLayout.rowsPerImage = 1;
// Define the texture copy view
wgpu::ImageCopyTexture imageCopyTexture = {};
imageCopyTexture.texture = texture;
imageCopyTexture.origin = { 0, 0, 0 };
imageCopyTexture.mipLevel = 0;
// Write the white pixel data to the texture
Context::getInstance().getDevice().GetQueue().WriteTexture(&imageCopyTexture, whitePixel, sizeof(whitePixel), &textureDataLayout, &textureSize);
if (pTextureView) {
TextureViewDescriptor textureViewDesc;
textureViewDesc.aspect = TextureAspect::All;
textureViewDesc.baseArrayLayer = 0;
textureViewDesc.arrayLayerCount = 1;
textureViewDesc.baseMipLevel = 0;
textureViewDesc.mipLevelCount = textureDesc.mipLevelCount;
textureViewDesc.dimension = TextureViewDimension::e2D;
textureViewDesc.format = textureDesc.format;
*pTextureView = texture.CreateView(&textureViewDesc);
}
return texture;
}
std::string Utils::loadFile(const std::filesystem::path& path) {
std::ifstream file(path);
if (!file.is_open()) {
std::cerr << path.c_str() << " not found ! " << std::endl;
}
file.seekg(0, std::ios::end);
size_t size = file.tellg();
std::string str = std::string(size, ' ');
file.seekg(0);
file.read(str.data(), size);
return str;
};
Sampler Utils::createDefaultSampler()
{
SamplerDescriptor samplerDesc;
samplerDesc.addressModeU = AddressMode::Repeat;
samplerDesc.addressModeV = AddressMode::Repeat;
samplerDesc.addressModeW = AddressMode::Repeat;
samplerDesc.magFilter = FilterMode::Linear;
samplerDesc.minFilter = FilterMode::Linear;
samplerDesc.mipmapFilter = MipmapFilterMode::Linear;
samplerDesc.lodMinClamp = 0.0f;
samplerDesc.lodMaxClamp = 8.0f;
samplerDesc.compare = CompareFunction::Undefined;
samplerDesc.maxAnisotropy = 1;
return Context::getInstance().getDevice().CreateSampler(&samplerDesc);
}
std::string Utils::GetBaseDir(const std::string& filepath) {
if (filepath.find_last_of("/\\") != std::string::npos)
return filepath.substr(0, filepath.find_last_of("/\\") + 1);
return "";
}
std::string Utils::getFileExtension(const std::string& filePath) {
size_t dotPosition = filePath.find_last_of('.');
if (dotPosition == std::string::npos) {
// No extension found
return "";
}
size_t slashPosition = filePath.find_last_of("/\\");
if (slashPosition != std::string::npos && slashPosition > dotPosition) {
// Dot found, but it's part of the directory name
return "";
}
return filePath.substr(dotPosition + 1);
}
// Auxiliary function for loadTexture
template<typename T>
static void writeMipMaps(
Texture texture,
Extent3D textureSize,
uint32_t mipLevelCount,
const T* pixelData
) {
Queue queue = Context::getInstance().getDevice().GetQueue();
// Arguments telling which part of the texture we upload to
ImageCopyTexture destination;
destination.texture = texture;
destination.origin = { 0, 0, 0 };
destination.aspect = TextureAspect::All;
// Arguments telling how the C++ side pixel memory is laid out
TextureDataLayout source;
source.offset = 0;
// Create image data
Extent3D mipLevelSize = textureSize;
std::vector<T> previousLevelPixels;
Extent3D previousMipLevelSize;
for (uint32_t level = 0; level < mipLevelCount; ++level) {
std::vector<T> pixels(4 * mipLevelSize.width * mipLevelSize.height);
if (level == 0) {
// We cannot really avoid this copy since we need this
// in previousLevelPixels at the next iteration
memcpy(pixels.data(), pixelData, pixels.size() * sizeof(T));
}
else {
// Create mip level data
for (uint32_t i = 0; i < mipLevelSize.width; ++i) {
for (uint32_t j = 0; j < mipLevelSize.height; ++j) {
T* p = &pixels[4 * (j * mipLevelSize.width + i)];
// Get the corresponding 4 pixels from the previous level
T* p00 = &previousLevelPixels[4 * ((2 * j + 0) * previousMipLevelSize.width + (2 * i + 0))];
T* p01 = &previousLevelPixels[4 * ((2 * j + 0) * previousMipLevelSize.width + (2 * i + 1))];
T* p10 = &previousLevelPixels[4 * ((2 * j + 1) * previousMipLevelSize.width + (2 * i + 0))];
T* p11 = &previousLevelPixels[4 * ((2 * j + 1) * previousMipLevelSize.width + (2 * i + 1))];
// Average
p[0] = (p00[0] + p01[0] + p10[0] + p11[0]) / (T)4;
p[1] = (p00[1] + p01[1] + p10[1] + p11[1]) / (T)4;
p[2] = (p00[2] + p01[2] + p10[2] + p11[2]) / (T)4;
p[3] = (p00[3] + p01[3] + p10[3] + p11[3]) / (T)4;
}
}
}
// Upload data to the GPU texture
destination.mipLevel = level;
source.bytesPerRow = 4 * mipLevelSize.width * sizeof(T);
source.rowsPerImage = mipLevelSize.height;
queue.WriteTexture(&destination, pixels.data(), pixels.size() * sizeof(T), &source, &mipLevelSize);
previousLevelPixels = std::move(pixels);
previousMipLevelSize = mipLevelSize;
mipLevelSize.width /= 2;
mipLevelSize.height /= 2;
}
}
static uint32_t bit_width(uint32_t m) {
if (m == 0) return 0;
else { uint32_t w = 0; while (m >>= 1) ++w; return w; }
}
Texture Utils::loadTexture(void* pixelData, int& width, int& height, int& channels, TextureFormat format, TextureView* pTextureView) {
assert(pixelData);
// Use the width, height, channels and data variables here
TextureDescriptor textureDesc;
textureDesc.dimension = TextureDimension::e2D;
textureDesc.format = format; // by convention for bmp, png and jpg file. Be careful with other formats.
textureDesc.size = { (unsigned int)width, (unsigned int)height, 1 };
textureDesc.mipLevelCount = bit_width(std::max(textureDesc.size.width, textureDesc.size.height));
textureDesc.sampleCount = 1;
textureDesc.usage = TextureUsage::TextureBinding | TextureUsage::CopyDst;
textureDesc.viewFormatCount = 0;
textureDesc.viewFormats = nullptr;
Texture texture = Context::getInstance().getDevice().CreateTexture(&textureDesc);
// Upload data to the GPU texture
if(format != TextureFormat::RGBA32Float)
writeMipMaps<unsigned char>(texture, textureDesc.size, textureDesc.mipLevelCount, (unsigned char*)pixelData);
else
writeMipMaps<float>(texture, textureDesc.size, textureDesc.mipLevelCount, (float*)pixelData);
stbi_image_free(pixelData);
// (Do not use data after this)
if (pTextureView) {
TextureViewDescriptor textureViewDesc;
textureViewDesc.aspect = TextureAspect::All;
textureViewDesc.baseArrayLayer = 0;
textureViewDesc.arrayLayerCount = 1;
textureViewDesc.baseMipLevel = 0;
textureViewDesc.mipLevelCount = textureDesc.mipLevelCount;
textureViewDesc.dimension = TextureViewDimension::e2D;
textureViewDesc.format = textureDesc.format;
*pTextureView = texture.CreateView(&textureViewDesc);
}
return texture;
}
Texture Utils::loadImageFromPath(const std::string& path, TextureView* pTextureView, bool hdr) {
int width, height, channels;
void* data = nullptr;
TextureFormat format = TextureFormat::Undefined;
if (!hdr)
{
data = stbi_load(path.c_str(), &width, &height, &channels, 4);
format = TextureFormat::RGBA8Unorm;
}
else
{
data = stbi_loadf(path.c_str(), &width, &height, &channels, 4);
format = TextureFormat::RGBA32Float;
}
if (!data) {
std::cerr << "Failed to load image from path: " << path << std::endl;
}
return loadTexture(data, width, height, channels, format, pTextureView);
}
std::vector<std::string> Utils::getFiles(const std::string& directoryPath, std::vector<std::string> extensions)
{
std::vector<std::string> files;
for (const auto& entry : fs::recursive_directory_iterator(directoryPath)) {
for (auto& extension : extensions)
{
if (entry.is_regular_file() && entry.path().extension() == extension) {
files.push_back(entry.path().string());
}
}
}
return files;
}
TextureView Utils::createBuffer(uint32_t width, uint32_t height, TextureFormat format)
{
// Create the depth texture
TextureDescriptor textureDesc;
textureDesc.dimension = TextureDimension::e2D;
textureDesc.format = format;
textureDesc.mipLevelCount = 1;
textureDesc.sampleCount = 1;
textureDesc.size = { width, height, 1 };
textureDesc.usage = TextureUsage::RenderAttachment | TextureUsage::TextureBinding;
textureDesc.viewFormatCount = 1;
textureDesc.viewFormats = (TextureFormat*)&format;
Texture depthTexture = Context::getInstance().getDevice().CreateTexture(&textureDesc);
// Create the view of the depth texture manipulated by the rasterizer
TextureViewDescriptor textureViewDesc;
textureViewDesc.aspect = TextureAspect::All;;//TODO TextureAspect::DepthOnly; for depth
textureViewDesc.baseArrayLayer = 0;
textureViewDesc.arrayLayerCount = 1;
textureViewDesc.baseMipLevel = 0;
textureViewDesc.mipLevelCount = 1;
textureViewDesc.dimension = TextureViewDimension::e2D;
textureViewDesc.format = format;
return depthTexture.CreateView(&textureViewDesc);
}