python: Force unsigned comparison.
[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
130 ~st_surface() {
131 pipe_texture_reference(&$self->texture, NULL);
132 FREE($self);
133 }
134
135 void
136 get_tile_raw(unsigned x, unsigned y, unsigned w, unsigned h, char *raw, unsigned stride)
137 {
138 struct pipe_screen *screen = $self->texture->screen;
139 struct pipe_transfer *transfer;
140 transfer = screen->get_tex_transfer(screen,
141 $self->texture,
142 $self->face,
143 $self->level,
144 $self->zslice,
145 PIPE_TRANSFER_READ,
146 x, y, w, h);
147 if(transfer) {
148 pipe_get_tile_raw(transfer, 0, 0, w, h, raw, stride);
149 screen->tex_transfer_destroy(transfer);
150 }
151 }
152
153 void
154 put_tile_raw(unsigned x, unsigned y, unsigned w, unsigned h, const char *raw, unsigned stride)
155 {
156 struct pipe_screen *screen = $self->texture->screen;
157 struct pipe_transfer *transfer;
158 transfer = screen->get_tex_transfer(screen,
159 $self->texture,
160 $self->face,
161 $self->level,
162 $self->zslice,
163 PIPE_TRANSFER_WRITE,
164 x, y, w, h);
165 if(transfer) {
166 pipe_put_tile_raw(transfer, 0, 0, w, h, raw, stride);
167 screen->tex_transfer_destroy(transfer);
168 }
169 }
170
171 void
172 get_tile_rgba(unsigned x, unsigned y, unsigned w, unsigned h, float *rgba)
173 {
174 struct pipe_screen *screen = $self->texture->screen;
175 struct pipe_transfer *transfer;
176 transfer = screen->get_tex_transfer(screen,
177 $self->texture,
178 $self->face,
179 $self->level,
180 $self->zslice,
181 PIPE_TRANSFER_READ,
182 x, y, w, h);
183 if(transfer) {
184 pipe_get_tile_rgba(transfer, 0, 0, w, h, rgba);
185 screen->tex_transfer_destroy(transfer);
186 }
187 }
188
189 void
190 put_tile_rgba(unsigned x, unsigned y, unsigned w, unsigned h, const float *rgba)
191 {
192 struct pipe_screen *screen = $self->texture->screen;
193 struct pipe_transfer *transfer;
194 transfer = screen->get_tex_transfer(screen,
195 $self->texture,
196 $self->face,
197 $self->level,
198 $self->zslice,
199 PIPE_TRANSFER_WRITE,
200 x, y, w, h);
201 if(transfer) {
202 pipe_put_tile_rgba(transfer, 0, 0, w, h, rgba);
203 screen->tex_transfer_destroy(transfer);
204 }
205 }
206
207 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
208 void
209 get_tile_rgba8(unsigned x, unsigned y, unsigned w, unsigned h, char **STRING, int *LENGTH)
210 {
211 struct pipe_screen *screen = $self->texture->screen;
212 struct pipe_transfer *transfer;
213 float *rgba;
214 unsigned char *rgba8;
215 unsigned i, j, k;
216
217 *LENGTH = 0;
218 *STRING = NULL;
219
220 if (!$self)
221 return;
222
223 *LENGTH = h*w*4;
224 *STRING = (char *) malloc(*LENGTH);
225 if(!*STRING)
226 return;
227
228 rgba = malloc(w*4*sizeof(float));
229 if(!rgba)
230 return;
231
232 rgba8 = (unsigned char *) *STRING;
233
234 for(j = 0; j < h; ++j) {
235 transfer = screen->get_tex_transfer(screen,
236 $self->texture,
237 $self->face,
238 $self->level,
239 $self->zslice,
240 PIPE_TRANSFER_READ,
241 x, y + j,
242 w,
243 1);
244 if(transfer) {
245 pipe_get_tile_rgba(transfer,
246 0, 0, w, 1,
247 rgba);
248 for(i = 0; i < w; ++i)
249 for(k = 0; k <4; ++k)
250 rgba8[j*w*4 + i*4 + k] = float_to_ubyte(rgba[i*4 + k]);
251 screen->tex_transfer_destroy(transfer);
252 }
253 }
254
255 free(rgba);
256 }
257
258 void
259 get_tile_z(unsigned x, unsigned y, unsigned w, unsigned h, unsigned *z)
260 {
261 struct pipe_screen *screen = $self->texture->screen;
262 struct pipe_transfer *transfer;
263 transfer = screen->get_tex_transfer(screen,
264 $self->texture,
265 $self->face,
266 $self->level,
267 $self->zslice,
268 PIPE_TRANSFER_READ,
269 x, y, w, h);
270 if(transfer) {
271 pipe_get_tile_z(transfer, 0, 0, w, h, z);
272 screen->tex_transfer_destroy(transfer);
273 }
274 }
275
276 void
277 put_tile_z(unsigned x, unsigned y, unsigned w, unsigned h, const unsigned *z)
278 {
279 struct pipe_screen *screen = $self->texture->screen;
280 struct pipe_transfer *transfer;
281 transfer = screen->get_tex_transfer(screen,
282 $self->texture,
283 $self->face,
284 $self->level,
285 $self->zslice,
286 PIPE_TRANSFER_WRITE,
287 x, y, w, h);
288 if(transfer) {
289 pipe_put_tile_z(transfer, 0, 0, w, h, z);
290 screen->tex_transfer_destroy(transfer);
291 }
292 }
293
294 void
295 sample_rgba(float *rgba) {
296 st_sample_surface($self, rgba);
297 }
298
299 unsigned
300 compare_tile_rgba(unsigned x, unsigned y, unsigned w, unsigned h, const float *rgba, float tol = 0.0)
301 {
302 struct pipe_screen *screen = $self->texture->screen;
303 struct pipe_transfer *transfer;
304 float *rgba2;
305 const float *p1;
306 const float *p2;
307 unsigned i, j, n;
308
309 rgba2 = MALLOC(h*w*4*sizeof(float));
310 if(!rgba2)
311 return ~0;
312
313 transfer = screen->get_tex_transfer(screen,
314 $self->texture,
315 $self->face,
316 $self->level,
317 $self->zslice,
318 PIPE_TRANSFER_READ,
319 x, y, w, h);
320 if(!transfer) {
321 FREE(rgba2);
322 return ~0;
323 }
324
325 pipe_get_tile_rgba(transfer, 0, 0, w, h, rgba2);
326 screen->tex_transfer_destroy(transfer);
327
328 p1 = rgba;
329 p2 = rgba2;
330 n = 0;
331 for(i = h*w; i; --i) {
332 unsigned differs = 0;
333 for(j = 4; j; --j) {
334 float delta = *p2++ - *p1++;
335 if (delta < -tol || delta > tol)
336 differs = 1;
337 }
338 n += differs;
339 }
340
341 FREE(rgba2);
342
343 return n;
344 }
345
346 };
347
348 %{
349 static enum pipe_format
350 st_surface_format_get(struct st_surface *surface)
351 {
352 return surface->texture->format;
353 }
354
355 static unsigned
356 st_surface_width_get(struct st_surface *surface)
357 {
358 return surface->texture->width[surface->level];
359 }
360
361 static unsigned
362 st_surface_height_get(struct st_surface *surface)
363 {
364 return surface->texture->height[surface->level];
365 }
366 %}
367
368 /* Avoid naming conflict with p_inlines.h's pipe_buffer_read/write */
369 %rename(read) pipe_buffer_read_;
370 %rename(write) pipe_buffer_write_;
371
372 %extend pipe_buffer {
373
374 ~pipe_buffer() {
375 struct pipe_buffer *ptr = $self;
376 pipe_buffer_reference(&ptr, NULL);
377 }
378
379 unsigned __len__(void)
380 {
381 assert(p_atomic_read(&$self->reference.count) > 0);
382 return $self->size;
383 }
384
385 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
386 void read_(char **STRING, int *LENGTH)
387 {
388 struct pipe_screen *screen = $self->screen;
389
390 assert(p_atomic_read(&$self->reference.count) > 0);
391
392 *LENGTH = $self->size;
393 *STRING = (char *) malloc($self->size);
394 if(!*STRING)
395 return;
396
397 pipe_buffer_read(screen, $self, 0, $self->size, STRING);
398 }
399
400 %cstring_input_binary(const char *STRING, unsigned LENGTH);
401 void write_(const char *STRING, unsigned LENGTH, unsigned offset = 0)
402 {
403 struct pipe_screen *screen = $self->screen;
404
405 assert(p_atomic_read(&$self->reference.count) > 0);
406
407 if(offset > $self->size)
408 SWIG_exception(SWIG_ValueError, "offset must be smaller than buffer size");
409
410 if(offset + LENGTH > $self->size)
411 SWIG_exception(SWIG_ValueError, "data length must fit inside the buffer");
412
413 pipe_buffer_write(screen, $self, offset, LENGTH, STRING);
414
415 fail:
416 return;
417 }
418 };