/
main.cpp
299 lines (242 loc) · 6.95 KB
/
main.cpp
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
////////////////////////////////////////////////////////////////////////////////
// \author Jonathan Dupuy
// \brief AMD Catalyst 11.12 bug.
//
////////////////////////////////////////////////////////////////////////////////
// GL libraries
#include "glew.hpp"
#include "GL/freeglut.h"
// Standard librabries
#include <iostream>
#include <sstream>
#include <vector>
////////////////////////////////////////////////////////////////////////////////
// Global variables
//
////////////////////////////////////////////////////////////////////////////////
// constants
const GLint ELEM_CNT = 8;
// OpenGL objects
GLuint vertexArray = 0;
GLuint program = 0;
GLuint buffer = 0;
GLuint texture = 0;
// program code
const GLchar* vertexSrc[]={
"#version 420 core\n",
"layout(r32i) coherent uniform iimageBuffer imgData;\n",
"void main() {\n",
"imageAtomicCompSwap(imgData, gl_VertexID, -1, gl_VertexID);", // fails
// "imageAtomicExchange(imgData, gl_VertexID, gl_VertexID);", // works
"}\n"
};
////////////////////////////////////////////////////////////////////////////////
// Functions
//
////////////////////////////////////////////////////////////////////////////////
#ifndef _WIN32
////////////////////////////////////////////////////////////////////////////////
// Convert GL error code to string
GLvoid gl_debug_message_callback(GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
GLvoid* userParam)
{
std::cerr << "[DEBUG_OUTPUT] "
<< message
<< std::endl;
}
#else
const GLchar* gl_error_to_string(GLenum error)
{
switch(error)
{
case GL_NO_ERROR:
return "GL_NO_ERROR";
case GL_INVALID_ENUM:
return "GL_INVALID_ENUM";
case GL_INVALID_VALUE:
return "GL_INVALID_VALUE";
case GL_INVALID_OPERATION:
return "GL_INVALID_OPERATION";
case GL_INVALID_FRAMEBUFFER_OPERATION:
return "GL_INVALID_FRAMEBUFFER_OPERATION";
case GL_OUT_OF_MEMORY:
return "GL_OUT_OF_MEMORY";
default:
return "unknown code";
}
}
#endif
////////////////////////////////////////////////////////////////////////////////
// on init cb
void on_init()
{
#ifndef _WIN32
// Configure debug output
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
glDebugMessageCallbackARB(
reinterpret_cast<GLDEBUGPROCARB>(&gl_debug_message_callback),
NULL );
#endif
// gen names
glGenBuffers(1, &buffer);
glGenTextures(1, &texture);
glGenVertexArrays(1, &vertexArray);
program = glCreateProgram();
// build buffer
std::vector<GLint> bufferData(ELEM_CNT,-1);
glBindBuffer(GL_TEXTURE_BUFFER, buffer);
glBufferData(GL_TEXTURE_BUFFER,
sizeof(GLint)*bufferData.size(),
&bufferData[0],
GL_STATIC_DRAW);
glBindBuffer(GL_TEXTURE_BUFFER, 0);
// build texture
glBindTexture(GL_TEXTURE_BUFFER, texture);
glTexBuffer(GL_TEXTURE_BUFFER, GL_R32I, buffer);
// bind as image
glBindImageTexture(0,
texture,
0,
GL_FALSE,
0,
GL_READ_WRITE,
GL_R32I);
// build vao
glBindVertexArray(vertexArray);
// empty
glBindVertexArray(0);
// build program
GLuint vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 5, vertexSrc, NULL);
glCompileShader(vertex);
glAttachShader(program, vertex);
glDeleteShader(vertex);
glLinkProgram(program);
// set uniforms
glProgramUniform1i(program,
glGetUniformLocation(program, "imgData"),
0);
// draw data
glUseProgram(program);
glBindVertexArray(vertexArray);
glDrawArrays(GL_POINTS, 0, ELEM_CNT);
// map buffer and check data validity
glBindVertexArray(0);
glBindBuffer(GL_TEXTURE_BUFFER, buffer);
GLint *dataPtr = (GLint*) glMapBuffer(GL_TEXTURE_BUFFER, GL_READ_ONLY);
std::cout << "buffer content : ";
for(GLint i = 0; i<ELEM_CNT; ++i)
std::cout << dataPtr[i] << ' ';
std::cout << std::endl;
glUnmapBuffer(GL_TEXTURE_BUFFER);
#ifdef _WIN32
GLenum error = glGetError();
if(error!=GL_NO_ERROR)
std::cerr << "caught "
<< gl_error_to_string(error)
<< '\n';
#endif
}
////////////////////////////////////////////////////////////////////////////////
// on clean cb
void on_clean()
{
// delete objects
glDeleteBuffers(1, &buffer);
glDeleteTextures(1, &texture);
glDeleteVertexArrays(1, &vertexArray);
glDeleteProgram(program);
}
////////////////////////////////////////////////////////////////////////////////
// on update cb
void on_update()
{
// clear back buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
#ifdef _WIN32
GLenum error = glGetError();
if(error!=GL_NO_ERROR)
std::cerr << "caught "
<< gl_error_to_string(error)
<< '\n';
#endif
// redraw
glutSwapBuffers();
glutPostRedisplay();
}
////////////////////////////////////////////////////////////////////////////////
// on resize cb
void on_resize(GLint w, GLint h)
{
}
////////////////////////////////////////////////////////////////////////////////
// on key down cb
void on_key_down(GLubyte key, GLint x, GLint y)
{
if (key==27) // escape
glutLeaveMainLoop();
}
////////////////////////////////////////////////////////////////////////////////
// on mouse button cb
void on_mouse_button(GLint button, GLint state, GLint x, GLint y)
{
}
////////////////////////////////////////////////////////////////////////////////
// on mouse motion cb
void on_mouse_motion(GLint x, GLint y)
{
}
////////////////////////////////////////////////////////////////////////////////
// on mouse wheel cb
void on_mouse_wheel(GLint wheel, GLint direction, GLint x, GLint y)
{
}
////////////////////////////////////////////////////////////////////////////////
// Main
//
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char** argv)
{
const GLuint CONTEXT_MAJOR = 4;
const GLuint CONTEXT_MINOR = 2;
// init glut
glutInit(&argc, argv);
glutInitContextVersion(CONTEXT_MAJOR ,CONTEXT_MINOR);
glutInitContextFlags(GLUT_DEBUG);
glutInitContextProfile(GLUT_CORE_PROFILE);
// build window
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(800, 600);
glutInitWindowPosition(0, 0);
glutCreateWindow("unpack");
// init glew
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if(GLEW_OK != err)
{
std::stringstream ss;
ss << err;
std::cerr << "glewInit() gave error " << ss.str() << std::endl;
return 1;
}
// glewInit generates an INVALID_ENUM error for some reason...
glGetError();
// set callbacks
glutCloseFunc(&on_clean);
glutReshapeFunc(&on_resize);
glutDisplayFunc(&on_update);
glutKeyboardFunc(&on_key_down);
glutMouseFunc(&on_mouse_button);
glutPassiveMotionFunc(&on_mouse_motion);
glutMotionFunc(&on_mouse_motion);
glutMouseWheelFunc(&on_mouse_wheel);
// run
on_init();
glutMainLoop();
return 0;
}