1 /*
2  * Copyright (c) 2014 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
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 /** @file explicit-offset-bufferstorage.c
25  *
26  * This is a copy of the arb_uniform_buffer_object bufferstorage test updated
27  * to make use of explicit offsets.
28  *
29  * Test rendering with UBOs.  We draw four squares with different
30  * positions, sizes, rotations and colors where those parameters come
31  * from UBOs. Same as rendering.c, except that the UBOs are
32  * persistently mapped.
33  */
34 
35 #include "piglit-util-gl.h"
36 
37 PIGLIT_GL_TEST_CONFIG_BEGIN
38 
39 	config.supports_gl_core_version = 32;
40 	config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
41 
42 PIGLIT_GL_TEST_CONFIG_END
43 
44 static const char vert_shader_text[] =
45 	"#version 150\n"
46 	"#extension GL_ARB_enhanced_layouts : require\n"
47 	"#extension GL_ARB_uniform_buffer_object : require\n"
48 	"\n"
49 	"in vec4 piglit_vertex;\n"
50 	"\n"
51 	"layout(std140) uniform;\n"
52 	"uniform ub_pos_size {\n"
53 	"  layout(offset = 8) vec2 pos;\n"
54 	"  layout(offset = 24) float size;\n"
55 	"};\n"
56 	"uniform ub_rot {float rotation; };\n"
57 	"\n"
58 	"void main()\n"
59 	"{\n"
60 	"   mat2 m;\n"
61 	"   m[0][0] = m[1][1] = cos(rotation); \n"
62 	"   m[0][1] = sin(rotation); \n"
63 	"   m[1][0] = -m[0][1]; \n"
64 	"   gl_Position.xy = m * piglit_vertex.xy * vec2(size) + pos;\n"
65 	"   gl_Position.zw = vec2(0, 1);\n"
66 	"}\n";
67 
68 static const char frag_shader_text[] =
69 	"#version 150\n"
70 	"#extension GL_ARB_enhanced_layouts : require\n"
71 	"#extension GL_ARB_uniform_buffer_object : require\n"
72 	"\n"
73 	"layout(std140) uniform;\n"
74 	"uniform ub_color {\n"
75 	"  layout(offset = 0) vec4 color;\n"
76 	"  layout(offset = 20) float color_scale;\n"
77 	"} named_ub;\n"
78 	"\n"
79 	"void main()\n"
80 	"{\n"
81 	"   gl_FragColor = named_ub.color * named_ub.color_scale;\n"
82 	"}\n";
83 
84 #define NUM_SQUARES 4
85 #define NUM_UBOS 3
86 
87 /* Square positions and sizes */
88 static const float pos_size[NUM_SQUARES][7] = {
89 	{ 0.0, 0.0, -0.5, -0.5, 0.0, 0.0, 0.1 },
90 	{ 0.0, 0.0,  0.5, -0.5, 0.0, 0.0, 0.2 },
91 	{ 0.0, 0.0, -0.5,  0.5, 0.0, 0.0, 0.3 },
92 	{ 0.0, 0.0,  0.5,  0.5, 0.0, 0.0, 0.4 }
93 };
94 
95 /* Square color and color_scales */
96 static const float color[NUM_SQUARES][8] = {
97 	{ 2.0, 0.0, 0.0, 1.0, 0.0, 0.50, 0.0, 0.0 },
98 	{ 0.0, 4.0, 0.0, 1.0, 0.0, 0.25, 0.0, 0.0 },
99 	{ 0.0, 0.0, 5.0, 1.0, 0.0, 0.20, 0.0, 0.0 },
100 	{ 0.2, 0.2, 0.2, 0.2, 0.0, 5.00, 0.0, 0.0 }
101 };
102 
103 /* Square rotations */
104 static const float rotation[NUM_SQUARES] = {
105 	0.0,
106 	0.1,
107 	0.2,
108 	0.3
109 };
110 
111 static GLuint prog;
112 static GLuint buffers[NUM_UBOS];
113 static void *ubos[NUM_UBOS];
114 
115 
116 static void
setup_ubos(void)117 setup_ubos(void)
118 {
119 	static const char *names[NUM_UBOS] = {
120 		"ub_pos_size",
121 		"ub_color",
122 		"ub_rot"
123 	};
124 	int i;
125 
126 	glGenBuffers(NUM_UBOS, buffers);
127 
128 	for (i = 0; i < NUM_UBOS; i++) {
129 		GLint index, size;
130 
131 		/* query UBO index */
132 		index = glGetUniformBlockIndex(prog, names[i]);
133 
134 		/* query UBO size */
135 		glGetActiveUniformBlockiv(prog, index,
136 					  GL_UNIFORM_BLOCK_DATA_SIZE, &size);
137 
138 		printf("UBO %s: index = %d, size = %d\n",
139 		       names[i], index, size);
140 
141 		/* Allocate UBO */
142 		glBindBuffer(GL_UNIFORM_BUFFER, buffers[i]);
143 		glBufferStorage(GL_UNIFORM_BUFFER, size, NULL,
144 				GL_MAP_WRITE_BIT |
145 				GL_MAP_PERSISTENT_BIT |
146 				GL_MAP_COHERENT_BIT |
147 				GL_DYNAMIC_STORAGE_BIT);
148 
149 		piglit_check_gl_error(GL_NO_ERROR);
150 
151 		ubos[i] = glMapBufferRange(GL_UNIFORM_BUFFER, 0, size,
152 					   GL_MAP_WRITE_BIT |
153 					   GL_MAP_PERSISTENT_BIT |
154 					   GL_MAP_COHERENT_BIT);
155 
156 		piglit_check_gl_error(GL_NO_ERROR);
157 
158 		if (!ubos[i])
159 			piglit_report_result(PIGLIT_FAIL);
160 
161 		/* Attach UBO */
162 		glBindBufferBase(GL_UNIFORM_BUFFER, i, buffers[i]);
163 		glUniformBlockBinding(prog, index, i);
164 
165 		if (!piglit_check_gl_error(GL_NO_ERROR))
166 			piglit_report_result(PIGLIT_FAIL);
167 	}
168 }
169 
170 
171 void
piglit_init(int argc,char ** argv)172 piglit_init(int argc, char **argv)
173 {
174 	piglit_require_GLSL_version(150); /* Required for named blocks */
175 	piglit_require_extension("GL_ARB_enhanced_layouts");
176 	piglit_require_extension("GL_ARB_uniform_buffer_object");
177 	piglit_require_extension("GL_ARB_buffer_storage");
178 	piglit_require_extension("GL_ARB_map_buffer_range");
179 
180 	prog = piglit_build_simple_program(vert_shader_text, frag_shader_text);
181 	assert(prog);
182 	glUseProgram(prog);
183 
184 	setup_ubos();
185 
186 	glClearColor(0.2, 0.2, 0.2, 0.2);
187 }
188 
189 
190 static bool
probe(int x,int y,int color_index)191 probe(int x, int y, int color_index)
192 {
193 	float expected[4];
194 
195 	/* mul color by color_scale */
196 	expected[0] = color[color_index][0] * color[color_index][5];
197 	expected[1] = color[color_index][1] * color[color_index][5];
198 	expected[2] = color[color_index][2] * color[color_index][5];
199 	expected[3] = color[color_index][3] * color[color_index][5];
200 
201 	return piglit_probe_pixel_rgba(x, y, expected);
202 }
203 
204 
205 enum piglit_result
piglit_display(void)206 piglit_display(void)
207 {
208 	GLsync fence;
209 	bool pass = true;
210 	int x0 = piglit_width / 4;
211 	int x1 = piglit_width * 3 / 4;
212 	int y0 = piglit_height / 4;
213 	int y1 = piglit_height * 3 / 4;
214 	int i;
215 
216 	glViewport(0, 0, piglit_width, piglit_height);
217 
218 	glClear(GL_COLOR_BUFFER_BIT);
219 
220 	for (i = 0; i < NUM_SQUARES; i++) {
221 		/* Wait for any previous rendering to finish before
222 		 * updating the UBOs
223 		 */
224 		fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
225 		glClientWaitSync(fence, GL_SYNC_FLUSH_COMMANDS_BIT,
226 				 GL_TIMEOUT_IGNORED);
227 
228 		/* Load UBO data */
229 		memcpy(ubos[0], pos_size[i], sizeof(pos_size[0]));
230 		memcpy(ubos[1], color[i], sizeof(color[0]));
231 		memcpy(ubos[2], &rotation[i], sizeof(rotation[0]));
232 
233 		piglit_draw_rect(-1, -1, 2, 2);
234 	}
235 
236 	pass = probe(x0, y0, 0) && pass;
237 	pass = probe(x1, y0, 1) && pass;
238 	pass = probe(x0, y1, 2) && pass;
239 	pass = probe(x1, y1, 3) && pass;
240 
241 	piglit_present_results();
242 
243 	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
244 }
245