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>
35 %nodefaultctor st_context;
36 %nodefaultdtor st_context;
44 st_context_destroy($self);
48 * State functions (create/bind/destroy state objects)
51 void set_blend( const struct pipe_blend_state *state ) {
52 cso_set_blend($self->cso, state);
55 void set_fragment_sampler( unsigned index, const struct pipe_sampler_state *state ) {
56 cso_single_sampler($self->cso, index, state);
57 cso_single_sampler_done($self->cso);
60 void set_vertex_sampler( unsigned index, const struct pipe_sampler_state *state ) {
61 cso_single_vertex_sampler($self->cso, index, state);
62 cso_single_vertex_sampler_done($self->cso);
65 void set_rasterizer( const struct pipe_rasterizer_state *state ) {
66 cso_set_rasterizer($self->cso, state);
69 void set_depth_stencil_alpha(const struct pipe_depth_stencil_alpha_state *state) {
70 cso_set_depth_stencil_alpha($self->cso, state);
73 void set_fragment_shader( const struct pipe_shader_state *state ) {
77 cso_set_fragment_shader_handle($self->cso, NULL);
81 fs = $self->pipe->create_fs_state($self->pipe, state);
85 if(cso_set_fragment_shader_handle($self->cso, fs) != PIPE_OK)
88 cso_delete_fragment_shader($self->cso, $self->fs);
92 void set_vertex_shader( const struct pipe_shader_state *state ) {
96 cso_set_vertex_shader_handle($self->cso, NULL);
100 vs = $self->pipe->create_vs_state($self->pipe, state);
104 if(cso_set_vertex_shader_handle($self->cso, vs) != PIPE_OK)
107 cso_delete_vertex_shader($self->cso, $self->vs);
111 void set_geometry_shader( const struct pipe_shader_state *state ) {
115 cso_set_geometry_shader_handle($self->cso, NULL);
119 gs = $self->pipe->create_gs_state($self->pipe, state);
123 if(cso_set_geometry_shader_handle($self->cso, gs) != PIPE_OK)
126 cso_delete_geometry_shader($self->cso, $self->gs);
131 * Parameter-like state (or properties)
134 void set_blend_color(const struct pipe_blend_color *state ) {
135 cso_set_blend_color($self->cso, state);
138 void set_stencil_ref(const struct pipe_stencil_ref *state ) {
139 cso_set_stencil_ref($self->cso, state);
142 void set_clip(const struct pipe_clip_state *state ) {
143 $self->pipe->set_clip_state($self->pipe, state);
146 void set_constant_buffer(unsigned shader, unsigned index,
147 struct pipe_buffer *buffer )
149 $self->pipe->set_constant_buffer($self->pipe, shader, index, buffer);
152 void set_framebuffer(const struct pipe_framebuffer_state *state )
154 memcpy(&$self->framebuffer, state, sizeof *state);
155 cso_set_framebuffer($self->cso, state);
158 void set_polygon_stipple(const struct pipe_poly_stipple *state ) {
159 $self->pipe->set_polygon_stipple($self->pipe, state);
162 void set_scissor(const struct pipe_scissor_state *state ) {
163 $self->pipe->set_scissor_state($self->pipe, state);
166 void set_viewport(const struct pipe_viewport_state *state) {
167 cso_set_viewport($self->cso, state);
170 void set_fragment_sampler_texture(unsigned index,
171 struct pipe_texture *texture) {
172 struct pipe_sampler_view templ;
175 texture = $self->default_texture;
176 pipe_sampler_view_reference(&$self->fragment_sampler_views[index], NULL);
177 u_sampler_view_default_template(&templ,
180 $self->fragment_sampler_views[index] = $self->pipe->create_sampler_view($self->pipe,
183 $self->pipe->set_fragment_sampler_views($self->pipe,
185 $self->fragment_sampler_views);
188 void set_vertex_sampler_texture(unsigned index,
189 struct pipe_texture *texture) {
190 struct pipe_sampler_view templ;
193 texture = $self->default_texture;
194 pipe_sampler_view_reference(&$self->vertex_sampler_views[index], NULL);
195 u_sampler_view_default_template(&templ,
198 $self->vertex_sampler_views[index] = $self->pipe->create_sampler_view($self->pipe,
202 $self->pipe->set_vertex_sampler_views($self->pipe,
203 PIPE_MAX_VERTEX_SAMPLERS,
204 $self->vertex_sampler_views);
207 void set_vertex_buffer(unsigned index,
210 unsigned buffer_offset,
211 struct pipe_buffer *buffer)
214 struct pipe_vertex_buffer state;
216 memset(&state, 0, sizeof(state));
217 state.stride = stride;
218 state.max_index = max_index;
219 state.buffer_offset = buffer_offset;
220 state.buffer = buffer;
222 memcpy(&$self->vertex_buffers[index], &state, sizeof(state));
224 for(i = 0; i < PIPE_MAX_ATTRIBS; ++i)
225 if(self->vertex_buffers[i].buffer)
226 $self->num_vertex_buffers = i + 1;
228 $self->pipe->set_vertex_buffers($self->pipe,
229 $self->num_vertex_buffers,
230 $self->vertex_buffers);
233 void set_vertex_element(unsigned index,
234 const struct pipe_vertex_element *element)
236 memcpy(&$self->vertex_elements[index], element, sizeof(*element));
239 void set_vertex_elements(unsigned num)
241 $self->num_vertex_elements = num;
242 cso_set_vertex_elements($self->cso,
243 $self->num_vertex_elements,
244 $self->vertex_elements);
251 void draw_arrays(unsigned mode, unsigned start, unsigned count) {
252 $self->pipe->draw_arrays($self->pipe, mode, start, count);
255 void draw_elements( struct pipe_buffer *indexBuffer,
257 unsigned mode, unsigned start, unsigned count)
259 $self->pipe->draw_elements($self->pipe,
265 void draw_range_elements( struct pipe_buffer *indexBuffer,
266 unsigned indexSize, unsigned minIndex, unsigned maxIndex,
267 unsigned mode, unsigned start, unsigned count)
269 $self->pipe->draw_range_elements($self->pipe,
271 indexSize, minIndex, maxIndex,
275 void draw_vertices(unsigned prim,
277 unsigned num_attribs,
278 const float *vertices)
280 struct pipe_context *pipe = $self->pipe;
281 struct pipe_screen *screen = pipe->screen;
282 struct pipe_buffer *vbuf;
283 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
284 struct pipe_vertex_buffer vbuffer;
289 size = num_verts * num_attribs * 4 * sizeof(float);
291 vbuf = pipe_buffer_create(screen,
293 PIPE_BUFFER_USAGE_VERTEX,
298 map = pipe_buffer_map(screen, vbuf, PIPE_BUFFER_USAGE_CPU_WRITE);
301 memcpy(map, vertices, size);
302 pipe_buffer_unmap(screen, vbuf);
304 cso_save_vertex_elements($self->cso);
306 /* tell pipe about the vertex attributes */
307 for (i = 0; i < num_attribs; i++) {
308 velements[i].src_offset = i * 4 * sizeof(float);
309 velements[i].instance_divisor = 0;
310 velements[i].vertex_buffer_index = 0;
311 velements[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
313 cso_set_vertex_elements($self->cso, num_attribs, velements);
315 /* tell pipe about the vertex buffer */
316 memset(&vbuffer, 0, sizeof(vbuffer));
317 vbuffer.buffer = vbuf;
318 vbuffer.stride = num_attribs * 4 * sizeof(float); /* vertex size */
319 vbuffer.buffer_offset = 0;
320 vbuffer.max_index = num_verts - 1;
321 pipe->set_vertex_buffers(pipe, 1, &vbuffer);
324 pipe->draw_arrays(pipe, prim, 0, num_verts);
326 cso_restore_vertex_elements($self->cso);
329 pipe_buffer_reference(&vbuf, NULL);
335 clear(unsigned buffers, const float *rgba, double depth = 0.0f,
336 unsigned stencil = 0)
338 $self->pipe->clear($self->pipe, buffers, rgba, depth, stencil);
342 flush(unsigned flags = 0) {
343 struct pipe_fence_handle *fence = NULL;
344 $self->pipe->flush($self->pipe, flags | PIPE_FLUSH_RENDER_CACHE, &fence);
346 /* TODO: allow asynchronous operation */
347 $self->pipe->screen->fence_finish( $self->pipe->screen, fence, 0 );
348 $self->pipe->screen->fence_reference( $self->pipe->screen, &fence, NULL );
356 void surface_copy(struct st_surface *dst,
357 unsigned destx, unsigned desty,
358 struct st_surface *src,
359 unsigned srcx, unsigned srcy,
360 unsigned width, unsigned height)
362 struct pipe_surface *_dst = NULL;
363 struct pipe_surface *_src = NULL;
365 _dst = st_pipe_surface(dst, PIPE_BUFFER_USAGE_GPU_WRITE);
367 SWIG_exception(SWIG_ValueError, "couldn't acquire destination surface for writing");
369 _src = st_pipe_surface(src, PIPE_BUFFER_USAGE_GPU_READ);
371 SWIG_exception(SWIG_ValueError, "couldn't acquire source surface for reading");
373 $self->pipe->surface_copy($self->pipe, _dst, destx, desty, _src, srcx, srcy, width, height);
376 pipe_surface_reference(&_src, NULL);
377 pipe_surface_reference(&_dst, NULL);
380 void surface_fill(struct st_surface *dst,
381 unsigned x, unsigned y,
382 unsigned width, unsigned height,
385 struct pipe_surface *_dst = NULL;
387 _dst = st_pipe_surface(dst, PIPE_BUFFER_USAGE_GPU_WRITE);
389 SWIG_exception(SWIG_ValueError, "couldn't acquire destination surface for writing");
391 $self->pipe->surface_fill($self->pipe, _dst, x, y, width, height, value);
394 pipe_surface_reference(&_dst, NULL);
397 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
399 surface_read_raw(struct st_surface *surface,
400 unsigned x, unsigned y, unsigned w, unsigned h,
401 char **STRING, int *LENGTH)
403 struct pipe_texture *texture = surface->texture;
404 struct pipe_context *pipe = $self->pipe;
405 struct pipe_transfer *transfer;
408 stride = util_format_get_stride(texture->format, w);
409 *LENGTH = util_format_get_nblocksy(texture->format, h) * stride;
410 *STRING = (char *) malloc(*LENGTH);
414 transfer = pipe->get_tex_transfer(pipe,
422 pipe_get_tile_raw(pipe, transfer, 0, 0, w, h, *STRING, stride);
423 pipe->tex_transfer_destroy(pipe, transfer);
427 %cstring_input_binary(const char *STRING, unsigned LENGTH);
429 surface_write_raw(struct st_surface *surface,
430 unsigned x, unsigned y, unsigned w, unsigned h,
431 const char *STRING, unsigned LENGTH, unsigned stride = 0)
433 struct pipe_texture *texture = surface->texture;
434 struct pipe_context *pipe = $self->pipe;
435 struct pipe_transfer *transfer;
438 stride = util_format_get_stride(texture->format, w);
440 if(LENGTH < util_format_get_nblocksy(texture->format, h) * stride)
441 SWIG_exception(SWIG_ValueError, "offset must be smaller than buffer size");
443 transfer = pipe->get_tex_transfer(pipe,
451 SWIG_exception(SWIG_MemoryError, "couldn't initiate transfer");
453 pipe_put_tile_raw(pipe, transfer, 0, 0, w, h, STRING, stride);
454 pipe->tex_transfer_destroy(pipe, transfer);
461 surface_read_rgba(struct st_surface *surface,
462 unsigned x, unsigned y, unsigned w, unsigned h,
465 struct pipe_context *pipe = $self->pipe;
466 struct pipe_transfer *transfer;
467 transfer = pipe->get_tex_transfer(pipe,
475 pipe_get_tile_rgba(pipe, transfer, 0, 0, w, h, rgba);
476 pipe->tex_transfer_destroy(pipe, transfer);
481 surface_write_rgba(struct st_surface *surface,
482 unsigned x, unsigned y, unsigned w, unsigned h,
485 struct pipe_context *pipe = $self->pipe;
486 struct pipe_transfer *transfer;
487 transfer = pipe->get_tex_transfer(pipe,
495 pipe_put_tile_rgba(pipe, transfer, 0, 0, w, h, rgba);
496 pipe->tex_transfer_destroy(pipe, transfer);
500 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
502 surface_read_rgba8(struct st_surface *surface,
503 unsigned x, unsigned y, unsigned w, unsigned h,
504 char **STRING, int *LENGTH)
506 struct pipe_context *pipe = $self->pipe;
507 struct pipe_transfer *transfer;
509 unsigned char *rgba8;
519 *STRING = (char *) malloc(*LENGTH);
523 rgba = malloc(h*w*4*sizeof(float));
527 rgba8 = (unsigned char *) *STRING;
529 transfer = pipe->get_tex_transfer(pipe,
538 pipe_get_tile_rgba(pipe, transfer, 0, 0, w, h, rgba);
539 for(j = 0; j < h; ++j) {
540 for(i = 0; i < w; ++i)
541 for(k = 0; k <4; ++k)
542 rgba8[j*w*4 + i*4 + k] = float_to_ubyte(rgba[j*w*4 + i*4 + k]);
544 pipe->tex_transfer_destroy(pipe, transfer);
551 surface_read_z(struct st_surface *surface,
552 unsigned x, unsigned y, unsigned w, unsigned h,
555 struct pipe_context *pipe = $self->pipe;
556 struct pipe_transfer *transfer;
557 transfer = pipe->get_tex_transfer(pipe,
565 pipe_get_tile_z(pipe, transfer, 0, 0, w, h, z);
566 pipe->tex_transfer_destroy(pipe, transfer);
571 surface_write_z(struct st_surface *surface,
572 unsigned x, unsigned y, unsigned w, unsigned h,
575 struct pipe_context *pipe = $self->pipe;
576 struct pipe_transfer *transfer;
577 transfer = pipe->get_tex_transfer(pipe,
585 pipe_put_tile_z(pipe, transfer, 0, 0, w, h, z);
586 pipe->tex_transfer_destroy(pipe, transfer);
591 surface_sample_rgba(struct st_surface *surface,
595 st_sample_surface($self->pipe, surface, rgba, norm != 0);
599 surface_compare_rgba(struct st_surface *surface,
600 unsigned x, unsigned y, unsigned w, unsigned h,
601 const float *rgba, float tol = 0.0)
603 struct pipe_context *pipe = $self->pipe;
604 struct pipe_transfer *transfer;
610 rgba2 = MALLOC(h*w*4*sizeof(float));
614 transfer = pipe->get_tex_transfer(pipe,
626 pipe_get_tile_rgba(pipe, transfer, 0, 0, w, h, rgba2);
627 pipe->tex_transfer_destroy(pipe, transfer);
632 for(i = h*w; i; --i) {
633 unsigned differs = 0;
635 float delta = *p2++ - *p1++;
636 if (delta < -tol || delta > tol)