Merge branch '7.8'
[mesa.git] / src / gallium / tests / python / tests / texture_render.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 from gallium import *
31 from base import *
32
33
34 def lods(*dims):
35 size = max(dims)
36 lods = 0
37 while size:
38 lods += 1
39 size >>= 1
40 return lods
41
42
43 class TextureTest(TestCase):
44
45 tags = (
46 'target',
47 'format',
48 'width',
49 'height',
50 'depth',
51 'last_level',
52 'face',
53 'level',
54 'zslice',
55 )
56
57 def test(self):
58 dev = self.dev
59
60 target = self.target
61 format = self.format
62 width = self.width
63 height = self.height
64 depth = self.depth
65 last_level = self.last_level
66 face = self.face
67 level = self.level
68 zslice = self.zslice
69
70 # textures
71 dst_texture = dev.resource_create(
72 target = target,
73 format = format,
74 width = width,
75 height = height,
76 depth = depth,
77 last_level = last_level,
78 bind = PIPE_BIND_RENDER_TARGET,
79 )
80 if dst_texture is None:
81 raise TestSkip
82
83 dst_surface = dst_texture.get_surface(face = face, level = level, zslice = zslice)
84
85 ref_texture = dev.resource_create(
86 target = target,
87 format = format,
88 width = dst_surface.width,
89 height = dst_surface.height,
90 depth = 1,
91 last_level = 0,
92 bind = PIPE_BIND_SAMPLER_VIEW,
93 )
94
95 ref_surface = ref_texture.get_surface()
96
97 src_texture = dev.resource_create(
98 target = target,
99 format = PIPE_FORMAT_B8G8R8A8_UNORM,
100 width = dst_surface.width,
101 height = dst_surface.height,
102 depth = 1,
103 last_level = 0,
104 bind = PIPE_BIND_SAMPLER_VIEW,
105 )
106
107 src_surface = src_texture.get_surface()
108
109 expected_rgba = FloatArray(height*width*4)
110 ref_surface.sample_rgba(expected_rgba)
111
112 src_surface.put_tile_rgba(0, 0, src_surface.width, src_surface.height, expected_rgba)
113
114 ctx = self.dev.context_create()
115
116 # disabled blending/masking
117 blend = Blend()
118 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE
119 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE
120 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO
121 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO
122 blend.rt[0].colormask = PIPE_MASK_RGBA
123 ctx.set_blend(blend)
124
125 # no-op depth/stencil/alpha
126 depth_stencil_alpha = DepthStencilAlpha()
127 ctx.set_depth_stencil_alpha(depth_stencil_alpha)
128
129 # rasterizer
130 rasterizer = Rasterizer()
131 rasterizer.front_winding = PIPE_WINDING_CW
132 rasterizer.cull_mode = PIPE_WINDING_NONE
133 rasterizer.bypass_vs_clip_and_viewport = 1
134 ctx.set_rasterizer(rasterizer)
135
136 # samplers
137 sampler = Sampler()
138 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE
139 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE
140 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE
141 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST
142 sampler.min_img_filter = PIPE_TEX_MIPFILTER_NEAREST
143 sampler.mag_img_filter = PIPE_TEX_MIPFILTER_NEAREST
144 sampler.normalized_coords = 1
145 sampler.min_lod = 0
146 sampler.max_lod = PIPE_MAX_TEXTURE_LEVELS - 1
147 ctx.set_fragment_sampler(0, sampler)
148 ctx.set_fragment_sampler_texture(0, src_texture)
149
150 # framebuffer
151 cbuf_tex = dev.resource_create(
152 PIPE_FORMAT_B8G8R8A8_UNORM,
153 width,
154 height,
155 bind = PIPE_BIND_RENDER_TARGET,
156 )
157
158 fb = Framebuffer()
159 fb.width = dst_surface.width
160 fb.height = dst_surface.height
161 fb.nr_cbufs = 1
162 fb.set_cbuf(0, dst_surface)
163 ctx.set_framebuffer(fb)
164 rgba = FloatArray(4);
165 rgba[0] = 0.0
166 rgba[1] = 0.0
167 rgba[2] = 0.0
168 rgba[3] = 0.0
169 ctx.clear(PIPE_CLEAR_COLOR, rgba, 0.0, 0)
170 del fb
171
172 # vertex shader
173 vs = Shader('''
174 VERT
175 DCL IN[0], POSITION, CONSTANT
176 DCL IN[1], GENERIC, CONSTANT
177 DCL OUT[0], POSITION, CONSTANT
178 DCL OUT[1], GENERIC, CONSTANT
179 0:MOV OUT[0], IN[0]
180 1:MOV OUT[1], IN[1]
181 2:END
182 ''')
183 #vs.dump()
184 ctx.set_vertex_shader(vs)
185
186 # fragment shader
187 fs = Shader('''
188 FRAG
189 DCL IN[0], GENERIC[0], LINEAR
190 DCL OUT[0], COLOR, CONSTANT
191 DCL SAMP[0], CONSTANT
192 0:TEX OUT[0], IN[0], SAMP[0], 2D
193 1:END
194 ''')
195 #fs.dump()
196 ctx.set_fragment_shader(fs)
197
198 nverts = 4
199 nattrs = 2
200 verts = FloatArray(nverts * nattrs * 4)
201
202 x = 0
203 y = 0
204 w = dst_surface.width
205 h = dst_surface.height
206
207 pos = [
208 [x, y],
209 [x+w, y],
210 [x+w, y+h],
211 [x, y+h],
212 ]
213
214 tex = [
215 [0.0, 0.0],
216 [1.0, 0.0],
217 [1.0, 1.0],
218 [0.0, 1.0],
219 ]
220
221 for i in range(0, 4):
222 j = 8*i
223 verts[j + 0] = pos[i][0] # x
224 verts[j + 1] = pos[i][1] # y
225 verts[j + 2] = 0.0 # z
226 verts[j + 3] = 1.0 # w
227 verts[j + 4] = tex[i][0] # s
228 verts[j + 5] = tex[i][1] # r
229 verts[j + 6] = 0.0
230 verts[j + 7] = 1.0
231
232 ctx.draw_vertices(PIPE_PRIM_TRIANGLE_FAN,
233 nverts,
234 nattrs,
235 verts)
236
237 ctx.flush()
238
239 self.assert_rgba(dst_surface, x, y, w, h, expected_rgba, 4.0/256, 0.85)
240
241
242
243 def main():
244 dev = Device()
245 suite = TestSuite()
246
247 targets = [
248 PIPE_TEXTURE_2D,
249 PIPE_TEXTURE_CUBE,
250 #PIPE_TEXTURE_3D,
251 ]
252
253 formats = [
254 PIPE_FORMAT_B8G8R8A8_UNORM,
255 PIPE_FORMAT_B8G8R8X8_UNORM,
256 #PIPE_FORMAT_B8G8R8A8_SRGB,
257 PIPE_FORMAT_B5G6R5_UNORM,
258 PIPE_FORMAT_B5G5R5A1_UNORM,
259 PIPE_FORMAT_B4G4R4A4_UNORM,
260 #PIPE_FORMAT_Z32_UNORM,
261 #PIPE_FORMAT_S8_USCALED_Z24_UNORM,
262 #PIPE_FORMAT_X8Z24_UNORM,
263 #PIPE_FORMAT_Z16_UNORM,
264 #PIPE_FORMAT_S8_USCALED,
265 PIPE_FORMAT_A8_UNORM,
266 PIPE_FORMAT_L8_UNORM,
267 #PIPE_FORMAT_DXT1_RGB,
268 #PIPE_FORMAT_DXT1_RGBA,
269 #PIPE_FORMAT_DXT3_RGBA,
270 #PIPE_FORMAT_DXT5_RGBA,
271 ]
272
273 sizes = [64, 32, 16, 8, 4, 2, 1]
274 #sizes = [1020, 508, 252, 62, 30, 14, 6, 3]
275 #sizes = [64]
276 #sizes = [63]
277
278 faces = [
279 PIPE_TEX_FACE_POS_X,
280 PIPE_TEX_FACE_NEG_X,
281 PIPE_TEX_FACE_POS_Y,
282 PIPE_TEX_FACE_NEG_Y,
283 PIPE_TEX_FACE_POS_Z,
284 PIPE_TEX_FACE_NEG_Z,
285 ]
286
287 for target in targets:
288 for format in formats:
289 for size in sizes:
290 if target == PIPE_TEXTURE_3D:
291 depth = size
292 else:
293 depth = 1
294 for face in faces:
295 if target != PIPE_TEXTURE_CUBE and face:
296 continue
297 levels = lods(size)
298 for last_level in range(levels):
299 for level in range(0, last_level + 1):
300 zslice = 0
301 while zslice < depth >> level:
302 test = TextureTest(
303 dev = dev,
304 target = target,
305 format = format,
306 width = size,
307 height = size,
308 depth = depth,
309 last_level = last_level,
310 face = face,
311 level = level,
312 zslice = zslice,
313 )
314 suite.add_test(test)
315 zslice = (zslice + 1)*2 - 1
316 suite.run()
317
318
319 if __name__ == '__main__':
320 main()