python: Read rgba8 with a single transfer.
[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(h*w*4*sizeof(float));
229 if(!rgba)
230 return;
231
232 rgba8 = (unsigned char *) *STRING;
233
234 transfer = screen->get_tex_transfer(screen,
235 $self->texture,
236 $self->face,
237 $self->level,
238 $self->zslice,
239 PIPE_TRANSFER_READ,
240 x, y,
241 w, h);
242 if(transfer) {
243 pipe_get_tile_rgba(transfer, 0, 0, w, h, rgba);
244 for(j = 0; j < h; ++j) {
245 for(i = 0; i < w; ++i)
246 for(k = 0; k <4; ++k)
247 rgba8[j*w*4 + i*4 + k] = float_to_ubyte(rgba[j*w*4 + i*4 + k]);
248 }
249 screen->tex_transfer_destroy(transfer);
250 }
251
252 free(rgba);
253 }
254
255 void
256 get_tile_z(unsigned x, unsigned y, unsigned w, unsigned h, unsigned *z)
257 {
258 struct pipe_screen *screen = $self->texture->screen;
259 struct pipe_transfer *transfer;
260 transfer = screen->get_tex_transfer(screen,
261 $self->texture,
262 $self->face,
263 $self->level,
264 $self->zslice,
265 PIPE_TRANSFER_READ,
266 x, y, w, h);
267 if(transfer) {
268 pipe_get_tile_z(transfer, 0, 0, w, h, z);
269 screen->tex_transfer_destroy(transfer);
270 }
271 }
272
273 void
274 put_tile_z(unsigned x, unsigned y, unsigned w, unsigned h, const unsigned *z)
275 {
276 struct pipe_screen *screen = $self->texture->screen;
277 struct pipe_transfer *transfer;
278 transfer = screen->get_tex_transfer(screen,
279 $self->texture,
280 $self->face,
281 $self->level,
282 $self->zslice,
283 PIPE_TRANSFER_WRITE,
284 x, y, w, h);
285 if(transfer) {
286 pipe_put_tile_z(transfer, 0, 0, w, h, z);
287 screen->tex_transfer_destroy(transfer);
288 }
289 }
290
291 void
292 sample_rgba(float *rgba) {
293 st_sample_surface($self, rgba);
294 }
295
296 unsigned
297 compare_tile_rgba(unsigned x, unsigned y, unsigned w, unsigned h, const float *rgba, float tol = 0.0)
298 {
299 struct pipe_screen *screen = $self->texture->screen;
300 struct pipe_transfer *transfer;
301 float *rgba2;
302 const float *p1;
303 const float *p2;
304 unsigned i, j, n;
305
306 rgba2 = MALLOC(h*w*4*sizeof(float));
307 if(!rgba2)
308 return ~0;
309
310 transfer = screen->get_tex_transfer(screen,
311 $self->texture,
312 $self->face,
313 $self->level,
314 $self->zslice,
315 PIPE_TRANSFER_READ,
316 x, y, w, h);
317 if(!transfer) {
318 FREE(rgba2);
319 return ~0;
320 }
321
322 pipe_get_tile_rgba(transfer, 0, 0, w, h, rgba2);
323 screen->tex_transfer_destroy(transfer);
324
325 p1 = rgba;
326 p2 = rgba2;
327 n = 0;
328 for(i = h*w; i; --i) {
329 unsigned differs = 0;
330 for(j = 4; j; --j) {
331 float delta = *p2++ - *p1++;
332 if (delta < -tol || delta > tol)
333 differs = 1;
334 }
335 n += differs;
336 }
337
338 FREE(rgba2);
339
340 return n;
341 }
342
343 };
344
345 %{
346 static enum pipe_format
347 st_surface_format_get(struct st_surface *surface)
348 {
349 return surface->texture->format;
350 }
351
352 static unsigned
353 st_surface_width_get(struct st_surface *surface)
354 {
355 return surface->texture->width[surface->level];
356 }
357
358 static unsigned
359 st_surface_height_get(struct st_surface *surface)
360 {
361 return surface->texture->height[surface->level];
362 }
363 %}
364
365 /* Avoid naming conflict with p_inlines.h's pipe_buffer_read/write */
366 %rename(read) pipe_buffer_read_;
367 %rename(write) pipe_buffer_write_;
368
369 %extend pipe_buffer {
370
371 ~pipe_buffer() {
372 struct pipe_buffer *ptr = $self;
373 pipe_buffer_reference(&ptr, NULL);
374 }
375
376 unsigned __len__(void)
377 {
378 assert(p_atomic_read(&$self->reference.count) > 0);
379 return $self->size;
380 }
381
382 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
383 void read_(char **STRING, int *LENGTH)
384 {
385 struct pipe_screen *screen = $self->screen;
386
387 assert(p_atomic_read(&$self->reference.count) > 0);
388
389 *LENGTH = $self->size;
390 *STRING = (char *) malloc($self->size);
391 if(!*STRING)
392 return;
393
394 pipe_buffer_read(screen, $self, 0, $self->size, STRING);
395 }
396
397 %cstring_input_binary(const char *STRING, unsigned LENGTH);
398 void write_(const char *STRING, unsigned LENGTH, unsigned offset = 0)
399 {
400 struct pipe_screen *screen = $self->screen;
401
402 assert(p_atomic_read(&$self->reference.count) > 0);
403
404 if(offset > $self->size)
405 SWIG_exception(SWIG_ValueError, "offset must be smaller than buffer size");
406
407 if(offset + LENGTH > $self->size)
408 SWIG_exception(SWIG_ValueError, "data length must fit inside the buffer");
409
410 pipe_buffer_write(screen, $self, offset, LENGTH, STRING);
411
412 fail:
413 return;
414 }
415 };