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