st/mesa: do vertex and fragment color clamping in shaders
[mesa.git] / src / mesa / state_tracker / st_context.c
index d0dcdd4e29bdce5ae3776fe3820ac61ffa2865e9..a3fd4dbcb7f5ec36140b8c7e2dcd9f3c0c9c7a54 100644 (file)
  **************************************************************************/
 
 #include "main/imports.h"
+#include "main/accum.h"
 #include "main/context.h"
+#include "main/samplerobj.h"
 #include "main/shaderobj.h"
 #include "program/prog_cache.h"
 #include "vbo/vbo.h"
 #include "glapi/glapi.h"
 #include "st_context.h"
 #include "st_debug.h"
-#include "st_cb_accum.h"
 #include "st_cb_bitmap.h"
 #include "st_cb_blit.h"
 #include "st_cb_bufferobjects.h"
@@ -51,7 +52,9 @@
 #include "st_cb_texture.h"
 #include "st_cb_xformfb.h"
 #include "st_cb_flush.h"
+#include "st_cb_syncobj.h"
 #include "st_cb_strings.h"
+#include "st_cb_texturebarrier.h"
 #include "st_cb_viewport.h"
 #include "st_atom.h"
 #include "st_draw.h"
@@ -73,6 +76,17 @@ void st_invalidate_state(struct gl_context * ctx, GLuint new_state)
 {
    struct st_context *st = st_context(ctx);
 
+   /* Replace _NEW_FRAG_CLAMP with ST_NEW_FRAGMENT_PROGRAM for the fallback. */
+   if (st->clamp_frag_color_in_shader && (new_state & _NEW_FRAG_CLAMP)) {
+      new_state &= ~_NEW_FRAG_CLAMP;
+      st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
+   }
+
+   /* Update the vertex shader if ctx->Light._ClampVertexColor was changed. */
+   if (st->clamp_vert_color_in_shader && (new_state & _NEW_LIGHT)) {
+      st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
+   }
+
    st->dirty.mesa |= new_state;
    st->dirty.st |= ST_NEW_MESA;
 
@@ -130,9 +144,6 @@ st_create_context_priv( struct gl_context *ctx, struct pipe_context *pipe )
    else
       st->internal_target = PIPE_TEXTURE_RECT;
 
-   for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
-      st->state.sampler_list[i] = &st->state.samplers[i];
-
    for (i = 0; i < 3; i++) {
       memset(&st->velems_util_draw[i], 0, sizeof(struct pipe_vertex_element));
       st->velems_util_draw[i].src_offset = i * 4 * sizeof(float);
@@ -144,6 +155,10 @@ st_create_context_priv( struct gl_context *ctx, struct pipe_context *pipe )
    /* we want all vertex data to be placed in buffer objects */
    vbo_use_buffer_objects(ctx);
 
+
+   /* make sure that no VBOs are left mapped when we're drawing. */
+   vbo_always_unmap_buffers(ctx);
+
    /* Need these flags:
     */
    st->ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
@@ -170,10 +185,18 @@ struct st_context *st_create_context(gl_api api, struct pipe_context *pipe,
    struct gl_context *shareCtx = share ? share->ctx : NULL;
    struct dd_function_table funcs;
 
+   /* Sanity checks */
+   assert(MESA_SHADER_VERTEX == PIPE_SHADER_VERTEX);
+   assert(MESA_SHADER_FRAGMENT == PIPE_SHADER_FRAGMENT);
+   assert(MESA_SHADER_GEOMETRY == PIPE_SHADER_GEOMETRY);
+
    memset(&funcs, 0, sizeof(funcs));
    st_init_driver_functions(&funcs);
 
-   ctx = _mesa_create_context_for_api(api, visual, shareCtx, &funcs, NULL);
+   ctx = _mesa_create_context(api, visual, shareCtx, &funcs, NULL);
+   if (!ctx) {
+      return NULL;
+   }
 
    /* XXX: need a capability bit in gallium to query if the pipe
     * driver prefers DP4 or MUL/MAD for vertex transformation.
@@ -198,14 +221,13 @@ static void st_destroy_context_priv( struct st_context *st )
    st_destroy_drawpix(st);
    st_destroy_drawtex(st);
 
-   for (i = 0; i < Elements(st->state.sampler_views); i++) {
-      pipe_sampler_view_reference(&st->state.sampler_views[i], NULL);
+   /* Unreference any user vertex buffers. */
+   for (i = 0; i < st->num_user_attribs; i++) {
+      pipe_resource_reference(&st->user_attrib[i].buffer, NULL);
    }
 
-   for (i = 0; i < Elements(st->state.constants); i++) {
-      if (st->state.constants[i]) {
-         pipe_resource_reference(&st->state.constants[i], NULL);
-      }
+   for (i = 0; i < Elements(st->state.sampler_views); i++) {
+      pipe_sampler_view_reference(&st->state.sampler_views[i], NULL);
    }
 
    if (st->default_texture) {
@@ -239,14 +261,15 @@ void st_destroy_context( struct st_context *st )
    pipe->set_index_buffer(pipe, NULL);
 
    for (i = 0; i < PIPE_SHADER_TYPES; i++) {
-      pipe->set_constant_buffer(pipe, PIPE_SHADER_VERTEX, 0, NULL);
-      pipe_resource_reference(&st->state.constants[PIPE_SHADER_VERTEX], NULL);
+      pipe->set_constant_buffer(pipe, i, 0, NULL);
    }
 
    _mesa_delete_program_cache(st->ctx, st->pixel_xfer.cache);
 
    _vbo_DestroyContext(st->ctx);
 
+   st_destroy_program_variants(st);
+
    _mesa_free_context_data(ctx);
 
    st_destroy_context_priv(st);
@@ -262,8 +285,10 @@ void st_destroy_context( struct st_context *st )
 void st_init_driver_functions(struct dd_function_table *functions)
 {
    _mesa_init_shader_object_functions(functions);
+   _mesa_init_sampler_object_functions(functions);
+
+   functions->Accum = _mesa_accum;
 
-   st_init_accum_functions(functions);
    st_init_blit_functions(functions);
    st_init_bufferobject_functions(functions);
    st_init_clear_functions(functions);
@@ -282,11 +307,13 @@ void st_init_driver_functions(struct dd_function_table *functions)
    st_init_cond_render_functions(functions);
    st_init_readpixels_functions(functions);
    st_init_texture_functions(functions);
+   st_init_texture_barrier_functions(functions);
    st_init_flush_functions(functions);
    st_init_string_functions(functions);
    st_init_viewport_functions(functions);
 
    st_init_xformfb_functions(functions);
+   st_init_syncobj_functions(functions);
 
    functions->UpdateState = st_invalidate_state;
 }