r600: don't enable depth test if there is no depth buffer
[mesa.git] / progs / gallium / python / tests / regress / fragment-shader / fragment-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 import struct
30
31 from gallium import *
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 test(dev, name):
45 ctx = dev.context_create()
46
47 width = 320
48 height = 320
49 minz = 0.0
50 maxz = 1.0
51
52 # disabled blending/masking
53 blend = Blend()
54 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE
55 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE
56 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO
57 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO
58 blend.rt[0].colormask = PIPE_MASK_RGBA
59 ctx.set_blend(blend)
60
61 # depth/stencil/alpha
62 depth_stencil_alpha = DepthStencilAlpha()
63 depth_stencil_alpha.depth.enabled = 0
64 depth_stencil_alpha.depth.writemask = 1
65 depth_stencil_alpha.depth.func = PIPE_FUNC_LESS
66 ctx.set_depth_stencil_alpha(depth_stencil_alpha)
67
68 # rasterizer
69 rasterizer = Rasterizer()
70 rasterizer.front_winding = PIPE_WINDING_CW
71 rasterizer.cull_mode = PIPE_WINDING_NONE
72 rasterizer.scissor = 1
73 ctx.set_rasterizer(rasterizer)
74
75 # viewport
76 viewport = Viewport()
77 scale = FloatArray(4)
78 scale[0] = width / 2.0
79 scale[1] = -height / 2.0
80 scale[2] = (maxz - minz) / 2.0
81 scale[3] = 1.0
82 viewport.scale = scale
83 translate = FloatArray(4)
84 translate[0] = width / 2.0
85 translate[1] = height / 2.0
86 translate[2] = (maxz - minz) / 2.0
87 translate[3] = 0.0
88 viewport.translate = translate
89 ctx.set_viewport(viewport)
90
91 # samplers
92 sampler = Sampler()
93 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE
94 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE
95 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE
96 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE
97 sampler.min_img_filter = PIPE_TEX_MIPFILTER_NEAREST
98 sampler.mag_img_filter = PIPE_TEX_MIPFILTER_NEAREST
99 sampler.normalized_coords = 1
100 ctx.set_fragment_sampler(0, sampler)
101
102 # scissor
103 scissor = Scissor()
104 scissor.minx = 0
105 scissor.miny = 0
106 scissor.maxx = width
107 scissor.maxy = height
108 ctx.set_scissor(scissor)
109
110 clip = Clip()
111 clip.nr = 0
112 ctx.set_clip(clip)
113
114 # framebuffer
115 cbuf = dev.texture_create(
116 PIPE_FORMAT_B8G8R8X8_UNORM,
117 width, height,
118 tex_usage=PIPE_TEXTURE_USAGE_RENDER_TARGET,
119 ).get_surface()
120 fb = Framebuffer()
121 fb.width = width
122 fb.height = height
123 fb.nr_cbufs = 1
124 fb.set_cbuf(0, cbuf)
125 ctx.set_framebuffer(fb)
126 rgba = FloatArray(4);
127 rgba[0] = 0.5
128 rgba[1] = 0.5
129 rgba[2] = 0.5
130 rgba[3] = 0.5
131 ctx.clear(PIPE_CLEAR_COLOR, rgba, 0.0, 0)
132
133 # vertex shader
134 vs = Shader('''
135 VERT
136 DCL IN[0], POSITION
137 DCL IN[1], COLOR
138 DCL OUT[0], POSITION
139 DCL OUT[1], COLOR
140 MOV OUT[0], IN[0]
141 MOV OUT[1], IN[1]
142 END
143 ''')
144 ctx.set_vertex_shader(vs)
145
146 # fragment shader
147 fs = Shader(file('frag-' + name + '.sh', 'rt').read())
148 ctx.set_fragment_shader(fs)
149
150 constbuf0 = dev.buffer_create(64,
151 (PIPE_BUFFER_USAGE_CONSTANT |
152 PIPE_BUFFER_USAGE_GPU_READ |
153 PIPE_BUFFER_USAGE_CPU_WRITE),
154 4 * 4 * 4)
155
156 cbdata = ''
157 cbdata += struct.pack('4f', 0.4, 0.0, 0.0, 1.0)
158 cbdata += struct.pack('4f', 1.0, 1.0, 1.0, 1.0)
159 cbdata += struct.pack('4f', 2.0, 2.0, 2.0, 2.0)
160 cbdata += struct.pack('4f', 4.0, 8.0, 16.0, 32.0)
161
162 constbuf0.write(cbdata, 0)
163
164 ctx.set_constant_buffer(PIPE_SHADER_FRAGMENT,
165 0,
166 constbuf0)
167
168 constbuf1 = dev.buffer_create(64,
169 (PIPE_BUFFER_USAGE_CONSTANT |
170 PIPE_BUFFER_USAGE_GPU_READ |
171 PIPE_BUFFER_USAGE_CPU_WRITE),
172 4 * 4 * 4)
173
174 cbdata = ''
175 cbdata += struct.pack('4f', 0.1, 0.1, 0.1, 0.1)
176 cbdata += struct.pack('4f', 0.25, 0.25, 0.25, 0.25)
177 cbdata += struct.pack('4f', 0.5, 0.5, 0.5, 0.5)
178 cbdata += struct.pack('4f', 0.75, 0.75, 0.75, 0.75)
179
180 constbuf1.write(cbdata, 0)
181
182 ctx.set_constant_buffer(PIPE_SHADER_FRAGMENT,
183 1,
184 constbuf1)
185
186 xy = [
187 -0.8, -0.8,
188 0.8, -0.8,
189 0.0, 0.8,
190 ]
191 color = [
192 1.0, 0.0, 0.0,
193 0.0, 1.0, 0.0,
194 0.0, 0.0, 1.0,
195 ]
196
197 nverts = 3
198 nattrs = 2
199 verts = FloatArray(nverts * nattrs * 4)
200
201 for i in range(0, nverts):
202 verts[i * nattrs * 4 + 0] = xy[i * 2 + 0] # x
203 verts[i * nattrs * 4 + 1] = xy[i * 2 + 1] # y
204 verts[i * nattrs * 4 + 2] = 0.5 # z
205 verts[i * nattrs * 4 + 3] = 1.0 # w
206 verts[i * nattrs * 4 + 4] = color[i * 3 + 0] # r
207 verts[i * nattrs * 4 + 5] = color[i * 3 + 1] # g
208 verts[i * nattrs * 4 + 6] = color[i * 3 + 2] # b
209 verts[i * nattrs * 4 + 7] = 1.0 # a
210
211 ctx.draw_vertices(PIPE_PRIM_TRIANGLES,
212 nverts,
213 nattrs,
214 verts)
215
216 ctx.flush()
217
218 save_image('frag-' + name + '.png', cbuf)
219
220 def main():
221 tests = [
222 'abs',
223 'add',
224 'cb-1d',
225 'cb-2d',
226 'dp3',
227 'dp4',
228 'dst',
229 'ex2',
230 'flr',
231 'frc',
232 'lg2',
233 'lit',
234 'lrp',
235 'mad',
236 'max',
237 'min',
238 'mov',
239 'mul',
240 'rcp',
241 'rsq',
242 'sge',
243 'slt',
244 'srcmod-abs',
245 'srcmod-absneg',
246 'srcmod-neg',
247 'srcmod-swz',
248 'sub',
249 'xpd',
250 ]
251
252 dev = Device()
253 for t in tests:
254 test(dev, t)
255
256 if __name__ == '__main__':
257 main()