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 ##########################################################################
33 def save_image(filename
, surface
):
34 pixels
= FloatArray(surface
.height
*surface
.width
*4)
35 surface
.get_tile_rgba(0, 0, surface
.width
, surface
.height
, pixels
)
40 size
=(surface
.width
, surface
.height
),
42 outpixels
= outimage
.load()
43 for y
in range(0, surface
.height
):
44 for x
in range(0, surface
.width
):
45 offset
= (y
*surface
.width
+ x
)*4
46 r
, g
, b
, a
= [int(pixels
[offset
+ ch
]*255) for ch
in range(4)]
47 outpixels
[x
, y
] = r
, g
, b
48 outimage
.save(filename
, "PNG")
52 ctx
= dev
.context_create()
57 # disabled blending/masking
59 blend
.rgb_src_factor
= PIPE_BLENDFACTOR_ONE
60 blend
.alpha_src_factor
= PIPE_BLENDFACTOR_ONE
61 blend
.rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
62 blend
.alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
63 blend
.colormask
= PIPE_MASK_RGBA
66 # no-op depth/stencil/alpha
67 depth_stencil_alpha
= DepthStencilAlpha()
68 ctx
.set_depth_stencil_alpha(depth_stencil_alpha
)
71 rasterizer
= Rasterizer()
72 rasterizer
.front_winding
= PIPE_WINDING_CW
73 rasterizer
.cull_mode
= PIPE_WINDING_NONE
74 rasterizer
.bypass_clipping
= 1
75 #rasterizer.bypass_vs = 1
76 ctx
.set_rasterizer(rasterizer
)
78 # viewport (identity, we setup vertices in wincoords)
85 viewport
.scale
= scale
86 translate
= FloatArray(4)
91 viewport
.translate
= translate
92 ctx
.set_viewport(viewport
)
96 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
97 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
98 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
99 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
100 sampler
.min_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
101 sampler
.mag_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
102 sampler
.normalized_coords
= 1
103 ctx
.set_sampler(0, sampler
)
106 texture
= dev
.texture_create(PIPE_FORMAT_A8R8G8B8_UNORM
,
108 usage
=PIPE_TEXTURE_USAGE_RENDER_TARGET
)
109 ctx
.set_sampler_texture(0, texture
)
112 surface
= texture
.get_surface(usage
= PIPE_BUFFER_USAGE_GPU_WRITE
)
114 fb
.width
= surface
.width
115 fb
.height
= surface
.height
117 fb
.set_cbuf(0, surface
)
118 ctx
.set_framebuffer(fb
)
123 DCL IN[0], POSITION, CONSTANT
124 DCL IN[1], COLOR, CONSTANT
125 DCL OUT[0], POSITION, CONSTANT
126 DCL OUT[1], COLOR, CONSTANT
132 ctx
.set_vertex_shader(vs
)
137 DCL IN[0], COLOR, LINEAR
138 DCL OUT[0], COLOR, CONSTANT
143 ctx
.set_fragment_shader(fs
)
147 verts
= FloatArray(nverts
* nattrs
* 4)
149 verts
[ 0] = 128.0 # x1
150 verts
[ 1] = 32.0 # y1
157 verts
[ 8] = 32.0 # x2
158 verts
[ 9] = 224.0 # y2
165 verts
[16] = 224.0 # x3
166 verts
[17] = 224.0 # y3
174 ctx
.surface_clear(surface
, 0x00000000)
176 ctx
.draw_vertices(PIPE_PRIM_TRIANGLES
,
183 save_image("tri.png", surface
)
188 dev
= Device(hardware
= False)
192 if __name__
== '__main__':