2f1d8b22fbe0f549b5ccfe3dbd20e677a6156088
[mesa.git] / src / gallium / state_trackers / python / gallium.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 %module gallium;
36
37 %{
38
39 #include <stdio.h>
40 #include <Python.h>
41
42 #include "pipe/p_screen.h"
43 #include "pipe/p_context.h"
44 #include "pipe/p_inlines.h"
45 #include "pipe/p_util.h"
46 #include "pipe/p_shader_tokens.h"
47 #include "cso_cache/cso_context.h"
48 #include "util/u_draw_quad.h"
49 #include "util/p_tile.h"
50 #include "tgsi/util/tgsi_text.h"
51 #include "tgsi/util/tgsi_dump.h"
52
53 #include "st_device.h"
54
55 %}
56
57 %include "carrays.i"
58 %array_class(unsigned char, ByteArray);
59 %array_class(int, IntArray);
60 %array_class(float, FloatArray);
61
62
63 %rename(Device) st_device;
64 %rename(Context) st_context;
65 %rename(Texture) pipe_texture;
66 %rename(Surface) pipe_surface;
67
68 %rename(Buffer) pipe_buffer;
69
70 %rename(BlendColor) pipe_blend_color;
71 %rename(Blend) pipe_blend_state;
72 %rename(Clip) pipe_clip_state;
73 %rename(ConstantBuffer) pipe_constant_buffer;
74 %rename(DepthStencilAlpha) pipe_depth_stencil_alpha_state;
75 %rename(FormatBlock) pipe_format_block;
76 %rename(Framebuffer) pipe_framebuffer_state;
77 %rename(PolyStipple) pipe_poly_stipple;
78 %rename(Rasterizer) pipe_rasterizer_state;
79 %rename(Sampler) pipe_sampler_state;
80 %rename(Scissor) pipe_scissor_state;
81 %rename(Shader) pipe_shader_state;
82 %rename(VertexBuffer) pipe_vertex_buffer;
83 %rename(VertexElement) pipe_vertex_element;
84 %rename(Viewport) pipe_viewport_state;
85
86 %nodefaultctor st_device;
87 %nodefaultctor st_context;
88 %nodefaultctor pipe_texture;
89 %nodefaultctor pipe_surface;
90 %nodefaultctor pipe_buffer;
91
92 %nodefaultdtor st_device;
93 %nodefaultdtor st_context;
94 %nodefaultdtor pipe_texture;
95 %nodefaultdtor pipe_surface;
96 %nodefaultdtor pipe_buffer;
97
98 %ignore pipe_texture::screen;
99
100 %ignore pipe_surface::winsys;
101 %immutable pipe_surface::texture;
102 %immutable pipe_surface::buffer;
103
104 %include "p_format.i";
105 %include "pipe/p_defines.h";
106 %include "pipe/p_state.h";
107 %include "pipe/p_shader_tokens.h";
108
109
110 struct st_device {
111 };
112
113 struct st_context {
114 };
115
116
117 %newobject st_device::texture_create;
118 %newobject st_device::context_create;
119 %newobject st_device::buffer_create;
120
121 %extend st_device {
122
123 st_device(int hardware = 1) {
124 return st_device_create(hardware ? TRUE : FALSE);
125 }
126
127 ~st_device() {
128 st_device_destroy($self);
129 }
130
131 const char * get_name( void ) {
132 return $self->screen->get_name($self->screen);
133 }
134
135 const char * get_vendor( void ) {
136 return $self->screen->get_vendor($self->screen);
137 }
138
139 /**
140 * Query an integer-valued capability/parameter/limit
141 * \param param one of PIPE_CAP_x
142 */
143 int get_param( int param ) {
144 return $self->screen->get_param($self->screen, param);
145 }
146
147 /**
148 * Query a float-valued capability/parameter/limit
149 * \param param one of PIPE_CAP_x
150 */
151 float get_paramf( int param ) {
152 return $self->screen->get_paramf($self->screen, param);
153 }
154
155 /**
156 * Check if the given pipe_format is supported as a texture or
157 * drawing surface.
158 * \param type one of PIPE_TEXTURE, PIPE_SURFACE
159 */
160 int is_format_supported( enum pipe_format format, unsigned type ) {
161 return $self->screen->is_format_supported( $self->screen, format, type);
162 }
163
164 struct st_context *
165 context_create(void) {
166 return st_context_create($self);
167 }
168
169 struct pipe_texture *
170 texture_create(
171 enum pipe_format format,
172 unsigned width,
173 unsigned height,
174 unsigned depth = 1,
175 unsigned last_level = 0,
176 enum pipe_texture_target target = PIPE_TEXTURE_2D,
177 unsigned usage = 0
178 ) {
179 struct pipe_texture templat;
180 struct pipe_texture *texture;
181 memset(&templat, 0, sizeof(templat));
182 templat.format = format;
183 pf_get_block(templat.format, &templat.block);
184 templat.width[0] = width;
185 templat.height[0] = height;
186 templat.depth[0] = depth;
187 templat.last_level = last_level;
188 templat.target = target;
189 templat.tex_usage = usage;
190 texture = $self->screen->texture_create($self->screen, &templat);
191 fprintf(stderr, "creating texture %p\n", texture);
192 return texture;
193 }
194
195 struct pipe_buffer *
196 buffer_create(unsigned size, unsigned alignment = 0, unsigned usage = 0) {
197 return $self->screen->winsys->buffer_create($self->screen->winsys, alignment, usage, size);
198 }
199
200 };
201
202
203 %extend st_context {
204
205 ~st_context() {
206 st_context_destroy($self);
207 }
208
209 /*
210 * State functions (create/bind/destroy state objects)
211 */
212
213 void set_blend( const struct pipe_blend_state *state ) {
214 cso_set_blend($self->cso, state);
215 }
216
217 void set_sampler( unsigned index, const struct pipe_sampler_state *state ) {
218 cso_single_sampler($self->cso, index, state);
219 cso_single_sampler_done($self->cso);
220 }
221
222 void set_rasterizer( const struct pipe_rasterizer_state *state ) {
223 cso_set_rasterizer($self->cso, state);
224 }
225
226 void set_depth_stencil_alpha(const struct pipe_depth_stencil_alpha_state *state) {
227 cso_set_depth_stencil_alpha($self->cso, state);
228 }
229
230 void set_fragment_shader( const struct pipe_shader_state *state ) {
231 void *fs;
232
233 fs = $self->pipe->create_fs_state($self->pipe, state);
234 if(!fs)
235 return;
236
237 if(cso_set_fragment_shader_handle($self->cso, fs) != PIPE_OK)
238 return;
239
240 cso_delete_fragment_shader($self->cso, $self->fs);
241 $self->fs = fs;
242 }
243
244 void set_vertex_shader( const struct pipe_shader_state *state ) {
245 void *vs;
246
247 vs = $self->pipe->create_vs_state($self->pipe, state);
248 if(!vs)
249 return;
250
251 if(cso_set_vertex_shader_handle($self->cso, vs) != PIPE_OK)
252 return;
253
254 cso_delete_vertex_shader($self->cso, $self->vs);
255 $self->vs = vs;
256 }
257
258 /*
259 * Parameter-like state (or properties)
260 */
261
262 void set_blend_color(const struct pipe_blend_color *state ) {
263 cso_set_blend_color($self->cso, state);
264 }
265
266 void set_clip(const struct pipe_clip_state *state ) {
267 $self->pipe->set_clip_state($self->pipe, state);
268 }
269
270 void set_constant_buffer(unsigned shader, unsigned index,
271 const struct pipe_constant_buffer *buf ) {
272 $self->pipe->set_constant_buffer($self->pipe, shader, index, buf);
273 }
274
275 void set_framebuffer(const struct pipe_framebuffer_state *state ) {
276 cso_set_framebuffer($self->cso, state);
277 }
278
279 void set_polygon_stipple(const struct pipe_poly_stipple *state ) {
280 $self->pipe->set_polygon_stipple($self->pipe, state);
281 }
282
283 void set_scissor(const struct pipe_scissor_state *state ) {
284 $self->pipe->set_scissor_state($self->pipe, state);
285 }
286
287 void set_viewport(const struct pipe_viewport_state *state) {
288 cso_set_viewport($self->cso, state);
289 }
290
291 void set_sampler_texture(unsigned index,
292 struct pipe_texture *texture) {
293 if(!texture)
294 texture = $self->default_texture;
295 pipe_texture_reference(&$self->sampler_textures[index], texture);
296 $self->pipe->set_sampler_textures($self->pipe,
297 PIPE_MAX_SAMPLERS,
298 $self->sampler_textures);
299 }
300
301 void set_vertex_buffer(unsigned index,
302 const struct pipe_vertex_buffer *buffer) {
303 memcpy(&$self->vertex_buffers[index], buffer, sizeof(*buffer));
304 $self->pipe->set_vertex_buffers($self->pipe, PIPE_MAX_ATTRIBS, $self->vertex_buffers);
305 }
306
307 void set_vertex_element(unsigned index,
308 const struct pipe_vertex_element *element) {
309 memcpy(&$self->vertex_elements[index], element, sizeof(*element));
310 $self->pipe->set_vertex_elements($self->pipe, PIPE_MAX_ATTRIBS, $self->vertex_elements);
311 }
312
313 /*
314 * Draw functions
315 */
316
317 void draw_arrays(unsigned mode, unsigned start, unsigned count) {
318 $self->pipe->draw_arrays($self->pipe, mode, start, count);
319 }
320
321 void draw_elements( struct pipe_buffer *indexBuffer,
322 unsigned indexSize,
323 unsigned mode, unsigned start, unsigned count) {
324 $self->pipe->draw_elements($self->pipe, indexBuffer, indexSize, mode, start, count);
325 }
326
327 void draw_vertices(unsigned prim,
328 unsigned num_verts,
329 unsigned num_attribs,
330 const float *vertices)
331 {
332 struct pipe_context *pipe = $self->pipe;
333 struct pipe_winsys *winsys = pipe->winsys;
334 struct pipe_buffer *vbuf;
335 float *map;
336 unsigned size;
337
338 size = num_verts * num_attribs * 4 * sizeof(float);
339
340 vbuf = winsys->buffer_create(winsys,
341 32,
342 PIPE_BUFFER_USAGE_VERTEX,
343 size);
344 if(!vbuf)
345 goto error1;
346
347 map = winsys->buffer_map(winsys, vbuf, PIPE_BUFFER_USAGE_CPU_WRITE);
348 if (!map)
349 goto error2;
350 memcpy(map, vertices, size);
351 pipe->winsys->buffer_unmap(pipe->winsys, vbuf);
352
353 util_draw_vertex_buffer(pipe, vbuf, prim, num_verts, num_attribs);
354
355 error2:
356 pipe_buffer_reference(pipe->winsys, &vbuf, NULL);
357 error1:
358 ;
359 }
360
361 void
362 flush(void) {
363 struct pipe_fence_handle *fence = NULL;
364 $self->pipe->flush($self->pipe, PIPE_FLUSH_RENDER_CACHE, &fence);
365 /* TODO: allow asynchronous operation */
366 $self->pipe->winsys->fence_finish( $self->pipe->winsys, fence, 0 );
367 $self->pipe->winsys->fence_reference( $self->pipe->winsys, &fence, NULL );
368 }
369
370 /*
371 * Surface functions
372 */
373
374 void surface_copy(int do_flip,
375 struct pipe_surface *dest,
376 unsigned destx, unsigned desty,
377 struct pipe_surface *src,
378 unsigned srcx, unsigned srcy,
379 unsigned width, unsigned height) {
380 $self->pipe->surface_copy($self->pipe, do_flip, dest, destx, desty, src, srcx, srcy, width, height);
381 }
382
383 void surface_fill(struct pipe_surface *dst,
384 unsigned x, unsigned y,
385 unsigned width, unsigned height,
386 unsigned value) {
387 $self->pipe->surface_fill($self->pipe, dst, x, y, width, height, value);
388 }
389
390 void surface_clear(struct pipe_surface *surface, unsigned value = 0) {
391 $self->pipe->clear($self->pipe, surface, value);
392 }
393
394 };
395
396
397 %newobject pipe_texture::get_surface;
398
399 %extend pipe_texture {
400
401 ~pipe_texture() {
402 struct pipe_texture *ptr = $self;
403 fprintf(stderr, "destroying texture %p\n", $self);
404 pipe_texture_reference(&ptr, NULL);
405 }
406
407 /** Get a surface which is a "view" into a texture */
408 struct pipe_surface *
409 get_surface(unsigned face=0, unsigned level=0, unsigned zslice=0, unsigned usage=0 )
410 {
411 struct pipe_screen *screen = $self->screen;
412 struct pipe_surface *surface;
413 surface = screen->get_tex_surface(screen, $self, face, level, zslice, usage);
414 fprintf(stderr, "creating surface %p\n", surface);
415 return surface;
416 }
417
418 };
419
420
421 %extend pipe_surface {
422
423 ~pipe_surface() {
424 struct pipe_surface *ptr = $self;
425 fprintf(stderr, "destroying surface %p\n", $self);
426 pipe_surface_reference(&ptr, NULL);
427 }
428
429 // gets mapped to pipe_surface_map automatically
430 void * map( unsigned flags );
431
432 // gets mapped to pipe_surface_unmap automatically
433 void unmap( void );
434
435 void
436 get_tile_raw(unsigned x, unsigned y, unsigned w, unsigned h, unsigned char *p, unsigned stride) {
437 pipe_get_tile_raw($self, x, y, w, h, p, stride);
438 }
439
440 void
441 put_tile_raw(unsigned x, unsigned y, unsigned w, unsigned h, const unsigned char *p, unsigned stride) {
442 pipe_put_tile_raw($self, x, y, w, h, p, stride);
443 }
444
445 void
446 get_tile_rgba(unsigned x, unsigned y, unsigned w, unsigned h, float *p) {
447 pipe_get_tile_rgba($self, x, y, w, h, p);
448 }
449
450 void
451 put_tile_rgba(unsigned x, unsigned y, unsigned w, unsigned h, const float *p) {
452 pipe_put_tile_rgba($self, x, y, w, h, p);
453 }
454
455 void
456 get_tile_z(unsigned x, unsigned y, unsigned w, unsigned h, unsigned *z) {
457 pipe_get_tile_z($self, x, y, w, h, z);
458 }
459
460 void
461 put_tile_z(unsigned x, unsigned y, unsigned w, unsigned h, const unsigned *z) {
462 pipe_put_tile_z($self, x, y, w, h, z);
463 }
464
465 };
466
467
468 %extend pipe_framebuffer_state {
469
470 pipe_framebuffer_state(void) {
471 return CALLOC_STRUCT(pipe_framebuffer_state);
472 }
473
474 ~pipe_framebuffer_state() {
475 unsigned index;
476 for(index = 0; index < PIPE_MAX_COLOR_BUFS; ++index)
477 pipe_surface_reference(&$self->cbufs[index], NULL);
478 pipe_surface_reference(&$self->zsbuf, NULL);
479 FREE($self);
480 }
481
482 void
483 set_cbuf(unsigned index, struct pipe_surface *surface) {
484 pipe_surface_reference(&$self->cbufs[index], surface);
485 }
486
487 void
488 set_zsbuf(struct pipe_surface *surface) {
489 pipe_surface_reference(&$self->zsbuf, surface);
490 }
491
492 };
493
494
495 %extend pipe_shader_state {
496
497 pipe_shader_state(const char *text, unsigned num_tokens = 1024) {
498 struct tgsi_token *tokens;
499 struct pipe_shader_state *shader;
500
501 tokens = MALLOC(num_tokens * sizeof(struct tgsi_token));
502 if(!tokens)
503 goto error1;
504
505 if(tgsi_text_translate(text, tokens, num_tokens ) != TRUE)
506 goto error2;
507
508 shader = CALLOC_STRUCT(pipe_shader_state);
509 if(!shader)
510 goto error3;
511
512 shader->tokens = tokens;
513
514 return shader;
515
516 error3:
517 error2:
518 FREE(tokens);
519 error1:
520 return NULL;
521 }
522
523 ~pipe_shader_state() {
524 FREE((void*)$self->tokens);
525 FREE($self);
526 }
527
528 void dump(unsigned flags = 0) {
529 tgsi_dump($self->tokens, flags);
530 }
531 }