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)
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()
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)
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
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))
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)
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:
##########################################################################
+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):
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):
ctx = dev.context_create()
- width = 256
- height = 256
+ width = 64
+ height = 64
# disabled blending/masking
blend = Blend()
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
''')
fs.dump()
ctx.set_fragment_shader(fs)
-
+
nverts = 4
nattrs = 2
verts = FloatArray(nverts * nattrs * 4)
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():