Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1.  
  2. /*
  3.  * Copyright 2012 Advanced Micro Devices, Inc.
  4.  *
  5.  * Permission is hereby granted, free of charge, to any person obtaining a
  6.  * copy of this software and associated documentation files (the "Software"),
  7.  * to deal in the Software without restriction, including without limitation
  8.  * on the rights to use, copy, modify, merge, publish, distribute, sub
  9.  * license, and/or sell copies of the Software, and to permit persons to whom
  10.  * the Software is furnished to do so, subject to the following conditions:
  11.  *
  12.  * The above copyright notice and this permission notice (including the next
  13.  * paragraph) shall be included in all copies or substantial portions of the
  14.  * Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  19.  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
  20.  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  21.  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  22.  * USE OR OTHER DEALINGS IN THE SOFTWARE.
  23.  *
  24.  * Authors:
  25.  *      Tom Stellard <thomas.stellard@amd.com>
  26.  *      Michel Dänzer <michel.daenzer@amd.com>
  27.  *      Christian König <christian.koenig@amd.com>
  28.  */
  29.  
  30. #include "gallivm/lp_bld_tgsi_action.h"
  31. #include "gallivm/lp_bld_const.h"
  32. #include "gallivm/lp_bld_gather.h"
  33. #include "gallivm/lp_bld_intr.h"
  34. #include "gallivm/lp_bld_logic.h"
  35. #include "gallivm/lp_bld_tgsi.h"
  36. #include "gallivm/lp_bld_arit.h"
  37. #include "radeon_llvm.h"
  38. #include "radeon_llvm_emit.h"
  39. #include "util/u_memory.h"
  40. #include "tgsi/tgsi_info.h"
  41. #include "tgsi/tgsi_parse.h"
  42. #include "tgsi/tgsi_scan.h"
  43. #include "tgsi/tgsi_util.h"
  44. #include "tgsi/tgsi_dump.h"
  45.  
  46. #include "radeonsi_pipe.h"
  47. #include "radeonsi_shader.h"
  48. #include "si_state.h"
  49. #include "sid.h"
  50.  
  51. #include <assert.h>
  52. #include <errno.h>
  53. #include <stdio.h>
  54.  
  55. struct si_shader_context
  56. {
  57.         struct radeon_llvm_context radeon_bld;
  58.         struct tgsi_parse_context parse;
  59.         struct tgsi_token * tokens;
  60.         struct si_pipe_shader *shader;
  61.         unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
  62.         LLVMValueRef const_md;
  63.         LLVMValueRef const_resource;
  64. #if HAVE_LLVM >= 0x0304
  65.         LLVMValueRef ddxy_lds;
  66. #endif
  67.         LLVMValueRef *constants;
  68.         LLVMValueRef *resources;
  69.         LLVMValueRef *samplers;
  70. };
  71.  
  72. static struct si_shader_context * si_shader_context(
  73.         struct lp_build_tgsi_context * bld_base)
  74. {
  75.         return (struct si_shader_context *)bld_base;
  76. }
  77.  
  78.  
  79. #define PERSPECTIVE_BASE 0
  80. #define LINEAR_BASE 9
  81.  
  82. #define SAMPLE_OFFSET 0
  83. #define CENTER_OFFSET 2
  84. #define CENTROID_OFSET 4
  85.  
  86. #define USE_SGPR_MAX_SUFFIX_LEN 5
  87. #define CONST_ADDR_SPACE 2
  88. #define LOCAL_ADDR_SPACE 3
  89. #define USER_SGPR_ADDR_SPACE 8
  90.  
  91. /**
  92.  * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
  93.  *
  94.  * @param offset The offset parameter specifies the number of
  95.  * elements to offset, not the number of bytes or dwords.  An element is the
  96.  * the type pointed to by the base_ptr parameter (e.g. int is the element of
  97.  * an int* pointer)
  98.  *
  99.  * When LLVM lowers the load instruction, it will convert the element offset
  100.  * into a dword offset automatically.
  101.  *
  102.  */
  103. static LLVMValueRef build_indexed_load(
  104.         struct si_shader_context * si_shader_ctx,
  105.         LLVMValueRef base_ptr,
  106.         LLVMValueRef offset)
  107. {
  108.         struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
  109.  
  110.         LLVMValueRef computed_ptr = LLVMBuildGEP(
  111.                 base->gallivm->builder, base_ptr, &offset, 1, "");
  112.  
  113.         LLVMValueRef result = LLVMBuildLoad(base->gallivm->builder, computed_ptr, "");
  114.         LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
  115.         return result;
  116. }
  117.  
  118. static LLVMValueRef get_instance_index(
  119.         struct radeon_llvm_context * radeon_bld,
  120.         unsigned divisor)
  121. {
  122.         struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
  123.  
  124.         LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_INSTANCE_ID);
  125.         result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
  126.                         radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
  127.  
  128.         if (divisor > 1)
  129.                 result = LLVMBuildUDiv(gallivm->builder, result,
  130.                                 lp_build_const_int32(gallivm, divisor), "");
  131.  
  132.         return result;
  133. }
  134.  
  135. static void declare_input_vs(
  136.         struct si_shader_context * si_shader_ctx,
  137.         unsigned input_index,
  138.         const struct tgsi_full_declaration *decl)
  139. {
  140.         struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
  141.         unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
  142.  
  143.         unsigned chan;
  144.  
  145.         LLVMValueRef t_list_ptr;
  146.         LLVMValueRef t_offset;
  147.         LLVMValueRef t_list;
  148.         LLVMValueRef attribute_offset;
  149.         LLVMValueRef buffer_index;
  150.         LLVMValueRef args[3];
  151.         LLVMTypeRef vec4_type;
  152.         LLVMValueRef input;
  153.  
  154.         /* Load the T list */
  155.         t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
  156.  
  157.         t_offset = lp_build_const_int32(base->gallivm, input_index);
  158.  
  159.         t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
  160.  
  161.         /* Build the attribute offset */
  162.         attribute_offset = lp_build_const_int32(base->gallivm, 0);
  163.  
  164.         if (divisor) {
  165.                 /* Build index from instance ID, start instance and divisor */
  166.                 si_shader_ctx->shader->shader.uses_instanceid = true;
  167.                 buffer_index = get_instance_index(&si_shader_ctx->radeon_bld, divisor);
  168.         } else {
  169.                 /* Load the buffer index, which is always stored in VGPR0
  170.                  * for Vertex Shaders */
  171.                 buffer_index = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_ID);
  172.         }
  173.  
  174.         vec4_type = LLVMVectorType(base->elem_type, 4);
  175.         args[0] = t_list;
  176.         args[1] = attribute_offset;
  177.         args[2] = buffer_index;
  178.         input = build_intrinsic(base->gallivm->builder,
  179.                 "llvm.SI.vs.load.input", vec4_type, args, 3,
  180.                 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
  181.  
  182.         /* Break up the vec4 into individual components */
  183.         for (chan = 0; chan < 4; chan++) {
  184.                 LLVMValueRef llvm_chan = lp_build_const_int32(base->gallivm, chan);
  185.                 /* XXX: Use a helper function for this.  There is one in
  186.                  * tgsi_llvm.c. */
  187.                 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
  188.                                 LLVMBuildExtractElement(base->gallivm->builder,
  189.                                 input, llvm_chan, "");
  190.         }
  191. }
  192.  
  193. static void declare_input_fs(
  194.         struct si_shader_context * si_shader_ctx,
  195.         unsigned input_index,
  196.         const struct tgsi_full_declaration *decl)
  197. {
  198.         struct si_shader *shader = &si_shader_ctx->shader->shader;
  199.         struct lp_build_context * base =
  200.                                 &si_shader_ctx->radeon_bld.soa.bld_base.base;
  201.         struct lp_build_context *uint =
  202.                                 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
  203.         struct gallivm_state * gallivm = base->gallivm;
  204.         LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
  205.         LLVMValueRef main_fn = si_shader_ctx->radeon_bld.main_fn;
  206.  
  207.         LLVMValueRef interp_param;
  208.         const char * intr_name;
  209.  
  210.         /* This value is:
  211.          * [15:0] NewPrimMask (Bit mask for each quad.  It is set it the
  212.          *                     quad begins a new primitive.  Bit 0 always needs
  213.          *                     to be unset)
  214.          * [32:16] ParamOffset
  215.          *
  216.          */
  217.         LLVMValueRef params = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_PRIM_MASK);
  218.         LLVMValueRef attr_number;
  219.  
  220.         unsigned chan;
  221.  
  222.         if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
  223.                 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
  224.                         unsigned soa_index =
  225.                                 radeon_llvm_reg_index_soa(input_index, chan);
  226.                         si_shader_ctx->radeon_bld.inputs[soa_index] =
  227.                                 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
  228.  
  229.                         if (chan == 3)
  230.                                 /* RCP for fragcoord.w */
  231.                                 si_shader_ctx->radeon_bld.inputs[soa_index] =
  232.                                         LLVMBuildFDiv(gallivm->builder,
  233.                                                       lp_build_const_float(gallivm, 1.0f),
  234.                                                       si_shader_ctx->radeon_bld.inputs[soa_index],
  235.                                                       "");
  236.                 }
  237.                 return;
  238.         }
  239.  
  240.         if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
  241.                 LLVMValueRef face, is_face_positive;
  242.  
  243.                 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
  244.  
  245.                 is_face_positive = LLVMBuildFCmp(gallivm->builder,
  246.                                                  LLVMRealUGT, face,
  247.                                                  lp_build_const_float(gallivm, 0.0f),
  248.                                                  "");
  249.  
  250.                 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
  251.                         LLVMBuildSelect(gallivm->builder,
  252.                                         is_face_positive,
  253.                                         lp_build_const_float(gallivm, 1.0f),
  254.                                         lp_build_const_float(gallivm, 0.0f),
  255.                                         "");
  256.                 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
  257.                 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
  258.                         lp_build_const_float(gallivm, 0.0f);
  259.                 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
  260.                         lp_build_const_float(gallivm, 1.0f);
  261.  
  262.                 return;
  263.         }
  264.  
  265.         shader->input[input_index].param_offset = shader->ninterp++;
  266.         attr_number = lp_build_const_int32(gallivm,
  267.                                            shader->input[input_index].param_offset);
  268.  
  269.         /* XXX: Handle all possible interpolation modes */
  270.         switch (decl->Interp.Interpolate) {
  271.         case TGSI_INTERPOLATE_COLOR:
  272.                 if (si_shader_ctx->shader->key.ps.flatshade) {
  273.                         interp_param = 0;
  274.                 } else {
  275.                         if (decl->Interp.Centroid)
  276.                                 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
  277.                         else
  278.                                 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
  279.                 }
  280.                 break;
  281.         case TGSI_INTERPOLATE_CONSTANT:
  282.                 interp_param = 0;
  283.                 break;
  284.         case TGSI_INTERPOLATE_LINEAR:
  285.                 if (decl->Interp.Centroid)
  286.                         interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
  287.                 else
  288.                         interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
  289.                 break;
  290.         case TGSI_INTERPOLATE_PERSPECTIVE:
  291.                 if (decl->Interp.Centroid)
  292.                         interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
  293.                 else
  294.                         interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
  295.                 break;
  296.         default:
  297.                 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
  298.                 return;
  299.         }
  300.  
  301.         intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
  302.  
  303.         /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
  304.         if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
  305.             si_shader_ctx->shader->key.ps.color_two_side) {
  306.                 LLVMValueRef args[4];
  307.                 LLVMValueRef face, is_face_positive;
  308.                 LLVMValueRef back_attr_number =
  309.                         lp_build_const_int32(gallivm,
  310.                                              shader->input[input_index].param_offset + 1);
  311.  
  312.                 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
  313.  
  314.                 is_face_positive = LLVMBuildFCmp(gallivm->builder,
  315.                                                  LLVMRealUGT, face,
  316.                                                  lp_build_const_float(gallivm, 0.0f),
  317.                                                  "");
  318.  
  319.                 args[2] = params;
  320.                 args[3] = interp_param;
  321.                 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
  322.                         LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
  323.                         unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
  324.                         LLVMValueRef front, back;
  325.  
  326.                         args[0] = llvm_chan;
  327.                         args[1] = attr_number;
  328.                         front = build_intrinsic(base->gallivm->builder, intr_name,
  329.                                                 input_type, args, args[3] ? 4 : 3,
  330.                                                 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
  331.  
  332.                         args[1] = back_attr_number;
  333.                         back = build_intrinsic(base->gallivm->builder, intr_name,
  334.                                                input_type, args, args[3] ? 4 : 3,
  335.                                                LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
  336.  
  337.                         si_shader_ctx->radeon_bld.inputs[soa_index] =
  338.                                 LLVMBuildSelect(gallivm->builder,
  339.                                                 is_face_positive,
  340.                                                 front,
  341.                                                 back,
  342.                                                 "");
  343.                 }
  344.  
  345.                 shader->ninterp++;
  346.         } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
  347.                 LLVMValueRef args[4];
  348.  
  349.                 args[0] = uint->zero;
  350.                 args[1] = attr_number;
  351.                 args[2] = params;
  352.                 args[3] = interp_param;
  353.                 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
  354.                         build_intrinsic(base->gallivm->builder, intr_name,
  355.                                                 input_type, args, args[3] ? 4 : 3,
  356.                                                 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
  357.                 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
  358.                 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
  359.                         lp_build_const_float(gallivm, 0.0f);
  360.                 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
  361.                         lp_build_const_float(gallivm, 1.0f);
  362.         } else {
  363.                 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
  364.                         LLVMValueRef args[4];
  365.                         LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
  366.                         unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
  367.                         args[0] = llvm_chan;
  368.                         args[1] = attr_number;
  369.                         args[2] = params;
  370.                         args[3] = interp_param;
  371.                         si_shader_ctx->radeon_bld.inputs[soa_index] =
  372.                                 build_intrinsic(base->gallivm->builder, intr_name,
  373.                                                 input_type, args, args[3] ? 4 : 3,
  374.                                                 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
  375.                 }
  376.         }
  377. }
  378.  
  379. static void declare_input(
  380.         struct radeon_llvm_context * radeon_bld,
  381.         unsigned input_index,
  382.         const struct tgsi_full_declaration *decl)
  383. {
  384.         struct si_shader_context * si_shader_ctx =
  385.                                 si_shader_context(&radeon_bld->soa.bld_base);
  386.         if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
  387.                 declare_input_vs(si_shader_ctx, input_index, decl);
  388.         } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
  389.                 declare_input_fs(si_shader_ctx, input_index, decl);
  390.         } else {
  391.                 fprintf(stderr, "Warning: Unsupported shader type,\n");
  392.         }
  393. }
  394.  
  395. static void declare_system_value(
  396.         struct radeon_llvm_context * radeon_bld,
  397.         unsigned index,
  398.         const struct tgsi_full_declaration *decl)
  399. {
  400.  
  401.         LLVMValueRef value = 0;
  402.  
  403.         switch (decl->Semantic.Name) {
  404.         case TGSI_SEMANTIC_INSTANCEID:
  405.                 value = get_instance_index(radeon_bld, 1);
  406.                 break;
  407.  
  408.         case TGSI_SEMANTIC_VERTEXID:
  409.                 value = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_VERTEX_ID);
  410.                 break;
  411.  
  412.         default:
  413.                 assert(!"unknown system value");
  414.                 return;
  415.         }
  416.  
  417.         radeon_bld->system_values[index] = value;
  418. }
  419.  
  420. static LLVMValueRef fetch_constant(
  421.         struct lp_build_tgsi_context * bld_base,
  422.         const struct tgsi_full_src_register *reg,
  423.         enum tgsi_opcode_type type,
  424.         unsigned swizzle)
  425. {
  426.         struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
  427.         struct lp_build_context * base = &bld_base->base;
  428.         const struct tgsi_ind_register *ireg = &reg->Indirect;
  429.         unsigned idx;
  430.  
  431.         LLVMValueRef args[2];
  432.         LLVMValueRef addr;
  433.         LLVMValueRef result;
  434.  
  435.         if (swizzle == LP_CHAN_ALL) {
  436.                 unsigned chan;
  437.                 LLVMValueRef values[4];
  438.                 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
  439.                         values[chan] = fetch_constant(bld_base, reg, type, chan);
  440.  
  441.                 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
  442.         }
  443.  
  444.         idx = reg->Register.Index * 4 + swizzle;
  445.         if (!reg->Register.Indirect)
  446.                 return bitcast(bld_base, type, si_shader_ctx->constants[idx]);
  447.  
  448.         args[0] = si_shader_ctx->const_resource;
  449.         args[1] = lp_build_const_int32(base->gallivm, idx * 4);
  450.         addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
  451.         addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
  452.         addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
  453.         args[1] = lp_build_add(&bld_base->uint_bld, addr, args[1]);
  454.  
  455.         result = build_intrinsic(base->gallivm->builder, "llvm.SI.load.const", base->elem_type,
  456.                                  args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
  457.  
  458.         return bitcast(bld_base, type, result);
  459. }
  460.  
  461. /* Initialize arguments for the shader export intrinsic */
  462. static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
  463.                                      struct tgsi_full_declaration *d,
  464.                                      unsigned index,
  465.                                      unsigned target,
  466.                                      LLVMValueRef *args)
  467. {
  468.         struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
  469.         struct lp_build_context *uint =
  470.                                 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
  471.         struct lp_build_context *base = &bld_base->base;
  472.         unsigned compressed = 0;
  473.         unsigned chan;
  474.  
  475.         if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
  476.                 int cbuf = target - V_008DFC_SQ_EXP_MRT;
  477.  
  478.                 if (cbuf >= 0 && cbuf < 8) {
  479.                         compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
  480.  
  481.                         if (compressed)
  482.                                 si_shader_ctx->shader->spi_shader_col_format |=
  483.                                         V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
  484.                         else
  485.                                 si_shader_ctx->shader->spi_shader_col_format |=
  486.                                         V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
  487.  
  488.                         si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
  489.                 }
  490.         }
  491.  
  492.         if (compressed) {
  493.                 /* Pixel shader needs to pack output values before export */
  494.                 for (chan = 0; chan < 2; chan++ ) {
  495.                         LLVMValueRef *out_ptr =
  496.                                 si_shader_ctx->radeon_bld.soa.outputs[index];
  497.                         args[0] = LLVMBuildLoad(base->gallivm->builder,
  498.                                                 out_ptr[2 * chan], "");
  499.                         args[1] = LLVMBuildLoad(base->gallivm->builder,
  500.                                                 out_ptr[2 * chan + 1], "");
  501.                         args[chan + 5] =
  502.                                 build_intrinsic(base->gallivm->builder,
  503.                                                 "llvm.SI.packf16",
  504.                                                 LLVMInt32TypeInContext(base->gallivm->context),
  505.                                                 args, 2,
  506.                                                 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
  507.                         args[chan + 7] = args[chan + 5] =
  508.                                 LLVMBuildBitCast(base->gallivm->builder,
  509.                                                  args[chan + 5],
  510.                                                  LLVMFloatTypeInContext(base->gallivm->context),
  511.                                                  "");
  512.                 }
  513.  
  514.                 /* Set COMPR flag */
  515.                 args[4] = uint->one;
  516.         } else {
  517.                 for (chan = 0; chan < 4; chan++ ) {
  518.                         LLVMValueRef out_ptr =
  519.                                 si_shader_ctx->radeon_bld.soa.outputs[index][chan];
  520.                         /* +5 because the first output value will be
  521.                          * the 6th argument to the intrinsic. */
  522.                         args[chan + 5] = LLVMBuildLoad(base->gallivm->builder,
  523.                                                        out_ptr, "");
  524.                 }
  525.  
  526.                 /* Clear COMPR flag */
  527.                 args[4] = uint->zero;
  528.         }
  529.  
  530.         /* XXX: This controls which components of the output
  531.          * registers actually get exported. (e.g bit 0 means export
  532.          * X component, bit 1 means export Y component, etc.)  I'm
  533.          * hard coding this to 0xf for now.  In the future, we might
  534.          * want to do something else. */
  535.         args[0] = lp_build_const_int32(base->gallivm, 0xf);
  536.  
  537.         /* Specify whether the EXEC mask represents the valid mask */
  538.         args[1] = uint->zero;
  539.  
  540.         /* Specify whether this is the last export */
  541.         args[2] = uint->zero;
  542.  
  543.         /* Specify the target we are exporting */
  544.         args[3] = lp_build_const_int32(base->gallivm, target);
  545.  
  546.         /* XXX: We probably need to keep track of the output
  547.          * values, so we know what we are passing to the next
  548.          * stage. */
  549. }
  550.  
  551. static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
  552.                           unsigned index)
  553. {
  554.         struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
  555.         struct gallivm_state *gallivm = bld_base->base.gallivm;
  556.  
  557.         if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
  558.                 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][3];
  559.                 LLVMValueRef alpha_pass =
  560.                         lp_build_cmp(&bld_base->base,
  561.                                      si_shader_ctx->shader->key.ps.alpha_func,
  562.                                      LLVMBuildLoad(gallivm->builder, out_ptr, ""),
  563.                                      lp_build_const_float(gallivm, si_shader_ctx->shader->key.ps.alpha_ref));
  564.                 LLVMValueRef arg =
  565.                         lp_build_select(&bld_base->base,
  566.                                         alpha_pass,
  567.                                         lp_build_const_float(gallivm, 1.0f),
  568.                                         lp_build_const_float(gallivm, -1.0f));
  569.  
  570.                 build_intrinsic(gallivm->builder,
  571.                                 "llvm.AMDGPU.kill",
  572.                                 LLVMVoidTypeInContext(gallivm->context),
  573.                                 &arg, 1, 0);
  574.         } else {
  575.                 build_intrinsic(gallivm->builder,
  576.                                 "llvm.AMDGPU.kilp",
  577.                                 LLVMVoidTypeInContext(gallivm->context),
  578.                                 NULL, 0, 0);
  579.         }
  580. }
  581.  
  582. static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
  583.                                     LLVMValueRef (*pos)[9], unsigned index)
  584. {
  585.         struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
  586.         struct lp_build_context *base = &bld_base->base;
  587.         struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
  588.         unsigned reg_index;
  589.         unsigned chan;
  590.         unsigned const_chan;
  591.         LLVMValueRef out_elts[4];
  592.         LLVMValueRef base_elt;
  593.         LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
  594.         LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, uint->one);
  595.  
  596.         for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
  597.                 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][chan];
  598.                 out_elts[chan] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
  599.         }
  600.  
  601.         for (reg_index = 0; reg_index < 2; reg_index ++) {
  602.                 LLVMValueRef *args = pos[2 + reg_index];
  603.  
  604.                 args[5] =
  605.                 args[6] =
  606.                 args[7] =
  607.                 args[8] = lp_build_const_float(base->gallivm, 0.0f);
  608.  
  609.                 /* Compute dot products of position and user clip plane vectors */
  610.                 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
  611.                         for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
  612.                                 args[0] = const_resource;
  613.                                 args[1] = lp_build_const_int32(base->gallivm,
  614.                                                                ((reg_index * 4 + chan) * 4 +
  615.                                                                 const_chan) * 4);
  616.                                 base_elt = build_intrinsic(base->gallivm->builder,
  617.                                                            "llvm.SI.load.const",
  618.                                                            base->elem_type,
  619.                                                            args, 2,
  620.                                                            LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
  621.                                 args[5 + chan] =
  622.                                         lp_build_add(base, args[5 + chan],
  623.                                                      lp_build_mul(base, base_elt,
  624.                                                                   out_elts[const_chan]));
  625.                         }
  626.                 }
  627.  
  628.                 args[0] = lp_build_const_int32(base->gallivm, 0xf);
  629.                 args[1] = uint->zero;
  630.                 args[2] = uint->zero;
  631.                 args[3] = lp_build_const_int32(base->gallivm,
  632.                                                V_008DFC_SQ_EXP_POS + 2 + reg_index);
  633.                 args[4] = uint->zero;
  634.         }
  635. }
  636.  
  637. /* XXX: This is partially implemented for VS only at this point.  It is not complete */
  638. static void si_llvm_emit_epilogue(struct lp_build_tgsi_context * bld_base)
  639. {
  640.         struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
  641.         struct si_shader * shader = &si_shader_ctx->shader->shader;
  642.         struct lp_build_context * base = &bld_base->base;
  643.         struct lp_build_context * uint =
  644.                                 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
  645.         struct tgsi_parse_context *parse = &si_shader_ctx->parse;
  646.         LLVMValueRef args[9];
  647.         LLVMValueRef last_args[9] = { 0 };
  648.         LLVMValueRef pos_args[4][9] = { { 0 } };
  649.         unsigned semantic_name;
  650.         unsigned color_count = 0;
  651.         unsigned param_count = 0;
  652.         int depth_index = -1, stencil_index = -1;
  653.         int i;
  654.  
  655.         while (!tgsi_parse_end_of_tokens(parse)) {
  656.                 struct tgsi_full_declaration *d =
  657.                                         &parse->FullToken.FullDeclaration;
  658.                 unsigned target;
  659.                 unsigned index;
  660.  
  661.                 tgsi_parse_token(parse);
  662.  
  663.                 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
  664.                     parse->FullToken.FullProperty.Property.PropertyName ==
  665.                     TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
  666.                         shader->fs_write_all = TRUE;
  667.  
  668.                 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
  669.                         continue;
  670.  
  671.                 switch (d->Declaration.File) {
  672.                 case TGSI_FILE_INPUT:
  673.                         i = shader->ninput++;
  674.                         assert(i < Elements(shader->input));
  675.                         shader->input[i].name = d->Semantic.Name;
  676.                         shader->input[i].sid = d->Semantic.Index;
  677.                         shader->input[i].interpolate = d->Interp.Interpolate;
  678.                         shader->input[i].centroid = d->Interp.Centroid;
  679.                         continue;
  680.  
  681.                 case TGSI_FILE_OUTPUT:
  682.                         i = shader->noutput++;
  683.                         assert(i < Elements(shader->output));
  684.                         shader->output[i].name = d->Semantic.Name;
  685.                         shader->output[i].sid = d->Semantic.Index;
  686.                         shader->output[i].interpolate = d->Interp.Interpolate;
  687.                         break;
  688.  
  689.                 default:
  690.                         continue;
  691.                 }
  692.  
  693.                 semantic_name = d->Semantic.Name;
  694. handle_semantic:
  695.                 for (index = d->Range.First; index <= d->Range.Last; index++) {
  696.                         /* Select the correct target */
  697.                         switch(semantic_name) {
  698.                         case TGSI_SEMANTIC_PSIZE:
  699.                                 shader->vs_out_misc_write = 1;
  700.                                 shader->vs_out_point_size = 1;
  701.                                 target = V_008DFC_SQ_EXP_POS + 1;
  702.                                 break;
  703.                         case TGSI_SEMANTIC_POSITION:
  704.                                 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
  705.                                         target = V_008DFC_SQ_EXP_POS;
  706.                                         break;
  707.                                 } else {
  708.                                         depth_index = index;
  709.                                         continue;
  710.                                 }
  711.                         case TGSI_SEMANTIC_STENCIL:
  712.                                 stencil_index = index;
  713.                                 continue;
  714.                         case TGSI_SEMANTIC_COLOR:
  715.                                 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
  716.                         case TGSI_SEMANTIC_BCOLOR:
  717.                                         target = V_008DFC_SQ_EXP_PARAM + param_count;
  718.                                         shader->output[i].param_offset = param_count;
  719.                                         param_count++;
  720.                                 } else {
  721.                                         target = V_008DFC_SQ_EXP_MRT + color_count;
  722.                                         if (color_count == 0 &&
  723.                                             si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
  724.                                                 si_alpha_test(bld_base, index);
  725.  
  726.                                         color_count++;
  727.                                 }
  728.                                 break;
  729.                         case TGSI_SEMANTIC_CLIPDIST:
  730.                                 shader->clip_dist_write |=
  731.                                         d->Declaration.UsageMask << (d->Semantic.Index << 2);
  732.                                 target = V_008DFC_SQ_EXP_POS + 2 + d->Semantic.Index;
  733.                                 break;
  734.                         case TGSI_SEMANTIC_CLIPVERTEX:
  735.                                 si_llvm_emit_clipvertex(bld_base, pos_args, index);
  736.                                 shader->clip_dist_write = 0xFF;
  737.                                 continue;
  738.                         case TGSI_SEMANTIC_FOG:
  739.                         case TGSI_SEMANTIC_GENERIC:
  740.                                 target = V_008DFC_SQ_EXP_PARAM + param_count;
  741.                                 shader->output[i].param_offset = param_count;
  742.                                 param_count++;
  743.                                 break;
  744.                         default:
  745.                                 target = 0;
  746.                                 fprintf(stderr,
  747.                                         "Warning: SI unhandled output type:%d\n",
  748.                                         semantic_name);
  749.                         }
  750.  
  751.                         si_llvm_init_export_args(bld_base, d, index, target, args);
  752.  
  753.                         if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX &&
  754.                             target >= V_008DFC_SQ_EXP_POS &&
  755.                             target <= (V_008DFC_SQ_EXP_POS + 3)) {
  756.                                 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
  757.                                        args, sizeof(args));
  758.                         } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT &&
  759.                                    semantic_name == TGSI_SEMANTIC_COLOR) {
  760.                                 if (last_args[0]) {
  761.                                         lp_build_intrinsic(base->gallivm->builder,
  762.                                                            "llvm.SI.export",
  763.                                                            LLVMVoidTypeInContext(base->gallivm->context),
  764.                                                            last_args, 9);
  765.                                 }
  766.  
  767.                                 memcpy(last_args, args, sizeof(args));
  768.                         } else {
  769.                                 lp_build_intrinsic(base->gallivm->builder,
  770.                                                    "llvm.SI.export",
  771.                                                    LLVMVoidTypeInContext(base->gallivm->context),
  772.                                                    args, 9);
  773.                         }
  774.  
  775.                 }
  776.  
  777.                 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
  778.                         semantic_name = TGSI_SEMANTIC_GENERIC;
  779.                         goto handle_semantic;
  780.                 }
  781.         }
  782.  
  783.         if (depth_index >= 0 || stencil_index >= 0) {
  784.                 LLVMValueRef out_ptr;
  785.                 unsigned mask = 0;
  786.  
  787.                 /* Specify the target we are exporting */
  788.                 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
  789.  
  790.                 if (depth_index >= 0) {
  791.                         out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
  792.                         args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
  793.                         mask |= 0x1;
  794.  
  795.                         if (stencil_index < 0) {
  796.                                 args[6] =
  797.                                 args[7] =
  798.                                 args[8] = args[5];
  799.                         }
  800.                 }
  801.  
  802.                 if (stencil_index >= 0) {
  803.                         out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
  804.                         args[7] =
  805.                         args[8] =
  806.                         args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
  807.                         /* Only setting the stencil component bit (0x2) here
  808.                          * breaks some stencil piglit tests
  809.                          */
  810.                         mask |= 0x3;
  811.  
  812.                         if (depth_index < 0)
  813.                                 args[5] = args[6];
  814.                 }
  815.  
  816.                 /* Specify which components to enable */
  817.                 args[0] = lp_build_const_int32(base->gallivm, mask);
  818.  
  819.                 args[1] =
  820.                 args[2] =
  821.                 args[4] = uint->zero;
  822.  
  823.                 if (last_args[0])
  824.                         lp_build_intrinsic(base->gallivm->builder,
  825.                                            "llvm.SI.export",
  826.                                            LLVMVoidTypeInContext(base->gallivm->context),
  827.                                            args, 9);
  828.                 else
  829.                         memcpy(last_args, args, sizeof(args));
  830.         }
  831.  
  832.         if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
  833.                 unsigned pos_idx = 0;
  834.  
  835.                 for (i = 0; i < 4; i++)
  836.                         if (pos_args[i][0])
  837.                                 shader->nr_pos_exports++;
  838.  
  839.                 for (i = 0; i < 4; i++) {
  840.                         if (!pos_args[i][0])
  841.                                 continue;
  842.  
  843.                         /* Specify the target we are exporting */
  844.                         pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
  845.  
  846.                         if (pos_idx == shader->nr_pos_exports)
  847.                                 /* Specify that this is the last export */
  848.                                 pos_args[i][2] = uint->one;
  849.  
  850.                         lp_build_intrinsic(base->gallivm->builder,
  851.                                            "llvm.SI.export",
  852.                                            LLVMVoidTypeInContext(base->gallivm->context),
  853.                                            pos_args[i], 9);
  854.                 }
  855.         } else {
  856.                 if (!last_args[0]) {
  857.                         /* Specify which components to enable */
  858.                         last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
  859.  
  860.                         /* Specify the target we are exporting */
  861.                         last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
  862.  
  863.                         /* Set COMPR flag to zero to export data as 32-bit */
  864.                         last_args[4] = uint->zero;
  865.  
  866.                         /* dummy bits */
  867.                         last_args[5]= uint->zero;
  868.                         last_args[6]= uint->zero;
  869.                         last_args[7]= uint->zero;
  870.                         last_args[8]= uint->zero;
  871.  
  872.                         si_shader_ctx->shader->spi_shader_col_format |=
  873.                                 V_028714_SPI_SHADER_32_ABGR;
  874.                         si_shader_ctx->shader->cb_shader_mask |= S_02823C_OUTPUT0_ENABLE(0xf);
  875.                 }
  876.  
  877.                 /* Specify whether the EXEC mask represents the valid mask */
  878.                 last_args[1] = uint->one;
  879.  
  880.                 if (shader->fs_write_all && shader->nr_cbufs > 1) {
  881.                         int i;
  882.  
  883.                         /* Specify that this is not yet the last export */
  884.                         last_args[2] = lp_build_const_int32(base->gallivm, 0);
  885.  
  886.                         for (i = 1; i < shader->nr_cbufs; i++) {
  887.                                 /* Specify the target we are exporting */
  888.                                 last_args[3] = lp_build_const_int32(base->gallivm,
  889.                                                                     V_008DFC_SQ_EXP_MRT + i);
  890.  
  891.                                 lp_build_intrinsic(base->gallivm->builder,
  892.                                                    "llvm.SI.export",
  893.                                                    LLVMVoidTypeInContext(base->gallivm->context),
  894.                                                    last_args, 9);
  895.  
  896.                                 si_shader_ctx->shader->spi_shader_col_format |=
  897.                                         si_shader_ctx->shader->spi_shader_col_format << 4;
  898.                                 si_shader_ctx->shader->cb_shader_mask |=
  899.                                         si_shader_ctx->shader->cb_shader_mask << 4;
  900.                         }
  901.  
  902.                         last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
  903.                 }
  904.  
  905.                 /* Specify that this is the last export */
  906.                 last_args[2] = lp_build_const_int32(base->gallivm, 1);
  907.  
  908.                 lp_build_intrinsic(base->gallivm->builder,
  909.                                    "llvm.SI.export",
  910.                                    LLVMVoidTypeInContext(base->gallivm->context),
  911.                                    last_args, 9);
  912.         }
  913. /* XXX: Look up what this function does */
  914. /*              ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
  915. }
  916.  
  917. static void tex_fetch_args(
  918.         struct lp_build_tgsi_context * bld_base,
  919.         struct lp_build_emit_data * emit_data)
  920. {
  921.         struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
  922.         struct gallivm_state *gallivm = bld_base->base.gallivm;
  923.         const struct tgsi_full_instruction * inst = emit_data->inst;
  924.         unsigned opcode = inst->Instruction.Opcode;
  925.         unsigned target = inst->Texture.Texture;
  926.         unsigned sampler_src;
  927.         LLVMValueRef coords[4];
  928.         LLVMValueRef address[16];
  929.         int ref_pos;
  930.         unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
  931.         unsigned count = 0;
  932.         unsigned chan;
  933.  
  934.         /* Fetch and project texture coordinates */
  935.         coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
  936.         for (chan = 0; chan < 3; chan++ ) {
  937.                 coords[chan] = lp_build_emit_fetch(bld_base,
  938.                                                    emit_data->inst, 0,
  939.                                                    chan);
  940.                 if (opcode == TGSI_OPCODE_TXP)
  941.                         coords[chan] = lp_build_emit_llvm_binary(bld_base,
  942.                                                                  TGSI_OPCODE_DIV,
  943.                                                                  coords[chan],
  944.                                                                  coords[3]);
  945.         }
  946.  
  947.         if (opcode == TGSI_OPCODE_TXP)
  948.                 coords[3] = bld_base->base.one;
  949.  
  950.         /* Pack LOD bias value */
  951.         if (opcode == TGSI_OPCODE_TXB)
  952.                 address[count++] = coords[3];
  953.  
  954.         if (target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE)
  955.                 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
  956.  
  957.         /* Pack depth comparison value */
  958.         switch (target) {
  959.         case TGSI_TEXTURE_SHADOW1D:
  960.         case TGSI_TEXTURE_SHADOW1D_ARRAY:
  961.         case TGSI_TEXTURE_SHADOW2D:
  962.         case TGSI_TEXTURE_SHADOWRECT:
  963.         case TGSI_TEXTURE_SHADOWCUBE:
  964.         case TGSI_TEXTURE_SHADOW2D_ARRAY:
  965.                 assert(ref_pos >= 0);
  966.                 address[count++] = coords[ref_pos];
  967.                 break;
  968.         case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
  969.                 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
  970.         }
  971.  
  972.         /* Pack user derivatives */
  973.         if (opcode == TGSI_OPCODE_TXD) {
  974.                 for (chan = 0; chan < 2; chan++) {
  975.                         address[count++] = lp_build_emit_fetch(bld_base, inst, 1, chan);
  976.                         if (num_coords > 1)
  977.                                 address[count++] = lp_build_emit_fetch(bld_base, inst, 2, chan);
  978.                 }
  979.         }
  980.  
  981.         /* Pack texture coordinates */
  982.         address[count++] = coords[0];
  983.         if (num_coords > 1)
  984.                 address[count++] = coords[1];
  985.         if (num_coords > 2)
  986.                 address[count++] = coords[2];
  987.  
  988.         /* Pack array slice */
  989.         switch (target) {
  990.         case TGSI_TEXTURE_1D_ARRAY:
  991.                 address[count++] = coords[1];
  992.         }
  993.         switch (target) {
  994.         case TGSI_TEXTURE_2D_ARRAY:
  995.         case TGSI_TEXTURE_2D_ARRAY_MSAA:
  996.         case TGSI_TEXTURE_SHADOW2D_ARRAY:
  997.                 address[count++] = coords[2];
  998.         }
  999.         switch (target) {
  1000.         case TGSI_TEXTURE_CUBE_ARRAY:
  1001.         case TGSI_TEXTURE_SHADOW1D_ARRAY:
  1002.         case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
  1003.                 address[count++] = coords[3];
  1004.         }
  1005.  
  1006.         /* Pack LOD */
  1007.         if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
  1008.                 address[count++] = coords[3];
  1009.  
  1010.         if (count > 16) {
  1011.                 assert(!"Cannot handle more than 16 texture address parameters");
  1012.                 count = 16;
  1013.         }
  1014.  
  1015.         for (chan = 0; chan < count; chan++ ) {
  1016.                 address[chan] = LLVMBuildBitCast(gallivm->builder,
  1017.                                                  address[chan],
  1018.                                                  LLVMInt32TypeInContext(gallivm->context),
  1019.                                                  "");
  1020.         }
  1021.  
  1022.         sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
  1023.  
  1024.         /* Resource */
  1025.         emit_data->args[1] = si_shader_ctx->resources[emit_data->inst->Src[sampler_src].Register.Index];
  1026.  
  1027.         if (opcode == TGSI_OPCODE_TXF) {
  1028.                 /* add tex offsets */
  1029.                 if (inst->Texture.NumOffsets) {
  1030.                         struct lp_build_context *uint_bld = &bld_base->uint_bld;
  1031.                         struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
  1032.                         const struct tgsi_texture_offset * off = inst->TexOffsets;
  1033.  
  1034.                         assert(inst->Texture.NumOffsets == 1);
  1035.  
  1036.                         address[0] =
  1037.                                 lp_build_add(uint_bld, address[0],
  1038.                                              bld->immediates[off->Index][off->SwizzleX]);
  1039.                         if (num_coords > 1)
  1040.                                 address[1] =
  1041.                                         lp_build_add(uint_bld, address[1],
  1042.                                                      bld->immediates[off->Index][off->SwizzleY]);
  1043.                         if (num_coords > 2)
  1044.                                 address[2] =
  1045.                                         lp_build_add(uint_bld, address[2],
  1046.                                                      bld->immediates[off->Index][off->SwizzleZ]);
  1047.                 }
  1048.  
  1049.                 emit_data->dst_type = LLVMVectorType(
  1050.                         LLVMInt32TypeInContext(bld_base->base.gallivm->context),
  1051.                         4);
  1052.  
  1053.                 emit_data->arg_count = 3;
  1054.         } else {
  1055.                 /* Sampler */
  1056.                 emit_data->args[2] = si_shader_ctx->samplers[emit_data->inst->Src[sampler_src].Register.Index];
  1057.  
  1058.                 emit_data->dst_type = LLVMVectorType(
  1059.                         LLVMFloatTypeInContext(bld_base->base.gallivm->context),
  1060.                         4);
  1061.  
  1062.                 emit_data->arg_count = 4;
  1063.         }
  1064.  
  1065.         /* Dimensions */
  1066.         emit_data->args[emit_data->arg_count - 1] =
  1067.                 lp_build_const_int32(bld_base->base.gallivm, target);
  1068.  
  1069.         /* Pad to power of two vector */
  1070.         while (count < util_next_power_of_two(count))
  1071.                 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
  1072.  
  1073.         emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
  1074. }
  1075.  
  1076. static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
  1077.                                 struct lp_build_tgsi_context * bld_base,
  1078.                                 struct lp_build_emit_data * emit_data)
  1079. {
  1080.         struct lp_build_context * base = &bld_base->base;
  1081.         char intr_name[23];
  1082.  
  1083.         sprintf(intr_name, "%sv%ui32", action->intr_name,
  1084.                 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
  1085.  
  1086.         emit_data->output[emit_data->chan] = build_intrinsic(
  1087.                 base->gallivm->builder, intr_name, emit_data->dst_type,
  1088.                 emit_data->args, emit_data->arg_count,
  1089.                 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
  1090. }
  1091.  
  1092. static void txq_fetch_args(
  1093.         struct lp_build_tgsi_context * bld_base,
  1094.         struct lp_build_emit_data * emit_data)
  1095. {
  1096.         struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
  1097.         const struct tgsi_full_instruction *inst = emit_data->inst;
  1098.  
  1099.         /* Mip level */
  1100.         emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
  1101.  
  1102.         /* Resource */
  1103.         emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
  1104.  
  1105.         /* Dimensions */
  1106.         emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
  1107.                                                   inst->Texture.Texture);
  1108.  
  1109.         emit_data->arg_count = 3;
  1110.  
  1111.         emit_data->dst_type = LLVMVectorType(
  1112.                 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
  1113.                 4);
  1114. }
  1115.  
  1116. #if HAVE_LLVM >= 0x0304
  1117.  
  1118. static void si_llvm_emit_ddxy(
  1119.         const struct lp_build_tgsi_action * action,
  1120.         struct lp_build_tgsi_context * bld_base,
  1121.         struct lp_build_emit_data * emit_data)
  1122. {
  1123.         struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
  1124.         struct gallivm_state *gallivm = bld_base->base.gallivm;
  1125.         struct lp_build_context * base = &bld_base->base;
  1126.         const struct tgsi_full_instruction *inst = emit_data->inst;
  1127.         unsigned opcode = inst->Instruction.Opcode;
  1128.         LLVMValueRef indices[2];
  1129.         LLVMValueRef store_ptr, load_ptr0, load_ptr1;
  1130.         LLVMValueRef tl, trbl, result[4];
  1131.         LLVMTypeRef i32;
  1132.         unsigned swizzle[4];
  1133.         unsigned c;
  1134.  
  1135.         i32 = LLVMInt32TypeInContext(gallivm->context);
  1136.  
  1137.         indices[0] = bld_base->uint_bld.zero;
  1138.         indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
  1139.                                      NULL, 0, LLVMReadNoneAttribute);
  1140.         store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
  1141.                                  indices, 2, "");
  1142.  
  1143.         indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
  1144.                                   lp_build_const_int32(gallivm, 0xfffffffc), "");
  1145.         load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
  1146.                                  indices, 2, "");
  1147.  
  1148.         indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
  1149.                                   lp_build_const_int32(gallivm,
  1150.                                                        opcode == TGSI_OPCODE_DDX ? 1 : 2),
  1151.                                   "");
  1152.         load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
  1153.                                  indices, 2, "");
  1154.  
  1155.         for (c = 0; c < 4; ++c) {
  1156.                 unsigned i;
  1157.  
  1158.                 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
  1159.                 for (i = 0; i < c; ++i) {
  1160.                         if (swizzle[i] == swizzle[c]) {
  1161.                                 result[c] = result[i];
  1162.                                 break;
  1163.                         }
  1164.                 }
  1165.                 if (i != c)
  1166.                         continue;
  1167.  
  1168.                 LLVMBuildStore(gallivm->builder,
  1169.                                LLVMBuildBitCast(gallivm->builder,
  1170.                                                 lp_build_emit_fetch(bld_base, inst, 0, c),
  1171.                                                 i32, ""),
  1172.                                store_ptr);
  1173.  
  1174.                 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
  1175.                 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
  1176.  
  1177.                 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
  1178.                 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
  1179.  
  1180.                 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
  1181.         }
  1182.  
  1183.         emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
  1184. }
  1185.  
  1186. #endif /* HAVE_LLVM >= 0x0304 */
  1187.  
  1188. static const struct lp_build_tgsi_action tex_action = {
  1189.         .fetch_args = tex_fetch_args,
  1190.         .emit = build_tex_intrinsic,
  1191.         .intr_name = "llvm.SI.sample."
  1192. };
  1193.  
  1194. static const struct lp_build_tgsi_action txb_action = {
  1195.         .fetch_args = tex_fetch_args,
  1196.         .emit = build_tex_intrinsic,
  1197.         .intr_name = "llvm.SI.sampleb."
  1198. };
  1199.  
  1200. #if HAVE_LLVM >= 0x0304
  1201. static const struct lp_build_tgsi_action txd_action = {
  1202.         .fetch_args = tex_fetch_args,
  1203.         .emit = build_tex_intrinsic,
  1204.         .intr_name = "llvm.SI.sampled."
  1205. };
  1206. #endif
  1207.  
  1208. static const struct lp_build_tgsi_action txf_action = {
  1209.         .fetch_args = tex_fetch_args,
  1210.         .emit = build_tex_intrinsic,
  1211.         .intr_name = "llvm.SI.imageload."
  1212. };
  1213.  
  1214. static const struct lp_build_tgsi_action txl_action = {
  1215.         .fetch_args = tex_fetch_args,
  1216.         .emit = build_tex_intrinsic,
  1217.         .intr_name = "llvm.SI.samplel."
  1218. };
  1219.  
  1220. static const struct lp_build_tgsi_action txq_action = {
  1221.         .fetch_args = txq_fetch_args,
  1222.         .emit = build_tgsi_intrinsic_nomem,
  1223.         .intr_name = "llvm.SI.resinfo"
  1224. };
  1225.  
  1226. static void create_meta_data(struct si_shader_context *si_shader_ctx)
  1227. {
  1228.         struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
  1229.         LLVMValueRef args[3];
  1230.  
  1231.         args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
  1232.         args[1] = 0;
  1233.         args[2] = lp_build_const_int32(gallivm, 1);
  1234.  
  1235.         si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
  1236. }
  1237.  
  1238. static void create_function(struct si_shader_context *si_shader_ctx)
  1239. {
  1240.         struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
  1241.         struct gallivm_state *gallivm = bld_base->base.gallivm;
  1242.         LLVMTypeRef params[20], f32, i8, i32, v2i32, v3i32;
  1243.         unsigned i;
  1244.  
  1245.         i8 = LLVMInt8TypeInContext(gallivm->context);
  1246.         i32 = LLVMInt32TypeInContext(gallivm->context);
  1247.         f32 = LLVMFloatTypeInContext(gallivm->context);
  1248.         v2i32 = LLVMVectorType(i32, 2);
  1249.         v3i32 = LLVMVectorType(i32, 3);
  1250.  
  1251.         params[SI_PARAM_CONST] = LLVMPointerType(LLVMVectorType(i8, 16), CONST_ADDR_SPACE);
  1252.         params[SI_PARAM_SAMPLER] = params[SI_PARAM_CONST];
  1253.         params[SI_PARAM_RESOURCE] = LLVMPointerType(LLVMVectorType(i8, 32), CONST_ADDR_SPACE);
  1254.  
  1255.         if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
  1256.                 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_SAMPLER];
  1257.                 params[SI_PARAM_START_INSTANCE] = i32;
  1258.                 params[SI_PARAM_VERTEX_ID] = i32;
  1259.                 params[SI_PARAM_DUMMY_0] = i32;
  1260.                 params[SI_PARAM_DUMMY_1] = i32;
  1261.                 params[SI_PARAM_INSTANCE_ID] = i32;
  1262.                 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 9);
  1263.  
  1264.         } else {
  1265.                 params[SI_PARAM_PRIM_MASK] = i32;
  1266.                 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
  1267.                 params[SI_PARAM_PERSP_CENTER] = v2i32;
  1268.                 params[SI_PARAM_PERSP_CENTROID] = v2i32;
  1269.                 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
  1270.                 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
  1271.                 params[SI_PARAM_LINEAR_CENTER] = v2i32;
  1272.                 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
  1273.                 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
  1274.                 params[SI_PARAM_POS_X_FLOAT] = f32;
  1275.                 params[SI_PARAM_POS_Y_FLOAT] = f32;
  1276.                 params[SI_PARAM_POS_Z_FLOAT] = f32;
  1277.                 params[SI_PARAM_POS_W_FLOAT] = f32;
  1278.                 params[SI_PARAM_FRONT_FACE] = f32;
  1279.                 params[SI_PARAM_ANCILLARY] = f32;
  1280.                 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
  1281.                 params[SI_PARAM_POS_FIXED_PT] = f32;
  1282.                 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 20);
  1283.         }
  1284.  
  1285.         radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
  1286.         for (i = SI_PARAM_CONST; i <= SI_PARAM_VERTEX_BUFFER; ++i) {
  1287.                 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
  1288.                 LLVMAddAttribute(P, LLVMInRegAttribute);
  1289.         }
  1290.  
  1291.         if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
  1292.                 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
  1293.                                               SI_PARAM_START_INSTANCE);
  1294.                 LLVMAddAttribute(P, LLVMInRegAttribute);
  1295.         }
  1296.  
  1297. #if HAVE_LLVM >= 0x0304
  1298.         if (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
  1299.             bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0)
  1300.                 si_shader_ctx->ddxy_lds =
  1301.                         LLVMAddGlobalInAddressSpace(gallivm->module,
  1302.                                                     LLVMArrayType(i32, 64),
  1303.                                                     "ddxy_lds",
  1304.                                                     LOCAL_ADDR_SPACE);
  1305. #endif
  1306. }
  1307.  
  1308. static void preload_constants(struct si_shader_context *si_shader_ctx)
  1309. {
  1310.         struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
  1311.         struct gallivm_state * gallivm = bld_base->base.gallivm;
  1312.         const struct tgsi_shader_info * info = bld_base->info;
  1313.  
  1314.         unsigned i, num_const = info->file_max[TGSI_FILE_CONSTANT] + 1;
  1315.  
  1316.         LLVMValueRef ptr;
  1317.  
  1318.         if (num_const == 0)
  1319.                 return;
  1320.  
  1321.         /* Allocate space for the constant values */
  1322.         si_shader_ctx->constants = CALLOC(num_const * 4, sizeof(LLVMValueRef));
  1323.  
  1324.         /* Load the resource descriptor */
  1325.         ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
  1326.         si_shader_ctx->const_resource = build_indexed_load(si_shader_ctx, ptr, bld_base->uint_bld.zero);
  1327.  
  1328.         /* Load the constants, we rely on the code sinking to do the rest */
  1329.         for (i = 0; i < num_const * 4; ++i) {
  1330.                 LLVMValueRef args[2] = {
  1331.                         si_shader_ctx->const_resource,
  1332.                         lp_build_const_int32(gallivm, i * 4)
  1333.                 };
  1334.                 si_shader_ctx->constants[i] = build_intrinsic(gallivm->builder, "llvm.SI.load.const",
  1335.                         bld_base->base.elem_type, args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
  1336.         }
  1337. }
  1338.  
  1339. static void preload_samplers(struct si_shader_context *si_shader_ctx)
  1340. {
  1341.         struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
  1342.         struct gallivm_state * gallivm = bld_base->base.gallivm;
  1343.         const struct tgsi_shader_info * info = bld_base->info;
  1344.  
  1345.         unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
  1346.  
  1347.         LLVMValueRef res_ptr, samp_ptr;
  1348.         LLVMValueRef offset;
  1349.  
  1350.         if (num_samplers == 0)
  1351.                 return;
  1352.  
  1353.         /* Allocate space for the values */
  1354.         si_shader_ctx->resources = CALLOC(num_samplers, sizeof(LLVMValueRef));
  1355.         si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
  1356.  
  1357.         res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
  1358.         samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
  1359.  
  1360.         /* Load the resources and samplers, we rely on the code sinking to do the rest */
  1361.         for (i = 0; i < num_samplers; ++i) {
  1362.  
  1363.                 /* Resource */
  1364.                 offset = lp_build_const_int32(gallivm, i);
  1365.                 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
  1366.  
  1367.                 /* Sampler */
  1368.                 offset = lp_build_const_int32(gallivm, i);
  1369.                 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
  1370.         }
  1371. }
  1372.  
  1373. int si_compile_llvm(struct r600_context *rctx, struct si_pipe_shader *shader,
  1374.                                                         LLVMModuleRef mod)
  1375. {
  1376.         unsigned i;
  1377.         uint32_t *ptr;
  1378.         bool dump;
  1379.         struct radeon_llvm_binary binary;
  1380.  
  1381.         dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
  1382.  
  1383.         memset(&binary, 0, sizeof(binary));
  1384.         radeon_llvm_compile(mod, &binary,
  1385.                 r600_get_llvm_processor_name(rctx->screen->family), dump);
  1386.         if (dump) {
  1387.                 fprintf(stderr, "SI CODE:\n");
  1388.                 for (i = 0; i < binary.code_size; i+=4 ) {
  1389.                         fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
  1390.                                 binary.code[i + 2], binary.code[i + 1],
  1391.                                 binary.code[i]);
  1392.                 }
  1393.         }
  1394.  
  1395.         /* XXX: We may be able to emit some of these values directly rather than
  1396.          * extracting fields to be emitted later.
  1397.          */
  1398.         for (i = 0; i < binary.config_size; i+= 8) {
  1399.                 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
  1400.                 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
  1401.                 switch (reg) {
  1402.                 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
  1403.                 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
  1404.                 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
  1405.                 case R_00B848_COMPUTE_PGM_RSRC1:
  1406.                         shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
  1407.                         shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
  1408.                         break;
  1409.                 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
  1410.                         shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
  1411.                         break;
  1412.                 case R_00B84C_COMPUTE_PGM_RSRC2:
  1413.                         shader->lds_size = G_00B84C_LDS_SIZE(value);
  1414.                         break;
  1415.                 case R_0286CC_SPI_PS_INPUT_ENA:
  1416.                         shader->spi_ps_input_ena = value;
  1417.                         break;
  1418.                 default:
  1419.                         fprintf(stderr, "Warning: Compiler emitted unknown "
  1420.                                 "config register: 0x%x\n", reg);
  1421.                         break;
  1422.                 }
  1423.         }
  1424.  
  1425.         /* copy new shader */
  1426.         si_resource_reference(&shader->bo, NULL);
  1427.         shader->bo = si_resource_create_custom(rctx->context.screen, PIPE_USAGE_IMMUTABLE,
  1428.                                                binary.code_size);
  1429.         if (shader->bo == NULL) {
  1430.                 return -ENOMEM;
  1431.         }
  1432.  
  1433.         ptr = (uint32_t*)rctx->ws->buffer_map(shader->bo->cs_buf, rctx->cs, PIPE_TRANSFER_WRITE);
  1434.         if (0 /*R600_BIG_ENDIAN*/) {
  1435.                 for (i = 0; i < binary.code_size / 4; ++i) {
  1436.                         ptr[i] = util_bswap32(*(uint32_t*)(binary.code + i*4));
  1437.                 }
  1438.         } else {
  1439.                 memcpy(ptr, binary.code, binary.code_size);
  1440.         }
  1441.         rctx->ws->buffer_unmap(shader->bo->cs_buf);
  1442.  
  1443.         free(binary.code);
  1444.         free(binary.config);
  1445.  
  1446.         return 0;
  1447. }
  1448.  
  1449. int si_pipe_shader_create(
  1450.         struct pipe_context *ctx,
  1451.         struct si_pipe_shader *shader)
  1452. {
  1453.         struct r600_context *rctx = (struct r600_context*)ctx;
  1454.         struct si_pipe_shader_selector *sel = shader->selector;
  1455.         struct si_shader_context si_shader_ctx;
  1456.         struct tgsi_shader_info shader_info;
  1457.         struct lp_build_tgsi_context * bld_base;
  1458.         LLVMModuleRef mod;
  1459.         bool dump;
  1460.         int r = 0;
  1461.  
  1462.         dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
  1463.  
  1464.         assert(shader->shader.noutput == 0);
  1465.         assert(shader->shader.ninterp == 0);
  1466.         assert(shader->shader.ninput == 0);
  1467.  
  1468.         memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
  1469.         radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
  1470.         bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
  1471.  
  1472.         tgsi_scan_shader(sel->tokens, &shader_info);
  1473.  
  1474.         shader->shader.uses_kill = shader_info.uses_kill;
  1475.         shader->shader.uses_instanceid = shader_info.uses_instanceid;
  1476.         bld_base->info = &shader_info;
  1477.         bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
  1478.         bld_base->emit_epilogue = si_llvm_emit_epilogue;
  1479.  
  1480.         bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
  1481.         bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
  1482. #if HAVE_LLVM >= 0x0304
  1483.         bld_base->op_actions[TGSI_OPCODE_TXD] = txd_action;
  1484. #endif
  1485.         bld_base->op_actions[TGSI_OPCODE_TXF] = txf_action;
  1486.         bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
  1487.         bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
  1488.         bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
  1489.  
  1490. #if HAVE_LLVM >= 0x0304
  1491.         bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
  1492.         bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
  1493. #endif
  1494.  
  1495.         si_shader_ctx.radeon_bld.load_input = declare_input;
  1496.         si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
  1497.         si_shader_ctx.tokens = sel->tokens;
  1498.         tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
  1499.         si_shader_ctx.shader = shader;
  1500.         si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
  1501.  
  1502.         create_meta_data(&si_shader_ctx);
  1503.         create_function(&si_shader_ctx);
  1504.         preload_constants(&si_shader_ctx);
  1505.         preload_samplers(&si_shader_ctx);
  1506.  
  1507.         shader->shader.nr_cbufs = rctx->framebuffer.nr_cbufs;
  1508.  
  1509.         /* Dump TGSI code before doing TGSI->LLVM conversion in case the
  1510.          * conversion fails. */
  1511.         if (dump) {
  1512.                 tgsi_dump(sel->tokens, 0);
  1513.         }
  1514.  
  1515.         if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
  1516.                 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
  1517.                 FREE(si_shader_ctx.constants);
  1518.                 FREE(si_shader_ctx.resources);
  1519.                 FREE(si_shader_ctx.samplers);
  1520.                 return -EINVAL;
  1521.         }
  1522.  
  1523.         radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
  1524.  
  1525.         mod = bld_base->base.gallivm->module;
  1526.         r = si_compile_llvm(rctx, shader, mod);
  1527.  
  1528.         radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
  1529.         tgsi_parse_free(&si_shader_ctx.parse);
  1530.  
  1531.         FREE(si_shader_ctx.constants);
  1532.         FREE(si_shader_ctx.resources);
  1533.         FREE(si_shader_ctx.samplers);
  1534.  
  1535.         return r;
  1536. }
  1537.  
  1538. void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
  1539. {
  1540.         si_resource_reference(&shader->bo, NULL);
  1541. }
  1542.