Merge branch 'gallium-conditional-rendering'
[mesa.git] / src / mesa / state_tracker / st_context.c
index e584a6ceeacda420cf0628b176f3755e6a772c4b..e4f18c842ca79e8472638ac48e2f72ef24984c76 100644 (file)
 #include "main/matrix.h"
 #include "main/buffers.h"
 #include "main/scissor.h"
+#include "main/viewport.h"
 #include "vbo/vbo.h"
 #include "shader/shader_api.h"
 #include "glapi/glapi.h"
 #include "st_public.h"
+#include "st_debug.h"
 #include "st_context.h"
 #include "st_cb_accum.h"
 #include "st_cb_bitmap.h"
 #include "st_cb_blit.h"
 #include "st_cb_bufferobjects.h"
 #include "st_cb_clear.h"
+#include "st_cb_condrender.h"
 #if FEATURE_drawpix
 #include "st_cb_drawpixels.h"
 #include "st_cb_rasterpos.h"
@@ -49,7 +52,6 @@
 #include "st_cb_drawtex.h"
 #endif
 #include "st_cb_fbo.h"
-#include "st_cb_get.h"
 #if FEATURE_feedback
 #include "st_cb_feedback.h"
 #endif
 #include "st_cb_texture.h"
 #include "st_cb_flush.h"
 #include "st_cb_strings.h"
+#include "st_cb_viewport.h"
 #include "st_atom.h"
 #include "st_draw.h"
 #include "st_extensions.h"
 #include "st_gen_mipmap.h"
 #include "st_program.h"
 #include "pipe/p_context.h"
-#include "pipe/p_inlines.h"
 #include "draw/draw_context.h"
 #include "cso_cache/cso_cache.h"
 #include "cso_cache/cso_context.h"
@@ -105,13 +107,16 @@ static struct st_context *
 st_create_context_priv( GLcontext *ctx, struct pipe_context *pipe )
 {
    uint i;
-   struct st_context *st = CALLOC_STRUCT( st_context );
+   struct st_context *st = ST_CALLOC_STRUCT( st_context );
    
    ctx->st = st;
 
    st->ctx = ctx;
    st->pipe = pipe;
 
+   /* XXX: this is one-off, per-screen init: */
+   st_debug_init();
+   
    /* state tracker needs the VBO module */
    _vbo_CreateContext(ctx);
 
@@ -176,6 +181,12 @@ struct st_context *st_create_context(struct pipe_context *pipe,
 
    ctx = _mesa_create_context(visual, shareCtx, &funcs, NULL);
 
+   /* XXX: need a capability bit in gallium to query if the pipe
+    * driver prefers DP4 or MUL/MAD for vertex transformation.
+    */
+   if (debug_get_bool_option("MESA_MVP_DP4", FALSE))
+      _mesa_set_mvp_with_dp4( ctx, GL_TRUE );
+
    return st_create_context_priv(ctx, pipe);
 }
 
@@ -208,7 +219,7 @@ static void st_destroy_context_priv( struct st_context *st )
 
    for (i = 0; i < Elements(st->state.constants); i++) {
       if (st->state.constants[i].buffer) {
-         pipe_buffer_reference(st->pipe->screen, &st->state.constants[i].buffer, NULL);
+         pipe_buffer_reference(&st->state.constants[i].buffer, NULL);
       }
    }
 
@@ -217,7 +228,7 @@ static void st_destroy_context_priv( struct st_context *st )
       st->default_texture = NULL;
    }
 
-   free( st );
+   _mesa_free( st );
 }
 
  
@@ -226,6 +237,7 @@ void st_destroy_context( struct st_context *st )
    struct pipe_context *pipe = st->pipe;
    struct cso_context *cso = st->cso_context;
    GLcontext *ctx = st->ctx;
+   GLuint i;
 
    /* need to unbind and destroy CSO objects before anything else */
    cso_release_all(st->cso_context);
@@ -233,6 +245,12 @@ void st_destroy_context( struct st_context *st )
    st_reference_fragprog(st, &st->fp, NULL);
    st_reference_vertprog(st, &st->vp, NULL);
 
+   /* release framebuffer surfaces */
+   for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
+      pipe_surface_reference(&st->state.framebuffer.cbufs[i], NULL);
+   }
+   pipe_surface_reference(&st->state.framebuffer.zsbuf, NULL);
+
    _mesa_delete_program_cache(st->ctx, st->pixel_xfer.cache);
 
    _vbo_DestroyContext(st->ctx);
@@ -245,34 +263,39 @@ void st_destroy_context( struct st_context *st )
 
    pipe->destroy( pipe );
 
-   free(ctx);
+   _mesa_free(ctx);
 }
 
 
-void st_make_current(struct st_context *st,
-                     struct st_framebuffer *draw,
-                     struct st_framebuffer *read)
+GLboolean
+st_make_current(struct st_context *st,
+                struct st_framebuffer *draw,
+                struct st_framebuffer *read)
 {
+   /* Call this periodically to detect when the user has begun using
+    * GL rendering from multiple threads.
+    */
+   _glapi_check_multithread();
+
    if (st) {
-      GLboolean firstTime = st->ctx->FirstTimeCurrent;
-      _mesa_make_current(st->ctx, &draw->Base, &read->Base);
-      /* Need to initialize viewport here since draw->Base->Width/Height
-       * will still be zero at this point.
-       * This could be improved, but would require rather extensive work
-       * elsewhere (allocate rb surface storage sooner)
-       */
-      if (firstTime) {
-         GLuint w = draw->InitWidth, h = draw->InitHeight;
-         _mesa_set_viewport(st->ctx, 0, 0, w, h);
-         _mesa_set_scissor(st->ctx, 0, 0, w, h);
+      if (!_mesa_make_current(st->ctx, &draw->Base, &read->Base))
+         return GL_FALSE;
 
-      }
+      _mesa_check_init_viewport(st->ctx, draw->InitWidth, draw->InitHeight);
+
+      return GL_TRUE;
    }
    else {
-      _mesa_make_current(NULL, NULL, NULL);
+      return _mesa_make_current(NULL, NULL, NULL);
    }
 }
 
+struct st_context *st_get_current(void)
+{
+   GET_CURRENT_CONTEXT(ctx);
+
+   return (ctx == NULL) ? NULL : ctx->st;
+}
 
 void st_copy_context_state(struct st_context *dst,
                            struct st_context *src,
@@ -308,18 +331,19 @@ void st_init_driver_functions(struct dd_function_table *functions)
    st_init_rasterpos_functions(functions);
 #endif
    st_init_fbo_functions(functions);
-   st_init_get_functions(functions);
 #if FEATURE_feedback
    st_init_feedback_functions(functions);
 #endif
    st_init_program_functions(functions);
-#if FEATURE_ARB_occlusion_query
+#if FEATURE_queryobj
    st_init_query_functions(functions);
 #endif
+   st_init_cond_render_functions(functions);
    st_init_readpixels_functions(functions);
    st_init_texture_functions(functions);
    st_init_flush_functions(functions);
    st_init_string_functions(functions);
+   st_init_viewport_functions(functions);
 
    functions->UpdateState = st_invalidate_state;
 }