gallivm: use texture target from shader instead of static state for size query
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_sample_soa.c
index 53e3628f56e911b963807a9f58bf35c1ca636ea6..e403ac83c6a5c0cd186832f477da1a53a11c9cb6 100644 (file)
@@ -1055,22 +1055,36 @@ lp_build_sample_mipmap(struct lp_build_sample_context *bld,
 
 
 /**
- * Clamp layer coord to valid values.
+ * Build (per-coord) layer value.
+ * Either clamp layer to valid values or fill in optional out_of_bounds
+ * value and just return value unclamped.
  */
 static LLVMValueRef
 lp_build_layer_coord(struct lp_build_sample_context *bld,
                      unsigned texture_unit,
-                     LLVMValueRef layer)
+                     LLVMValueRef layer,
+                     LLVMValueRef *out_of_bounds)
 {
-   LLVMValueRef maxlayer;
+   LLVMValueRef num_layers;
+   struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
 
-   maxlayer = bld->dynamic_state->depth(bld->dynamic_state,
-                                        bld->gallivm, texture_unit);
-   maxlayer = lp_build_sub(&bld->int_bld, maxlayer, bld->int_bld.one);
-   maxlayer = lp_build_broadcast_scalar(&bld->int_coord_bld, maxlayer);
-   return lp_build_clamp(&bld->int_coord_bld, layer,
-                         bld->int_coord_bld.zero, maxlayer);
+   num_layers = bld->dynamic_state->depth(bld->dynamic_state,
+                                          bld->gallivm, texture_unit);
 
+   if (out_of_bounds) {
+      LLVMValueRef out1, out;
+      num_layers = lp_build_broadcast_scalar(int_coord_bld, num_layers);
+      out = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, layer, int_coord_bld->zero);
+      out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, layer, num_layers);
+      *out_of_bounds = lp_build_or(int_coord_bld, out, out1);
+      return layer;
+   }
+   else {
+      LLVMValueRef maxlayer;
+      maxlayer = lp_build_sub(&bld->int_bld, num_layers, bld->int_bld.one);
+      maxlayer = lp_build_broadcast_scalar(int_coord_bld, maxlayer);
+      return lp_build_clamp(int_coord_bld, layer, int_coord_bld->zero, maxlayer);
+   }
 }
 
 
@@ -1123,11 +1137,11 @@ lp_build_sample_common(struct lp_build_sample_context *bld,
    }
    else if (target == PIPE_TEXTURE_1D_ARRAY) {
       *r = lp_build_iround(&bld->coord_bld, *t);
-      *r = lp_build_layer_coord(bld, texture_index, *r);
+      *r = lp_build_layer_coord(bld, texture_index, *r, NULL);
    }
    else if (target == PIPE_TEXTURE_2D_ARRAY) {
       *r = lp_build_iround(&bld->coord_bld, *r);
-      *r = lp_build_layer_coord(bld, texture_index, *r);
+      *r = lp_build_layer_coord(bld, texture_index, *r, NULL);
    }
 
    /*
@@ -1162,7 +1176,7 @@ lp_build_sample_common(struct lp_build_sample_context *bld,
           * bad x86 code to be emitted.
           */
          assert(*lod_ipart);
-         lp_build_nearest_mip_level(bld, texture_index, *lod_ipart, ilevel0);
+         lp_build_nearest_mip_level(bld, texture_index, *lod_ipart, ilevel0, NULL);
       }
       else {
          first_level = bld->dynamic_state->first_level(bld->dynamic_state,
@@ -1173,7 +1187,7 @@ lp_build_sample_common(struct lp_build_sample_context *bld,
       break;
    case PIPE_TEX_MIPFILTER_NEAREST:
       assert(*lod_ipart);
-      lp_build_nearest_mip_level(bld, texture_index, *lod_ipart, ilevel0);
+      lp_build_nearest_mip_level(bld, texture_index, *lod_ipart, ilevel0, NULL);
       break;
    case PIPE_TEX_MIPFILTER_LINEAR:
       assert(*lod_ipart);
@@ -1300,13 +1314,15 @@ lp_build_fetch_texel(struct lp_build_sample_context *bld,
    struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
    unsigned dims = bld->dims, chan;
    unsigned target = bld->static_texture_state->target;
+   boolean out_of_bound_ret_zero = TRUE;
    LLVMValueRef size, ilevel;
    LLVMValueRef row_stride_vec = NULL, img_stride_vec = NULL;
    LLVMValueRef x = coords[0], y = coords[1], z = coords[2];
    LLVMValueRef width, height, depth, i, j;
    LLVMValueRef offset, out_of_bounds, out1;
 
-   /* XXX just like ordinary sampling, we don't handle per-pixel lod (yet). */
+   out_of_bounds = int_coord_bld->zero;
+
    if (explicit_lod && bld->static_texture_state->target != PIPE_BUFFER) {
       if (bld->num_lods != int_coord_bld->type.length) {
          ilevel = lp_build_pack_aos_scalars(bld->gallivm, int_coord_bld->type,
@@ -1315,11 +1331,18 @@ lp_build_fetch_texel(struct lp_build_sample_context *bld,
       else {
          ilevel = explicit_lod;
       }
-      lp_build_nearest_mip_level(bld, texture_unit, ilevel, &ilevel);
+      lp_build_nearest_mip_level(bld, texture_unit, ilevel, &ilevel,
+                                 out_of_bound_ret_zero ? &out_of_bounds : NULL);
    }
    else {
-      bld->num_lods = 1;
-      ilevel = lp_build_const_int32(bld->gallivm, 0);
+      assert(bld->num_lods == 1);
+      if (bld->static_texture_state->target != PIPE_BUFFER) {
+         ilevel = bld->dynamic_state->first_level(bld->dynamic_state,
+                                                  bld->gallivm, texture_unit);
+      }
+      else {
+         ilevel = lp_build_const_int32(bld->gallivm, 0);
+      }
    }
    lp_build_mipmap_level_sizes(bld, ilevel,
                                &size,
@@ -1330,19 +1353,27 @@ lp_build_fetch_texel(struct lp_build_sample_context *bld,
    if (target == PIPE_TEXTURE_1D_ARRAY ||
        target == PIPE_TEXTURE_2D_ARRAY) {
       if (target == PIPE_TEXTURE_1D_ARRAY) {
-         z = lp_build_layer_coord(bld, texture_unit, y);
+         z = y;
+      }
+      if (out_of_bound_ret_zero) {
+         z = lp_build_layer_coord(bld, texture_unit, z, &out1);
+         out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
       }
       else {
-         z = lp_build_layer_coord(bld, texture_unit, z);
+         z = lp_build_layer_coord(bld, texture_unit, z, NULL);
       }
    }
 
    /* This is a lot like border sampling */
    if (offsets[0]) {
-      /* XXX coords are really unsigned, offsets are signed */
+      /*
+       * coords are really unsigned, offsets are signed, but I don't think
+       * exceeding 31 bits is possible
+       */
       x = lp_build_add(int_coord_bld, x, offsets[0]);
    }
-   out_of_bounds = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, x, int_coord_bld->zero);
+   out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, x, int_coord_bld->zero);
+   out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
    out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, x, width);
    out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
 
@@ -1385,11 +1416,10 @@ lp_build_fetch_texel(struct lp_build_sample_context *bld,
                            i, j,
                            colors_out);
 
-   if (0) {
+   if (out_of_bound_ret_zero) {
       /*
-       * Not needed except for ARB_robust_buffer_access_behavior.
+       * Only needed for ARB_robust_buffer_access_behavior and d3d10.
        * Could use min/max above instead of out-of-bounds comparisons
-       * (in fact cast to unsigned and min only is sufficient)
        * if we don't care about the result returned for out-of-bounds.
        */
       for (chan = 0; chan < 4; chan++) {
@@ -1415,6 +1445,8 @@ lp_build_sample_compare(struct lp_build_sample_context *bld,
    LLVMBuilderRef builder = bld->gallivm->builder;
    LLVMValueRef res, p;
    const unsigned chan = 0;
+   unsigned chan_type;
+   const struct util_format_description *format_desc;
 
    if (bld->static_sampler_state->compare_mode == PIPE_TEX_COMPARE_NONE)
       return;
@@ -1436,17 +1468,42 @@ lp_build_sample_compare(struct lp_build_sample_context *bld,
                       coord, tex);
    }
 
-   /* Clamp p coords to [0,1] */
-   p = lp_build_clamp(&bld->coord_bld, p,
-                      bld->coord_bld.zero,
-                      bld->coord_bld.one);
+   /* Clamp p coords to [0,1] for fixed function depth texture format */
+   format_desc = util_format_description(bld->static_texture_state->format);
+   /* not entirely sure we couldn't end up with non-valid swizzle here */
+   chan_type = format_desc->swizzle[0] <= UTIL_FORMAT_SWIZZLE_W ?
+                  format_desc->channel[format_desc->swizzle[0]].type :
+                  UTIL_FORMAT_TYPE_FLOAT;
+   if (chan_type != UTIL_FORMAT_TYPE_FLOAT) {
+      p = lp_build_clamp(&bld->coord_bld, p,
+                         bld->coord_bld.zero, bld->coord_bld.one);
+   }
+
+   /*
+    * technically this is not entirely correct for unorm depth as the ref value
+    * should be converted to the depth format (quantization!) and comparison
+    * then done in texture format.
+    */
 
    /* result = (p FUNC texel) ? 1 : 0 */
-   res = lp_build_cmp(texel_bld, bld->static_sampler_state->compare_func,
-                      p, texel[chan]);
+   /*
+    * honor d3d10 floating point rules here, which state that comparisons
+    * are ordered except NOT_EQUAL which is unordered.
+    */
+   if (bld->static_sampler_state->compare_func != PIPE_FUNC_NOTEQUAL) {
+      res = lp_build_cmp_ordered(texel_bld, bld->static_sampler_state->compare_func,
+                                 p, texel[chan]);
+   }
+   else {
+      res = lp_build_cmp(texel_bld, bld->static_sampler_state->compare_func,
+                         p, texel[chan]);
+   }
    res = lp_build_select(texel_bld, res, texel_bld->one, texel_bld->zero);
 
-   /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
+   /*
+    * returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE.
+    * This should be ok because sampler swizzle is applied on top of it.
+    */
    texel[0] =
    texel[1] =
    texel[2] = res;
@@ -1589,7 +1646,8 @@ lp_build_sample_soa(struct gallivm_state *gallivm,
         (!is_fetch && mip_filter != PIPE_TEX_MIPFILTER_NONE)))
       bld.num_lods = type.length;
    /* TODO: for true scalar_lod should only use 1 lod value */
-   else if (!is_fetch && mip_filter != PIPE_TEX_MIPFILTER_NONE) {
+   else if ((is_fetch && explicit_lod && bld.static_texture_state->target != PIPE_BUFFER ) ||
+            (!is_fetch && mip_filter != PIPE_TEX_MIPFILTER_NONE)) {
       bld.num_lods = num_quads;
    }
    else {
@@ -1885,20 +1943,49 @@ lp_build_size_query_soa(struct gallivm_state *gallivm,
                         struct lp_sampler_dynamic_state *dynamic_state,
                         struct lp_type int_type,
                         unsigned texture_unit,
+                        unsigned target,
                         boolean need_nr_mips,
+                        boolean scalar_lod,
                         LLVMValueRef explicit_lod,
                         LLVMValueRef *sizes_out)
 {
-   LLVMValueRef lod;
-   LLVMValueRef size;
+   LLVMValueRef lod, level, size;
    LLVMValueRef first_level = NULL;
    int dims, i;
    boolean has_array;
+   unsigned num_lods = 1;
    struct lp_build_context bld_int_vec;
 
-   dims = texture_dims(static_state->target);
+   /*
+    * Do some sanity verification about bound texture and shader dcl target.
+    * Not entirely sure what's possible but assume array/non-array
+    * always compatible (probably not ok for OpenGL but d3d10 has no
+    * distinction of arrays at the resource level).
+    * Everything else looks bogus (though not entirely sure about rect/2d).
+    * Currently disabled because it causes assertion failures if there's
+    * nothing bound (or rather a dummy texture, not that this case would
+    * return the right values).
+    */
+   if (0 && static_state->target != target) {
+      if (static_state->target == PIPE_TEXTURE_1D)
+         assert(target == PIPE_TEXTURE_1D_ARRAY);
+      else if (static_state->target == PIPE_TEXTURE_1D_ARRAY)
+         assert(target == PIPE_TEXTURE_1D);
+      else if (static_state->target == PIPE_TEXTURE_2D)
+         assert(target == PIPE_TEXTURE_2D_ARRAY);
+      else if (static_state->target == PIPE_TEXTURE_2D_ARRAY)
+         assert(target == PIPE_TEXTURE_2D);
+      else if (static_state->target == PIPE_TEXTURE_CUBE)
+         assert(target == PIPE_TEXTURE_CUBE_ARRAY);
+      else if (static_state->target == PIPE_TEXTURE_CUBE_ARRAY)
+         assert(target == PIPE_TEXTURE_CUBE);
+      else
+         assert(0);
+   }
+
+   dims = texture_dims(target);
 
-   switch (static_state->target) {
+   switch (target) {
    case PIPE_TEXTURE_1D_ARRAY:
    case PIPE_TEXTURE_2D_ARRAY:
       has_array = TRUE;
@@ -1916,9 +2003,8 @@ lp_build_size_query_soa(struct gallivm_state *gallivm,
       /* FIXME: this needs to honor per-element lod */
       lod = LLVMBuildExtractElement(gallivm->builder, explicit_lod, lp_build_const_int32(gallivm, 0), "");
       first_level = dynamic_state->first_level(dynamic_state, gallivm, texture_unit);
-      lod = lp_build_broadcast_scalar(&bld_int_vec,
-                                      LLVMBuildAdd(gallivm->builder, lod, first_level, "lod"));
-
+      level = LLVMBuildAdd(gallivm->builder, lod, first_level, "level");
+      lod = lp_build_broadcast_scalar(&bld_int_vec, level);
    } else {
       lod = bld_int_vec.zero;
    }
@@ -1954,10 +2040,30 @@ lp_build_size_query_soa(struct gallivm_state *gallivm,
                                     lp_build_const_int32(gallivm, dims), "");
 
    /*
-    * XXX for out-of-bounds lod, should set size to zero vector here
-    * (for dx10-style only, i.e. need_nr_mips)
+    * d3d10 requires zero for x/y/z values (but not w, i.e. mip levels)
+    * if level is out of bounds (note this can't cover unbound texture
+    * here, which also requires returning zero).
     */
-
+   if (explicit_lod && need_nr_mips) {
+      LLVMValueRef last_level, out, out1;
+      struct lp_build_context leveli_bld;
+
+      /* everything is scalar for now */
+      lp_build_context_init(&leveli_bld, gallivm, lp_type_int_vec(32, 32));
+      last_level = dynamic_state->last_level(dynamic_state, gallivm, texture_unit);
+
+      out = lp_build_cmp(&leveli_bld, PIPE_FUNC_LESS, level, first_level);
+      out1 = lp_build_cmp(&leveli_bld, PIPE_FUNC_GREATER, level, last_level);
+      out = lp_build_or(&leveli_bld, out, out1);
+      if (num_lods == 1) {
+         out = lp_build_broadcast_scalar(&bld_int_vec, out);
+      }
+      else {
+         /* TODO */
+         assert(0);
+      }
+      size = lp_build_andnot(&bld_int_vec, size, out);
+   }
    for (i = 0; i < dims + (has_array ? 1 : 0); i++) {
       sizes_out[i] = lp_build_extract_broadcast(gallivm, bld_int_vec.type, int_type,
                                                 size,