ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_shader_draw_parameters / drawid-indirect.c
blobe27591c7734659483c8b33c4eeb6d17161c39fbf
1 /*
2 * Copyright © 2015 Intel Corporation
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:
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, 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.
24 /**
25 * \file drawid.c
27 * Test for gl_DrawIDARB with indirect multi-draw. For mesa, this is
28 * interesting because this is the only mode where we actually get
29 * multiple _mesa_prim into the backend in one call. This tests that
30 * we properly reemit state to update gl_DrawIDARB between rendering,
31 * which on i965 involves reemitting vertex buffer state.
33 * Also, on i965, we source the vertex and instance ID from an
34 * internal vertex buffer for direct rdraw, but point the vertex
35 * buffer the parameter buffer for indirect draws. The baseinstance
36 * subtest verifies that this all works right. Conversely, the
37 * vertexid subtest doesn't reference gl_DrawIDARB and is useful for
38 * validating that we don't reemit vertex buffer state between multi
39 * draw calls. We can't test for that with this test, of course, but
40 * we can inspect the generate command stream from the driver.
43 #include "piglit-util-gl.h"
45 PIGLIT_GL_TEST_CONFIG_BEGIN
47 config.supports_gl_core_version = 31;
48 config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE;
50 PIGLIT_GL_TEST_CONFIG_END
52 static const char vs_template[] =
53 "#version 330\n"
54 "#extension GL_ARB_shader_draw_parameters: require\n"
55 "\n"
56 "layout(location = 0) in vec2 pos;\n"
57 "layout(location = 1) in ivec4 ref;\n"
58 "out vec4 color;\n"
59 "\n"
60 "void main()\n"
61 "{\n"
62 " gl_Position = vec4(pos, 0.0, 1.0);\n"
63 " if (%s)\n"
64 " color = vec4(0, 1, 0, 1);\n"
65 " else\n"
66 " color = vec4(1, 0, 0, 1);\n"
67 "}\n";
69 static const char fs_text[] =
70 "#version 130\n"
71 "\n"
72 "in vec4 color;\n"
73 "\n"
74 "void main()\n"
75 "{\n"
76 " gl_FragColor = color;\n"
77 "}\n";
79 void
80 piglit_init(int argc, char **argv)
82 GLuint prog;
83 char *vs_text;
85 if (strcmp(argv[1], "drawid") == 0) {
86 (void)!asprintf(&vs_text, vs_template,
87 "ref.x == gl_DrawIDARB");
88 } else if (strcmp(argv[1], "basevertex") == 0) {
89 (void)!asprintf(&vs_text, vs_template,
90 "ref.xy == ivec2(gl_DrawIDARB, gl_BaseVertexARB)");
91 } else if (strcmp(argv[1], "baseinstance") == 0) {
92 (void)!asprintf(&vs_text, vs_template,
93 "ref.xz == ivec2(gl_DrawIDARB, gl_BaseInstanceARB)");
94 } else if (strcmp(argv[1], "vertexid") == 0) {
95 (void)!asprintf(&vs_text, vs_template,
96 "ref.w == gl_VertexID");
97 } else {
98 printf("Unknown subtest: %s\n", argv[1]);
99 piglit_report_result(PIGLIT_FAIL);
102 piglit_require_GLSL_version(330);
104 piglit_require_extension("GL_ARB_shader_draw_parameters");
105 piglit_require_extension("GL_ARB_base_instance");
107 prog = piglit_build_simple_program(vs_text, fs_text);
109 glUseProgram(prog);
112 struct cmd {
113 GLuint count;
114 GLuint instanceCount;
115 GLuint firstIndex;
116 GLuint baseVertex;
117 GLuint baseInstance;
120 enum piglit_result
121 piglit_display()
123 bool pass;
125 struct {
126 float vertex_array[16];
127 int reference_array[32];
128 int indices[6];
129 } geometry = {
130 .vertex_array = {
131 -1, -1,
132 0, -1,
133 0, 1,
134 -1, 1,
136 0, -1,
137 1, -1,
138 1, 1,
139 0, 1,
141 .reference_array = {
142 0, 0, 0, 0,
143 0, 0, 0, 1,
144 0, 0, 0, 2,
145 0, 0, 0, 3,
147 1, 4, 7, 4,
148 1, 4, 7, 5,
149 1, 4, 7, 6,
150 1, 4, 7, 7,
154 const int indices[12] = {
155 0, 1, 2,
156 0, 2, 3,
159 float green[] = {0, 1, 0, 1};
161 GLuint vao, vbo, ibo, dbo;
162 glGenVertexArrays(1, &vao);
163 glBindVertexArray(vao);
165 glGenBuffers(1, &vbo);
166 glBindBuffer(GL_ARRAY_BUFFER, vbo);
167 glBufferData(GL_ARRAY_BUFFER, sizeof(geometry), &geometry, GL_STATIC_DRAW);
169 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE,
170 2 * sizeof(GLfloat),
171 (void *) ((char *) &geometry.vertex_array - (char *) &geometry));
173 glVertexAttribIPointer(1, 4, GL_UNSIGNED_INT,
174 4 * sizeof(int),
175 (void *) ((char *) &geometry.reference_array - (char *) &geometry));
177 /* Enable the attributes */
178 glEnableVertexAttribArray(0);
179 glEnableVertexAttribArray(1);
181 glGenBuffers(1, &ibo);
182 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
183 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
185 struct cmd cmds[] = {
187 .count = 6,
188 .instanceCount = 1,
189 .firstIndex = 0,
190 .baseVertex = 0,
191 .baseInstance = 0
194 .count = 6,
195 .instanceCount = 1,
196 .firstIndex = 0,
197 .baseVertex = 4,
198 .baseInstance = 7
202 glGenBuffers(1, &dbo);
203 glBindBuffer(GL_DRAW_INDIRECT_BUFFER, dbo);
204 glBufferData(GL_DRAW_INDIRECT_BUFFER, sizeof(cmds), cmds, GL_STATIC_DRAW);
206 glMultiDrawElementsIndirect(GL_TRIANGLES,
207 GL_UNSIGNED_INT,
208 0, 2, 0);
210 pass = piglit_probe_rect_rgba(0, 0, piglit_width, piglit_height,
211 green);
213 piglit_present_results();
215 return pass ? PIGLIT_PASS : PIGLIT_FAIL;