Files
third_party_mesa3d/src/mesa/tnl/t_vb_program.c

438 lines
15 KiB
C
Raw Normal View History

/* $Id: t_vb_program.c,v 1.6 2002/01/05 20:51:13 brianp Exp $ */
2001-12-14 02:50:01 +00:00
/*
* Mesa 3-D graphics library
* Version: 4.1
*
* Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
2001-12-14 02:50:01 +00:00
*
* 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
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* 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 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 NONINFRINGEMENT. IN NO EVENT SHALL
* BRIAN PAUL 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.
*/
/*
* -------- Regarding NV_vertex_program --------
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistribution of the source code must contain a copyright notice
* and this list of conditions;
*
* o Redistribution in binary and source code form must contain the
* following Notice in the software and any documentation and/or other
* materials provided with the distribution; and
*
* o The name of Nvidia may not be used to promote or endorse software
* derived from the software.
*
* NOTICE: Nvidia hereby grants to each recipient a non-exclusive worldwide
* royalty free patent license under patent claims that are licensable by
* Nvidia and which are necessarily required and for which no commercially
* viable non infringing alternative exists to make, use, sell, offer to sell,
* import and otherwise transfer the vertex extension for the Mesa 3D Graphics
* Library as distributed in source code and object code form. No hardware or
* hardware implementation (including a semiconductor implementation and chips)
* are licensed hereunder. If a recipient makes a patent claim or institutes
* patent litigation against Nvidia or Nvidia's customers for use or sale of
* Nvidia products, then this license grant as to such recipient shall
* immediately terminate and recipient immediately agrees to cease use and
* distribution of the Mesa Program and derivatives thereof.
*
* THE MESA 3D GRAPHICS LIBRARY IS PROVIDED ON AN "AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING,
* WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-NFRINGEMENT
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* NVIDIA SHALL NOT HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION
* LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE MESA 3D GRAPHICS
* LIBRARY OR EVIDENCE OR THE EXERCISE OF ANY RIGHTS GRANTED HEREUNDR, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* If you do not comply with this agreement, then Nvidia may cancel the license
* and rights granted herein.
* ---------------------------------------------
*/
/*
* Authors:
* Brian Paul
*/
#include "glheader.h"
#include "api_noop.h"
#include "colormac.h"
#include "context.h"
#include "dlist.h"
#include "hash.h"
#include "light.h"
#include "macros.h"
#include "mem.h"
#include "mmath.h"
#include "simple_list.h"
#include "mtypes.h"
#include "vpexec.h"
#include "math/m_translate.h"
#include "t_context.h"
#include "t_pipeline.h"
#include "t_imm_api.h"
#include "t_imm_exec.h"
struct vp_stage_data {
GLvector4f clipCoords; /* post-modelview/projection coords */
GLvector4f ndcCoords; /* normalized device coords */
2001-12-14 02:50:01 +00:00
struct gl_client_array color0[2]; /* front and back */
struct gl_client_array color1[2]; /* front and back */
GLvector4f texCoord[MAX_TEXTURE_UNITS];
GLvector4f fogCoord;
2001-12-14 02:50:01 +00:00
GLvector1f pointSize;
GLubyte *clipmask;
GLubyte ormask, andmask;
2001-12-14 02:50:01 +00:00
};
#define VP_STAGE_DATA(stage) ((struct vp_stage_data *)(stage->privatePtr))
static GLboolean run_vp( GLcontext *ctx, struct gl_pipeline_stage *stage )
{
TNLcontext *tnl = TNL_CONTEXT(ctx);
struct vp_stage_data *store = VP_STAGE_DATA(stage);
struct vertex_buffer *VB = &tnl->vb;
struct vp_machine *machine = &(ctx->VertexProgram.Machine);
GLint i;
/* convenience pointers */
GLfloat (*clip)[4] = (GLfloat (*)[4]) store->clipCoords.data;
GLfloat (*color0)[4] = (GLfloat (*)[4]) store->color0[0].Ptr;
GLfloat (*color1)[4] = (GLfloat (*)[4]) store->color1[0].Ptr;
GLfloat (*bfcolor0)[4] = (GLfloat (*)[4]) store->color0[1].Ptr;
GLfloat (*bfcolor1)[4] = (GLfloat (*)[4]) store->color1[1].Ptr;
GLfloat (*fog)[4] = (GLfloat (*)[4]) store->fogCoord.data;
GLfloat *pointSize = (GLfloat *) store->pointSize.data;
GLfloat (*texture0)[4] = (GLfloat (*)[4]) store->texCoord[0].data;
GLfloat (*texture1)[4] = (GLfloat (*)[4]) store->texCoord[1].data;
GLfloat (*texture2)[4] = (GLfloat (*)[4]) store->texCoord[2].data;
GLfloat (*texture3)[4] = (GLfloat (*)[4]) store->texCoord[3].data;
2001-12-14 02:50:01 +00:00
_mesa_init_tracked_matrices(ctx);
_mesa_init_vp_registers(ctx); /* sets temp regs to (0,0,0,1) */
for (i = 0; i < VB->Count; i++) {
GLuint attr;
#if 0
2001-12-14 02:50:01 +00:00
printf("Input %d: %f, %f, %f, %f\n", i,
VB->AttribPtr[0]->data[i][0],
VB->AttribPtr[0]->data[i][1],
VB->AttribPtr[0]->data[i][2],
VB->AttribPtr[0]->data[i][3]);
printf(" color: %f, %f, %f, %f\n",
VB->AttribPtr[3]->data[i][0],
VB->AttribPtr[3]->data[i][1],
VB->AttribPtr[3]->data[i][2],
VB->AttribPtr[3]->data[i][3]);
2001-12-15 02:13:32 +00:00
printf(" normal: %f, %f, %f, %f\n",
VB->AttribPtr[2]->data[i][0],
VB->AttribPtr[2]->data[i][1],
VB->AttribPtr[2]->data[i][2],
VB->AttribPtr[2]->data[i][3]);
#endif
2001-12-14 02:50:01 +00:00
/* load the input attribute registers */
for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
2001-12-14 02:50:01 +00:00
if (VB->Flag[i] & (1 << attr)) {
COPY_4V(machine->Registers[VP_INPUT_REG_START + attr],
VB->AttribPtr[attr]->data[i]);
}
}
/* execute the program */
ASSERT(ctx->VertexProgram.Current);
_mesa_exec_program(ctx, ctx->VertexProgram.Current);
2001-12-14 02:50:01 +00:00
#if 0
2001-12-14 02:50:01 +00:00
printf("Output %d: %f, %f, %f, %f\n", i,
2001-12-15 21:31:28 +00:00
machine->Registers[VP_OUT_HPOS][0],
machine->Registers[VP_OUT_HPOS][1],
machine->Registers[VP_OUT_HPOS][2],
machine->Registers[VP_OUT_HPOS][3]);
2001-12-14 02:50:01 +00:00
printf(" color: %f, %f, %f, %f\n",
2001-12-15 21:31:28 +00:00
machine->Registers[VP_OUT_COL0][0],
machine->Registers[VP_OUT_COL0][1],
machine->Registers[VP_OUT_COL0][2],
machine->Registers[VP_OUT_COL0][3]);
#endif
2001-12-14 02:50:01 +00:00
/* store the attribute output registers into the VB arrays */
COPY_4V(clip[i], machine->Registers[VP_OUT_HPOS]);
COPY_4V(color0[i], machine->Registers[VP_OUT_COL0]);
COPY_4V(color1[i], machine->Registers[VP_OUT_COL1]);
COPY_4V(bfcolor0[i], machine->Registers[VP_OUT_BFC0]);
COPY_4V(bfcolor1[i], machine->Registers[VP_OUT_BFC1]);
fog[i][0] = machine->Registers[VP_OUT_FOGC][0];
2001-12-14 02:50:01 +00:00
pointSize[i] = machine->Registers[VP_OUT_PSIZ][0];
COPY_4V(texture0[i], machine->Registers[VP_OUT_TEX0]);
COPY_4V(texture1[i], machine->Registers[VP_OUT_TEX0]);
COPY_4V(texture2[i], machine->Registers[VP_OUT_TEX0]);
COPY_4V(texture3[i], machine->Registers[VP_OUT_TEX0]);
}
VB->ClipPtr = &store->clipCoords;
VB->ClipPtr->size = 4;
VB->ClipPtr->count = VB->Count;
2001-12-14 02:50:01 +00:00
VB->ColorPtr[0] = &store->color0[0];
VB->ColorPtr[1] = &store->color0[1];
VB->SecondaryColorPtr[0] = &store->color1[0];
VB->SecondaryColorPtr[1] = &store->color1[1];
VB->AttribPtr[VERT_ATTRIB_FOG] = &store->fogCoord;
2001-12-14 02:50:01 +00:00
VB->PointSizePtr = &store->pointSize;
VB->TexCoordPtr[0] = &store->texCoord[0];
VB->TexCoordPtr[1] = &store->texCoord[1];
VB->TexCoordPtr[2] = &store->texCoord[2];
VB->TexCoordPtr[3] = &store->texCoord[3];
/* Cliptest and perspective divide. Clip functions must clear
* the clipmask.
2001-12-14 02:50:01 +00:00
*/
store->ormask = 0;
store->andmask = CLIP_ALL_BITS;
2001-12-14 02:50:01 +00:00
if (tnl->NeedNdcCoords) {
VB->NdcPtr =
_mesa_clip_tab[VB->ClipPtr->size]( VB->ClipPtr,
&store->ndcCoords,
store->clipmask,
&store->ormask,
&store->andmask );
2001-12-14 02:50:01 +00:00
}
else {
VB->NdcPtr = 0;
_mesa_clip_np_tab[VB->ClipPtr->size]( VB->ClipPtr,
0,
store->clipmask,
&store->ormask,
&store->andmask );
2001-12-14 02:50:01 +00:00
}
if (store->andmask) /* All vertices are outside the frustum */
return GL_FALSE;
2001-12-14 02:50:01 +00:00
/* This is where we'd do clip testing against the user-defined
* clipping planes, but they're not supported by vertex programs.
2001-12-14 02:50:01 +00:00
*/
VB->ClipOrMask = store->ormask;
VB->ClipMask = store->clipmask;
/* XXXX what's this?
if (VB->ClipPtr == VB->ObjPtr && (VB->importable_data & VERT_OBJ_BIT))
VB->importable_data |= VERT_CLIP;
*/
2001-12-14 02:50:01 +00:00
return GL_TRUE;
}
/* Called in place of do_lighting when the light table may have changed.
*/
static GLboolean run_validate_program( GLcontext *ctx,
struct gl_pipeline_stage *stage )
{
#if 000
/* XXX do we need any validation for vertex programs? */
2001-12-14 02:50:01 +00:00
GLuint ind = 0;
light_func *tab;
if (ctx->Visual.rgbMode) {
if (ctx->Light._NeedVertices) {
if (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)
tab = _tnl_light_spec_tab;
else
tab = _tnl_light_tab;
}
else {
if (ctx->Light.EnabledList.next == ctx->Light.EnabledList.prev)
tab = _tnl_light_fast_single_tab;
else
tab = _tnl_light_fast_tab;
}
}
else
tab = _tnl_light_ci_tab;
if (ctx->Light.ColorMaterialEnabled)
ind |= LIGHT_COLORMATERIAL;
if (ctx->Light.Model.TwoSide)
ind |= LIGHT_TWOSIDE;
VP_STAGE_DATA(stage)->light_func_tab = &tab[ind];
/* This and the above should only be done on _NEW_LIGHT:
*/
_mesa_validate_all_lighting_tables( ctx );
#endif
/* Now run the stage...
*/
stage->run = run_vp;
return stage->run( ctx, stage );
}
#if 0
static void alloc_4chan( struct gl_client_array *a, GLuint sz )
{
a->Ptr = ALIGN_MALLOC( sz * sizeof(GLchan) * 4, 32 );
a->Size = 4;
a->Type = CHAN_TYPE;
a->Stride = 0;
a->StrideB = sizeof(GLchan) * 4;
a->Enabled = 0;
a->Flags = 0;
}
#endif
static void alloc_4float( struct gl_client_array *a, GLuint sz )
{
a->Ptr = ALIGN_MALLOC( sz * sizeof(GLfloat) * 4, 32 );
a->Size = 4;
a->Type = GL_FLOAT;
a->Stride = 0;
a->StrideB = sizeof(GLfloat) * 4;
a->Enabled = 0;
a->Flags = 0;
}
/* Called the first time stage->run is called. In effect, don't
* allocate data until the first time the stage is run.
*/
static GLboolean run_init_vp( GLcontext *ctx,
struct gl_pipeline_stage *stage )
{
TNLcontext *tnl = TNL_CONTEXT(ctx);
struct vertex_buffer *VB = &(tnl->vb);
struct vp_stage_data *store;
const GLuint size = VB->Size;
GLuint i;
stage->privatePtr = MALLOC(sizeof(*store));
store = VP_STAGE_DATA(stage);
if (!store)
return GL_FALSE;
/* The output of a vertex program is: */
_mesa_vector4f_alloc( &store->clipCoords, 0, size, 32 );
_mesa_vector4f_alloc( &store->ndcCoords, 0, size, 32 );
2001-12-14 02:50:01 +00:00
alloc_4float( &store->color0[0], size );
alloc_4float( &store->color0[1], size );
alloc_4float( &store->color1[0], size );
alloc_4float( &store->color1[1], size );
for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++)
_mesa_vector4f_alloc( &store->texCoord[i], 0, VB->Size, 32 );
_mesa_vector4f_alloc( &store->fogCoord, 0, size, 32 );
2001-12-14 02:50:01 +00:00
_mesa_vector1f_alloc( &store->pointSize, 0, size, 32 );
store->clipmask = (GLubyte *) ALIGN_MALLOC(sizeof(GLubyte)*size, 32 );
2001-12-14 02:50:01 +00:00
/* Now validate the stage derived data...
*/
stage->run = run_validate_program;
return stage->run( ctx, stage );
}
/*
* Check if vertex program mode is enabled.
* If so, configure the pipeline stage's type, inputs, and outputs.
*/
static void check_vp( GLcontext *ctx, struct gl_pipeline_stage *stage )
{
stage->active = ctx->VertexProgram.Enabled;
2001-12-15 02:13:32 +00:00
2001-12-14 02:50:01 +00:00
if (stage->active) {
#if 000
if (stage->privatePtr)
stage->run = run_validate_program;
stage->inputs = VERT_NORMAL_BIT|VERT_MATERIAL;
if (ctx->Light._NeedVertices)
stage->inputs |= VERT_EYE; /* effectively, even when lighting in obj */
if (ctx->Light.ColorMaterialEnabled)
stage->inputs |= VERT_COLOR0_BIT;
stage->outputs = VERT_COLOR0_BIT;
if (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)
stage->outputs |= VERT_COLOR1_BIT;
#endif
}
}
static void dtr( struct gl_pipeline_stage *stage )
{
struct vp_stage_data *store = VP_STAGE_DATA(stage);
if (store) {
GLuint i;
_mesa_vector4f_free( &store->clipCoords );
_mesa_vector4f_free( &store->ndcCoords );
2001-12-14 02:50:01 +00:00
ALIGN_FREE( store->color0[0].Ptr );
ALIGN_FREE( store->color0[1].Ptr );
ALIGN_FREE( store->color1[0].Ptr );
ALIGN_FREE( store->color1[1].Ptr );
for (i = 0 ; i < MAX_TEXTURE_UNITS ; i++)
if (store->texCoord[i].data)
_mesa_vector4f_free( &store->texCoord[i] );
_mesa_vector4f_free( &store->fogCoord );
2001-12-14 02:50:01 +00:00
_mesa_vector1f_free( &store->pointSize );
ALIGN_FREE( store->clipmask );
2001-12-14 02:50:01 +00:00
FREE( store );
stage->privatePtr = 0;
}
}
const struct gl_pipeline_stage _tnl_vertex_program_stage =
{
"vertex-program",
_NEW_ALL, /*XXX FIX */ /* recheck */
_NEW_ALL, /*XXX FIX */ /* recalc -- modelview dependency
* otherwise not captured by inputs
* (which may be VERT_OBJ_BIT) */
GL_FALSE, /* active */
0, /* inputs */
VERT_CLIP | VERT_COLOR0_BIT, /* outputs */
0, /* changed_inputs */
NULL, /* private_data */
dtr, /* destroy */
check_vp, /* check */
run_init_vp /* run -- initially set to ctr */
};