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,
316 unsigned mode, unsigned start, unsigned count)
317 {
318 $self->pipe->draw_elements($self->pipe,
319 indexBuffer,
320 indexSize,
321 mode, start, count);
322 }
323
324 void draw_range_elements( struct pipe_resource *indexBuffer,
325 unsigned indexSize, unsigned minIndex, unsigned maxIndex,
326 unsigned mode, unsigned start, unsigned count)
327 {
328 $self->pipe->draw_range_elements($self->pipe,
329 indexBuffer,
330 indexSize, minIndex, maxIndex,
331 mode, start, count);
332 }
333
334 void draw_vertices(unsigned prim,
335 unsigned num_verts,
336 unsigned num_attribs,
337 const float *vertices)
338 {
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;
345 float *map;
346 unsigned size;
347 unsigned i;
348
349 size = num_verts * num_attribs * 4 * sizeof(float);
350
351 vbuf = pipe_buffer_create(screen,
352 PIPE_BIND_VERTEX_BUFFER,
353 size);
354 if(!vbuf)
355 goto error1;
356
357 map = pipe_buffer_map(pipe, vbuf, PIPE_TRANSFER_WRITE, &transfer);
358 if (!map)
359 goto error2;
360 memcpy(map, vertices, size);
361 pipe_buffer_unmap(pipe, vbuf, transfer);
362
363 cso_save_vertex_elements($self->cso);
364
365 /* tell pipe about the vertex attributes */
366 for (i = 0; i < num_attribs; i++) {
367 velements[i].src_offset = i * 4 * sizeof(float);
368 velements[i].instance_divisor = 0;
369 velements[i].vertex_buffer_index = 0;
370 velements[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
371 }
372 cso_set_vertex_elements($self->cso, num_attribs, velements);
373
374 /* tell pipe about the vertex buffer */
375 memset(&vbuffer, 0, sizeof(vbuffer));
376 vbuffer.buffer = vbuf;
377 vbuffer.stride = num_attribs * 4 * sizeof(float); /* vertex size */
378 vbuffer.buffer_offset = 0;
379 vbuffer.max_index = num_verts - 1;
380 pipe->set_vertex_buffers(pipe, 1, &vbuffer);
381
382 /* draw */
383 pipe->draw_arrays(pipe, prim, 0, num_verts);
384
385 cso_restore_vertex_elements($self->cso);
386
387 error2:
388 pipe_resource_reference(&vbuf, NULL);
389 error1:
390 ;
391 }
392
393 void
394 clear(unsigned buffers, const float *rgba, double depth = 0.0f,
395 unsigned stencil = 0)
396 {
397 $self->pipe->clear($self->pipe, buffers, rgba, depth, stencil);
398 }
399
400 void
401 flush(unsigned flags = 0) {
402 struct pipe_fence_handle *fence = NULL;
403 $self->pipe->flush($self->pipe, flags | PIPE_FLUSH_RENDER_CACHE, &fence);
404 if(fence) {
405 /* TODO: allow asynchronous operation */
406 $self->pipe->screen->fence_finish( $self->pipe->screen, fence, 0 );
407 $self->pipe->screen->fence_reference( $self->pipe->screen, &fence, NULL );
408 }
409 }
410
411 /*
412 * Surface functions
413 */
414
415 void surface_copy(struct st_surface *dst,
416 unsigned destx, unsigned desty,
417 struct st_surface *src,
418 unsigned srcx, unsigned srcy,
419 unsigned width, unsigned height)
420 {
421 struct pipe_surface *_dst = NULL;
422 struct pipe_surface *_src = NULL;
423
424 _dst = st_pipe_surface(dst, PIPE_BIND_BLIT_DESTINATION);
425 if(!_dst)
426 SWIG_exception(SWIG_ValueError, "couldn't acquire destination surface for writing");
427
428 _src = st_pipe_surface(src, PIPE_BIND_BLIT_SOURCE);
429 if(!_src)
430 SWIG_exception(SWIG_ValueError, "couldn't acquire source surface for reading");
431
432 $self->pipe->surface_copy($self->pipe, _dst, destx, desty, _src, srcx, srcy, width, height);
433
434 fail:
435 pipe_surface_reference(&_src, NULL);
436 pipe_surface_reference(&_dst, NULL);
437 }
438
439 void surface_fill(struct st_surface *dst,
440 unsigned x, unsigned y,
441 unsigned width, unsigned height,
442 unsigned value)
443 {
444 struct pipe_surface *_dst = NULL;
445
446 _dst = st_pipe_surface(dst, PIPE_BIND_BLIT_DESTINATION);
447 if(!_dst)
448 SWIG_exception(SWIG_ValueError, "couldn't acquire destination surface for writing");
449
450 $self->pipe->surface_fill($self->pipe, _dst, x, y, width, height, value);
451
452 fail:
453 pipe_surface_reference(&_dst, NULL);
454 }
455
456 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
457 void
458 surface_read_raw(struct st_surface *surface,
459 unsigned x, unsigned y, unsigned w, unsigned h,
460 char **STRING, int *LENGTH)
461 {
462 struct pipe_resource *texture = surface->texture;
463 struct pipe_context *pipe = $self->pipe;
464 struct pipe_transfer *transfer;
465 unsigned stride;
466
467 stride = util_format_get_stride(texture->format, w);
468 *LENGTH = util_format_get_nblocksy(texture->format, h) * stride;
469 *STRING = (char *) malloc(*LENGTH);
470 if(!*STRING)
471 return;
472
473 transfer = pipe_get_transfer(pipe,
474 surface->texture,
475 surface->face,
476 surface->level,
477 surface->zslice,
478 PIPE_TRANSFER_READ,
479 x, y, w, h);
480 if(transfer) {
481 pipe_get_tile_raw(pipe, transfer, 0, 0, w, h, *STRING, stride);
482 pipe->transfer_destroy(pipe, transfer);
483 }
484 }
485
486 %cstring_input_binary(const char *STRING, unsigned LENGTH);
487 void
488 surface_write_raw(struct st_surface *surface,
489 unsigned x, unsigned y, unsigned w, unsigned h,
490 const char *STRING, unsigned LENGTH, unsigned stride = 0)
491 {
492 struct pipe_resource *texture = surface->texture;
493 struct pipe_context *pipe = $self->pipe;
494 struct pipe_transfer *transfer;
495
496 if(stride == 0)
497 stride = util_format_get_stride(texture->format, w);
498
499 if(LENGTH < util_format_get_nblocksy(texture->format, h) * stride)
500 SWIG_exception(SWIG_ValueError, "offset must be smaller than buffer size");
501
502 transfer = pipe_get_transfer(pipe,
503 surface->texture,
504 surface->face,
505 surface->level,
506 surface->zslice,
507 PIPE_TRANSFER_WRITE,
508 x, y, w, h);
509 if(!transfer)
510 SWIG_exception(SWIG_MemoryError, "couldn't initiate transfer");
511
512 pipe_put_tile_raw(pipe, transfer, 0, 0, w, h, STRING, stride);
513 pipe->transfer_destroy(pipe, transfer);
514
515 fail:
516 return;
517 }
518
519 void
520 surface_read_rgba(struct st_surface *surface,
521 unsigned x, unsigned y, unsigned w, unsigned h,
522 float *rgba)
523 {
524 struct pipe_context *pipe = $self->pipe;
525 struct pipe_transfer *transfer;
526 transfer = pipe_get_transfer(pipe,
527 surface->texture,
528 surface->face,
529 surface->level,
530 surface->zslice,
531 PIPE_TRANSFER_READ,
532 x, y, w, h);
533 if(transfer) {
534 pipe_get_tile_rgba(pipe, transfer, 0, 0, w, h, rgba);
535 pipe->transfer_destroy(pipe, transfer);
536 }
537 }
538
539 void
540 surface_write_rgba(struct st_surface *surface,
541 unsigned x, unsigned y, unsigned w, unsigned h,
542 const float *rgba)
543 {
544 struct pipe_context *pipe = $self->pipe;
545 struct pipe_transfer *transfer;
546 transfer = pipe_get_transfer(pipe,
547 surface->texture,
548 surface->face,
549 surface->level,
550 surface->zslice,
551 PIPE_TRANSFER_WRITE,
552 x, y, w, h);
553 if(transfer) {
554 pipe_put_tile_rgba(pipe, transfer, 0, 0, w, h, rgba);
555 pipe->transfer_destroy(pipe, transfer);
556 }
557 }
558
559 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
560 void
561 surface_read_rgba8(struct st_surface *surface,
562 unsigned x, unsigned y, unsigned w, unsigned h,
563 char **STRING, int *LENGTH)
564 {
565 struct pipe_context *pipe = $self->pipe;
566 struct pipe_transfer *transfer;
567 float *rgba;
568 unsigned char *rgba8;
569 unsigned i, j, k;
570
571 *LENGTH = 0;
572 *STRING = NULL;
573
574 if (!surface)
575 return;
576
577 *LENGTH = h*w*4;
578 *STRING = (char *) malloc(*LENGTH);
579 if(!*STRING)
580 return;
581
582 rgba = malloc(h*w*4*sizeof(float));
583 if(!rgba)
584 return;
585
586 rgba8 = (unsigned char *) *STRING;
587
588 transfer = pipe_get_transfer(pipe,
589 surface->texture,
590 surface->face,
591 surface->level,
592 surface->zslice,
593 PIPE_TRANSFER_READ,
594 x, y, w, h);
595 if(transfer) {
596 pipe_get_tile_rgba(pipe, transfer, 0, 0, w, h, rgba);
597 for(j = 0; j < h; ++j) {
598 for(i = 0; i < w; ++i)
599 for(k = 0; k <4; ++k)
600 rgba8[j*w*4 + i*4 + k] = float_to_ubyte(rgba[j*w*4 + i*4 + k]);
601 }
602 pipe->transfer_destroy(pipe, transfer);
603 }
604
605 free(rgba);
606 }
607
608 void
609 surface_read_z(struct st_surface *surface,
610 unsigned x, unsigned y, unsigned w, unsigned h,
611 unsigned *z)
612 {
613 struct pipe_context *pipe = $self->pipe;
614 struct pipe_transfer *transfer;
615 transfer = pipe_get_transfer(pipe,
616 surface->texture,
617 surface->face,
618 surface->level,
619 surface->zslice,
620 PIPE_TRANSFER_READ,
621 x, y, w, h);
622 if(transfer) {
623 pipe_get_tile_z(pipe, transfer, 0, 0, w, h, z);
624 pipe->transfer_destroy(pipe, transfer);
625 }
626 }
627
628 void
629 surface_write_z(struct st_surface *surface,
630 unsigned x, unsigned y, unsigned w, unsigned h,
631 const unsigned *z)
632 {
633 struct pipe_context *pipe = $self->pipe;
634 struct pipe_transfer *transfer;
635 transfer = pipe_get_transfer(pipe,
636 surface->texture,
637 surface->face,
638 surface->level,
639 surface->zslice,
640 PIPE_TRANSFER_WRITE,
641 x, y, w, h);
642 if(transfer) {
643 pipe_put_tile_z(pipe, transfer, 0, 0, w, h, z);
644 pipe->transfer_destroy(pipe, transfer);
645 }
646 }
647
648 void
649 surface_sample_rgba(struct st_surface *surface,
650 float *rgba,
651 int norm = 0)
652 {
653 st_sample_surface($self->pipe, surface, rgba, norm != 0);
654 }
655
656 unsigned
657 surface_compare_rgba(struct st_surface *surface,
658 unsigned x, unsigned y, unsigned w, unsigned h,
659 const float *rgba, float tol = 0.0)
660 {
661 struct pipe_context *pipe = $self->pipe;
662 struct pipe_transfer *transfer;
663 float *rgba2;
664 const float *p1;
665 const float *p2;
666 unsigned i, j, n;
667
668 rgba2 = MALLOC(h*w*4*sizeof(float));
669 if(!rgba2)
670 return ~0;
671
672 transfer = pipe_get_transfer(pipe,
673 surface->texture,
674 surface->face,
675 surface->level,
676 surface->zslice,
677 PIPE_TRANSFER_READ,
678 x, y, w, h);
679 if(!transfer) {
680 FREE(rgba2);
681 return ~0;
682 }
683
684 pipe_get_tile_rgba(pipe, transfer, 0, 0, w, h, rgba2);
685 pipe->transfer_destroy(pipe, transfer);
686
687 p1 = rgba;
688 p2 = rgba2;
689 n = 0;
690 for(i = h*w; i; --i) {
691 unsigned differs = 0;
692 for(j = 4; j; --j) {
693 float delta = *p2++ - *p1++;
694 if (delta < -tol || delta > tol)
695 differs = 1;
696 }
697 n += differs;
698 }
699
700 FREE(rgba2);
701
702 return n;
703 }
704
705 %cstring_input_binary(const char *STRING, unsigned LENGTH);
706 void
707 transfer_inline_write(struct pipe_resource *resource,
708 struct pipe_subresource *sr,
709 unsigned usage,
710 const struct pipe_box *box,
711 const char *STRING, unsigned LENGTH,
712 unsigned stride,
713 unsigned slice_stride)
714 {
715 struct pipe_context *pipe = $self->pipe;
716
717 pipe->transfer_inline_write(pipe, resource, *sr, usage, box, STRING, stride, slice_stride);
718 }
719
720 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
721 void buffer_read(struct pipe_resource *buffer,
722 char **STRING, int *LENGTH)
723 {
724 struct pipe_context *pipe = $self->pipe;
725
726 assert(buffer->target == PIPE_BUFFER);
727
728 *LENGTH = buffer->width0;
729 *STRING = (char *) malloc(buffer->width0);
730 if(!*STRING)
731 return;
732
733 pipe_buffer_read(pipe, buffer, 0, buffer->width0, *STRING);
734 }
735
736 void buffer_write(struct pipe_resource *buffer,
737 const char *STRING, unsigned LENGTH, unsigned offset = 0)
738 {
739 struct pipe_context *pipe = $self->pipe;
740
741 assert(buffer->target == PIPE_BUFFER);
742
743 if(offset > buffer->width0)
744 SWIG_exception(SWIG_ValueError, "offset must be smaller than buffer size");
745
746 if(offset + LENGTH > buffer->width0)
747 SWIG_exception(SWIG_ValueError, "data length must fit inside the buffer");
748
749 pipe_buffer_write(pipe, buffer, offset, LENGTH, STRING);
750
751 fail:
752 return;
753 }
754
755 };