perf/pixel-rate: new pixel throughput microbenchmark
[piglit.git] / tests / spec / ext_draw_instanced / drawarrays.c
blob36158cf23020d43c405af639d43de9d782c165bf
1 /*
2 * Copyright (c) 2020 Simon Zeni
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:
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.
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 THE AUTHORS OR COYPRIGTH
19 * HOLDERS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
22 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 /**
26 * @file
27 * Tests GL_EXT_draw_instanced
30 #include "piglit-util-gl.h"
31 #include "piglit-matrix.h"
33 PIGLIT_GL_TEST_CONFIG_BEGIN
35 config.supports_gl_es_version = 20;
37 config.window_width = 400;
38 config.window_height = 400;
39 config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE;
40 config.khr_no_error_support = PIGLIT_NO_ERRORS;
42 PIGLIT_GL_TEST_CONFIG_END
44 static const char *TestName = "ext-draw-instanced";
46 #define PRIMS 8
48 /* Ortho projection width, height */
49 #define W 10.0
50 #define H 10.0
52 static const char *VertShaderText =
53 "#version 100 \n"
54 "#extension GL_EXT_draw_instanced: enable \n"
55 "uniform vec4 Colors[8]; \n"
56 "uniform vec2 Positions[8]; \n"
57 "uniform mat4 MVP; \n"
58 "attribute vec2 Vertex; \n"
59 "varying vec4 color; \n"
60 "void main() \n"
61 "{ \n"
62 " vec2 pos = Positions[gl_InstanceIDEXT]; \n"
63 " vec4 p = vec4(Vertex + pos, 0.0, 1.0); \n"
64 " gl_Position = MVP * p; \n"
65 " color = Colors[gl_InstanceIDEXT]; \n"
66 "} \n";
68 static const char *FragShaderText =
69 "#version 100 \n"
70 "precision highp float;\n"
71 "varying vec4 color; \n"
72 "void main() \n"
73 "{ \n"
74 " gl_FragColor = color; \n"
75 "} \n";
77 static GLuint VertShader, FragShader, Program;
78 static GLint VertexAttrib;
79 static GLint ColorsUniform, PositionsUniform, MVPUniform;
80 static float modelview[16], projection[16], modelviewproj[16];
82 /* Instance positions in uniform array */
83 static const GLfloat Positions[PRIMS][2] = {
84 { -6, 6 },
85 { -4, 4 },
86 { -2, 2 },
87 { 0, 0 },
88 { 2, -2 },
89 { 4, -4 },
90 { 6, -6 },
91 { 8, -8 }
94 /* Instance colors in vertex array */
95 static const GLfloat Colors[PRIMS][4] = {
96 {1, 0, 0, 1},
97 {0, 1, 0, 1},
98 {0, 0, 1, 1},
99 {1, 1, 0, 1},
100 {0, 1, 1, 1},
101 {1, 0, 1, 1},
102 {1, 1, 1, 1},
103 {0.5, 0.5, 0.5, 1},
106 /** Convert object position to window position */
107 static void
108 objpos_to_winpos(const float obj[2], int win[2])
110 float winpos[4];
111 float objpos[4] = { obj[0], obj[1], 0.0, 1.0 };
113 piglit_project_to_window(winpos, objpos, modelview, projection,
114 0, 0, piglit_width, piglit_height);
115 win[0] = (int) winpos[0];
116 win[1] = (int) winpos[1];
119 enum piglit_result
120 piglit_display(void)
122 static const GLfloat verts[4][2] = {
123 {-1.0, -1.0}, {1.0, -1.0}, {1.0, 1.0}, {-1.0, 1.0}
126 glVertexAttribPointer(VertexAttrib, 2, GL_FLOAT, GL_FALSE, 0, verts);
127 glEnableVertexAttribArray(VertexAttrib);
129 glViewport(0, 0, piglit_width, piglit_height);
130 glClear(GL_COLOR_BUFFER_BIT);
132 glUseProgram(Program);
134 glDrawArraysInstancedEXT(GL_TRIANGLE_FAN, 0, 4, PRIMS);
136 /* check rendering */
137 GLint i, pos[2];
138 for (i = 0; i < PRIMS; i++) {
139 /* compute probe location */
140 objpos_to_winpos(Positions[i], pos);
142 if (!piglit_probe_pixel_rgba(pos[0], pos[1], Colors[i])) {
143 fprintf(stderr, "%s: instance %d failed to draw correctly\n",
144 TestName, i);
145 return PIGLIT_FAIL;
149 glUseProgram(0);
151 glDisableVertexAttribArray(VertexAttrib);
153 piglit_present_results();
155 return PIGLIT_PASS;
158 void
159 piglit_init(int argc, char **argv)
161 piglit_require_extension("GL_EXT_draw_instanced");
163 VertShader = piglit_compile_shader_text(GL_VERTEX_SHADER, VertShaderText);
164 assert(VertShader);
166 FragShader = piglit_compile_shader_text(GL_FRAGMENT_SHADER, FragShaderText);
167 assert(FragShader);
169 Program = piglit_link_simple_program(VertShader, FragShader);
171 glUseProgram(Program);
173 VertexAttrib = glGetAttribLocation(Program, "Vertex");
175 ColorsUniform = glGetUniformLocation(Program, "Colors");
176 PositionsUniform = glGetUniformLocation(Program, "Positions");
177 MVPUniform = glGetUniformLocation(Program, "MVP");
179 glUniform4fv(ColorsUniform, PRIMS, (GLfloat *)Colors);
180 glUniform2fv(PositionsUniform, PRIMS, (GLfloat *)Positions);
182 /* Setup coordinate transformation */
183 piglit_scale_matrix(modelview, 0.5, 0.5, 1.0);
184 piglit_ortho_matrix(projection, -0.5 * W, 0.5 * W,
185 -0.5 * H, 0.5 * H, -1.0, 1.0);
186 piglit_matrix_mul_matrix(modelviewproj, modelview, projection);
188 glUniformMatrix4fv(MVPUniform, 1, GL_FALSE, modelviewproj);
190 glUseProgram(0);