ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_clear_texture / cube.c
blob0e6003943ba7b34318837250e7656af85aadd7d0
1 /*
2 * Copyright (c) 2014 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.
24 /** @file cube.c
26 * A test of using glClearTexSubImage to clear faces of a cube
27 * texture. Each face is cleared to a separate color and then all of
28 * the faces are rendered and probed.
31 #include "piglit-util-gl.h"
33 struct vertex {
34 float pos;
35 float tex_coord[3];
38 struct face {
39 /* Color used for this face */
40 float color[3];
41 /* Texture coordinates needed to access this face */
42 float tex_coord[3];
44 GLenum target;
47 static const struct face
48 faces[] = {
49 { { 0.0f, 0.0f, 1.0f }, { +1.0f, 0.0f, 0.0f },
50 GL_TEXTURE_CUBE_MAP_POSITIVE_X },
51 { { 0.0f, 1.0f, 0.0f }, { -1.0f, 0.0f, 0.0f },
52 GL_TEXTURE_CUBE_MAP_NEGATIVE_X },
53 { { 0.0f, 1.0f, 1.0f }, { 0.0f, +1.0f, 0.0f },
54 GL_TEXTURE_CUBE_MAP_POSITIVE_Y },
55 { { 1.0f, 0.0f, 0.0f }, { 0.0f, -1.0f, 0.0f },
56 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y },
57 { { 1.0f, 0.0f, 1.0f }, { 0.0f, 0.0f, +1.0f },
58 GL_TEXTURE_CUBE_MAP_POSITIVE_Z },
59 { { 1.0f, 1.0f, 0.0f }, { 0.0f, 0.0f, -1.0f },
60 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z },
63 PIGLIT_GL_TEST_CONFIG_BEGIN
65 config.supports_gl_compat_version = 20;
66 config.supports_gl_es_version = 20;
68 config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
69 config.khr_no_error_support = PIGLIT_NO_ERRORS;
71 PIGLIT_GL_TEST_CONFIG_END
73 static GLuint
74 create_texture(void)
76 GLuint tex;
77 int i;
79 glGenTextures(1, &tex);
80 glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
82 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
84 for (i = 0; i < 6; i++) {
85 glTexImage2D(faces[i].target,
86 0, /* level */
87 GL_RGB,
88 1, 1, /* width/height */
89 0, /* border */
90 GL_RGB,
91 GL_UNSIGNED_BYTE,
92 NULL);
95 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
96 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
98 if (!piglit_check_gl_error(GL_NO_ERROR))
99 piglit_report_result(PIGLIT_FAIL);
101 return tex;
104 static void
105 clear_texture(GLuint tex)
107 int i;
109 for (i = 0; i < 6; i++) {
110 glClearTexSubImage(tex,
111 0, /* level */
112 0, 0, i, /* x/y/z */
113 1, 1, 1, /* width/height/depth */
114 GL_RGB,
115 GL_FLOAT,
116 faces[i].color);
119 if (!piglit_check_gl_error(GL_NO_ERROR))
120 piglit_report_result(PIGLIT_FAIL);
123 static void
124 init_program(void)
126 GLuint prog;
127 GLuint uniform;
129 static const char vs_source[] =
130 "attribute float piglit_vertex;\n"
131 "attribute vec3 piglit_texcoord;\n"
132 "uniform vec2 fb_size;\n"
133 "varying vec3 tex_coord;\n"
134 "\n"
135 "void main()\n"
136 "{\n"
137 " gl_Position = vec4(vec2(piglit_vertex, 0.5) * 2.0 /\n"
138 " fb_size - 1.0,\n"
139 " 0.0, 1.0);\n"
140 " tex_coord = piglit_texcoord;\n"
141 "}\n";
142 static const char fs_source[] =
143 "uniform samplerCube tex;\n"
144 "varying vec3 tex_coord;\n"
145 "\n"
146 "void main()\n"
147 "{\n"
148 " gl_FragColor = textureCube(tex, tex_coord);\n"
149 "}\n";
151 prog = piglit_build_simple_program(vs_source, fs_source);
153 glUseProgram(prog);
155 uniform = glGetUniformLocation(prog, "tex");
156 glUniform1i(uniform, 0);
158 uniform = glGetUniformLocation(prog, "fb_size");
159 glUniform2f(uniform, piglit_width, piglit_height);
162 void
163 piglit_init(int argc, char **argv)
165 /* glClearTexture is either in the GL_ARB_clear_texture
166 * extension or in core in GL 4.4
168 if (piglit_get_gl_version() < 44 &&
169 !piglit_is_extension_supported("GL_ARB_clear_texture")) {
170 printf("OpenGL 4.4 or GL_ARB_clear_texture is required.\n");
171 piglit_report_result(PIGLIT_SKIP);
174 init_program();
177 static void
178 draw_faces(void)
180 struct vertex vertices[6];
181 int i;
183 for (i = 0; i < 6; i++) {
184 vertices[i].pos = i + 0.5f;
185 memcpy(vertices[i].tex_coord,
186 faces[i].tex_coord,
187 sizeof faces[i].tex_coord);
190 glEnableVertexAttribArray(PIGLIT_ATTRIB_POS);
191 glVertexAttribPointer(PIGLIT_ATTRIB_POS,
192 1, /* size */
193 GL_FLOAT,
194 GL_FALSE, /* normalized */
195 sizeof vertices[0],
196 &vertices[0].pos);
197 glEnableVertexAttribArray(PIGLIT_ATTRIB_TEX);
198 glVertexAttribPointer(PIGLIT_ATTRIB_TEX,
199 3, /* size */
200 GL_FLOAT,
201 GL_FALSE, /* normalized */
202 sizeof vertices[0],
203 vertices[0].tex_coord);
205 glDrawArrays(GL_POINTS, 0, 6);
207 glDisableVertexAttribArray(PIGLIT_ATTRIB_POS);
208 glDisableVertexAttribArray(PIGLIT_ATTRIB_TEX);
211 enum piglit_result
212 piglit_display(void)
214 bool pass = true;
215 GLuint tex;
216 int i;
218 tex = create_texture();
220 clear_texture(tex);
222 glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
224 draw_faces();
226 glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
228 glDeleteTextures(1, &tex);
230 for (i = 0; i < 6; i++)
231 pass &= piglit_probe_pixel_rgb(i, 0, faces[i].color);
233 piglit_present_results();
235 return pass ? PIGLIT_PASS : PIGLIT_FAIL;