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,
272 unsigned buffer_offset,
273 struct pipe_resource *buffer)
276 struct pipe_vertex_buffer state;
278 memset(&state, 0, sizeof(state));
279 state.stride = stride;
280 state.max_index = max_index;
281 state.buffer_offset = buffer_offset;
282 state.buffer = buffer;
284 memcpy(&$self->vertex_buffers[index], &state, sizeof(state));
286 for(i = 0; i < PIPE_MAX_ATTRIBS; ++i)
287 if(self->vertex_buffers[i].buffer)
288 $self->num_vertex_buffers = i + 1;
290 $self->pipe->set_vertex_buffers($self->pipe,
291 $self->num_vertex_buffers,
292 $self->vertex_buffers);
295 void set_index_buffer(unsigned index_size,
297 struct pipe_resource *buffer)
299 struct pipe_index_buffer ib;
301 memset(&ib, 0, sizeof(ib));
302 ib.index_size = index_size;
306 $self->pipe->set_index_buffer($self->pipe, &ib);
309 void set_vertex_element(unsigned index,
310 const struct pipe_vertex_element *element)
312 memcpy(&$self->vertex_elements[index], element, sizeof(*element));
315 void set_vertex_elements(unsigned num)
317 $self->num_vertex_elements = num;
318 cso_set_vertex_elements($self->cso,
319 $self->num_vertex_elements,
320 $self->vertex_elements);
327 void draw_arrays(unsigned mode, unsigned start, unsigned count) {
328 util_draw_arrays($self->pipe, mode, start, count);
331 void draw_vbo(const struct pipe_draw_info *info)
333 $self->pipe->draw_vbo($self->pipe, info);
336 void draw_vertices(unsigned prim,
338 unsigned num_attribs,
339 const float *vertices)
341 struct pipe_context *pipe = $self->pipe;
342 struct pipe_screen *screen = pipe->screen;
343 struct pipe_resource *vbuf;
344 struct pipe_transfer *transfer;
345 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
346 struct pipe_vertex_buffer vbuffer;
351 size = num_verts * num_attribs * 4 * sizeof(float);
353 vbuf = pipe_buffer_create(screen,
354 PIPE_BIND_VERTEX_BUFFER,
359 map = pipe_buffer_map(pipe, vbuf, PIPE_TRANSFER_WRITE, &transfer);
362 memcpy(map, vertices, size);
363 pipe_buffer_unmap(pipe, transfer);
365 cso_save_vertex_elements($self->cso);
367 /* tell pipe about the vertex attributes */
368 for (i = 0; i < num_attribs; i++) {
369 velements[i].src_offset = i * 4 * sizeof(float);
370 velements[i].instance_divisor = 0;
371 velements[i].vertex_buffer_index = 0;
372 velements[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
374 cso_set_vertex_elements($self->cso, num_attribs, velements);
376 /* tell pipe about the vertex buffer */
377 memset(&vbuffer, 0, sizeof(vbuffer));
378 vbuffer.buffer = vbuf;
379 vbuffer.stride = num_attribs * 4 * sizeof(float); /* vertex size */
380 vbuffer.buffer_offset = 0;
381 vbuffer.max_index = num_verts - 1;
382 pipe->set_vertex_buffers(pipe, 1, &vbuffer);
385 util_draw_arrays(pipe, prim, 0, num_verts);
387 cso_restore_vertex_elements($self->cso);
390 pipe_resource_reference(&vbuf, NULL);
396 clear(unsigned buffers, const float *rgba, double depth = 0.0f,
397 unsigned stencil = 0)
399 $self->pipe->clear($self->pipe, buffers, rgba, depth, stencil);
403 flush(unsigned flags = 0) {
404 struct pipe_fence_handle *fence = NULL;
405 $self->pipe->flush($self->pipe, flags | PIPE_FLUSH_RENDER_CACHE, &fence);
407 /* TODO: allow asynchronous operation */
408 $self->pipe->screen->fence_finish( $self->pipe->screen, fence, 0 );
409 $self->pipe->screen->fence_reference( $self->pipe->screen, &fence, NULL );
417 void resource_copy_region(struct pipe_resource *dst,
419 unsigned dstx, unsigned dsty, unsigned dstz,
420 struct pipe_resource *src,
422 const struct pipe_box *src_box)
424 $self->pipe->resource_copy_region($self->pipe,
425 dst, dst_level, dstx, dsty, dstz,
426 src, src_level, src_box);
430 void clear_render_target(struct st_surface *dst,
432 unsigned x, unsigned y,
433 unsigned width, unsigned height)
435 struct pipe_surface *_dst = NULL;
437 _dst = st_pipe_surface($self->pipe, dst, PIPE_BIND_RENDER_TARGET);
439 SWIG_exception(SWIG_ValueError, "couldn't acquire destination surface for writing");
441 $self->pipe->clear_render_target($self->pipe, _dst, rgba, x, y, width, height);
444 pipe_surface_reference(&_dst, NULL);
447 void clear_depth_stencil(struct st_surface *dst,
448 unsigned clear_flags,
451 unsigned x, unsigned y,
452 unsigned width, unsigned height)
454 struct pipe_surface *_dst = NULL;
456 _dst = st_pipe_surface($self->pipe, dst, PIPE_BIND_DEPTH_STENCIL);
458 SWIG_exception(SWIG_ValueError, "couldn't acquire destination surface for writing");
460 $self->pipe->clear_depth_stencil($self->pipe, _dst, clear_flags, depth, stencil,
461 x, y, width, height);
464 pipe_surface_reference(&_dst, NULL);
467 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
469 surface_read_raw(struct st_surface *surface,
470 unsigned x, unsigned y, unsigned w, unsigned h,
471 char **STRING, int *LENGTH)
473 struct pipe_resource *texture = surface->texture;
474 struct pipe_context *pipe = $self->pipe;
475 struct pipe_transfer *transfer;
478 stride = util_format_get_stride(texture->format, w);
479 *LENGTH = util_format_get_nblocksy(texture->format, h) * stride;
480 *STRING = (char *) malloc(*LENGTH);
484 transfer = pipe_get_transfer(pipe,
491 pipe_get_tile_raw(pipe, transfer, 0, 0, w, h, *STRING, stride);
492 pipe->transfer_destroy(pipe, transfer);
496 %cstring_input_binary(const char *STRING, unsigned LENGTH);
498 surface_write_raw(struct st_surface *surface,
499 unsigned x, unsigned y, unsigned w, unsigned h,
500 const char *STRING, unsigned LENGTH, unsigned stride = 0)
502 struct pipe_resource *texture = surface->texture;
503 struct pipe_context *pipe = $self->pipe;
504 struct pipe_transfer *transfer;
507 stride = util_format_get_stride(texture->format, w);
509 if(LENGTH < util_format_get_nblocksy(texture->format, h) * stride)
510 SWIG_exception(SWIG_ValueError, "offset must be smaller than buffer size");
512 transfer = pipe_get_transfer(pipe,
519 SWIG_exception(SWIG_MemoryError, "couldn't initiate transfer");
521 pipe_put_tile_raw(pipe, transfer, 0, 0, w, h, STRING, stride);
522 pipe->transfer_destroy(pipe, transfer);
529 surface_read_rgba(struct st_surface *surface,
530 unsigned x, unsigned y, unsigned w, unsigned h,
533 struct pipe_context *pipe = $self->pipe;
534 struct pipe_transfer *transfer;
535 transfer = pipe_get_transfer(pipe,
542 pipe_get_tile_rgba(pipe, transfer, 0, 0, w, h, rgba);
543 pipe->transfer_destroy(pipe, transfer);
548 surface_write_rgba(struct st_surface *surface,
549 unsigned x, unsigned y, unsigned w, unsigned h,
552 struct pipe_context *pipe = $self->pipe;
553 struct pipe_transfer *transfer;
554 transfer = pipe_get_transfer(pipe,
561 pipe_put_tile_rgba(pipe, transfer, 0, 0, w, h, rgba);
562 pipe->transfer_destroy(pipe, transfer);
566 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
568 surface_read_rgba8(struct st_surface *surface,
569 unsigned x, unsigned y, unsigned w, unsigned h,
570 char **STRING, int *LENGTH)
572 struct pipe_context *pipe = $self->pipe;
573 struct pipe_transfer *transfer;
575 unsigned char *rgba8;
585 *STRING = (char *) malloc(*LENGTH);
589 rgba = malloc(h*w*4*sizeof(float));
593 rgba8 = (unsigned char *) *STRING;
595 transfer = pipe_get_transfer(pipe,
602 pipe_get_tile_rgba(pipe, transfer, 0, 0, w, h, rgba);
603 for(j = 0; j < h; ++j) {
604 for(i = 0; i < w; ++i)
605 for(k = 0; k <4; ++k)
606 rgba8[j*w*4 + i*4 + k] = float_to_ubyte(rgba[j*w*4 + i*4 + k]);
608 pipe->transfer_destroy(pipe, transfer);
615 surface_read_z(struct st_surface *surface,
616 unsigned x, unsigned y, unsigned w, unsigned h,
619 struct pipe_context *pipe = $self->pipe;
620 struct pipe_transfer *transfer;
621 transfer = pipe_get_transfer(pipe,
628 pipe_get_tile_z(pipe, transfer, 0, 0, w, h, z);
629 pipe->transfer_destroy(pipe, transfer);
634 surface_write_z(struct st_surface *surface,
635 unsigned x, unsigned y, unsigned w, unsigned h,
638 struct pipe_context *pipe = $self->pipe;
639 struct pipe_transfer *transfer;
640 transfer = pipe_get_transfer(pipe,
647 pipe_put_tile_z(pipe, transfer, 0, 0, w, h, z);
648 pipe->transfer_destroy(pipe, transfer);
653 surface_sample_rgba(struct st_surface *surface,
657 st_sample_surface($self->pipe, surface, rgba, norm != 0);
661 surface_compare_rgba(struct st_surface *surface,
662 unsigned x, unsigned y, unsigned w, unsigned h,
663 const float *rgba, float tol = 0.0)
665 struct pipe_context *pipe = $self->pipe;
666 struct pipe_transfer *transfer;
672 rgba2 = MALLOC(h*w*4*sizeof(float));
676 transfer = pipe_get_transfer(pipe,
687 pipe_get_tile_rgba(pipe, transfer, 0, 0, w, h, rgba2);
688 pipe->transfer_destroy(pipe, transfer);
693 for(i = h*w; i; --i) {
694 unsigned differs = 0;
696 float delta = *p2++ - *p1++;
697 if (delta < -tol || delta > tol)
708 %cstring_input_binary(const char *STRING, unsigned LENGTH);
710 transfer_inline_write(struct pipe_resource *resource,
713 const struct pipe_box *box,
714 const char *STRING, unsigned LENGTH,
716 unsigned layer_stride)
718 struct pipe_context *pipe = $self->pipe;
720 pipe->transfer_inline_write(pipe, resource, level, usage, box, STRING, stride, layer_stride);
723 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
724 void buffer_read(struct pipe_resource *buffer,
725 char **STRING, int *LENGTH)
727 struct pipe_context *pipe = $self->pipe;
729 assert(buffer->target == PIPE_BUFFER);
731 *LENGTH = buffer->width0;
732 *STRING = (char *) malloc(buffer->width0);
736 pipe_buffer_read(pipe, buffer, 0, buffer->width0, *STRING);
739 void buffer_write(struct pipe_resource *buffer,
740 const char *STRING, unsigned LENGTH, unsigned offset = 0)
742 struct pipe_context *pipe = $self->pipe;
744 assert(buffer->target == PIPE_BUFFER);
746 if(offset > buffer->width0)
747 SWIG_exception(SWIG_ValueError, "offset must be smaller than buffer size");
749 if(offset + LENGTH > buffer->width0)
750 SWIG_exception(SWIG_ValueError, "data length must fit inside the buffer");
752 pipe_buffer_write(pipe, buffer, offset, LENGTH, STRING);