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" |
| 43 | #include "tgsi/tgsi_dump.h" |
| 44 | |
| 45 | #include "radeonsi_pipe.h" |
| 46 | #include "radeonsi_shader.h" |
Christian König | f67fae0 | 2012-07-17 23:43:00 +0200 | [diff] [blame] | 47 | #include "si_state.h" |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 48 | #include "sid.h" |
| 49 | |
| 50 | #include <assert.h> |
| 51 | #include <errno.h> |
| 52 | #include <stdio.h> |
| 53 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 54 | struct si_shader_context |
| 55 | { |
| 56 | struct radeon_llvm_context radeon_bld; |
| 57 | struct r600_context *rctx; |
| 58 | struct tgsi_parse_context parse; |
| 59 | struct tgsi_token * tokens; |
| 60 | struct si_pipe_shader *shader; |
Michel Dänzer | 44ef033 | 2012-10-05 16:59:10 +0200 | [diff] [blame] | 61 | struct si_shader_key key; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 62 | unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */ |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 63 | LLVMValueRef const_md; |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 64 | LLVMValueRef const_resource; |
| 65 | LLVMValueRef *constants; |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 66 | LLVMValueRef *resources; |
| 67 | LLVMValueRef *samplers; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | static struct si_shader_context * si_shader_context( |
| 71 | struct lp_build_tgsi_context * bld_base) |
| 72 | { |
| 73 | return (struct si_shader_context *)bld_base; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | #define PERSPECTIVE_BASE 0 |
| 78 | #define LINEAR_BASE 9 |
| 79 | |
| 80 | #define SAMPLE_OFFSET 0 |
| 81 | #define CENTER_OFFSET 2 |
| 82 | #define CENTROID_OFSET 4 |
| 83 | |
| 84 | #define USE_SGPR_MAX_SUFFIX_LEN 5 |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 85 | #define CONST_ADDR_SPACE 2 |
Tom Stellard | 89ece08 | 2012-05-29 11:36:29 -0400 | [diff] [blame] | 86 | #define USER_SGPR_ADDR_SPACE 8 |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 87 | |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 88 | /** |
| 89 | * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad |
| 90 | * |
| 91 | * @param offset The offset parameter specifies the number of |
| 92 | * elements to offset, not the number of bytes or dwords. An element is the |
| 93 | * the type pointed to by the base_ptr parameter (e.g. int is the element of |
| 94 | * an int* pointer) |
| 95 | * |
| 96 | * When LLVM lowers the load instruction, it will convert the element offset |
| 97 | * into a dword offset automatically. |
| 98 | * |
| 99 | */ |
| 100 | static LLVMValueRef build_indexed_load( |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 101 | struct si_shader_context * si_shader_ctx, |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 102 | LLVMValueRef base_ptr, |
| 103 | LLVMValueRef offset) |
| 104 | { |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 105 | 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] | 106 | |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 107 | LLVMValueRef computed_ptr = LLVMBuildGEP( |
| 108 | base->gallivm->builder, base_ptr, &offset, 1, ""); |
| 109 | |
| 110 | LLVMValueRef result = LLVMBuildLoad(base->gallivm->builder, computed_ptr, ""); |
| 111 | LLVMSetMetadata(result, 1, si_shader_ctx->const_md); |
| 112 | return result; |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 113 | } |
| 114 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 115 | static void declare_input_vs( |
| 116 | struct si_shader_context * si_shader_ctx, |
| 117 | unsigned input_index, |
| 118 | const struct tgsi_full_declaration *decl) |
| 119 | { |
| 120 | LLVMValueRef t_list_ptr; |
| 121 | LLVMValueRef t_offset; |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 122 | LLVMValueRef t_list; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 123 | LLVMValueRef attribute_offset; |
| 124 | LLVMValueRef buffer_index_reg; |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 125 | LLVMValueRef args[3]; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 126 | LLVMTypeRef vec4_type; |
| 127 | LLVMValueRef input; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 128 | struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base; |
Christian König | b15e3ae | 2012-07-25 11:22:59 +0200 | [diff] [blame] | 129 | //struct pipe_vertex_element *velem = &rctx->vertex_elements->elements[input_index]; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 130 | unsigned chan; |
| 131 | |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 132 | /* Load the T list */ |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 133 | 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] | 134 | |
Christian König | b15e3ae | 2012-07-25 11:22:59 +0200 | [diff] [blame] | 135 | t_offset = lp_build_const_int32(base->gallivm, input_index); |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 136 | |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 137 | 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] | 138 | |
| 139 | /* Build the attribute offset */ |
Christian König | b15e3ae | 2012-07-25 11:22:59 +0200 | [diff] [blame] | 140 | attribute_offset = lp_build_const_int32(base->gallivm, 0); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 141 | |
Christian König | c497321 | 2013-03-05 12:14:02 +0100 | [diff] [blame] | 142 | /* Load the buffer index, which is always stored in VGPR0 |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 143 | * for Vertex Shaders */ |
Christian König | e4ed587 | 2013-03-21 18:02:52 +0100 | [diff] [blame^] | 144 | buffer_index_reg = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_ID); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 145 | |
| 146 | vec4_type = LLVMVectorType(base->elem_type, 4); |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 147 | args[0] = t_list; |
| 148 | args[1] = attribute_offset; |
| 149 | args[2] = buffer_index_reg; |
Christian König | 44e3224 | 2013-03-20 12:10:35 +0100 | [diff] [blame] | 150 | input = build_intrinsic(base->gallivm->builder, |
| 151 | "llvm.SI.vs.load.input", vec4_type, args, 3, |
| 152 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 153 | |
| 154 | /* Break up the vec4 into individual components */ |
| 155 | for (chan = 0; chan < 4; chan++) { |
| 156 | LLVMValueRef llvm_chan = lp_build_const_int32(base->gallivm, chan); |
| 157 | /* XXX: Use a helper function for this. There is one in |
| 158 | * tgsi_llvm.c. */ |
| 159 | si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] = |
| 160 | LLVMBuildExtractElement(base->gallivm->builder, |
| 161 | input, llvm_chan, ""); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static void declare_input_fs( |
| 166 | struct si_shader_context * si_shader_ctx, |
| 167 | unsigned input_index, |
| 168 | const struct tgsi_full_declaration *decl) |
| 169 | { |
Michel Dänzer | c3db19e | 2012-09-27 20:01:33 +0200 | [diff] [blame] | 170 | struct si_shader *shader = &si_shader_ctx->shader->shader; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 171 | struct lp_build_context * base = |
| 172 | &si_shader_ctx->radeon_bld.soa.bld_base.base; |
| 173 | struct gallivm_state * gallivm = base->gallivm; |
Tom Stellard | 0fb1e68 | 2012-09-06 16:18:11 -0400 | [diff] [blame] | 174 | LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context); |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 175 | LLVMValueRef main_fn = si_shader_ctx->radeon_bld.main_fn; |
| 176 | |
| 177 | LLVMValueRef interp_param; |
| 178 | const char * intr_name; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 179 | |
| 180 | /* This value is: |
| 181 | * [15:0] NewPrimMask (Bit mask for each quad. It is set it the |
| 182 | * quad begins a new primitive. Bit 0 always needs |
| 183 | * to be unset) |
| 184 | * [32:16] ParamOffset |
| 185 | * |
| 186 | */ |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 187 | 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] | 188 | LLVMValueRef attr_number; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 189 | |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 190 | unsigned chan; |
| 191 | |
Tom Stellard | 0fb1e68 | 2012-09-06 16:18:11 -0400 | [diff] [blame] | 192 | if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) { |
| 193 | for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) { |
Tom Stellard | 0fb1e68 | 2012-09-06 16:18:11 -0400 | [diff] [blame] | 194 | unsigned soa_index = |
| 195 | radeon_llvm_reg_index_soa(input_index, chan); |
Tom Stellard | 0fb1e68 | 2012-09-06 16:18:11 -0400 | [diff] [blame] | 196 | si_shader_ctx->radeon_bld.inputs[soa_index] = |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 197 | LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan); |
Michel Dänzer | 954bc4a | 2013-02-13 15:57:23 +0100 | [diff] [blame] | 198 | |
| 199 | if (chan == 3) |
| 200 | /* RCP for fragcoord.w */ |
| 201 | si_shader_ctx->radeon_bld.inputs[soa_index] = |
| 202 | LLVMBuildFDiv(gallivm->builder, |
| 203 | lp_build_const_float(gallivm, 1.0f), |
| 204 | si_shader_ctx->radeon_bld.inputs[soa_index], |
| 205 | ""); |
Tom Stellard | 0fb1e68 | 2012-09-06 16:18:11 -0400 | [diff] [blame] | 206 | } |
| 207 | return; |
| 208 | } |
| 209 | |
Michel Dänzer | 97078b1 | 2012-09-25 12:41:31 +0200 | [diff] [blame] | 210 | if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) { |
| 211 | LLVMValueRef face, is_face_positive; |
| 212 | |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 213 | face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE); |
| 214 | |
Michel Dänzer | 97078b1 | 2012-09-25 12:41:31 +0200 | [diff] [blame] | 215 | is_face_positive = LLVMBuildFCmp(gallivm->builder, |
| 216 | LLVMRealUGT, face, |
| 217 | lp_build_const_float(gallivm, 0.0f), |
| 218 | ""); |
| 219 | |
| 220 | si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] = |
| 221 | LLVMBuildSelect(gallivm->builder, |
| 222 | is_face_positive, |
| 223 | lp_build_const_float(gallivm, 1.0f), |
| 224 | lp_build_const_float(gallivm, 0.0f), |
| 225 | ""); |
| 226 | si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] = |
| 227 | si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] = |
| 228 | lp_build_const_float(gallivm, 0.0f); |
| 229 | si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] = |
| 230 | lp_build_const_float(gallivm, 1.0f); |
| 231 | |
| 232 | return; |
| 233 | } |
| 234 | |
Michel Dänzer | c3db19e | 2012-09-27 20:01:33 +0200 | [diff] [blame] | 235 | shader->input[input_index].param_offset = shader->ninterp++; |
| 236 | attr_number = lp_build_const_int32(gallivm, |
| 237 | shader->input[input_index].param_offset); |
| 238 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 239 | /* XXX: Handle all possible interpolation modes */ |
Francisco Jerez | 1279923 | 2012-04-30 18:27:52 +0200 | [diff] [blame] | 240 | switch (decl->Interp.Interpolate) { |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 241 | case TGSI_INTERPOLATE_COLOR: |
Michel Dänzer | 18272c9 | 2013-02-13 12:54:13 +0100 | [diff] [blame] | 242 | if (si_shader_ctx->key.flatshade) { |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 243 | interp_param = 0; |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame] | 244 | } else { |
| 245 | if (decl->Interp.Centroid) |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 246 | interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID); |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame] | 247 | else |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 248 | interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER); |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame] | 249 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 250 | break; |
| 251 | case TGSI_INTERPOLATE_CONSTANT: |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 252 | interp_param = 0; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 253 | break; |
| 254 | case TGSI_INTERPOLATE_LINEAR: |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame] | 255 | if (decl->Interp.Centroid) |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 256 | interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID); |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame] | 257 | else |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 258 | interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER); |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame] | 259 | break; |
| 260 | case TGSI_INTERPOLATE_PERSPECTIVE: |
| 261 | if (decl->Interp.Centroid) |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 262 | interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID); |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame] | 263 | else |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 264 | interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 265 | break; |
| 266 | default: |
| 267 | fprintf(stderr, "Warning: Unhandled interpolation mode.\n"); |
| 268 | return; |
| 269 | } |
| 270 | |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 271 | intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant"; |
| 272 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 273 | /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */ |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 274 | if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR && |
| 275 | si_shader_ctx->key.color_two_side) { |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 276 | LLVMValueRef args[4]; |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 277 | LLVMValueRef face, is_face_positive; |
| 278 | LLVMValueRef back_attr_number = |
| 279 | lp_build_const_int32(gallivm, |
| 280 | shader->input[input_index].param_offset + 1); |
| 281 | |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 282 | face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE); |
| 283 | |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 284 | is_face_positive = LLVMBuildFCmp(gallivm->builder, |
| 285 | LLVMRealUGT, face, |
| 286 | lp_build_const_float(gallivm, 0.0f), |
| 287 | ""); |
| 288 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 289 | args[2] = params; |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 290 | args[3] = interp_param; |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 291 | for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) { |
| 292 | LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan); |
| 293 | unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan); |
| 294 | LLVMValueRef front, back; |
| 295 | |
| 296 | args[0] = llvm_chan; |
| 297 | args[1] = attr_number; |
| 298 | front = build_intrinsic(base->gallivm->builder, intr_name, |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 299 | input_type, args, args[3] ? 4 : 3, |
Christian König | 44e3224 | 2013-03-20 12:10:35 +0100 | [diff] [blame] | 300 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 301 | |
| 302 | args[1] = back_attr_number; |
| 303 | back = build_intrinsic(base->gallivm->builder, intr_name, |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 304 | input_type, args, args[3] ? 4 : 3, |
Christian König | 44e3224 | 2013-03-20 12:10:35 +0100 | [diff] [blame] | 305 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 306 | |
| 307 | si_shader_ctx->radeon_bld.inputs[soa_index] = |
| 308 | LLVMBuildSelect(gallivm->builder, |
| 309 | is_face_positive, |
| 310 | front, |
| 311 | back, |
| 312 | ""); |
| 313 | } |
| 314 | |
| 315 | shader->ninterp++; |
| 316 | } else { |
| 317 | for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) { |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 318 | LLVMValueRef args[4]; |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 319 | LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan); |
| 320 | unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan); |
| 321 | args[0] = llvm_chan; |
| 322 | args[1] = attr_number; |
| 323 | args[2] = params; |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 324 | args[3] = interp_param; |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 325 | si_shader_ctx->radeon_bld.inputs[soa_index] = |
| 326 | 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 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | static void declare_input( |
| 334 | struct radeon_llvm_context * radeon_bld, |
| 335 | unsigned input_index, |
| 336 | const struct tgsi_full_declaration *decl) |
| 337 | { |
| 338 | struct si_shader_context * si_shader_ctx = |
| 339 | si_shader_context(&radeon_bld->soa.bld_base); |
| 340 | if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) { |
| 341 | declare_input_vs(si_shader_ctx, input_index, decl); |
| 342 | } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) { |
| 343 | declare_input_fs(si_shader_ctx, input_index, decl); |
| 344 | } else { |
| 345 | fprintf(stderr, "Warning: Unsupported shader type,\n"); |
| 346 | } |
| 347 | } |
| 348 | |
Christian König | e4ed587 | 2013-03-21 18:02:52 +0100 | [diff] [blame^] | 349 | static void declare_system_value( |
| 350 | struct radeon_llvm_context * radeon_bld, |
| 351 | unsigned index, |
| 352 | const struct tgsi_full_declaration *decl) |
| 353 | { |
| 354 | LLVMValueRef value = 0; |
| 355 | |
| 356 | switch (decl->Semantic.Name) { |
| 357 | case TGSI_SEMANTIC_INSTANCEID: |
| 358 | value = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_INSTANCE_ID); |
| 359 | break; |
| 360 | |
| 361 | case TGSI_SEMANTIC_VERTEXID: |
| 362 | value = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_VERTEX_ID); |
| 363 | break; |
| 364 | |
| 365 | default: |
| 366 | assert(!"unknown system value"); |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | radeon_bld->system_values[index] = value; |
| 371 | } |
| 372 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 373 | static LLVMValueRef fetch_constant( |
| 374 | struct lp_build_tgsi_context * bld_base, |
| 375 | const struct tgsi_full_src_register *reg, |
| 376 | enum tgsi_opcode_type type, |
| 377 | unsigned swizzle) |
| 378 | { |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 379 | struct si_shader_context *si_shader_ctx = si_shader_context(bld_base); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 380 | struct lp_build_context * base = &bld_base->base; |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 381 | const struct tgsi_ind_register *ireg = ®->Indirect; |
| 382 | unsigned idx; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 383 | |
Christian König | f5298b0 | 2013-02-28 14:50:07 +0100 | [diff] [blame] | 384 | LLVMValueRef args[2]; |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 385 | LLVMValueRef addr; |
Christian König | f5298b0 | 2013-02-28 14:50:07 +0100 | [diff] [blame] | 386 | LLVMValueRef result; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 387 | |
Christian König | 8514f5a | 2013-02-04 17:46:42 +0100 | [diff] [blame] | 388 | if (swizzle == LP_CHAN_ALL) { |
| 389 | unsigned chan; |
| 390 | LLVMValueRef values[4]; |
| 391 | for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) |
| 392 | values[chan] = fetch_constant(bld_base, reg, type, chan); |
| 393 | |
| 394 | return lp_build_gather_values(bld_base->base.gallivm, values, 4); |
| 395 | } |
| 396 | |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 397 | idx = reg->Register.Index * 4 + swizzle; |
| 398 | if (!reg->Register.Indirect) |
| 399 | return bitcast(bld_base, type, si_shader_ctx->constants[idx]); |
Christian König | f5298b0 | 2013-02-28 14:50:07 +0100 | [diff] [blame] | 400 | |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 401 | args[0] = si_shader_ctx->const_resource; |
| 402 | args[1] = lp_build_const_int32(base->gallivm, idx * 4); |
| 403 | addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle]; |
| 404 | addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg"); |
| 405 | addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16); |
| 406 | 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] | 407 | |
Christian König | f5298b0 | 2013-02-28 14:50:07 +0100 | [diff] [blame] | 408 | 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] | 409 | args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 410 | |
Christian König | f5298b0 | 2013-02-28 14:50:07 +0100 | [diff] [blame] | 411 | return bitcast(bld_base, type, result); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 412 | } |
| 413 | |
Michel Dänzer | 26c7139 | 2012-08-24 12:03:11 +0200 | [diff] [blame] | 414 | /* Initialize arguments for the shader export intrinsic */ |
| 415 | static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base, |
| 416 | struct tgsi_full_declaration *d, |
| 417 | unsigned index, |
| 418 | unsigned target, |
| 419 | LLVMValueRef *args) |
| 420 | { |
| 421 | struct si_shader_context *si_shader_ctx = si_shader_context(bld_base); |
| 422 | struct lp_build_context *uint = |
| 423 | &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld; |
| 424 | struct lp_build_context *base = &bld_base->base; |
| 425 | unsigned compressed = 0; |
| 426 | unsigned chan; |
| 427 | |
Michel Dänzer | f402acd | 2012-08-22 18:15:36 +0200 | [diff] [blame] | 428 | if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) { |
| 429 | int cbuf = target - V_008DFC_SQ_EXP_MRT; |
| 430 | |
| 431 | if (cbuf >= 0 && cbuf < 8) { |
Michel Dänzer | 44ef033 | 2012-10-05 16:59:10 +0200 | [diff] [blame] | 432 | compressed = (si_shader_ctx->key.export_16bpc >> cbuf) & 0x1; |
Michel Dänzer | 1ace200 | 2012-12-21 15:39:26 +0100 | [diff] [blame] | 433 | |
| 434 | if (compressed) |
| 435 | si_shader_ctx->shader->spi_shader_col_format |= |
| 436 | V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf); |
| 437 | else |
| 438 | si_shader_ctx->shader->spi_shader_col_format |= |
| 439 | V_028714_SPI_SHADER_32_ABGR << (4 * cbuf); |
Michel Dänzer | f402acd | 2012-08-22 18:15:36 +0200 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
| 443 | if (compressed) { |
| 444 | /* Pixel shader needs to pack output values before export */ |
| 445 | for (chan = 0; chan < 2; chan++ ) { |
| 446 | LLVMValueRef *out_ptr = |
| 447 | si_shader_ctx->radeon_bld.soa.outputs[index]; |
| 448 | args[0] = LLVMBuildLoad(base->gallivm->builder, |
| 449 | out_ptr[2 * chan], ""); |
| 450 | args[1] = LLVMBuildLoad(base->gallivm->builder, |
| 451 | out_ptr[2 * chan + 1], ""); |
| 452 | args[chan + 5] = |
| 453 | build_intrinsic(base->gallivm->builder, |
| 454 | "llvm.SI.packf16", |
| 455 | LLVMInt32TypeInContext(base->gallivm->context), |
| 456 | args, 2, |
Christian König | e4188ee | 2013-02-27 22:39:26 +0100 | [diff] [blame] | 457 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
Michel Dänzer | 8b6aec6 | 2012-11-27 19:53:58 +0100 | [diff] [blame] | 458 | args[chan + 7] = args[chan + 5] = |
| 459 | LLVMBuildBitCast(base->gallivm->builder, |
| 460 | args[chan + 5], |
| 461 | LLVMFloatTypeInContext(base->gallivm->context), |
| 462 | ""); |
Michel Dänzer | f402acd | 2012-08-22 18:15:36 +0200 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* Set COMPR flag */ |
| 466 | args[4] = uint->one; |
| 467 | } else { |
| 468 | for (chan = 0; chan < 4; chan++ ) { |
| 469 | LLVMValueRef out_ptr = |
| 470 | si_shader_ctx->radeon_bld.soa.outputs[index][chan]; |
| 471 | /* +5 because the first output value will be |
| 472 | * the 6th argument to the intrinsic. */ |
| 473 | args[chan + 5] = LLVMBuildLoad(base->gallivm->builder, |
| 474 | out_ptr, ""); |
| 475 | } |
| 476 | |
| 477 | /* Clear COMPR flag */ |
| 478 | args[4] = uint->zero; |
Michel Dänzer | 26c7139 | 2012-08-24 12:03:11 +0200 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /* XXX: This controls which components of the output |
| 482 | * registers actually get exported. (e.g bit 0 means export |
| 483 | * X component, bit 1 means export Y component, etc.) I'm |
| 484 | * hard coding this to 0xf for now. In the future, we might |
| 485 | * want to do something else. */ |
| 486 | args[0] = lp_build_const_int32(base->gallivm, 0xf); |
| 487 | |
| 488 | /* Specify whether the EXEC mask represents the valid mask */ |
| 489 | args[1] = uint->zero; |
| 490 | |
| 491 | /* Specify whether this is the last export */ |
| 492 | args[2] = uint->zero; |
| 493 | |
| 494 | /* Specify the target we are exporting */ |
| 495 | args[3] = lp_build_const_int32(base->gallivm, target); |
| 496 | |
Michel Dänzer | 26c7139 | 2012-08-24 12:03:11 +0200 | [diff] [blame] | 497 | /* XXX: We probably need to keep track of the output |
| 498 | * values, so we know what we are passing to the next |
| 499 | * stage. */ |
| 500 | } |
| 501 | |
Michel Dänzer | 7708a86 | 2012-11-02 15:57:30 +0100 | [diff] [blame] | 502 | static void si_alpha_test(struct lp_build_tgsi_context *bld_base, |
| 503 | unsigned index) |
| 504 | { |
| 505 | struct si_shader_context *si_shader_ctx = si_shader_context(bld_base); |
| 506 | struct gallivm_state *gallivm = bld_base->base.gallivm; |
| 507 | |
| 508 | if (si_shader_ctx->key.alpha_func != PIPE_FUNC_NEVER) { |
| 509 | LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][3]; |
| 510 | LLVMValueRef alpha_pass = |
| 511 | lp_build_cmp(&bld_base->base, |
| 512 | si_shader_ctx->key.alpha_func, |
| 513 | LLVMBuildLoad(gallivm->builder, out_ptr, ""), |
| 514 | lp_build_const_float(gallivm, si_shader_ctx->key.alpha_ref)); |
| 515 | LLVMValueRef arg = |
| 516 | lp_build_select(&bld_base->base, |
| 517 | alpha_pass, |
| 518 | lp_build_const_float(gallivm, 1.0f), |
| 519 | lp_build_const_float(gallivm, -1.0f)); |
| 520 | |
| 521 | build_intrinsic(gallivm->builder, |
| 522 | "llvm.AMDGPU.kill", |
| 523 | LLVMVoidTypeInContext(gallivm->context), |
| 524 | &arg, 1, 0); |
| 525 | } else { |
| 526 | build_intrinsic(gallivm->builder, |
| 527 | "llvm.AMDGPU.kilp", |
| 528 | LLVMVoidTypeInContext(gallivm->context), |
| 529 | NULL, 0, 0); |
| 530 | } |
| 531 | } |
| 532 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 533 | /* XXX: This is partially implemented for VS only at this point. It is not complete */ |
| 534 | static void si_llvm_emit_epilogue(struct lp_build_tgsi_context * bld_base) |
| 535 | { |
| 536 | 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] | 537 | struct si_shader * shader = &si_shader_ctx->shader->shader; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 538 | struct lp_build_context * base = &bld_base->base; |
| 539 | struct lp_build_context * uint = |
| 540 | &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld; |
| 541 | struct tgsi_parse_context *parse = &si_shader_ctx->parse; |
Michel Dänzer | 1a616c1 | 2012-11-13 17:35:09 +0100 | [diff] [blame] | 542 | LLVMValueRef args[9]; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 543 | LLVMValueRef last_args[9] = { 0 }; |
Christian König | 3508815 | 2012-08-01 22:35:24 +0200 | [diff] [blame] | 544 | unsigned color_count = 0; |
| 545 | unsigned param_count = 0; |
Michel Dänzer | 1a616c1 | 2012-11-13 17:35:09 +0100 | [diff] [blame] | 546 | int depth_index = -1, stencil_index = -1; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 547 | |
| 548 | while (!tgsi_parse_end_of_tokens(parse)) { |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 549 | struct tgsi_full_declaration *d = |
| 550 | &parse->FullToken.FullDeclaration; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 551 | unsigned target; |
| 552 | unsigned index; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 553 | int i; |
| 554 | |
| 555 | tgsi_parse_token(parse); |
Michel Dänzer | c840270 | 2013-02-12 18:37:22 +0100 | [diff] [blame] | 556 | |
| 557 | if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY && |
| 558 | parse->FullToken.FullProperty.Property.PropertyName == |
| 559 | TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS) |
| 560 | shader->fs_write_all = TRUE; |
| 561 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 562 | if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION) |
| 563 | continue; |
| 564 | |
| 565 | switch (d->Declaration.File) { |
| 566 | case TGSI_FILE_INPUT: |
| 567 | i = shader->ninput++; |
| 568 | shader->input[i].name = d->Semantic.Name; |
| 569 | shader->input[i].sid = d->Semantic.Index; |
Francisco Jerez | 1279923 | 2012-04-30 18:27:52 +0200 | [diff] [blame] | 570 | shader->input[i].interpolate = d->Interp.Interpolate; |
| 571 | shader->input[i].centroid = d->Interp.Centroid; |
Christian König | 3508815 | 2012-08-01 22:35:24 +0200 | [diff] [blame] | 572 | continue; |
| 573 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 574 | case TGSI_FILE_OUTPUT: |
| 575 | i = shader->noutput++; |
| 576 | shader->output[i].name = d->Semantic.Name; |
| 577 | shader->output[i].sid = d->Semantic.Index; |
Francisco Jerez | 1279923 | 2012-04-30 18:27:52 +0200 | [diff] [blame] | 578 | shader->output[i].interpolate = d->Interp.Interpolate; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 579 | break; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 580 | |
Christian König | 3508815 | 2012-08-01 22:35:24 +0200 | [diff] [blame] | 581 | default: |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 582 | continue; |
Christian König | 3508815 | 2012-08-01 22:35:24 +0200 | [diff] [blame] | 583 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 584 | |
| 585 | for (index = d->Range.First; index <= d->Range.Last; index++) { |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 586 | /* Select the correct target */ |
| 587 | switch(d->Semantic.Name) { |
Tom Stellard | c3c323a | 2012-08-30 10:35:36 -0400 | [diff] [blame] | 588 | case TGSI_SEMANTIC_PSIZE: |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 589 | target = V_008DFC_SQ_EXP_POS; |
| 590 | break; |
Michel Dänzer | 1a616c1 | 2012-11-13 17:35:09 +0100 | [diff] [blame] | 591 | case TGSI_SEMANTIC_POSITION: |
| 592 | if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) { |
| 593 | target = V_008DFC_SQ_EXP_POS; |
| 594 | break; |
| 595 | } else { |
| 596 | depth_index = index; |
| 597 | continue; |
| 598 | } |
| 599 | case TGSI_SEMANTIC_STENCIL: |
| 600 | stencil_index = index; |
| 601 | continue; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 602 | case TGSI_SEMANTIC_COLOR: |
| 603 | if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) { |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 604 | case TGSI_SEMANTIC_BCOLOR: |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 605 | target = V_008DFC_SQ_EXP_PARAM + param_count; |
Michel Dänzer | dd9d619 | 2012-05-18 15:01:10 +0200 | [diff] [blame] | 606 | shader->output[i].param_offset = param_count; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 607 | param_count++; |
| 608 | } else { |
| 609 | target = V_008DFC_SQ_EXP_MRT + color_count; |
Michel Dänzer | 7708a86 | 2012-11-02 15:57:30 +0100 | [diff] [blame] | 610 | if (color_count == 0 && |
| 611 | si_shader_ctx->key.alpha_func != PIPE_FUNC_ALWAYS) |
| 612 | si_alpha_test(bld_base, index); |
| 613 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 614 | color_count++; |
| 615 | } |
| 616 | break; |
Michel Dänzer | 30b3037 | 2012-09-06 17:53:04 +0200 | [diff] [blame] | 617 | case TGSI_SEMANTIC_FOG: |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 618 | case TGSI_SEMANTIC_GENERIC: |
| 619 | target = V_008DFC_SQ_EXP_PARAM + param_count; |
Michel Dänzer | dd9d619 | 2012-05-18 15:01:10 +0200 | [diff] [blame] | 620 | shader->output[i].param_offset = param_count; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 621 | param_count++; |
| 622 | break; |
| 623 | default: |
| 624 | target = 0; |
| 625 | fprintf(stderr, |
| 626 | "Warning: SI unhandled output type:%d\n", |
| 627 | d->Semantic.Name); |
| 628 | } |
| 629 | |
Michel Dänzer | 26c7139 | 2012-08-24 12:03:11 +0200 | [diff] [blame] | 630 | si_llvm_init_export_args(bld_base, d, index, target, args); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 631 | |
| 632 | if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX ? |
| 633 | (d->Semantic.Name == TGSI_SEMANTIC_POSITION) : |
| 634 | (d->Semantic.Name == TGSI_SEMANTIC_COLOR)) { |
| 635 | if (last_args[0]) { |
| 636 | lp_build_intrinsic(base->gallivm->builder, |
| 637 | "llvm.SI.export", |
| 638 | LLVMVoidTypeInContext(base->gallivm->context), |
| 639 | last_args, 9); |
| 640 | } |
| 641 | |
| 642 | memcpy(last_args, args, sizeof(args)); |
| 643 | } else { |
| 644 | lp_build_intrinsic(base->gallivm->builder, |
| 645 | "llvm.SI.export", |
| 646 | LLVMVoidTypeInContext(base->gallivm->context), |
| 647 | args, 9); |
| 648 | } |
| 649 | |
| 650 | } |
| 651 | } |
| 652 | |
Michel Dänzer | 1a616c1 | 2012-11-13 17:35:09 +0100 | [diff] [blame] | 653 | if (depth_index >= 0 || stencil_index >= 0) { |
| 654 | LLVMValueRef out_ptr; |
| 655 | unsigned mask = 0; |
| 656 | |
| 657 | /* Specify the target we are exporting */ |
| 658 | args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ); |
| 659 | |
| 660 | if (depth_index >= 0) { |
| 661 | out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2]; |
| 662 | args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, ""); |
| 663 | mask |= 0x1; |
| 664 | |
| 665 | if (stencil_index < 0) { |
| 666 | args[6] = |
| 667 | args[7] = |
| 668 | args[8] = args[5]; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | if (stencil_index >= 0) { |
| 673 | out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1]; |
| 674 | args[7] = |
| 675 | args[8] = |
| 676 | args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, ""); |
| 677 | mask |= 0x2; |
| 678 | |
| 679 | if (depth_index < 0) |
| 680 | args[5] = args[6]; |
| 681 | } |
| 682 | |
| 683 | /* Specify which components to enable */ |
| 684 | args[0] = lp_build_const_int32(base->gallivm, mask); |
| 685 | |
| 686 | args[1] = |
| 687 | args[2] = |
| 688 | args[4] = uint->zero; |
| 689 | |
| 690 | if (last_args[0]) |
| 691 | lp_build_intrinsic(base->gallivm->builder, |
| 692 | "llvm.SI.export", |
| 693 | LLVMVoidTypeInContext(base->gallivm->context), |
| 694 | args, 9); |
| 695 | else |
| 696 | memcpy(last_args, args, sizeof(args)); |
| 697 | } |
| 698 | |
Christian König | f18fd25 | 2012-07-25 21:58:46 +0200 | [diff] [blame] | 699 | if (!last_args[0]) { |
| 700 | assert(si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT); |
| 701 | |
| 702 | /* Specify which components to enable */ |
| 703 | last_args[0] = lp_build_const_int32(base->gallivm, 0x0); |
| 704 | |
| 705 | /* Specify the target we are exporting */ |
| 706 | last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT); |
| 707 | |
| 708 | /* Set COMPR flag to zero to export data as 32-bit */ |
| 709 | last_args[4] = uint->zero; |
| 710 | |
| 711 | /* dummy bits */ |
| 712 | last_args[5]= uint->zero; |
| 713 | last_args[6]= uint->zero; |
| 714 | last_args[7]= uint->zero; |
| 715 | last_args[8]= uint->zero; |
Michel Dänzer | 1ace200 | 2012-12-21 15:39:26 +0100 | [diff] [blame] | 716 | |
| 717 | si_shader_ctx->shader->spi_shader_col_format |= |
| 718 | V_028714_SPI_SHADER_32_ABGR; |
Christian König | f18fd25 | 2012-07-25 21:58:46 +0200 | [diff] [blame] | 719 | } |
| 720 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 721 | /* Specify whether the EXEC mask represents the valid mask */ |
| 722 | last_args[1] = lp_build_const_int32(base->gallivm, |
| 723 | si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT); |
| 724 | |
Michel Dänzer | c840270 | 2013-02-12 18:37:22 +0100 | [diff] [blame] | 725 | if (shader->fs_write_all && shader->nr_cbufs > 1) { |
| 726 | int i; |
| 727 | |
| 728 | /* Specify that this is not yet the last export */ |
| 729 | last_args[2] = lp_build_const_int32(base->gallivm, 0); |
| 730 | |
| 731 | for (i = 1; i < shader->nr_cbufs; i++) { |
| 732 | /* Specify the target we are exporting */ |
| 733 | last_args[3] = lp_build_const_int32(base->gallivm, |
| 734 | V_008DFC_SQ_EXP_MRT + i); |
| 735 | |
| 736 | lp_build_intrinsic(base->gallivm->builder, |
| 737 | "llvm.SI.export", |
| 738 | LLVMVoidTypeInContext(base->gallivm->context), |
| 739 | last_args, 9); |
| 740 | |
| 741 | si_shader_ctx->shader->spi_shader_col_format |= |
| 742 | si_shader_ctx->shader->spi_shader_col_format << 4; |
| 743 | } |
| 744 | |
| 745 | last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT); |
| 746 | } |
| 747 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 748 | /* Specify that this is the last export */ |
| 749 | last_args[2] = lp_build_const_int32(base->gallivm, 1); |
| 750 | |
| 751 | lp_build_intrinsic(base->gallivm->builder, |
| 752 | "llvm.SI.export", |
| 753 | LLVMVoidTypeInContext(base->gallivm->context), |
| 754 | last_args, 9); |
| 755 | |
| 756 | /* XXX: Look up what this function does */ |
| 757 | /* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/ |
| 758 | } |
| 759 | |
| 760 | static void tex_fetch_args( |
| 761 | struct lp_build_tgsi_context * bld_base, |
| 762 | struct lp_build_emit_data * emit_data) |
| 763 | { |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 764 | 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] | 765 | struct gallivm_state *gallivm = bld_base->base.gallivm; |
Michel Dänzer | c2bae6b | 2012-08-02 17:19:22 +0200 | [diff] [blame] | 766 | const struct tgsi_full_instruction * inst = emit_data->inst; |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 767 | unsigned opcode = inst->Instruction.Opcode; |
| 768 | unsigned target = inst->Texture.Texture; |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 769 | LLVMValueRef coords[4]; |
| 770 | LLVMValueRef address[16]; |
| 771 | unsigned count = 0; |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 772 | unsigned chan; |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 773 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 774 | /* WriteMask */ |
Christian König | 250b7fd | 2012-08-01 23:18:14 +0200 | [diff] [blame] | 775 | /* XXX: should be optimized using emit_data->inst->Dst[0].Register.WriteMask*/ |
| 776 | emit_data->args[0] = lp_build_const_int32(bld_base->base.gallivm, 0xf); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 777 | |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 778 | /* Fetch and project texture coordinates */ |
| 779 | 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] | 780 | for (chan = 0; chan < 3; chan++ ) { |
| 781 | coords[chan] = lp_build_emit_fetch(bld_base, |
| 782 | emit_data->inst, 0, |
| 783 | chan); |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 784 | if (opcode == TGSI_OPCODE_TXP) |
Michel Dänzer | c2bae6b | 2012-08-02 17:19:22 +0200 | [diff] [blame] | 785 | coords[chan] = lp_build_emit_llvm_binary(bld_base, |
| 786 | TGSI_OPCODE_DIV, |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 787 | coords[chan], |
| 788 | coords[3]); |
| 789 | } |
| 790 | |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 791 | if (opcode == TGSI_OPCODE_TXP) |
| 792 | coords[3] = bld_base->base.one; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 793 | |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 794 | /* Pack LOD bias value */ |
| 795 | if (opcode == TGSI_OPCODE_TXB) |
| 796 | address[count++] = coords[3]; |
Vadim Girlin | 8cf552b | 2012-12-18 17:39:19 +0400 | [diff] [blame] | 797 | |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 798 | if ((target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE) && |
| 799 | opcode != TGSI_OPCODE_TXQ) |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 800 | radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords); |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 801 | |
| 802 | /* Pack depth comparison value */ |
| 803 | switch (target) { |
| 804 | case TGSI_TEXTURE_SHADOW1D: |
| 805 | case TGSI_TEXTURE_SHADOW1D_ARRAY: |
| 806 | case TGSI_TEXTURE_SHADOW2D: |
| 807 | case TGSI_TEXTURE_SHADOWRECT: |
| 808 | address[count++] = coords[2]; |
| 809 | break; |
| 810 | case TGSI_TEXTURE_SHADOWCUBE: |
| 811 | case TGSI_TEXTURE_SHADOW2D_ARRAY: |
| 812 | address[count++] = coords[3]; |
| 813 | break; |
| 814 | case TGSI_TEXTURE_SHADOWCUBE_ARRAY: |
| 815 | address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0); |
Michel Dänzer | e0f2ffc | 2012-12-03 12:46:30 +0100 | [diff] [blame] | 816 | } |
| 817 | |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 818 | /* Pack texture coordinates */ |
| 819 | address[count++] = coords[0]; |
| 820 | switch (target) { |
| 821 | case TGSI_TEXTURE_2D: |
| 822 | case TGSI_TEXTURE_2D_ARRAY: |
| 823 | case TGSI_TEXTURE_3D: |
| 824 | case TGSI_TEXTURE_CUBE: |
| 825 | case TGSI_TEXTURE_RECT: |
| 826 | case TGSI_TEXTURE_SHADOW2D: |
| 827 | case TGSI_TEXTURE_SHADOWRECT: |
| 828 | case TGSI_TEXTURE_SHADOW2D_ARRAY: |
| 829 | case TGSI_TEXTURE_SHADOWCUBE: |
| 830 | case TGSI_TEXTURE_2D_MSAA: |
| 831 | case TGSI_TEXTURE_2D_ARRAY_MSAA: |
| 832 | case TGSI_TEXTURE_CUBE_ARRAY: |
| 833 | case TGSI_TEXTURE_SHADOWCUBE_ARRAY: |
| 834 | address[count++] = coords[1]; |
| 835 | } |
| 836 | switch (target) { |
| 837 | case TGSI_TEXTURE_3D: |
| 838 | case TGSI_TEXTURE_CUBE: |
| 839 | case TGSI_TEXTURE_SHADOWCUBE: |
| 840 | case TGSI_TEXTURE_CUBE_ARRAY: |
| 841 | case TGSI_TEXTURE_SHADOWCUBE_ARRAY: |
| 842 | address[count++] = coords[2]; |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 843 | } |
| 844 | |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 845 | /* Pack array slice */ |
| 846 | switch (target) { |
| 847 | case TGSI_TEXTURE_1D_ARRAY: |
| 848 | address[count++] = coords[1]; |
| 849 | } |
| 850 | switch (target) { |
| 851 | case TGSI_TEXTURE_2D_ARRAY: |
| 852 | case TGSI_TEXTURE_2D_ARRAY_MSAA: |
| 853 | case TGSI_TEXTURE_SHADOW2D_ARRAY: |
| 854 | address[count++] = coords[2]; |
| 855 | } |
| 856 | switch (target) { |
| 857 | case TGSI_TEXTURE_CUBE_ARRAY: |
| 858 | case TGSI_TEXTURE_SHADOW1D_ARRAY: |
| 859 | case TGSI_TEXTURE_SHADOWCUBE_ARRAY: |
| 860 | address[count++] = coords[3]; |
| 861 | } |
| 862 | |
| 863 | /* Pack LOD */ |
| 864 | if (opcode == TGSI_OPCODE_TXL) |
| 865 | address[count++] = coords[3]; |
| 866 | |
| 867 | if (count > 16) { |
| 868 | assert(!"Cannot handle more than 16 texture address parameters"); |
| 869 | count = 16; |
| 870 | } |
| 871 | |
| 872 | for (chan = 0; chan < count; chan++ ) { |
| 873 | address[chan] = LLVMBuildBitCast(gallivm->builder, |
| 874 | address[chan], |
| 875 | LLVMInt32TypeInContext(gallivm->context), |
| 876 | ""); |
| 877 | } |
| 878 | |
| 879 | /* Pad to power of two vector */ |
| 880 | while (count < util_next_power_of_two(count)) |
| 881 | address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context)); |
| 882 | |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 883 | emit_data->args[1] = lp_build_gather_values(gallivm, address, count); |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 884 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 885 | /* Resource */ |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 886 | emit_data->args[2] = si_shader_ctx->resources[emit_data->inst->Src[1].Register.Index]; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 887 | |
| 888 | /* Sampler */ |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 889 | emit_data->args[3] = si_shader_ctx->samplers[emit_data->inst->Src[1].Register.Index]; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 890 | |
| 891 | /* Dimensions */ |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 892 | emit_data->args[4] = lp_build_const_int32(bld_base->base.gallivm, target); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 893 | |
Michel Dänzer | 6eb0d3d | 2012-11-30 11:38:24 +0100 | [diff] [blame] | 894 | emit_data->arg_count = 5; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 895 | /* XXX: To optimize, we could use a float or v2f32, if the last bits of |
| 896 | * the writemask are clear */ |
| 897 | emit_data->dst_type = LLVMVectorType( |
| 898 | LLVMFloatTypeInContext(bld_base->base.gallivm->context), |
| 899 | 4); |
| 900 | } |
| 901 | |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 902 | static void build_tex_intrinsic(const struct lp_build_tgsi_action * action, |
| 903 | struct lp_build_tgsi_context * bld_base, |
| 904 | struct lp_build_emit_data * emit_data) |
| 905 | { |
| 906 | struct lp_build_context * base = &bld_base->base; |
| 907 | char intr_name[23]; |
| 908 | |
| 909 | sprintf(intr_name, "%sv%ui32", action->intr_name, |
| 910 | LLVMGetVectorSize(LLVMTypeOf(emit_data->args[1]))); |
| 911 | |
Christian König | 44e3224 | 2013-03-20 12:10:35 +0100 | [diff] [blame] | 912 | emit_data->output[emit_data->chan] = build_intrinsic( |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 913 | base->gallivm->builder, intr_name, emit_data->dst_type, |
Christian König | 44e3224 | 2013-03-20 12:10:35 +0100 | [diff] [blame] | 914 | emit_data->args, emit_data->arg_count, |
| 915 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 916 | } |
| 917 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 918 | static const struct lp_build_tgsi_action tex_action = { |
| 919 | .fetch_args = tex_fetch_args, |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 920 | .emit = build_tex_intrinsic, |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 921 | .intr_name = "llvm.SI.sample." |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 922 | }; |
| 923 | |
Michel Dänzer | 3e20513 | 2012-11-06 17:39:01 +0100 | [diff] [blame] | 924 | static const struct lp_build_tgsi_action txb_action = { |
| 925 | .fetch_args = tex_fetch_args, |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 926 | .emit = build_tex_intrinsic, |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 927 | .intr_name = "llvm.SI.sampleb." |
Michel Dänzer | 3e20513 | 2012-11-06 17:39:01 +0100 | [diff] [blame] | 928 | }; |
| 929 | |
Michel Dänzer | 56ae9be | 2012-11-06 17:41:50 +0100 | [diff] [blame] | 930 | static const struct lp_build_tgsi_action txl_action = { |
| 931 | .fetch_args = tex_fetch_args, |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 932 | .emit = build_tex_intrinsic, |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 933 | .intr_name = "llvm.SI.samplel." |
Michel Dänzer | 56ae9be | 2012-11-06 17:41:50 +0100 | [diff] [blame] | 934 | }; |
| 935 | |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 936 | static void create_meta_data(struct si_shader_context *si_shader_ctx) |
| 937 | { |
| 938 | struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm; |
| 939 | LLVMValueRef args[3]; |
| 940 | |
| 941 | args[0] = LLVMMDStringInContext(gallivm->context, "const", 5); |
| 942 | args[1] = 0; |
| 943 | args[2] = lp_build_const_int32(gallivm, 1); |
| 944 | |
| 945 | si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3); |
| 946 | } |
| 947 | |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 948 | static void create_function(struct si_shader_context *si_shader_ctx) |
| 949 | { |
| 950 | struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm; |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 951 | LLVMTypeRef params[20], f32, i8, i32, v2i32, v3i32; |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 952 | unsigned i; |
| 953 | |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 954 | i8 = LLVMInt8TypeInContext(gallivm->context); |
Christian König | c497321 | 2013-03-05 12:14:02 +0100 | [diff] [blame] | 955 | i32 = LLVMInt32TypeInContext(gallivm->context); |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 956 | f32 = LLVMFloatTypeInContext(gallivm->context); |
| 957 | v2i32 = LLVMVectorType(i32, 2); |
| 958 | v3i32 = LLVMVectorType(i32, 3); |
Christian König | c497321 | 2013-03-05 12:14:02 +0100 | [diff] [blame] | 959 | |
Christian König | f5298b0 | 2013-02-28 14:50:07 +0100 | [diff] [blame] | 960 | params[SI_PARAM_CONST] = LLVMPointerType(LLVMVectorType(i8, 16), CONST_ADDR_SPACE); |
| 961 | params[SI_PARAM_SAMPLER] = params[SI_PARAM_CONST]; |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 962 | params[SI_PARAM_RESOURCE] = LLVMPointerType(LLVMVectorType(i8, 32), CONST_ADDR_SPACE); |
| 963 | |
Christian König | c497321 | 2013-03-05 12:14:02 +0100 | [diff] [blame] | 964 | if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) { |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 965 | params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_SAMPLER]; |
Christian König | e4ed587 | 2013-03-21 18:02:52 +0100 | [diff] [blame^] | 966 | params[SI_PARAM_VERTEX_ID] = i32; |
| 967 | params[SI_PARAM_DUMMY_0] = i32; |
| 968 | params[SI_PARAM_DUMMY_1] = i32; |
| 969 | params[SI_PARAM_INSTANCE_ID] = i32; |
| 970 | radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 8); |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 971 | |
Christian König | c497321 | 2013-03-05 12:14:02 +0100 | [diff] [blame] | 972 | } else { |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 973 | params[SI_PARAM_PRIM_MASK] = i32; |
| 974 | params[SI_PARAM_PERSP_SAMPLE] = v2i32; |
| 975 | params[SI_PARAM_PERSP_CENTER] = v2i32; |
| 976 | params[SI_PARAM_PERSP_CENTROID] = v2i32; |
| 977 | params[SI_PARAM_PERSP_PULL_MODEL] = v3i32; |
| 978 | params[SI_PARAM_LINEAR_SAMPLE] = v2i32; |
| 979 | params[SI_PARAM_LINEAR_CENTER] = v2i32; |
| 980 | params[SI_PARAM_LINEAR_CENTROID] = v2i32; |
| 981 | params[SI_PARAM_LINE_STIPPLE_TEX] = f32; |
| 982 | params[SI_PARAM_POS_X_FLOAT] = f32; |
| 983 | params[SI_PARAM_POS_Y_FLOAT] = f32; |
| 984 | params[SI_PARAM_POS_Z_FLOAT] = f32; |
| 985 | params[SI_PARAM_POS_W_FLOAT] = f32; |
| 986 | params[SI_PARAM_FRONT_FACE] = f32; |
| 987 | params[SI_PARAM_ANCILLARY] = f32; |
| 988 | params[SI_PARAM_SAMPLE_COVERAGE] = f32; |
| 989 | params[SI_PARAM_POS_FIXED_PT] = f32; |
| 990 | radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 20); |
Christian König | c497321 | 2013-03-05 12:14:02 +0100 | [diff] [blame] | 991 | } |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 992 | |
| 993 | radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type); |
| 994 | for (i = SI_PARAM_CONST; i <= SI_PARAM_VERTEX_BUFFER; ++i) { |
| 995 | LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i); |
| 996 | LLVMAddAttribute(P, LLVMInRegAttribute); |
| 997 | } |
| 998 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 999 | |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 1000 | static void preload_constants(struct si_shader_context *si_shader_ctx) |
| 1001 | { |
| 1002 | struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base; |
| 1003 | struct gallivm_state * gallivm = bld_base->base.gallivm; |
| 1004 | const struct tgsi_shader_info * info = bld_base->info; |
| 1005 | |
| 1006 | unsigned i, num_const = info->file_max[TGSI_FILE_CONSTANT] + 1; |
| 1007 | |
| 1008 | LLVMValueRef ptr; |
| 1009 | |
| 1010 | if (num_const == 0) |
| 1011 | return; |
| 1012 | |
| 1013 | /* Allocate space for the constant values */ |
| 1014 | si_shader_ctx->constants = CALLOC(num_const * 4, sizeof(LLVMValueRef)); |
| 1015 | |
| 1016 | /* Load the resource descriptor */ |
| 1017 | ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST); |
| 1018 | si_shader_ctx->const_resource = build_indexed_load(si_shader_ctx, ptr, bld_base->uint_bld.zero); |
| 1019 | |
| 1020 | /* Load the constants, we rely on the code sinking to do the rest */ |
| 1021 | for (i = 0; i < num_const * 4; ++i) { |
| 1022 | LLVMValueRef args[2] = { |
| 1023 | si_shader_ctx->const_resource, |
| 1024 | lp_build_const_int32(gallivm, i * 4) |
| 1025 | }; |
| 1026 | si_shader_ctx->constants[i] = build_intrinsic(gallivm->builder, "llvm.SI.load.const", |
| 1027 | bld_base->base.elem_type, args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
| 1028 | } |
| 1029 | } |
| 1030 | |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 1031 | static void preload_samplers(struct si_shader_context *si_shader_ctx) |
| 1032 | { |
| 1033 | struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base; |
| 1034 | struct gallivm_state * gallivm = bld_base->base.gallivm; |
| 1035 | const struct tgsi_shader_info * info = bld_base->info; |
| 1036 | |
| 1037 | unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1; |
| 1038 | |
| 1039 | LLVMValueRef res_ptr, samp_ptr; |
| 1040 | LLVMValueRef offset; |
| 1041 | |
| 1042 | if (num_samplers == 0) |
| 1043 | return; |
| 1044 | |
| 1045 | /* Allocate space for the values */ |
| 1046 | si_shader_ctx->resources = CALLOC(num_samplers, sizeof(LLVMValueRef)); |
| 1047 | si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef)); |
| 1048 | |
| 1049 | res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE); |
| 1050 | samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER); |
| 1051 | |
| 1052 | /* Load the resources and samplers, we rely on the code sinking to do the rest */ |
| 1053 | for (i = 0; i < num_samplers; ++i) { |
| 1054 | |
| 1055 | /* Resource */ |
| 1056 | offset = lp_build_const_int32(gallivm, i); |
| 1057 | si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset); |
| 1058 | |
| 1059 | /* Sampler */ |
| 1060 | offset = lp_build_const_int32(gallivm, i); |
| 1061 | si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset); |
| 1062 | } |
| 1063 | } |
| 1064 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1065 | int si_pipe_shader_create( |
| 1066 | struct pipe_context *ctx, |
Michel Dänzer | 44ef033 | 2012-10-05 16:59:10 +0200 | [diff] [blame] | 1067 | struct si_pipe_shader *shader, |
| 1068 | struct si_shader_key key) |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1069 | { |
| 1070 | struct r600_context *rctx = (struct r600_context*)ctx; |
Michel Dänzer | d1e40b3 | 2012-08-23 17:10:37 +0200 | [diff] [blame] | 1071 | struct si_pipe_shader_selector *sel = shader->selector; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1072 | struct si_shader_context si_shader_ctx; |
| 1073 | struct tgsi_shader_info shader_info; |
| 1074 | struct lp_build_tgsi_context * bld_base; |
| 1075 | LLVMModuleRef mod; |
| 1076 | unsigned char * inst_bytes; |
| 1077 | unsigned inst_byte_count; |
| 1078 | unsigned i; |
Christian König | d51b9b7 | 2012-07-24 18:50:49 +0200 | [diff] [blame] | 1079 | uint32_t *ptr; |
Michel Dänzer | 4c4ef9c | 2012-06-07 19:30:47 +0200 | [diff] [blame] | 1080 | bool dump; |
| 1081 | |
| 1082 | dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1083 | |
Michel Dänzer | 82e38ac | 2012-09-27 16:39:26 +0200 | [diff] [blame] | 1084 | assert(shader->shader.noutput == 0); |
| 1085 | assert(shader->shader.ninterp == 0); |
| 1086 | assert(shader->shader.ninput == 0); |
| 1087 | |
Michel Dänzer | cfebaf9 | 2012-08-31 19:04:08 +0200 | [diff] [blame] | 1088 | memset(&si_shader_ctx, 0, sizeof(si_shader_ctx)); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1089 | radeon_llvm_context_init(&si_shader_ctx.radeon_bld); |
| 1090 | bld_base = &si_shader_ctx.radeon_bld.soa.bld_base; |
| 1091 | |
Michel Dänzer | d1e40b3 | 2012-08-23 17:10:37 +0200 | [diff] [blame] | 1092 | tgsi_scan_shader(sel->tokens, &shader_info); |
Michel Dänzer | e44dfd4 | 2012-11-07 17:33:08 +0100 | [diff] [blame] | 1093 | shader->shader.uses_kill = shader_info.uses_kill; |
Christian König | e4ed587 | 2013-03-21 18:02:52 +0100 | [diff] [blame^] | 1094 | shader->shader.uses_instanceid = shader_info.uses_instanceid; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1095 | bld_base->info = &shader_info; |
| 1096 | bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1097 | bld_base->emit_epilogue = si_llvm_emit_epilogue; |
| 1098 | |
| 1099 | bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action; |
Michel Dänzer | 3e20513 | 2012-11-06 17:39:01 +0100 | [diff] [blame] | 1100 | bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action; |
Michel Dänzer | 56ae9be | 2012-11-06 17:41:50 +0100 | [diff] [blame] | 1101 | bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action; |
Michel Dänzer | c2bae6b | 2012-08-02 17:19:22 +0200 | [diff] [blame] | 1102 | bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1103 | |
| 1104 | si_shader_ctx.radeon_bld.load_input = declare_input; |
Christian König | e4ed587 | 2013-03-21 18:02:52 +0100 | [diff] [blame^] | 1105 | si_shader_ctx.radeon_bld.load_system_value = declare_system_value; |
Michel Dänzer | d1e40b3 | 2012-08-23 17:10:37 +0200 | [diff] [blame] | 1106 | si_shader_ctx.tokens = sel->tokens; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1107 | tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens); |
| 1108 | si_shader_ctx.shader = shader; |
Michel Dänzer | 44ef033 | 2012-10-05 16:59:10 +0200 | [diff] [blame] | 1109 | si_shader_ctx.key = key; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1110 | si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor; |
| 1111 | si_shader_ctx.rctx = rctx; |
| 1112 | |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 1113 | create_meta_data(&si_shader_ctx); |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 1114 | create_function(&si_shader_ctx); |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 1115 | preload_constants(&si_shader_ctx); |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 1116 | preload_samplers(&si_shader_ctx); |
Christian König | b8f4ca3 | 2013-03-04 15:35:30 +0100 | [diff] [blame] | 1117 | |
Christian König | 835098a | 2012-07-17 21:28:10 +0200 | [diff] [blame] | 1118 | shader->shader.nr_cbufs = rctx->framebuffer.nr_cbufs; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1119 | |
Tom Stellard | 185fc9a | 2012-07-12 10:40:47 -0400 | [diff] [blame] | 1120 | /* Dump TGSI code before doing TGSI->LLVM conversion in case the |
| 1121 | * conversion fails. */ |
| 1122 | if (dump) { |
Michel Dänzer | d1e40b3 | 2012-08-23 17:10:37 +0200 | [diff] [blame] | 1123 | tgsi_dump(sel->tokens, 0); |
Tom Stellard | 185fc9a | 2012-07-12 10:40:47 -0400 | [diff] [blame] | 1124 | } |
| 1125 | |
Michel Dänzer | d1e40b3 | 2012-08-23 17:10:37 +0200 | [diff] [blame] | 1126 | if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) { |
Michel Dänzer | 82cd9c0 | 2012-08-08 15:35:42 +0200 | [diff] [blame] | 1127 | fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n"); |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 1128 | FREE(si_shader_ctx.constants); |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 1129 | FREE(si_shader_ctx.resources); |
| 1130 | FREE(si_shader_ctx.samplers); |
Michel Dänzer | 82cd9c0 | 2012-08-08 15:35:42 +0200 | [diff] [blame] | 1131 | return -EINVAL; |
| 1132 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1133 | |
| 1134 | radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld); |
| 1135 | |
| 1136 | mod = bld_base->base.gallivm->module; |
Michel Dänzer | 4c4ef9c | 2012-06-07 19:30:47 +0200 | [diff] [blame] | 1137 | if (dump) { |
Michel Dänzer | 4c4ef9c | 2012-06-07 19:30:47 +0200 | [diff] [blame] | 1138 | LLVMDumpModule(mod); |
| 1139 | } |
| 1140 | radeon_llvm_compile(mod, &inst_bytes, &inst_byte_count, "SI", dump); |
| 1141 | if (dump) { |
| 1142 | fprintf(stderr, "SI CODE:\n"); |
| 1143 | for (i = 0; i < inst_byte_count; i+=4 ) { |
| 1144 | fprintf(stderr, "%02x%02x%02x%02x\n", inst_bytes[i + 3], |
| 1145 | inst_bytes[i + 2], inst_bytes[i + 1], |
| 1146 | inst_bytes[i]); |
| 1147 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | shader->num_sgprs = util_le32_to_cpu(*(uint32_t*)inst_bytes); |
| 1151 | shader->num_vgprs = util_le32_to_cpu(*(uint32_t*)(inst_bytes + 4)); |
| 1152 | shader->spi_ps_input_ena = util_le32_to_cpu(*(uint32_t*)(inst_bytes + 8)); |
| 1153 | |
Michel Dänzer | 4b64fa2 | 2012-08-15 18:22:46 +0200 | [diff] [blame] | 1154 | radeon_llvm_dispose(&si_shader_ctx.radeon_bld); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1155 | tgsi_parse_free(&si_shader_ctx.parse); |
| 1156 | |
| 1157 | /* copy new shader */ |
Christian König | d51b9b7 | 2012-07-24 18:50:49 +0200 | [diff] [blame] | 1158 | si_resource_reference(&shader->bo, NULL); |
| 1159 | shader->bo = si_resource_create_custom(ctx->screen, PIPE_USAGE_IMMUTABLE, |
| 1160 | inst_byte_count - 12); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1161 | if (shader->bo == NULL) { |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 1162 | FREE(si_shader_ctx.constants); |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 1163 | FREE(si_shader_ctx.resources); |
| 1164 | FREE(si_shader_ctx.samplers); |
Christian König | d51b9b7 | 2012-07-24 18:50:49 +0200 | [diff] [blame] | 1165 | return -ENOMEM; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1166 | } |
| 1167 | |
Christian König | d51b9b7 | 2012-07-24 18:50:49 +0200 | [diff] [blame] | 1168 | ptr = (uint32_t*)rctx->ws->buffer_map(shader->bo->cs_buf, rctx->cs, PIPE_TRANSFER_WRITE); |
| 1169 | if (0 /*R600_BIG_ENDIAN*/) { |
| 1170 | for (i = 0; i < (inst_byte_count-12)/4; ++i) { |
| 1171 | ptr[i] = util_bswap32(*(uint32_t*)(inst_bytes+12 + i*4)); |
| 1172 | } |
| 1173 | } else { |
| 1174 | memcpy(ptr, inst_bytes + 12, inst_byte_count - 12); |
| 1175 | } |
| 1176 | rctx->ws->buffer_unmap(shader->bo->cs_buf); |
| 1177 | |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 1178 | FREE(si_shader_ctx.constants); |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 1179 | FREE(si_shader_ctx.resources); |
| 1180 | FREE(si_shader_ctx.samplers); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1181 | free(inst_bytes); |
| 1182 | |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
| 1186 | void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader) |
| 1187 | { |
Christian König | fe41287 | 2012-07-24 18:47:19 +0200 | [diff] [blame] | 1188 | si_resource_reference(&shader->bo, NULL); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1189 | } |