perf/pixel-rate: new pixel throughput microbenchmark
[piglit.git] / tests / shaders / glsl-fwidth.c
blob4f45b6bc23094c0797bab89e1a6cc7e2c7a5cc14
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-gl.h"
33 PIGLIT_GL_TEST_CONFIG_BEGIN
35 config.supports_gl_compat_version = 10;
37 config.window_width = 400;
38 config.window_height = 300;
39 config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
41 PIGLIT_GL_TEST_CONFIG_END
43 static void loadTex(void);
44 static void compileLinkProg(void);
46 static GLuint tex[1];
47 static GLint prog1;
48 static GLint vs1;
49 static GLint fs1;
50 static GLint prog2;
51 static GLint fs2;
53 static GLfloat verts[12] = {175.0, 125.0, 0.0,
54 175.0, 175.0, 0.0,
55 125.0, 125.0, 0.0,
56 125.0, 175.0, 0.0};
58 static GLfloat texCoords[8] = {1.0, 0.0,
59 1.0, 1.0,
60 0.0, 0.0,
61 0.0, 1.0};
64 static const char *vertShaderText =
65 "attribute vec2 textureCoords;\n"
66 "varying vec2 texCoords;\n"
67 "void main()\n"
68 "{ \n"
69 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
70 " texCoords = textureCoords;\n"
71 "} \n";
73 static const char *fragShaderText =
74 "uniform sampler2D tex2d;\n"
75 "varying vec2 texCoords;\n"
76 "void main()\n"
77 "{ \n"
78 " gl_FragColor = texture2D(tex2d, texCoords);\n"
79 "} \n";
81 static const char *fragShaderText2 =
82 "uniform sampler2D tex2d;\n"
83 "varying vec2 texCoords;\n"
84 "void main()\n"
85 "{ \n"
86 " gl_FragColor = vec4(fwidth(texCoords),0.0,1.0);\n"
87 "} \n";
91 void
92 piglit_init(int argc, char **argv)
94 piglit_require_gl_version(20);
96 compileLinkProg();
98 loadTex();
100 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
102 glEnable(GL_TEXTURE_2D);
103 glClearColor(0.6, 0.6, 0.6, 1.0);
106 static void
107 compileLinkProg(void)
109 GLint stat;
111 vs1 = glCreateShader(GL_VERTEX_SHADER);
112 fs1 = glCreateShader(GL_FRAGMENT_SHADER);
114 fs2 = glCreateShader(GL_FRAGMENT_SHADER);
116 glShaderSource(vs1, 1, (const GLchar **) &vertShaderText, NULL);
117 glShaderSource(fs1, 1, (const GLchar **) &fragShaderText, NULL);
118 glShaderSource(fs2, 1, (const GLchar **) &fragShaderText2, NULL);
120 glCompileShader(vs1);
121 glGetShaderiv(vs1, GL_COMPILE_STATUS, &stat);
122 if (!stat) {
123 printf("error compiling vertex shader1!\n");
124 exit(1);
127 glCompileShader(fs1);
128 glGetShaderiv(fs1, GL_COMPILE_STATUS, &stat);
129 if (!stat) {
130 printf("error compiling fragment shader1!\n");
131 exit(1);
134 glCompileShader(fs2);
135 glGetShaderiv(fs2, GL_COMPILE_STATUS, &stat);
136 if (!stat) {
137 printf("error compiling fragment shader2!\n");
138 exit(1);
142 prog1 = glCreateProgram();
143 glAttachShader(prog1, vs1);
144 glAttachShader(prog1, fs1);
145 glBindAttribLocation(prog1, 1, "textureCoords");
146 glLinkProgram(prog1);
147 glUseProgram(prog1);
149 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat),
150 verts);
151 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2*sizeof(GLfloat),
152 texCoords);
153 glEnableVertexAttribArray(0);
154 glEnableVertexAttribArray(1);
157 prog2 = glCreateProgram();
158 glAttachShader(prog2, vs1);
159 glAttachShader(prog2, fs2);
160 glBindAttribLocation(prog2, 1, "textureCoords");
161 glLinkProgram(prog2);
162 glUseProgram(prog2);
164 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat),
165 verts);
166 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2*sizeof(GLfloat),
167 texCoords);
168 glEnableVertexAttribArray(0);
169 glEnableVertexAttribArray(1);
172 static void
173 loadTex(void)
175 #define height 2
176 #define width 2
177 int i, j;
179 GLfloat texData[width][height][4];
180 for (i=0; i < width; ++i) {
181 for (j=0; j < height; ++j) {
182 if ((i+j) & 1) {
183 texData[i][j][0] = 1;
184 texData[i][j][1] = 0;
185 texData[i][j][2] = 1;
186 texData[i][j][3] = 0;
188 else {
189 texData[i][j][0] = 0;
190 texData[i][j][1] = 1;
191 texData[i][j][2] = 0;
192 texData[i][j][3] = 1;
197 glGenTextures(1, tex);
198 glActiveTexture(GL_TEXTURE0);
199 glBindTexture(GL_TEXTURE_2D, tex[0]);
200 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE);
201 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
202 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
203 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
204 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
205 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
206 GL_RGBA, GL_FLOAT, texData);
208 #undef height
209 #undef width
213 enum piglit_result
214 piglit_display(void)
216 GLboolean pass = GL_TRUE;
218 float mostlyBlack[3] = {0.019608, 0.019608, 0.0};
219 float green[3] = {0, 1, 0};
221 glClear(GL_COLOR_BUFFER_BIT);
223 glPushMatrix();
225 glUseProgram(prog1);
226 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
228 glTranslatef(75.0, 0.0, 0.0);
230 glUseProgram(prog2);
231 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
233 glPopMatrix();
235 pass = pass && piglit_probe_pixel_rgb(132, 125, green);
236 pass = pass && piglit_probe_pixel_rgb(205, 125, mostlyBlack);
238 glFinish();
239 piglit_present_results();
241 return pass ? PIGLIT_PASS : PIGLIT_FAIL;