Overte C++ Documentation
GLShaders.h
1 //
2 // Created by Bradley Austin Davis 2016/09/27
3 // Copyright 2014 High Fidelity, Inc.
4 //
5 // Distributed under the Apache License, Version 2.0.
6 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
7 //
8 
9 #pragma once
10 #ifndef hifi_GLShaders_h
11 #define hifi_GLShaders_h
12 
13 #include "Config.h"
14 
15 #include <functional>
16 #include <string>
17 #include <unordered_map>
18 #include <unordered_set>
19 #include <vector>
20 
21 namespace gl {
22 
23 struct ShaderBinding {
24  int index;
25  std::string name;
26  GLint size{ -1 };
27  GLint binding{ -1 };
28 };
29 
30 struct Uniform : public ShaderBinding {
31  Uniform(){};
32  Uniform(GLint program, int index) { load(program, index); };
33  using Vector = std::vector<Uniform>;
34  GLenum type{ GL_FLOAT };
35 
36  void load(GLuint glprogram, int index);
37  // Incredibly slow on mac, DO NOT USE
38  static Vector load(GLuint glprogram, const std::function<bool(const Uniform&)>& filter);
39  static Vector loadTextures(GLuint glprogram);
40  static Vector load(GLuint glprogram);
41  static Vector load(GLuint glprogram, const std::vector<GLuint>& indices);
42  static Vector load(GLuint glprogram, const std::vector<const char*>& names);
43  static Vector load(GLuint glprogram, const std::vector<std::string>& names);
44  static Uniform loadByName(GLuint glprogram, const std::string& names);
45 
46  template <typename C>
47  static Vector loadByName(GLuint glprogram, const C& names) {
48  if (names.empty()) {
49  return {};
50  }
51  std::vector<const char*> cnames;
52  cnames.reserve(names.size());
53  for (const auto& name : names) {
54  cnames.push_back(name.c_str());
55  }
56  return load(glprogram, cnames);
57  }
58 };
59 
60 using Uniforms = Uniform::Vector;
61 
62 struct UniformBlock : public ShaderBinding {
63  UniformBlock(){};
64  UniformBlock(GLint program, int index) { load(program, index); };
65 
66  using Vector = std::vector<UniformBlock>;
67  void load(GLuint glprogram, int index);
68  static Vector load(GLuint glprogram);
69 };
70 
71 using UniformBlocks = UniformBlock::Vector;
72 
73 struct Input : public ShaderBinding {
74  Input(){};
75  Input(GLint program, int index) { load(program, index); };
76  using Vector = std::vector<Uniform>;
77  GLenum type{ GL_FLOAT };
78 
79  void load(GLuint glprogram, int index);
80  static Vector load(GLuint glprogram);
81 };
82 
83 using Inputs = Input::Vector;
84 
85 struct CachedShader {
86  GLenum format{ 0 };
87  std::string source;
88  std::vector<char> binary;
89  inline operator bool() const { return format != 0 && !binary.empty(); }
90 };
91 
92 using ShaderCache = std::unordered_map<std::string, CachedShader>;
93 
94 std::string getShaderHash(const std::string& shaderSource);
95 void loadShaderCache(ShaderCache& cache);
96 void saveShaderCache(const ShaderCache& cache);
97 
98 #ifdef SEPARATE_PROGRAM
99 bool compileShader(GLenum shaderDomain,
100  const std::string& shaderSource,
101  GLuint& shaderObject,
102  GLuint& programObject,
103  std::string& message);
104 bool compileShader(GLenum shaderDomain,
105  const std::vector<std::string>& shaderSources,
106  GLuint& shaderObject,
107  GLuint& programObject,
108  std::string& message);
109 #else
110 bool compileShader(GLenum shaderDomain, const std::string& shaderSource, GLuint& shaderObject, std::string& message);
111 bool compileShader(GLenum shaderDomain,
112  const std::vector<std::string>& shaderSources,
113  GLuint& shaderObject,
114  std::string& message);
115 #endif
116 
117 GLuint buildProgram(const std::vector<GLuint>& glshaders);
118 GLuint buildProgram(const CachedShader& binary);
119 bool linkProgram(GLuint glprogram, std::string& message);
120 void getShaderInfoLog(GLuint glshader, std::string& message);
121 void getProgramInfoLog(GLuint glprogram, std::string& message);
122 void getProgramBinary(GLuint glprogram, CachedShader& cachedShader);
123 } // namespace gl
124 
125 #endif