Skip to content

Commit

Permalink
Add ability to debug shaders when using plugin provided shader editor…
Browse files Browse the repository at this point in the history
… + bump version
  • Loading branch information
dfranx committed Oct 3, 2021
1 parent 729b5fa commit 659d7d3
Show file tree
Hide file tree
Showing 10 changed files with 401 additions and 283 deletions.
20 changes: 14 additions & 6 deletions src/SHADERed/Objects/PluginAPI/Plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ namespace ed {
typedef int (*GetIncludePathCountFn)();
typedef const char* (*GetIncludePathFn)(void* project, int index);
typedef const char* (*GetMessagesCurrentItemFn)(void* messages);

typedef void (*OnEditorContentChangeFn)(void* UI, void* plugin, int langID, int editorID);
typedef unsigned int* (*GetPipelineItemSPIRVFn)(void* item, ed::plugin::ShaderStage stage, int* dataLen);
typedef void (*RegisterShortcutFn)(void* plugin, const char* name);
Expand Down Expand Up @@ -132,7 +132,6 @@ namespace ed {

typedef float (*ScaleSizeFn)(float size);


/********** IPlugin2 **********/
typedef int (*GetHostIPluginMaxVersionFn)();
typedef void (*ImGuiFileDialogOpenFn)(const char* key, const char* title, const char* filter);
Expand All @@ -148,13 +147,17 @@ namespace ed {
typedef void* (*GetEditorPipelineItemFn)(void* UI, void* plugin, int langID, int editorID);
typedef void (*SetViewportSizeFn)(float w, float h);
typedef int (*IsObjectBoundFn)(void* Objects, const char* name, void* pipelineItem);
typedef void (*DebuggerStepIntoPluginEditorFn)(void* Debugger, void* Code, void* Plugin, int lang, int editor);
typedef void (*DebuggerGetVariableValueFn)(void* Debugger, const char* name, char* value, int valueLength);
typedef void (*DebuggerStopPluginEditorFn)(void* Debugger, void* Code, void* Plugin, int lang, int editor);
typedef bool (*DebuggerIsVMRunningFn)(void* Debugger);
}

// CreatePlugin(), DestroyPlugin(ptr), GetPluginAPIVersion(), GetPluginVersion(), GetPluginName()
class IPlugin1 {
public:
virtual int GetVersion() { return 1; }

virtual bool Init(bool isWeb, int sedVersion) = 0;
virtual void InitUI(void* ctx) = 0;
virtual void OnEvent(void* e) = 0; // e is &SDL_Event
Expand Down Expand Up @@ -342,7 +345,7 @@ namespace ed {
virtual void HandlePluginMessage(const char* sender, char* msg, int msgLen) = 0;
virtual void HandleApplicationEvent(ed::plugin::ApplicationEvent event, void* data1, void* data2) = 0;
virtual void HandleNotification(int id) = 0;

// host functions
void *Renderer, *Messages, *Project, *UI, *ObjectManager, *PipelineManager, *Plugins, *Debugger;
pluginfn::AddObjectFn AddObject;
Expand Down Expand Up @@ -460,7 +463,7 @@ namespace ed {
virtual bool PipelineItem_SupportsImmediateMode(const char* type, void* data, ed::plugin::ShaderStage stage) = 0;
virtual bool PipelineItem_HasCustomImmediateModeCompiler(const char* type, void* data, ed::plugin::ShaderStage stage) = 0;
virtual bool PipelineItem_ImmediateModeCompile(const char* type, void* data, ed::plugin::ShaderStage stage, const char* expression) = 0;

virtual unsigned int ImmediateMode_GetSPIRVSize() = 0;
virtual unsigned int* ImmediateMode_GetSPIRV() = 0;
virtual unsigned int ImmediateMode_GetVariableCount() = 0;
Expand All @@ -483,10 +486,15 @@ namespace ed {

virtual void PluginManager_RegisterPlugins() = 0;
virtual const unsigned int* CustomLanguage_CompileToSPIRV2(void* item, int langID, const char* src, size_t src_len, ed::plugin::ShaderStage stage, const char* entry, ed::plugin::ShaderMacro* macros, size_t macroCount, size_t* spv_length, bool* compiled) = 0;

virtual void ShaderEditor_SetLineIndicator(int langID, int editorID, int line) = 0;

pluginfn::RegisterPluginFn RegisterPlugin;
pluginfn::GetEditorPipelineItemFn GetEditorPipelineItem;
pluginfn::SetViewportSizeFn SetViewportSize;
pluginfn::IsObjectBoundFn IsObjectBound;
pluginfn::DebuggerStepIntoPluginEditorFn DebuggerStepIntoPluginEditor;
pluginfn::DebuggerGetVariableValueFn DebuggerGetVariableValue;
pluginfn::DebuggerStopPluginEditorFn DebuggerStopPluginEditor;
pluginfn::DebuggerIsVMRunningFn DebuggerIsVMRunning;
};
}
46 changes: 45 additions & 1 deletion src/SHADERed/Objects/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,34 @@ namespace ed {
ObjectManager* obj = (ObjectManager*)Objects;
return obj->IsBound(obj->Get(name), (PipelineItem*)pipelineItem);
};
plugin3->DebuggerStepIntoPluginEditor = [](void* Debugger, void* UI, void* Plugin, int lang, int editorID) {
((ed::DebugInformation*)Debugger)->StepInto();
int curLine = ((ed::DebugInformation*)Debugger)->GetCurrentLine();
((IPlugin3*)Plugin)->ShaderEditor_SetLineIndicator(lang, editorID, curLine);
};
plugin3->DebuggerGetVariableValue = [](void* Debugger, const char* name, char* value, int valueLength) {
ed::DebugInformation* dbgr = ((ed::DebugInformation*)Debugger);

spvm_result_t memType;
size_t memCount = 0;
spvm_member_t mem = dbgr->GetVariable(name, memCount, memType);

std::stringstream ss;
dbgr->GetVariableValueAsString(ss, dbgr->GetVM(), memType, mem, memCount, "");

std::string res = ss.str();
if (res.size() >= valueLength - 1)
res = res.substr(0, valueLength - 1);

strcpy(value, res.c_str());
};
plugin3->DebuggerStopPluginEditor = [](void* Debugger, void* UI, void* Plugin, int lang, int editorID) {
((ed::DebugInformation*)Debugger)->SetDebugging(false);
((IPlugin3*)Plugin)->ShaderEditor_SetLineIndicator(lang, editorID, -1);
};
plugin3->DebuggerIsVMRunning = [](void* Debugger) -> bool {
return ((ed::DebugInformation*)Debugger)->IsVMRunning();
};
}

#ifdef SHADERED_DESKTOP
Expand All @@ -1059,8 +1087,24 @@ namespace ed {

if (initResult)
ed::Logger::Get().Log("Plugin \"" + pname + "\" successfully initialized.");
else
else {
ed::Logger::Get().Log("Failed to initialize plugin \"" + pname + "\".");

#if defined(__linux__) || defined(__unix__) || defined(__APPLE__)
DestroyPluginFn fnDestroyPlugin = (DestroyPluginFn)dlsym(procDLL, "DestroyPlugin");
if (fnDestroyPlugin)
(*fnDestroyPlugin)(plugin);

dlclose(procDLL);
#else
DestroyPluginFn fnDestroyPlugin = (DestroyPluginFn)GetProcAddress((HINSTANCE)procDLL, "DestroyPlugin");
if (fnDestroyPlugin)
(*fnDestroyPlugin)(plugin);

FreeLibrary((HINSTANCE)procDLL);
#endif
return;
}

m_plugins.push_back(plugin);
m_proc.push_back(procDLL);
Expand Down
2 changes: 1 addition & 1 deletion src/SHADERed/Objects/WebAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace ed {
const std::string WebAPI::URL = "http://api.shadered.org";
const char* WebAPI::Version = "1.5.4";
const char* WebAPI::Version = "1.5.5";

bool isDigitsOnly(const std::string& str)
{
Expand Down
2 changes: 1 addition & 1 deletion src/SHADERed/Objects/WebAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace ed {
class WebAPI {
public:
static const std::string URL;
static const int InternalVersion = 25;
static const int InternalVersion = 26;
static const char* Version;

// info that /api/search will return
Expand Down
2 changes: 1 addition & 1 deletion src/SHADERed/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#define SHADERED_DESKTOP
// #define SHADERED_WEB
#define SHADERED_VERSION 1005004
#define SHADERED_VERSION 1005005

#define DEBUG_ID_START 1
#define DEBUG_PRIMITIVE_GROUP 170
Expand Down
34 changes: 31 additions & 3 deletions src/SHADERed/UI/CodeEditorUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,29 @@ namespace ed {
return nullptr;
}

PluginShaderEditor CodeEditorUI::GetPluginEditor(PipelineItem* item, ed::ShaderStage stage)
{
for (int i = 0; i < m_items.size(); i++)
if (m_items[i] == item && m_shaderStage[i] == stage)
return m_pluginEditor[i];
return PluginShaderEditor();
}
PluginShaderEditor CodeEditorUI::GetPluginEditor(const std::string& path)
{
for (int i = 0; i < m_paths.size(); i++)
if (m_paths[i] == path)
return m_pluginEditor[i];
return PluginShaderEditor();
}
std::string CodeEditorUI::GetPluginEditorPath(const PluginShaderEditor& editor)
{
for (int i = 0; i < m_pluginEditor.size(); i++)
if (m_pluginEditor[i].ID == editor.ID && m_pluginEditor[i].LanguageID == editor.LanguageID &&
m_pluginEditor[i].Plugin == editor.Plugin)
return m_paths[i];
return "";
}

void CodeEditorUI::CloseAll(PipelineItem* item)
{
for (int i = 0; i < m_items.size(); i++) {
Expand Down Expand Up @@ -1456,9 +1479,14 @@ namespace ed {

void CodeEditorUI::StopDebugging()
{
for (auto& ed : m_editor) {
if (ed)
ed->SetCurrentLineIndicator(-1);
for (int i = 0; i < m_editor.size(); i++) {
if (m_editor[i])
m_editor[i]->SetCurrentLineIndicator(-1);
else {
if (m_pluginEditor[i].Plugin->GetVersion() >= 3)
((IPlugin3*)m_pluginEditor[i].Plugin)->ShaderEditor_SetLineIndicator(m_pluginEditor[i].LanguageID,
m_pluginEditor[i].ID, -1);
}
}
}
void CodeEditorUI::StopThreads()
Expand Down
39 changes: 21 additions & 18 deletions src/SHADERed/UI/CodeEditorUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ namespace ed {
std::string Code;
};

struct PluginShaderEditor {
PluginShaderEditor()
{
Plugin = nullptr;
LanguageID = -1;
ID = -1;
}
PluginShaderEditor(IPlugin1* pl, int langID, int id)
{
Plugin = pl;
LanguageID = langID;
ID = id;
}
IPlugin1* Plugin;
int LanguageID;
int ID;
};

class CodeEditorUI : public UIView {
public:
CodeEditorUI(GUIManager* ui, ed::InterfaceManager* objects, const std::string& name = "", bool visible = false);
Expand Down Expand Up @@ -50,6 +68,9 @@ namespace ed {
void OpenPluginCode(PipelineItem* item, const char* filepath, int id);
TextEditor* Get(PipelineItem* item, ed::ShaderStage stage);
TextEditor* Get(const std::string& path);
PluginShaderEditor GetPluginEditor(PipelineItem* item, ed::ShaderStage stage);
PluginShaderEditor GetPluginEditor(const std::string& path);
std::string GetPluginEditorPath(const PluginShaderEditor& editor);


void SetTheme(const TextEditor::Palette& colors);
Expand Down Expand Up @@ -98,24 +119,6 @@ namespace ed {

std::vector<CodeSnippet> m_snippets;

struct PluginShaderEditor {
PluginShaderEditor()
{
Plugin = nullptr;
LanguageID = -1;
ID = -1;
}
PluginShaderEditor(IPlugin1* pl, int langID, int id)
{
Plugin = pl;
LanguageID = langID;
ID = id;
}
IPlugin1* Plugin;
int LanguageID;
int ID;
};

// code editor font
ImFont* m_font;

Expand Down
5 changes: 4 additions & 1 deletion src/SHADERed/UI/PipelineUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,12 @@ namespace ed {
codeUI->StopDebugging();
codeUI->Open(m_modalItem, ShaderStage::Compute);
TextEditor* editor = codeUI->Get(m_modalItem, ShaderStage::Compute);
PluginShaderEditor pluginEditor;
if (editor == nullptr)
pluginEditor = codeUI->GetPluginEditor(m_modalItem, ShaderStage::Compute);

m_data->Debugger.PrepareComputeShader(m_modalItem, m_thread[0], m_thread[1], m_thread[2]);
((PixelInspectUI*)m_ui->Get(ViewID::PixelInspect))->StartDebugging(editor, nullptr);
((PixelInspectUI*)m_ui->Get(ViewID::PixelInspect))->StartDebugging(editor, pluginEditor, nullptr);

m_closePopup();
}
Expand Down
Loading

0 comments on commit 659d7d3

Please sign in to comment.