2 ##########################################################################
4 # Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
15 # The above copyright notice and this permission notice (including the
16 # next paragraph) shall be included in all copies or substantial portions
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 TUNGSTEN GRAPHICS 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.
27 ##########################################################################
32 from data
import generate_data
35 def compare_rgba(width
, height
, rgba1
, rgba2
, tol
=0.01):
37 for y
in range(0, height
):
38 for x
in range(0, width
):
40 offset
= (y
*width
+ x
)*4 + ch
43 if abs(v1
- v2
) > tol
:
44 sys
.stderr
.write("x=%u, y=%u, ch=%u differ: %f vs %f\n",
49 class TextureTest(Test
):
51 def __init__(self
, **kargs
):
53 self
.__dict
__.update(kargs
)
58 format
= PIPE_FORMAT_A8R8G8B8_UNORM
59 #format = PIPE_FORMAT_DXT1_RGB
61 if not dev
.is_format_supported(format
, PIPE_TEXTURE
):
63 if not dev
.is_format_supported(format
, PIPE_SURFACE
):
66 ctx
= dev
.context_create()
71 # disabled blending/masking
73 blend
.rgb_src_factor
= PIPE_BLENDFACTOR_ONE
74 blend
.alpha_src_factor
= PIPE_BLENDFACTOR_ONE
75 blend
.rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
76 blend
.alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
77 blend
.colormask
= PIPE_MASK_RGBA
80 # no-op depth/stencil/alpha
81 depth_stencil_alpha
= DepthStencilAlpha()
82 ctx
.set_depth_stencil_alpha(depth_stencil_alpha
)
85 rasterizer
= Rasterizer()
86 rasterizer
.front_winding
= PIPE_WINDING_CW
87 rasterizer
.cull_mode
= PIPE_WINDING_NONE
88 rasterizer
.bypass_clipping
= 1
89 #rasterizer.bypass_vs = 1
90 ctx
.set_rasterizer(rasterizer
)
92 # viewport (identity, we setup vertices in wincoords)
99 viewport
.scale
= scale
100 translate
= FloatArray(4)
105 viewport
.translate
= translate
106 ctx
.set_viewport(viewport
)
110 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
111 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
112 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
113 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
114 sampler
.min_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
115 sampler
.mag_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
116 sampler
.normalized_coords
= 1
117 ctx
.set_sampler(0, sampler
)
120 texture
= dev
.texture_create(format
,
123 ctx
.set_sampler_texture(0, texture
)
125 surface
= texture
.get_surface(usage
= PIPE_BUFFER_USAGE_CPU_READ|PIPE_BUFFER_USAGE_CPU_WRITE
)
127 expected_rgba
= generate_data(surface
)
129 cbuf_tex
= dev
.texture_create(PIPE_FORMAT_A8R8G8B8_UNORM
,
134 cbuf
= cbuf_tex
.get_surface(usage
= PIPE_BUFFER_USAGE_GPU_WRITE
)
136 fb
.width
= cbuf
.width
137 fb
.height
= cbuf
.height
140 ctx
.set_framebuffer(fb
)
145 DCL IN[0], POSITION, CONSTANT
146 DCL IN[1], GENERIC, CONSTANT
147 DCL OUT[0], POSITION, CONSTANT
148 DCL OUT[1], GENERIC, CONSTANT
154 ctx
.set_vertex_shader(vs
)
159 DCL IN[0], GENERIC[0], PERSPECTIVE
160 DCL OUT[0], COLOR, CONSTANT
161 DCL SAMP[0], CONSTANT
162 0:TEX OUT[0], IN[0], SAMP[0], 2D
166 ctx
.set_fragment_shader(fs
)
170 verts
= FloatArray(nverts
* nattrs
* 4)
185 verts
[ 8] = x
+ w
# x2
193 verts
[16] = x
+ w
# x3
194 verts
[17] = y
+ h
# y3
202 verts
[25] = y
+ h
# y4
210 ctx
.surface_clear(cbuf
, 0x00000000)
212 ctx
.draw_vertices(PIPE_PRIM_TRIANGLE_FAN
,
219 rgba
= FloatArray(surface
.height
*surface
.width
*4)
221 cbuf
.get_tile_rgba(x
, y
, w
, h
, rgba
)
223 compare_rgba(width
, height
, rgba
, expected_rgba
)
225 #save_image("texture1.png", surface)
226 #save_image("texture2.png", cbuf)
231 test
= TextureTest(dev
= dev
)
235 if __name__
== '__main__':