summaryrefslogtreecommitdiff
path: root/src/shader.h
blob: ae02887d0fa8ba664e454263b7e936b7222d15b6 (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
#ifndef _SHADER_H
#define _SHADER_H

#include <array>
#include <sstream>
#include <algorithm>

#include "texture.h"

#define MAX_TEXTURES 8

namespace Mauri
{

struct Uniform
{
    Uniform() { this->value = &this->default_value; }

    std::string name;
    std::string uname;
    std::string precision = "";

    UniformType type;

    vec4 default_value = vec4(0.f);
    vec4 *value;
};

struct Combo
{
    Combo(const std::string &name, s32 value) : name(name), value(value)
    {
        std::transform(this->name.begin(), this->name.end(), this->name.begin(), ::toupper);
    }
    std::string name;
    s32 value;
    auto to_string() const -> const std::string;
};

struct Variable
{
    std::string name;
    UniformType type;
    auto to_string() const -> const std::string;
};

class Engine;
class Object;

class Shader
{
  public:
    Shader(const std::string &name);
    Shader(Shader *origin);
    ~Shader();

    std::string name;

    std::vector<Combo> combos;
    std::vector<Uniform *> uniforms;

    std::array<std::string, MAX_TEXTURES> textures = {};
    std::array<std::string, MAX_TEXTURES> texture_combos = {};

    u32 texture_count = 0;

    std::stringstream vs_source;
    std::stringstream fs_source;

    std::vector<Variable> variables;

    auto origin() -> Shader * { return this->origin_ ? this->origin_ : this; }

    auto compile(const std::vector<Combo> &combo_overrides) -> RenderShader *;

    auto bind(RenderShader *shader, Engine *engine, Object *object, mat4x4 *model,
        const std::array<Texture *, MAX_TEXTURES> *textures) -> void;

private:
    Shader *origin_ = nullptr;

    auto build_shader_source(const std::string_view &isource, std::stringstream &out) -> void;
};

extern auto get_shader(const std::string &name) -> Shader *;
extern auto unload_shaders() -> void;

} // namespace Mauri

#endif // _SHADER_H