Add more structure constructor tests.
[piglit/hramrach.git] / tests / shaders / glsl-fwidth.c
blobb3553add5115e8768d2fc577fc90bf42e72d7814
1 /*
2 * Copyright © 2009 Intel Corporation
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
21 * DEALINGS IN THE SOFTWARE.
24 // author: Ben Holmes
27 * This test uses the built-in glsl function fwidth.
31 #include "piglit-util.h"
33 int piglit_width = 400, piglit_height = 300;
34 int piglit_window_mode = GLUT_RGB | GLUT_DOUBLE;
36 static void loadTex(void);
37 static void compileLinkProg(void);
39 static GLuint tex[1];
40 static GLint prog1;
41 static GLint vs1;
42 static GLint fs1;
43 static GLint prog2;
44 static GLint fs2;
46 static GLfloat verts[12] = {175.0, 125.0, 0.0,
47 175.0, 175.0, 0.0,
48 125.0, 125.0, 0.0,
49 125.0, 175.0, 0.0};
51 static GLfloat texCoords[8] = {1.0, 0.0,
52 1.0, 1.0,
53 0.0, 0.0,
54 0.0, 1.0};
57 static const char *vertShaderText =
58 "attribute vec2 textureCoords;\n"
59 "varying vec2 texCoords;\n"
60 "void main()\n"
61 "{ \n"
62 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
63 " texCoords = textureCoords;\n"
64 "} \n";
66 static const char *fragShaderText =
67 "uniform sampler2D tex2d;\n"
68 "varying vec2 texCoords;\n"
69 "void main()\n"
70 "{ \n"
71 " gl_FragColor = texture2D(tex2d, texCoords);\n"
72 "} \n";
74 static const char *fragShaderText2 =
75 "uniform sampler2D tex2d;\n"
76 "varying vec2 texCoords;\n"
77 "void main()\n"
78 "{ \n"
79 " gl_FragColor = vec4(fwidth(texCoords),0.0,1.0);\n"
80 "} \n";
84 void
85 piglit_init(int argc, char **argv)
87 if (!GLEW_VERSION_2_0) {
88 printf("Requires OpenGL 2.0\n");
89 piglit_report_result(PIGLIT_SKIP);
92 compileLinkProg();
94 loadTex();
96 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
98 glEnable(GL_TEXTURE_2D);
99 glClearColor(0.6, 0.6, 0.6, 1.0);
102 static void
103 compileLinkProg(void)
105 GLint stat;
107 vs1 = glCreateShader(GL_VERTEX_SHADER);
108 fs1 = glCreateShader(GL_FRAGMENT_SHADER);
110 fs2 = glCreateShader(GL_FRAGMENT_SHADER);
112 glShaderSource(vs1, 1, (const GLchar **) &vertShaderText, NULL);
113 glShaderSource(fs1, 1, (const GLchar **) &fragShaderText, NULL);
114 glShaderSource(fs2, 1, (const GLchar **) &fragShaderText2, NULL);
116 glCompileShader(vs1);
117 glGetShaderiv(vs1, GL_COMPILE_STATUS, &stat);
118 if (!stat) {
119 printf("error compiling vertex shader1!\n");
120 exit(1);
123 glCompileShader(fs1);
124 glGetShaderiv(fs1, GL_COMPILE_STATUS, &stat);
125 if (!stat) {
126 printf("error compiling fragment shader1!\n");
127 exit(1);
130 glCompileShader(fs2);
131 glGetShaderiv(fs2, GL_COMPILE_STATUS, &stat);
132 if (!stat) {
133 printf("error compiling fragment shader2!\n");
134 exit(1);
138 prog1 = glCreateProgram();
139 glAttachShader(prog1, vs1);
140 glAttachShader(prog1, fs1);
141 glBindAttribLocation(prog1, 1, "textureCoords");
142 glLinkProgram(prog1);
143 glUseProgram(prog1);
145 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat),
146 verts);
147 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2*sizeof(GLfloat),
148 texCoords);
149 glEnableVertexAttribArray(0);
150 glEnableVertexAttribArray(1);
153 prog2 = glCreateProgram();
154 glAttachShader(prog2, vs1);
155 glAttachShader(prog2, fs2);
156 glBindAttribLocation(prog2, 1, "textureCoords");
157 glLinkProgram(prog2);
158 glUseProgram(prog2);
160 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat),
161 verts);
162 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2*sizeof(GLfloat),
163 texCoords);
164 glEnableVertexAttribArray(0);
165 glEnableVertexAttribArray(1);
168 static void
169 loadTex(void)
171 #define height 2
172 #define width 2
173 int i, j;
175 GLfloat texData[width][height][4];
176 for (i=0; i < width; ++i) {
177 for (j=0; j < height; ++j) {
178 if ((i+j) & 1) {
179 texData[i][j][0] = 1;
180 texData[i][j][1] = 0;
181 texData[i][j][2] = 1;
182 texData[i][j][3] = 0;
184 else {
185 texData[i][j][0] = 0;
186 texData[i][j][1] = 1;
187 texData[i][j][2] = 0;
188 texData[i][j][3] = 1;
193 glGenTextures(1, tex);
194 glActiveTexture(GL_TEXTURE0);
195 glBindTexture(GL_TEXTURE_2D, tex[0]);
196 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE);
197 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
198 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
199 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
200 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
201 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
202 GL_RGBA, GL_FLOAT, texData);
204 #undef height
205 #undef width
209 enum piglit_result
210 piglit_display(void)
212 GLboolean pass = GL_TRUE;
214 float mostlyBlack[3] = {0.019608, 0.019608, 0.0};
215 float green[3] = {0, 1, 0};
217 glClear(GL_COLOR_BUFFER_BIT);
219 glPushMatrix();
221 glUseProgram(prog1);
222 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
224 glTranslatef(75.0, 0.0, 0.0);
226 glUseProgram(prog2);
227 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
229 glPopMatrix();
231 pass = pass && piglit_probe_pixel_rgb(132, 125, green);
232 pass = pass && piglit_probe_pixel_rgb(205, 125, mostlyBlack);
234 glFinish();
235 glutSwapBuffers();
237 return pass ? PIGLIT_SUCCESS : PIGLIT_FAILURE;