1 /*
2  * Copyright © 2012 Intel Corporation
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 layout-std140-base-size-and-alignment.c
25  *
26  * Tests that glGetActiveUniformsiv() returns the correct offset for
27  * any basic type valid in std140, and for a float just following
28  * that, thus testing the size and base alignment for them.
29  */
30 
31 #include "piglit-util-gl.h"
32 #include "uniform-types.h"
33 
34 PIGLIT_GL_TEST_CONFIG_BEGIN
35 
36 	config.supports_gl_compat_version = 10;
37 	config.supports_gl_core_version = 31;
38 	config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
39 	config.khr_no_error_support = PIGLIT_NO_ERRORS;
40 
41 PIGLIT_GL_TEST_CONFIG_END
42 
43 static int
align(int v,int a)44 align(int v, int a)
45 {
46 	return (v + a - 1) & ~(a - 1);
47 }
48 
49 static bool
test_format(const struct uniform_type * type,bool row_major)50 test_format(const struct uniform_type *type, bool row_major)
51 {
52 	/* Using 140 to get unsigned ints. */
53 	const char *fs_template =
54 		"#version 140\n"
55 		"layout(std140) uniform ubo {\n"
56 		"	float pad;\n"
57 		"	%s %s u;\n"
58 		"	float size_test;\n"
59 		"};\n"
60 		"\n"
61 		"void main() {\n"
62 		"	gl_FragColor = vec4(pad) + vec4(%s) + vec4(size_test);\n"
63 		"}\n";
64 	char *fs_source;
65 	GLuint prog;
66 	const char *uniform_names[] = { "u", "size_test" };
67 	GLuint uniform_indices[2];
68 	GLint offsets[2];
69 	int offset, size, expected_offset;
70 	const struct uniform_type *transposed_type;
71 	bool pass;
72 	const char *deref;
73 
74 	if (row_major)
75 		transposed_type = get_transposed_type(type);
76 	else
77 		transposed_type = type;
78 
79 	if (type->size == 4)
80 		deref = "u";
81 	else if (type->size <= 16)
82 		deref = "u.x";
83 	else
84 		deref = "u[0].x";
85 
86 	(void)!asprintf(&fs_source, fs_template,
87 		 row_major && type->size > 16 ? "layout(row_major) " : "",
88 		 type->type,
89 		 deref);
90 	prog = piglit_build_simple_program(NULL, fs_source);
91 	free(fs_source);
92 
93 	glGetUniformIndices(prog, 2, uniform_names, uniform_indices);
94 	glGetActiveUniformsiv(prog, 2, uniform_indices,
95 			      GL_UNIFORM_OFFSET, offsets);
96 
97 	glDeleteProgram(prog);
98 
99 	offset = offsets[0];
100 	size = offsets[1] - offsets[0];
101 
102 	/* "pad" at the start of the UBO is a float, so our test
103 	 * uniform would start at byte 4 if not for alignment.
104 	 */
105 	expected_offset = 4;
106 	expected_offset = align(expected_offset, transposed_type->alignment);
107 
108 	pass = (offset == expected_offset &&
109 		size == transposed_type->size);
110 
111 	printf("%-10s %10s %8d %-16d %8d %-16d%s\n",
112 	       type->type,
113 	       row_major ? "y" : "n",
114 	       offset,
115 	       expected_offset,
116 	       size,
117 	       transposed_type->size,
118 	       pass ? "" : " FAIL");
119 
120 	return pass;
121 }
122 
123 void
piglit_init(int argc,char ** argv)124 piglit_init(int argc, char **argv)
125 {
126 	bool pass = true;
127 	unsigned int i;
128 
129 	piglit_require_extension("GL_ARB_uniform_buffer_object");
130 	piglit_require_GLSL_version(140);
131 
132 	printf("%-10s %10s %8s %16s %8s %-16s\n",
133 	       "type", "row_major",
134 	       "offset", "expected offset", "size", "expected size");
135 
136 	for (i = 0; uniform_types[i].type; i++) {
137 		pass = test_format(&uniform_types[i], false) && pass;
138 		pass = test_format(&uniform_types[i], true) && pass;
139 	}
140 
141 	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
142 }
143 
piglit_display(void)144 enum piglit_result piglit_display(void)
145 {
146 	/* UNREACHED */
147 	return PIGLIT_FAIL;
148 }
149