Merge remote branch 'origin/7.8'
[mesa.git] / progs / gallium / python / tests / texture_transfer.py
1 #!/usr/bin/env python
2 ##########################################################################
3 #
4 # Copyright 2009 VMware, Inc.
5 # Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
6 # All Rights Reserved.
7 #
8 # Permission is hereby granted, free of charge, to any person obtaining a
9 # copy of this software and associated documentation files (the
10 # "Software"), to deal in the Software without restriction, including
11 # without limitation the rights to use, copy, modify, merge, publish,
12 # distribute, sub license, and/or sell copies of the Software, and to
13 # permit persons to whom the Software is furnished to do so, subject to
14 # the following conditions:
15 #
16 # The above copyright notice and this permission notice (including the
17 # next paragraph) shall be included in all copies or substantial portions
18 # of the Software.
19 #
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 # IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
24 # ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #
28 ##########################################################################
29
30
31 import os
32 import random
33
34 from gallium import *
35 from base import *
36
37
38 def lods(*dims):
39 size = max(dims)
40 lods = 0
41 while size:
42 lods += 1
43 size >>= 1
44 return lods
45
46
47 class TextureTest(TestCase):
48
49 tags = (
50 'target',
51 'format',
52 'width',
53 'height',
54 'depth',
55 'last_level',
56 'face',
57 'level',
58 'zslice',
59 )
60
61 def test(self):
62 dev = self.dev
63 ctx = self.ctx
64
65 target = self.target
66 format = self.format
67 width = self.width
68 height = self.height
69 depth = self.depth
70 last_level = self.last_level
71 face = self.face
72 level = self.level
73 zslice = self.zslice
74
75 tex_usage = PIPE_TEXTURE_USAGE_SAMPLER
76 geom_flags = 0
77 if not dev.is_format_supported(format, target, tex_usage, geom_flags):
78 raise TestSkip
79
80 # textures
81 texture = dev.texture_create(
82 target = target,
83 format = format,
84 width = width,
85 height = height,
86 depth = depth,
87 last_level = last_level,
88 tex_usage = tex_usage,
89 )
90
91 surface = texture.get_surface(face, level, zslice)
92
93 stride = util_format_get_stride(format, surface.width)
94 size = util_format_get_nblocksy(format, surface.height) * stride
95
96 in_raw = os.urandom(size)
97
98 ctx.surface_write_raw(surface, 0, 0, surface.width, surface.height, in_raw, stride)
99
100 out_raw = ctx.surface_read_raw(surface, 0, 0, surface.width, surface.height)
101
102 if in_raw != out_raw:
103 raise TestFailure
104
105
106 def main():
107 dev = Device()
108 ctx = dev.context_create()
109 suite = TestSuite()
110
111 targets = [
112 PIPE_TEXTURE_2D,
113 PIPE_TEXTURE_CUBE,
114 PIPE_TEXTURE_3D,
115 ]
116
117 sizes = [64, 32, 16, 8, 4, 2, 1]
118 #sizes = [1020, 508, 252, 62, 30, 14, 6, 3]
119 #sizes = [64]
120 #sizes = [63]
121
122 faces = [
123 PIPE_TEX_FACE_POS_X,
124 PIPE_TEX_FACE_NEG_X,
125 PIPE_TEX_FACE_POS_Y,
126 PIPE_TEX_FACE_NEG_Y,
127 PIPE_TEX_FACE_POS_Z,
128 PIPE_TEX_FACE_NEG_Z,
129 ]
130
131 try:
132 n = int(sys.argv[1])
133 except:
134 n = 10000
135
136 for i in range(n):
137 format = random.choice(formats.keys())
138 if not util_format_is_depth_or_stencil(format):
139 is_depth_or_stencil = util_format_is_depth_or_stencil(format)
140
141 if is_depth_or_stencil:
142 target = PIPE_TEXTURE_2D
143 else:
144 target = random.choice(targets)
145
146 size = random.choice(sizes)
147
148 if target == PIPE_TEXTURE_3D:
149 depth = size
150 else:
151 depth = 1
152
153 if target == PIPE_TEXTURE_CUBE:
154 face = random.choice(faces)
155 else:
156 face = PIPE_TEX_FACE_POS_X
157
158 levels = lods(size)
159 last_level = random.randint(0, levels - 1)
160 level = random.randint(0, last_level)
161 zslice = random.randint(0, max(depth >> level, 1) - 1)
162
163 test = TextureTest(
164 dev = dev,
165 ctx = ctx,
166 target = target,
167 format = format,
168 width = size,
169 height = size,
170 depth = depth,
171 last_level = last_level,
172 face = face,
173 level = level,
174 zslice = zslice,
175 )
176 suite.add_test(test)
177 suite.run()
178
179
180 if __name__ == '__main__':
181 main()