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