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
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
24 * Marek Olšák <maraeo@gmail.com>
25 * Samuel Pitoiset <samuel.pitoiset@gmail.com>
29 * Test that samplers accessed using texture handles are not counted against
31 * Derived from Marek's max-samplers test.
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
=
50 "#extension GL_ARB_bindless_texture: require\n"
53 "layout (bindless_sampler) uniform;\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"
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"
70 static const char *fs_source
=
72 "#extension GL_ARB_bindless_texture: require\n"
75 "layout (bindless_sampler) uniform;\n"
77 "uniform sampler2D fragment_tex[NUM]; \n"
78 "uniform int fragment_index;"
79 "in vec3 vertex_tex_color; \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"
91 static int max_vs_textures
, max_fs_textures
;
94 get_texture_color(int unit
, float out
[4])
96 out
[0] = (unit
% 16) / 15.0;
97 out
[1] = (unit
/ 16) / 15.0;
103 set_uniform(GLuint prog
, const char *name
, int value
)
105 GLuint loc
= glGetUniformLocation(prog
, name
);
107 glUniform1i(loc
, value
);
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
;
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
);
151 probe_pixel(int num
, int x
, int y
)
155 get_texture_color(num
, expected
);
157 if (piglit_probe_pixel_rgb(x
, y
, expected
))
160 printf(" When testing texture num %i\n", num
);
167 GLboolean pass
= GL_TRUE
;
170 glClear(GL_COLOR_BUFFER_BIT
);
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
;
184 if (x
+20 > piglit_width
) {
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
;
198 if (x
+20 > piglit_width
) {
204 piglit_check_gl_error(GL_NO_ERROR
);
205 piglit_present_results();
207 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
211 set_texture_handle(GLuint prog
, const char *name
, GLuint64 handle
)
215 loc
= glGetUniformLocation(prog
, name
);
217 glUniformHandleui64vARB(loc
, 1, &handle
);
221 new_bindless_texture(int idx
)
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
);
246 piglit_init(int argc
, char **argv
)
249 int max_combined_textures
, i
, num
;
251 float texcoord
= 0.5;
254 piglit_require_extension("GL_ARB_bindless_texture");
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
);
279 /* initialize resident textures */
281 for (i
= 0; i
< max_vs_textures
; i
++) {
283 sprintf(name
, "vertex_tex[%i]", i
);
284 handle
= new_bindless_texture(num
);
285 set_texture_handle(prog
, name
, handle
);
289 for (i
= 0; i
< max_fs_textures
; i
++) {
291 sprintf(name
, "fragment_tex[%i]", i
);
292 handle
= new_bindless_texture(num
);
293 set_texture_handle(prog
, name
, handle
);
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
);