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