st/python: Move surface read/write methods to context.
[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 /*
131 * Parameter-like state (or properties)
132 */
133
134 void set_blend_color(const struct pipe_blend_color *state ) {
135 cso_set_blend_color($self->cso, state);
136 }
137
138 void set_stencil_ref(const struct pipe_stencil_ref *state ) {
139 cso_set_stencil_ref($self->cso, state);
140 }
141
142 void set_clip(const struct pipe_clip_state *state ) {
143 $self->pipe->set_clip_state($self->pipe, state);
144 }
145
146 void set_constant_buffer(unsigned shader, unsigned index,
147 struct pipe_buffer *buffer )
148 {
149 $self->pipe->set_constant_buffer($self->pipe, shader, index, buffer);
150 }
151
152 void set_framebuffer(const struct pipe_framebuffer_state *state )
153 {
154 memcpy(&$self->framebuffer, state, sizeof *state);
155 cso_set_framebuffer($self->cso, state);
156 }
157
158 void set_polygon_stipple(const struct pipe_poly_stipple *state ) {
159 $self->pipe->set_polygon_stipple($self->pipe, state);
160 }
161
162 void set_scissor(const struct pipe_scissor_state *state ) {
163 $self->pipe->set_scissor_state($self->pipe, state);
164 }
165
166 void set_viewport(const struct pipe_viewport_state *state) {
167 cso_set_viewport($self->cso, state);
168 }
169
170 void set_fragment_sampler_texture(unsigned index,
171 struct pipe_texture *texture) {
172 struct pipe_sampler_view templ;
173
174 if(!texture)
175 texture = $self->default_texture;
176 pipe_sampler_view_reference(&$self->fragment_sampler_views[index], NULL);
177 u_sampler_view_default_template(&templ,
178 texture,
179 texture->format);
180 $self->fragment_sampler_views[index] = $self->pipe->create_sampler_view($self->pipe,
181 texture,
182 &templ);
183 $self->pipe->set_fragment_sampler_views($self->pipe,
184 PIPE_MAX_SAMPLERS,
185 $self->fragment_sampler_views);
186 }
187
188 void set_vertex_sampler_texture(unsigned index,
189 struct pipe_texture *texture) {
190 struct pipe_sampler_view templ;
191
192 if(!texture)
193 texture = $self->default_texture;
194 pipe_sampler_view_reference(&$self->vertex_sampler_views[index], NULL);
195 u_sampler_view_default_template(&templ,
196 texture,
197 texture->format);
198 $self->vertex_sampler_views[index] = $self->pipe->create_sampler_view($self->pipe,
199 texture,
200 &templ);
201
202 $self->pipe->set_vertex_sampler_views($self->pipe,
203 PIPE_MAX_VERTEX_SAMPLERS,
204 $self->vertex_sampler_views);
205 }
206
207 void set_vertex_buffer(unsigned index,
208 unsigned stride,
209 unsigned max_index,
210 unsigned buffer_offset,
211 struct pipe_buffer *buffer)
212 {
213 unsigned i;
214 struct pipe_vertex_buffer state;
215
216 memset(&state, 0, sizeof(state));
217 state.stride = stride;
218 state.max_index = max_index;
219 state.buffer_offset = buffer_offset;
220 state.buffer = buffer;
221
222 memcpy(&$self->vertex_buffers[index], &state, sizeof(state));
223
224 for(i = 0; i < PIPE_MAX_ATTRIBS; ++i)
225 if(self->vertex_buffers[i].buffer)
226 $self->num_vertex_buffers = i + 1;
227
228 $self->pipe->set_vertex_buffers($self->pipe,
229 $self->num_vertex_buffers,
230 $self->vertex_buffers);
231 }
232
233 void set_vertex_element(unsigned index,
234 const struct pipe_vertex_element *element)
235 {
236 memcpy(&$self->vertex_elements[index], element, sizeof(*element));
237 }
238
239 void set_vertex_elements(unsigned num)
240 {
241 $self->num_vertex_elements = num;
242 cso_set_vertex_elements($self->cso,
243 $self->num_vertex_elements,
244 $self->vertex_elements);
245 }
246
247 /*
248 * Draw functions
249 */
250
251 void draw_arrays(unsigned mode, unsigned start, unsigned count) {
252 $self->pipe->draw_arrays($self->pipe, mode, start, count);
253 }
254
255 void draw_elements( struct pipe_buffer *indexBuffer,
256 unsigned indexSize,
257 unsigned mode, unsigned start, unsigned count)
258 {
259 $self->pipe->draw_elements($self->pipe,
260 indexBuffer,
261 indexSize,
262 mode, start, count);
263 }
264
265 void draw_range_elements( struct pipe_buffer *indexBuffer,
266 unsigned indexSize, unsigned minIndex, unsigned maxIndex,
267 unsigned mode, unsigned start, unsigned count)
268 {
269 $self->pipe->draw_range_elements($self->pipe,
270 indexBuffer,
271 indexSize, minIndex, maxIndex,
272 mode, start, count);
273 }
274
275 void draw_vertices(unsigned prim,
276 unsigned num_verts,
277 unsigned num_attribs,
278 const float *vertices)
279 {
280 struct pipe_context *pipe = $self->pipe;
281 struct pipe_screen *screen = pipe->screen;
282 struct pipe_buffer *vbuf;
283 float *map;
284 unsigned size;
285
286 size = num_verts * num_attribs * 4 * sizeof(float);
287
288 vbuf = pipe_buffer_create(screen,
289 32,
290 PIPE_BUFFER_USAGE_VERTEX,
291 size);
292 if(!vbuf)
293 goto error1;
294
295 map = pipe_buffer_map(screen, vbuf, PIPE_BUFFER_USAGE_CPU_WRITE);
296 if (!map)
297 goto error2;
298 memcpy(map, vertices, size);
299 pipe_buffer_unmap(screen, vbuf);
300
301 util_draw_vertex_buffer(pipe, vbuf, 0, prim, num_verts, num_attribs);
302
303 error2:
304 pipe_buffer_reference(&vbuf, NULL);
305 error1:
306 ;
307 }
308
309 void
310 flush(unsigned flags = 0) {
311 struct pipe_fence_handle *fence = NULL;
312 $self->pipe->flush($self->pipe, flags | PIPE_FLUSH_RENDER_CACHE, &fence);
313 if(fence) {
314 /* TODO: allow asynchronous operation */
315 $self->pipe->screen->fence_finish( $self->pipe->screen, fence, 0 );
316 $self->pipe->screen->fence_reference( $self->pipe->screen, &fence, NULL );
317 }
318 }
319
320 /*
321 * Surface functions
322 */
323
324 void surface_copy(struct st_surface *dst,
325 unsigned destx, unsigned desty,
326 struct st_surface *src,
327 unsigned srcx, unsigned srcy,
328 unsigned width, unsigned height)
329 {
330 struct pipe_surface *_dst = NULL;
331 struct pipe_surface *_src = NULL;
332
333 _dst = st_pipe_surface(dst, PIPE_BUFFER_USAGE_GPU_WRITE);
334 if(!_dst)
335 SWIG_exception(SWIG_ValueError, "couldn't acquire destination surface for writing");
336
337 _src = st_pipe_surface(src, PIPE_BUFFER_USAGE_GPU_READ);
338 if(!_src)
339 SWIG_exception(SWIG_ValueError, "couldn't acquire source surface for reading");
340
341 $self->pipe->surface_copy($self->pipe, _dst, destx, desty, _src, srcx, srcy, width, height);
342
343 fail:
344 pipe_surface_reference(&_src, NULL);
345 pipe_surface_reference(&_dst, NULL);
346 }
347
348 void surface_fill(struct st_surface *dst,
349 unsigned x, unsigned y,
350 unsigned width, unsigned height,
351 unsigned value)
352 {
353 struct pipe_surface *_dst = NULL;
354
355 _dst = st_pipe_surface(dst, PIPE_BUFFER_USAGE_GPU_WRITE);
356 if(!_dst)
357 SWIG_exception(SWIG_ValueError, "couldn't acquire destination surface for writing");
358
359 $self->pipe->surface_fill($self->pipe, _dst, x, y, width, height, value);
360
361 fail:
362 pipe_surface_reference(&_dst, NULL);
363 }
364
365 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
366 void
367 surface_read_raw(struct st_surface *surface,
368 unsigned x, unsigned y, unsigned w, unsigned h,
369 char **STRING, int *LENGTH)
370 {
371 struct pipe_texture *texture = surface->texture;
372 struct pipe_context *pipe = $self->pipe;
373 struct pipe_transfer *transfer;
374 unsigned stride;
375
376 stride = util_format_get_stride(texture->format, w);
377 *LENGTH = util_format_get_nblocksy(texture->format, h) * stride;
378 *STRING = (char *) malloc(*LENGTH);
379 if(!*STRING)
380 return;
381
382 transfer = pipe->get_tex_transfer(pipe,
383 surface->texture,
384 surface->face,
385 surface->level,
386 surface->zslice,
387 PIPE_TRANSFER_READ,
388 x, y, w, h);
389 if(transfer) {
390 pipe_get_tile_raw(pipe, transfer, 0, 0, w, h, *STRING, stride);
391 pipe->tex_transfer_destroy(pipe, transfer);
392 }
393 }
394
395 %cstring_input_binary(const char *STRING, unsigned LENGTH);
396 void
397 surface_write_raw(struct st_surface *surface,
398 unsigned x, unsigned y, unsigned w, unsigned h,
399 const char *STRING, unsigned LENGTH, unsigned stride = 0)
400 {
401 struct pipe_texture *texture = surface->texture;
402 struct pipe_context *pipe = $self->pipe;
403 struct pipe_transfer *transfer;
404
405 if(stride == 0)
406 stride = util_format_get_stride(texture->format, w);
407
408 if(LENGTH < util_format_get_nblocksy(texture->format, h) * stride)
409 SWIG_exception(SWIG_ValueError, "offset must be smaller than buffer size");
410
411 transfer = pipe->get_tex_transfer(pipe,
412 surface->texture,
413 surface->face,
414 surface->level,
415 surface->zslice,
416 PIPE_TRANSFER_WRITE,
417 x, y, w, h);
418 if(!transfer)
419 SWIG_exception(SWIG_MemoryError, "couldn't initiate transfer");
420
421 pipe_put_tile_raw(pipe, transfer, 0, 0, w, h, STRING, stride);
422 pipe->tex_transfer_destroy(pipe, transfer);
423
424 fail:
425 return;
426 }
427
428 void
429 surface_read_rgba(struct st_surface *surface,
430 unsigned x, unsigned y, unsigned w, unsigned h,
431 float *rgba)
432 {
433 struct pipe_context *pipe = $self->pipe;
434 struct pipe_transfer *transfer;
435 transfer = pipe->get_tex_transfer(pipe,
436 surface->texture,
437 surface->face,
438 surface->level,
439 surface->zslice,
440 PIPE_TRANSFER_READ,
441 x, y, w, h);
442 if(transfer) {
443 pipe_get_tile_rgba(pipe, transfer, 0, 0, w, h, rgba);
444 pipe->tex_transfer_destroy(pipe, transfer);
445 }
446 }
447
448 void
449 surface_write_rgba(struct st_surface *surface,
450 unsigned x, unsigned y, unsigned w, unsigned h,
451 const float *rgba)
452 {
453 struct pipe_context *pipe = $self->pipe;
454 struct pipe_transfer *transfer;
455 transfer = pipe->get_tex_transfer(pipe,
456 surface->texture,
457 surface->face,
458 surface->level,
459 surface->zslice,
460 PIPE_TRANSFER_WRITE,
461 x, y, w, h);
462 if(transfer) {
463 pipe_put_tile_rgba(pipe, transfer, 0, 0, w, h, rgba);
464 pipe->tex_transfer_destroy(pipe, transfer);
465 }
466 }
467
468 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
469 void
470 surface_read_rgba8(struct st_surface *surface,
471 unsigned x, unsigned y, unsigned w, unsigned h,
472 char **STRING, int *LENGTH)
473 {
474 struct pipe_context *pipe = $self->pipe;
475 struct pipe_transfer *transfer;
476 float *rgba;
477 unsigned char *rgba8;
478 unsigned i, j, k;
479
480 *LENGTH = 0;
481 *STRING = NULL;
482
483 if (!surface)
484 return;
485
486 *LENGTH = h*w*4;
487 *STRING = (char *) malloc(*LENGTH);
488 if(!*STRING)
489 return;
490
491 rgba = malloc(h*w*4*sizeof(float));
492 if(!rgba)
493 return;
494
495 rgba8 = (unsigned char *) *STRING;
496
497 transfer = pipe->get_tex_transfer(pipe,
498 surface->texture,
499 surface->face,
500 surface->level,
501 surface->zslice,
502 PIPE_TRANSFER_READ,
503 x, y,
504 w, h);
505 if(transfer) {
506 pipe_get_tile_rgba(pipe, transfer, 0, 0, w, h, rgba);
507 for(j = 0; j < h; ++j) {
508 for(i = 0; i < w; ++i)
509 for(k = 0; k <4; ++k)
510 rgba8[j*w*4 + i*4 + k] = float_to_ubyte(rgba[j*w*4 + i*4 + k]);
511 }
512 pipe->tex_transfer_destroy(pipe, transfer);
513 }
514
515 free(rgba);
516 }
517
518 void
519 surface_read_z(struct st_surface *surface,
520 unsigned x, unsigned y, unsigned w, unsigned h,
521 unsigned *z)
522 {
523 struct pipe_context *pipe = $self->pipe;
524 struct pipe_transfer *transfer;
525 transfer = pipe->get_tex_transfer(pipe,
526 surface->texture,
527 surface->face,
528 surface->level,
529 surface->zslice,
530 PIPE_TRANSFER_READ,
531 x, y, w, h);
532 if(transfer) {
533 pipe_get_tile_z(pipe, transfer, 0, 0, w, h, z);
534 pipe->tex_transfer_destroy(pipe, transfer);
535 }
536 }
537
538 void
539 surface_write_z(struct st_surface *surface,
540 unsigned x, unsigned y, unsigned w, unsigned h,
541 const unsigned *z)
542 {
543 struct pipe_context *pipe = $self->pipe;
544 struct pipe_transfer *transfer;
545 transfer = pipe->get_tex_transfer(pipe,
546 surface->texture,
547 surface->face,
548 surface->level,
549 surface->zslice,
550 PIPE_TRANSFER_WRITE,
551 x, y, w, h);
552 if(transfer) {
553 pipe_put_tile_z(pipe, transfer, 0, 0, w, h, z);
554 pipe->tex_transfer_destroy(pipe, transfer);
555 }
556 }
557
558 void
559 surface_sample_rgba(struct st_surface *surface,
560 float *rgba)
561 {
562 st_sample_surface($self->pipe, surface, rgba);
563 }
564
565 unsigned
566 surface_compare_rgba(struct st_surface *surface,
567 unsigned x, unsigned y, unsigned w, unsigned h,
568 const float *rgba, float tol = 0.0)
569 {
570 struct pipe_context *pipe = $self->pipe;
571 struct pipe_transfer *transfer;
572 float *rgba2;
573 const float *p1;
574 const float *p2;
575 unsigned i, j, n;
576
577 rgba2 = MALLOC(h*w*4*sizeof(float));
578 if(!rgba2)
579 return ~0;
580
581 transfer = pipe->get_tex_transfer(pipe,
582 surface->texture,
583 surface->face,
584 surface->level,
585 surface->zslice,
586 PIPE_TRANSFER_READ,
587 x, y, w, h);
588 if(!transfer) {
589 FREE(rgba2);
590 return ~0;
591 }
592
593 pipe_get_tile_rgba(pipe, transfer, 0, 0, w, h, rgba2);
594 pipe->tex_transfer_destroy(pipe, transfer);
595
596 p1 = rgba;
597 p2 = rgba2;
598 n = 0;
599 for(i = h*w; i; --i) {
600 unsigned differs = 0;
601 for(j = 4; j; --j) {
602 float delta = *p2++ - *p1++;
603 if (delta < -tol || delta > tol)
604 differs = 1;
605 }
606 n += differs;
607 }
608
609 FREE(rgba2);
610
611 return n;
612 }
613
614 };