gallium: added st_bind/release_teximage() functions
authorBrian Paul <brian.paul@tungstengraphics.com>
Mon, 1 Sep 2008 19:10:00 +0000 (13:10 -0600)
committerBrian Paul <brian.paul@tungstengraphics.com>
Mon, 1 Sep 2008 19:10:54 +0000 (13:10 -0600)
src/mesa/state_tracker/st_cb_fbo.h
src/mesa/state_tracker/st_public.h
src/mesa/state_tracker/st_texture.c

index ff56001a4e14fa1f5fb4cf018fa654d479009c3b..44fa9fe9a4fd1f12e25d39be6027a981c78a4889 100644 (file)
@@ -47,6 +47,10 @@ struct st_renderbuffer
 
    struct st_texture_object *rtt;  /**< GL render to texture's texture */
    int rtt_level, rtt_face, rtt_slice;
+
+   /** Render to texture state */
+   struct pipe_texture *texture_save;
+   struct pipe_surface *surface_save;
 };
 
 
index ca4e9577b1bac1be0d49f650bd34da6ca0368e9c..5cfb2e41f24d9ce27cbd38952e5c3ff3dbc6d976 100644 (file)
 #define ST_SURFACE_BACK_RIGHT   3
 #define ST_SURFACE_DEPTH        8
 
+#define ST_TEXTURE_2D    0x2
+#define ST_TEXTURE_RGB   0x1
+#define ST_TEXTURE_RGBA  0x2
+
 
 struct st_context;
 struct st_framebuffer;
@@ -93,6 +97,15 @@ void st_notify_swapbuffers(struct st_framebuffer *stfb);
 void st_notify_swapbuffers_complete(struct st_framebuffer *stfb);
 
 
+/** Redirect rendering into stfb's surface to a texture image */
+int st_bind_teximage(struct st_framebuffer *stfb, uint surfIndex,
+                     int target, int format, int level);
+
+/** Undo surface-to-texture binding */
+int st_release_teximage(struct st_framebuffer *stfb, uint surfIndex,
+                        int target, int format, int level);
+
+
 /** Generic function type */
 typedef void (*st_proc)();
 
index 73cebff33f7f94040ddc77f5d2f216c2233b1ca2..a23c7fe7907b31d744bb198733cf27ba56eafa1c 100644 (file)
 
 #include "st_context.h"
 #include "st_format.h"
+#include "st_public.h"
 #include "st_texture.h"
+#include "st_cb_fbo.h"
 #include "main/enums.h"
+#include "main/teximage.h"
 
 #undef Elements  /* fix re-defined macro warning */
 
@@ -351,3 +354,92 @@ st_texture_image_copy(struct pipe_context *pipe,
       screen->tex_surface_release(screen, &dst_surface);
    }
 }
+
+
+/** Redirect rendering into stfb's surface to a texture image */
+int
+st_bind_teximage(struct st_framebuffer *stfb, uint surfIndex,
+                 int target, int format, int level)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   struct st_context *st = ctx->st;
+   struct pipe_context *pipe = st->pipe;
+   struct pipe_screen *screen = pipe->screen;
+   const GLuint unit = ctx->Texture.CurrentUnit;
+   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
+   struct gl_texture_object *texObj;
+   struct gl_texture_image *texImage;
+   struct st_texture_image *stImage;
+   struct st_renderbuffer *strb;
+   GLint face = 0, slice = 0;
+
+   assert(surfIndex <= ST_SURFACE_DEPTH);
+
+   strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
+
+   if (strb->texture_save || strb->surface_save) {
+      /* Error! */
+      return 0;
+   }
+
+   if (target == ST_TEXTURE_2D) {
+      texObj = texUnit->Current2D;
+      texImage = _mesa_get_tex_image(ctx, texObj, GL_TEXTURE_2D, level);
+      stImage = st_texture_image(texImage);
+   }
+   else {
+      /* unsupported target */
+      return 0;
+   }
+
+   st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
+
+   /* save the renderbuffer's surface/texture info */
+   pipe_texture_reference(&strb->texture_save, strb->texture);
+   pipe_surface_reference(&strb->surface_save, strb->surface);
+
+   /* plug in new surface/texture info */
+   pipe_texture_reference(&strb->texture, stImage->pt);
+   strb->surface = screen->get_tex_surface(screen, strb->texture,
+                                           face, level, slice,
+                                           (PIPE_BUFFER_USAGE_GPU_READ |
+                                            PIPE_BUFFER_USAGE_GPU_WRITE));
+
+   st->dirty.st |= ST_NEW_FRAMEBUFFER;
+
+   return 1;
+}
+
+
+/** Undo surface-to-texture binding */
+int
+st_release_teximage(struct st_framebuffer *stfb, uint surfIndex,
+                    int target, int format, int level)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   struct st_context *st = ctx->st;
+   struct pipe_context *pipe = st->pipe;
+   struct st_renderbuffer *strb;
+
+   assert(surfIndex <= ST_SURFACE_DEPTH);
+
+   strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
+
+   if (!strb->texture_save || !strb->surface_save) {
+      /* Error! */
+      return 0;
+   }
+
+   st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
+
+   /* free tex surface, restore original */
+   pipe_surface_reference(&strb->surface, strb->surface_save);
+   pipe_texture_reference(&strb->texture, strb->texture_save);
+
+   pipe_surface_reference(&strb->surface_save, NULL);
+   pipe_texture_reference(&strb->texture_save, NULL);
+
+   st->dirty.st |= ST_NEW_FRAMEBUFFER;
+
+   return 1;
+}