⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 t_save_api.c

📁 winNT技术操作系统,国外开放的原代码和LIUX一样
💻 C
📖 第 1 页 / 共 4 页
字号:
/* $XFree86$ */
/**************************************************************************

Copyright 2002 Tungsten Graphics Inc., Cedar Park, Texas.

All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
on the rights to use, copy, modify, merge, publish, distribute, sub
license, and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
TUNGSTEN GRAPHICS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.

**************************************************************************/

/*
 * Authors:
 *   Keith Whitwell <keith@tungstengraphics.com>
 */



/* Display list compiler attempts to store lists of vertices with the
 * same vertex layout.  Additionally it attempts to minimize the need
 * for execute-time fixup of these vertex lists, allowing them to be
 * cached on hardware.
 *
 * There are still some circumstances where this can be thwarted, for
 * example by building a list that consists of one very long primitive
 * (eg Begin(Triangles), 1000 vertices, End), and calling that list
 * from inside a different begin/end object (Begin(Lines), CallList,
 * End).  
 *
 * In that case the code will have to replay the list as individual
 * commands through the Exec dispatch table, or fix up the copied
 * vertices at execute-time.
 *
 * The other case where fixup is required is when a vertex attribute
 * is introduced in the middle of a primitive.  Eg:
 *  Begin(Lines)
 *  TexCoord1f()           Vertex2f()
 *  TexCoord1f() Color3f() Vertex2f()
 *  End()
 *
 *  If the current value of Color isn't known at compile-time, this
 *  primitive will require fixup.
 *
 *
 * The list compiler currently doesn't attempt to compile lists
 * containing EvalCoord or EvalPoint commands.  On encountering one of
 * these, compilation falls back to opcodes.  
 *
 * This could be improved to fallback only when a mix of EvalCoord and
 * Vertex commands are issued within a single primitive.
 */


#include "glheader.h"
#include "context.h"
#include "dlist.h"
#include "enums.h"
#include "macros.h"
#include "api_validate.h"
#include "api_arrayelt.h"
#include "vtxfmt.h"
#include "t_save_api.h"
#include "dispatch.h"

/*
 * NOTE: Old 'parity' issue is gone, but copying can still be
 * wrong-footed on replay.
 */
static GLuint _save_copy_vertices( GLcontext *ctx, 
				   const struct tnl_vertex_list *node )
{
   TNLcontext *tnl = TNL_CONTEXT( ctx );
   const struct tnl_prim *prim = &node->prim[node->prim_count-1];
   GLuint nr = prim->count;
   GLuint sz = tnl->save.vertex_size;
   const GLfloat *src = node->buffer + prim->start * sz;
   GLfloat *dst = tnl->save.copied.buffer;
   GLuint ovf, i;

   if (prim->mode & PRIM_END)
      return 0;
	 
   switch( prim->mode & PRIM_MODE_MASK )
   {
   case GL_POINTS:
      return 0;
   case GL_LINES:
      ovf = nr&1;
      for (i = 0 ; i < ovf ; i++)
	 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz*sizeof(GLfloat) );
      return i;
   case GL_TRIANGLES:
      ovf = nr%3;
      for (i = 0 ; i < ovf ; i++)
	 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz*sizeof(GLfloat) );
      return i;
   case GL_QUADS:
      ovf = nr&3;
      for (i = 0 ; i < ovf ; i++)
	 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz*sizeof(GLfloat) );
      return i;
   case GL_LINE_STRIP:
      if (nr == 0) 
	 return 0;
      else {
	 _mesa_memcpy( dst, src+(nr-1)*sz, sz*sizeof(GLfloat) );
	 return 1;
      }
   case GL_LINE_LOOP:
   case GL_TRIANGLE_FAN:
   case GL_POLYGON:
      if (nr == 0) 
	 return 0;
      else if (nr == 1) {
	 _mesa_memcpy( dst, src+0, sz*sizeof(GLfloat) );
	 return 1;
      } else {
	 _mesa_memcpy( dst, src+0, sz*sizeof(GLfloat) );
	 _mesa_memcpy( dst+sz, src+(nr-1)*sz, sz*sizeof(GLfloat) );
	 return 2;
      }
   case GL_TRIANGLE_STRIP:
   case GL_QUAD_STRIP:
      switch (nr) {
      case 0: ovf = 0; break;
      case 1: ovf = 1; break;
      default: ovf = 2 + (nr&1); break;
      }
      for (i = 0 ; i < ovf ; i++)
	 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz*sizeof(GLfloat) );
      return i;
   default:
      assert(0);
      return 0;
   }
}


static void
build_normal_lengths( struct tnl_vertex_list *node )
{
   GLuint i;
   GLfloat *len;
   GLfloat *n = node->buffer;
   GLuint stride = node->vertex_size;
   GLuint count = node->count;

   len = node->normal_lengths = (GLfloat *) MALLOC( count * sizeof(GLfloat) );
   if (!len)
      return;

   /* Find the normal of the first vertex:
    */
   for (i = 0 ; i < _TNL_ATTRIB_NORMAL ; i++) 
      n += node->attrsz[i];

   for (i = 0 ; i < count ; i++, n += stride) {
      len[i] = LEN_3FV( n );
      if (len[i] > 0.0F) len[i] = 1.0F / len[i];
   } 
}

static struct tnl_vertex_store *alloc_vertex_store( GLcontext *ctx )
{
   struct tnl_vertex_store *store = MALLOC_STRUCT(tnl_vertex_store);
   (void) ctx;
   store->used = 0;
   store->refcount = 1;
   return store;
}

static struct tnl_primitive_store *alloc_prim_store( GLcontext *ctx )
{
   struct tnl_primitive_store *store = MALLOC_STRUCT(tnl_primitive_store);
   (void) ctx;
   store->used = 0;
   store->refcount = 1;
   return store;
}

static void _save_reset_counters( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);

   tnl->save.prim = tnl->save.prim_store->buffer + tnl->save.prim_store->used;
   tnl->save.buffer = (tnl->save.vertex_store->buffer + 
		       tnl->save.vertex_store->used);

   if (tnl->save.vertex_size)
      tnl->save.initial_counter = ((SAVE_BUFFER_SIZE - 
				    tnl->save.vertex_store->used) / 
				   tnl->save.vertex_size);
   else
      tnl->save.initial_counter = 0;

   if (tnl->save.initial_counter > ctx->Const.MaxArrayLockSize )
      tnl->save.initial_counter = ctx->Const.MaxArrayLockSize;

   tnl->save.counter = tnl->save.initial_counter;
   tnl->save.prim_count = 0;
   tnl->save.prim_max = SAVE_PRIM_SIZE - tnl->save.prim_store->used;
   tnl->save.copied.nr = 0;
   tnl->save.dangling_attr_ref = 0;
}


/* Insert the active immediate struct onto the display list currently
 * being built.
 */
static void _save_compile_vertex_list( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   struct tnl_vertex_list *node;

   /* Allocate space for this structure in the display list currently
    * being compiled.
    */
   node = (struct tnl_vertex_list *)
      _mesa_alloc_instruction(ctx, tnl->save.opcode_vertex_list, sizeof(*node));

   if (!node)
      return;

   /* Duplicate our template, increment refcounts to the storage structs:
    */
   _mesa_memcpy(node->attrsz, tnl->save.attrsz, sizeof(node->attrsz));
   node->vertex_size = tnl->save.vertex_size;
   node->buffer = tnl->save.buffer;
   node->count = tnl->save.initial_counter - tnl->save.counter;
   node->wrap_count = tnl->save.copied.nr;
   node->have_materials = tnl->save.have_materials;
   node->dangling_attr_ref = tnl->save.dangling_attr_ref;
   node->normal_lengths = NULL;
   node->prim = tnl->save.prim;
   node->prim_count = tnl->save.prim_count;
   node->vertex_store = tnl->save.vertex_store;
   node->prim_store = tnl->save.prim_store;

   node->vertex_store->refcount++;
   node->prim_store->refcount++;

   assert(node->attrsz[_TNL_ATTRIB_POS] != 0 ||
	  node->count == 0);

   if (tnl->save.dangling_attr_ref)
      ctx->ListState.CurrentList->flags |= MESA_DLIST_DANGLING_REFS;

   /* Maybe calculate normal lengths:
    */
   if (tnl->CalcDListNormalLengths && 
       node->attrsz[_TNL_ATTRIB_NORMAL] == 3 &&
       !(ctx->ListState.CurrentList->flags & MESA_DLIST_DANGLING_REFS))
      build_normal_lengths( node );


   tnl->save.vertex_store->used += tnl->save.vertex_size * node->count;
   tnl->save.prim_store->used += node->prim_count;

   /* Decide whether the storage structs are full, or can be used for
    * the next vertex lists as well.
    */
   if (tnl->save.vertex_store->used > 
       SAVE_BUFFER_SIZE - 16 * (tnl->save.vertex_size + 4)) {

      tnl->save.vertex_store->refcount--; 
      assert(tnl->save.vertex_store->refcount != 0);
      tnl->save.vertex_store = alloc_vertex_store( ctx );
      tnl->save.vbptr = tnl->save.vertex_store->buffer;
   } 

   if (tnl->save.prim_store->used > SAVE_PRIM_SIZE - 6) {
      tnl->save.prim_store->refcount--; 
      assert(tnl->save.prim_store->refcount != 0);
      tnl->save.prim_store = alloc_prim_store( ctx );
   } 

   /* Reset our structures for the next run of vertices:
    */
   _save_reset_counters( ctx );

   /* Copy duplicated vertices 
    */
   tnl->save.copied.nr = _save_copy_vertices( ctx, node );


   /* Deal with GL_COMPILE_AND_EXECUTE:
    */
   if (ctx->ExecuteFlag) {
      _tnl_playback_vertex_list( ctx, (void *) node );
   }
}


/* TODO -- If no new vertices have been stored, don't bother saving
 * it.
 */
static void _save_wrap_buffers( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   GLint i = tnl->save.prim_count - 1;
   GLenum mode;

   assert(i < (GLint) tnl->save.prim_max);
   assert(i >= 0);

   /* Close off in-progress primitive.
    */
   tnl->save.prim[i].count = ((tnl->save.initial_counter - tnl->save.counter) - 
			     tnl->save.prim[i].start);
   mode = tnl->save.prim[i].mode & ~(PRIM_BEGIN|PRIM_END);
   
   /* store the copied vertices, and allocate a new list.
    */
   _save_compile_vertex_list( ctx );

   /* Restart interrupted primitive
    */
   tnl->save.prim[0].mode = mode;
   tnl->save.prim[0].start = 0;
   tnl->save.prim[0].count = 0;
   tnl->save.prim_count = 1;
}



/* Called only when buffers are wrapped as the result of filling the
 * vertex_store struct.  
 */
static void _save_wrap_filled_vertex( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   GLfloat *data = tnl->save.copied.buffer;
   GLuint i;

   /* Emit a glEnd to close off the last vertex list.
    */
   _save_wrap_buffers( ctx );
   
    /* Copy stored stored vertices to start of new list.
    */
   assert(tnl->save.counter > tnl->save.copied.nr);

   for (i = 0 ; i < tnl->save.copied.nr ; i++) {
      _mesa_memcpy( tnl->save.vbptr, data, tnl->save.vertex_size * sizeof(GLfloat));
      data += tnl->save.vertex_size;
      tnl->save.vbptr += tnl->save.vertex_size;
      tnl->save.counter--;
   }
}


static void _save_copy_to_current( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx); 
   GLuint i;

   for (i = _TNL_ATTRIB_POS+1 ; i <= _TNL_ATTRIB_INDEX ; i++) {
      if (tnl->save.attrsz[i]) {
	 tnl->save.currentsz[i][0] = tnl->save.attrsz[i];
	 COPY_CLEAN_4V(tnl->save.current[i], 
		    tnl->save.attrsz[i], 
		    tnl->save.attrptr[i]);
      }
   }

   /* Edgeflag requires special treatment: 
    *
    * TODO: change edgeflag to GLfloat in Mesa.
    */
   if (tnl->save.attrsz[_TNL_ATTRIB_EDGEFLAG]) {
      ctx->ListState.ActiveEdgeFlag = 1;
      tnl->save.CurrentFloatEdgeFlag = 
	 tnl->save.attrptr[_TNL_ATTRIB_EDGEFLAG][0];
      ctx->ListState.CurrentEdgeFlag = 
	 (tnl->save.CurrentFloatEdgeFlag == 1.0);
   }
}


static void _save_copy_from_current( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx); 
   GLint i;

   for (i = _TNL_ATTRIB_POS+1 ; i <= _TNL_ATTRIB_INDEX ; i++) 
      switch (tnl->save.attrsz[i]) {
      case 4: tnl->save.attrptr[i][3] = tnl->save.current[i][3];
      case 3: tnl->save.attrptr[i][2] = tnl->save.current[i][2];
      case 2: tnl->save.attrptr[i][1] = tnl->save.current[i][1];
      case 1: tnl->save.attrptr[i][0] = tnl->save.current[i][0];
      case 0: break;
      }

   /* Edgeflag requires special treatment:
    */
   if (tnl->save.attrsz[_TNL_ATTRIB_EDGEFLAG]) {
      tnl->save.CurrentFloatEdgeFlag = (GLfloat)ctx->ListState.CurrentEdgeFlag;
      tnl->save.attrptr[_TNL_ATTRIB_EDGEFLAG][0] = tnl->save.CurrentFloatEdgeFlag;
   }
}




/* Flush existing data, set new attrib size, replay copied vertices.
 */ 
static void _save_upgrade_vertex( GLcontext *ctx, 
				 GLuint attr,
				 GLuint newsz )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   GLuint oldsz;
   GLuint i;
   GLfloat *tmp;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -