summaryrefslogtreecommitdiff
path: root/Runtime/Graphics/ShaderCompiler.cpp
blob: b3db629f74ee8d17e0b30190fb96dfdbe8e56dc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "ShaderCompiler.h"

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;
}