Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling Global Illumination (GI VCT) for sensors in SDF #2550

Merged
merged 25 commits into from
Sep 10, 2024
Merged
Changes from 3 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4afb41b
Hack to enable GI for sensors
athenaz2 Aug 21, 2024
e36edfd
Parse and set SDF parameters for GI VCT
athenaz2 Aug 26, 2024
bd93e33
Add doxygen comments and cleaned up code a bit
athenaz2 Aug 27, 2024
f401172
Update src/systems/sensors/Sensors.cc
athenaz2 Aug 27, 2024
f82c68e
Add comments documenting helper funcs as well as clarifying comments;…
athenaz2 Aug 28, 2024
5a1d4ee
Add GI demo SDF as example
athenaz2 Aug 28, 2024
bb70589
Add test scene SDFs and test file
athenaz2 Aug 29, 2024
f725194
Minor codecheck modification
athenaz2 Aug 29, 2024
843c0c8
Minor var rename
athenaz2 Aug 29, 2024
407ab1c
Merge branch 'gz-sim8' into athenaz/gi_for_sensors
azeey Aug 29, 2024
0ab686a
Add surrounding box in SDF, set test expectations to be more stark
athenaz2 Aug 30, 2024
8738398
Minor codecheck modification for SDF, disable GIEnabled test for macO…
athenaz2 Aug 30, 2024
56b841b
Comment out GINotEnabled test to check segfault issue
athenaz2 Aug 30, 2024
0293b5f
Disable test if MESA_GL_VERSION_OVERRIDE env var is set
athenaz2 Aug 31, 2024
db226c2
Add GI tutorial
athenaz2 Sep 3, 2024
e310454
At end of RenderThread, reset giVct shared ptr
athenaz2 Sep 4, 2024
420231d
Merge branch 'gz-sim8' into athenaz/gi_for_sensors
athenaz2 Sep 4, 2024
3f64dd0
Trim trailing whitespace
athenaz2 Sep 5, 2024
6a7a6c2
Merge branch 'athenaz/gi_for_sensors' of https://github.com/athenaz2/…
athenaz2 Sep 5, 2024
10f1b37
Merge branch 'gz-sim8' into athenaz/gi_for_sensors
athenaz2 Sep 6, 2024
7f49293
Apply suggestions from code review
athenaz2 Sep 6, 2024
f04599f
Apply suggestions from code review
athenaz2 Sep 6, 2024
95530a6
Remove commented out sections in SDFs, remove GUI plugins from SDF be…
athenaz2 Sep 7, 2024
f03476b
remove whitespace
iche033 Sep 9, 2024
966f48c
hide scene manager and view control gui plugins
iche033 Sep 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 201 additions & 0 deletions src/systems/sensors/Sensors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,71 @@

#include "gz/sim/rendering/Events.hh"
#include "gz/sim/rendering/RenderUtil.hh"
#include "gz/rendering/GlobalIlluminationVct.hh"

using namespace gz;
using namespace sim;
using namespace systems;

/// \brief GI parameters holding default data
struct GiDefaultData
{
/// \brief See rendering::GlobalIlluminationVct::SetResolution
math::Vector3d resolution{16, 16, 16};

/// \brief See rendering::GlobalIlluminationVct::SetOctantCount
math::Vector3d octantCount{1, 1, 1};

/// \brief See rendering::GlobalIlluminationVct::SetBounceCount
uint32_t bounceCount = 6;

/// \brief See rendering::GlobalIlluminationVct::SetHighQuality
bool highQuality = true;

/// \brief See rendering::GlobalIlluminationVct::SetAnisotropic
bool anisotropic = true;

/// \brief See rendering::GlobalIlluminationVct::SetThinWallCounter
float thinWallCounter = 1.0f;

/// \brief See rendering::GlobalIlluminationVct::SetConserveMemory
bool conserveMemory = false;

/// \brief See rendering::GlobalIlluminationVct::DebugVisualizationMode
uint32_t debugVisMode = rendering::GlobalIlluminationVct::DVM_None;
};

/// \brief GI VCT flag and parameters
struct GiVctParameters
{
/// \brief VCT enabled flag
bool enabled = false;

/// \brief See rendering::GlobalIlluminationVct::SetResolution
uint32_t resolution[3];

/// \brief See rendering::GlobalIlluminationVct::SetOctantCount
uint32_t octantCount[3];

/// \brief See rendering::GlobalIlluminationVct::SetBounceCount
uint32_t bounceCount;

/// \brief See rendering::GlobalIlluminationVct::SetHighQuality
bool highQuality;

/// \brief See rendering::GlobalIlluminationVct::SetAnisotropic
bool anisotropic;

/// \brief See rendering::GlobalIlluminationVct::SetThinWallCounter
float thinWallCounter;

/// \brief See rendering::GlobalIlluminationVct::SetConserveMemory
bool conserveMemory;

/// \brief See rendering::GlobalIlluminationVct::DebugVisualizationMode
uint32_t debugVisMode;
};

// Private data class.
class gz::sim::systems::SensorsPrivate
{
Expand All @@ -89,6 +149,18 @@ class gz::sim::systems::SensorsPrivate
/// generate sensor data
public: rendering::ScenePtr scene;

/// \brief Pointer to GlobalIlluminationVct
public: rendering::GlobalIlluminationVctPtr giVct;

/// \brief GI VCT parameters passed to giVct
public: GiVctParameters giVctParameters;

/// \brief Default GI data
public: GiDefaultData giDefaultData;

/// \brief GI built flag
public: bool giBuilt = false;

/// \brief Temperature used by thermal camera. Defaults to temperature at
/// sea level
public: double ambientTemperature = 288.15;
Expand Down Expand Up @@ -288,6 +360,28 @@ void SensorsPrivate::WaitForInit()
#endif
this->scene = this->renderUtil.Scene();
this->scene->SetCameraPassCountPerGpuFlush(6u);

if (this->giVctParameters.enabled)
{
this->giVct = this->scene->CreateGlobalIlluminationVct();
this->giVct->SetParticipatingVisuals(
rendering::GlobalIlluminationBase::DYNAMIC_VISUALS |
rendering::GlobalIlluminationBase::STATIC_VISUALS);

this->giVct->SetResolution(this->giVctParameters.resolution);
this->giVct->SetOctantCount(this->giVctParameters.octantCount);
this->giVct->SetBounceCount(this->giVctParameters.bounceCount);
this->giVct->SetAnisotropic(this->giVctParameters.anisotropic);
this->giVct->SetHighQuality(this->giVctParameters.highQuality);
this->giVct->SetConserveMemory(this->giVctParameters.conserveMemory);
this->giVct->SetThinWallCounter(this->giVctParameters.thinWallCounter);

this->giVct->SetDebugVisualization(
rendering::GlobalIlluminationVct::DVM_None);

this->scene->SetActiveGlobalIllumination(this->giVct);
}

this->initialized = true;
}

Expand Down Expand Up @@ -359,6 +453,19 @@ void SensorsPrivate::RunOnce()
// We only need to do this once per frame It is important to call
// sensors::RenderingSensor::SetManualSceneUpdate and set it to true
// so we don't waste cycles doing one scene graph update per sensor

if (!this->giBuilt)
{
if (this->giVctParameters.enabled)
{
this->giVct->Build();
this->giVct->SetDebugVisualization(
static_cast<rendering::GlobalIlluminationVct::DebugVisualizationMode>
(this->giVctParameters.debugVisMode));
this->giBuilt = true;
}
}

this->scene->PreRender();
}

Expand Down Expand Up @@ -521,6 +628,52 @@ Sensors::~Sensors()
this->dataPtr->Stop();
}

//TODO: why does math::Vector3d work but not math::Vector3i?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static void ConvertDoubleToUInt32x3(sdf::ElementConstPtr _parentElem,
const char* _childName, uint32_t _valueToSet[3], math::Vector3d _defaultValue)
{
math::Vector3d parsedValues = (_parentElem == nullptr) ?
_defaultValue : _parentElem->Get<math::Vector3d>(_childName, _defaultValue).first;

_valueToSet[0] = static_cast<uint32_t>(parsedValues[0]);
_valueToSet[1] = static_cast<uint32_t>(parsedValues[1]);
_valueToSet[2] = static_cast<uint32_t>(parsedValues[2]);
}

static void ConvertDoubleToUInt32x3(uint32_t _valueToSet[3], math::Vector3d _defaultValue)
{
ConvertDoubleToUInt32x3(nullptr, "", _valueToSet, _defaultValue);
}
athenaz2 marked this conversation as resolved.
Show resolved Hide resolved

static void SetDebugVisMode(const std::string &_text,
uint32_t &_modeToSet, uint32_t _defaultMode)
{
if (_text == "albedo")
{
_modeToSet = rendering::GlobalIlluminationVct::DVM_Albedo;
}
else if (_text == "normal")
{
_modeToSet = rendering::GlobalIlluminationVct::DVM_Normal;
}
else if (_text == "emissive")
{
_modeToSet = rendering::GlobalIlluminationVct::DVM_Emissive;
}
else if (_text == "lighting")
{
_modeToSet = rendering::GlobalIlluminationVct::DVM_Lighting;
}
else if (_text == "none")
{
_modeToSet = rendering::GlobalIlluminationVct::DVM_None;
}
else
{
_modeToSet = _defaultMode;
}
}

//////////////////////////////////////////////////
void Sensors::Configure(const Entity &/*_id*/,
const std::shared_ptr<const sdf::Element> &_sdf,
Expand Down Expand Up @@ -549,6 +702,54 @@ void Sensors::Configure(const Entity &/*_id*/,
if (_sdf->HasElement("ambient_light"))
this->dataPtr->ambientLight = _sdf->Get<math::Color>("ambient_light");

// Get the global illumination technique and its parameters, if specified
if (_sdf->HasElement("global_illumination"))
{
if (engineName != "ogre2")
{
gzerr << "Global illumination is only supported by the ogre2 render engine" << std::endl;
}
else
{
auto giElem = _sdf->FindElement("global_illumination");
std::string giType = giElem->GetAttribute("type")->GetAsString();
if (giType == "vct")
{
this->dataPtr->giVctParameters.enabled = giElem->Get<bool>("enabled", this->dataPtr->giVctParameters.enabled).first;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrap lines to 80char

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked style with codecheck in f82c68e


if (giElem->HasElement("resolution"))
athenaz2 marked this conversation as resolved.
Show resolved Hide resolved
ConvertDoubleToUInt32x3(giElem, "resolution", this->dataPtr->giVctParameters.resolution, this->dataPtr->giDefaultData.resolution);
else
ConvertDoubleToUInt32x3(this->dataPtr->giVctParameters.resolution, this->dataPtr->giDefaultData.resolution);

if (giElem->HasElement("octant_count"))
ConvertDoubleToUInt32x3(giElem, "octant_count", this->dataPtr->giVctParameters.octantCount, this->dataPtr->giDefaultData.octantCount);
else
ConvertDoubleToUInt32x3(this->dataPtr->giVctParameters.octantCount, this->dataPtr->giDefaultData.octantCount);

this->dataPtr->giVctParameters.bounceCount = giElem->Get<uint32_t>("bounce_count", this->dataPtr->giVctParameters.bounceCount).first;
this->dataPtr->giVctParameters.highQuality = giElem->Get<bool>("high_quality", this->dataPtr->giVctParameters.highQuality).first;
this->dataPtr->giVctParameters.anisotropic = giElem->Get<bool>("anisotropic", this->dataPtr->giVctParameters.anisotropic).first;
this->dataPtr->giVctParameters.thinWallCounter = giElem->Get<float>("thin_wall_counter", this->dataPtr->giVctParameters.thinWallCounter).first;
this->dataPtr->giVctParameters.conserveMemory = giElem->Get<bool>("conserve_memory", this->dataPtr->giVctParameters.conserveMemory).first;

if (giElem->HasElement("debug_vis_mode"))
{
const std::string text = giElem->Get<std::string>("debug_vis_mode", "none").first;
SetDebugVisMode(text, this->dataPtr->giVctParameters.debugVisMode, this->dataPtr->giDefaultData.debugVisMode);
}
}
else if (giType == "civct")
{
//todo: add CIVCT here. should also check if apiBackend is vulkan
gzerr << "GI CI VCT is not supported" << std::endl;
} else
{
gzerr << "GI method type [" << giType << "] is not supported." << std::endl;
}
}
}

this->dataPtr->renderUtil.SetEngineName(engineName);
#ifdef __APPLE__
if (apiBackend.empty())
Expand Down