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