3 # Copyright © 2011, 2014 Intel Corporation
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the "Software"),
7 # to deal in the Software without restriction, including without limitation
8 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 # and/or sell copies of the Software, and to permit persons to whom the
10 # Software is furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice (including the next
13 # paragraph) shall be included in all copies or substantial portions of the
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 # DEALINGS IN THE SOFTWARE.
24 """Generate Interpopation tests.
26 Correct interpolation of vertex shader outputs depends on (a)
27 whether an interpolation qualifier is present in the shader source,
28 and if so which qualifier is used, (b) if no interpolation qualifier
29 is present, whether the output is a user-defined variable or a
30 built-in color, and (c) if the output is a built-in color, the
31 setting of the ShadeModel() setting. In addition, we would like to
32 test correct interpolation under various clipping scenarios.
34 To verify that all the combinations of these possibilities work
35 correctly, this script generates a shader_runner test to check
36 proper interpolation for every combination of the following
39 - which interpolation qualifier is used ("flat", "noperspective",
40 "smooth", or no qualifier)
42 - which variable is used (gl_FrontColor, gl_BackColor,
43 gl_FrontSecondaryColor, gl_BackSecondaryColor, or a non-built-in
46 - the setting of ShadeModel() (either GL_SMOOTH or GL_FLAT)
48 - whether the triangle in question is clipped using gl_ClipVertex,
49 clipped using gl_ClipDistance, clipped against the fixed viewing
52 The tests operate by drawing a triangle with a different value of
53 the variable at each vertex, and then probing within the interior of
54 the triangle to verify that interpolation was performed correctly.
55 The triangle is drawn in a frustum projection, with a different z
56 value for each vertex, so that there will be a detectable difference
57 in behavior between noperspective and smooth interpolation.
59 When testing clipping, we clip off the frontmost corner of the
60 triangle; this ensures that the proportion of the triangle's screen
61 real estate that is clipped is significantly larger than the
62 proportion of the triangle's 3D coordinate space that is clipped.
63 So if the GL implementation doesn't perform perspective-correct
64 interpolation generating clipped vertices, we will notice.
66 This program outputs, to stdout, the name of each file it generates.
72 from templates
import template_file
73 from modules
import utils
75 TEMPLATE
= template_file(os
.path
.basename(os
.path
.splitext(__file__
)[0]),
76 'template.shader_test.mako')
80 def __init__(self
, interpolation_qualifier
, variable
, shade_model
,
82 """Get ready to generate a test using the given settings.
84 interpolation_qualifier is a string representing the desired
85 interpolation qualifier that should appear in GLSL code
86 ('flat', 'noperspective', or 'smooth'), or None if no
87 qualifier should appear.
89 variable is the name of the variable on which to test
90 interpolation. If the name begins with 'gl_', it should be
91 one of the four vertex shader variables that are allowed to be
92 redeclared with an interpolation qualifier (see GLSL 1.30
93 section 4.3.7 "Interpolation"). Namely: gl_FrontColor,
94 gl_BackColor, gl_FrontSecondaryColor, or
95 gl_BackSecondaryColor.
97 shade_model is which shade model the GL state should be put in
98 using the glShadeModel() command--either 'smooth' or 'flat'.
100 clipping is the variety of clipping which should be tested:
101 either 'fixed' to test a triangle that extends beyond the
102 fixed view volume (we test clipping against the "near" plane),
103 'vertex' to test a triangle which has one corner clipped using
104 gl_ClipVertex, or 'distance' to test a triangle which has one
105 corner clipped using gl_ClipDistance.
107 self
.interpolation_qualifier
= interpolation_qualifier
108 self
.vs_variable
= variable
109 self
.shade_model
= shade_model
110 self
.clipping
= clipping
112 # When colors are mapped into the fragment shader, the string
113 # 'Front' or 'Back' is dropped from the variable name, since
114 # e.g. gl_Color is mapped to gl_FrontColor for front-facing
115 # triangles, and gl_BackColor for back-facing triangles.
116 self
.fs_variable
= variable
.replace('Front', '').replace('Back', '')
118 # True if we are testing a BackColor, so we'll need to draw a
119 # back-facing triangle.
120 self
.backfacing
= variable
.find('Back') != -1
122 # True if we are testing a built-in color variable, False if
123 # we are testing a generic vertex shader output.
124 self
.builtin_variable
= variable
[:3] == 'gl_'
126 # Determine whether the test requires GLSL 1.30. If it does,
127 # use "in" and "out" to qualify shader inputs and outputs.
128 # Otherwise use the old keywords "attribute" and "varying".
129 # shader_runner will insert a #version directive based on
131 if self
.interpolation_qualifier
or self
.clipping
== 'distance':
132 self
.glsl_version
= '1.30'
134 self
.vs_output
= 'out'
137 self
.glsl_version
= '1.10'
138 self
.vs_input
= 'attribute'
139 self
.vs_output
= 'varying'
140 self
.fs_input
= 'varying'
142 # Determine the location of the near and far planes for the
143 # frustum projection. The triangle fits between z coordinates
144 # -1 and -3; we use 1.75 as the near plane when we want to
146 if self
.clipping
== 'fixed':
147 self
.frustum_near
= 1.75
149 self
.frustum_near
= 1.0
150 self
.frustum_far
= 3.0
152 # Determine whether we expect the GL implementation to use
153 # flatshading, non-perspective interpolation, or perspective
155 if self
.interpolation_qualifier
:
156 # According to GLSL 1.30 section 4.3.7 ("Interpolation"),
157 # "When an interpolation qualifier is used, it overrides
158 # settings established through the OpenGL API."
159 self
.expected_behavior
= self
.interpolation_qualifier
160 elif self
.builtin_variable
:
161 # According to GL 3.0 section 2.19.7 ("Flatshading"), "If
162 # a vertex shader is active, the flat shading control
163 # applies to the built-in varying variables gl FrontColor,
164 # gl BackColor, gl FrontSecondaryColor and gl
165 # BackSecondaryColor. Non-color varying variables can be
166 # specified as being flat-shaded via the flat qualifier,
167 # as described in section 4.3.6 of the OpenGL Shading
168 # Language Specification."
169 self
.expected_behavior
= self
.shade_model
171 # The specs do not explicitly state how non-built-in
172 # variables are to be interpolated in the case where no
173 # interpolation qualifier is used. However, it seems to
174 # be heavily implied by the text of GL 3.0 section 2.19.6
175 # ("Flatshading"--see above) that smooth
176 # (perspective-correct) interpolation is intended,
177 # regardless of the setting of glShadeModel().
178 self
.expected_behavior
= 'smooth'
182 'spec', 'glsl-{0}'.format(self
.glsl_version
),
183 'execution', 'interpolation',
184 'interpolation-{0}-{1}-{2}-{3}.shader_test'.format(
185 self
.interpolation_qualifier
or 'none', self
.vs_variable
,
186 self
.shade_model
, self
.clipping
or 'none'))
188 def vertex_data(self
):
189 table
= ['vertex/float/3 input_data/float/4',
190 '-1.0 -1.0 -1.0 1.0 0.0 0.0 1.0',
191 ' 0.0 2.0 -2.0 0.0 1.0 0.0 1.0',
192 ' 3.0 -3.0 -3.0 0.0 0.0 1.0 1.0']
193 if not self
.backfacing
:
194 # The vertices above are ordered such that the front of
195 # the triangle faces away from the viewer. If we are
196 # trying to render the front face, then swap the first two
197 # vertices. This shows us the front face of the triangle
198 # without changing the provoking vertex (which is the
200 table
= [table
[0], table
[2], table
[1], table
[3]]
203 def probe_data(self
):
204 # Loop over possible barycentric coordinates with a spacing of
205 # 1/num_subdivisions. Skip points on the triangle edges and
206 # corners so that rounding does not cause us to accidentally
207 # probe a pixel that's outside the triangle.
209 for i
in range(1, num_subdivisions
- 1):
210 for j
in range(1, num_subdivisions
- i
):
211 # Compute 3D barycentric coordinates--these will be
212 # used to compute the expected interpolated values
213 # when using smooth (perspective-correct)
214 # interpolation. The vertex associated with b3d_0=1.0
215 # is colored red, the vertex associated with b3d_1=1.0
216 # is colored green, and the vertex associated with
217 # b3d_2=1.0 is colored blue.
218 b3d_0
= float(num_subdivisions
- i
- j
)/num_subdivisions
219 b3d_1
= float(i
)/num_subdivisions
220 b3d_2
= float(j
)/num_subdivisions
221 # Compute 3D coordinates based on those barycentric
222 # coordinates. These will be used, among other
223 # things, to determine whether this part of the
224 # triangle is clipped.
225 x3d
= -b3d_0
+ 3.0*b3d_2
226 y3d
= -b3d_0
+ 2.0*b3d_1
- 3.0*b3d_2
227 z3d
= -b3d_0
- 2.0*b3d_1
- 3.0*b3d_2
228 # Use perspective division to compute 2D screen
229 # coordinates. These will be used with "relative
230 # probe rgba", which treats the lower left corner of
231 # the screen as (0, 0) and the upper right is (1, 1).
232 x2d
= (-x3d
/z3d
+ 1.0) / 2.0
233 y2d
= (-y3d
/z3d
+ 1.0) / 2.0
234 # Finally, compute a second set of barycentric
235 # coordinates based on the 2D screen
236 # coordinates--these will be used to compute the
237 # expected interpolated values when using
238 # noperspective (screen-coordinate) interpolation.
239 b2d_0
= 1.0 - x2d
- 0.5*y2d
241 b2d_2
= x2d
- 0.5*y2d
243 if self
.clipping
and -z3d
< 1.75:
244 # Points whose -z coordinate is less than 1.75
246 yield x2d
, y2d
, 0.0, 0.0, 0.0, 0.0
247 elif self
.expected_behavior
== 'flat':
248 # When flatshading, all points on the triangle
249 # should inherit the color of the third vertex,
251 yield x2d
, y2d
, 0.0, 0.0, 1.0, 1.0
252 elif self
.expected_behavior
== 'noperspective':
253 # Since the 3 triangle vertices are red, green,
254 # and blue, the interpolated color channels should
255 # be exactly equal to the barycentric coordinates.
256 # For "noperspective" shading, we use the
257 # barycentric coordinates that we computed based
258 # on 2D screen position.
259 yield x2d
, y2d
, b2d_0
, b2d_1
, b2d_2
, 1.0
261 # For "smooth" (perspective correct) shading, we
262 # use the barycentric coordinates that we used to
263 # compute the 3D position.
264 assert self
.expected_behavior
== 'smooth'
265 yield x2d
, y2d
, b3d_0
, b3d_1
, b3d_2
, 1.0
268 filename
= self
.filename()
269 dirname
= os
.path
.dirname(filename
)
270 utils
.safe_makedirs(dirname
)
271 with
open(filename
, 'w') as f
:
272 f
.write(TEMPLATE
.render_unicode(args
=self
))
276 for interpolation_qualifier
in ['flat', 'smooth', 'noperspective', '']:
277 for variable
in ['gl_FrontColor', 'gl_BackColor',
278 'gl_FrontSecondaryColor', 'gl_BackSecondaryColor',
280 for shade_model
in ['smooth', 'flat']:
281 for clipping
in ['vertex', 'distance', 'fixed', '']:
282 yield Test(interpolation_qualifier
, variable
, shade_model
,
287 for test
in all_tests():
289 print(test
.filename())
292 if __name__
== '__main__':