diff --git a/CMakeSettings.json b/CMakeSettings.json index 07b444e..84f0da2 100644 --- a/CMakeSettings.json +++ b/CMakeSettings.json @@ -15,7 +15,7 @@ { "name": "Release", "generator": "Visual Studio 17 2022 Win64", - "configurationType": "RelWithDebInfo", + "configurationType": "Release", "buildRoot": "C:/Users/Dynamitos/Seele/bin/", "cmakeCommandArgs": "-DCMAKE_DEBUG_POSTFIX=\"\" -DCMAKE_PLATFORM=x64", "buildCommandArgs": "", @@ -23,6 +23,19 @@ "inheritEnvironments": [ "msvc_x64" ], "intelliSenseMode": "windows-msvc-x64", "installRoot": "C:/Program Files/Seele" + }, + { + "name": "RelWithDebInfo", + "generator": "Visual Studio 17 2022 Win64", + "configurationType": "RelWithDebInfo", + "buildRoot": "C:/Users/Dynamitos/Seele/bin/", + "installRoot": "C:/Program Files/Seele", + "cmakeCommandArgs": "-DCMAKE_DEBUG_POSTFIX=\"\" -DCMAKE_PLATFORM=x64", + "buildCommandArgs": "", + "ctestCommandArgs": "", + "inheritEnvironments": [ "msvc_x64" ], + "intelliSenseMode": "windows-msvc-x64", + "variables": [] } ] } \ No newline at end of file diff --git a/res/shaders/BasePass.slang b/res/shaders/BasePass.slang index c1eb669..7cb7899 100644 --- a/res/shaders/BasePass.slang +++ b/res/shaders/BasePass.slang @@ -19,6 +19,7 @@ float4 fragmentMain(in FragmentParameter params) : SV_Target { MaterialParameter materialParams = params.getMaterialParameter(); LightingParameter lightingParams = params.getLightingParameter(); + let brdf = pMaterial.prepare(materialParams); float3 result = float3(0, 0, 0); for(int i = 0; i < pLightEnv.numDirectionalLights; ++i) diff --git a/res/shaders/lib/BRDF.slang b/res/shaders/lib/BRDF.slang index 82b83e6..5c637c1 100644 --- a/res/shaders/lib/BRDF.slang +++ b/res/shaders/lib/BRDF.slang @@ -2,7 +2,7 @@ import Common; interface IBRDF { - float3 evaluate(float3 viewDir_TS, float3 lightDir_TS, float3 lightColor); + float3 evaluate(float3x3 tbn, float3 viewDir_WS, float3 lightDir_WS, float3 lightColor); }; struct BlinnPhong : IBRDF @@ -23,13 +23,14 @@ struct BlinnPhong : IBRDF sheen = 1; } - float3 evaluate(float3 viewDir_TS, float3 lightDir_TS, float3 lightColor) + float3 evaluate(float3x3 tbn, float3 viewDir_WS, float3 lightDir_WS, float3 lightColor) { - float nDotL = saturate(dot(normal, lightDir_TS)); - float3 h = normalize(lightDir_TS + viewDir_TS); - float nDotH = saturate(dot(normal, h)); + float3 normal_WS = mul(tbn, normal); + float diffuse = max(dot(normal_WS, lightDir_WS), 0); + float3 h = lightDir_WS + viewDir_WS; + float specular = dot(normal_WS, h); - return baseColor; + return lightDir_WS * 2 + float3(1, 1, 1);//baseColor * (diffuse + specular) * lightColor; } }; @@ -48,7 +49,7 @@ struct DisneyBRDF : IBRDF float clearCoat = 0; float clearCoatGloss = 1; - float3 evaluate(float3 viewDir_TS, float3 lightDir_TS, float3 lightColor) + float3 evaluate(float3x3 tbn, float3 viewDir_TS, float3 lightDir_TS, float3 lightColor) { return baseColor; } diff --git a/res/shaders/lib/LightEnv.slang b/res/shaders/lib/LightEnv.slang index 044acc2..174e74a 100644 --- a/res/shaders/lib/LightEnv.slang +++ b/res/shaders/lib/LightEnv.slang @@ -14,8 +14,7 @@ struct DirectionalLight : ILightEnv float3 illuminate(LightingParameter params, B brdf) { - float3 lightDir_TS = mul(params.tbn, normalize(direction.xyz)); - return brdf.evaluate(params.viewDir_TS, -lightDir_TS, color.xyz); + return brdf.evaluate(params.tbn, params.viewDir_WS, -normalize(direction.xyz), color.xyz); } }; @@ -26,11 +25,10 @@ struct PointLight : ILightEnv float3 illuminate(LightingParameter params, B brdf) { - float3 position_TS = mul(params.tbn, position_WS.xyz); - float3 lightDir_TS = position_TS - params.position_TS; - float d = length(lightDir_TS); + float3 lightDir_WS = position_WS.xyz - params.position_WS; + float d = length(lightDir_WS); float illuminance = max(1 - d / colorRange.w, 0); - return illuminance * brdf.evaluate(params.viewDir_TS, normalize(lightDir_TS), colorRange.xyz); + return brdf.evaluate(params.tbn, params.viewDir_WS, normalize(lightDir_WS), colorRange.xyz); } bool insidePlane(Plane plane) diff --git a/res/shaders/lib/MaterialParameter.slang b/res/shaders/lib/MaterialParameter.slang index 9bebd86..b88ec31 100644 --- a/res/shaders/lib/MaterialParameter.slang +++ b/res/shaders/lib/MaterialParameter.slang @@ -2,7 +2,6 @@ import Common; struct MaterialParameter { - float3 position_TS; float3 position_WS; float2 texCoords; float3 vertexColor; @@ -11,18 +10,16 @@ struct MaterialParameter // data used by light environment struct LightingParameter { - // world to tangent space float3x3 tbn; - float3 position_TS; - float3 viewDir_TS; + float3 position_WS; + float3 viewDir_WS; }; // data passed to fragment shader struct FragmentParameter { float4 position_CS : SV_Position; - float3 position_TS : POSITION0; - float3 viewDir_TS : POSITION1; + float3 viewDir_WS : POSITION1; float3 normal_WS : NORMAL0; float3 tangent_WS : TANGENT0; float3 biTangent_WS : TANGENT1; @@ -32,7 +29,6 @@ struct FragmentParameter MaterialParameter getMaterialParameter() { MaterialParameter result; - result.position_TS = position_TS; result.position_WS = position_WS; result.texCoords = texCoords; result.vertexColor = vertexColor; @@ -41,9 +37,9 @@ struct FragmentParameter LightingParameter getLightingParameter() { LightingParameter result; - result.tbn = transpose(float3x3(normalize(tangent_WS), normalize(biTangent_WS), normalize(normal_WS))); - result.position_TS = position_TS; - result.viewDir_TS = viewDir_TS; + result.tbn = float3x3(normalize(tangent_WS), normalize(biTangent_WS), normalize(normal_WS)); + result.position_WS = position_WS; + result.viewDir_WS = viewDir_WS; return result; } }; @@ -63,14 +59,11 @@ struct VertexAttributes float4 worldPos = mul(transformMatrix, modelPos); float4 viewPos = mul(pViewParams.viewMatrix, worldPos); float4 clipPos = mul(pViewParams.projectionMatrix, viewPos); - float3 tangent_WS = mul(transformMatrix, float4(normalize(tangent_MS), 0)).xyz; - float3 biTangent_WS = mul(transformMatrix, float4(normalize(biTangent_MS), 0)).xyz; - float3 normal_WS = mul(transformMatrix, float4(normalize(normal_MS), 0)).xyz; - // Transforms from world space into tangent space - float3x3 tbn = transpose(float3x3(tangent_WS, biTangent_WS, normal_WS)); + float3 tangent_WS = mul(float3x3(transformMatrix), normalize(tangent_MS)); + float3 biTangent_WS = mul(float3x3(transformMatrix), normalize(biTangent_MS)); + float3 normal_WS = mul(float3x3(transformMatrix), normalize(normal_MS)); FragmentParameter result; - result.position_TS = mul(tbn, worldPos.xyz); - result.viewDir_TS = mul(tbn, pViewParams.cameraPos_WS.xyz - worldPos.xyz); + result.viewDir_WS = pViewParams.cameraPos_WS.xyz - worldPos.xyz; result.normal_WS = normal_WS; result.tangent_WS = tangent_WS; result.biTangent_WS = biTangent_WS; diff --git a/src/Editor/Asset/MeshLoader.cpp b/src/Editor/Asset/MeshLoader.cpp index 9651743..9d379aa 100644 --- a/src/Editor/Asset/MeshLoader.cpp +++ b/src/Editor/Asset/MeshLoader.cpp @@ -109,7 +109,7 @@ void MeshLoader::loadMaterials(const aiScene* scene, const std::string& baseName if(material->GetTexture(aiTextureType_NORMALS, 0, &texPath) == AI_SUCCESS) { } - std::string outMatFilename = matCode["name"].get().append(".asset"); + std::string outMatFilename = matCode["name"].get().append(".json"); std::ofstream outMatFile = std::ofstream(meshDirectory / outMatFilename); outMatFile << std::setw(4) << matCode; outMatFile.flush(); diff --git a/src/Editor/main.cpp b/src/Editor/main.cpp index f1865da..c9c6e52 100644 --- a/src/Editor/main.cpp +++ b/src/Editor/main.cpp @@ -217,14 +217,14 @@ int main() WindowCreateInfo mainWindowInfo; mainWindowInfo.title = "SeeleEngine"; - mainWindowInfo.width = 1280; - mainWindowInfo.height = 720; + mainWindowInfo.width = 1920; + mainWindowInfo.height = 1080; mainWindowInfo.numSamples = 1; mainWindowInfo.preferredFormat = Gfx::SE_FORMAT_B8G8R8A8_SRGB; auto window = windowManager->addWindow(graphics, mainWindowInfo); ViewportCreateInfo sceneViewInfo; - sceneViewInfo.dimensions.size.x = 1280; - sceneViewInfo.dimensions.size.y = 720; + sceneViewInfo.dimensions.size.x = 1920; + sceneViewInfo.dimensions.size.y = 1080; sceneViewInfo.dimensions.offset.x = 0; sceneViewInfo.dimensions.offset.y = 0; OGameView sceneView = new Editor::PlayView(graphics, window, sceneViewInfo, binaryPath.generic_string()); diff --git a/src/Engine/Actor/CameraActor.cpp b/src/Engine/Actor/CameraActor.cpp index 79b6e4d..6ba8af5 100644 --- a/src/Engine/Actor/CameraActor.cpp +++ b/src/Engine/Actor/CameraActor.cpp @@ -7,7 +7,7 @@ CameraActor::CameraActor(PScene scene) : Actor(scene) { attachComponent(); - accessComponent().setRelativeLocation(Vector(10, 5, 14)); + accessComponent().setPosition(Vector(10, 5, 14)); } CameraActor::~CameraActor() diff --git a/src/Engine/Component/Camera.cpp b/src/Engine/Component/Camera.cpp index cfb7bf7..b88a2bd 100644 --- a/src/Engine/Component/Camera.cpp +++ b/src/Engine/Component/Camera.cpp @@ -11,8 +11,8 @@ Camera::Camera() : viewMatrix(Matrix4()) , bNeedsViewBuild(false) { - yaw = 0; - pitch = 0.2; + yaw = 3.1415f/2; + pitch = 0; } Camera::~Camera() @@ -21,40 +21,37 @@ Camera::~Camera() void Camera::mouseMove(float deltaYaw, float deltaPitch) { - yaw -= deltaYaw / 500.f; + yaw += deltaYaw / 500.f; pitch += deltaPitch / 500.f; - //std::cout << "Yaw: " << yaw << " Pitch: " << pitch << std::endl; Vector cameraDirection = glm::normalize(Vector(cos(yaw) * cos(pitch), sin(pitch), sin(yaw) * cos(pitch))); - Vector xyz = glm::cross(cameraDirection, Vector(0, 0, 1)); - Quaternion result = Quaternion(glm::dot(cameraDirection, Vector(0, 0, 1)) + 1, xyz.x, xyz.y, xyz.z); - //std::cout << "Result " << Vector(0, 0, 1) * glm::normalize(result) << " cameraDirection: " << cameraDirection << std::endl; - getTransform().setRelativeRotation(result); + Matrix4 viewMat = glm::lookAt(getTransform().getPosition(), getTransform().getPosition() + cameraDirection, Vector(0, 1, 0)); + getTransform().setRotation(viewMat); // quaternion from matrix, janky but im bad at math bNeedsViewBuild = true; } void Camera::mouseScroll(float x) { - getTransform().addRelativeLocation(getTransform().getForward()*x); + getTransform().translate(getTransform().getForward()*x); bNeedsViewBuild = true; } void Camera::moveX(float amount) { - getTransform().addRelativeLocation(getTransform().getForward()*amount); + getTransform().translate(getTransform().getForward()*amount); bNeedsViewBuild = true; } void Camera::moveY(float amount) { - getTransform().addRelativeLocation(getTransform().getRight()*amount); + getTransform().translate(getTransform().getRight()*amount); bNeedsViewBuild = true; } void Camera::buildViewMatrix() { - Vector eyePos = getTransform().getPosition();//getAbsoluteTransform().getPosition(); - Vector lookAt = eyePos + glm::normalize(Vector(cos(yaw) * cos(pitch), sin(pitch), sin(yaw) * cos(pitch))); + Vector eyePos = getTransform().getPosition(); + Vector lookAt = eyePos + getTransform().getForward(); viewMatrix = glm::lookAt(eyePos, lookAt, Vector(0, 1, 0)); - + cameraPos = eyePos; bNeedsViewBuild = false; } diff --git a/src/Engine/Component/Camera.h b/src/Engine/Component/Camera.h index f3401ba..788e17d 100644 --- a/src/Engine/Component/Camera.h +++ b/src/Engine/Component/Camera.h @@ -21,7 +21,7 @@ struct Camera } Vector getCameraPosition() const { - return Vector(viewMatrix[3]); + return cameraPos; } void mouseMove(float deltaX, float deltaY); void mouseScroll(float x); @@ -29,6 +29,8 @@ struct Camera void moveY(float amount); void buildViewMatrix(); Matrix4 viewMatrix; + Vector cameraPos; + Vector cameraDirection; //Transforms relative to actor float yaw; float pitch; diff --git a/src/Engine/Component/Transform.cpp b/src/Engine/Component/Transform.cpp index a62c547..35476ed 100644 --- a/src/Engine/Component/Transform.cpp +++ b/src/Engine/Component/Transform.cpp @@ -4,7 +4,6 @@ using namespace Seele::Component; DEFINE_COMPONENT(Transform) - void Transform::setPosition(Vector pos) { transform.setPosition(pos); @@ -19,32 +18,15 @@ void Transform::setScale(Vector scale) { transform.setScale(scale); } - -void Transform::setRelativeLocation(Vector location) +void Transform::translate(Vector direction) { - transform = Math::Transform(location, transform.getRotation(), transform.getScale()); + transform.setPosition(transform.getPosition() + direction); } -void Transform::setRelativeRotation(Vector rotation) +void Transform::rotate(Quaternion quat) { - transform = Math::Transform(transform.getPosition(), Quaternion(rotation), transform.getScale()); + transform.setRotation(transform.getRotation() * quat); } -void Transform::setRelativeRotation(Quaternion rotation) +void Transform::scale(Vector scale) { - transform = Math::Transform(transform.getPosition(), rotation, transform.getScale()); -} -void Transform::setRelativeScale(Vector scale) -{ - transform = Math::Transform(transform.getPosition(), transform.getRotation(), scale); -} -void Transform::addRelativeLocation(Vector translation) -{ - transform = Math::Transform(transform.getPosition() + translation, transform.getRotation(), transform.getScale()); -} -void Transform::addRelativeRotation(Vector rotation) -{ - transform = Math::Transform(transform.getPosition(), transform.getRotation() * Quaternion(rotation), transform.getScale()); -} -void Transform::addRelativeRotation(Quaternion rotation) -{ - transform = Math::Transform(transform.getPosition(), transform.getRotation() * rotation, transform.getScale()); + transform.setScale(transform.getScale() + scale); } \ No newline at end of file diff --git a/src/Engine/Component/Transform.h b/src/Engine/Component/Transform.h index dd1187c..5a31693 100644 --- a/src/Engine/Component/Transform.h +++ b/src/Engine/Component/Transform.h @@ -21,15 +21,9 @@ struct Transform void setPosition(Vector pos); void setRotation(Quaternion quat); void setScale(Vector scale); - - void setRelativeLocation(Vector location); - void setRelativeRotation(Quaternion rotation); - void setRelativeRotation(Vector rotation); - void setRelativeScale(Vector scale); - - void addRelativeLocation(Vector translation); - void addRelativeRotation(Quaternion rotation); - void addRelativeRotation(Vector rotation); + void translate(Vector direction); + void rotate(Quaternion quat); + void scale(Vector scale); private: Math::Transform transform; }; diff --git a/src/Engine/Graphics/RenderPass/BasePass.cpp b/src/Engine/Graphics/RenderPass/BasePass.cpp index dc9afc9..5c6e553 100644 --- a/src/Engine/Graphics/RenderPass/BasePass.cpp +++ b/src/Engine/Graphics/RenderPass/BasePass.cpp @@ -165,8 +165,9 @@ void BasePass::render() { if (meshData.numIndices > 0) { - command->drawIndexed(meshData.numIndices, 1, meshData.firstIndex, meshData.indicesOffset, instanceOffset++); + command->drawIndexed(meshData.numIndices, 1, meshData.firstIndex, meshData.indicesOffset, instanceOffset); } + instanceOffset++; } } } diff --git a/src/Engine/Graphics/VertexData.cpp b/src/Engine/Graphics/VertexData.cpp index 0780d48..4f83fae 100644 --- a/src/Engine/Graphics/VertexData.cpp +++ b/src/Engine/Graphics/VertexData.cpp @@ -10,7 +10,7 @@ using namespace Seele; -constexpr static uint64 NUM_DEFAULT_ELEMENTS = 16 * 1024; +constexpr static uint64 NUM_DEFAULT_ELEMENTS = 1024 * 1024; void VertexData::resetMeshData() { @@ -166,7 +166,7 @@ MeshId VertexData::allocateVertexData(uint64 numVertices) head += numVertices; if (head > verticesAllocated) { - verticesAllocated = head; + verticesAllocated = std::max(head, verticesAllocated + NUM_DEFAULT_ELEMENTS); resizeBuffers(); } return res; diff --git a/src/Engine/Graphics/Vulkan/Descriptor.cpp b/src/Engine/Graphics/Vulkan/Descriptor.cpp index dceadff..189868f 100644 --- a/src/Engine/Graphics/Vulkan/Descriptor.cpp +++ b/src/Engine/Graphics/Vulkan/Descriptor.cpp @@ -62,12 +62,10 @@ void DescriptorLayout::create() PipelineLayout::~PipelineLayout() { - if (layoutHandle != VK_NULL_HANDLE) - { - vkDestroyPipelineLayout(graphics->getDevice(), layoutHandle, nullptr); - } } +Map cachedLayouts; + void PipelineLayout::create() { vulkanDescriptorLayouts.resize(descriptorSetLayouts.size()); @@ -105,7 +103,14 @@ void PipelineLayout::create() layoutHash = CRC::Calculate(createInfo.pPushConstantRanges, sizeof(VkPushConstantRange) * createInfo.pushConstantRangeCount, CRC::CRC_32()); layoutHash = CRC::Calculate(createInfo.pSetLayouts, sizeof(VkDescriptorSetLayout) * createInfo.setLayoutCount, CRC::CRC_32(), layoutHash); + if (cachedLayouts.contains(layoutHash)) + { + layoutHandle = cachedLayouts[layoutHash]; + return; + } + VK_CHECK(vkCreatePipelineLayout(graphics->getDevice(), &createInfo, nullptr, &layoutHandle)); + cachedLayouts[layoutHash] = layoutHandle; } void PipelineLayout::reset() @@ -391,7 +396,6 @@ Gfx::PDescriptorSet DescriptorPool::allocateDescriptorSet() { //If it hasnt been initialized, allocate it VK_CHECK(vkAllocateDescriptorSets(graphics->getDevice(), &allocInfo, &cachedHandles[setIndex]->setHandle)); - std::cout << "New descriptor " << cachedHandles[setIndex]->setHandle << std::endl; } cachedHandles[setIndex]->allocate(); diff --git a/src/Engine/Math/Transform.cpp b/src/Engine/Math/Transform.cpp index 3ef757d..35fee80 100644 --- a/src/Engine/Math/Transform.cpp +++ b/src/Engine/Math/Transform.cpp @@ -1,5 +1,6 @@ #include "Transform.h" #include +#include "Transform.h" using namespace Seele; using namespace Seele::Math; diff --git a/src/Engine/Scene/LightEnvironment.cpp b/src/Engine/Scene/LightEnvironment.cpp index 2c1063e..f2cec2c 100644 --- a/src/Engine/Scene/LightEnvironment.cpp +++ b/src/Engine/Scene/LightEnvironment.cpp @@ -60,10 +60,6 @@ void LightEnvironment::addPointLight(Component::PointLight pointLight) void LightEnvironment::commit() { - dirs.add(Component::DirectionalLight{ - .color = Vector4(1, 1, 1, 1), - .direction = Vector4(1, 0, 1, 0), - }); lightEnv.numDirectionalLights = dirs.size(); lightEnv.numPointLights = points.size(); lightEnvBuffer->updateContents(DataSource{ diff --git a/src/Engine/Scene/Scene.cpp b/src/Engine/Scene/Scene.cpp index 28e5c1f..2a1d9ca 100644 --- a/src/Engine/Scene/Scene.cpp +++ b/src/Engine/Scene/Scene.cpp @@ -25,7 +25,6 @@ Scene::~Scene() void Scene::update(float deltaTime) { - lightEnv->reset(); physics.update(deltaTime); } diff --git a/src/Engine/System/KeyboardInput.cpp b/src/Engine/System/KeyboardInput.cpp index 75b75a1..540ed08 100644 --- a/src/Engine/System/KeyboardInput.cpp +++ b/src/Engine/System/KeyboardInput.cpp @@ -22,6 +22,10 @@ void KeyboardInput::update(Component::KeyboardInput& input) input.mouseY = mouseY; input.mouse1 = mouse1; input.mouse2 = mouse2; + deltaX = mouseX - lastMouseX; + deltaY = mouseY - lastMouseY; + lastMouseX = mouseX; + lastMouseY = mouseY; } void KeyboardInput::keyCallback(KeyCode code, InputAction action, KeyModifier modifier) @@ -33,10 +37,6 @@ void KeyboardInput::mouseCallback(double x, double y) { mouseX = x; mouseY = y; - deltaX = x - lastMouseX; - deltaY = y - lastMouseY; - lastMouseX = x; - lastMouseY = y; } void KeyboardInput::mouseButtonCallback(MouseButton button, InputAction action, KeyModifier modifier) diff --git a/src/Engine/System/LightGather.cpp b/src/Engine/System/LightGather.cpp index 702d807..fe0d905 100644 --- a/src/Engine/System/LightGather.cpp +++ b/src/Engine/System/LightGather.cpp @@ -15,4 +15,11 @@ LightGather::~LightGather() void LightGather::update() { + lightEnv->reset(); + scene->view([this](Component::PointLight& pointLight) { + lightEnv->addPointLight(pointLight); + }); + scene->view([this](Component::DirectionalLight& dirLight) { + lightEnv->addDirectionalLight(dirLight); + }); }