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

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

namespace amalgine
{
    class VertexShaderTooManyAttributes : public std::runtime_error
    {
    public:
        VertexShaderTooManyAttributes(i32 maxVertexAttributes);
    };
    
    class VertexShaderAttributeAlreadyDefined : public std::runtime_error
    {
    public:
        VertexShaderAttributeAlreadyDefined(const std::string &attributeName);
    };
    
    class VertexShaderInvalidAttributeName : public std::runtime_error
    {
    public:
        VertexShaderInvalidAttributeName(const std::string &attributeName);
    };
    
    class VertexShaderFunctionAlreadyDefined : public std::runtime_error
    {
    public:
        VertexShaderFunctionAlreadyDefined(const std::string &funcName);
    };
    
    using VertexShaderMainFunc = std::function<ShaderVec4()>;
    
    /*
     * Using the same VertexShader instance in multiple threads to define data is not thread safe.
     * All get*** functions are thread safe.
     */
    class CompiledVertexShader;
    
    class VertexShader
    {
        DISABLE_COPY(VertexShader)
        friend class ShaderProgram;
    public:
        VertexShader();
        
        const std::string& getInputAttributeName(i32 attributeIndex);
        ShaderInputVec2 defineInputVec2(const std::string &name);
        void defineMain(VertexShaderMainFunc mainFunc);
        Result<CompiledVertexShader*> compile();
    private:
        void writeHeader(const std::string &code);
        void writeBody(const std::string &code);
        std::string build() const;
        
        /*
         * Throws VertexShaderTooManyInputAttributes if too many vertex attributes are defined for the platform.
         * Throws VertexShaderAttributeAlreadyDefined if a vertex attribute with the same name has already been defined.
         */
        i32 defineInputVariable(const std::string &variableName, const char *typeName);
    private:
        std::string header;
        std::string body;
        int locationCounter;
        i32 maxVertexAttribs; // Could make this static
        std::unordered_map<std::string, i32> vertexAttributes;
        std::vector<std::string> vertexAttributeNames;
        bool mainFuncDefined;
    };
}