radeonsi: don't emit unnecessary NULL exports for unbound targets (v3)
[mesa.git] / src / gallium / drivers / radeonsi / si_shader.c
index 0a92a7b54e60ba24251caefd02b2a6b099379923..34b84eb81d9d02eef8ade73abc3ef871ce4d3561 100644 (file)
@@ -86,8 +86,9 @@ struct si_shader_context
        LLVMValueRef const_buffers[SI_NUM_CONST_BUFFERS];
        LLVMValueRef lds;
        LLVMValueRef *constants[SI_NUM_CONST_BUFFERS];
-       LLVMValueRef sampler_views[SI_NUM_SAMPLER_VIEWS];
-       LLVMValueRef sampler_states[SI_NUM_SAMPLER_STATES];
+       LLVMValueRef sampler_views[SI_NUM_SAMPLERS];
+       LLVMValueRef sampler_states[SI_NUM_SAMPLERS];
+       LLVMValueRef fmasks[SI_NUM_USER_SAMPLERS];
        LLVMValueRef so_buffers[4];
        LLVMValueRef esgs_ring;
        LLVMValueRef gsvs_ring[4];
@@ -858,48 +859,42 @@ static unsigned select_interp_param(struct si_shader_context *si_shader_ctx,
        }
 }
 
-static void declare_input_fs(
-       struct radeon_llvm_context *radeon_bld,
-       unsigned input_index,
-       const struct tgsi_full_declaration *decl)
+/**
+ * Interpolate a fragment shader input.
+ *
+ * @param si_shader_ctx                context
+ * @param input_index          index of the input in hardware
+ * @param semantic_name                TGSI_SEMANTIC_*
+ * @param semantic_index       semantic index
+ * @param num_interp_inputs    number of all interpolated inputs (= BCOLOR offset)
+ * @param colors_read_mask     color components read (4 bits for each color, 8 bits in total)
+ * @param interp_param         interpolation weights (i,j)
+ * @param prim_mask            SI_PARAM_PRIM_MASK
+ * @param face                 SI_PARAM_FRONT_FACE
+ * @param result               the return value (4 components)
+ */
+static void interp_fs_input(struct si_shader_context *si_shader_ctx,
+                           unsigned input_index,
+                           unsigned semantic_name,
+                           unsigned semantic_index,
+                           unsigned num_interp_inputs,
+                           unsigned colors_read_mask,
+                           LLVMValueRef interp_param,
+                           LLVMValueRef prim_mask,
+                           LLVMValueRef face,
+                           LLVMValueRef result[4])
 {
-       struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
-       struct si_shader_context *si_shader_ctx =
-               si_shader_context(&radeon_bld->soa.bld_base);
-       struct si_shader *shader = si_shader_ctx->shader;
-       struct lp_build_context *uint = &radeon_bld->soa.bld_base.uint_bld;
+       struct lp_build_context *base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
+       struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
        struct gallivm_state *gallivm = base->gallivm;
        LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
-       LLVMValueRef main_fn = radeon_bld->main_fn;
-
-       LLVMValueRef interp_param = NULL;
-       int interp_param_idx;
        const char * intr_name;
-
-       /* This value is:
-        * [15:0] NewPrimMask (Bit mask for each quad.  It is set it the
-        *                     quad begins a new primitive.  Bit 0 always needs
-        *                     to be unset)
-        * [32:16] ParamOffset
-        *
-        */
-       LLVMValueRef params = LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
        LLVMValueRef attr_number;
 
        unsigned chan;
 
        attr_number = lp_build_const_int32(gallivm, input_index);
 
-       interp_param_idx = lookup_interp_param_index(decl->Interp.Interpolate,
-                                                    decl->Interp.Location);
-       if (interp_param_idx == -1)
-               return;
-       else if (interp_param_idx) {
-               interp_param_idx = select_interp_param(si_shader_ctx,
-                                                      interp_param_idx);
-               interp_param = LLVMGetParam(main_fn, interp_param_idx);
-       }
-
        /* fs.constant returns the param from the middle vertex, so it's not
         * really useful for flat shading. It's meant to be used for custom
         * interpolation (but the intrinsic can't fetch from the other two
@@ -912,32 +907,28 @@ static void declare_input_fs(
         */
        intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
 
-       if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
+       if (semantic_name == TGSI_SEMANTIC_COLOR &&
            si_shader_ctx->shader->key.ps.color_two_side) {
-               struct tgsi_shader_info *info = &shader->selector->info;
                LLVMValueRef args[4];
-               LLVMValueRef face, is_face_positive;
+               LLVMValueRef is_face_positive;
                LLVMValueRef back_attr_number;
 
                /* If BCOLOR0 is used, BCOLOR1 is at offset "num_inputs + 1",
                 * otherwise it's at offset "num_inputs".
                 */
-               unsigned back_attr_offset = shader->selector->info.num_inputs;
-               if (decl->Semantic.Index == 1 && info->colors_read & 0xf)
+               unsigned back_attr_offset = num_interp_inputs;
+               if (semantic_index == 1 && colors_read_mask & 0xf)
                        back_attr_offset += 1;
 
                back_attr_number = lp_build_const_int32(gallivm, back_attr_offset);
 
-               face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
-
                is_face_positive = LLVMBuildICmp(gallivm->builder, LLVMIntNE,
                                                 face, uint->zero, "");
 
-               args[2] = params;
+               args[2] = prim_mask;
                args[3] = interp_param;
                for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
                        LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
-                       unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
                        LLVMValueRef front, back;
 
                        args[0] = llvm_chan;
@@ -951,46 +942,71 @@ static void declare_input_fs(
                                               input_type, args, args[3] ? 4 : 3,
                                               LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
 
-                       radeon_bld->inputs[soa_index] =
-                               LLVMBuildSelect(gallivm->builder,
+                       result[chan] = LLVMBuildSelect(gallivm->builder,
                                                is_face_positive,
                                                front,
                                                back,
                                                "");
                }
-       } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
+       } else if (semantic_name == TGSI_SEMANTIC_FOG) {
                LLVMValueRef args[4];
 
                args[0] = uint->zero;
                args[1] = attr_number;
-               args[2] = params;
+               args[2] = prim_mask;
                args[3] = interp_param;
-               radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
-                       lp_build_intrinsic(gallivm->builder, intr_name,
+               result[0] = lp_build_intrinsic(gallivm->builder, intr_name,
                                        input_type, args, args[3] ? 4 : 3,
                                        LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
-               radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
-               radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
-                       lp_build_const_float(gallivm, 0.0f);
-               radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
-                       lp_build_const_float(gallivm, 1.0f);
+               result[1] =
+               result[2] = lp_build_const_float(gallivm, 0.0f);
+               result[3] = lp_build_const_float(gallivm, 1.0f);
        } else {
                for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
                        LLVMValueRef args[4];
                        LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
-                       unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
+
                        args[0] = llvm_chan;
                        args[1] = attr_number;
-                       args[2] = params;
+                       args[2] = prim_mask;
                        args[3] = interp_param;
-                       radeon_bld->inputs[soa_index] =
-                               lp_build_intrinsic(gallivm->builder, intr_name,
+                       result[chan] = lp_build_intrinsic(gallivm->builder, intr_name,
                                                input_type, args, args[3] ? 4 : 3,
                                                LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
                }
        }
 }
 
+static void declare_input_fs(
+       struct radeon_llvm_context *radeon_bld,
+       unsigned input_index,
+       const struct tgsi_full_declaration *decl)
+{
+       struct si_shader_context *si_shader_ctx =
+               si_shader_context(&radeon_bld->soa.bld_base);
+       struct si_shader *shader = si_shader_ctx->shader;
+       LLVMValueRef main_fn = radeon_bld->main_fn;
+       LLVMValueRef interp_param = NULL;
+       int interp_param_idx;
+
+       interp_param_idx = lookup_interp_param_index(decl->Interp.Interpolate,
+                                                    decl->Interp.Location);
+       if (interp_param_idx == -1)
+               return;
+       else if (interp_param_idx) {
+               interp_param_idx = select_interp_param(si_shader_ctx,
+                                                      interp_param_idx);
+               interp_param = LLVMGetParam(main_fn, interp_param_idx);
+       }
+
+       interp_fs_input(si_shader_ctx, input_index, decl->Semantic.Name,
+                       decl->Semantic.Index, shader->selector->info.num_inputs,
+                       shader->selector->info.colors_read, interp_param,
+                       LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK),
+                       LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE),
+                       &radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)]);
+}
+
 static LLVMValueRef get_sample_id(struct radeon_llvm_context *radeon_bld)
 {
        return unpack_param(si_shader_context(&radeon_bld->soa.bld_base),
@@ -1042,7 +1058,6 @@ static void declare_system_value(
        struct si_shader_context *si_shader_ctx =
                si_shader_context(&radeon_bld->soa.bld_base);
        struct lp_build_context *bld = &radeon_bld->soa.bld_base.base;
-       struct lp_build_context *uint_bld = &radeon_bld->soa.bld_base.uint_bld;
        struct gallivm_state *gallivm = &radeon_bld->gallivm;
        LLVMValueRef value = 0;
 
@@ -1118,12 +1133,10 @@ static void declare_system_value(
        }
 
        case TGSI_SEMANTIC_SAMPLEMASK:
-               /* Smoothing isn't MSAA in GL, but it's MSAA in hardware.
-                * Therefore, force gl_SampleMaskIn to 1 for GL. */
-               if (si_shader_ctx->shader->key.ps.poly_line_smoothing)
-                       value = uint_bld->one;
-               else
-                       value = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_SAMPLE_COVERAGE);
+               /* This can only occur with the OpenGL Core profile, which
+                * doesn't support smoothing.
+                */
+               value = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_SAMPLE_COVERAGE);
                break;
 
        case TGSI_SEMANTIC_TESSCOORD:
@@ -1947,21 +1960,20 @@ handle_semantic:
        }
 }
 
-/* This only writes the tessellation factor levels. */
-static void si_llvm_emit_tcs_epilogue(struct lp_build_tgsi_context *bld_base)
+static void si_write_tess_factors(struct lp_build_tgsi_context *bld_base,
+                                 LLVMValueRef rel_patch_id,
+                                 LLVMValueRef invocation_id,
+                                 LLVMValueRef tcs_out_current_patch_data_offset)
 {
        struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
        struct gallivm_state *gallivm = bld_base->base.gallivm;
        struct si_shader *shader = si_shader_ctx->shader;
        unsigned tess_inner_index, tess_outer_index;
-       LLVMValueRef lds_base, lds_inner, lds_outer;
-       LLVMValueRef tf_base, rel_patch_id, byteoffset, buffer, rw_buffers;
-       LLVMValueRef out[6], vec0, vec1, invocation_id;
+       LLVMValueRef lds_base, lds_inner, lds_outer, byteoffset, buffer;
+       LLVMValueRef out[6], vec0, vec1, rw_buffers, tf_base;
        unsigned stride, outer_comps, inner_comps, i;
        struct lp_build_if_state if_ctx;
 
-       invocation_id = unpack_param(si_shader_ctx, SI_PARAM_REL_IDS, 8, 5);
-
        /* Do this only for invocation 0, because the tess levels are per-patch,
         * not per-vertex.
         *
@@ -2000,7 +2012,7 @@ static void si_llvm_emit_tcs_epilogue(struct lp_build_tgsi_context *bld_base)
        tess_inner_index = si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSINNER, 0);
        tess_outer_index = si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSOUTER, 0);
 
-       lds_base = get_tcs_out_current_patch_data_offset(si_shader_ctx);
+       lds_base = tcs_out_current_patch_data_offset;
        lds_inner = LLVMBuildAdd(gallivm->builder, lds_base,
                                 lp_build_const_int32(gallivm,
                                                      tess_inner_index * 4), "");
@@ -2029,7 +2041,6 @@ static void si_llvm_emit_tcs_epilogue(struct lp_build_tgsi_context *bld_base)
        /* Get the offset. */
        tf_base = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
                               SI_PARAM_TESS_FACTOR_OFFSET);
-       rel_patch_id = get_rel_patch_id(si_shader_ctx);
        byteoffset = LLVMBuildMul(gallivm->builder, rel_patch_id,
                                  lp_build_const_int32(gallivm, 4 * stride), "");
 
@@ -2042,6 +2053,20 @@ static void si_llvm_emit_tcs_epilogue(struct lp_build_tgsi_context *bld_base)
        lp_build_endif(&if_ctx);
 }
 
+/* This only writes the tessellation factor levels. */
+static void si_llvm_emit_tcs_epilogue(struct lp_build_tgsi_context *bld_base)
+{
+       struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
+       LLVMValueRef invocation_id;
+
+       invocation_id = unpack_param(si_shader_ctx, SI_PARAM_REL_IDS, 8, 5);
+
+       si_write_tess_factors(bld_base,
+                             get_rel_patch_id(si_shader_ctx),
+                             invocation_id,
+                             get_tcs_out_current_patch_data_offset(si_shader_ctx));
+}
+
 static void si_llvm_emit_ls_epilogue(struct lp_build_tgsi_context * bld_base)
 {
        struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
@@ -2262,7 +2287,6 @@ static void si_export_mrt_color(struct lp_build_tgsi_context *bld_base,
 {
        struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
        struct lp_build_context *base = &bld_base->base;
-       LLVMValueRef args[9];
        int i;
 
        /* Clamp color */
@@ -2284,27 +2308,46 @@ static void si_export_mrt_color(struct lp_build_tgsi_context *bld_base,
                color[3] = si_scale_alpha_by_sample_mask(bld_base, color[3]);
 
        /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
-       if (index == 0 &&
-           si_shader_ctx->shader->key.ps.last_cbuf > 0) {
-               for (int c = 1; c <= si_shader_ctx->shader->key.ps.last_cbuf; c++) {
+       if (si_shader_ctx->shader->key.ps.last_cbuf > 0) {
+               LLVMValueRef args[8][9];
+               int c, last = -1;
+
+               /* Get the export arguments, also find out what the last one is. */
+               for (c = 0; c <= si_shader_ctx->shader->key.ps.last_cbuf; c++) {
                        si_llvm_init_export_args(bld_base, color,
-                                                V_008DFC_SQ_EXP_MRT + c, args);
+                                                V_008DFC_SQ_EXP_MRT + c, args[c]);
+                       if (args[c][0] != bld_base->uint_bld.zero)
+                               last = c;
+               }
+
+               /* Emit all exports. */
+               for (c = 0; c <= si_shader_ctx->shader->key.ps.last_cbuf; c++) {
+                       if (is_last && last == c) {
+                               args[c][1] = bld_base->uint_bld.one; /* whether the EXEC mask is valid */
+                               args[c][2] = bld_base->uint_bld.one; /* DONE bit */
+                       } else if (args[c][0] == bld_base->uint_bld.zero)
+                               continue; /* unnecessary NULL export */
+
                        lp_build_intrinsic(base->gallivm->builder, "llvm.SI.export",
                                           LLVMVoidTypeInContext(base->gallivm->context),
-                                          args, 9, 0);
+                                          args[c], 9, 0);
                }
+       } else {
+               LLVMValueRef args[9];
+
+               /* Export */
+               si_llvm_init_export_args(bld_base, color, V_008DFC_SQ_EXP_MRT + index,
+                                        args);
+               if (is_last) {
+                       args[1] = bld_base->uint_bld.one; /* whether the EXEC mask is valid */
+                       args[2] = bld_base->uint_bld.one; /* DONE bit */
+               } else if (args[0] == bld_base->uint_bld.zero)
+                       return; /* unnecessary NULL export */
+
+               lp_build_intrinsic(base->gallivm->builder, "llvm.SI.export",
+                                  LLVMVoidTypeInContext(base->gallivm->context),
+                                  args, 9, 0);
        }
-
-       /* Export */
-       si_llvm_init_export_args(bld_base, color, V_008DFC_SQ_EXP_MRT + index,
-                                args);
-       if (is_last) {
-               args[1] = bld_base->uint_bld.one; /* whether the EXEC mask is valid */
-               args[2] = bld_base->uint_bld.one; /* DONE bit */
-       }
-       lp_build_intrinsic(base->gallivm->builder, "llvm.SI.export",
-                          LLVMVoidTypeInContext(base->gallivm->context),
-                          args, 9, 0);
 }
 
 static void si_export_null(struct lp_build_tgsi_context *bld_base)
@@ -2339,19 +2382,43 @@ static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
        int last_color_export = -1;
        int i;
 
-       /* If there are no outputs, add a dummy export. */
-       if (!info->num_outputs) {
-               si_export_null(bld_base);
-               return;
-       }
-
        /* Determine the last export. If MRTZ is present, it's always last.
         * Otherwise, find the last color export.
         */
-       if (!info->writes_z && !info->writes_stencil && !info->writes_samplemask)
-               for (i = 0; i < info->num_outputs; i++)
-                       if (info->output_semantic_name[i] == TGSI_SEMANTIC_COLOR)
+       if (!info->writes_z && !info->writes_stencil && !info->writes_samplemask) {
+               unsigned spi_format = shader->key.ps.spi_shader_col_format;
+
+               /* Don't export NULL and return if alpha-test is enabled. */
+               if (shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS &&
+                   shader->key.ps.alpha_func != PIPE_FUNC_NEVER &&
+                   (spi_format & 0xf) == 0)
+                       spi_format |= V_028714_SPI_SHADER_32_AR;
+
+               for (i = 0; i < info->num_outputs; i++) {
+                       unsigned index = info->output_semantic_index[i];
+
+                       if (info->output_semantic_name[i] != TGSI_SEMANTIC_COLOR)
+                               continue;
+
+                       /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
+                       if (shader->key.ps.last_cbuf > 0) {
+                               /* Just set this if any of the colorbuffers are enabled. */
+                               if (spi_format &
+                                   ((1llu << (4 * (shader->key.ps.last_cbuf + 1))) - 1))
+                                       last_color_export = i;
+                               continue;
+                       }
+
+                       if ((spi_format >> (index * 4)) & 0xf)
                                last_color_export = i;
+               }
+
+               /* If there are no outputs, export NULL. */
+               if (last_color_export == -1) {
+                       si_export_null(bld_base);
+                       return;
+               }
+       }
 
        for (i = 0; i < info->num_outputs; i++) {
                unsigned semantic_name = info->output_semantic_name[i];
@@ -2456,13 +2523,58 @@ static void set_tex_fetch_args(struct gallivm_state *gallivm,
 
 static const struct lp_build_tgsi_action tex_action;
 
+enum desc_type {
+       DESC_IMAGE,
+       DESC_FMASK,
+       DESC_SAMPLER
+};
+
+static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
+{
+       return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
+                              CONST_ADDR_SPACE);
+}
+
+/**
+ * Load an image view, fmask view. or sampler state descriptor.
+ */
+static LLVMValueRef get_sampler_desc(struct si_shader_context *si_shader_ctx,
+                                    LLVMValueRef index, enum desc_type type)
+{
+       struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
+       LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
+       LLVMBuilderRef builder = gallivm->builder;
+       LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
+                                       SI_PARAM_SAMPLERS);
+
+       switch (type) {
+       case DESC_IMAGE:
+               /* The image is at [0:7]. */
+               index = LLVMBuildMul(builder, index, LLVMConstInt(i32, 2, 0), "");
+               break;
+       case DESC_FMASK:
+               /* The FMASK is at [8:15]. */
+               index = LLVMBuildMul(builder, index, LLVMConstInt(i32, 2, 0), "");
+               index = LLVMBuildAdd(builder, index, LLVMConstInt(i32, 1, 0), "");
+               break;
+       case DESC_SAMPLER:
+               /* The sampler state is at [12:15]. */
+               index = LLVMBuildMul(builder, index, LLVMConstInt(i32, 4, 0), "");
+               index = LLVMBuildAdd(builder, index, LLVMConstInt(i32, 3, 0), "");
+               ptr = LLVMBuildPointerCast(builder, ptr,
+                                          const_array(LLVMVectorType(i32, 4), 0), "");
+               break;
+       }
+
+       return build_indexed_load_const(si_shader_ctx, ptr, index);
+}
+
 static void tex_fetch_ptrs(
        struct lp_build_tgsi_context * bld_base,
        struct lp_build_emit_data * emit_data,
        LLVMValueRef *res_ptr, LLVMValueRef *samp_ptr, LLVMValueRef *fmask_ptr)
 {
        struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
-       struct gallivm_state *gallivm = bld_base->base.gallivm;
        const struct tgsi_full_instruction * inst = emit_data->inst;
        unsigned target = inst->Texture.Texture;
        unsigned sampler_src;
@@ -2477,24 +2589,20 @@ static void tex_fetch_ptrs(
 
                ind_index = get_indirect_index(si_shader_ctx, &reg->Indirect, reg->Register.Index);
 
-               *res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER_VIEWS);
-               *res_ptr = build_indexed_load_const(si_shader_ctx, *res_ptr, ind_index);
-
-               *samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER_STATES);
-               *samp_ptr = build_indexed_load_const(si_shader_ctx, *samp_ptr, ind_index);
+               *res_ptr = get_sampler_desc(si_shader_ctx, ind_index, DESC_IMAGE);
 
                if (target == TGSI_TEXTURE_2D_MSAA ||
                    target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
-                       ind_index = LLVMBuildAdd(gallivm->builder, ind_index,
-                                                lp_build_const_int32(gallivm,
-                                                                     SI_FMASK_TEX_OFFSET), "");
-                       *fmask_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER_VIEWS);
-                       *fmask_ptr = build_indexed_load_const(si_shader_ctx, *fmask_ptr, ind_index);
+                       *samp_ptr = NULL;
+                       *fmask_ptr = get_sampler_desc(si_shader_ctx, ind_index, DESC_FMASK);
+               } else {
+                       *samp_ptr = get_sampler_desc(si_shader_ctx, ind_index, DESC_SAMPLER);
+                       *fmask_ptr = NULL;
                }
        } else {
                *res_ptr = si_shader_ctx->sampler_views[sampler_index];
                *samp_ptr = si_shader_ctx->sampler_states[sampler_index];
-               *fmask_ptr = si_shader_ctx->sampler_views[SI_FMASK_TEX_OFFSET + sampler_index];
+               *fmask_ptr = si_shader_ctx->fmasks[sampler_index];
        }
 }
 
@@ -3474,12 +3582,6 @@ static void create_meta_data(struct si_shader_context *si_shader_ctx)
        si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
 }
 
-static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
-{
-       return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
-                              CONST_ADDR_SPACE);
-}
-
 static void declare_streamout_params(struct si_shader_context *si_shader_ctx,
                                     struct pipe_stream_output_info *so,
                                     LLVMTypeRef *params, LLVMTypeRef i32,
@@ -3506,7 +3608,7 @@ static void create_function(struct si_shader_context *si_shader_ctx)
        struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
        struct gallivm_state *gallivm = bld_base->base.gallivm;
        struct si_shader *shader = si_shader_ctx->shader;
-       LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32, v16i8, v4i32, v8i32;
+       LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32, v16i8, v8i32;
        unsigned i, last_array_pointer, last_sgpr, num_params;
 
        i8 = LLVMInt8TypeInContext(gallivm->context);
@@ -3514,15 +3616,14 @@ static void create_function(struct si_shader_context *si_shader_ctx)
        f32 = LLVMFloatTypeInContext(gallivm->context);
        v2i32 = LLVMVectorType(i32, 2);
        v3i32 = LLVMVectorType(i32, 3);
-       v4i32 = LLVMVectorType(i32, 4);
        v8i32 = LLVMVectorType(i32, 8);
        v16i8 = LLVMVectorType(i8, 16);
 
        params[SI_PARAM_RW_BUFFERS] = const_array(v16i8, SI_NUM_RW_BUFFERS);
        params[SI_PARAM_CONST_BUFFERS] = const_array(v16i8, SI_NUM_CONST_BUFFERS);
-       params[SI_PARAM_SAMPLER_STATES] = const_array(v4i32, SI_NUM_SAMPLER_STATES);
-       params[SI_PARAM_SAMPLER_VIEWS] = const_array(v8i32, SI_NUM_SAMPLER_VIEWS);
-       last_array_pointer = SI_PARAM_SAMPLER_VIEWS;
+       params[SI_PARAM_SAMPLERS] = const_array(v8i32, SI_NUM_SAMPLERS);
+       params[SI_PARAM_UNUSED] = LLVMPointerType(i32, CONST_ADDR_SPACE);
+       last_array_pointer = SI_PARAM_UNUSED;
 
        switch (si_shader_ctx->type) {
        case TGSI_PROCESSOR_VERTEX:
@@ -3642,10 +3743,6 @@ static void create_function(struct si_shader_context *si_shader_ctx)
        radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
        radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
 
-       if (shader->dx10_clamp_mode)
-               LLVMAddTargetDependentFunctionAttr(si_shader_ctx->radeon_bld.main_fn,
-                                                  "enable-no-nans-fp-math", "true");
-
        for (i = 0; i <= last_sgpr; ++i) {
                LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
 
@@ -3727,34 +3824,26 @@ static void preload_samplers(struct si_shader_context *si_shader_ctx)
        struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
        struct gallivm_state * gallivm = bld_base->base.gallivm;
        const struct tgsi_shader_info * info = bld_base->info;
-
        unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
-
-       LLVMValueRef res_ptr, samp_ptr;
        LLVMValueRef offset;
 
        if (num_samplers == 0)
                return;
 
-       res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER_VIEWS);
-       samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER_STATES);
-
        /* Load the resources and samplers, we rely on the code sinking to do the rest */
        for (i = 0; i < num_samplers; ++i) {
                /* Resource */
                offset = lp_build_const_int32(gallivm, i);
-               si_shader_ctx->sampler_views[i] = build_indexed_load_const(si_shader_ctx, res_ptr, offset);
-
-               /* Sampler */
-               offset = lp_build_const_int32(gallivm, i);
-               si_shader_ctx->sampler_states[i] = build_indexed_load_const(si_shader_ctx, samp_ptr, offset);
+               si_shader_ctx->sampler_views[i] =
+                       get_sampler_desc(si_shader_ctx, offset, DESC_IMAGE);
 
                /* FMASK resource */
-               if (info->is_msaa_sampler[i]) {
-                       offset = lp_build_const_int32(gallivm, SI_FMASK_TEX_OFFSET + i);
-                       si_shader_ctx->sampler_views[SI_FMASK_TEX_OFFSET + i] =
-                               build_indexed_load_const(si_shader_ctx, res_ptr, offset);
-               }
+               if (info->is_msaa_sampler[i])
+                       si_shader_ctx->fmasks[i] =
+                               get_sampler_desc(si_shader_ctx, offset, DESC_FMASK);
+               else
+                       si_shader_ctx->sampler_states[i] =
+                               get_sampler_desc(si_shader_ctx, offset, DESC_SAMPLER);
        }
 }
 
@@ -3863,7 +3952,7 @@ void si_shader_binary_read_config(struct radeon_shader_binary *binary,
                        conf->spi_ps_input_ena = value;
                        break;
                case R_0286D0_SPI_PS_INPUT_ADDR:
-                       /* Not used yet, but will be in the future */
+                       conf->spi_ps_input_addr = value;
                        break;
                case R_0286E8_SPI_TMPRING_SIZE:
                case R_00B860_COMPUTE_TMPRING_SIZE:
@@ -3883,6 +3972,9 @@ void si_shader_binary_read_config(struct radeon_shader_binary *binary,
                        }
                        break;
                }
+
+               if (!conf->spi_ps_input_addr)
+                       conf->spi_ps_input_addr = conf->spi_ps_input_ena;
        }
 }
 
@@ -4024,6 +4116,13 @@ static void si_shader_dump_stats(struct si_screen *sscreen,
                max_simd_waves = MIN2(max_simd_waves, 16384 / lds_per_wave);
 
        if (r600_can_dump_shader(&sscreen->b, processor)) {
+               if (processor == TGSI_PROCESSOR_FRAGMENT) {
+                       fprintf(stderr, "*** SHADER CONFIG ***\n"
+                               "SPI_PS_INPUT_ADDR = 0x%04x\n"
+                               "SPI_PS_INPUT_ENA  = 0x%04x\n",
+                               conf->spi_ps_input_addr, conf->spi_ps_input_ena);
+               }
+
                fprintf(stderr, "*** SHADER STATS ***\n"
                        "SGPRS: %d\n"
                        "VGPRS: %d\n"
@@ -4063,7 +4162,8 @@ int si_compile_llvm(struct si_screen *sscreen,
                    LLVMTargetMachineRef tm,
                    LLVMModuleRef mod,
                    struct pipe_debug_callback *debug,
-                   unsigned processor)
+                   unsigned processor,
+                   const char *name)
 {
        int r = 0;
        unsigned count = p_atomic_inc_return(&sscreen->b.num_compilations);
@@ -4071,8 +4171,11 @@ int si_compile_llvm(struct si_screen *sscreen,
        if (r600_can_dump_shader(&sscreen->b, processor)) {
                fprintf(stderr, "radeonsi: Compiling shader %d\n", count);
 
-               if (!(sscreen->b.debug_flags & (DBG_NO_IR | DBG_PREOPT_IR)))
+               if (!(sscreen->b.debug_flags & (DBG_NO_IR | DBG_PREOPT_IR))) {
+                       fprintf(stderr, "%s LLVM IR:\n\n", name);
                        LLVMDumpModule(mod);
+                       fprintf(stderr, "\n");
+               }
        }
 
        if (!si_replace_shader(count, binary)) {
@@ -4085,6 +4188,20 @@ int si_compile_llvm(struct si_screen *sscreen,
 
        si_shader_binary_read_config(binary, conf, 0);
 
+       /* Enable 64-bit and 16-bit denormals, because there is no performance
+        * cost.
+        *
+        * If denormals are enabled, all floating-point output modifiers are
+        * ignored.
+        *
+        * Don't enable denormals for 32-bit floats, because:
+        * - Floating-point output modifiers would be ignored by the hw.
+        * - Some opcodes don't support denormals, such as v_mad_f32. We would
+        *   have to stop using those.
+        * - SI & CI would be very slow.
+        */
+       conf->float_mode |= V_00B028_FP_64_DENORMS;
+
        FREE(binary->config);
        FREE(binary->global_symbol_offsets);
        binary->config = NULL;
@@ -4095,7 +4212,7 @@ int si_compile_llvm(struct si_screen *sscreen,
 /* Generate code for the hardware VS shader stage to go with a geometry shader */
 static int si_generate_gs_copy_shader(struct si_screen *sscreen,
                                      struct si_shader_context *si_shader_ctx,
-                                     struct si_shader *gs, bool dump,
+                                     struct si_shader *gs,
                                      struct pipe_debug_callback *debug)
 {
        struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
@@ -4165,14 +4282,14 @@ static int si_generate_gs_copy_shader(struct si_screen *sscreen,
 
        radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
 
-       if (dump)
-               fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
-
        r = si_compile_llvm(sscreen, &si_shader_ctx->shader->binary,
                            &si_shader_ctx->shader->config, si_shader_ctx->tm,
                            bld_base->base.gallivm->module,
-                           debug, TGSI_PROCESSOR_GEOMETRY);
+                           debug, TGSI_PROCESSOR_GEOMETRY,
+                           "GS Copy Shader");
        if (!r) {
+               if (r600_can_dump_shader(&sscreen->b, TGSI_PROCESSOR_GEOMETRY))
+                       fprintf(stderr, "GS Copy Shader:\n");
                si_shader_dump(sscreen, si_shader_ctx->shader, debug,
                               TGSI_PROCESSOR_GEOMETRY);
                r = si_shader_binary_upload(sscreen, si_shader_ctx->shader);
@@ -4299,7 +4416,6 @@ int si_shader_create(struct si_screen *sscreen, LLVMTargetMachineRef tm,
        int r = 0;
        bool poly_stipple = sel->type == PIPE_SHADER_FRAGMENT &&
                            shader->key.ps.poly_stipple;
-       bool dump = r600_can_dump_shader(&sscreen->b, sel->info.processor);
 
        if (poly_stipple) {
                tokens = util_pstipple_create_fragment_shader(tokens, NULL,
@@ -4310,7 +4426,8 @@ int si_shader_create(struct si_screen *sscreen, LLVMTargetMachineRef tm,
 
        /* Dump TGSI code before doing TGSI->LLVM conversion in case the
         * conversion fails. */
-       if (dump && !(sscreen->b.debug_flags & DBG_NO_TGSI)) {
+       if (r600_can_dump_shader(&sscreen->b, sel->info.processor) &&
+           !(sscreen->b.debug_flags & DBG_NO_TGSI)) {
                si_dump_shader_key(sel->type, &shader->key, stderr);
                tgsi_dump(tokens, 0);
                si_dump_streamout(&sel->so);
@@ -4319,9 +4436,6 @@ int si_shader_create(struct si_screen *sscreen, LLVMTargetMachineRef tm,
        si_init_shader_ctx(&si_shader_ctx, sscreen, shader, tm,
                           poly_stipple ? &stipple_shader_info : &sel->info);
 
-       if (sel->type != PIPE_SHADER_COMPUTE)
-               shader->dx10_clamp_mode = true;
-
        shader->uses_instanceid = sel->info.uses_instanceid;
 
        bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
@@ -4395,7 +4509,7 @@ int si_shader_create(struct si_screen *sscreen, LLVMTargetMachineRef tm,
        radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
 
        r = si_compile_llvm(sscreen, &shader->binary, &shader->config, tm,
-                           mod, debug, si_shader_ctx.type);
+                           mod, debug, si_shader_ctx.type, "TGSI shader");
        if (r) {
                fprintf(stderr, "LLVM failed to compile shader\n");
                goto out;
@@ -4416,7 +4530,7 @@ int si_shader_create(struct si_screen *sscreen, LLVMTargetMachineRef tm,
                shader->gs_copy_shader->selector = shader->selector;
                si_shader_ctx.shader = shader->gs_copy_shader;
                if ((r = si_generate_gs_copy_shader(sscreen, &si_shader_ctx,
-                                                   shader, dump, debug))) {
+                                                   shader, debug))) {
                        free(shader->gs_copy_shader);
                        shader->gs_copy_shader = NULL;
                        goto out;