From dfcfc2bb1e379ae9539846b32f531a310dc121ad Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Wed, 1 May 2024 19:05:48 +0200 Subject: [PATCH] It finally imports somewhat --- res/shaders/BasePass.slang | 6 +- res/shaders/lib/BRDF.slang | 2 - res/shaders/lib/MaterialParameter.slang | 18 ++-- res/shaders/lib/StaticMeshVertexData.slang | 2 +- src/Editor/Asset/MeshLoader.cpp | 10 +-- src/Editor/Window/SceneView.h | 1 - src/Engine/Containers/Array.h | 8 +- src/Engine/Graphics/Shader.cpp | 94 +++++++++++++------- src/Engine/Graphics/Shader.h | 8 +- src/Engine/Graphics/StaticMeshVertexData.cpp | 12 +-- src/Engine/System/ComponentSystem.h | 10 +-- src/Engine/System/Executor.h | 2 - src/Engine/System/SystemBase.h | 2 +- src/Engine/System/SystemGraph.cpp | 4 +- src/Engine/System/SystemGraph.h | 2 +- src/Engine/ThreadPool.cpp | 7 ++ src/Engine/ThreadPool.h | 1 + src/Engine/Window/GameView.cpp | 2 +- src/Engine/Window/GameView.h | 1 - 19 files changed, 108 insertions(+), 84 deletions(-) diff --git a/res/shaders/BasePass.slang b/res/shaders/BasePass.slang index 2b1d37f..8921e89 100644 --- a/res/shaders/BasePass.slang +++ b/res/shaders/BasePass.slang @@ -26,10 +26,10 @@ float4 fragmentMain(in FragmentParameter params) : SV_Target { result += pLightEnv.directionalLights[i].illuminate(lightingParams, brdf); } - for(uint i = 0; i < pLightEnv.numPointLights; ++i) + for(uint i = 0; i < lightCount; ++i) { - //uint lightIndex = pLightCullingData.lightIndexList[startOffset + i]; - result += pLightEnv.pointLights[i].illuminate(lightingParams, brdf); + uint lightIndex = pLightCullingData.lightIndexList[startOffset + i]; + result += pLightEnv.pointLights[lightIndex].illuminate(lightingParams, brdf); } return float4(result, 1.0f); } diff --git a/res/shaders/lib/BRDF.slang b/res/shaders/lib/BRDF.slang index d38e1a4..6bf42d1 100644 --- a/res/shaders/lib/BRDF.slang +++ b/res/shaders/lib/BRDF.slang @@ -10,7 +10,6 @@ struct BlinnPhong : IBRDF float3 baseColor; float metallic; float3 normal; - float3 specular; float roughness; float sheen; @@ -18,7 +17,6 @@ struct BlinnPhong : IBRDF { metallic = 0; normal = float3(0, 0, 1); - specular = 0.5; roughness = 0.5; sheen = 1; } diff --git a/res/shaders/lib/MaterialParameter.slang b/res/shaders/lib/MaterialParameter.slang index 130eec5..f3466f8 100644 --- a/res/shaders/lib/MaterialParameter.slang +++ b/res/shaders/lib/MaterialParameter.slang @@ -1,11 +1,9 @@ import Common; -#define MAX_NUM_TEXCOORDS 8 - struct MaterialParameter { float3 position_WS; - float2 texCoords[MAX_NUM_TEXCOORDS]; + float2 texCoords[1]; float3 vertexColor; }; @@ -27,16 +25,13 @@ struct FragmentParameter float3 biTangent_WS : TANGENT1; float3 position_WS : POSITION2; float3 vertexColor : COLOR0; - float2 texCoords[MAX_NUM_TEXCOORDS] : TEXCOORD0; + float2 texCoords : TEXCOORD0; MaterialParameter getMaterialParameter() { MaterialParameter result; result.position_WS = position_WS; result.vertexColor = vertexColor; - for(int i = 0; i < MAX_NUM_TEXCOORDS; ++i) - { - result.texCoords[i] = texCoords[i]; - } + result.texCoords[0] = texCoords; return result; } LightingParameter getLightingParameter() @@ -57,7 +52,7 @@ struct VertexAttributes float3 tangent_MS; float3 biTangent_MS; float3 vertexColor; - float2 texCoords[MAX_NUM_TEXCOORDS]; + float2 texCoords; FragmentParameter getParameter(float4x4 transformMatrix) { float4 modelPos = float4(position_MS, 1); @@ -75,10 +70,7 @@ struct VertexAttributes result.position_WS = worldPos.xyz; result.position_CS = clipPos; result.vertexColor = vertexColor; - for(int i = 0; i < MAX_NUM_TEXCOORDS; ++i) - { - result.texCoords[i] = texCoords[i]; - } + result.texCoords = texCoords; return result; } }; diff --git a/res/shaders/lib/StaticMeshVertexData.slang b/res/shaders/lib/StaticMeshVertexData.slang index bbef82a..9d7d647 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[0] = float2(texCoords[2 * index + 0], texCoords[2 * index + 1]); + attributes.texCoords = 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/MeshLoader.cpp b/src/Editor/Asset/MeshLoader.cpp index da5651d..37bdba1 100644 --- a/src/Editor/Asset/MeshLoader.cpp +++ b/src/Editor/Asset/MeshLoader.cpp @@ -378,8 +378,8 @@ void MeshLoader::loadMaterials(const aiScene* scene, const Mapkey = subKey; - expressions.back()->inputs["lhs"].source = "1"; - expressions.back()->inputs["rhs"].source = mulKey; + expressions.back()->inputs["lhs"].source = mulKey; + expressions.back()->inputs["rhs"].source = "float3(1,1,1)"; outputNormal = subKey; } @@ -388,7 +388,7 @@ void MeshLoader::loadMaterials(const aiScene* scene, const Map indices(mesh->mNumFaces * 3); //#pragma omp parallel for - for (int32 faceIndex = 0; faceIndex < mesh->mNumFaces; ++faceIndex) + for (size_t faceIndex = 0; faceIndex < mesh->mNumFaces; ++faceIndex) { indices[faceIndex * 3 + 0] = mesh->mFaces[faceIndex].mIndices[0]; indices[faceIndex * 3 + 1] = mesh->mFaces[faceIndex].mIndices[1]; @@ -503,7 +503,7 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const ArrayloadMesh(id, indices, meshlets); - collider.physicsMesh.addCollider(positions, indices, Matrix4(1.0f)); + //collider.physicsMesh.addCollider(positions, indices, Matrix4(1.0f)); globalMeshes[meshIndex] = new Mesh(); globalMeshes[meshIndex]->vertexData = vertexData; diff --git a/src/Editor/Window/SceneView.h b/src/Editor/Window/SceneView.h index 4e32837..1ec01eb 100644 --- a/src/Editor/Window/SceneView.h +++ b/src/Editor/Window/SceneView.h @@ -31,7 +31,6 @@ private: RenderGraph renderGraph; - ThreadPool pool; ViewportControl cameraSystem; virtual void keyCallback(KeyCode code, InputAction action, KeyModifier modifier) override; diff --git a/src/Engine/Containers/Array.h b/src/Engine/Containers/Array.h index ee46952..64f322e 100644 --- a/src/Engine/Containers/Array.h +++ b/src/Engine/Containers/Array.h @@ -632,13 +632,14 @@ private: } else { + allocated = calculateGrowth(newSize); // The array is not big enough, so we make a new one - T *newData = allocateArray(newSize); + T *newData = allocateArray(allocated); if constexpr (std::is_integral_v || std::is_floating_point_v) { std::memcpy(newData, _data, sizeof(T) * arraySize); - std::memset(&newData[arraySize], 0, sizeof(T) * (newSize - arraySize)); + std::memset(&newData[arraySize], 0, sizeof(T) * (allocated - arraySize)); } else { @@ -648,14 +649,13 @@ private: newData[i] = std::forward(_data[i]); } // As well as default initialize the others - for (size_type i = arraySize; i < newSize; ++i) + for (size_type i = arraySize; i < allocated; ++i) { std::allocator_traits::construct(allocator, &newData[i], value); } } deallocateArray(_data, allocated); arraySize = newSize; - allocated = newSize; _data = newData; } } diff --git a/src/Engine/Graphics/Shader.cpp b/src/Engine/Graphics/Shader.cpp index f234549..727a525 100644 --- a/src/Engine/Graphics/Shader.cpp +++ b/src/Engine/Graphics/Shader.cpp @@ -1,4 +1,5 @@ #include "Shader.h" +#include "ThreadPool.h" #include "Graphics/Initializer.h" #include "Graphics/RenderPass/DepthPrepass.h" #include "Graphics/RenderPass/BasePass.h" @@ -51,57 +52,83 @@ void ShaderCompiler::registerRenderPass(Gfx::PPipelineLayout layout, std::string void ShaderCompiler::compile() { + List> work; for (const auto& [name, pass] : passes) { - ShaderPermutation permutation; - if (pass.useMeshShading) - { - permutation.setMeshFile(pass.mainFile); - } - else - { - permutation.setVertexFile(pass.mainFile); - } - if (pass.hasFragmentShader) - { - permutation.setFragmentFile(pass.fragmentFile); - } - if (pass.hasTaskShader) - { - permutation.setTaskFile(pass.taskFile); - } for (const auto& [vdName, vd] : vertexData) { - permutation.setVertexData(vd->getTypeName()); if (pass.useMaterial) { for (const auto& [matName, mat] : materials) { - OPipelineLayout layout = graphics->createPipelineLayout(pass.baseLayout->getName(), pass.baseLayout); - layout->addDescriptorLayout(vd->getVertexDataLayout()); - layout->addDescriptorLayout(vd->getInstanceDataLayout()); - layout->addDescriptorLayout(mat->getDescriptorLayout()); - permutation.setMaterial(mat->getName()); - createShaders(permutation, std::move(layout)); + work.add([=]() { + ShaderPermutation permutation; + if (pass.useMeshShading) + { + permutation.setMeshFile(pass.mainFile); + } + else + { + permutation.setVertexFile(pass.mainFile); + } + if (pass.hasFragmentShader) + { + permutation.setFragmentFile(pass.fragmentFile); + } + if (pass.hasTaskShader) + { + permutation.setTaskFile(pass.taskFile); + } + permutation.setVertexData(vd->getTypeName()); + OPipelineLayout layout = graphics->createPipelineLayout(pass.baseLayout->getName(), pass.baseLayout); + layout->addDescriptorLayout(vd->getVertexDataLayout()); + layout->addDescriptorLayout(vd->getInstanceDataLayout()); + layout->addDescriptorLayout(mat->getDescriptorLayout()); + permutation.setMaterial(mat->getName()); + createShaders(permutation, std::move(layout)); + }); } } else { - OPipelineLayout layout = graphics->createPipelineLayout(pass.baseLayout->getName(), pass.baseLayout); - layout->addDescriptorLayout(vd->getVertexDataLayout()); - layout->addDescriptorLayout(vd->getInstanceDataLayout()); - createShaders(permutation, std::move(layout)); + work.add([=]() { + ShaderPermutation permutation; + if (pass.useMeshShading) + { + permutation.setMeshFile(pass.mainFile); + } + else + { + permutation.setVertexFile(pass.mainFile); + } + if (pass.hasFragmentShader) + { + permutation.setFragmentFile(pass.fragmentFile); + } + if (pass.hasTaskShader) + { + permutation.setTaskFile(pass.taskFile); + } + permutation.setVertexData(vd->getTypeName()); + OPipelineLayout layout = graphics->createPipelineLayout(pass.baseLayout->getName(), pass.baseLayout); + layout->addDescriptorLayout(vd->getVertexDataLayout()); + layout->addDescriptorLayout(vd->getInstanceDataLayout()); + createShaders(permutation, std::move(layout)); + }); } } } + getThreadPool().runAndWait(std::move(work)); } void ShaderCompiler::createShaders(ShaderPermutation permutation, Gfx::OPipelineLayout layout) { - std::scoped_lock lock(shadersLock); PermutationId perm = PermutationId(permutation); - if (shaders.contains(perm)) - return; + { + std::scoped_lock lock(shadersLock); + if (shaders.contains(perm)) + return; + } ShaderCollection collection; collection.pipelineLayout = std::move(layout); @@ -142,5 +169,8 @@ void ShaderCompiler::createShaders(ShaderPermutation permutation, Gfx::OPipeline collection.fragmentShader = graphics->createFragmentShader(createInfo); } collection.pipelineLayout->create(); - shaders[perm] = std::move(collection); + { + std::scoped_lock lock(shadersLock); + shaders[perm] = std::move(collection); + } } diff --git a/src/Engine/Graphics/Shader.h b/src/Engine/Graphics/Shader.h index 70e07c6..f2b8406 100644 --- a/src/Engine/Graphics/Shader.h +++ b/src/Engine/Graphics/Shader.h @@ -58,10 +58,10 @@ struct ShaderPermutation char fragmentFile[32]; char vertexDataName[32]; char materialName[64]; - uint8 hasFragment : 1; - uint8 useMeshShading : 1; - uint8 hasTaskShader : 1; - uint8 useMaterial : 1; + uint8 hasFragment; + uint8 useMeshShading; + uint8 hasTaskShader; + uint8 useMaterial; //TODO: lightmapping etc ShaderPermutation() { diff --git a/src/Engine/Graphics/StaticMeshVertexData.cpp b/src/Engine/Graphics/StaticMeshVertexData.cpp index fa3c011..9d117d7 100644 --- a/src/Engine/Graphics/StaticMeshVertexData.cpp +++ b/src/Engine/Graphics/StaticMeshVertexData.cpp @@ -105,12 +105,12 @@ void StaticMeshVertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveB Array tan(numVertices); Array bit(numVertices); Array col(numVertices); - 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)); + 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); diff --git a/src/Engine/System/ComponentSystem.h b/src/Engine/System/ComponentSystem.h index 05c72e8..71def7c 100644 --- a/src/Engine/System/ComponentSystem.h +++ b/src/Engine/System/ComponentSystem.h @@ -24,7 +24,7 @@ public: return Dependencies<>(); } template - void setupView(Dependencies, ThreadPool& pool) + void setupView(Dependencies) { List> work; registry.view().each([&](Components&... comp, Deps&... deps){ @@ -33,12 +33,12 @@ public: update(comp...); }); }); - pool.runAndWait(std::move(work)); + getThreadPool().runAndWait(std::move(work)); } - virtual void run(ThreadPool& pool, double delta) override + virtual void run(double delta) override { - SystemBase::run(pool, delta); - setupView((getDependencies() | ...), pool); + SystemBase::run(delta); + setupView((getDependencies() | ...)); } virtual void update() override {} virtual void update(Components&... components) = 0; diff --git a/src/Engine/System/Executor.h b/src/Engine/System/Executor.h index cc6014c..2e76d40 100644 --- a/src/Engine/System/Executor.h +++ b/src/Engine/System/Executor.h @@ -11,8 +11,6 @@ class Executor public: Executor(); ~Executor(); -private: - ThreadPool pool; }; } // namespace System } // namespace Seele diff --git a/src/Engine/System/SystemBase.h b/src/Engine/System/SystemBase.h index afd08ad..a40ccee 100644 --- a/src/Engine/System/SystemBase.h +++ b/src/Engine/System/SystemBase.h @@ -12,7 +12,7 @@ class SystemBase public: SystemBase(PScene scene) : registry(scene->registry), scene(scene) {} virtual ~SystemBase() {} - virtual void run(ThreadPool&, double delta) + virtual void run(double delta) { deltaTime = delta; update(); diff --git a/src/Engine/System/SystemGraph.cpp b/src/Engine/System/SystemGraph.cpp index 047d5d3..6a7e02d 100644 --- a/src/Engine/System/SystemGraph.cpp +++ b/src/Engine/System/SystemGraph.cpp @@ -7,9 +7,9 @@ void SystemGraph::addSystem(System::OSystemBase system) systems.add(std::move(system)); } -void SystemGraph::run(ThreadPool& threadPool, float deltaTime) +void SystemGraph::run(float deltaTime) { for(auto& system : systems) { - system->run(threadPool, deltaTime); + system->run(deltaTime); } } diff --git a/src/Engine/System/SystemGraph.h b/src/Engine/System/SystemGraph.h index d294350..54a5a14 100644 --- a/src/Engine/System/SystemGraph.h +++ b/src/Engine/System/SystemGraph.h @@ -9,7 +9,7 @@ class SystemGraph { public: void addSystem(System::OSystemBase system); - void run(ThreadPool& threadPool, float deltaTime); + void run(float deltaTime); private: Array systems; }; diff --git a/src/Engine/ThreadPool.cpp b/src/Engine/ThreadPool.cpp index cf52c31..6578445 100644 --- a/src/Engine/ThreadPool.cpp +++ b/src/Engine/ThreadPool.cpp @@ -61,3 +61,10 @@ void ThreadPool::work() } } } + +static ThreadPool threadPool; + +ThreadPool& Seele::getThreadPool() +{ + return threadPool; +} diff --git a/src/Engine/ThreadPool.h b/src/Engine/ThreadPool.h index 15b0899..5ff7f3c 100644 --- a/src/Engine/ThreadPool.h +++ b/src/Engine/ThreadPool.h @@ -26,4 +26,5 @@ private: Task currentTask; bool running = true; }; +ThreadPool& getThreadPool(); } \ No newline at end of file diff --git a/src/Engine/Window/GameView.cpp b/src/Engine/Window/GameView.cpp index bb9a8fb..2257cc9 100644 --- a/src/Engine/Window/GameView.cpp +++ b/src/Engine/Window/GameView.cpp @@ -43,7 +43,7 @@ void GameView::update() { vd->resetMeshData(); } - systemGraph->run(threadPool, updateTime); + systemGraph->run(updateTime); scene->update(updateTime); for (VertexData* vd : VertexData::getList()) { diff --git a/src/Engine/Window/GameView.h b/src/Engine/Window/GameView.h index 976fbde..ac1357d 100644 --- a/src/Engine/Window/GameView.h +++ b/src/Engine/Window/GameView.h @@ -36,7 +36,6 @@ protected: PSystemGraph systemGraph; System::PKeyboardInput keyboardSystem; - ThreadPool threadPool; float updateTime = 0; virtual void keyCallback(Seele::KeyCode code, Seele::InputAction action, Seele::KeyModifier modifier) override;