radeonsi: force NaNs to 0
[mesa.git] / src / gallium / drivers / llvmpipe / lp_state_setup.c
index 97bfede19bfed3919ae45c13f869f517b4f56f0f..49741db66f03993340c2382bba714a3a851dfce5 100644 (file)
@@ -50,6 +50,9 @@
 #include "lp_state_setup.h"
 
 
+/** Setup shader number (for debugging) */
+static unsigned setup_no = 0;
+
 
 /* currently organized to interpolate full float[4] attributes even
  * when some elements are unused.  Later, can pack vertex data more
@@ -83,26 +86,26 @@ struct lp_setup_args
 
 static void
 store_coef(struct gallivm_state *gallivm,
-          struct lp_setup_args *args,
-          unsigned slot,
-          LLVMValueRef a0,
-          LLVMValueRef dadx,
-          LLVMValueRef dady)
+           struct lp_setup_args *args,
+           unsigned slot,
+           LLVMValueRef a0,
+           LLVMValueRef dadx,
+           LLVMValueRef dady)
 {
    LLVMBuilderRef builder = gallivm->builder;
    LLVMValueRef idx = lp_build_const_int32(gallivm, slot);
 
    LLVMBuildStore(builder,
-                 a0, 
-                 LLVMBuildGEP(builder, args->a0, &idx, 1, ""));
+                  a0,
+                  LLVMBuildGEP(builder, args->a0, &idx, 1, ""));
 
    LLVMBuildStore(builder,
-                 dadx, 
-                 LLVMBuildGEP(builder, args->dadx, &idx, 1, ""));
+                  dadx,
+                  LLVMBuildGEP(builder, args->dadx, &idx, 1, ""));
 
    LLVMBuildStore(builder,
-                 dady, 
-                 LLVMBuildGEP(builder, args->dady, &idx, 1, ""));
+                  dady,
+                  LLVMBuildGEP(builder, args->dady, &idx, 1, ""));
 }
 
 
@@ -131,20 +134,22 @@ emit_facing_coef(struct gallivm_state *gallivm,
    LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context);
    LLVMValueRef a0_0 = args->facing;
    LLVMValueRef a0_0f = LLVMBuildSIToFP(builder, a0_0, float_type, "");
+   LLVMValueRef a0, face_val;
    const unsigned char swizzles[4] = { PIPE_SWIZZLE_RED, PIPE_SWIZZLE_ZERO,
                                        PIPE_SWIZZLE_ZERO, PIPE_SWIZZLE_ZERO };
    /* Our face val is either 1 or 0 so we do
     * face = (val * 2) - 1
     * to make it 1 or -1
     */
-   LLVMValueRef face_val =
+   face_val =
       LLVMBuildFAdd(builder,
                     LLVMBuildFMul(builder, a0_0f,
                                   lp_build_const_float(gallivm, 2.0),
                                   ""),
                     lp_build_const_float(gallivm, -1.0),
                     "facing");
-   LLVMValueRef a0 = lp_build_swizzle_aos(&args->bld, face_val, swizzles);
+   face_val = lp_build_broadcast_scalar(&args->bld, face_val);
+   a0 = lp_build_swizzle_aos(&args->bld, face_val, swizzles);
 
    store_coef(gallivm, args, slot, a0, args->bld.zero, args->bld.zero);
 }
@@ -374,6 +379,19 @@ load_attribute(struct gallivm_state *gallivm,
    }
 }
 
+/*
+ * FIXME: interpolation is always done wrt fb origin (0/0).
+ * However, if some (small) tri is far away from the origin and gradients
+ * are large, this can lead to HUGE errors, since the a0 value calculated
+ * here can get very large (with the actual values inside the triangle way
+ * smaller), leading to complete loss of accuracy. This could be prevented
+ * by using some point inside (or at corner) of the tri as interpolation
+ * origin, or just use barycentric interpolation (which GL suggests and is
+ * what real hw does - you can get the barycentric coordinates from the
+ * edge functions in rasterization in principle (though we skip these
+ * sometimes completely in case of tris covering a block fully,
+ * which obviously wouldn't work)).
+ */
 static void 
 emit_coef4( struct gallivm_state *gallivm,
             struct lp_setup_args *args,
@@ -383,6 +401,7 @@ emit_coef4( struct gallivm_state *gallivm,
             LLVMValueRef a2)
 {
    LLVMBuilderRef b = gallivm->builder;
+   LLVMValueRef attr_0;
    LLVMValueRef dy20_ooa = args->dy20_ooa;
    LLVMValueRef dy01_ooa = args->dy01_ooa;
    LLVMValueRef dx20_ooa = args->dx20_ooa;
@@ -406,10 +425,10 @@ emit_coef4( struct gallivm_state *gallivm,
 
    /* Calculate a0 - the attribute value at the origin
     */
-   LLVMValueRef dadx_x0       = LLVMBuildFMul(b, dadx, x0_center, "dadx_x0");
-   LLVMValueRef dady_y0       = LLVMBuildFMul(b, dady, y0_center, "dady_y0"); 
-   LLVMValueRef attr_v0       = LLVMBuildFAdd(b, dadx_x0, dady_y0, "attr_v0");
-   LLVMValueRef attr_0        = LLVMBuildFSub(b, a0, attr_v0, "attr_0");
+   LLVMValueRef dadx_x0    = LLVMBuildFMul(b, dadx, x0_center, "dadx_x0");
+   LLVMValueRef dady_y0    = LLVMBuildFMul(b, dady, y0_center, "dady_y0");
+   LLVMValueRef attr_v0    = LLVMBuildFAdd(b, dadx_x0, dady_y0, "attr_v0");
+   attr_0                  = LLVMBuildFSub(b, a0, attr_v0, "attr_0");
 
    store_coef(gallivm, args, slot, attr_0, dadx, dady);
 }
@@ -621,12 +640,13 @@ init_args(struct gallivm_state *gallivm,
    LLVMValueRef zeroi = lp_build_const_int32(gallivm, 0);
    LLVMValueRef pixel_center, xy0_center, dxy01, dxy20, dyx20;
    LLVMValueRef e, f, ef, ooa;
-   LLVMValueRef shuffles[4];
+   LLVMValueRef shuffles[4], shuf10;
    LLVMValueRef attr_pos[3];
    struct lp_type typef4 = lp_type_float_vec(32, 128);
    struct lp_build_context bld;
 
    lp_build_context_init(&bld, gallivm, typef4);
+   args->bld = bld;
 
    /* The internal position input is in slot zero:
     */
@@ -648,8 +668,9 @@ init_args(struct gallivm_state *gallivm,
    shuffles[1] = zeroi;
    shuffles[2] = LLVMGetUndef(shuf_type);
    shuffles[3] = LLVMGetUndef(shuf_type);
+   shuf10 = LLVMConstVector(shuffles, 4);
 
-   dyx20 = LLVMBuildShuffleVector(b, dxy20, dxy20, LLVMConstVector(shuffles, 4), "");
+   dyx20 = LLVMBuildShuffleVector(b, dxy20, dxy20, shuf10, "");
 
    ef = LLVMBuildFMul(b, dxy01, dyx20, "ef");
    e = LLVMBuildExtractElement(b, ef, zeroi, "");
@@ -676,8 +697,6 @@ init_args(struct gallivm_state *gallivm,
    args->x0_center = lp_build_extract_broadcast(gallivm, typef4, typef4, xy0_center, zeroi);
    args->y0_center = lp_build_extract_broadcast(gallivm, typef4, typef4, xy0_center, onei);
 
-   args->bld = bld;
-
    emit_linear_coef(gallivm, args, 0, attr_pos);
 }
 
@@ -692,7 +711,7 @@ generate_setup_variant(struct lp_setup_variant_key *key,
    struct lp_setup_variant *variant = NULL;
    struct gallivm_state *gallivm;
    struct lp_setup_args args;
-   char func_name[256];
+   char func_name[64];
    LLVMTypeRef vec4f_type;
    LLVMTypeRef func_type;
    LLVMTypeRef arg_types[7];
@@ -707,7 +726,12 @@ generate_setup_variant(struct lp_setup_variant_key *key,
    if (variant == NULL)
       goto fail;
 
-   variant->gallivm = gallivm = gallivm_create();
+   variant->no = setup_no++;
+
+   util_snprintf(func_name, sizeof(func_name), "setup_variant_%u",
+                 variant->no);
+
+   variant->gallivm = gallivm = gallivm_create(func_name, lp->context);
    if (!variant->gallivm) {
       goto fail;
    }
@@ -721,9 +745,6 @@ generate_setup_variant(struct lp_setup_variant_key *key,
    memcpy(&variant->key, key, key->size);
    variant->list_item_global.base = variant;
 
-   util_snprintf(func_name, sizeof(func_name), "fs%u_setup%u",
-                 0, variant->no);
-
    /* Currently always deal with full 4-wide vertex attributes from
     * the vertices.
     */
@@ -785,6 +806,8 @@ generate_setup_variant(struct lp_setup_variant_key *key,
    if (!variant->jit_function)
       goto fail;
 
+   gallivm_free_ir(variant->gallivm);
+
    /*
     * Update timing information:
     */
@@ -793,22 +816,17 @@ generate_setup_variant(struct lp_setup_variant_key *key,
       LP_COUNT_ADD(llvm_compile_time, t1 - t0);
       LP_COUNT_ADD(nr_llvm_compiles, 1);
    }
-   
+
    return variant;
 
 fail:
    if (variant) {
-      if (variant->function) {
-         gallivm_free_function(gallivm,
-                               variant->function,
-                               variant->jit_function);
-      }
       if (variant->gallivm) {
          gallivm_destroy(variant->gallivm);
       }
       FREE(variant);
    }
-   
+
    return NULL;
 }
 
@@ -816,13 +834,13 @@ fail:
 
 static void
 lp_make_setup_variant_key(struct llvmpipe_context *lp,
-                         struct lp_setup_variant_key *key)
+                          struct lp_setup_variant_key *key)
 {
    struct lp_fragment_shader *fs = lp->fs;
    unsigned i;
 
    assert(sizeof key->inputs[0] == sizeof(uint));
-   
+
    key->num_inputs = fs->info.base.num_inputs;
    key->flatshade_first = lp->rasterizer->flatshade_first;
    key->pixel_center_half = lp->rasterizer->half_pixel_center;
@@ -860,9 +878,9 @@ lp_make_setup_variant_key(struct llvmpipe_context *lp,
    for (i = 0; i < key->num_inputs; i++) {
       if (key->inputs[i].interp == LP_INTERP_COLOR) {
          if (lp->rasterizer->flatshade)
-           key->inputs[i].interp = LP_INTERP_CONSTANT;
-        else
-           key->inputs[i].interp = LP_INTERP_PERSPECTIVE;
+            key->inputs[i].interp = LP_INTERP_CONSTANT;
+         else
+            key->inputs[i].interp = LP_INTERP_PERSPECTIVE;
       }
    }
 
@@ -871,17 +889,11 @@ lp_make_setup_variant_key(struct llvmpipe_context *lp,
 
 static void
 remove_setup_variant(struct llvmpipe_context *lp,
-                    struct lp_setup_variant *variant)
+                     struct lp_setup_variant *variant)
 {
    if (gallivm_debug & GALLIVM_DEBUG_IR) {
       debug_printf("llvmpipe: del setup_variant #%u total %u\n",
-                  variant->no, lp->nr_setup_variants);
-   }
-
-   if (variant->function) {
-      gallivm_free_function(variant->gallivm,
-                            variant->function,
-                            variant->jit_function);
+                   variant->no, lp->nr_setup_variants);
    }
 
    if (variant->gallivm) {
@@ -940,7 +952,7 @@ llvmpipe_update_setup(struct llvmpipe_context *lp)
 
    foreach(li, &lp->setup_variants_list) {
       if(li->base->key.size == key->size &&
-        memcmp(&li->base->key, key, key->size) == 0) {
+         memcmp(&li->base->key, key, key->size) == 0) {
          variant = li->base;
          break;
       }
@@ -951,19 +963,17 @@ llvmpipe_update_setup(struct llvmpipe_context *lp)
    }
    else {
       if (lp->nr_setup_variants >= LP_MAX_SETUP_VARIANTS) {
-        cull_setup_variants(lp);
+         cull_setup_variants(lp);
       }
 
       variant = generate_setup_variant(key, lp);
       if (variant) {
          insert_at_head(&lp->setup_variants_list, &variant->list_item_global);
          lp->nr_setup_variants++;
-         llvmpipe_variant_count++;
       }
    }
 
-   lp_setup_set_setup_variant(lp->setup,
-                             variant);
+   lp_setup_set_setup_variant(lp->setup, variant);
 }
 
 void
@@ -979,10 +989,10 @@ lp_delete_setup_variants(struct llvmpipe_context *lp)
 }
 
 void
-lp_dump_setup_coef( const struct lp_setup_variant_key *key,
-                   const float (*sa0)[4],
-                   const float (*sdadx)[4],
-                   const float (*sdady)[4])
+lp_dump_setup_coef(const struct lp_setup_variant_key *key,
+                   const float (*sa0)[4],
+                   const float (*sdadx)[4],
+                   const float (*sdady)[4])
 {
    int i, slot;
 
@@ -992,23 +1002,20 @@ lp_dump_setup_coef( const struct lp_setup_variant_key *key,
       float dady = sdady[0][i];
 
       debug_printf("POS.%c: a0 = %f, dadx = %f, dady = %f\n",
-                  "xyzw"[i],
-                  a0, dadx, dady);
+                   "xyzw"[i], a0, dadx, dady);
    }
 
    for (slot = 0; slot < key->num_inputs; slot++) {
       unsigned usage_mask = key->inputs[slot].usage_mask;
       for (i = 0; i < TGSI_NUM_CHANNELS; i++) {
-        if (usage_mask & (1 << i)) {
-           float a0   = sa0  [1 + slot][i];
-           float dadx = sdadx[1 + slot][i];
-           float dady = sdady[1 + slot][i];
-
-           debug_printf("IN[%u].%c: a0 = %f, dadx = %f, dady = %f\n",
-                        slot,
-                        "xyzw"[i],
-                        a0, dadx, dady);
-        }
+         if (usage_mask & (1 << i)) {
+            float a0   = sa0  [1 + slot][i];
+            float dadx = sdadx[1 + slot][i];
+            float dady = sdady[1 + slot][i];
+
+            debug_printf("IN[%u].%c: a0 = %f, dadx = %f, dady = %f\n",
+                         slot, "xyzw"[i], a0, dadx, dady);
+         }
       }
    }
 }