From 2c1669aab4824e8e55f7b89221b116b3d785abb6 Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Wed, 1 May 2024 14:10:52 +0200 Subject: [PATCH] Reworked mesh asset loading --- res/shaders/lib/BRDF.slang | 2 +- res/shaders/lib/MaterialParameter.slang | 18 +- res/shaders/lib/StaticMeshVertexData.slang | 2 +- src/Editor/Asset/MaterialLoader.cpp | 66 ++- src/Editor/Asset/MeshLoader.cpp | 555 ++++++++++++------ src/Editor/Asset/MeshLoader.h | 6 +- src/Editor/Asset/TextureLoader.cpp | 47 +- src/Editor/main.cpp | 2 +- src/Engine/Asset/AssetRegistry.cpp | 107 ++-- src/Engine/Asset/AssetRegistry.h | 12 +- src/Engine/Asset/MaterialAsset.h | 1 + src/Engine/Asset/MaterialInstanceAsset.cpp | 7 +- src/Engine/Asset/TextureAsset.cpp | 70 +-- src/Engine/Asset/TextureAsset.h | 7 +- src/Engine/Graphics/Mesh.cpp | 7 +- .../Graphics/RenderPass/SkyboxRenderPass.cpp | 4 +- src/Engine/Graphics/StaticMeshVertexData.cpp | 54 +- src/Engine/Graphics/StaticMeshVertexData.h | 1 + src/Engine/Graphics/VertexData.cpp | 2 + src/Engine/Graphics/VertexData.h | 1 + src/Engine/Material/ShaderExpression.cpp | 89 ++- src/Engine/Material/ShaderExpression.h | 13 +- src/Engine/Serialization/Serialization.h | 34 +- 23 files changed, 647 insertions(+), 460 deletions(-) diff --git a/res/shaders/lib/BRDF.slang b/res/shaders/lib/BRDF.slang index 8ceed43..d38e1a4 100644 --- a/res/shaders/lib/BRDF.slang +++ b/res/shaders/lib/BRDF.slang @@ -10,7 +10,7 @@ struct BlinnPhong : IBRDF float3 baseColor; float metallic; float3 normal; - float specular; + float3 specular; float roughness; float sheen; diff --git a/res/shaders/lib/MaterialParameter.slang b/res/shaders/lib/MaterialParameter.slang index 3fc8f2b..130eec5 100644 --- a/res/shaders/lib/MaterialParameter.slang +++ b/res/shaders/lib/MaterialParameter.slang @@ -1,9 +1,11 @@ import Common; +#define MAX_NUM_TEXCOORDS 8 + struct MaterialParameter { float3 position_WS; - float2 texCoords; + float2 texCoords[MAX_NUM_TEXCOORDS]; float3 vertexColor; }; @@ -24,14 +26,17 @@ struct FragmentParameter float3 tangent_WS : TANGENT0; float3 biTangent_WS : TANGENT1; float3 position_WS : POSITION2; - float2 texCoords : TEXCOORD0; float3 vertexColor : COLOR0; + float2 texCoords[MAX_NUM_TEXCOORDS] : TEXCOORD0; MaterialParameter getMaterialParameter() { MaterialParameter result; result.position_WS = position_WS; - result.texCoords = texCoords; result.vertexColor = vertexColor; + for(int i = 0; i < MAX_NUM_TEXCOORDS; ++i) + { + result.texCoords[i] = texCoords[i]; + } return result; } LightingParameter getLightingParameter() @@ -51,8 +56,8 @@ struct VertexAttributes float3 normal_MS; float3 tangent_MS; float3 biTangent_MS; - float2 texCoords; float3 vertexColor; + float2 texCoords[MAX_NUM_TEXCOORDS]; FragmentParameter getParameter(float4x4 transformMatrix) { float4 modelPos = float4(position_MS, 1); @@ -69,8 +74,11 @@ struct VertexAttributes result.biTangent_WS = biTangent_WS; result.position_WS = worldPos.xyz; result.position_CS = clipPos; - result.texCoords = texCoords; result.vertexColor = vertexColor; + for(int i = 0; i < MAX_NUM_TEXCOORDS; ++i) + { + result.texCoords[i] = texCoords[i]; + } return result; } }; diff --git a/res/shaders/lib/StaticMeshVertexData.slang b/res/shaders/lib/StaticMeshVertexData.slang index 9d7d647..bbef82a 100644 --- a/res/shaders/lib/StaticMeshVertexData.slang +++ b/res/shaders/lib/StaticMeshVertexData.slang @@ -11,7 +11,7 @@ struct StaticMeshVertexData : IVertexData 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.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]); return attributes; } diff --git a/src/Editor/Asset/MaterialLoader.cpp b/src/Editor/Asset/MaterialLoader.cpp index 28da073..a3a61fe 100644 --- a/src/Editor/Asset/MaterialLoader.cpp +++ b/src/Editor/Asset/MaterialLoader.cpp @@ -66,75 +66,93 @@ void MaterialLoader::import(MaterialImportArgs args, PMaterialAsset asset) // TODO: ALIGNMENT RULES 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()); + } + OFloatParameter p = new FloatParameter(param.key(), defaultData, uniformBufferOffset, uniformBinding); if(uniformBinding == -1) { layout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = bindingCounter, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER,}); uniformBinding = bindingCounter++; } uniformBufferOffset += 4; - if(defaultValue != param.value().end()) - { - p->data = std::stof(defaultValue.value().get()); - } parameters.add(p->key); expressions.add(std::move(p)); } // TODO: ALIGNMENT RULES 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().c_str()); + } + OVectorParameter p = new VectorParameter(param.key(), defaultData, uniformBufferOffset, uniformBinding); if(uniformBinding == -1) { layout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = bindingCounter, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER,}); uniformBinding = bindingCounter++; } - uniformBufferOffset += 12; - if(defaultValue != param.value().end()) - { - p->data = parseVector(defaultValue.value().get().c_str()); - } + uniformBufferOffset += 16; parameters.add(p->key); expressions.add(std::move(p)); } else if(type.compare("Texture2D") == 0) { - OTextureParameter p = new TextureParameter(param.key(), 0, bindingCounter); - layout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = bindingCounter++, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,}); + PTextureAsset texture; if(defaultValue != param.value().end()) { std::string defaultString = defaultValue.value().get(); - p->data = AssetRegistry::findTexture(defaultString); + auto slashPos = defaultString.rfind("/"); + std::string folder = ""; + if (slashPos != std::string::npos) + { + folder = defaultString.substr(0, slashPos - 1); + defaultString = defaultString.substr(slashPos, defaultString.length()); + } + + texture = AssetRegistry::findTexture(folder, defaultString); } - if(p->data == nullptr) + if(texture == nullptr) { - p->data = AssetRegistry::findTexture(""); // this will return placeholder texture + 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); expressions.add(std::move(p)); } 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,}); - p->data = graphics->createSampler({}); parameters.add(p->key); expressions.add(std::move(p)); } else if (type.compare("Sampler2D") == 0) { - OCombinedTextureParameter p = new CombinedTextureParameter(param.key(), 0, bindingCounter); - layout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = bindingCounter++, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLER, }); - p->sampler = graphics->createSampler({}); + PTextureAsset texture; if (defaultValue != param.value().end()) { std::string defaultString = defaultValue.value().get(); - p->data = AssetRegistry::findTexture(defaultString); + auto slashPos = defaultString.rfind("/"); + std::string folder = ""; + if (slashPos != std::string::npos) + { + folder = defaultString.substr(0, slashPos - 1); + defaultString = defaultString.substr(slashPos, defaultString.length()); + } + + texture = AssetRegistry::findTexture(folder, defaultString); } - if (p->data == nullptr) + if (texture == nullptr) { - p->data = AssetRegistry::findTexture(""); // this will return placeholder texture + 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); expressions.add(std::move(p)); } diff --git a/src/Editor/Asset/MeshLoader.cpp b/src/Editor/Asset/MeshLoader.cpp index 2a61532..da5651d 100644 --- a/src/Editor/Asset/MeshLoader.cpp +++ b/src/Editor/Asset/MeshLoader.cpp @@ -5,11 +5,11 @@ #include "Graphics/StaticMeshVertexData.h" #include "Asset/AssetImporter.h" #include "Asset/MaterialAsset.h" +#include "Graphics/Shader.h" #include #include #include #include -#include #include #include #include @@ -41,172 +41,378 @@ void MeshLoader::importAsset(MeshImportArgs args) import(args, ref); } -void MeshLoader::loadMaterials(const aiScene* scene, const std::string& baseName, const std::filesystem::path& meshDirectory, const std::string& importPath, Array& globalMaterials) + +void MeshLoader::convertAssimpARGB(unsigned char* dst, aiTexel* src, uint32 numPixels) { - using json = nlohmann::json; - for(uint32 i = 0; i < scene->mNumMaterials; ++i) + for (uint32 i = 0; i < numPixels; ++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; - uint32 baseColorIndex = 0; - uint32 normalIndex = 0; - int32 codeIndex = -1; - if(material->GetTexture(aiTextureType_DIFFUSE, 0, &texPath) == AI_SUCCESS) + 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& 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 { - auto texFilename = std::filesystem::path(texPath.C_Str()).stem(); - if (texFilename == "white") + if (tex->mHeight == 0) { - matCode["code"].push_back( - { - { "exp", "Const" }, - { "value", "float3(1, 1, 1)"} - } - ); - baseColorIndex = ++codeIndex; + // 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 { - 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; + // 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; } } - 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; - AssetImporter::importMaterial(MaterialImportArgs{ - .filePath = meshDirectory / outMatFilename, + std::cout << "Loading model texture " << texPath.string() << std::endl; + AssetImporter::importTexture(TextureImportArgs{ + .filePath = texPath, .importPath = importPath, }); - std::string materialAsset; - if (importPath.empty()) + 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& textures, const std::string& baseName, const std::filesystem::path& meshDirectory, const std::string& importPath, Array& globalMaterials) +{ + for(uint32 i = 0; i < scene->mNumMaterials; ++i) + { + aiMaterial* material = scene->mMaterials[i]; + aiString texPath; + std::string materialName = fmt::format("M{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 + materialName.erase(std::remove(materialName.begin(), materialName.end(), '-'), materialName.end()); // dots break adding the .asset extension later + Gfx::ODescriptorLayout materialLayout = graphics->createDescriptorLayout("pMaterial"); + Array expressions; + Array parameters; + + materialLayout->addDescriptorBinding(Gfx::DescriptorBinding{ + .binding = 0, + .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::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, + }); + texture = AssetRegistry::findTexture(importPath, texFilename.stem().string()); + } + else + { + std::cout << "couldnt find " << texPath.C_Str() << std::endl; + } + 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::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) { - materialAsset = matCode["name"].get(); + addTextureParameter(KEY_DIFFUSE_TEXTURE, aiTextureType_DIFFUSE, i, outputDiffuse); } - else + uint32 numSpecular = material->GetTextureCount(aiTextureType_SPECULAR); + for (uint32 i = 0; i < numSpecular; ++i) { - materialAsset = fmt::format("{0}/{1}", importPath, matCode["name"].get()); + addTextureParameter(KEY_SPECULAR_TEXTURE, aiTextureType_SPECULAR, i, outputSpecular); } - PMaterialAsset baseMat = AssetRegistry::findMaterial(materialAsset); + + 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{ .name = fmt::format("{0}_Inst_0", baseMat->getName()), .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 &meshNodes) void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array& materials, Array& globalMeshes, Component::Collider& collider) { +//#pragma omp parallel for for (uint32 meshIndex = 0; meshIndex < scene->mNumMeshes; ++meshIndex) { aiMesh* mesh = scene->mMeshes[meshIndex]; @@ -242,7 +449,7 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array colors(mesh->mNumVertices); StaticMeshVertexData* vertexData = StaticMeshVertexData::getInstance(); -#pragma omp parallel for +//#pragma omp parallel for for (int32 i = 0; i < mesh->mNumVertices; ++i) { 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 ArrayloadColors(id, colors); Array indices(mesh->mNumFaces * 3); -#pragma omp parallel for +//#pragma omp parallel for for (int32 faceIndex = 0; faceIndex < mesh->mNumFaces; ++faceIndex) { indices[faceIndex * 3 + 0] = mesh->mFaces[faceIndex].mIndices[0]; @@ -308,49 +515,6 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const ArraymNumTextures; ++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) { @@ -390,9 +554,10 @@ void MeshLoader::import(MeshImportArgs args, PMeshAsset meshAsset) const aiScene *scene = importer.ApplyPostProcessing(aiProcess_CalcTangentSpace); std::cout << importer.GetErrorString() << std::endl; + Map textures; + loadTextures(scene, args.filePath.parent_path(), args.importPath, textures); Array globalMaterials(scene->mNumMaterials); - loadTextures(scene, args.filePath.parent_path(), args.importPath); - loadMaterials(scene, args.filePath.stem().string(), args.filePath.parent_path(), args.importPath, globalMaterials); + loadMaterials(scene, textures, args.filePath.stem().string(), args.filePath.parent_path(), args.importPath, globalMaterials); Array globalMeshes(scene->mNumMeshes); Component::Collider collider; diff --git a/src/Editor/Asset/MeshLoader.h b/src/Editor/Asset/MeshLoader.h index efbfaa0..7e21071 100644 --- a/src/Editor/Asset/MeshLoader.h +++ b/src/Editor/Asset/MeshLoader.h @@ -1,6 +1,7 @@ #pragma once #include "MinimalEngine.h" #include "Containers/List.h" +#include "Containers/Map.h" #include "Component/Collider.h" #include @@ -12,6 +13,7 @@ namespace Seele DECLARE_REF(Mesh) DECLARE_REF(MeshAsset) DECLARE_REF(MaterialInstanceAsset) +DECLARE_REF(TextureAsset) DECLARE_NAME_REF(Gfx, Graphics) struct MeshImportArgs { @@ -25,8 +27,8 @@ public: ~MeshLoader(); void importAsset(MeshImportArgs args); private: - void loadMaterials(const aiScene* scene, const std::string& baseName, const std::filesystem::path& meshDirectory, const std::string& importPath, Array& globalMaterials); - void loadTextures(const aiScene* scene, const std::filesystem::path& meshDirectory, const std::string& importPath); + void loadTextures(const aiScene* scene, const std::filesystem::path& meshDirectory, const std::string& importPath, Map& textures); + void loadMaterials(const aiScene* scene, const Map& textures, const std::string& baseName, const std::filesystem::path& meshDirectory, const std::string& importPath, Array& globalMaterials); void loadGlobalMeshes(const aiScene* scene, const Array& materials, Array& globalMeshes, Component::Collider& collider); void convertAssimpARGB(unsigned char* dst, aiTexel* src, uint32 numPixels); diff --git a/src/Editor/Asset/TextureLoader.cpp b/src/Editor/Asset/TextureLoader.cpp index 808a378..2f99f72 100644 --- a/src/Editor/Asset/TextureLoader.cpp +++ b/src/Editor/Asset/TextureLoader.cpp @@ -35,10 +35,11 @@ TextureLoader::~TextureLoader() void TextureLoader::importAsset(TextureImportArgs args) { - std::filesystem::path assetPath = args.filePath.filename(); - assetPath.replace_extension("asset"); + std::string str = args.filePath.filename().string(); + 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; asset->setStatus(Asset::Status::Loading); AssetRegistry::get().registerTexture(std::move(asset)); @@ -55,14 +56,15 @@ void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset) int totalWidth = 0, totalHeight = 0, n = 0; unsigned char* data = stbi_load(args.filePath.string().c_str(), &totalWidth, &totalHeight, &n, 4); ktxTexture2* kTexture = nullptr; - ktxTextureCreateInfo createInfo; - createInfo.vkFormat = VK_FORMAT_R8G8B8A8_UNORM; - createInfo.baseDepth = 1; - createInfo.numLevels = 1; - createInfo.numLayers = 1; - createInfo.isArray = false; - createInfo.generateMipmaps = false; - + ktxTextureCreateInfo createInfo = { + .vkFormat = VK_FORMAT_R8G8B8A8_UNORM, + .baseDepth = 1, + .numLevels = 1, + .numLayers = 1, + .isArray = false, + .generateMipmaps = false, + }; + if (args.type == TextureImportType::TEXTURE_CUBEMAP) { uint32 faceWidth = totalWidth / 4; @@ -113,18 +115,17 @@ void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset) ktxTexture_SetImageFromMemory(ktxTexture(kTexture), 0, 0, 0, data, totalWidth * totalHeight * 4 * sizeof(unsigned char)); } - std::cout << "starting compression of " << textureAsset->getAssetIdentifier() << std::endl; ktxBasisParams params2 = { .structSize = sizeof(ktxBasisParams), .uastc = false, .threadCount = std::thread::hardware_concurrency(), .compressionLevel = 0, - .qualityLevel = 0, + .qualityLevel = 1, }; - ktx_error_code_e error = ktxTexture2_CompressBasisEx(kTexture, ¶ms2); - assert(error == KTX_SUCCESS); + //ktx_error_code_e error = ktxTexture2_CompressBasisEx(kTexture, ¶ms2); + //assert(error == KTX_SUCCESS); ktx_uint8_t* dest; ktx_size_t size; @@ -134,13 +135,9 @@ void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset) std::memcpy(memory.data(), dest, size); free(dest); - Array rawPixels(totalWidth * totalHeight * 4); - std::memcpy(rawPixels.data(), data, rawPixels.size()); - stbi_image_free(data); ArchiveBuffer temp(graphics); - Serialization::save(temp, rawPixels); Serialization::save(temp, memory); temp.rewind(); textureAsset->load(temp); @@ -148,16 +145,6 @@ void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset) { return; } - auto stream = AssetRegistry::createWriteStream((std::filesystem::path(textureAsset->folderPath) / textureAsset->getName()).replace_extension("asset").string(), std::ios::binary); - ArchiveBuffer archive; - 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; + AssetRegistry::get().saveAsset(textureAsset, TextureAsset::IDENTIFIER, textureAsset->getFolderPath(), textureAsset->getName()); } diff --git a/src/Editor/main.cpp b/src/Editor/main.cpp index 12fe82d..3d6b53d 100644 --- a/src/Editor/main.cpp +++ b/src/Editor/main.cpp @@ -59,7 +59,7 @@ int main() { .filePath = sourcePath / "import/models/cube.fbx", }); 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" }); WindowCreateInfo mainWindowInfo; diff --git a/src/Engine/Asset/AssetRegistry.cpp b/src/Engine/Asset/AssetRegistry.cpp index e17ce64..c6c819e 100644 --- a/src/Engine/Asset/AssetRegistry.cpp +++ b/src/Engine/Asset/AssetRegistry.cpp @@ -26,69 +26,54 @@ void AssetRegistry::init(std::filesystem::path rootFolder, Gfx::PGraphics graphi 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; - std::string fileName = filePath; - size_t slashLoc = filePath.rfind("/"); - if(slashLoc != std::string::npos) + if (!folderPath.empty()) { - folder = get().getOrCreateFolder(filePath.substr(0, slashLoc)); - fileName = filePath.substr(slashLoc+1, filePath.size()); + folder = get().getOrCreateFolder(folderPath); } - 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; - std::string fileName = filePath; - size_t slashLoc = filePath.rfind("/"); - if (slashLoc != std::string::npos) + if (!folderPath.empty()) { - folder = get().getOrCreateFolder(filePath.substr(0, slashLoc)); - fileName = filePath.substr(slashLoc + 1, filePath.size()); + folder = get().getOrCreateFolder(folderPath); } - 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; - std::string fileName = filePath; - size_t slashLoc = filePath.rfind("/"); - if(slashLoc != std::string::npos) + if (!folderPath.empty()) { - folder = get().getOrCreateFolder(filePath.substr(0, slashLoc)); - fileName = filePath.substr(slashLoc+1, filePath.size()); + folder = get().getOrCreateFolder(folderPath); } - 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; - std::string fileName = filePath; - size_t slashLoc = filePath.rfind("/"); - if (slashLoc != std::string::npos) + if (!folderPath.empty()) { - folder = get().getOrCreateFolder(filePath.substr(0, slashLoc)); - fileName = filePath.substr(slashLoc + 1, filePath.size()); + folder = get().getOrCreateFolder(folderPath); } - 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; - std::string fileName = filePath; - size_t slashLoc = filePath.rfind("/"); - if (slashLoc != std::string::npos) + if (!folderPath.empty()) { - folder = get().getOrCreateFolder(filePath.substr(0, slashLoc)); - fileName = filePath.substr(slashLoc + 1, filePath.size()); + folder = get().getOrCreateFolder(folderPath); } - 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) @@ -126,6 +111,31 @@ void AssetRegistry::saveRegistry() 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) { @@ -318,7 +328,7 @@ void AssetRegistry::saveAsset(PAsset asset, uint64 identifier, const std::filesy if (name.empty()) 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); ArchiveBuffer buffer(graphics); // write identifier @@ -367,31 +377,6 @@ void AssetRegistry::registerMaterialInstance(OMaterialInstanceAsset 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) { diff --git a/src/Engine/Asset/AssetRegistry.h b/src/Engine/Asset/AssetRegistry.h index 188ab43..0005e1b 100644 --- a/src/Engine/Asset/AssetRegistry.h +++ b/src/Engine/Asset/AssetRegistry.h @@ -21,11 +21,11 @@ public: static std::filesystem::path getRootFolder(); - static PMeshAsset findMesh(const std::string& filePath); - static PTextureAsset findTexture(const std::string& filePath); - static PFontAsset findFont(const std::string& name); - static PMaterialAsset findMaterial(const std::string& filePath); - static PMaterialInstanceAsset findMaterialInstance(const std::string& filePath); + static PMeshAsset findMesh(std::string_view folderPath, std::string_view filePath); + static PTextureAsset findTexture(std::string_view folderPath, std::string_view filePath); + static PFontAsset findFont(std::string_view folderPath, std::string_view name); + static PMaterialAsset findMaterial(std::string_view folderPath, std::string_view 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::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(); }; - AssetFolder* getOrCreateFolder(std::string foldername); + AssetFolder* getOrCreateFolder(std::string_view foldername); AssetRegistry(); static AssetRegistry* getInstance(); private: diff --git a/src/Engine/Asset/MaterialAsset.h b/src/Engine/Asset/MaterialAsset.h index 7aab9aa..386deb4 100644 --- a/src/Engine/Asset/MaterialAsset.h +++ b/src/Engine/Asset/MaterialAsset.h @@ -24,6 +24,7 @@ public: private: OMaterial material; friend class MaterialLoader; + friend class MeshLoader; }; DEFINE_REF(MaterialAsset) } // namespace Seele diff --git a/src/Engine/Asset/MaterialInstanceAsset.cpp b/src/Engine/Asset/MaterialInstanceAsset.cpp index aa39b81..4f4c0d2 100644 --- a/src/Engine/Asset/MaterialInstanceAsset.cpp +++ b/src/Engine/Asset/MaterialInstanceAsset.cpp @@ -22,16 +22,19 @@ MaterialInstanceAsset::~MaterialInstanceAsset() void MaterialInstanceAsset::save(ArchiveBuffer& buffer) const { - Serialization::save(buffer, baseMaterial->getAssetIdentifier()); + Serialization::save(buffer, baseMaterial->getFolderPath()); + Serialization::save(buffer, baseMaterial->getName()); material->save(buffer); } void MaterialInstanceAsset::load(ArchiveBuffer& buffer) { + std::string folder; + Serialization::load(buffer, folder); std::string id; Serialization::load(buffer, id); material = new MaterialInstance(); material->load(buffer); - baseMaterial = AssetRegistry::findMaterial(id); + baseMaterial = AssetRegistry::findMaterial(folder, id); material->setBaseMaterial(baseMaterial); } diff --git a/src/Engine/Asset/TextureAsset.cpp b/src/Engine/Asset/TextureAsset.cpp index 7923817..d82e5d3 100644 --- a/src/Engine/Asset/TextureAsset.cpp +++ b/src/Engine/Asset/TextureAsset.cpp @@ -20,91 +20,60 @@ TextureAsset::TextureAsset(std::string_view folderPath, std::string_view name) 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 faceData; - const_cast(texture.getHandle())->download(0, depth, face, faceData); - KTX_CHECK(ktxTexture_SetImageFromMemory(ktxTexture(kTexture), 0, depth, face, faceData.data(), faceData.size())); - } - } char writer[100]; 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, writer); - KTX_CHECK(ktxTexture2_CompressAstc(kTexture, 0)); - ktx_uint8_t* texData; ktx_size_t texSize; - KTX_CHECK(ktxTexture_WriteToMemory(ktxTexture(kTexture), &texData, &texSize)); + KTX_CHECK(ktxTexture_WriteToMemory(ktxTexture(ktxHandle), &texData, &texSize)); Array rawData(texSize); std::memcpy(rawData.data(), texData, texSize); Serialization::save(buffer, rawData); - free(texData);*/ - assert(false); // TODO + free(texData); } void TextureAsset::load(ArchiveBuffer& buffer) { Gfx::PGraphics graphics = buffer.getGraphics(); - Serialization::load(buffer, rawPixels); Array rawData; Serialization::load(buffer, rawData); - ktxTexture2* kTexture; KTX_CHECK(ktxTexture_CreateFromMemory(rawData.data(), rawData.size(), KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, - (ktxTexture**)&kTexture)); + (ktxTexture**)&ktxHandle)); - ktx_error_code_e e = ktxTexture2_TranscodeBasis(kTexture, KTX_TTF_BC7_RGBA, 0); - assert(e == ktx_error_code_e::KTX_SUCCESS); + //ktx_error_code_e e = ktxTexture2_TranscodeBasis(ktxHandle, KTX_TTF_BC7_RGBA, 0); + //assert(e == ktx_error_code_e::KTX_SUCCESS); TextureCreateInfo createInfo = { .sourceData = { - .size = ktxTexture_GetDataSize(ktxTexture(kTexture)), - .data = ktxTexture_GetData(ktxTexture(kTexture)), + .size = ktxTexture_GetDataSize(ktxTexture(ktxHandle)), + .data = ktxTexture_GetData(ktxTexture(ktxHandle)), .owner = Gfx::QueueType::TRANSFER, }, - .format = (Gfx::SeFormat)kTexture->vkFormat, - .width = kTexture->baseWidth, - .height = kTexture->baseHeight, - .depth = kTexture->baseDepth, - .mipLevels = kTexture->numLevels, - .layers = kTexture->numFaces, - .elements = kTexture->numLayers, + .format = (Gfx::SeFormat)ktxHandle->vkFormat, + .width = ktxHandle->baseWidth, + .height = ktxHandle->baseHeight, + .depth = ktxHandle->baseDepth, + .mipLevels = ktxHandle->numLevels, + .layers = ktxHandle->numFaces, + .elements = ktxHandle->numLayers, .usage = Gfx::SE_IMAGE_USAGE_SAMPLED_BIT, }; - if (kTexture->isCubemap) + if (ktxHandle->isCubemap) { texture = graphics->createTextureCube(createInfo); } - else if (kTexture->isArray) + else if (ktxHandle->isArray) { texture = graphics->createTexture3D(createInfo); } @@ -113,7 +82,6 @@ void TextureAsset::load(ArchiveBuffer& buffer) texture = graphics->createTexture2D(createInfo); } texture->transferOwnership(Gfx::QueueType::GRAPHICS); - ktxTexture_Destroy(ktxTexture(kTexture)); } void TextureAsset::setTexture(Gfx::OTexture _texture) diff --git a/src/Engine/Asset/TextureAsset.h b/src/Engine/Asset/TextureAsset.h index af275dc..7c4dab3 100644 --- a/src/Engine/Asset/TextureAsset.h +++ b/src/Engine/Asset/TextureAsset.h @@ -1,6 +1,7 @@ #pragma once #include "Asset.h" +struct ktxTexture2; namespace Seele { DECLARE_NAME_REF(Gfx, Texture) @@ -18,14 +19,10 @@ public: { return texture; } - const Array& getRawPixels() - { - return rawPixels; - } uint32 getWidth(); uint32 getHeight(); private: - Array rawPixels; + struct ktxTexture2* ktxHandle; Gfx::OTexture texture; friend class TextureLoader; }; diff --git a/src/Engine/Graphics/Mesh.cpp b/src/Engine/Graphics/Mesh.cpp index b1553cb..63d6904 100644 --- a/src/Engine/Graphics/Mesh.cpp +++ b/src/Engine/Graphics/Mesh.cpp @@ -19,7 +19,8 @@ void Mesh::save(ArchiveBuffer& buffer) const Serialization::save(buffer, vertexCount); Serialization::save(buffer, indices); Serialization::save(buffer, meshlets); - Serialization::save(buffer, referencedMaterial->getAssetIdentifier()); + Serialization::save(buffer, referencedMaterial->getFolderPath()); + Serialization::save(buffer, referencedMaterial->getName()); vertexData->serializeMesh(id, vertexCount, buffer); } @@ -32,9 +33,11 @@ void Mesh::load(ArchiveBuffer& buffer) vertexData = VertexData::findByTypeName(typeName); Serialization::load(buffer, indices); Serialization::load(buffer, meshlets); + std::string refFolder; + Serialization::load(buffer, refFolder); std::string refId; Serialization::load(buffer, refId); - referencedMaterial = AssetRegistry::findMaterialInstance(refId); + referencedMaterial = AssetRegistry::findMaterialInstance(refFolder, refId); id = vertexData->allocateVertexData(vertexCount); vertexData->loadMesh(id, indices, meshlets); vertexData->deserializeMesh(id, buffer); diff --git a/src/Engine/Graphics/RenderPass/SkyboxRenderPass.cpp b/src/Engine/Graphics/RenderPass/SkyboxRenderPass.cpp index c59c44a..050de4f 100644 --- a/src/Engine/Graphics/RenderPass/SkyboxRenderPass.cpp +++ b/src/Engine/Graphics/RenderPass/SkyboxRenderPass.cpp @@ -9,8 +9,8 @@ SkyboxRenderPass::SkyboxRenderPass(Gfx::PGraphics graphics, PScene scene) : RenderPass(graphics, scene) { skybox = Seele::Component::Skybox{ - .day = AssetRegistry::findTexture("FS000_Day_01")->getTexture().cast(), - .night = AssetRegistry::findTexture("FS000_Night_01")->getTexture().cast(), + //.day = AssetRegistry::findTexture("FS000_Day_01")->getTexture().cast(), + //.night = AssetRegistry::findTexture("FS000_Night_01")->getTexture().cast(), .fogColor = Vector(0.1, 0.1, 0.8), .blendFactor = 0, }; diff --git a/src/Engine/Graphics/StaticMeshVertexData.cpp b/src/Engine/Graphics/StaticMeshVertexData.cpp index f1d3f4f..fa3c011 100644 --- a/src/Engine/Graphics/StaticMeshVertexData.cpp +++ b/src/Engine/Graphics/StaticMeshVertexData.cpp @@ -22,7 +22,11 @@ StaticMeshVertexData* StaticMeshVertexData::getInstance() void StaticMeshVertexData::loadPositions(MeshId id, const Array& data) { - uint64 offset = meshOffsets[id]; + uint64 offset; + { + std::unique_lock l(mutex); + offset = meshOffsets[id]; + } assert(offset + data.size() <= head); std::memcpy(positionData.data() + offset, data.data(), data.size() * sizeof(Vector)); dirty = true; @@ -30,7 +34,11 @@ void StaticMeshVertexData::loadPositions(MeshId id, const Array& data) void StaticMeshVertexData::loadTexCoords(MeshId id, const Array& data) { - uint64 offset = meshOffsets[id]; + uint64 offset; + { + std::unique_lock l(mutex); + offset = meshOffsets[id]; + } assert(offset + data.size() <= head); std::memcpy(texCoordsData.data() + offset, data.data(), data.size() * sizeof(Vector2)); dirty = true; @@ -38,7 +46,11 @@ void StaticMeshVertexData::loadTexCoords(MeshId id, const Array& data) void StaticMeshVertexData::loadNormals(MeshId id, const Array& data) { - uint64 offset = meshOffsets[id]; + uint64 offset; + { + std::unique_lock l(mutex); + offset = meshOffsets[id]; + } assert(offset + data.size() <= head); std::memcpy(normalData.data() + offset, data.data(), data.size() * sizeof(Vector)); dirty = true; @@ -46,7 +58,11 @@ void StaticMeshVertexData::loadNormals(MeshId id, const Array& data) void StaticMeshVertexData::loadTangents(MeshId id, const Array& data) { - uint64 offset = meshOffsets[id]; + uint64 offset; + { + std::unique_lock l(mutex); + offset = meshOffsets[id]; + } assert(offset + data.size() <= head); std::memcpy(tangentData.data() + offset, data.data(), data.size() * sizeof(Vector)); dirty = true; @@ -54,7 +70,11 @@ void StaticMeshVertexData::loadTangents(MeshId id, const Array& data) void StaticMeshVertexData::loadBiTangents(MeshId id, const Array& data) { - uint64 offset = meshOffsets[id]; + uint64 offset; + { + std::unique_lock l(mutex); + offset = meshOffsets[id]; + } assert(offset + data.size() <= head); std::memcpy(biTangentData.data() + offset, data.data(), data.size() * sizeof(Vector)); dirty = true; @@ -62,7 +82,11 @@ void StaticMeshVertexData::loadBiTangents(MeshId id, const Array& data) void Seele::StaticMeshVertexData::loadColors(MeshId id, const Array& data) { - uint64 offset = meshOffsets[id]; + uint64 offset; + { + std::unique_lock l(mutex); + offset = meshOffsets[id]; + } assert(offset + data.size() <= head); std::memcpy(colorData.data() + offset, data.data(), data.size() * sizeof(Vector)); dirty = true; @@ -70,19 +94,23 @@ void Seele::StaticMeshVertexData::loadColors(MeshId id, const Array& dat void StaticMeshVertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer) { - uint64 offset = meshOffsets[id]; + uint64 offset; + { + std::unique_lock l(mutex); + offset = meshOffsets[id]; + } Array pos(numVertices); Array tex(numVertices); Array nor(numVertices); Array tan(numVertices); Array bit(numVertices); Array col(numVertices); - std::copy(positionData.begin() + offset, positionData.begin() + offset + numVertices, pos.begin()); - std::copy(texCoordsData.begin() + offset, texCoordsData.begin() + offset + numVertices, tex.begin()); - std::copy(normalData.begin() + offset, normalData.begin() + offset + numVertices, nor.begin()); - std::copy(tangentData.begin() + offset, tangentData.begin() + offset + numVertices, tan.begin()); - std::copy(biTangentData.begin() + offset, biTangentData.begin() + offset + numVertices, bit.begin()); - std::copy(colorData.begin() + offset, colorData.begin() + offset + numVertices, col.begin()); + std::memcpy(positionData.data() + offset, pos.data(), numVertices * sizeof(Vector)); + std::memcpy(texCoordsData.data() + offset, tex.data(), numVertices * sizeof(Vector2)); + std::memcpy(normalData.data() + offset, nor.data(), numVertices * sizeof(Vector)); + std::memcpy(tangentData.data() + offset, tan.data(), numVertices * sizeof(Vector)); + std::memcpy(biTangentData.data() + offset, bit.data(), numVertices * sizeof(Vector)); + std::memcpy(colorData.data() + offset, col.data(), numVertices * sizeof(Vector)); Serialization::save(buffer, pos); Serialization::save(buffer, tex); Serialization::save(buffer, nor); diff --git a/src/Engine/Graphics/StaticMeshVertexData.h b/src/Engine/Graphics/StaticMeshVertexData.h index 75caf3d..b135a84 100644 --- a/src/Engine/Graphics/StaticMeshVertexData.h +++ b/src/Engine/Graphics/StaticMeshVertexData.h @@ -29,6 +29,7 @@ public: private: virtual void resizeBuffers() override; virtual void updateBuffers() override; + std::mutex mutex; Gfx::OShaderBuffer positions; Array positionData; Gfx::OShaderBuffer texCoords; diff --git a/src/Engine/Graphics/VertexData.cpp b/src/Engine/Graphics/VertexData.cpp index 4bf276a..453e9de 100644 --- a/src/Engine/Graphics/VertexData.cpp +++ b/src/Engine/Graphics/VertexData.cpp @@ -146,6 +146,7 @@ void VertexData::createDescriptors() void VertexData::loadMesh(MeshId id, Array loadedIndices, Array loadedMeshlets) { + std::unique_lock l(vertexDataLock); meshlets.reserve(meshlets.size() + loadedMeshlets.size()); vertexIndices.reserve(vertexIndices.size() + loadedMeshlets.size() * Gfx::numVerticesPerMeshlet); primitiveIndices.reserve(primitiveIndices.size() + loadedMeshlets.size() * Gfx::numPrimitivesPerMeshlet * 3); @@ -225,6 +226,7 @@ void VertexData::loadMesh(MeshId id, Array loadedIndices, Array MeshId VertexData::allocateVertexData(uint64 numVertices) { + std::unique_lock l(vertexDataLock); MeshId res{ idCounter++ }; meshOffsets[res] = head; meshVertexCounts[res] = numVertices; diff --git a/src/Engine/Graphics/VertexData.h b/src/Engine/Graphics/VertexData.h index 28898a2..8ee608c 100644 --- a/src/Engine/Graphics/VertexData.h +++ b/src/Engine/Graphics/VertexData.h @@ -95,6 +95,7 @@ protected: }; std::mutex materialDataLock; Map materialData; + std::mutex vertexDataLock; Map> meshData; Map meshOffsets; Map meshVertexCounts; diff --git a/src/Engine/Material/ShaderExpression.cpp b/src/Engine/Material/ShaderExpression.cpp index 6c48c68..3876433 100644 --- a/src/Engine/Material/ShaderExpression.cpp +++ b/src/Engine/Material/ShaderExpression.cpp @@ -91,9 +91,9 @@ void ShaderParameter::load(ArchiveBuffer& buffer) 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) - , data(0) + , data(data) { output.name = name; output.type = ExpressionType::FLOAT; @@ -122,12 +122,12 @@ void FloatParameter::updateDescriptorSet(Gfx::PDescriptorSet, uint8* dst) 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) - , data() + , data(data) { output.name = name; output.type = ExpressionType::FLOAT3; @@ -144,7 +144,7 @@ void VectorParameter::updateDescriptorSet(Gfx::PDescriptorSet, uint8* dst) void VectorParameter::generateDeclaration(std::ofstream& stream) const { - stream << "\tlayout(offset = " << byteOffset << ") float3 " << key << ";\n"; + stream << "\tfloat3 " << key << ";\n"; } void VectorParameter::save(ArchiveBuffer& buffer) const @@ -159,8 +159,9 @@ void VectorParameter::load(ArchiveBuffer& buffer) Serialization::load(buffer, data); } -TextureParameter::TextureParameter(std::string name, uint32 byteOffset, uint32 binding) - : ShaderParameter(name, byteOffset, binding) +TextureParameter::TextureParameter(std::string name, PTextureAsset asset, uint32 binding) + : ShaderParameter(name, 0, binding) + , data(asset) { output.name = name; output.type = ExpressionType::TEXTURE; @@ -183,19 +184,23 @@ void TextureParameter::generateDeclaration(std::ofstream& stream) const void TextureParameter::save(ArchiveBuffer& buffer) const { ShaderParameter::save(buffer); - Serialization::save(buffer, data->getAssetIdentifier()); + Serialization::save(buffer, data->getFolderPath()); + Serialization::save(buffer, data->getName()); } void TextureParameter::load(ArchiveBuffer& buffer) { ShaderParameter::load(buffer); + std::string folder; + Serialization::load(buffer, folder); std::string filename; Serialization::load(buffer, filename); - data = AssetRegistry::findTexture(filename); + data = AssetRegistry::findTexture(folder, filename); } -SamplerParameter::SamplerParameter(std::string name, uint32 byteOffset, uint32 binding) - : ShaderParameter(name, byteOffset, binding) +SamplerParameter::SamplerParameter(std::string name, Gfx::OSampler sampler, uint32 binding) + : ShaderParameter(name, 0, binding) + , data(std::move(sampler)) { output.name = name; output.type = ExpressionType::SAMPLER; @@ -227,8 +232,10 @@ void SamplerParameter::load(ArchiveBuffer& buffer) data = buffer.getGraphics()->createSampler(SamplerCreateInfo{}); } -CombinedTextureParameter::CombinedTextureParameter(std::string name, uint32 byteOffset, uint32 binding) - : ShaderParameter(name, byteOffset, binding) +CombinedTextureParameter::CombinedTextureParameter(std::string name, PTextureAsset data, Gfx::OSampler sampler, uint32 binding) + : ShaderParameter(name, 0, binding) + , data(data) + , sampler(std::move(sampler)) { output.name = name; output.type = ExpressionType::TEXTURE; @@ -252,15 +259,18 @@ void CombinedTextureParameter::generateDeclaration(std::ofstream& stream) const void CombinedTextureParameter::save(ArchiveBuffer& buffer) const { ShaderParameter::save(buffer); - Serialization::save(buffer, data->getAssetIdentifier()); + Serialization::save(buffer, data->getFolderPath()); + Serialization::save(buffer, data->getName()); } void CombinedTextureParameter::load(ArchiveBuffer& buffer) { ShaderParameter::load(buffer); + std::string folder; + Serialization::load(buffer, folder); std::string filename; Serialization::load(buffer, filename); - data = AssetRegistry::findTexture(filename); + data = AssetRegistry::findTexture(folder, filename); sampler = buffer.getGraphics()->createSampler({}); } @@ -313,7 +323,18 @@ std::string AddExpression::evaluate(Map& varState) con { std::string varName = fmt::format("exp_{}", key); 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 @@ -330,7 +351,18 @@ std::string SubExpression::evaluate(Map& varState) con { std::string varName = fmt::format("exp_{}", key); 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 @@ -347,7 +379,17 @@ std::string MulExpression::evaluate(Map& varState) con { std::string varName = fmt::format("exp_{}", key); 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 @@ -408,13 +450,18 @@ std::string SampleExpression::evaluate(Map& varState) { std::string varName = fmt::format("exp_{}", key); 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")) { - 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 { - 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); } } diff --git a/src/Engine/Material/ShaderExpression.h b/src/Engine/Material/ShaderExpression.h index f3ca1a7..0866a39 100644 --- a/src/Engine/Material/ShaderExpression.h +++ b/src/Engine/Material/ShaderExpression.h @@ -69,7 +69,7 @@ struct FloatParameter : public ShaderParameter static constexpr uint64 IDENTIFIER = 0x01; float data = 0.0f; FloatParameter() {} - FloatParameter(std::string name, uint32 byteOffset, uint32 binding); + FloatParameter(std::string name, float data, uint32 byteOffset, uint32 binding); virtual ~FloatParameter(); virtual void updateDescriptorSet(Gfx::PDescriptorSet descriptorSet, uint8* dst) override; virtual void generateDeclaration(std::ofstream& stream) const override; @@ -83,7 +83,7 @@ struct VectorParameter : public ShaderParameter static constexpr uint64 IDENTIFIER = 0x02; Vector data = Vector(); VectorParameter() {} - VectorParameter(std::string name, uint32 byteOffset, uint32 binding); + VectorParameter(std::string name, Vector data, uint32 byteOffset, uint32 binding); virtual ~VectorParameter(); virtual void updateDescriptorSet(Gfx::PDescriptorSet descriptorSet, uint8* dst) override; virtual void generateDeclaration(std::ofstream& stream) const override; @@ -97,7 +97,7 @@ struct TextureParameter : public ShaderParameter static constexpr uint64 IDENTIFIER = 0x04; PTextureAsset data = nullptr; TextureParameter() {} - TextureParameter(std::string name, uint32 byteOffset, uint32 binding); + TextureParameter(std::string name, PTextureAsset data, uint32 binding); virtual ~TextureParameter(); virtual void updateDescriptorSet(Gfx::PDescriptorSet descriptorSet, uint8* dst) override; virtual void generateDeclaration(std::ofstream& stream) const override; @@ -112,7 +112,7 @@ struct SamplerParameter : public ShaderParameter static constexpr uint64 IDENTIFIER = 0x08; Gfx::OSampler data = nullptr; SamplerParameter() {} - SamplerParameter(std::string name, uint32 byteOffset, uint32 binding); + SamplerParameter(std::string name, Gfx::OSampler sampler, uint32 binding); virtual ~SamplerParameter(); virtual void updateDescriptorSet(Gfx::PDescriptorSet descriptorSet, uint8* dst) override; virtual void generateDeclaration(std::ofstream& stream) const override; @@ -125,9 +125,9 @@ struct CombinedTextureParameter : public ShaderParameter { static constexpr uint64 IDENTIFIER = 0x10; PTextureAsset data = nullptr; - Gfx::PSampler sampler = nullptr; + Gfx::OSampler sampler = nullptr; CombinedTextureParameter() {} - CombinedTextureParameter(std::string name, uint32 byteOffset, uint32 binding); + CombinedTextureParameter(std::string name, PTextureAsset data, Gfx::OSampler sampler, uint32 binding); virtual ~CombinedTextureParameter(); virtual void updateDescriptorSet(Gfx::PDescriptorSet descriptorSet, uint8* dst) override; virtual void generateDeclaration(std::ofstream& stream) const override; @@ -189,6 +189,7 @@ struct SwizzleExpression : public ShaderExpression static constexpr uint64 IDENTIFIER = 0x15; StaticArray comp = {-1, -1, -1, -1}; SwizzleExpression() {} + SwizzleExpression(StaticArray comp) : comp(std::move(comp)) {} virtual ~SwizzleExpression() {} virtual uint64 getIdentifier() const override { return IDENTIFIER; } virtual std::string evaluate(Map& varState) const override; diff --git a/src/Engine/Serialization/Serialization.h b/src/Engine/Serialization/Serialization.h index 86cee82..2e2162a 100644 --- a/src/Engine/Serialization/Serialization.h +++ b/src/Engine/Serialization/Serialization.h @@ -55,41 +55,11 @@ namespace Serialization } static void load(ArchiveBuffer& buffer, Matrix4& mat) { - load(buffer, mat[0][0]); - 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]); + buffer.readBytes(&mat, sizeof(Matrix4)); } static void save(ArchiveBuffer& buffer, const Matrix4& mat) { - save(buffer, mat[0][0]); - 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]); + buffer.writeBytes(&mat, sizeof(Matrix4)); } static void load(ArchiveBuffer& buffer, Vector& vec) {