glsl-derivs.c 5.91 KB
Newer Older
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
/*
 * Copyright © 2009 Intel Corporation
 *
 * 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.
 */

// author: Ben Holmes

/*
 * This test uses the built-in glsl derivative functions (dFdx and dFdy)
 * on varyings after other math has been used on them.
 */

#include "piglit-util.h"

33 34 35 36 37 38
static void compileLinkProg(void);
static void loadTex(void);

int piglit_width = 400, piglit_height = 300;
int piglit_window_mode = GLUT_RGB | GLUT_DOUBLE;

39 40 41 42 43 44 45 46 47
static GLuint tex[1];
static GLint prog1;
static GLint vs1;
static GLint fs1;
static GLint prog2;
static GLint fs2;


static GLfloat verts[12] = {175.0, 125.0, 0.0,
48 49 50
			    175.0, 225.0, 0.0,
			    125.0, 125.0, 0.0,
			    125.0, 225.0, 0.0};
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

static GLfloat texCoords[8] = {1.0, 0.0,
				1.0, 1.0,
				0.0, 0.0,
				0.0, 1.0};


static const char *vertShaderText =
	"attribute vec2 textureCoords;\n"
	"varying vec2 texCoords;\n"
	"void main()\n"
	"{ \n"
	"	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
	"	texCoords = textureCoords;\n"
	"} \n";

static const char *fragShaderText =
	"uniform sampler2D tex2d;\n"
	"varying vec2 texCoords;\n"
	"void main()\n"
	"{ \n"
	"	gl_FragColor = texture2D(tex2d, texCoords);\n"
	"} \n";

static const char *fragShaderText2 =
	"uniform sampler2D tex2d;\n"
	"varying vec2 texCoords;\n"
	"void main()\n"
	"{ \n"
Vinson Lee's avatar
Vinson Lee committed
80
	" vec2 num = texCoords * 40.0;\n"
81 82 83 84 85
	" gl_FragColor = vec4(dFdx(num.x), dFdy(num.y),0.0,1.0);\n"
	"} \n";



86 87
void
piglit_init(int argc, char **argv)
88
{
Paul Berry's avatar
Paul Berry committed
89
	if (piglit_get_gl_version() < 20) {
90 91 92
		printf("Requires OpenGL 2.0\n");
		piglit_report_result(PIGLIT_SKIP);
	}
93

94 95 96 97 98
	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);

	compileLinkProg();

	loadTex();
99 100 101 102 103 104

	glEnable(GL_TEXTURE_2D);
	glClearColor(0.6, 0.6, 0.6, 1.0);
}

static void
Vinson Lee's avatar
Vinson Lee committed
105
compileLinkProg(void)
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
{
	GLint stat;

	vs1 = glCreateShader(GL_VERTEX_SHADER);
	fs1 = glCreateShader(GL_FRAGMENT_SHADER);

	fs2 = glCreateShader(GL_FRAGMENT_SHADER);

	glShaderSource(vs1, 1, (const GLchar **) &vertShaderText, NULL);
	glShaderSource(fs1, 1, (const GLchar **) &fragShaderText, NULL);
	glShaderSource(fs2, 1, (const GLchar **) &fragShaderText2, NULL);

	glCompileShader(vs1);
	glGetShaderiv(vs1, GL_COMPILE_STATUS, &stat);
	if (!stat) {
                printf("error compiling vertex shader1!\n");
                exit(1);
        }

	glCompileShader(fs1);
	glGetShaderiv(fs1, GL_COMPILE_STATUS, &stat);
	if (!stat) {
		printf("error compiling fragment shader1!\n");
		exit(1);
	}

	glCompileShader(fs2);
	glGetShaderiv(fs2, GL_COMPILE_STATUS, &stat);
	if (!stat) {
		printf("error compiling fragment shader2!\n");
		exit(1);
	}


	prog1 = glCreateProgram();
	glAttachShader(prog1, vs1);
	glAttachShader(prog1, fs1);
	glBindAttribLocation(prog1, 1, "textureCoords");
	glLinkProgram(prog1);
	glUseProgram(prog1);

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat),
                                verts);
	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2*sizeof(GLfloat),
				texCoords);
	glEnableVertexAttribArray(0);
	glEnableVertexAttribArray(1);


	prog2 = glCreateProgram();
	glAttachShader(prog2, vs1);
	glAttachShader(prog2, fs2);
	glBindAttribLocation(prog2, 1, "textureCoords");
	glLinkProgram(prog2);
	glUseProgram(prog2);

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat),
                               verts);
	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2*sizeof(GLfloat),
				texCoords);
	glEnableVertexAttribArray(0);
	glEnableVertexAttribArray(1);
}

static void
Vinson Lee's avatar
Vinson Lee committed
171
loadTex(void)
172
{
173 174
	#define height 2
	#define width 2
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
	int i, j;

	GLfloat texData[width][height][4];
	for (i=0; i < width; ++i) {
		for (j=0; j < height; ++j) {
			if ((i+j) & 1) {
				texData[i][j][0] = 1;
				texData[i][j][1] = 0;
				texData[i][j][2] = 1;
				texData[i][j][3] = 0;
			}
			else {
				texData[i][j][0] = 0;
				texData[i][j][1] = 1;
				texData[i][j][2] = 0;
				texData[i][j][3] = 1;
			}
		}
	}

	glGenTextures(1, tex);
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, tex[0]);
	glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
			GL_RGBA, GL_FLOAT, texData);

206 207
	#undef height
	#undef width
208 209 210
}


211 212
enum piglit_result
piglit_display(void)
213
{
214 215 216 217
	GLboolean pass = GL_TRUE;

	float deriv[3] = {0.8, 0.4, 0.0};
	float green[3] = {0, 1, 0};
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

	glClear(GL_COLOR_BUFFER_BIT);

	glPushMatrix();

	glUseProgram(prog1);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

	glTranslatef(75.0, 0.0, 0.0);

	glUseProgram(prog2);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

	glPopMatrix();

	pass = pass && piglit_probe_pixel_rgb(132, 125, green);
234
	pass = pass && piglit_probe_pixel_rgb(205, 125, deriv);
235 236 237 238

	glFinish();
	glutSwapBuffers();

239
	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
240 241
}