softpipe: pass sampler state to shadow_compare()
[mesa.git] / src / gallium / drivers / softpipe / sp_tex_sample.c
index 631c60966c2e423d744a883a870901e40850e186..6af0fcb92d5389a1aa0e6ffb197571de11c6616d 100644 (file)
@@ -2,6 +2,7 @@
  * 
  * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
  * All Rights Reserved.
+ * Copyright 2008 VMware, Inc.  All rights reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the
  */
 
 #include "sp_context.h"
-#include "sp_headers.h"
+#include "sp_quad.h"
 #include "sp_surface.h"
 #include "sp_texture.h"
 #include "sp_tex_sample.h"
 #include "sp_tile_cache.h"
 #include "pipe/p_context.h"
 #include "pipe/p_defines.h"
-#include "tgsi/tgsi_exec.h"
 #include "util/u_math.h"
 #include "util/u_memory.h"
 
 
+
 /*
  * Note, the FRAC macro has to work perfectly.  Otherwise you'll sometimes
  * see 1-pixel bands of improperly weighted linear-filtered textures.
@@ -583,6 +584,7 @@ choose_mipmap_levels(const struct pipe_texture *texture,
                      const float s[QUAD_SIZE],
                      const float t[QUAD_SIZE],
                      const float p[QUAD_SIZE],
+                     boolean computeLambda,
                      float lodbias,
                      unsigned *level0, unsigned *level1, float *levelBlend,
                      unsigned *imgFilter)
@@ -611,7 +613,7 @@ choose_mipmap_levels(const struct pipe_texture *texture,
    else {
       float lambda;
 
-      if (1)
+      if (computeLambda)
          /* fragment shader */
          lambda = compute_lambda(texture, sampler, s, t, p, lodbias);
       else
@@ -705,13 +707,13 @@ get_texel(const struct tgsi_sampler *tgsi_sampler,
  * RGBA channels.  We look at the red channel here.
  */
 static INLINE void
-shadow_compare(uint compare_func,
+shadow_compare(const struct pipe_sampler_state *sampler,
                float rgba[NUM_CHANNELS][QUAD_SIZE],
                const float p[QUAD_SIZE],
                uint j)
 {
    int k;
-   switch (compare_func) {
+   switch (sampler->compare_func) {
    case PIPE_FUNC_LESS:
       k = p[j] < rgba[0][j];
       break;
@@ -742,7 +744,9 @@ shadow_compare(uint compare_func,
       break;
    }
 
+   /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
    rgba[0][j] = rgba[1][j] = rgba[2][j] = (float) k;
+   rgba[3][j] = 1.0F;
 }
 
 
@@ -755,6 +759,7 @@ sp_get_samples_2d_common(const struct tgsi_sampler *tgsi_sampler,
                          const float s[QUAD_SIZE],
                          const float t[QUAD_SIZE],
                          const float p[QUAD_SIZE],
+                         boolean computeLambda,
                          float lodbias,
                          float rgba[NUM_CHANNELS][QUAD_SIZE],
                          const unsigned faces[4])
@@ -764,12 +769,11 @@ sp_get_samples_2d_common(const struct tgsi_sampler *tgsi_sampler,
    const uint unit = samp->unit;
    const struct pipe_texture *texture = sp->texture[unit];
    const struct pipe_sampler_state *sampler = sp->sampler[unit];
-   const uint compare_func = sampler->compare_func;
    unsigned level0, level1, j, imgFilter;
    int width, height;
    float levelBlend;
 
-   choose_mipmap_levels(texture, sampler, s, t, p, lodbias,
+   choose_mipmap_levels(texture, sampler, s, t, p, computeLambda, lodbias,
                         &level0, &level1, &levelBlend, &imgFilter);
 
    assert(sampler->normalized_coords);
@@ -789,7 +793,7 @@ sp_get_samples_2d_common(const struct tgsi_sampler *tgsi_sampler,
          for (j = 0; j < QUAD_SIZE; j++) {
             get_texel(tgsi_sampler, faces[j], level0, x[j], y[j], 0, rgba, j);
             if (sampler->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) {
-               shadow_compare(compare_func, rgba, p, j);
+               shadow_compare(sampler, rgba, p, j);
             }
 
             if (level0 != level1) {
@@ -801,7 +805,7 @@ sp_get_samples_2d_common(const struct tgsi_sampler *tgsi_sampler,
                get_texel(tgsi_sampler, faces[j], level1, x[j], y[j], 0,
                          rgba2, j);
                if (sampler->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE){
-                  shadow_compare(compare_func, rgba2, p, j);
+                  shadow_compare(sampler, rgba2, p, j);
                }
 
                for (c = 0; c < NUM_CHANNELS; c++) {
@@ -828,10 +832,10 @@ sp_get_samples_2d_common(const struct tgsi_sampler *tgsi_sampler,
             get_texel(tgsi_sampler, faces[j], level0, x0[j], y1[j], 0, tx, 2);
             get_texel(tgsi_sampler, faces[j], level0, x1[j], y1[j], 0, tx, 3);
             if (sampler->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) {
-               shadow_compare(compare_func, tx, p, 0);
-               shadow_compare(compare_func, tx, p, 1);
-               shadow_compare(compare_func, tx, p, 2);
-               shadow_compare(compare_func, tx, p, 3);
+               shadow_compare(sampler, tx, p, 0);
+               shadow_compare(sampler, tx, p, 1);
+               shadow_compare(sampler, tx, p, 2);
+               shadow_compare(sampler, tx, p, 3);
             }
 
             /* interpolate R, G, B, A */
@@ -853,10 +857,10 @@ sp_get_samples_2d_common(const struct tgsi_sampler *tgsi_sampler,
                get_texel(tgsi_sampler, faces[j], level1, x0[j], y1[j], 0, tx, 2);
                get_texel(tgsi_sampler, faces[j], level1, x1[j], y1[j], 0, tx, 3);
                if (sampler->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE){
-                  shadow_compare(compare_func, tx, p, 0);
-                  shadow_compare(compare_func, tx, p, 1);
-                  shadow_compare(compare_func, tx, p, 2);
-                  shadow_compare(compare_func, tx, p, 3);
+                  shadow_compare(sampler, tx, p, 0);
+                  shadow_compare(sampler, tx, p, 1);
+                  shadow_compare(sampler, tx, p, 2);
+                  shadow_compare(sampler, tx, p, 3);
                }
 
                /* interpolate R, G, B, A */
@@ -883,12 +887,14 @@ sp_get_samples_1d(const struct tgsi_sampler *sampler,
                   const float s[QUAD_SIZE],
                   const float t[QUAD_SIZE],
                   const float p[QUAD_SIZE],
+                  boolean computeLambda,
                   float lodbias,
                   float rgba[NUM_CHANNELS][QUAD_SIZE])
 {
    static const unsigned faces[4] = {0, 0, 0, 0};
    static const float tzero[4] = {0, 0, 0, 0};
-   sp_get_samples_2d_common(sampler, s, tzero, NULL, lodbias, rgba, faces);
+   sp_get_samples_2d_common(sampler, s, tzero, NULL,
+                            computeLambda, lodbias, rgba, faces);
 }
 
 
@@ -897,11 +903,13 @@ sp_get_samples_2d(const struct tgsi_sampler *sampler,
                   const float s[QUAD_SIZE],
                   const float t[QUAD_SIZE],
                   const float p[QUAD_SIZE],
+                  boolean computeLambda,
                   float lodbias,
                   float rgba[NUM_CHANNELS][QUAD_SIZE])
 {
    static const unsigned faces[4] = {0, 0, 0, 0};
-   sp_get_samples_2d_common(sampler, s, t, p, lodbias, rgba, faces);
+   sp_get_samples_2d_common(sampler, s, t, p,
+                            computeLambda, lodbias, rgba, faces);
 }
 
 
@@ -910,6 +918,7 @@ sp_get_samples_3d(const struct tgsi_sampler *tgsi_sampler,
                   const float s[QUAD_SIZE],
                   const float t[QUAD_SIZE],
                   const float p[QUAD_SIZE],
+                  boolean computeLambda,
                   float lodbias,
                   float rgba[NUM_CHANNELS][QUAD_SIZE])
 {
@@ -924,7 +933,7 @@ sp_get_samples_3d(const struct tgsi_sampler *tgsi_sampler,
    float levelBlend;
    const uint face = 0;
 
-   choose_mipmap_levels(texture, sampler, s, t, p, lodbias,
+   choose_mipmap_levels(texture, sampler, s, t, p, computeLambda, lodbias,
                         &level0, &level1, &levelBlend, &imgFilter);
 
    assert(sampler->normalized_coords);
@@ -1037,6 +1046,7 @@ sp_get_samples_cube(const struct tgsi_sampler *sampler,
                     const float s[QUAD_SIZE],
                     const float t[QUAD_SIZE],
                     const float p[QUAD_SIZE],
+                    boolean computeLambda,
                     float lodbias,
                     float rgba[NUM_CHANNELS][QUAD_SIZE])
 {
@@ -1045,7 +1055,8 @@ sp_get_samples_cube(const struct tgsi_sampler *sampler,
    for (j = 0; j < QUAD_SIZE; j++) {
       faces[j] = choose_cube_face(s[j], t[j], p[j], ssss + j, tttt + j);
    }
-   sp_get_samples_2d_common(sampler, ssss, tttt, NULL, lodbias, rgba, faces);
+   sp_get_samples_2d_common(sampler, ssss, tttt, NULL,
+                            computeLambda, lodbias, rgba, faces);
 }
 
 
@@ -1054,6 +1065,7 @@ sp_get_samples_rect(const struct tgsi_sampler *tgsi_sampler,
                     const float s[QUAD_SIZE],
                     const float t[QUAD_SIZE],
                     const float p[QUAD_SIZE],
+                    boolean computeLambda,
                     float lodbias,
                     float rgba[NUM_CHANNELS][QUAD_SIZE])
 {
@@ -1063,12 +1075,11 @@ sp_get_samples_rect(const struct tgsi_sampler *tgsi_sampler,
    const struct pipe_texture *texture = sp->texture[unit];
    const struct pipe_sampler_state *sampler = sp->sampler[unit];
    const uint face = 0;
-   const uint compare_func = sampler->compare_func;
    unsigned level0, level1, j, imgFilter;
    int width, height;
    float levelBlend;
 
-   choose_mipmap_levels(texture, sampler, s, t, p, lodbias,
+   choose_mipmap_levels(texture, sampler, s, t, p, computeLambda, lodbias,
                         &level0, &level1, &levelBlend, &imgFilter);
 
    /* texture RECTS cannot be mipmapped */
@@ -1088,7 +1099,7 @@ sp_get_samples_rect(const struct tgsi_sampler *tgsi_sampler,
          for (j = 0; j < QUAD_SIZE; j++) {
             get_texel(tgsi_sampler, face, level0, x[j], y[j], 0, rgba, j);
             if (sampler->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) {
-               shadow_compare(compare_func, rgba, p, j);
+               shadow_compare(sampler, rgba, p, j);
             }
          }
       }
@@ -1108,10 +1119,10 @@ sp_get_samples_rect(const struct tgsi_sampler *tgsi_sampler,
             get_texel(tgsi_sampler, face, level0, x0[j], y1[j], 0, tx, 2);
             get_texel(tgsi_sampler, face, level0, x1[j], y1[j], 0, tx, 3);
             if (sampler->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) {
-               shadow_compare(compare_func, tx, p, 0);
-               shadow_compare(compare_func, tx, p, 1);
-               shadow_compare(compare_func, tx, p, 2);
-               shadow_compare(compare_func, tx, p, 3);
+               shadow_compare(sampler, tx, p, 0);
+               shadow_compare(sampler, tx, p, 1);
+               shadow_compare(sampler, tx, p, 2);
+               shadow_compare(sampler, tx, p, 3);
             }
             for (c = 0; c < 4; c++) {
                rgba[c][j] = lerp_2d(xw[j], yw[j],
@@ -1127,14 +1138,14 @@ sp_get_samples_rect(const struct tgsi_sampler *tgsi_sampler,
 
 
 /**
- * Called via tgsi_sampler::get_samples()
- * Get four filtered RGBA values from the sampler's texture.
+ * Common code for vertex/fragment program texture sampling.
  */
-void
+static INLINE void
 sp_get_samples(struct tgsi_sampler *tgsi_sampler,
                const float s[QUAD_SIZE],
                const float t[QUAD_SIZE],
                const float p[QUAD_SIZE],
+               boolean computeLambda,
                float lodbias,
                float rgba[NUM_CHANNELS][QUAD_SIZE])
 {
@@ -1150,21 +1161,21 @@ sp_get_samples(struct tgsi_sampler *tgsi_sampler,
    switch (texture->target) {
    case PIPE_TEXTURE_1D:
       assert(sampler->normalized_coords);
-      sp_get_samples_1d(tgsi_sampler, s, t, p, lodbias, rgba);
+      sp_get_samples_1d(tgsi_sampler, s, t, p, computeLambda, lodbias, rgba);
       break;
    case PIPE_TEXTURE_2D:
       if (sampler->normalized_coords)
-         sp_get_samples_2d(tgsi_sampler, s, t, p, lodbias, rgba);
+         sp_get_samples_2d(tgsi_sampler, s, t, p, computeLambda, lodbias, rgba);
       else
-         sp_get_samples_rect(tgsi_sampler, s, t, p, lodbias, rgba);
+         sp_get_samples_rect(tgsi_sampler, s, t, p, computeLambda, lodbias, rgba);
       break;
    case PIPE_TEXTURE_3D:
       assert(sampler->normalized_coords);
-      sp_get_samples_3d(tgsi_sampler, s, t, p, lodbias, rgba);
+      sp_get_samples_3d(tgsi_sampler, s, t, p, computeLambda, lodbias, rgba);
       break;
    case PIPE_TEXTURE_CUBE:
       assert(sampler->normalized_coords);
-      sp_get_samples_cube(tgsi_sampler, s, t, p, lodbias, rgba);
+      sp_get_samples_cube(tgsi_sampler, s, t, p, computeLambda, lodbias, rgba);
       break;
    default:
       assert(0);
@@ -1185,3 +1196,34 @@ sp_get_samples(struct tgsi_sampler *tgsi_sampler,
 #endif
 }
 
+
+/**
+ * Called via tgsi_sampler::get_samples() when running a fragment shader.
+ * Get four filtered RGBA values from the sampler's texture.
+ */
+void
+sp_get_samples_fragment(struct tgsi_sampler *tgsi_sampler,
+                        const float s[QUAD_SIZE],
+                        const float t[QUAD_SIZE],
+                        const float p[QUAD_SIZE],
+                        float lodbias,
+                        float rgba[NUM_CHANNELS][QUAD_SIZE])
+{
+   sp_get_samples(tgsi_sampler, s, t, p, TRUE, lodbias, rgba);
+}
+
+
+/**
+ * Called via tgsi_sampler::get_samples() when running a vertex shader.
+ * Get four filtered RGBA values from the sampler's texture.
+ */
+void
+sp_get_samples_vertex(struct tgsi_sampler *tgsi_sampler,
+                      const float s[QUAD_SIZE],
+                      const float t[QUAD_SIZE],
+                      const float p[QUAD_SIZE],
+                      float lodbias,
+                      float rgba[NUM_CHANNELS][QUAD_SIZE])
+{
+   sp_get_samples(tgsi_sampler, s, t, p, FALSE, lodbias, rgba);
+}