File: gpuProgram.h

package info (click to toggle)
meshlab 1.3.2+dfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 21,096 kB
  • ctags: 33,630
  • sloc: cpp: 224,813; ansic: 8,170; xml: 119; makefile: 80
file content (404 lines) | stat: -rw-r--r-- 15,558 bytes parent folder | download | duplicates (3)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/****************************************************************************
* Render Radiance Scaling                                                   *
* Meshlab's plugin                                                          *
*                                                                           *
* Copyright(C) 2010                                                         *
* Vergne Romain, Dumas Olivier                                              *
* INRIA - Institut Nationnal de Recherche en Informatique et Automatique    *
*                                                                           *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/
#ifndef GPUPROGRAM_H
#define GPUPROGRAM_H

#include <GL/glew.h>
#include <iostream>
#include "gpuShader.h"
#include <string>
#include <vector>
#include <map>

class GPUProgram {
    
 public:
  GPUProgram(GPUShader *vs=NULL,GPUShader *fs=NULL,GPUShader *geom=NULL,
             int inputGeometry=0,int outputGeometry=0,int outVertices=0);

  GPUProgram(const std::string &vsFile="",
             const std::string &fsFile="",
             const std::string &gsFile="",
             int inputGeometry=0,
             int outputGeometry=0,
             int outVertices=0);
    
  ~GPUProgram();
    
  void addUniform(const std::string &uniformName);
  void addAttribute(const std::string &attributeName);
  void reload();

  inline GLuint id() const;
  bool haveShaderOfType(SHADER_TYPE type);
  std::string filename(SHADER_TYPE type);

  inline void enable();
  inline void disable();

  inline GLint getUniformLocation(const std::string &uniformName);
  inline GLint getAttributeLocation(const std::string &attributeName);

  inline void setUniform1f(const std::string &uniformName,GLfloat v);
  inline void setUniform2f(const std::string &uniformName,GLfloat v1,GLfloat v2);
  inline void setUniform3f(const std::string &uniformName,GLfloat v1,GLfloat v2,GLfloat v3);
  inline void setUniform4f(const std::string &uniformName,GLfloat v1,GLfloat v2,GLfloat v3,GLfloat v4);
  inline void setUniform1f(GLint loc,GLfloat v);
  inline void setUniform2f(GLint loc,GLfloat v1,GLfloat v2);
  inline void setUniform3f(GLint loc,GLfloat v1,GLfloat v2,GLfloat v3);
  inline void setUniform4f(GLint loc,GLfloat v1,GLfloat v2,GLfloat v3,GLfloat v4);

  inline void setUniform1i(const std::string &uniformName,GLint v);
  inline void setUniform2i(const std::string &uniformName,GLint v1,GLint v2);
  inline void setUniform3i(const std::string &uniformName,GLint v1,GLint v2,GLint v3);
  inline void setUniform4i(const std::string &uniformName,GLint v1,GLint v2,GLint v3,GLint v4);
  inline void setUniform1i(GLint loc,GLint v);
  inline void setUniform2i(GLint loc,GLint v1,GLint v2);
  inline void setUniform3i(GLint loc,GLint v1,GLint v2,GLint v3);
  inline void setUniform4i(GLint loc,GLint v1,GLint v2,GLint v3,GLint v4);

  inline void setUniform1fv(const std::string &uniformName,GLfloat *v,GLsizei count=1);
  inline void setUniform2fv(const std::string &uniformName,GLfloat *v,GLsizei count=1);
  inline void setUniform3fv(const std::string &uniformName,GLfloat *v,GLsizei count=1);
  inline void setUniform4fv(const std::string &uniformName,GLfloat *v,GLsizei count=1);
  inline void setUniform1fv(GLint loc,GLfloat *v,GLsizei count=1);
  inline void setUniform2fv(GLint loc,GLfloat *v,GLsizei count=1);
  inline void setUniform3fv(GLint loc,GLfloat *v,GLsizei count=1);
  inline void setUniform4fv(GLint loc,GLfloat *v,GLsizei count=1);

  inline void setUniform1iv(const std::string &uniformName,GLint *v,GLsizei count=1);
  inline void setUniform2iv(const std::string &uniformName,GLint *v,GLsizei count=1);
  inline void setUniform3iv(const std::string &uniformName,GLint *v,GLsizei count=1);
  inline void setUniform4iv(const std::string &uniformName,GLint *v,GLsizei count=1);
  inline void setUniform1iv(GLint loc,GLint *v,GLsizei count=1);
  inline void setUniform2iv(GLint loc,GLint *v,GLsizei count=1);
  inline void setUniform3iv(GLint loc,GLint *v,GLsizei count=1);
  inline void setUniform4iv(GLint loc,GLint *v,GLsizei count=1);
 
  inline void setUniformMatrix2fv(const std::string &uniformName,GLfloat *v,GLsizei count=1,GLboolean transpose=false);
  inline void setUniformMatrix3fv(const std::string &uniformName,GLfloat *v,GLsizei count=1,GLboolean transpose=false);
  inline void setUniformMatrix4fv(const std::string &uniformName,GLfloat *v,GLsizei count=1,GLboolean transpose=false);
  inline void setUniformMatrix2fv(GLint loc,GLfloat *v,GLsizei count=1,GLboolean transpose=false);
  inline void setUniformMatrix3fv(GLint loc,GLfloat *v,GLsizei count=1,GLboolean transpose=false);
  inline void setUniformMatrix4fv(GLint loc,GLfloat *v,GLsizei count=1,GLboolean transpose=false);

  inline void setUniformTexture(const std::string &uniformName,GLint num,GLenum type,GLuint textureName);
  inline void setUniformTexture(GLint loc,GLint num,GLenum type,GLuint textureName);

  inline void  setUniformBuffer(const std::string &uniformName,GLuint buffer);
  inline void  setUniformBuffer(GLint loc,GLuint buffer);
  inline GLint getUniformBufferSize(const std::string &uniformName);
  inline GLint getUniformBufferSize(GLint loc);

 protected:
  void attach();
  bool link();
  bool attachAndLink();
  void detach();
  void setGeometryParameters(int inputGeometry,int outputGeometry,int outVertices);
    
 private:
        
  GPUShader* _vs;
  GPUShader* _fs;
  GPUShader* _gs;
  GLuint     _programId;
    
  std::map<std::string,GLint>                _uniformLocations;
  std::map<std::string,GLint>                _attributeLocations;
  std::map<GLuint,std::pair<GLenum,GLenum> > _textures;

  int _inputGeometry;
  int _outputGeometry;
  int _outVertices;
};
  
inline void GPUProgram::enable() {
  glUseProgramObjectARB(_programId);
    
  for(std::map<GLuint,std::pair<GLenum,GLenum> >::iterator i=_textures.begin();i!=_textures.end();++i) {
    glActiveTexture((*i).second.first);
    glBindTexture((*i).second.second,(*i).first);
    glEnable((*i).second.second);
  }

}

inline void GPUProgram::disable() {
  for(std::map<GLuint,std::pair<GLenum,GLenum> >::reverse_iterator i=_textures.rbegin();i!=_textures.rend();++i) {
    glActiveTexture((*i).second.first);
    glDisable((*i).second.second);
  }

  glUseProgramObjectARB(0);
}

inline GLuint GPUProgram::id() const {
  return _programId;
}
  
inline void GPUProgram::setUniform1f(const std::string &uniformName,GLfloat v) {
  glUniform1f(_uniformLocations[uniformName],v);
}

inline void GPUProgram::setUniform2f(const std::string &uniformName,GLfloat v1,GLfloat v2) {
  glUniform2f(_uniformLocations[uniformName],v1,v2);
}

inline void GPUProgram::setUniform3f(const std::string &uniformName,GLfloat v1,GLfloat v2,GLfloat v3) {
  glUniform3f(_uniformLocations[uniformName],v1,v2,v3);
}
      
inline void GPUProgram::setUniform4f(const std::string &uniformName,GLfloat v1,GLfloat v2,GLfloat v3,GLfloat v4) {
  glUniform4f(_uniformLocations[uniformName],v1,v2,v3,v4);
}

inline void GPUProgram::setUniform1i(const std::string &uniformName,GLint v) {
  glUniform1i(_uniformLocations[uniformName],v);
}

inline void GPUProgram::setUniform2i(const std::string &uniformName,GLint v1,GLint v2) {
  glUniform2i(_uniformLocations[uniformName],v1,v2);
}

inline void GPUProgram::setUniform3i(const std::string &uniformName,GLint v1,GLint v2,GLint v3) {
  glUniform3i(_uniformLocations[uniformName],v1,v2,v3);
}
      
inline void GPUProgram::setUniform4i(const std::string &uniformName,GLint v1,GLint v2,GLint v3,GLint v4) {
  glUniform4i(_uniformLocations[uniformName],v1,v2,v3,v4);
}

inline void GPUProgram::setUniform1fv(const std::string &uniformName,GLfloat *v,GLsizei count) {
  glUniform1fv(_uniformLocations[uniformName],count,v);
}

inline void GPUProgram::setUniform2fv(const std::string &uniformName,GLfloat *v,GLsizei count) {
  glUniform2fv(_uniformLocations[uniformName],count,v);
}

inline void GPUProgram::setUniform3fv(const std::string &uniformName,GLfloat *v,GLsizei count) {
  glUniform3fv(_uniformLocations[uniformName],count,v);
}

inline void GPUProgram::setUniform4fv(const std::string &uniformName,GLfloat *v,GLsizei count) {
  glUniform4fv(_uniformLocations[uniformName],count,v);
}

inline void GPUProgram::setUniform1iv(const std::string &uniformName,GLint *v,GLsizei count) {
  glUniform1iv(_uniformLocations[uniformName],count,v);
}

inline void GPUProgram::setUniform2iv(const std::string &uniformName,GLint *v,GLsizei count) {
  glUniform2iv(_uniformLocations[uniformName],count,v);
}

inline void GPUProgram::setUniform3iv(const std::string &uniformName,GLint *v,GLsizei count) {
  glUniform3iv(_uniformLocations[uniformName],count,v);
}

inline void GPUProgram::setUniform4iv(const std::string &uniformName,GLint *v,GLsizei count) {
  glUniform4iv(_uniformLocations[uniformName],count,v);
}

inline void GPUProgram::setUniformMatrix2fv(const std::string &uniformName,GLfloat *v,GLsizei count,GLboolean transpose) {
  glUniformMatrix2fv(_uniformLocations[uniformName],count,transpose,v);
}

inline void GPUProgram::setUniformMatrix3fv(const std::string &uniformName,GLfloat *v,GLsizei count,GLboolean transpose) {
  glUniformMatrix3fv(_uniformLocations[uniformName],count,transpose,v);
}

inline void GPUProgram::setUniformMatrix4fv(const std::string &uniformName,GLfloat *v,GLsizei count,GLboolean transpose) {
  glUniformMatrix4fv(_uniformLocations[uniformName],count,transpose,v);
}
  
inline void GPUProgram::setUniformTexture(const std::string &uniformName,GLint num,GLenum type,GLuint textureName) {
  GLenum textureNum;

  const std::map<GLuint,std::pair<GLenum,GLenum> >::iterator it = _textures.find(textureName);

  if(it==_textures.end()) {
    textureNum = GL_TEXTURE0+_textures.size();
  } else {
    textureNum = (*it).second.first;
  }
    
  glPushAttrib(GL_TEXTURE_BIT);
  glActiveTexture(textureNum);
  glBindTexture(type,textureName);
  glEnable(type);
    
  glUniform1i(_uniformLocations[uniformName],num);
  _textures[textureName] = std::pair<GLenum,GLenum>(textureNum,type);

  glDisable(type);
  glPopAttrib();
}

inline void GPUProgram::setUniform1f(GLint loc,GLfloat v) {
  glUniform1f(loc,v);
}

inline void GPUProgram::setUniform2f(GLint loc,GLfloat v1,GLfloat v2) {
  glUniform2f(loc,v1,v2);
}

inline void GPUProgram::setUniform3f(GLint loc,GLfloat v1,GLfloat v2,GLfloat v3) {
  glUniform3f(loc,v1,v2,v3);
}
      
inline void GPUProgram::setUniform4f(GLint loc,GLfloat v1,GLfloat v2,GLfloat v3,GLfloat v4) {
  glUniform4f(loc,v1,v2,v3,v4);
}

inline void GPUProgram::setUniform1i(GLint loc,GLint v) {
  glUniform1i(loc,v);
}

inline void GPUProgram::setUniform2i(GLint loc,GLint v1,GLint v2) {
  glUniform2i(loc,v1,v2);
}

inline void GPUProgram::setUniform3i(GLint loc,GLint v1,GLint v2,GLint v3) {
  glUniform3i(loc,v1,v2,v3);
}
      
inline void GPUProgram::setUniform4i(GLint loc,GLint v1,GLint v2,GLint v3,GLint v4) {
  glUniform4i(loc,v1,v2,v3,v4);
}

inline void GPUProgram::setUniform1fv(GLint loc,GLfloat *v,GLsizei count) {
  glUniform1fv(loc,count,v);
}

inline void GPUProgram::setUniform2fv(GLint loc,GLfloat *v,GLsizei count) {
  glUniform2fv(loc,count,v);
}

inline void GPUProgram::setUniform3fv(GLint loc,GLfloat *v,GLsizei count) {
  glUniform3fv(loc,count,v);
}

inline void GPUProgram::setUniform4fv(GLint loc,GLfloat *v,GLsizei count) {
  glUniform4fv(loc,count,v);
}

inline void GPUProgram::setUniform1iv(GLint loc,GLint *v,GLsizei count) {
  glUniform1iv(loc,count,v);
}

inline void GPUProgram::setUniform2iv(GLint loc,GLint *v,GLsizei count) {
  glUniform2iv(loc,count,v);
}

inline void GPUProgram::setUniform3iv(GLint loc,GLint *v,GLsizei count) {
  glUniform3iv(loc,count,v);
}

inline void GPUProgram::setUniform4iv(GLint loc,GLint *v,GLsizei count) {
  glUniform4iv(loc,count,v);
}

inline void GPUProgram::setUniformMatrix2fv(GLint loc,GLfloat *v,GLsizei count,GLboolean transpose) {
  glUniformMatrix2fv(loc,count,transpose,v);
}

inline void GPUProgram::setUniformMatrix3fv(GLint loc,GLfloat *v,GLsizei count,GLboolean transpose) {
  glUniformMatrix3fv(loc,count,transpose,v);
}

inline void GPUProgram::setUniformMatrix4fv(GLint loc,GLfloat *v,GLsizei count,GLboolean transpose) {
  glUniformMatrix4fv(loc,count,transpose,v);
}
  
inline void GPUProgram::setUniformTexture(GLint loc,GLint num,GLenum type,GLuint textureName) {
  GLenum textureNum;

  const std::map<GLuint,std::pair<GLenum,GLenum> >::iterator it = _textures.find(textureName);

  if(it==_textures.end()) {
    textureNum = GL_TEXTURE0+_textures.size();
  } else {
    textureNum = (*it).second.first;
  }
    
  glPushAttrib(GL_TEXTURE_BIT);
  glActiveTexture(textureNum);
  glBindTexture(type,textureName);
  glEnable(type);
    
  glUniform1i(loc,num);
  _textures[textureName] = std::pair<GLenum,GLenum>(textureNum,type);

  glDisable(type);
  glPopAttrib();
}

inline GLint GPUProgram::getUniformLocation(const std::string &uniformName) {
  return _uniformLocations[uniformName];
}

inline GLint GPUProgram::getAttributeLocation(const std::string &attributeName) {
  return _attributeLocations[attributeName];
}

inline void GPUProgram::setUniformBuffer(const std::string &uniformName,GLuint buffer) {
  glUniformBufferEXT(_programId,_uniformLocations[uniformName],buffer);
}

inline void GPUProgram::setUniformBuffer(GLint loc,GLuint buffer) {
  glUniformBufferEXT(_programId,loc,buffer);
}

inline GLint GPUProgram::getUniformBufferSize(const std::string &uniformName) {
  return glGetUniformBufferSizeEXT(_programId,_uniformLocations[uniformName]);
}

inline GLint GPUProgram::getUniformBufferSize(GLint loc) {
  return glGetUniformBufferSizeEXT(_programId,loc);
}

static inline
void CheckErrorsGL(const char* location=NULL,std::ostream& ostr = std::cerr) {
    
  GLuint errnum;
  const char *errstr;
  while((errnum=glGetError())) {
    ostr << " **************************************************** " << std::endl;
    ostr << " Checking OpenGL Error " << std::endl;
      
    std::cout << " **************************************************** " << std::endl;
    std::cout << " Checking OpenGL Error " << std::endl;
      
    errstr = reinterpret_cast<const char *>(gluErrorString(errnum));
    ostr << errstr;
      
    if(location) ostr << " at " << location;
    ostr << std::endl;
    std::cout << " **************************************************** " << std::endl;
    ostr << " **************************************************** " << std::endl;
  }
}

#endif // GPUPROGRAM_H