2 * Copyright (c) 2009 Nicolai Hähnle
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Nicolai Hähnle <nhaehnle@gmail.com>
30 * Test that reloading shader source and relinking programs works correctly.
35 #include "piglit-util.h"
36 #include "piglit-framework.h"
42 int piglit_width
= 100, piglit_height
= 100;
43 int piglit_window_mode
= GLUT_RGB
;
45 static const char vs_one
[] =
46 "varying vec4 color;\n"
48 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
49 " color = vec4(0.0, 0.4, 0.0, 1.0);\n"
52 static const char vs_two
[] =
53 "varying vec4 color;\n"
55 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
56 " color = vec4(0.4, 0.4, 0.0, 1.0);\n"
59 static const char fs_one
[] =
60 "varying vec4 color;\n"
62 " gl_FragColor = color;\n"
65 static const char fs_two
[] =
66 "varying vec4 color;\n"
68 " gl_FragColor = color + vec4(0.4, 0.0, 0.4, 0.0);\n"
71 static const GLfloat expect_one_one
[3] = { 0.0, 0.4, 0.0 };
72 static const GLfloat expect_one_two
[3] = { 0.4, 0.4, 0.4 };
73 static const GLfloat expect_two_one
[3] = { 0.4, 0.4, 0.0 };
74 static const GLfloat expect_two_two
[3] = { 0.8, 0.4, 0.4 };
78 static GLuint program
;
81 static void compile_shader(GLuint shader
, const char * text
)
85 glShaderSourceARB(shader
, 1, (const GLchar
**)&text
, NULL
);
86 glCompileShaderARB(shader
);
88 glGetObjectParameterivARB(shader
, GL_OBJECT_COMPILE_STATUS_ARB
, &status
);
92 glGetInfoLogARB(shader
, 1000, &len
, log
);
93 fprintf(stderr
, "Error: problem compiling shader: %s\n", log
);
94 piglit_report_result(PIGLIT_FAILURE
);
98 static void link_program(GLuint program
)
102 glLinkProgramARB(program
);
103 glGetObjectParameterivARB(program
, GL_OBJECT_LINK_STATUS_ARB
, &status
);
107 glGetInfoLogARB(program
, 1000, &len
, log
);
108 fprintf(stderr
, "Error: problem linking program: %s\n", log
);
109 piglit_report_result(PIGLIT_FAILURE
);
113 static void setup_shaders(const char * vstext
, const char * fstext
)
115 compile_shader(vs
, vstext
);
116 compile_shader(fs
, fstext
);
117 link_program(program
);
119 glUseProgramObjectARB(program
);
125 enum piglit_result result
= PIGLIT_SUCCESS
;
127 piglit_ortho_projection(piglit_width
, piglit_height
, GL_FALSE
);
129 glClear(GL_COLOR_BUFFER_BIT
);
131 setup_shaders(vs_one
, fs_one
);
132 piglit_draw_rect(0, 0, piglit_width
/2, piglit_height
/2);
133 if (!piglit_probe_pixel_rgb(piglit_width
/4, piglit_height
/4, expect_one_one
))
134 result
= PIGLIT_FAILURE
;
136 setup_shaders(vs_one
, fs_two
);
137 piglit_draw_rect(piglit_width
/2, 0, piglit_width
/2, piglit_height
/2);
138 if (!piglit_probe_pixel_rgb(3*piglit_width
/4, piglit_height
/4, expect_one_two
))
139 result
= PIGLIT_FAILURE
;
141 setup_shaders(vs_two
, fs_one
);
142 piglit_draw_rect(0, piglit_height
/2, piglit_width
/2, piglit_height
/2);
143 if (!piglit_probe_pixel_rgb(piglit_width
/4, 3*piglit_height
/4, expect_two_one
))
144 result
= PIGLIT_FAILURE
;
146 setup_shaders(vs_two
, fs_two
);
147 piglit_draw_rect(piglit_width
/2, piglit_height
/2, piglit_width
/2, piglit_height
/2);
148 if (!piglit_probe_pixel_rgb(3*piglit_width
/4, 3*piglit_height
/4, expect_two_two
))
149 result
= PIGLIT_FAILURE
;
155 piglit_init(int argc
, char **argv
)
157 if (!GLEW_ARB_shader_objects
|| !GLEW_ARB_vertex_shader
|| !GLEW_ARB_fragment_shader
) {
158 printf("Requires ARB_shader_objects and ARB_{vertex,fragment}_shader\n");
159 piglit_report_result(PIGLIT_SKIP
);
162 vs
= glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
);
163 fs
= glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB
);
164 program
= glCreateProgramObjectARB();
165 glAttachObjectARB(program
, vs
);
166 glAttachObjectARB(program
, fs
);