python: Make get/put_tile_raw more user friendlier.
[mesa.git] / src / gallium / state_trackers / python / p_texture.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
36 %nodefaultctor pipe_texture;
37 %nodefaultctor st_surface;
38 %nodefaultctor pipe_buffer;
39
40 %nodefaultdtor pipe_texture;
41 %nodefaultdtor st_surface;
42 %nodefaultdtor pipe_buffer;
43
44 %ignore pipe_texture::screen;
45
46 %immutable st_surface::texture;
47 %immutable st_surface::face;
48 %immutable st_surface::level;
49 %immutable st_surface::zslice;
50
51 %newobject pipe_texture::get_surface;
52
53
54 %extend pipe_texture {
55
56 ~pipe_texture() {
57 struct pipe_texture *ptr = $self;
58 pipe_texture_reference(&ptr, NULL);
59 }
60
61 unsigned get_width(unsigned level=0) {
62 return $self->width[level];
63 }
64
65 unsigned get_height(unsigned level=0) {
66 return $self->height[level];
67 }
68
69 unsigned get_depth(unsigned level=0) {
70 return $self->depth[level];
71 }
72
73 unsigned get_nblocksx(unsigned level=0) {
74 return $self->nblocksx[level];
75 }
76
77 unsigned get_nblocksy(unsigned level=0) {
78 return $self->nblocksy[level];
79 }
80
81 /** Get a surface which is a "view" into a texture */
82 struct st_surface *
83 get_surface(unsigned face=0, unsigned level=0, unsigned zslice=0)
84 {
85 struct st_surface *surface;
86
87 if(face >= ($self->target == PIPE_TEXTURE_CUBE ? 6U : 1U))
88 SWIG_exception(SWIG_ValueError, "face out of bounds");
89 if(level > $self->last_level)
90 SWIG_exception(SWIG_ValueError, "level out of bounds");
91 if(zslice >= $self->depth[level])
92 SWIG_exception(SWIG_ValueError, "zslice out of bounds");
93
94 surface = CALLOC_STRUCT(st_surface);
95 if(!surface)
96 return NULL;
97
98 pipe_texture_reference(&surface->texture, $self);
99 surface->face = face;
100 surface->level = level;
101 surface->zslice = zslice;
102
103 return surface;
104
105 fail:
106 return NULL;
107 }
108
109 };
110
111 struct st_surface
112 {
113 %immutable;
114
115 struct pipe_texture *texture;
116 unsigned face;
117 unsigned level;
118 unsigned zslice;
119
120 };
121
122 %extend st_surface {
123
124 %immutable;
125
126 unsigned format;
127 unsigned width;
128 unsigned height;
129 unsigned nblocksx;
130 unsigned nblocksy;
131
132 ~st_surface() {
133 pipe_texture_reference(&$self->texture, NULL);
134 FREE($self);
135 }
136
137 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
138 void get_tile_raw(unsigned x, unsigned y, unsigned w, unsigned h, char **STRING, int *LENGTH)
139 {
140 struct pipe_texture *texture = $self->texture;
141 struct pipe_screen *screen = texture->screen;
142 struct pipe_transfer *transfer;
143 unsigned stride;
144
145 stride = pf_get_nblocksx(&texture->block, w) * texture->block.size;
146 *LENGTH = pf_get_nblocksy(&texture->block, h) * stride;
147 *STRING = (char *) malloc(*LENGTH);
148 if(!*STRING)
149 return;
150
151 transfer = screen->get_tex_transfer(screen,
152 $self->texture,
153 $self->face,
154 $self->level,
155 $self->zslice,
156 PIPE_TRANSFER_READ,
157 x, y, w, h);
158 if(transfer) {
159 pipe_get_tile_raw(transfer, 0, 0, w, h, *STRING, stride);
160 screen->tex_transfer_destroy(transfer);
161 }
162 }
163
164 %cstring_input_binary(const char *STRING, unsigned LENGTH);
165 void put_tile_raw(unsigned x, unsigned y, unsigned w, unsigned h, const char *STRING, unsigned LENGTH, unsigned stride = 0)
166 {
167 struct pipe_texture *texture = $self->texture;
168 struct pipe_screen *screen = texture->screen;
169 struct pipe_transfer *transfer;
170
171 if(stride == 0)
172 stride = pf_get_nblocksx(&texture->block, w) * texture->block.size;
173
174 if(LENGTH < pf_get_nblocksy(&texture->block, h) * stride)
175 SWIG_exception(SWIG_ValueError, "offset must be smaller than buffer size");
176
177 transfer = screen->get_tex_transfer(screen,
178 $self->texture,
179 $self->face,
180 $self->level,
181 $self->zslice,
182 PIPE_TRANSFER_WRITE,
183 x, y, w, h);
184 if(!transfer)
185 SWIG_exception(SWIG_MemoryError, "couldn't initiate transfer");
186
187 pipe_put_tile_raw(transfer, 0, 0, w, h, STRING, stride);
188 screen->tex_transfer_destroy(transfer);
189
190 fail:
191 return;
192 }
193
194 void
195 get_tile_rgba(unsigned x, unsigned y, unsigned w, unsigned h, float *rgba)
196 {
197 struct pipe_screen *screen = $self->texture->screen;
198 struct pipe_transfer *transfer;
199 transfer = screen->get_tex_transfer(screen,
200 $self->texture,
201 $self->face,
202 $self->level,
203 $self->zslice,
204 PIPE_TRANSFER_READ,
205 x, y, w, h);
206 if(transfer) {
207 pipe_get_tile_rgba(transfer, 0, 0, w, h, rgba);
208 screen->tex_transfer_destroy(transfer);
209 }
210 }
211
212 void
213 put_tile_rgba(unsigned x, unsigned y, unsigned w, unsigned h, const float *rgba)
214 {
215 struct pipe_screen *screen = $self->texture->screen;
216 struct pipe_transfer *transfer;
217 transfer = screen->get_tex_transfer(screen,
218 $self->texture,
219 $self->face,
220 $self->level,
221 $self->zslice,
222 PIPE_TRANSFER_WRITE,
223 x, y, w, h);
224 if(transfer) {
225 pipe_put_tile_rgba(transfer, 0, 0, w, h, rgba);
226 screen->tex_transfer_destroy(transfer);
227 }
228 }
229
230 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
231 void
232 get_tile_rgba8(unsigned x, unsigned y, unsigned w, unsigned h, char **STRING, int *LENGTH)
233 {
234 struct pipe_screen *screen = $self->texture->screen;
235 struct pipe_transfer *transfer;
236 float *rgba;
237 unsigned char *rgba8;
238 unsigned i, j, k;
239
240 *LENGTH = 0;
241 *STRING = NULL;
242
243 if (!$self)
244 return;
245
246 *LENGTH = h*w*4;
247 *STRING = (char *) malloc(*LENGTH);
248 if(!*STRING)
249 return;
250
251 rgba = malloc(h*w*4*sizeof(float));
252 if(!rgba)
253 return;
254
255 rgba8 = (unsigned char *) *STRING;
256
257 transfer = screen->get_tex_transfer(screen,
258 $self->texture,
259 $self->face,
260 $self->level,
261 $self->zslice,
262 PIPE_TRANSFER_READ,
263 x, y,
264 w, h);
265 if(transfer) {
266 pipe_get_tile_rgba(transfer, 0, 0, w, h, rgba);
267 for(j = 0; j < h; ++j) {
268 for(i = 0; i < w; ++i)
269 for(k = 0; k <4; ++k)
270 rgba8[j*w*4 + i*4 + k] = float_to_ubyte(rgba[j*w*4 + i*4 + k]);
271 }
272 screen->tex_transfer_destroy(transfer);
273 }
274
275 free(rgba);
276 }
277
278 void
279 get_tile_z(unsigned x, unsigned y, unsigned w, unsigned h, unsigned *z)
280 {
281 struct pipe_screen *screen = $self->texture->screen;
282 struct pipe_transfer *transfer;
283 transfer = screen->get_tex_transfer(screen,
284 $self->texture,
285 $self->face,
286 $self->level,
287 $self->zslice,
288 PIPE_TRANSFER_READ,
289 x, y, w, h);
290 if(transfer) {
291 pipe_get_tile_z(transfer, 0, 0, w, h, z);
292 screen->tex_transfer_destroy(transfer);
293 }
294 }
295
296 void
297 put_tile_z(unsigned x, unsigned y, unsigned w, unsigned h, const unsigned *z)
298 {
299 struct pipe_screen *screen = $self->texture->screen;
300 struct pipe_transfer *transfer;
301 transfer = screen->get_tex_transfer(screen,
302 $self->texture,
303 $self->face,
304 $self->level,
305 $self->zslice,
306 PIPE_TRANSFER_WRITE,
307 x, y, w, h);
308 if(transfer) {
309 pipe_put_tile_z(transfer, 0, 0, w, h, z);
310 screen->tex_transfer_destroy(transfer);
311 }
312 }
313
314 void
315 sample_rgba(float *rgba) {
316 st_sample_surface($self, rgba);
317 }
318
319 unsigned
320 compare_tile_rgba(unsigned x, unsigned y, unsigned w, unsigned h, const float *rgba, float tol = 0.0)
321 {
322 struct pipe_screen *screen = $self->texture->screen;
323 struct pipe_transfer *transfer;
324 float *rgba2;
325 const float *p1;
326 const float *p2;
327 unsigned i, j, n;
328
329 rgba2 = MALLOC(h*w*4*sizeof(float));
330 if(!rgba2)
331 return ~0;
332
333 transfer = screen->get_tex_transfer(screen,
334 $self->texture,
335 $self->face,
336 $self->level,
337 $self->zslice,
338 PIPE_TRANSFER_READ,
339 x, y, w, h);
340 if(!transfer) {
341 FREE(rgba2);
342 return ~0;
343 }
344
345 pipe_get_tile_rgba(transfer, 0, 0, w, h, rgba2);
346 screen->tex_transfer_destroy(transfer);
347
348 p1 = rgba;
349 p2 = rgba2;
350 n = 0;
351 for(i = h*w; i; --i) {
352 unsigned differs = 0;
353 for(j = 4; j; --j) {
354 float delta = *p2++ - *p1++;
355 if (delta < -tol || delta > tol)
356 differs = 1;
357 }
358 n += differs;
359 }
360
361 FREE(rgba2);
362
363 return n;
364 }
365
366 };
367
368 %{
369 static enum pipe_format
370 st_surface_format_get(struct st_surface *surface)
371 {
372 return surface->texture->format;
373 }
374
375 static unsigned
376 st_surface_width_get(struct st_surface *surface)
377 {
378 return surface->texture->width[surface->level];
379 }
380
381 static unsigned
382 st_surface_height_get(struct st_surface *surface)
383 {
384 return surface->texture->height[surface->level];
385 }
386
387 static unsigned
388 st_surface_nblocksx_get(struct st_surface *surface)
389 {
390 return surface->texture->nblocksx[surface->level];
391 }
392
393 static unsigned
394 st_surface_nblocksy_get(struct st_surface *surface)
395 {
396 return surface->texture->nblocksy[surface->level];
397 }
398 %}
399
400 /* Avoid naming conflict with p_inlines.h's pipe_buffer_read/write */
401 %rename(read) pipe_buffer_read_;
402 %rename(write) pipe_buffer_write_;
403
404 %extend pipe_buffer {
405
406 ~pipe_buffer() {
407 struct pipe_buffer *ptr = $self;
408 pipe_buffer_reference(&ptr, NULL);
409 }
410
411 unsigned __len__(void)
412 {
413 assert(p_atomic_read(&$self->reference.count) > 0);
414 return $self->size;
415 }
416
417 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
418 void read_(char **STRING, int *LENGTH)
419 {
420 struct pipe_screen *screen = $self->screen;
421
422 assert(p_atomic_read(&$self->reference.count) > 0);
423
424 *LENGTH = $self->size;
425 *STRING = (char *) malloc($self->size);
426 if(!*STRING)
427 return;
428
429 pipe_buffer_read(screen, $self, 0, $self->size, STRING);
430 }
431
432 %cstring_input_binary(const char *STRING, unsigned LENGTH);
433 void write_(const char *STRING, unsigned LENGTH, unsigned offset = 0)
434 {
435 struct pipe_screen *screen = $self->screen;
436
437 assert(p_atomic_read(&$self->reference.count) > 0);
438
439 if(offset > $self->size)
440 SWIG_exception(SWIG_ValueError, "offset must be smaller than buffer size");
441
442 if(offset + LENGTH > $self->size)
443 SWIG_exception(SWIG_ValueError, "data length must fit inside the buffer");
444
445 pipe_buffer_write(screen, $self, offset, LENGTH, STRING);
446
447 fail:
448 return;
449 }
450 };