-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader.h
139 lines (96 loc) · 3.54 KB
/
shader.h
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
/*
This file is part of OpenGL-StarterPack (GLSP), an open source OpenGL based framework
that facilitates and speeds up demo and project creation by offering an abstraction to
the basic objects of OpenGL as well as incluing the necessary libraries.
MIT License
Copyright (c) 2024 Antonio Espinosa Garcia
*/
#ifndef __SHADER__
#define __SHADER__
#include <unordered_map>
#include <sstream>
#include <fstream>
#include <GLSP/core.h>
GLSP_NAMESPACE_BEGIN
/*
Struct that contains all graphic pipeline stages sources for compiling a shader.
Compute source is not included due to the fact that it is a stage independent to the graphic pipeline
(Check ComputeShader class).
*/
struct ShaderStageSource
{
std::string vertexBit;
std::string fragmentBit;
std::string geometryBit;
std::string tesselationCtrlBit;
std::string tesselationEvalBit;
};
class Shader
{
protected:
unsigned int m_ID; // PROGRAM ID
ShaderType m_type;
std::unordered_map<const char *, int> m_uniformLocationCache; // Legacy uniform pipeline
std::unordered_map<const char *, int> m_uniformBlockCache; // Unifrom buffer pipeline
virtual unsigned int get_uniform_location(const char *name);
virtual unsigned int get_uniform_block(const char *name);
virtual unsigned int compile(unsigned int type, const char *source);
virtual unsigned int create_program(ShaderStageSource source);
Shader(ShaderType t): m_type(t){} //Utility constructor for inheritance
public:
/*
Admitted files: glsl. This kind of file is custom made, it has the benefit of containing all stages in one single file
*/
Shader(const char *filename, ShaderType t);
Shader(ShaderStageSource src, ShaderType t);
~Shader() {}
void bind() const;
void unbind() const;
inline ShaderType get_type() { return m_type; }
#pragma region LEGACY UNIFORM PIPELINE
void set_bool(const char *name, bool value) const;
void set_int(const char *name, int value) const;
void set_float(const char *name, float value);
void set_mat4(const char *name, glm::mat4 value);
void set_vec2(const char *name, glm::vec2 value);
void set_vec3(const char *name, glm::vec3 value);
void set_vec4(const char *name, glm::vec4 value);
#pragma endregion
#pragma region UBO PIPELINE
void set_uniform_block(const char *name, unsigned int id);
#pragma endregion
/*
Useful if not using .glsl files. It compiles the .frag, .vert etc files and outputs a string ready to be attached to a ShaderStageSource for creating a shader.
*/
static std::string parse_shader_stage(const char *filename);
/*
Parse .glsl file.
*/
static ShaderStageSource parse_shader(const char *filename);
};
#pragma region COMPUTE SHADER
/*
GPGPU dedicated shader. This functionality is not related to the graphic pipeline by any means.
*/
class ComputeShader : public Shader
{
private:
unsigned int compile(unsigned int type, const char *source);
unsigned int create_program(const std::string &src);
public:
ComputeShader(const char *filename);
/*
Launch the shader kernel in the GPU
*/
void dispatch(Extent3D workGroupExtent, bool autoBarrier = true, unsigned int barrierType =GL_ALL_BARRIER_BITS) const;
static void set_barrier(unsigned int barrierType = GL_ALL_BARRIER_BITS );
/*
GPU limitation query functions
*/
static Extent3D query_max_workgroup_extent();
static Extent3D query_max_workgroup_number();
static int query_max_invocations();
};
#pragma endregion
GLSP_NAMESPACE_END
#endif