cl: Don't use device_infos if num_device_infos == 0
[piglit.git] / tests / general / vertex-fallbacks.c
blobcdb0edc96a365c8dbe4d58a4ac6e07502e9d7d1c
1 /*
2 * Copyright (c) 2021 VMware, Inc.
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 DEALINGS
21 * IN THE SOFTWARE.
23 * This test was designed to exercise as many of the Mesa Gallium draw
24 * module's vertex pipeline stages as possible, particularly those which
25 * allocate extra vertex attributes.
27 * It does not yet check for correctness, and is instead meant to be used with
28 * memory debuggers to trap buffer overflows.
30 * Authors:
31 * Jose Fonseca <jfonseca@vmware.com>
34 #include "piglit-util-gl.h"
36 static const GLint W = 800;
37 static const GLint H = 600;
39 PIGLIT_GL_TEST_CONFIG_BEGIN
41 config.supports_gl_compat_version = 30;
43 config.window_width = W;
44 config.window_height = H;
46 config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE;
47 config.khr_no_error_support = PIGLIT_NO_ERRORS;
49 PIGLIT_GL_TEST_CONFIG_END
51 static const char *
52 vertexShader =
53 "#version 130\n"
54 "in vec4 A0;\n"
55 "in vec4 A1;\n"
56 "flat out vec4 fs_color0;\n"
57 "flat out vec4 fs_color1;\n"
58 "flat out vec4 fs_generic0;\n"
59 "uniform vec4 CS[6];\n"
60 "void main() {\n"
61 " vec4 position;\n"
62 " position = A0.xxxx * CS[0];\n"
63 " position += A0.yyyy * CS[1];\n"
64 " position += A0.zzzz * CS[2];\n"
65 " position += A0.wwww * CS[3];\n"
66 " gl_ClipDistance[0] = dot(position, CS[4]);\n"
67 " gl_ClipDistance[1] = dot(position, CS[5]);\n"
68 " gl_Position = position;\n"
69 " fs_color0 = A1;\n"
70 " fs_color1 = vec4(0, 0, 0, 0);\n"
71 " fs_generic0 = vec4(0, 0, 0, 1);\n"
72 "}\n"
75 static const char *
76 fragmentShader =
77 "#version 130\n"
78 "#extension GL_EXT_gpu_shader4 : require\n"
79 "flat in vec4 fs_color0;\n"
80 "out vec4 fragColor0;\n"
81 "void main(){\n"
82 " fragColor0.r = gl_PrimitiveID / 12.0;\n"
83 " fragColor0.g = gl_FrontFacing ? 1.0 : 0.0;\n"
84 " fragColor0.b = fs_color0.b;\n"
85 " fragColor0.a = 1.0;\n"
86 "}\n"
89 static const float
90 uniforms[][4] = {
91 { 0.90533f, 3.223402e-08f, -2.673027e-08f, -2.670354e-08f },
92 { -2.417551e-08f, 1.207107f, 5.346054e-08f, 5.340707e-08f },
93 { 4.835103e-08f, -1.289361e-07f, 0.5005005f, 0.5f },
94 { 0.0f, 0.0f, 2.902903f, 3.0f },
95 { 0.0f, 0.0f, 0.0f, 0.0f },
96 { 0.0f, 0.0f, 0.0f, 0.0f },
99 typedef struct Vertex {
100 float position[3];
101 unsigned color;
102 } Vertex;
104 static const Vertex
105 vertices[] = {
106 { { -1.0f, -1.0f, -1.0f }, 0xffffffffU },
107 { { -1.0f, -1.0f, 1.0f }, 0xffffff00U },
108 { { -1.0f, 1.0f, -1.0f }, 0xffff00ffU },
109 { { -1.0f, 1.0f, 1.0f }, 0xffff0000U },
110 { { 1.0f, -1.0f, -1.0f }, 0xff00ffffU },
111 { { 1.0f, -1.0f, 1.0f }, 0xff00ff00U },
112 { { 1.0f, 1.0f, -1.0f }, 0xff0000ffU },
113 { { 1.0f, 1.0f, 1.0f }, 0xff000000U },
116 static const GLushort
117 indices[] = {
118 0, 1, 3,
119 3, 2, 0,
120 4, 5, 7,
121 7, 6, 4,
122 0, 1, 5,
123 5, 4, 0,
124 2, 3, 7,
125 7, 6, 2,
126 0, 2, 6,
127 6, 4, 0,
128 1, 3, 7,
129 7, 5, 1,
132 static const GLubyte
133 stipple[32][4] = {
134 { 0x80, 0x80, 0x80, 0x80 },
135 { 0x00, 0x00, 0x00, 0x00 },
136 { 0x00, 0x00, 0x00, 0x00 },
137 { 0x00, 0x00, 0x00, 0x00 },
138 { 0x00, 0x00, 0x00, 0x00 },
139 { 0x00, 0x00, 0x00, 0x00 },
140 { 0x00, 0x00, 0x00, 0x00 },
141 { 0x00, 0x00, 0x00, 0x00 },
142 { 0x80, 0x80, 0x80, 0x80 },
143 { 0x00, 0x00, 0x00, 0x00 },
144 { 0x00, 0x00, 0x00, 0x00 },
145 { 0x00, 0x00, 0x00, 0x00 },
146 { 0x00, 0x00, 0x00, 0x00 },
147 { 0x00, 0x00, 0x00, 0x00 },
148 { 0x00, 0x00, 0x00, 0x00 },
149 { 0x00, 0x00, 0x00, 0x00 },
150 { 0x80, 0x80, 0x80, 0x80 },
151 { 0x00, 0x00, 0x00, 0x00 },
152 { 0x00, 0x00, 0x00, 0x00 },
153 { 0x00, 0x00, 0x00, 0x00 },
154 { 0x00, 0x00, 0x00, 0x00 },
155 { 0x00, 0x00, 0x00, 0x00 },
156 { 0x00, 0x00, 0x00, 0x00 },
157 { 0x00, 0x00, 0x00, 0x00 },
158 { 0x80, 0x80, 0x80, 0x80 },
159 { 0x00, 0x00, 0x00, 0x00 },
160 { 0x00, 0x00, 0x00, 0x00 },
161 { 0x00, 0x00, 0x00, 0x00 },
162 { 0x00, 0x00, 0x00, 0x00 },
163 { 0x00, 0x00, 0x00, 0x00 },
164 { 0x00, 0x00, 0x00, 0x00 },
165 { 0x00, 0x00, 0x00, 0x00 },
168 enum piglit_result piglit_display(void)
170 GLuint program = glCreateProgram();
171 GLuint vs = piglit_compile_shader_text(GL_VERTEX_SHADER,
172 vertexShader);
173 GLuint fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER,
174 fragmentShader);
175 glAttachShader(program, vs);
176 glDeleteShader(vs);
177 glAttachShader(program, fs);
178 glDeleteShader(fs);
179 glBindAttribLocation(program, 0, "A0");
180 glBindAttribLocation(program, 1, "A1");
181 glBindFragDataLocation(program, 0, "fragColor0");
182 glLinkProgram(program);
183 glDetachShader(program, fs);
184 glDetachShader(program, vs);
185 if (!piglit_link_check_status(program)) {
186 glDeleteProgram(program);
187 piglit_report_result(PIGLIT_FAIL);
189 glUseProgram(program);
190 GLint l0 = glGetUniformLocation(program, "CS[0]");
191 assert(l0 >= 0);
192 glUniform4fv(l0, ARRAY_SIZE(uniforms), uniforms[0]);
194 glViewport(0, 0, W, H);
195 glDepthRange(0.0, 1.0);
196 glEnable(GL_SCISSOR_TEST);
197 glScissor(0, 0, W, H);
198 glDisable(GL_DEPTH_TEST);
199 glDepthFunc(GL_LESS);
200 glEnable(GL_CLIP_DISTANCE0);
201 glEnable(GL_CLIP_DISTANCE1);
202 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
204 glFrontFace(GL_CCW);
206 /* Smooth points for front faces.
208 glPolygonMode(GL_FRONT, GL_POINT);
209 glEnable(GL_POINT_SMOOTH);
210 glPointSize(9.0);
211 if (GL_FALSE) {
212 glEnable(GL_POLYGON_STIPPLE);
213 glPolygonStipple(stipple[0]);
216 /* Smooth lines for back faces.
218 glPolygonMode(GL_BACK, GL_LINE);
219 glEnable(GL_LINE_SMOOTH);
220 glLineWidth(3.0);
221 glEnable(GL_LINE_STIPPLE);
222 glLineStipple(3, 0xfafa);
224 GLuint vao = 0;
225 glGenVertexArrays(1, &vao);
226 glBindVertexArray(vao);
227 GLuint vb = 0;
228 glCreateBuffers(1, &vb);
229 glBindBuffer(GL_ARRAY_BUFFER, vb);
230 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
231 GLuint ib = 0;
232 glCreateBuffers(1, &ib);
233 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib);
234 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof indices, indices, GL_STATIC_DRAW);
235 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
236 (const void *)offsetof(Vertex, position));
237 glVertexAttribPointer(1, GL_BGRA, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex),
238 (const void *)offsetof(Vertex, color));
239 glEnableVertexAttribArray(0);
240 glEnableVertexAttribArray(1);
242 glValidateProgram(program);
243 GLint status = GL_FALSE;
244 glGetProgramiv(program, GL_VALIDATE_STATUS, &status);
245 if (status != GL_TRUE) {
246 piglit_report_result(PIGLIT_FAIL);
249 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
250 glClear(GL_COLOR_BUFFER_BIT);
252 glDrawElementsBaseVertex(GL_TRIANGLES, ARRAY_SIZE(indices),
253 GL_UNSIGNED_SHORT, NULL, 0);
255 piglit_present_results();
257 glBindVertexArray(0);
258 glUseProgram(0);
259 glDeleteBuffers(1, &vb);
260 glDeleteBuffers(1, &ib);
261 glDeleteVertexArrays(1, &vao);
262 glDeleteProgram(program);
264 return PIGLIT_PASS;
267 void piglit_init(int argc, char **argv)
269 piglit_require_extension("GL_EXT_gpu_shader4");