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);
130 struct pipe_sampler_view *
131 create_sampler_view(struct pipe_resource *texture,
132 enum pipe_format format = PIPE_FORMAT_NONE,
133 unsigned first_level = 0,
134 unsigned last_level = ~0,
135 unsigned first_layer = 0,
136 unsigned last_layer = ~0,
137 unsigned swizzle_r = 0,
138 unsigned swizzle_g = 1,
139 unsigned swizzle_b = 2,
140 unsigned swizzle_a = 3)
142 struct pipe_context *pipe = $self->pipe;
143 struct pipe_sampler_view templat;
145 memset(&templat, 0, sizeof templat);
146 if (format == PIPE_FORMAT_NONE) {
147 templat.format = texture->format;
149 templat.format = format;
151 templat.u.tex.last_level = MIN2(last_level, texture->last_level);
152 templat.u.tex.first_level = first_level;
153 templat.u.tex.first_layer = first_layer;
154 templat.u.tex.last_layer = last_layer;
155 templat.swizzle_r = swizzle_r;
156 templat.swizzle_g = swizzle_g;
157 templat.swizzle_b = swizzle_b;
158 templat.swizzle_a = swizzle_a;
160 return pipe->create_sampler_view(pipe, texture, &templat);
164 sampler_view_destroy(struct pipe_context *ctx,
165 struct pipe_sampler_view *view)
167 struct pipe_context *pipe = $self->pipe;
169 pipe->sampler_view_destroy(pipe, view);
173 * Parameter-like state (or properties)
176 void set_blend_color(const struct pipe_blend_color *state ) {
177 cso_set_blend_color($self->cso, state);
180 void set_stencil_ref(const struct pipe_stencil_ref *state ) {
181 cso_set_stencil_ref($self->cso, state);
184 void set_clip(const struct pipe_clip_state *state ) {
185 $self->pipe->set_clip_state($self->pipe, state);
188 void set_constant_buffer(unsigned shader, unsigned index,
189 struct pipe_resource *buffer )
191 $self->pipe->set_constant_buffer($self->pipe, shader, index, buffer);
194 void set_framebuffer(const struct pipe_framebuffer_state *state )
196 memcpy(&$self->framebuffer, state, sizeof *state);
197 cso_set_framebuffer($self->cso, state);
200 void set_polygon_stipple(const struct pipe_poly_stipple *state ) {
201 $self->pipe->set_polygon_stipple($self->pipe, state);
204 void set_scissor(const struct pipe_scissor_state *state ) {
205 $self->pipe->set_scissor_state($self->pipe, state);
208 void set_viewport(const struct pipe_viewport_state *state) {
209 cso_set_viewport($self->cso, state);
212 void set_fragment_sampler_view(unsigned index,
213 struct pipe_sampler_view *view)
215 pipe_sampler_view_reference(&$self->fragment_sampler_views[index], view);
217 $self->pipe->set_fragment_sampler_views($self->pipe,
219 $self->fragment_sampler_views);
222 void set_vertex_sampler_view(unsigned index,
223 struct pipe_sampler_view *view)
225 pipe_sampler_view_reference(&$self->vertex_sampler_views[index], view);
227 $self->pipe->set_vertex_sampler_views($self->pipe,
228 PIPE_MAX_VERTEX_SAMPLERS,
229 $self->vertex_sampler_views);
232 void set_fragment_sampler_texture(unsigned index,
233 struct pipe_resource *texture) {
234 struct pipe_sampler_view templ;
237 texture = $self->default_texture;
238 pipe_sampler_view_reference(&$self->fragment_sampler_views[index], NULL);
239 u_sampler_view_default_template(&templ,
242 $self->fragment_sampler_views[index] = $self->pipe->create_sampler_view($self->pipe,
245 $self->pipe->set_fragment_sampler_views($self->pipe,
247 $self->fragment_sampler_views);
250 void set_vertex_sampler_texture(unsigned index,
251 struct pipe_resource *texture) {
252 struct pipe_sampler_view templ;
255 texture = $self->default_texture;
256 pipe_sampler_view_reference(&$self->vertex_sampler_views[index], NULL);
257 u_sampler_view_default_template(&templ,
260 $self->vertex_sampler_views[index] = $self->pipe->create_sampler_view($self->pipe,
264 $self->pipe->set_vertex_sampler_views($self->pipe,
265 PIPE_MAX_VERTEX_SAMPLERS,
266 $self->vertex_sampler_views);
269 void set_vertex_buffer(unsigned index,
271 unsigned buffer_offset,
272 struct pipe_resource *buffer)
275 struct pipe_vertex_buffer state;
277 memset(&state, 0, sizeof(state));
278 state.stride = stride;
279 state.buffer_offset = buffer_offset;
280 state.buffer = buffer;
282 memcpy(&$self->vertex_buffers[index], &state, sizeof(state));
284 for(i = 0; i < PIPE_MAX_ATTRIBS; ++i)
285 if(self->vertex_buffers[i].buffer)
286 $self->num_vertex_buffers = i + 1;
288 $self->pipe->set_vertex_buffers($self->pipe,
289 $self->num_vertex_buffers,
290 $self->vertex_buffers);
293 void set_index_buffer(unsigned index_size,
295 struct pipe_resource *buffer)
297 struct pipe_index_buffer ib;
299 memset(&ib, 0, sizeof(ib));
300 ib.index_size = index_size;
304 $self->pipe->set_index_buffer($self->pipe, &ib);
307 void set_vertex_element(unsigned index,
308 const struct pipe_vertex_element *element)
310 memcpy(&$self->vertex_elements[index], element, sizeof(*element));
313 void set_vertex_elements(unsigned num)
315 $self->num_vertex_elements = num;
316 cso_set_vertex_elements($self->cso,
317 $self->num_vertex_elements,
318 $self->vertex_elements);
325 void draw_arrays(unsigned mode, unsigned start, unsigned count) {
326 util_draw_arrays($self->pipe, mode, start, count);
329 void draw_vbo(const struct pipe_draw_info *info)
331 $self->pipe->draw_vbo($self->pipe, info);
334 void draw_vertices(unsigned prim,
336 unsigned num_attribs,
337 const float *vertices)
339 struct pipe_context *pipe = $self->pipe;
340 struct pipe_screen *screen = pipe->screen;
341 struct pipe_resource *vbuf;
342 struct pipe_transfer *transfer;
343 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
344 struct pipe_vertex_buffer vbuffer;
349 size = num_verts * num_attribs * 4 * sizeof(float);
351 vbuf = pipe_buffer_create(screen,
352 PIPE_BIND_VERTEX_BUFFER,
358 map = pipe_buffer_map(pipe, vbuf, PIPE_TRANSFER_WRITE, &transfer);
361 memcpy(map, vertices, size);
362 pipe_buffer_unmap(pipe, transfer);
364 cso_save_vertex_elements($self->cso);
366 /* tell pipe about the vertex attributes */
367 for (i = 0; i < num_attribs; i++) {
368 velements[i].src_offset = i * 4 * sizeof(float);
369 velements[i].instance_divisor = 0;
370 velements[i].vertex_buffer_index = 0;
371 velements[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
373 cso_set_vertex_elements($self->cso, num_attribs, velements);
375 /* tell pipe about the vertex buffer */
376 memset(&vbuffer, 0, sizeof(vbuffer));
377 vbuffer.buffer = vbuf;
378 vbuffer.stride = num_attribs * 4 * sizeof(float); /* vertex size */
379 vbuffer.buffer_offset = 0;
380 pipe->set_vertex_buffers(pipe, 1, &vbuffer);
383 util_draw_arrays(pipe, prim, 0, num_verts);
385 cso_restore_vertex_elements($self->cso);
388 pipe_resource_reference(&vbuf, NULL);
394 clear(unsigned buffers, const float *rgba, double depth = 0.0f,
395 unsigned stencil = 0)
397 $self->pipe->clear($self->pipe, buffers, rgba, depth, stencil);
401 flush(unsigned flags = 0) {
402 struct pipe_fence_handle *fence = NULL;
403 $self->pipe->flush($self->pipe, &fence);
405 /* TODO: allow asynchronous operation */
406 $self->pipe->screen->fence_finish( $self->pipe->screen, fence, PIPE_TIMEOUT_INFINITE );
407 $self->pipe->screen->fence_reference( $self->pipe->screen, &fence, NULL );
415 void resource_copy_region(struct pipe_resource *dst,
417 unsigned dstx, unsigned dsty, unsigned dstz,
418 struct pipe_resource *src,
420 const struct pipe_box *src_box)
422 $self->pipe->resource_copy_region($self->pipe,
423 dst, dst_level, dstx, dsty, dstz,
424 src, src_level, src_box);
428 void clear_render_target(struct st_surface *dst,
430 unsigned x, unsigned y,
431 unsigned width, unsigned height)
433 struct pipe_surface *_dst = NULL;
435 _dst = st_pipe_surface($self->pipe, dst, PIPE_BIND_RENDER_TARGET);
437 SWIG_exception(SWIG_ValueError, "couldn't acquire destination surface for writing");
439 $self->pipe->clear_render_target($self->pipe, _dst, rgba, x, y, width, height);
442 pipe_surface_reference(&_dst, NULL);
445 void clear_depth_stencil(struct st_surface *dst,
446 unsigned clear_flags,
449 unsigned x, unsigned y,
450 unsigned width, unsigned height)
452 struct pipe_surface *_dst = NULL;
454 _dst = st_pipe_surface($self->pipe, dst, PIPE_BIND_DEPTH_STENCIL);
456 SWIG_exception(SWIG_ValueError, "couldn't acquire destination surface for writing");
458 $self->pipe->clear_depth_stencil($self->pipe, _dst, clear_flags, depth, stencil,
459 x, y, width, height);
462 pipe_surface_reference(&_dst, NULL);
465 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
467 surface_read_raw(struct st_surface *surface,
468 unsigned x, unsigned y, unsigned w, unsigned h,
469 char **STRING, int *LENGTH)
471 struct pipe_resource *texture = surface->texture;
472 struct pipe_context *pipe = $self->pipe;
473 struct pipe_transfer *transfer;
476 stride = util_format_get_stride(texture->format, w);
477 *LENGTH = util_format_get_nblocksy(texture->format, h) * stride;
478 *STRING = (char *) malloc(*LENGTH);
482 transfer = pipe_get_transfer(pipe,
489 pipe_get_tile_raw(pipe, transfer, 0, 0, w, h, *STRING, stride);
490 pipe->transfer_destroy(pipe, transfer);
494 %cstring_input_binary(const char *STRING, unsigned LENGTH);
496 surface_write_raw(struct st_surface *surface,
497 unsigned x, unsigned y, unsigned w, unsigned h,
498 const char *STRING, unsigned LENGTH, unsigned stride = 0)
500 struct pipe_resource *texture = surface->texture;
501 struct pipe_context *pipe = $self->pipe;
502 struct pipe_transfer *transfer;
505 stride = util_format_get_stride(texture->format, w);
507 if(LENGTH < util_format_get_nblocksy(texture->format, h) * stride)
508 SWIG_exception(SWIG_ValueError, "offset must be smaller than buffer size");
510 transfer = pipe_get_transfer(pipe,
517 SWIG_exception(SWIG_MemoryError, "couldn't initiate transfer");
519 pipe_put_tile_raw(pipe, transfer, 0, 0, w, h, STRING, stride);
520 pipe->transfer_destroy(pipe, transfer);
527 surface_read_rgba(struct st_surface *surface,
528 unsigned x, unsigned y, unsigned w, unsigned h,
531 struct pipe_context *pipe = $self->pipe;
532 struct pipe_transfer *transfer;
533 transfer = pipe_get_transfer(pipe,
540 pipe_get_tile_rgba(pipe, transfer, 0, 0, w, h, rgba);
541 pipe->transfer_destroy(pipe, transfer);
546 surface_write_rgba(struct st_surface *surface,
547 unsigned x, unsigned y, unsigned w, unsigned h,
550 struct pipe_context *pipe = $self->pipe;
551 struct pipe_transfer *transfer;
552 transfer = pipe_get_transfer(pipe,
559 pipe_put_tile_rgba(pipe, transfer, 0, 0, w, h, rgba);
560 pipe->transfer_destroy(pipe, transfer);
564 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
566 surface_read_rgba8(struct st_surface *surface,
567 unsigned x, unsigned y, unsigned w, unsigned h,
568 char **STRING, int *LENGTH)
570 struct pipe_context *pipe = $self->pipe;
571 struct pipe_transfer *transfer;
573 unsigned char *rgba8;
583 *STRING = (char *) malloc(*LENGTH);
587 rgba = malloc(h*w*4*sizeof(float));
591 rgba8 = (unsigned char *) *STRING;
593 transfer = pipe_get_transfer(pipe,
600 pipe_get_tile_rgba(pipe, transfer, 0, 0, w, h, rgba);
601 for(j = 0; j < h; ++j) {
602 for(i = 0; i < w; ++i)
603 for(k = 0; k <4; ++k)
604 rgba8[j*w*4 + i*4 + k] = float_to_ubyte(rgba[j*w*4 + i*4 + k]);
606 pipe->transfer_destroy(pipe, transfer);
613 surface_read_z(struct st_surface *surface,
614 unsigned x, unsigned y, unsigned w, unsigned h,
617 struct pipe_context *pipe = $self->pipe;
618 struct pipe_transfer *transfer;
619 transfer = pipe_get_transfer(pipe,
626 pipe_get_tile_z(pipe, transfer, 0, 0, w, h, z);
627 pipe->transfer_destroy(pipe, transfer);
632 surface_write_z(struct st_surface *surface,
633 unsigned x, unsigned y, unsigned w, unsigned h,
636 struct pipe_context *pipe = $self->pipe;
637 struct pipe_transfer *transfer;
638 transfer = pipe_get_transfer(pipe,
645 pipe_put_tile_z(pipe, transfer, 0, 0, w, h, z);
646 pipe->transfer_destroy(pipe, transfer);
651 surface_sample_rgba(struct st_surface *surface,
655 st_sample_surface($self->pipe, surface, rgba, norm != 0);
659 surface_compare_rgba(struct st_surface *surface,
660 unsigned x, unsigned y, unsigned w, unsigned h,
661 const float *rgba, float tol = 0.0)
663 struct pipe_context *pipe = $self->pipe;
664 struct pipe_transfer *transfer;
670 rgba2 = MALLOC(h*w*4*sizeof(float));
674 transfer = pipe_get_transfer(pipe,
685 pipe_get_tile_rgba(pipe, transfer, 0, 0, w, h, rgba2);
686 pipe->transfer_destroy(pipe, transfer);
691 for(i = h*w; i; --i) {
692 unsigned differs = 0;
694 float delta = *p2++ - *p1++;
695 if (delta < -tol || delta > tol)
706 %cstring_input_binary(const char *STRING, unsigned LENGTH);
708 transfer_inline_write(struct pipe_resource *resource,
711 const struct pipe_box *box,
712 const char *STRING, unsigned LENGTH,
714 unsigned layer_stride)
716 struct pipe_context *pipe = $self->pipe;
718 pipe->transfer_inline_write(pipe, resource, level, usage, box, STRING, stride, layer_stride);
721 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
722 void buffer_read(struct pipe_resource *buffer,
723 char **STRING, int *LENGTH)
725 struct pipe_context *pipe = $self->pipe;
727 assert(buffer->target == PIPE_BUFFER);
729 *LENGTH = buffer->width0;
730 *STRING = (char *) malloc(buffer->width0);
734 pipe_buffer_read(pipe, buffer, 0, buffer->width0, *STRING);
737 void buffer_write(struct pipe_resource *buffer,
738 const char *STRING, unsigned LENGTH, unsigned offset = 0)
740 struct pipe_context *pipe = $self->pipe;
742 assert(buffer->target == PIPE_BUFFER);
744 if(offset > buffer->width0)
745 SWIG_exception(SWIG_ValueError, "offset must be smaller than buffer size");
747 if(offset + LENGTH > buffer->width0)
748 SWIG_exception(SWIG_ValueError, "data length must fit inside the buffer");
750 pipe_buffer_write(pipe, buffer, offset, LENGTH, STRING);