python: Allow hardware support.
[mesa.git] / src / gallium / state_trackers / python / tests / texture.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 from base import *
32 from data import generate_data
33
34
35 def compare_rgba(width, height, rgba1, rgba2, tol=0.01):
36 result = True
37 for y in range(0, height):
38 for x in range(0, width):
39 for ch in range(4):
40 offset = (y*width + x)*4 + ch
41 v1 = rgba1[offset]
42 v2 = rgba2[offset]
43 if abs(v1 - v2) > tol:
44 sys.stderr.write("x=%u, y=%u, ch=%u differ: %f vs %f\n",
45 x, y, ch, v1, v2)
46 result = False
47
48
49 class TextureTest(Test):
50
51 def __init__(self, **kargs):
52 Test.__init__(self)
53 self.__dict__.update(kargs)
54
55 def run(self):
56 dev = self.dev
57
58 format = PIPE_FORMAT_A8R8G8B8_UNORM
59 #format = PIPE_FORMAT_DXT1_RGB
60
61 if not dev.is_format_supported(format, PIPE_TEXTURE):
62 pass
63 if not dev.is_format_supported(format, PIPE_SURFACE):
64 pass
65
66 ctx = dev.context_create()
67
68 width = 256
69 height = 256
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.bypass_vs = 1
90 ctx.set_rasterizer(rasterizer)
91
92 # viewport (identity, we setup vertices in wincoords)
93 viewport = Viewport()
94 scale = FloatArray(4)
95 scale[0] = 1.0
96 scale[1] = 1.0
97 scale[2] = 1.0
98 scale[3] = 1.0
99 viewport.scale = scale
100 translate = FloatArray(4)
101 translate[0] = 0.0
102 translate[1] = 0.0
103 translate[2] = 0.0
104 translate[3] = 0.0
105 viewport.translate = translate
106 ctx.set_viewport(viewport)
107
108 # samplers
109 sampler = Sampler()
110 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE
111 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE
112 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE
113 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE
114 sampler.min_img_filter = PIPE_TEX_MIPFILTER_NEAREST
115 sampler.mag_img_filter = PIPE_TEX_MIPFILTER_NEAREST
116 sampler.normalized_coords = 1
117 ctx.set_sampler(0, sampler)
118
119 # texture
120 texture = dev.texture_create(format,
121 width,
122 height)
123 ctx.set_sampler_texture(0, texture)
124
125 surface = texture.get_surface(usage = PIPE_BUFFER_USAGE_CPU_READ|PIPE_BUFFER_USAGE_CPU_WRITE)
126
127 expected_rgba = generate_data(surface)
128
129 cbuf_tex = dev.texture_create(PIPE_FORMAT_A8R8G8B8_UNORM,
130 width,
131 height)
132
133 # drawing dest
134 cbuf = cbuf_tex.get_surface(usage = PIPE_BUFFER_USAGE_GPU_WRITE)
135 fb = Framebuffer()
136 fb.width = cbuf.width
137 fb.height = cbuf.height
138 fb.num_cbufs = 1
139 fb.set_cbuf(0, cbuf)
140 ctx.set_framebuffer(fb)
141
142 # vertex shader
143 vs = Shader('''
144 VERT1.1
145 DCL IN[0], POSITION, CONSTANT
146 DCL IN[1], GENERIC, CONSTANT
147 DCL OUT[0], POSITION, CONSTANT
148 DCL OUT[1], GENERIC, CONSTANT
149 0:MOV OUT[0], IN[0]
150 1:MOV OUT[1], IN[1]
151 2:END
152 ''')
153 #vs.dump()
154 ctx.set_vertex_shader(vs)
155
156 # fragment shader
157 fs = Shader('''
158 FRAG1.1
159 DCL IN[0], GENERIC[0], PERSPECTIVE
160 DCL OUT[0], COLOR, CONSTANT
161 DCL SAMP[0], CONSTANT
162 0:TEX OUT[0], IN[0], SAMP[0], 2D
163 1:END
164 ''')
165 fs.dump()
166 ctx.set_fragment_shader(fs)
167
168 nverts = 4
169 nattrs = 2
170 verts = FloatArray(nverts * nattrs * 4)
171
172 x = 0
173 y = 0
174 w = width
175 h = height
176
177 verts[ 0] = x # x1
178 verts[ 1] = y # y1
179 verts[ 2] = 0.0 # z1
180 verts[ 3] = 1.0 # w1
181 verts[ 4] = 0.0 # s1
182 verts[ 5] = 0.0 # t
183 verts[ 6] = 0.0
184 verts[ 7] = 0.0
185 verts[ 8] = x + w # x2
186 verts[ 9] = y # y2
187 verts[10] = 0.0 # z2
188 verts[11] = 1.0 # w2
189 verts[12] = 1.0 # s2
190 verts[13] = 0.0 # t2
191 verts[14] = 0.0
192 verts[15] = 0.0
193 verts[16] = x + w # x3
194 verts[17] = y + h # y3
195 verts[18] = 0.0 # z3
196 verts[19] = 1.0 # w3
197 verts[20] = 1.0 # s3
198 verts[21] = 1.0 # t3
199 verts[22] = 0.0
200 verts[23] = 0.0
201 verts[24] = x # x4
202 verts[25] = y + h # y4
203 verts[26] = 0.0 # z4
204 verts[27] = 1.0 # w4
205 verts[28] = 0.0 # s4
206 verts[29] = 1.0 # t4
207 verts[30] = 0.0
208 verts[31] = 0.0
209
210 ctx.surface_clear(cbuf, 0x00000000)
211
212 ctx.draw_vertices(PIPE_PRIM_TRIANGLE_FAN,
213 nverts,
214 nattrs,
215 verts)
216
217 ctx.flush()
218
219 rgba = FloatArray(surface.height*surface.width*4)
220
221 cbuf.get_tile_rgba(x, y, w, h, rgba)
222
223 compare_rgba(width, height, rgba, expected_rgba)
224
225 #save_image("texture1.png", surface)
226 #save_image("texture2.png", cbuf)
227
228
229 def main():
230 dev = Device()
231 test = TextureTest(dev = dev)
232 test.run()
233
234
235 if __name__ == '__main__':
236 main()