1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * SWIG interface definion for Gallium types.
32 * @author Jose Fonseca <jrfonseca@tungstengraphics.com>
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"
53 #include "st_device.h"
54 #include "st_sample.h"
59 %array_class(unsigned char, ByteArray);
60 %array_class(int, IntArray);
61 %array_class(float, FloatArray);
64 %rename(Device) st_device;
65 %rename(Context) st_context;
66 %rename(Texture) pipe_texture;
67 %rename(Surface) pipe_surface;
69 %rename(Buffer) pipe_buffer;
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;
87 %nodefaultctor st_device;
88 %nodefaultctor st_context;
89 %nodefaultctor pipe_texture;
90 %nodefaultctor pipe_surface;
91 %nodefaultctor pipe_buffer;
93 %nodefaultdtor st_device;
94 %nodefaultdtor st_context;
95 %nodefaultdtor pipe_texture;
96 %nodefaultdtor pipe_surface;
97 %nodefaultdtor pipe_buffer;
99 %ignore pipe_texture::screen;
101 %ignore pipe_surface::winsys;
102 %immutable pipe_surface::texture;
103 %immutable pipe_surface::buffer;
105 %include "p_format.i";
106 %include "pipe/p_defines.h";
107 %include "pipe/p_state.h";
108 %include "pipe/p_shader_tokens.h";
118 %newobject st_device::texture_create;
119 %newobject st_device::context_create;
120 %newobject st_device::buffer_create;
124 st_device(int hardware = 1) {
125 return st_device_create(hardware ? TRUE : FALSE);
129 st_device_destroy($self);
132 const char * get_name( void ) {
133 return $self->screen->get_name($self->screen);
136 const char * get_vendor( void ) {
137 return $self->screen->get_vendor($self->screen);
141 * Query an integer-valued capability/parameter/limit
142 * \param param one of PIPE_CAP_x
144 int get_param( int param ) {
145 return $self->screen->get_param($self->screen, param);
149 * Query a float-valued capability/parameter/limit
150 * \param param one of PIPE_CAP_x
152 float get_paramf( int param ) {
153 return $self->screen->get_paramf($self->screen, param);
157 * Check if the given pipe_format is supported as a texture or
159 * \param type one of PIPE_TEXTURE, PIPE_SURFACE
161 int is_format_supported( enum pipe_format format,
162 enum pipe_texture_target target,
164 unsigned geom_flags ) {
165 return $self->screen->is_format_supported( $self->screen,
173 context_create(void) {
174 return st_context_create($self);
177 struct pipe_texture *
179 enum pipe_format format,
183 unsigned last_level = 0,
184 enum pipe_texture_target target = PIPE_TEXTURE_2D,
185 unsigned tex_usage = 0
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);
201 buffer_create(unsigned size, unsigned alignment = 0, unsigned usage = 0) {
202 return $self->screen->winsys->buffer_create($self->screen->winsys, alignment, usage, size);
211 st_context_destroy($self);
215 * State functions (create/bind/destroy state objects)
218 void set_blend( const struct pipe_blend_state *state ) {
219 cso_set_blend($self->cso, state);
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);
227 void set_rasterizer( const struct pipe_rasterizer_state *state ) {
228 cso_set_rasterizer($self->cso, state);
231 void set_depth_stencil_alpha(const struct pipe_depth_stencil_alpha_state *state) {
232 cso_set_depth_stencil_alpha($self->cso, state);
235 void set_fragment_shader( const struct pipe_shader_state *state ) {
238 fs = $self->pipe->create_fs_state($self->pipe, state);
242 if(cso_set_fragment_shader_handle($self->cso, fs) != PIPE_OK)
245 cso_delete_fragment_shader($self->cso, $self->fs);
249 void set_vertex_shader( const struct pipe_shader_state *state ) {
252 vs = $self->pipe->create_vs_state($self->pipe, state);
256 if(cso_set_vertex_shader_handle($self->cso, vs) != PIPE_OK)
259 cso_delete_vertex_shader($self->cso, $self->vs);
264 * Parameter-like state (or properties)
267 void set_blend_color(const struct pipe_blend_color *state ) {
268 cso_set_blend_color($self->cso, state);
271 void set_clip(const struct pipe_clip_state *state ) {
272 $self->pipe->set_clip_state($self->pipe, state);
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);
280 void set_framebuffer(const struct pipe_framebuffer_state *state ) {
281 cso_set_framebuffer($self->cso, state);
284 void set_polygon_stipple(const struct pipe_poly_stipple *state ) {
285 $self->pipe->set_polygon_stipple($self->pipe, state);
288 void set_scissor(const struct pipe_scissor_state *state ) {
289 $self->pipe->set_scissor_state($self->pipe, state);
292 void set_viewport(const struct pipe_viewport_state *state) {
293 cso_set_viewport($self->cso, state);
296 void set_sampler_texture(unsigned index,
297 struct pipe_texture *texture) {
299 texture = $self->default_texture;
300 pipe_texture_reference(&$self->sampler_textures[index], texture);
301 $self->pipe->set_sampler_textures($self->pipe,
303 $self->sampler_textures);
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);
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);
322 void draw_arrays(unsigned mode, unsigned start, unsigned count) {
323 $self->pipe->draw_arrays($self->pipe, mode, start, count);
326 void draw_elements( struct pipe_buffer *indexBuffer,
328 unsigned mode, unsigned start, unsigned count) {
329 $self->pipe->draw_elements($self->pipe, indexBuffer, indexSize, mode, start, count);
332 void draw_vertices(unsigned prim,
334 unsigned num_attribs,
335 const float *vertices)
337 struct pipe_context *pipe = $self->pipe;
338 struct pipe_winsys *winsys = pipe->winsys;
339 struct pipe_buffer *vbuf;
343 size = num_verts * num_attribs * 4 * sizeof(float);
345 vbuf = winsys->buffer_create(winsys,
347 PIPE_BUFFER_USAGE_VERTEX,
352 map = winsys->buffer_map(winsys, vbuf, PIPE_BUFFER_USAGE_CPU_WRITE);
355 memcpy(map, vertices, size);
356 pipe->winsys->buffer_unmap(pipe->winsys, vbuf);
358 util_draw_vertex_buffer(pipe, vbuf, prim, num_verts, num_attribs);
361 pipe_buffer_reference(pipe->winsys, &vbuf, NULL);
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 );
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);
388 void surface_fill(struct pipe_surface *dst,
389 unsigned x, unsigned y,
390 unsigned width, unsigned height,
392 $self->pipe->surface_fill($self->pipe, dst, x, y, width, height, value);
395 void surface_clear(struct pipe_surface *surface, unsigned value = 0) {
396 $self->pipe->clear($self->pipe, surface, value);
402 %newobject pipe_texture::get_surface;
404 %extend pipe_texture {
407 struct pipe_texture *ptr = $self;
408 pipe_texture_reference(&ptr, NULL);
411 unsigned get_width(unsigned level=0) {
412 return $self->width[level];
415 unsigned get_height(unsigned level=0) {
416 return $self->height[level];
419 unsigned get_depth(unsigned level=0) {
420 return $self->depth[level];
423 unsigned get_nblocksx(unsigned level=0) {
424 return $self->nblocksx[level];
427 unsigned get_nblocksy(unsigned level=0) {
428 return $self->nblocksy[level];
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 )
435 struct pipe_screen *screen = $self->screen;
436 return screen->get_tex_surface(screen, $self, face, level, zslice, usage);
442 %extend pipe_surface {
445 struct pipe_surface *ptr = $self;
446 pipe_surface_reference(&ptr, NULL);
449 // gets mapped to pipe_surface_map automatically
450 void * map( unsigned flags );
452 // gets mapped to pipe_surface_unmap automatically
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);
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);
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);
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);
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);
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);
486 sample_rgba(float *rgba) {
487 st_sample_surface($self, rgba);
491 compare_tile_rgba(unsigned x, unsigned y, unsigned w, unsigned h, const float *rgba, float tol = 0.0)
498 rgba2 = MALLOC(h*w*4*sizeof(float));
502 pipe_get_tile_rgba($self, x, y, w, h, rgba2);
507 for(i = h*w; i; --i) {
508 unsigned differs = 0;
510 float delta = *p2++ - *p1++;
511 if (delta < -tol || delta > tol)
525 %extend pipe_framebuffer_state {
527 pipe_framebuffer_state(void) {
528 return CALLOC_STRUCT(pipe_framebuffer_state);
531 ~pipe_framebuffer_state() {
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);
540 set_cbuf(unsigned index, struct pipe_surface *surface) {
541 pipe_surface_reference(&$self->cbufs[index], surface);
545 set_zsbuf(struct pipe_surface *surface) {
546 pipe_surface_reference(&$self->zsbuf, surface);
552 %extend pipe_shader_state {
554 pipe_shader_state(const char *text, unsigned num_tokens = 1024) {
555 struct tgsi_token *tokens;
556 struct pipe_shader_state *shader;
558 tokens = MALLOC(num_tokens * sizeof(struct tgsi_token));
562 if(tgsi_text_translate(text, tokens, num_tokens ) != TRUE)
565 shader = CALLOC_STRUCT(pipe_shader_state);
569 shader->tokens = tokens;
580 ~pipe_shader_state() {
581 FREE((void*)$self->tokens);
585 void dump(unsigned flags = 0) {
586 tgsi_dump($self->tokens, flags);