1 /* Copyright © 2015 Ilia Mirkin
2  * Copyright © 2017 VMware, Inc.
3  *
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:
10  *
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.
14  *
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.
22  */
23 
24 /** @file indexed.c
25  *
26  * Tests that we can sample texture buffers with sampler indexing.
27  */
28 
29 #include "piglit-util-gl.h"
30 
31 PIGLIT_GL_TEST_CONFIG_BEGIN
32 	config.supports_gl_core_version = 32;
33 	config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
34 	config.khr_no_error_support = PIGLIT_NO_ERRORS;
35 PIGLIT_GL_TEST_CONFIG_END
36 
37 enum piglit_result
piglit_display(void)38 piglit_display(void)
39 {
40 	static const float green[4] = {0, 1, 0, 0};
41 	bool pass;
42 
43 	glViewport(0, 0, piglit_width, piglit_height);
44 	glClearColor(0.2, 0.2, 0.2, 0.2);
45 	glClear(GL_COLOR_BUFFER_BIT);
46 
47 	piglit_draw_rect(-1, -1, 2, 2);
48 
49 	pass = piglit_probe_rect_rgba(0, 0, piglit_width, piglit_height, green);
50 
51 	piglit_present_results();
52 
53 	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
54 }
55 
56 void
piglit_init(int argc,char ** argv)57 piglit_init(int argc, char **argv)
58 {
59 	static const char *vs_source =
60 		"#version 150\n"
61 		"in vec4 piglit_vertex;\n"
62 		"void main()\n"
63 		"{\n"
64 		"	gl_Position = piglit_vertex;\n"
65 		"}\n";
66 
67 	static const char *fs_source =
68 		"#version 150\n"
69 		"#extension GL_ARB_gpu_shader5: require\n"
70 		"uniform samplerBuffer s[2];\n"
71 		"uniform int offset;\n"
72 		"uniform int index = 1;\n"
73 		"void main()\n"
74 		"{\n"
75 		"	gl_FragColor = texelFetch(s[index], offset);\n"
76 		"}\n";
77 
78 	GLuint tex[2], tbo[2];
79 	GLint indices[2] = { 0, 1 };
80 	static const uint8_t datag[4] = {0x00, 0xff, 0x00, 0x00};
81 	static const uint8_t datar[4] = {0xff, 0x00, 0x00, 0x00};
82 	GLuint prog;
83 	GLint size = 4;
84 	piglit_require_extension("GL_ARB_gpu_shader5");
85 
86 	prog = piglit_build_simple_program(vs_source, fs_source);
87 	glUseProgram(prog);
88 
89 	glGenBuffers(2, tbo);
90 	glGenTextures(2, tex);
91 	glBindBuffer(GL_TEXTURE_BUFFER, tbo[0]);
92 	glBindTexture(GL_TEXTURE_BUFFER, tex[0]);
93 	glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, tbo[0]);
94 	glBufferData(GL_TEXTURE_BUFFER,
95 		     size * sizeof(datar), NULL, GL_STATIC_READ);
96 	glBufferSubData(GL_TEXTURE_BUFFER,
97 			(size - 1) * sizeof(datar), sizeof(datar), datar);
98 
99 	glActiveTexture(GL_TEXTURE1);
100 	glBindBuffer(GL_TEXTURE_BUFFER, tbo[1]);
101 	glBindTexture(GL_TEXTURE_BUFFER, tex[1]);
102 	glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, tbo[1]);
103 	glBufferData(GL_TEXTURE_BUFFER,
104 		     size * sizeof(datag), NULL, GL_STATIC_READ);
105 	glBufferSubData(GL_TEXTURE_BUFFER,
106 			(size - 1) * sizeof(datag), sizeof(datag), datag);
107 
108 	glUniform1i(glGetUniformLocation(prog, "offset"), size - 1);
109 	glUniform1iv(glGetUniformLocation(prog, "s"), 2, indices);
110 }
111