diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 95749e2d7..220b41703 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -185,7 +185,7 @@ jobs: ios: name: iOS - runs-on: macos-13 + runs-on: macos-15 needs: version steps: @@ -217,7 +217,7 @@ jobs: tvos: name: tvOS - runs-on: macos-13 + runs-on: macos-15 needs: version steps: @@ -334,6 +334,7 @@ jobs: name: FreeBSD runs-on: ubuntu-latest needs: version + if: false steps: - name: Checkout repository @@ -582,6 +583,7 @@ jobs: - name: Upload FreeBSD uses: actions/upload-release-asset@v1 + if: false with: upload_url: ${{ steps.create_release.outputs.upload_url }} asset_path: ThunderEngine-freebsd-x64.7z diff --git a/engine/includes/commandbuffer.h b/engine/includes/commandbuffer.h index 3830bd4ac..378e6100c 100644 --- a/engine/includes/commandbuffer.h +++ b/engine/includes/commandbuffer.h @@ -14,25 +14,16 @@ class ComputeInstance; class RenderTarget; class Texture; class Mesh; +class Camera; struct Global { Matrix4 view; Matrix4 projection; - Matrix4 cameraView; - Matrix4 cameraProjection; - Matrix4 cameraProjectionInv; - Matrix4 cameraScreenToWorld; Matrix4 cameraWorldToScreen; Vector4 cameraPosition; - Vector4 cameraTarget; - Vector4 cameraScreen; - Vector4 shadowPageSize; - - float clip; - float time; - float deltaTime; - float padding[13]; + Vector4 cameraParams; + Vector4 params; }; class ENGINE_EXPORT CommandBuffer: public Object { @@ -41,6 +32,8 @@ class ENGINE_EXPORT CommandBuffer: public Object { public: CommandBuffer(); + void begin(); + virtual void dispatchCompute(ComputeInstance *shader, int32_t groupsX, int32_t groupsY, int32_t groupsZ); virtual void drawMesh(Mesh *mesh, uint32_t sub, uint32_t layer, MaterialInstance &instance); @@ -49,12 +42,12 @@ class ENGINE_EXPORT CommandBuffer: public Object { virtual void setViewProjection(const Matrix4 &view, const Matrix4 &projection); - virtual void setGlobalValue(const char *name, const Variant &value); - virtual void setGlobalTexture(const TString &name, Texture *texture); virtual void setViewport(int32_t x, int32_t y, int32_t width, int32_t height); + virtual void setCameraProperties(Camera *camera); + virtual void enableScissor(int32_t x, int32_t y, int32_t width, int32_t height); virtual void disableScissor(); @@ -68,8 +61,6 @@ class ENGINE_EXPORT CommandBuffer: public Object { virtual void beginDebugMarker(const TString &name); virtual void endDebugMarker(); - Vector2 viewport() const; - static Vector4 idToColor(uint32_t id); static bool isInited(); @@ -81,11 +72,6 @@ class ENGINE_EXPORT CommandBuffer: public Object { Material::Textures m_textures; - int32_t m_viewportX; - int32_t m_viewportY; - int32_t m_viewportWidth; - int32_t m_viewportHeight; - }; #endif // COMMANDBUFFER_H diff --git a/engine/includes/components/camera.h b/engine/includes/components/camera.h index a1c8fa986..f5343de26 100644 --- a/engine/includes/components/camera.h +++ b/engine/includes/components/camera.h @@ -14,7 +14,7 @@ class ENGINE_EXPORT Camera : public Component { A_PROPERTY(float, near, Camera::nearPlane, Camera::setNear), A_PROPERTY(float, far, Camera::farPlane, Camera::setFar), A_PROPERTY(float, size, Camera::orthoSize, Camera::setOrthoSize), - A_PROPERTY(float, focalDistance, Camera::focal, Camera::setFocal), + A_PROPERTY(float, focalDistance, Camera::focalDistance, Camera::setFocalDistance), A_PROPERTYEX(Vector4, backgroundColor, Camera::color, Camera::setColor, "editor=Color"), A_PROPERTY(bool, orthographic, Camera::orthographic, Camera::setOrthographic) ) @@ -46,8 +46,8 @@ class ENGINE_EXPORT Camera : public Component { float farPlane() const; void setFar(const float distance); - float focal() const; - void setFocal(const float focal); + float focalDistance() const; + void setFocalDistance(const float focal); float fov() const; void setFov(const float angle); diff --git a/engine/includes/pipelinecontext.h b/engine/includes/pipelinecontext.h index 69f22a13c..e86997bf8 100644 --- a/engine/includes/pipelinecontext.h +++ b/engine/includes/pipelinecontext.h @@ -77,6 +77,7 @@ class ENGINE_EXPORT PipelineContext : public Object { void setCurrentCamera(Camera *camera); Camera *currentCamera() const; + Vector2 size() const; void resize(int32_t width, int32_t height); void invalidateTasks(); @@ -100,9 +101,6 @@ class ENGINE_EXPORT PipelineContext : public Object { Callbacks m_postObservers; - Matrix4 m_cameraView; - Matrix4 m_cameraProjection; - AABBox m_worldBound; RenderList m_sceneRenderables; diff --git a/engine/src/commandbuffer.cpp b/engine/src/commandbuffer.cpp index d7365cee1..3ee5dbc7f 100644 --- a/engine/src/commandbuffer.cpp +++ b/engine/src/commandbuffer.cpp @@ -1,6 +1,8 @@ #include "commandbuffer.h" -#include +#include "components/camera.h" +#include "components/transform.h" +#include "timer.h" static bool s_Inited = false; @@ -13,12 +15,14 @@ static bool s_Inited = false; It provides methods for issuing rendering commands, setting global parameters, and managing textures. */ -CommandBuffer::CommandBuffer() : - m_viewportX(0), - m_viewportY(0), - m_viewportWidth(1), - m_viewportHeight(1) { +CommandBuffer::CommandBuffer() { + uint32_t size = Texture::maxTextureSize(); + m_global.params.x = 1.0f / (float)size; +} +void CommandBuffer::begin() { + m_global.params.y = Timer::time(); + m_global.params.z = Timer::deltaTime(); } /*! Dispatches a compute \a shader with the specified workgroup dimensions. @@ -79,28 +83,7 @@ void CommandBuffer::setInited() { void CommandBuffer::setViewProjection(const Matrix4 &view, const Matrix4 &projection) { m_global.view = view; m_global.projection = projection; -} -/*! - Sets a global \a value based on its \a name. -*/ -void CommandBuffer::setGlobalValue(const char *name, const Variant &value) { - static const std::unordered_map> offsets = { - {"camera.position", std::make_pair(offsetof(Global, cameraPosition), sizeof(Global::cameraPosition))}, - {"camera.target", std::make_pair(offsetof(Global, cameraTarget), sizeof(Global::cameraTarget))}, - {"camera.view", std::make_pair(offsetof(Global, cameraView), sizeof(Global::cameraView))}, - {"camera.projectionInv", std::make_pair(offsetof(Global, cameraProjectionInv), sizeof(Global::cameraProjectionInv))}, - {"camera.projection", std::make_pair(offsetof(Global, cameraProjection), sizeof(Global::cameraProjection))}, - {"camera.screenToWorld", std::make_pair(offsetof(Global, cameraScreenToWorld), sizeof(Global::cameraScreenToWorld))}, - {"camera.worldToScreen", std::make_pair(offsetof(Global, cameraWorldToScreen), sizeof(Global::cameraWorldToScreen))}, - {"camera.screen", std::make_pair(offsetof(Global, cameraScreen), sizeof(Global::cameraScreen))}, - {"shadow.pageSize", std::make_pair(offsetof(Global, shadowPageSize), sizeof(Global::shadowPageSize))}, - }; - - auto it = offsets.find(name); - if(it != offsets.end()) { - void *src = value.data(); - memcpy(reinterpret_cast(&m_global) + it->second.first, src, it->second.second); - } + m_global.cameraWorldToScreen = projection * view; } /*! Sets a global \a texture based on its \a name. @@ -157,12 +140,6 @@ void CommandBuffer::beginDebugMarker(const TString &name) { */ void CommandBuffer::endDebugMarker() { -} -/*! - Returns Vector2 representing the viewport dimensions. -*/ -Vector2 CommandBuffer::viewport() const { - return Vector2(m_viewportWidth, m_viewportHeight); } /*! Sets the viewport dimensions. @@ -170,12 +147,21 @@ Vector2 CommandBuffer::viewport() const { \a width and \a height viewport dimensions. */ void CommandBuffer::setViewport(int32_t x, int32_t y, int32_t width, int32_t height) { - m_viewportX = x; - m_viewportY = y; - m_viewportWidth = width; - m_viewportHeight = height; + m_global.cameraParams.z = 1.0f / (float)width; + m_global.cameraParams.w = 1.0f / (float)height; +} +/*! + Sets the \a camera specific global variables. + This function sets up view, projection and clipping planes global shader variables. +*/ +void CommandBuffer::setCameraProperties(Camera *camera) { + setViewProjection(camera->viewMatrix(), camera->projectionMatrix()); + + Transform *t = camera->transform(); - setGlobalValue("camera.screen", Vector4(width, height, 1.0f / (float)width, 1.0f / (float)height)); + m_global.cameraPosition = t->worldPosition(); + m_global.cameraParams.x = camera->nearPlane(); + m_global.cameraParams.y = camera->farPlane(); } /*! Enables scissor testing with the specified parameters. diff --git a/engine/src/components/camera.cpp b/engine/src/components/camera.cpp index 04aba8a1c..7be6d775f 100644 --- a/engine/src/components/camera.cpp +++ b/engine/src/components/camera.cpp @@ -162,15 +162,14 @@ void Camera::setRatio(float ratio) { /*! Returns a focal distance for the camera. */ -float Camera::focal() const { +float Camera::focalDistance() const { return m_focal; } /*! Sets a \a focal distance for the camera. */ -void Camera::setFocal(const float focal) { +void Camera::setFocalDistance(const float focal) { m_focal = focal; - recalcProjection(); } /*! Returns the color with which the screen will be cleared. diff --git a/engine/src/editor/viewport/cameracontroller.cpp b/engine/src/editor/viewport/cameracontroller.cpp index ccc0c188f..7ae5dbcac 100644 --- a/engine/src/editor/viewport/cameracontroller.cpp +++ b/engine/src/editor/viewport/cameracontroller.cpp @@ -48,7 +48,7 @@ CameraController::CameraController() : Actor *actor = Engine::composeActor(gCamera); m_activeCamera = actor->getComponent(); - m_activeCamera->setFocal(10.0f); + m_activeCamera->setFocalDistance(10.0f); m_activeCamera->setOrthoSize(10.0f); m_activeCamera->setColor(Vector4(0.2f, 0.2f, 0.2f, 0.0f)); @@ -138,7 +138,7 @@ void CameraController::update() { } } else if(Input::isMouseButton(Input::MOUSE_MIDDLE) && !m_blockMove) { Transform *t = m_activeCamera->transform(); - float mult = m_activeCamera->orthographic() ? 1.0f : m_activeCamera->focal() * 0.1f; + float mult = m_activeCamera->orthographic() ? 1.0f : m_activeCamera->focalDistance() * 0.1f; cameraMove((t->quaternion() * p) * mult); m_saved = Vector2(pos.x, pos.y); } @@ -166,7 +166,7 @@ void CameraController::move() { } if(m_focalLengthTarget > 0.0f) { - m_activeCamera->setFocal(MIX(m_activeCamera->focal(), m_focalLengthTarget, m_transferProgress)); + m_activeCamera->setFocalDistance(MIX(m_activeCamera->focalDistance(), m_focalLengthTarget, m_transferProgress)); } m_transferProgress += 2.0f * DT; @@ -185,7 +185,7 @@ void CameraController::move() { } if(m_focalLengthTarget > 0.0f) { - m_activeCamera->setFocal(m_focalLengthTarget); + m_activeCamera->setFocalDistance(m_focalLengthTarget); } } } @@ -196,7 +196,7 @@ void CameraController::move() { dir.normalize(); Vector3 delta = (dir * m_cameraSpeed.z) + dir.cross(Vector3(0.0f, 1.0f, 0.0f)) * m_cameraSpeed.x; - t->setPosition(pos - delta * m_activeCamera->focal() * 0.1f); + t->setPosition(pos - delta * m_activeCamera->focalDistance() * 0.1f); m_cameraSpeed -= m_cameraSpeed * 10.0f * DT; if(m_cameraSpeed.length() <= .01f) { @@ -270,9 +270,9 @@ void CameraController::cameraZoom(float delta) { m_activeCamera->setOrthoSize(scale); } else { float scale = delta * 0.01f; - float focal = CLAMP(m_activeCamera->focal() - scale, m_zoomLimit.x, m_zoomLimit.y); + float focal = CLAMP(m_activeCamera->focalDistance() - scale, m_zoomLimit.x, m_zoomLimit.y); - m_activeCamera->setFocal(focal); + m_activeCamera->setFocalDistance(focal); Transform *t = m_activeCamera->transform(); t->setPosition(t->position() - t->quaternion() * Vector3(0.0f, 0.0f, scale)); @@ -285,15 +285,15 @@ void CameraController::cameraRotate(const Vector3 &delta) { Vector3 euler = t->rotation() - delta; if(!m_cameraFree) { - Vector3 dir = t->position() - t->quaternion() * Vector3(0.0f, 0.0f, m_activeCamera->focal()); - t->setPosition(dir + Quaternion(euler) * Vector3(0.0f, 0.0f, m_activeCamera->focal())); + Vector3 dir = t->position() - t->quaternion() * Vector3(0.0f, 0.0f, m_activeCamera->focalDistance()); + t->setPosition(dir + Quaternion(euler) * Vector3(0.0f, 0.0f, m_activeCamera->focalDistance())); } t->setRotation(euler); } void CameraController::cameraMove(const Vector3 &delta) { Transform *t = m_activeCamera->transform(); - t->setPosition(t->position() - delta * ((m_activeCamera->orthographic()) ? m_activeCamera->orthoSize() : m_activeCamera->focal())); + t->setPosition(t->position() - delta * ((m_activeCamera->orthographic()) ? m_activeCamera->orthoSize() : m_activeCamera->focalDistance())); } void CameraController::restoreState(const VariantMap &state) { @@ -316,7 +316,7 @@ void CameraController::restoreState(const VariantMap &state) { it = state.find(gFocal); if(it != state.end()) { - m_activeCamera->setFocal((*it).second.toFloat()); + m_activeCamera->setFocalDistance((*it).second.toFloat()); } it = state.find(gOrthoSize); @@ -338,7 +338,7 @@ VariantMap CameraController::saveState() const { result[gPosition] = t->position(); result[gRotation] = t->rotation(); result[gOrthographic] = m_activeCamera->orthographic(); - result[gFocal] = m_activeCamera->focal(); + result[gFocal] = m_activeCamera->focalDistance(); result[gOrthoSize] = m_activeCamera->orthoSize(); result[gGridAxis] = static_cast(m_gridAxis); } diff --git a/engine/src/pipelinecontext.cpp b/engine/src/pipelinecontext.cpp index 43181fa7e..82421cd09 100644 --- a/engine/src/pipelinecontext.cpp +++ b/engine/src/pipelinecontext.cpp @@ -24,7 +24,6 @@ #include -#include #include "frustum.h" @@ -57,9 +56,6 @@ PipelineContext::PipelineContext() : } setPipeline(Engine::loadResource(Engine::value(".pipeline", ".embedded/Deferred.pipeline").toString())); - - uint32_t size = Texture::maxTextureSize(); - m_buffer->setGlobalValue("shadow.pageSize", Vector4(1.0f / size, 1.0f / size, size, size)); } PipelineContext::~PipelineContext() { @@ -108,25 +104,19 @@ void PipelineContext::draw(Camera *camera) { void PipelineContext::setCurrentCamera(Camera *camera) { m_camera = camera; - m_cameraView = m_camera->viewMatrix(); - m_cameraProjection = m_camera->projectionMatrix(); - Matrix4 vp = m_cameraProjection * m_cameraView; - - Transform *c = m_camera->transform(); - - m_buffer->setGlobalValue("camera.position", Vector4(c->worldPosition(), m_camera->nearPlane())); - m_buffer->setGlobalValue("camera.target", Vector4(c->worldTransform().rotation() * Vector3(0.0f, 0.0f, 1.0f), m_camera->farPlane())); - m_buffer->setGlobalValue("camera.view", m_cameraView); - m_buffer->setGlobalValue("camera.projectionInv", m_cameraProjection.inverse()); - m_buffer->setGlobalValue("camera.projection", m_cameraProjection); - m_buffer->setGlobalValue("camera.screenToWorld", vp.inverse()); - m_buffer->setGlobalValue("camera.worldToScreen", vp); + cameraReset(); } /*! Resets the camera view and projection matrices in the command buffer. */ void PipelineContext::cameraReset() { - m_buffer->setViewProjection(m_cameraView, m_cameraProjection); + m_buffer->setCameraProperties(m_camera); +} +/*! + Returns screen size +*/ +Vector2 PipelineContext::size() const { + return Vector2(m_width, m_height); } /*! Resizes the pipeline context to the specified \a width and \a height. Updates render tasks accordingly. @@ -140,7 +130,7 @@ void PipelineContext::resize(int32_t width, int32_t height) { it->resize(m_width, m_height); } - m_buffer->setGlobalValue("camera.screen", Vector4(1.0f / (float)m_width, 1.0f / (float)m_height, m_width, m_height)); + m_buffer->setViewport(0, 0, m_width, m_height); } } /*! diff --git a/engine/src/pipelinetasks/ambientocclusion.cpp b/engine/src/pipelinetasks/ambientocclusion.cpp index d1c950f7e..9848facc0 100644 --- a/engine/src/pipelinetasks/ambientocclusion.cpp +++ b/engine/src/pipelinetasks/ambientocclusion.cpp @@ -8,8 +8,6 @@ #include "pipelinecontext.h" #include "commandbuffer.h" -#include - #define KERNEL_SIZE 16 namespace { diff --git a/engine/src/pipelinetasks/depthoffield.cpp b/engine/src/pipelinetasks/depthoffield.cpp index 6f490da04..f8cc580d7 100644 --- a/engine/src/pipelinetasks/depthoffield.cpp +++ b/engine/src/pipelinetasks/depthoffield.cpp @@ -80,7 +80,7 @@ void DepthOfField::exec() { // Focus Distance Camera *camera = Camera::current(); - float focal = camera->focal(); + float focal = camera->focalDistance(); if(focal != m_focusDistance) { m_focusDistance = focal; if(m_dofMaterial) { diff --git a/engine/tests/tst_actor.h b/engine/tests/tst_actor.h index 95497147c..505a1646f 100644 --- a/engine/tests/tst_actor.h +++ b/engine/tests/tst_actor.h @@ -122,7 +122,7 @@ TEST_F(ActorTest, Prefab_serialization) { Actor *level1 = Engine::composeActor("", "Level1", root); Camera *prefabCamera = dynamic_cast(level1->addComponent("Camera")); ASSERT_TRUE(prefabCamera != nullptr); - prefabCamera->setFocal(10.0f); + prefabCamera->setFocalDistance(10.0f); // Make a prefab Prefab *prefab = Engine::objectCreate(""); @@ -137,7 +137,7 @@ TEST_F(ActorTest, Prefab_serialization) { ASSERT_TRUE(clone->transform()->position() == t0->position()); Camera *cloneCamera = dynamic_cast(clone->componentInChild("Camera")); ASSERT_TRUE(cloneCamera != nullptr); - ASSERT_TRUE(cloneCamera->focal() == 10.0f); + ASSERT_TRUE(cloneCamera->focalDistance() == 10.0f); ASSERT_TRUE(cloneCamera->actor()->name() == "Level1"); // Change a property of clone @@ -165,7 +165,7 @@ TEST_F(ActorTest, Prefab_serialization) { ASSERT_TRUE(result->transform()->position() == t0->position()); Camera *resultCamera = dynamic_cast(result->componentInChild("Camera")); ASSERT_TRUE(resultCamera != nullptr); - ASSERT_TRUE(resultCamera->focal() == 10.0f); + ASSERT_TRUE(resultCamera->focalDistance() == 10.0f); ASSERT_TRUE(resultCamera->actor()->name() == "Level1"); Transform *resultCameraTransform = resultCamera->transform(); ASSERT_TRUE(resultCameraTransform->position() == Vector3(3.0f, 2.0f, 1.0f)); diff --git a/modules/editor/shadertools/converter/functions/camera.h b/modules/editor/shadertools/converter/functions/camera.h index d045eb9e1..9097f2efb 100644 --- a/modules/editor/shadertools/converter/functions/camera.h +++ b/modules/editor/shadertools/converter/functions/camera.h @@ -16,7 +16,7 @@ class CameraPosition : public ShaderNode { } int32_t build(TString &code, std::stack &stack, const AbstractNodeGraph::Link &link, int32_t &depth, int32_t &type) override { - stack.push("g.cameraPosition.xyz"); + stack.push("cameraPosition()"); return ShaderNode::build(code, stack, link, depth, type); } }; @@ -34,7 +34,7 @@ class CameraDirection : public ShaderNode { } int32_t build(TString &code, std::stack &stack, const AbstractNodeGraph::Link &link, int32_t &depth, int32_t &type) override { - stack.push("g.cameraTarget.xyz"); + stack.push("cameraTarget()"); return ShaderNode::build(code, stack, link, depth, type); } }; @@ -57,13 +57,13 @@ class ScreenSize : public ShaderNode { int32_t build(TString &code, std::stack &stack, const AbstractNodeGraph::Link &link, int32_t &depth, int32_t &type) override { if(link.oport->m_name == "Width") { - stack.push("g.cameraScreen.x"); + stack.push("screenSize().x"); } else if(link.oport->m_name == "Height") { - stack.push("g.cameraScreen.y"); + stack.push("screenSize().y"); } else if(link.oport->m_name == "1/Width") { - stack.push("g.cameraScreen.z"); + stack.push("screenSizeNorm().x"); } else if(link.oport->m_name == "1/Height") { - stack.push("g.cameraScreen.w"); + stack.push("screenSizeNorm().y"); } return ShaderNode::build(code, stack, link, depth, type); @@ -91,7 +91,7 @@ class ScreenPosition : public ShaderNode { int32_t build(TString &code, std::stack &stack, const AbstractNodeGraph::Link &link, int32_t &depth, int32_t &type) override { if(m_position == -1) { - code += TString("\tvec4 local%1 = gl_FragCoord").arg(TString::number(depth)) + (m_normalized ? " / g.cameraScreen;\n" : ";\n"); + code += TString("\tvec4 local%1 = gl_FragCoord").arg(TString::number(depth)) + (m_normalized ? " / vec4(cameraScreen(), 1.0f, 1.0f);\n" : ";\n"); } int32_t result = ShaderNode::build(code, stack, link, depth, type); @@ -140,9 +140,9 @@ class ProjectionMatrix : public ShaderNode { int32_t build(TString &code, std::stack &stack, const AbstractNodeGraph::Link &link, int32_t &depth, int32_t &type) override { if(m_inverted) { - stack.push("g.cameraProjectionInv"); + stack.push("projectionMatrixInv()"); } else { - stack.push("g.cameraProjection"); + stack.push("projectionMatrix()"); } return ShaderNode::build(code, stack, link, depth, type); diff --git a/modules/editor/shadertools/converter/functions/coordinates.h b/modules/editor/shadertools/converter/functions/coordinates.h index 060e3ba84..bb4af06db 100644 --- a/modules/editor/shadertools/converter/functions/coordinates.h +++ b/modules/editor/shadertools/converter/functions/coordinates.h @@ -77,7 +77,7 @@ class CoordPanner : public ShaderNode { std::vector args = getArguments(code, stack, depth, type); if(!args.empty()) { TString value = args[0]; - value.append(TString(" + vec2(%1, %2) * g.time").arg(TString::number(m_speed.x), TString::number(m_speed.y))); + value.append(TString(" + vec2(%1, %2) * time()").arg(TString::number(m_speed.x), TString::number(m_speed.y))); code.append(TString("\tvec2 local%1 = %2;\n").arg(TString::number(depth), value)); } else { diff --git a/modules/editor/shadertools/converter/functions/surface.h b/modules/editor/shadertools/converter/functions/surface.h index 608a1677a..58557f5fa 100644 --- a/modules/editor/shadertools/converter/functions/surface.h +++ b/modules/editor/shadertools/converter/functions/surface.h @@ -39,7 +39,7 @@ class Fresnel : public ShaderNode { if(key == "Normal") { return "_n"; } else if(key == "View Dir") { - return "_view"; + return "normalize(_vertex - cameraPosition())"; } return TString::number(m_power); } @@ -76,7 +76,7 @@ class SurfaceDepth : public ShaderNode { if(m_position == -1) { std::vector args = getArguments(code, stack, depth, type); - code.append(TString("float local%1 = (- _modelView * vec4(%2, 1.0f)).z;\n") + code.append(TString("float local%1 = (- modelViewMatrix() * vec4(%2, 1.0f)).z;\n") .arg(TString::number(depth), args[0])); } diff --git a/modules/editor/shadertools/converter/functions/time.h b/modules/editor/shadertools/converter/functions/time.h index bb0f9256e..2544538e0 100644 --- a/modules/editor/shadertools/converter/functions/time.h +++ b/modules/editor/shadertools/converter/functions/time.h @@ -41,7 +41,7 @@ class Time : public ShaderNode { } virtual TString getVariable() const { - return "g.time"; + return "time()"; } float scale() const { return m_scale; } @@ -63,7 +63,7 @@ class DeltaTime : public Time { DeltaTime() { } TString getVariable() const override { - return "g.deltaTime"; + return "deltaTime()"; } }; @@ -78,7 +78,7 @@ class CosTime : public Time { CosTime() { } TString getVariable() const override { - return "cos(g.time)"; + return "cos(time())"; } }; @@ -93,7 +93,7 @@ class SinTime : public Time { SinTime() { } TString getVariable() const override { - return "sin(g.time)"; + return "sin(time())"; } }; diff --git a/modules/editor/shadertools/converter/shaderbuilder.cpp b/modules/editor/shadertools/converter/shaderbuilder.cpp index da664f74c..ba7bbe5a2 100644 --- a/modules/editor/shadertools/converter/shaderbuilder.cpp +++ b/modules/editor/shadertools/converter/shaderbuilder.cpp @@ -139,12 +139,6 @@ ShaderBuilderSettings::Rhi ShaderBuilder::currentRhi() { void ShaderBuilder::buildInstanceData(const VariantMap &user, PragmaMap &pragmas) { TString result; - TString modelMatrix = - " mat4 modelMatrix = mat4(vec4(instance.data[_instanceOffset + 0].xyz, 0.0f),\n" - " vec4(instance.data[_instanceOffset + 1].xyz, 0.0f),\n" - " vec4(instance.data[_instanceOffset + 2].xyz, 0.0f),\n" - " vec4(instance.data[_instanceOffset + 3].xyz, 1.0f));\n"; - TString objectId = " _objectId = vec4(instance.data[_instanceOffset + 0].w,\n" " instance.data[_instanceOffset + 1].w,\n" @@ -236,7 +230,6 @@ void ShaderBuilder::buildInstanceData(const VariantMap &user, PragmaMap &pragmas } } - result += modelMatrix; result += uniforms; pragmas["instance"] = result; diff --git a/modules/editor/shadertools/converter/spirvconverter.h b/modules/editor/shadertools/converter/spirvconverter.h index a059b7c61..e8560fb3b 100644 --- a/modules/editor/shadertools/converter/spirvconverter.h +++ b/modules/editor/shadertools/converter/spirvconverter.h @@ -224,6 +224,7 @@ class SpirVConverter { } } else { aError() << "[Shader]" << shader.getInfoLog(); + aError() << buff; } return std::vector(); } diff --git a/modules/editor/shadertools/shaders/BRDF.h b/modules/editor/shadertools/shaders/BRDF.h index e7177a86d..3978cc7c9 100644 --- a/modules/editor/shadertools/shaders/BRDF.h +++ b/modules/editor/shadertools/shaders/BRDF.h @@ -106,3 +106,52 @@ vec3 closestPointOnSegment(vec3 a, vec3 b, vec3 p) { float c2 = dot(v, v); return a + v * clamp(c1 / c2, 0.0, 1.0); } +// Shadow map functions +float getShadowSample(sampler2D map, vec2 coord, float t) { + return step(t, texture(map, coord).x); +} + +float getShadowSampleLinear(sampler2D map, vec2 coord, float t) { + vec2 pos = coord / shadowPageSize() + vec2(0.5f); + vec2 frac = fract(pos); + vec2 start = (pos - frac) * shadowPageSize(); + + float bl = getShadowSample(map, start, t); + float br = getShadowSample(map, start + vec2(shadowPageSize(), 0.0f), t); + float tl = getShadowSample(map, start + vec2(0.0f, shadowPageSize()), t); + float tr = getShadowSample(map, start + vec2(shadowPageSize()), t); + + float a = mix(bl, tl, frac.y); + float b = mix(br, tr, frac.y); + + return mix(a, b, frac.x); +} + +float getShadowSamplePCF(sampler2D map, vec2 coord, float t) { + const float NUM_SAMPLES = 4.0f; + const float SAMPLES_START = (NUM_SAMPLES - 1.0f) * 0.5; + + float result = 0.0f; + for(float y = -SAMPLES_START; y <= SAMPLES_START; y += 1.0f) { + for(float x = -SAMPLES_START; x <= SAMPLES_START; x += 1.0f) { + result += getShadowSampleLinear(map, coord + vec2(x, y) * shadowPageSize(), t); + } + } + return result / (NUM_SAMPLES * NUM_SAMPLES); +} + +float getShadowVarianceSample(sampler2D map, vec2 coord, float t) { + vec2 m = texture(map, coord).xy; + + float p = step(t, m.x); + float v = max(m.y - m.x * m.x, 0.000002f); // 0.000002 = Min variance + + float d = t - m.x; + float pm = linstep(0.2, 1.0f, v / (v + d * d)); + + return max(p, pm); +} + +float getShadow(sampler2D map, vec2 coord, float t) { + return clamp(getShadowSamplePCF(map, coord, t), 0.0f, 1.0f); +} diff --git a/modules/editor/shadertools/shaders/Billboard.vert b/modules/editor/shadertools/shaders/Billboard.vert index da0ac1887..c4dd39543 100644 --- a/modules/editor/shadertools/shaders/Billboard.vert +++ b/modules/editor/shadertools/shaders/Billboard.vert @@ -2,8 +2,6 @@ #pragma flags -#include "ShaderLayout.h" - layout(location = 0) in vec3 vertex; layout(location = 1) in vec2 uv0; layout(location = 2) in vec4 color; @@ -18,22 +16,16 @@ layout(location = 2) out vec4 _color; layout(location = 5) out vec3 _b; #endif -layout(location = 6) out vec3 _view; -layout(location = 7) flat out vec4 _objectId; -layout(location = 8) flat out int _instanceOffset; -layout(location = 9) out mat4 _modelView; +layout(location = 6) flat out vec4 _objectId; +layout(location = 7) flat out int _instanceOffset; + +#include "ShaderLayout.h" #pragma vertexFunctions void main(void) { #pragma offset - _modelView = g.view; - - vec3 camera = vec3(g.view[0].w, - g.view[1].w, - g.view[2].w); - vec3 PositionOffset = vec3(0.0f); #pragma vertex @@ -57,29 +49,24 @@ void main(void) { float x = cos(angle) * sizeRot.x + sin(angle) * sizeRot.y; float y = sin(angle) * sizeRot.x - cos(angle) * sizeRot.y; - vec3 target = g.cameraTarget.xyz; - if(g.cameraProjection[2].w < 0.0f) { - target = worldPosition.xyz; - } - - vec3 normal = normalize(g.cameraPosition.xyz - target); + vec3 normal = cameraDirection(); vec3 right = normalize(cross(vec3(0.0f, 1.0f, 0.0f), normal)); vec3 up = normalize(cross(normal, right)); - vec3 v = (up * x + right * y) + worldPosition + PositionOffset; - - _vertex = g.projection * (_modelView * vec4(v, 1.0f)); - _view = normalize(v - camera); - #ifdef USE_TBN _n = vec3(0.0f); _t = vec3(0.0f); _b = vec3(0.0f); #endif + + _vertex = vec4((up * x + right * y) + worldPosition + PositionOffset, 1.0f); + + vec4 pos = cameraWorldToScreen() * _vertex; + #ifdef ORIGIN_TOP - _vertex.y = -_vertex.y; + pos.y = -pos.y; #endif _color = color; _uv0 = uv0 * uvScale.xy + uvOffset.xy; - gl_Position = _vertex; + gl_Position = pos; } diff --git a/modules/editor/shadertools/shaders/Fullscreen.vert b/modules/editor/shadertools/shaders/Fullscreen.vert index ed5574d21..728eb245f 100644 --- a/modules/editor/shadertools/shaders/Fullscreen.vert +++ b/modules/editor/shadertools/shaders/Fullscreen.vert @@ -2,10 +2,6 @@ #pragma flags -#define NO_INSTANCE - -#include "ShaderLayout.h" - layout(location = 0) in vec3 vertex; layout(location = 1) in vec2 uv0; layout(location = 2) in vec4 color; diff --git a/modules/editor/shadertools/shaders/Functions.h b/modules/editor/shadertools/shaders/Functions.h index 7356bda8b..9a2140f1a 100644 --- a/modules/editor/shadertools/shaders/Functions.h +++ b/modules/editor/shadertools/shaders/Functions.h @@ -49,53 +49,3 @@ float luminanceApprox(vec3 rgb) { float linstep(float l, float h, float v) { return clamp((v - l) / (h - l), 0.0f, 1.0f); } - -// Shadow map functions -float getShadowSample(sampler2D map, vec2 coord, float t) { - return step(t, texture(map, coord).x); -} - -float getShadowSampleLinear(sampler2D map, vec2 coord, float t) { - vec2 pos = coord / g.shadowPageSize.xy + vec2(0.5); - vec2 frac = fract(pos); - vec2 start = (pos - frac) * g.shadowPageSize.xy; - - float bl = getShadowSample(map, start, t); - float br = getShadowSample(map, start + vec2(g.shadowPageSize.x, 0.0f), t); - float tl = getShadowSample(map, start + vec2(0.0f, g.shadowPageSize.y), t); - float tr = getShadowSample(map, start + g.shadowPageSize.xy, t); - - float a = mix(bl, tl, frac.y); - float b = mix(br, tr, frac.y); - - return mix(a, b, frac.x); -} - -float getShadowSamplePCF(sampler2D map, vec2 coord, float t) { - const float NUM_SAMPLES = 4.0f; - const float SAMPLES_START = (NUM_SAMPLES - 1.0f) * 0.5; - - float result = 0.0f; - for(float y = -SAMPLES_START; y <= SAMPLES_START; y += 1.0f) { - for(float x = -SAMPLES_START; x <= SAMPLES_START; x += 1.0f) { - result += getShadowSampleLinear(map, coord + vec2(x, y) * g.shadowPageSize.xy, t); - } - } - return result / (NUM_SAMPLES * NUM_SAMPLES); -} - -float getShadowVarianceSample(sampler2D map, vec2 coord, float t) { - vec2 m = texture(map, coord).xy; - - float p = step(t, m.x); - float v = max(m.y - m.x * m.x, 0.000002f); // 0.0f00002 = Min variance - - float d = t - m.x; - float pm = linstep(0.2, 1.0f, v / (v + d * d)); - - return max(p, pm); -} - -float getShadow(sampler2D map, vec2 coord, float t) { - return clamp(getShadowSamplePCF(map, coord, t), 0.0f, 1.0f); -} diff --git a/modules/editor/shadertools/shaders/Shader.frag b/modules/editor/shadertools/shaders/Shader.frag index 49b2200ff..29a8f06ba 100644 --- a/modules/editor/shadertools/shaders/Shader.frag +++ b/modules/editor/shadertools/shaders/Shader.frag @@ -2,8 +2,6 @@ #pragma flags -#include "ShaderLayout.h" - layout(location = 0) in vec4 _vertex; layout(location = 1) in vec2 _uv0; layout(location = 2) in vec4 _color; @@ -14,10 +12,8 @@ layout(location = 2) in vec4 _color; layout(location = 5) in vec3 _b; #endif -layout(location = 6) in vec3 _view; -layout(location = 7) flat in vec4 _objectId; -layout(location = 8) flat in int _instanceOffset; -layout(location = 9) in mat4 _modelView; +layout(location = 6) flat in vec4 _objectId; +layout(location = 7) flat in int _instanceOffset; layout(location = 0) out vec4 gbuffer0; #ifdef USE_GBUFFER @@ -30,6 +26,8 @@ layout(location = 0) out vec4 gbuffer0; #pragma uniforms +#include "ShaderLayout.h" + #include "Functions.h" #pragma fragmentFunctions @@ -44,11 +42,12 @@ void main(void) { float Roughness; float Opacity; float IOR; + float ClipValue = 0.5f; #pragma fragment - float alpha = Opacity * _color.w; - if(g.clip >= alpha) { + float alpha = Opacity; + if(alpha < ClipValue) { discard; } @@ -76,7 +75,7 @@ void main(void) { gbuffer3 = vec4(Roughness, 0.0f, Metallic, 1.0f); // Variables #else - gbuffer0 = vec4(Emissive, alpha); + gbuffer0 = vec4(Emissive, alpha * _color.w); #endif diff --git a/modules/editor/shadertools/shaders/ShaderLayout.h b/modules/editor/shadertools/shaders/ShaderLayout.h index 3d5254e6b..6b290da0c 100644 --- a/modules/editor/shadertools/shaders/ShaderLayout.h +++ b/modules/editor/shadertools/shaders/ShaderLayout.h @@ -5,22 +5,69 @@ layout(set = 1, binding = GLOBAL) uniform Global { mat4 view; mat4 projection; - mat4 cameraView; - mat4 cameraProjection; - mat4 cameraProjectionInv; - mat4 cameraScreenToWorld; mat4 cameraWorldToScreen; + mat4 cameraAndParams; +} g; - vec4 cameraPosition; - vec4 cameraTarget; - vec4 cameraScreen; - vec4 shadowPageSize; +mat4 viewMatrix() { + return g.view; +} - float clip; - float time; - float deltaTime; - float padding[13]; -} g; +mat4 projectionMatrix() { + return g.projection; +} + +mat4 projectionMatrixInv() { + return inverse(projectionMatrix()); +} + +mat4 cameraWorldToScreen() { + return g.cameraWorldToScreen; +} + +mat4 cameraScreenToWorld() { + return inverse(cameraWorldToScreen()); +} + +bool isOrtho() { + return g.projection[2].w < 0.0f; +} + +float nearClipPlane() { + return g.cameraAndParams[1].x; +} + +float farClipPlane() { + return g.cameraAndParams[1].y; +} + +float time() { + return g.cameraAndParams[2].y; +} + +float deltaTime() { + return g.cameraAndParams[2].z; +} + +float shadowPageSize() { + return g.cameraAndParams[2].x; +} + +vec2 screenSizeNorm() { + return g.cameraAndParams[1].zw; +} + +vec2 screenSize() { + return 1.0f / screenSizeNorm(); +} + +vec3 cameraPosition() { + return g.cameraAndParams[0].xyz; +} + +vec3 cameraDirection() { + return mat3(viewMatrix()) * vec3(0.0f, 0.0f, 1.0f); +} #ifndef NO_INSTANCE @@ -34,4 +81,15 @@ layout(std140, binding = LOCAL) uniform InstanceData { } instance; #endif +mat4 modelMatrix() { + return mat4(vec4(instance.data[_instanceOffset + 0].xyz, 0.0f), + vec4(instance.data[_instanceOffset + 1].xyz, 0.0f), + vec4(instance.data[_instanceOffset + 2].xyz, 0.0f), + vec4(instance.data[_instanceOffset + 3].xyz, 1.0f)); +} + +mat4 modelViewMatrix() { + return viewMatrix() * modelMatrix(); +} + #endif diff --git a/modules/editor/shadertools/shaders/Skinned.vert b/modules/editor/shadertools/shaders/Skinned.vert index 988f9dd71..ae58d9fbd 100644 --- a/modules/editor/shadertools/shaders/Skinned.vert +++ b/modules/editor/shadertools/shaders/Skinned.vert @@ -2,8 +2,6 @@ #pragma flags -#include "ShaderLayout.h" - layout(location = 0) in vec3 vertex; layout(location = 1) in vec2 uv0; layout(location = 2) in vec4 color; @@ -26,10 +24,10 @@ layout(location = 2) out vec4 _color; layout(location = 5) out vec3 _b; #endif -layout(location = 6) out vec3 _view; -layout(location = 7) flat out vec4 _objectId; -layout(location = 8) flat out int _instanceOffset; -layout(location = 9) out mat4 _modelView; +layout(location = 6) flat out vec4 _objectId; +layout(location = 7) flat out int _instanceOffset; + +#include "ShaderLayout.h" #pragma vertexFunctions @@ -44,12 +42,6 @@ void main(void) { #pragma objectId - _modelView = g.view * modelMatrix; - - vec3 camera = vec3(g.view[0].w, - g.view[1].w, - g.view[2].w); - vec3 PositionOffset = vec3(0.0f); #pragma vertex @@ -90,22 +82,21 @@ void main(void) { } } + mat4 _modelMatrix = modelMatrix(); #ifdef USE_TBN - mat3 rot = mat3(modelMatrix); + mat3 rot = mat3(_modelMatrix); _t = normalize(rot * _t); _n = normalize(rot * _n); _b = cross(_t, _n); #endif - vec3 v = finalVector.xyz / finalVector.w + PositionOffset; - _vertex = g.projection * (_modelView * vec4(v, 1.0)); - _view = normalize(v - g.cameraPosition.xyz); + _vertex = _modelMatrix * vec4(finalVector.xyz / finalVector.w + PositionOffset, 1.0f); + vec4 pos = cameraWorldToScreen() * _vertex; #ifdef ORIGIN_TOP - _vertex.y = -_vertex.y; + pos.y = -pos.y; #endif - _color = color; _uv0 = uv0; - gl_Position = _vertex; + gl_Position = pos; } diff --git a/modules/editor/shadertools/shaders/Static.vert b/modules/editor/shadertools/shaders/Static.vert index bbbe6a5e0..7dd8b119f 100644 --- a/modules/editor/shadertools/shaders/Static.vert +++ b/modules/editor/shadertools/shaders/Static.vert @@ -2,8 +2,6 @@ #pragma flags -#include "ShaderLayout.h" - layout(location = 0) in vec3 vertex; layout(location = 1) in vec2 uv0; layout(location = 2) in vec4 color; @@ -23,10 +21,10 @@ layout(location = 2) out vec4 _color; layout(location = 5) out vec3 _b; #endif -layout(location = 6) out vec3 _view; -layout(location = 7) flat out vec4 _objectId; -layout(location = 8) flat out int _instanceOffset; -layout(location = 9) out mat4 _modelView; +layout(location = 6) flat out vec4 _objectId; +layout(location = 7) flat out int _instanceOffset; + +#include "ShaderLayout.h" #pragma vertexFunctions @@ -37,29 +35,24 @@ void main(void) { #pragma objectId - _modelView = g.view * modelMatrix; - - vec3 camera = vec3(g.view[0].w, - g.view[1].w, - g.view[2].w); - vec3 PositionOffset = vec3(0.0f); #pragma vertex + mat4 _modelMatrix = modelMatrix(); #ifdef USE_TBN - mat3 rot = mat3(modelMatrix); + mat3 rot = mat3(_modelMatrix); _t = normalize(rot * tangent); _n = normalize(rot * normal); _b = cross(_t, _n); #endif - vec4 v = vec4(vertex + PositionOffset, 1.0); - _vertex = g.projection * (_modelView * v); - _view = normalize((modelMatrix * v).xyz - g.cameraPosition.xyz); + _vertex = _modelMatrix * vec4(vertex + PositionOffset, 1.0); + vec4 pos = cameraWorldToScreen() * _vertex; + #ifdef ORIGIN_TOP - _vertex.y = -_vertex.y; + pos.y = -pos.y; #endif _color = color; _uv0 = uv0; - gl_Position = _vertex; + gl_Position = pos; } diff --git a/modules/editor/shadertools/shaders/functions/parallaxOffset.mtlf b/modules/editor/shadertools/shaders/functions/parallaxOffset.mtlf index 8f5eff962..6356a45e0 100644 --- a/modules/editor/shadertools/shaders/functions/parallaxOffset.mtlf +++ b/modules/editor/shadertools/shaders/functions/parallaxOffset.mtlf @@ -2,7 +2,7 @@ - + diff --git a/modules/editor/texturetools/editor/spritecontroller.cpp b/modules/editor/texturetools/editor/spritecontroller.cpp index e25a4ae7b..927d8f19e 100644 --- a/modules/editor/texturetools/editor/spritecontroller.cpp +++ b/modules/editor/texturetools/editor/spritecontroller.cpp @@ -38,7 +38,7 @@ void SpriteController::setSize(uint32_t width, uint32_t height) { if(cam) { cam->transform()->setPosition(Vector3(m_width * 0.5f, m_height * 0.5f, 1.0f)); cam->setOrthoSize(MAX(m_width, m_height)); - cam->setFocal(m_height); + cam->setFocalDistance(m_height); } } diff --git a/modules/renders/rendergl/includes/commandbuffergl.h b/modules/renders/rendergl/includes/commandbuffergl.h index 6366b233f..2d6a27779 100644 --- a/modules/renders/rendergl/includes/commandbuffergl.h +++ b/modules/renders/rendergl/includes/commandbuffergl.h @@ -9,8 +9,6 @@ class CommandBufferGL : public CommandBuffer { public: CommandBufferGL(); - void begin(); - void dispatchCompute(ComputeInstance *shader, int32_t groupsX, int32_t groupsY, int32_t groupsZ) override; void drawMesh(Mesh *mesh, uint32_t sub, uint32_t layer, MaterialInstance &instance) override; diff --git a/modules/renders/rendergl/src/commandbuffergl.cpp b/modules/renders/rendergl/src/commandbuffergl.cpp index 2ef6d9651..e18937f80 100644 --- a/modules/renders/rendergl/src/commandbuffergl.cpp +++ b/modules/renders/rendergl/src/commandbuffergl.cpp @@ -1,7 +1,5 @@ #include "commandbuffergl.h" -#include - #include "agl.h" #include "resources/meshgl.h" @@ -9,21 +7,10 @@ #include "resources/rendertargetgl.h" #include "resources/computeshadergl.h" -#include -#include - CommandBufferGL::CommandBufferGL() { PROFILE_FUNCTION(); } -void CommandBufferGL::begin() { - PROFILE_FUNCTION(); - - m_global.time = Timer::time(); - m_global.deltaTime = Timer::deltaTime(); - m_global.clip = 0.1f; -} - void CommandBufferGL::dispatchCompute(ComputeInstance *shader, int32_t groupsX, int32_t groupsY, int32_t groupsZ) { #ifndef THUNDER_MOBILE PROFILE_FUNCTION(); diff --git a/modules/renders/rendermt/includes/commandbuffermt.h b/modules/renders/rendermt/includes/commandbuffermt.h index efa9d829c..1928186cc 100644 --- a/modules/renders/rendermt/includes/commandbuffermt.h +++ b/modules/renders/rendermt/includes/commandbuffermt.h @@ -36,6 +36,8 @@ class CommandBufferMt : public CommandBuffer { void disableScissor() override; protected: + MTL::Viewport m_viewport; + MTL::CommandBuffer *m_commandBuffer; MTL::RenderCommandEncoder *m_encoder; diff --git a/modules/renders/rendermt/src/commandbuffermt.cpp b/modules/renders/rendermt/src/commandbuffermt.cpp index 24370c039..f4d1ee48d 100644 --- a/modules/renders/rendermt/src/commandbuffermt.cpp +++ b/modules/renders/rendermt/src/commandbuffermt.cpp @@ -1,14 +1,10 @@ #include "commandbuffermt.h" -#include - #include "resources/meshmt.h" #include "resources/materialmt.h" #include "resources/rendertargetmt.h" #include "resources/computeshadermt.h" -#include - CommandBufferMt::CommandBufferMt() : m_commandBuffer(nullptr), m_encoder(nullptr) { @@ -20,9 +16,7 @@ void CommandBufferMt::begin(MTL::CommandBuffer *cmd) { m_commandBuffer = cmd; - m_global.time = Timer::time(); - m_global.deltaTime = Timer::deltaTime(); - m_global.clip = 0.1f; + CommandBuffer::begin(); } void CommandBufferMt::end() { @@ -117,7 +111,14 @@ void CommandBufferMt::setViewport(int32_t x, int32_t y, int32_t width, int32_t h CommandBuffer::setViewport(x, y, width, height); if(m_encoder) { - m_encoder->setViewport({(float)m_viewportX, (float)m_viewportY, (float)m_viewportWidth, (float)m_viewportHeight, 0.0f, 1.0f}); + m_viewport.originX = (float)x; + m_viewport.originY = (float)y; + m_viewport.width = (float)width; + m_viewport.height = (float)height; + m_viewport.znear = (float)0.0f; + m_viewport.zfar = (float)1.0f; + + m_encoder->setViewport(m_viewport); } } @@ -129,6 +130,6 @@ void CommandBufferMt::enableScissor(int32_t x, int32_t y, int32_t width, int32_t void CommandBufferMt::disableScissor() { if(m_encoder) { - m_encoder->setScissorRect({(uint32_t)m_viewportX, (uint32_t)m_viewportY, (uint32_t)m_viewportWidth, (uint32_t)m_viewportHeight}); + m_encoder->setScissorRect({(uint32_t)m_viewport.originX, (uint32_t)m_viewport.originY, (uint32_t)m_viewport.width, (uint32_t)m_viewport.height}); } } diff --git a/modules/renders/rendervk/includes/commandbuffervk.h b/modules/renders/rendervk/includes/commandbuffervk.h index 33a4cab5c..b7083e42a 100644 --- a/modules/renders/rendervk/includes/commandbuffervk.h +++ b/modules/renders/rendervk/includes/commandbuffervk.h @@ -44,6 +44,8 @@ class CommandBufferVk : public CommandBuffer { VkCommandBuffer m_commandBuffer; + VkViewport m_viewport; + static PFN_vkSetDebugUtilsObjectNameEXT vkSetDebugUtilsObjectNameEXT; static PFN_vkCmdBeginDebugUtilsLabelEXT vkCmdBeginDebugUtilsLabelEXT; static PFN_vkCmdEndDebugUtilsLabelEXT vkCmdEndDebugUtilsLabelEXT; diff --git a/modules/renders/rendervk/src/commandbuffervk.cpp b/modules/renders/rendervk/src/commandbuffervk.cpp index c899e27c5..51fca6f0a 100644 --- a/modules/renders/rendervk/src/commandbuffervk.cpp +++ b/modules/renders/rendervk/src/commandbuffervk.cpp @@ -26,12 +26,11 @@ void CommandBufferVk::begin(VkCommandBuffer buffer) { m_commandBuffer = buffer; - m_global.time = Timer::deltaTime(); - m_global.clip = 0.1f; + CommandBuffer::begin(); VkDevice device = WrapperVk::device(); - for(auto it : m_textures) { + for(auto &it : m_textures) { if(it.texture && it.texture->state() == Resource::ToBeUpdated) { TextureVk *texture = static_cast(it.texture); VkDescriptorImageInfo info = {}; @@ -134,15 +133,14 @@ void CommandBufferVk::setViewport(int32_t x, int32_t y, int32_t width, int32_t h CommandBuffer::setViewport(x, y, width, height); - VkViewport viewport = {}; - viewport.x = (float)m_viewportX; - viewport.y = (float)m_viewportY; - viewport.width = (float)m_viewportWidth; - viewport.height = (float)m_viewportHeight; - viewport.minDepth = 0.0f; - viewport.maxDepth = 1.0f; + m_viewport.x = (float)x; + m_viewport.y = (float)y; + m_viewport.width = (float)width; + m_viewport.height = (float)height; + m_viewport.minDepth = 0.0f; + m_viewport.maxDepth = 1.0f; - vkCmdSetViewport(m_commandBuffer, 0, 1, &viewport); + vkCmdSetViewport(m_commandBuffer, 0, 1, &m_viewport); enableScissor(x, y, width, height); } @@ -161,8 +159,8 @@ void CommandBufferVk::disableScissor() { PROFILE_FUNCTION(); VkRect2D scissor = {}; - scissor.offset = {m_viewportX, m_viewportY}; - scissor.extent = {(uint32_t)m_viewportWidth, (uint32_t)m_viewportHeight}; + scissor.offset = {(int32_t)m_viewport.x, (int32_t)m_viewport.y}; + scissor.extent = {(uint32_t)m_viewport.width, (uint32_t)m_viewport.height}; vkCmdSetScissor(m_commandBuffer, 0, 1, &scissor); } diff --git a/modules/uikit/src/pipelinetasks/guilayer.cpp b/modules/uikit/src/pipelinetasks/guilayer.cpp index e6a5de32f..2445a9db4 100644 --- a/modules/uikit/src/pipelinetasks/guilayer.cpp +++ b/modules/uikit/src/pipelinetasks/guilayer.cpp @@ -5,7 +5,6 @@ #include "uisystem.h" #include -#include #include "components/actor.h" #include "components/world.h" @@ -51,7 +50,7 @@ void GuiLayer::exec() { for(auto it : m_uiComponents) { if(it->parentWidget() == nullptr && it->rectTransform()) { // Root widget - it->rectTransform()->setSize(buffer->viewport()); + it->rectTransform()->setSize(m_context->size()); it->draw(*buffer); diff --git a/worldeditor/bin/editor/materials/checkerboard.shader b/worldeditor/bin/editor/materials/checkerboard.shader index ac642ba6f..3acbcc801 100644 --- a/worldeditor/bin/editor/materials/checkerboard.shader +++ b/worldeditor/bin/editor/materials/checkerboard.shader @@ -7,19 +7,17 @@ #pragma flags -#include "ShaderLayout.h" - layout(location = 0) in vec4 _vertex; layout(location = 1) in vec2 _uv0; layout(location = 2) in vec4 _color; -layout(location = 6) in vec3 _view; -layout(location = 7) flat in vec4 _objectId; -layout(location = 8) flat in int _instanceOffset; -layout(location = 9) in mat4 _modelView; +layout(location = 6) flat in vec4 _objectId; +layout(location = 7) flat in int _instanceOffset; layout(location = 0) out vec4 rgb; +#include "ShaderLayout.h" + void main() { #pragma instance diff --git a/worldeditor/bin/editor/materials/cubemap.shader b/worldeditor/bin/editor/materials/cubemap.shader index 76fbfc253..0dc2b7c81 100644 --- a/worldeditor/bin/editor/materials/cubemap.shader +++ b/worldeditor/bin/editor/materials/cubemap.shader @@ -23,7 +23,7 @@ layout(location = 0) out vec3 _vertex; void main(void) { _vertex = vertex; - gl_Position = g.projection * (g.view * vec4(_vertex, 1.0)); + gl_Position = cameraWorldToScreen() * vec4(_vertex, 1.0); } ]]> = _vertex.z) ? _color : vec4(_color.xyz, _color.w * 0.25); } ]]> diff --git a/worldeditor/bin/editor/materials/grid.shader b/worldeditor/bin/editor/materials/grid.shader index f79fd3ae5..7d1e865e6 100644 --- a/worldeditor/bin/editor/materials/grid.shader +++ b/worldeditor/bin/editor/materials/grid.shader @@ -6,24 +6,49 @@ - + = _vertex.z) ? _color : vec4(_color.xyz, 0.0f); } ]]> diff --git a/worldeditor/bin/engine/materials/AreaLight.shader b/worldeditor/bin/engine/materials/AreaLight.shader index f661ae5d6..8c14fe062 100644 --- a/worldeditor/bin/engine/materials/AreaLight.shader +++ b/worldeditor/bin/engine/materials/AreaLight.shader @@ -42,12 +42,11 @@ layout(binding = LOCAL) uniform InstanceData { layout(location = 0) in vec3 vertex; layout(location = 0) out vec4 _vertex; +layout(location = 1) flat out mat4 _screenToWorld; void main(void) { - mat4 _modelView = g.view * uni.model; - - _vertex = g.projection * (_modelView * vec4(vertex, 1.0)); - + _vertex = cameraWorldToScreen() * uni.model * vec4(vertex, 1.0); + _screenToWorld = cameraScreenToWorld(); gl_Position = _vertex; } ]]> @@ -83,11 +82,7 @@ layout(binding = UNIFORM + 4) uniform sampler2D depthMap; layout(binding = UNIFORM + 5) uniform sampler2D shadowMap; layout(location = 0) in vec4 _vertex; -layout(location = 1) in vec2 _uv0; -layout(location = 2) in vec4 _color; -layout(location = 3) in vec3 _n; -layout(location = 4) in vec3 _t; -layout(location = 5) in vec3 _b; +layout(location = 1) flat in mat4 _screenToWorld; layout(location = 0) out vec4 rgb; @@ -157,7 +152,7 @@ void main (void) { // Light model LIT if(slice0.w > 0.0) { float depth = texture(depthMap, proj).x; - vec3 world = getWorld(g.cameraScreenToWorld, proj, depth); + vec3 world = getWorld(_screenToWorld, proj, depth); vec3 dir = uni.position.xyz - world; float dist = length(dir); @@ -183,7 +178,7 @@ void main (void) { vec3 albedo = slice2.xyz; // Vectors - vec3 v = normalize(g.cameraPosition.xyz - world); + vec3 v = normalize(cameraPosition() - world); vec3 n = normalize(slice0.xyz * 2.0 - 1.0); vec3 r = -reflect(v, n); diff --git a/worldeditor/bin/engine/materials/BlurOcclusion.shader b/worldeditor/bin/engine/materials/BlurOcclusion.shader index 4eb8686ae..8523d66fa 100644 --- a/worldeditor/bin/engine/materials/BlurOcclusion.shader +++ b/worldeditor/bin/engine/materials/BlurOcclusion.shader @@ -27,7 +27,7 @@ void main() { for(int x = -blurRange; x < blurRange; x++) { for(int y = -blurRange; y < blurRange; y++) { - vec2 offset = vec2(float(x), float(y)) * g.cameraScreen.zw; + vec2 offset = vec2(float(x), float(y)) * screenSizeNorm(); result += texture(aoMap, _uv0 + offset).r; n++; } diff --git a/worldeditor/bin/engine/materials/DOF.shader b/worldeditor/bin/engine/materials/DOF.shader index 24496be57..b129065a2 100644 --- a/worldeditor/bin/engine/materials/DOF.shader +++ b/worldeditor/bin/engine/materials/DOF.shader @@ -59,17 +59,17 @@ float cocSize(float depth) { void main(void) { color = texture(highMap, _uv0); - float centerDepth = getLinearDepth(texture(depthMap, _uv0).x, g.cameraPosition.w, g.cameraTarget.w); + float centerDepth = getLinearDepth(texture(depthMap, _uv0).x, nearClipPlane(), farClipPlane()); if(centerDepth < uni.skyDistance) { float centerSize = cocSize(centerDepth); float t = 1.0f; float radius = radScale; for(float ang = 0.0f; radius < uni.blurSize; ang += goldenAngle) { - vec2 tc = _uv0 + vec2(cos(ang), sin(ang)) * g.cameraScreen.zw * radius; + vec2 tc = _uv0 + vec2(cos(ang), sin(ang)) * screenSizeNorm() * radius; vec3 sampleColor = texture(lowMap, tc).xyz; - float sampleDepth = getLinearDepth(texture(depthMap, tc).x, g.cameraPosition.w, g.cameraTarget.w); + float sampleDepth = getLinearDepth(texture(depthMap, tc).x, nearClipPlane(), farClipPlane()); float sampleSize = cocSize(sampleDepth); if(sampleDepth > centerDepth) { diff --git a/worldeditor/bin/engine/materials/DefaultFont.shader b/worldeditor/bin/engine/materials/DefaultFont.shader index f017beb8a..2d7a9c20b 100644 --- a/worldeditor/bin/engine/materials/DefaultFont.shader +++ b/worldeditor/bin/engine/materials/DefaultFont.shader @@ -9,8 +9,6 @@ #pragma flags -#include "ShaderLayout.h" - layout(location = 0) in vec3 vertex; layout(location = 1) in vec2 uv0; layout(location = 2) in vec4 color; @@ -20,6 +18,8 @@ layout(location = 1) out vec4 _color; layout(location = 2) flat out vec4 _objectId; layout(location = 3) flat out int _instanceOffset; +#include "ShaderLayout.h" + void main(void) { #pragma offset @@ -30,7 +30,7 @@ void main(void) { _uv = uv0; _color = color * mainColor; - vec4 vertex = g.projection * ((g.view * modelMatrix) * vec4(vertex, 1.0)); + vec4 vertex = cameraWorldToScreen() * modelMatrix() * vec4(vertex, 1.0f); #ifdef ORIGIN_TOP vertex.y = -vertex.y; #endif @@ -42,10 +42,6 @@ void main(void) { #pragma flags -#include "ShaderLayout.h" - -layout(binding = UNIFORM) uniform sampler2D mainTexture; - layout(location = 0) in vec2 _uv; layout(location = 1) in vec4 _color; layout(location = 2) flat in vec4 _objectId; @@ -53,6 +49,10 @@ layout(location = 3) flat in int _instanceOffset; layout(location = 0) out vec4 color; +#include "ShaderLayout.h" + +layout(binding = UNIFORM) uniform sampler2D mainTexture; + const float softness = 0.0625f; void main() { @@ -63,7 +63,7 @@ void main() { float mask = smoothstep(min, max, texture(mainTexture, _uv).x); - if(g.clip >= mask) { + if(mask < 0.1f) { discard; } diff --git a/worldeditor/bin/engine/materials/DefaultMesh.shader b/worldeditor/bin/engine/materials/DefaultMesh.shader index b54830fe4..a5e1cca6c 100644 --- a/worldeditor/bin/engine/materials/DefaultMesh.shader +++ b/worldeditor/bin/engine/materials/DefaultMesh.shader @@ -5,8 +5,6 @@ #pragma flags -#include "ShaderLayout.h" - layout(location = 0) in vec4 _vertex; layout(location = 1) in vec2 _uv0; layout(location = 2) in vec4 _color; @@ -14,8 +12,8 @@ layout(location = 3) in vec3 _n; layout(location = 4) in vec3 _t; layout(location = 5) in vec3 _b; -layout(location = 7) flat in vec4 _objectId; -layout(location = 8) flat in int _instanceOffset; +layout(location = 6) flat in vec4 _objectId; +layout(location = 7) flat in int _instanceOffset; layout(location = 0) out vec4 gbuffer0; #ifndef VISIBILITY_BUFFER @@ -24,6 +22,8 @@ layout(location = 0) out vec4 gbuffer0; layout(location = 3) out vec4 gbuffer3; #endif +#include "ShaderLayout.h" + void main() { #ifdef VISIBILITY_BUFFER gbuffer0 = _objectId; diff --git a/worldeditor/bin/engine/materials/DefaultPostEffect.shader b/worldeditor/bin/engine/materials/DefaultPostEffect.shader index c0dfdb5ef..2a9752c66 100644 --- a/worldeditor/bin/engine/materials/DefaultPostEffect.shader +++ b/worldeditor/bin/engine/materials/DefaultPostEffect.shader @@ -7,6 +7,8 @@ #pragma flags +#define NO_INSTANCE + #include "ShaderLayout.h" layout(location = 0) in vec3 vertex; @@ -28,6 +30,8 @@ void main(void) { #pragma flags +#define NO_INSTANCE + #include "ShaderLayout.h" layout(binding = UNIFORM) uniform sampler2D mainTexture; diff --git a/worldeditor/bin/engine/materials/DefaultSprite.shader b/worldeditor/bin/engine/materials/DefaultSprite.shader index da1f4e067..6f06e9d69 100644 --- a/worldeditor/bin/engine/materials/DefaultSprite.shader +++ b/worldeditor/bin/engine/materials/DefaultSprite.shader @@ -8,26 +8,24 @@ #pragma flags -#include "ShaderLayout.h" - -layout(binding = UNIFORM) uniform sampler2D mainTexture; - layout(location = 0) in vec4 _vertex; layout(location = 1) in vec2 _uv0; layout(location = 2) in vec4 _color; -layout(location = 6) in vec3 _view; -layout(location = 7) flat in vec4 _objectId; -layout(location = 8) flat in int _instanceOffset; -layout(location = 9) in mat4 _modelView; +layout(location = 6) flat in vec4 _objectId; +layout(location = 7) flat in int _instanceOffset; layout(location = 0) out vec4 color; +#include "ShaderLayout.h" + +layout(binding = UNIFORM) uniform sampler2D mainTexture; + void main() { #pragma instance vec4 rgb = texture(mainTexture, _uv0.xy) * _color * mainColor; - if(g.clip >= rgb.a) { + if(rgb.a < 0.1f) { discard; } diff --git a/worldeditor/bin/engine/materials/DirectLight.shader b/worldeditor/bin/engine/materials/DirectLight.shader index c4b6aef76..710cc52aa 100644 --- a/worldeditor/bin/engine/materials/DirectLight.shader +++ b/worldeditor/bin/engine/materials/DirectLight.shader @@ -31,9 +31,11 @@ layout(location = 3) in vec3 normal; layout(location = 4) in vec3 tangent; layout(location = 0) out vec4 _vertex; +layout(location = 1) flat out mat4 _screenToWorld; void main(void) { _vertex = vec4(vertex * 2.0, 1.0); + _screenToWorld = cameraScreenToWorld(); gl_Position = _vertex; } ]]> @@ -67,6 +69,7 @@ layout(binding = UNIFORM + 4) uniform sampler2D depthMap; layout(binding = UNIFORM + 5) uniform sampler2D shadowMap; layout(location = 0) in vec4 _vertex; +layout(location = 1) flat in mat4 _screenToWorld; layout(location = 0) out vec4 rgb; @@ -82,7 +85,7 @@ void main(void) { // Light model LIT if(slice0.w > 0.0) { float depth = texture(depthMap, proj).x; - vec3 world = getWorld(g.cameraScreenToWorld, proj, depth); + vec3 world = getWorld(_screenToWorld, proj, depth); vec3 n = normalize(slice0.xyz * 2.0 - 1.0); @@ -94,7 +97,7 @@ void main(void) { vec4 slice2 = texture(diffuseMap, proj); vec3 albedo = slice2.xyz; - vec3 v = normalize(g.cameraPosition.xyz - world); + vec3 v = normalize(cameraPosition() - world); vec3 h = normalize(uni.direction.xyz + v); float cosTheta = clamp(dot(uni.direction.xyz, n), 0.0, 1.0); diff --git a/worldeditor/bin/engine/materials/Downsample.shader b/worldeditor/bin/engine/materials/Downsample.shader index db24b5832..4e3c521b4 100644 --- a/worldeditor/bin/engine/materials/Downsample.shader +++ b/worldeditor/bin/engine/materials/Downsample.shader @@ -20,10 +20,10 @@ layout(location = 2) in vec4 _color; layout(location = 0) out vec4 color; void main(void) { - color = texture(rgbMap, _uv0 + g.cameraScreen.zw * vec2( 0.5f, 0.5f)) + - texture(rgbMap, _uv0 + g.cameraScreen.zw * vec2(-0.5f,-0.5f)) + - texture(rgbMap, _uv0 + g.cameraScreen.zw * vec2( 0.5f,-0.5f)) + - texture(rgbMap, _uv0 + g.cameraScreen.zw * vec2(-0.5f, 0.5f)); + color = texture(rgbMap, _uv0 + screenSizeNorm() * vec2( 0.5f, 0.5f)) + + texture(rgbMap, _uv0 + screenSizeNorm() * vec2(-0.5f,-0.5f)) + + texture(rgbMap, _uv0 + screenSizeNorm() * vec2( 0.5f,-0.5f)) + + texture(rgbMap, _uv0 + screenSizeNorm() * vec2(-0.5f, 0.5f)); color *= 0.25f; } diff --git a/worldeditor/bin/engine/materials/FXAA.shader b/worldeditor/bin/engine/materials/FXAA.shader index 7594c8c1b..acf2c0daa 100644 --- a/worldeditor/bin/engine/materials/FXAA.shader +++ b/worldeditor/bin/engine/materials/FXAA.shader @@ -26,10 +26,10 @@ layout(location = 0) out vec4 rgb; #include "Functions.h" void main (void) { - vec2 nw = _uv0 + vec2(-g.cameraScreen.z,-g.cameraScreen.w); - vec2 ne = _uv0 + vec2( g.cameraScreen.z,-g.cameraScreen.w); - vec2 sw = _uv0 + vec2(-g.cameraScreen.z, g.cameraScreen.w); - vec2 se = _uv0 + vec2( g.cameraScreen.z, g.cameraScreen.w); + vec2 nw = _uv0 + vec2(-screenSizeNorm().x,-screenSizeNorm().y); + vec2 ne = _uv0 + vec2( screenSizeNorm().x,-screenSizeNorm().y); + vec2 sw = _uv0 + vec2(-screenSizeNorm().x, screenSizeNorm().y); + vec2 se = _uv0 + vec2( screenSizeNorm().x, screenSizeNorm().y); vec3 rgbNW = texture( rgbMap, nw ).xyz; vec3 rgbNE = texture( rgbMap, ne ).xyz; @@ -55,7 +55,7 @@ void main (void) { dir = min(vec2( FXAA_SPAN_MAX, FXAA_SPAN_MAX), max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), - dir * rcpDirMin)) * g.cameraScreen.zw; + dir * rcpDirMin)) * screenSizeNorm(); vec3 rgbA = 0.5f * ( texture(rgbMap, _uv0 + dir * (1.0f / 3.0f - 0.5f)).xyz + diff --git a/worldeditor/bin/engine/materials/Frame.shader b/worldeditor/bin/engine/materials/Frame.shader index 5b46a514b..ae7a76dbc 100644 --- a/worldeditor/bin/engine/materials/Frame.shader +++ b/worldeditor/bin/engine/materials/Frame.shader @@ -13,20 +13,18 @@ #pragma flags -#include "ShaderLayout.h" -#include "Functions.h" - layout(location = 0) in vec4 _vertex; layout(location = 1) in vec2 _uv0; layout(location = 2) in vec4 _color; -layout(location = 6) in vec3 _view; -layout(location = 7) flat in vec4 _objectId; -layout(location = 8) flat in int _instanceOffset; -layout(location = 9) in mat4 _modelView; +layout(location = 6) flat in vec4 _objectId; +layout(location = 7) flat in int _instanceOffset; layout(location = 0) out vec4 rgb; +#include "ShaderLayout.h" +#include "Functions.h" + void main(void) { #pragma instance diff --git a/worldeditor/bin/engine/materials/IblReflections.shader b/worldeditor/bin/engine/materials/IblReflections.shader index c3b377649..b00c13b69 100644 --- a/worldeditor/bin/engine/materials/IblReflections.shader +++ b/worldeditor/bin/engine/materials/IblReflections.shader @@ -20,9 +20,11 @@ layout(location = 0) in vec3 vertex; layout(location = 0) out vec3 _vertex; +layout(location = 1) flat out mat4 _screenToWorld; void main(void) { _vertex = vertex * 2.0f; + _screenToWorld = cameraScreenToWorld(); gl_Position = vec4(_vertex, 1.0f); } ]]> @@ -44,6 +46,7 @@ layout(binding = UNIFORM + 5) uniform sampler2D sslrMap; layout(binding = UNIFORM + 6) uniform samplerCube iblMap; layout(location = 0) in vec3 _vertex; +layout(location = 1) flat in mat4 _screenToWorld; layout(location = 0) out vec4 color; @@ -62,8 +65,8 @@ void main(void) { if(normSamp.a > 0.0f) { vec3 n = normSamp.xyz * 2.0f - 1.0f; - vec3 world = getWorld(g.cameraScreenToWorld, proj, depth); - vec3 v = normalize(world - g.cameraPosition.xyz); + vec3 world = getWorld(_screenToWorld, proj, depth); + vec3 v = normalize(world - cameraPosition()); vec3 refl = reflect(v, n); float rough = texture(paramsMap, proj).x; @@ -77,7 +80,7 @@ void main(void) { } return; } - color = texture(iblMap, normalize(getWorld(g.cameraScreenToWorld, proj, 1.0f))); + color = texture(iblMap, normalize(getWorld(_screenToWorld, proj, 1.0f))); } ]]> diff --git a/worldeditor/bin/engine/materials/Link.shader b/worldeditor/bin/engine/materials/Link.shader index e79ea646b..d014f217d 100644 --- a/worldeditor/bin/engine/materials/Link.shader +++ b/worldeditor/bin/engine/materials/Link.shader @@ -13,10 +13,8 @@ layout(location = 0) in vec4 _vertex; layout(location = 1) in vec2 _uv0; layout(location = 2) in vec4 _color; -layout(location = 6) in vec3 _view; -layout(location = 7) flat in vec4 _objectId; -layout(location = 8) flat in int _instanceOffset; -layout(location = 9) in mat4 _modelView; +layout(location = 6) flat in vec4 _objectId; +layout(location = 7) flat in int _instanceOffset; layout(location = 0) out vec4 rgb; diff --git a/worldeditor/bin/engine/materials/PointLight.shader b/worldeditor/bin/engine/materials/PointLight.shader index b5625c8a0..d2958a8d3 100644 --- a/worldeditor/bin/engine/materials/PointLight.shader +++ b/worldeditor/bin/engine/materials/PointLight.shader @@ -38,12 +38,11 @@ layout(binding = LOCAL) uniform InstanceData { layout(location = 0) in vec3 vertex; layout(location = 0) out vec4 _vertex; +layout(location = 1) flat out mat4 _screenToWorld; void main(void) { - mat4 _modelView = g.view * uni.model; - - _vertex = g.projection * (_modelView * vec4(vertex, 1.0)); - + _vertex = cameraWorldToScreen() * uni.model * vec4(vertex, 1.0); + _screenToWorld = cameraScreenToWorld(); gl_Position = _vertex; } ]]> @@ -77,6 +76,7 @@ layout(binding = UNIFORM + 3) uniform sampler2D depthMap; layout(binding = UNIFORM + 4) uniform sampler2D shadowMap; layout(location = 0) in vec4 _vertex; +layout(location = 1) flat in mat4 _screenToWorld; layout(location = 0) out vec4 rgb; @@ -102,7 +102,7 @@ void main (void) { // Light model LIT if(slice0.w > 0.0) { float depth = texture(depthMap, proj).x; - vec3 world = getWorld(g.cameraScreenToWorld, proj, depth); + vec3 world = getWorld(_screenToWorld, proj, depth); vec3 dir = uni.position.xyz - world; float dist = length(dir); @@ -127,7 +127,7 @@ void main (void) { vec3 albedo = slice2.xyz; // Vectors - vec3 v = normalize(g.cameraPosition.xyz - world); + vec3 v = normalize(cameraPosition() - world); vec3 n = normalize(slice0.xyz * 2.0 - 1.0); vec3 r = -reflect(v, n); diff --git a/worldeditor/bin/engine/materials/SSAO.shader b/worldeditor/bin/engine/materials/SSAO.shader index a3d9ba56d..8d29c1497 100644 --- a/worldeditor/bin/engine/materials/SSAO.shader +++ b/worldeditor/bin/engine/materials/SSAO.shader @@ -8,6 +8,33 @@ + 0.0f) { - vec3 viewPos = getWorld(g.cameraProjectionInv, _uv0, depth); + vec3 viewPos = getWorld(_projectionInv, _uv0, depth); - vec3 view = mat3(g.cameraView) * (norm.xyz * 2.0f - 1.0f); + vec3 view = mat3(viewMatrix()) * (norm.xyz * 2.0f - 1.0f); vec3 normal = normalize(view); vec3 random = texture(noiseMap, _uv0 * scale).xyz; @@ -61,17 +88,17 @@ void main(void) { vec3 samp = tbn * uni.samplesKernel[i]; samp = viewPos + samp * uni.radius; - vec4 offset = g.cameraProjection * vec4(samp, 1.0f); + vec4 offset = projectionMatrix() * vec4(samp, 1.0f); offset.xyz /= offset.w; offset.xyz = offset.xyz * 0.5f + 0.5f; float sampleDepth = texture(depthMap, offset.xy).x; - sampleDepth = getWorld(g.cameraProjectionInv, offset.xy, sampleDepth).z; + sampleDepth = getWorld(_projectionInv, offset.xy, sampleDepth).z; float rangeCheck = smoothstep(0.0f, 1.0f, uni.radius / abs(viewPos.z - sampleDepth)); ssao += step(samp.z + uni.bias, sampleDepth) * rangeCheck; } - color = vec4(vec3(1.0f - ssao / MAX_SAMPLE_COUNT), 1.0); + color = vec4(vec3(1.0f - ssao / MAX_SAMPLE_COUNT), 1.0f); return; } } diff --git a/worldeditor/bin/engine/materials/SSLR.shader b/worldeditor/bin/engine/materials/SSLR.shader index 0aad5b995..c5595cb97 100644 --- a/worldeditor/bin/engine/materials/SSLR.shader +++ b/worldeditor/bin/engine/materials/SSLR.shader @@ -20,9 +20,11 @@ layout(location = 3) in vec3 normal; layout(location = 4) in vec3 tangent; layout(location = 0) out vec3 _vertex; +layout(location = 1) flat out mat4 _screenToWorld; void main(void) { _vertex = vertex * 2.0f; + _screenToWorld = cameraScreenToWorld(); gl_Position = vec4(_vertex, 1.0f); } ]]> @@ -41,6 +43,7 @@ layout(binding = UNIFORM + 2) uniform sampler2D paramsMap; layout(binding = UNIFORM + 3) uniform sampler2D emissiveMap; layout(location = 0) in vec3 _vertex; +layout(location = 1) flat in mat4 _screenToWorld; layout(location = 0) out vec4 color; @@ -112,13 +115,13 @@ void main(void) { } vec3 origin = vec3(proj, depth); - vec3 world = getWorld(g.cameraScreenToWorld, origin.xy, origin.z); + vec3 world = getWorld(_screenToWorld, origin.xy, origin.z); - vec3 v = normalize(world - g.cameraPosition.xyz); + vec3 v = normalize(world - cameraPosition()); vec3 n = normals.xyz * 2.0 - 1.0; vec3 refl = reflect(v, n); - vec4 ray = g.cameraWorldToScreen * vec4(world + refl, 1.0); + vec4 ray = cameraWorldToScreen() * vec4(world + refl, 1.0); ray /= ray.w; ray.xyz = ray.xyz * 0.5 + 0.5; diff --git a/worldeditor/bin/engine/materials/SpotLight.shader b/worldeditor/bin/engine/materials/SpotLight.shader index f9e9ee66f..4241a4117 100644 --- a/worldeditor/bin/engine/materials/SpotLight.shader +++ b/worldeditor/bin/engine/materials/SpotLight.shader @@ -38,12 +38,11 @@ layout(binding = LOCAL) uniform InstanceData { layout(location = 0) in vec3 vertex; layout(location = 0) out vec4 _vertex; +layout(location = 1) flat out mat4 _screenToWorld; void main(void) { - mat4 _modelView = g.view * uni.model; - - _vertex = g.projection * (_modelView * vec4(vertex, 1.0)); - + _vertex = cameraWorldToScreen() * uni.model * vec4(vertex, 1.0); + _screenToWorld = cameraScreenToWorld(); gl_Position = _vertex; } ]]> @@ -77,11 +76,7 @@ layout(binding = UNIFORM + 3) uniform sampler2D depthMap; layout(binding = UNIFORM + 4) uniform sampler2D shadowMap; layout(location = 0) in vec4 _vertex; -layout(location = 1) in vec2 _uv0; -layout(location = 2) in vec4 _color; -layout(location = 3) in vec3 _n; -layout(location = 4) in vec3 _t; -layout(location = 5) in vec3 _b; +layout(location = 1) flat in mat4 _screenToWorld; layout(location = 0) out vec4 rgb; @@ -97,7 +92,7 @@ void main (void) { // Light model LIT if(slice0.w > 0.0) { float depth = texture(depthMap, proj).x; - vec3 world = getWorld(g.cameraScreenToWorld, proj, depth); + vec3 world = getWorld(_screenToWorld, proj, depth); vec3 n = normalize(slice0.xyz * 2.0 - 1.0); @@ -120,7 +115,7 @@ void main (void) { vec4 slice2 = texture(diffuseMap, proj); vec3 albedo = slice2.xyz; - vec3 v = normalize(g.cameraPosition.xyz - world); + vec3 v = normalize(cameraPosition() - world); vec3 h = normalize(l + v); float cosTheta = clamp(dot(l, n), 0.0, 1.0); diff --git a/worldeditor/src/screens/scenecomposer/objectcontroller.cpp b/worldeditor/src/screens/scenecomposer/objectcontroller.cpp index 783fe9316..20ca09ef1 100644 --- a/worldeditor/src/screens/scenecomposer/objectcontroller.cpp +++ b/worldeditor/src/screens/scenecomposer/objectcontroller.cpp @@ -34,8 +34,6 @@ #include "actions/deleteobjects.h" #include "actions/createcomponent.h" #include "actions/createobjectserial.h" -#include "actions/duplicateobjects.h" -#include "actions/changeobjectproperty.h" #include "config.h"