Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1 | |
Christian König | ce40e47 | 2012-08-02 12:14:59 +0200 | [diff] [blame] | 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 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 30 | #include "gallivm/lp_bld_tgsi_action.h" |
| 31 | #include "gallivm/lp_bld_const.h" |
Michel Dänzer | c2bae6b | 2012-08-02 17:19:22 +0200 | [diff] [blame] | 32 | #include "gallivm/lp_bld_gather.h" |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 33 | #include "gallivm/lp_bld_intr.h" |
Michel Dänzer | 7708a86 | 2012-11-02 15:57:30 +0100 | [diff] [blame] | 34 | #include "gallivm/lp_bld_logic.h" |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 35 | #include "gallivm/lp_bld_tgsi.h" |
Christian König | 5e616cf | 2013-03-07 11:58:56 +0100 | [diff] [blame] | 36 | #include "gallivm/lp_bld_arit.h" |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 37 | #include "radeon_llvm.h" |
Tom Stellard | 509ddb0 | 2012-04-16 17:48:44 -0400 | [diff] [blame] | 38 | #include "radeon_llvm_emit.h" |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 39 | #include "util/u_memory.h" |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 40 | #include "tgsi/tgsi_info.h" |
| 41 | #include "tgsi/tgsi_parse.h" |
| 42 | #include "tgsi/tgsi_scan.h" |
Michel Dänzer | beaa5eb | 2013-05-24 13:23:26 +0100 | [diff] [blame] | 43 | #include "tgsi/tgsi_util.h" |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 44 | #include "tgsi/tgsi_dump.h" |
| 45 | |
| 46 | #include "radeonsi_pipe.h" |
| 47 | #include "radeonsi_shader.h" |
Christian König | f67fae0 | 2012-07-17 23:43:00 +0200 | [diff] [blame] | 48 | #include "si_state.h" |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 49 | #include "sid.h" |
| 50 | |
| 51 | #include <assert.h> |
| 52 | #include <errno.h> |
| 53 | #include <stdio.h> |
| 54 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 55 | struct si_shader_context |
| 56 | { |
| 57 | struct radeon_llvm_context radeon_bld; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 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. */ |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 62 | LLVMValueRef const_md; |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 63 | LLVMValueRef const_resource; |
Michel Dänzer | a06ee5a | 2013-06-19 18:14:01 +0200 | [diff] [blame] | 64 | #if HAVE_LLVM >= 0x0304 |
| 65 | LLVMValueRef ddxy_lds; |
| 66 | #endif |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 67 | LLVMValueRef *constants; |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 68 | LLVMValueRef *resources; |
| 69 | LLVMValueRef *samplers; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 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 |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 87 | #define CONST_ADDR_SPACE 2 |
Michel Dänzer | a06ee5a | 2013-06-19 18:14:01 +0200 | [diff] [blame] | 88 | #define LOCAL_ADDR_SPACE 3 |
Tom Stellard | 89ece08 | 2012-05-29 11:36:29 -0400 | [diff] [blame] | 89 | #define USER_SGPR_ADDR_SPACE 8 |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 90 | |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 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( |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 104 | struct si_shader_context * si_shader_ctx, |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 105 | LLVMValueRef base_ptr, |
| 106 | LLVMValueRef offset) |
| 107 | { |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 108 | struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base; |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 109 | |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 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; |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 116 | } |
| 117 | |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 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 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 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 | { |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 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 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 145 | LLVMValueRef t_list_ptr; |
| 146 | LLVMValueRef t_offset; |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 147 | LLVMValueRef t_list; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 148 | LLVMValueRef attribute_offset; |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 149 | LLVMValueRef buffer_index; |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 150 | LLVMValueRef args[3]; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 151 | LLVMTypeRef vec4_type; |
| 152 | LLVMValueRef input; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 153 | |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 154 | /* Load the T list */ |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 155 | t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 156 | |
Christian König | b15e3ae | 2012-07-25 11:22:59 +0200 | [diff] [blame] | 157 | t_offset = lp_build_const_int32(base->gallivm, input_index); |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 158 | |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 159 | t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset); |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 160 | |
| 161 | /* Build the attribute offset */ |
Christian König | b15e3ae | 2012-07-25 11:22:59 +0200 | [diff] [blame] | 162 | attribute_offset = lp_build_const_int32(base->gallivm, 0); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 163 | |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 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 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 173 | |
| 174 | vec4_type = LLVMVectorType(base->elem_type, 4); |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 175 | args[0] = t_list; |
| 176 | args[1] = attribute_offset; |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 177 | args[2] = buffer_index; |
Christian König | 44e3224 | 2013-03-20 12:10:35 +0100 | [diff] [blame] | 178 | input = build_intrinsic(base->gallivm->builder, |
| 179 | "llvm.SI.vs.load.input", vec4_type, args, 3, |
| 180 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 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 | { |
Michel Dänzer | c3db19e | 2012-09-27 20:01:33 +0200 | [diff] [blame] | 198 | struct si_shader *shader = &si_shader_ctx->shader->shader; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 199 | struct lp_build_context * base = |
| 200 | &si_shader_ctx->radeon_bld.soa.bld_base.base; |
| 201 | struct gallivm_state * gallivm = base->gallivm; |
Tom Stellard | 0fb1e68 | 2012-09-06 16:18:11 -0400 | [diff] [blame] | 202 | LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context); |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 203 | LLVMValueRef main_fn = si_shader_ctx->radeon_bld.main_fn; |
| 204 | |
| 205 | LLVMValueRef interp_param; |
| 206 | const char * intr_name; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 207 | |
| 208 | /* This value is: |
| 209 | * [15:0] NewPrimMask (Bit mask for each quad. It is set it the |
| 210 | * quad begins a new primitive. Bit 0 always needs |
| 211 | * to be unset) |
| 212 | * [32:16] ParamOffset |
| 213 | * |
| 214 | */ |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 215 | LLVMValueRef params = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_PRIM_MASK); |
Michel Dänzer | c3db19e | 2012-09-27 20:01:33 +0200 | [diff] [blame] | 216 | LLVMValueRef attr_number; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 217 | |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 218 | unsigned chan; |
| 219 | |
Tom Stellard | 0fb1e68 | 2012-09-06 16:18:11 -0400 | [diff] [blame] | 220 | if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) { |
| 221 | for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) { |
Tom Stellard | 0fb1e68 | 2012-09-06 16:18:11 -0400 | [diff] [blame] | 222 | unsigned soa_index = |
| 223 | radeon_llvm_reg_index_soa(input_index, chan); |
Tom Stellard | 0fb1e68 | 2012-09-06 16:18:11 -0400 | [diff] [blame] | 224 | si_shader_ctx->radeon_bld.inputs[soa_index] = |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 225 | LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan); |
Michel Dänzer | 954bc4a | 2013-02-13 15:57:23 +0100 | [diff] [blame] | 226 | |
| 227 | if (chan == 3) |
| 228 | /* RCP for fragcoord.w */ |
| 229 | si_shader_ctx->radeon_bld.inputs[soa_index] = |
| 230 | LLVMBuildFDiv(gallivm->builder, |
| 231 | lp_build_const_float(gallivm, 1.0f), |
| 232 | si_shader_ctx->radeon_bld.inputs[soa_index], |
| 233 | ""); |
Tom Stellard | 0fb1e68 | 2012-09-06 16:18:11 -0400 | [diff] [blame] | 234 | } |
| 235 | return; |
| 236 | } |
| 237 | |
Michel Dänzer | 97078b1 | 2012-09-25 12:41:31 +0200 | [diff] [blame] | 238 | if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) { |
| 239 | LLVMValueRef face, is_face_positive; |
| 240 | |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 241 | face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE); |
| 242 | |
Michel Dänzer | 97078b1 | 2012-09-25 12:41:31 +0200 | [diff] [blame] | 243 | is_face_positive = LLVMBuildFCmp(gallivm->builder, |
| 244 | LLVMRealUGT, face, |
| 245 | lp_build_const_float(gallivm, 0.0f), |
| 246 | ""); |
| 247 | |
| 248 | si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] = |
| 249 | LLVMBuildSelect(gallivm->builder, |
| 250 | is_face_positive, |
| 251 | lp_build_const_float(gallivm, 1.0f), |
| 252 | lp_build_const_float(gallivm, 0.0f), |
| 253 | ""); |
| 254 | si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] = |
| 255 | si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] = |
| 256 | lp_build_const_float(gallivm, 0.0f); |
| 257 | si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] = |
| 258 | lp_build_const_float(gallivm, 1.0f); |
| 259 | |
| 260 | return; |
| 261 | } |
| 262 | |
Michel Dänzer | c3db19e | 2012-09-27 20:01:33 +0200 | [diff] [blame] | 263 | shader->input[input_index].param_offset = shader->ninterp++; |
| 264 | attr_number = lp_build_const_int32(gallivm, |
| 265 | shader->input[input_index].param_offset); |
| 266 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 267 | /* XXX: Handle all possible interpolation modes */ |
Francisco Jerez | 1279923 | 2012-04-30 18:27:52 +0200 | [diff] [blame] | 268 | switch (decl->Interp.Interpolate) { |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 269 | case TGSI_INTERPOLATE_COLOR: |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 270 | if (si_shader_ctx->shader->key.ps.flatshade) { |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 271 | interp_param = 0; |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame] | 272 | } else { |
| 273 | if (decl->Interp.Centroid) |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 274 | interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID); |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame] | 275 | else |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 276 | interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER); |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame] | 277 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 278 | break; |
| 279 | case TGSI_INTERPOLATE_CONSTANT: |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 280 | interp_param = 0; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 281 | break; |
| 282 | case TGSI_INTERPOLATE_LINEAR: |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame] | 283 | if (decl->Interp.Centroid) |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 284 | interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID); |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame] | 285 | else |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 286 | interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER); |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame] | 287 | break; |
| 288 | case TGSI_INTERPOLATE_PERSPECTIVE: |
| 289 | if (decl->Interp.Centroid) |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 290 | interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID); |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame] | 291 | else |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 292 | interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 293 | break; |
| 294 | default: |
| 295 | fprintf(stderr, "Warning: Unhandled interpolation mode.\n"); |
| 296 | return; |
| 297 | } |
| 298 | |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 299 | intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant"; |
| 300 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 301 | /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */ |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 302 | if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR && |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 303 | si_shader_ctx->shader->key.ps.color_two_side) { |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 304 | LLVMValueRef args[4]; |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 305 | LLVMValueRef face, is_face_positive; |
| 306 | LLVMValueRef back_attr_number = |
| 307 | lp_build_const_int32(gallivm, |
| 308 | shader->input[input_index].param_offset + 1); |
| 309 | |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 310 | face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE); |
| 311 | |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 312 | is_face_positive = LLVMBuildFCmp(gallivm->builder, |
| 313 | LLVMRealUGT, face, |
| 314 | lp_build_const_float(gallivm, 0.0f), |
| 315 | ""); |
| 316 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 317 | args[2] = params; |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 318 | args[3] = interp_param; |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 319 | for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) { |
| 320 | LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan); |
| 321 | unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan); |
| 322 | LLVMValueRef front, back; |
| 323 | |
| 324 | args[0] = llvm_chan; |
| 325 | args[1] = attr_number; |
| 326 | front = build_intrinsic(base->gallivm->builder, intr_name, |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 327 | input_type, args, args[3] ? 4 : 3, |
Christian König | 44e3224 | 2013-03-20 12:10:35 +0100 | [diff] [blame] | 328 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 329 | |
| 330 | args[1] = back_attr_number; |
| 331 | back = build_intrinsic(base->gallivm->builder, intr_name, |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 332 | input_type, args, args[3] ? 4 : 3, |
Christian König | 44e3224 | 2013-03-20 12:10:35 +0100 | [diff] [blame] | 333 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 334 | |
| 335 | si_shader_ctx->radeon_bld.inputs[soa_index] = |
| 336 | LLVMBuildSelect(gallivm->builder, |
| 337 | is_face_positive, |
| 338 | front, |
| 339 | back, |
| 340 | ""); |
| 341 | } |
| 342 | |
| 343 | shader->ninterp++; |
| 344 | } else { |
| 345 | for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) { |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 346 | LLVMValueRef args[4]; |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 347 | LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan); |
| 348 | unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan); |
| 349 | args[0] = llvm_chan; |
| 350 | args[1] = attr_number; |
| 351 | args[2] = params; |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 352 | args[3] = interp_param; |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 353 | si_shader_ctx->radeon_bld.inputs[soa_index] = |
| 354 | build_intrinsic(base->gallivm->builder, intr_name, |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 355 | input_type, args, args[3] ? 4 : 3, |
Christian König | 44e3224 | 2013-03-20 12:10:35 +0100 | [diff] [blame] | 356 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 357 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
| 361 | static void declare_input( |
| 362 | struct radeon_llvm_context * radeon_bld, |
| 363 | unsigned input_index, |
| 364 | const struct tgsi_full_declaration *decl) |
| 365 | { |
| 366 | struct si_shader_context * si_shader_ctx = |
| 367 | si_shader_context(&radeon_bld->soa.bld_base); |
| 368 | if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) { |
| 369 | declare_input_vs(si_shader_ctx, input_index, decl); |
| 370 | } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) { |
| 371 | declare_input_fs(si_shader_ctx, input_index, decl); |
| 372 | } else { |
| 373 | fprintf(stderr, "Warning: Unsupported shader type,\n"); |
| 374 | } |
| 375 | } |
| 376 | |
Christian König | e4ed587 | 2013-03-21 18:02:52 +0100 | [diff] [blame] | 377 | static void declare_system_value( |
| 378 | struct radeon_llvm_context * radeon_bld, |
| 379 | unsigned index, |
| 380 | const struct tgsi_full_declaration *decl) |
| 381 | { |
Christian König | cf9b31f | 2013-03-21 18:30:23 +0100 | [diff] [blame] | 382 | |
Christian König | e4ed587 | 2013-03-21 18:02:52 +0100 | [diff] [blame] | 383 | LLVMValueRef value = 0; |
| 384 | |
| 385 | switch (decl->Semantic.Name) { |
| 386 | case TGSI_SEMANTIC_INSTANCEID: |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 387 | value = get_instance_index(radeon_bld, 1); |
Christian König | e4ed587 | 2013-03-21 18:02:52 +0100 | [diff] [blame] | 388 | break; |
| 389 | |
| 390 | case TGSI_SEMANTIC_VERTEXID: |
| 391 | value = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_VERTEX_ID); |
| 392 | break; |
| 393 | |
| 394 | default: |
| 395 | assert(!"unknown system value"); |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | radeon_bld->system_values[index] = value; |
| 400 | } |
| 401 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 402 | static LLVMValueRef fetch_constant( |
| 403 | struct lp_build_tgsi_context * bld_base, |
| 404 | const struct tgsi_full_src_register *reg, |
| 405 | enum tgsi_opcode_type type, |
| 406 | unsigned swizzle) |
| 407 | { |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 408 | struct si_shader_context *si_shader_ctx = si_shader_context(bld_base); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 409 | struct lp_build_context * base = &bld_base->base; |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 410 | const struct tgsi_ind_register *ireg = ®->Indirect; |
| 411 | unsigned idx; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 412 | |
Christian König | f5298b0 | 2013-02-28 14:50:07 +0100 | [diff] [blame] | 413 | LLVMValueRef args[2]; |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 414 | LLVMValueRef addr; |
Christian König | f5298b0 | 2013-02-28 14:50:07 +0100 | [diff] [blame] | 415 | LLVMValueRef result; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 416 | |
Christian König | 8514f5a | 2013-02-04 17:46:42 +0100 | [diff] [blame] | 417 | if (swizzle == LP_CHAN_ALL) { |
| 418 | unsigned chan; |
| 419 | LLVMValueRef values[4]; |
| 420 | for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) |
| 421 | values[chan] = fetch_constant(bld_base, reg, type, chan); |
| 422 | |
| 423 | return lp_build_gather_values(bld_base->base.gallivm, values, 4); |
| 424 | } |
| 425 | |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 426 | idx = reg->Register.Index * 4 + swizzle; |
| 427 | if (!reg->Register.Indirect) |
| 428 | return bitcast(bld_base, type, si_shader_ctx->constants[idx]); |
Christian König | f5298b0 | 2013-02-28 14:50:07 +0100 | [diff] [blame] | 429 | |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 430 | args[0] = si_shader_ctx->const_resource; |
| 431 | args[1] = lp_build_const_int32(base->gallivm, idx * 4); |
| 432 | addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle]; |
| 433 | addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg"); |
| 434 | addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16); |
| 435 | args[1] = lp_build_add(&bld_base->uint_bld, addr, args[1]); |
Christian König | e7723b5 | 2012-08-24 12:55:34 +0200 | [diff] [blame] | 436 | |
Christian König | f5298b0 | 2013-02-28 14:50:07 +0100 | [diff] [blame] | 437 | result = build_intrinsic(base->gallivm->builder, "llvm.SI.load.const", base->elem_type, |
Christian König | 44e3224 | 2013-03-20 12:10:35 +0100 | [diff] [blame] | 438 | args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 439 | |
Christian König | f5298b0 | 2013-02-28 14:50:07 +0100 | [diff] [blame] | 440 | return bitcast(bld_base, type, result); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 441 | } |
| 442 | |
Michel Dänzer | 26c7139 | 2012-08-24 12:03:11 +0200 | [diff] [blame] | 443 | /* Initialize arguments for the shader export intrinsic */ |
| 444 | static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base, |
| 445 | struct tgsi_full_declaration *d, |
| 446 | unsigned index, |
| 447 | unsigned target, |
| 448 | LLVMValueRef *args) |
| 449 | { |
| 450 | struct si_shader_context *si_shader_ctx = si_shader_context(bld_base); |
| 451 | struct lp_build_context *uint = |
| 452 | &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld; |
| 453 | struct lp_build_context *base = &bld_base->base; |
| 454 | unsigned compressed = 0; |
| 455 | unsigned chan; |
| 456 | |
Michel Dänzer | f402acd | 2012-08-22 18:15:36 +0200 | [diff] [blame] | 457 | if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) { |
| 458 | int cbuf = target - V_008DFC_SQ_EXP_MRT; |
| 459 | |
| 460 | if (cbuf >= 0 && cbuf < 8) { |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 461 | compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1; |
Michel Dänzer | 1ace200 | 2012-12-21 15:39:26 +0100 | [diff] [blame] | 462 | |
| 463 | if (compressed) |
| 464 | si_shader_ctx->shader->spi_shader_col_format |= |
| 465 | V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf); |
| 466 | else |
| 467 | si_shader_ctx->shader->spi_shader_col_format |= |
| 468 | V_028714_SPI_SHADER_32_ABGR << (4 * cbuf); |
Michel Dänzer | e369f40 | 2013-04-30 16:34:10 +0200 | [diff] [blame] | 469 | |
| 470 | si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf); |
Michel Dänzer | f402acd | 2012-08-22 18:15:36 +0200 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
| 474 | if (compressed) { |
| 475 | /* Pixel shader needs to pack output values before export */ |
| 476 | for (chan = 0; chan < 2; chan++ ) { |
| 477 | LLVMValueRef *out_ptr = |
| 478 | si_shader_ctx->radeon_bld.soa.outputs[index]; |
| 479 | args[0] = LLVMBuildLoad(base->gallivm->builder, |
| 480 | out_ptr[2 * chan], ""); |
| 481 | args[1] = LLVMBuildLoad(base->gallivm->builder, |
| 482 | out_ptr[2 * chan + 1], ""); |
| 483 | args[chan + 5] = |
| 484 | build_intrinsic(base->gallivm->builder, |
| 485 | "llvm.SI.packf16", |
| 486 | LLVMInt32TypeInContext(base->gallivm->context), |
| 487 | args, 2, |
Christian König | e4188ee | 2013-02-27 22:39:26 +0100 | [diff] [blame] | 488 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
Michel Dänzer | 8b6aec6 | 2012-11-27 19:53:58 +0100 | [diff] [blame] | 489 | args[chan + 7] = args[chan + 5] = |
| 490 | LLVMBuildBitCast(base->gallivm->builder, |
| 491 | args[chan + 5], |
| 492 | LLVMFloatTypeInContext(base->gallivm->context), |
| 493 | ""); |
Michel Dänzer | f402acd | 2012-08-22 18:15:36 +0200 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | /* Set COMPR flag */ |
| 497 | args[4] = uint->one; |
| 498 | } else { |
| 499 | for (chan = 0; chan < 4; chan++ ) { |
| 500 | LLVMValueRef out_ptr = |
| 501 | si_shader_ctx->radeon_bld.soa.outputs[index][chan]; |
| 502 | /* +5 because the first output value will be |
| 503 | * the 6th argument to the intrinsic. */ |
| 504 | args[chan + 5] = LLVMBuildLoad(base->gallivm->builder, |
| 505 | out_ptr, ""); |
| 506 | } |
| 507 | |
| 508 | /* Clear COMPR flag */ |
| 509 | args[4] = uint->zero; |
Michel Dänzer | 26c7139 | 2012-08-24 12:03:11 +0200 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /* XXX: This controls which components of the output |
| 513 | * registers actually get exported. (e.g bit 0 means export |
| 514 | * X component, bit 1 means export Y component, etc.) I'm |
| 515 | * hard coding this to 0xf for now. In the future, we might |
| 516 | * want to do something else. */ |
| 517 | args[0] = lp_build_const_int32(base->gallivm, 0xf); |
| 518 | |
| 519 | /* Specify whether the EXEC mask represents the valid mask */ |
| 520 | args[1] = uint->zero; |
| 521 | |
| 522 | /* Specify whether this is the last export */ |
| 523 | args[2] = uint->zero; |
| 524 | |
| 525 | /* Specify the target we are exporting */ |
| 526 | args[3] = lp_build_const_int32(base->gallivm, target); |
| 527 | |
Michel Dänzer | 26c7139 | 2012-08-24 12:03:11 +0200 | [diff] [blame] | 528 | /* XXX: We probably need to keep track of the output |
| 529 | * values, so we know what we are passing to the next |
| 530 | * stage. */ |
| 531 | } |
| 532 | |
Michel Dänzer | 7708a86 | 2012-11-02 15:57:30 +0100 | [diff] [blame] | 533 | static void si_alpha_test(struct lp_build_tgsi_context *bld_base, |
| 534 | unsigned index) |
| 535 | { |
| 536 | struct si_shader_context *si_shader_ctx = si_shader_context(bld_base); |
| 537 | struct gallivm_state *gallivm = bld_base->base.gallivm; |
| 538 | |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 539 | if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) { |
Michel Dänzer | 7708a86 | 2012-11-02 15:57:30 +0100 | [diff] [blame] | 540 | LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][3]; |
| 541 | LLVMValueRef alpha_pass = |
| 542 | lp_build_cmp(&bld_base->base, |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 543 | si_shader_ctx->shader->key.ps.alpha_func, |
Michel Dänzer | 7708a86 | 2012-11-02 15:57:30 +0100 | [diff] [blame] | 544 | LLVMBuildLoad(gallivm->builder, out_ptr, ""), |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 545 | lp_build_const_float(gallivm, si_shader_ctx->shader->key.ps.alpha_ref)); |
Michel Dänzer | 7708a86 | 2012-11-02 15:57:30 +0100 | [diff] [blame] | 546 | LLVMValueRef arg = |
| 547 | lp_build_select(&bld_base->base, |
| 548 | alpha_pass, |
| 549 | lp_build_const_float(gallivm, 1.0f), |
| 550 | lp_build_const_float(gallivm, -1.0f)); |
| 551 | |
| 552 | build_intrinsic(gallivm->builder, |
| 553 | "llvm.AMDGPU.kill", |
| 554 | LLVMVoidTypeInContext(gallivm->context), |
| 555 | &arg, 1, 0); |
| 556 | } else { |
| 557 | build_intrinsic(gallivm->builder, |
| 558 | "llvm.AMDGPU.kilp", |
| 559 | LLVMVoidTypeInContext(gallivm->context), |
| 560 | NULL, 0, 0); |
| 561 | } |
| 562 | } |
| 563 | |
Marek Olšák | 6d4755a | 2013-07-30 22:29:29 +0200 | [diff] [blame] | 564 | static void si_alpha_to_one(struct lp_build_tgsi_context *bld_base, |
| 565 | unsigned index) |
| 566 | { |
| 567 | struct si_shader_context *si_shader_ctx = si_shader_context(bld_base); |
| 568 | |
| 569 | /* set alpha to one */ |
| 570 | LLVMBuildStore(bld_base->base.gallivm->builder, |
| 571 | bld_base->base.one, |
| 572 | si_shader_ctx->radeon_bld.soa.outputs[index][3]); |
| 573 | } |
| 574 | |
Michel Dänzer | e3befbc | 2013-05-15 18:09:50 +0200 | [diff] [blame] | 575 | static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base, |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 576 | LLVMValueRef (*pos)[9], unsigned index) |
Michel Dänzer | e3befbc | 2013-05-15 18:09:50 +0200 | [diff] [blame] | 577 | { |
| 578 | struct si_shader_context *si_shader_ctx = si_shader_context(bld_base); |
Michel Dänzer | 2f98dc2 | 2013-08-08 16:58:00 +0200 | [diff] [blame] | 579 | struct si_pipe_shader *shader = si_shader_ctx->shader; |
Michel Dänzer | e3befbc | 2013-05-15 18:09:50 +0200 | [diff] [blame] | 580 | struct lp_build_context *base = &bld_base->base; |
| 581 | struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld; |
Michel Dänzer | e3befbc | 2013-05-15 18:09:50 +0200 | [diff] [blame] | 582 | unsigned reg_index; |
| 583 | unsigned chan; |
| 584 | unsigned const_chan; |
| 585 | LLVMValueRef out_elts[4]; |
| 586 | LLVMValueRef base_elt; |
| 587 | LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST); |
| 588 | LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, uint->one); |
| 589 | |
| 590 | for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) { |
| 591 | LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][chan]; |
| 592 | out_elts[chan] = LLVMBuildLoad(base->gallivm->builder, out_ptr, ""); |
| 593 | } |
| 594 | |
| 595 | for (reg_index = 0; reg_index < 2; reg_index ++) { |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 596 | LLVMValueRef *args = pos[2 + reg_index]; |
| 597 | |
Michel Dänzer | 2f98dc2 | 2013-08-08 16:58:00 +0200 | [diff] [blame] | 598 | if (!(shader->key.vs.ucps_enabled & (1 << reg_index))) |
| 599 | continue; |
| 600 | |
| 601 | shader->shader.clip_dist_write |= 0xf << (4 * reg_index); |
| 602 | |
Michel Dänzer | e3befbc | 2013-05-15 18:09:50 +0200 | [diff] [blame] | 603 | args[5] = |
| 604 | args[6] = |
| 605 | args[7] = |
| 606 | args[8] = lp_build_const_float(base->gallivm, 0.0f); |
| 607 | |
| 608 | /* Compute dot products of position and user clip plane vectors */ |
| 609 | for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) { |
| 610 | for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) { |
| 611 | args[0] = const_resource; |
| 612 | args[1] = lp_build_const_int32(base->gallivm, |
| 613 | ((reg_index * 4 + chan) * 4 + |
| 614 | const_chan) * 4); |
| 615 | base_elt = build_intrinsic(base->gallivm->builder, |
| 616 | "llvm.SI.load.const", |
| 617 | base->elem_type, |
| 618 | args, 2, |
| 619 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
| 620 | args[5 + chan] = |
| 621 | lp_build_add(base, args[5 + chan], |
| 622 | lp_build_mul(base, base_elt, |
| 623 | out_elts[const_chan])); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | args[0] = lp_build_const_int32(base->gallivm, 0xf); |
| 628 | args[1] = uint->zero; |
| 629 | args[2] = uint->zero; |
| 630 | args[3] = lp_build_const_int32(base->gallivm, |
| 631 | V_008DFC_SQ_EXP_POS + 2 + reg_index); |
| 632 | args[4] = uint->zero; |
Michel Dänzer | e3befbc | 2013-05-15 18:09:50 +0200 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 636 | /* XXX: This is partially implemented for VS only at this point. It is not complete */ |
| 637 | static void si_llvm_emit_epilogue(struct lp_build_tgsi_context * bld_base) |
| 638 | { |
| 639 | struct si_shader_context * si_shader_ctx = si_shader_context(bld_base); |
Christian König | 3c09f11 | 2012-07-18 17:39:15 +0200 | [diff] [blame] | 640 | struct si_shader * shader = &si_shader_ctx->shader->shader; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 641 | struct lp_build_context * base = &bld_base->base; |
| 642 | struct lp_build_context * uint = |
| 643 | &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld; |
| 644 | struct tgsi_parse_context *parse = &si_shader_ctx->parse; |
Michel Dänzer | 1a616c1 | 2012-11-13 17:35:09 +0100 | [diff] [blame] | 645 | LLVMValueRef args[9]; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 646 | LLVMValueRef last_args[9] = { 0 }; |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 647 | LLVMValueRef pos_args[4][9] = { { 0 } }; |
Michel Dänzer | 0afeea5 | 2013-05-02 14:53:17 +0200 | [diff] [blame] | 648 | unsigned semantic_name; |
Christian König | 3508815 | 2012-08-01 22:35:24 +0200 | [diff] [blame] | 649 | unsigned color_count = 0; |
| 650 | unsigned param_count = 0; |
Michel Dänzer | 1a616c1 | 2012-11-13 17:35:09 +0100 | [diff] [blame] | 651 | int depth_index = -1, stencil_index = -1; |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 652 | int i; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 653 | |
| 654 | while (!tgsi_parse_end_of_tokens(parse)) { |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 655 | struct tgsi_full_declaration *d = |
| 656 | &parse->FullToken.FullDeclaration; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 657 | unsigned target; |
| 658 | unsigned index; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 659 | |
| 660 | tgsi_parse_token(parse); |
Michel Dänzer | c840270 | 2013-02-12 18:37:22 +0100 | [diff] [blame] | 661 | |
| 662 | if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY && |
| 663 | parse->FullToken.FullProperty.Property.PropertyName == |
| 664 | TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS) |
| 665 | shader->fs_write_all = TRUE; |
| 666 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 667 | if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION) |
| 668 | continue; |
| 669 | |
| 670 | switch (d->Declaration.File) { |
| 671 | case TGSI_FILE_INPUT: |
| 672 | i = shader->ninput++; |
Marek Olšák | 2eac0aa | 2013-05-14 19:37:17 +0200 | [diff] [blame] | 673 | assert(i < Elements(shader->input)); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 674 | shader->input[i].name = d->Semantic.Name; |
| 675 | shader->input[i].sid = d->Semantic.Index; |
Francisco Jerez | 1279923 | 2012-04-30 18:27:52 +0200 | [diff] [blame] | 676 | shader->input[i].interpolate = d->Interp.Interpolate; |
| 677 | shader->input[i].centroid = d->Interp.Centroid; |
Christian König | 3508815 | 2012-08-01 22:35:24 +0200 | [diff] [blame] | 678 | continue; |
| 679 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 680 | case TGSI_FILE_OUTPUT: |
| 681 | i = shader->noutput++; |
Marek Olšák | 2eac0aa | 2013-05-14 19:37:17 +0200 | [diff] [blame] | 682 | assert(i < Elements(shader->output)); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 683 | shader->output[i].name = d->Semantic.Name; |
| 684 | shader->output[i].sid = d->Semantic.Index; |
Francisco Jerez | 1279923 | 2012-04-30 18:27:52 +0200 | [diff] [blame] | 685 | shader->output[i].interpolate = d->Interp.Interpolate; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 686 | break; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 687 | |
Christian König | 3508815 | 2012-08-01 22:35:24 +0200 | [diff] [blame] | 688 | default: |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 689 | continue; |
Christian König | 3508815 | 2012-08-01 22:35:24 +0200 | [diff] [blame] | 690 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 691 | |
Michel Dänzer | 0afeea5 | 2013-05-02 14:53:17 +0200 | [diff] [blame] | 692 | semantic_name = d->Semantic.Name; |
| 693 | handle_semantic: |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 694 | for (index = d->Range.First; index <= d->Range.Last; index++) { |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 695 | /* Select the correct target */ |
Michel Dänzer | 0afeea5 | 2013-05-02 14:53:17 +0200 | [diff] [blame] | 696 | switch(semantic_name) { |
Tom Stellard | c3c323a | 2012-08-30 10:35:36 -0400 | [diff] [blame] | 697 | case TGSI_SEMANTIC_PSIZE: |
Michel Dänzer | 4730dea | 2013-05-03 17:59:34 +0200 | [diff] [blame] | 698 | shader->vs_out_misc_write = 1; |
| 699 | shader->vs_out_point_size = 1; |
| 700 | target = V_008DFC_SQ_EXP_POS + 1; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 701 | break; |
Michel Dänzer | 1a616c1 | 2012-11-13 17:35:09 +0100 | [diff] [blame] | 702 | case TGSI_SEMANTIC_POSITION: |
| 703 | if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) { |
| 704 | target = V_008DFC_SQ_EXP_POS; |
| 705 | break; |
| 706 | } else { |
| 707 | depth_index = index; |
| 708 | continue; |
| 709 | } |
| 710 | case TGSI_SEMANTIC_STENCIL: |
| 711 | stencil_index = index; |
| 712 | continue; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 713 | case TGSI_SEMANTIC_COLOR: |
| 714 | if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) { |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 715 | case TGSI_SEMANTIC_BCOLOR: |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 716 | target = V_008DFC_SQ_EXP_PARAM + param_count; |
Michel Dänzer | dd9d619 | 2012-05-18 15:01:10 +0200 | [diff] [blame] | 717 | shader->output[i].param_offset = param_count; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 718 | param_count++; |
| 719 | } else { |
| 720 | target = V_008DFC_SQ_EXP_MRT + color_count; |
Marek Olšák | 6d4755a | 2013-07-30 22:29:29 +0200 | [diff] [blame] | 721 | if (si_shader_ctx->shader->key.ps.alpha_to_one) { |
| 722 | si_alpha_to_one(bld_base, index); |
| 723 | } |
Michel Dänzer | 7708a86 | 2012-11-02 15:57:30 +0100 | [diff] [blame] | 724 | if (color_count == 0 && |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 725 | si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS) |
Michel Dänzer | 7708a86 | 2012-11-02 15:57:30 +0100 | [diff] [blame] | 726 | si_alpha_test(bld_base, index); |
| 727 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 728 | color_count++; |
| 729 | } |
| 730 | break; |
Michel Dänzer | 0afeea5 | 2013-05-02 14:53:17 +0200 | [diff] [blame] | 731 | case TGSI_SEMANTIC_CLIPDIST: |
Michel Dänzer | 2f98dc2 | 2013-08-08 16:58:00 +0200 | [diff] [blame] | 732 | if (!(si_shader_ctx->shader->key.vs.ucps_enabled & |
| 733 | (1 << d->Semantic.Index))) |
| 734 | continue; |
Michel Dänzer | 0afeea5 | 2013-05-02 14:53:17 +0200 | [diff] [blame] | 735 | shader->clip_dist_write |= |
| 736 | d->Declaration.UsageMask << (d->Semantic.Index << 2); |
| 737 | target = V_008DFC_SQ_EXP_POS + 2 + d->Semantic.Index; |
| 738 | break; |
Michel Dänzer | e3befbc | 2013-05-15 18:09:50 +0200 | [diff] [blame] | 739 | case TGSI_SEMANTIC_CLIPVERTEX: |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 740 | si_llvm_emit_clipvertex(bld_base, pos_args, index); |
Michel Dänzer | e3befbc | 2013-05-15 18:09:50 +0200 | [diff] [blame] | 741 | continue; |
Michel Dänzer | 30b3037 | 2012-09-06 17:53:04 +0200 | [diff] [blame] | 742 | case TGSI_SEMANTIC_FOG: |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 743 | case TGSI_SEMANTIC_GENERIC: |
| 744 | target = V_008DFC_SQ_EXP_PARAM + param_count; |
Michel Dänzer | dd9d619 | 2012-05-18 15:01:10 +0200 | [diff] [blame] | 745 | shader->output[i].param_offset = param_count; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 746 | param_count++; |
| 747 | break; |
| 748 | default: |
| 749 | target = 0; |
| 750 | fprintf(stderr, |
| 751 | "Warning: SI unhandled output type:%d\n", |
Michel Dänzer | 0afeea5 | 2013-05-02 14:53:17 +0200 | [diff] [blame] | 752 | semantic_name); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 753 | } |
| 754 | |
Michel Dänzer | 26c7139 | 2012-08-24 12:03:11 +0200 | [diff] [blame] | 755 | si_llvm_init_export_args(bld_base, d, index, target, args); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 756 | |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 757 | if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX && |
| 758 | target >= V_008DFC_SQ_EXP_POS && |
| 759 | target <= (V_008DFC_SQ_EXP_POS + 3)) { |
| 760 | memcpy(pos_args[target - V_008DFC_SQ_EXP_POS], |
| 761 | args, sizeof(args)); |
| 762 | } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT && |
| 763 | semantic_name == TGSI_SEMANTIC_COLOR) { |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 764 | if (last_args[0]) { |
| 765 | lp_build_intrinsic(base->gallivm->builder, |
| 766 | "llvm.SI.export", |
| 767 | LLVMVoidTypeInContext(base->gallivm->context), |
| 768 | last_args, 9); |
| 769 | } |
| 770 | |
| 771 | memcpy(last_args, args, sizeof(args)); |
| 772 | } else { |
| 773 | lp_build_intrinsic(base->gallivm->builder, |
| 774 | "llvm.SI.export", |
| 775 | LLVMVoidTypeInContext(base->gallivm->context), |
| 776 | args, 9); |
| 777 | } |
| 778 | |
| 779 | } |
Michel Dänzer | 0afeea5 | 2013-05-02 14:53:17 +0200 | [diff] [blame] | 780 | |
| 781 | if (semantic_name == TGSI_SEMANTIC_CLIPDIST) { |
| 782 | semantic_name = TGSI_SEMANTIC_GENERIC; |
| 783 | goto handle_semantic; |
| 784 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 785 | } |
| 786 | |
Michel Dänzer | 1a616c1 | 2012-11-13 17:35:09 +0100 | [diff] [blame] | 787 | if (depth_index >= 0 || stencil_index >= 0) { |
| 788 | LLVMValueRef out_ptr; |
| 789 | unsigned mask = 0; |
| 790 | |
| 791 | /* Specify the target we are exporting */ |
| 792 | args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ); |
| 793 | |
| 794 | if (depth_index >= 0) { |
| 795 | out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2]; |
| 796 | args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, ""); |
| 797 | mask |= 0x1; |
| 798 | |
| 799 | if (stencil_index < 0) { |
| 800 | args[6] = |
| 801 | args[7] = |
| 802 | args[8] = args[5]; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | if (stencil_index >= 0) { |
| 807 | out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1]; |
| 808 | args[7] = |
| 809 | args[8] = |
| 810 | args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, ""); |
| 811 | mask |= 0x2; |
| 812 | |
| 813 | if (depth_index < 0) |
| 814 | args[5] = args[6]; |
| 815 | } |
| 816 | |
| 817 | /* Specify which components to enable */ |
| 818 | args[0] = lp_build_const_int32(base->gallivm, mask); |
| 819 | |
| 820 | args[1] = |
| 821 | args[2] = |
| 822 | args[4] = uint->zero; |
| 823 | |
| 824 | if (last_args[0]) |
| 825 | lp_build_intrinsic(base->gallivm->builder, |
| 826 | "llvm.SI.export", |
| 827 | LLVMVoidTypeInContext(base->gallivm->context), |
| 828 | args, 9); |
| 829 | else |
| 830 | memcpy(last_args, args, sizeof(args)); |
| 831 | } |
| 832 | |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 833 | if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) { |
| 834 | unsigned pos_idx = 0; |
Christian König | f18fd25 | 2012-07-25 21:58:46 +0200 | [diff] [blame] | 835 | |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 836 | for (i = 0; i < 4; i++) |
| 837 | if (pos_args[i][0]) |
| 838 | shader->nr_pos_exports++; |
Christian König | f18fd25 | 2012-07-25 21:58:46 +0200 | [diff] [blame] | 839 | |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 840 | for (i = 0; i < 4; i++) { |
| 841 | if (!pos_args[i][0]) |
| 842 | continue; |
Christian König | f18fd25 | 2012-07-25 21:58:46 +0200 | [diff] [blame] | 843 | |
Michel Dänzer | c840270 | 2013-02-12 18:37:22 +0100 | [diff] [blame] | 844 | /* Specify the target we are exporting */ |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 845 | pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++); |
| 846 | |
| 847 | if (pos_idx == shader->nr_pos_exports) |
| 848 | /* Specify that this is the last export */ |
| 849 | pos_args[i][2] = uint->one; |
Michel Dänzer | c840270 | 2013-02-12 18:37:22 +0100 | [diff] [blame] | 850 | |
| 851 | lp_build_intrinsic(base->gallivm->builder, |
| 852 | "llvm.SI.export", |
| 853 | LLVMVoidTypeInContext(base->gallivm->context), |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 854 | pos_args[i], 9); |
| 855 | } |
| 856 | } else { |
| 857 | if (!last_args[0]) { |
| 858 | /* Specify which components to enable */ |
| 859 | last_args[0] = lp_build_const_int32(base->gallivm, 0x0); |
| 860 | |
| 861 | /* Specify the target we are exporting */ |
| 862 | last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT); |
| 863 | |
| 864 | /* Set COMPR flag to zero to export data as 32-bit */ |
| 865 | last_args[4] = uint->zero; |
| 866 | |
| 867 | /* dummy bits */ |
| 868 | last_args[5]= uint->zero; |
| 869 | last_args[6]= uint->zero; |
| 870 | last_args[7]= uint->zero; |
| 871 | last_args[8]= uint->zero; |
Michel Dänzer | c840270 | 2013-02-12 18:37:22 +0100 | [diff] [blame] | 872 | |
| 873 | si_shader_ctx->shader->spi_shader_col_format |= |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 874 | V_028714_SPI_SHADER_32_ABGR; |
| 875 | si_shader_ctx->shader->cb_shader_mask |= S_02823C_OUTPUT0_ENABLE(0xf); |
Michel Dänzer | c840270 | 2013-02-12 18:37:22 +0100 | [diff] [blame] | 876 | } |
| 877 | |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 878 | /* Specify whether the EXEC mask represents the valid mask */ |
| 879 | last_args[1] = uint->one; |
| 880 | |
| 881 | if (shader->fs_write_all && shader->nr_cbufs > 1) { |
| 882 | int i; |
| 883 | |
| 884 | /* Specify that this is not yet the last export */ |
| 885 | last_args[2] = lp_build_const_int32(base->gallivm, 0); |
| 886 | |
| 887 | for (i = 1; i < shader->nr_cbufs; i++) { |
| 888 | /* Specify the target we are exporting */ |
| 889 | last_args[3] = lp_build_const_int32(base->gallivm, |
| 890 | V_008DFC_SQ_EXP_MRT + i); |
| 891 | |
| 892 | lp_build_intrinsic(base->gallivm->builder, |
| 893 | "llvm.SI.export", |
| 894 | LLVMVoidTypeInContext(base->gallivm->context), |
| 895 | last_args, 9); |
| 896 | |
| 897 | si_shader_ctx->shader->spi_shader_col_format |= |
| 898 | si_shader_ctx->shader->spi_shader_col_format << 4; |
| 899 | si_shader_ctx->shader->cb_shader_mask |= |
| 900 | si_shader_ctx->shader->cb_shader_mask << 4; |
| 901 | } |
| 902 | |
| 903 | last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT); |
| 904 | } |
| 905 | |
| 906 | /* Specify that this is the last export */ |
| 907 | last_args[2] = lp_build_const_int32(base->gallivm, 1); |
| 908 | |
| 909 | lp_build_intrinsic(base->gallivm->builder, |
| 910 | "llvm.SI.export", |
| 911 | LLVMVoidTypeInContext(base->gallivm->context), |
| 912 | last_args, 9); |
Michel Dänzer | c840270 | 2013-02-12 18:37:22 +0100 | [diff] [blame] | 913 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 914 | /* XXX: Look up what this function does */ |
| 915 | /* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/ |
| 916 | } |
| 917 | |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame^] | 918 | static const struct lp_build_tgsi_action txf_action; |
| 919 | |
| 920 | static void build_tex_intrinsic(const struct lp_build_tgsi_action * action, |
| 921 | struct lp_build_tgsi_context * bld_base, |
| 922 | struct lp_build_emit_data * emit_data); |
| 923 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 924 | static void tex_fetch_args( |
| 925 | struct lp_build_tgsi_context * bld_base, |
| 926 | struct lp_build_emit_data * emit_data) |
| 927 | { |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 928 | struct si_shader_context *si_shader_ctx = si_shader_context(bld_base); |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 929 | struct gallivm_state *gallivm = bld_base->base.gallivm; |
Michel Dänzer | c2bae6b | 2012-08-02 17:19:22 +0200 | [diff] [blame] | 930 | const struct tgsi_full_instruction * inst = emit_data->inst; |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 931 | unsigned opcode = inst->Instruction.Opcode; |
| 932 | unsigned target = inst->Texture.Texture; |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame^] | 933 | unsigned sampler_src, sampler_index; |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 934 | LLVMValueRef coords[4]; |
| 935 | LLVMValueRef address[16]; |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame^] | 936 | LLVMValueRef sample_index_rewrite = NULL; |
| 937 | LLVMValueRef sample_chan = NULL; |
Michel Dänzer | beaa5eb | 2013-05-24 13:23:26 +0100 | [diff] [blame] | 938 | int ref_pos; |
| 939 | unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos); |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 940 | unsigned count = 0; |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 941 | unsigned chan; |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 942 | |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 943 | /* Fetch and project texture coordinates */ |
| 944 | coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W); |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 945 | for (chan = 0; chan < 3; chan++ ) { |
| 946 | coords[chan] = lp_build_emit_fetch(bld_base, |
| 947 | emit_data->inst, 0, |
| 948 | chan); |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 949 | if (opcode == TGSI_OPCODE_TXP) |
Michel Dänzer | c2bae6b | 2012-08-02 17:19:22 +0200 | [diff] [blame] | 950 | coords[chan] = lp_build_emit_llvm_binary(bld_base, |
| 951 | TGSI_OPCODE_DIV, |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 952 | coords[chan], |
| 953 | coords[3]); |
| 954 | } |
| 955 | |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 956 | if (opcode == TGSI_OPCODE_TXP) |
| 957 | coords[3] = bld_base->base.one; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 958 | |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 959 | /* Pack LOD bias value */ |
| 960 | if (opcode == TGSI_OPCODE_TXB) |
| 961 | address[count++] = coords[3]; |
Vadim Girlin | 8cf552b | 2012-12-18 17:39:19 +0400 | [diff] [blame] | 962 | |
Michel Dänzer | 0495adb | 2013-05-06 12:45:14 +0200 | [diff] [blame] | 963 | if (target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE) |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 964 | radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords); |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 965 | |
| 966 | /* Pack depth comparison value */ |
| 967 | switch (target) { |
| 968 | case TGSI_TEXTURE_SHADOW1D: |
| 969 | case TGSI_TEXTURE_SHADOW1D_ARRAY: |
| 970 | case TGSI_TEXTURE_SHADOW2D: |
| 971 | case TGSI_TEXTURE_SHADOWRECT: |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 972 | case TGSI_TEXTURE_SHADOWCUBE: |
| 973 | case TGSI_TEXTURE_SHADOW2D_ARRAY: |
Michel Dänzer | beaa5eb | 2013-05-24 13:23:26 +0100 | [diff] [blame] | 974 | assert(ref_pos >= 0); |
| 975 | address[count++] = coords[ref_pos]; |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 976 | break; |
| 977 | case TGSI_TEXTURE_SHADOWCUBE_ARRAY: |
| 978 | address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0); |
Michel Dänzer | e0f2ffc | 2012-12-03 12:46:30 +0100 | [diff] [blame] | 979 | } |
| 980 | |
Michel Dänzer | a6b83c0 | 2013-02-21 16:10:55 +0100 | [diff] [blame] | 981 | /* Pack user derivatives */ |
| 982 | if (opcode == TGSI_OPCODE_TXD) { |
| 983 | for (chan = 0; chan < 2; chan++) { |
| 984 | address[count++] = lp_build_emit_fetch(bld_base, inst, 1, chan); |
| 985 | if (num_coords > 1) |
| 986 | address[count++] = lp_build_emit_fetch(bld_base, inst, 2, chan); |
| 987 | } |
| 988 | } |
| 989 | |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 990 | /* Pack texture coordinates */ |
| 991 | address[count++] = coords[0]; |
Michel Dänzer | beaa5eb | 2013-05-24 13:23:26 +0100 | [diff] [blame] | 992 | if (num_coords > 1) |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 993 | address[count++] = coords[1]; |
Michel Dänzer | beaa5eb | 2013-05-24 13:23:26 +0100 | [diff] [blame] | 994 | if (num_coords > 2) |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 995 | address[count++] = coords[2]; |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 996 | |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 997 | /* Pack array slice */ |
| 998 | switch (target) { |
| 999 | case TGSI_TEXTURE_1D_ARRAY: |
| 1000 | address[count++] = coords[1]; |
| 1001 | } |
| 1002 | switch (target) { |
| 1003 | case TGSI_TEXTURE_2D_ARRAY: |
| 1004 | case TGSI_TEXTURE_2D_ARRAY_MSAA: |
| 1005 | case TGSI_TEXTURE_SHADOW2D_ARRAY: |
| 1006 | address[count++] = coords[2]; |
| 1007 | } |
| 1008 | switch (target) { |
| 1009 | case TGSI_TEXTURE_CUBE_ARRAY: |
| 1010 | case TGSI_TEXTURE_SHADOW1D_ARRAY: |
| 1011 | case TGSI_TEXTURE_SHADOWCUBE_ARRAY: |
| 1012 | address[count++] = coords[3]; |
| 1013 | } |
| 1014 | |
| 1015 | /* Pack LOD */ |
Michel Dänzer | 3623111 | 2013-05-02 09:44:45 +0200 | [diff] [blame] | 1016 | if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF) |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 1017 | address[count++] = coords[3]; |
| 1018 | |
| 1019 | if (count > 16) { |
| 1020 | assert(!"Cannot handle more than 16 texture address parameters"); |
| 1021 | count = 16; |
| 1022 | } |
| 1023 | |
| 1024 | for (chan = 0; chan < count; chan++ ) { |
| 1025 | address[chan] = LLVMBuildBitCast(gallivm->builder, |
| 1026 | address[chan], |
| 1027 | LLVMInt32TypeInContext(gallivm->context), |
| 1028 | ""); |
| 1029 | } |
| 1030 | |
Michel Dänzer | a6b83c0 | 2013-02-21 16:10:55 +0100 | [diff] [blame] | 1031 | sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1; |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame^] | 1032 | sampler_index = emit_data->inst->Src[sampler_src].Register.Index; |
| 1033 | |
| 1034 | /* Adjust the sample index according to FMASK. |
| 1035 | * |
| 1036 | * For uncompressed MSAA surfaces, FMASK should return 0x76543210, |
| 1037 | * which is the identity mapping. Each nibble says which physical sample |
| 1038 | * should be fetched to get that sample. |
| 1039 | * |
| 1040 | * For example, 0x11111100 means there are only 2 samples stored and |
| 1041 | * the second sample covers 3/4 of the pixel. When reading samples 0 |
| 1042 | * and 1, return physical sample 0 (determined by the first two 0s |
| 1043 | * in FMASK), otherwise return physical sample 1. |
| 1044 | * |
| 1045 | * The sample index should be adjusted as follows: |
| 1046 | * sample_index = (fmask >> (sample_index * 4)) & 0xF; |
| 1047 | */ |
| 1048 | if (target == TGSI_TEXTURE_2D_MSAA || |
| 1049 | target == TGSI_TEXTURE_2D_ARRAY_MSAA) { |
| 1050 | struct lp_build_context *uint_bld = &bld_base->uint_bld; |
| 1051 | struct lp_build_emit_data txf_emit_data = *emit_data; |
| 1052 | LLVMValueRef txf_address[16]; |
| 1053 | unsigned txf_count = count; |
| 1054 | |
| 1055 | memcpy(txf_address, address, sizeof(address)); |
| 1056 | |
| 1057 | /* Pad to a power-of-two size. */ |
| 1058 | while (txf_count < util_next_power_of_two(txf_count)) |
| 1059 | txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context)); |
| 1060 | |
| 1061 | /* Read FMASK using TXF. */ |
| 1062 | txf_emit_data.chan = 0; |
| 1063 | txf_emit_data.dst_type = LLVMVectorType( |
| 1064 | LLVMInt32TypeInContext(bld_base->base.gallivm->context), 4); |
| 1065 | txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count); |
| 1066 | txf_emit_data.args[1] = si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index]; |
| 1067 | txf_emit_data.args[2] = lp_build_const_int32(bld_base->base.gallivm, target); |
| 1068 | txf_emit_data.arg_count = 3; |
| 1069 | |
| 1070 | build_tex_intrinsic(&txf_action, bld_base, &txf_emit_data); |
| 1071 | |
| 1072 | /* Initialize some constants. */ |
| 1073 | if (target == TGSI_TEXTURE_2D_MSAA) { |
| 1074 | sample_chan = LLVMConstInt(uint_bld->elem_type, 2, 0); |
| 1075 | } else { |
| 1076 | sample_chan = LLVMConstInt(uint_bld->elem_type, 3, 0); |
| 1077 | } |
| 1078 | |
| 1079 | LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0); |
| 1080 | LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0); |
| 1081 | |
| 1082 | /* Apply the formula. */ |
| 1083 | LLVMValueRef fmask = |
| 1084 | LLVMBuildExtractElement(gallivm->builder, |
| 1085 | txf_emit_data.output[0], |
| 1086 | uint_bld->zero, ""); |
| 1087 | |
| 1088 | LLVMValueRef sample_index = |
| 1089 | LLVMBuildExtractElement(gallivm->builder, |
| 1090 | txf_emit_data.args[0], |
| 1091 | sample_chan, ""); |
| 1092 | |
| 1093 | LLVMValueRef sample_index4 = |
| 1094 | LLVMBuildMul(gallivm->builder, sample_index, four, ""); |
| 1095 | |
| 1096 | LLVMValueRef shifted_fmask = |
| 1097 | LLVMBuildLShr(gallivm->builder, fmask, sample_index4, ""); |
| 1098 | |
| 1099 | LLVMValueRef final_sample = |
| 1100 | LLVMBuildAnd(gallivm->builder, shifted_fmask, F, ""); |
| 1101 | |
| 1102 | /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK |
| 1103 | * resource descriptor is 0 (invalid), |
| 1104 | */ |
| 1105 | LLVMValueRef fmask_desc = |
| 1106 | LLVMBuildBitCast(gallivm->builder, |
| 1107 | si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index], |
| 1108 | LLVMVectorType(uint_bld->elem_type, 8), ""); |
| 1109 | |
| 1110 | LLVMValueRef fmask_word1 = |
| 1111 | LLVMBuildExtractElement(gallivm->builder, fmask_desc, |
| 1112 | uint_bld->one, ""); |
| 1113 | |
| 1114 | LLVMValueRef word1_is_nonzero = |
| 1115 | LLVMBuildICmp(gallivm->builder, LLVMIntNE, |
| 1116 | fmask_word1, uint_bld->zero, ""); |
| 1117 | |
| 1118 | sample_index_rewrite = |
| 1119 | LLVMBuildSelect(gallivm->builder, word1_is_nonzero, |
| 1120 | final_sample, sample_index, ""); |
| 1121 | } |
Michel Dänzer | a6b83c0 | 2013-02-21 16:10:55 +0100 | [diff] [blame] | 1122 | |
Michel Dänzer | 3623111 | 2013-05-02 09:44:45 +0200 | [diff] [blame] | 1123 | /* Resource */ |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame^] | 1124 | emit_data->args[1] = si_shader_ctx->resources[sampler_index]; |
Michel Dänzer | 3623111 | 2013-05-02 09:44:45 +0200 | [diff] [blame] | 1125 | |
| 1126 | if (opcode == TGSI_OPCODE_TXF) { |
| 1127 | /* add tex offsets */ |
| 1128 | if (inst->Texture.NumOffsets) { |
| 1129 | struct lp_build_context *uint_bld = &bld_base->uint_bld; |
| 1130 | struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base); |
| 1131 | const struct tgsi_texture_offset * off = inst->TexOffsets; |
| 1132 | |
| 1133 | assert(inst->Texture.NumOffsets == 1); |
| 1134 | |
| 1135 | address[0] = |
| 1136 | lp_build_add(uint_bld, address[0], |
| 1137 | bld->immediates[off->Index][off->SwizzleX]); |
| 1138 | if (num_coords > 1) |
| 1139 | address[1] = |
| 1140 | lp_build_add(uint_bld, address[1], |
| 1141 | bld->immediates[off->Index][off->SwizzleY]); |
| 1142 | if (num_coords > 2) |
| 1143 | address[2] = |
| 1144 | lp_build_add(uint_bld, address[2], |
| 1145 | bld->immediates[off->Index][off->SwizzleZ]); |
| 1146 | } |
| 1147 | |
| 1148 | emit_data->dst_type = LLVMVectorType( |
| 1149 | LLVMInt32TypeInContext(bld_base->base.gallivm->context), |
| 1150 | 4); |
| 1151 | |
| 1152 | emit_data->arg_count = 3; |
| 1153 | } else { |
| 1154 | /* Sampler */ |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame^] | 1155 | emit_data->args[2] = si_shader_ctx->samplers[sampler_index]; |
Michel Dänzer | 3623111 | 2013-05-02 09:44:45 +0200 | [diff] [blame] | 1156 | |
| 1157 | emit_data->dst_type = LLVMVectorType( |
| 1158 | LLVMFloatTypeInContext(bld_base->base.gallivm->context), |
| 1159 | 4); |
| 1160 | |
| 1161 | emit_data->arg_count = 4; |
| 1162 | } |
| 1163 | |
| 1164 | /* Dimensions */ |
| 1165 | emit_data->args[emit_data->arg_count - 1] = |
| 1166 | lp_build_const_int32(bld_base->base.gallivm, target); |
| 1167 | |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 1168 | /* Pad to power of two vector */ |
| 1169 | while (count < util_next_power_of_two(count)) |
| 1170 | address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context)); |
| 1171 | |
Christian König | ccf3e8f | 2013-03-26 15:09:27 +0100 | [diff] [blame] | 1172 | emit_data->args[0] = lp_build_gather_values(gallivm, address, count); |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame^] | 1173 | |
| 1174 | /* Replace the MSAA sample index if needed. */ |
| 1175 | if (sample_index_rewrite) { |
| 1176 | emit_data->args[0] = |
| 1177 | LLVMBuildInsertElement(gallivm->builder, emit_data->args[0], |
| 1178 | sample_index_rewrite, sample_chan, ""); |
| 1179 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1180 | } |
| 1181 | |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 1182 | static void build_tex_intrinsic(const struct lp_build_tgsi_action * action, |
| 1183 | struct lp_build_tgsi_context * bld_base, |
| 1184 | struct lp_build_emit_data * emit_data) |
| 1185 | { |
| 1186 | struct lp_build_context * base = &bld_base->base; |
| 1187 | char intr_name[23]; |
| 1188 | |
| 1189 | sprintf(intr_name, "%sv%ui32", action->intr_name, |
Christian König | ccf3e8f | 2013-03-26 15:09:27 +0100 | [diff] [blame] | 1190 | LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0]))); |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 1191 | |
Christian König | 44e3224 | 2013-03-20 12:10:35 +0100 | [diff] [blame] | 1192 | emit_data->output[emit_data->chan] = build_intrinsic( |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 1193 | base->gallivm->builder, intr_name, emit_data->dst_type, |
Christian König | 44e3224 | 2013-03-20 12:10:35 +0100 | [diff] [blame] | 1194 | emit_data->args, emit_data->arg_count, |
| 1195 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 1196 | } |
| 1197 | |
Michel Dänzer | 0495adb | 2013-05-06 12:45:14 +0200 | [diff] [blame] | 1198 | static void txq_fetch_args( |
| 1199 | struct lp_build_tgsi_context * bld_base, |
| 1200 | struct lp_build_emit_data * emit_data) |
| 1201 | { |
| 1202 | struct si_shader_context *si_shader_ctx = si_shader_context(bld_base); |
| 1203 | const struct tgsi_full_instruction *inst = emit_data->inst; |
| 1204 | |
| 1205 | /* Mip level */ |
| 1206 | emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X); |
| 1207 | |
| 1208 | /* Resource */ |
| 1209 | emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index]; |
| 1210 | |
| 1211 | /* Dimensions */ |
| 1212 | emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm, |
| 1213 | inst->Texture.Texture); |
| 1214 | |
| 1215 | emit_data->arg_count = 3; |
| 1216 | |
| 1217 | emit_data->dst_type = LLVMVectorType( |
| 1218 | LLVMInt32TypeInContext(bld_base->base.gallivm->context), |
| 1219 | 4); |
| 1220 | } |
| 1221 | |
Michel Dänzer | a06ee5a | 2013-06-19 18:14:01 +0200 | [diff] [blame] | 1222 | #if HAVE_LLVM >= 0x0304 |
| 1223 | |
| 1224 | static void si_llvm_emit_ddxy( |
| 1225 | const struct lp_build_tgsi_action * action, |
| 1226 | struct lp_build_tgsi_context * bld_base, |
| 1227 | struct lp_build_emit_data * emit_data) |
| 1228 | { |
| 1229 | struct si_shader_context *si_shader_ctx = si_shader_context(bld_base); |
| 1230 | struct gallivm_state *gallivm = bld_base->base.gallivm; |
| 1231 | struct lp_build_context * base = &bld_base->base; |
| 1232 | const struct tgsi_full_instruction *inst = emit_data->inst; |
| 1233 | unsigned opcode = inst->Instruction.Opcode; |
| 1234 | LLVMValueRef indices[2]; |
| 1235 | LLVMValueRef store_ptr, load_ptr0, load_ptr1; |
| 1236 | LLVMValueRef tl, trbl, result[4]; |
| 1237 | LLVMTypeRef i32; |
| 1238 | unsigned swizzle[4]; |
| 1239 | unsigned c; |
| 1240 | |
| 1241 | i32 = LLVMInt32TypeInContext(gallivm->context); |
| 1242 | |
| 1243 | indices[0] = bld_base->uint_bld.zero; |
| 1244 | indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32, |
| 1245 | NULL, 0, LLVMReadNoneAttribute); |
| 1246 | store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds, |
| 1247 | indices, 2, ""); |
| 1248 | |
| 1249 | indices[1] = LLVMBuildAnd(gallivm->builder, indices[1], |
| 1250 | lp_build_const_int32(gallivm, 0xfffffffc), ""); |
| 1251 | load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds, |
| 1252 | indices, 2, ""); |
| 1253 | |
| 1254 | indices[1] = LLVMBuildAdd(gallivm->builder, indices[1], |
| 1255 | lp_build_const_int32(gallivm, |
| 1256 | opcode == TGSI_OPCODE_DDX ? 1 : 2), |
| 1257 | ""); |
| 1258 | load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds, |
| 1259 | indices, 2, ""); |
| 1260 | |
| 1261 | for (c = 0; c < 4; ++c) { |
| 1262 | unsigned i; |
| 1263 | |
| 1264 | swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c); |
| 1265 | for (i = 0; i < c; ++i) { |
| 1266 | if (swizzle[i] == swizzle[c]) { |
| 1267 | result[c] = result[i]; |
| 1268 | break; |
| 1269 | } |
| 1270 | } |
| 1271 | if (i != c) |
| 1272 | continue; |
| 1273 | |
| 1274 | LLVMBuildStore(gallivm->builder, |
| 1275 | LLVMBuildBitCast(gallivm->builder, |
| 1276 | lp_build_emit_fetch(bld_base, inst, 0, c), |
| 1277 | i32, ""), |
| 1278 | store_ptr); |
| 1279 | |
| 1280 | tl = LLVMBuildLoad(gallivm->builder, load_ptr0, ""); |
| 1281 | tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, ""); |
| 1282 | |
| 1283 | trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, ""); |
| 1284 | trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, ""); |
| 1285 | |
| 1286 | result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, ""); |
| 1287 | } |
| 1288 | |
| 1289 | emit_data->output[0] = lp_build_gather_values(gallivm, result, 4); |
| 1290 | } |
| 1291 | |
| 1292 | #endif /* HAVE_LLVM >= 0x0304 */ |
| 1293 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1294 | static const struct lp_build_tgsi_action tex_action = { |
| 1295 | .fetch_args = tex_fetch_args, |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 1296 | .emit = build_tex_intrinsic, |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 1297 | .intr_name = "llvm.SI.sample." |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1298 | }; |
| 1299 | |
Michel Dänzer | 3e20513 | 2012-11-06 17:39:01 +0100 | [diff] [blame] | 1300 | static const struct lp_build_tgsi_action txb_action = { |
| 1301 | .fetch_args = tex_fetch_args, |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 1302 | .emit = build_tex_intrinsic, |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 1303 | .intr_name = "llvm.SI.sampleb." |
Michel Dänzer | 3e20513 | 2012-11-06 17:39:01 +0100 | [diff] [blame] | 1304 | }; |
| 1305 | |
Michel Dänzer | a6b83c0 | 2013-02-21 16:10:55 +0100 | [diff] [blame] | 1306 | #if HAVE_LLVM >= 0x0304 |
| 1307 | static const struct lp_build_tgsi_action txd_action = { |
| 1308 | .fetch_args = tex_fetch_args, |
| 1309 | .emit = build_tex_intrinsic, |
| 1310 | .intr_name = "llvm.SI.sampled." |
| 1311 | }; |
| 1312 | #endif |
| 1313 | |
Michel Dänzer | 3623111 | 2013-05-02 09:44:45 +0200 | [diff] [blame] | 1314 | static const struct lp_build_tgsi_action txf_action = { |
| 1315 | .fetch_args = tex_fetch_args, |
| 1316 | .emit = build_tex_intrinsic, |
| 1317 | .intr_name = "llvm.SI.imageload." |
| 1318 | }; |
| 1319 | |
Michel Dänzer | 56ae9be | 2012-11-06 17:41:50 +0100 | [diff] [blame] | 1320 | static const struct lp_build_tgsi_action txl_action = { |
| 1321 | .fetch_args = tex_fetch_args, |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 1322 | .emit = build_tex_intrinsic, |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 1323 | .intr_name = "llvm.SI.samplel." |
Michel Dänzer | 56ae9be | 2012-11-06 17:41:50 +0100 | [diff] [blame] | 1324 | }; |
| 1325 | |
Michel Dänzer | 0495adb | 2013-05-06 12:45:14 +0200 | [diff] [blame] | 1326 | static const struct lp_build_tgsi_action txq_action = { |
| 1327 | .fetch_args = txq_fetch_args, |
| 1328 | .emit = build_tgsi_intrinsic_nomem, |
| 1329 | .intr_name = "llvm.SI.resinfo" |
| 1330 | }; |
| 1331 | |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 1332 | static void create_meta_data(struct si_shader_context *si_shader_ctx) |
| 1333 | { |
| 1334 | struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm; |
| 1335 | LLVMValueRef args[3]; |
| 1336 | |
| 1337 | args[0] = LLVMMDStringInContext(gallivm->context, "const", 5); |
| 1338 | args[1] = 0; |
| 1339 | args[2] = lp_build_const_int32(gallivm, 1); |
| 1340 | |
| 1341 | si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3); |
| 1342 | } |
| 1343 | |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 1344 | static void create_function(struct si_shader_context *si_shader_ctx) |
| 1345 | { |
Michel Dänzer | a06ee5a | 2013-06-19 18:14:01 +0200 | [diff] [blame] | 1346 | struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base; |
| 1347 | struct gallivm_state *gallivm = bld_base->base.gallivm; |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 1348 | LLVMTypeRef params[20], f32, i8, i32, v2i32, v3i32; |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 1349 | unsigned i; |
| 1350 | |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 1351 | i8 = LLVMInt8TypeInContext(gallivm->context); |
Christian König | c497321 | 2013-03-05 12:14:02 +0100 | [diff] [blame] | 1352 | i32 = LLVMInt32TypeInContext(gallivm->context); |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 1353 | f32 = LLVMFloatTypeInContext(gallivm->context); |
| 1354 | v2i32 = LLVMVectorType(i32, 2); |
| 1355 | v3i32 = LLVMVectorType(i32, 3); |
Christian König | c497321 | 2013-03-05 12:14:02 +0100 | [diff] [blame] | 1356 | |
Christian König | f5298b0 | 2013-02-28 14:50:07 +0100 | [diff] [blame] | 1357 | params[SI_PARAM_CONST] = LLVMPointerType(LLVMVectorType(i8, 16), CONST_ADDR_SPACE); |
| 1358 | params[SI_PARAM_SAMPLER] = params[SI_PARAM_CONST]; |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 1359 | params[SI_PARAM_RESOURCE] = LLVMPointerType(LLVMVectorType(i8, 32), CONST_ADDR_SPACE); |
| 1360 | |
Christian König | c497321 | 2013-03-05 12:14:02 +0100 | [diff] [blame] | 1361 | if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) { |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 1362 | params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_SAMPLER]; |
Christian König | cf9b31f | 2013-03-21 18:30:23 +0100 | [diff] [blame] | 1363 | params[SI_PARAM_START_INSTANCE] = i32; |
Christian König | e4ed587 | 2013-03-21 18:02:52 +0100 | [diff] [blame] | 1364 | params[SI_PARAM_VERTEX_ID] = i32; |
| 1365 | params[SI_PARAM_DUMMY_0] = i32; |
| 1366 | params[SI_PARAM_DUMMY_1] = i32; |
| 1367 | params[SI_PARAM_INSTANCE_ID] = i32; |
Christian König | cf9b31f | 2013-03-21 18:30:23 +0100 | [diff] [blame] | 1368 | radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 9); |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 1369 | |
Christian König | c497321 | 2013-03-05 12:14:02 +0100 | [diff] [blame] | 1370 | } else { |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 1371 | params[SI_PARAM_PRIM_MASK] = i32; |
| 1372 | params[SI_PARAM_PERSP_SAMPLE] = v2i32; |
| 1373 | params[SI_PARAM_PERSP_CENTER] = v2i32; |
| 1374 | params[SI_PARAM_PERSP_CENTROID] = v2i32; |
| 1375 | params[SI_PARAM_PERSP_PULL_MODEL] = v3i32; |
| 1376 | params[SI_PARAM_LINEAR_SAMPLE] = v2i32; |
| 1377 | params[SI_PARAM_LINEAR_CENTER] = v2i32; |
| 1378 | params[SI_PARAM_LINEAR_CENTROID] = v2i32; |
| 1379 | params[SI_PARAM_LINE_STIPPLE_TEX] = f32; |
| 1380 | params[SI_PARAM_POS_X_FLOAT] = f32; |
| 1381 | params[SI_PARAM_POS_Y_FLOAT] = f32; |
| 1382 | params[SI_PARAM_POS_Z_FLOAT] = f32; |
| 1383 | params[SI_PARAM_POS_W_FLOAT] = f32; |
| 1384 | params[SI_PARAM_FRONT_FACE] = f32; |
| 1385 | params[SI_PARAM_ANCILLARY] = f32; |
| 1386 | params[SI_PARAM_SAMPLE_COVERAGE] = f32; |
| 1387 | params[SI_PARAM_POS_FIXED_PT] = f32; |
| 1388 | radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 20); |
Christian König | c497321 | 2013-03-05 12:14:02 +0100 | [diff] [blame] | 1389 | } |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 1390 | |
| 1391 | radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type); |
| 1392 | for (i = SI_PARAM_CONST; i <= SI_PARAM_VERTEX_BUFFER; ++i) { |
| 1393 | LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i); |
| 1394 | LLVMAddAttribute(P, LLVMInRegAttribute); |
| 1395 | } |
Christian König | cf9b31f | 2013-03-21 18:30:23 +0100 | [diff] [blame] | 1396 | |
| 1397 | if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) { |
| 1398 | LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, |
| 1399 | SI_PARAM_START_INSTANCE); |
| 1400 | LLVMAddAttribute(P, LLVMInRegAttribute); |
| 1401 | } |
Michel Dänzer | a06ee5a | 2013-06-19 18:14:01 +0200 | [diff] [blame] | 1402 | |
| 1403 | #if HAVE_LLVM >= 0x0304 |
| 1404 | if (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 || |
| 1405 | bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0) |
| 1406 | si_shader_ctx->ddxy_lds = |
| 1407 | LLVMAddGlobalInAddressSpace(gallivm->module, |
| 1408 | LLVMArrayType(i32, 64), |
| 1409 | "ddxy_lds", |
| 1410 | LOCAL_ADDR_SPACE); |
| 1411 | #endif |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 1412 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1413 | |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 1414 | static void preload_constants(struct si_shader_context *si_shader_ctx) |
| 1415 | { |
| 1416 | struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base; |
| 1417 | struct gallivm_state * gallivm = bld_base->base.gallivm; |
| 1418 | const struct tgsi_shader_info * info = bld_base->info; |
| 1419 | |
| 1420 | unsigned i, num_const = info->file_max[TGSI_FILE_CONSTANT] + 1; |
| 1421 | |
| 1422 | LLVMValueRef ptr; |
| 1423 | |
| 1424 | if (num_const == 0) |
| 1425 | return; |
| 1426 | |
| 1427 | /* Allocate space for the constant values */ |
| 1428 | si_shader_ctx->constants = CALLOC(num_const * 4, sizeof(LLVMValueRef)); |
| 1429 | |
| 1430 | /* Load the resource descriptor */ |
| 1431 | ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST); |
| 1432 | si_shader_ctx->const_resource = build_indexed_load(si_shader_ctx, ptr, bld_base->uint_bld.zero); |
| 1433 | |
| 1434 | /* Load the constants, we rely on the code sinking to do the rest */ |
| 1435 | for (i = 0; i < num_const * 4; ++i) { |
| 1436 | LLVMValueRef args[2] = { |
| 1437 | si_shader_ctx->const_resource, |
| 1438 | lp_build_const_int32(gallivm, i * 4) |
| 1439 | }; |
| 1440 | si_shader_ctx->constants[i] = build_intrinsic(gallivm->builder, "llvm.SI.load.const", |
| 1441 | bld_base->base.elem_type, args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
| 1442 | } |
| 1443 | } |
| 1444 | |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 1445 | static void preload_samplers(struct si_shader_context *si_shader_ctx) |
| 1446 | { |
| 1447 | struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base; |
| 1448 | struct gallivm_state * gallivm = bld_base->base.gallivm; |
| 1449 | const struct tgsi_shader_info * info = bld_base->info; |
| 1450 | |
| 1451 | unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1; |
| 1452 | |
| 1453 | LLVMValueRef res_ptr, samp_ptr; |
| 1454 | LLVMValueRef offset; |
| 1455 | |
| 1456 | if (num_samplers == 0) |
| 1457 | return; |
| 1458 | |
| 1459 | /* Allocate space for the values */ |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame^] | 1460 | si_shader_ctx->resources = CALLOC(NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef)); |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 1461 | si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef)); |
| 1462 | |
| 1463 | res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE); |
| 1464 | samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER); |
| 1465 | |
| 1466 | /* Load the resources and samplers, we rely on the code sinking to do the rest */ |
| 1467 | for (i = 0; i < num_samplers; ++i) { |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 1468 | /* Resource */ |
| 1469 | offset = lp_build_const_int32(gallivm, i); |
| 1470 | si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset); |
| 1471 | |
| 1472 | /* Sampler */ |
| 1473 | offset = lp_build_const_int32(gallivm, i); |
| 1474 | si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset); |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame^] | 1475 | |
| 1476 | /* FMASK resource */ |
| 1477 | if (info->is_msaa_sampler[i]) { |
| 1478 | offset = lp_build_const_int32(gallivm, FMASK_TEX_OFFSET + i); |
| 1479 | si_shader_ctx->resources[FMASK_TEX_OFFSET + i] = |
| 1480 | build_indexed_load(si_shader_ctx, res_ptr, offset); |
| 1481 | } |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 1482 | } |
| 1483 | } |
| 1484 | |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1485 | int si_compile_llvm(struct r600_context *rctx, struct si_pipe_shader *shader, |
| 1486 | LLVMModuleRef mod) |
| 1487 | { |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1488 | unsigned i; |
| 1489 | uint32_t *ptr; |
| 1490 | bool dump; |
Tom Stellard | 7782d19 | 2013-04-04 09:57:13 -0700 | [diff] [blame] | 1491 | struct radeon_llvm_binary binary; |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1492 | |
| 1493 | dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE); |
| 1494 | |
Tom Stellard | 7782d19 | 2013-04-04 09:57:13 -0700 | [diff] [blame] | 1495 | memset(&binary, 0, sizeof(binary)); |
| 1496 | radeon_llvm_compile(mod, &binary, |
| 1497 | r600_get_llvm_processor_name(rctx->screen->family), dump); |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1498 | if (dump) { |
| 1499 | fprintf(stderr, "SI CODE:\n"); |
Tom Stellard | 7782d19 | 2013-04-04 09:57:13 -0700 | [diff] [blame] | 1500 | for (i = 0; i < binary.code_size; i+=4 ) { |
| 1501 | fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3], |
| 1502 | binary.code[i + 2], binary.code[i + 1], |
| 1503 | binary.code[i]); |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1504 | } |
| 1505 | } |
| 1506 | |
Tom Stellard | d50343d | 2013-04-04 16:21:06 -0400 | [diff] [blame] | 1507 | /* XXX: We may be able to emit some of these values directly rather than |
| 1508 | * extracting fields to be emitted later. |
| 1509 | */ |
| 1510 | for (i = 0; i < binary.config_size; i+= 8) { |
| 1511 | unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i)); |
| 1512 | unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4)); |
| 1513 | switch (reg) { |
| 1514 | case R_00B028_SPI_SHADER_PGM_RSRC1_PS: |
| 1515 | case R_00B128_SPI_SHADER_PGM_RSRC1_VS: |
| 1516 | case R_00B228_SPI_SHADER_PGM_RSRC1_GS: |
| 1517 | case R_00B848_COMPUTE_PGM_RSRC1: |
| 1518 | shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8; |
| 1519 | shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4; |
| 1520 | break; |
Michel Dänzer | a06ee5a | 2013-06-19 18:14:01 +0200 | [diff] [blame] | 1521 | case R_00B02C_SPI_SHADER_PGM_RSRC2_PS: |
| 1522 | shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value); |
| 1523 | break; |
| 1524 | case R_00B84C_COMPUTE_PGM_RSRC2: |
| 1525 | shader->lds_size = G_00B84C_LDS_SIZE(value); |
| 1526 | break; |
Tom Stellard | d50343d | 2013-04-04 16:21:06 -0400 | [diff] [blame] | 1527 | case R_0286CC_SPI_PS_INPUT_ENA: |
| 1528 | shader->spi_ps_input_ena = value; |
| 1529 | break; |
| 1530 | default: |
| 1531 | fprintf(stderr, "Warning: Compiler emitted unknown " |
| 1532 | "config register: 0x%x\n", reg); |
| 1533 | break; |
| 1534 | } |
| 1535 | } |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1536 | |
| 1537 | /* copy new shader */ |
| 1538 | si_resource_reference(&shader->bo, NULL); |
| 1539 | shader->bo = si_resource_create_custom(rctx->context.screen, PIPE_USAGE_IMMUTABLE, |
Tom Stellard | d50343d | 2013-04-04 16:21:06 -0400 | [diff] [blame] | 1540 | binary.code_size); |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1541 | if (shader->bo == NULL) { |
| 1542 | return -ENOMEM; |
| 1543 | } |
| 1544 | |
| 1545 | ptr = (uint32_t*)rctx->ws->buffer_map(shader->bo->cs_buf, rctx->cs, PIPE_TRANSFER_WRITE); |
| 1546 | if (0 /*R600_BIG_ENDIAN*/) { |
Tom Stellard | d50343d | 2013-04-04 16:21:06 -0400 | [diff] [blame] | 1547 | for (i = 0; i < binary.code_size / 4; ++i) { |
| 1548 | ptr[i] = util_bswap32(*(uint32_t*)(binary.code + i*4)); |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1549 | } |
| 1550 | } else { |
Tom Stellard | d50343d | 2013-04-04 16:21:06 -0400 | [diff] [blame] | 1551 | memcpy(ptr, binary.code, binary.code_size); |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1552 | } |
| 1553 | rctx->ws->buffer_unmap(shader->bo->cs_buf); |
| 1554 | |
Tom Stellard | 7782d19 | 2013-04-04 09:57:13 -0700 | [diff] [blame] | 1555 | free(binary.code); |
| 1556 | free(binary.config); |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1557 | |
| 1558 | return 0; |
| 1559 | } |
| 1560 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1561 | int si_pipe_shader_create( |
| 1562 | struct pipe_context *ctx, |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 1563 | struct si_pipe_shader *shader) |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1564 | { |
| 1565 | struct r600_context *rctx = (struct r600_context*)ctx; |
Michel Dänzer | d1e40b3 | 2012-08-23 17:10:37 +0200 | [diff] [blame] | 1566 | struct si_pipe_shader_selector *sel = shader->selector; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1567 | struct si_shader_context si_shader_ctx; |
| 1568 | struct tgsi_shader_info shader_info; |
| 1569 | struct lp_build_tgsi_context * bld_base; |
| 1570 | LLVMModuleRef mod; |
Michel Dänzer | 4c4ef9c | 2012-06-07 19:30:47 +0200 | [diff] [blame] | 1571 | bool dump; |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1572 | int r = 0; |
Michel Dänzer | 4c4ef9c | 2012-06-07 19:30:47 +0200 | [diff] [blame] | 1573 | |
| 1574 | dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1575 | |
Michel Dänzer | 82e38ac | 2012-09-27 16:39:26 +0200 | [diff] [blame] | 1576 | assert(shader->shader.noutput == 0); |
| 1577 | assert(shader->shader.ninterp == 0); |
| 1578 | assert(shader->shader.ninput == 0); |
| 1579 | |
Michel Dänzer | cfebaf9 | 2012-08-31 19:04:08 +0200 | [diff] [blame] | 1580 | memset(&si_shader_ctx, 0, sizeof(si_shader_ctx)); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1581 | radeon_llvm_context_init(&si_shader_ctx.radeon_bld); |
| 1582 | bld_base = &si_shader_ctx.radeon_bld.soa.bld_base; |
| 1583 | |
Michel Dänzer | d1e40b3 | 2012-08-23 17:10:37 +0200 | [diff] [blame] | 1584 | tgsi_scan_shader(sel->tokens, &shader_info); |
Michel Dänzer | a06ee5a | 2013-06-19 18:14:01 +0200 | [diff] [blame] | 1585 | |
Michel Dänzer | e44dfd4 | 2012-11-07 17:33:08 +0100 | [diff] [blame] | 1586 | shader->shader.uses_kill = shader_info.uses_kill; |
Christian König | e4ed587 | 2013-03-21 18:02:52 +0100 | [diff] [blame] | 1587 | shader->shader.uses_instanceid = shader_info.uses_instanceid; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1588 | bld_base->info = &shader_info; |
| 1589 | bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1590 | bld_base->emit_epilogue = si_llvm_emit_epilogue; |
| 1591 | |
| 1592 | bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action; |
Michel Dänzer | 3e20513 | 2012-11-06 17:39:01 +0100 | [diff] [blame] | 1593 | bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action; |
Michel Dänzer | a6b83c0 | 2013-02-21 16:10:55 +0100 | [diff] [blame] | 1594 | #if HAVE_LLVM >= 0x0304 |
| 1595 | bld_base->op_actions[TGSI_OPCODE_TXD] = txd_action; |
| 1596 | #endif |
Michel Dänzer | 3623111 | 2013-05-02 09:44:45 +0200 | [diff] [blame] | 1597 | bld_base->op_actions[TGSI_OPCODE_TXF] = txf_action; |
Michel Dänzer | 56ae9be | 2012-11-06 17:41:50 +0100 | [diff] [blame] | 1598 | bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action; |
Michel Dänzer | c2bae6b | 2012-08-02 17:19:22 +0200 | [diff] [blame] | 1599 | bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action; |
Michel Dänzer | 0495adb | 2013-05-06 12:45:14 +0200 | [diff] [blame] | 1600 | bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1601 | |
Michel Dänzer | a06ee5a | 2013-06-19 18:14:01 +0200 | [diff] [blame] | 1602 | #if HAVE_LLVM >= 0x0304 |
| 1603 | bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy; |
| 1604 | bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy; |
| 1605 | #endif |
| 1606 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1607 | si_shader_ctx.radeon_bld.load_input = declare_input; |
Christian König | e4ed587 | 2013-03-21 18:02:52 +0100 | [diff] [blame] | 1608 | si_shader_ctx.radeon_bld.load_system_value = declare_system_value; |
Michel Dänzer | d1e40b3 | 2012-08-23 17:10:37 +0200 | [diff] [blame] | 1609 | si_shader_ctx.tokens = sel->tokens; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1610 | tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens); |
| 1611 | si_shader_ctx.shader = shader; |
| 1612 | si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1613 | |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 1614 | create_meta_data(&si_shader_ctx); |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 1615 | create_function(&si_shader_ctx); |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 1616 | preload_constants(&si_shader_ctx); |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 1617 | preload_samplers(&si_shader_ctx); |
Christian König | b8f4ca3 | 2013-03-04 15:35:30 +0100 | [diff] [blame] | 1618 | |
Christian König | 835098a | 2012-07-17 21:28:10 +0200 | [diff] [blame] | 1619 | shader->shader.nr_cbufs = rctx->framebuffer.nr_cbufs; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1620 | |
Tom Stellard | 185fc9a | 2012-07-12 10:40:47 -0400 | [diff] [blame] | 1621 | /* Dump TGSI code before doing TGSI->LLVM conversion in case the |
| 1622 | * conversion fails. */ |
| 1623 | if (dump) { |
Michel Dänzer | d1e40b3 | 2012-08-23 17:10:37 +0200 | [diff] [blame] | 1624 | tgsi_dump(sel->tokens, 0); |
Tom Stellard | 185fc9a | 2012-07-12 10:40:47 -0400 | [diff] [blame] | 1625 | } |
| 1626 | |
Michel Dänzer | d1e40b3 | 2012-08-23 17:10:37 +0200 | [diff] [blame] | 1627 | if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) { |
Michel Dänzer | 82cd9c0 | 2012-08-08 15:35:42 +0200 | [diff] [blame] | 1628 | fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n"); |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 1629 | FREE(si_shader_ctx.constants); |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 1630 | FREE(si_shader_ctx.resources); |
| 1631 | FREE(si_shader_ctx.samplers); |
Michel Dänzer | 82cd9c0 | 2012-08-08 15:35:42 +0200 | [diff] [blame] | 1632 | return -EINVAL; |
| 1633 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1634 | |
| 1635 | radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld); |
| 1636 | |
| 1637 | mod = bld_base->base.gallivm->module; |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1638 | r = si_compile_llvm(rctx, shader, mod); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1639 | |
Michel Dänzer | 4b64fa2 | 2012-08-15 18:22:46 +0200 | [diff] [blame] | 1640 | radeon_llvm_dispose(&si_shader_ctx.radeon_bld); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1641 | tgsi_parse_free(&si_shader_ctx.parse); |
| 1642 | |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 1643 | FREE(si_shader_ctx.constants); |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 1644 | FREE(si_shader_ctx.resources); |
| 1645 | FREE(si_shader_ctx.samplers); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1646 | |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1647 | return r; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader) |
| 1651 | { |
Christian König | fe41287 | 2012-07-24 18:47:19 +0200 | [diff] [blame] | 1652 | si_resource_reference(&shader->bo, NULL); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1653 | } |