1 /*
2  * Copyright (c) 2010 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  * on the rights to use, copy, modify, merge, publish, distribute, sub
8  * license, and/or sell copies of the Software, and to permit persons to whom
9  * the 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,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NON-INFRINGEMENT.  IN NO EVENT SHALL VMWARE AND/OR THEIR SUPPLIERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 /**
26  * @file
27  * Tests GL_ARB_draw_instanced
28  */
29 
30 #include "piglit-util-gl.h"
31 
32 PIGLIT_GL_TEST_CONFIG_BEGIN
33 
34 	config.supports_gl_compat_version = 10;
35 
36 	config.window_width = 500;
37 	config.window_height = 500;
38 	config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE;
39 	config.khr_no_error_support = PIGLIT_NO_ERRORS;
40 
41 PIGLIT_GL_TEST_CONFIG_END
42 
43 static const char *TestName = "draw-instanced";
44 
45 static GLint PosUniform, ColorUniform;
46 
47 #define PRIMS 8
48 
49 static const char *VertShaderText =
50    "#extension GL_ARB_draw_instanced: enable \n"
51    "uniform vec2 Pos[8]; \n"
52    "uniform vec4 Color[8]; \n"
53    "void main() \n"
54    "{ \n"
55 #if 0
56    "   vec4 p = ftransform(); \n"
57    "   vec2 pos = Pos[gl_InstanceIDARB]; \n"
58    "   p.x += pos.x; \n"
59    "   p.y += pos.y; \n"
60    "   gl_Position = p; \n"
61 #else
62    "   vec4 p = gl_Vertex; \n"
63    "   vec2 pos = Pos[gl_InstanceIDARB]; \n"
64    "   p.x += pos.x; \n"
65    "   p.y += pos.y; \n"
66    "   gl_Position = gl_ModelViewProjectionMatrix * p; \n"
67 #endif
68    "   gl_FrontColor = Color[gl_InstanceIDARB]; \n"
69    "} \n";
70 
71 static const char *FragShaderText =
72    "void main() \n"
73    "{ \n"
74    "   gl_FragColor = gl_Color; \n"
75    "} \n";
76 
77 
78 static GLuint VertShader, FragShader, Program;
79 
80 static const GLfloat Positions[PRIMS][2] = {
81    { -6, 6 },
82    { -4, 4 },
83    { -2, 2 },
84    {  0, 0 },
85    {  2, -2 },
86    {  4, -4 },
87    {  6, -6 },
88    {  8, -8 }
89 };
90 
91 static const GLfloat Colors[PRIMS][4] = {
92    {1, 0, 0, 1},
93    {0, 1, 0, 1},
94    {0, 0, 1, 1},
95    {1, 1, 0, 1},
96    {0, 1, 1, 1},
97    {1, 0, 1, 1},
98    {1, 1, 1, 1},
99    {0.5, 0.5, 0.5, 1},
100 };
101 
102 
103 static GLboolean
test_instancing(void)104 test_instancing(void)
105 {
106    static const GLfloat verts[4][2] = {
107       {-1, -1}, {1, -1}, {1, 1}, {-1, 1}
108    };
109 
110    glVertexPointer(2, GL_FLOAT, 0, verts);
111    glEnableClientState(GL_VERTEX_ARRAY);
112 
113    glClear(GL_COLOR_BUFFER_BIT);
114 
115    glUseProgram(Program);
116 
117    glDrawArraysInstancedARB(GL_POLYGON, 0, 4, PRIMS);
118 
119    glUseProgram(0);
120 
121    {
122       GLint i;
123       GLint pos[4];
124 
125       for (i = 0; i < PRIMS; i++) {
126          /* use glRasterPos to determine where to read a sample pixel */
127          glRasterPos2fv(Positions[i]);
128          glGetIntegerv(GL_CURRENT_RASTER_POSITION, pos);
129 
130          if (!piglit_probe_pixel_rgba(pos[0], pos[1], Colors[i])) {
131             fprintf(stderr, "%s: instance %d failed to draw correctly\n",
132                     TestName, i);
133             piglit_present_results();
134             return GL_FALSE;
135          }
136       }
137    }
138 
139    piglit_present_results();
140 
141    return GL_TRUE;
142 }
143 
144 
145 enum piglit_result
piglit_display(void)146 piglit_display(void)
147 {
148    if (!test_instancing())
149       return PIGLIT_FAIL;
150 
151    return PIGLIT_PASS;
152 }
153 
154 
155 void
piglit_init(int argc,char ** argv)156 piglit_init(int argc, char **argv)
157 {
158    piglit_require_extension("GL_ARB_draw_instanced");
159 
160    VertShader = piglit_compile_shader_text(GL_VERTEX_SHADER, VertShaderText);
161    assert(VertShader);
162 
163    FragShader = piglit_compile_shader_text(GL_FRAGMENT_SHADER, FragShaderText);
164    assert(FragShader);
165 
166    Program = piglit_link_simple_program(VertShader, FragShader);
167 
168    glUseProgram(Program);
169 
170    PosUniform = glGetUniformLocation(Program, "Pos");
171    ColorUniform = glGetUniformLocation(Program, "Color");
172 
173    glUniform2fv(PosUniform, PRIMS, (GLfloat *) Positions);
174    glUniform4fv(ColorUniform, PRIMS, (GLfloat *) Colors);
175 
176    glMatrixMode(GL_PROJECTION);
177    glLoadIdentity();
178    glFrustum(-5, 5, -5, 5, 10, 20);
179 
180    glMatrixMode(GL_MODELVIEW);
181    glLoadIdentity();
182    glTranslatef(0, 0, -11.0);
183    glScalef(0.5, 0.5, 1.0);
184 }
185