Merge commit 'origin/gallium-master-merge'
[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 pixels = FloatArray(surface.height*surface.width*4)
35 surface.get_tile_rgba(0, 0, surface.width, surface.height, pixels)
36
37 import Image
38 outimage = Image.new(
39 mode='RGB',
40 size=(surface.width, surface.height),
41 color=(0,0,0))
42 outpixels = outimage.load()
43 for y in range(0, surface.height):
44 for x in range(0, surface.width):
45 offset = (y*surface.width + x)*4
46 r, g, b, a = [int(pixels[offset + ch]*255) for ch in range(4)]
47 outpixels[x, y] = r, g, b
48 return outimage
49
50 def save_image(filename, surface):
51 outimage = make_image(surface)
52 outimage.save(filename, "PNG")
53
54 def show_image(surface):
55 outimage = make_image(surface)
56
57 import Tkinter as tk
58 from PIL import Image, ImageTk
59 root = tk.Tk()
60
61 root.title('background image')
62
63 image1 = ImageTk.PhotoImage(outimage)
64 w = image1.width()
65 h = image1.height()
66 x = 100
67 y = 100
68 root.geometry("%dx%d+%d+%d" % (w, h, x, y))
69 panel1 = tk.Label(root, image=image1)
70 panel1.pack(side='top', fill='both', expand='yes')
71 panel1.image = image1
72 root.mainloop()
73
74
75 def test(dev):
76 ctx = dev.context_create()
77
78 width = 255
79 height = 255
80
81 # disabled blending/masking
82 blend = Blend()
83 blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE
84 blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE
85 blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO
86 blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO
87 blend.colormask = PIPE_MASK_RGBA
88 ctx.set_blend(blend)
89
90 # no-op depth/stencil/alpha
91 depth_stencil_alpha = DepthStencilAlpha()
92 ctx.set_depth_stencil_alpha(depth_stencil_alpha)
93
94 # rasterizer
95 rasterizer = Rasterizer()
96 rasterizer.front_winding = PIPE_WINDING_CW
97 rasterizer.cull_mode = PIPE_WINDING_NONE
98 rasterizer.bypass_clipping = 1
99 rasterizer.scissor = 1
100 #rasterizer.bypass_vs = 1
101 ctx.set_rasterizer(rasterizer)
102
103 # viewport (identity, we setup vertices in wincoords)
104 viewport = Viewport()
105 scale = FloatArray(4)
106 scale[0] = 1.0
107 scale[1] = 1.0
108 scale[2] = 1.0
109 scale[3] = 1.0
110 viewport.scale = scale
111 translate = FloatArray(4)
112 translate[0] = 0.0
113 translate[1] = 0.0
114 translate[2] = 0.0
115 translate[3] = 0.0
116 viewport.translate = translate
117 ctx.set_viewport(viewport)
118
119 # samplers
120 sampler = Sampler()
121 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE
122 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE
123 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE
124 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE
125 sampler.min_img_filter = PIPE_TEX_MIPFILTER_NEAREST
126 sampler.mag_img_filter = PIPE_TEX_MIPFILTER_NEAREST
127 sampler.normalized_coords = 1
128 ctx.set_sampler(0, sampler)
129
130 # scissor
131 scissor = Scissor()
132 scissor.minx = 0
133 scissor.miny = 0
134 scissor.maxx = width
135 scissor.maxy = height
136 ctx.set_scissor(scissor)
137
138 clip = Clip()
139 clip.nr = 0
140 ctx.set_clip(clip)
141
142 # framebuffer
143 cbuf = dev.texture_create(
144 PIPE_FORMAT_X8R8G8B8_UNORM,
145 width, height,
146 tex_usage=PIPE_TEXTURE_USAGE_DISPLAY_TARGET,
147 )
148 _cbuf = cbuf.get_surface(usage = PIPE_BUFFER_USAGE_GPU_READ|PIPE_BUFFER_USAGE_GPU_WRITE)
149 fb = Framebuffer()
150 fb.width = width
151 fb.height = height
152 fb.num_cbufs = 1
153 fb.set_cbuf(0, _cbuf)
154 ctx.set_framebuffer(fb)
155 _cbuf.clear_value = 0x00000000
156 ctx.surface_clear(_cbuf, _cbuf.clear_value)
157 del _cbuf
158
159 # vertex shader
160 vs = Shader('''
161 VERT1.1
162 DCL IN[0], POSITION, CONSTANT
163 DCL IN[1], COLOR, CONSTANT
164 DCL OUT[0], POSITION, CONSTANT
165 DCL OUT[1], COLOR, CONSTANT
166 0:MOV OUT[0], IN[0]
167 1:MOV OUT[1], IN[1]
168 2:END
169 ''')
170 ctx.set_vertex_shader(vs)
171
172 # fragment shader
173 fs = Shader('''
174 FRAG1.1
175 DCL IN[0], COLOR, LINEAR
176 DCL OUT[0], COLOR, CONSTANT
177 0:MOV OUT[0], IN[0]
178 1:END
179 ''')
180 ctx.set_fragment_shader(fs)
181
182 nverts = 3
183 nattrs = 2
184 verts = FloatArray(nverts * nattrs * 4)
185
186 verts[ 0] = 128.0 # x1
187 verts[ 1] = 32.0 # y1
188 verts[ 2] = 0.0 # z1
189 verts[ 3] = 1.0 # w1
190 verts[ 4] = 1.0 # r1
191 verts[ 5] = 0.0 # g1
192 verts[ 6] = 0.0 # b1
193 verts[ 7] = 1.0 # a1
194 verts[ 8] = 32.0 # x2
195 verts[ 9] = 224.0 # y2
196 verts[10] = 0.0 # z2
197 verts[11] = 1.0 # w2
198 verts[12] = 0.0 # r2
199 verts[13] = 1.0 # g2
200 verts[14] = 0.0 # b2
201 verts[15] = 1.0 # a2
202 verts[16] = 224.0 # x3
203 verts[17] = 224.0 # y3
204 verts[18] = 0.0 # z3
205 verts[19] = 1.0 # w3
206 verts[20] = 0.0 # r3
207 verts[21] = 0.0 # g3
208 verts[22] = 1.0 # b3
209 verts[23] = 1.0 # a3
210
211 ctx.draw_vertices(PIPE_PRIM_TRIANGLES,
212 nverts,
213 nattrs,
214 verts)
215
216 ctx.flush()
217
218 show_image(cbuf.get_surface(usage = PIPE_BUFFER_USAGE_CPU_READ|PIPE_BUFFER_USAGE_CPU_WRITE))
219 #save_image('tri.png', cbuf.get_surface(usage = PIPE_BUFFER_USAGE_CPU_READ|PIPE_BUFFER_USAGE_CPU_WRITE))
220
221
222
223 def main():
224 dev = Device()
225 test(dev)
226
227
228 if __name__ == '__main__':
229 main()