2 ##########################################################################
4 # Copyright 2009 VMware, Inc.
5 # Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
16 # The above copyright notice and this permission notice (including the
17 # next paragraph) shall be included in all copies or substantial portions
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.
28 ##########################################################################
46 def minify(dims
, level
= 1):
47 return [max(dim
>>level
, 1) for dim
in dims
]
50 def tex_coords(texture
, face
, level
, zslice
):
58 if texture
.target
== PIPE_TEXTURE_2D
:
59 return [[s
, t
, 0.0] for s
, t
in st
]
60 elif texture
.target
== PIPE_TEXTURE_3D
:
61 depth
= texture
.get_depth(level
)
63 r
= float(zslice
)/float(depth
- 1)
66 return [[s
, t
, r
] for s
, t
in st
]
67 elif texture
.target
== PIPE_TEXTURE_CUBE
:
70 # See http://developer.nvidia.com/object/cube_map_ogl_tutorial.html
73 if face
== PIPE_TEX_FACE_POS_X
:
77 if face
== PIPE_TEX_FACE_NEG_X
:
81 if face
== PIPE_TEX_FACE_POS_Y
:
85 if face
== PIPE_TEX_FACE_NEG_Y
:
89 if face
== PIPE_TEX_FACE_POS_Z
:
93 if face
== PIPE_TEX_FACE_NEG_Z
:
97 result
.append([rx
, ry
, rz
])
101 return n
& (n
- 1) == 0
104 class TextureColorSampleTest(TestCase
):
127 last_level
= self
.last_level
134 tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
137 geom_flags |
= PIPE_TEXTURE_GEOM_NON_SQUARE
138 if not is_pot(width
) or not is_pot(height
) or not is_pot(depth
):
139 geom_flags |
= PIPE_TEXTURE_GEOM_NON_POWER_OF_TWO
141 if not dev
.is_format_supported(format
, target
, tex_usage
, geom_flags
):
144 # disabled blending/masking
146 blend
.rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_ONE
147 blend
.rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_ONE
148 blend
.rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
149 blend
.rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
150 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
153 # no-op depth/stencil/alpha
154 depth_stencil_alpha
= DepthStencilAlpha()
155 ctx
.set_depth_stencil_alpha(depth_stencil_alpha
)
158 rasterizer
= Rasterizer()
159 rasterizer
.front_winding
= PIPE_WINDING_CW
160 rasterizer
.cull_mode
= PIPE_WINDING_NONE
161 ctx
.set_rasterizer(rasterizer
)
165 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
166 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
167 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
168 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NEAREST
169 sampler
.min_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
170 sampler
.mag_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
171 sampler
.normalized_coords
= 1
173 sampler
.max_lod
= PIPE_MAX_TEXTURE_LEVELS
- 1
174 ctx
.set_fragment_sampler(0, sampler
)
177 texture
= dev
.texture_create(
183 last_level
= last_level
,
184 tex_usage
= tex_usage
,
187 expected_rgba
= FloatArray(height
*width
*4)
188 surface
= texture
.get_surface(
194 ctx
.surface_sample_rgba(surface
, expected_rgba
, True)
196 ctx
.set_fragment_sampler_texture(0, texture
)
199 viewport
= Viewport()
200 scale
= FloatArray(4)
203 scale
[2] = (maxz
- minz
) / 2.0
205 viewport
.scale
= scale
206 translate
= FloatArray(4)
209 translate
[2] = (maxz
- minz
) / 2.0
211 viewport
.translate
= translate
212 ctx
.set_viewport(viewport
)
219 scissor
.maxy
= height
220 ctx
.set_scissor(scissor
)
228 cbuf_tex
= dev
.texture_create(
229 PIPE_FORMAT_B8G8R8A8_UNORM
,
232 tex_usage
= PIPE_TEXTURE_USAGE_RENDER_TARGET
,
235 cbuf
= cbuf_tex
.get_surface()
241 ctx
.set_framebuffer(fb
)
242 rgba
= FloatArray(4);
247 ctx
.clear(PIPE_CLEAR_COLOR
, rgba
, 0.0, 0)
253 DCL IN[0], POSITION, CONSTANT
254 DCL IN[1], GENERIC, CONSTANT
255 DCL OUT[0], POSITION, CONSTANT
256 DCL OUT[1], GENERIC, CONSTANT
262 ctx
.set_vertex_shader(vs
)
266 PIPE_TEXTURE_1D
: "1D",
267 PIPE_TEXTURE_2D
: "2D",
268 PIPE_TEXTURE_3D
: "3D",
269 PIPE_TEXTURE_CUBE
: "CUBE",
273 DCL IN[0], GENERIC[0], LINEAR
274 DCL OUT[0], COLOR, CONSTANT
275 DCL SAMP[0], CONSTANT
276 0:TEX OUT[0], IN[0], SAMP[0], %s
280 ctx
.set_fragment_shader(fs
)
284 verts
= FloatArray(nverts
* nattrs
* 4)
288 w
, h
= minify((width
, height
), level
)
297 tex
= tex_coords(texture
, face
, level
, zslice
)
299 for i
in range(0, 4):
301 verts
[j
+ 0] = pos
[i
][0]/float(width
) # x
302 verts
[j
+ 1] = pos
[i
][1]/float(height
) # y
303 verts
[j
+ 2] = 0.0 # z
304 verts
[j
+ 3] = 1.0 # w
305 verts
[j
+ 4] = tex
[i
][0] # s
306 verts
[j
+ 5] = tex
[i
][1] # r
307 verts
[j
+ 6] = tex
[i
][2] # q
310 ctx
.draw_vertices(PIPE_PRIM_TRIANGLE_FAN
,
317 cbuf
= cbuf_tex
.get_surface()
319 self
.assert_rgba(ctx
, cbuf
, x
, y
, w
, h
, expected_rgba
, 4.0/256, 0.85)
322 class TextureDepthSampleTest(TestCase
):
345 last_level
= self
.last_level
352 tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
355 geom_flags |
= PIPE_TEXTURE_GEOM_NON_SQUARE
356 if not is_pot(width
) or not is_pot(height
) or not is_pot(depth
):
357 geom_flags |
= PIPE_TEXTURE_GEOM_NON_POWER_OF_TWO
359 if not dev
.is_format_supported(format
, target
, tex_usage
, geom_flags
):
362 # disabled blending/masking
364 blend
.rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_ONE
365 blend
.rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_ONE
366 blend
.rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
367 blend
.rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
368 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
371 # depth/stencil/alpha
372 depth_stencil_alpha
= DepthStencilAlpha()
373 depth_stencil_alpha
.depth
.enabled
= 1
374 depth_stencil_alpha
.depth
.writemask
= 1
375 depth_stencil_alpha
.depth
.func
= PIPE_FUNC_LESS
376 ctx
.set_depth_stencil_alpha(depth_stencil_alpha
)
379 rasterizer
= Rasterizer()
380 rasterizer
.front_winding
= PIPE_WINDING_CW
381 rasterizer
.cull_mode
= PIPE_WINDING_NONE
382 ctx
.set_rasterizer(rasterizer
)
385 viewport
= Viewport()
386 scale
= FloatArray(4)
389 scale
[2] = (maxz
- minz
) / 2.0
391 viewport
.scale
= scale
392 translate
= FloatArray(4)
395 translate
[2] = (maxz
- minz
) / 2.0
397 viewport
.translate
= translate
398 ctx
.set_viewport(viewport
)
402 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
403 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
404 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
405 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NEAREST
406 sampler
.min_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
407 sampler
.mag_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
408 sampler
.normalized_coords
= 1
410 sampler
.max_lod
= PIPE_MAX_TEXTURE_LEVELS
- 1
411 ctx
.set_fragment_sampler(0, sampler
)
414 texture
= dev
.texture_create(
420 last_level
= last_level
,
421 tex_usage
= tex_usage
,
424 expected_rgba
= FloatArray(height
*width
*4)
425 surface
= texture
.get_surface(
431 ctx
.surface_sample_rgba(surface
, expected_rgba
, True)
433 ctx
.set_fragment_sampler_texture(0, texture
)
440 scissor
.maxy
= height
441 ctx
.set_scissor(scissor
)
449 cbuf_tex
= dev
.texture_create(
450 PIPE_FORMAT_B8G8R8A8_UNORM
,
453 tex_usage
= PIPE_TEXTURE_USAGE_RENDER_TARGET
,
456 zsbuf_tex
= dev
.texture_create(
457 PIPE_FORMAT_X8Z24_UNORM
,
460 tex_usage
= PIPE_TEXTURE_USAGE_RENDER_TARGET
,
463 cbuf
= cbuf_tex
.get_surface()
464 zsbuf
= zsbuf_tex
.get_surface()
471 ctx
.set_framebuffer(fb
)
472 rgba
= FloatArray(4);
477 ctx
.clear(PIPE_CLEAR_DEPTHSTENCIL
, rgba
, 1.0, 0)
483 DCL IN[0], POSITION, CONSTANT
484 DCL IN[1], GENERIC, CONSTANT
485 DCL OUT[0], POSITION, CONSTANT
486 DCL OUT[1], GENERIC, CONSTANT
492 ctx
.set_vertex_shader(vs
)
496 PIPE_TEXTURE_1D
: "1D",
497 PIPE_TEXTURE_2D
: "2D",
498 PIPE_TEXTURE_3D
: "3D",
499 PIPE_TEXTURE_CUBE
: "CUBE",
503 DCL IN[0], GENERIC[0], LINEAR
504 DCL SAMP[0], CONSTANT
505 DCL OUT[0].z, POSITION
506 0:TEX OUT[0].z, IN[0], SAMP[0], %s
510 ctx
.set_fragment_shader(fs
)
514 verts
= FloatArray(nverts
* nattrs
* 4)
518 w
, h
= minify((width
, height
), level
)
527 tex
= tex_coords(texture
, face
, level
, zslice
)
529 for i
in range(0, 4):
531 verts
[j
+ 0] = pos
[i
][0]/float(width
) # x
532 verts
[j
+ 1] = pos
[i
][1]/float(height
) # y
533 verts
[j
+ 2] = 0.0 # z
534 verts
[j
+ 3] = 1.0 # w
535 verts
[j
+ 4] = tex
[i
][0] # s
536 verts
[j
+ 5] = tex
[i
][1] # r
537 verts
[j
+ 6] = tex
[i
][2] # q
540 ctx
.draw_vertices(PIPE_PRIM_TRIANGLE_FAN
,
547 zsbuf
= zsbuf_tex
.get_surface()
549 self
.assert_rgba(ctx
, zsbuf
, x
, y
, w
, h
, expected_rgba
, 4.0/256, 0.85)
555 random
.seed(0xdead3eef)
558 ctx
= dev
.context_create()
567 #sizes = [64, 32, 16, 8, 4, 2, 1]
568 #sizes = [1020, 508, 252, 62, 30, 14, 6, 3]
587 format
= random
.choice(formats
.keys())
588 if not util_format_is_depth_or_stencil(format
):
589 is_depth_or_stencil
= util_format_is_depth_or_stencil(format
)
591 if is_depth_or_stencil
:
592 target
= PIPE_TEXTURE_2D
594 target
= random
.choice(targets
)
596 size
= random
.choice(sizes
)
598 if target
== PIPE_TEXTURE_3D
:
603 if target
== PIPE_TEXTURE_CUBE
:
604 face
= random
.choice(faces
)
606 face
= PIPE_TEX_FACE_POS_X
609 last_level
= random
.randint(0, levels
- 1)
610 level
= random
.randint(0, last_level
)
611 zslice
= random
.randint(0, max(depth
>> level
, 1) - 1)
613 if is_depth_or_stencil
:
614 klass
= TextureDepthSampleTest
616 klass
= TextureColorSampleTest
626 last_level
= last_level
,
635 if __name__
== '__main__':