From 02c20c4e0638bad741c4490e57a02f76bc1a7e37 Mon Sep 17 00:00:00 2001
From: Dave Airlie <airlied@redhat.com>
Date: Sat, 17 Apr 2010 17:06:06 +1000
Subject: [PATCH] gallium: initial depth mode support - WIP

Initial attempt to add softpipe + gallium depth mode, trying to pass piglit
depth-tex-modes. fail so far.
---
 src/gallium/drivers/softpipe/sp_tex_sample.c |   41 +++++++++++++++++++++++--
 src/gallium/include/pipe/p_defines.h         |    3 ++
 src/gallium/include/pipe/p_state.h           |    1 +
 src/mesa/state_tracker/st_atom.h             |    1 +
 src/mesa/state_tracker/st_atom_depth.c       |   16 ++++++++++
 src/mesa/state_tracker/st_atom_sampler.c     |    1 +
 6 files changed, 59 insertions(+), 4 deletions(-)

diff --git a/src/gallium/drivers/softpipe/sp_tex_sample.c b/src/gallium/drivers/softpipe/sp_tex_sample.c
index ff83c66..61b238c 100644
--- a/src/gallium/drivers/softpipe/sp_tex_sample.c
+++ b/src/gallium/drivers/softpipe/sp_tex_sample.c
@@ -1531,6 +1531,24 @@ sample_compare(struct tgsi_sampler *tgsi_sampler,
 
    samp->mip_filter(tgsi_sampler, s, t, p, c0, control, rgba);
 
+   if (!sampler->compare_mode) {
+      switch (sampler->depth_mode) {
+      case PIPE_TEX_DEPTH_MODE_LUMINANCE:
+	 for (j = 0; j < 4; j++) {
+	    rgba[3][j] = 1.0F;
+	 }
+	 break;
+      case PIPE_TEX_DEPTH_MODE_INTENSITY:
+	 break;
+      case PIPE_TEX_DEPTH_MODE_ALPHA:
+	 for (j = 0; j < 4; j++) {
+	    rgba[0][j] = rgba[1][j] = rgba[2][j] = 0.0f;
+	 }
+	 break;
+      }
+      return;
+   }
+
    /**
     * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
     * When we sampled the depth texture, the depth value was put into all
@@ -1591,9 +1609,24 @@ sample_compare(struct tgsi_sampler *tgsi_sampler,
    val = 0.25F * (k0 + k1 + k2 + k3);
 
    /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
-   for (j = 0; j < 4; j++) {
-      rgba[0][j] = rgba[1][j] = rgba[2][j] = val;
-      rgba[3][j] = 1.0F;
+   switch (sampler->depth_mode) {
+   case PIPE_TEX_DEPTH_MODE_LUMINANCE:
+      for (j = 0; j < 4; j++) {
+	 rgba[0][j] = rgba[1][j] = rgba[2][j] = val;
+	 rgba[3][j] = 1.0F;
+      }
+      break;
+   case PIPE_TEX_DEPTH_MODE_INTENSITY:
+      for (j = 0; j < 4; j++) {
+       rgba[0][j] = rgba[1][j] = rgba[2][j] = rgba[3][j] = val;
+      }
+      break;
+   case PIPE_TEX_DEPTH_MODE_ALPHA:
+      for (j = 0; j < 4; j++) {
+	 rgba[0][j] = rgba[1][j] = rgba[2][j] = 0.0f;
+       rgba[3][j] = val;
+      }
+      break;
    }
 }
 
@@ -1957,7 +1990,7 @@ sp_create_sampler_varient( const struct pipe_sampler_state *sampler,
       break;
    }
 
-   if (sampler->compare_mode != PIPE_TEX_COMPARE_NONE) {
+   if (sampler->compare_mode != PIPE_TEX_COMPARE_NONE || sampler->depth_mode) {
       samp->compare = sample_compare;
    }
    else {
diff --git a/src/gallium/include/pipe/p_defines.h b/src/gallium/include/pipe/p_defines.h
index 0a4bd58..1bf45da 100644
--- a/src/gallium/include/pipe/p_defines.h
+++ b/src/gallium/include/pipe/p_defines.h
@@ -176,6 +176,9 @@ enum pipe_texture_target {
 #define PIPE_TEX_COMPARE_NONE          0
 #define PIPE_TEX_COMPARE_R_TO_TEXTURE  1
 
+#define PIPE_TEX_DEPTH_MODE_LUMINANCE 0
+#define PIPE_TEX_DEPTH_MODE_INTENSITY 1
+#define PIPE_TEX_DEPTH_MODE_ALPHA 2
 
 /**
  * Surface layout -- a hint?  Or some driver-internal poking out into
diff --git a/src/gallium/include/pipe/p_state.h b/src/gallium/include/pipe/p_state.h
index 24cb266..312b428 100644
--- a/src/gallium/include/pipe/p_state.h
+++ b/src/gallium/include/pipe/p_state.h
@@ -257,6 +257,7 @@ struct pipe_sampler_state
    unsigned mag_img_filter:2;    /**< PIPE_TEX_FILTER_x */
    unsigned compare_mode:1;      /**< PIPE_TEX_COMPARE_x */
    unsigned compare_func:3;      /**< PIPE_FUNC_x */
+   unsigned depth_mode:2;        /**< PIPE_TEX_DEPTH_MODE_x */
    unsigned normalized_coords:1; /**< Are coords normalized to [0,1]? */
    unsigned max_anisotropy:6;
    float lod_bias;               /**< LOD/lambda bias */
diff --git a/src/mesa/state_tracker/st_atom.h b/src/mesa/state_tracker/st_atom.h
index f34b492..9867f1f 100644
--- a/src/mesa/state_tracker/st_atom.h
+++ b/src/mesa/state_tracker/st_atom.h
@@ -63,5 +63,6 @@ extern const struct st_tracked_state st_update_pixel_transfer;
 
 
 uint st_compare_func_to_pipe(GLenum func);
+uint st_depth_mode_to_pipe(GLenum mode);
 
 #endif
diff --git a/src/mesa/state_tracker/st_atom_depth.c b/src/mesa/state_tracker/st_atom_depth.c
index 3c07afb..c70ec43 100644
--- a/src/mesa/state_tracker/st_atom_depth.c
+++ b/src/mesa/state_tracker/st_atom_depth.c
@@ -60,6 +60,22 @@ st_compare_func_to_pipe(GLenum func)
    return func - GL_NEVER;
 }
 
+GLuint
+st_depth_mode_to_pipe(GLenum mode)
+{
+   switch(mode) {
+
+   case GL_INTENSITY:
+      return PIPE_TEX_DEPTH_MODE_INTENSITY;
+   case GL_ALPHA:
+      return PIPE_TEX_DEPTH_MODE_ALPHA;
+   case GL_LUMINANCE:
+      return PIPE_TEX_DEPTH_MODE_LUMINANCE;      
+   default:
+      assert(0);
+      return 0;
+   }
+}
 
 /**
  * Convert GLenum stencil op tokens to pipe tokens.
diff --git a/src/mesa/state_tracker/st_atom_sampler.c b/src/mesa/state_tracker/st_atom_sampler.c
index a8262a5..24e3081 100644
--- a/src/mesa/state_tracker/st_atom_sampler.c
+++ b/src/mesa/state_tracker/st_atom_sampler.c
@@ -219,6 +219,7 @@ update_samplers(struct st_context *st)
             sampler->compare_func
                = st_compare_func_to_pipe(texobj->CompareFunc);
          }
+	 sampler->depth_mode = st_depth_mode_to_pipe(texobj->DepthMode);
 
          st->state.num_samplers = su + 1;
 
-- 
1.6.6.1

