Skip to content

Commit

Permalink
Merge pull request #1177 from HifiExperiments/stages
Browse files Browse the repository at this point in the history
Templated render stages
  • Loading branch information
ksuprynowicz authored Nov 29, 2024
2 parents 965ee15 + bdf3568 commit b8b6222
Showing 41 changed files with 423 additions and 908 deletions.
47 changes: 24 additions & 23 deletions libraries/entities-renderer/src/RenderableZoneEntityItem.cpp
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
//
// Created by Clement on 4/22/15.
// Copyright 2015 High Fidelity, Inc.
// Copyright 2024 Overte e.V.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
@@ -39,46 +40,46 @@ ZoneEntityRenderer::ZoneEntityRenderer(const EntityItemPointer& entity)
void ZoneEntityRenderer::onRemoveFromSceneTyped(const TypedEntityPointer& entity) {
if (_stage) {
if (!LightStage::isIndexInvalid(_sunIndex)) {
_stage->removeLight(_sunIndex);
_stage->removeElement(_sunIndex);
_sunIndex = INVALID_INDEX;
}
if (!LightStage::isIndexInvalid(_ambientIndex)) {
_stage->removeLight(_ambientIndex);
_stage->removeElement(_ambientIndex);
_ambientIndex = INVALID_INDEX;
}
}

if (_backgroundStage) {
if (!BackgroundStage::isIndexInvalid(_backgroundIndex)) {
_backgroundStage->removeBackground(_backgroundIndex);
_backgroundStage->removeElement(_backgroundIndex);
_backgroundIndex = INVALID_INDEX;
}
}

if (_hazeStage) {
if (!HazeStage::isIndexInvalid(_hazeIndex)) {
_hazeStage->removeHaze(_hazeIndex);
_hazeStage->removeElement(_hazeIndex);
_hazeIndex = INVALID_INDEX;
}
}

if (_bloomStage) {
if (!BloomStage::isIndexInvalid(_bloomIndex)) {
_bloomStage->removeBloom(_bloomIndex);
_bloomStage->removeElement(_bloomIndex);
_bloomIndex = INVALID_INDEX;
}
}

if (_tonemappingStage) {
if (!TonemappingStage::isIndexInvalid(_tonemappingIndex)) {
_tonemappingStage->removeTonemapping(_tonemappingIndex);
_tonemappingStage->removeElement(_tonemappingIndex);
_tonemappingIndex = INVALID_INDEX;
}
}

if (_ambientOcclusionStage) {
if (!AmbientOcclusionStage::isIndexInvalid(_ambientOcclusionIndex)) {
_ambientOcclusionStage->removeAmbientOcclusion(_ambientOcclusionIndex);
_ambientOcclusionStage->removeElement(_ambientOcclusionIndex);
_ambientOcclusionIndex = INVALID_INDEX;
}
}
@@ -123,7 +124,7 @@ void ZoneEntityRenderer::doRender(RenderArgs* args) {
{ // Sun
if (_needSunUpdate) {
if (LightStage::isIndexInvalid(_sunIndex)) {
_sunIndex = _stage->addLight(_sunLight);
_sunIndex = _stage->addElement(_sunLight);
} else {
_stage->updateLightArrayBuffer(_sunIndex);
}
@@ -136,7 +137,7 @@ void ZoneEntityRenderer::doRender(RenderArgs* args) {

if (_needAmbientUpdate) {
if (LightStage::isIndexInvalid(_ambientIndex)) {
_ambientIndex = _stage->addLight(_ambientLight);
_ambientIndex = _stage->addElement(_ambientLight);
} else {
_stage->updateLightArrayBuffer(_ambientIndex);
}
@@ -149,7 +150,7 @@ void ZoneEntityRenderer::doRender(RenderArgs* args) {

if (_needBackgroundUpdate) {
if (BackgroundStage::isIndexInvalid(_backgroundIndex)) {
_backgroundIndex = _backgroundStage->addBackground(_background);
_backgroundIndex = _backgroundStage->addElement(_background);
}
_needBackgroundUpdate = false;
}
@@ -158,7 +159,7 @@ void ZoneEntityRenderer::doRender(RenderArgs* args) {
{
if (_needHazeUpdate) {
if (HazeStage::isIndexInvalid(_hazeIndex)) {
_hazeIndex = _hazeStage->addHaze(_haze);
_hazeIndex = _hazeStage->addElement(_haze);
}
_needHazeUpdate = false;
}
@@ -167,7 +168,7 @@ void ZoneEntityRenderer::doRender(RenderArgs* args) {
{
if (_needBloomUpdate) {
if (BloomStage::isIndexInvalid(_bloomIndex)) {
_bloomIndex = _bloomStage->addBloom(_bloom);
_bloomIndex = _bloomStage->addElement(_bloom);
}
_needBloomUpdate = false;
}
@@ -176,7 +177,7 @@ void ZoneEntityRenderer::doRender(RenderArgs* args) {
{
if (_needTonemappingUpdate) {
if (TonemappingStage::isIndexInvalid(_tonemappingIndex)) {
_tonemappingIndex = _tonemappingStage->addTonemapping(_tonemapping);
_tonemappingIndex = _tonemappingStage->addElement(_tonemapping);
}
_needTonemappingUpdate = false;
}
@@ -185,7 +186,7 @@ void ZoneEntityRenderer::doRender(RenderArgs* args) {
{
if (_needAmbientOcclusionUpdate) {
if (AmbientOcclusionStage::isIndexInvalid(_ambientOcclusionIndex)) {
_ambientOcclusionIndex = _ambientOcclusionStage->addAmbientOcclusion(_ambientOcclusion);
_ambientOcclusionIndex = _ambientOcclusionStage->addElement(_ambientOcclusion);
}
_needAmbientOcclusionUpdate = false;
}
@@ -205,9 +206,9 @@ void ZoneEntityRenderer::doRender(RenderArgs* args) {
}

if (_skyboxMode == COMPONENT_MODE_DISABLED) {
_backgroundStage->_currentFrame.pushBackground(INVALID_INDEX);
_backgroundStage->_currentFrame.pushElement(INVALID_INDEX);
} else if (_skyboxMode == COMPONENT_MODE_ENABLED) {
_backgroundStage->_currentFrame.pushBackground(_backgroundIndex);
_backgroundStage->_currentFrame.pushElement(_backgroundIndex);
}

if (_ambientLightMode == COMPONENT_MODE_DISABLED) {
@@ -218,25 +219,25 @@ void ZoneEntityRenderer::doRender(RenderArgs* args) {

// Haze only if the mode is not inherit, as the model deals with on/off
if (_hazeMode != COMPONENT_MODE_INHERIT) {
_hazeStage->_currentFrame.pushHaze(_hazeIndex);
_hazeStage->_currentFrame.pushElement(_hazeIndex);
}

if (_bloomMode == COMPONENT_MODE_DISABLED) {
_bloomStage->_currentFrame.pushBloom(INVALID_INDEX);
_bloomStage->_currentFrame.pushElement(INVALID_INDEX);
} else if (_bloomMode == COMPONENT_MODE_ENABLED) {
_bloomStage->_currentFrame.pushBloom(_bloomIndex);
_bloomStage->_currentFrame.pushElement(_bloomIndex);
}

if (_tonemappingMode == COMPONENT_MODE_DISABLED) {
_tonemappingStage->_currentFrame.pushTonemapping(0); // Use the fallback tonemapping for "off"
_tonemappingStage->_currentFrame.pushElement(0); // Use the fallback tonemapping for "off"
} else if (_tonemappingMode == COMPONENT_MODE_ENABLED) {
_tonemappingStage->_currentFrame.pushTonemapping(_tonemappingIndex);
_tonemappingStage->_currentFrame.pushElement(_tonemappingIndex);
}

if (_ambientOcclusionMode == COMPONENT_MODE_DISABLED) {
_ambientOcclusionStage->_currentFrame.pushAmbientOcclusion(INVALID_INDEX);
_ambientOcclusionStage->_currentFrame.pushElement(INVALID_INDEX);
} else if (_ambientOcclusionMode == COMPONENT_MODE_ENABLED) {
_ambientOcclusionStage->_currentFrame.pushAmbientOcclusion(_ambientOcclusionIndex);
_ambientOcclusionStage->_currentFrame.pushElement(_ambientOcclusionIndex);
}
}

5 changes: 3 additions & 2 deletions libraries/render-utils/src/AmbientOcclusionEffect.cpp
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
//
// Created by Niraj Venkat on 7/15/15.
// Copyright 2015 High Fidelity, Inc.
// Copyright 2024 Overte e.V.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
@@ -606,8 +607,8 @@ void AmbientOcclusionEffect::run(const render::RenderContextPointer& renderConte
graphics::AmbientOcclusionPointer ambientOcclusion;
if (_debug) {
ambientOcclusion = _debugAmbientOcclusion;
} else if (ambientOcclusionStage && ambientOcclusionFrame->_ambientOcclusions.size()) {
ambientOcclusion = ambientOcclusionStage->getAmbientOcclusion(ambientOcclusionFrame->_ambientOcclusions.front());
} else if (ambientOcclusionStage && ambientOcclusionFrame->_elements.size()) {
ambientOcclusion = ambientOcclusionStage->getElement(ambientOcclusionFrame->_elements.front());
}

if (!ambientOcclusion || !lightingModel->isAmbientOcclusionEnabled()) {
48 changes: 3 additions & 45 deletions libraries/render-utils/src/AmbientOcclusionStage.cpp
Original file line number Diff line number Diff line change
@@ -7,50 +7,8 @@
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "AmbientOcclusionStage.h"

#include <gpu/Context.h>

std::string AmbientOcclusionStage::_stageName { "AMBIENT_OCCLUSION_STAGE" };
const AmbientOcclusionStage::Index AmbientOcclusionStage::INVALID_INDEX { render::indexed_container::INVALID_INDEX };

AmbientOcclusionStage::Index AmbientOcclusionStage::findAmbientOcclusion(const AmbientOcclusionPointer& ambientOcclusion) const {
auto found = _ambientOcclusionMap.find(ambientOcclusion);
if (found != _ambientOcclusionMap.end()) {
return INVALID_INDEX;
} else {
return (*found).second;
}
}

AmbientOcclusionStage::Index AmbientOcclusionStage::addAmbientOcclusion(const AmbientOcclusionPointer& ambientOcclusion) {
auto found = _ambientOcclusionMap.find(ambientOcclusion);
if (found == _ambientOcclusionMap.end()) {
auto ambientOcclusionId = _ambientOcclusions.newElement(ambientOcclusion);
// Avoid failing to allocate a ambientOcclusion, just pass
if (ambientOcclusionId != INVALID_INDEX) {
// Insert the ambientOcclusion and its index in the reverse map
_ambientOcclusionMap.insert(AmbientOcclusionMap::value_type(ambientOcclusion, ambientOcclusionId));
}
return ambientOcclusionId;
} else {
return (*found).second;
}
}

AmbientOcclusionStage::AmbientOcclusionPointer AmbientOcclusionStage::removeAmbientOcclusion(Index index) {
AmbientOcclusionPointer removed = _ambientOcclusions.freeElement(index);
if (removed) {
_ambientOcclusionMap.erase(removed);
}
return removed;
}

AmbientOcclusionStageSetup::AmbientOcclusionStageSetup() {}
#include "AmbientOcclusionStage.h"

void AmbientOcclusionStageSetup::run(const render::RenderContextPointer& renderContext) {
auto stage = renderContext->_scene->getStage(AmbientOcclusionStage::getName());
if (!stage) {
renderContext->_scene->resetStage(AmbientOcclusionStage::getName(), std::make_shared<AmbientOcclusionStage>());
}
}
template <>
std::string render::PointerStage<graphics::AmbientOcclusion, graphics::AmbientOcclusionPointer>::_name { "AMBIENT_OCCLUSION_STAGE" };
65 changes: 4 additions & 61 deletions libraries/render-utils/src/AmbientOcclusionStage.h
Original file line number Diff line number Diff line change
@@ -11,74 +11,17 @@
#ifndef hifi_render_utils_AmbientOcclusionStage_h
#define hifi_render_utils_AmbientOcclusionStage_h

#include <graphics/Stage.h>
#include <set>
#include <unordered_map>
#include <render/IndexedContainer.h>
#include <render/Stage.h>

#include <render/Forward.h>
#include <render/DrawTask.h>
#include <graphics/AmbientOcclusion.h>
#include <render/Stage.h>
#include <render/StageSetup.h>

// AmbientOcclusion stage to set up ambientOcclusion-related rendering tasks
class AmbientOcclusionStage : public render::Stage {
public:
static std::string _stageName;
static const std::string& getName() { return _stageName; }

using Index = render::indexed_container::Index;
static const Index INVALID_INDEX;
static bool isIndexInvalid(Index index) { return index == INVALID_INDEX; }

using AmbientOcclusionPointer = graphics::AmbientOcclusionPointer;
using AmbientOcclusions = render::indexed_container::IndexedPointerVector<graphics::AmbientOcclusion>;
using AmbientOcclusionMap = std::unordered_map<AmbientOcclusionPointer, Index>;

using AmbientOcclusionIndices = std::vector<Index>;

Index findAmbientOcclusion(const AmbientOcclusionPointer& ambientOcclusion) const;
Index addAmbientOcclusion(const AmbientOcclusionPointer& ambientOcclusion);

AmbientOcclusionPointer removeAmbientOcclusion(Index index);

bool checkAmbientOcclusionId(Index index) const { return _ambientOcclusions.checkIndex(index); }

Index getNumAmbientOcclusions() const { return _ambientOcclusions.getNumElements(); }
Index getNumFreeAmbientOcclusions() const { return _ambientOcclusions.getNumFreeIndices(); }
Index getNumAllocatedAmbientOcclusions() const { return _ambientOcclusions.getNumAllocatedIndices(); }

AmbientOcclusionPointer getAmbientOcclusion(Index ambientOcclusionId) const {
return _ambientOcclusions.get(ambientOcclusionId);
}

AmbientOcclusions _ambientOcclusions;
AmbientOcclusionMap _ambientOcclusionMap;

class Frame {
public:
Frame() {}

void clear() { _ambientOcclusions.clear(); }

void pushAmbientOcclusion(AmbientOcclusionStage::Index index) { _ambientOcclusions.emplace_back(index); }

AmbientOcclusionStage::AmbientOcclusionIndices _ambientOcclusions;
};
using FramePointer = std::shared_ptr<Frame>;

Frame _currentFrame;
};
class AmbientOcclusionStage : public render::PointerStage<graphics::AmbientOcclusion, graphics::AmbientOcclusionPointer> {};
using AmbientOcclusionStagePointer = std::shared_ptr<AmbientOcclusionStage>;

class AmbientOcclusionStageSetup {
class AmbientOcclusionStageSetup : public render::StageSetup<AmbientOcclusionStage> {
public:
using JobModel = render::Job::Model<AmbientOcclusionStageSetup>;

AmbientOcclusionStageSetup();
void run(const render::RenderContextPointer& renderContext);

protected:
};

#endif
1 change: 1 addition & 0 deletions libraries/render-utils/src/AssembleLightingStageTask.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//
// Created by Samuel Gateau on 2018/12/06
// Copyright 2013-2018 High Fidelity, Inc.
// Copyright 2024 Overte e.V.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
Loading

0 comments on commit b8b6222

Please sign in to comment.