Merge branch '7.8'
[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_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);
58 }
59
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);
63 }
64
65 void set_rasterizer( const struct pipe_rasterizer_state *state ) {
66 cso_set_rasterizer($self->cso, state);
67 }
68
69 void set_depth_stencil_alpha(const struct pipe_depth_stencil_alpha_state *state) {
70 cso_set_depth_stencil_alpha($self->cso, state);
71 }
72
73 void set_fragment_shader( const struct pipe_shader_state *state ) {
74 void *fs;
75
76 if(!state) {
77 cso_set_fragment_shader_handle($self->cso, NULL);
78 return;
79 }
80
81 fs = $self->pipe->create_fs_state($self->pipe, state);
82 if(!fs)
83 return;
84
85 if(cso_set_fragment_shader_handle($self->cso, fs) != PIPE_OK)
86 return;
87
88 cso_delete_fragment_shader($self->cso, $self->fs);
89 $self->fs = fs;
90 }
91
92 void set_vertex_shader( const struct pipe_shader_state *state ) {
93 void *vs;
94
95 if(!state) {
96 cso_set_vertex_shader_handle($self->cso, NULL);
97 return;
98 }
99
100 vs = $self->pipe->create_vs_state($self->pipe, state);
101 if(!vs)
102 return;
103
104 if(cso_set_vertex_shader_handle($self->cso, vs) != PIPE_OK)
105 return;
106
107 cso_delete_vertex_shader($self->cso, $self->vs);
108 $self->vs = vs;
109 }
110
111 void set_geometry_shader( const struct pipe_shader_state *state ) {
112 void *gs;
113
114 if(!state) {
115 cso_set_geometry_shader_handle($self->cso, NULL);
116 return;
117 }
118
119 gs = $self->pipe->create_gs_state($self->pipe, state);
120 if(!gs)
121 return;
122
123 if(cso_set_geometry_shader_handle($self->cso, gs) != PIPE_OK)
124 return;
125
126 cso_delete_geometry_shader($self->cso, $self->gs);
127 $self->gs = gs;
128 }
129
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)
139 {
140 struct pipe_context *pipe = $self->pipe;
141 struct pipe_sampler_view templat;
142
143 memset(&templat, 0, sizeof templat);
144 if (format == PIPE_FORMAT_NONE) {
145 templat.format = texture->format;
146 } else {
147 templat.format = format;
148 }
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;
156
157 return pipe->create_sampler_view(pipe, texture, &templat);
158 }
159
160 void
161 sampler_view_destroy(struct pipe_context *ctx,
162 struct pipe_sampler_view *view)
163 {
164 struct pipe_context *pipe = $self->pipe;
165
166 pipe->sampler_view_destroy(pipe, view);
167 }
168
169 /*
170 * Parameter-like state (or properties)
171 */
172
173 void set_blend_color(const struct pipe_blend_color *state ) {
174 cso_set_blend_color($self->cso, state);
175 }
176
177 void set_stencil_ref(const struct pipe_stencil_ref *state ) {
178 cso_set_stencil_ref($self->cso, state);
179 }
180
181 void set_clip(const struct pipe_clip_state *state ) {
182 $self->pipe->set_clip_state($self->pipe, state);
183 }
184
185 void set_constant_buffer(unsigned shader, unsigned index,
186 struct pipe_resource *buffer )
187 {
188 $self->pipe->set_constant_buffer($self->pipe, shader, index, buffer);
189 }
190
191 void set_framebuffer(const struct pipe_framebuffer_state *state )
192 {
193 memcpy(&$self->framebuffer, state, sizeof *state);
194 cso_set_framebuffer($self->cso, state);
195 }
196
197 void set_polygon_stipple(const struct pipe_poly_stipple *state ) {
198 $self->pipe->set_polygon_stipple($self->pipe, state);
199 }
200
201 void set_scissor(const struct pipe_scissor_state *state ) {
202 $self->pipe->set_scissor_state($self->pipe, state);
203 }
204
205 void set_viewport(const struct pipe_viewport_state *state) {
206 cso_set_viewport($self->cso, state);
207 }
208
209 void set_fragment_sampler_view(unsigned index,
210 struct pipe_sampler_view *view)
211 {
212 pipe_sampler_view_reference(&$self->fragment_sampler_views[index], view);
213
214 $self->pipe->set_fragment_sampler_views($self->pipe,
215 PIPE_MAX_SAMPLERS,
216 $self->fragment_sampler_views);
217 }
218
219 void set_vertex_sampler_view(unsigned index,
220 struct pipe_sampler_view *view)
221 {
222 pipe_sampler_view_reference(&$self->vertex_sampler_views[index], view);
223
224 $self->pipe->set_vertex_sampler_views($self->pipe,
225 PIPE_MAX_VERTEX_SAMPLERS,
226 $self->vertex_sampler_views);
227 }
228
229 void set_fragment_sampler_texture(unsigned index,
230 struct pipe_resource *texture) {
231 struct pipe_sampler_view templ;
232
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);
245 }
246
247 void set_vertex_sampler_texture(unsigned index,
248 struct pipe_resource *texture) {
249 struct pipe_sampler_view templ;
250
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);
260
261 $self->pipe->set_vertex_sampler_views($self->pipe,
262 PIPE_MAX_VERTEX_SAMPLERS,
263 $self->vertex_sampler_views);
264 }
265
266 void set_vertex_buffer(unsigned index,
267 unsigned stride,
268 unsigned max_index,
269 unsigned buffer_offset,
270 struct pipe_resource *buffer)
271 {
272 unsigned i;
273 struct pipe_vertex_buffer state;
274
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;
280
281 memcpy(&$self->vertex_buffers[index], &state, sizeof(state));
282
283 for(i = 0; i < PIPE_MAX_ATTRIBS; ++i)
284 if(self->vertex_buffers[i].buffer)
285 $self->num_vertex_buffers = i + 1;
286
287 $self->pipe->set_vertex_buffers($self->pipe,
288 $self->num_vertex_buffers,
289 $self->vertex_buffers);
290 }
291
292 void set_vertex_element(unsigned index,
293 const struct pipe_vertex_element *element)
294 {
295 memcpy(&$self->vertex_elements[index], element, sizeof(*element));
296 }
297
298 void set_vertex_elements(unsigned num)
299 {
300 $self->num_vertex_elements = num;
301 cso_set_vertex_elements($self->cso,
302 $self->num_vertex_elements,
303 $self->vertex_elements);
304 }
305
306 /*
307 * Draw functions
308 */
309
310 void draw_arrays(unsigned mode, unsigned start, unsigned count) {
311 $self->pipe->draw_arrays($self->pipe, mode, start, count);
312 }
313
314 void draw_elements( struct pipe_resource *indexBuffer,
315 unsigned indexSize, int indexBias,
316 unsigned mode, unsigned start, unsigned count)
317 {
318 $self->pipe->draw_elements($self->pipe,
319 indexBuffer,
320 indexSize,
321 indexBias,
322 mode, start, count);
323 }
324
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)
329 {
330 $self->pipe->draw_range_elements($self->pipe,
331 indexBuffer, indexSize, indexBias,
332 minIndex, maxIndex,
333 mode, start, count);
334 }
335
336 void draw_vertices(unsigned prim,
337 unsigned num_verts,
338 unsigned num_attribs,
339 const float *vertices)
340 {
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;
350
351 size = num_verts * num_attribs * 4 * sizeof(float);
352
353 vbuf = pipe_buffer_create(screen,
354 PIPE_BIND_VERTEX_BUFFER,
355 size);
356 if(!vbuf)
357 goto error1;
358
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);
364
365 cso_save_vertex_elements($self->cso);
366
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;
373 }
374 cso_set_vertex_elements($self->cso, num_attribs, velements);
375
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);
383
384 /* draw */
385 pipe->draw_arrays(pipe, prim, 0, num_verts);
386
387 cso_restore_vertex_elements($self->cso);
388
389 error2:
390 pipe_resource_reference(&vbuf, NULL);
391 error1:
392 ;
393 }
394
395 void
396 clear(unsigned buffers, const float *rgba, double depth = 0.0f,
397 unsigned stencil = 0)
398 {
399 $self->pipe->clear($self->pipe, buffers, rgba, depth, stencil);
400 }
401
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 );
410 }
411 }
412
413 /*
414 * Surface functions
415 */
416
417 void surface_copy(struct st_surface *dst,
418 unsigned destx, unsigned desty,
419 struct st_surface *src,
420 unsigned srcx, unsigned srcy,
421 unsigned width, unsigned height)
422 {
423 struct pipe_surface *_dst = NULL;
424 struct pipe_surface *_src = NULL;
425
426 _dst = st_pipe_surface(dst, PIPE_BIND_BLIT_DESTINATION);
427 if(!_dst)
428 SWIG_exception(SWIG_ValueError, "couldn't acquire destination surface for writing");
429
430 _src = st_pipe_surface(src, PIPE_BIND_BLIT_SOURCE);
431 if(!_src)
432 SWIG_exception(SWIG_ValueError, "couldn't acquire source surface for reading");
433
434 $self->pipe->surface_copy($self->pipe, _dst, destx, desty, _src, srcx, srcy, width, height);
435
436 fail:
437 pipe_surface_reference(&_src, NULL);
438 pipe_surface_reference(&_dst, NULL);
439 }
440
441 void surface_fill(struct st_surface *dst,
442 unsigned x, unsigned y,
443 unsigned width, unsigned height,
444 unsigned value)
445 {
446 struct pipe_surface *_dst = NULL;
447
448 _dst = st_pipe_surface(dst, PIPE_BIND_BLIT_DESTINATION);
449 if(!_dst)
450 SWIG_exception(SWIG_ValueError, "couldn't acquire destination surface for writing");
451
452 $self->pipe->surface_fill($self->pipe, _dst, x, y, width, height, value);
453
454 fail:
455 pipe_surface_reference(&_dst, NULL);
456 }
457
458 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
459 void
460 surface_read_raw(struct st_surface *surface,
461 unsigned x, unsigned y, unsigned w, unsigned h,
462 char **STRING, int *LENGTH)
463 {
464 struct pipe_resource *texture = surface->texture;
465 struct pipe_context *pipe = $self->pipe;
466 struct pipe_transfer *transfer;
467 unsigned stride;
468
469 stride = util_format_get_stride(texture->format, w);
470 *LENGTH = util_format_get_nblocksy(texture->format, h) * stride;
471 *STRING = (char *) malloc(*LENGTH);
472 if(!*STRING)
473 return;
474
475 transfer = pipe_get_transfer(pipe,
476 surface->texture,
477 surface->face,
478 surface->level,
479 surface->zslice,
480 PIPE_TRANSFER_READ,
481 x, y, w, h);
482 if(transfer) {
483 pipe_get_tile_raw(pipe, transfer, 0, 0, w, h, *STRING, stride);
484 pipe->transfer_destroy(pipe, transfer);
485 }
486 }
487
488 %cstring_input_binary(const char *STRING, unsigned LENGTH);
489 void
490 surface_write_raw(struct st_surface *surface,
491 unsigned x, unsigned y, unsigned w, unsigned h,
492 const char *STRING, unsigned LENGTH, unsigned stride = 0)
493 {
494 struct pipe_resource *texture = surface->texture;
495 struct pipe_context *pipe = $self->pipe;
496 struct pipe_transfer *transfer;
497
498 if(stride == 0)
499 stride = util_format_get_stride(texture->format, w);
500
501 if(LENGTH < util_format_get_nblocksy(texture->format, h) * stride)
502 SWIG_exception(SWIG_ValueError, "offset must be smaller than buffer size");
503
504 transfer = pipe_get_transfer(pipe,
505 surface->texture,
506 surface->face,
507 surface->level,
508 surface->zslice,
509 PIPE_TRANSFER_WRITE,
510 x, y, w, h);
511 if(!transfer)
512 SWIG_exception(SWIG_MemoryError, "couldn't initiate transfer");
513
514 pipe_put_tile_raw(pipe, transfer, 0, 0, w, h, STRING, stride);
515 pipe->transfer_destroy(pipe, transfer);
516
517 fail:
518 return;
519 }
520
521 void
522 surface_read_rgba(struct st_surface *surface,
523 unsigned x, unsigned y, unsigned w, unsigned h,
524 float *rgba)
525 {
526 struct pipe_context *pipe = $self->pipe;
527 struct pipe_transfer *transfer;
528 transfer = pipe_get_transfer(pipe,
529 surface->texture,
530 surface->face,
531 surface->level,
532 surface->zslice,
533 PIPE_TRANSFER_READ,
534 x, y, w, h);
535 if(transfer) {
536 pipe_get_tile_rgba(pipe, transfer, 0, 0, w, h, rgba);
537 pipe->transfer_destroy(pipe, transfer);
538 }
539 }
540
541 void
542 surface_write_rgba(struct st_surface *surface,
543 unsigned x, unsigned y, unsigned w, unsigned h,
544 const float *rgba)
545 {
546 struct pipe_context *pipe = $self->pipe;
547 struct pipe_transfer *transfer;
548 transfer = pipe_get_transfer(pipe,
549 surface->texture,
550 surface->face,
551 surface->level,
552 surface->zslice,
553 PIPE_TRANSFER_WRITE,
554 x, y, w, h);
555 if(transfer) {
556 pipe_put_tile_rgba(pipe, transfer, 0, 0, w, h, rgba);
557 pipe->transfer_destroy(pipe, transfer);
558 }
559 }
560
561 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
562 void
563 surface_read_rgba8(struct st_surface *surface,
564 unsigned x, unsigned y, unsigned w, unsigned h,
565 char **STRING, int *LENGTH)
566 {
567 struct pipe_context *pipe = $self->pipe;
568 struct pipe_transfer *transfer;
569 float *rgba;
570 unsigned char *rgba8;
571 unsigned i, j, k;
572
573 *LENGTH = 0;
574 *STRING = NULL;
575
576 if (!surface)
577 return;
578
579 *LENGTH = h*w*4;
580 *STRING = (char *) malloc(*LENGTH);
581 if(!*STRING)
582 return;
583
584 rgba = malloc(h*w*4*sizeof(float));
585 if(!rgba)
586 return;
587
588 rgba8 = (unsigned char *) *STRING;
589
590 transfer = pipe_get_transfer(pipe,
591 surface->texture,
592 surface->face,
593 surface->level,
594 surface->zslice,
595 PIPE_TRANSFER_READ,
596 x, y, w, h);
597 if(transfer) {
598 pipe_get_tile_rgba(pipe, transfer, 0, 0, w, h, rgba);
599 for(j = 0; j < h; ++j) {
600 for(i = 0; i < w; ++i)
601 for(k = 0; k <4; ++k)
602 rgba8[j*w*4 + i*4 + k] = float_to_ubyte(rgba[j*w*4 + i*4 + k]);
603 }
604 pipe->transfer_destroy(pipe, transfer);
605 }
606
607 free(rgba);
608 }
609
610 void
611 surface_read_z(struct st_surface *surface,
612 unsigned x, unsigned y, unsigned w, unsigned h,
613 unsigned *z)
614 {
615 struct pipe_context *pipe = $self->pipe;
616 struct pipe_transfer *transfer;
617 transfer = pipe_get_transfer(pipe,
618 surface->texture,
619 surface->face,
620 surface->level,
621 surface->zslice,
622 PIPE_TRANSFER_READ,
623 x, y, w, h);
624 if(transfer) {
625 pipe_get_tile_z(pipe, transfer, 0, 0, w, h, z);
626 pipe->transfer_destroy(pipe, transfer);
627 }
628 }
629
630 void
631 surface_write_z(struct st_surface *surface,
632 unsigned x, unsigned y, unsigned w, unsigned h,
633 const unsigned *z)
634 {
635 struct pipe_context *pipe = $self->pipe;
636 struct pipe_transfer *transfer;
637 transfer = pipe_get_transfer(pipe,
638 surface->texture,
639 surface->face,
640 surface->level,
641 surface->zslice,
642 PIPE_TRANSFER_WRITE,
643 x, y, w, h);
644 if(transfer) {
645 pipe_put_tile_z(pipe, transfer, 0, 0, w, h, z);
646 pipe->transfer_destroy(pipe, transfer);
647 }
648 }
649
650 void
651 surface_sample_rgba(struct st_surface *surface,
652 float *rgba,
653 int norm = 0)
654 {
655 st_sample_surface($self->pipe, surface, rgba, norm != 0);
656 }
657
658 unsigned
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)
662 {
663 struct pipe_context *pipe = $self->pipe;
664 struct pipe_transfer *transfer;
665 float *rgba2;
666 const float *p1;
667 const float *p2;
668 unsigned i, j, n;
669
670 rgba2 = MALLOC(h*w*4*sizeof(float));
671 if(!rgba2)
672 return ~0;
673
674 transfer = pipe_get_transfer(pipe,
675 surface->texture,
676 surface->face,
677 surface->level,
678 surface->zslice,
679 PIPE_TRANSFER_READ,
680 x, y, w, h);
681 if(!transfer) {
682 FREE(rgba2);
683 return ~0;
684 }
685
686 pipe_get_tile_rgba(pipe, transfer, 0, 0, w, h, rgba2);
687 pipe->transfer_destroy(pipe, transfer);
688
689 p1 = rgba;
690 p2 = rgba2;
691 n = 0;
692 for(i = h*w; i; --i) {
693 unsigned differs = 0;
694 for(j = 4; j; --j) {
695 float delta = *p2++ - *p1++;
696 if (delta < -tol || delta > tol)
697 differs = 1;
698 }
699 n += differs;
700 }
701
702 FREE(rgba2);
703
704 return n;
705 }
706
707 %cstring_input_binary(const char *STRING, unsigned LENGTH);
708 void
709 transfer_inline_write(struct pipe_resource *resource,
710 struct pipe_subresource *sr,
711 unsigned usage,
712 const struct pipe_box *box,
713 const char *STRING, unsigned LENGTH,
714 unsigned stride,
715 unsigned slice_stride)
716 {
717 struct pipe_context *pipe = $self->pipe;
718
719 pipe->transfer_inline_write(pipe, resource, *sr, usage, box, STRING, stride, slice_stride);
720 }
721
722 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
723 void buffer_read(struct pipe_resource *buffer,
724 char **STRING, int *LENGTH)
725 {
726 struct pipe_context *pipe = $self->pipe;
727
728 assert(buffer->target == PIPE_BUFFER);
729
730 *LENGTH = buffer->width0;
731 *STRING = (char *) malloc(buffer->width0);
732 if(!*STRING)
733 return;
734
735 pipe_buffer_read(pipe, buffer, 0, buffer->width0, *STRING);
736 }
737
738 void buffer_write(struct pipe_resource *buffer,
739 const char *STRING, unsigned LENGTH, unsigned offset = 0)
740 {
741 struct pipe_context *pipe = $self->pipe;
742
743 assert(buffer->target == PIPE_BUFFER);
744
745 if(offset > buffer->width0)
746 SWIG_exception(SWIG_ValueError, "offset must be smaller than buffer size");
747
748 if(offset + LENGTH > buffer->width0)
749 SWIG_exception(SWIG_ValueError, "data length must fit inside the buffer");
750
751 pipe_buffer_write(pipe, buffer, offset, LENGTH, STRING);
752
753 fail:
754 return;
755 }
756
757 };