Merge commit 'origin/gallium-master-merge'
[mesa.git] / src / gallium / state_trackers / python / p_context.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 %nodefaultctor st_context;
36 %nodefaultdtor st_context;
37
38 struct st_context {
39 };
40
41 %extend st_context {
42
43 ~st_context() {
44 st_context_destroy($self);
45 }
46
47 /*
48 * State functions (create/bind/destroy state objects)
49 */
50
51 void set_blend( const struct pipe_blend_state *state ) {
52 cso_set_blend($self->cso, state);
53 }
54
55 void set_sampler( unsigned index, const struct pipe_sampler_state *state ) {
56 cso_single_sampler($self->cso, index, state);
57 cso_single_sampler_done($self->cso);
58 }
59
60 void set_rasterizer( const struct pipe_rasterizer_state *state ) {
61 cso_set_rasterizer($self->cso, state);
62 }
63
64 void set_depth_stencil_alpha(const struct pipe_depth_stencil_alpha_state *state) {
65 cso_set_depth_stencil_alpha($self->cso, state);
66 }
67
68 void set_fragment_shader( const struct pipe_shader_state *state ) {
69 void *fs;
70
71 if(!state) {
72 cso_set_fragment_shader_handle($self->cso, NULL);
73 return;
74 }
75
76 fs = $self->pipe->create_fs_state($self->pipe, state);
77 if(!fs)
78 return;
79
80 if(cso_set_fragment_shader_handle($self->cso, fs) != PIPE_OK)
81 return;
82
83 cso_delete_fragment_shader($self->cso, $self->fs);
84 $self->fs = fs;
85 }
86
87 void set_vertex_shader( const struct pipe_shader_state *state ) {
88 void *vs;
89
90 if(!state) {
91 cso_set_vertex_shader_handle($self->cso, NULL);
92 return;
93 }
94
95 vs = $self->pipe->create_vs_state($self->pipe, state);
96 if(!vs)
97 return;
98
99 if(cso_set_vertex_shader_handle($self->cso, vs) != PIPE_OK)
100 return;
101
102 cso_delete_vertex_shader($self->cso, $self->vs);
103 $self->vs = vs;
104 }
105
106 /*
107 * Parameter-like state (or properties)
108 */
109
110 void set_blend_color(const struct pipe_blend_color *state ) {
111 cso_set_blend_color($self->cso, state);
112 }
113
114 void set_clip(const struct pipe_clip_state *state ) {
115 $self->pipe->set_clip_state($self->pipe, state);
116 }
117
118 void set_constant_buffer(unsigned shader, unsigned index,
119 struct st_buffer *buffer )
120 {
121 struct pipe_constant_buffer state;
122 memset(&state, 0, sizeof(state));
123 state.buffer = buffer ? buffer->buffer : NULL;
124 state.size = buffer->buffer->size;
125 $self->pipe->set_constant_buffer($self->pipe, shader, index, &state);
126 }
127
128 void set_framebuffer(const struct pipe_framebuffer_state *state ) {
129 cso_set_framebuffer($self->cso, state);
130 }
131
132 void set_polygon_stipple(const struct pipe_poly_stipple *state ) {
133 $self->pipe->set_polygon_stipple($self->pipe, state);
134 }
135
136 void set_scissor(const struct pipe_scissor_state *state ) {
137 $self->pipe->set_scissor_state($self->pipe, state);
138 }
139
140 void set_viewport(const struct pipe_viewport_state *state) {
141 cso_set_viewport($self->cso, state);
142 }
143
144 void set_sampler_texture(unsigned index,
145 struct pipe_texture *texture) {
146 if(!texture)
147 texture = $self->default_texture;
148 pipe_texture_reference(&$self->sampler_textures[index], texture);
149 $self->pipe->set_sampler_textures($self->pipe,
150 PIPE_MAX_SAMPLERS,
151 $self->sampler_textures);
152 }
153
154 void set_vertex_buffer(unsigned index,
155 unsigned pitch,
156 unsigned max_index,
157 unsigned buffer_offset,
158 struct st_buffer *buffer)
159 {
160 unsigned i;
161 struct pipe_vertex_buffer state;
162
163 memset(&state, 0, sizeof(state));
164 state.pitch = pitch;
165 state.max_index = max_index;
166 state.buffer_offset = buffer_offset;
167 state.buffer = buffer ? buffer->buffer : NULL;
168
169 memcpy(&$self->vertex_buffers[index], &state, sizeof(state));
170
171 for(i = 0; i < PIPE_MAX_ATTRIBS; ++i)
172 if(self->vertex_buffers[i].buffer)
173 $self->num_vertex_buffers = i + 1;
174
175 $self->pipe->set_vertex_buffers($self->pipe,
176 $self->num_vertex_buffers,
177 $self->vertex_buffers);
178 }
179
180 void set_vertex_element(unsigned index,
181 const struct pipe_vertex_element *element)
182 {
183 memcpy(&$self->vertex_elements[index], element, sizeof(*element));
184 }
185
186 void set_vertex_elements(unsigned num)
187 {
188 $self->num_vertex_elements = num;
189 $self->pipe->set_vertex_elements($self->pipe,
190 $self->num_vertex_elements,
191 $self->vertex_elements);
192 }
193
194 /*
195 * Draw functions
196 */
197
198 void draw_arrays(unsigned mode, unsigned start, unsigned count) {
199 $self->pipe->draw_arrays($self->pipe, mode, start, count);
200 }
201
202 void draw_elements( struct st_buffer *indexBuffer,
203 unsigned indexSize,
204 unsigned mode, unsigned start, unsigned count)
205 {
206 $self->pipe->draw_elements($self->pipe,
207 indexBuffer->buffer,
208 indexSize,
209 mode, start, count);
210 }
211
212 void draw_range_elements( struct st_buffer *indexBuffer,
213 unsigned indexSize, unsigned minIndex, unsigned maxIndex,
214 unsigned mode, unsigned start, unsigned count)
215 {
216 $self->pipe->draw_range_elements($self->pipe,
217 indexBuffer->buffer,
218 indexSize, minIndex, maxIndex,
219 mode, start, count);
220 }
221
222 void draw_vertices(unsigned prim,
223 unsigned num_verts,
224 unsigned num_attribs,
225 const float *vertices)
226 {
227 struct pipe_context *pipe = $self->pipe;
228 struct pipe_screen *screen = pipe->screen;
229 struct pipe_buffer *vbuf;
230 float *map;
231 unsigned size;
232
233 size = num_verts * num_attribs * 4 * sizeof(float);
234
235 vbuf = pipe_buffer_create(screen,
236 32,
237 PIPE_BUFFER_USAGE_VERTEX,
238 size);
239 if(!vbuf)
240 goto error1;
241
242 map = pipe_buffer_map(screen, vbuf, PIPE_BUFFER_USAGE_CPU_WRITE);
243 if (!map)
244 goto error2;
245 memcpy(map, vertices, size);
246 pipe_buffer_unmap(screen, vbuf);
247
248 util_draw_vertex_buffer(pipe, vbuf, 0, prim, num_verts, num_attribs);
249
250 error2:
251 pipe_buffer_reference(screen, &vbuf, NULL);
252 error1:
253 ;
254 }
255
256 void
257 flush(unsigned flags = 0) {
258 struct pipe_fence_handle *fence = NULL;
259 $self->pipe->flush($self->pipe, flags | PIPE_FLUSH_RENDER_CACHE, &fence);
260 /* TODO: allow asynchronous operation */
261 $self->pipe->winsys->fence_finish( $self->pipe->winsys, fence, 0 );
262 $self->pipe->winsys->fence_reference( $self->pipe->winsys, &fence, NULL );
263 }
264
265 /*
266 * Surface functions
267 */
268
269 void surface_copy(int do_flip,
270 struct pipe_surface *dest,
271 unsigned destx, unsigned desty,
272 struct pipe_surface *src,
273 unsigned srcx, unsigned srcy,
274 unsigned width, unsigned height) {
275 $self->pipe->surface_copy($self->pipe, do_flip, dest, destx, desty, src, srcx, srcy, width, height);
276 }
277
278 void surface_fill(struct pipe_surface *dst,
279 unsigned x, unsigned y,
280 unsigned width, unsigned height,
281 unsigned value) {
282 $self->pipe->surface_fill($self->pipe, dst, x, y, width, height, value);
283 }
284
285 void surface_clear(struct pipe_surface *surface, unsigned value = 0) {
286 $self->pipe->clear($self->pipe, surface, value);
287 }
288
289 };