Implement gl_PointCoord attribute for GLSL fragment shaders.
authorBrian <brian.paul@tungstengraphics.com>
Wed, 24 Oct 2007 17:37:05 +0000 (11:37 -0600)
committerBrian <brian.paul@tungstengraphics.com>
Wed, 24 Oct 2007 17:37:05 +0000 (11:37 -0600)
Contains the normalized fragment position within a point sprite.

src/mesa/shader/slang/slang_codegen.c
src/mesa/shader/slang/slang_compile.c
src/mesa/swrast/s_fog.c
src/mesa/swrast/s_fragprog.c
src/mesa/swrast/s_points.c
src/mesa/swrast/s_span.c

index d368009ca50a020fa399d469323774f814271623..a44e42b6d6de3d4b8c7298799aa4a8a70a9ed98a 100644 (file)
@@ -282,6 +282,8 @@ sampler_to_texture_index(const slang_type_specifier_type type)
 }
 
 
+#define SWIZZLE_ZWWW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
+
 /**
  * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
  * a vertex or fragment program input variable.  Return -1 if the input
@@ -316,9 +318,11 @@ _slang_input_index(const char *name, GLenum target, GLuint *swizzleOut)
       { "gl_FragCoord", FRAG_ATTRIB_WPOS, SWIZZLE_NOOP },
       { "gl_Color", FRAG_ATTRIB_COL0, SWIZZLE_NOOP },
       { "gl_SecondaryColor", FRAG_ATTRIB_COL1, SWIZZLE_NOOP },
-      { "gl_FogFragCoord", FRAG_ATTRIB_FOGC, SWIZZLE_XXXX },
       { "gl_TexCoord", FRAG_ATTRIB_TEX0, SWIZZLE_NOOP },
+      /* note: we're packing several quantities into the fogcoord vector */
+      { "gl_FogFragCoord", FRAG_ATTRIB_FOGC, SWIZZLE_XXXX },
       { "gl_FrontFacing", FRAG_ATTRIB_FOGC, SWIZZLE_YYYY }, /*XXX*/
+      { "gl_PointCoord", FRAG_ATTRIB_FOGC, SWIZZLE_ZWWW },
       { NULL, 0, SWIZZLE_NOOP }
    };
    GLuint i;
index 4e29e8dcc90adcbb406dbec9a2c4d40b4ea09a4a..2be89a5ce05235c0915d1870cb5637fc6960b88d 100644 (file)
@@ -1983,6 +1983,10 @@ static const byte slang_120_core_gc[] = {
 #include "library/slang_120_core_gc.h"
 };
 
+static const byte slang_120_fragment_gc[] = {
+#include "library/slang_builtin_120_fragment_gc.h"
+};
+
 static const byte slang_common_builtin_gc[] = {
 #include "library/slang_common_builtin_gc.h"
 };
@@ -2059,6 +2063,13 @@ compile_object(grammar * id, const char *source, slang_code_object * object,
                              SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
                              &object->builtin[SLANG_BUILTIN_COMMON], NULL))
             return GL_FALSE;
+#if FEATURE_ARB_shading_language_120
+         if (!compile_binary(slang_120_fragment_gc,
+                             &object->builtin[SLANG_BUILTIN_TARGET],
+                             SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
+                             &object->builtin[SLANG_BUILTIN_COMMON], NULL))
+            return GL_FALSE;
+#endif
       }
       else if (type == SLANG_UNIT_VERTEX_SHADER) {
          if (!compile_binary(slang_vertex_builtin_gc,
index ed47964a66aeae42ada37b89c20fc4f78fa85bc6..7b143f6e5bbad78c714beedcccd448192b843a45 100644 (file)
@@ -168,7 +168,6 @@ _swrast_fog_rgba_span( const GLcontext *ctx, SWspan *span )
    GLfloat rFog, gFog, bFog;
 
    ASSERT(swrast->_FogEnabled);
-   ASSERT(swrast->_ActiveAttribMask & FRAG_BIT_FOGC);
    ASSERT(span->arrayMask & SPAN_RGBA);
 
    /* compute (scaled) fog color */
index 14c9868c1803ba4adfef8acc5757d56438614e30..6656ebc0d0cbd4e60b6ba75b299cd7ffc9baca3b 100644 (file)
@@ -113,6 +113,7 @@ init_machine(GLcontext *ctx, struct gl_program_machine *machine,
    machine->DerivY = (GLfloat (*)[4]) span->attrStepY;
    machine->NumDeriv = FRAG_ATTRIB_MAX;
 
+   /* if running a GLSL program (not ARB_fragment_program) */
    if (ctx->Shader.CurrentProgram) {
       /* Store front/back facing value in register FOGC.Y */
       machine->Attribs[FRAG_ATTRIB_FOGC][col][1] = (GLfloat) ctx->_Facing;
index d54adc8fd18f3cdaf5e191bf0e8c214eaba0fddb..ce73365a4ea1bd41a0fe9a84caefec0f4175b9d1 100644 (file)
@@ -55,7 +55,7 @@ sprite_point(GLcontext *ctx, const SWvertex *vert)
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
    SWspan span;
    GLfloat size;
-   GLuint tCoords[MAX_TEXTURE_COORD_UNITS];
+   GLuint tCoords[MAX_TEXTURE_COORD_UNITS + 1];
    GLuint numTcoords = 0;
    GLfloat t0, dtdy;
 
@@ -99,57 +99,71 @@ sprite_point(GLcontext *ctx, const SWvertex *vert)
    span.attrStepX[FRAG_ATTRIB_WPOS][3] = 0.0F;
    span.attrStepY[FRAG_ATTRIB_WPOS][3] = 0.0F;
 
-   ATTRIB_LOOP_BEGIN
-      if (attr >= FRAG_ATTRIB_TEX0 && attr < FRAG_ATTRIB_VAR0) {
-         const GLuint u = attr - FRAG_ATTRIB_TEX0;
-         /* a texcoord */
-         if (ctx->Point.CoordReplace[u]) {
-            GLfloat s, r, dsdx;
-
-            s = 0.0;
-            dsdx = 1.0 / size;
-
-            if (ctx->Point.SpriteOrigin == GL_LOWER_LEFT) {
-               t0 = 0.0;
-               dtdy = 1.0 / size;
-            }
-            else {
-               /* GL_UPPER_LEFT */
-               t0 = 1.0;
-               dtdy = -1.0 / size;
-            }
-            tCoords[numTcoords++] = attr;
-
-            if (ctx->Point.SpriteRMode == GL_ZERO)
-               r = 0.0F;
-            else if (ctx->Point.SpriteRMode == GL_S)
-               r = vert->attrib[attr][0];
-            else /* GL_R */
-               r = vert->attrib[attr][2];
-
-            span.attrStart[attr][0] = s;
-            span.attrStart[attr][1] = 0.0; /* overwritten below */
-            span.attrStart[attr][2] = r;
-            span.attrStart[attr][3] = 1.0;
-
-            span.attrStepX[attr][0] = dsdx;
-            span.attrStepX[attr][1] = 0.0;
-            span.attrStepX[attr][2] = 0.0;
-            span.attrStepX[attr][3] = 0.0;
-
-            span.attrStepY[attr][0] = 0.0;
-            span.attrStepY[attr][1] = dtdy;
-            span.attrStepY[attr][2] = 0.0;
-            span.attrStepY[attr][3] = 0.0;
+   {
+      GLfloat s, r, dsdx;
+
+      /* texcoord / pointcoord interpolants */
+      s = 0.0;
+      dsdx = 1.0 / size;
+      if (ctx->Point.SpriteOrigin == GL_LOWER_LEFT) {
+         t0 = 0.0;
+         dtdy = 1.0 / size;
+      }
+      else {
+         /* GL_UPPER_LEFT */
+         t0 = 1.0;
+         dtdy = -1.0 / size;
+      }
 
+      ATTRIB_LOOP_BEGIN
+         if (attr >= FRAG_ATTRIB_TEX0 && attr < FRAG_ATTRIB_VAR0) {
+            const GLuint u = attr - FRAG_ATTRIB_TEX0;
+            /* a texcoord */
+            if (ctx->Point.CoordReplace[u]) {
+               tCoords[numTcoords++] = attr;
+
+               if (ctx->Point.SpriteRMode == GL_ZERO)
+                  r = 0.0F;
+               else if (ctx->Point.SpriteRMode == GL_S)
+                  r = vert->attrib[attr][0];
+               else /* GL_R */
+                  r = vert->attrib[attr][2];
+
+               span.attrStart[attr][0] = s;
+               span.attrStart[attr][1] = 0.0; /* overwritten below */
+               span.attrStart[attr][2] = r;
+               span.attrStart[attr][3] = 1.0;
+
+               span.attrStepX[attr][0] = dsdx;
+               span.attrStepX[attr][1] = 0.0;
+               span.attrStepX[attr][2] = 0.0;
+               span.attrStepX[attr][3] = 0.0;
+
+               span.attrStepY[attr][0] = 0.0;
+               span.attrStepY[attr][1] = dtdy;
+               span.attrStepY[attr][2] = 0.0;
+               span.attrStepY[attr][3] = 0.0;
+
+               continue;
+            }
+         }
+         else if (attr == FRAG_ATTRIB_FOGC) {
+            /* GLSL gl_PointCoord is stored in fog.zw */
+            span.attrStart[FRAG_ATTRIB_FOGC][2] = 0.0;
+            span.attrStart[FRAG_ATTRIB_FOGC][3] = 0.0; /* t0 set below */
+            span.attrStepX[FRAG_ATTRIB_FOGC][2] = dsdx;
+            span.attrStepX[FRAG_ATTRIB_FOGC][3] = 0.0;
+            span.attrStepY[FRAG_ATTRIB_FOGC][2] = 0.0;
+            span.attrStepY[FRAG_ATTRIB_FOGC][3] = dtdy;
+            tCoords[numTcoords++] = FRAG_ATTRIB_FOGC;
             continue;
          }
-      }
-      /* use vertex's texcoord/attrib */
-      COPY_4V(span.attrStart[attr], vert->attrib[attr]);
-      ASSIGN_4V(span.attrStepX[attr], 0, 0, 0, 0);
-      ASSIGN_4V(span.attrStepY[attr], 0, 0, 0, 0);
-   ATTRIB_LOOP_END
+         /* use vertex's texcoord/attrib */
+         COPY_4V(span.attrStart[attr], vert->attrib[attr]);
+         ASSIGN_4V(span.attrStepX[attr], 0, 0, 0, 0);
+         ASSIGN_4V(span.attrStepY[attr], 0, 0, 0, 0);
+      ATTRIB_LOOP_END;
+   }
 
    /* compute pos, bounds and render */
    {
@@ -184,7 +198,10 @@ sprite_point(GLcontext *ctx, const SWvertex *vert)
          GLuint i;
          /* setup texcoord T for this row */
          for (i = 0; i < numTcoords; i++) {
-            span.attrStart[tCoords[i]][1] = tcoord;
+            if (tCoords[i] == FRAG_ATTRIB_FOGC)
+               span.attrStart[FRAG_ATTRIB_FOGC][3] = tcoord;
+            else
+               span.attrStart[tCoords[i]][1] = tcoord;
          }
 
          /* these might get changed by span clipping */
index f1e58bd3d81bbfff2d361fcfb35f28a4093e60c3..000e192a4a52cefbf150e47cec7ce3b3a8beae1f 100644 (file)
@@ -1183,7 +1183,7 @@ shade_texture_span(GLcontext *ctx, SWspan *span)
       if (span->primitive == GL_BITMAP && span->array->ChanType != GL_FLOAT) {
          convert_color_type(span, GL_FLOAT, 0);
       }
-      if (span->primitive != GL_POINT) {
+      if (span->primitive != GL_POINT || ctx->Point.PointSprite) {
          /* for points, we populated the arrays already */
          interpolate_active_attribs(ctx, span, ~0);
       }