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
|
// shader handling
// Author: Supakorn "Jamie" Rassameemasmuang
#include "common.h"
#ifdef HAVE_GL
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <iostream>
#include "shaders.h"
int GLSLversion;
GLuint compileAndLinkShader(std::vector<ShaderfileModePair> const& shaders,
std::vector<std::string> const& defineflags,
bool ssbo, bool interlock, bool compute)
{
GLuint shader = glCreateProgram();
std::vector<GLuint> compiledShaders;
size_t n=shaders.size();
for(size_t i=0; i < n; ++i) {
GLint newshader=createShaderFile(shaders[i].first,shaders[i].second,
defineflags,ssbo,interlock,compute);
if((ssbo || interlock || compute) && newshader == 0) return 0;
glAttachShader(shader,newshader);
compiledShaders.push_back(newshader);
}
glBindAttribLocation(shader,positionAttrib,"position");
glBindAttribLocation(shader,normalAttrib,"normal");
glBindAttribLocation(shader,materialAttrib,"material");
glBindAttribLocation(shader,colorAttrib,"color");
glBindAttribLocation(shader,widthAttrib,"width");
glLinkProgram(shader);
for(size_t i=0; i < n; ++i) {
glDetachShader(shader,compiledShaders[i]);
glDeleteShader(compiledShaders[i]);
}
return shader;
}
GLuint createShader(const std::string& src, int shaderType,
const std::string& filename, bool ssbo, bool interlock,
bool compute)
{
const GLchar *source=src.c_str();
GLuint shader=glCreateShader(shaderType);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if(status != GL_TRUE) {
if(ssbo || interlock || compute) return 0;
GLint length;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
std::vector<GLchar> msg(length);
glGetShaderInfoLog(shader, length, &length, msg.data());
size_t n=msg.size();
for(size_t i=0; i < n; ++i)
std::cerr << msg[i];
std::cerr << std::endl << "GL Compile error" << std::endl;
std::stringstream s(src);
std::string line;
unsigned int k=0;
while(getline(s,line))
std::cerr << ++k << ": " << line << std::endl;
exit(-1);
}
return shader;
}
GLuint createShaderFile(std::string file, int shaderType,
std::vector<std::string> const& defineflags,
bool ssbo, bool interlock, bool compute)
{
std::ifstream shaderFile;
shaderFile.open(file.c_str());
std::stringstream shaderSrc;
shaderSrc << "#version " << GLSLversion << "\n";
#ifndef __APPLE__
shaderSrc << "#extension GL_ARB_uniform_buffer_object : enable" << "\n";
#ifdef HAVE_SSBO
if(ssbo) {
shaderSrc << "#extension GL_ARB_shader_storage_buffer_object : enable" << "\n";
shaderSrc << "#extension GL_ARB_shader_atomic_counters : enable" << "\n";
if(interlock)
shaderSrc << "#extension GL_ARB_fragment_shader_interlock : enable"
<< "\n";
if(compute)
shaderSrc << "#extension GL_ARB_compute_shader : enable" << "\n";
}
#endif
#endif
size_t n=defineflags.size();
for(size_t i=0; i < n; ++i)
shaderSrc << "#define " << defineflags[i] << "\n";
if(shaderFile) {
shaderSrc << shaderFile.rdbuf();
shaderFile.close();
} else {
std::cerr << "Cannot read from shader file " << file << std::endl;
exit(-1);
}
return createShader(shaderSrc.str(),shaderType,file,ssbo,interlock,compute);
}
#endif
|