2 ##########################################################################
4 # Copyright 2009 VMware, Inc.
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 VMWARE 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 data
= surface
.get_tile_rgba8(0, 0, surface
.width
, surface
.height
)
37 outimage
= Image
.fromstring('RGBA', (surface
.width
, surface
.height
), data
, "raw", 'RGBA', 0, 1)
40 def save_image(filename
, surface
):
41 outimage
= make_image(surface
)
42 outimage
.save(filename
, "PNG")
45 ctx
= dev
.context_create()
52 # disabled blending/masking
54 blend
.rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_ONE
55 blend
.rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_ONE
56 blend
.rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
57 blend
.rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
58 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
62 depth_stencil_alpha
= DepthStencilAlpha()
63 depth_stencil_alpha
.depth
.enabled
= 0
64 depth_stencil_alpha
.depth
.writemask
= 1
65 depth_stencil_alpha
.depth
.func
= PIPE_FUNC_LESS
66 ctx
.set_depth_stencil_alpha(depth_stencil_alpha
)
69 rasterizer
= Rasterizer()
70 rasterizer
.front_winding
= PIPE_WINDING_CW
71 rasterizer
.cull_mode
= PIPE_WINDING_NONE
72 rasterizer
.scissor
= 1
73 ctx
.set_rasterizer(rasterizer
)
78 scale
[0] = width
/ 2.0
79 scale
[1] = -height
/ 2.0
80 scale
[2] = (maxz
- minz
) / 2.0
82 viewport
.scale
= scale
83 translate
= FloatArray(4)
84 translate
[0] = width
/ 2.0
85 translate
[1] = height
/ 2.0
86 translate
[2] = (maxz
- minz
) / 2.0
88 viewport
.translate
= translate
89 ctx
.set_viewport(viewport
)
93 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
94 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
95 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
96 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
97 sampler
.min_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
98 sampler
.mag_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
99 sampler
.normalized_coords
= 1
100 ctx
.set_fragment_sampler(0, sampler
)
107 scissor
.maxy
= height
108 ctx
.set_scissor(scissor
)
115 cbuf
= dev
.resource_create(
116 PIPE_FORMAT_B8G8R8X8_UNORM
,
118 bind
=PIPE_BIND_RENDER_TARGET
,
125 ctx
.set_framebuffer(fb
)
126 rgba
= FloatArray(4);
131 ctx
.clear(PIPE_CLEAR_COLOR
, rgba
, 0.0, 0)
144 ctx
.set_vertex_shader(vs
)
147 fs
= Shader(file('frag-' + name
+ '.sh', 'rt').read())
148 ctx
.set_fragment_shader(fs
)
150 constbuf0
= dev
.buffer_create(64,
151 (PIPE_BUFFER_USAGE_CONSTANT |
152 PIPE_BUFFER_USAGE_GPU_READ |
153 PIPE_BUFFER_USAGE_CPU_WRITE
),
157 cbdata
+= struct
.pack('4f', 0.4, 0.0, 0.0, 1.0)
158 cbdata
+= struct
.pack('4f', 1.0, 1.0, 1.0, 1.0)
159 cbdata
+= struct
.pack('4f', 2.0, 2.0, 2.0, 2.0)
160 cbdata
+= struct
.pack('4f', 4.0, 8.0, 16.0, 32.0)
162 constbuf0
.write(cbdata
, 0)
164 ctx
.set_constant_buffer(PIPE_SHADER_FRAGMENT
,
168 constbuf1
= dev
.buffer_create(64,
169 (PIPE_BUFFER_USAGE_CONSTANT |
170 PIPE_BUFFER_USAGE_GPU_READ |
171 PIPE_BUFFER_USAGE_CPU_WRITE
),
175 cbdata
+= struct
.pack('4f', 0.1, 0.1, 0.1, 0.1)
176 cbdata
+= struct
.pack('4f', 0.25, 0.25, 0.25, 0.25)
177 cbdata
+= struct
.pack('4f', 0.5, 0.5, 0.5, 0.5)
178 cbdata
+= struct
.pack('4f', 0.75, 0.75, 0.75, 0.75)
180 constbuf1
.write(cbdata
, 0)
182 ctx
.set_constant_buffer(PIPE_SHADER_FRAGMENT
,
199 verts
= FloatArray(nverts
* nattrs
* 4)
201 for i
in range(0, nverts
):
202 verts
[i
* nattrs
* 4 + 0] = xy
[i
* 2 + 0] # x
203 verts
[i
* nattrs
* 4 + 1] = xy
[i
* 2 + 1] # y
204 verts
[i
* nattrs
* 4 + 2] = 0.5 # z
205 verts
[i
* nattrs
* 4 + 3] = 1.0 # w
206 verts
[i
* nattrs
* 4 + 4] = color
[i
* 3 + 0] # r
207 verts
[i
* nattrs
* 4 + 5] = color
[i
* 3 + 1] # g
208 verts
[i
* nattrs
* 4 + 6] = color
[i
* 3 + 2] # b
209 verts
[i
* nattrs
* 4 + 7] = 1.0 # a
211 ctx
.draw_vertices(PIPE_PRIM_TRIANGLES
,
218 save_image('frag-' + name
+ '.png', cbuf
)
256 if __name__
== '__main__':