From fb67e0d20264e615545e3af2b9fa8e18eb50526e Mon Sep 17 00:00:00 2001 From: chai Date: Sun, 31 Oct 2021 20:19:31 +0800 Subject: * shader compiler --- Runtime/Graphics/GfxDevice.cpp | 5 ++--- Runtime/Graphics/RenderCommands.h | 2 +- Runtime/Graphics/Shader.cpp | 13 ++++++++++--- Runtime/Graphics/Shader.h | 2 ++ Runtime/Graphics/ShaderCompiler.cpp | 25 +++++++++++++++++++------ 5 files changed, 34 insertions(+), 13 deletions(-) (limited to 'Runtime/Graphics') diff --git a/Runtime/Graphics/GfxDevice.cpp b/Runtime/Graphics/GfxDevice.cpp index f318487..9abea12 100644 --- a/Runtime/Graphics/GfxDevice.cpp +++ b/Runtime/Graphics/GfxDevice.cpp @@ -78,11 +78,10 @@ void GfxDevice::UseShader(LuaBind::State& state, Shader* shader, int idx) glUseProgram(id); + shader->ExecuteCommand(); + m_Shader.shader = shader; m_Shader.ref.SetRef(state, idx); - - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } void GfxDevice::UnuseShader() diff --git a/Runtime/Graphics/RenderCommands.h b/Runtime/Graphics/RenderCommands.h index 8dc2dbe..123e6f0 100644 --- a/Runtime/Graphics/RenderCommands.h +++ b/Runtime/Graphics/RenderCommands.h @@ -105,7 +105,7 @@ struct Cmd_Blend : RenderCommand case EBlend::Blend_Constant_Alpha: dst = GL_CONSTANT_ALPHA; break; case EBlend::Blend_One_Minus_Constant_Alpha: dst = GL_ONE_MINUS_CONSTANT_ALPHA; break; } - glBlendFunc(srcFac, dstFac); + glBlendFunc(src, dst); } }; diff --git a/Runtime/Graphics/Shader.cpp b/Runtime/Graphics/Shader.cpp index ca5a572..4b4c9e0 100644 --- a/Runtime/Graphics/Shader.cpp +++ b/Runtime/Graphics/Shader.cpp @@ -51,14 +51,13 @@ Shader::Shader(LuaBind::VM*vm, std::string& glslShader) // stl的string会在大小超过阈值的情况下在栈里分配,并用RAII保证释放 std::string vsh ; std::string fsh ; - RenderCommandGroup cmd; try { - GLSLCompiler::Compile(glslShader, vsh, fsh, cmd); + GLSLCompiler::Compile(glslShader, vsh, fsh, m_Commands); } catch (GLSLCompileException& e) { - ReleaseRenderCommandGroup(cmd); + ReleaseRenderCommandGroup(m_Commands); throw ShaderCompileExecption(e.what()); } CompileProgram(vsh.c_str(), fsh.c_str()); @@ -151,4 +150,12 @@ void Shader::ReCompileFrag(std::string frag) bool Shader::IsValid() { return m_ProgramID != 0; +} + +void Shader::ExecuteCommand() +{ + for (int i = 0; i < m_Commands.size(); ++i) + { + m_Commands[i]->Execute(); + } } \ No newline at end of file diff --git a/Runtime/Graphics/Shader.h b/Runtime/Graphics/Shader.h index 095d9fe..6b2b1bc 100644 --- a/Runtime/Graphics/Shader.h +++ b/Runtime/Graphics/Shader.h @@ -24,6 +24,8 @@ public: bool IsValid(); + void ExecuteCommand(); + GET(GLint, ID, m_ProgramID); private: diff --git a/Runtime/Graphics/ShaderCompiler.cpp b/Runtime/Graphics/ShaderCompiler.cpp index 637fd59..a3dcf50 100644 --- a/Runtime/Graphics/ShaderCompiler.cpp +++ b/Runtime/Graphics/ShaderCompiler.cpp @@ -32,14 +32,23 @@ if(pos == string::npos || !IsLabelActive(src, label)) {\ CheckLabel(FSH_BEGIN); CheckLabel(FSH_END); - bool hasCmd = IsLabelActive(src, CMD_BEGIN) && IsLabelActive(src, CMD_END); vsh = GetContent(src, VSH_BEGIN, VSH_END); fsh = GetContent(src, FSH_BEGIN, FSH_END); - string cmd = GetContent(src, CMD_BEGIN, CMD_END); - if (cmd.size() > 0) - ParseCmd(cmd, group); + bool hasCmd = IsLabelActive(src, CMD_BEGIN) && IsLabelActive(src, CMD_END); + if (hasCmd) + { + string cmd = GetContent(src, CMD_BEGIN, CMD_END); + if (cmd.size() > 0) + { + ParseCmd(cmd, group); + } + else + { + hasCmd = false; + } + } string common; common = TrimContent(src, VSH_BEGIN, VSH_END); @@ -55,6 +64,10 @@ std::string GLSLCompiler::GetContent(std::string& src, const char* from, const c { int begin = src.find(from); int end = src.find(to); + if (begin == string::npos || end == string::npos) + { + return ""; + } std::string content = src.substr(begin + strlen(from), end - begin - strlen(from)); return content; } @@ -132,7 +145,7 @@ void GLSLCompiler::ParseCmd(std::string& cmds, RenderCommandGroup& group) } } -#define IsSeperator(c) (c == ' ' || c == '\r' || c == '\n' || c == '\9') +#define IsSeperator(c) (c == ' ' || c == '\r' || c == '\n' || c == /*tab*/9) // 找到行内的第一个单词,作为命令名 bool GLSLCompiler::FindCmdPos(std::string& line, int* start, int* end) @@ -291,7 +304,7 @@ void GLSLCompiler::CommandDepthWrite(std::string& p, RenderCommandGroup& group) else { delete pwrite; - s_CompileError = string("Compile Shader Error: Invalid parameter of Cmd_DepthWrite: " + params[0]); + s_CompileError = string("Compile Shader Error: Invalid parameter of DepthWrite: " + params[0]); throw GLSLCompileException(s_CompileError.c_str()); } group.push_back(pwrite); -- cgit v1.1-26-g67d0