[Piglit] [PATCH 6/6] Add tests of interpolation qualifier behavior.

Paul Berry stereotype441 at gmail.com
Thu Oct 20 14:12:28 PDT 2011


This patch adds 160 generated tests to verify correct behavior of the
GLSL 1.30 interpolation qualifiers under all combinations of the
following circumstances:

- When the interpolation qualifier is 'flat', 'noperspective',
  'smooth', or not specified.

- When the variable in question is 'gl_FrontColor', 'gl_BackColor',
  'gl_FrontSecondaryColor', 'gl_BackSecondaryColor', or a non-built-in
  vertex shader output.

- When the GL shade model (set with the glShadeModel command) is
  'GL_SMOOTH' or 'GL_FLAT'.

- When the triangle is clipped using gl_ClipDistance, using
  gl_ClipVertex, using fixed clip planes, or not clipped at all.

Note: in a perfect world, we wouldn't need to test all 160
combinations of these possibilities (e.g. the GL shade model should
only have an effect when no interpolation qualifier is specified, and
clipping should have no effect whatsoever on interpolation).  However,
based on what I know about how interpolation currently works in Mesa's
i965 driver, it seems prudent to err on the side of caution and test
all 160 possible combinations.

The tests all operate by painting a triangle on the screen using a
frustum projection, where the three vertices are colored red, green,
and blue, and each vertex has a different Z coordinate (so that we can
test proper perspective interpolation).  Then they use "relative probe
rgba" on various points in the interior of the triangle to verify that
the colors have been interpolated correctly.

I've validated the tests using nVidia's proprietary Linux driver, and
they all pass except for the tests that attempt to override the
default interpolation behavior of one of the built-in gl_Color
variables.  In those cases, the nVidia driver favors the interpolation
mode that is set in GL state.  GLSL 1.30 section 4.3.7
("Interpolation") explicitly states that the interpolation behavior of
these variables may be overridden, and that "When an interpolation
qualifier is used, it overrides settings established through the
OpenGL API."  So this seems pretty clearly to be due to a bug in the
nVidia driver.

As an aid in review, here is an example of one of the tests created by
the test generator script.  This one is called
"interpolation-noperspective-gl_FrontColor-flat-fixed.shader_test":

[require]
GLSL >= 1.30

[vertex shader]
in vec4 vertex;
in vec4 input_data;
noperspective out vec4 gl_FrontColor;
void main()
{
  gl_Position = gl_ModelViewProjectionMatrix * vertex;
  gl_FrontColor = input_data;
}

[fragment shader]
noperspective in vec4 gl_Color;
void main()
{
  gl_FragColor = gl_Color;
}

[vertex data]
vertex/float/3  input_data/float/4
 0.0  2.0 -2.0  0.0 1.0 0.0 1.0
-1.0 -1.0 -1.0  1.0 0.0 0.0 1.0
 3.0 -3.0 -3.0  0.0 0.0 1.0 1.0

[test]
frustum -1.75 1.75 -1.75 1.75 1.75 3.0
clear color 0.0 0.0 0.0 0.0
clear
enable GL_VERTEX_PROGRAM_TWO_SIDE
shade model flat
draw arrays GL_TRIANGLES 0 3
relative probe rgba (0.444444444444, 0.222222222222) (0.0, 0.0, 0.0, 0.0)
relative probe rgba (0.636363636364, 0.181818181818) (0.272727272727, 0.181818181818, 0.545454545455, 1.0)
relative probe rgba (0.769230769231, 0.153846153846) (0.153846153846, 0.153846153846, 0.692307692308, 1.0)
relative probe rgba (0.866666666667, 0.133333333333) (0.0666666666667, 0.133333333333, 0.8, 1.0)
relative probe rgba (0.5, 0.4) (0.0, 0.0, 0.0, 0.0)
relative probe rgba (0.666666666667, 0.333333333333) (0.166666666667, 0.333333333333, 0.5, 1.0)
relative probe rgba (0.785714285714, 0.285714285714) (0.0714285714286, 0.285714285714, 0.642857142857, 1.0)
relative probe rgba (0.545454545455, 0.545454545455) (0.181818181818, 0.545454545455, 0.272727272727, 1.0)
relative probe rgba (0.692307692308, 0.461538461538) (0.0769230769231, 0.461538461538, 0.461538461538, 1.0)
relative probe rgba (0.583333333333, 0.666666666667) (0.0833333333333, 0.666666666667, 0.25, 1.0)
---
 generated_tests/CMakeLists.txt             |    6 +-
 generated_tests/gen_interpolation_tests.py |  354 ++++++++++++++++++++++++++++
 2 files changed, 359 insertions(+), 1 deletions(-)
 create mode 100644 generated_tests/gen_interpolation_tests.py

diff --git a/generated_tests/CMakeLists.txt b/generated_tests/CMakeLists.txt
index 2843a5d..ea6072b 100644
--- a/generated_tests/CMakeLists.txt
+++ b/generated_tests/CMakeLists.txt
@@ -27,9 +27,13 @@ piglit_make_generated_tests(
 	constant_array_size_tests.list
 	gen_constant_array_size_tests.py
 	builtin_function.py)
+piglit_make_generated_tests(
+	interpolation_tests.list
+	gen_interpolation_tests.py)
 
 # Add a "gen-tests" target that can be used to generate all the
 # tests without doing any other compilation.
 add_custom_target(gen-tests ALL
 	DEPENDS builtin_uniform_tests.list
-		constant_array_size_tests.list)
+		constant_array_size_tests.list
+		interpolation_tests.list)
diff --git a/generated_tests/gen_interpolation_tests.py b/generated_tests/gen_interpolation_tests.py
new file mode 100644
index 0000000..a1799c4
--- /dev/null
+++ b/generated_tests/gen_interpolation_tests.py
@@ -0,0 +1,354 @@
+# coding=utf-8
+#
+# Copyright © 2011 Intel Corporation
+#
+# Permission is hereby granted, free of charge, to any person obtaining a
+# copy of this software and associated documentation files (the "Software"),
+# to deal in the Software without restriction, including without limitation
+# the rights to use, copy, modify, merge, publish, distribute, sublicense,
+# and/or sell copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice (including the next
+# paragraph) shall be included in all copies or substantial portions of the
+# Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+# DEALINGS IN THE SOFTWARE.
+
+# Correct interpolation of vertex shader outputs depends on (a)
+# whether an interpolation qualifier is present in the shader source,
+# and if so which qualifier is used, (b) if no interpolation qualifier
+# is present, whether the output is a user-defined variable or a
+# built-in color, and (c) if the output is a built-in color, the
+# setting of the ShadeModel() setting.  In addition, we would like to
+# test correct interpolation under various clipping scenarios.
+#
+# To verify that all the combinations of these possibilities work
+# correctly, this script generates a shader_runner test to check
+# proper interpolation for every combination of the following
+# variables:
+#
+# - which interpolation qualifier is used ("flat", "noperspective",
+#   "smooth", or no qualifier)
+#
+# - which variable is used (gl_FrontColor, gl_BackColor,
+#   gl_FrontSecondaryColor, gl_BackSecondaryColor, or a non-built-in
+#   variable)
+#
+# - the setting of ShadeModel() (either GL_SMOOTH or GL_FLAT)
+#
+# - whether the triangle in question is clipped using gl_ClipVertex,
+#   clipped using gl_ClipDistance, clipped against the fixed viewing
+#   volume, or unclipped.
+#
+# The tests operate by drawing a triangle with a different value of
+# the variable at each vertex, and then probing within the interior of
+# the triangle to verify that interpolation was performed correctly.
+# The triangle is drawn in a frustum projection, with a different z
+# value for each vertex, so that there will be a detectable difference
+# in behavior between noperspective and smooth interpolation.
+#
+# When testing clipping, we clip off the frontmost corner of the
+# triangle; this ensures that the proportion of the triangle's screen
+# real estate that is clipped is significantly larger than the
+# proportion of the triangle's 3D coordinate space that is clipped.
+# So if the GL implementation doesn't perform perspective-correct
+# interpolation generating clipped vertices, we will notice.
+#
+# This program outputs, to stdout, the name of each file it generates.
+
+import os
+
+
+class Test(object):
+    def __init__(self, interpolation_qualifier, variable, shade_model,
+		 clipping):
+	"""Get ready to generate a test using the given settings.
+
+	interpolation_qualifier is a string representing the desired
+	interpolation qualifier that should appear in GLSL code
+	('flat', 'noperspective', or 'smooth'), or None if no
+	qualifier should appear.
+
+	variable is the name of the variable on which to test
+	interpolation.  If the name begins with 'gl_', it should be
+	one of the four vertex shader variables that are allowed to be
+	redeclared with an interpolation qualifier (see GLSL 1.30
+	section 4.3.7 "Interpolation").  Namely: gl_FrontColor,
+	gl_BackColor, gl_FrontSecondaryColor, or
+	gl_BackSecondaryColor.
+
+	shade_model is which shade model the GL state should be put in
+	using the glShadeModel() command--either 'smooth' or 'flat'.
+
+	clipping is the variety of clipping which should be tested:
+	either 'fixed' to test a triangle that extends beyond the
+	fixed view volume (we test clipping against the "near" plane),
+	'vertex' to test a triangle which has one corner clipped using
+	gl_ClipVertex, or 'distance' to test a triangle which has one
+	corner clipped using gl_ClipDistance.
+	"""
+	self.interpolation_qualifier = interpolation_qualifier
+	self.vs_variable = variable
+	self.shade_model = shade_model
+	self.clipping = clipping
+
+	# When colors are mapped into the fragment shader, the string
+	# 'Front' or 'Back' is dropped from the variable name, since
+	# e.g. gl_Color is mapped to gl_FrontColor for front-facing
+	# triangles, and gl_BackColor for back-facing triangles.
+	self.fs_variable = variable.replace('Front', '').replace('Back', '')
+
+	# True if we are testing a BackColor, so we'll need to draw a
+	# back-facing triangle.
+	self.backfacing = variable.find('Back') != -1
+
+	# True if we are testing a built-in color variable, False if
+	# we are testing a generic vertex shader output.
+	self.builtin_variable = variable[:3] == 'gl_'
+
+	# Determine whether the test requires GLSL 1.30.  If it does,
+	# use an appropriate version directive and use "in" and "out"
+	# to qualify shader inputs and outputs.  Otherwise use the old
+	# keywords "attribute" and "varying".
+	if self.interpolation_qualifier or self.clipping == 'distance':
+	    self.glsl_version = '1.30'
+	    self.version_directive = '#version 130\n'
+	    self.vs_input = 'in'
+	    self.vs_output = 'out'
+	    self.fs_input = 'in'
+	else:
+	    self.glsl_version = '1.10'
+	    self.version_directive = ''
+	    self.vs_input = 'attribute'
+	    self.vs_output = 'varying'
+	    self.fs_input = 'varying'
+
+	# Determine the location of the near and far planes for the
+	# frustum projection.  The triangle fits between z coordinates
+	# -1 and -3; we use 1.75 as the near plane when we want to
+	# force clipping.
+	if self.clipping == 'fixed':
+	    self.frustum_near = 1.75
+	else:
+	    self.frustum_near = 1.0
+	self.frustum_far = 3.0
+
+	# Determine whether we expect the GL implementation to use
+	# flatshading, non-perspective interpolation, or perspective
+	# interpolation.
+	if self.interpolation_qualifier:
+	    # According to GLSL 1.30 section 4.3.7 ("Interpolation"),
+	    # "When an interpolation qualifier is used, it overrides
+	    # settings established through the OpenGL API."
+	    self.expected_behavior = self.interpolation_qualifier
+	elif self.builtin_variable:
+	    # According to GL 3.0 section 2.19.7 ("Flatshading"), "If
+	    # a vertex shader is active, the flat shading control
+	    # applies to the built-in varying variables gl FrontColor,
+	    # gl BackColor, gl FrontSecondaryColor and gl
+	    # BackSecondaryColor.  Non-color varying variables can be
+	    # specified as being flat-shaded via the flat qualifier,
+	    # as described in section 4.3.6 of the OpenGL Shading
+	    # Language Specification."
+	    self.expected_behavior = self.shade_model
+	else:
+	    # The specs do not explicitly state how non-built-in
+	    # variables are to be interpolated in the case where no
+	    # interpolation qualifier is used.  However, it seems to
+	    # be heavily implied by the text of GL 3.0 section 2.19.6
+	    # ("Flatshading"--see above) that smooth
+	    # (perspective-correct) interpolation is intended,
+	    # regardless of the setting of glShadeModel().
+	    self.expected_behavior = 'smooth'
+
+    def filename(self):
+	return os.path.join(
+	    'spec', 'glsl-{0}'.format(self.glsl_version),
+	    'execution', 'interpolation',
+	    'interpolation-{0}-{1}-{2}-{3}.shader_test'.format(
+		self.interpolation_qualifier or 'none', self.vs_variable,
+		self.shade_model, self.clipping or 'none'))
+
+    def vertex_data(self):
+	table = ['vertex/float/3  input_data/float/4',
+		 '-1.0 -1.0 -1.0  1.0 0.0 0.0 1.0',
+		 ' 0.0  2.0 -2.0  0.0 1.0 0.0 1.0',
+		 ' 3.0 -3.0 -3.0  0.0 0.0 1.0 1.0']
+	if not self.backfacing:
+	    # The vertices above are ordered such that the front of
+	    # the triangle faces away from the viewer.  If we are
+	    # trying to render the front face, then swap the first two
+	    # vertices.  This shows us the front face of the triangle
+	    # without changing the provoking vertex (which is the
+	    # third vertex).
+	    table = [table[0], table[2], table[1], table[3]]
+	return table
+
+    def probe_data(self):
+	# Loop over possible barycentric coordinates with a spacing of
+	# 1/num_subdivisions.  Skip points on the triangle edges and
+	# corners so that rounding does not cause us to accidentally
+	# probe a pixel that's outside the triangle.
+	num_subdivisions = 6
+	for i in xrange(1, num_subdivisions - 1):
+	    for j in xrange(1, num_subdivisions - i):
+		# Compute 3D barycentric coordinates--these will be
+		# used to compute the expected interpolated values
+		# when using smooth (perspective-correct)
+		# interpolation.  The vertex associated with b3d_0=1.0
+		# is colored red, the vertex associated with b3d_1=1.0
+		# is colored green, and the vertex associated with
+		# b3d_2=1.0 is colored blue.
+		b3d_0 = float(num_subdivisions - i - j)/num_subdivisions
+		b3d_1 = float(i)/num_subdivisions
+		b3d_2 = float(j)/num_subdivisions
+		# Compute 3D coordinates based on those barycentric
+		# coordinates.  These will be used, among other
+		# things, to determine whether this part of the
+		# triangle is clipped.
+		x3d = -b3d_0 + 3.0*b3d_2
+		y3d = -b3d_0 + 2.0*b3d_1 - 3.0*b3d_2
+		z3d = -b3d_0 - 2.0*b3d_1 - 3.0*b3d_2
+		# Use perspective division to compute 2D screen
+		# coordinates.  These will be used with "relative
+		# probe rgba", which treats the lower left corner of
+		# the screen as (0, 0) and the upper right is (1, 1).
+		x2d = (-x3d/z3d + 1.0) / 2.0
+		y2d = (-y3d/z3d + 1.0) / 2.0
+		# Finally, compute a second set of barycentric
+		# coordinates based on the 2D screen
+		# coordinates--these will be used to compute the
+		# expected interpolated values when using
+		# noperspective (screen-coordinate) interpolation.
+		b2d_0 = 1.0 - x2d - 0.5*y2d
+		b2d_1 = y2d
+		b2d_2 = x2d - 0.5*y2d
+
+		if self.clipping and -z3d < 1.75:
+		    # Points whose -z coordinate is less than 1.75
+		    # should be clipped.
+		    yield x2d, y2d, 0.0, 0.0, 0.0, 0.0
+		elif self.expected_behavior == 'flat':
+		    # When flatshading, all points on the triangle
+		    # should inherit the color of the third vertex,
+		    # which is blue.
+		    yield x2d, y2d, 0.0, 0.0, 1.0, 1.0
+		elif self.expected_behavior == 'noperspective':
+		    # Since the 3 triangle vertices are red, green,
+		    # and blue, the interpolated color channels should
+		    # be exactly equal to the barycentric coordinates.
+		    # For "noperspective" shading, we use the
+		    # barycentric coordinates that we computed based
+		    # on 2D screen position.
+		    yield x2d, y2d, b2d_0, b2d_1, b2d_2, 1.0
+		else:
+		    # For "smooth" (perspective correct) shading, we
+		    # use the barycentric coordinates that we used to
+		    # compute the 3D position.
+		    assert self.expected_behavior == 'smooth'
+		    yield x2d, y2d, b3d_0, b3d_1, b3d_2, 1.0
+
+    def generate(self):
+	if self.builtin_variable:
+	    test = '# Test proper interpolation of {0}\n'.format(
+		self.vs_variable)
+	else:
+	    test = '# Test proper interpolation of a non-built-in variable\n'
+	if self.interpolation_qualifier:
+	    test += '# When qualified with {0!r}\n'.format(
+		self.interpolation_qualifier)
+	else:
+	    test += '# When no interpolation qualifier present\n'
+	test += '# And ShadeModel is {0!r}\n'.format(self.shade_model)
+	if self.clipping == 'fixed':
+	    test += '# And clipping via fixed planes\n'
+	elif self.clipping == 'vertex':
+	    test += '# And clipping via gl_ClipVertex\n'
+	elif self.clipping == 'distance':
+	    test += '# And clipping via gl_ClipDistance\n'
+	else:
+	    assert self.clipping is None
+	test += '[require]\n'
+	test += 'GLSL >= {0}\n'.format(self.glsl_version)
+	test += '\n'
+	test += '[vertex shader]\n'
+	test += self.version_directive
+	test += '{0} vec4 vertex;\n'.format(self.vs_input)
+	test += '{0} vec4 input_data;\n'.format(self.vs_input)
+	if self.interpolation_qualifier or not self.builtin_variable:
+	    test += '{0} {1} vec4 {2};'.format(
+		self.interpolation_qualifier or '',
+		self.vs_output, self.vs_variable).strip() + '\n'
+	test += 'void main()\n'
+	test += '{\n'
+	test += '  gl_Position = gl_ModelViewProjectionMatrix * vertex;\n'
+	test += '  {0} = input_data;\n'.format(self.vs_variable)
+	if self.clipping == 'distance':
+	    test += '  gl_ClipDistance[0] = -1.75 - vertex.z;\n'
+	elif self.clipping == 'vertex':
+	    test += '  gl_ClipVertex = vertex;\n'
+	test += '}\n'
+	test += '\n'
+	test += '[fragment shader]\n'
+	test += self.version_directive
+	if self.interpolation_qualifier or not self.builtin_variable:
+	    test += '{0} {1} vec4 {2};'.format(
+		self.interpolation_qualifier or '',
+		self.fs_input, self.fs_variable).strip() + '\n'
+	test += 'void main()\n'
+	test += '{\n'
+	test += '  gl_FragColor = {0};\n'.format(self.fs_variable)
+	test += '}\n'
+	test += '\n'
+	test += '[vertex data]\n'
+	test += ''.join(s + '\n' for s in self.vertex_data())
+	test += '\n'
+	test += '[test]\n'
+	test += 'frustum -{0} {0} -{0} {0} {0} {1}\n'.format(
+	    self.frustum_near, self.frustum_far)
+	test += 'clear color 0.0 0.0 0.0 0.0\n'
+	test += 'clear\n'
+	test += 'enable GL_VERTEX_PROGRAM_TWO_SIDE\n'
+	test += 'shade model {0}\n'.format(self.shade_model)
+	if self.clipping == 'distance' or self.clipping == 'vertex':
+	    test += 'enable GL_CLIP_PLANE0\n'
+	if self.clipping == 'vertex':
+	    test += 'clip plane 0 0.0 0.0 -1.0 -1.75\n'
+	test += 'draw arrays GL_TRIANGLES 0 3\n'
+	for x, y, r, g, b, a in self.probe_data():
+	    test += ('relative probe rgba ({0}, {1}) ({2}, {3}, {4}, {5})\n'
+		     .format(x, y, r, g, b, a))
+	filename = self.filename()
+	dirname = os.path.dirname(filename)
+	if not os.path.exists(dirname):
+	    os.makedirs(dirname)
+	with open(filename, 'w') as f:
+	    f.write(test)
+
+
+def all_tests():
+    for interpolation_qualifier in ['flat', 'smooth', 'noperspective', None]:
+	for variable in ['gl_FrontColor', 'gl_BackColor',
+			 'gl_FrontSecondaryColor', 'gl_BackSecondaryColor',
+			 'other']:
+	    for shade_model in ['smooth', 'flat']:
+		for clipping in ['vertex', 'distance', 'fixed', None]:
+		    yield Test(interpolation_qualifier, variable, shade_model,
+			       clipping)
+
+
+def main():
+    for test in all_tests():
+	test.generate()
+	print test.filename()
+
+
+if __name__ == '__main__':
+    main()
-- 
1.7.6.4



More information about the Piglit mailing list