Merge commit 'origin/7.8'
[mesa.git] / progs / gallium / python / tests / surface_copy.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 import os
31 import random
32
33 from gallium import *
34 from base import *
35
36
37 def lods(*dims):
38 size = max(dims)
39 lods = 0
40 while size:
41 lods += 1
42 size >>= 1
43 return lods
44
45
46 class TextureTest(TestCase):
47
48 tags = (
49 'target',
50 'format',
51 'width',
52 'height',
53 'depth',
54 'last_level',
55 'face',
56 'level',
57 'zslice',
58 )
59
60 def test(self):
61 dev = self.dev
62 ctx = self.ctx
63
64 target = self.target
65 format = self.format
66 width = self.width
67 height = self.height
68 depth = self.depth
69 last_level = self.last_level
70 face = self.face
71 level = self.level
72 zslice = self.zslice
73
74 tex_usage = PIPE_TEXTURE_USAGE_SAMPLER
75 geom_flags = 0
76 if not dev.is_format_supported(format, target, tex_usage, geom_flags):
77 raise TestSkip
78
79 if not dev.is_format_supported(format, target, tex_usage, geom_flags):
80 raise TestSkip
81
82 # textures
83 dst_texture = dev.texture_create(
84 target = target,
85 format = format,
86 width = width,
87 height = height,
88 depth = depth,
89 last_level = last_level,
90 tex_usage = tex_usage,
91 )
92
93 dst_surface = dst_texture.get_surface(face = face, level = level, zslice = zslice)
94
95 src_texture = dev.texture_create(
96 target = target,
97 format = format,
98 width = dst_surface.width,
99 height = dst_surface.height,
100 depth = 1,
101 last_level = 0,
102 tex_usage = PIPE_TEXTURE_USAGE_SAMPLER,
103 )
104
105 src_surface = src_texture.get_surface()
106
107 w = dst_surface.width
108 h = dst_surface.height
109
110 stride = util_format_get_stride(format, w)
111 size = util_format_get_nblocksy(format, h) * stride
112 src_raw = os.urandom(size)
113
114 ctx.surface_write_raw(src_surface, 0, 0, w, h, src_raw, stride)
115
116 ctx.surface_copy(dst_surface, 0, 0,
117 src_surface, 0, 0, w, h)
118
119 dst_raw = ctx.surface_read_raw(dst_surface, 0, 0, w, h)
120
121 if dst_raw != src_raw:
122 raise TestFailure
123
124
125 def main():
126 dev = Device()
127 ctx = dev.context_create()
128 suite = TestSuite()
129
130 targets = [
131 PIPE_TEXTURE_2D,
132 PIPE_TEXTURE_CUBE,
133 PIPE_TEXTURE_3D,
134 ]
135
136 sizes = [64, 32, 16, 8, 4, 2, 1]
137 #sizes = [1020, 508, 252, 62, 30, 14, 6, 3]
138 #sizes = [64]
139 #sizes = [63]
140
141 faces = [
142 PIPE_TEX_FACE_POS_X,
143 PIPE_TEX_FACE_NEG_X,
144 PIPE_TEX_FACE_POS_Y,
145 PIPE_TEX_FACE_NEG_Y,
146 PIPE_TEX_FACE_POS_Z,
147 PIPE_TEX_FACE_NEG_Z,
148 ]
149
150 try:
151 n = int(sys.argv[1])
152 except:
153 n = 10000
154
155 for i in range(n):
156 format = random.choice(formats.keys())
157 if not util_format_is_depth_or_stencil(format):
158 is_depth_or_stencil = util_format_is_depth_or_stencil(format)
159
160 if is_depth_or_stencil:
161 target = PIPE_TEXTURE_2D
162 else:
163 target = random.choice(targets)
164
165 size = random.choice(sizes)
166
167 if target == PIPE_TEXTURE_3D:
168 depth = size
169 else:
170 depth = 1
171
172 if target == PIPE_TEXTURE_CUBE:
173 face = random.choice(faces)
174 else:
175 face = PIPE_TEX_FACE_POS_X
176
177 levels = lods(size)
178 last_level = random.randint(0, levels - 1)
179 level = random.randint(0, last_level)
180 zslice = random.randint(0, max(depth >> level, 1) - 1)
181
182 test = TextureTest(
183 dev = dev,
184 ctx = ctx,
185 target = target,
186 format = format,
187 width = size,
188 height = size,
189 depth = depth,
190 last_level = last_level,
191 face = face,
192 level = level,
193 zslice = zslice,
194 )
195 suite.add_test(test)
196 suite.run()
197
198
199 if __name__ == '__main__':
200 main()