python: Request/respect the texture & buffer usage flags in the examples.
authorJosé Fonseca <jrfonseca@tungstengraphics.com>
Tue, 15 Jul 2008 08:14:07 +0000 (17:14 +0900)
committerJosé Fonseca <jrfonseca@tungstengraphics.com>
Tue, 15 Jul 2008 08:58:48 +0000 (17:58 +0900)
src/gallium/state_trackers/python/samples/tri.py
src/gallium/state_trackers/python/tests/base.py
src/gallium/state_trackers/python/tests/texture.py

index f915c2621537b8dc3652463c904a22b2c035ee6a..e5044a02458bc9708faaf27e3b7d98fadbf50bb1 100644 (file)
@@ -30,7 +30,7 @@
 from gallium import *
 
 
-def save_image(filename, surface):
+def make_image(surface):
     pixels = FloatArray(surface.height*surface.width*4)
     surface.get_tile_rgba(0, 0, surface.width, surface.height, pixels)
 
@@ -45,14 +45,38 @@ def save_image(filename, surface):
             offset = (y*surface.width + x)*4
             r, g, b, a = [int(pixels[offset + ch]*255) for ch in range(4)]
             outpixels[x, y] = r, g, b
+    return outimage
+
+def save_image(filename, surface):
+    outimage = make_image(surface)
     outimage.save(filename, "PNG")
 
+def show_image(surface):
+    outimage = make_image(surface)
+    
+    import Tkinter as tk
+    from PIL import Image, ImageTk
+    root = tk.Tk()
+    
+    root.title('background image')
+    
+    image1 = ImageTk.PhotoImage(outimage)
+    w = image1.width()
+    h = image1.height()
+    x = 100
+    y = 100
+    root.geometry("%dx%d+%d+%d" % (w, h, x, y))
+    panel1 = tk.Label(root, image=image1)
+    panel1.pack(side='top', fill='both', expand='yes')
+    panel1.image = image1
+    root.mainloop()
+
 
 def test(dev):
     ctx = dev.context_create()
 
-    width = 256
-    height = 256
+    width = 255
+    height = 255
 
     # disabled blending/masking
     blend = Blend()
@@ -72,6 +96,7 @@ def test(dev):
     rasterizer.front_winding = PIPE_WINDING_CW
     rasterizer.cull_mode = PIPE_WINDING_NONE
     rasterizer.bypass_clipping = 1
+    rasterizer.scissor = 1
     #rasterizer.bypass_vs = 1
     ctx.set_rasterizer(rasterizer)
 
@@ -102,21 +127,41 @@ def test(dev):
     sampler.normalized_coords = 1
     ctx.set_sampler(0, sampler)
 
-    #  texture 
-    texture = dev.texture_create(PIPE_FORMAT_A8R8G8B8_UNORM, 
-                                 width, height, 
-                                 usage=PIPE_TEXTURE_USAGE_RENDER_TARGET)
-    ctx.set_sampler_texture(0, texture)
-
-    #  drawing dest 
-    surface = texture.get_surface(usage = PIPE_BUFFER_USAGE_GPU_WRITE)
+    # scissor
+    scissor = Scissor()
+    scissor.minx = 0
+    scissor.miny = 0
+    scissor.maxx = width
+    scissor.maxy = height
+    ctx.set_scissor(scissor)
+
+    clip = Clip()
+    clip.nr = 0
+    ctx.set_clip(clip)
+
+    # framebuffer
+    cbuf = dev.texture_create(PIPE_FORMAT_X8R8G8B8_UNORM, 
+                              width, height,
+                              usage=PIPE_TEXTURE_USAGE_DISPLAY_TARGET)
+    zbuf = dev.texture_create(PIPE_FORMAT_X8Z24_UNORM, 
+                              width, height,
+                              usage=PIPE_TEXTURE_USAGE_DEPTH_STENCIL)
+    _cbuf = cbuf.get_surface(usage = PIPE_BUFFER_USAGE_GPU_READ|PIPE_BUFFER_USAGE_GPU_WRITE)
+    _zsbuf = zbuf.get_surface(usage = PIPE_BUFFER_USAGE_GPU_READ|PIPE_BUFFER_USAGE_GPU_WRITE)
     fb = Framebuffer()
-    fb.width = surface.width
-    fb.height = surface.height
+    fb.width = width
+    fb.height = height
     fb.num_cbufs = 1
-    fb.set_cbuf(0, surface)
+    fb.set_cbuf(0, _cbuf)
+    fb.set_zsbuf(_zsbuf)
     ctx.set_framebuffer(fb)
-
+    _cbuf.clear_value = 0x00000000
+    _zsbuf.clear_value = 0x00ffffff
+    ctx.surface_clear(_cbuf, _cbuf.clear_value)
+    ctx.surface_clear(_zsbuf, _zsbuf.clear_value)
+    del _cbuf
+    del _zsbuf
+    
     # vertex shader
     vs = Shader('''
         VERT1.1
@@ -171,16 +216,14 @@ def test(dev):
     verts[22] =   1.0 # b3
     verts[23] =   1.0 # a3
 
-    ctx.surface_clear(surface, 0x00000000)
-    
     ctx.draw_vertices(PIPE_PRIM_TRIANGLES,
                       nverts, 
                       nattrs, 
                       verts)
 
     ctx.flush()
-
-    save_image("tri.png", surface)
+    
+    show_image(cbuf.get_surface(usage = PIPE_BUFFER_USAGE_CPU_READ|PIPE_BUFFER_USAGE_CPU_WRITE))
 
 
 
index 5778ad4382ab93260a70b950de1f7bda28ccbfbf..185a59996e67db0e866ed6a3711bca1dd47ffe53 100644 (file)
@@ -37,7 +37,7 @@ for name, value in globals().items():
         formats[value] = name
 
 
-def save_image(filename, surface):
+def make_image(surface):
     pixels = FloatArray(surface.height*surface.width*4)
     surface.get_tile_rgba(0, 0, surface.width, surface.height, pixels)
 
@@ -52,8 +52,41 @@ def save_image(filename, surface):
             offset = (y*surface.width + x)*4
             r, g, b, a = [int(pixels[offset + ch]*255) for ch in range(4)]
             outpixels[x, y] = r, g, b
+    return outimage
+
+def save_image(filename, surface):
+    outimage = make_image(surface)
     outimage.save(filename, "PNG")
 
+def show_image(*surfaces):
+    import Tkinter as tk
+    from PIL import Image, ImageTk
+
+    root = tk.Tk()
+    
+    x = 64
+    y = 64
+    
+    for i in range(len(surfaces)):
+        surface = surfaces[i]
+        outimage = make_image(surface)
+        
+        if i:
+            window = tk.Toplevel(root)
+        else:
+            window = root    
+        window.title('Image %u' % (i+1))
+        image1 = ImageTk.PhotoImage(outimage)
+        w = image1.width()
+        h = image1.height()
+        window.geometry("%dx%d+%d+%d" % (w, h, x, y))
+        panel1 = tk.Label(window, image=image1)
+        panel1.pack(side='top', fill='both', expand='yes')
+        panel1.image = image1
+        x += w + 2
+    
+    root.mainloop()
+
 
 
 class Test:
index ad11e8dcc9040fd12a3ab749c0e32b6a9a9000c1..84ceebb169413565c67436b43a6f66bd187fc463 100644 (file)
 ##########################################################################
 
 
+import sys
 from gallium import *
 from base import *
 from data import generate_data
 
 
 def compare_rgba(width, height, rgba1, rgba2, tol=0.01):
-    result = True
+    errors = 0
     for y in range(0, height):
         for x in range(0, width):
             for ch in range(4):
@@ -41,9 +42,14 @@ def compare_rgba(width, height, rgba1, rgba2, tol=0.01):
                 v1 = rgba1[offset]
                 v2 = rgba2[offset]
                 if abs(v1 - v2) > tol:
-                    sys.stderr.write("x=%u, y=%u, ch=%u differ: %f vs %f\n", 
-                                     x, y, ch, v1, v2)
-                    result = False
+                    if errors == 0:
+                        sys.stderr.write("x=%u, y=%u, ch=%u differ: %f vs %f\n" % (x, y, ch, v1, v2))
+                    if errors == 1:
+                        sys.stderr.write("...\n")
+                    errors += 1
+    if errors:
+        sys.stderr.write("%u out of %u pixels differ\n" % (errors/4, height*width))
+    return errors == 0
 
 
 class TextureTest(Test):
@@ -65,8 +71,8 @@ class TextureTest(Test):
         
         ctx = dev.context_create()
     
-        width = 256
-        height = 256
+        width = 64
+        height = 64
     
         # disabled blending/masking
         blend = Blend()
@@ -122,21 +128,21 @@ class TextureTest(Test):
                                      height)
         ctx.set_sampler_texture(0, texture)
         
-        surface = texture.get_surface(usage = PIPE_BUFFER_USAGE_CPU_READ|PIPE_BUFFER_USAGE_CPU_WRITE)
-
-        expected_rgba = generate_data(surface)
+        expected_rgba = generate_data(texture.get_surface(usage = PIPE_BUFFER_USAGE_CPU_READ|PIPE_BUFFER_USAGE_CPU_WRITE))
         
         cbuf_tex = dev.texture_create(PIPE_FORMAT_A8R8G8B8_UNORM, 
                                       width, 
-                                      height)
+                                      height,
+                                      usage = PIPE_TEXTURE_USAGE_RENDER_TARGET)
 
         #  drawing dest 
-        cbuf = cbuf_tex.get_surface(usage = PIPE_BUFFER_USAGE_GPU_WRITE)
+        cbuf = cbuf_tex.get_surface(usage = PIPE_BUFFER_USAGE_GPU_WRITE|PIPE_BUFFER_USAGE_GPU_READ)
         fb = Framebuffer()
-        fb.width = cbuf.width
-        fb.height = cbuf.height
+        fb.width = width
+        fb.height = height
         fb.num_cbufs = 1
         fb.set_cbuf(0, cbuf)
+        ctx.surface_clear(cbuf, 0x00000000)
         ctx.set_framebuffer(fb)
     
         # vertex shader
@@ -164,7 +170,7 @@ class TextureTest(Test):
         ''')
         fs.dump()
         ctx.set_fragment_shader(fs)
-    
+
         nverts = 4
         nattrs = 2
         verts = FloatArray(nverts * nattrs * 4)
@@ -207,23 +213,21 @@ class TextureTest(Test):
         verts[30] =   0.0
         verts[31] =   0.0
     
-        ctx.surface_clear(cbuf, 0x00000000)
-        
         ctx.draw_vertices(PIPE_PRIM_TRIANGLE_FAN,
                           nverts, 
                           nattrs, 
                           verts)
     
         ctx.flush()
-    
-        rgba = FloatArray(surface.height*surface.width*4)
 
-        cbuf.get_tile_rgba(x, y, w, h, rgba)
+        rgba = FloatArray(height*width*4)
+
+        cbuf_tex.get_surface(usage = PIPE_BUFFER_USAGE_CPU_READ).get_tile_rgba(x, y, w, h, rgba)
 
         compare_rgba(width, height, rgba, expected_rgba)
         
-        #save_image("texture1.png", surface)
-        #save_image("texture2.png", cbuf)
+        show_image(texture.get_surface(usage = PIPE_BUFFER_USAGE_CPU_READ), 
+                   cbuf_tex.get_surface(usage = PIPE_BUFFER_USAGE_CPU_READ))
 
 
 def main():