aboutsummaryrefslogtreecommitdiff
path: root/tests/glx/glx-multithread-shader-compile.c
blob: b04c1b2437bd74580bd2a40efd781e7a09ed2ee5 (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
/*
 * Copyright (c) 2012 Mathias Fröhlich
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

/**
 * @file glx-multithread-shader-compile.c
 * @author Mathias Fröhlich
 *
 * Create two GLX contexts and concurrently compile shaders.
 * Exercises a race conditon with the r600 llvm compiler.
 */

#include "piglit-util-gl-common.h"
#include "piglit-glx-util.h"
#include "pthread.h"

static const char *vert_shader_text =
   "void main() \n"
   "{ \n"
   "   gl_Position = ftransform(); \n"
   "   gl_FrontColor = gl_Color; \n"
   "} \n";

static const char *frag_shader_text =
   "void main() \n"
   "{ \n"
   "   gl_FragColor = vec4(1.0) - gl_Color; \n"
   "} \n";

static pthread_mutex_t mutex;

static void *
thread_func(void *arg)
{
	Display *dpy;
	XVisualInfo *visinfo;
	Window win;
	unsigned i;

	dpy = piglit_get_glx_display();
	visinfo = piglit_get_glx_visual(dpy);
	win = piglit_get_glx_window(dpy, visinfo);

	for (i = 0; i < 100; ++i) {
		GLXContext ctx;
		GLuint vert_shader, frag_shader;
		GLuint program;

		ctx = piglit_get_glx_context(dpy, visinfo);
		glXMakeCurrent(dpy, win, ctx);

		/* Ok, not nice but should be safe due to all threads working
		 * on the same type of context.
                 */
		pthread_mutex_lock(&mutex);
		piglit_dispatch_default_init();
		pthread_mutex_unlock(&mutex);

		vert_shader = piglit_compile_shader_text(GL_VERTEX_SHADER, vert_shader_text);
		piglit_check_gl_error(GL_NO_ERROR);

		frag_shader = piglit_compile_shader_text(GL_FRAGMENT_SHADER, frag_shader_text);
		piglit_check_gl_error(GL_NO_ERROR);

		program = piglit_link_simple_program(vert_shader, frag_shader);
		piglit_check_gl_error(GL_NO_ERROR);

		glUseProgram(program);
		piglit_check_gl_error(GL_NO_ERROR);

		glXDestroyContext(dpy, ctx);
	}

	return NULL;
}

int
main(int argc, char **argv)
{
	int ret;
	pthread_t thread1, thread2;

	pthread_mutex_init(&mutex, NULL);

	/* Now, spawn some threads that compile simple shaders.
	 */
	pthread_create(&thread1, NULL, thread_func, NULL);
	pthread_create(&thread2, NULL, thread_func, NULL);

	ret = pthread_join(thread1, NULL);
	assert(ret == 0);
	ret = pthread_join(thread2, NULL);
	assert(ret == 0);

	pthread_mutex_destroy(&mutex);

	piglit_report_result(PIGLIT_PASS);
	return 0;
}