cmake: move defaults into the per-platform section
[piglit.git] / tests / shaders / glsl-fs-texturecube.c
blob3e3a4ed7a80a38389f0b190f00df23c92c7f3f78
1 /*
2 * Copyright © 2010 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 DEALINGS
21 * IN THE SOFTWARE.
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
28 /** @file glsl-fs-texturecube.c
30 * Tests that we can access cubemaps in the fragment shader.
33 #include "piglit-util-gl.h"
35 PIGLIT_GL_TEST_CONFIG_BEGIN
37 config.supports_gl_compat_version = 10;
39 config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE;
41 PIGLIT_GL_TEST_CONFIG_END
43 static GLint prog;
45 static GLfloat colors[][3] = {
46 {1.0, 1.0, 1.0},
47 {1.0, 1.0, 0.0},
48 {1.0, 0.0, 0.0},
49 {1.0, 0.0, 1.0},
50 {0.0, 0.0, 1.0},
51 {0.0, 1.0, 1.0},
52 {0.0, 1.0, 0.0},
56 static void
57 set_face_image(GLenum face, int size, int color)
59 GLfloat *color1 = colors[color];
60 GLfloat *color2 = colors[(color + 1) % ARRAY_SIZE(colors)];
61 GLfloat *tex;
62 int x, y;
64 tex = malloc(size * size * 3 * sizeof(GLfloat));
66 /* Set the texture for this face to one corner being color2 and the
67 * rest color1. If the texture is 1x1, then it's all color1.
69 for (y = 0; y < size; y++) {
70 for (x = 0; x < size; x++) {
71 GLfloat *chosen_color;
73 if (y >= (size / 2) || x >= (size / 2))
74 chosen_color = color1;
75 else
76 chosen_color = color2;
78 tex[(y * size + x) * 3 + 0] = chosen_color[0];
79 tex[(y * size + x) * 3 + 1] = chosen_color[1];
80 tex[(y * size + x) * 3 + 2] = chosen_color[2];
84 glTexImage2D(face, 0, GL_RGB, size, size, 0, GL_RGB, GL_FLOAT, tex);
86 free(tex);
90 enum piglit_result
91 piglit_display(void)
93 GLboolean pass = GL_TRUE;
94 GLuint tex;
95 int face;
97 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
99 /* Create the texture. */
100 glGenTextures(1, &tex);
101 glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, tex);
103 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB,
104 GL_TEXTURE_MIN_FILTER, GL_NEAREST);
105 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB,
106 GL_TEXTURE_MAG_FILTER, GL_NEAREST);
107 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB,
108 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
109 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB,
110 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
112 /* Fill in faces on each level */
113 for (face = 0; face < 6; face++) {
114 set_face_image(cube_face_targets[face], 32, face);
117 glEnable(GL_TEXTURE_CUBE_MAP_ARB);
119 for (face = 0; face < 6; face++) {
120 int x1 = piglit_width * face / 6;
121 int x2 = piglit_width * (face + 1) / 6;
122 int y1 = 0;
123 int y2 = piglit_height;
124 int tx1 = x1 + 1;
125 int tx2 = x2 - 1;
126 int ty1 = y1 + 1;
127 int ty2 = y2 - 1;
128 GLfloat *color1 = colors[face];
129 GLfloat *color2 = colors[face + 1];
131 glBegin(GL_QUADS);
133 glTexCoord3fv(cube_face_texcoords[face][0]);
134 glVertex2f(x1, y1);
136 glTexCoord3fv(cube_face_texcoords[face][1]);
137 glVertex2f(x2, y1);
139 glTexCoord3fv(cube_face_texcoords[face][2]);
140 glVertex2f(x2, y2);
142 glTexCoord3fv(cube_face_texcoords[face][3]);
143 glVertex2f(x1, y2);
145 glEnd();
147 pass = pass && piglit_probe_pixel_rgb(tx1, ty1, color2);
148 pass = pass && piglit_probe_pixel_rgb(tx2, ty1, color1);
149 pass = pass && piglit_probe_pixel_rgb(tx1, ty2, color1);
150 pass = pass && piglit_probe_pixel_rgb(tx2, ty2, color1);
153 glDeleteTextures(1, &tex);
155 piglit_present_results();
157 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
160 void piglit_init(int argc, char **argv)
162 GLint vs, fs;
163 int loc;
164 GLboolean bias = GL_FALSE;
165 char *fs_name;
166 int i = 0;
168 for (i = 0; i < argc; i++) {
169 if (!strcmp(argv[i], "-bias"))
170 bias = GL_TRUE;
173 piglit_require_gl_version(20);
174 piglit_require_extension("GL_ARB_texture_cube_map");
176 vs = piglit_compile_shader(GL_VERTEX_SHADER,
177 "shaders/glsl-tex-mvp.vert");
178 if (bias) {
179 fs_name = "shaders/glsl-fs-texturecube-bias.frag";
180 } else {
181 fs_name = "shaders/glsl-fs-texturecube.frag";
183 fs = piglit_compile_shader(GL_FRAGMENT_SHADER, fs_name);
185 prog = piglit_link_simple_program(vs, fs);
186 if (!piglit_link_check_status(prog))
187 piglit_report_result(PIGLIT_FAIL);
189 glUseProgram(prog);
191 loc = glGetUniformLocation(prog, "sampler");
192 glUniform1i(loc, 0);