Add more structure constructor tests.
[piglit/hramrach.git] / tests / shaders / glsl-reload-source.c
blobce33b10e360264821535e4b58c0933f70377a524
1 /*
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
13 * Software.
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
21 * IN THE SOFTWARE.
23 * Authors:
24 * Nicolai Hähnle <nhaehnle@gmail.com>
28 /**
29 * \file
30 * Test that reloading shader source and relinking programs works correctly.
33 #include <stdio.h>
35 #include "piglit-util.h"
36 #include "piglit-framework.h"
38 #ifndef APIENTRY
39 #define APIENTRY
40 #endif
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"
47 "void main() {\n"
48 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
49 " color = vec4(0.0, 0.4, 0.0, 1.0);\n"
50 "}\n";
52 static const char vs_two[] =
53 "varying vec4 color;\n"
54 "void main() {\n"
55 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
56 " color = vec4(0.4, 0.4, 0.0, 1.0);\n"
57 "}\n";
59 static const char fs_one[] =
60 "varying vec4 color;\n"
61 "void main() {\n"
62 " gl_FragColor = color;\n"
63 "}\n";
65 static const char fs_two[] =
66 "varying vec4 color;\n"
67 "void main() {\n"
68 " gl_FragColor = color + vec4(0.4, 0.0, 0.4, 0.0);\n"
69 "}\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 };
76 static GLuint vs;
77 static GLuint fs;
78 static GLuint program;
81 static void compile_shader(GLuint shader, const char * text)
83 GLint status;
85 glShaderSourceARB(shader, 1, (const GLchar **)&text, NULL);
86 glCompileShaderARB(shader);
88 glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
89 if (!status) {
90 GLchar log[1000];
91 GLsizei len;
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)
100 GLint status;
102 glLinkProgramARB(program);
103 glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &status);
104 if (!status) {
105 GLchar log[1000];
106 GLsizei len;
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);
122 enum piglit_result
123 piglit_display(void)
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;
151 return result;
154 void
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);