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/util/tgsi_text.h"
51 #include "tgsi/util/tgsi_dump.h"
53 #include "st_device.h"
58 %array_class(unsigned char, ByteArray);
59 %array_class(int, IntArray);
60 %array_class(float, FloatArray);
63 %rename(Device) st_device;
64 %rename(Context) st_context;
65 %rename(Texture) pipe_texture;
66 %rename(Surface) pipe_surface;
68 %rename(Buffer) pipe_buffer;
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;
86 %nodefaultctor st_device;
87 %nodefaultctor st_context;
88 %nodefaultctor pipe_texture;
89 %nodefaultctor pipe_surface;
90 %nodefaultctor pipe_buffer;
92 %nodefaultdtor st_device;
93 %nodefaultdtor st_context;
94 %nodefaultdtor pipe_texture;
95 %nodefaultdtor pipe_surface;
96 %nodefaultdtor pipe_buffer;
98 %ignore pipe_texture::screen;
100 %ignore pipe_surface::winsys;
101 %immutable pipe_surface::texture;
102 %immutable pipe_surface::buffer;
104 %include "p_format.i";
105 %include "pipe/p_defines.h";
106 %include "pipe/p_state.h";
107 %include "pipe/p_shader_tokens.h";
117 %newobject st_device::texture_create;
118 %newobject st_device::context_create;
119 %newobject st_device::buffer_create;
123 st_device(int hardware = 1) {
124 return st_device_create(hardware ? TRUE : FALSE);
128 st_device_destroy($self);
131 const char * get_name( void ) {
132 return $self->screen->get_name($self->screen);
135 const char * get_vendor( void ) {
136 return $self->screen->get_vendor($self->screen);
140 * Query an integer-valued capability/parameter/limit
141 * \param param one of PIPE_CAP_x
143 int get_param( int param ) {
144 return $self->screen->get_param($self->screen, param);
148 * Query a float-valued capability/parameter/limit
149 * \param param one of PIPE_CAP_x
151 float get_paramf( int param ) {
152 return $self->screen->get_paramf($self->screen, param);
156 * Check if the given pipe_format is supported as a texture or
158 * \param type one of PIPE_TEXTURE, PIPE_SURFACE
160 int is_format_supported( enum pipe_format format, unsigned type ) {
161 return $self->screen->is_format_supported( $self->screen, format, type);
165 context_create(void) {
166 return st_context_create($self);
169 struct pipe_texture *
171 enum pipe_format format,
175 unsigned last_level = 0,
176 enum pipe_texture_target target = PIPE_TEXTURE_2D,
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);
196 buffer_create(unsigned size, unsigned alignment = 0, unsigned usage = 0) {
197 return $self->screen->winsys->buffer_create($self->screen->winsys, alignment, usage, size);
206 st_context_destroy($self);
210 * State functions (create/bind/destroy state objects)
213 void set_blend( const struct pipe_blend_state *state ) {
214 cso_set_blend($self->cso, state);
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);
222 void set_rasterizer( const struct pipe_rasterizer_state *state ) {
223 cso_set_rasterizer($self->cso, state);
226 void set_depth_stencil_alpha(const struct pipe_depth_stencil_alpha_state *state) {
227 cso_set_depth_stencil_alpha($self->cso, state);
230 void set_fragment_shader( const struct pipe_shader_state *state ) {
233 fs = $self->pipe->create_fs_state($self->pipe, state);
237 if(cso_set_fragment_shader_handle($self->cso, fs) != PIPE_OK)
240 cso_delete_fragment_shader($self->cso, $self->fs);
244 void set_vertex_shader( const struct pipe_shader_state *state ) {
247 vs = $self->pipe->create_vs_state($self->pipe, state);
251 if(cso_set_vertex_shader_handle($self->cso, vs) != PIPE_OK)
254 cso_delete_vertex_shader($self->cso, $self->vs);
259 * Parameter-like state (or properties)
262 void set_blend_color(const struct pipe_blend_color *state ) {
263 cso_set_blend_color($self->cso, state);
266 void set_clip(const struct pipe_clip_state *state ) {
267 $self->pipe->set_clip_state($self->pipe, state);
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);
275 void set_framebuffer(const struct pipe_framebuffer_state *state ) {
276 cso_set_framebuffer($self->cso, state);
279 void set_polygon_stipple(const struct pipe_poly_stipple *state ) {
280 $self->pipe->set_polygon_stipple($self->pipe, state);
283 void set_scissor(const struct pipe_scissor_state *state ) {
284 $self->pipe->set_scissor_state($self->pipe, state);
287 void set_viewport(const struct pipe_viewport_state *state) {
288 cso_set_viewport($self->cso, state);
291 void set_sampler_texture(unsigned index,
292 struct pipe_texture *texture) {
294 texture = $self->default_texture;
295 pipe_texture_reference(&$self->sampler_textures[index], texture);
296 $self->pipe->set_sampler_textures($self->pipe,
298 $self->sampler_textures);
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);
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);
317 void draw_arrays(unsigned mode, unsigned start, unsigned count) {
318 $self->pipe->draw_arrays($self->pipe, mode, start, count);
321 void draw_elements( struct pipe_buffer *indexBuffer,
323 unsigned mode, unsigned start, unsigned count) {
324 $self->pipe->draw_elements($self->pipe, indexBuffer, indexSize, mode, start, count);
327 void draw_vertices(unsigned prim,
329 unsigned num_attribs,
330 const float *vertices)
332 struct pipe_context *pipe = $self->pipe;
333 struct pipe_winsys *winsys = pipe->winsys;
334 struct pipe_buffer *vbuf;
338 size = num_verts * num_attribs * 4 * sizeof(float);
340 vbuf = winsys->buffer_create(winsys,
342 PIPE_BUFFER_USAGE_VERTEX,
347 map = winsys->buffer_map(winsys, vbuf, PIPE_BUFFER_USAGE_CPU_WRITE);
350 memcpy(map, vertices, size);
351 pipe->winsys->buffer_unmap(pipe->winsys, vbuf);
353 util_draw_vertex_buffer(pipe, vbuf, prim, num_verts, num_attribs);
356 pipe_buffer_reference(pipe->winsys, &vbuf, NULL);
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 );
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);
383 void surface_fill(struct pipe_surface *dst,
384 unsigned x, unsigned y,
385 unsigned width, unsigned height,
387 $self->pipe->surface_fill($self->pipe, dst, x, y, width, height, value);
390 void surface_clear(struct pipe_surface *surface, unsigned value = 0) {
391 $self->pipe->clear($self->pipe, surface, value);
397 %newobject pipe_texture::get_surface;
399 %extend pipe_texture {
402 struct pipe_texture *ptr = $self;
403 fprintf(stderr, "destroying texture %p\n", $self);
404 pipe_texture_reference(&ptr, NULL);
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 )
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);
421 %extend pipe_surface {
424 struct pipe_surface *ptr = $self;
425 fprintf(stderr, "destroying surface %p\n", $self);
426 pipe_surface_reference(&ptr, NULL);
429 // gets mapped to pipe_surface_map automatically
430 void * map( unsigned flags );
432 // gets mapped to pipe_surface_unmap automatically
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);
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);
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);
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);
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);
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);
468 %extend pipe_framebuffer_state {
470 pipe_framebuffer_state(void) {
471 return CALLOC_STRUCT(pipe_framebuffer_state);
474 ~pipe_framebuffer_state() {
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);
483 set_cbuf(unsigned index, struct pipe_surface *surface) {
484 pipe_surface_reference(&$self->cbufs[index], surface);
488 set_zsbuf(struct pipe_surface *surface) {
489 pipe_surface_reference(&$self->zsbuf, surface);
495 %extend pipe_shader_state {
497 pipe_shader_state(const char *text, unsigned num_tokens = 1024) {
498 struct tgsi_token *tokens;
499 struct pipe_shader_state *shader;
501 tokens = MALLOC(num_tokens * sizeof(struct tgsi_token));
505 if(tgsi_text_translate(text, tokens, num_tokens ) != TRUE)
508 shader = CALLOC_STRUCT(pipe_shader_state);
512 shader->tokens = tokens;
523 ~pipe_shader_state() {
524 FREE((void*)$self->tokens);
528 void dump(unsigned flags = 0) {
529 tgsi_dump($self->tokens, flags);