swrast: simplify assertion to silence warning
[mesa.git] / src / gallium / tests / 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 bind = PIPE_BIND_SAMPLER_VIEW
75 geom_flags = 0
76 sample_count = 0
77 if not dev.is_format_supported(format, target, sample_count, bind, geom_flags):
78 raise TestSkip
79
80 if not dev.is_format_supported(format, target, sample_count, bind, geom_flags):
81 raise TestSkip
82
83 # textures
84 dst_texture = dev.resource_create(
85 target = target,
86 format = format,
87 width = width,
88 height = height,
89 depth = depth,
90 last_level = last_level,
91 bind = bind,
92 )
93
94 dst_surface = dst_texture.get_surface(face = face, level = level, zslice = zslice)
95
96 src_texture = dev.resource_create(
97 target = target,
98 format = format,
99 width = dst_surface.width,
100 height = dst_surface.height,
101 depth = 1,
102 last_level = 0,
103 bind = PIPE_BIND_SAMPLER_VIEW,
104 )
105
106 src_surface = src_texture.get_surface()
107
108 w = dst_surface.width
109 h = dst_surface.height
110
111 stride = util_format_get_stride(format, w)
112 size = util_format_get_nblocksy(format, h) * stride
113 src_raw = os.urandom(size)
114
115 ctx.surface_write_raw(src_surface, 0, 0, w, h, src_raw, stride)
116
117 ctx.surface_copy(dst_surface, 0, 0,
118 src_surface, 0, 0, w, h)
119
120 dst_raw = ctx.surface_read_raw(dst_surface, 0, 0, w, h)
121
122 if dst_raw != src_raw:
123 raise TestFailure
124
125
126 def main():
127 dev = Device()
128 ctx = dev.context_create()
129 suite = TestSuite()
130
131 targets = [
132 PIPE_TEXTURE_2D,
133 PIPE_TEXTURE_CUBE,
134 PIPE_TEXTURE_3D,
135 ]
136
137 sizes = [64, 32, 16, 8, 4, 2, 1]
138 #sizes = [1020, 508, 252, 62, 30, 14, 6, 3]
139 #sizes = [64]
140 #sizes = [63]
141
142 faces = [
143 PIPE_TEX_FACE_POS_X,
144 PIPE_TEX_FACE_NEG_X,
145 PIPE_TEX_FACE_POS_Y,
146 PIPE_TEX_FACE_NEG_Y,
147 PIPE_TEX_FACE_POS_Z,
148 PIPE_TEX_FACE_NEG_Z,
149 ]
150
151 try:
152 n = int(sys.argv[1])
153 except:
154 n = 10000
155
156 for i in range(n):
157 format = random.choice(formats.keys())
158 if not util_format_is_depth_or_stencil(format):
159 is_depth_or_stencil = util_format_is_depth_or_stencil(format)
160
161 if is_depth_or_stencil:
162 target = PIPE_TEXTURE_2D
163 else:
164 target = random.choice(targets)
165
166 size = random.choice(sizes)
167
168 if target == PIPE_TEXTURE_3D:
169 depth = size
170 else:
171 depth = 1
172
173 if target == PIPE_TEXTURE_CUBE:
174 face = random.choice(faces)
175 else:
176 face = PIPE_TEX_FACE_POS_X
177
178 levels = lods(size)
179 last_level = random.randint(0, levels - 1)
180 level = random.randint(0, last_level)
181 zslice = random.randint(0, max(depth >> level, 1) - 1)
182
183 test = TextureTest(
184 dev = dev,
185 ctx = ctx,
186 target = target,
187 format = format,
188 width = size,
189 height = size,
190 depth = depth,
191 last_level = last_level,
192 face = face,
193 level = level,
194 zslice = zslice,
195 )
196 suite.add_test(test)
197 suite.run()
198
199
200 if __name__ == '__main__':
201 main()