aboutsummaryrefslogtreecommitdiff
path: root/src/RenderBackend/OpenGL/Shader.cpp
blob: fad1702a23e4acf9d7b3a7158a7b39e8db291bc1 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "../../../include/RenderBackend/OpenGL/Shader.hpp"
#include "../../../include/RenderBackend/OpenGL/opengl.hpp"
#include "../../../include/RenderBackend/OpenGL/CompiledShader.hpp"
#include <cassert>

using namespace std;

namespace amalgine {
    ShaderTooManyAttributes::ShaderTooManyAttributes(i32 maxAttributes) : 
        runtime_error(string("Attempting to define more than ") + to_string(maxAttributes) + " attributes")
    {
        
    }
    
    ShaderAttributeAlreadyDefined::ShaderAttributeAlreadyDefined(const std::string &attributeName) : 
        runtime_error(string("An attribute with the name ") + attributeName + " has already been defined")
    {
        
    }
    
    ShaderInvalidAttributeName::ShaderInvalidAttributeName(const std::string &attributeName) : 
        runtime_error(string("Attribute name ") + attributeName + " is invalid")
    {
        
    }
    
    ShaderFunctionAlreadyDefined::ShaderFunctionAlreadyDefined(const std::string &funcName) : 
        runtime_error(string("Shader function already defined: ") + funcName)
    {
        
    }
    
    Shader::Shader() : inputLocationCounter(0), outputLocationCounter(0), mainFuncDefined(false)
    {
        writeHeader("#version 330 core\n\n");
    }
    
    const string& Shader::getOutputAttributeName(i32 attributeIndex)
    {
        assert(attributeIndex < (i32)outputAttributeNames.size());
        return outputAttributeNames[attributeIndex].name;
    }

    const string& Shader::getInputAttributeName(i32 attributeIndex) const
    {
        assert(attributeIndex < (i32)inputAttributeNames.size());
        return inputAttributeNames[attributeIndex].name;
    }
    
    AttributeType Shader::getInputAttributeType(i32 attributeIndex) const
    {
        assert(attributeIndex < (i32)inputAttributeNames.size());
        return getAttributeTypeByName(inputAttributeNames[attributeIndex].typeName);
    }
    
    ShaderOutputVec4 Shader::defineOutputVec4(const std::string &name)
    {
        i32 attributeIndex = defineOutputVariable(name, "vec4");
        return ShaderOutputVec4(this, attributeIndex);
    }
    
    // TODO: Generate warning if global variable is defined but not assigned to?
    ShaderGlobalVec3 Shader::defineGlobalVec3(const std::string &name)
    {
        if(!isShaderVariableNameValid(name.c_str()))
            throw ShaderInvalidAttributeName(name);
        
        if(uniforms.find(name) != uniforms.end())
            throw ShaderAttributeAlreadyDefined(name);
        
        writeHeader("uniform vec3 ");
        writeHeader(name);
        writeHeader(";\n");
        
        ShaderGlobalVec3 globalVec(name);
        uniforms[name] = globalVec.getVecObject();
        return ShaderGlobalVec3(globalVec);
    }
    
    i32 Shader::defineOutputVariable(const string &variableName, const char *typeName)
    {
        if(!isShaderVariableNameValid(variableName.c_str()))
            throw ShaderInvalidAttributeName(variableName);
        
        if(outputLocationCounter + 1 > maxAttribs)
            throw ShaderTooManyAttributes(maxAttribs);
        
        if(outputAttributes.find(variableName) != outputAttributes.end())
            throw ShaderAttributeAlreadyDefined(variableName);
        
        i32 attributeIndex = outputLocationCounter;
        outputAttributes[variableName] = outputLocationCounter;
        writeHeader("out ");
        writeHeader(typeName);
        writeHeader(" ");
        writeHeader(variableName);
        writeHeader(";\n");
        
        ShaderAttribute shaderAttribute;
        shaderAttribute.name = variableName;
        shaderAttribute.typeName = typeName;
        outputAttributeNames.push_back(shaderAttribute);
        ++outputLocationCounter;
        return attributeIndex;
    }

    ShaderInputVec2 Shader::defineInputVec2(const std::string &name)
    {
        i32 attributeIndex = defineInputVariable(name, "vec2");
        return ShaderInputVec2(this, attributeIndex);
    }

    i32 Shader::defineInputVariable(const string &variableName, const char *typeName)
    {
        if(!isShaderVariableNameValid(variableName.c_str()))
            throw ShaderInvalidAttributeName(variableName);
        
        if(inputLocationCounter + 1 > maxAttribs)
            throw ShaderTooManyAttributes(maxAttribs);
        
        if(inputAttributes.find(variableName) != inputAttributes.end())
            throw ShaderAttributeAlreadyDefined(variableName);
        
        i32 attributeIndex = inputLocationCounter;
        inputAttributes[variableName] = inputLocationCounter;
        ShaderAttribute shaderAttribute;
        shaderAttribute.name = variableName;
        shaderAttribute.typeName = typeName;
        inputAttributeNames.push_back(shaderAttribute);
        
        writeHeader("layout(location = ");
        writeHeader(to_string(inputLocationCounter));
        ++inputLocationCounter;
        writeHeader(") in ");
        writeHeader(typeName);
        writeHeader(" ");
        writeHeader(variableName);
        writeHeader(";\n");
        return attributeIndex;
    }
    
    void Shader::assign(const ShaderOutputVec4 &lhsVariable, const ShaderVec4 &rhsVariable)
    {
        writeBody(lhsVariable.getName());
        writeBody(" = ");
        writeBody(rhsVariable.getOutput());
        writeBody(";\n");
    }
    
    string Shader::build() const
    {
        std::string result;
        result.reserve(header.size() + 2 + body.size());
        result += header;
        result += "\n";
        result += body;
        return result;
    }
    
    void Shader::writeHeader(const string &code)
    {
        header += code;
    }
    
    void Shader::writeBody(const string &code)
    {
        body += code;
    }
}