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.
73 from mako
import exceptions
75 from templates
import template_file
76 from modules
import utils
78 TEMPLATE
= template_file(os
.path
.basename(os
.path
.splitext(__file__
)[0]),
79 'template.shader_test.mako')
83 def __init__(self
, interpolation_qualifier
, variable
, shade_model
,
85 """Get ready to generate a test using the given settings.
87 interpolation_qualifier is a string representing the desired
88 interpolation qualifier that should appear in GLSL code
89 ('flat', 'noperspective', or 'smooth'), or None if no
90 qualifier should appear.
92 variable is the name of the variable on which to test
93 interpolation. If the name begins with 'gl_', it should be
94 one of the four vertex shader variables that are allowed to be
95 redeclared with an interpolation qualifier (see GLSL 1.30
96 section 4.3.7 "Interpolation"). Namely: gl_FrontColor,
97 gl_BackColor, gl_FrontSecondaryColor, or
98 gl_BackSecondaryColor.
100 shade_model is which shade model the GL state should be put in
101 using the glShadeModel() command--either 'smooth' or 'flat'.
103 clipping is the variety of clipping which should be tested:
104 either 'fixed' to test a triangle that extends beyond the
105 fixed view volume (we test clipping against the "near" plane),
106 'vertex' to test a triangle which has one corner clipped using
107 gl_ClipVertex, or 'distance' to test a triangle which has one
108 corner clipped using gl_ClipDistance.
110 self
.interpolation_qualifier
= interpolation_qualifier
111 self
.vs_variable
= variable
112 self
.shade_model
= shade_model
113 self
.clipping
= clipping
115 # When colors are mapped into the fragment shader, the string
116 # 'Front' or 'Back' is dropped from the variable name, since
117 # e.g. gl_Color is mapped to gl_FrontColor for front-facing
118 # triangles, and gl_BackColor for back-facing triangles.
119 self
.fs_variable
= variable
.replace('Front', '').replace('Back', '')
121 # True if we are testing a BackColor, so we'll need to draw a
122 # back-facing triangle.
123 self
.backfacing
= variable
.find('Back') != -1
125 # True if we are testing a built-in color variable, False if
126 # we are testing a generic vertex shader output.
127 self
.builtin_variable
= variable
[:3] == 'gl_'
129 # Determine whether the test requires GLSL 1.30. If it does,
130 # use "in" and "out" to qualify shader inputs and outputs.
131 # Otherwise use the old keywords "attribute" and "varying".
132 # shader_runner will insert a #version directive based on
134 if self
.interpolation_qualifier
or self
.clipping
== 'distance':
135 self
.glsl_version
= '1.30'
137 self
.vs_output
= 'out'
140 self
.glsl_version
= '1.10'
141 self
.vs_input
= 'attribute'
142 self
.vs_output
= 'varying'
143 self
.fs_input
= 'varying'
145 # Determine the location of the near and far planes for the
146 # frustum projection. The triangle fits between z coordinates
147 # -1 and -3; we use 1.75 as the near plane when we want to
149 if self
.clipping
== 'fixed':
150 self
.frustum_near
= 1.75
152 self
.frustum_near
= 1.0
153 self
.frustum_far
= 3.0
155 # Determine whether we expect the GL implementation to use
156 # flatshading, non-perspective interpolation, or perspective
158 if self
.interpolation_qualifier
:
159 # According to GLSL 1.30 section 4.3.7 ("Interpolation"),
160 # "When an interpolation qualifier is used, it overrides
161 # settings established through the OpenGL API."
162 self
.expected_behavior
= self
.interpolation_qualifier
163 elif self
.builtin_variable
:
164 # According to GL 3.0 section 2.19.7 ("Flatshading"), "If
165 # a vertex shader is active, the flat shading control
166 # applies to the built-in varying variables gl FrontColor,
167 # gl BackColor, gl FrontSecondaryColor and gl
168 # BackSecondaryColor. Non-color varying variables can be
169 # specified as being flat-shaded via the flat qualifier,
170 # as described in section 4.3.6 of the OpenGL Shading
171 # Language Specification."
172 self
.expected_behavior
= self
.shade_model
174 # The specs do not explicitly state how non-built-in
175 # variables are to be interpolated in the case where no
176 # interpolation qualifier is used. However, it seems to
177 # be heavily implied by the text of GL 3.0 section 2.19.6
178 # ("Flatshading"--see above) that smooth
179 # (perspective-correct) interpolation is intended,
180 # regardless of the setting of glShadeModel().
181 self
.expected_behavior
= 'smooth'
185 'spec', 'glsl-{0}'.format(self
.glsl_version
),
186 'execution', 'interpolation',
187 'interpolation-{0}-{1}-{2}-{3}.shader_test'.format(
188 self
.interpolation_qualifier
or 'none', self
.vs_variable
,
189 self
.shade_model
, self
.clipping
or 'none'))
191 def vertex_data(self
):
192 table
= ['vertex/float/3 input_data/float/4',
193 '-1.0 -1.0 -1.0 1.0 0.0 0.0 1.0',
194 ' 0.0 2.0 -2.0 0.0 1.0 0.0 1.0',
195 ' 3.0 -3.0 -3.0 0.0 0.0 1.0 1.0']
196 if not self
.backfacing
:
197 # The vertices above are ordered such that the front of
198 # the triangle faces away from the viewer. If we are
199 # trying to render the front face, then swap the first two
200 # vertices. This shows us the front face of the triangle
201 # without changing the provoking vertex (which is the
203 table
= [table
[0], table
[2], table
[1], table
[3]]
206 def probe_data(self
):
207 # Loop over possible barycentric coordinates with a spacing of
208 # 1/num_subdivisions. Skip points on the triangle edges and
209 # corners so that rounding does not cause us to accidentally
210 # probe a pixel that's outside the triangle.
212 for i
in range(1, num_subdivisions
- 1):
213 for j
in range(1, num_subdivisions
- i
):
214 # Compute 3D barycentric coordinates--these will be
215 # used to compute the expected interpolated values
216 # when using smooth (perspective-correct)
217 # interpolation. The vertex associated with b3d_0=1.0
218 # is colored red, the vertex associated with b3d_1=1.0
219 # is colored green, and the vertex associated with
220 # b3d_2=1.0 is colored blue.
221 b3d_0
= float(num_subdivisions
- i
- j
)/num_subdivisions
222 b3d_1
= float(i
)/num_subdivisions
223 b3d_2
= float(j
)/num_subdivisions
224 # Compute 3D coordinates based on those barycentric
225 # coordinates. These will be used, among other
226 # things, to determine whether this part of the
227 # triangle is clipped.
228 x3d
= -b3d_0
+ 3.0*b3d_2
229 y3d
= -b3d_0
+ 2.0*b3d_1
- 3.0*b3d_2
230 z3d
= -b3d_0
- 2.0*b3d_1
- 3.0*b3d_2
231 # Use perspective division to compute 2D screen
232 # coordinates. These will be used with "relative
233 # probe rgba", which treats the lower left corner of
234 # the screen as (0, 0) and the upper right is (1, 1).
235 x2d
= (-x3d
/z3d
+ 1.0) / 2.0
236 y2d
= (-y3d
/z3d
+ 1.0) / 2.0
237 # Finally, compute a second set of barycentric
238 # coordinates based on the 2D screen
239 # coordinates--these will be used to compute the
240 # expected interpolated values when using
241 # noperspective (screen-coordinate) interpolation.
242 b2d_0
= 1.0 - x2d
- 0.5*y2d
244 b2d_2
= x2d
- 0.5*y2d
246 if self
.clipping
and -z3d
< 1.75:
247 # Points whose -z coordinate is less than 1.75
249 yield x2d
, y2d
, 0.0, 0.0, 0.0, 0.0
250 elif self
.expected_behavior
== 'flat':
251 # When flatshading, all points on the triangle
252 # should inherit the color of the third vertex,
254 yield x2d
, y2d
, 0.0, 0.0, 1.0, 1.0
255 elif self
.expected_behavior
== 'noperspective':
256 # Since the 3 triangle vertices are red, green,
257 # and blue, the interpolated color channels should
258 # be exactly equal to the barycentric coordinates.
259 # For "noperspective" shading, we use the
260 # barycentric coordinates that we computed based
261 # on 2D screen position.
262 yield x2d
, y2d
, b2d_0
, b2d_1
, b2d_2
, 1.0
264 # For "smooth" (perspective correct) shading, we
265 # use the barycentric coordinates that we used to
266 # compute the 3D position.
267 assert self
.expected_behavior
== 'smooth'
268 yield x2d
, y2d
, b3d_0
, b3d_1
, b3d_2
, 1.0
271 filename
= self
.filename()
272 dirname
= os
.path
.dirname(filename
)
273 utils
.safe_makedirs(dirname
)
274 with
open(filename
, 'w') as f
:
276 f
.write(TEMPLATE
.render_unicode(args
=self
))
278 print(exceptions
.text_error_template().render(), file=sys
.stderr
)
283 for interpolation_qualifier
in ['flat', 'smooth', 'noperspective', '']:
284 for variable
in ['gl_FrontColor', 'gl_BackColor',
285 'gl_FrontSecondaryColor', 'gl_BackSecondaryColor',
287 for shade_model
in ['smooth', 'flat']:
288 for clipping
in ['vertex', 'distance', 'fixed', '']:
289 yield Test(interpolation_qualifier
, variable
, shade_model
,
294 for test
in all_tests():
296 print(test
.filename())
299 if __name__
== '__main__':