home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / Mesa-3.1 / src / vb.c < prev    next >
C/C++ Source or Header  |  2000-01-07  |  11KB  |  348 lines

  1. /* $Id: vb.c,v 1.11 1999/10/20 09:21:40 keithw Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.1
  6.  * 
  7.  * Copyright (C) 1999  Brian Paul   All Rights Reserved.
  8.  * 
  9.  * Permission is hereby granted, free of charge, to any person obtaining a
  10.  * copy of this software and associated documentation files (the "Software"),
  11.  * to deal in the Software without restriction, including without limitation
  12.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13.  * and/or sell copies of the Software, and to permit persons to whom the
  14.  * Software is furnished to do so, subject to the following conditions:
  15.  * 
  16.  * The above copyright notice and this permission notice shall be included
  17.  * in all copies or substantial portions of the Software.
  18.  * 
  19.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  22.  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  23.  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  */
  26.  
  27.  
  28.  
  29.  
  30.  
  31. #ifdef PC_HEADER
  32. #include "all.h"
  33. #else
  34. #ifndef XFree86Server
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #else
  38. #include "GL/xf86glx.h"
  39. #endif
  40. #include "types.h"
  41. #include "vb.h"
  42. #include "vbxform.h"
  43. #include "xform.h"
  44. #endif
  45.  
  46.  
  47. /*
  48.  * Allocate and initialize a vertex buffer.
  49.  */
  50. struct vertex_buffer *gl_vb_create_for_immediate( GLcontext *ctx )
  51. {
  52.    struct vertex_buffer *VB;
  53.    struct immediate *IM;
  54.    GLuint alignment = 32;
  55.  
  56.    VB = CALLOC_STRUCT(vertex_buffer);
  57.    if (!VB) 
  58.       return 0;
  59.  
  60.    VB->ctx = ctx;
  61.    VB->ClipAndMask = CLIP_ALL_BITS;
  62.    VB->Size = VB_SIZE;
  63.    VB->FirstFree = VB_MAX;
  64.    VB->Type = VB_IMMEDIATE;
  65.    VB->Start = VB_START;
  66.    VB->pipeline = &ctx->CVA.elt;
  67.  
  68.    gl_vector4f_alloc( &VB->Eye, 2, VEC_WRITABLE, VB_SIZE, alignment );
  69.    gl_vector4f_alloc( &VB->Clip, 2, VEC_WRITABLE, VB_SIZE, alignment );
  70.    gl_vector4f_alloc( &VB->Win, 2, VEC_WRITABLE, VB_SIZE, alignment );
  71.    gl_vector4ub_alloc( &VB->BColor, VEC_WRITABLE, VB_SIZE, alignment );      
  72.    gl_vector1ui_alloc( &VB->BIndex, VEC_WRITABLE, VB_SIZE, alignment );
  73.  
  74.    VB->ClipMask = (GLubyte *)MALLOC(sizeof(GLubyte) * VB_SIZE);
  75.    VB->UserClipMask = (GLubyte *)CALLOC(sizeof(GLubyte) * VB_SIZE);
  76.    VB->CullMask = (GLubyte *)MALLOC(sizeof(GLubyte) * VB_SIZE);
  77.    VB->NormCullMask = (GLubyte *)MALLOC(sizeof(GLubyte) * VB_SIZE);
  78.    VB->Spec[0] = (GLubyte (*)[4])MALLOC(sizeof(GLubyte) * 4 * VB_SIZE);
  79.    VB->Spec[1] = (GLubyte (*)[4])MALLOC(sizeof(GLubyte) * 4 * VB_SIZE);
  80.  
  81.    IM = VB->IM = gl_immediate_alloc( ctx );
  82.       
  83.    VB->store.Obj = &IM->v.Obj;
  84.    VB->store.Normal = &IM->v.Normal;
  85.    VB->store.Color = 0;        /* not used */
  86.    VB->store.Index = 0;        /* not used */
  87.    VB->store.EdgeFlag = &IM->v.EdgeFlag;
  88.    VB->store.TexCoord[0] = &IM->v.TexCoord[0];
  89.    VB->store.TexCoord[1] = &IM->v.TexCoord[1];
  90.    VB->store.Elt = &IM->v.Elt;
  91.       
  92.    VB->LitColor[0] = VB->FoggedColor[0] = &IM->v.Color;
  93.    VB->LitColor[1] = VB->FoggedColor[1] = &VB->BColor;
  94.  
  95.    VB->LitIndex[0] = VB->FoggedIndex[0] = &IM->v.Index;
  96.    VB->LitIndex[1] = VB->FoggedIndex[1] = &VB->BIndex;
  97.    
  98.  
  99.    VB->prev_buffer = IM;
  100.    /* prev_buffer is now also a reference for the immediate buffer */
  101.    IM->ref_count++;
  102.  
  103.    if (ctx->Driver.RegisterVB)
  104.       ctx->Driver.RegisterVB( VB );
  105.  
  106.    return VB;
  107. }
  108.  
  109.  
  110.  
  111. struct vertex_buffer *gl_vb_create_for_cva( GLcontext *ctx, GLuint size )
  112. {
  113.    struct vertex_buffer *VB;
  114.    GLuint alignment = 32;
  115.  
  116.    VB = CALLOC_STRUCT(vertex_buffer);
  117.    if (!VB) 
  118.       return 0;
  119.  
  120.    /* set non-zero fields */
  121.  
  122.    VB->ctx = ctx;
  123.    VB->Type = VB_CVA_PRECALC;
  124.    VB->FirstFree = size;
  125.    VB->CullDone = 1;
  126.  
  127.    size += VB_MAX_CLIPPED_VERTS;
  128.    VB->Size = size;
  129.    VB->ClipAndMask = CLIP_ALL_BITS;
  130.    VB->pipeline = &ctx->CVA.pre;
  131.  
  132.    VB->ClipMask = (GLubyte *)MALLOC(sizeof(GLubyte) * size);
  133.    VB->UserClipMask = (GLubyte *)CALLOC(sizeof(GLubyte) * size);
  134.    VB->Spec[0] = (GLubyte (*)[4])MALLOC(sizeof(GLubyte) * 4 * size);
  135.    VB->Spec[1] = (GLubyte (*)[4])MALLOC(sizeof(GLubyte) * 4 * size);
  136.    VB->Flag = (GLuint *)MALLOC(sizeof(GLuint) * size);
  137.  
  138.    gl_vector4f_alloc( &VB->Eye, 2, VEC_WRITABLE, size, alignment );
  139.    gl_vector4f_alloc( &VB->Clip, 2, VEC_WRITABLE, size, alignment );
  140.    gl_vector4f_alloc( &VB->Win, 2, VEC_WRITABLE, size, alignment );
  141.  
  142.    VB->store.Obj         = (GLvector4f *)  CALLOC(sizeof(GLvector4f));
  143.    VB->store.Normal      = (GLvector3f *)  CALLOC(sizeof(GLvector3f));
  144.    VB->store.Color       = 0;
  145.    VB->store.Index       = 0;
  146.    VB->store.EdgeFlag    = (GLvector1ub *) CALLOC(sizeof(GLvector1ub));
  147.    VB->store.TexCoord[0] = (GLvector4f *)  CALLOC(sizeof(GLvector4f));
  148.    VB->store.TexCoord[1] = (GLvector4f *)  CALLOC(sizeof(GLvector4f));
  149.    VB->store.Elt         = (GLvector1ui *) CALLOC(sizeof(GLvector1ui));
  150.    VB->LitColor[0]       = (GLvector4ub *) CALLOC(sizeof(GLvector4ub));
  151.    VB->LitColor[1]       = (GLvector4ub *) CALLOC(sizeof(GLvector4ub));
  152.    VB->LitIndex[0]       = (GLvector1ui *) CALLOC(sizeof(GLvector1ui));
  153.    VB->LitIndex[1]       = (GLvector1ui *) CALLOC(sizeof(GLvector1ui));
  154.    VB->FoggedColor[0]    = (GLvector4ub *) CALLOC(sizeof(GLvector4ub));
  155.    VB->FoggedColor[1]    = (GLvector4ub *) CALLOC(sizeof(GLvector4ub));
  156.    VB->FoggedIndex[0]    = (GLvector1ui *) CALLOC(sizeof(GLvector1ui));
  157.    VB->FoggedIndex[1]    = (GLvector1ui *) CALLOC(sizeof(GLvector1ui));
  158.  
  159.    VB->ColorPtr = VB->Color[0] = VB->LitColor[0];
  160.    VB->IndexPtr = VB->Index[0] = VB->LitIndex[0];
  161.    VB->Specular = VB->Spec[0];
  162.    VB->TexCoordPtr[0] = VB->store.TexCoord[0];
  163.    VB->TexCoordPtr[1] = VB->store.TexCoord[1];
  164.    VB->EdgeFlagPtr = VB->store.EdgeFlag;
  165.    VB->NormalPtr = VB->store.Normal;
  166.    VB->ObjPtr = VB->store.Obj;
  167.    VB->EltPtr = VB->store.Elt;
  168.       
  169.    gl_vector4f_alloc( VB->store.Obj, 2, VEC_WRITABLE, size, alignment );
  170.    gl_vector3f_alloc( VB->store.Normal, VEC_WRITABLE, size, alignment );
  171.    gl_vector1ub_alloc( VB->store.EdgeFlag, VEC_WRITABLE, size, alignment );
  172.    gl_vector4f_alloc( VB->store.TexCoord[0], 2, VEC_WRITABLE, size, alignment );
  173.    gl_vector4f_alloc( VB->store.TexCoord[1], 2, VEC_WRITABLE, size, alignment );
  174.  
  175.    /* TODO: allocate these on demand.
  176.     */
  177.    gl_vector4ub_alloc( VB->LitColor[0], VEC_WRITABLE, size, alignment );
  178.    gl_vector4ub_alloc( VB->LitColor[1], VEC_WRITABLE, size, alignment );
  179.    gl_vector1ui_alloc( VB->LitIndex[0], VEC_WRITABLE, size, alignment );
  180.    gl_vector1ui_alloc( VB->LitIndex[1], VEC_WRITABLE, size, alignment );
  181.    gl_vector4ub_alloc( VB->FoggedColor[0], VEC_WRITABLE, size, alignment );
  182.    gl_vector4ub_alloc( VB->FoggedColor[1], VEC_WRITABLE, size, alignment );
  183.    gl_vector1ui_alloc( VB->FoggedIndex[0], VEC_WRITABLE, size, alignment );
  184.    gl_vector1ui_alloc( VB->FoggedIndex[1], VEC_WRITABLE, size, alignment );
  185.  
  186.  
  187.  
  188.    VB->prev_buffer = 0;
  189.    VB->Start = 0;
  190.  
  191.    if (ctx->Driver.RegisterVB)
  192.       ctx->Driver.RegisterVB( VB );
  193.  
  194.    return VB;
  195. }
  196.  
  197. void gl_vb_free( struct vertex_buffer *VB )
  198. {
  199.    gl_vector4f_free( &VB->Eye );
  200.    gl_vector4f_free( &VB->Clip );
  201.    gl_vector4f_free( &VB->Win );
  202.    gl_vector4ub_free( &VB->BColor );
  203.    gl_vector1ui_free( &VB->BIndex );
  204.  
  205.    if ( VB->prev_buffer && ! --VB->prev_buffer->ref_count )
  206.       gl_immediate_free( VB->prev_buffer );
  207.  
  208.    if (VB->IM) {
  209.       if ( ! --VB->IM->ref_count )
  210.       gl_immediate_free( VB->IM );
  211.  
  212.       FREE( VB->CullMask );
  213.       FREE( VB->NormCullMask );
  214.    } else {
  215.       if (VB->store.Elt) 
  216.       gl_vector4f_free( VB->store.Obj );          FREE( VB->store.Obj );
  217.       gl_vector3f_free( VB->store.Normal );       FREE( VB->store.Normal );
  218.       gl_vector1ub_free( VB->store.EdgeFlag );    FREE( VB->store.EdgeFlag );
  219.       gl_vector4f_free( VB->store.TexCoord[0] );  FREE( VB->store.TexCoord[0]);
  220.       gl_vector4f_free( VB->store.TexCoord[1] );  FREE( VB->store.TexCoord[1]);
  221.  
  222.       gl_vector4ub_free( VB->LitColor[0] );       FREE( VB->LitColor[0] );
  223.       gl_vector4ub_free( VB->LitColor[1] );       FREE( VB->LitColor[1] );
  224.       gl_vector1ui_free( VB->LitIndex[0] );       FREE( VB->LitIndex[0] );
  225.       gl_vector1ui_free( VB->LitIndex[1] );       FREE( VB->LitIndex[1] );
  226.  
  227.       gl_vector4ub_free( VB->FoggedColor[0] );    FREE( VB->FoggedColor[0] );
  228.       gl_vector4ub_free( VB->FoggedColor[1] );    FREE( VB->FoggedColor[1] );
  229.       gl_vector1ui_free( VB->FoggedIndex[0] );    FREE( VB->FoggedIndex[0] );
  230.       gl_vector1ui_free( VB->FoggedIndex[1] );    FREE( VB->FoggedIndex[1] );
  231.  
  232.       FREE( VB->Flag );
  233.    }
  234.  
  235.    if (VB->tmp_f) FREE(VB->tmp_f);
  236.    if (VB->tmp_m) FREE(VB->tmp_m);
  237.    if (VB->EvaluatedFlags) FREE(VB->EvaluatedFlags);
  238.  
  239.    FREE( VB->Spec[0] );
  240.    FREE( VB->Spec[1] );
  241.    FREE( VB->ClipMask );
  242.    FREE( VB->UserClipMask );
  243.  
  244.    if (VB->ctx->Driver.UnregisterVB)
  245.       VB->ctx->Driver.UnregisterVB( VB );
  246.  
  247.    FREE( VB );
  248. }
  249.  
  250.  
  251. struct immediate *gl_immediate_alloc( GLcontext *ctx )
  252. {
  253.    static int id = 0;
  254.    struct immediate *IM;
  255.    GLuint j;
  256.  
  257.    if (ctx->freed_im_queue) {
  258.       IM = ctx->freed_im_queue;
  259.       ctx->freed_im_queue = IM->next;
  260.       ctx->nr_im_queued--; 
  261.       IM->ref_count = 1;
  262.       return IM;
  263.    }
  264.  
  265.    IM= (struct immediate *) MALLOC(sizeof(*IM));
  266.    if (!IM) return 0;
  267.  
  268.    IM->id = id++;
  269.    IM->ref_count = 1;
  270.    IM->backref = ctx;
  271.    IM->maybe_transform_vb = gl_maybe_transform_vb;
  272. /*     IM->Bounds = 0; */
  273.    IM->NormalLengths = 0;
  274.    IM->LastCalcedLength = 0;
  275.    IM->FlushElt = 0;
  276.    IM->LastPrimitive = VB_START;
  277.    IM->Count = VB_MAX;        /* force clear of Flag. */
  278.    IM->Start = VB_START;    
  279.    IM->Material = 0;
  280.    IM->MaterialMask = 0;
  281.  
  282.    if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
  283.       fprintf(stderr, "alloc immediate %d\n", id);
  284.  
  285.    gl_vector4f_init( &IM->v.Obj, VEC_WRITABLE, IM->Obj );
  286.    gl_vector3f_init( &IM->v.Normal, VEC_WRITABLE, IM->Normal );
  287.    gl_vector4ub_init( &IM->v.Color, VEC_WRITABLE, IM->Color );
  288.    gl_vector1ui_init( &IM->v.Index, VEC_WRITABLE, IM->Index );
  289.    gl_vector1ub_init( &IM->v.EdgeFlag, VEC_WRITABLE, IM->EdgeFlag );
  290.    gl_vector1ui_init( &IM->v.Elt, VEC_WRITABLE, IM->Elt );
  291.  
  292.    for (j=0;j<MAX_TEXTURE_UNITS;j++) {
  293.       IM->TexCoordPtr[j] = IM->TexCoord[j];
  294.       gl_vector4f_init( &IM->v.TexCoord[j], VEC_WRITABLE, IM->TexCoord[j]);
  295.  
  296.       /* Precalculate some flags and keep them in a handy place.
  297.        */
  298.       IM->TF1[j] = VERT_TEX0_1 << (j*NR_TEXSIZE_BITS);
  299.       IM->TF2[j] = VERT_TEX0_12 << (j*NR_TEXSIZE_BITS);
  300.       IM->TF3[j] = VERT_TEX0_123 << (j*NR_TEXSIZE_BITS);
  301.       IM->TF4[j] = VERT_TEX0_1234 << (j*NR_TEXSIZE_BITS);
  302.    }
  303.  
  304.    return IM;
  305. }
  306.  
  307.  
  308. void gl_immediate_free( struct immediate *IM )
  309. {
  310.    GLcontext *ctx = IM->backref;
  311.  
  312.    if (IM->NormalLengths) {
  313.       FREE( IM->NormalLengths );
  314.       IM->NormalLengths = 0;
  315.       IM->LastCalcedLength = 0;
  316.    }
  317.  
  318.    if (IM->Material) {
  319.       FREE( IM->Material );
  320.       FREE( IM->MaterialMask );
  321.       IM->Material = 0;
  322.       IM->MaterialMask = 0;
  323.    }
  324.  
  325.    if (ctx->nr_im_queued > 5) {
  326.       if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
  327.      fprintf(stderr, "really free immediate %d\n", IM->id);
  328.  
  329.       FREE( IM );
  330.    }
  331.    else {
  332.       if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
  333.      fprintf(stderr, "requeue immediate %d\n", IM->id);
  334.  
  335.       IM->next = ctx->freed_im_queue;
  336.       ctx->freed_im_queue = IM;
  337.       ctx->nr_im_queued++; 
  338.    }
  339. }
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.