summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-10-31 20:19:31 +0800
committerchai <chaifix@163.com>2021-10-31 20:19:31 +0800
commitfb67e0d20264e615545e3af2b9fa8e18eb50526e (patch)
tree236f3cdad56ccfa41f04f4b7443cbbd037ed6b48
parent9e0ad4279c8ccf2f419694e4ca15a1372e8b20d6 (diff)
* shader compiler
-rw-r--r--Data/Resources/Shaders/Editor-Text.glsl8
-rw-r--r--Runtime/Graphics/GfxDevice.cpp5
-rw-r--r--Runtime/Graphics/RenderCommands.h2
-rw-r--r--Runtime/Graphics/Shader.cpp13
-rw-r--r--Runtime/Graphics/Shader.h2
-rw-r--r--Runtime/Graphics/ShaderCompiler.cpp25
6 files changed, 36 insertions, 19 deletions
diff --git a/Data/Resources/Shaders/Editor-Text.glsl b/Data/Resources/Shaders/Editor-Text.glsl
index f589e07..e20d36b 100644
--- a/Data/Resources/Shaders/Editor-Text.glsl
+++ b/Data/Resources/Shaders/Editor-Text.glsl
@@ -1,11 +1,9 @@
#version 330 core
CMD_BEGIN
-
-Cull Both
+Cull Off
Blend SrcAlpha OneMinusSrcAlpha
DepthTest Off
-
CMD_END
uniform mat4 gamelab_mat_mvp;
@@ -24,7 +22,6 @@ void main()
gl_Position = clip;
uv = vUV;
}
-
VSH_END
FSH_BEGIN
@@ -38,5 +35,4 @@ void main()
vec4 sampled = vec4(1,1,1,texture(uiTex, uv).r);
FragColor = sampled;
}
-
-FSH_END
+FSH_END \ No newline at end of file
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);