glsl2: Add and use new variable mode ir_var_temporary
[mesa/nouveau-pmpeg.git] / src / gallium / state_trackers / python / p_context.i
blobcf0144b5dc60ac99b4c1359fbc6c2d3f8164adac
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:
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.
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 **************************************************************************/
28 /**
29 * @file
30 * SWIG interface definion for Gallium types.
32 * @author Jose Fonseca <jrfonseca@tungstengraphics.com>
35 %nodefaultctor st_context;
36 %nodefaultdtor st_context;
38 struct st_context {
41 %extend st_context {
43 ~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 ) {
74 void *fs;
76 if(!state) {
77 cso_set_fragment_shader_handle($self->cso, NULL);
78 return;
81 fs = $self->pipe->create_fs_state($self->pipe, state);
82 if(!fs)
83 return;
85 if(cso_set_fragment_shader_handle($self->cso, fs) != PIPE_OK)
86 return;
88 cso_delete_fragment_shader($self->cso, $self->fs);
89 $self->fs = fs;
92 void set_vertex_shader( const struct pipe_shader_state *state ) {
93 void *vs;
95 if(!state) {
96 cso_set_vertex_shader_handle($self->cso, NULL);
97 return;
100 vs = $self->pipe->create_vs_state($self->pipe, state);
101 if(!vs)
102 return;
104 if(cso_set_vertex_shader_handle($self->cso, vs) != PIPE_OK)
105 return;
107 cso_delete_vertex_shader($self->cso, $self->vs);
108 $self->vs = vs;
111 void set_geometry_shader( const struct pipe_shader_state *state ) {
112 void *gs;
114 if(!state) {
115 cso_set_geometry_shader_handle($self->cso, NULL);
116 return;
119 gs = $self->pipe->create_gs_state($self->pipe, state);
120 if(!gs)
121 return;
123 if(cso_set_geometry_shader_handle($self->cso, gs) != PIPE_OK)
124 return;
126 cso_delete_geometry_shader($self->cso, $self->gs);
127 $self->gs = 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 swizzle_r = 0,
136 unsigned swizzle_g = 1,
137 unsigned swizzle_b = 2,
138 unsigned swizzle_a = 3)
140 struct pipe_context *pipe = $self->pipe;
141 struct pipe_sampler_view templat;
143 memset(&templat, 0, sizeof templat);
144 if (format == PIPE_FORMAT_NONE) {
145 templat.format = texture->format;
146 } else {
147 templat.format = format;
149 templat.last_level = MIN2(last_level, texture->last_level);
150 templat.first_level = first_level;
151 templat.last_level = last_level;
152 templat.swizzle_r = swizzle_r;
153 templat.swizzle_g = swizzle_g;
154 templat.swizzle_b = swizzle_b;
155 templat.swizzle_a = swizzle_a;
157 return pipe->create_sampler_view(pipe, texture, &templat);
160 void
161 sampler_view_destroy(struct pipe_context *ctx,
162 struct pipe_sampler_view *view)
164 struct pipe_context *pipe = $self->pipe;
166 pipe->sampler_view_destroy(pipe, view);
170 * Parameter-like state (or properties)
173 void set_blend_color(const struct pipe_blend_color *state ) {
174 cso_set_blend_color($self->cso, state);
177 void set_stencil_ref(const struct pipe_stencil_ref *state ) {
178 cso_set_stencil_ref($self->cso, state);
181 void set_clip(const struct pipe_clip_state *state ) {
182 $self->pipe->set_clip_state($self->pipe, state);
185 void set_constant_buffer(unsigned shader, unsigned index,
186 struct pipe_resource *buffer )
188 $self->pipe->set_constant_buffer($self->pipe, shader, index, buffer);
191 void set_framebuffer(const struct pipe_framebuffer_state *state )
193 memcpy(&$self->framebuffer, state, sizeof *state);
194 cso_set_framebuffer($self->cso, state);
197 void set_polygon_stipple(const struct pipe_poly_stipple *state ) {
198 $self->pipe->set_polygon_stipple($self->pipe, state);
201 void set_scissor(const struct pipe_scissor_state *state ) {
202 $self->pipe->set_scissor_state($self->pipe, state);
205 void set_viewport(const struct pipe_viewport_state *state) {
206 cso_set_viewport($self->cso, state);
209 void set_fragment_sampler_view(unsigned index,
210 struct pipe_sampler_view *view)
212 pipe_sampler_view_reference(&$self->fragment_sampler_views[index], view);
214 $self->pipe->set_fragment_sampler_views($self->pipe,
215 PIPE_MAX_SAMPLERS,
216 $self->fragment_sampler_views);
219 void set_vertex_sampler_view(unsigned index,
220 struct pipe_sampler_view *view)
222 pipe_sampler_view_reference(&$self->vertex_sampler_views[index], view);
224 $self->pipe->set_vertex_sampler_views($self->pipe,
225 PIPE_MAX_VERTEX_SAMPLERS,
226 $self->vertex_sampler_views);
229 void set_fragment_sampler_texture(unsigned index,
230 struct pipe_resource *texture) {
231 struct pipe_sampler_view templ;
233 if(!texture)
234 texture = $self->default_texture;
235 pipe_sampler_view_reference(&$self->fragment_sampler_views[index], NULL);
236 u_sampler_view_default_template(&templ,
237 texture,
238 texture->format);
239 $self->fragment_sampler_views[index] = $self->pipe->create_sampler_view($self->pipe,
240 texture,
241 &templ);
242 $self->pipe->set_fragment_sampler_views($self->pipe,
243 PIPE_MAX_SAMPLERS,
244 $self->fragment_sampler_views);
247 void set_vertex_sampler_texture(unsigned index,
248 struct pipe_resource *texture) {
249 struct pipe_sampler_view templ;
251 if(!texture)
252 texture = $self->default_texture;
253 pipe_sampler_view_reference(&$self->vertex_sampler_views[index], NULL);
254 u_sampler_view_default_template(&templ,
255 texture,
256 texture->format);
257 $self->vertex_sampler_views[index] = $self->pipe->create_sampler_view($self->pipe,
258 texture,
259 &templ);
261 $self->pipe->set_vertex_sampler_views($self->pipe,
262 PIPE_MAX_VERTEX_SAMPLERS,
263 $self->vertex_sampler_views);
266 void set_vertex_buffer(unsigned index,
267 unsigned stride,
268 unsigned max_index,
269 unsigned buffer_offset,
270 struct pipe_resource *buffer)
272 unsigned i;
273 struct pipe_vertex_buffer state;
275 memset(&state, 0, sizeof(state));
276 state.stride = stride;
277 state.max_index = max_index;
278 state.buffer_offset = buffer_offset;
279 state.buffer = buffer;
281 memcpy(&$self->vertex_buffers[index], &state, sizeof(state));
283 for(i = 0; i < PIPE_MAX_ATTRIBS; ++i)
284 if(self->vertex_buffers[i].buffer)
285 $self->num_vertex_buffers = i + 1;
287 $self->pipe->set_vertex_buffers($self->pipe,
288 $self->num_vertex_buffers,
289 $self->vertex_buffers);
292 void set_vertex_element(unsigned index,
293 const struct pipe_vertex_element *element)
295 memcpy(&$self->vertex_elements[index], element, sizeof(*element));
298 void set_vertex_elements(unsigned num)
300 $self->num_vertex_elements = num;
301 cso_set_vertex_elements($self->cso,
302 $self->num_vertex_elements,
303 $self->vertex_elements);
307 * Draw functions
310 void draw_arrays(unsigned mode, unsigned start, unsigned count) {
311 $self->pipe->draw_arrays($self->pipe, mode, start, count);
314 void draw_elements( struct pipe_resource *indexBuffer,
315 unsigned indexSize, int indexBias,
316 unsigned mode, unsigned start, unsigned count)
318 $self->pipe->draw_elements($self->pipe,
319 indexBuffer,
320 indexSize,
321 indexBias,
322 mode, start, count);
325 void draw_range_elements( struct pipe_resource *indexBuffer,
326 unsigned indexSize, int indexBias,
327 unsigned minIndex, unsigned maxIndex,
328 unsigned mode, unsigned start, unsigned count)
330 $self->pipe->draw_range_elements($self->pipe,
331 indexBuffer, indexSize, indexBias,
332 minIndex, maxIndex,
333 mode, start, count);
336 void draw_vertices(unsigned prim,
337 unsigned num_verts,
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;
347 float *map;
348 unsigned size;
349 unsigned i;
351 size = num_verts * num_attribs * 4 * sizeof(float);
353 vbuf = pipe_buffer_create(screen,
354 PIPE_BIND_VERTEX_BUFFER,
355 size);
356 if(!vbuf)
357 goto error1;
359 map = pipe_buffer_map(pipe, vbuf, PIPE_TRANSFER_WRITE, &transfer);
360 if (!map)
361 goto error2;
362 memcpy(map, vertices, size);
363 pipe_buffer_unmap(pipe, vbuf, 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);
384 /* draw */
385 pipe->draw_arrays(pipe, prim, 0, num_verts);
387 cso_restore_vertex_elements($self->cso);
389 error2:
390 pipe_resource_reference(&vbuf, NULL);
391 error1:
395 void
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);
402 void
403 flush(unsigned flags = 0) {
404 struct pipe_fence_handle *fence = NULL;
405 $self->pipe->flush($self->pipe, flags | PIPE_FLUSH_RENDER_CACHE, &fence);
406 if(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 );
414 * Surface functions
417 void resource_copy_region(struct pipe_resource *dst,
418 struct pipe_subresource subdst,
419 unsigned dstx, unsigned dsty, unsigned dstz,
420 struct pipe_resource *src,
421 struct pipe_subresource subsrc,
422 unsigned srcx, unsigned srcy, unsigned srcz,
423 unsigned width, unsigned height)
425 $self->pipe->resource_copy_region($self->pipe,
426 dst, subdst, dstx, dsty, dstz,
427 src, subsrc, srcx, srcy, srcz,
428 width, height);
432 void clear_render_target(struct st_surface *dst,
433 float *rgba,
434 unsigned x, unsigned y,
435 unsigned width, unsigned height)
437 struct pipe_surface *_dst = NULL;
439 _dst = st_pipe_surface(dst, PIPE_BIND_RENDER_TARGET);
440 if(!_dst)
441 SWIG_exception(SWIG_ValueError, "couldn't acquire destination surface for writing");
443 $self->pipe->clear_render_target($self->pipe, _dst, rgba, x, y, width, height);
445 fail:
446 pipe_surface_reference(&_dst, NULL);
449 void clear_depth_stencil(struct st_surface *dst,
450 unsigned clear_flags,
451 double depth,
452 unsigned stencil,
453 unsigned x, unsigned y,
454 unsigned width, unsigned height)
456 struct pipe_surface *_dst = NULL;
458 _dst = st_pipe_surface(dst, PIPE_BIND_DEPTH_STENCIL);
459 if(!_dst)
460 SWIG_exception(SWIG_ValueError, "couldn't acquire destination surface for writing");
462 $self->pipe->clear_depth_stencil($self->pipe, _dst, clear_flags, depth, stencil,
463 x, y, width, height);
465 fail:
466 pipe_surface_reference(&_dst, NULL);
469 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
470 void
471 surface_read_raw(struct st_surface *surface,
472 unsigned x, unsigned y, unsigned w, unsigned h,
473 char **STRING, int *LENGTH)
475 struct pipe_resource *texture = surface->texture;
476 struct pipe_context *pipe = $self->pipe;
477 struct pipe_transfer *transfer;
478 unsigned stride;
480 stride = util_format_get_stride(texture->format, w);
481 *LENGTH = util_format_get_nblocksy(texture->format, h) * stride;
482 *STRING = (char *) malloc(*LENGTH);
483 if(!*STRING)
484 return;
486 transfer = pipe_get_transfer(pipe,
487 surface->texture,
488 surface->face,
489 surface->level,
490 surface->zslice,
491 PIPE_TRANSFER_READ,
492 x, y, w, h);
493 if(transfer) {
494 pipe_get_tile_raw(pipe, transfer, 0, 0, w, h, *STRING, stride);
495 pipe->transfer_destroy(pipe, transfer);
499 %cstring_input_binary(const char *STRING, unsigned LENGTH);
500 void
501 surface_write_raw(struct st_surface *surface,
502 unsigned x, unsigned y, unsigned w, unsigned h,
503 const char *STRING, unsigned LENGTH, unsigned stride = 0)
505 struct pipe_resource *texture = surface->texture;
506 struct pipe_context *pipe = $self->pipe;
507 struct pipe_transfer *transfer;
509 if(stride == 0)
510 stride = util_format_get_stride(texture->format, w);
512 if(LENGTH < util_format_get_nblocksy(texture->format, h) * stride)
513 SWIG_exception(SWIG_ValueError, "offset must be smaller than buffer size");
515 transfer = pipe_get_transfer(pipe,
516 surface->texture,
517 surface->face,
518 surface->level,
519 surface->zslice,
520 PIPE_TRANSFER_WRITE,
521 x, y, w, h);
522 if(!transfer)
523 SWIG_exception(SWIG_MemoryError, "couldn't initiate transfer");
525 pipe_put_tile_raw(pipe, transfer, 0, 0, w, h, STRING, stride);
526 pipe->transfer_destroy(pipe, transfer);
528 fail:
529 return;
532 void
533 surface_read_rgba(struct st_surface *surface,
534 unsigned x, unsigned y, unsigned w, unsigned h,
535 float *rgba)
537 struct pipe_context *pipe = $self->pipe;
538 struct pipe_transfer *transfer;
539 transfer = pipe_get_transfer(pipe,
540 surface->texture,
541 surface->face,
542 surface->level,
543 surface->zslice,
544 PIPE_TRANSFER_READ,
545 x, y, w, h);
546 if(transfer) {
547 pipe_get_tile_rgba(pipe, transfer, 0, 0, w, h, rgba);
548 pipe->transfer_destroy(pipe, transfer);
552 void
553 surface_write_rgba(struct st_surface *surface,
554 unsigned x, unsigned y, unsigned w, unsigned h,
555 const float *rgba)
557 struct pipe_context *pipe = $self->pipe;
558 struct pipe_transfer *transfer;
559 transfer = pipe_get_transfer(pipe,
560 surface->texture,
561 surface->face,
562 surface->level,
563 surface->zslice,
564 PIPE_TRANSFER_WRITE,
565 x, y, w, h);
566 if(transfer) {
567 pipe_put_tile_rgba(pipe, transfer, 0, 0, w, h, rgba);
568 pipe->transfer_destroy(pipe, transfer);
572 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
573 void
574 surface_read_rgba8(struct st_surface *surface,
575 unsigned x, unsigned y, unsigned w, unsigned h,
576 char **STRING, int *LENGTH)
578 struct pipe_context *pipe = $self->pipe;
579 struct pipe_transfer *transfer;
580 float *rgba;
581 unsigned char *rgba8;
582 unsigned i, j, k;
584 *LENGTH = 0;
585 *STRING = NULL;
587 if (!surface)
588 return;
590 *LENGTH = h*w*4;
591 *STRING = (char *) malloc(*LENGTH);
592 if(!*STRING)
593 return;
595 rgba = malloc(h*w*4*sizeof(float));
596 if(!rgba)
597 return;
599 rgba8 = (unsigned char *) *STRING;
601 transfer = pipe_get_transfer(pipe,
602 surface->texture,
603 surface->face,
604 surface->level,
605 surface->zslice,
606 PIPE_TRANSFER_READ,
607 x, y, w, h);
608 if(transfer) {
609 pipe_get_tile_rgba(pipe, transfer, 0, 0, w, h, rgba);
610 for(j = 0; j < h; ++j) {
611 for(i = 0; i < w; ++i)
612 for(k = 0; k <4; ++k)
613 rgba8[j*w*4 + i*4 + k] = float_to_ubyte(rgba[j*w*4 + i*4 + k]);
615 pipe->transfer_destroy(pipe, transfer);
618 free(rgba);
621 void
622 surface_read_z(struct st_surface *surface,
623 unsigned x, unsigned y, unsigned w, unsigned h,
624 unsigned *z)
626 struct pipe_context *pipe = $self->pipe;
627 struct pipe_transfer *transfer;
628 transfer = pipe_get_transfer(pipe,
629 surface->texture,
630 surface->face,
631 surface->level,
632 surface->zslice,
633 PIPE_TRANSFER_READ,
634 x, y, w, h);
635 if(transfer) {
636 pipe_get_tile_z(pipe, transfer, 0, 0, w, h, z);
637 pipe->transfer_destroy(pipe, transfer);
641 void
642 surface_write_z(struct st_surface *surface,
643 unsigned x, unsigned y, unsigned w, unsigned h,
644 const unsigned *z)
646 struct pipe_context *pipe = $self->pipe;
647 struct pipe_transfer *transfer;
648 transfer = pipe_get_transfer(pipe,
649 surface->texture,
650 surface->face,
651 surface->level,
652 surface->zslice,
653 PIPE_TRANSFER_WRITE,
654 x, y, w, h);
655 if(transfer) {
656 pipe_put_tile_z(pipe, transfer, 0, 0, w, h, z);
657 pipe->transfer_destroy(pipe, transfer);
661 void
662 surface_sample_rgba(struct st_surface *surface,
663 float *rgba,
664 int norm = 0)
666 st_sample_surface($self->pipe, surface, rgba, norm != 0);
669 unsigned
670 surface_compare_rgba(struct st_surface *surface,
671 unsigned x, unsigned y, unsigned w, unsigned h,
672 const float *rgba, float tol = 0.0)
674 struct pipe_context *pipe = $self->pipe;
675 struct pipe_transfer *transfer;
676 float *rgba2;
677 const float *p1;
678 const float *p2;
679 unsigned i, j, n;
681 rgba2 = MALLOC(h*w*4*sizeof(float));
682 if(!rgba2)
683 return ~0;
685 transfer = pipe_get_transfer(pipe,
686 surface->texture,
687 surface->face,
688 surface->level,
689 surface->zslice,
690 PIPE_TRANSFER_READ,
691 x, y, w, h);
692 if(!transfer) {
693 FREE(rgba2);
694 return ~0;
697 pipe_get_tile_rgba(pipe, transfer, 0, 0, w, h, rgba2);
698 pipe->transfer_destroy(pipe, transfer);
700 p1 = rgba;
701 p2 = rgba2;
702 n = 0;
703 for(i = h*w; i; --i) {
704 unsigned differs = 0;
705 for(j = 4; j; --j) {
706 float delta = *p2++ - *p1++;
707 if (delta < -tol || delta > tol)
708 differs = 1;
710 n += differs;
713 FREE(rgba2);
715 return n;
718 %cstring_input_binary(const char *STRING, unsigned LENGTH);
719 void
720 transfer_inline_write(struct pipe_resource *resource,
721 struct pipe_subresource *sr,
722 unsigned usage,
723 const struct pipe_box *box,
724 const char *STRING, unsigned LENGTH,
725 unsigned stride,
726 unsigned slice_stride)
728 struct pipe_context *pipe = $self->pipe;
730 pipe->transfer_inline_write(pipe, resource, *sr, usage, box, STRING, stride, slice_stride);
733 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
734 void buffer_read(struct pipe_resource *buffer,
735 char **STRING, int *LENGTH)
737 struct pipe_context *pipe = $self->pipe;
739 assert(buffer->target == PIPE_BUFFER);
741 *LENGTH = buffer->width0;
742 *STRING = (char *) malloc(buffer->width0);
743 if(!*STRING)
744 return;
746 pipe_buffer_read(pipe, buffer, 0, buffer->width0, *STRING);
749 void buffer_write(struct pipe_resource *buffer,
750 const char *STRING, unsigned LENGTH, unsigned offset = 0)
752 struct pipe_context *pipe = $self->pipe;
754 assert(buffer->target == PIPE_BUFFER);
756 if(offset > buffer->width0)
757 SWIG_exception(SWIG_ValueError, "offset must be smaller than buffer size");
759 if(offset + LENGTH > buffer->width0)
760 SWIG_exception(SWIG_ValueError, "data length must fit inside the buffer");
762 pipe_buffer_write(pipe, buffer, offset, LENGTH, STRING);
764 fail:
765 return;