Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / state_trackers / python / p_context.i
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * SWIG interface definion for Gallium types.
31 *
32 * @author Jose Fonseca <jrfonseca@tungstengraphics.com>
33 */
34
35 %nodefaultctor st_context;
36 %nodefaultdtor st_context;
37
38 struct st_context {
39 };
40
41 %extend st_context {
42
43 ~st_context() {
44 st_context_destroy($self);
45 }
46
47 /*
48 * State functions (create/bind/destroy state objects)
49 */
50
51 void set_blend( const struct pipe_blend_state *state ) {
52 cso_set_blend($self->cso, state);
53 }
54
55 void set_fragment_sampler( unsigned index, const struct pipe_sampler_state *state ) {
56 cso_single_sampler($self->cso, index, state);
57 cso_single_sampler_done($self->cso);
58 }
59
60 void set_vertex_sampler( unsigned index, const struct pipe_sampler_state *state ) {
61 cso_single_vertex_sampler($self->cso, index, state);
62 cso_single_vertex_sampler_done($self->cso);
63 }
64
65 void set_rasterizer( const struct pipe_rasterizer_state *state ) {
66 cso_set_rasterizer($self->cso, state);
67 }
68
69 void set_depth_stencil_alpha(const struct pipe_depth_stencil_alpha_state *state) {
70 cso_set_depth_stencil_alpha($self->cso, state);
71 }
72
73 void set_fragment_shader( const struct pipe_shader_state *state ) {
74 void *fs;
75
76 if(!state) {
77 cso_set_fragment_shader_handle($self->cso, NULL);
78 return;
79 }
80
81 fs = $self->pipe->create_fs_state($self->pipe, state);
82 if(!fs)
83 return;
84
85 if(cso_set_fragment_shader_handle($self->cso, fs) != PIPE_OK)
86 return;
87
88 cso_delete_fragment_shader($self->cso, $self->fs);
89 $self->fs = fs;
90 }
91
92 void set_vertex_shader( const struct pipe_shader_state *state ) {
93 void *vs;
94
95 if(!state) {
96 cso_set_vertex_shader_handle($self->cso, NULL);
97 return;
98 }
99
100 vs = $self->pipe->create_vs_state($self->pipe, state);
101 if(!vs)
102 return;
103
104 if(cso_set_vertex_shader_handle($self->cso, vs) != PIPE_OK)
105 return;
106
107 cso_delete_vertex_shader($self->cso, $self->vs);
108 $self->vs = vs;
109 }
110
111 void set_geometry_shader( const struct pipe_shader_state *state ) {
112 void *gs;
113
114 if(!state) {
115 cso_set_geometry_shader_handle($self->cso, NULL);
116 return;
117 }
118
119 gs = $self->pipe->create_gs_state($self->pipe, state);
120 if(!gs)
121 return;
122
123 if(cso_set_geometry_shader_handle($self->cso, gs) != PIPE_OK)
124 return;
125
126 cso_delete_geometry_shader($self->cso, $self->gs);
127 $self->gs = gs;
128 }
129
130 /*
131 * Parameter-like state (or properties)
132 */
133
134 void set_blend_color(const struct pipe_blend_color *state ) {
135 cso_set_blend_color($self->cso, state);
136 }
137
138 void set_clip(const struct pipe_clip_state *state ) {
139 $self->pipe->set_clip_state($self->pipe, state);
140 }
141
142 void set_constant_buffer(unsigned shader, unsigned index,
143 struct pipe_buffer *buffer )
144 {
145 struct pipe_constant_buffer state;
146 memset(&state, 0, sizeof(state));
147 state.buffer = buffer;
148 $self->pipe->set_constant_buffer($self->pipe, shader, index, &state);
149 }
150
151 void set_framebuffer(const struct pipe_framebuffer_state *state )
152 {
153 memcpy(&$self->framebuffer, state, sizeof *state);
154 cso_set_framebuffer($self->cso, state);
155 }
156
157 void set_polygon_stipple(const struct pipe_poly_stipple *state ) {
158 $self->pipe->set_polygon_stipple($self->pipe, state);
159 }
160
161 void set_scissor(const struct pipe_scissor_state *state ) {
162 $self->pipe->set_scissor_state($self->pipe, state);
163 }
164
165 void set_viewport(const struct pipe_viewport_state *state) {
166 cso_set_viewport($self->cso, state);
167 }
168
169 void set_fragment_sampler_texture(unsigned index,
170 struct pipe_texture *texture) {
171 if(!texture)
172 texture = $self->default_texture;
173 pipe_texture_reference(&$self->fragment_sampler_textures[index], texture);
174 $self->pipe->set_fragment_sampler_textures($self->pipe,
175 PIPE_MAX_SAMPLERS,
176 $self->fragment_sampler_textures);
177 }
178
179 void set_vertex_sampler_texture(unsigned index,
180 struct pipe_texture *texture) {
181 if(!texture)
182 texture = $self->default_texture;
183 pipe_texture_reference(&$self->vertex_sampler_textures[index], texture);
184 $self->pipe->set_vertex_sampler_textures($self->pipe,
185 PIPE_MAX_VERTEX_SAMPLERS,
186 $self->vertex_sampler_textures);
187 }
188
189 void set_vertex_buffer(unsigned index,
190 unsigned stride,
191 unsigned max_index,
192 unsigned buffer_offset,
193 struct pipe_buffer *buffer)
194 {
195 unsigned i;
196 struct pipe_vertex_buffer state;
197
198 memset(&state, 0, sizeof(state));
199 state.stride = stride;
200 state.max_index = max_index;
201 state.buffer_offset = buffer_offset;
202 state.buffer = buffer;
203
204 memcpy(&$self->vertex_buffers[index], &state, sizeof(state));
205
206 for(i = 0; i < PIPE_MAX_ATTRIBS; ++i)
207 if(self->vertex_buffers[i].buffer)
208 $self->num_vertex_buffers = i + 1;
209
210 $self->pipe->set_vertex_buffers($self->pipe,
211 $self->num_vertex_buffers,
212 $self->vertex_buffers);
213 }
214
215 void set_vertex_element(unsigned index,
216 const struct pipe_vertex_element *element)
217 {
218 memcpy(&$self->vertex_elements[index], element, sizeof(*element));
219 }
220
221 void set_vertex_elements(unsigned num)
222 {
223 $self->num_vertex_elements = num;
224 $self->pipe->set_vertex_elements($self->pipe,
225 $self->num_vertex_elements,
226 $self->vertex_elements);
227 }
228
229 /*
230 * Draw functions
231 */
232
233 void draw_arrays(unsigned mode, unsigned start, unsigned count) {
234 $self->pipe->draw_arrays($self->pipe, mode, start, count);
235 }
236
237 void draw_elements( struct pipe_buffer *indexBuffer,
238 unsigned indexSize,
239 unsigned mode, unsigned start, unsigned count)
240 {
241 $self->pipe->draw_elements($self->pipe,
242 indexBuffer,
243 indexSize,
244 mode, start, count);
245 }
246
247 void draw_range_elements( struct pipe_buffer *indexBuffer,
248 unsigned indexSize, unsigned minIndex, unsigned maxIndex,
249 unsigned mode, unsigned start, unsigned count)
250 {
251 $self->pipe->draw_range_elements($self->pipe,
252 indexBuffer,
253 indexSize, minIndex, maxIndex,
254 mode, start, count);
255 }
256
257 void draw_vertices(unsigned prim,
258 unsigned num_verts,
259 unsigned num_attribs,
260 const float *vertices)
261 {
262 struct pipe_context *pipe = $self->pipe;
263 struct pipe_screen *screen = pipe->screen;
264 struct pipe_buffer *vbuf;
265 float *map;
266 unsigned size;
267
268 size = num_verts * num_attribs * 4 * sizeof(float);
269
270 vbuf = pipe_buffer_create(screen,
271 32,
272 PIPE_BUFFER_USAGE_VERTEX,
273 size);
274 if(!vbuf)
275 goto error1;
276
277 map = pipe_buffer_map(screen, vbuf, PIPE_BUFFER_USAGE_CPU_WRITE);
278 if (!map)
279 goto error2;
280 memcpy(map, vertices, size);
281 pipe_buffer_unmap(screen, vbuf);
282
283 util_draw_vertex_buffer(pipe, vbuf, 0, prim, num_verts, num_attribs);
284
285 error2:
286 pipe_buffer_reference(&vbuf, NULL);
287 error1:
288 ;
289 }
290
291 void
292 flush(unsigned flags = 0) {
293 struct pipe_fence_handle *fence = NULL;
294 $self->pipe->flush($self->pipe, flags | PIPE_FLUSH_RENDER_CACHE, &fence);
295 if(fence) {
296 /* TODO: allow asynchronous operation */
297 $self->pipe->screen->fence_finish( $self->pipe->screen, fence, 0 );
298 $self->pipe->screen->fence_reference( $self->pipe->screen, &fence, NULL );
299 }
300 }
301
302 /*
303 * Surface functions
304 */
305
306 void surface_copy(struct st_surface *dst,
307 unsigned destx, unsigned desty,
308 struct st_surface *src,
309 unsigned srcx, unsigned srcy,
310 unsigned width, unsigned height)
311 {
312 struct pipe_surface *_dst = NULL;
313 struct pipe_surface *_src = NULL;
314
315 _dst = st_pipe_surface(dst, PIPE_BUFFER_USAGE_GPU_WRITE);
316 if(!_dst)
317 SWIG_exception(SWIG_ValueError, "couldn't acquire destination surface for writing");
318
319 _src = st_pipe_surface(src, PIPE_BUFFER_USAGE_GPU_READ);
320 if(!_src)
321 SWIG_exception(SWIG_ValueError, "couldn't acquire source surface for reading");
322
323 $self->pipe->surface_copy($self->pipe, _dst, destx, desty, _src, srcx, srcy, width, height);
324
325 fail:
326 pipe_surface_reference(&_src, NULL);
327 pipe_surface_reference(&_dst, NULL);
328 }
329
330 void surface_fill(struct st_surface *dst,
331 unsigned x, unsigned y,
332 unsigned width, unsigned height,
333 unsigned value)
334 {
335 struct pipe_surface *_dst = NULL;
336
337 _dst = st_pipe_surface(dst, PIPE_BUFFER_USAGE_GPU_WRITE);
338 if(!_dst)
339 SWIG_exception(SWIG_ValueError, "couldn't acquire destination surface for writing");
340
341 $self->pipe->surface_fill($self->pipe, _dst, x, y, width, height, value);
342
343 fail:
344 pipe_surface_reference(&_dst, NULL);
345 }
346
347 void clear(unsigned buffers, const float *rgba, double depth = 0.0f,
348 unsigned stencil = 0)
349 {
350 $self->pipe->clear($self->pipe, buffers, rgba, depth, stencil);
351 }
352
353 };