Skip to content

Commit

Permalink
feat(rendering): add setting to toggle screen picking pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas7770 committed Jan 24, 2024
1 parent 32cf96c commit a0e8fdb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 31 deletions.
3 changes: 2 additions & 1 deletion engine/include/cubos/engine/renderer/deferred_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 4 additions & 2 deletions engine/include/cubos/engine/renderer/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
50 changes: 27 additions & 23 deletions engine/src/cubos/engine/renderer/deferred_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<DeferredGrid>(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<DeferredGrid>(drawCmd.grid);
mPickingIndexBp->setConstant(drawCmd.entityIndex);
mRenderDevice.setVertexArray(grid->va);
mRenderDevice.setIndexBuffer(grid->ib);
mRenderDevice.drawTrianglesIndexed(0, grid->indexCount);
}
}

// 6. SSAO pass.
Expand Down
6 changes: 4 additions & 2 deletions engine/src/cubos/engine/renderer/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ static void checkPaletteUpdateSystem(Assets& assets, Renderer& renderer, ActiveV
}

static void draw(Renderer& renderer, const ActiveCameras& activeCameras, RendererFrame& frame,
Query<const LocalToWorld&, const Camera&, Opt<const Viewport&>> query, ScreenPicker& screenPicker)
Query<const LocalToWorld&, const Camera&, Opt<const Viewport&>> query, ScreenPicker& screenPicker,
Settings& settings)
{
Camera cameras[4]{};
glm::mat4 views[4]{};
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions engine/src/cubos/engine/renderer/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit a0e8fdb

Please sign in to comment.