1/*
2 * Mesa 3-D graphics library
3 * Version:  7.0
4 *
5 * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 *    Brian Paul
26 */
27
28#include "main/glheader.h"
29#include "main/mtypes.h"
30#include "main/dd.h"
31#include "main/imports.h"
32#include "t_context.h"
33#include "t_pipeline.h"
34
35
36struct point_stage_data {
37   GLvector4f PointSize;
38};
39
40#define POINT_STAGE_DATA(stage) ((struct point_stage_data *)stage->privatePtr)
41
42
43/**
44 * Compute point size for each vertex from the vertex eye-space Z
45 * coordinate and the point size attenuation factors.
46 * Only done when point size attenuation is enabled and vertex program is
47 * disabled.
48 */
49static GLboolean
50run_point_stage(struct gl_context *ctx, struct tnl_pipeline_stage *stage)
51{
52   if (ctx->Point._Attenuated && !ctx->VertexProgram._Current) {
53      struct point_stage_data *store = POINT_STAGE_DATA(stage);
54      struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
55      const GLfloat *eyeCoord = (GLfloat *) VB->EyePtr->data + 2;
56      const GLint eyeCoordStride = VB->EyePtr->stride / sizeof(GLfloat);
57      const GLfloat p0 = ctx->Point.Params[0];
58      const GLfloat p1 = ctx->Point.Params[1];
59      const GLfloat p2 = ctx->Point.Params[2];
60      const GLfloat pointSize = ctx->Point.Size;
61      GLfloat (*size)[4] = store->PointSize.data;
62      GLuint i;
63
64      for (i = 0; i < VB->Count; i++) {
65         const GLfloat dist = FABSF(*eyeCoord);
66         const GLfloat q = p0 + dist * (p1 + dist * p2);
67         const GLfloat atten = (q != 0.0F) ? INV_SQRTF(q) : 1.0F;
68         size[i][0] = pointSize * atten; /* clamping done in rasterization */
69         eyeCoord += eyeCoordStride;
70      }
71
72      VB->AttribPtr[_TNL_ATTRIB_POINTSIZE] = &store->PointSize;
73   }
74
75   return GL_TRUE;
76}
77
78
79static GLboolean
80alloc_point_data(struct gl_context *ctx, struct tnl_pipeline_stage *stage)
81{
82   struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
83   struct point_stage_data *store;
84   stage->privatePtr = malloc(sizeof(*store));
85   store = POINT_STAGE_DATA(stage);
86   if (!store)
87      return GL_FALSE;
88
89   _mesa_vector4f_alloc( &store->PointSize, 0, VB->Size, 32 );
90   return GL_TRUE;
91}
92
93
94static void
95free_point_data(struct tnl_pipeline_stage *stage)
96{
97   struct point_stage_data *store = POINT_STAGE_DATA(stage);
98   if (store) {
99      _mesa_vector4f_free( &store->PointSize );
100      free( store );
101      stage->privatePtr = NULL;
102   }
103}
104
105
106const struct tnl_pipeline_stage _tnl_point_attenuation_stage =
107{
108   "point size attenuation",	/* name */
109   NULL,			/* stage private data */
110   alloc_point_data,		/* alloc data */
111   free_point_data,		/* destructor */
112   NULL,
113   run_point_stage		/* run */
114};
115