Merge branch '7.8'
[mesa.git] / src / gallium / tests / python / samples / gs.py
1 #!/usr/bin/env python
2 ##########################################################################
3 #
4 # Copyright 2009 VMware
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 minz = 0.0
71 maxz = 1.0
72
73 # disabled blending/masking
74 blend = Blend()
75 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE
76 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE
77 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO
78 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO
79 blend.rt[0].colormask = PIPE_MASK_RGBA
80 ctx.set_blend(blend)
81
82 # depth/stencil/alpha
83 depth_stencil_alpha = DepthStencilAlpha()
84 depth_stencil_alpha.depth.enabled = 1
85 depth_stencil_alpha.depth.writemask = 1
86 depth_stencil_alpha.depth.func = PIPE_FUNC_LESS
87 ctx.set_depth_stencil_alpha(depth_stencil_alpha)
88
89 # rasterizer
90 rasterizer = Rasterizer()
91 rasterizer.front_winding = PIPE_WINDING_CW
92 rasterizer.cull_mode = PIPE_WINDING_NONE
93 rasterizer.scissor = 1
94 ctx.set_rasterizer(rasterizer)
95
96 # viewport
97 viewport = Viewport()
98 scale = FloatArray(4)
99 scale[0] = width / 2.0
100 scale[1] = -height / 2.0
101 scale[2] = (maxz - minz) / 2.0
102 scale[3] = 1.0
103 viewport.scale = scale
104 translate = FloatArray(4)
105 translate[0] = width / 2.0
106 translate[1] = height / 2.0
107 translate[2] = (maxz - minz) / 2.0
108 translate[3] = 0.0
109 viewport.translate = translate
110 ctx.set_viewport(viewport)
111
112 # samplers
113 sampler = Sampler()
114 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE
115 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE
116 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE
117 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE
118 sampler.min_img_filter = PIPE_TEX_MIPFILTER_NEAREST
119 sampler.mag_img_filter = PIPE_TEX_MIPFILTER_NEAREST
120 sampler.normalized_coords = 1
121 ctx.set_sampler(0, sampler)
122
123 # scissor
124 scissor = Scissor()
125 scissor.minx = 0
126 scissor.miny = 0
127 scissor.maxx = width
128 scissor.maxy = height
129 ctx.set_scissor(scissor)
130
131 clip = Clip()
132 clip.nr = 0
133 ctx.set_clip(clip)
134
135 # framebuffer
136 cbuf = dev.resource_create(
137 PIPE_FORMAT_B8G8R8X8_UNORM,
138 width, height,
139 bind=PIPE_BIND_RENDER_TARGET,
140 ).get_surface()
141 zbuf = dev.resource_create(
142 PIPE_FORMAT_Z32_UNORM,
143 width, height,
144 bind=PIPE_BIND_DEPTH_STENCIL,
145 ).get_surface()
146 fb = Framebuffer()
147 fb.width = width
148 fb.height = height
149 fb.nr_cbufs = 1
150 fb.set_cbuf(0, cbuf)
151 fb.set_zsbuf(zbuf)
152 ctx.set_framebuffer(fb)
153 rgba = FloatArray(4);
154 rgba[0] = 0.0
155 rgba[1] = 0.0
156 rgba[2] = 0.0
157 rgba[3] = 0.0
158 ctx.clear(PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTHSTENCIL, rgba, 1.0, 0xff)
159
160 # vertex shader
161 vs = Shader('''
162 VERT
163 DCL IN[0], POSITION, CONSTANT
164 DCL IN[1], COLOR, CONSTANT
165 DCL OUT[0], POSITION, CONSTANT
166 DCL OUT[1], COLOR, CONSTANT
167 0:MOV OUT[0], IN[0]
168 1:MOV OUT[1], IN[1]
169 2:END
170 ''')
171 ctx.set_vertex_shader(vs)
172
173 gs = Shader('''
174 GEOM
175 PROPERTY GS_INPUT_PRIMITIVE TRIANGLES
176 PROPERTY GS_OUTPUT_PRIMITIVE TRIANGLE_STRIP
177 DCL IN[][0], POSITION, CONSTANT
178 DCL IN[][1], COLOR, CONSTANT
179 DCL OUT[0], POSITION, CONSTANT
180 DCL OUT[1], COLOR, CONSTANT
181 0:MOV OUT[0], IN[0][0]
182 1:MOV OUT[1], IN[0][1]
183 2:EMIT
184 3:MOV OUT[0], IN[1][0]
185 4:MOV OUT[1], IN[1][1]
186 5:EMIT
187 6:MOV OUT[0], IN[2][0]
188 7:MOV OUT[1], IN[2][1]
189 8:EMIT
190 9:ENDPRIM
191 10:END
192 ''')
193 ctx.set_geometry_shader(gs)
194
195 # fragment shader
196 fs = Shader('''
197 FRAG
198 DCL IN[0], COLOR, LINEAR
199 DCL OUT[0], COLOR, CONSTANT
200 0:MOV OUT[0], IN[0]
201 1:END
202 ''')
203 ctx.set_fragment_shader(fs)
204
205 nverts = 3
206 nattrs = 2
207 verts = FloatArray(nverts * nattrs * 4)
208
209 verts[ 0] = 0.0 # x1
210 verts[ 1] = 0.8 # y1
211 verts[ 2] = 0.2 # z1
212 verts[ 3] = 1.0 # w1
213 verts[ 4] = 1.0 # r1
214 verts[ 5] = 0.0 # g1
215 verts[ 6] = 0.0 # b1
216 verts[ 7] = 1.0 # a1
217 verts[ 8] = -0.8 # x2
218 verts[ 9] = -0.8 # y2
219 verts[10] = 0.5 # z2
220 verts[11] = 1.0 # w2
221 verts[12] = 0.0 # r2
222 verts[13] = 1.0 # g2
223 verts[14] = 0.0 # b2
224 verts[15] = 1.0 # a2
225 verts[16] = 0.8 # x3
226 verts[17] = -0.8 # y3
227 verts[18] = 0.8 # z3
228 verts[19] = 1.0 # w3
229 verts[20] = 0.0 # r3
230 verts[21] = 0.0 # g3
231 verts[22] = 1.0 # b3
232 verts[23] = 1.0 # a3
233
234 ctx.draw_vertices(PIPE_PRIM_TRIANGLES,
235 nverts,
236 nattrs,
237 verts)
238
239 ctx.flush()
240
241 show_image(cbuf)
242 #show_image(zbuf)
243 #save_image('cbuf.png', cbuf)
244 #save_image('zbuf.png', zbuf)
245
246
247
248 def main():
249 dev = Device()
250 test(dev)
251
252
253 if __name__ == '__main__':
254 main()