gallium: Create OGL state tracker wrappers for various CPU access operations.
[mesa.git] / src / mesa / state_tracker / st_cb_fbo.c
index 3560a040c859a53af5fc68df6bdc27acee93766b..1590f275e2a3c435d090b3cc2a612b6b6ba1a0f5 100644 (file)
@@ -41,8 +41,7 @@
 
 #include "pipe/p_context.h"
 #include "pipe/p_defines.h"
-#include "pipe/p_inlines.h"
-#include "pipe/p_winsys.h"
+#include "pipe/p_screen.h"
 #include "st_context.h"
 #include "st_cb_fbo.h"
 #include "st_cb_texture.h"
@@ -92,32 +91,27 @@ st_renderbuffer_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
    struct pipe_texture template;
    unsigned surface_usage;
 
-   /* Free the old surface (and texture if we hold the last
-    * reference):
+   /* Free the old surface and texture
     */
    pipe_surface_reference( &strb->surface, NULL );
+   pipe_texture_reference( &strb->texture, NULL );
 
+   /* Setup new texture template.
+    */
    memset(&template, 0, sizeof(template));
-
+   template.target = PIPE_TEXTURE_2D;
    if (strb->format != PIPE_FORMAT_NONE) {
       template.format = strb->format;
    }
    else {
       template.format = st_choose_renderbuffer_format(pipe, internalFormat);
    }
-
-   strb->Base.Width  = width;
-   strb->Base.Height = height;
-   init_renderbuffer_bits(strb, template.format);
-
-   template.target = PIPE_TEXTURE_2D;
-   template.compressed = 0;
-   template.cpp = pf_get_size(template.format);
+   pf_get_block(template.format, &template.block);
    template.width[0] = width;
    template.height[0] = height;
    template.depth[0] = 1;
    template.last_level = 0;
-
+   template.nr_samples = rb->NumSamples;
    if (pf_is_depth_stencil(template.format)) {
       template.tex_usage = PIPE_TEXTURE_USAGE_DEPTH_STENCIL;
    }
@@ -126,6 +120,10 @@ st_renderbuffer_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
                             PIPE_TEXTURE_USAGE_RENDER_TARGET);
    }
 
+   /* init renderbuffer fields */
+   strb->Base.Width  = width;
+   strb->Base.Height = height;
+   init_renderbuffer_bits(strb, template.format);
 
    /* Probably need dedicated flags for surface usage too: 
     */
@@ -168,12 +166,10 @@ st_renderbuffer_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
                                                   0, 0, 0,
                                                   surface_usage );
 
-   assert(strb->surface->buffer);
+   assert(strb->surface->texture);
    assert(strb->surface->format);
-   assert(strb->surface->cpp);
    assert(strb->surface->width == width);
    assert(strb->surface->height == height);
-   assert(strb->surface->pitch);
 
 
    return strb->surface != NULL;
@@ -190,7 +186,7 @@ st_renderbuffer_delete(struct gl_renderbuffer *rb)
    ASSERT(strb);
    pipe_surface_reference(&strb->surface, NULL);
    pipe_texture_reference(&strb->texture, NULL);
-   free(strb);
+   _mesa_free(strb);
 }
 
 
@@ -228,7 +224,7 @@ st_new_framebuffer(GLcontext *ctx, GLuint name)
 static struct gl_renderbuffer *
 st_new_renderbuffer(GLcontext *ctx, GLuint name)
 {
-   struct st_renderbuffer *strb = CALLOC_STRUCT(st_renderbuffer);
+   struct st_renderbuffer *strb = ST_CALLOC_STRUCT(st_renderbuffer);
    if (strb) {
       _mesa_init_renderbuffer(&strb->Base, name);
       strb->Base.Delete = st_renderbuffer_delete;
@@ -246,11 +242,11 @@ st_new_renderbuffer(GLcontext *ctx, GLuint name)
  * renderbuffer).  The window system code determines the format.
  */
 struct gl_renderbuffer *
-st_new_renderbuffer_fb(enum pipe_format format)
+st_new_renderbuffer_fb(enum pipe_format format, int samples)
 {
    struct st_renderbuffer *strb;
 
-   strb = CALLOC_STRUCT(st_renderbuffer);
+   strb = ST_CALLOC_STRUCT(st_renderbuffer);
    if (!strb) {
       _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
       return NULL;
@@ -258,6 +254,7 @@ st_new_renderbuffer_fb(enum pipe_format format)
 
    _mesa_init_renderbuffer(&strb->Base, 0);
    strb->Base.ClassID = 0x4242; /* just a unique value */
+   strb->Base.NumSamples = samples;
    strb->format = format;
 
    switch (format) {
@@ -346,18 +343,15 @@ st_render_texture(GLcontext *ctx,
                   struct gl_framebuffer *fb,
                   struct gl_renderbuffer_attachment *att)
 {
-   struct st_context *st = ctx->st;
    struct st_renderbuffer *strb;
    struct gl_renderbuffer *rb;
-   struct pipe_context *pipe = st->pipe;
-   struct pipe_screen *screen = pipe->screen;
-   struct pipe_texture *pt;
+   struct pipe_texture *pt = st_get_texobj_texture(att->Texture);
    struct st_texture_object *stObj;
    const struct gl_texture_image *texImage =
       att->Texture->Image[att->CubeMapFace][att->TextureLevel];
 
-
-   assert(!att->Renderbuffer);
+   if (!pt) 
+      return;
 
    /* create new renderbuffer which wraps the texture image */
    rb = st_new_renderbuffer(ctx, 0);
@@ -384,8 +378,6 @@ st_render_texture(GLcontext *ctx,
    rb->Height = texImage->Height2;
    /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
 
-   pt = st_get_texobj_texture(att->Texture);
-   assert(pt);
    /*printf("***** pipe texture %d x %d\n", pt->width[0], pt->height[0]);*/
 
    pipe_texture_reference( &strb->texture, pt );
@@ -417,15 +409,15 @@ static void
 st_finish_render_texture(GLcontext *ctx,
                          struct gl_renderbuffer_attachment *att)
 {
-   struct pipe_screen *screen = ctx->st->pipe->screen;
    struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer);
 
-   assert(strb);
+   if (!strb)
+      return;
 
-   ctx->st->pipe->flush(ctx->st->pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
+   st_flush( ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL );
 
    if (strb->surface)
-      screen->tex_surface_release( screen, &strb->surface );
+      pipe_surface_reference( &strb->surface, NULL );
 
    strb->rtt = NULL;
 
@@ -440,6 +432,25 @@ st_finish_render_texture(GLcontext *ctx,
 }
 
 
+/**
+ * Check that the framebuffer configuration is valid in terms of what
+ * the driver can support.
+ *
+ * For Gallium we only supports combined Z+stencil, not separate buffers.
+ */
+static void
+st_validate_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
+{
+   const struct gl_renderbuffer *depthRb =
+      fb->Attachment[BUFFER_DEPTH].Renderbuffer;
+   const struct gl_renderbuffer *stencilRb =
+      fb->Attachment[BUFFER_STENCIL].Renderbuffer;
+
+   if (stencilRb && depthRb && stencilRb != depthRb) {
+      fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
+   }
+}
+
 
 void st_init_fbo_functions(struct dd_function_table *functions)
 {
@@ -449,6 +460,7 @@ void st_init_fbo_functions(struct dd_function_table *functions)
    functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer;
    functions->RenderTexture = st_render_texture;
    functions->FinishRenderTexture = st_finish_render_texture;
+   functions->ValidateFramebuffer = st_validate_framebuffer;
    /* no longer needed by core Mesa, drivers handle resizes...
    functions->ResizeBuffers = st_resize_buffers;
    */