aboutsummaryrefslogtreecommitdiff
path: root/include/RenderBackend/OpenGL/Shader.hpp
blob: 7aecc4ede9643d3f6ad65f0fa96f5e69ac42e07f (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
#pragma once

#include "../../DataView.hpp"
#include "../../utils.hpp"
#include "../../Result.hpp"
#include "CommonShader.hpp"
#include "ShaderVec.hpp"
#include <string>
#include <unordered_map>
#include <vector>
#include <stdexcept>
#include <functional>

namespace amalgine {
    class ShaderTooManyAttributes : public std::runtime_error
    {
    public:
        ShaderTooManyAttributes(i32 maxAttributes);
    };
    
    class ShaderAttributeAlreadyDefined : public std::runtime_error
    {
    public:
        ShaderAttributeAlreadyDefined(const std::string &attributeName);
    };
    
    class ShaderInvalidAttributeName : public std::runtime_error
    {
    public:
        ShaderInvalidAttributeName(const std::string &attributeName);
    };
    
    class ShaderFunctionAlreadyDefined : public std::runtime_error
    {
    public:
        ShaderFunctionAlreadyDefined(const std::string &funcName);
    };
    
    class Shader
    {
        DISABLE_COPY(Shader)
        friend class ShaderProgram;
    public:
        Shader();
        
        const std::string& getOutputAttributeName(i32 attributeIndex);
        const std::string& getInputAttributeName(i32 attributeIndex) const;
        AttributeType getInputAttributeType(i32 attributeIndex) const;
        ShaderOutputVec4 defineOutputVec4(const std::string &name);
        ShaderGlobalVec3 defineGlobalVec3(const std::string &name);
        ShaderInputVec2 defineInputVec2(const std::string &name);
        i32 defineInputVariable(const std::string &variableName, const char *typeName);
        
        void assign(const ShaderOutputVec4 &lhsVariable, const ShaderVec4 &rhsVariable);
    protected:
        void writeHeader(const std::string &code);
        void writeBody(const std::string &code);
        std::string build() const;
        
        /*
         * Throws ShaderTooManyAttributes if too many attributes are defined for the platform.
         * Throws ShaderAttributeAlreadyDefined if a attribute with the same name has already been defined.
         */
        i32 defineOutputVariable(const std::string &variableName, const char *typeName);
    protected:
        std::string header;
        std::string body;
        int inputLocationCounter;
        int outputLocationCounter;
        // TOOD: Verify if this is correct. This same variable is used for both output and input variables
        i32 maxAttribs; // Could make this static
        std::unordered_map<std::string, i32> inputAttributes;
        std::vector<ShaderAttribute> inputAttributeNames;
        std::unordered_map<std::string, i32> outputAttributes;
        std::vector<ShaderAttribute> outputAttributeNames;
        std::unordered_map<std::string, ShaderGlobalVec> uniforms;
        bool mainFuncDefined;
    };
}