ovr_multiview: add some basic glsl tests
[piglit.git] / tests / spec / arb_bindless_texture / limit.c
blobd27018a3b1a7ba0052a5cc2f31a5acbfff00531c
1 /*
2 * Copyright (C) 2017 Valve 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 * Marek Olšák <maraeo@gmail.com>
25 * Samuel Pitoiset <samuel.pitoiset@gmail.com>
28 /**
29 * Test that samplers accessed using texture handles are not counted against
30 * the texture limits.
31 * Derived from Marek's max-samplers test.
34 #include "common.h"
36 PIGLIT_GL_TEST_CONFIG_BEGIN
38 config.supports_gl_compat_version = 33;
39 config.supports_gl_core_version = 33;
41 config.window_width = 300;
42 config.window_height = 300;
43 config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
44 config.khr_no_error_support = PIGLIT_NO_ERRORS;
46 PIGLIT_GL_TEST_CONFIG_END
48 static const char *vs_source =
49 "#version 330\n"
50 "#extension GL_ARB_bindless_texture: require\n"
51 "\n"
52 "#define NUM %i \n"
53 "layout (bindless_sampler) uniform;\n"
54 "\n"
55 "uniform sampler2D vertex_tex[NUM]; \n"
56 "uniform int vertex_index;"
57 "in vec4 piglit_vertex;\n"
58 "out vec3 vertex_tex_color; \n"
59 "\n"
60 "void main() \n"
61 "{ \n"
62 " int i; \n"
63 " gl_Position = piglit_vertex;\n"
64 " vertex_tex_color = vec3(0.0); \n"
65 " for (i = 0; i < NUM; i++) \n"
66 " if (i == vertex_index) \n"
67 " vertex_tex_color = textureLod(vertex_tex[i], vec2(%f), 0.0).xyz; \n"
68 "} \n";
70 static const char *fs_source =
71 "#version 330\n"
72 "#extension GL_ARB_bindless_texture: require\n"
73 "\n"
74 "#define NUM %i \n"
75 "layout (bindless_sampler) uniform;\n"
76 "\n"
77 "uniform sampler2D fragment_tex[NUM]; \n"
78 "uniform int fragment_index;"
79 "in vec3 vertex_tex_color; \n"
80 "void main() \n"
81 "{ \n"
82 " int i; \n"
83 " vec3 fragment_tex_color = vec3(0.0); \n"
84 " for (i = 0; i < NUM; i++) \n"
85 " if (i == fragment_index) \n"
86 " fragment_tex_color = texture2D(fragment_tex[i], vec2(%f), 0.0).xyz; \n"
87 " gl_FragColor = vec4(fragment_tex_color + vertex_tex_color, 1.0); \n"
88 "} \n";
90 GLuint prog;
91 static int max_vs_textures, max_fs_textures;
93 static void
94 get_texture_color(int unit, float out[4])
96 out[0] = (unit % 16) / 15.0;
97 out[1] = (unit / 16) / 15.0;
98 out[2] = 0;
99 out[3] = 1;
102 static void
103 set_uniform(GLuint prog, const char *name, int value)
105 GLuint loc = glGetUniformLocation(prog, name);
106 if (loc != -1)
107 glUniform1i(loc, value);
110 static GLvoid
111 draw_rect_core(int ix, int iy, int iw, int ih)
113 float x = -1 + 2.0*ix/piglit_width;
114 float y = -1 + 2.0*iy/piglit_height;
115 float w = 2.0*iw/piglit_width;
116 float h = 2.0*ih/piglit_height;
117 float verts[4][4];
118 GLuint vbo;
120 verts[0][0] = x;
121 verts[0][1] = y;
122 verts[0][2] = 0.0;
123 verts[0][3] = 1.0;
124 verts[1][0] = x + w;
125 verts[1][1] = y;
126 verts[1][2] = 0.0;
127 verts[1][3] = 1.0;
128 verts[2][0] = x + w;
129 verts[2][1] = y + h;
130 verts[2][2] = 0.0;
131 verts[2][3] = 1.0;
132 verts[3][0] = x;
133 verts[3][1] = y + h;
134 verts[3][2] = 0.0;
135 verts[3][3] = 1.0;
137 glGenBuffers(1, &vbo);
138 glBindBuffer(GL_ARRAY_BUFFER, vbo);
139 glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
141 glEnableVertexAttribArray(0);
142 glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL);
144 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
146 glDisableVertexAttribArray(0);
147 glDeleteBuffers(1, &vbo);
150 static GLboolean
151 probe_pixel(int num, int x, int y)
153 float expected[4];
155 get_texture_color(num, expected);
157 if (piglit_probe_pixel_rgb(x, y, expected))
158 return GL_TRUE;
160 printf(" When testing texture num %i\n", num);
161 return GL_FALSE;
164 enum piglit_result
165 piglit_display(void)
167 GLboolean pass = GL_TRUE;
168 int i, num, x, y;
170 glClear(GL_COLOR_BUFFER_BIT);
172 x = 0;
173 y = 0;
174 num = 0;
176 set_uniform(prog, "fragment_index", max_fs_textures);
177 for (i = 0; i < max_vs_textures; i++) {
178 set_uniform(prog, "vertex_index", i);
179 draw_rect_core(x, y, 20, 20);
180 pass = probe_pixel(num, x+10, y+10) && pass;
182 num++;
183 x += 20;
184 if (x+20 > piglit_width) {
185 x = 0;
186 y += 20;
190 set_uniform(prog, "vertex_index", max_vs_textures);
191 for (i = 0; i < max_fs_textures; i++) {
192 set_uniform(prog, "fragment_index", i);
193 draw_rect_core(x, y, 20, 20);
194 pass = probe_pixel(num, x+10, y+10) && pass;
196 num++;
197 x += 20;
198 if (x+20 > piglit_width) {
199 x = 0;
200 y += 20;
204 piglit_check_gl_error(GL_NO_ERROR);
205 piglit_present_results();
207 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
210 static void
211 set_texture_handle(GLuint prog, const char *name, GLuint64 handle)
213 GLint loc;
215 loc = glGetUniformLocation(prog, name);
216 if (loc != -1)
217 glUniformHandleui64vARB(loc, 1, &handle);
220 static GLuint64
221 new_bindless_texture(int idx)
223 GLuint64 handle;
224 float color[4];
225 GLuint tex;
227 get_texture_color(idx, color);
229 glGenTextures(1, &tex);
230 glBindTexture(GL_TEXTURE_2D, tex);
231 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
232 GL_RGBA, GL_FLOAT, color);
233 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
234 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
235 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
236 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
237 glBindTexture(GL_TEXTURE_2D, 0);
239 handle = glGetTextureHandleARB(tex);
240 glMakeTextureHandleResidentARB(handle);
242 return handle;
245 void
246 piglit_init(int argc, char **argv)
248 GLuint vs, fs, vao;
249 int max_combined_textures, i, num;
250 char str[2048];
251 float texcoord = 0.5;
252 GLuint64 handle;
254 piglit_require_extension("GL_ARB_bindless_texture");
256 /* get limits */
257 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_fs_textures);
258 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &max_vs_textures);
259 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_textures);
260 printf("GL_MAX_TEXTURE_IMAGE_UNITS = %d\n", max_fs_textures);
261 printf("GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS = %d\n", max_vs_textures);
262 printf("GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS = %d\n", max_combined_textures);
264 assert(max_fs_textures <= max_combined_textures);
266 /* use max_combined_textures + max_vs_textures */
267 max_vs_textures = MIN2(max_vs_textures, max_combined_textures - max_fs_textures);
268 max_fs_textures = max_combined_textures;
270 /* compile shaders */
271 sprintf(str, vs_source, max_vs_textures, texcoord);
272 vs = piglit_compile_shader_text(GL_VERTEX_SHADER, str);
273 sprintf(str, fs_source, max_fs_textures, texcoord);
274 fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, str);
276 prog = piglit_link_simple_program(vs, fs);
277 glUseProgram(prog);
279 /* initialize resident textures */
280 num = 0;
281 for (i = 0; i < max_vs_textures; i++) {
282 char name[64];
283 sprintf(name, "vertex_tex[%i]", i);
284 handle = new_bindless_texture(num);
285 set_texture_handle(prog, name, handle);
286 num++;
289 for (i = 0; i < max_fs_textures; i++) {
290 char name[64];
291 sprintf(name, "fragment_tex[%i]", i);
292 handle = new_bindless_texture(num);
293 set_texture_handle(prog, name, handle);
294 num++;
297 piglit_check_gl_error(GL_NO_ERROR);
299 glClearColor(0.0, 0.0, 1.0, 1.0);
301 glGenVertexArrays(1, &vao);
302 glBindVertexArray(vao);
304 piglit_check_gl_error(GL_NO_ERROR);