923a6285289cd6adc9b6a935434175804ff0ee9c
[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
128 };
129
130 %{
131 static enum pipe_format
132 st_surface_format_get(struct st_surface *surface)
133 {
134 return surface->texture->format;
135 }
136
137 static unsigned
138 st_surface_width_get(struct st_surface *surface)
139 {
140 return u_minify(surface->texture->width0, surface->level);
141 }
142
143 static unsigned
144 st_surface_height_get(struct st_surface *surface)
145 {
146 return u_minify(surface->texture->height0, surface->level);
147 }
148 %}
149
150 /* Avoid naming conflict with p_inlines.h's pipe_buffer_read/write */
151 %rename(read) read_;
152 %rename(write) write_;
153
154 %extend pipe_buffer {
155
156 ~pipe_buffer() {
157 struct pipe_buffer *ptr = $self;
158 pipe_buffer_reference(&ptr, NULL);
159 }
160
161 unsigned __len__(void)
162 {
163 assert(p_atomic_read(&$self->reference.count) > 0);
164 return $self->size;
165 }
166
167 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
168 void read_(char **STRING, int *LENGTH)
169 {
170 struct pipe_screen *screen = $self->screen;
171
172 assert(p_atomic_read(&$self->reference.count) > 0);
173
174 *LENGTH = $self->size;
175 *STRING = (char *) malloc($self->size);
176 if(!*STRING)
177 return;
178
179 pipe_buffer_read(screen, $self, 0, $self->size, *STRING);
180 }
181
182 %cstring_input_binary(const char *STRING, unsigned LENGTH);
183 void write_(const char *STRING, unsigned LENGTH, unsigned offset = 0)
184 {
185 struct pipe_screen *screen = $self->screen;
186
187 assert(p_atomic_read(&$self->reference.count) > 0);
188
189 if(offset > $self->size)
190 SWIG_exception(SWIG_ValueError, "offset must be smaller than buffer size");
191
192 if(offset + LENGTH > $self->size)
193 SWIG_exception(SWIG_ValueError, "data length must fit inside the buffer");
194
195 pipe_buffer_write(screen, $self, offset, LENGTH, STRING);
196
197 fail:
198 return;
199 }
200 };