diff --git a/engine/include/cubos/engine/renderer/deferred_renderer.hpp b/engine/include/cubos/engine/renderer/deferred_renderer.hpp index 97f6c1951c..8f9e078d71 100644 --- a/engine/include/cubos/engine/renderer/deferred_renderer.hpp +++ b/engine/include/cubos/engine/renderer/deferred_renderer.hpp @@ -48,7 +48,8 @@ namespace cubos::engine void onResize(glm::uvec2 size) override; void onRender(const glm::mat4& view, const Viewport& viewport, const Camera& camera, const RendererFrame& frame, - core::gl::Framebuffer target, core::gl::Framebuffer pickingBuffer) override; + core::gl::Framebuffer target, core::gl::Framebuffer pickingBuffer, + bool enableScreenPicking) override; private: void createSSAOTextures(); diff --git a/engine/include/cubos/engine/renderer/renderer.hpp b/engine/include/cubos/engine/renderer/renderer.hpp index a18889b581..c5148ef262 100644 --- a/engine/include/cubos/engine/renderer/renderer.hpp +++ b/engine/include/cubos/engine/renderer/renderer.hpp @@ -94,10 +94,11 @@ namespace cubos::engine /// @param camera Camera to use. /// @param frame Frame to draw. /// @param pickingBuffer Screen picking framebuffer. + /// @param enableScreenPicking Whether to draw to the screen picking buffer. /// @param usePostProcessing Whether to use post processing. /// @param target Target framebuffer to draw to. void render(const glm::mat4& view, const Viewport& viewport, const engine::Camera& camera, - const RendererFrame& frame, const core::gl::Framebuffer& pickingBuffer, + const RendererFrame& frame, const core::gl::Framebuffer& pickingBuffer, bool enableScreenPicking, bool usePostProcessing = true, const core::gl::Framebuffer& target = nullptr); /// @brief Gets a reference to the post processing manager. @@ -126,9 +127,10 @@ namespace cubos::engine /// @param frame Frame to draw. /// @param target Target framebuffer. /// @param pickingBuffer Screen picking framebuffer. + /// @param enableScreenPicking Whether to draw to the screen picking buffer. virtual void onRender(const glm::mat4& view, const Viewport& viewport, const Camera& camera, const RendererFrame& frame, core::gl::Framebuffer target, - core::gl::Framebuffer pickingBuffer) = 0; + core::gl::Framebuffer pickingBuffer, bool enableScreenPicking) = 0; private: /// @brief Called when the internal texture used for post processing needs to be resized. diff --git a/engine/src/cubos/engine/renderer/deferred_renderer.cpp b/engine/src/cubos/engine/renderer/deferred_renderer.cpp index 8914142848..e50b2921f7 100644 --- a/engine/src/cubos/engine/renderer/deferred_renderer.cpp +++ b/engine/src/cubos/engine/renderer/deferred_renderer.cpp @@ -658,7 +658,8 @@ void DeferredRenderer::onResize(glm::uvec2 size) } void DeferredRenderer::onRender(const glm::mat4& view, const Viewport& viewport, const Camera& camera, - const RendererFrame& frame, Framebuffer target, core::gl::Framebuffer pickingBuffer) + const RendererFrame& frame, Framebuffer target, core::gl::Framebuffer pickingBuffer, + bool enableScreenPicking) { // Steps: // 1. Prepare the MVP matrix. @@ -786,29 +787,32 @@ void DeferredRenderer::onRender(const glm::mat4& view, const Viewport& viewport, mRenderDevice.drawTrianglesIndexed(0, grid->indexCount); } - // 5. Picking pass: - // 5.1. Set the picking pass state. - mRenderDevice.setFramebuffer(pickingBuffer); - mRenderDevice.setRasterState(mPickingRasterState); - mRenderDevice.setBlendState(mPickingBlendState); - mRenderDevice.setDepthStencilState(mPickingDepthStencilState); - mRenderDevice.setShaderPipeline(mPickingPipeline); - mPickingMVpBp->bind(mVpBuffer); - - // 5.2. For each draw command: - for (const auto& drawCmd : frame.drawCmds()) + if (enableScreenPicking) { - // 5.2.1. Update the MVP constant buffer with the model matrix. - mvp.m = drawCmd.modelMat; - memcpy(mVpBuffer->map(), &mvp, sizeof(MVP)); - mVpBuffer->unmap(); - - // 5.2.2. Draw the entity identifiers to the entity picking framebuffer. - auto grid = std::static_pointer_cast(drawCmd.grid); - mPickingIndexBp->setConstant(drawCmd.entityIndex); - mRenderDevice.setVertexArray(grid->va); - mRenderDevice.setIndexBuffer(grid->ib); - mRenderDevice.drawTrianglesIndexed(0, grid->indexCount); + // 5. Picking pass: + // 5.1. Set the picking pass state. + mRenderDevice.setFramebuffer(pickingBuffer); + mRenderDevice.setRasterState(mPickingRasterState); + mRenderDevice.setBlendState(mPickingBlendState); + mRenderDevice.setDepthStencilState(mPickingDepthStencilState); + mRenderDevice.setShaderPipeline(mPickingPipeline); + mPickingMVpBp->bind(mVpBuffer); + + // 5.2. For each draw command: + for (const auto& drawCmd : frame.drawCmds()) + { + // 5.2.1. Update the MVP constant buffer with the model matrix. + mvp.m = drawCmd.modelMat; + memcpy(mVpBuffer->map(), &mvp, sizeof(MVP)); + mVpBuffer->unmap(); + + // 5.2.2. Draw the entity identifiers to the entity picking framebuffer. + auto grid = std::static_pointer_cast(drawCmd.grid); + mPickingIndexBp->setConstant(drawCmd.entityIndex); + mRenderDevice.setVertexArray(grid->va); + mRenderDevice.setIndexBuffer(grid->ib); + mRenderDevice.drawTrianglesIndexed(0, grid->indexCount); + } } // 6. SSAO pass. diff --git a/engine/src/cubos/engine/renderer/plugin.cpp b/engine/src/cubos/engine/renderer/plugin.cpp index 86d055908a..69454e7fc2 100644 --- a/engine/src/cubos/engine/renderer/plugin.cpp +++ b/engine/src/cubos/engine/renderer/plugin.cpp @@ -116,7 +116,8 @@ static void checkPaletteUpdateSystem(Assets& assets, Renderer& renderer, ActiveV } static void draw(Renderer& renderer, const ActiveCameras& activeCameras, RendererFrame& frame, - Query> query, ScreenPicker& screenPicker) + Query> query, ScreenPicker& screenPicker, + Settings& settings) { Camera cameras[4]{}; glm::mat4 views[4]{}; @@ -159,7 +160,8 @@ static void draw(Renderer& renderer, const ActiveCameras& activeCameras, Rendere for (int i = 0; i < cameraCount; ++i) { - renderer->render(views[i], viewports[i], cameras[i], frame, screenPicker.framebuffer()); + renderer->render(views[i], viewports[i], cameras[i], frame, screenPicker.framebuffer(), + settings.getBool("cubos.renderer.screenpicking.enabled", true)); } frame.clear(); diff --git a/engine/src/cubos/engine/renderer/renderer.cpp b/engine/src/cubos/engine/renderer/renderer.cpp index c9193bfb27..b471608c2e 100644 --- a/engine/src/cubos/engine/renderer/renderer.cpp +++ b/engine/src/cubos/engine/renderer/renderer.cpp @@ -26,17 +26,17 @@ glm::uvec2 BaseRenderer::size() const void BaseRenderer::render(const glm::mat4& view, const Viewport& viewport, const engine::Camera& camera, const RendererFrame& frame, const core::gl::Framebuffer& pickingBuffer, - bool usePostProcessing, const core::gl::Framebuffer& target) + bool enableScreenPicking, bool usePostProcessing, const core::gl::Framebuffer& target) { if (usePostProcessing && mPpsManager.passCount() > 0) { - this->onRender(view, viewport, camera, frame, mFramebuffer, pickingBuffer); + this->onRender(view, viewport, camera, frame, mFramebuffer, pickingBuffer, enableScreenPicking); mPpsManager.provideInput(PostProcessingInput::Lighting, mTexture); mPpsManager.execute(target); } else { - this->onRender(view, viewport, camera, frame, target, pickingBuffer); + this->onRender(view, viewport, camera, frame, target, pickingBuffer, enableScreenPicking); } }