1 /*
2 * Copyright 2018 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 * 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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /**
25 * Test vertex shader with gl_FrontMaterial and per-vertex materials.
26 *
27 * When using a VS, per-vertex materials are not used. Instead, the global
28 * material coefficients are the ones accessed via the gl_Front/BackMaterial
29 * built-in uniforms.
30 */
31
32 #include "piglit-util-gl.h"
33
34 PIGLIT_GL_TEST_CONFIG_BEGIN
35 config.supports_gl_compat_version = 20;
36 config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE;
37 config.khr_no_error_support = PIGLIT_NO_ERRORS;
38 PIGLIT_GL_TEST_CONFIG_END
39
40
41 /* VS: color = front ambient + front diffuse + two generic attributes */
42 static const char *vs =
43 "attribute vec4 generic1, generic2; \n"
44 "void main()\n"
45 "{\n"
46 " gl_FrontColor = gl_FrontMaterial.ambient + \n"
47 " gl_FrontMaterial.diffuse + \n"
48 " generic1 + generic2; \n"
49 " gl_Position = ftransform(); \n"
50 "}\n";
51
52 static const char *fs =
53 "uniform bool draw_secondary;\n"
54 "void main()\n"
55 "{\n"
56 " gl_FragColor = gl_Color;\n"
57 "}\n";
58
59
60 static int generic1_attr, generic2_attr;
61
62
63 static bool
test(bool use_dlist)64 test(bool use_dlist)
65 {
66 static const float red[] = {1.0, 0.0, 0.0, 1.0};
67 static const float green[] = {0.0, 1.0, 0.0, 1.0};
68 static const float ambient[] = {0.0, 0.3, 0.5, 1.0};
69 static const float diffuse[] = {0.0, 0.2, 0.3, 1.0};
70 static const float generic1[] = { 0.2, 0.3, -0.4, 0.0};
71 static const float generic2[] = {-0.2, 0.2, -0.4, 0.0};
72 static const float pos[4][2] = {
73 { -1, -1 },
74 { 1, -1 },
75 { 1, 1 },
76 { -1, 1 }
77 };
78 GLuint list = 0;
79 bool pass = true;
80
81 glClearColor(0.5, 0.5, 0.5, 0.5);
82 glClear(GL_COLOR_BUFFER_BIT);
83
84 if (use_dlist) {
85 list = glGenLists(1);
86 glNewList(1, GL_COMPILE);
87 }
88
89 /* These are the values picked up by gl_FrontMaterial.ambient, diffuse
90 */
91 glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
92 glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
93
94 glBegin(GL_TRIANGLE_FAN);
95 for (int i = 0; i < 4; i++) {
96 /* These material values should be ignored */
97 glMaterialfv(GL_FRONT, GL_AMBIENT, red);
98 glMaterialfv(GL_FRONT, GL_DIFFUSE, red);
99 glVertexAttrib4fv(generic1_attr, generic1);
100 glVertexAttrib4fv(generic2_attr, generic2);
101 glVertex2fv(pos[i]);
102 }
103 glEnd();
104
105 if (use_dlist) {
106 glEndList();
107 glCallList(list);
108 glDeleteLists(list, 1);
109 }
110
111 pass = piglit_probe_rect_rgba(0, 0, piglit_width, piglit_height,
112 green);
113 piglit_present_results();
114
115 if (!pass) {
116 printf("Fail while testing %s\n",
117 use_dlist ? "display list" : "immediate mode");
118 }
119
120 return pass;
121 }
122
123
124 enum piglit_result
piglit_display(void)125 piglit_display(void)
126 {
127 bool pass = test(false);
128 pass = test(true) && pass;
129 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
130 }
131
132
133 void
piglit_init(int argc,char ** argv)134 piglit_init(int argc, char **argv)
135 {
136 GLint prog = piglit_build_simple_program(vs, fs);
137 glUseProgram(prog);
138
139 generic1_attr = glGetAttribLocation(prog, "generic1");
140 generic2_attr = glGetAttribLocation(prog, "generic2");
141 }
142