conversion-explicit: use a different value for normalized +/- min
[piglit.git] / tests / spec / arb_texture_view / sampling-2d-array-as-2d-layer.c
blob1c219cc98460748e33b5d50310d05681f41afe66
1 /*
2 * Copyright © 2015 Glenn Kennard
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 * Author: Glenn Kennard <glenn.kennard@gmail.com>
26 /**
27 * \file sampling-2d-array-as-2d-layer.c
28 * This tests that you can cast from a 2D Array texture to a regular 2D texture
29 * with layer>0 and sample from the latter.
32 #include "piglit-util-gl.h"
34 PIGLIT_GL_TEST_CONFIG_BEGIN
35 config.supports_gl_compat_version = 30;
36 config.supports_gl_es_version = 31;
37 config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE;
38 config.khr_no_error_support = PIGLIT_NO_ERRORS;
40 PIGLIT_GL_TEST_CONFIG_END
42 static const GLubyte green[] = {0, 255, 0, 255};
43 static const float greenf[] = {0, 1.0f, 0, 1.0f};
44 static const GLubyte red[] = {255, 0, 0, 255};
46 typedef struct Params {
47 int num_layers;
48 int width;
49 int height;
50 const char * const desc;
51 } Params;
53 /* test a few size combinations that tend to require particular alignment
54 requirements by the hardware */
55 static const Params testparams[] = {
56 { 8, 1, 1, "1x1" },
57 { 3, 2, 1, "2x1" },
58 { 3, 8, 1, "8x1" },
59 { 1, 16, 1, "16x1" },
60 { 5, 1, 16, "1x16" },
61 { 9, 32, 32, "32x32" },
62 { 2, 64, 64, "64x64" },
63 { 4, 128, 64, "128x64" },
64 { 3, 35, 67, "35x67" }
67 static GLubyte *makesolidimage(int w, int h, const GLubyte color[4])
69 GLubyte *p = malloc(w * h * 4 * sizeof(GLubyte));
70 size_t n;
71 assert(p);
72 for (n = 0; n < w * h; n++) {
73 p[n*4 + 0] = color[0];
74 p[n*4 + 1] = color[1];
75 p[n*4 + 2] = color[2];
76 p[n*4 + 3] = color[3];
78 return p;
81 static bool
82 test_single_layer(const Params* p, int layer)
84 int l;
85 GLuint tex_src, tex_view;
86 GLboolean pass;
87 GLubyte *image;
89 assert(layer < p->num_layers);
91 glGenTextures(1, &tex_src);
92 glBindTexture(GL_TEXTURE_2D_ARRAY, tex_src);
94 glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGBA8, p->width, p->height, p->num_layers);
96 /* load each array layer with red */
97 image = makesolidimage(p->width, p->height, red);
98 for (l = 0; l < p->num_layers; l++) {
99 glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, l,
100 p->width, p->height, 1, GL_RGBA, GL_UNSIGNED_BYTE, image);
103 /* make layer to check red, but green for pixel at (0,0) which should be the only one sampled */
104 memcpy(image, green, sizeof(green));
106 glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, layer,
107 p->width, p->height, 1, GL_RGBA, GL_UNSIGNED_BYTE, image);
109 free(image);
111 glGenTextures(1, &tex_view);
112 /* checked layer is supposed to be green */
113 glTextureView(tex_view, GL_TEXTURE_2D, tex_src, GL_RGBA8,
114 0, 1, layer, 1);
115 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
116 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
117 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
118 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
120 glBindTexture(GL_TEXTURE_2D, tex_view);
122 /* draw it! */
123 piglit_draw_rect(-1, -1, 2, 2);
125 pass = piglit_probe_rect_rgba(0, 0, piglit_width, piglit_height, greenf);
126 if (!pass) {
127 printf("layer %d failed\n", layer);
130 glDeleteTextures(1, &tex_view);
131 glDeleteTextures(1, &tex_src);
133 return pass;
136 enum piglit_result
137 piglit_display(void)
139 GLboolean pass = GL_TRUE;
140 size_t n;
141 int layer;
143 glViewport(0, 0, piglit_width, piglit_height);
144 glClearColor(0.0, 0.0, 1.0, 1.0);
146 for (n = 0; n < ARRAY_SIZE(testparams); n++) {
147 GLboolean subtest_pass = GL_TRUE;
148 for (layer = 0; layer < testparams[n].num_layers; layer++) {
149 glClear(GL_COLOR_BUFFER_BIT);
151 subtest_pass &= test_single_layer(&testparams[n], layer);
153 piglit_report_subtest_result(subtest_pass ? PIGLIT_PASS : PIGLIT_FAIL,
154 "%s", testparams[n].desc);
155 pass &= subtest_pass;
158 piglit_present_results();
160 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
163 #ifdef PIGLIT_USE_OPENGL
164 #define GLSL_VERSION "130"
165 #else
166 #define GLSL_VERSION "310 es"
167 #endif
169 static const char *vs =
170 "#version " GLSL_VERSION "\n"
171 "in vec4 piglit_vertex;\n"
172 "void main() { \n"
173 " gl_Position = piglit_vertex;\n"
174 "}\n";
176 static const char *fs =
177 "#version " GLSL_VERSION "\n"
178 "#ifdef GL_ES\n"
179 "precision highp float;\n"
180 "precision highp sampler2D;\n"
181 "#endif\n"
182 "uniform sampler2D tex;\n"
183 "out vec4 color;\n"
184 "void main() { \n"
185 " ivec2 size = textureSize(tex, 0);\n"
186 /* texel in (0,0) should be the only green texel in the entire texture */
187 " vec2 offset = vec2(0.5/float(size.x), 0.5/float(size.y));\n"
188 " color = vec4(texture(tex, offset).xyz, 1.0);\n"
189 "}\n";
191 void
192 piglit_init(int argc, char **argv)
194 int tex_loc_view, prog_view;
196 #ifdef PIGLIT_USE_OPENGL
197 piglit_require_extension("GL_ARB_texture_view");
198 piglit_require_extension("GL_ARB_texture_storage");
199 #else
200 piglit_require_extension("GL_OES_texture_view");
201 #endif
203 /* setup shaders and program object for texture rendering */
204 prog_view = piglit_build_simple_program(vs, fs);
205 tex_loc_view = glGetUniformLocation(prog_view, "tex");
207 glUseProgram(prog_view);
208 glDeleteProgram(prog_view);
209 glUniform1i(tex_loc_view, 0);