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 make_image(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
50 def save_image(filename
, surface
):
51 outimage
= make_image(surface
)
52 outimage
.save(filename
, "PNG")
54 def show_image(surface
):
55 outimage
= make_image(surface
)
58 from PIL
import Image
, ImageTk
61 root
.title('background image')
63 image1
= ImageTk
.PhotoImage(outimage
)
68 root
.geometry("%dx%d+%d+%d" % (w
, h
, x
, y
))
69 panel1
= tk
.Label(root
, image
=image1
)
70 panel1
.pack(side
='top', fill
='both', expand
='yes')
76 ctx
= dev
.context_create()
81 # disabled blending/masking
83 blend
.rgb_src_factor
= PIPE_BLENDFACTOR_ONE
84 blend
.alpha_src_factor
= PIPE_BLENDFACTOR_ONE
85 blend
.rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
86 blend
.alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
87 blend
.colormask
= PIPE_MASK_RGBA
90 # no-op depth/stencil/alpha
91 depth_stencil_alpha
= DepthStencilAlpha()
92 ctx
.set_depth_stencil_alpha(depth_stencil_alpha
)
95 rasterizer
= Rasterizer()
96 rasterizer
.front_winding
= PIPE_WINDING_CW
97 rasterizer
.cull_mode
= PIPE_WINDING_NONE
98 rasterizer
.bypass_clipping
= 1
99 rasterizer
.scissor
= 1
100 #rasterizer.bypass_vs = 1
101 ctx
.set_rasterizer(rasterizer
)
103 # viewport (identity, we setup vertices in wincoords)
104 viewport
= Viewport()
105 scale
= FloatArray(4)
110 viewport
.scale
= scale
111 translate
= FloatArray(4)
116 viewport
.translate
= translate
117 ctx
.set_viewport(viewport
)
121 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
122 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
123 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
124 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
125 sampler
.min_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
126 sampler
.mag_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
127 sampler
.normalized_coords
= 1
128 ctx
.set_sampler(0, sampler
)
135 scissor
.maxy
= height
136 ctx
.set_scissor(scissor
)
143 cbuf
= dev
.texture_create(
144 PIPE_FORMAT_X8R8G8B8_UNORM
,
146 tex_usage
=PIPE_TEXTURE_USAGE_DISPLAY_TARGET
,
148 _cbuf
= cbuf
.get_surface(usage
= PIPE_BUFFER_USAGE_GPU_READ|PIPE_BUFFER_USAGE_GPU_WRITE
)
153 fb
.set_cbuf(0, _cbuf
)
154 ctx
.set_framebuffer(fb
)
155 _cbuf
.clear_value
= 0x00000000
156 ctx
.surface_clear(_cbuf
, _cbuf
.clear_value
)
162 DCL IN[0], POSITION, CONSTANT
163 DCL IN[1], COLOR, CONSTANT
164 DCL OUT[0], POSITION, CONSTANT
165 DCL OUT[1], COLOR, CONSTANT
170 ctx
.set_vertex_shader(vs
)
175 DCL IN[0], COLOR, LINEAR
176 DCL OUT[0], COLOR, CONSTANT
180 ctx
.set_fragment_shader(fs
)
184 verts
= FloatArray(nverts
* nattrs
* 4)
186 verts
[ 0] = 128.0 # x1
187 verts
[ 1] = 32.0 # y1
194 verts
[ 8] = 32.0 # x2
195 verts
[ 9] = 224.0 # y2
202 verts
[16] = 224.0 # x3
203 verts
[17] = 224.0 # y3
211 ctx
.draw_vertices(PIPE_PRIM_TRIANGLES
,
218 show_image(cbuf
.get_surface(usage
= PIPE_BUFFER_USAGE_CPU_READ|PIPE_BUFFER_USAGE_CPU_WRITE
))
219 #save_image('tri.png', cbuf.get_surface(usage = PIPE_BUFFER_USAGE_CPU_READ|PIPE_BUFFER_USAGE_CPU_WRITE))
228 if __name__
== '__main__':