st_atom.c

Go to the documentation of this file.
00001 /**************************************************************************
00002  * 
00003  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
00004  * All Rights Reserved.
00005  * 
00006  * Permission is hereby granted, free of charge, to any person obtaining a
00007  * copy of this software and associated documentation files (the
00008  * "Software"), to deal in the Software without restriction, including
00009  * without limitation the rights to use, copy, modify, merge, publish,
00010  * distribute, sub license, and/or sell copies of the Software, and to
00011  * permit persons to whom the Software is furnished to do so, subject to
00012  * the following conditions:
00013  * 
00014  * The above copyright notice and this permission notice (including the
00015  * next paragraph) shall be included in all copies or substantial portions
00016  * of the Software.
00017  * 
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00019  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00020  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
00021  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
00022  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
00023  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
00024  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00025  * 
00026  **************************************************************************/
00027 
00028 
00029 #include "main/glheader.h"
00030 #include "main/context.h"
00031 
00032 #include "pipe/p_defines.h"
00033 #include "st_context.h"
00034 #include "st_atom.h"
00035 #include "st_cb_bitmap.h"
00036 #include "st_program.h"
00037 
00038        
00039 
00040 /* This is used to initialize st->atoms[].  We could use this list
00041  * directly except for a single atom, st_update_constants, which has a
00042  * .dirty value which changes according to the parameters of the
00043  * current fragment and vertex programs, and so cannot be a static
00044  * value.
00045  */
00046 static const struct st_tracked_state *atoms[] =
00047 {
00048    &st_update_depth_stencil_alpha,
00049    &st_update_clip,
00050 
00051    &st_finalize_textures,
00052    &st_update_shader,
00053 
00054    &st_update_rasterizer,
00055    &st_update_polygon_stipple,
00056    &st_update_viewport,
00057    &st_update_scissor,
00058    &st_update_blend,
00059    &st_update_sampler,
00060    &st_update_texture,
00061    &st_update_framebuffer,
00062    &st_update_vs_constants,
00063    &st_update_fs_constants,
00064    &st_update_pixel_transfer
00065 };
00066 
00067 
00068 void st_init_atoms( struct st_context *st )
00069 {
00070    GLuint i;
00071 
00072    st->atoms = malloc(sizeof(atoms));
00073    st->nr_atoms = sizeof(atoms)/sizeof(*atoms);
00074    memcpy(st->atoms, atoms, sizeof(atoms));
00075 
00076    /* Patch in a pointer to the dynamic state atom:
00077     */
00078    for (i = 0; i < st->nr_atoms; i++) {
00079       if (st->atoms[i] == &st_update_vs_constants) {
00080          st->atoms[i] = &st->constants.tracked_state[PIPE_SHADER_VERTEX];
00081          st->atoms[i][0] = st_update_vs_constants;
00082       }
00083 
00084       if (st->atoms[i] == &st_update_fs_constants) {
00085          st->atoms[i] = &st->constants.tracked_state[PIPE_SHADER_FRAGMENT];
00086          st->atoms[i][0] = st_update_fs_constants;
00087       }
00088    }
00089 }
00090 
00091 
00092 void st_destroy_atoms( struct st_context *st )
00093 {
00094    if (st->atoms) {
00095       free(st->atoms);
00096       st->atoms = NULL;
00097    }
00098 }
00099 
00100 
00101 /***********************************************************************
00102  */
00103 
00104 static GLboolean check_state( const struct st_state_flags *a,
00105                               const struct st_state_flags *b )
00106 {
00107    return ((a->mesa & b->mesa) ||
00108            (a->st & b->st));
00109 }
00110 
00111 static void accumulate_state( struct st_state_flags *a,
00112                               const struct st_state_flags *b )
00113 {
00114    a->mesa |= b->mesa;
00115    a->st |= b->st;
00116 }
00117 
00118 
00119 static void xor_states( struct st_state_flags *result,
00120                              const struct st_state_flags *a,
00121                               const struct st_state_flags *b )
00122 {
00123    result->mesa = a->mesa ^ b->mesa;
00124    result->st = a->st ^ b->st;
00125 }
00126 
00127 
00128 /* Too complex to figure out, just check every time:
00129  */
00130 static void check_program_state( struct st_context *st )
00131 {
00132    GLcontext *ctx = st->ctx;
00133 
00134    if (ctx->VertexProgram._Current != &st->vp->Base)
00135       st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
00136 
00137    if (ctx->FragmentProgram._Current != &st->fp->Base)
00138       st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
00139 
00140 }
00141 
00142 
00143 /***********************************************************************
00144  * Update all derived state:
00145  */
00146 
00147 void st_validate_state( struct st_context *st )
00148 {
00149    struct st_state_flags *state = &st->dirty;
00150    GLuint i;
00151 
00152    /* The bitmap cache is immune to pixel unpack changes.
00153     * Note that GLUT makes several calls to glPixelStore for each
00154     * bitmap char it draws so this is an important check.
00155     */
00156    if (state->mesa & ~_NEW_PACKUNPACK)
00157       st_flush_bitmap_cache(st);
00158 
00159    check_program_state( st );
00160 
00161    if (state->st == 0)
00162       return;
00163 
00164 //   _mesa_printf("%s %x/%x\n", __FUNCTION__, state->mesa, state->st);
00165 
00166    if (1) {
00167       /* Debug version which enforces various sanity checks on the
00168        * state flags which are generated and checked to help ensure
00169        * state atoms are ordered correctly in the list.
00170        */
00171       struct st_state_flags examined, prev;      
00172       memset(&examined, 0, sizeof(examined));
00173       prev = *state;
00174 
00175       for (i = 0; i < st->nr_atoms; i++) {       
00176          const struct st_tracked_state *atom = st->atoms[i];
00177          struct st_state_flags generated;
00178          
00179 //       _mesa_printf("atom %s %x/%x\n", atom->name, atom->dirty.mesa, atom->dirty.st);
00180 
00181          if (!(atom->dirty.mesa || atom->dirty.st) ||
00182              !atom->update) {
00183             _mesa_printf("malformed atom %s\n", atom->name);
00184             assert(0);
00185          }
00186 
00187          if (check_state(state, &atom->dirty)) {
00188             st->atoms[i]->update( st );
00189 //          _mesa_printf("after: %x\n", atom->dirty.mesa);
00190          }
00191 
00192          accumulate_state(&examined, &atom->dirty);
00193 
00194          /* generated = (prev ^ state)
00195           * if (examined & generated)
00196           *     fail;
00197           */
00198          xor_states(&generated, &prev, state);
00199          assert(!check_state(&examined, &generated));
00200          prev = *state;
00201       }
00202 //      _mesa_printf("\n");
00203 
00204    }
00205    else {
00206       const GLuint nr = st->nr_atoms;
00207 
00208       for (i = 0; i < nr; i++) {         
00209          if (check_state(state, &st->atoms[i]->dirty))
00210             st->atoms[i]->update( st );
00211       }
00212    }
00213 
00214    memset(state, 0, sizeof(*state));
00215 }
00216 
00217 
00218 

Generated on Tue Sep 29 06:25:18 2009 for Gallium3D by  doxygen 1.5.4