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

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

namespace amalgine
{
    class PixelShaderTooManyAttributes : public std::runtime_error
    {
    public:
        PixelShaderTooManyAttributes(i32 maxPixelAttributes);
    };
    
    class PixelShaderAttributeAlreadyDefined : public std::runtime_error
    {
    public:
        PixelShaderAttributeAlreadyDefined(const std::string &attributeName);
    };
    
    class PixelShaderInvalidAttributeName : public std::runtime_error
    {
    public:
        PixelShaderInvalidAttributeName(const std::string &attributeName);
    };
    
    class PixelShaderFunctionAlreadyDefined : public std::runtime_error
    {
    public:
        PixelShaderFunctionAlreadyDefined(const std::string &funcName);
    };
    
    using PixelShaderMainFunc = std::function<void()>;
    
    /*
     * Using the same PixelShader instance in multiple threads to define data is not thread safe.
     * All get*** functions are thread safe.
     */
    class PixelShader
    {
    public:
        PixelShader();
        
        const std::string& getOutputAttributeName(i32 attributeIndex);
        ShaderOutputVec4 defineOutputVec4(const std::string &name);
        void defineMain(PixelShaderMainFunc mainFunc);
        void assign(const ShaderOutputVec4 &lhsVariable, const ShaderVec4 &rhsVariable);
        std::string build();
    private:
        void writeHeader(const std::string &code);
        void writeBody(const std::string &code);
        
        /*
         * Throws PixelShaderTooManyAttributes if too many pixel attributes are defined for the platform.
         * Throws PixelShaderAttributeAlreadyDefined if a pixel attribute with the same name has already been defined.
         */
        i32 defineOutputVariable(const std::string &variableName, const char *typeName);
    private:
        std::string header;
        std::string body;
        int locationCounter;
        i32 maxPixelAttribs; // Could make this static
        std::unordered_map<std::string, i32> pixelAttributes;
        std::vector<std::string> pixelAttributeNames;
        bool mainFuncDefined;
    };
}