summaryrefslogtreecommitdiff
path: root/Runtime/Graphics/ShaderCompiler.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Runtime/Graphics/ShaderCompiler.cpp')
-rw-r--r--Runtime/Graphics/ShaderCompiler.cpp35
1 files changed, 34 insertions, 1 deletions
diff --git a/Runtime/Graphics/ShaderCompiler.cpp b/Runtime/Graphics/ShaderCompiler.cpp
index 66413e9..b3db629 100644
--- a/Runtime/Graphics/ShaderCompiler.cpp
+++ b/Runtime/Graphics/ShaderCompiler.cpp
@@ -1,7 +1,40 @@
#include "ShaderCompiler.h"
-void GLSLCompiler::Compile(std::string& src, std::string& vsh, std::string& fsh)
+using namespace std;
+
+const char* VSH_BEGIN = "VSH_BEGIN";
+const char* VSH_END = "VSH_END";
+const char* FSH_BEGIN = "FSH_BEGIN";
+const char* FSH_END = "FSH_END";
+
+// GLSL分为三部分
+// VERTEX_SHADER_BEGIN 和 VERTEX_SHADER_END之间的顶点着色器
+// FRAGMENT_SHADER_BEGIN 和 FRAGMENT_SHADER_END之间的片段着色器
+// 两者之外的公共部分
+void GLSLCompiler::Compile(std::string& src, std::string& vsh, std::string& fsh)/*throw GLSLCompileException*/
{
+ int vsh_begin = src.find(VSH_BEGIN);
+ if (vsh_begin == string::npos)
+ throw GLSLCompileException("Compile Shader Error: No VSH_BEGIN label");
+ int vsh_end = src.find(VSH_END);
+ if (vsh_end == string::npos)
+ throw GLSLCompileException("Compile Shader Error: No VSH_END label");
+ int fsh_begin = src.find(FSH_BEGIN);
+ if (fsh_begin == string::npos)
+ throw GLSLCompileException("Compile Shader Error: No FSH_BEGIN label");
+ int fsh_end = src.find(FSH_END);
+ if (fsh_end == string::npos)
+ throw GLSLCompileException("Compile Shader Error: No FSH_END label");
+
+ vsh = src.substr(vsh_begin + strlen(VSH_BEGIN), vsh_end - vsh_begin - strlen(VSH_BEGIN));
+ fsh = src.substr(fsh_begin + strlen(FSH_BEGIN), fsh_end - fsh_begin - strlen(FSH_BEGIN));
+ string common;
+ common = src.erase(vsh_begin, vsh_end + strlen(VSH_END) - vsh_begin);
+ int fsh_begin2 = common.find(FSH_BEGIN);
+ int fsh_end2 = common.find(FSH_END);
+ common = common.erase(fsh_begin2, fsh_end2 + strlen(FSH_END) - fsh_begin2);
+ vsh = common + vsh;
+ fsh = common + fsh;
}