aboutsummaryrefslogtreecommitdiff
path: root/samples/OpenGL/compute_shader/main.c
blob: a5194395167e8f0d8a0ca51397e0fd3e7ce2d874 (plain)
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
#include <GL/glew.h>
#include <GL/glut.h>
#include <stdbool.h>
#include <stdio.h>

#include "debuggl/debuggl.h"
#include "linmath.h/linmath.h"

#include "positiongen.h"
#include "solid.h"
#include "stats.h"

#define HAS_ARCBALL 0

static struct {
	int width;
	int height;
	float aspect;
} window;

static struct {
	GLuint vbo;
	GLuint vao;
	int grid[2];
} vertexbuffer;

static GLuint timerquery;

#if HAS_ARCBALL
#define MAT4X4_IDENTITY {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}}
static struct dragging {
	bool left;
	vec2 v;
} pointer_dragging;
static mat4x4 view = MAT4X4_IDENTITY;
static mat4x4 arcball = MAT4X4_IDENTITY;
#endif

static struct stats_running drawtime_stats = STATS_RUNNING_INIT;

static
int create_vertexbuffer(int rows, int cols)
{
	size_t const dim = 4;

	debuggl_check( glGenVertexArrays(1, &vertexbuffer.vao) );
	debuggl_check( glBindVertexArray(vertexbuffer.vao) );

	debuggl_check( glGenBuffers(1, &vertexbuffer.vbo) );
	debuggl_check( glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer.vbo) );

	debuggl_check( glBufferStorage(GL_ARRAY_BUFFER,
		sizeof(GLfloat)*dim * rows * cols,
		NULL, 0 ) );

	debuggl_check( glEnableVertexAttribArray(0) );
	debuggl_check( glVertexAttribPointer(0, dim, GL_FLOAT, GL_FALSE, 0, 0) );

	vertexbuffer.grid[0] = rows;
	vertexbuffer.grid[1] = cols;

	debuggl_check( glBindBuffer(GL_ARRAY_BUFFER, 0) );
	debuggl_check( glBindVertexArray(0) );

	return 0;
}

static 
int load_shaders(void)
{
	int rc;
	(void)(0
	|| (rc= positiongen_load() )
	|| (rc= solid_load() )
	);
	return rc;
}

static
int create_resources(void)
{
	int rc;
	(void)(0
	|| (rc= create_vertexbuffer(1024, 1024) )
	|| (rc= load_shaders() )
	);

	if( !rc ) {
		debuggl_check( glGenQueries(1, &timerquery) );
	}

	return rc;
}

static
void keyboard(unsigned char key, int x, int y)
{
	switch(key) {
	case 'r':
	case 'R':
		load_shaders();
		glutPostRedisplay();
		break;
	}
}

#if HAS_ARCBALL
static
void pointer_button(int button, int state, int x, int y)
{
	if( GLUT_LEFT_BUTTON == button ) {
		if( GLUT_UP == state ) {
			pointer_dragging.left = false;

			mat4x4_mul(view, arcball, view);
			mat4x4_orthonormalize(view, view);
			mat4x4_identity(arcball);
		}
		else {
			pointer_dragging.left = true;

			pointer_dragging.v[0] =  2.f * (float)x / window.width  - 1.f;
			pointer_dragging.v[1] = -2.f * (float)y / window.height + 1.f;
		}
	}
}

static
void pointer_drag_motion(int x, int y)
{
	if( pointer_dragging.left ) {
		vec2 motion_v = {
			 2.f * (float)x / window.width  - 1.f,
			-2.f * (float)y / window.height + 1.f
		};

		mat4x4_identity(arcball);
		mat4x4_arcball(arcball, arcball,
			pointer_dragging.v,
			motion_v,
			1 );
		glutPostRedisplay();
	}
}
#endif

static
void reshape(int w, int h)
{
	window.width = w;
	window.height = h;
	window.aspect = (float)w / (float)h;

	stats_running_reset(&drawtime_stats);

	glutPostRedisplay();
}

static
void display(void)
{
	mat4x4 proj;
	mat4x4_identity(proj);

	float const fov = 0.5;
	mat4x4_frustum(proj,
		-window.aspect*fov,
		 window.aspect*fov,
		-fov,
		 fov, 1, 5);

	mat4x4 mv;
	mat4x4_identity(mv);
	mat4x4_translate(mv, 0, 0, -3);
#if HAS_ARCBALL
	mat4x4_mul(mv, mv, arcball);
	mat4x4_mul(mv, mv, view);
#endif

	glViewport(0,0,window.width,window.height);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	positiongen_launch(
		glutGet(GLUT_ELAPSED_TIME)*0.001,
		vertexbuffer.vbo,
		vertexbuffer.grid[0],
		vertexbuffer.grid[1] );

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
	float const white[4] = {1., 1., 1., 0.01};
	glBeginQuery(GL_TIME_ELAPSED, timerquery);
	solid_draw(
		GL_POINTS,
		vertexbuffer.vao,
		vertexbuffer.grid[0] * vertexbuffer.grid[1],
		white,
		mv[0], proj[0]);
	glEndQuery(GL_TIME_ELAPSED);

	glutSwapBuffers();

	GLuint timeresult;
	glGetQueryObjectuiv(timerquery, GL_QUERY_RESULT, &timeresult);
	stats_running_push(&drawtime_stats, timeresult*0.001);
}

enum {
	win_width_inc = 64, win_height_inc = 64,
	win_width_max = 1024, win_height_max = 1024
};
static int win_width = win_width_inc, win_height = win_height_inc;

static
void idle(void)
{
	if( 499 < stats_running_N(&drawtime_stats) ) {
		printf("%4d x %4d: ( %7.1f +/- %7.1f )us\n",
			win_width, win_height,
			stats_running_mean(&drawtime_stats),
			sqrt(stats_running_variance(&drawtime_stats)) );

		win_width += win_width_inc;
		if( win_width_max < win_width ) {
			win_height += win_height_inc;
			if( win_height_max < win_height ) {
				exit(0);
			}
			win_width = win_width_inc;
		}
		glutReshapeWindow(win_width, win_height);
	}
	glutPostRedisplay();
}

static
void gldebugcallback(
	GLenum source,
	GLenum type,
	GLuint id,
	GLenum severity,
	GLsizei length,
	const GLchar *message,
	const void *userParam)
{
	fprintf(stderr, "(GL) %s\n", message);
}

static
void glinfodump(void)
{
	printf(
		"OpenGL vendor:   %s\n"
		"OpenGL renderer: %s\n"
		"OpenGL version:  %s\n",
		"  GLSL version:  %s\n",
		glGetString(GL_VENDOR),
		glGetString(GL_RENDERER),
		glGetString(GL_VERSION),
		glGetString(GL_SHADING_LANGUAGE_VERSION) );
}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);

	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
	glutInitWindowPosition(0,0);
	glutInitWindowSize(win_width, win_height);
	glutCreateWindow("compute shader point overdraw benchmark");
	if( GLEW_OK != glewInit() ) { return 1; }

	glDebugMessageCallback((GLDEBUGPROC)gldebugcallback, NULL);
	glEnable(GL_DEBUG_OUTPUT);

	if( create_resources() ) { return 2; }

	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
#if HAS_ARCBALL
	glutMouseFunc(pointer_button);
	glutMotionFunc(pointer_drag_motion);
#endif
	glutDisplayFunc(display);
	glutIdleFunc(idle);

	glutMainLoop();
	return 0;
}