python: More efficient blits from surfaces.
[mesa.git] / src / gallium / state_trackers / python / samples / tri.py
1 #!/usr/bin/env python
2 ##########################################################################
3 #
4 # Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
5 # All Rights Reserved.
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a
8 # copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sub license, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
14 #
15 # The above copyright notice and this permission notice (including the
16 # next paragraph) shall be included in all copies or substantial portions
17 # of the Software.
18 #
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 # IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 # ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #
27 ##########################################################################
28
29
30 from gallium import *
31
32
33 def make_image(surface):
34 data = surface.get_tile_rgba8(0, 0, surface.width, surface.height)
35
36 import Image
37 outimage = Image.fromstring('RGBA', (surface.width, surface.height), data, "raw", 'RGBA', 0, 1)
38 return outimage
39
40 def save_image(filename, surface):
41 outimage = make_image(surface)
42 outimage.save(filename, "PNG")
43
44 def show_image(surface):
45 outimage = make_image(surface)
46
47 import Tkinter as tk
48 from PIL import Image, ImageTk
49 root = tk.Tk()
50
51 root.title('background image')
52
53 image1 = ImageTk.PhotoImage(outimage)
54 w = image1.width()
55 h = image1.height()
56 x = 100
57 y = 100
58 root.geometry("%dx%d+%d+%d" % (w, h, x, y))
59 panel1 = tk.Label(root, image=image1)
60 panel1.pack(side='top', fill='both', expand='yes')
61 panel1.image = image1
62 root.mainloop()
63
64
65 def test(dev):
66 ctx = dev.context_create()
67
68 width = 255
69 height = 255
70
71 # disabled blending/masking
72 blend = Blend()
73 blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE
74 blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE
75 blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO
76 blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO
77 blend.colormask = PIPE_MASK_RGBA
78 ctx.set_blend(blend)
79
80 # no-op depth/stencil/alpha
81 depth_stencil_alpha = DepthStencilAlpha()
82 ctx.set_depth_stencil_alpha(depth_stencil_alpha)
83
84 # rasterizer
85 rasterizer = Rasterizer()
86 rasterizer.front_winding = PIPE_WINDING_CW
87 rasterizer.cull_mode = PIPE_WINDING_NONE
88 rasterizer.bypass_clipping = 1
89 rasterizer.scissor = 1
90 #rasterizer.bypass_vs = 1
91 ctx.set_rasterizer(rasterizer)
92
93 # viewport (identity, we setup vertices in wincoords)
94 viewport = Viewport()
95 scale = FloatArray(4)
96 scale[0] = 1.0
97 scale[1] = 1.0
98 scale[2] = 1.0
99 scale[3] = 1.0
100 viewport.scale = scale
101 translate = FloatArray(4)
102 translate[0] = 0.0
103 translate[1] = 0.0
104 translate[2] = 0.0
105 translate[3] = 0.0
106 viewport.translate = translate
107 ctx.set_viewport(viewport)
108
109 # samplers
110 sampler = Sampler()
111 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE
112 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE
113 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE
114 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE
115 sampler.min_img_filter = PIPE_TEX_MIPFILTER_NEAREST
116 sampler.mag_img_filter = PIPE_TEX_MIPFILTER_NEAREST
117 sampler.normalized_coords = 1
118 ctx.set_sampler(0, sampler)
119
120 # scissor
121 scissor = Scissor()
122 scissor.minx = 0
123 scissor.miny = 0
124 scissor.maxx = width
125 scissor.maxy = height
126 ctx.set_scissor(scissor)
127
128 clip = Clip()
129 clip.nr = 0
130 ctx.set_clip(clip)
131
132 # framebuffer
133 cbuf = dev.texture_create(
134 PIPE_FORMAT_X8R8G8B8_UNORM,
135 width, height,
136 tex_usage=PIPE_TEXTURE_USAGE_DISPLAY_TARGET,
137 )
138 _cbuf = cbuf.get_surface(usage = PIPE_BUFFER_USAGE_GPU_READ|PIPE_BUFFER_USAGE_GPU_WRITE)
139 fb = Framebuffer()
140 fb.width = width
141 fb.height = height
142 fb.num_cbufs = 1
143 fb.set_cbuf(0, _cbuf)
144 ctx.set_framebuffer(fb)
145 _cbuf.clear_value = 0x00000000
146 ctx.surface_clear(_cbuf, _cbuf.clear_value)
147 del _cbuf
148
149 # vertex shader
150 vs = Shader('''
151 VERT1.1
152 DCL IN[0], POSITION, CONSTANT
153 DCL IN[1], COLOR, CONSTANT
154 DCL OUT[0], POSITION, CONSTANT
155 DCL OUT[1], COLOR, CONSTANT
156 0:MOV OUT[0], IN[0]
157 1:MOV OUT[1], IN[1]
158 2:END
159 ''')
160 ctx.set_vertex_shader(vs)
161
162 # fragment shader
163 fs = Shader('''
164 FRAG1.1
165 DCL IN[0], COLOR, LINEAR
166 DCL OUT[0], COLOR, CONSTANT
167 0:MOV OUT[0], IN[0]
168 1:END
169 ''')
170 ctx.set_fragment_shader(fs)
171
172 nverts = 3
173 nattrs = 2
174 verts = FloatArray(nverts * nattrs * 4)
175
176 verts[ 0] = 128.0 # x1
177 verts[ 1] = 32.0 # y1
178 verts[ 2] = 0.0 # z1
179 verts[ 3] = 1.0 # w1
180 verts[ 4] = 1.0 # r1
181 verts[ 5] = 0.0 # g1
182 verts[ 6] = 0.0 # b1
183 verts[ 7] = 1.0 # a1
184 verts[ 8] = 32.0 # x2
185 verts[ 9] = 224.0 # y2
186 verts[10] = 0.0 # z2
187 verts[11] = 1.0 # w2
188 verts[12] = 0.0 # r2
189 verts[13] = 1.0 # g2
190 verts[14] = 0.0 # b2
191 verts[15] = 1.0 # a2
192 verts[16] = 224.0 # x3
193 verts[17] = 224.0 # y3
194 verts[18] = 0.0 # z3
195 verts[19] = 1.0 # w3
196 verts[20] = 0.0 # r3
197 verts[21] = 0.0 # g3
198 verts[22] = 1.0 # b3
199 verts[23] = 1.0 # a3
200
201 ctx.draw_vertices(PIPE_PRIM_TRIANGLES,
202 nverts,
203 nattrs,
204 verts)
205
206 ctx.flush()
207
208 show_image(cbuf.get_surface(usage = PIPE_BUFFER_USAGE_CPU_READ|PIPE_BUFFER_USAGE_CPU_WRITE))
209 #save_image('tri.png', cbuf.get_surface(usage = PIPE_BUFFER_USAGE_CPU_READ|PIPE_BUFFER_USAGE_CPU_WRITE))
210
211
212
213 def main():
214 dev = Device()
215 test(dev)
216
217
218 if __name__ == '__main__':
219 main()