Reworked mesh asset loading

This commit is contained in:
Dynamitos
2024-05-01 14:10:52 +02:00
parent 19922f4624
commit 2c1669aab4
23 changed files with 647 additions and 460 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ struct BlinnPhong : IBRDF
float3 baseColor; float3 baseColor;
float metallic; float metallic;
float3 normal; float3 normal;
float specular; float3 specular;
float roughness; float roughness;
float sheen; float sheen;
+13 -5
View File
@@ -1,9 +1,11 @@
import Common; import Common;
#define MAX_NUM_TEXCOORDS 8
struct MaterialParameter struct MaterialParameter
{ {
float3 position_WS; float3 position_WS;
float2 texCoords; float2 texCoords[MAX_NUM_TEXCOORDS];
float3 vertexColor; float3 vertexColor;
}; };
@@ -24,14 +26,17 @@ struct FragmentParameter
float3 tangent_WS : TANGENT0; float3 tangent_WS : TANGENT0;
float3 biTangent_WS : TANGENT1; float3 biTangent_WS : TANGENT1;
float3 position_WS : POSITION2; float3 position_WS : POSITION2;
float2 texCoords : TEXCOORD0;
float3 vertexColor : COLOR0; float3 vertexColor : COLOR0;
float2 texCoords[MAX_NUM_TEXCOORDS] : TEXCOORD0;
MaterialParameter getMaterialParameter() MaterialParameter getMaterialParameter()
{ {
MaterialParameter result; MaterialParameter result;
result.position_WS = position_WS; result.position_WS = position_WS;
result.texCoords = texCoords;
result.vertexColor = vertexColor; result.vertexColor = vertexColor;
for(int i = 0; i < MAX_NUM_TEXCOORDS; ++i)
{
result.texCoords[i] = texCoords[i];
}
return result; return result;
} }
LightingParameter getLightingParameter() LightingParameter getLightingParameter()
@@ -51,8 +56,8 @@ struct VertexAttributes
float3 normal_MS; float3 normal_MS;
float3 tangent_MS; float3 tangent_MS;
float3 biTangent_MS; float3 biTangent_MS;
float2 texCoords;
float3 vertexColor; float3 vertexColor;
float2 texCoords[MAX_NUM_TEXCOORDS];
FragmentParameter getParameter(float4x4 transformMatrix) FragmentParameter getParameter(float4x4 transformMatrix)
{ {
float4 modelPos = float4(position_MS, 1); float4 modelPos = float4(position_MS, 1);
@@ -69,8 +74,11 @@ struct VertexAttributes
result.biTangent_WS = biTangent_WS; result.biTangent_WS = biTangent_WS;
result.position_WS = worldPos.xyz; result.position_WS = worldPos.xyz;
result.position_CS = clipPos; result.position_CS = clipPos;
result.texCoords = texCoords;
result.vertexColor = vertexColor; result.vertexColor = vertexColor;
for(int i = 0; i < MAX_NUM_TEXCOORDS; ++i)
{
result.texCoords[i] = texCoords[i];
}
return result; return result;
} }
}; };
+1 -1
View File
@@ -11,7 +11,7 @@ struct StaticMeshVertexData : IVertexData
attributes.normal_MS = float3(normals[3 * index + 0], normals[3 * index + 1], normals[3 * index + 2]); attributes.normal_MS = float3(normals[3 * index + 0], normals[3 * index + 1], normals[3 * index + 2]);
attributes.tangent_MS = float3(tangents[3 * index + 0], tangents[3 * index + 1], tangents[3 * index + 2]); attributes.tangent_MS = float3(tangents[3 * index + 0], tangents[3 * index + 1], tangents[3 * index + 2]);
attributes.biTangent_MS = float3(biTangents[3 * index + 0], biTangents[3 * index + 1], biTangents[3 * index + 2]); attributes.biTangent_MS = float3(biTangents[3 * index + 0], biTangents[3 * index + 1], biTangents[3 * index + 2]);
attributes.texCoords = float2(texCoords[2 * index + 0], texCoords[2 * index + 1]); attributes.texCoords[0] = float2(texCoords[2 * index + 0], texCoords[2 * index + 1]);
attributes.vertexColor = float3(color[3 * index + 0], color[3 * index + 1], color[3 * index + 2]); attributes.vertexColor = float3(color[3 * index + 0], color[3 * index + 1], color[3 * index + 2]);
return attributes; return attributes;
} }
+44 -26
View File
@@ -66,75 +66,93 @@ void MaterialLoader::import(MaterialImportArgs args, PMaterialAsset asset)
// TODO: ALIGNMENT RULES // TODO: ALIGNMENT RULES
if(type.compare("float") == 0) if(type.compare("float") == 0)
{ {
OFloatParameter p = new FloatParameter(param.key(), uniformBufferOffset, uniformBinding); float defaultData = 0.f;
if (defaultValue != param.value().end())
{
defaultData = std::stof(defaultValue.value().get<std::string>());
}
OFloatParameter p = new FloatParameter(param.key(), defaultData, uniformBufferOffset, uniformBinding);
if(uniformBinding == -1) if(uniformBinding == -1)
{ {
layout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = bindingCounter, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER,}); layout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = bindingCounter, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER,});
uniformBinding = bindingCounter++; uniformBinding = bindingCounter++;
} }
uniformBufferOffset += 4; uniformBufferOffset += 4;
if(defaultValue != param.value().end())
{
p->data = std::stof(defaultValue.value().get<std::string>());
}
parameters.add(p->key); parameters.add(p->key);
expressions.add(std::move(p)); expressions.add(std::move(p));
} }
// TODO: ALIGNMENT RULES // TODO: ALIGNMENT RULES
else if(type.compare("float3") == 0) else if(type.compare("float3") == 0)
{ {
OVectorParameter p = new VectorParameter(param.key(), uniformBufferOffset, uniformBinding); Vector defaultData = Vector(0, 0, 0);
if (defaultValue != param.value().end())
{
defaultData = parseVector(defaultValue.value().get<std::string>().c_str());
}
OVectorParameter p = new VectorParameter(param.key(), defaultData, uniformBufferOffset, uniformBinding);
if(uniformBinding == -1) if(uniformBinding == -1)
{ {
layout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = bindingCounter, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER,}); layout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = bindingCounter, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER,});
uniformBinding = bindingCounter++; uniformBinding = bindingCounter++;
} }
uniformBufferOffset += 12; uniformBufferOffset += 16;
if(defaultValue != param.value().end())
{
p->data = parseVector(defaultValue.value().get<std::string>().c_str());
}
parameters.add(p->key); parameters.add(p->key);
expressions.add(std::move(p)); expressions.add(std::move(p));
} }
else if(type.compare("Texture2D") == 0) else if(type.compare("Texture2D") == 0)
{ {
OTextureParameter p = new TextureParameter(param.key(), 0, bindingCounter); PTextureAsset texture;
layout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = bindingCounter++, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,});
if(defaultValue != param.value().end()) if(defaultValue != param.value().end())
{ {
std::string defaultString = defaultValue.value().get<std::string>(); std::string defaultString = defaultValue.value().get<std::string>();
p->data = AssetRegistry::findTexture(defaultString); auto slashPos = defaultString.rfind("/");
} std::string folder = "";
if(p->data == nullptr) if (slashPos != std::string::npos)
{ {
p->data = AssetRegistry::findTexture(""); // this will return placeholder texture folder = defaultString.substr(0, slashPos - 1);
defaultString = defaultString.substr(slashPos, defaultString.length());
} }
texture = AssetRegistry::findTexture(folder, defaultString);
}
if(texture == nullptr)
{
texture = AssetRegistry::findTexture("", ""); // this will return placeholder texture
}
OTextureParameter p = new TextureParameter(param.key(), texture, bindingCounter);
layout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = bindingCounter++, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE, });
parameters.add(p->key); parameters.add(p->key);
expressions.add(std::move(p)); expressions.add(std::move(p));
} }
else if(type.compare("Sampler") == 0) else if(type.compare("Sampler") == 0)
{ {
OSamplerParameter p = new SamplerParameter(param.key(), 0, bindingCounter); OSamplerParameter p = new SamplerParameter(param.key(), graphics->createSampler({}), bindingCounter);
layout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = bindingCounter++, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLER,}); layout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = bindingCounter++, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLER,});
p->data = graphics->createSampler({});
parameters.add(p->key); parameters.add(p->key);
expressions.add(std::move(p)); expressions.add(std::move(p));
} }
else if (type.compare("Sampler2D") == 0) else if (type.compare("Sampler2D") == 0)
{ {
OCombinedTextureParameter p = new CombinedTextureParameter(param.key(), 0, bindingCounter); PTextureAsset texture;
layout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = bindingCounter++, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLER, });
p->sampler = graphics->createSampler({});
if (defaultValue != param.value().end()) if (defaultValue != param.value().end())
{ {
std::string defaultString = defaultValue.value().get<std::string>(); std::string defaultString = defaultValue.value().get<std::string>();
p->data = AssetRegistry::findTexture(defaultString); auto slashPos = defaultString.rfind("/");
} std::string folder = "";
if (p->data == nullptr) if (slashPos != std::string::npos)
{ {
p->data = AssetRegistry::findTexture(""); // this will return placeholder texture folder = defaultString.substr(0, slashPos - 1);
defaultString = defaultString.substr(slashPos, defaultString.length());
} }
texture = AssetRegistry::findTexture(folder, defaultString);
}
if (texture == nullptr)
{
texture = AssetRegistry::findTexture("", ""); // this will return placeholder texture
}
OCombinedTextureParameter p = new CombinedTextureParameter(param.key(), texture, graphics->createSampler({}), bindingCounter);
layout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = bindingCounter++, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLER, });
parameters.add(p->key); parameters.add(p->key);
expressions.add(std::move(p)); expressions.add(std::move(p));
} }
+363 -198
View File
@@ -5,11 +5,11 @@
#include "Graphics/StaticMeshVertexData.h" #include "Graphics/StaticMeshVertexData.h"
#include "Asset/AssetImporter.h" #include "Asset/AssetImporter.h"
#include "Asset/MaterialAsset.h" #include "Asset/MaterialAsset.h"
#include "Graphics/Shader.h"
#include <set> #include <set>
#include <fmt/core.h> #include <fmt/core.h>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp>
#include <stb_image_write.h> #include <stb_image_write.h>
#include <assimp/config.h> #include <assimp/config.h>
#include <assimp/Importer.hpp> #include <assimp/Importer.hpp>
@@ -41,172 +41,378 @@ void MeshLoader::importAsset(MeshImportArgs args)
import(args, ref); import(args, ref);
} }
void MeshLoader::loadMaterials(const aiScene* scene, const std::string& baseName, const std::filesystem::path& meshDirectory, const std::string& importPath, Array<PMaterialInstanceAsset>& globalMaterials)
void MeshLoader::convertAssimpARGB(unsigned char* dst, aiTexel* src, uint32 numPixels)
{
for (uint32 i = 0; i < numPixels; ++i)
{
dst[i * 4 + 0] = src[i].r;
dst[i * 4 + 1] = src[i].g;
dst[i * 4 + 2] = src[i].b;
dst[i * 4 + 3] = src[i].a;
}
}
void MeshLoader::loadTextures(const aiScene* scene, const std::filesystem::path& meshDirectory, const std::string& importPath, Map<std::string, PTextureAsset>& textures)
{
for (uint32 i = 0; i < scene->mNumTextures; ++i)
{
aiTexture* tex = scene->mTextures[i];
auto texPath = std::filesystem::path(tex->mFilename.C_Str());
if (std::filesystem::exists(texPath))
{ }
else if(std::filesystem::exists(meshDirectory / texPath))
{
texPath = meshDirectory / texPath;
}
else
{
if (tex->mHeight == 0)
{
// already compressed, just dump it to the disk
std::ofstream file(texPath, std::ios::binary);
file.write((const char*)tex->pcData, tex->mWidth);
file.flush();
}
else
{
// recompress data so that the TextureLoader can read it
unsigned char* texData = new unsigned char[tex->mWidth * tex->mHeight * 4];
convertAssimpARGB(texData, tex->pcData, tex->mWidth * tex->mHeight);
stbi_write_png(texPath.string().c_str(), tex->mWidth, tex->mHeight, 4, tex->pcData, tex->mWidth * 32);
delete[] texData;
}
}
std::cout << "Loading model texture " << texPath.string() << std::endl;
AssetImporter::importTexture(TextureImportArgs{
.filePath = texPath,
.importPath = importPath,
});
textures[texPath.string()] = AssetRegistry::findTexture(importPath, texPath.string());
}
}
constexpr const char* KEY_DIFFUSE_COLOR = "k_d";
constexpr const char* KEY_SPECULAR_COLOR = "k_s";
constexpr const char* KEY_AMBIENT_COLOR = "k_a";
constexpr const char* KEY_NORMAL = "n";
constexpr const char* KEY_DIFFUSE_TEXTURE = "tex_d";
constexpr const char* KEY_SPECULAR_TEXTURE = "tex_s";
constexpr const char* KEY_AMBIENT_TEXTURE = "tex_a";
constexpr const char* KEY_NORMAL_TEXTURE = "tex_n";
void MeshLoader::loadMaterials(const aiScene* scene, const Map<std::string, PTextureAsset>& textures, const std::string& baseName, const std::filesystem::path& meshDirectory, const std::string& importPath, Array<PMaterialInstanceAsset>& globalMaterials)
{ {
using json = nlohmann::json;
for(uint32 i = 0; i < scene->mNumMaterials; ++i) for(uint32 i = 0; i < scene->mNumMaterials; ++i)
{ {
aiMaterial* material = scene->mMaterials[i]; aiMaterial* material = scene->mMaterials[i];
json matCode;
std::string materialName = fmt::format("{0}{1}{2}", baseName, material->GetName().C_Str(), i);
materialName.erase(std::remove(materialName.begin(), materialName.end(), '.'), materialName.end()); // dots break adding the .asset extension later
matCode["name"] = materialName;
matCode["profile"] = "CelShading";
aiString texPath; aiString texPath;
uint32 baseColorIndex = 0; std::string materialName = fmt::format("M{0}{1}{2}", baseName, material->GetName().C_Str(), i);
uint32 normalIndex = 0; materialName.erase(std::remove(materialName.begin(), materialName.end(), '.'), materialName.end()); // dots break adding the .asset extension later
int32 codeIndex = -1; materialName.erase(std::remove(materialName.begin(), materialName.end(), '-'), materialName.end()); // dots break adding the .asset extension later
if(material->GetTexture(aiTextureType_DIFFUSE, 0, &texPath) == AI_SUCCESS) Gfx::ODescriptorLayout materialLayout = graphics->createDescriptorLayout("pMaterial");
{ Array<OShaderExpression> expressions;
auto texFilename = std::filesystem::path(texPath.C_Str()).stem(); Array<std::string> parameters;
if (texFilename == "white")
{
matCode["code"].push_back(
{
{ "exp", "Const" },
{ "value", "float3(1, 1, 1)"}
}
);
baseColorIndex = ++codeIndex;
}
else
{
AssetImporter::importTexture(TextureImportArgs{
.filePath = meshDirectory / texPath.C_Str(),
.importPath = importPath,
});
matCode["params"]["diffuseTexture"] =
{
{"type", "Texture2D"},
{"default", fmt::format("{0}/{1}", importPath, texFilename.string())}
};
matCode["params"]["diffuseSampler"] =
{
{"type", "Sampler"}
};
matCode["code"].push_back(
{
{ "exp", "Sample" },
{ "texture", "diffuseTexture" },
{ "sampler", "diffuseSampler" },
{ "coords", "input.texCoords" }
}
);
++codeIndex;
matCode["code"].push_back(
{
{ "exp", "Swizzle" },
{ "target", codeIndex },
{ "comp", json::array({0, 1, 2}) },
}
);
baseColorIndex = ++codeIndex;
}
}
else
{
matCode["code"].push_back(
{
{ "exp", "Const" },
{ "value", "input.vertexColor.xyz" }
}
);
baseColorIndex = ++codeIndex;
}
if(material->GetTexture(aiTextureType_NORMALS, 0, &texPath) == AI_SUCCESS)
{
auto texFilename = std::filesystem::path(texPath.C_Str()).stem();
AssetImporter::importTexture(TextureImportArgs{
.filePath = meshDirectory / texPath.C_Str(),
.importPath = importPath,
});
matCode["params"]["normalTexture"] =
{
{"type", "Texture2D"},
{"default", fmt::format("{0}/{1}", importPath, texFilename.string())}
};
matCode["params"]["normalSampler"] = {
{"type", "Sampler"},
};
matCode["code"].push_back(
{
{ "exp", "Sample" },
{ "texture", "normalTexture" },
{ "sampler", "normalSampler" },
{ "coords", "input.texCoords" }
}
);
++codeIndex;
matCode["code"].push_back(
{
{ "exp", "Swizzle" },
{ "target", codeIndex },
{ "comp", json::array({0, 1, 2}) },
}
);
++codeIndex;
matCode["code"].push_back(
{
{ "exp", "Mul" },
{ "lhs", "2" },
{ "rhs", codeIndex },
}
);
++codeIndex;
matCode["code"].push_back(
{
{ "exp", "Sub" },
{ "lhs", codeIndex },
{ "rhs", "float3(1, 1, 1)"},
}
);
normalIndex = ++codeIndex;
}
else
{
matCode["code"].push_back(
{
{ "exp", "Const" },
{ "value", "float3(0, 0, 1)" }
}
);
normalIndex = ++codeIndex;
}
matCode["code"].push_back(
{
{ "exp", "BRDF" },
{ "profile", matCode["profile"]},
{ "values", {
{ "baseColor", baseColorIndex },
{ "normal", normalIndex },
}}
}
);
std::string outMatFilename = materialName.append(".json");
std::ofstream outMatFile = std::ofstream(meshDirectory / outMatFilename);
outMatFile << std::setw(4) << matCode;
outMatFile.flush();
outMatFile.close();
std::cout << "writing json to " << outMatFilename << std::endl; materialLayout->addDescriptorBinding(Gfx::DescriptorBinding{
AssetImporter::importMaterial(MaterialImportArgs{ .binding = 0,
.filePath = meshDirectory / outMatFilename, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.shaderStages = Gfx::SE_SHADER_STAGE_FRAGMENT_BIT,
});
size_t uniformSize = 0;
uint32 bindingCounter = 1;
auto addVectorParameter = [&](std::string paramKey, const char* matKey, int type, int index)
{
aiColor3D color;
material->Get(matKey, type, index, color);
expressions.add(new VectorParameter(paramKey, Vector(color.r, color.g, color.b), uniformSize, 0));
uniformSize = (uniformSize + sizeof(Vector4) - 1) / sizeof(Vector4) * sizeof(Vector4);
uniformSize += sizeof(Vector);
parameters.add(paramKey);
};
auto addTextureParameter = [&](std::string paramKey, aiTextureType type, int index, std::string& result)
{
aiString texPath;
aiTextureMapping mapping;
uint32 uvIndex = 0;
float blend = std::numeric_limits<float>::max();
aiTextureOp op;
aiTextureMapMode mapMode;
if (material->GetTexture(type, index, &texPath, &mapping, &uvIndex, &blend, &op, &mapMode) != AI_SUCCESS)
{
std::cout << "fuck" << std::endl;
}
std::string textureKey = fmt::format("{0}Texture{1}", paramKey, index);
auto texFilename = std::filesystem::path(texPath.C_Str());
PTextureAsset texture;
if (textures.contains(texFilename.string()))
{
texture = textures[texFilename.string()];
}
else if (std::filesystem::exists(texFilename))
{
AssetImporter::importTexture(TextureImportArgs{
.filePath = texFilename,
.importPath = importPath, .importPath = importPath,
}); });
std::string materialAsset; texture = AssetRegistry::findTexture(importPath, texFilename.stem().string());
if (importPath.empty())
{
materialAsset = matCode["name"].get<std::string>();
} }
else else
{ {
materialAsset = fmt::format("{0}/{1}", importPath, matCode["name"].get<std::string>()); std::cout << "couldnt find " << texPath.C_Str() << std::endl;
} }
PMaterialAsset baseMat = AssetRegistry::findMaterial(materialAsset); expressions.add(new TextureParameter(textureKey, texture, bindingCounter));
materialLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.binding = bindingCounter,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
.textureType = Gfx::SE_IMAGE_VIEW_TYPE_2D,
.shaderStages = Gfx::SE_SHADER_STAGE_FRAGMENT_BIT,
});
parameters.add(textureKey);
bindingCounter++;
std::string samplerKey = std::format("{0}Sampler{1}", paramKey, index);
SamplerCreateInfo samplerInfo = {};
switch (mapMode)
{
case aiTextureMapMode_Wrap:
samplerInfo.addressModeU = Gfx::SE_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.addressModeV = Gfx::SE_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.addressModeW = Gfx::SE_SAMPLER_ADDRESS_MODE_REPEAT;
break;
case aiTextureMapMode_Clamp:
samplerInfo.addressModeU = Gfx::SE_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
samplerInfo.addressModeV = Gfx::SE_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
samplerInfo.addressModeW = Gfx::SE_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
break;
case aiTextureMapMode_Decal:
samplerInfo.addressModeU = Gfx::SE_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
samplerInfo.addressModeV = Gfx::SE_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
samplerInfo.addressModeW = Gfx::SE_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
break;
case aiTextureMapMode_Mirror:
samplerInfo.addressModeU = Gfx::SE_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT;
samplerInfo.addressModeV = Gfx::SE_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT;
samplerInfo.addressModeW = Gfx::SE_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT;
break;
}
expressions.add(new SamplerParameter(samplerKey, graphics->createSampler(samplerInfo), bindingCounter));
materialLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.binding = bindingCounter,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLER,
.shaderStages = Gfx::SE_SHADER_STAGE_FRAGMENT_BIT,
});
parameters.add(samplerKey);
bindingCounter++;
std::string sampleKey = fmt::format("{0}Sample{1}", paramKey, index);
expressions.add(new SampleExpression());
expressions.back()->key = sampleKey;
expressions.back()->inputs["texture"].source = textureKey;
expressions.back()->inputs["sampler"].source = samplerKey;
expressions.back()->inputs["coords"].source = fmt::format("input.texCoords[{0}]", uvIndex);
std::string colorExtract = fmt::format("{0}Extract{1}", paramKey, index);
expressions.add(new SwizzleExpression({ 0, 1, 2, -1 }));
expressions.back()->key = colorExtract;
expressions.back()->inputs["target"].source = sampleKey;
//TODO: extract alpha, set opacity
if (blend == std::numeric_limits<float>::max())
{
result = colorExtract;
return;
}
std::string blendFactorKey = fmt::format("{0}BlendFactor{1}", paramKey, index);
expressions.add(new FloatParameter(blendFactorKey, blend, uniformSize, 0));
uniformSize += sizeof(float);
parameters.add(blendFactorKey);
std::string strengthKey = fmt::format("{0}Strength{1}", paramKey, index);
expressions.add(new MulExpression());
expressions.back()->key = strengthKey;
expressions.back()->inputs["lhs"].source = colorExtract;
expressions.back()->inputs["rhs"].source = blendFactorKey;
std::string blendKey = fmt::format("{0}Blend{1}", paramKey, index);
switch (op) {
/** T = T1 * T2 */
case aiTextureOp_Multiply:
expressions.add(new MulExpression());
break;
/** T = T1 - T2 */
case aiTextureOp_Subtract:
expressions.add(new SubExpression());
break;
/** T = T1 / T2 */
case aiTextureOp_Divide:
//expressions[blendKey] = new DivExpression();
throw std::logic_error("Not implemented");
/** T = (T1 + T2) - (T1 * T2) */
case aiTextureOp_SmoothAdd:
throw std::logic_error("Not implemented");
/** T = T1 + (T2-0.5) */
case aiTextureOp_SignedAdd:
throw std::logic_error("Not implemented");
/** T = T1 + T2 */
case aiTextureOp_Add:
default:
expressions.add(new AddExpression());
break;
}
expressions.back()->key = blendKey;
expressions.back()->inputs["lhs"].source = result;
expressions.back()->inputs["rhs"].source = strengthKey;
result = blendKey;
};
addVectorParameter(KEY_DIFFUSE_COLOR, AI_MATKEY_COLOR_DIFFUSE);
addVectorParameter(KEY_SPECULAR_COLOR, AI_MATKEY_COLOR_SPECULAR);
addVectorParameter(KEY_AMBIENT_COLOR, AI_MATKEY_COLOR_AMBIENT);
std::string outputDiffuse = KEY_DIFFUSE_COLOR;
std::string outputSpecular = KEY_SPECULAR_COLOR;
std::string outputAmbient = KEY_AMBIENT_COLOR;
std::string outputNormal = "";
uint32 numDiffuseTextures = material->GetTextureCount(aiTextureType_DIFFUSE);
for (uint32 i = 0; i < numDiffuseTextures; ++i)
{
addTextureParameter(KEY_DIFFUSE_TEXTURE, aiTextureType_DIFFUSE, i, outputDiffuse);
}
uint32 numSpecular = material->GetTextureCount(aiTextureType_SPECULAR);
for (uint32 i = 0; i < numSpecular; ++i)
{
addTextureParameter(KEY_SPECULAR_TEXTURE, aiTextureType_SPECULAR, i, outputSpecular);
}
uint32 numAmbient = material->GetTextureCount(aiTextureType_AMBIENT);
for (uint32 i = 0; i < numSpecular; ++i)
{
addTextureParameter(KEY_AMBIENT_COLOR, aiTextureType_AMBIENT, i, outputAmbient);
}
uint32 numNormal = material->GetTextureCount(aiTextureType_NORMALS);
if (numNormal > 1)
{
std::cout << "More than 1 normal??" << std::endl;
}
else if (numNormal == 1)
{
aiString texPath;
aiTextureMapping mapping;
uint32 uvIndex;
if (material->GetTexture(aiTextureType_NORMALS, 0, &texPath, &mapping, &uvIndex) != AI_SUCCESS)
{
std::cout << "fuck" << std::endl;
}
std::string textureKey = fmt::format("NormalTexture");
auto texFilename = std::filesystem::path(texPath.C_Str());
PTextureAsset texture;
if (textures.contains(texFilename.string()))
{
texture = textures[texFilename.string()];
}
else if (std::filesystem::exists(texFilename))
{
AssetImporter::importTexture(TextureImportArgs{
.filePath = texFilename,
.importPath = importPath,
});
texture = AssetRegistry::findTexture(importPath, texFilename.stem().string());
}
if (texture != nullptr)
{
expressions.add(new TextureParameter(textureKey, texture, bindingCounter));
materialLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.binding = bindingCounter,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
.shaderStages = Gfx::SE_SHADER_STAGE_FRAGMENT_BIT,
});
parameters.add(textureKey);
bindingCounter++;
std::string samplerKey = "NormalSampler";
SamplerCreateInfo samplerInfo = {};
expressions.add(new SamplerParameter(samplerKey, graphics->createSampler(samplerInfo), bindingCounter));
materialLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.binding = bindingCounter,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLER,
.shaderStages = Gfx::SE_SHADER_STAGE_FRAGMENT_BIT,
});
parameters.add(samplerKey);
bindingCounter++;
std::string sampleKey = "NormalSample";
expressions.add(new SampleExpression());
expressions.back()->key = sampleKey;
expressions.back()->inputs["texture"].source = textureKey;
expressions.back()->inputs["sampler"].source = samplerKey;
expressions.back()->inputs["coords"].source = fmt::format("input.texCoords[{0}]", uvIndex);
std::string normalExtract = "NormalExtract";
expressions.add(new SwizzleExpression({ 0, 1, 2, -1 }));
expressions.back()->key = normalExtract;
expressions.back()->inputs["target"].source = sampleKey;
std::string mulKey = "NormalMul";
expressions.add(new MulExpression());
expressions.back()->key = mulKey;
expressions.back()->inputs["lhs"].source = "2";
expressions.back()->inputs["rhs"].source = normalExtract;
std::string subKey = "NormalSub";
expressions.add(new SubExpression());
expressions.back()->key = subKey;
expressions.back()->inputs["lhs"].source = "1";
expressions.back()->inputs["rhs"].source = mulKey;
outputNormal = subKey;
}
}
MaterialNode brdf;
brdf.profile = "BlinnPhong";
brdf.variables["baseColor"] = outputDiffuse;
brdf.variables["specular"] = outputSpecular;
if (!outputNormal.empty())
{
brdf.variables["normal"] = outputNormal;
}
materialLayout->create();
OMaterialAsset baseMat = new MaterialAsset(importPath, materialName);
baseMat->material = new Material(graphics,
std::move(materialLayout),
uniformSize, 0, materialName,
std::move(expressions),
std::move(parameters),
std::move(brdf)
);
baseMat->material->compile();
graphics->getShaderCompiler()->registerMaterial(baseMat->material);
globalMaterials[i] = baseMat->instantiate(InstantiationParameter{ globalMaterials[i] = baseMat->instantiate(InstantiationParameter{
.name = fmt::format("{0}_Inst_0", baseMat->getName()), .name = fmt::format("{0}_Inst_0", baseMat->getName()),
.folderPath = baseMat->getFolderPath(), .folderPath = baseMat->getFolderPath(),
}); });
AssetRegistry::get().saveAsset(PMaterialAsset(baseMat), MaterialAsset::IDENTIFIER, baseMat->getFolderPath(), baseMat->getName());
AssetRegistry::get().registerMaterial(std::move(baseMat));
} }
} }
@@ -225,6 +431,7 @@ void findMeshRoots(aiNode *node, List<aiNode *> &meshNodes)
void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialInstanceAsset>& materials, Array<OMesh>& globalMeshes, Component::Collider& collider) void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialInstanceAsset>& materials, Array<OMesh>& globalMeshes, Component::Collider& collider)
{ {
//#pragma omp parallel for
for (uint32 meshIndex = 0; meshIndex < scene->mNumMeshes; ++meshIndex) for (uint32 meshIndex = 0; meshIndex < scene->mNumMeshes; ++meshIndex)
{ {
aiMesh* mesh = scene->mMeshes[meshIndex]; aiMesh* mesh = scene->mMeshes[meshIndex];
@@ -242,7 +449,7 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialIns
Array<Vector> colors(mesh->mNumVertices); Array<Vector> colors(mesh->mNumVertices);
StaticMeshVertexData* vertexData = StaticMeshVertexData::getInstance(); StaticMeshVertexData* vertexData = StaticMeshVertexData::getInstance();
#pragma omp parallel for //#pragma omp parallel for
for (int32 i = 0; i < mesh->mNumVertices; ++i) for (int32 i = 0; i < mesh->mNumVertices; ++i)
{ {
positions[i] = Vector(mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z); positions[i] = Vector(mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z);
@@ -283,7 +490,7 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialIns
vertexData->loadColors(id, colors); vertexData->loadColors(id, colors);
Array<uint32> indices(mesh->mNumFaces * 3); Array<uint32> indices(mesh->mNumFaces * 3);
#pragma omp parallel for //#pragma omp parallel for
for (int32 faceIndex = 0; faceIndex < mesh->mNumFaces; ++faceIndex) for (int32 faceIndex = 0; faceIndex < mesh->mNumFaces; ++faceIndex)
{ {
indices[faceIndex * 3 + 0] = mesh->mFaces[faceIndex].mIndices[0]; indices[faceIndex * 3 + 0] = mesh->mFaces[faceIndex].mIndices[0];
@@ -308,49 +515,6 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialIns
} }
} }
void MeshLoader::convertAssimpARGB(unsigned char* dst, aiTexel* src, uint32 numPixels)
{
for(uint32 i = 0; i < numPixels; ++i)
{
dst[i * 4 + 0] = src[i].r;
dst[i * 4 + 1] = src[i].g;
dst[i * 4 + 2] = src[i].b;
dst[i * 4 + 3] = src[i].a;
}
}
void MeshLoader::loadTextures(const aiScene* scene, const std::filesystem::path& meshDirectory, const std::string& importPath)
{
for (uint32 i = 0; i < scene->mNumTextures; ++i)
{
aiTexture* tex = scene->mTextures[i];
auto texPath = std::filesystem::path(tex->mFilename.C_Str());
auto texPngPath = meshDirectory;
texPngPath.append(fmt::format("{0}.{1}", texPath.filename().string(), tex->achFormatHint));
if (!std::filesystem::exists(texPngPath))
{
if (tex->mHeight == 0)
{
// already compressed, just dump it to the disk
std::ofstream file(texPngPath, std::ios::binary);
file.write((const char*)tex->pcData, tex->mWidth);
file.flush();
}
else
{
// recompress data so that the TextureLoader can read it
unsigned char* texData = new unsigned char[tex->mWidth * tex->mHeight * 4];
convertAssimpARGB(texData, tex->pcData, tex->mWidth * tex->mHeight);
stbi_write_png(texPngPath.string().c_str(), tex->mWidth, tex->mHeight, 4, tex->pcData, tex->mWidth * 32);
delete[] texData;
}
}
std::cout << "Loading model texture " << texPngPath.string() << std::endl;
AssetImporter::importTexture(TextureImportArgs {
.filePath = texPngPath,
.importPath = importPath,
});
}
}
Matrix4 convertMatrix(aiMatrix4x4 matrix) Matrix4 convertMatrix(aiMatrix4x4 matrix)
{ {
@@ -390,9 +554,10 @@ void MeshLoader::import(MeshImportArgs args, PMeshAsset meshAsset)
const aiScene *scene = importer.ApplyPostProcessing(aiProcess_CalcTangentSpace); const aiScene *scene = importer.ApplyPostProcessing(aiProcess_CalcTangentSpace);
std::cout << importer.GetErrorString() << std::endl; std::cout << importer.GetErrorString() << std::endl;
Map<std::string, PTextureAsset> textures;
loadTextures(scene, args.filePath.parent_path(), args.importPath, textures);
Array<PMaterialInstanceAsset> globalMaterials(scene->mNumMaterials); Array<PMaterialInstanceAsset> globalMaterials(scene->mNumMaterials);
loadTextures(scene, args.filePath.parent_path(), args.importPath); loadMaterials(scene, textures, args.filePath.stem().string(), args.filePath.parent_path(), args.importPath, globalMaterials);
loadMaterials(scene, args.filePath.stem().string(), args.filePath.parent_path(), args.importPath, globalMaterials);
Array<OMesh> globalMeshes(scene->mNumMeshes); Array<OMesh> globalMeshes(scene->mNumMeshes);
Component::Collider collider; Component::Collider collider;
+4 -2
View File
@@ -1,6 +1,7 @@
#pragma once #pragma once
#include "MinimalEngine.h" #include "MinimalEngine.h"
#include "Containers/List.h" #include "Containers/List.h"
#include "Containers/Map.h"
#include "Component/Collider.h" #include "Component/Collider.h"
#include <filesystem> #include <filesystem>
@@ -12,6 +13,7 @@ namespace Seele
DECLARE_REF(Mesh) DECLARE_REF(Mesh)
DECLARE_REF(MeshAsset) DECLARE_REF(MeshAsset)
DECLARE_REF(MaterialInstanceAsset) DECLARE_REF(MaterialInstanceAsset)
DECLARE_REF(TextureAsset)
DECLARE_NAME_REF(Gfx, Graphics) DECLARE_NAME_REF(Gfx, Graphics)
struct MeshImportArgs struct MeshImportArgs
{ {
@@ -25,8 +27,8 @@ public:
~MeshLoader(); ~MeshLoader();
void importAsset(MeshImportArgs args); void importAsset(MeshImportArgs args);
private: private:
void loadMaterials(const aiScene* scene, const std::string& baseName, const std::filesystem::path& meshDirectory, const std::string& importPath, Array<PMaterialInstanceAsset>& globalMaterials); void loadTextures(const aiScene* scene, const std::filesystem::path& meshDirectory, const std::string& importPath, Map<std::string, PTextureAsset>& textures);
void loadTextures(const aiScene* scene, const std::filesystem::path& meshDirectory, const std::string& importPath); void loadMaterials(const aiScene* scene, const Map<std::string, PTextureAsset>& textures, const std::string& baseName, const std::filesystem::path& meshDirectory, const std::string& importPath, Array<PMaterialInstanceAsset>& globalMaterials);
void loadGlobalMeshes(const aiScene* scene, const Array<PMaterialInstanceAsset>& materials, Array<OMesh>& globalMeshes, Component::Collider& collider); void loadGlobalMeshes(const aiScene* scene, const Array<PMaterialInstanceAsset>& materials, Array<OMesh>& globalMeshes, Component::Collider& collider);
void convertAssimpARGB(unsigned char* dst, aiTexel* src, uint32 numPixels); void convertAssimpARGB(unsigned char* dst, aiTexel* src, uint32 numPixels);
+16 -29
View File
@@ -35,10 +35,11 @@ TextureLoader::~TextureLoader()
void TextureLoader::importAsset(TextureImportArgs args) void TextureLoader::importAsset(TextureImportArgs args)
{ {
std::filesystem::path assetPath = args.filePath.filename(); std::string str = args.filePath.filename().string();
assetPath.replace_extension("asset"); auto pos = str.rfind(".");
str.replace(str.begin() + pos, str.end(), "");
OTextureAsset asset = new TextureAsset(args.importPath, assetPath.stem().string()); OTextureAsset asset = new TextureAsset(args.importPath, str);
PTextureAsset ref = asset; PTextureAsset ref = asset;
asset->setStatus(Asset::Status::Loading); asset->setStatus(Asset::Status::Loading);
AssetRegistry::get().registerTexture(std::move(asset)); AssetRegistry::get().registerTexture(std::move(asset));
@@ -55,13 +56,14 @@ void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset)
int totalWidth = 0, totalHeight = 0, n = 0; int totalWidth = 0, totalHeight = 0, n = 0;
unsigned char* data = stbi_load(args.filePath.string().c_str(), &totalWidth, &totalHeight, &n, 4); unsigned char* data = stbi_load(args.filePath.string().c_str(), &totalWidth, &totalHeight, &n, 4);
ktxTexture2* kTexture = nullptr; ktxTexture2* kTexture = nullptr;
ktxTextureCreateInfo createInfo; ktxTextureCreateInfo createInfo = {
createInfo.vkFormat = VK_FORMAT_R8G8B8A8_UNORM; .vkFormat = VK_FORMAT_R8G8B8A8_UNORM,
createInfo.baseDepth = 1; .baseDepth = 1,
createInfo.numLevels = 1; .numLevels = 1,
createInfo.numLayers = 1; .numLayers = 1,
createInfo.isArray = false; .isArray = false,
createInfo.generateMipmaps = false; .generateMipmaps = false,
};
if (args.type == TextureImportType::TEXTURE_CUBEMAP) if (args.type == TextureImportType::TEXTURE_CUBEMAP)
{ {
@@ -113,18 +115,17 @@ void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset)
ktxTexture_SetImageFromMemory(ktxTexture(kTexture), ktxTexture_SetImageFromMemory(ktxTexture(kTexture),
0, 0, 0, data, totalWidth * totalHeight * 4 * sizeof(unsigned char)); 0, 0, 0, data, totalWidth * totalHeight * 4 * sizeof(unsigned char));
} }
std::cout << "starting compression of " << textureAsset->getAssetIdentifier() << std::endl;
ktxBasisParams params2 = { ktxBasisParams params2 = {
.structSize = sizeof(ktxBasisParams), .structSize = sizeof(ktxBasisParams),
.uastc = false, .uastc = false,
.threadCount = std::thread::hardware_concurrency(), .threadCount = std::thread::hardware_concurrency(),
.compressionLevel = 0, .compressionLevel = 0,
.qualityLevel = 0, .qualityLevel = 1,
}; };
ktx_error_code_e error = ktxTexture2_CompressBasisEx(kTexture, &params2); //ktx_error_code_e error = ktxTexture2_CompressBasisEx(kTexture, &params2);
assert(error == KTX_SUCCESS); //assert(error == KTX_SUCCESS);
ktx_uint8_t* dest; ktx_uint8_t* dest;
ktx_size_t size; ktx_size_t size;
@@ -134,13 +135,9 @@ void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset)
std::memcpy(memory.data(), dest, size); std::memcpy(memory.data(), dest, size);
free(dest); free(dest);
Array<uint8> rawPixels(totalWidth * totalHeight * 4);
std::memcpy(rawPixels.data(), data, rawPixels.size());
stbi_image_free(data); stbi_image_free(data);
ArchiveBuffer temp(graphics); ArchiveBuffer temp(graphics);
Serialization::save(temp, rawPixels);
Serialization::save(temp, memory); Serialization::save(temp, memory);
temp.rewind(); temp.rewind();
textureAsset->load(temp); textureAsset->load(temp);
@@ -148,16 +145,6 @@ void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset)
{ {
return; return;
} }
auto stream = AssetRegistry::createWriteStream((std::filesystem::path(textureAsset->folderPath) / textureAsset->getName()).replace_extension("asset").string(), std::ios::binary);
ArchiveBuffer archive; AssetRegistry::get().saveAsset(textureAsset, TextureAsset::IDENTIFIER, textureAsset->getFolderPath(), textureAsset->getName());
Serialization::save(archive, TextureAsset::IDENTIFIER);
Serialization::save(archive, textureAsset->getName());
Serialization::save(archive, textureAsset->getFolderPath());
Serialization::save(archive, rawPixels);
Serialization::save(archive, memory);
archive.writeToStream(stream);
////co_return;
} }
+1 -1
View File
@@ -59,7 +59,7 @@ int main() {
.filePath = sourcePath / "import/models/cube.fbx", .filePath = sourcePath / "import/models/cube.fbx",
}); });
AssetImporter::importMesh(MeshImportArgs{ AssetImporter::importMesh(MeshImportArgs{
.filePath = sourcePath / "import/models/after-the-rain-vr-sound/source/Whitechapel.obj", .filePath = sourcePath / "import/models/after-the-rain-vr-sound/source/Whitechapel.fbx",
.importPath = "Whitechapel" .importPath = "Whitechapel"
}); });
WindowCreateInfo mainWindowInfo; WindowCreateInfo mainWindowInfo;
+46 -61
View File
@@ -26,69 +26,54 @@ void AssetRegistry::init(std::filesystem::path rootFolder, Gfx::PGraphics graphi
get().initialize(rootFolder, graphics); get().initialize(rootFolder, graphics);
} }
PMeshAsset AssetRegistry::findMesh(const std::string &filePath) PMeshAsset AssetRegistry::findMesh(std::string_view folderPath, std::string_view filePath)
{ {
AssetFolder* folder = get().assetRoot; AssetFolder* folder = get().assetRoot;
std::string fileName = filePath; if (!folderPath.empty())
size_t slashLoc = filePath.rfind("/");
if(slashLoc != std::string::npos)
{ {
folder = get().getOrCreateFolder(filePath.substr(0, slashLoc)); folder = get().getOrCreateFolder(folderPath);
fileName = filePath.substr(slashLoc+1, filePath.size());
} }
return folder->meshes.at(fileName); return folder->meshes.at(std::string(filePath));
} }
PTextureAsset AssetRegistry::findTexture(const std::string &filePath) PTextureAsset AssetRegistry::findTexture(std::string_view folderPath, std::string_view filePath)
{ {
AssetFolder* folder = get().assetRoot; AssetFolder* folder = get().assetRoot;
std::string fileName = filePath; if (!folderPath.empty())
size_t slashLoc = filePath.rfind("/");
if (slashLoc != std::string::npos)
{ {
folder = get().getOrCreateFolder(filePath.substr(0, slashLoc)); folder = get().getOrCreateFolder(folderPath);
fileName = filePath.substr(slashLoc + 1, filePath.size());
} }
return folder->textures.at(fileName); return folder->textures.at(std::string(filePath));
} }
PFontAsset AssetRegistry::findFont(const std::string& filePath) PFontAsset AssetRegistry::findFont(std::string_view folderPath, std::string_view filePath)
{ {
AssetFolder* folder = get().assetRoot; AssetFolder* folder = get().assetRoot;
std::string fileName = filePath; if (!folderPath.empty())
size_t slashLoc = filePath.rfind("/");
if(slashLoc != std::string::npos)
{ {
folder = get().getOrCreateFolder(filePath.substr(0, slashLoc)); folder = get().getOrCreateFolder(folderPath);
fileName = filePath.substr(slashLoc+1, filePath.size());
} }
return folder->fonts.at(fileName); return folder->fonts.at(std::string(filePath));
} }
PMaterialAsset AssetRegistry::findMaterial(const std::string &filePath) PMaterialAsset AssetRegistry::findMaterial(std::string_view folderPath, std::string_view filePath)
{ {
AssetFolder* folder = get().assetRoot; AssetFolder* folder = get().assetRoot;
std::string fileName = filePath; if (!folderPath.empty())
size_t slashLoc = filePath.rfind("/");
if (slashLoc != std::string::npos)
{ {
folder = get().getOrCreateFolder(filePath.substr(0, slashLoc)); folder = get().getOrCreateFolder(folderPath);
fileName = filePath.substr(slashLoc + 1, filePath.size());
} }
return folder->materials.at(fileName); return folder->materials.at(std::string(filePath));
} }
PMaterialInstanceAsset AssetRegistry::findMaterialInstance(const std::string &filePath) PMaterialInstanceAsset AssetRegistry::findMaterialInstance(std::string_view folderPath, std::string_view filePath)
{ {
AssetFolder* folder = get().assetRoot; AssetFolder* folder = get().assetRoot;
std::string fileName = filePath; if (!folderPath.empty())
size_t slashLoc = filePath.rfind("/");
if (slashLoc != std::string::npos)
{ {
folder = get().getOrCreateFolder(filePath.substr(0, slashLoc)); folder = get().getOrCreateFolder(folderPath);
fileName = filePath.substr(slashLoc + 1, filePath.size());
} }
return folder->instances.at(fileName); return folder->instances.at(std::string(filePath));
} }
std::ofstream AssetRegistry::createWriteStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode) std::ofstream AssetRegistry::createWriteStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode)
@@ -126,6 +111,31 @@ void AssetRegistry::saveRegistry()
get().saveRegistryInternal(); get().saveRegistryInternal();
} }
AssetRegistry::AssetFolder* AssetRegistry::getOrCreateFolder(std::string_view fullPath)
{
AssetFolder* result = assetRoot;
std::string temp = std::string(fullPath);
while (!temp.empty())
{
size_t slashLoc = temp.find("/");
if (slashLoc == std::string::npos)
{
if (!result->children.contains(temp))
{
result->children[temp] = new AssetFolder(temp);
}
return result->children[temp];
}
std::string folderName = temp.substr(0, slashLoc);
if (!result->children.contains(folderName))
{
result->children[folderName] = new AssetFolder(fullPath);
}
result = result->children[folderName];
temp = temp.substr(slashLoc + 1, temp.size());
}
return result;
}
void AssetRegistry::initialize(const std::filesystem::path &_rootFolder, Gfx::PGraphics _graphics) void AssetRegistry::initialize(const std::filesystem::path &_rootFolder, Gfx::PGraphics _graphics)
{ {
@@ -318,7 +328,7 @@ void AssetRegistry::saveAsset(PAsset asset, uint64 identifier, const std::filesy
if (name.empty()) if (name.empty())
return; return;
std::string path = (folderPath / name).replace_extension("asset").string(); std::string path = (folderPath / name).string().append(".asset");
auto assetStream = createWriteStream(std::move(path), std::ios::binary); auto assetStream = createWriteStream(std::move(path), std::ios::binary);
ArchiveBuffer buffer(graphics); ArchiveBuffer buffer(graphics);
// write identifier // write identifier
@@ -367,31 +377,6 @@ void AssetRegistry::registerMaterialInstance(OMaterialInstanceAsset material)
folder->instances[material->getName()] = std::move(material); folder->instances[material->getName()] = std::move(material);
} }
AssetRegistry::AssetFolder* AssetRegistry::getOrCreateFolder(std::string fullPath)
{
AssetFolder* result = assetRoot;
std::string temp = fullPath;
while(!fullPath.empty())
{
size_t slashLoc = fullPath.find("/");
if(slashLoc == std::string::npos)
{
if (!result->children.contains(fullPath))
{
result->children[fullPath] = new AssetFolder(fullPath);
}
return result->children[fullPath];
}
std::string folderName = fullPath.substr(0, slashLoc);
if (!result->children.contains(folderName))
{
result->children[folderName] = new AssetFolder(temp);
}
result = result->children[folderName];
fullPath = fullPath.substr(slashLoc+1, fullPath.size());
}
return result;
}
std::ofstream AssetRegistry::internalCreateWriteStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode) std::ofstream AssetRegistry::internalCreateWriteStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode)
{ {
+6 -6
View File
@@ -21,11 +21,11 @@ public:
static std::filesystem::path getRootFolder(); static std::filesystem::path getRootFolder();
static PMeshAsset findMesh(const std::string& filePath); static PMeshAsset findMesh(std::string_view folderPath, std::string_view filePath);
static PTextureAsset findTexture(const std::string& filePath); static PTextureAsset findTexture(std::string_view folderPath, std::string_view filePath);
static PFontAsset findFont(const std::string& name); static PFontAsset findFont(std::string_view folderPath, std::string_view name);
static PMaterialAsset findMaterial(const std::string& filePath); static PMaterialAsset findMaterial(std::string_view folderPath, std::string_view filePath);
static PMaterialInstanceAsset findMaterialInstance(const std::string& filePath); static PMaterialInstanceAsset findMaterialInstance(std::string_view folderPath, std::string_view filePath);
static std::ofstream createWriteStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode = std::ios::out); static std::ofstream createWriteStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode = std::ios::out);
static std::ifstream createReadStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode = std::ios::in); static std::ifstream createReadStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode = std::ios::in);
@@ -46,7 +46,7 @@ public:
AssetFolder(std::string_view folderPath); AssetFolder(std::string_view folderPath);
~AssetFolder(); ~AssetFolder();
}; };
AssetFolder* getOrCreateFolder(std::string foldername); AssetFolder* getOrCreateFolder(std::string_view foldername);
AssetRegistry(); AssetRegistry();
static AssetRegistry* getInstance(); static AssetRegistry* getInstance();
private: private:
+1
View File
@@ -24,6 +24,7 @@ public:
private: private:
OMaterial material; OMaterial material;
friend class MaterialLoader; friend class MaterialLoader;
friend class MeshLoader;
}; };
DEFINE_REF(MaterialAsset) DEFINE_REF(MaterialAsset)
} // namespace Seele } // namespace Seele
+5 -2
View File
@@ -22,16 +22,19 @@ MaterialInstanceAsset::~MaterialInstanceAsset()
void MaterialInstanceAsset::save(ArchiveBuffer& buffer) const void MaterialInstanceAsset::save(ArchiveBuffer& buffer) const
{ {
Serialization::save(buffer, baseMaterial->getAssetIdentifier()); Serialization::save(buffer, baseMaterial->getFolderPath());
Serialization::save(buffer, baseMaterial->getName());
material->save(buffer); material->save(buffer);
} }
void MaterialInstanceAsset::load(ArchiveBuffer& buffer) void MaterialInstanceAsset::load(ArchiveBuffer& buffer)
{ {
std::string folder;
Serialization::load(buffer, folder);
std::string id; std::string id;
Serialization::load(buffer, id); Serialization::load(buffer, id);
material = new MaterialInstance(); material = new MaterialInstance();
material->load(buffer); material->load(buffer);
baseMaterial = AssetRegistry::findMaterial(id); baseMaterial = AssetRegistry::findMaterial(folder, id);
material->setBaseMaterial(baseMaterial); material->setBaseMaterial(baseMaterial);
} }
+19 -51
View File
@@ -20,91 +20,60 @@ TextureAsset::TextureAsset(std::string_view folderPath, std::string_view name)
TextureAsset::~TextureAsset() TextureAsset::~TextureAsset()
{ {
ktxTexture_Destroy(ktxTexture(ktxHandle));
} }
void TextureAsset::save(ArchiveBuffer&) const void TextureAsset::save(ArchiveBuffer& buffer) const
{ {
/*ktxTexture2* kTexture;
ktxTextureCreateInfo createInfo = {
.vkFormat = (uint32_t)texture->getFormat(),
.baseWidth = texture->getSizeX(),
.baseHeight = texture->getSizeY(),
.baseDepth = texture->getSizeZ(),
.numDimensions = texture->getSizeZ() > 1 ? 3u : texture->getSizeY() > 1 ? 2u : 1u,
.numLevels = texture->getMipLevels(),
.numLayers = 1,
.numFaces = texture->getNumFaces(),
.isArray = false,
.generateMipmaps = false,
};
KTX_CHECK(ktxTexture2_Create(&createInfo, KTX_TEXTURE_CREATE_ALLOC_STORAGE, &kTexture));
for (uint32 depth = 0; depth < texture->getSizeZ(); ++depth)
{
for (uint32 face = 0; face < texture->getNumFaces(); ++face)
{
// technically, downloading cant be const, because we have to allocate temporary buffers and change layouts
// but practically the texture stays the same
Array<uint8> faceData;
const_cast<Gfx::Texture*>(texture.getHandle())->download(0, depth, face, faceData);
KTX_CHECK(ktxTexture_SetImageFromMemory(ktxTexture(kTexture), 0, depth, face, faceData.data(), faceData.size()));
}
}
char writer[100]; char writer[100];
snprintf(writer, sizeof(writer), "%s version %s", "SeeleEngine", "0.0.1"); snprintf(writer, sizeof(writer), "%s version %s", "SeeleEngine", "0.0.1");
ktxHashList_AddKVPair(&kTexture->kvDataHead, KTX_WRITER_KEY, ktxHashList_AddKVPair(&ktxHandle->kvDataHead, KTX_WRITER_KEY,
(ktx_uint32_t)strlen(writer) + 1, (ktx_uint32_t)strlen(writer) + 1,
writer); writer);
KTX_CHECK(ktxTexture2_CompressAstc(kTexture, 0));
ktx_uint8_t* texData; ktx_uint8_t* texData;
ktx_size_t texSize; ktx_size_t texSize;
KTX_CHECK(ktxTexture_WriteToMemory(ktxTexture(kTexture), &texData, &texSize)); KTX_CHECK(ktxTexture_WriteToMemory(ktxTexture(ktxHandle), &texData, &texSize));
Array<uint8> rawData(texSize); Array<uint8> rawData(texSize);
std::memcpy(rawData.data(), texData, texSize); std::memcpy(rawData.data(), texData, texSize);
Serialization::save(buffer, rawData); Serialization::save(buffer, rawData);
free(texData);*/ free(texData);
assert(false); // TODO
} }
void TextureAsset::load(ArchiveBuffer& buffer) void TextureAsset::load(ArchiveBuffer& buffer)
{ {
Gfx::PGraphics graphics = buffer.getGraphics(); Gfx::PGraphics graphics = buffer.getGraphics();
Serialization::load(buffer, rawPixels);
Array<uint8> rawData; Array<uint8> rawData;
Serialization::load(buffer, rawData); Serialization::load(buffer, rawData);
ktxTexture2* kTexture;
KTX_CHECK(ktxTexture_CreateFromMemory(rawData.data(), KTX_CHECK(ktxTexture_CreateFromMemory(rawData.data(),
rawData.size(), rawData.size(),
KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT,
(ktxTexture**)&kTexture)); (ktxTexture**)&ktxHandle));
ktx_error_code_e e = ktxTexture2_TranscodeBasis(kTexture, KTX_TTF_BC7_RGBA, 0); //ktx_error_code_e e = ktxTexture2_TranscodeBasis(ktxHandle, KTX_TTF_BC7_RGBA, 0);
assert(e == ktx_error_code_e::KTX_SUCCESS); //assert(e == ktx_error_code_e::KTX_SUCCESS);
TextureCreateInfo createInfo = { TextureCreateInfo createInfo = {
.sourceData = { .sourceData = {
.size = ktxTexture_GetDataSize(ktxTexture(kTexture)), .size = ktxTexture_GetDataSize(ktxTexture(ktxHandle)),
.data = ktxTexture_GetData(ktxTexture(kTexture)), .data = ktxTexture_GetData(ktxTexture(ktxHandle)),
.owner = Gfx::QueueType::TRANSFER, .owner = Gfx::QueueType::TRANSFER,
}, },
.format = (Gfx::SeFormat)kTexture->vkFormat, .format = (Gfx::SeFormat)ktxHandle->vkFormat,
.width = kTexture->baseWidth, .width = ktxHandle->baseWidth,
.height = kTexture->baseHeight, .height = ktxHandle->baseHeight,
.depth = kTexture->baseDepth, .depth = ktxHandle->baseDepth,
.mipLevels = kTexture->numLevels, .mipLevels = ktxHandle->numLevels,
.layers = kTexture->numFaces, .layers = ktxHandle->numFaces,
.elements = kTexture->numLayers, .elements = ktxHandle->numLayers,
.usage = Gfx::SE_IMAGE_USAGE_SAMPLED_BIT, .usage = Gfx::SE_IMAGE_USAGE_SAMPLED_BIT,
}; };
if (kTexture->isCubemap) if (ktxHandle->isCubemap)
{ {
texture = graphics->createTextureCube(createInfo); texture = graphics->createTextureCube(createInfo);
} }
else if (kTexture->isArray) else if (ktxHandle->isArray)
{ {
texture = graphics->createTexture3D(createInfo); texture = graphics->createTexture3D(createInfo);
} }
@@ -113,7 +82,6 @@ void TextureAsset::load(ArchiveBuffer& buffer)
texture = graphics->createTexture2D(createInfo); texture = graphics->createTexture2D(createInfo);
} }
texture->transferOwnership(Gfx::QueueType::GRAPHICS); texture->transferOwnership(Gfx::QueueType::GRAPHICS);
ktxTexture_Destroy(ktxTexture(kTexture));
} }
void TextureAsset::setTexture(Gfx::OTexture _texture) void TextureAsset::setTexture(Gfx::OTexture _texture)
+2 -5
View File
@@ -1,6 +1,7 @@
#pragma once #pragma once
#include "Asset.h" #include "Asset.h"
struct ktxTexture2;
namespace Seele namespace Seele
{ {
DECLARE_NAME_REF(Gfx, Texture) DECLARE_NAME_REF(Gfx, Texture)
@@ -18,14 +19,10 @@ public:
{ {
return texture; return texture;
} }
const Array<uint8>& getRawPixels()
{
return rawPixels;
}
uint32 getWidth(); uint32 getWidth();
uint32 getHeight(); uint32 getHeight();
private: private:
Array<uint8> rawPixels; struct ktxTexture2* ktxHandle;
Gfx::OTexture texture; Gfx::OTexture texture;
friend class TextureLoader; friend class TextureLoader;
}; };
+5 -2
View File
@@ -19,7 +19,8 @@ void Mesh::save(ArchiveBuffer& buffer) const
Serialization::save(buffer, vertexCount); Serialization::save(buffer, vertexCount);
Serialization::save(buffer, indices); Serialization::save(buffer, indices);
Serialization::save(buffer, meshlets); Serialization::save(buffer, meshlets);
Serialization::save(buffer, referencedMaterial->getAssetIdentifier()); Serialization::save(buffer, referencedMaterial->getFolderPath());
Serialization::save(buffer, referencedMaterial->getName());
vertexData->serializeMesh(id, vertexCount, buffer); vertexData->serializeMesh(id, vertexCount, buffer);
} }
@@ -32,9 +33,11 @@ void Mesh::load(ArchiveBuffer& buffer)
vertexData = VertexData::findByTypeName(typeName); vertexData = VertexData::findByTypeName(typeName);
Serialization::load(buffer, indices); Serialization::load(buffer, indices);
Serialization::load(buffer, meshlets); Serialization::load(buffer, meshlets);
std::string refFolder;
Serialization::load(buffer, refFolder);
std::string refId; std::string refId;
Serialization::load(buffer, refId); Serialization::load(buffer, refId);
referencedMaterial = AssetRegistry::findMaterialInstance(refId); referencedMaterial = AssetRegistry::findMaterialInstance(refFolder, refId);
id = vertexData->allocateVertexData(vertexCount); id = vertexData->allocateVertexData(vertexCount);
vertexData->loadMesh(id, indices, meshlets); vertexData->loadMesh(id, indices, meshlets);
vertexData->deserializeMesh(id, buffer); vertexData->deserializeMesh(id, buffer);
@@ -9,8 +9,8 @@ SkyboxRenderPass::SkyboxRenderPass(Gfx::PGraphics graphics, PScene scene)
: RenderPass(graphics, scene) : RenderPass(graphics, scene)
{ {
skybox = Seele::Component::Skybox{ skybox = Seele::Component::Skybox{
.day = AssetRegistry::findTexture("FS000_Day_01")->getTexture().cast<Gfx::TextureCube>(), //.day = AssetRegistry::findTexture("FS000_Day_01")->getTexture().cast<Gfx::TextureCube>(),
.night = AssetRegistry::findTexture("FS000_Night_01")->getTexture().cast<Gfx::TextureCube>(), //.night = AssetRegistry::findTexture("FS000_Night_01")->getTexture().cast<Gfx::TextureCube>(),
.fogColor = Vector(0.1, 0.1, 0.8), .fogColor = Vector(0.1, 0.1, 0.8),
.blendFactor = 0, .blendFactor = 0,
}; };
+41 -13
View File
@@ -22,7 +22,11 @@ StaticMeshVertexData* StaticMeshVertexData::getInstance()
void StaticMeshVertexData::loadPositions(MeshId id, const Array<Vector>& data) void StaticMeshVertexData::loadPositions(MeshId id, const Array<Vector>& data)
{ {
uint64 offset = meshOffsets[id]; uint64 offset;
{
std::unique_lock l(mutex);
offset = meshOffsets[id];
}
assert(offset + data.size() <= head); assert(offset + data.size() <= head);
std::memcpy(positionData.data() + offset, data.data(), data.size() * sizeof(Vector)); std::memcpy(positionData.data() + offset, data.data(), data.size() * sizeof(Vector));
dirty = true; dirty = true;
@@ -30,7 +34,11 @@ void StaticMeshVertexData::loadPositions(MeshId id, const Array<Vector>& data)
void StaticMeshVertexData::loadTexCoords(MeshId id, const Array<Vector2>& data) void StaticMeshVertexData::loadTexCoords(MeshId id, const Array<Vector2>& data)
{ {
uint64 offset = meshOffsets[id]; uint64 offset;
{
std::unique_lock l(mutex);
offset = meshOffsets[id];
}
assert(offset + data.size() <= head); assert(offset + data.size() <= head);
std::memcpy(texCoordsData.data() + offset, data.data(), data.size() * sizeof(Vector2)); std::memcpy(texCoordsData.data() + offset, data.data(), data.size() * sizeof(Vector2));
dirty = true; dirty = true;
@@ -38,7 +46,11 @@ void StaticMeshVertexData::loadTexCoords(MeshId id, const Array<Vector2>& data)
void StaticMeshVertexData::loadNormals(MeshId id, const Array<Vector>& data) void StaticMeshVertexData::loadNormals(MeshId id, const Array<Vector>& data)
{ {
uint64 offset = meshOffsets[id]; uint64 offset;
{
std::unique_lock l(mutex);
offset = meshOffsets[id];
}
assert(offset + data.size() <= head); assert(offset + data.size() <= head);
std::memcpy(normalData.data() + offset, data.data(), data.size() * sizeof(Vector)); std::memcpy(normalData.data() + offset, data.data(), data.size() * sizeof(Vector));
dirty = true; dirty = true;
@@ -46,7 +58,11 @@ void StaticMeshVertexData::loadNormals(MeshId id, const Array<Vector>& data)
void StaticMeshVertexData::loadTangents(MeshId id, const Array<Vector>& data) void StaticMeshVertexData::loadTangents(MeshId id, const Array<Vector>& data)
{ {
uint64 offset = meshOffsets[id]; uint64 offset;
{
std::unique_lock l(mutex);
offset = meshOffsets[id];
}
assert(offset + data.size() <= head); assert(offset + data.size() <= head);
std::memcpy(tangentData.data() + offset, data.data(), data.size() * sizeof(Vector)); std::memcpy(tangentData.data() + offset, data.data(), data.size() * sizeof(Vector));
dirty = true; dirty = true;
@@ -54,7 +70,11 @@ void StaticMeshVertexData::loadTangents(MeshId id, const Array<Vector>& data)
void StaticMeshVertexData::loadBiTangents(MeshId id, const Array<Vector>& data) void StaticMeshVertexData::loadBiTangents(MeshId id, const Array<Vector>& data)
{ {
uint64 offset = meshOffsets[id]; uint64 offset;
{
std::unique_lock l(mutex);
offset = meshOffsets[id];
}
assert(offset + data.size() <= head); assert(offset + data.size() <= head);
std::memcpy(biTangentData.data() + offset, data.data(), data.size() * sizeof(Vector)); std::memcpy(biTangentData.data() + offset, data.data(), data.size() * sizeof(Vector));
dirty = true; dirty = true;
@@ -62,7 +82,11 @@ void StaticMeshVertexData::loadBiTangents(MeshId id, const Array<Vector>& data)
void Seele::StaticMeshVertexData::loadColors(MeshId id, const Array<Vector>& data) void Seele::StaticMeshVertexData::loadColors(MeshId id, const Array<Vector>& data)
{ {
uint64 offset = meshOffsets[id]; uint64 offset;
{
std::unique_lock l(mutex);
offset = meshOffsets[id];
}
assert(offset + data.size() <= head); assert(offset + data.size() <= head);
std::memcpy(colorData.data() + offset, data.data(), data.size() * sizeof(Vector)); std::memcpy(colorData.data() + offset, data.data(), data.size() * sizeof(Vector));
dirty = true; dirty = true;
@@ -70,19 +94,23 @@ void Seele::StaticMeshVertexData::loadColors(MeshId id, const Array<Vector>& dat
void StaticMeshVertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer) void StaticMeshVertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer)
{ {
uint64 offset = meshOffsets[id]; uint64 offset;
{
std::unique_lock l(mutex);
offset = meshOffsets[id];
}
Array<Vector> pos(numVertices); Array<Vector> pos(numVertices);
Array<Vector2> tex(numVertices); Array<Vector2> tex(numVertices);
Array<Vector> nor(numVertices); Array<Vector> nor(numVertices);
Array<Vector> tan(numVertices); Array<Vector> tan(numVertices);
Array<Vector> bit(numVertices); Array<Vector> bit(numVertices);
Array<Vector> col(numVertices); Array<Vector> col(numVertices);
std::copy(positionData.begin() + offset, positionData.begin() + offset + numVertices, pos.begin()); std::memcpy(positionData.data() + offset, pos.data(), numVertices * sizeof(Vector));
std::copy(texCoordsData.begin() + offset, texCoordsData.begin() + offset + numVertices, tex.begin()); std::memcpy(texCoordsData.data() + offset, tex.data(), numVertices * sizeof(Vector2));
std::copy(normalData.begin() + offset, normalData.begin() + offset + numVertices, nor.begin()); std::memcpy(normalData.data() + offset, nor.data(), numVertices * sizeof(Vector));
std::copy(tangentData.begin() + offset, tangentData.begin() + offset + numVertices, tan.begin()); std::memcpy(tangentData.data() + offset, tan.data(), numVertices * sizeof(Vector));
std::copy(biTangentData.begin() + offset, biTangentData.begin() + offset + numVertices, bit.begin()); std::memcpy(biTangentData.data() + offset, bit.data(), numVertices * sizeof(Vector));
std::copy(colorData.begin() + offset, colorData.begin() + offset + numVertices, col.begin()); std::memcpy(colorData.data() + offset, col.data(), numVertices * sizeof(Vector));
Serialization::save(buffer, pos); Serialization::save(buffer, pos);
Serialization::save(buffer, tex); Serialization::save(buffer, tex);
Serialization::save(buffer, nor); Serialization::save(buffer, nor);
@@ -29,6 +29,7 @@ public:
private: private:
virtual void resizeBuffers() override; virtual void resizeBuffers() override;
virtual void updateBuffers() override; virtual void updateBuffers() override;
std::mutex mutex;
Gfx::OShaderBuffer positions; Gfx::OShaderBuffer positions;
Array<Vector> positionData; Array<Vector> positionData;
Gfx::OShaderBuffer texCoords; Gfx::OShaderBuffer texCoords;
+2
View File
@@ -146,6 +146,7 @@ void VertexData::createDescriptors()
void VertexData::loadMesh(MeshId id, Array<uint32> loadedIndices, Array<Meshlet> loadedMeshlets) void VertexData::loadMesh(MeshId id, Array<uint32> loadedIndices, Array<Meshlet> loadedMeshlets)
{ {
std::unique_lock l(vertexDataLock);
meshlets.reserve(meshlets.size() + loadedMeshlets.size()); meshlets.reserve(meshlets.size() + loadedMeshlets.size());
vertexIndices.reserve(vertexIndices.size() + loadedMeshlets.size() * Gfx::numVerticesPerMeshlet); vertexIndices.reserve(vertexIndices.size() + loadedMeshlets.size() * Gfx::numVerticesPerMeshlet);
primitiveIndices.reserve(primitiveIndices.size() + loadedMeshlets.size() * Gfx::numPrimitivesPerMeshlet * 3); primitiveIndices.reserve(primitiveIndices.size() + loadedMeshlets.size() * Gfx::numPrimitivesPerMeshlet * 3);
@@ -225,6 +226,7 @@ void VertexData::loadMesh(MeshId id, Array<uint32> loadedIndices, Array<Meshlet>
MeshId VertexData::allocateVertexData(uint64 numVertices) MeshId VertexData::allocateVertexData(uint64 numVertices)
{ {
std::unique_lock l(vertexDataLock);
MeshId res{ idCounter++ }; MeshId res{ idCounter++ };
meshOffsets[res] = head; meshOffsets[res] = head;
meshVertexCounts[res] = numVertices; meshVertexCounts[res] = numVertices;
+1
View File
@@ -95,6 +95,7 @@ protected:
}; };
std::mutex materialDataLock; std::mutex materialDataLock;
Map<std::string, MaterialData> materialData; Map<std::string, MaterialData> materialData;
std::mutex vertexDataLock;
Map<MeshId, Array<MeshData>> meshData; Map<MeshId, Array<MeshData>> meshData;
Map<MeshId, uint64> meshOffsets; Map<MeshId, uint64> meshOffsets;
Map<MeshId, uint64> meshVertexCounts; Map<MeshId, uint64> meshVertexCounts;
+68 -21
View File
@@ -91,9 +91,9 @@ void ShaderParameter::load(ArchiveBuffer& buffer)
Serialization::load(buffer, binding); Serialization::load(buffer, binding);
} }
FloatParameter::FloatParameter(std::string name, uint32 byteOffset, uint32 binding) FloatParameter::FloatParameter(std::string name, float data, uint32 byteOffset, uint32 binding)
: ShaderParameter(name, byteOffset, binding) : ShaderParameter(name, byteOffset, binding)
, data(0) , data(data)
{ {
output.name = name; output.name = name;
output.type = ExpressionType::FLOAT; output.type = ExpressionType::FLOAT;
@@ -122,12 +122,12 @@ void FloatParameter::updateDescriptorSet(Gfx::PDescriptorSet, uint8* dst)
void FloatParameter::generateDeclaration(std::ofstream& stream) const void FloatParameter::generateDeclaration(std::ofstream& stream) const
{ {
stream << "\tlayout(offset = " << byteOffset << ") float " << key << ";\n"; stream << "\tfloat " << key << ";\n";
} }
VectorParameter::VectorParameter(std::string name, uint32 byteOffset, uint32 binding) VectorParameter::VectorParameter(std::string name, Vector data, uint32 byteOffset, uint32 binding)
: ShaderParameter(name, byteOffset, binding) : ShaderParameter(name, byteOffset, binding)
, data() , data(data)
{ {
output.name = name; output.name = name;
output.type = ExpressionType::FLOAT3; output.type = ExpressionType::FLOAT3;
@@ -144,7 +144,7 @@ void VectorParameter::updateDescriptorSet(Gfx::PDescriptorSet, uint8* dst)
void VectorParameter::generateDeclaration(std::ofstream& stream) const void VectorParameter::generateDeclaration(std::ofstream& stream) const
{ {
stream << "\tlayout(offset = " << byteOffset << ") float3 " << key << ";\n"; stream << "\tfloat3 " << key << ";\n";
} }
void VectorParameter::save(ArchiveBuffer& buffer) const void VectorParameter::save(ArchiveBuffer& buffer) const
@@ -159,8 +159,9 @@ void VectorParameter::load(ArchiveBuffer& buffer)
Serialization::load(buffer, data); Serialization::load(buffer, data);
} }
TextureParameter::TextureParameter(std::string name, uint32 byteOffset, uint32 binding) TextureParameter::TextureParameter(std::string name, PTextureAsset asset, uint32 binding)
: ShaderParameter(name, byteOffset, binding) : ShaderParameter(name, 0, binding)
, data(asset)
{ {
output.name = name; output.name = name;
output.type = ExpressionType::TEXTURE; output.type = ExpressionType::TEXTURE;
@@ -183,19 +184,23 @@ void TextureParameter::generateDeclaration(std::ofstream& stream) const
void TextureParameter::save(ArchiveBuffer& buffer) const void TextureParameter::save(ArchiveBuffer& buffer) const
{ {
ShaderParameter::save(buffer); ShaderParameter::save(buffer);
Serialization::save(buffer, data->getAssetIdentifier()); Serialization::save(buffer, data->getFolderPath());
Serialization::save(buffer, data->getName());
} }
void TextureParameter::load(ArchiveBuffer& buffer) void TextureParameter::load(ArchiveBuffer& buffer)
{ {
ShaderParameter::load(buffer); ShaderParameter::load(buffer);
std::string folder;
Serialization::load(buffer, folder);
std::string filename; std::string filename;
Serialization::load(buffer, filename); Serialization::load(buffer, filename);
data = AssetRegistry::findTexture(filename); data = AssetRegistry::findTexture(folder, filename);
} }
SamplerParameter::SamplerParameter(std::string name, uint32 byteOffset, uint32 binding) SamplerParameter::SamplerParameter(std::string name, Gfx::OSampler sampler, uint32 binding)
: ShaderParameter(name, byteOffset, binding) : ShaderParameter(name, 0, binding)
, data(std::move(sampler))
{ {
output.name = name; output.name = name;
output.type = ExpressionType::SAMPLER; output.type = ExpressionType::SAMPLER;
@@ -227,8 +232,10 @@ void SamplerParameter::load(ArchiveBuffer& buffer)
data = buffer.getGraphics()->createSampler(SamplerCreateInfo{}); data = buffer.getGraphics()->createSampler(SamplerCreateInfo{});
} }
CombinedTextureParameter::CombinedTextureParameter(std::string name, uint32 byteOffset, uint32 binding) CombinedTextureParameter::CombinedTextureParameter(std::string name, PTextureAsset data, Gfx::OSampler sampler, uint32 binding)
: ShaderParameter(name, byteOffset, binding) : ShaderParameter(name, 0, binding)
, data(data)
, sampler(std::move(sampler))
{ {
output.name = name; output.name = name;
output.type = ExpressionType::TEXTURE; output.type = ExpressionType::TEXTURE;
@@ -252,15 +259,18 @@ void CombinedTextureParameter::generateDeclaration(std::ofstream& stream) const
void CombinedTextureParameter::save(ArchiveBuffer& buffer) const void CombinedTextureParameter::save(ArchiveBuffer& buffer) const
{ {
ShaderParameter::save(buffer); ShaderParameter::save(buffer);
Serialization::save(buffer, data->getAssetIdentifier()); Serialization::save(buffer, data->getFolderPath());
Serialization::save(buffer, data->getName());
} }
void CombinedTextureParameter::load(ArchiveBuffer& buffer) void CombinedTextureParameter::load(ArchiveBuffer& buffer)
{ {
ShaderParameter::load(buffer); ShaderParameter::load(buffer);
std::string folder;
Serialization::load(buffer, folder);
std::string filename; std::string filename;
Serialization::load(buffer, filename); Serialization::load(buffer, filename);
data = AssetRegistry::findTexture(filename); data = AssetRegistry::findTexture(folder, filename);
sampler = buffer.getGraphics()->createSampler({}); sampler = buffer.getGraphics()->createSampler({});
} }
@@ -313,7 +323,18 @@ std::string AddExpression::evaluate(Map<std::string, std::string>& varState) con
{ {
std::string varName = fmt::format("exp_{}", key); std::string varName = fmt::format("exp_{}", key);
varState[key] = varName; varState[key] = varName;
return fmt::format("let {} = {} + {};\n", varName, varState[inputs.at("lhs").source], varState[inputs.at("rhs").source]); std::string lhs = inputs.at("lhs").source;
if (varState.contains(lhs))
{
lhs = varState[lhs];
}
std::string rhs = inputs.at("rhs").source;
if (varState.contains(rhs))
{
rhs = varState[rhs];
}
return fmt::format("let {} = {} + {};\n", varName, lhs, rhs);
} }
void AddExpression::save(ArchiveBuffer& buffer) const void AddExpression::save(ArchiveBuffer& buffer) const
@@ -330,7 +351,18 @@ std::string SubExpression::evaluate(Map<std::string, std::string>& varState) con
{ {
std::string varName = fmt::format("exp_{}", key); std::string varName = fmt::format("exp_{}", key);
varState[key] = varName; varState[key] = varName;
return fmt::format("let {} = {} - {};\n", varName, varState[inputs.at("lhs").source], varState[inputs.at("rhs").source]); std::string lhs = inputs.at("lhs").source;
if (varState.contains(lhs))
{
lhs = varState[lhs];
}
std::string rhs = inputs.at("rhs").source;
if (varState.contains(rhs))
{
rhs = varState[rhs];
}
return fmt::format("let {} = {} - {};\n", varName, lhs, rhs);
} }
void SubExpression::save(ArchiveBuffer& buffer) const void SubExpression::save(ArchiveBuffer& buffer) const
@@ -347,7 +379,17 @@ std::string MulExpression::evaluate(Map<std::string, std::string>& varState) con
{ {
std::string varName = fmt::format("exp_{}", key); std::string varName = fmt::format("exp_{}", key);
varState[key] = varName; varState[key] = varName;
return fmt::format("let {} = {} * {};\n", varName, varState[inputs.at("lhs").source], varState[inputs.at("rhs").source]); std::string lhs = inputs.at("lhs").source;
if (varState.contains(lhs))
{
lhs = varState[lhs];
}
std::string rhs = inputs.at("rhs").source;
if (varState.contains(rhs))
{
rhs = varState[rhs];
}
return fmt::format("let {} = {} * {};\n", varName, lhs, rhs);
} }
void MulExpression::save(ArchiveBuffer& buffer) const void MulExpression::save(ArchiveBuffer& buffer) const
@@ -408,13 +450,18 @@ std::string SampleExpression::evaluate(Map<std::string, std::string>& varState)
{ {
std::string varName = fmt::format("exp_{}", key); std::string varName = fmt::format("exp_{}", key);
varState[key] = varName; varState[key] = varName;
std::string texCoords = inputs.at("coords").source;
if (varState.contains(inputs.at("coords").source))
{
texCoords = varState[inputs.at("coords").source];
}
if (inputs.contains("texture")) if (inputs.contains("texture"))
{ {
return fmt::format("let {} = {}.Sample({}, {});\n", varName, varState[inputs.at("texture").source], varState[inputs.at("sampler").source], varState[inputs.at("coords").source]); return fmt::format("let {} = {}.Sample({}, {});\n", varName, varState[inputs.at("texture").source], varState[inputs.at("sampler").source], texCoords);
} }
else else
{ {
return fmt::format("let {} = {}.Sample({});\n", varName, varState[inputs.at("sampler").source], varState[inputs.at("coords").source]); return fmt::format("let {} = {}.Sample({});\n", varName, varState[inputs.at("sampler").source], texCoords);
} }
} }
+7 -6
View File
@@ -69,7 +69,7 @@ struct FloatParameter : public ShaderParameter
static constexpr uint64 IDENTIFIER = 0x01; static constexpr uint64 IDENTIFIER = 0x01;
float data = 0.0f; float data = 0.0f;
FloatParameter() {} FloatParameter() {}
FloatParameter(std::string name, uint32 byteOffset, uint32 binding); FloatParameter(std::string name, float data, uint32 byteOffset, uint32 binding);
virtual ~FloatParameter(); virtual ~FloatParameter();
virtual void updateDescriptorSet(Gfx::PDescriptorSet descriptorSet, uint8* dst) override; virtual void updateDescriptorSet(Gfx::PDescriptorSet descriptorSet, uint8* dst) override;
virtual void generateDeclaration(std::ofstream& stream) const override; virtual void generateDeclaration(std::ofstream& stream) const override;
@@ -83,7 +83,7 @@ struct VectorParameter : public ShaderParameter
static constexpr uint64 IDENTIFIER = 0x02; static constexpr uint64 IDENTIFIER = 0x02;
Vector data = Vector(); Vector data = Vector();
VectorParameter() {} VectorParameter() {}
VectorParameter(std::string name, uint32 byteOffset, uint32 binding); VectorParameter(std::string name, Vector data, uint32 byteOffset, uint32 binding);
virtual ~VectorParameter(); virtual ~VectorParameter();
virtual void updateDescriptorSet(Gfx::PDescriptorSet descriptorSet, uint8* dst) override; virtual void updateDescriptorSet(Gfx::PDescriptorSet descriptorSet, uint8* dst) override;
virtual void generateDeclaration(std::ofstream& stream) const override; virtual void generateDeclaration(std::ofstream& stream) const override;
@@ -97,7 +97,7 @@ struct TextureParameter : public ShaderParameter
static constexpr uint64 IDENTIFIER = 0x04; static constexpr uint64 IDENTIFIER = 0x04;
PTextureAsset data = nullptr; PTextureAsset data = nullptr;
TextureParameter() {} TextureParameter() {}
TextureParameter(std::string name, uint32 byteOffset, uint32 binding); TextureParameter(std::string name, PTextureAsset data, uint32 binding);
virtual ~TextureParameter(); virtual ~TextureParameter();
virtual void updateDescriptorSet(Gfx::PDescriptorSet descriptorSet, uint8* dst) override; virtual void updateDescriptorSet(Gfx::PDescriptorSet descriptorSet, uint8* dst) override;
virtual void generateDeclaration(std::ofstream& stream) const override; virtual void generateDeclaration(std::ofstream& stream) const override;
@@ -112,7 +112,7 @@ struct SamplerParameter : public ShaderParameter
static constexpr uint64 IDENTIFIER = 0x08; static constexpr uint64 IDENTIFIER = 0x08;
Gfx::OSampler data = nullptr; Gfx::OSampler data = nullptr;
SamplerParameter() {} SamplerParameter() {}
SamplerParameter(std::string name, uint32 byteOffset, uint32 binding); SamplerParameter(std::string name, Gfx::OSampler sampler, uint32 binding);
virtual ~SamplerParameter(); virtual ~SamplerParameter();
virtual void updateDescriptorSet(Gfx::PDescriptorSet descriptorSet, uint8* dst) override; virtual void updateDescriptorSet(Gfx::PDescriptorSet descriptorSet, uint8* dst) override;
virtual void generateDeclaration(std::ofstream& stream) const override; virtual void generateDeclaration(std::ofstream& stream) const override;
@@ -125,9 +125,9 @@ struct CombinedTextureParameter : public ShaderParameter
{ {
static constexpr uint64 IDENTIFIER = 0x10; static constexpr uint64 IDENTIFIER = 0x10;
PTextureAsset data = nullptr; PTextureAsset data = nullptr;
Gfx::PSampler sampler = nullptr; Gfx::OSampler sampler = nullptr;
CombinedTextureParameter() {} CombinedTextureParameter() {}
CombinedTextureParameter(std::string name, uint32 byteOffset, uint32 binding); CombinedTextureParameter(std::string name, PTextureAsset data, Gfx::OSampler sampler, uint32 binding);
virtual ~CombinedTextureParameter(); virtual ~CombinedTextureParameter();
virtual void updateDescriptorSet(Gfx::PDescriptorSet descriptorSet, uint8* dst) override; virtual void updateDescriptorSet(Gfx::PDescriptorSet descriptorSet, uint8* dst) override;
virtual void generateDeclaration(std::ofstream& stream) const override; virtual void generateDeclaration(std::ofstream& stream) const override;
@@ -189,6 +189,7 @@ struct SwizzleExpression : public ShaderExpression
static constexpr uint64 IDENTIFIER = 0x15; static constexpr uint64 IDENTIFIER = 0x15;
StaticArray<int32, 4> comp = {-1, -1, -1, -1}; StaticArray<int32, 4> comp = {-1, -1, -1, -1};
SwizzleExpression() {} SwizzleExpression() {}
SwizzleExpression(StaticArray<int32,4> comp) : comp(std::move(comp)) {}
virtual ~SwizzleExpression() {} virtual ~SwizzleExpression() {}
virtual uint64 getIdentifier() const override { return IDENTIFIER; } virtual uint64 getIdentifier() const override { return IDENTIFIER; }
virtual std::string evaluate(Map<std::string, std::string>& varState) const override; virtual std::string evaluate(Map<std::string, std::string>& varState) const override;
+2 -32
View File
@@ -55,41 +55,11 @@ namespace Serialization
} }
static void load(ArchiveBuffer& buffer, Matrix4& mat) static void load(ArchiveBuffer& buffer, Matrix4& mat)
{ {
load(buffer, mat[0][0]); buffer.readBytes(&mat, sizeof(Matrix4));
load(buffer, mat[0][1]);
load(buffer, mat[0][2]);
load(buffer, mat[0][3]);
load(buffer, mat[1][0]);
load(buffer, mat[1][1]);
load(buffer, mat[1][2]);
load(buffer, mat[1][3]);
load(buffer, mat[2][0]);
load(buffer, mat[2][1]);
load(buffer, mat[2][2]);
load(buffer, mat[2][3]);
load(buffer, mat[3][0]);
load(buffer, mat[3][1]);
load(buffer, mat[3][2]);
load(buffer, mat[3][3]);
} }
static void save(ArchiveBuffer& buffer, const Matrix4& mat) static void save(ArchiveBuffer& buffer, const Matrix4& mat)
{ {
save(buffer, mat[0][0]); buffer.writeBytes(&mat, sizeof(Matrix4));
save(buffer, mat[0][1]);
save(buffer, mat[0][2]);
save(buffer, mat[0][3]);
save(buffer, mat[1][0]);
save(buffer, mat[1][1]);
save(buffer, mat[1][2]);
save(buffer, mat[1][3]);
save(buffer, mat[2][0]);
save(buffer, mat[2][1]);
save(buffer, mat[2][2]);
save(buffer, mat[2][3]);
save(buffer, mat[3][0]);
save(buffer, mat[3][1]);
save(buffer, mat[3][2]);
save(buffer, mat[3][3]);
} }
static void load(ArchiveBuffer& buffer, Vector& vec) static void load(ArchiveBuffer& buffer, Vector& vec)
{ {