python: Test all miplevels.
authorJosé Fonseca <jrfonseca@tungstengraphics.com>
Wed, 16 Jul 2008 10:36:59 +0000 (19:36 +0900)
committerJosé Fonseca <jrfonseca@tungstengraphics.com>
Wed, 16 Jul 2008 10:36:59 +0000 (19:36 +0900)
src/gallium/state_trackers/python/tests/base.py
src/gallium/state_trackers/python/tests/texture.py

index d83abdfa7650855fea3e2ee78759486df156e904..c50abf7f69cefbfd676e5bbf4033d8e01bba4f01 100644 (file)
 ##########################################################################
 
 
+"""Base classes for tests.
+
+Loosely inspired on Python's unittest module.
+"""
+
+
 from gallium import *
 
 
@@ -87,17 +93,54 @@ def show_image(width, height, **rgbas):
     root.mainloop()
 
 
+class TestFailure(Exception):
 
-class Test:
+    pass
+
+class TestSkip(Exception):
     
+    pass
+
+
+class Test:
+
     def __init__(self):
         pass
+
+    def _run(self, result):
+        raise NotImplementedError
     
+    def run(self):
+        result = TestResult()
+        self._run(result)
+        result.summary()
+
+
+class TestCase(Test):
+    
+    def __init__(self, dev, **kargs):
+        Test.__init__(self)
+        self.dev = dev
+        self.__dict__.update(kargs)
+
     def description(self):
         raise NotImplementedError
         
-    def run(self):
+    def test(self):
         raise NotImplementedError
+    
+    def _run(self, result):
+        result.test_start(self)
+        try:
+            self.test()
+        except KeyboardInterrupt:
+            raise
+        except TestSkip:
+            result.test_skipped(self)
+        except TestFailure:
+            result.test_failed(self)
+        else:
+            result.test_passed(self)
 
 
 class TestSuite(Test):
@@ -112,21 +155,35 @@ class TestSuite(Test):
     def add_test(self, test):
         self.tests.append(test) 
     
-    def run(self):
+    def _run(self, result):
         for test in self.tests:
-            print "Running %s..." % test.description()
-            test.run()
+            test._run(result)
 
 
-class TextureTemplate:
+class TestResult:
     
-    def __init__(self, format=PIPE_FORMAT_R8G8B8A8_UNORM, width=1, height=1, depth=1, last_level=0, target=PIPE_TEXTURE_2D):
-        self.format = format
-        self.width = width
-        self.height = height
-        self.depth = depth
-        self.last_level = last_level
-        self.target = target
-
-
+    def __init__(self):
+        self.tests = 0
+        self.passed = 0
+        self.skipped = 0
+        self.failed = 0
+    
+    def test_start(self, test):
+        self.tests += 1
+        print "Running %s..." % test.description()
+    
+    def test_passed(self, test):
+        self.passed += 1
+        print "PASS"
+            
+    def test_skipped(self, test):
+        self.skipped += 1
+        print "SKIP"
+        
+    def test_failed(self):
+        self.failed += 1
+        print "FAIL"
 
+    def summary(self):
+        print "%u tests, %u passed, %u skipped, %u failed" % (self.tests, self.passed, self.skipped, self.failed)
\ No newline at end of file
index 7812d8aebf50d543ad14807621ddfe1df829bf61..7c7245e65c6c52404b39760bfc07870c1b4561e3 100644 (file)
@@ -52,30 +52,39 @@ def compare_rgba(width, height, rgba1, rgba2, tol=4.0/256):
     return errors == 0
 
 
-class TextureTest(Test):
-    
-    def __init__(self, **kargs):
-        Test.__init__(self)
-        self.__dict__.update(kargs)
+def lods(*dims):
+    size = max(dims)
+    lods = 0
+    while size:
+        lods += 1
+        size >>= 1
+    return lods
+
+
+def minify(dims, level = 1):
+    return [max(dim>>level, 1) for dim in dims]
+
 
+class TextureTest(TestCase):
+    
     def description(self):
-        return "%s %ux%u" % (formats[self.format], self.width, self.height)
-        
-    def run(self):
+        return "%s %ux%u level=%u" % (formats[self.format], self.width, self.height, self.level)
+    
+    def test(self):
         dev = self.dev
         
         format = self.format
         width = self.width
         height = self.height
+        level = self.level
+        
+        levels = lods(width, height)
+
         
         if not dev.is_format_supported(format, PIPE_TEXTURE):
-            print "SKIP"
-            return
-        if not dev.is_format_supported(format, PIPE_SURFACE):
-            print "SKIP"
-            return
+            raise TestSkip
         
-        ctx = dev.context_create()
+        ctx = self.dev.context_create()
     
         # disabled blending/masking
         blend = Blend()
@@ -123,15 +132,20 @@ class TextureTest(Test):
         sampler.min_img_filter = PIPE_TEX_MIPFILTER_NEAREST
         sampler.mag_img_filter = PIPE_TEX_MIPFILTER_NEAREST
         sampler.normalized_coords = 1
+        sampler.min_lod = level
+        sampler.max_lod = level
         ctx.set_sampler(0, sampler)
     
         #  texture 
         texture = dev.texture_create(format, 
                                      width, 
-                                     height)
+                                     height,
+                                     last_level = levels - 1)
         ctx.set_sampler_texture(0, texture)
         
-        expected_rgba = generate_data(texture.get_surface(usage = PIPE_BUFFER_USAGE_CPU_READ|PIPE_BUFFER_USAGE_CPU_WRITE))
+        expected_rgba = generate_data(texture.get_surface(
+            usage = PIPE_BUFFER_USAGE_CPU_READ|PIPE_BUFFER_USAGE_CPU_WRITE,
+            level = level))
         
         #  framebuffer 
         cbuf_tex = dev.texture_create(PIPE_FORMAT_A8R8G8B8_UNORM, 
@@ -181,8 +195,7 @@ class TextureTest(Test):
     
         x = 0
         y = 0
-        w = width
-        h = height
+        w, h = minify((width, height), level)
     
         verts[ 0] =     x # x1
         verts[ 1] =     y # y1
@@ -223,32 +236,35 @@ class TextureTest(Test):
                           verts)
     
         ctx.flush()
-
-        del ctx
-        
-        rgba = FloatArray(height*width*4)
+    
+        rgba = FloatArray(h*w*4)
 
         cbuf_tex.get_surface(usage = PIPE_BUFFER_USAGE_CPU_READ).get_tile_rgba(x, y, w, h, rgba)
 
-        if compare_rgba(width, height, rgba, expected_rgba):
-            print "OK"
-        else:
-            print "FAIL"
+        if not compare_rgba(w, h, rgba, expected_rgba):
         
-            show_image(width, height, Result=rgba, Expected=expected_rgba)
+            show_image(w, h, Result=rgba, Expected=expected_rgba)
             #save_image(width, height, rgba, "result.png")
             #save_image(width, height, expected_rgba, "expected.png")
             sys.exit(0)
+            
+            raise TestFailure
+
+        del ctx
+        
 
 
 def main():
     dev = Device()
     suite = TestSuite()
     formats = [PIPE_FORMAT_A8R8G8B8_UNORM, PIPE_FORMAT_YCBCR, PIPE_FORMAT_DXT1_RGB]
+    #formats = [PIPE_FORMAT_A8R8G8B8_UNORM, PIPE_FORMAT_DXT1_RGB]
     sizes = [64, 32, 16, 8, 4, 2]
     for format in formats:
         for size in sizes:
-            suite.add_test(TextureTest(dev=dev, format=format, width=size, height=size))
+            levels = lods(size)
+            for level in range(levels):
+                suite.add_test(TextureTest(dev=dev, format=format, width=size, height=size, level=level))
     suite.run()