Skip to content

Commit

Permalink
Qt: Shader editor now works
Browse files Browse the repository at this point in the history
  • Loading branch information
wheremyfoodat committed Jul 14, 2024
1 parent d874778 commit ae2c060
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 24 deletions.
5 changes: 2 additions & 3 deletions include/panda_qt/shader_editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ class ShaderEditorWindow : public QDialog {
Zep::IZepReplProvider replProvider;
static constexpr float fontSize = 14.0f;

public:
// Whether this backend supports shader editor
bool shaderEditorSupported = true;
bool supported = true;

public:
ShaderEditorWindow(QWidget* parent, const std::string& filename, const std::string& initialText);
void setText(const std::string& text) { zepWidget.GetEditor().GetMRUBuffer()->SetText(text); }

void setEnable(bool enable);
};
1 change: 1 addition & 0 deletions include/renderer_gl/renderer_gl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class RendererGL final : public Renderer {
// Note: The caller is responsible for deleting the currently bound FBO before calling this
void setFBO(uint handle) { screenFramebuffer.m_handle = handle; }
void resetStateManager() { gl.reset(); }
void initUbershader(OpenGL::Program& program);

#ifdef PANDA3DS_FRONTEND_QT
virtual void initGraphicsContext([[maybe_unused]] GL::Context* context) override { initGraphicsContextInternal(); }
Expand Down
59 changes: 39 additions & 20 deletions src/core/renderer_gl/renderer_gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,7 @@ void RendererGL::initGraphicsContextInternal() {
OpenGL::Shader vert({vertexShaderSource.begin(), vertexShaderSource.size()}, OpenGL::Vertex);
OpenGL::Shader frag({fragmentShaderSource.begin(), fragmentShaderSource.size()}, OpenGL::Fragment);
triangleProgram.create({vert, frag});
gl.useProgram(triangleProgram);

textureEnvSourceLoc = OpenGL::uniformLocation(triangleProgram, "u_textureEnvSource");
textureEnvOperandLoc = OpenGL::uniformLocation(triangleProgram, "u_textureEnvOperand");
textureEnvCombinerLoc = OpenGL::uniformLocation(triangleProgram, "u_textureEnvCombiner");
textureEnvColorLoc = OpenGL::uniformLocation(triangleProgram, "u_textureEnvColor");
textureEnvScaleLoc = OpenGL::uniformLocation(triangleProgram, "u_textureEnvScale");

depthScaleLoc = OpenGL::uniformLocation(triangleProgram, "u_depthScale");
depthOffsetLoc = OpenGL::uniformLocation(triangleProgram, "u_depthOffset");
depthmapEnableLoc = OpenGL::uniformLocation(triangleProgram, "u_depthmapEnable");
picaRegLoc = OpenGL::uniformLocation(triangleProgram, "u_picaRegs");

// Init sampler objects. Texture 0 goes in texture unit 0, texture 1 in TU 1, texture 2 in TU 2, and the light maps go in TU 3
glUniform1i(OpenGL::uniformLocation(triangleProgram, "u_tex0"), 0);
glUniform1i(OpenGL::uniformLocation(triangleProgram, "u_tex1"), 1);
glUniform1i(OpenGL::uniformLocation(triangleProgram, "u_tex2"), 2);
glUniform1i(OpenGL::uniformLocation(triangleProgram, "u_tex_lighting_lut"), 3);
initUbershader(triangleProgram);

auto displayVertexShaderSource = gl_resources.open("opengl_display.vert");
auto displayFragmentShaderSource = gl_resources.open("opengl_display.frag");
Expand Down Expand Up @@ -814,5 +797,41 @@ void RendererGL::deinitGraphicsContext() {
printf("RendererGL::DeinitGraphicsContext called\n");
}

std::string RendererGL::getUbershader() { return ""; }
void RendererGL::setUbershader(const std::string& shader) {}
std::string RendererGL::getUbershader() {
auto gl_resources = cmrc::RendererGL::get_filesystem();
auto fragmentShader = gl_resources.open("opengl_fragment_shader.frag");

return std::string(fragmentShader.begin(), fragmentShader.end());
}

void RendererGL::setUbershader(const std::string& shader) {
auto gl_resources = cmrc::RendererGL::get_filesystem();
auto vertexShaderSource = gl_resources.open("opengl_vertex_shader.vert");

OpenGL::Shader vert({vertexShaderSource.begin(), vertexShaderSource.size()}, OpenGL::Vertex);
OpenGL::Shader frag(shader, OpenGL::Fragment);
triangleProgram.create({vert, frag});

initUbershader(triangleProgram);
}

void RendererGL::initUbershader(OpenGL::Program& program) {
gl.useProgram(program);

textureEnvSourceLoc = OpenGL::uniformLocation(program, "u_textureEnvSource");
textureEnvOperandLoc = OpenGL::uniformLocation(program, "u_textureEnvOperand");
textureEnvCombinerLoc = OpenGL::uniformLocation(program, "u_textureEnvCombiner");
textureEnvColorLoc = OpenGL::uniformLocation(program, "u_textureEnvColor");
textureEnvScaleLoc = OpenGL::uniformLocation(program, "u_textureEnvScale");

depthScaleLoc = OpenGL::uniformLocation(program, "u_depthScale");
depthOffsetLoc = OpenGL::uniformLocation(program, "u_depthOffset");
depthmapEnableLoc = OpenGL::uniformLocation(program, "u_depthmapEnable");
picaRegLoc = OpenGL::uniformLocation(program, "u_picaRegs");

// Init sampler objects. Texture 0 goes in texture unit 0, texture 1 in TU 1, texture 2 in TU 2, and the light maps go in TU 3
glUniform1i(OpenGL::uniformLocation(program, "u_tex0"), 0);
glUniform1i(OpenGL::uniformLocation(program, "u_tex1"), 1);
glUniform1i(OpenGL::uniformLocation(program, "u_tex2"), 2);
glUniform1i(OpenGL::uniformLocation(program, "u_tex_lighting_lut"), 3);
}
9 changes: 9 additions & 0 deletions src/panda_qt/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent)
patchWindow = new PatchWindow(this);
luaEditor = new TextEditorWindow(this, "script.lua", "");
shaderEditor = new ShaderEditorWindow(this, "shader.glsl", "");

shaderEditor->setEnable(emu->getRenderer()->supportsShaderReload());
if (shaderEditor->supported) {
shaderEditor->setText(emu->getRenderer()->getUbershader());
}

auto args = QCoreApplication::arguments();
if (args.size() > 1) {
Expand Down Expand Up @@ -351,6 +355,11 @@ void MainWindow::dispatchMessage(const EmulatorMessage& message) {
emu->getServiceManager().getHID().setTouchScreenPress(message.touchscreen.x, message.touchscreen.y);
break;
case MessageType::ReleaseTouchscreen: emu->getServiceManager().getHID().releaseTouchScreen(); break;

case MessageType::ReloadUbershader:
emu->getRenderer()->setUbershader(*message.string.str);
delete message.string.str;
break;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/panda_qt/shader_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ ShaderEditorWindow::ShaderEditorWindow(QWidget* parent, const std::string& filen
}

void ShaderEditorWindow::setEnable(bool enable) {
shaderEditorSupported = enable;
supported = enable;

if (enable) {
setDisabled(false);
Expand Down

0 comments on commit ae2c060

Please sign in to comment.