Merge branch 'mesa_7_6_branch'
[mesa.git] / src / mesa / state_tracker / st_atom_sampler.c
index 9abd0b3531d90d8b71b0fa263d3569e62d929121..50ce82811c567c31da8a53df1c34e57dd4b2225e 100644 (file)
@@ -35,6 +35,7 @@
 #include "main/macros.h"
 
 #include "st_context.h"
+#include "st_cb_texture.h"
 #include "st_atom.h"
 #include "st_program.h"
 #include "pipe/p_context.h"
@@ -47,7 +48,7 @@
  * Convert GLenum texcoord wrap tokens to pipe tokens.
  */
 static GLuint
-gl_wrap_to_sp(GLenum wrap)
+gl_wrap_xlate(GLenum wrap)
 {
    switch (wrap) {
    case GL_REPEAT:
@@ -67,7 +68,7 @@ gl_wrap_to_sp(GLenum wrap)
    case GL_MIRROR_CLAMP_TO_BORDER_EXT:
       return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER;
    default:
-      abort();
+      assert(0);
       return 0;
    }
 }
@@ -117,10 +118,44 @@ gl_filter_to_img_filter(GLenum filter)
 }
 
 
+static void
+xlate_border_color(const GLfloat *colorIn, GLenum baseFormat, GLfloat *colorOut)
+{
+   switch (baseFormat) {
+   case GL_RGB:
+      colorOut[0] = colorIn[0];
+      colorOut[1] = colorIn[1];
+      colorOut[2] = colorIn[2];
+      colorOut[3] = 1.0F;
+      break;
+   case GL_ALPHA:
+      colorOut[0] = colorOut[1] = colorOut[2] = 0.0;
+      colorOut[3] = colorIn[3];
+      break;
+   case GL_LUMINANCE:
+      colorOut[0] = colorOut[1] = colorOut[2] = colorIn[0];
+      colorOut[3] = 1.0;
+      break;
+   case GL_LUMINANCE_ALPHA:
+      colorOut[0] = colorOut[1] = colorOut[2] = colorIn[0];
+      colorOut[3] = colorIn[3];
+      break;
+   case GL_INTENSITY:
+      colorOut[0] = colorOut[1] = colorOut[2] = colorOut[3] = colorIn[0];
+      break;
+   default:
+      COPY_4V(colorOut, colorIn);
+   }
+}
+
+
 static void 
 update_samplers(struct st_context *st)
 {
-   const struct st_fragment_program *fs = st->fp;
+   struct gl_vertex_program *vprog = st->ctx->VertexProgram._Current;
+   struct gl_fragment_program *fprog = st->ctx->FragmentProgram._Current;
+   const GLbitfield samplersUsed = (vprog->Base.SamplersUsed |
+                                    fprog->Base.SamplersUsed);
    GLuint su;
 
    st->state.num_samplers = 0;
@@ -131,16 +166,26 @@ update_samplers(struct st_context *st)
 
       memset(sampler, 0, sizeof(*sampler));
 
-      if (fs->Base.Base.SamplersUsed & (1 << su)) {
-         GLuint texUnit = fs->Base.Base.SamplerUnits[su];
-         const struct gl_texture_object *texobj
-            = st->ctx->Texture.Unit[texUnit]._Current;
+      if (samplersUsed & (1 << su)) {
+         struct gl_texture_object *texobj;
+         struct gl_texture_image *teximg;
+         GLuint texUnit;
+
+         if (fprog->Base.SamplersUsed & (1 << su))
+            texUnit = fprog->Base.SamplerUnits[su];
+         else
+            texUnit = vprog->Base.SamplerUnits[su];
 
-         assert(texobj);
+         texobj = st->ctx->Texture.Unit[texUnit]._Current;
+         if (!texobj) {
+            texobj = st_get_default_texture(st);
+         }
 
-         sampler->wrap_s = gl_wrap_to_sp(texobj->WrapS);
-         sampler->wrap_t = gl_wrap_to_sp(texobj->WrapT);
-         sampler->wrap_r = gl_wrap_to_sp(texobj->WrapR);
+         teximg = texobj->Image[0][texobj->BaseLevel];
+
+         sampler->wrap_s = gl_wrap_xlate(texobj->WrapS);
+         sampler->wrap_t = gl_wrap_xlate(texobj->WrapT);
+         sampler->wrap_r = gl_wrap_xlate(texobj->WrapR);
 
          sampler->min_img_filter = gl_filter_to_img_filter(texobj->MinFilter);
          sampler->min_mip_filter = gl_filter_to_mip_filter(texobj->MinFilter);
@@ -163,10 +208,9 @@ update_samplers(struct st_context *st)
             assert(sampler->min_lod <= sampler->max_lod);
          }
 
-         sampler->border_color[0] = texobj->BorderColor[RCOMP];
-         sampler->border_color[1] = texobj->BorderColor[GCOMP];
-         sampler->border_color[2] = texobj->BorderColor[BCOMP];
-         sampler->border_color[3] = texobj->BorderColor[ACOMP];
+         xlate_border_color(texobj->BorderColor,
+                            teximg ? teximg->TexFormat->BaseFormat : GL_RGBA,
+                            sampler->border_color);
 
         sampler->max_anisotropy = texobj->MaxAnisotropy;
          if (sampler->max_anisotropy > 1.0) {
@@ -183,11 +227,11 @@ update_samplers(struct st_context *st)
 
          st->state.num_samplers = su + 1;
 
-         /* XXX more sampler state here */
-
+         /*printf("%s su=%u non-null\n", __FUNCTION__, su);*/
          cso_single_sampler(st->cso_context, su, sampler);
       }
       else {
+         /*printf("%s su=%u null\n", __FUNCTION__, su);*/
          cso_single_sampler(st->cso_context, su, NULL);
       }
    }