swrast: simplify assertion to silence warning
[mesa.git] / src / gallium / state_trackers / python / p_texture.i
index 9396522961e62669c7a9979e57f888f35d518061..1f4b4fd596bf1ca4e6228d1037f0d77885939185 100644 (file)
  */
 
 
-%nodefaultctor pipe_texture;
-%nodefaultctor pipe_surface;
-%nodefaultctor st_buffer;
+%nodefaultctor pipe_resource;
+%nodefaultctor st_surface;
 
-%nodefaultdtor pipe_texture;
-%nodefaultdtor pipe_surface;
-%nodefaultdtor st_buffer;
+%nodefaultdtor pipe_resource;
+%nodefaultdtor st_surface;
 
-%ignore pipe_texture::screen;
+%ignore pipe_resource::screen;
 
-%ignore pipe_surface::winsys;
-%immutable pipe_surface::texture;
-%immutable pipe_surface::buffer;
+%immutable st_surface::texture;
+%immutable st_surface::level;
+%immutable st_surface::layer;
 
-%newobject pipe_texture::get_surface;
+%newobject pipe_resource::get_surface;
 
+/* Avoid naming conflict with p_inlines.h's pipe_buffer_read/write */ 
+%rename(read) read_; 
+%rename(write) write_; 
 
-%extend pipe_texture {
-   
-   ~pipe_texture() {
-      struct pipe_texture *ptr = $self;
-      pipe_texture_reference(&ptr, NULL);
+%extend pipe_resource {
+
+   ~pipe_resource() {
+      struct pipe_resource *ptr = $self;
+      pipe_resource_reference(&ptr, NULL);
    }
-   
+
    unsigned get_width(unsigned level=0) {
-      return $self->width[level];
+      return u_minify($self->width0, level);
    }
-   
+
    unsigned get_height(unsigned level=0) {
-      return $self->height[level];
+      return u_minify($self->height0, level);
    }
-   
+
    unsigned get_depth(unsigned level=0) {
-      return $self->depth[level];
-   }
-   
-   unsigned get_nblocksx(unsigned level=0) {
-      return $self->nblocksx[level];
-   }
-   
-   unsigned get_nblocksy(unsigned level=0) {
-      return $self->nblocksy[level];
+      return u_minify($self->depth0, level);
    }
-   
+
    /** Get a surface which is a "view" into a texture */
-   struct pipe_surface *
-   get_surface(unsigned face=0, unsigned level=0, unsigned zslice=0, unsigned usage=0 )
+   struct st_surface *
+   get_surface(unsigned level=0, unsigned layer=0)
    {
-      struct pipe_screen *screen = $self->screen;
-      return screen->get_tex_surface(screen, $self, face, level, zslice, usage);
-   }
-   
-};
-
-
-%extend pipe_surface {
-   
-   ~pipe_surface() {
-      struct pipe_surface *ptr = $self;
-      pipe_surface_reference(&ptr, NULL);
-   }
-   
-   // gets mapped to pipe_surface_map automatically
-   void * map( unsigned flags );
+      struct st_surface *surface;
 
-   // gets mapped to pipe_surface_unmap automatically
-   void unmap( void );
+      if(level > $self->last_level)
+         SWIG_exception(SWIG_ValueError, "level out of bounds");
+      if(layer >= ($self->target == PIPE_TEXTURE_3D ?
+                         u_minify($self->depth0, level) : $self->depth0))
+         SWIG_exception(SWIG_ValueError, "layer out of bounds");
 
-   void
-   get_tile_raw(unsigned x, unsigned y, unsigned w, unsigned h, char *raw, unsigned stride) {
-      pipe_get_tile_raw($self, x, y, w, h, raw, stride);
-   }
+      surface = CALLOC_STRUCT(st_surface);
+      if(!surface)
+         return NULL;
 
-   void
-   put_tile_raw(unsigned x, unsigned y, unsigned w, unsigned h, const char *raw, unsigned stride) {
-      pipe_put_tile_raw($self, x, y, w, h, raw, stride);
-   }
+      pipe_resource_reference(&surface->texture, $self);
+      surface->level = level;
+      surface->layer = layer;
 
-   void
-   get_tile_rgba(unsigned x, unsigned y, unsigned w, unsigned h, float *rgba) {
-      pipe_get_tile_rgba($self, x, y, w, h, rgba);
-   }
+      return surface;
 
-   void
-   put_tile_rgba(unsigned x, unsigned y, unsigned w, unsigned h, const float *rgba) {
-      pipe_put_tile_rgba($self, x, y, w, h, rgba);
+   fail:
+      return NULL;
    }
 
-   %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
-   void
-   get_tile_rgba8(unsigned x, unsigned y, unsigned w, unsigned h, char **STRING, int *LENGTH) 
+   unsigned __len__(void) 
    {
-      unsigned surface_usage;
-      float *rgba;
-      unsigned char *rgba8;
-      unsigned i, j, k;
+      assert($self->target == PIPE_BUFFER);
+      assert(p_atomic_read(&$self->reference.count) > 0);
+      return $self->width0;
+   }
 
-      *LENGTH = 0;
-      *STRING = NULL;
-      
-      if (!$self)
-         return;
+};
 
-      *LENGTH = h*w*4;
-      *STRING = (char *) malloc(*LENGTH);
-      if(!*STRING)
-         return;
-      
-      rgba = malloc(w*4*sizeof(float));
-      if(!rgba)
-         return;
-      
-      rgba8 = (unsigned char *) *STRING;
+struct st_surface
+{
+   %immutable;
 
-      /* XXX: force mappable surface */
-      surface_usage = $self->usage;
-      $self->usage |= PIPE_BUFFER_USAGE_CPU_READ;
+   struct pipe_resource *texture;
+   unsigned level;
+   unsigned layer;
 
-      for(j = 0; j < h; ++j) {
-         pipe_get_tile_rgba($self,
-                            x, y + j, w, 1,
-                            rgba);
-         for(i = 0; i < w; ++i)
-            for(k = 0; k <4; ++k)
-               rgba8[j*w*4 + i*4 + k] = float_to_ubyte(rgba[i*4 + k]);
-      }
-      
-      $self->usage = surface_usage;
-      
-      free(rgba);
-   }
+};
 
-   void
-   get_tile_z(unsigned x, unsigned y, unsigned w, unsigned h, unsigned *z) {
-      pipe_get_tile_z($self, x, y, w, h, z);
-   }
+%extend st_surface {
 
-   void
-   put_tile_z(unsigned x, unsigned y, unsigned w, unsigned h, const unsigned *z) {
-      pipe_put_tile_z($self, x, y, w, h, z);
-   }
-   
-   void
-   sample_rgba(float *rgba) {
-      st_sample_surface($self, rgba);
-   }
-   
-   unsigned
-   compare_tile_rgba(unsigned x, unsigned y, unsigned w, unsigned h, const float *rgba, float tol = 0.0) 
-   {
-      float *rgba2;
-      const float *p1;
-      const float *p2;
-      unsigned i, j, n;
-      
-      rgba2 = MALLOC(h*w*4*sizeof(float));
-      if(!rgba2)
-         return ~0;
+   %immutable;
 
-      pipe_get_tile_rgba($self, x, y, w, h, rgba2);
+   unsigned format;
+   unsigned width;
+   unsigned height;
 
-      p1 = rgba;
-      p2 = rgba2;
-      n = 0;
-      for(i = h*w; i; --i) {
-         unsigned differs = 0;
-         for(j = 4; j; --j) {
-            float delta = *p2++ - *p1++;
-            if (delta < -tol || delta > tol)
-                differs = 1;
-         }
-         n += differs;
-      }
-      
-      FREE(rgba2);
-      
-      return n;
+   ~st_surface() {
+      pipe_resource_reference(&$self->texture, NULL);
+      FREE($self);
    }
 
-};
 
-struct st_buffer {
 };
 
-%extend st_buffer {
-   
-   ~st_buffer() {
-      st_buffer_destroy($self);
-   }
-   
-   unsigned __len__(void) 
+%{
+   static enum pipe_format
+   st_surface_format_get(struct st_surface *surface)
    {
-      assert($self->buffer->refcount);
-      return $self->buffer->size;
+      return surface->texture->format;
    }
    
-   %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
-   void read(char **STRING, int *LENGTH)
+   static unsigned
+   st_surface_width_get(struct st_surface *surface)
    {
-      struct pipe_screen *screen = $self->st_dev->screen;
-      const char *map;
-      
-      assert($self->buffer->refcount);
-      
-      *LENGTH = $self->buffer->size;
-      *STRING = (char *) malloc($self->buffer->size);
-      if(!*STRING)
-         return;
-      
-      map = pipe_buffer_map(screen, $self->buffer, PIPE_BUFFER_USAGE_CPU_READ);
-      if(map) {
-         memcpy(*STRING, map, $self->buffer->size);
-         pipe_buffer_unmap(screen, $self->buffer);
-      }
+      return u_minify(surface->texture->width0, surface->level);
    }
    
-   %cstring_input_binary(const char *STRING, unsigned LENGTH);
-   void write(const char *STRING, unsigned LENGTH, unsigned offset = 0) 
+   static unsigned
+   st_surface_height_get(struct st_surface *surface)
    {
-      struct pipe_screen *screen = $self->st_dev->screen;
-      char *map;
-      
-      assert($self->buffer->refcount);
-      
-      if(offset > $self->buffer->size) {
-         PyErr_SetString(PyExc_ValueError, "offset must be smaller than buffer size");
-         return;
-      }
-
-      if(offset + LENGTH > $self->buffer->size) {
-         PyErr_SetString(PyExc_ValueError, "data length must fit inside the buffer");
-         return;
-      }
-
-      map = pipe_buffer_map(screen, $self->buffer, PIPE_BUFFER_USAGE_CPU_WRITE);
-      if(map) {
-         memcpy(map + offset, STRING, LENGTH);
-         pipe_buffer_unmap(screen, $self->buffer);
-      }
+      return u_minify(surface->texture->height0, surface->level);
    }
-};
+%}