Choreonoid  1.8
GLSLProgram.h
Go to the documentation of this file.
1 
5 #ifndef CNOID_BASE_GLSL_PROGRAM_H
6 #define CNOID_BASE_GLSL_PROGRAM_H
7 
8 #include "glcore.h"
9 #include <cnoid/EigenTypes>
10 #include <vector>
11 #include <string>
12 #include <cstring>
13 #include "exportdecl.h"
14 
15 namespace cnoid {
16 
17 class CNOID_EXPORT GLSLProgram
18 {
19 public:
20  GLSLProgram();
21  GLSLProgram(const GLSLProgram&) = delete;
22  ~GLSLProgram() = default;
23  GLSLProgram& operator=(const GLSLProgram&) = delete;
24 
25  void release();
26  void loadShader(const char* filename, int shaderType);
27  void link();
28  void validate();
29  void use();
30 
31  GLint handle() const { return programHandle; }
32  bool isLinked() const { return isLinked_; }
33 
34  GLuint getUniformLocation(const char* name) const {
35  return glGetUniformLocation(programHandle, name);
36  }
37  GLuint getUniformLocation(const std::string& name) const {
38  return glGetUniformLocation(programHandle, name.c_str());
39  }
40  GLuint getSubroutineIndex(GLenum shaderType, const GLchar* name) const {
41 #ifdef CNOID_GL_CORE_4_4
42  return glGetSubroutineIndex(programHandle, shaderType, name);
43 #else
44  return -1;
45 #endif
46  }
47  void setUniformSubroutines(GLenum shaderType, GLsizei count, const GLuint *indices){
48 #ifdef CNOID_GL_CORE_4_4
49  return glUniformSubroutinesuiv(shaderType, count, indices);
50 #endif
51  }
52 
53 private:
54  GLuint programHandle;
55  bool isLinked_;
56 };
57 
58 
59 class CNOID_EXPORT GLSLUniformBlockBuffer
60 {
61 public:
64 
65  bool initialize(GLSLProgram& program, const std::string& blockName);
66 
70  GLuint checkUniform(const char* name);
71 
72  GLuint checkUniformMatrix(const char* name);
73 
74  void bind(GLSLProgram& program, GLuint bindingPoint) {
75  GLuint blockIndex = glGetUniformBlockIndex(program.handle(), blockName.c_str());
76  glUniformBlockBinding(program.handle(), blockIndex, bindingPoint);
77  }
78 
79  void bindBufferBase(GLuint bindingPoint) {
80  glBindBufferBase(GL_UNIFORM_BUFFER, bindingPoint, uboHandle);
81  }
82 
83  void write(GLuint index, float v){
84  std::memcpy(&localBuffer[infos[index].offset], &v, sizeof(v));
85  }
86 
87  void write(GLuint index, const Vector3f& v){
88  std::memcpy(&localBuffer[infos[index].offset], v.data(), sizeof(v));
89  }
90 
91  void write(GLuint index, const Vector4f& v){
92  std::memcpy(&localBuffer[infos[index].offset], v.data(), sizeof(v));
93  }
94 
95  void write(GLuint index, const Matrix3f& M){
96  const UniformInfo& info = infos[index];
97  const int size = 3 * sizeof(Matrix3f::Scalar);
98  std::memcpy(&localBuffer[info.offset], &M(0, 0), size);
99  std::memcpy(&localBuffer[info.offset + info.matrixStrides], &M(0, 1), size);
100  std::memcpy(&localBuffer[info.offset + info.matrixStrides * 2], &M(0, 2), size);
101  }
102 
103  void write(GLuint index, const Matrix4f& M){
104  std::memcpy(&localBuffer[infos[index].offset], M.data(), sizeof(M));
105  }
106 
107  void write(GLuint index, const Affine3f& T){
108  std::memcpy(&localBuffer[infos[index].offset], T.matrix().data(), sizeof(T));
109  }
110 
111  void flush(){
112  glBindBuffer(GL_UNIFORM_BUFFER, uboHandle);
113  glBufferSubData(GL_UNIFORM_BUFFER, 0, localBuffer.size(), &localBuffer[0]);
114  }
115 
116 private:
117  GLuint uboHandle;
118  std::vector<GLubyte> localBuffer;
119  struct UniformInfo {
120  GLint offset;
121  GLint matrixStrides;
122  };
123  std::vector<UniformInfo> infos;
124  GLuint lastProgramHandle;
125  std::string blockName;
126 };
127 
128 }
129 
130 #endif
cnoid::GLSLProgram::getSubroutineIndex
GLuint getSubroutineIndex(GLenum shaderType, const GLchar *name) const
Definition: GLSLProgram.h:40
cnoid::GLSLProgram::isLinked
bool isLinked() const
Definition: GLSLProgram.h:32
cnoid::GLSLUniformBlockBuffer::write
void write(GLuint index, const Affine3f &T)
Definition: GLSLProgram.h:107
cnoid::GLSLProgram::setUniformSubroutines
void setUniformSubroutines(GLenum shaderType, GLsizei count, const GLuint *indices)
Definition: GLSLProgram.h:47
cnoid::GLSLUniformBlockBuffer::write
void write(GLuint index, const Matrix3f &M)
Definition: GLSLProgram.h:95
cnoid::GLSLProgram::getUniformLocation
GLuint getUniformLocation(const char *name) const
Definition: GLSLProgram.h:34
cnoid::GLSLUniformBlockBuffer::write
void write(GLuint index, float v)
Definition: GLSLProgram.h:83
cnoid::GLSLUniformBlockBuffer::bindBufferBase
void bindBufferBase(GLuint bindingPoint)
Definition: GLSLProgram.h:79
cnoid::GLSLUniformBlockBuffer::write
void write(GLuint index, const Vector4f &v)
Definition: GLSLProgram.h:91
cnoid::GLSLProgram::getUniformLocation
GLuint getUniformLocation(const std::string &name) const
Definition: GLSLProgram.h:37
cnoid::GLSLProgram::handle
GLint handle() const
Definition: GLSLProgram.h:31
cnoid::GLSLUniformBlockBuffer::bind
void bind(GLSLProgram &program, GLuint bindingPoint)
Definition: GLSLProgram.h:74
cnoid::GLSLUniformBlockBuffer::write
void write(GLuint index, const Vector3f &v)
Definition: GLSLProgram.h:87
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::GLSLUniformBlockBuffer
Definition: GLSLProgram.h:59
cnoid::GLSLUniformBlockBuffer::flush
void flush()
Definition: GLSLProgram.h:111
cnoid::GLSLUniformBlockBuffer::write
void write(GLuint index, const Matrix4f &M)
Definition: GLSLProgram.h:103
cnoid::GLSLProgram
Definition: GLSLProgram.h:17