Skip to content

Commit 7820f68

Browse files
committed
Update the shadersui to allow runtime shader editing
1 parent 312acd9 commit 7820f68

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

src/osgEarth/ImGui/ShaderGUI

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,21 @@
2020
#define OSGEARTH_IMGUI_SHADER_GUI
2121

2222
#include "ImGui"
23-
#include <osgEarth/Threading>
23+
#include <osgEarth/Registry>
24+
#include <osgEarth/VirtualProgram>
2425
#include <osg/Uniform>
2526
#include <osg/StateSet>
2627
#include <chrono>
2728
#include <list>
29+
#include <vector>
2830

2931
namespace osgEarth
3032
{
3133
namespace GUI
3234
{
3335
using namespace osgEarth;
3436
using namespace osgEarth::Threading;
37+
using namespace osgEarth::Util;
3538

3639
class ShaderGUI : public BaseGUI
3740
{
@@ -49,6 +52,8 @@ namespace osgEarth
4952
};
5053
std::vector<DefineSpec> _defines;
5154

55+
ProgramRepo::ProgramMap _db;
56+
5257
public:
5358
ShaderGUI(osg::ArgumentParser* args) : BaseGUI("Shaders")
5459
{
@@ -117,6 +122,49 @@ namespace osgEarth
117122
ri.getCurrentCamera()->getOrCreateStateSet()->setDefine(def._name, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);
118123
}
119124
}
125+
126+
ImGui::Separator();
127+
ImGui::TextColored(ImVec4(1, 1, 0, 1), "Programs:");
128+
129+
if (ImGui::Button("Reload"))
130+
{
131+
_db = Registry::programRepo().copy();
132+
}
133+
134+
for (auto& entry : _db)
135+
{
136+
if (entry.second->_users.size() > 0)
137+
{
138+
auto program = entry.second->_program;
139+
auto name = program->getName();
140+
if (name.empty()) name = "[no name]";
141+
if (ImGui::TreeNode(name.c_str()))
142+
{
143+
auto num = program->getNumShaders();
144+
for (unsigned i = 0; i < num; ++i)
145+
{
146+
auto shader = program->getShader(i);
147+
auto shader_name = shader->getName();
148+
if (shader_name.empty()) shader_name = "[no name]";
149+
if (ImGui::TreeNode(shader->getName().c_str()))
150+
{
151+
if (ImGui::Button("Compile"))
152+
{
153+
program->releaseGLObjects(nullptr);
154+
}
155+
auto text = shader->getShaderSource();
156+
if (ImGuiUtil::InputTextMultiline("##shader_source", &text, ImVec2(-1, -1)))
157+
{
158+
shader->setShaderSource(text);
159+
program->releaseGLObjects(nullptr);
160+
}
161+
ImGui::TreePop();
162+
}
163+
}
164+
ImGui::TreePop();
165+
}
166+
}
167+
}
120168
}
121169
ImGui::End();
122170
}

src/osgEarth/ImGui/ViewpointsGUI

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ namespace osgEarth {
6666
{
6767
_viewpoints.push_back(Viewpoint(j));
6868

69-
#if 0 // works, but conflicts with the ViewpointsExtension :)
70-
if (key < 9)
69+
// warning, if viewpoints:map_keys == true, this will conflict.
70+
if (false) //key < 9)
7171
{
7272
EventRouter::get(view(ri)).onKeyPress(
7373
(osgGA::GUIEventAdapter::KeySymbol)((int)osgGA::GUIEventAdapter::KEY_1 + key),
@@ -78,7 +78,6 @@ namespace osgEarth {
7878
});
7979
key++;
8080
}
81-
#endif
8281
}
8382
}
8483
}

src/osgEarth/VirtualProgram

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace osgEarth
3737
{
3838
namespace Util
3939
{
40-
class /*internal*/ ProgramRepo : public Threading::Mutexed<osg::Referenced>
40+
class OSGEARTH_EXPORT ProgramRepo : public Threading::Mutexed<osg::Referenced>
4141
{
4242
public:
4343
// Program key (must be sorted set)
@@ -92,6 +92,13 @@ namespace osgEarth
9292

9393
~ProgramRepo();
9494

95+
ProgramMap copy() const {
96+
lock();
97+
auto result = _db;
98+
unlock();
99+
return result;
100+
}
101+
95102
private:
96103
mutable ProgramMap _db;
97104
bool _releaseUnusedPrograms;

0 commit comments

Comments
 (0)