File: gpuProgram.cpp

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 (224 lines) | stat: -rw-r--r-- 6,172 bytes parent folder | download | duplicates (4)
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
/****************************************************************************
* 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.                                                         *
*                                                                           *
****************************************************************************/
#include "gpuProgram.h"
#include <iostream>

using namespace std;
      

GPUProgram::GPUProgram(GPUShader* vs,GPUShader* fs,GPUShader *gs,
                       int inputGeometry,int outputGeometry,int outVertices)
  : _vs(vs),
    _fs(fs),
    _gs(gs),
    _inputGeometry(inputGeometry),
    _outputGeometry(outputGeometry),
    _outVertices(outVertices) {

  _programId = glCreateProgram();
  setGeometryParameters(_inputGeometry,_outputGeometry,_outVertices);
  attachAndLink();
    }

GPUProgram::GPUProgram(const string &vsFile,
                       const string &fsFile,
                       const string &gsFile,
                       int inputGeometry,
                       int outputGeometry,
                       int outVertices)
  :_inputGeometry(inputGeometry),
   _outputGeometry(outputGeometry),
   _outVertices(outVertices) {

  _vs = _fs = _gs = NULL;

  if(vsFile!="")
    _vs = new GPUShader(VERT,vsFile);

  if(fsFile!="")
    _fs = new GPUShader(FRAG,fsFile);

  if(gsFile!="")
    _gs = new GPUShader(GEOM,gsFile);

  _programId = glCreateProgram();
  setGeometryParameters(_inputGeometry,_outputGeometry,_outVertices);
  attachAndLink();
   }
      
GPUProgram::~GPUProgram() {
      
  detach();
    
  if(_vs!=NULL) {
    delete _vs;
  }

  if(_fs!=NULL) {
    delete _fs;
  }

  if(_gs!=NULL) {
    delete _gs;
  }
    
  glDeleteProgram(_programId);
}

void GPUProgram::setGeometryParameters(int inputGeometry,int outputGeometry,int outVertices) {
#ifdef GL_EXT_geometry_shader4
  if(GL_EXT_geometry_shader4 && _gs!=NULL && _gs->id()!=0) {
    glProgramParameteriEXT(_programId,GL_GEOMETRY_INPUT_TYPE_EXT,inputGeometry);
    glProgramParameteriEXT(_programId,GL_GEOMETRY_OUTPUT_TYPE_EXT,outputGeometry);
    glProgramParameteriEXT(_programId,GL_GEOMETRY_VERTICES_OUT_EXT,outVertices);
  }
#endif
}

void GPUProgram::attach() {
  if(_vs!=NULL) {
    glAttachShader(_programId,_vs->id()); 
  }

  if(_fs!=NULL) {
    glAttachShader(_programId,_fs->id());
  }

  if(_gs!=NULL) {
    glAttachShader(_programId,_gs->id());       
  }
}

void GPUProgram::detach() {
        
  if(_vs!=NULL) {
    glDetachShader(_programId,_vs->id()); 
  }

  if(_fs!=NULL) {
    glDetachShader(_programId,_fs->id());
  }

  if(_gs!=NULL) {
    glDetachShader(_programId,_gs->id());  
  }
}

bool GPUProgram::link() {
  int linked = 1;

  glLinkProgram(_programId);
      
  glGetObjectParameterivARB(_programId,GL_OBJECT_LINK_STATUS_ARB,&linked);
       
  if(linked)
    return true;
    
  return false;
}

bool GPUProgram::attachAndLink() {
  attach();
  return link();
}

void GPUProgram::reload() {
      
  detach();

  bool allOk = true;
  if(_vs!=NULL) {
    allOk = allOk && _vs->loadAndCompile();      
  }

  if(_fs!=NULL) {
    allOk = allOk && _fs->loadAndCompile();
  }

  if(_gs!=NULL) {
    allOk = allOk && _gs->loadAndCompile();
  }
  
  if(!allOk){
    std::cout << "reload fail, maybe missing file" << std::endl;
  }
  setGeometryParameters(_inputGeometry,_outputGeometry,_outVertices);
  attachAndLink();

  // reload uniforms 
  for(map<string,GLint>::iterator i=_uniformLocations.begin();i!=_uniformLocations.end();i++) {
    _uniformLocations[(*i).first] = glGetUniformLocation(_programId,(*i).first.c_str());
  }

  // reload attributes
  for(map<string,GLint>::iterator i=_attributeLocations.begin();i!=_attributeLocations.end();i++) {
    _uniformLocations[(*i).first] = glGetAttribLocation(_programId,(*i).first.c_str());
  }

  // free textures 
  _textures.clear();
}

void GPUProgram::addUniform(const string &uniformName) {
  GLint location = glGetUniformLocation(_programId,uniformName.c_str());  
    
  _uniformLocations[uniformName] = location;
}

void GPUProgram::addAttribute(const string &attributeName) {
  GLint location = glGetAttribLocation(_programId,attributeName.c_str());  
    
  _attributeLocations[attributeName] = location;
}

bool GPUProgram::haveShaderOfType(SHADER_TYPE type) {
    
  if(type==VERT)
    return _vs!=NULL;
    
  if(type==FRAG)
    return _fs!=NULL;
    
  if(type==GEOM)
    return _gs!=NULL;
    
  cout << "Warning : unknown type !" << endl;
  
  return false;
}

string GPUProgram::filename(SHADER_TYPE type) {
    
  if(type==VERT && _vs!=NULL)
    return _vs->filename();
    
  if(type==FRAG && _fs!=NULL)
    return _fs->filename();
    
  if(type==GEOM && _gs!=NULL)
    return _gs->filename();

  cout << "Warning : unknown type !" << endl;
    
  return "";
}