python/regress: Do not create zbuf for vertex shader test.
[mesa.git] / src / gallium / state_trackers / python / tests / regress / vertex-shader / vertex-shader.py
1 #!/usr/bin/env python
2 ##########################################################################
3 #
4 # Copyright 2009 VMware, Inc.
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 VMWARE 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 def make_image(surface):
33 data = surface.get_tile_rgba8(0, 0, surface.width, surface.height)
34
35 import Image
36 outimage = Image.fromstring('RGBA', (surface.width, surface.height), data, "raw", 'RGBA', 0, 1)
37 return outimage
38
39 def save_image(filename, surface):
40 outimage = make_image(surface)
41 outimage.save(filename, "PNG")
42
43 def test(dev, name):
44 ctx = dev.context_create()
45
46 width = 320
47 height = 320
48 minz = 0.0
49 maxz = 1.0
50
51 # disabled blending/masking
52 blend = Blend()
53 blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE
54 blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE
55 blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO
56 blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO
57 blend.colormask = PIPE_MASK_RGBA
58 ctx.set_blend(blend)
59
60 # depth/stencil/alpha
61 depth_stencil_alpha = DepthStencilAlpha()
62 depth_stencil_alpha.depth.enabled = 0
63 depth_stencil_alpha.depth.writemask = 1
64 depth_stencil_alpha.depth.func = PIPE_FUNC_LESS
65 ctx.set_depth_stencil_alpha(depth_stencil_alpha)
66
67 # rasterizer
68 rasterizer = Rasterizer()
69 rasterizer.front_winding = PIPE_WINDING_CW
70 rasterizer.cull_mode = PIPE_WINDING_NONE
71 rasterizer.scissor = 1
72 ctx.set_rasterizer(rasterizer)
73
74 # viewport
75 viewport = Viewport()
76 scale = FloatArray(4)
77 scale[0] = width / 2.0
78 scale[1] = -height / 2.0
79 scale[2] = (maxz - minz) / 2.0
80 scale[3] = 1.0
81 viewport.scale = scale
82 translate = FloatArray(4)
83 translate[0] = width / 2.0
84 translate[1] = height / 2.0
85 translate[2] = (maxz - minz) / 2.0
86 translate[3] = 0.0
87 viewport.translate = translate
88 ctx.set_viewport(viewport)
89
90 # samplers
91 sampler = Sampler()
92 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE
93 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE
94 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE
95 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE
96 sampler.min_img_filter = PIPE_TEX_MIPFILTER_NEAREST
97 sampler.mag_img_filter = PIPE_TEX_MIPFILTER_NEAREST
98 sampler.normalized_coords = 1
99 ctx.set_sampler(0, sampler)
100
101 # scissor
102 scissor = Scissor()
103 scissor.minx = 0
104 scissor.miny = 0
105 scissor.maxx = width
106 scissor.maxy = height
107 ctx.set_scissor(scissor)
108
109 clip = Clip()
110 clip.nr = 0
111 ctx.set_clip(clip)
112
113 # framebuffer
114 cbuf = dev.texture_create(
115 PIPE_FORMAT_X8R8G8B8_UNORM,
116 width, height,
117 tex_usage=PIPE_TEXTURE_USAGE_DISPLAY_TARGET,
118 ).get_surface()
119 fb = Framebuffer()
120 fb.width = width
121 fb.height = height
122 fb.nr_cbufs = 1
123 fb.set_cbuf(0, cbuf)
124 ctx.set_framebuffer(fb)
125 ctx.surface_clear(cbuf, 0x80808080)
126
127 # vertex shader
128 vs = Shader(file('vert-' + name + '.sh', 'rt').read())
129 ctx.set_vertex_shader(vs)
130
131 # fragment shader
132 fs = Shader('''
133 FRAG1.1
134 DCL IN[0], COLOR, LINEAR
135 DCL OUT[0], COLOR, CONSTANT
136 0:MOV OUT[0], IN[0]
137 1:END
138 ''')
139 ctx.set_fragment_shader(fs)
140
141 xy = [
142 0.0, 0.8,
143 -0.2, 0.4,
144 0.2, 0.4,
145 -0.4, 0.0,
146 0.0, 0.0,
147 0.4, 0.0,
148 -0.6, -0.4,
149 -0.2, -0.4,
150 0.2, -0.4,
151 0.6, -0.4,
152 -0.8, -0.8,
153 -0.4, -0.8,
154 0.0, -0.8,
155 0.4, -0.8,
156 0.8, -0.8,
157 ]
158 color = [
159 1.0, 0.0, 0.0,
160 0.0, 1.0, 0.0,
161 0.0, 0.0, 1.0,
162 ]
163 tri = [
164 1, 2, 0,
165 3, 4, 1,
166 4, 2, 1,
167 4, 5, 2,
168 6, 7, 3,
169 7, 4, 3,
170 7, 8, 4,
171 8, 5, 4,
172 8, 9, 5,
173 10, 11, 6,
174 11, 7, 6,
175 11, 12, 7,
176 12, 8, 7,
177 12, 13, 8,
178 13, 9, 8,
179 13, 14, 9,
180 ]
181
182 nverts = 16 * 3
183 nattrs = 2
184 verts = FloatArray(nverts * nattrs * 4)
185
186 for i in range(0, nverts):
187 verts[i * nattrs * 4 + 0] = xy[tri[i] * 2 + 0] # x
188 verts[i * nattrs * 4 + 1] = xy[tri[i] * 2 + 1] # y
189 verts[i * nattrs * 4 + 2] = 0.5 # z
190 verts[i * nattrs * 4 + 3] = 1.0 # w
191 verts[i * nattrs * 4 + 4] = color[(i % 3) * 3 + 0] # r
192 verts[i * nattrs * 4 + 5] = color[(i % 3) * 3 + 1] # g
193 verts[i * nattrs * 4 + 6] = color[(i % 3) * 3 + 2] # b
194 verts[i * nattrs * 4 + 7] = 1.0 # a
195
196 ctx.draw_vertices(PIPE_PRIM_TRIANGLES,
197 nverts,
198 nattrs,
199 verts)
200
201 ctx.flush()
202
203 save_image('vert-' + name + '.png', cbuf)
204
205 def main():
206 tests = [
207 'abs',
208 'add',
209 'dp3',
210 'dp4',
211 'dst',
212 'ex2',
213 'frc',
214 'lg2',
215 'lit',
216 'lrp',
217 'mad',
218 'max',
219 'min',
220 'mov',
221 'mul',
222 'rcp',
223 'rsq',
224 'sge',
225 'slt',
226 'sub',
227 'xpd',
228 ]
229
230 html = '''<html>
231 <head>
232 <link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"screen.css\">
233 </head>
234 <body>
235 <div class=\"main\">
236 <h2>regression tests for <span style=\"color: #FF8000\">vertex</span> shader</h2>
237 <table><tbody>
238 '''
239
240 dev = Device()
241 for t in tests:
242 test(dev, t)
243 html += '<tr><td width=\"300px\">' + t + '</td>'
244 html += '<td><img src=\"vert-' + t + '.png\"></img></td></tr>\n'
245
246 html += '</tbody></table>\n</body>\n</html>\n'
247 file('vertex-shader.htm', 'wt').write(html)
248
249 if __name__ == '__main__':
250 main()