Skip to content

Commit

Permalink
Update the shadersui to allow runtime shader editing
Browse files Browse the repository at this point in the history
  • Loading branch information
gwaldron committed Jan 12, 2024
1 parent 312acd9 commit 7820f68
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
50 changes: 49 additions & 1 deletion src/osgEarth/ImGui/ShaderGUI
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@
#define OSGEARTH_IMGUI_SHADER_GUI

#include "ImGui"
#include <osgEarth/Threading>
#include <osgEarth/Registry>
#include <osgEarth/VirtualProgram>
#include <osg/Uniform>
#include <osg/StateSet>
#include <chrono>
#include <list>
#include <vector>

namespace osgEarth
{
namespace GUI
{
using namespace osgEarth;
using namespace osgEarth::Threading;
using namespace osgEarth::Util;

class ShaderGUI : public BaseGUI
{
Expand All @@ -49,6 +52,8 @@ namespace osgEarth
};
std::vector<DefineSpec> _defines;

ProgramRepo::ProgramMap _db;

public:
ShaderGUI(osg::ArgumentParser* args) : BaseGUI("Shaders")
{
Expand Down Expand Up @@ -117,6 +122,49 @@ namespace osgEarth
ri.getCurrentCamera()->getOrCreateStateSet()->setDefine(def._name, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);
}
}

ImGui::Separator();
ImGui::TextColored(ImVec4(1, 1, 0, 1), "Programs:");

if (ImGui::Button("Reload"))
{
_db = Registry::programRepo().copy();
}

for (auto& entry : _db)
{
if (entry.second->_users.size() > 0)
{
auto program = entry.second->_program;
auto name = program->getName();
if (name.empty()) name = "[no name]";
if (ImGui::TreeNode(name.c_str()))
{
auto num = program->getNumShaders();
for (unsigned i = 0; i < num; ++i)
{
auto shader = program->getShader(i);
auto shader_name = shader->getName();
if (shader_name.empty()) shader_name = "[no name]";
if (ImGui::TreeNode(shader->getName().c_str()))
{
if (ImGui::Button("Compile"))
{
program->releaseGLObjects(nullptr);
}
auto text = shader->getShaderSource();
if (ImGuiUtil::InputTextMultiline("##shader_source", &text, ImVec2(-1, -1)))
{
shader->setShaderSource(text);
program->releaseGLObjects(nullptr);
}
ImGui::TreePop();
}
}
ImGui::TreePop();
}
}
}
}
ImGui::End();
}
Expand Down
5 changes: 2 additions & 3 deletions src/osgEarth/ImGui/ViewpointsGUI
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ namespace osgEarth {
{
_viewpoints.push_back(Viewpoint(j));

#if 0 // works, but conflicts with the ViewpointsExtension :)
if (key < 9)
// warning, if viewpoints:map_keys == true, this will conflict.
if (false) //key < 9)
{
EventRouter::get(view(ri)).onKeyPress(
(osgGA::GUIEventAdapter::KeySymbol)((int)osgGA::GUIEventAdapter::KEY_1 + key),
Expand All @@ -78,7 +78,6 @@ namespace osgEarth {
});
key++;
}
#endif
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/osgEarth/VirtualProgram
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace osgEarth
{
namespace Util
{
class /*internal*/ ProgramRepo : public Threading::Mutexed<osg::Referenced>
class OSGEARTH_EXPORT ProgramRepo : public Threading::Mutexed<osg::Referenced>
{
public:
// Program key (must be sorted set)
Expand Down Expand Up @@ -92,6 +92,13 @@ namespace osgEarth

~ProgramRepo();

ProgramMap copy() const {
lock();
auto result = _db;
unlock();
return result;
}

private:
mutable ProgramMap _db;
bool _releaseUnusedPrograms;
Expand Down

0 comments on commit 7820f68

Please sign in to comment.