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(ctx
, surface
):
34 data
= ctx
.surface_read_rgba8(surface
, 0, 0, surface
.width
, surface
.height
)
37 outimage
= Image
.fromstring('RGBA', (surface
.width
, surface
.height
), data
, "raw", 'RGBA', 0, 1)
40 def save_image(ctx
, surface
, filename
):
41 outimage
= make_image(ctx
, surface
)
42 outimage
.save(filename
, "PNG")
44 def show_image(ctx
, surface
):
45 outimage
= make_image(ctx
, surface
)
48 from PIL
import Image
, ImageTk
51 root
.title('background image')
53 image1
= ImageTk
.PhotoImage(outimage
)
58 root
.geometry("%dx%d+%d+%d" % (w
, h
, x
, y
))
59 panel1
= tk
.Label(root
, image
=image1
)
60 panel1
.pack(side
='top', fill
='both', expand
='yes')
66 ctx
= dev
.context_create()
73 # disabled blending/masking
75 blend
.rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_ONE
76 blend
.rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_ONE
77 blend
.rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
78 blend
.rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
79 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
83 depth_stencil_alpha
= DepthStencilAlpha()
84 depth_stencil_alpha
.depth
.enabled
= 1
85 depth_stencil_alpha
.depth
.writemask
= 1
86 depth_stencil_alpha
.depth
.func
= PIPE_FUNC_LESS
87 ctx
.set_depth_stencil_alpha(depth_stencil_alpha
)
90 rasterizer
= Rasterizer()
91 rasterizer
.front_winding
= PIPE_WINDING_CW
92 rasterizer
.cull_mode
= PIPE_WINDING_NONE
93 rasterizer
.scissor
= 1
94 ctx
.set_rasterizer(rasterizer
)
99 scale
[0] = width
/ 2.0
100 scale
[1] = -height
/ 2.0
101 scale
[2] = (maxz
- minz
) / 2.0
103 viewport
.scale
= scale
104 translate
= FloatArray(4)
105 translate
[0] = width
/ 2.0
106 translate
[1] = height
/ 2.0
107 translate
[2] = (maxz
- minz
) / 2.0
109 viewport
.translate
= translate
110 ctx
.set_viewport(viewport
)
114 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
115 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
116 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
117 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
118 sampler
.min_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
119 sampler
.mag_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
120 sampler
.normalized_coords
= 1
121 ctx
.set_fragment_sampler(0, sampler
)
128 scissor
.maxy
= height
129 ctx
.set_scissor(scissor
)
137 cbuf
= dev
.texture_create(
138 PIPE_FORMAT_B8G8R8X8_UNORM
,
140 tex_usage
=PIPE_TEXTURE_USAGE_RENDER_TARGET
,
142 zbuf
= dev
.texture_create(
143 PIPE_FORMAT_Z32_UNORM
,
145 tex_usage
=PIPE_TEXTURE_USAGE_DEPTH_STENCIL
,
153 ctx
.set_framebuffer(fb
)
154 rgba
= FloatArray(4);
159 ctx
.clear(PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTHSTENCIL
, rgba
, 1.0, 0xff)
164 DCL IN[0], POSITION, CONSTANT
165 DCL IN[1], COLOR, CONSTANT
166 DCL OUT[0], POSITION, CONSTANT
167 DCL OUT[1], COLOR, CONSTANT
172 ctx
.set_vertex_shader(vs
)
177 DCL IN[0], COLOR, LINEAR
178 DCL OUT[0], COLOR, CONSTANT
182 ctx
.set_fragment_shader(fs
)
186 verts
= FloatArray(nverts
* nattrs
* 4)
196 verts
[ 8] = -0.8 # x2
197 verts
[ 9] = -0.8 # y2
205 verts
[17] = -0.8 # y3
213 ctx
.draw_vertices(PIPE_PRIM_TRIANGLES
,
220 show_image(ctx
, cbuf
)
221 show_image(ctx
, zbuf
)
222 save_image(ctx
, cbuf
, 'cbuf.png')
223 save_image(ctx
, zbuf
, 'zbuf.png')
232 if __name__
== '__main__':