From af7d624d06f5d4eb076d5c3a64f07385775cb8c8 Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Mon, 6 May 2024 18:36:16 +0200 Subject: [PATCH] Now able to import stuff properly --- res/shaders/BasePass.slang | 5 +- res/shaders/Placeholder.json | 15 +++++ res/shaders/lib/BRDF.slang | 13 ++-- res/shaders/lib/Common.slang | 1 + res/shaders/lib/LightEnv.slang | 10 +-- res/shaders/lib/MaterialParameter.slang | 22 ++++--- res/shaders/lib/StaticMeshVertexData.slang | 7 +- src/Editor/Asset/MeshLoader.cpp | 66 +++++++++++++------ src/Editor/Asset/MeshLoader.h | 4 +- src/Editor/main.cpp | 2 +- src/Engine/Graphics/Descriptor.h | 1 + src/Engine/Graphics/RenderPass/BasePass.cpp | 4 +- .../Graphics/RenderPass/DepthPrepass.cpp | 5 +- src/Engine/Graphics/RenderPass/UIPass.cpp | 1 - src/Engine/Graphics/StaticMeshVertexData.cpp | 60 +++++++++++------ src/Engine/Graphics/StaticMeshVertexData.h | 6 +- src/Engine/Graphics/VertexData.h | 2 + src/Engine/Graphics/Vulkan/Descriptor.cpp | 27 ++++++++ src/Engine/Graphics/Vulkan/Descriptor.h | 1 + src/Engine/Graphics/Vulkan/Window.cpp | 4 +- src/Engine/Graphics/Window.cpp | 12 ++++ 21 files changed, 189 insertions(+), 79 deletions(-) create mode 100644 res/shaders/Placeholder.json diff --git a/res/shaders/BasePass.slang b/res/shaders/BasePass.slang index 0bf8da6..25f0f7b 100644 --- a/res/shaders/BasePass.slang +++ b/res/shaders/BasePass.slang @@ -32,7 +32,8 @@ float4 fragmentMain(in FragmentParameter params) : SV_Target result += pLightEnv.pointLights[lightIndex].illuminate(lightingParams, brdf); } result += brdf.evaluateAmbient(); + // gamma correction result = result / (result + float3(1.0)); - float3 gammaCorrected = pow(result, float3(1.0/2.2)); - return float4(gammaCorrected, 1.0f); + result = pow(result, float3(1.0/2.2)); + return float4(result, 1.0f); } diff --git a/res/shaders/Placeholder.json b/res/shaders/Placeholder.json new file mode 100644 index 0000000..08d2e12 --- /dev/null +++ b/res/shaders/Placeholder.json @@ -0,0 +1,15 @@ +{ + "name": "Placeholder", + "params": { + }, + "code": [ + { + "exp": "BRDF", + "profile": "BlinnPhong", + "values": { + "baseColor": "float3(0, 1, 0)", + "normal": "float3(0, 0, 1)" + } + } + ] +} \ No newline at end of file diff --git a/res/shaders/lib/BRDF.slang b/res/shaders/lib/BRDF.slang index 88150a4..f7c2e4a 100644 --- a/res/shaders/lib/BRDF.slang +++ b/res/shaders/lib/BRDF.slang @@ -21,12 +21,12 @@ struct Phong : IBRDF float3 evaluate(float3x3 tbn, float3 viewDir_TS, float3 lightDir_TS, float3 lightColor) { - float3 n = normalize(normal); - float3 nDotL = dot(n, lightDir_TS); - float3 r = 2 * (nDotL) * n - lightDir_TS; + float3 normal_TS = normalize(normal); + float3 nDotL = dot(normal_TS, lightDir_TS); + float3 r = 2 * (nDotL) * normal_TS - lightDir_TS; float rDotV = dot(r, viewDir_TS); - return baseColor * max(nDotL, 0.0) * lightColor + specular * pow(max(rDotV, 0.0), shininess) * lightColor; + return baseColor * max(nDotL, 0.0) * lightColor + specular * pow(max(rDotV, 0.0), shininess); } float3 evaluateAmbient() @@ -40,6 +40,7 @@ struct BlinnPhong : IBRDF float3 baseColor; float3 specularColor; float3 normal; + float shininess; float3 ambient; __init() @@ -52,9 +53,9 @@ struct BlinnPhong : IBRDF float3 normal_TS = normalize(normal); float diffuse = max(dot(normal_TS, lightDir_TS), 0); float3 h = normalize(lightDir_TS + viewDir_TS); - float specular = saturate(dot(normal_TS, h)); + float specular = pow(saturate(dot(normal_TS, h)), shininess); - return (baseColor * diffuse * lightColor) + (specularColor * specular) * lightColor; + return (baseColor * diffuse * lightColor) + (specularColor * specular); } float3 evaluateAmbient() diff --git a/res/shaders/lib/Common.slang b/res/shaders/lib/Common.slang index 2b4c7cf..54df6ff 100644 --- a/res/shaders/lib/Common.slang +++ b/res/shaders/lib/Common.slang @@ -1,5 +1,6 @@ const static float PI = 3.1415926535897932f; const static uint BLOCK_SIZE = 32; +static const uint64_t MAX_TEXCOORDS = 8; struct ViewParameter { diff --git a/res/shaders/lib/LightEnv.slang b/res/shaders/lib/LightEnv.slang index 608ef05..4ddfb4d 100644 --- a/res/shaders/lib/LightEnv.slang +++ b/res/shaders/lib/LightEnv.slang @@ -33,21 +33,21 @@ struct PointLight : ILightEnv return illuminance * brdf.evaluate(params.tbn, params.viewDir_TS, -normalize(lightDir_TS), colorRange.xyz); } - bool insidePlane(Plane plane, float3 position_VS) + bool insidePlane(Plane plane, float3 position) { - return dot(plane.n, position_VS) - plane.d < -colorRange.w; + return dot(plane.n, position) - plane.d < -colorRange.w; } - bool insideFrustum(Frustum frustum, float3 position_VS, float minDepth, float maxDepth) + bool insideFrustum(Frustum frustum, float3 position, float minDepth, float maxDepth) { bool result = true; - if(position_VS.z - colorRange.w > minDepth || position_VS.z + colorRange.w < maxDepth) + if(position.z - colorRange.w > minDepth || position.z + colorRange.w < maxDepth) { result = false; } for(int i = 0; i < 4 && result; ++i) { - if(insidePlane(frustum.sides[i], position_VS)) + if(insidePlane(frustum.sides[i], position)) { result = false; } diff --git a/res/shaders/lib/MaterialParameter.slang b/res/shaders/lib/MaterialParameter.slang index f3466f8..d1bbb82 100644 --- a/res/shaders/lib/MaterialParameter.slang +++ b/res/shaders/lib/MaterialParameter.slang @@ -3,7 +3,7 @@ import Common; struct MaterialParameter { float3 position_WS; - float2 texCoords[1]; + float2 texCoords[MAX_TEXCOORDS]; float3 vertexColor; }; @@ -25,13 +25,16 @@ struct FragmentParameter float3 biTangent_WS : TANGENT1; float3 position_WS : POSITION2; float3 vertexColor : COLOR0; - float2 texCoords : TEXCOORD0; + float2 texCoords[MAX_TEXCOORDS] : TEXCOORD0; MaterialParameter getMaterialParameter() { MaterialParameter result; result.position_WS = position_WS; result.vertexColor = vertexColor; - result.texCoords[0] = texCoords; + for(uint i = 0; i < MAX_TEXCOORDS; ++i) + { + result.texCoords[i] = texCoords[i]; + } return result; } LightingParameter getLightingParameter() @@ -52,16 +55,16 @@ struct VertexAttributes float3 tangent_MS; float3 biTangent_MS; float3 vertexColor; - float2 texCoords; + float2 texCoords[MAX_TEXCOORDS]; FragmentParameter getParameter(float4x4 transformMatrix) { float4 modelPos = float4(position_MS, 1); float4 worldPos = mul(transformMatrix, modelPos); float4 viewPos = mul(pViewParams.viewMatrix, worldPos); float4 clipPos = mul(pViewParams.projectionMatrix, viewPos); - float3 tangent_WS = mul(float3x3(transformMatrix), tangent_MS); - float3 biTangent_WS = mul(float3x3(transformMatrix), biTangent_MS); - float3 normal_WS = mul(float3x3(transformMatrix), normal_MS); + float3 tangent_WS = normalize(mul(transformMatrix, float4(tangent_MS, 0.0)).xyz); + float3 biTangent_WS = normalize(mul(transformMatrix, float4(biTangent_MS, 0.0)).xyz); + float3 normal_WS = normalize(mul(transformMatrix, float4(normal_MS, 0.0)).xyz); FragmentParameter result; result.viewDir_WS = pViewParams.cameraPos_WS.xyz - worldPos.xyz; result.normal_WS = normal_WS; @@ -70,7 +73,10 @@ struct VertexAttributes result.position_WS = worldPos.xyz; result.position_CS = clipPos; result.vertexColor = vertexColor; - result.texCoords = texCoords; + for(uint i = 0; i < MAX_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..7645dda 100644 --- a/res/shaders/lib/StaticMeshVertexData.slang +++ b/res/shaders/lib/StaticMeshVertexData.slang @@ -11,14 +11,17 @@ 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]); + for(uint i = 0; i < MAX_TEXCOORDS; ++i) + { + attributes.texCoords[i] = float2(texCoords[i][2 * index + 0], texCoords[i][2 * index + 1]); + } attributes.vertexColor = float3(color[3 * index + 0], color[3 * index + 1], color[3 * index + 2]); return attributes; } StructuredBuffer positions; - StructuredBuffer texCoords; StructuredBuffer normals; StructuredBuffer tangents; StructuredBuffer biTangents; StructuredBuffer color; + StructuredBuffer texCoords[MAX_TEXCOORDS]; }; diff --git a/src/Editor/Asset/MeshLoader.cpp b/src/Editor/Asset/MeshLoader.cpp index a5ef027..2348a64 100644 --- a/src/Editor/Asset/MeshLoader.cpp +++ b/src/Editor/Asset/MeshLoader.cpp @@ -52,7 +52,7 @@ void MeshLoader::convertAssimpARGB(unsigned char* dst, aiTexel* src, uint32 numP dst[i * 4 + 3] = src[i].a; } } -void MeshLoader::loadTextures(const aiScene* scene, const std::filesystem::path& meshDirectory, const std::string& importPath, Map& textures) +void MeshLoader::loadTextures(const aiScene* scene, const std::filesystem::path& meshDirectory, const std::string& importPath, Array& textures) { for (uint32 i = 0; i < scene->mNumTextures; ++i) { @@ -66,6 +66,7 @@ void MeshLoader::loadTextures(const aiScene* scene, const std::filesystem::path& } else { + texPath = (meshDirectory / texPath).replace_extension("png"); if (tex->mHeight == 0) { // already compressed, just dump it to the disk @@ -87,7 +88,7 @@ void MeshLoader::loadTextures(const aiScene* scene, const std::filesystem::path& .filePath = texPath, .importPath = importPath, }); - textures[texPath.string()] = AssetRegistry::findTexture(importPath, texPath.string()); + textures.add(AssetRegistry::findTexture(importPath, texPath.stem().string())); } } @@ -107,7 +108,7 @@ constexpr const char* KEY_ROUGHNESS_TEXTURE = "tex_r"; constexpr const char* KEY_METALLIC_TEXTURE = "tex_m"; constexpr const char* KEY_AMBIENT_OCCLUSION_TEXTURE = "tex_ao"; -void MeshLoader::loadMaterials(const aiScene* scene, const Map& textures, const std::string& baseName, const std::filesystem::path& meshDirectory, const std::string& importPath, Array& globalMaterials) +void MeshLoader::loadMaterials(const aiScene* scene, const Array& textures, const std::string& baseName, const std::filesystem::path& meshDirectory, const std::string& importPath, Array& globalMaterials) { for(uint32 i = 0; i < scene->mNumMaterials; ++i) { @@ -150,7 +151,7 @@ void MeshLoader::loadMaterials(const aiScene* scene, const Map extractMask = {0, 1, 2, -1}) + auto addTextureParameter = [&](std::string paramKey, aiTextureType type, int index, std::string& result, StaticArray extractMask = { 0, 1, 2, -1 }) { aiString texPath; aiTextureMapping mapping; @@ -162,13 +163,14 @@ void MeshLoader::loadMaterials(const aiScene* scene, const Mapkey = "NormalMul"; + expressions.back()->inputs["lhs"].source = "2"; + expressions.back()->inputs["rhs"].source = outputNormal; + + expressions.add(new SubExpression()); + expressions.back()->key = "NormalSub"; + expressions.back()->inputs["lhs"].source = "NormalMul"; + expressions.back()->inputs["rhs"].source = "float3(1,1,1)"; + + brdf.variables["normal"] = "NormalSub"; } aiShadingMode mode; material->Get(AI_MATKEY_SHADING_MODEL, mode); switch (mode) { case aiShadingMode_Blinn: - brdf.profile = "Phong"; - brdf.variables["specular"] = outputSpecular; + brdf.profile = "BlinnPhong"; + brdf.variables["specularColor"] = outputSpecular; brdf.variables["ambient"] = outputAmbient; brdf.variables["shininess"] = outputShininess; break; case aiShadingMode_Phong: - brdf.profile = "BlinnPhong"; - brdf.variables["specularColor"] = outputSpecular; + brdf.profile = "Phong"; + brdf.variables["specular"] = outputSpecular; brdf.variables["ambient"] = outputAmbient; + brdf.variables["shininess"] = outputShininess; break; case aiShadingMode_Toon: brdf.profile = "CelShading"; @@ -458,7 +471,11 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array positions(mesh->mNumVertices); - Array texCoords(mesh->mNumVertices); + StaticArray, MAX_TEXCOORDS> texCoords; + for (size_t i = 0; i < MAX_TEXCOORDS; ++i) + { + texCoords[i].resize(mesh->mNumVertices); + } Array normals(mesh->mNumVertices); Array tangents(mesh->mNumVertices); Array biTangents(mesh->mNumVertices); @@ -469,13 +486,16 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const ArraymNumVertices; ++i) { positions[i] = Vector(mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z); - if (mesh->HasTextureCoords(0)) + for (size_t j = 0; j < MAX_TEXCOORDS; ++j) { - texCoords[i] = Vector2(mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].y); - } - else - { - texCoords[i] = Vector2(0, 0); + if (mesh->HasTextureCoords(j)) + { + texCoords[j][i] = Vector2(mesh->mTextureCoords[j][i].x, mesh->mTextureCoords[j][i].y); + } + else + { + texCoords[j][i] = Vector2(0, 0); + } } normals[i] = Vector(mesh->mNormals[i].x, mesh->mNormals[i].y, mesh->mNormals[i].z); if (mesh->HasTangentsAndBitangents()) @@ -499,7 +519,11 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const ArrayallocateVertexData(mesh->mNumVertices); vertexData->loadPositions(id, positions); - vertexData->loadTexCoords(id, texCoords); + + for (size_t i = 0; i < MAX_TEXCOORDS; ++i) + { + vertexData->loadTexCoords(id, i, texCoords[i]); + } vertexData->loadNormals(id, normals); vertexData->loadTangents(id, tangents); vertexData->loadBiTangents(id, biTangents); @@ -570,7 +594,7 @@ void MeshLoader::import(MeshImportArgs args, PMeshAsset meshAsset) const aiScene *scene = importer.ApplyPostProcessing(aiProcess_CalcTangentSpace); std::cout << importer.GetErrorString() << std::endl; - Map textures; + Array textures; loadTextures(scene, args.filePath.parent_path(), args.importPath, textures); Array globalMaterials(scene->mNumMaterials); loadMaterials(scene, textures, args.filePath.stem().string(), args.filePath.parent_path(), args.importPath, globalMaterials); diff --git a/src/Editor/Asset/MeshLoader.h b/src/Editor/Asset/MeshLoader.h index 7e21071..e1db7eb 100644 --- a/src/Editor/Asset/MeshLoader.h +++ b/src/Editor/Asset/MeshLoader.h @@ -27,8 +27,8 @@ public: ~MeshLoader(); void importAsset(MeshImportArgs args); private: - 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 loadTextures(const aiScene* scene, const std::filesystem::path& meshDirectory, const std::string& importPath, Array& textures); + void loadMaterials(const aiScene* scene, const Array& 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/main.cpp b/src/Editor/main.cpp index de733d1..0f8751c 100644 --- a/src/Editor/main.cpp +++ b/src/Editor/main.cpp @@ -62,7 +62,7 @@ int main() { // .filePath = sourcePath / "import/models/Arissa.fbx", // }); AssetImporter::importMesh(MeshImportArgs{ - .filePath = sourcePath / "import/models/after-the-rain-vr-sound/source/Whitechapel.fbx", + .filePath = sourcePath / "import/models/after-the-rain-vr-sound/source/Whitechapel.glb", .importPath = "Whitechapel" }); //AssetImporter::importMesh(MeshImportArgs{ diff --git a/src/Engine/Graphics/Descriptor.h b/src/Engine/Graphics/Descriptor.h index 185cd77..bed9a11 100644 --- a/src/Engine/Graphics/Descriptor.h +++ b/src/Engine/Graphics/Descriptor.h @@ -61,6 +61,7 @@ public: virtual void writeChanges() = 0; virtual void updateBuffer(uint32 binding, PUniformBuffer uniformBuffer) = 0; virtual void updateBuffer(uint32 binding, PShaderBuffer ShaderBuffer) = 0; + virtual void updateBuffer(uint32_t binding, uint32 index, Gfx::PShaderBuffer uniformBuffer) = 0; virtual void updateSampler(uint32 binding, PSampler sampler) = 0; virtual void updateTexture(uint32 binding, PTexture texture, PSampler samplerState = nullptr) = 0; virtual void updateTextureArray(uint32_t binding, Array texture) = 0; diff --git a/src/Engine/Graphics/RenderPass/BasePass.cpp b/src/Engine/Graphics/RenderPass/BasePass.cpp index 86ce23e..d818907 100644 --- a/src/Engine/Graphics/RenderPass/BasePass.cpp +++ b/src/Engine/Graphics/RenderPass/BasePass.cpp @@ -99,7 +99,7 @@ void BasePass::render() pipelineInfo.fragmentShader = collection->fragmentShader; pipelineInfo.pipelineLayout = collection->pipelineLayout; pipelineInfo.renderPass = renderPass; - pipelineInfo.depthStencilState.depthCompareOp = Gfx::SE_COMPARE_OP_LESS_OR_EQUAL; + pipelineInfo.depthStencilState.depthCompareOp = Gfx::SE_COMPARE_OP_GREATER_OR_EQUAL; pipelineInfo.multisampleState.samples = viewport->getSamples(); pipelineInfo.colorBlend.attachmentCount = 1; Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo)); @@ -112,7 +112,7 @@ void BasePass::render() pipelineInfo.fragmentShader = collection->fragmentShader; pipelineInfo.pipelineLayout = collection->pipelineLayout; pipelineInfo.renderPass = renderPass; - pipelineInfo.depthStencilState.depthCompareOp = Gfx::SE_COMPARE_OP_LESS_OR_EQUAL; + pipelineInfo.depthStencilState.depthCompareOp = Gfx::SE_COMPARE_OP_GREATER_OR_EQUAL; pipelineInfo.multisampleState.samples = viewport->getSamples(); pipelineInfo.colorBlend.attachmentCount = 1; Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo)); diff --git a/src/Engine/Graphics/RenderPass/DepthPrepass.cpp b/src/Engine/Graphics/RenderPass/DepthPrepass.cpp index 1aa25e6..f0d936b 100644 --- a/src/Engine/Graphics/RenderPass/DepthPrepass.cpp +++ b/src/Engine/Graphics/RenderPass/DepthPrepass.cpp @@ -79,7 +79,7 @@ void DepthPrepass::render() pipelineInfo.fragmentShader = collection->fragmentShader; pipelineInfo.pipelineLayout = collection->pipelineLayout; pipelineInfo.renderPass = renderPass; - pipelineInfo.depthStencilState.depthCompareOp = Gfx::SE_COMPARE_OP_LESS; + pipelineInfo.depthStencilState.depthCompareOp = Gfx::SE_COMPARE_OP_GREATER; pipelineInfo.multisampleState.samples = viewport->getSamples(); Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo)); command->bindPipeline(pipeline); @@ -92,7 +92,7 @@ void DepthPrepass::render() pipelineInfo.pipelineLayout = collection->pipelineLayout; pipelineInfo.renderPass = renderPass; //pipelineInfo.depthStencilState.depthWriteEnable = false; - pipelineInfo.depthStencilState.depthCompareOp = Gfx::SE_COMPARE_OP_LESS_OR_EQUAL; + pipelineInfo.depthStencilState.depthCompareOp = Gfx::SE_COMPARE_OP_GREATER; pipelineInfo.multisampleState.samples = viewport->getSamples(); Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo)); command->bindPipeline(pipeline); @@ -147,7 +147,6 @@ void DepthPrepass::publishOutputs() Gfx::RenderTargetAttachment(depthBuffer, Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_GENERAL, Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE); - depthAttachment.clear.depthStencil.depth = 1.0f; resources->registerRenderPassOutput("DEPTHPREPASS_DEPTH", depthAttachment); } diff --git a/src/Engine/Graphics/RenderPass/UIPass.cpp b/src/Engine/Graphics/RenderPass/UIPass.cpp index 9931e60..4d88aef 100644 --- a/src/Engine/Graphics/RenderPass/UIPass.cpp +++ b/src/Engine/Graphics/RenderPass/UIPass.cpp @@ -76,7 +76,6 @@ void UIPass::publishOutputs() Gfx::RenderTargetAttachment(depthBuffer, Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE); - depthAttachment.clear.depthStencil.depth = 1.0f; resources->registerRenderPassOutput("UIPASS_DEPTH", depthAttachment); TextureCreateInfo colorBufferInfo = { diff --git a/src/Engine/Graphics/StaticMeshVertexData.cpp b/src/Engine/Graphics/StaticMeshVertexData.cpp index 9d117d7..4c303b4 100644 --- a/src/Engine/Graphics/StaticMeshVertexData.cpp +++ b/src/Engine/Graphics/StaticMeshVertexData.cpp @@ -32,7 +32,7 @@ void StaticMeshVertexData::loadPositions(MeshId id, const Array& data) dirty = true; } -void StaticMeshVertexData::loadTexCoords(MeshId id, const Array& data) +void StaticMeshVertexData::loadTexCoords(MeshId id, uint64 index, const Array& data) { uint64 offset; { @@ -40,7 +40,7 @@ void StaticMeshVertexData::loadTexCoords(MeshId id, const Array& data) offset = meshOffsets[id]; } assert(offset + data.size() <= head); - std::memcpy(texCoordsData.data() + offset, data.data(), data.size() * sizeof(Vector2)); + std::memcpy(texCoordsData[index].data() + offset, data.data(), data.size() * sizeof(Vector2)); dirty = true; } @@ -100,19 +100,23 @@ void StaticMeshVertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveB offset = meshOffsets[id]; } Array pos(numVertices); - Array tex(numVertices); + Array tex[MAX_TEXCOORDS]; + for (size_t i = 0; i < MAX_TEXCOORDS; ++i) + { + tex[i].resize(numVertices); + std::memcpy(tex[i].data(), texCoordsData[i].data() + offset, numVertices * sizeof(Vector2)); + Serialization::save(buffer, tex[i]); + } Array nor(numVertices); Array tan(numVertices); Array bit(numVertices); Array col(numVertices); std::memcpy(pos.data(), positionData.data() + offset, numVertices * sizeof(Vector)); - std::memcpy(tex.data(), texCoordsData.data() + offset, numVertices * sizeof(Vector2)); std::memcpy(nor.data(), normalData.data() + offset, numVertices * sizeof(Vector)); std::memcpy(tan.data(), tangentData.data() + offset, numVertices * sizeof(Vector)); std::memcpy(bit.data(), biTangentData.data() + offset, numVertices * sizeof(Vector)); std::memcpy(col.data(), colorData.data() + offset, numVertices * sizeof(Vector)); Serialization::save(buffer, pos); - Serialization::save(buffer, tex); Serialization::save(buffer, nor); Serialization::save(buffer, tan); Serialization::save(buffer, bit); @@ -122,19 +126,22 @@ void StaticMeshVertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveB void StaticMeshVertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer) { Array pos; - Array tex; + Array tex[MAX_TEXCOORDS]; + for (size_t i = 0; i < MAX_TEXCOORDS; ++i) + { + Serialization::load(buffer, tex[i]); + loadTexCoords(id, i, tex[i]); + } Array nor; Array tan; Array bit; Array col; Serialization::load(buffer, pos); - Serialization::load(buffer, tex); Serialization::load(buffer, nor); Serialization::load(buffer, tan); Serialization::load(buffer, bit); Serialization::load(buffer, col); loadPositions(id, pos); - loadTexCoords(id, tex); loadNormals(id, nor); loadTangents(id, tan); loadBiTangents(id, bit); @@ -150,7 +157,7 @@ void StaticMeshVertexData::init(Gfx::PGraphics _graphics) descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 2, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER}); descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 3, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER}); descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 4, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER}); - descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 5, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER}); + descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 5, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, .descriptorCount = MAX_TEXCOORDS }); descriptorLayout->create(); descriptorSet = descriptorLayout->allocateDescriptorSet(); } @@ -159,7 +166,10 @@ void StaticMeshVertexData::destroy() { VertexData::destroy(); positions = nullptr; - texCoords = nullptr; + for (size_t i = 0; i < MAX_TEXCOORDS; ++i) + { + texCoords[i] = nullptr; + } normals = nullptr; tangents = nullptr; biTangents = nullptr; @@ -204,10 +214,13 @@ void StaticMeshVertexData::resizeBuffers() createInfo.sourceData.size = verticesAllocated * sizeof(Vector2); createInfo.name = "TexCoords"; createInfo.numElements = verticesAllocated * 2; - texCoords = graphics->createShaderBuffer(createInfo); + for (size_t i = 0; i < MAX_TEXCOORDS; ++i) + { + texCoords[i] = graphics->createShaderBuffer(createInfo); + texCoordsData[i].resize(verticesAllocated); + } positionData.resize(verticesAllocated); - texCoordsData.resize(verticesAllocated); normalData.resize(verticesAllocated); tangentData.resize(verticesAllocated); biTangentData.resize(verticesAllocated); @@ -220,10 +233,13 @@ void StaticMeshVertexData::updateBuffers() .size = positionData.size() * sizeof(Vector), .data = (uint8*)positionData.data(), }); - texCoords->updateContents(DataSource{ - .size = texCoordsData.size() * sizeof(Vector2), - .data = (uint8*)texCoordsData.data(), - }); + for (size_t i = 0; i < MAX_TEXCOORDS; ++i) + { + texCoords[i]->updateContents(DataSource{ + .size = texCoordsData[i].size() * sizeof(Vector2), + .data = (uint8*)texCoordsData[i].data(), + }); + } normals->updateContents(DataSource{ .size = normalData.size() * sizeof(Vector), .data = (uint8*)normalData.data(), @@ -243,10 +259,12 @@ void StaticMeshVertexData::updateBuffers() descriptorLayout->reset(); descriptorSet = descriptorLayout->allocateDescriptorSet(); descriptorSet->updateBuffer(0, positions); - descriptorSet->updateBuffer(1, texCoords); - descriptorSet->updateBuffer(2, normals); - descriptorSet->updateBuffer(3, tangents); - descriptorSet->updateBuffer(4, biTangents); - descriptorSet->updateBuffer(5, colors); + descriptorSet->updateBuffer(1, normals); + descriptorSet->updateBuffer(2, tangents); + descriptorSet->updateBuffer(3, biTangents); + descriptorSet->updateBuffer(4, colors); + for (size_t i = 0; i < MAX_TEXCOORDS; ++i) { + descriptorSet->updateBuffer(5, i, texCoords[i]); + } descriptorSet->writeChanges(); } diff --git a/src/Engine/Graphics/StaticMeshVertexData.h b/src/Engine/Graphics/StaticMeshVertexData.h index b135a84..45f58ae 100644 --- a/src/Engine/Graphics/StaticMeshVertexData.h +++ b/src/Engine/Graphics/StaticMeshVertexData.h @@ -13,7 +13,7 @@ public: virtual ~StaticMeshVertexData(); static StaticMeshVertexData* getInstance(); void loadPositions(MeshId id, const Array& data); - void loadTexCoords(MeshId id, const Array& data); + void loadTexCoords(MeshId id, uint64 index, const Array& data); void loadNormals(MeshId id, const Array& data); void loadTangents(MeshId id, const Array& data); void loadBiTangents(MeshId id, const Array& data); @@ -32,8 +32,8 @@ private: std::mutex mutex; Gfx::OShaderBuffer positions; Array positionData; - Gfx::OShaderBuffer texCoords; - Array texCoordsData; + Gfx::OShaderBuffer texCoords[MAX_TEXCOORDS]; + Array texCoordsData[MAX_TEXCOORDS]; Gfx::OShaderBuffer normals; Array normalData; Gfx::OShaderBuffer tangents; diff --git a/src/Engine/Graphics/VertexData.h b/src/Engine/Graphics/VertexData.h index 9bb4432..5998745 100644 --- a/src/Engine/Graphics/VertexData.h +++ b/src/Engine/Graphics/VertexData.h @@ -8,6 +8,8 @@ #include "Graphics/Buffer.h" #include "Meshlet.h" +constexpr uint64 MAX_TEXCOORDS = 8; + namespace Seele { DECLARE_REF(Mesh) diff --git a/src/Engine/Graphics/Vulkan/Descriptor.cpp b/src/Engine/Graphics/Vulkan/Descriptor.cpp index 77606b5..538c282 100644 --- a/src/Engine/Graphics/Vulkan/Descriptor.cpp +++ b/src/Engine/Graphics/Vulkan/Descriptor.cpp @@ -218,6 +218,33 @@ void DescriptorSet::updateBuffer(uint32_t binding, Gfx::PShaderBuffer shaderBuff cachedData[binding] = vulkanBuffer.getHandle(); } +void DescriptorSet::updateBuffer(uint32_t binding, uint32 index, Gfx::PShaderBuffer shaderBuffer) { + PShaderBuffer vulkanBuffer = shaderBuffer.cast(); + ShaderBuffer* cachedBuffer = reinterpret_cast(cachedData[binding]); + if (vulkanBuffer.getHandle() == cachedBuffer) { + return; + } + + bufferInfos.add(VkDescriptorBufferInfo{ + .buffer = vulkanBuffer->getHandle(), + .offset = 0, + .range = vulkanBuffer->getSize(), + }); + + writeDescriptors.add(VkWriteDescriptorSet{ + .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, + .pNext = nullptr, + .dstSet = setHandle, + .dstBinding = binding, + .dstArrayElement = index, + .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, + .pBufferInfo = &bufferInfos.back(), + }); + + cachedData[binding] = vulkanBuffer.getHandle(); +} + void DescriptorSet::updateSampler(uint32_t binding, Gfx::PSampler samplerState) { PSampler vulkanSampler = samplerState.cast(); Sampler* cachedSampler = reinterpret_cast(cachedData[binding]); diff --git a/src/Engine/Graphics/Vulkan/Descriptor.h b/src/Engine/Graphics/Vulkan/Descriptor.h index 8835709..9a8622c 100644 --- a/src/Engine/Graphics/Vulkan/Descriptor.h +++ b/src/Engine/Graphics/Vulkan/Descriptor.h @@ -50,6 +50,7 @@ public: virtual void writeChanges() override; virtual void updateBuffer(uint32_t binding, Gfx::PUniformBuffer uniformBuffer) override; virtual void updateBuffer(uint32_t binding, Gfx::PShaderBuffer uniformBuffer) override; + virtual void updateBuffer(uint32_t binding, uint32 index, Gfx::PShaderBuffer uniformBuffer) override; virtual void updateSampler(uint32_t binding, Gfx::PSampler samplerState) override; virtual void updateTexture(uint32_t binding, Gfx::PTexture texture, Gfx::PSampler sampler = nullptr) override; virtual void updateTextureArray(uint32_t binding, Array texture) override; diff --git a/src/Engine/Graphics/Vulkan/Window.cpp b/src/Engine/Graphics/Vulkan/Window.cpp index a8b7550..b38a51b 100644 --- a/src/Engine/Graphics/Vulkan/Window.cpp +++ b/src/Engine/Graphics/Vulkan/Window.cpp @@ -361,8 +361,8 @@ Viewport::Viewport(PWindow owner, const ViewportCreateInfo &viewportInfo) handle.x = static_cast(offsetX); handle.y = static_cast(offsetY) + handle.height; handle.height = -handle.height; - handle.minDepth = 0.f; - handle.maxDepth = 1.f; + handle.minDepth = 1.f; + handle.maxDepth = 0.f; } Viewport::~Viewport() diff --git a/src/Engine/Graphics/Window.cpp b/src/Engine/Graphics/Window.cpp index 7b558d5..e39c73c 100644 --- a/src/Engine/Graphics/Window.cpp +++ b/src/Engine/Graphics/Window.cpp @@ -31,6 +31,18 @@ Matrix4 Viewport::getProjectionMatrix() const { if (fieldOfView > 0.0f) { + //float h = 1.0 / tan(fieldOfView * 0.5); + //float w = h / (sizeX / static_cast(sizeY)); + //float zFar = 1000.0f; + //float zNear = 0.1f; + //float a = -zNear / (zFar - zNear); + //float b = (zNear * zFar) / (zFar - zNear); + //return Matrix4( + // w, 0, 0, 0, + // 0, -h, 0, 0, + // 0, 0, a, b, + // 0, 0, 1, 0 + //); return glm::perspective(fieldOfView, sizeX / static_cast(sizeY), 0.1f, 10000.0f); } else