python: Cope with null surfaces.
[mesa.git] / src / gallium / state_trackers / python / p_state.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 %ignore winsys;
38 %ignore pipe_vertex_buffer::buffer;
39
40 %include "pipe/p_compiler.h";
41 %include "pipe/p_state.h";
42
43
44 %array_class(struct pipe_stencil_state, StencilArray);
45
46
47 %extend pipe_framebuffer_state {
48
49 pipe_framebuffer_state(void) {
50 return CALLOC_STRUCT(pipe_framebuffer_state);
51 }
52
53 ~pipe_framebuffer_state() {
54 unsigned index;
55 for(index = 0; index < PIPE_MAX_COLOR_BUFS; ++index)
56 pipe_surface_reference(&$self->cbufs[index], NULL);
57 pipe_surface_reference(&$self->zsbuf, NULL);
58 FREE($self);
59 }
60
61 void
62 set_cbuf(unsigned index, struct st_surface *surface)
63 {
64 struct pipe_surface *_surface = NULL;
65
66 if(index >= PIPE_MAX_COLOR_BUFS)
67 SWIG_exception(SWIG_ValueError, "index out of bounds");
68
69 if(surface) {
70 _surface = st_pipe_surface(surface, PIPE_BUFFER_USAGE_GPU_WRITE);
71 if(!_surface)
72 SWIG_exception(SWIG_ValueError, "couldn't acquire surface for writing");
73 }
74
75 pipe_surface_reference(&$self->cbufs[index], _surface);
76
77 fail:
78 return;
79 }
80
81 void
82 set_zsbuf(struct st_surface *surface)
83 {
84 struct pipe_surface *_surface = NULL;
85
86 if(surface) {
87 _surface = st_pipe_surface(surface, PIPE_BUFFER_USAGE_GPU_WRITE);
88 if(!_surface)
89 SWIG_exception(SWIG_ValueError, "couldn't acquire surface for writing");
90 }
91
92 pipe_surface_reference(&$self->zsbuf, _surface);
93
94 fail:
95 return;
96 }
97
98 };
99
100
101 %extend pipe_shader_state {
102
103 pipe_shader_state(const char *text, unsigned num_tokens = 1024) {
104 struct tgsi_token *tokens;
105 struct pipe_shader_state *shader;
106
107 tokens = MALLOC(num_tokens * sizeof(struct tgsi_token));
108 if(!tokens)
109 goto error1;
110
111 if(tgsi_text_translate(text, tokens, num_tokens ) != TRUE)
112 goto error2;
113
114 shader = CALLOC_STRUCT(pipe_shader_state);
115 if(!shader)
116 goto error3;
117
118 shader->tokens = tokens;
119
120 return shader;
121
122 error3:
123 error2:
124 FREE(tokens);
125 error1:
126 return NULL;
127 }
128
129 ~pipe_shader_state() {
130 FREE((void*)$self->tokens);
131 FREE($self);
132 }
133
134 void dump(unsigned flags = 0) {
135 tgsi_dump($self->tokens, flags);
136 }
137 }