Merge commit 'origin/master' into gallium-0.2
[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 pipe_surface;
38 %nodefaultctor st_buffer;
39
40 %nodefaultdtor pipe_texture;
41 %nodefaultdtor pipe_surface;
42 %nodefaultdtor st_buffer;
43
44 %ignore pipe_texture::screen;
45
46 %ignore pipe_surface::winsys;
47 %immutable pipe_surface::texture;
48 %immutable pipe_surface::buffer;
49
50 %newobject pipe_texture::get_surface;
51
52
53 %extend pipe_texture {
54
55 ~pipe_texture() {
56 struct pipe_texture *ptr = $self;
57 pipe_texture_reference(&ptr, NULL);
58 }
59
60 unsigned get_width(unsigned level=0) {
61 return $self->width[level];
62 }
63
64 unsigned get_height(unsigned level=0) {
65 return $self->height[level];
66 }
67
68 unsigned get_depth(unsigned level=0) {
69 return $self->depth[level];
70 }
71
72 unsigned get_nblocksx(unsigned level=0) {
73 return $self->nblocksx[level];
74 }
75
76 unsigned get_nblocksy(unsigned level=0) {
77 return $self->nblocksy[level];
78 }
79
80 /** Get a surface which is a "view" into a texture */
81 struct pipe_surface *
82 get_surface(unsigned face=0, unsigned level=0, unsigned zslice=0, unsigned usage=0 )
83 {
84 struct pipe_screen *screen = $self->screen;
85 return screen->get_tex_surface(screen, $self, face, level, zslice, usage);
86 }
87
88 };
89
90
91 %extend pipe_surface {
92
93 ~pipe_surface() {
94 struct pipe_surface *ptr = $self;
95 pipe_surface_reference(&ptr, NULL);
96 }
97
98 // gets mapped to pipe_surface_map automatically
99 void * map( unsigned flags );
100
101 // gets mapped to pipe_surface_unmap automatically
102 void unmap( void );
103
104 void
105 get_tile_raw(unsigned x, unsigned y, unsigned w, unsigned h, char *raw, unsigned stride) {
106 pipe_get_tile_raw($self, x, y, w, h, raw, stride);
107 }
108
109 void
110 put_tile_raw(unsigned x, unsigned y, unsigned w, unsigned h, const char *raw, unsigned stride) {
111 pipe_put_tile_raw($self, x, y, w, h, raw, stride);
112 }
113
114 void
115 get_tile_rgba(unsigned x, unsigned y, unsigned w, unsigned h, float *rgba) {
116 pipe_get_tile_rgba($self, x, y, w, h, rgba);
117 }
118
119 void
120 put_tile_rgba(unsigned x, unsigned y, unsigned w, unsigned h, const float *rgba) {
121 pipe_put_tile_rgba($self, x, y, w, h, rgba);
122 }
123
124 void
125 get_tile_z(unsigned x, unsigned y, unsigned w, unsigned h, unsigned *z) {
126 pipe_get_tile_z($self, x, y, w, h, z);
127 }
128
129 void
130 put_tile_z(unsigned x, unsigned y, unsigned w, unsigned h, const unsigned *z) {
131 pipe_put_tile_z($self, x, y, w, h, z);
132 }
133
134 void
135 sample_rgba(float *rgba) {
136 st_sample_surface($self, rgba);
137 }
138
139 unsigned
140 compare_tile_rgba(unsigned x, unsigned y, unsigned w, unsigned h, const float *rgba, float tol = 0.0)
141 {
142 float *rgba2;
143 const float *p1;
144 const float *p2;
145 unsigned i, j, n;
146
147 rgba2 = MALLOC(h*w*4*sizeof(float));
148 if(!rgba2)
149 return ~0;
150
151 pipe_get_tile_rgba($self, x, y, w, h, rgba2);
152
153 p1 = rgba;
154 p2 = rgba2;
155 n = 0;
156 for(i = h*w; i; --i) {
157 unsigned differs = 0;
158 for(j = 4; j; --j) {
159 float delta = *p2++ - *p1++;
160 if (delta < -tol || delta > tol)
161 differs = 1;
162 }
163 n += differs;
164 }
165
166 FREE(rgba2);
167
168 return n;
169 }
170
171 };
172
173 struct st_buffer {
174 };
175
176 %extend st_buffer {
177
178 ~st_buffer() {
179 st_buffer_destroy($self);
180 }
181
182 unsigned __len__(void)
183 {
184 assert($self->buffer->refcount);
185 return $self->buffer->size;
186 }
187
188 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
189 void read(char **STRING, int *LENGTH)
190 {
191 struct pipe_screen *screen = $self->st_dev->screen;
192 const char *map;
193
194 assert($self->buffer->refcount);
195
196 *LENGTH = $self->buffer->size;
197 *STRING = (char *) malloc($self->buffer->size);
198 if(!*STRING)
199 return;
200
201 map = pipe_buffer_map(screen, $self->buffer, PIPE_BUFFER_USAGE_CPU_READ);
202 if(map) {
203 memcpy(*STRING, map, $self->buffer->size);
204 pipe_buffer_unmap(screen, $self->buffer);
205 }
206 }
207
208 %cstring_input_binary(const char *STRING, unsigned LENGTH);
209 void write(const char *STRING, unsigned LENGTH, unsigned offset = 0)
210 {
211 struct pipe_screen *screen = $self->st_dev->screen;
212 char *map;
213
214 assert($self->buffer->refcount);
215
216 if(offset > $self->buffer->size) {
217 PyErr_SetString(PyExc_ValueError, "offset must be smaller than buffer size");
218 return;
219 }
220
221 if(offset + LENGTH > $self->buffer->size) {
222 PyErr_SetString(PyExc_ValueError, "data length must fit inside the buffer");
223 return;
224 }
225
226 map = pipe_buffer_map(screen, $self->buffer, PIPE_BUFFER_USAGE_CPU_WRITE);
227 if(map) {
228 memcpy(map + offset, STRING, LENGTH);
229 pipe_buffer_unmap(screen, $self->buffer);
230 }
231 }
232 };