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 /**
25  * @file elements.c
26  *
27  * Tests that glDrawElementsInstancedARB() can render multiple
28  * instances and the instance IDs are propagated to the shader.
29  *
30  * This is a derivative of instance-array-dereference.c, which uses
31  * glDrawArraysInstancedARB().
32  */
33 
34 #include "piglit-util-gl.h"
35 
36 PIGLIT_GL_TEST_CONFIG_BEGIN
37 
38 	config.supports_gl_compat_version = 10;
39 	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
40 	config.khr_no_error_support = PIGLIT_NO_ERRORS;
41 
42 PIGLIT_GL_TEST_CONFIG_END
43 
44 static const char *vs_source =
45 	"#version 120\n"
46 	"#extension GL_ARB_draw_instanced: require\n"
47 	"\n"
48 	"uniform vec4 instance_colors[] = vec4[](vec4(0.0, 1.0, 0.0, 1.0),\n"
49 	"					 vec4(0.0, 1.0, 1.0, 1.0),\n"
50 	"					 vec4(0.0, 0.0, 1.0, 1.0));\n"
51 	"\n"
52 	"varying vec4 color;\n"
53 	"\n"
54 	"void main()\n"
55 	"{\n"
56 	"  color = instance_colors[gl_InstanceIDARB];\n"
57 	"\n"
58 	"  vec4 v = gl_Vertex;\n"
59 	"  v.x += 20.0 * float(gl_InstanceIDARB);\n"
60 	"\n"
61 	"  gl_Position = gl_ModelViewProjectionMatrix * v;\n"
62 	"}\n";
63 
64 static const char *fs_source =
65 	"varying vec4 color;\n"
66 	"void main()\n"
67 	"{\n"
68 	"  gl_FragColor = color;\n"
69 	"}\n";
70 
71 enum piglit_result
piglit_display(void)72 piglit_display(void)
73 {
74 	static const int indices[6] = {0, 1, 2, 0, 2, 3};
75 	static const float verts[] = {
76 		10, 10,
77 		20, 10,
78 		20, 20,
79 		10, 20,
80 	};
81 	static const float green[4] = {0.0, 1.0, 0.0, 1.0};
82 	static const float cyan[4]  = {0.0, 1.0, 1.0, 1.0};
83 	static const float blue[4]  = {0.0, 0.0, 1.0, 1.0};
84 	enum piglit_result result = PIGLIT_PASS;
85 
86 	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
87 
88 	glViewport(0, 0, piglit_width, piglit_height);
89 
90 	glClearColor(0.5, 0.5, 0.5, 0.0);
91 	glClear(GL_COLOR_BUFFER_BIT);
92 
93 	glVertexPointer(2, GL_FLOAT, 0, verts);
94 	glEnableClientState(GL_VERTEX_ARRAY);
95 	glDrawElementsInstancedARB(GL_TRIANGLES, 6, GL_UNSIGNED_INT,
96 				   indices, 3);
97 	glDisableClientState(GL_VERTEX_ARRAY);
98 
99 	if (!piglit_probe_rect_rgba(10, 10, 10, 10, green))
100 		result = PIGLIT_FAIL;
101 
102 	if (!piglit_probe_rect_rgba(30, 10, 10, 10, cyan))
103 		result = PIGLIT_FAIL;
104 
105 	if (!piglit_probe_rect_rgba(50, 10, 10, 10, blue))
106 		result = PIGLIT_FAIL;
107 
108 	piglit_present_results();
109 
110 	return result;
111 }
112 
113 void
piglit_init(int argc,char ** argv)114 piglit_init(int argc, char **argv)
115 {
116 	GLuint prog;
117 
118 	piglit_require_extension("GL_ARB_draw_instanced");
119 
120 	prog = piglit_build_simple_program(vs_source, fs_source);
121 
122 	glUseProgram(prog);
123 }
124