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