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" |
Marek Olšák | 8d03d92 | 2013-09-01 23:59:06 +0200 | [diff] [blame] | 37 | #include "gallivm/lp_bld_flow.h" |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 38 | #include "radeon_llvm.h" |
Tom Stellard | 509ddb0 | 2012-04-16 17:48:44 -0400 | [diff] [blame] | 39 | #include "radeon_llvm_emit.h" |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 40 | #include "util/u_memory.h" |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 41 | #include "tgsi/tgsi_info.h" |
| 42 | #include "tgsi/tgsi_parse.h" |
| 43 | #include "tgsi/tgsi_scan.h" |
Michel Dänzer | beaa5eb | 2013-05-24 13:23:26 +0100 | [diff] [blame] | 44 | #include "tgsi/tgsi_util.h" |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 45 | #include "tgsi/tgsi_dump.h" |
| 46 | |
| 47 | #include "radeonsi_pipe.h" |
| 48 | #include "radeonsi_shader.h" |
Christian König | f67fae0 | 2012-07-17 23:43:00 +0200 | [diff] [blame] | 49 | #include "si_state.h" |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 50 | #include "sid.h" |
| 51 | |
| 52 | #include <assert.h> |
| 53 | #include <errno.h> |
| 54 | #include <stdio.h> |
| 55 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 56 | struct si_shader_context |
| 57 | { |
| 58 | struct radeon_llvm_context radeon_bld; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 59 | struct tgsi_parse_context parse; |
| 60 | struct tgsi_token * tokens; |
| 61 | struct si_pipe_shader *shader; |
| 62 | unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */ |
Marek Olšák | 8d03d92 | 2013-09-01 23:59:06 +0200 | [diff] [blame] | 63 | int param_streamout_config; |
| 64 | int param_streamout_write_index; |
| 65 | int param_streamout_offset[4]; |
| 66 | int param_vertex_id; |
| 67 | int param_instance_id; |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 68 | LLVMValueRef const_md; |
Marek Olšák | 2fd4200 | 2013-10-25 11:45:47 +0200 | [diff] [blame] | 69 | LLVMValueRef const_resource[NUM_CONST_BUFFERS]; |
Michel Dänzer | a06ee5a | 2013-06-19 18:14:01 +0200 | [diff] [blame] | 70 | #if HAVE_LLVM >= 0x0304 |
| 71 | LLVMValueRef ddxy_lds; |
| 72 | #endif |
Marek Olšák | 2fd4200 | 2013-10-25 11:45:47 +0200 | [diff] [blame] | 73 | LLVMValueRef *constants[NUM_CONST_BUFFERS]; |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 74 | LLVMValueRef *resources; |
| 75 | LLVMValueRef *samplers; |
Marek Olšák | 8d03d92 | 2013-09-01 23:59:06 +0200 | [diff] [blame] | 76 | LLVMValueRef so_buffers[4]; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | static struct si_shader_context * si_shader_context( |
| 80 | struct lp_build_tgsi_context * bld_base) |
| 81 | { |
| 82 | return (struct si_shader_context *)bld_base; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | #define PERSPECTIVE_BASE 0 |
| 87 | #define LINEAR_BASE 9 |
| 88 | |
| 89 | #define SAMPLE_OFFSET 0 |
| 90 | #define CENTER_OFFSET 2 |
| 91 | #define CENTROID_OFSET 4 |
| 92 | |
| 93 | #define USE_SGPR_MAX_SUFFIX_LEN 5 |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 94 | #define CONST_ADDR_SPACE 2 |
Michel Dänzer | a06ee5a | 2013-06-19 18:14:01 +0200 | [diff] [blame] | 95 | #define LOCAL_ADDR_SPACE 3 |
Tom Stellard | 89ece08 | 2012-05-29 11:36:29 -0400 | [diff] [blame] | 96 | #define USER_SGPR_ADDR_SPACE 8 |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 97 | |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 98 | /** |
| 99 | * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad |
| 100 | * |
| 101 | * @param offset The offset parameter specifies the number of |
| 102 | * elements to offset, not the number of bytes or dwords. An element is the |
| 103 | * the type pointed to by the base_ptr parameter (e.g. int is the element of |
| 104 | * an int* pointer) |
| 105 | * |
| 106 | * When LLVM lowers the load instruction, it will convert the element offset |
| 107 | * into a dword offset automatically. |
| 108 | * |
| 109 | */ |
| 110 | static LLVMValueRef build_indexed_load( |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 111 | struct si_shader_context * si_shader_ctx, |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 112 | LLVMValueRef base_ptr, |
| 113 | LLVMValueRef offset) |
| 114 | { |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 115 | 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] | 116 | |
Vincent Lejeune | 6e51c2a | 2013-10-05 16:04:48 +0200 | [diff] [blame] | 117 | LLVMValueRef indices[2] = { |
| 118 | LLVMConstInt(LLVMInt64TypeInContext(base->gallivm->context), 0, false), |
| 119 | offset |
| 120 | }; |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 121 | LLVMValueRef computed_ptr = LLVMBuildGEP( |
Vincent Lejeune | 6e51c2a | 2013-10-05 16:04:48 +0200 | [diff] [blame] | 122 | base->gallivm->builder, base_ptr, indices, 2, ""); |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 123 | |
| 124 | LLVMValueRef result = LLVMBuildLoad(base->gallivm->builder, computed_ptr, ""); |
| 125 | LLVMSetMetadata(result, 1, si_shader_ctx->const_md); |
| 126 | return result; |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 127 | } |
| 128 | |
Marek Olšák | f317ce5 | 2013-09-05 15:39:57 +0200 | [diff] [blame] | 129 | static LLVMValueRef get_instance_index_for_fetch( |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 130 | struct radeon_llvm_context * radeon_bld, |
| 131 | unsigned divisor) |
| 132 | { |
Marek Olšák | 8d03d92 | 2013-09-01 23:59:06 +0200 | [diff] [blame] | 133 | struct si_shader_context *si_shader_ctx = |
| 134 | si_shader_context(&radeon_bld->soa.bld_base); |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 135 | struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm; |
| 136 | |
Marek Olšák | 8d03d92 | 2013-09-01 23:59:06 +0200 | [diff] [blame] | 137 | LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn, |
| 138 | si_shader_ctx->param_instance_id); |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 139 | result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam( |
| 140 | radeon_bld->main_fn, SI_PARAM_START_INSTANCE), ""); |
| 141 | |
| 142 | if (divisor > 1) |
| 143 | result = LLVMBuildUDiv(gallivm->builder, result, |
| 144 | lp_build_const_int32(gallivm, divisor), ""); |
| 145 | |
| 146 | return result; |
| 147 | } |
| 148 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 149 | static void declare_input_vs( |
| 150 | struct si_shader_context * si_shader_ctx, |
| 151 | unsigned input_index, |
| 152 | const struct tgsi_full_declaration *decl) |
| 153 | { |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 154 | struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base; |
| 155 | unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index]; |
| 156 | |
| 157 | unsigned chan; |
| 158 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 159 | LLVMValueRef t_list_ptr; |
| 160 | LLVMValueRef t_offset; |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 161 | LLVMValueRef t_list; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 162 | LLVMValueRef attribute_offset; |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 163 | LLVMValueRef buffer_index; |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 164 | LLVMValueRef args[3]; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 165 | LLVMTypeRef vec4_type; |
| 166 | LLVMValueRef input; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 167 | |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 168 | /* Load the T list */ |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 169 | 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] | 170 | |
Christian König | b15e3ae | 2012-07-25 11:22:59 +0200 | [diff] [blame] | 171 | t_offset = lp_build_const_int32(base->gallivm, input_index); |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 172 | |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 173 | 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] | 174 | |
| 175 | /* Build the attribute offset */ |
Christian König | b15e3ae | 2012-07-25 11:22:59 +0200 | [diff] [blame] | 176 | attribute_offset = lp_build_const_int32(base->gallivm, 0); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 177 | |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 178 | if (divisor) { |
| 179 | /* Build index from instance ID, start instance and divisor */ |
| 180 | si_shader_ctx->shader->shader.uses_instanceid = true; |
Marek Olšák | f317ce5 | 2013-09-05 15:39:57 +0200 | [diff] [blame] | 181 | buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor); |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 182 | } else { |
| 183 | /* Load the buffer index, which is always stored in VGPR0 |
| 184 | * for Vertex Shaders */ |
Marek Olšák | 8d03d92 | 2013-09-01 23:59:06 +0200 | [diff] [blame] | 185 | buffer_index = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, |
| 186 | si_shader_ctx->param_vertex_id); |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 187 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 188 | |
| 189 | vec4_type = LLVMVectorType(base->elem_type, 4); |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 190 | args[0] = t_list; |
| 191 | args[1] = attribute_offset; |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 192 | args[2] = buffer_index; |
Christian König | 44e3224 | 2013-03-20 12:10:35 +0100 | [diff] [blame] | 193 | input = build_intrinsic(base->gallivm->builder, |
| 194 | "llvm.SI.vs.load.input", vec4_type, args, 3, |
| 195 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 196 | |
| 197 | /* Break up the vec4 into individual components */ |
| 198 | for (chan = 0; chan < 4; chan++) { |
| 199 | LLVMValueRef llvm_chan = lp_build_const_int32(base->gallivm, chan); |
| 200 | /* XXX: Use a helper function for this. There is one in |
| 201 | * tgsi_llvm.c. */ |
| 202 | si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] = |
| 203 | LLVMBuildExtractElement(base->gallivm->builder, |
| 204 | input, llvm_chan, ""); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | static void declare_input_fs( |
| 209 | struct si_shader_context * si_shader_ctx, |
| 210 | unsigned input_index, |
| 211 | const struct tgsi_full_declaration *decl) |
| 212 | { |
Michel Dänzer | c3db19e | 2012-09-27 20:01:33 +0200 | [diff] [blame] | 213 | struct si_shader *shader = &si_shader_ctx->shader->shader; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 214 | struct lp_build_context * base = |
| 215 | &si_shader_ctx->radeon_bld.soa.bld_base.base; |
Michel Dänzer | 237cb07 | 2013-08-21 18:00:35 +0200 | [diff] [blame] | 216 | struct lp_build_context *uint = |
| 217 | &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 218 | struct gallivm_state * gallivm = base->gallivm; |
Tom Stellard | 0fb1e68 | 2012-09-06 16:18:11 -0400 | [diff] [blame] | 219 | LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context); |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 220 | LLVMValueRef main_fn = si_shader_ctx->radeon_bld.main_fn; |
| 221 | |
| 222 | LLVMValueRef interp_param; |
| 223 | const char * intr_name; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 224 | |
| 225 | /* This value is: |
| 226 | * [15:0] NewPrimMask (Bit mask for each quad. It is set it the |
| 227 | * quad begins a new primitive. Bit 0 always needs |
| 228 | * to be unset) |
| 229 | * [32:16] ParamOffset |
| 230 | * |
| 231 | */ |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 232 | 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] | 233 | LLVMValueRef attr_number; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 234 | |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 235 | unsigned chan; |
| 236 | |
Tom Stellard | 0fb1e68 | 2012-09-06 16:18:11 -0400 | [diff] [blame] | 237 | if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) { |
| 238 | for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) { |
Tom Stellard | 0fb1e68 | 2012-09-06 16:18:11 -0400 | [diff] [blame] | 239 | unsigned soa_index = |
| 240 | radeon_llvm_reg_index_soa(input_index, chan); |
Tom Stellard | 0fb1e68 | 2012-09-06 16:18:11 -0400 | [diff] [blame] | 241 | si_shader_ctx->radeon_bld.inputs[soa_index] = |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 242 | LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan); |
Michel Dänzer | 954bc4a | 2013-02-13 15:57:23 +0100 | [diff] [blame] | 243 | |
| 244 | if (chan == 3) |
| 245 | /* RCP for fragcoord.w */ |
| 246 | si_shader_ctx->radeon_bld.inputs[soa_index] = |
| 247 | LLVMBuildFDiv(gallivm->builder, |
| 248 | lp_build_const_float(gallivm, 1.0f), |
| 249 | si_shader_ctx->radeon_bld.inputs[soa_index], |
| 250 | ""); |
Tom Stellard | 0fb1e68 | 2012-09-06 16:18:11 -0400 | [diff] [blame] | 251 | } |
| 252 | return; |
| 253 | } |
| 254 | |
Michel Dänzer | 97078b1 | 2012-09-25 12:41:31 +0200 | [diff] [blame] | 255 | if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) { |
| 256 | LLVMValueRef face, is_face_positive; |
| 257 | |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 258 | face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE); |
| 259 | |
Michel Dänzer | 97078b1 | 2012-09-25 12:41:31 +0200 | [diff] [blame] | 260 | is_face_positive = LLVMBuildFCmp(gallivm->builder, |
| 261 | LLVMRealUGT, face, |
| 262 | lp_build_const_float(gallivm, 0.0f), |
| 263 | ""); |
| 264 | |
| 265 | si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] = |
| 266 | LLVMBuildSelect(gallivm->builder, |
| 267 | is_face_positive, |
| 268 | lp_build_const_float(gallivm, 1.0f), |
| 269 | lp_build_const_float(gallivm, 0.0f), |
| 270 | ""); |
| 271 | si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] = |
| 272 | si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] = |
| 273 | lp_build_const_float(gallivm, 0.0f); |
| 274 | si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] = |
| 275 | lp_build_const_float(gallivm, 1.0f); |
| 276 | |
| 277 | return; |
| 278 | } |
| 279 | |
Michel Dänzer | c3db19e | 2012-09-27 20:01:33 +0200 | [diff] [blame] | 280 | shader->input[input_index].param_offset = shader->ninterp++; |
| 281 | attr_number = lp_build_const_int32(gallivm, |
| 282 | shader->input[input_index].param_offset); |
| 283 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 284 | /* XXX: Handle all possible interpolation modes */ |
Francisco Jerez | 1279923 | 2012-04-30 18:27:52 +0200 | [diff] [blame] | 285 | switch (decl->Interp.Interpolate) { |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 286 | case TGSI_INTERPOLATE_COLOR: |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 287 | if (si_shader_ctx->shader->key.ps.flatshade) { |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 288 | interp_param = 0; |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame] | 289 | } else { |
| 290 | if (decl->Interp.Centroid) |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 291 | interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID); |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame] | 292 | else |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 293 | interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER); |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame] | 294 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 295 | break; |
| 296 | case TGSI_INTERPOLATE_CONSTANT: |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 297 | interp_param = 0; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 298 | break; |
| 299 | case TGSI_INTERPOLATE_LINEAR: |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame] | 300 | if (decl->Interp.Centroid) |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 301 | interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID); |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame] | 302 | else |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 303 | interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER); |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame] | 304 | break; |
| 305 | case TGSI_INTERPOLATE_PERSPECTIVE: |
| 306 | if (decl->Interp.Centroid) |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 307 | interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID); |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame] | 308 | else |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 309 | interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 310 | break; |
| 311 | default: |
| 312 | fprintf(stderr, "Warning: Unhandled interpolation mode.\n"); |
| 313 | return; |
| 314 | } |
| 315 | |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 316 | intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant"; |
| 317 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 318 | /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */ |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 319 | if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR && |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 320 | si_shader_ctx->shader->key.ps.color_two_side) { |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 321 | LLVMValueRef args[4]; |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 322 | LLVMValueRef face, is_face_positive; |
| 323 | LLVMValueRef back_attr_number = |
| 324 | lp_build_const_int32(gallivm, |
| 325 | shader->input[input_index].param_offset + 1); |
| 326 | |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 327 | face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE); |
| 328 | |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 329 | is_face_positive = LLVMBuildFCmp(gallivm->builder, |
| 330 | LLVMRealUGT, face, |
| 331 | lp_build_const_float(gallivm, 0.0f), |
| 332 | ""); |
| 333 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 334 | args[2] = params; |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 335 | args[3] = interp_param; |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 336 | for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) { |
| 337 | LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan); |
| 338 | unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan); |
| 339 | LLVMValueRef front, back; |
| 340 | |
| 341 | args[0] = llvm_chan; |
| 342 | args[1] = attr_number; |
| 343 | front = build_intrinsic(base->gallivm->builder, intr_name, |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 344 | input_type, args, args[3] ? 4 : 3, |
Christian König | 44e3224 | 2013-03-20 12:10:35 +0100 | [diff] [blame] | 345 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 346 | |
| 347 | args[1] = back_attr_number; |
| 348 | back = build_intrinsic(base->gallivm->builder, intr_name, |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 349 | input_type, args, args[3] ? 4 : 3, |
Christian König | 44e3224 | 2013-03-20 12:10:35 +0100 | [diff] [blame] | 350 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 351 | |
| 352 | si_shader_ctx->radeon_bld.inputs[soa_index] = |
| 353 | LLVMBuildSelect(gallivm->builder, |
| 354 | is_face_positive, |
| 355 | front, |
| 356 | back, |
| 357 | ""); |
| 358 | } |
| 359 | |
| 360 | shader->ninterp++; |
Michel Dänzer | 237cb07 | 2013-08-21 18:00:35 +0200 | [diff] [blame] | 361 | } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) { |
| 362 | LLVMValueRef args[4]; |
| 363 | |
| 364 | args[0] = uint->zero; |
| 365 | args[1] = attr_number; |
| 366 | args[2] = params; |
| 367 | args[3] = interp_param; |
| 368 | si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] = |
| 369 | build_intrinsic(base->gallivm->builder, intr_name, |
| 370 | input_type, args, args[3] ? 4 : 3, |
| 371 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
| 372 | si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] = |
| 373 | si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] = |
| 374 | lp_build_const_float(gallivm, 0.0f); |
| 375 | si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] = |
| 376 | lp_build_const_float(gallivm, 1.0f); |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 377 | } else { |
| 378 | for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) { |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 379 | LLVMValueRef args[4]; |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 380 | LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan); |
| 381 | unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan); |
| 382 | args[0] = llvm_chan; |
| 383 | args[1] = attr_number; |
| 384 | args[2] = params; |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 385 | args[3] = interp_param; |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 386 | si_shader_ctx->radeon_bld.inputs[soa_index] = |
| 387 | build_intrinsic(base->gallivm->builder, intr_name, |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 388 | input_type, args, args[3] ? 4 : 3, |
Christian König | 44e3224 | 2013-03-20 12:10:35 +0100 | [diff] [blame] | 389 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 390 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
| 394 | static void declare_input( |
| 395 | struct radeon_llvm_context * radeon_bld, |
| 396 | unsigned input_index, |
| 397 | const struct tgsi_full_declaration *decl) |
| 398 | { |
| 399 | struct si_shader_context * si_shader_ctx = |
| 400 | si_shader_context(&radeon_bld->soa.bld_base); |
| 401 | if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) { |
| 402 | declare_input_vs(si_shader_ctx, input_index, decl); |
| 403 | } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) { |
| 404 | declare_input_fs(si_shader_ctx, input_index, decl); |
| 405 | } else { |
| 406 | fprintf(stderr, "Warning: Unsupported shader type,\n"); |
| 407 | } |
| 408 | } |
| 409 | |
Christian König | e4ed587 | 2013-03-21 18:02:52 +0100 | [diff] [blame] | 410 | static void declare_system_value( |
| 411 | struct radeon_llvm_context * radeon_bld, |
| 412 | unsigned index, |
| 413 | const struct tgsi_full_declaration *decl) |
| 414 | { |
Marek Olšák | 8d03d92 | 2013-09-01 23:59:06 +0200 | [diff] [blame] | 415 | struct si_shader_context *si_shader_ctx = |
| 416 | si_shader_context(&radeon_bld->soa.bld_base); |
Christian König | e4ed587 | 2013-03-21 18:02:52 +0100 | [diff] [blame] | 417 | LLVMValueRef value = 0; |
| 418 | |
| 419 | switch (decl->Semantic.Name) { |
| 420 | case TGSI_SEMANTIC_INSTANCEID: |
Marek Olšák | f317ce5 | 2013-09-05 15:39:57 +0200 | [diff] [blame] | 421 | value = LLVMGetParam(radeon_bld->main_fn, |
| 422 | si_shader_ctx->param_instance_id); |
Christian König | e4ed587 | 2013-03-21 18:02:52 +0100 | [diff] [blame] | 423 | break; |
| 424 | |
| 425 | case TGSI_SEMANTIC_VERTEXID: |
Marek Olšák | 8d03d92 | 2013-09-01 23:59:06 +0200 | [diff] [blame] | 426 | value = LLVMGetParam(radeon_bld->main_fn, |
| 427 | si_shader_ctx->param_vertex_id); |
Christian König | e4ed587 | 2013-03-21 18:02:52 +0100 | [diff] [blame] | 428 | break; |
| 429 | |
| 430 | default: |
| 431 | assert(!"unknown system value"); |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | radeon_bld->system_values[index] = value; |
| 436 | } |
| 437 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 438 | static LLVMValueRef fetch_constant( |
| 439 | struct lp_build_tgsi_context * bld_base, |
| 440 | const struct tgsi_full_src_register *reg, |
| 441 | enum tgsi_opcode_type type, |
| 442 | unsigned swizzle) |
| 443 | { |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 444 | struct si_shader_context *si_shader_ctx = si_shader_context(bld_base); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 445 | struct lp_build_context * base = &bld_base->base; |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 446 | const struct tgsi_ind_register *ireg = ®->Indirect; |
Marek Olšák | 2fd4200 | 2013-10-25 11:45:47 +0200 | [diff] [blame] | 447 | unsigned buf, idx; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 448 | |
Christian König | f5298b0 | 2013-02-28 14:50:07 +0100 | [diff] [blame] | 449 | LLVMValueRef args[2]; |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 450 | LLVMValueRef addr; |
Christian König | f5298b0 | 2013-02-28 14:50:07 +0100 | [diff] [blame] | 451 | LLVMValueRef result; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 452 | |
Christian König | 8514f5a | 2013-02-04 17:46:42 +0100 | [diff] [blame] | 453 | if (swizzle == LP_CHAN_ALL) { |
| 454 | unsigned chan; |
| 455 | LLVMValueRef values[4]; |
| 456 | for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) |
| 457 | values[chan] = fetch_constant(bld_base, reg, type, chan); |
| 458 | |
| 459 | return lp_build_gather_values(bld_base->base.gallivm, values, 4); |
| 460 | } |
| 461 | |
Marek Olšák | 2fd4200 | 2013-10-25 11:45:47 +0200 | [diff] [blame] | 462 | buf = reg->Register.Dimension ? reg->Dimension.Index : 0; |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 463 | idx = reg->Register.Index * 4 + swizzle; |
Christian König | f5298b0 | 2013-02-28 14:50:07 +0100 | [diff] [blame] | 464 | |
Marek Olšák | 2fd4200 | 2013-10-25 11:45:47 +0200 | [diff] [blame] | 465 | if (!reg->Register.Indirect) |
| 466 | return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]); |
| 467 | |
| 468 | args[0] = si_shader_ctx->const_resource[buf]; |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 469 | args[1] = lp_build_const_int32(base->gallivm, idx * 4); |
| 470 | addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle]; |
| 471 | addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg"); |
| 472 | addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16); |
| 473 | 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] | 474 | |
Christian König | f5298b0 | 2013-02-28 14:50:07 +0100 | [diff] [blame] | 475 | 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] | 476 | args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 477 | |
Christian König | f5298b0 | 2013-02-28 14:50:07 +0100 | [diff] [blame] | 478 | return bitcast(bld_base, type, result); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 479 | } |
| 480 | |
Michel Dänzer | 26c7139 | 2012-08-24 12:03:11 +0200 | [diff] [blame] | 481 | /* Initialize arguments for the shader export intrinsic */ |
| 482 | static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base, |
| 483 | struct tgsi_full_declaration *d, |
| 484 | unsigned index, |
| 485 | unsigned target, |
| 486 | LLVMValueRef *args) |
| 487 | { |
| 488 | struct si_shader_context *si_shader_ctx = si_shader_context(bld_base); |
| 489 | struct lp_build_context *uint = |
| 490 | &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld; |
| 491 | struct lp_build_context *base = &bld_base->base; |
| 492 | unsigned compressed = 0; |
| 493 | unsigned chan; |
| 494 | |
Michel Dänzer | f402acd | 2012-08-22 18:15:36 +0200 | [diff] [blame] | 495 | if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) { |
| 496 | int cbuf = target - V_008DFC_SQ_EXP_MRT; |
| 497 | |
| 498 | if (cbuf >= 0 && cbuf < 8) { |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 499 | compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1; |
Michel Dänzer | 1ace200 | 2012-12-21 15:39:26 +0100 | [diff] [blame] | 500 | |
| 501 | if (compressed) |
| 502 | si_shader_ctx->shader->spi_shader_col_format |= |
| 503 | V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf); |
| 504 | else |
| 505 | si_shader_ctx->shader->spi_shader_col_format |= |
| 506 | V_028714_SPI_SHADER_32_ABGR << (4 * cbuf); |
Michel Dänzer | e369f40 | 2013-04-30 16:34:10 +0200 | [diff] [blame] | 507 | |
| 508 | si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf); |
Michel Dänzer | f402acd | 2012-08-22 18:15:36 +0200 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
| 512 | if (compressed) { |
| 513 | /* Pixel shader needs to pack output values before export */ |
| 514 | for (chan = 0; chan < 2; chan++ ) { |
| 515 | LLVMValueRef *out_ptr = |
| 516 | si_shader_ctx->radeon_bld.soa.outputs[index]; |
| 517 | args[0] = LLVMBuildLoad(base->gallivm->builder, |
| 518 | out_ptr[2 * chan], ""); |
| 519 | args[1] = LLVMBuildLoad(base->gallivm->builder, |
| 520 | out_ptr[2 * chan + 1], ""); |
| 521 | args[chan + 5] = |
| 522 | build_intrinsic(base->gallivm->builder, |
| 523 | "llvm.SI.packf16", |
| 524 | LLVMInt32TypeInContext(base->gallivm->context), |
| 525 | args, 2, |
Christian König | e4188ee | 2013-02-27 22:39:26 +0100 | [diff] [blame] | 526 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
Michel Dänzer | 8b6aec6 | 2012-11-27 19:53:58 +0100 | [diff] [blame] | 527 | args[chan + 7] = args[chan + 5] = |
| 528 | LLVMBuildBitCast(base->gallivm->builder, |
| 529 | args[chan + 5], |
| 530 | LLVMFloatTypeInContext(base->gallivm->context), |
| 531 | ""); |
Michel Dänzer | f402acd | 2012-08-22 18:15:36 +0200 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /* Set COMPR flag */ |
| 535 | args[4] = uint->one; |
| 536 | } else { |
| 537 | for (chan = 0; chan < 4; chan++ ) { |
| 538 | LLVMValueRef out_ptr = |
| 539 | si_shader_ctx->radeon_bld.soa.outputs[index][chan]; |
| 540 | /* +5 because the first output value will be |
| 541 | * the 6th argument to the intrinsic. */ |
| 542 | args[chan + 5] = LLVMBuildLoad(base->gallivm->builder, |
| 543 | out_ptr, ""); |
| 544 | } |
| 545 | |
| 546 | /* Clear COMPR flag */ |
| 547 | args[4] = uint->zero; |
Michel Dänzer | 26c7139 | 2012-08-24 12:03:11 +0200 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | /* XXX: This controls which components of the output |
| 551 | * registers actually get exported. (e.g bit 0 means export |
| 552 | * X component, bit 1 means export Y component, etc.) I'm |
| 553 | * hard coding this to 0xf for now. In the future, we might |
| 554 | * want to do something else. */ |
| 555 | args[0] = lp_build_const_int32(base->gallivm, 0xf); |
| 556 | |
| 557 | /* Specify whether the EXEC mask represents the valid mask */ |
| 558 | args[1] = uint->zero; |
| 559 | |
| 560 | /* Specify whether this is the last export */ |
| 561 | args[2] = uint->zero; |
| 562 | |
| 563 | /* Specify the target we are exporting */ |
| 564 | args[3] = lp_build_const_int32(base->gallivm, target); |
| 565 | |
Michel Dänzer | 26c7139 | 2012-08-24 12:03:11 +0200 | [diff] [blame] | 566 | /* XXX: We probably need to keep track of the output |
| 567 | * values, so we know what we are passing to the next |
| 568 | * stage. */ |
| 569 | } |
| 570 | |
Michel Dänzer | 7708a86 | 2012-11-02 15:57:30 +0100 | [diff] [blame] | 571 | static void si_alpha_test(struct lp_build_tgsi_context *bld_base, |
| 572 | unsigned index) |
| 573 | { |
| 574 | struct si_shader_context *si_shader_ctx = si_shader_context(bld_base); |
| 575 | struct gallivm_state *gallivm = bld_base->base.gallivm; |
| 576 | |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 577 | 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] | 578 | LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][3]; |
Vadim Girlin | 453ea2d | 2013-10-13 19:53:54 +0400 | [diff] [blame] | 579 | LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, |
| 580 | SI_PARAM_ALPHA_REF); |
| 581 | |
Michel Dänzer | 7708a86 | 2012-11-02 15:57:30 +0100 | [diff] [blame] | 582 | LLVMValueRef alpha_pass = |
| 583 | lp_build_cmp(&bld_base->base, |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 584 | si_shader_ctx->shader->key.ps.alpha_func, |
Michel Dänzer | 7708a86 | 2012-11-02 15:57:30 +0100 | [diff] [blame] | 585 | LLVMBuildLoad(gallivm->builder, out_ptr, ""), |
Vadim Girlin | 453ea2d | 2013-10-13 19:53:54 +0400 | [diff] [blame] | 586 | alpha_ref); |
Michel Dänzer | 7708a86 | 2012-11-02 15:57:30 +0100 | [diff] [blame] | 587 | LLVMValueRef arg = |
| 588 | lp_build_select(&bld_base->base, |
| 589 | alpha_pass, |
| 590 | lp_build_const_float(gallivm, 1.0f), |
| 591 | lp_build_const_float(gallivm, -1.0f)); |
| 592 | |
| 593 | build_intrinsic(gallivm->builder, |
| 594 | "llvm.AMDGPU.kill", |
| 595 | LLVMVoidTypeInContext(gallivm->context), |
| 596 | &arg, 1, 0); |
| 597 | } else { |
| 598 | build_intrinsic(gallivm->builder, |
| 599 | "llvm.AMDGPU.kilp", |
| 600 | LLVMVoidTypeInContext(gallivm->context), |
| 601 | NULL, 0, 0); |
| 602 | } |
| 603 | } |
| 604 | |
Marek Olšák | 6d4755a | 2013-07-30 22:29:29 +0200 | [diff] [blame] | 605 | static void si_alpha_to_one(struct lp_build_tgsi_context *bld_base, |
| 606 | unsigned index) |
| 607 | { |
| 608 | struct si_shader_context *si_shader_ctx = si_shader_context(bld_base); |
| 609 | |
| 610 | /* set alpha to one */ |
| 611 | LLVMBuildStore(bld_base->base.gallivm->builder, |
| 612 | bld_base->base.one, |
| 613 | si_shader_ctx->radeon_bld.soa.outputs[index][3]); |
| 614 | } |
| 615 | |
Michel Dänzer | e3befbc | 2013-05-15 18:09:50 +0200 | [diff] [blame] | 616 | 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] | 617 | LLVMValueRef (*pos)[9], unsigned index) |
Michel Dänzer | e3befbc | 2013-05-15 18:09:50 +0200 | [diff] [blame] | 618 | { |
| 619 | 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] | 620 | struct si_pipe_shader *shader = si_shader_ctx->shader; |
Michel Dänzer | e3befbc | 2013-05-15 18:09:50 +0200 | [diff] [blame] | 621 | struct lp_build_context *base = &bld_base->base; |
| 622 | 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] | 623 | unsigned reg_index; |
| 624 | unsigned chan; |
| 625 | unsigned const_chan; |
| 626 | LLVMValueRef out_elts[4]; |
| 627 | LLVMValueRef base_elt; |
| 628 | LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST); |
Marek Olšák | 2fd4200 | 2013-10-25 11:45:47 +0200 | [diff] [blame] | 629 | LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, NUM_PIPE_CONST_BUFFERS); |
| 630 | LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, constbuf_index); |
Michel Dänzer | e3befbc | 2013-05-15 18:09:50 +0200 | [diff] [blame] | 631 | |
| 632 | for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) { |
| 633 | LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][chan]; |
| 634 | out_elts[chan] = LLVMBuildLoad(base->gallivm->builder, out_ptr, ""); |
| 635 | } |
| 636 | |
| 637 | for (reg_index = 0; reg_index < 2; reg_index ++) { |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 638 | LLVMValueRef *args = pos[2 + reg_index]; |
| 639 | |
Michel Dänzer | 2f98dc2 | 2013-08-08 16:58:00 +0200 | [diff] [blame] | 640 | if (!(shader->key.vs.ucps_enabled & (1 << reg_index))) |
| 641 | continue; |
| 642 | |
| 643 | shader->shader.clip_dist_write |= 0xf << (4 * reg_index); |
| 644 | |
Michel Dänzer | e3befbc | 2013-05-15 18:09:50 +0200 | [diff] [blame] | 645 | args[5] = |
| 646 | args[6] = |
| 647 | args[7] = |
| 648 | args[8] = lp_build_const_float(base->gallivm, 0.0f); |
| 649 | |
| 650 | /* Compute dot products of position and user clip plane vectors */ |
| 651 | for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) { |
| 652 | for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) { |
| 653 | args[0] = const_resource; |
| 654 | args[1] = lp_build_const_int32(base->gallivm, |
| 655 | ((reg_index * 4 + chan) * 4 + |
| 656 | const_chan) * 4); |
| 657 | base_elt = build_intrinsic(base->gallivm->builder, |
| 658 | "llvm.SI.load.const", |
| 659 | base->elem_type, |
| 660 | args, 2, |
| 661 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
| 662 | args[5 + chan] = |
| 663 | lp_build_add(base, args[5 + chan], |
| 664 | lp_build_mul(base, base_elt, |
| 665 | out_elts[const_chan])); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | args[0] = lp_build_const_int32(base->gallivm, 0xf); |
| 670 | args[1] = uint->zero; |
| 671 | args[2] = uint->zero; |
| 672 | args[3] = lp_build_const_int32(base->gallivm, |
| 673 | V_008DFC_SQ_EXP_POS + 2 + reg_index); |
| 674 | args[4] = uint->zero; |
Michel Dänzer | e3befbc | 2013-05-15 18:09:50 +0200 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
Marek Olšák | 8d03d92 | 2013-09-01 23:59:06 +0200 | [diff] [blame] | 678 | static void si_dump_streamout(struct pipe_stream_output_info *so) |
| 679 | { |
| 680 | unsigned i; |
| 681 | |
| 682 | if (so->num_outputs) |
| 683 | fprintf(stderr, "STREAMOUT\n"); |
| 684 | |
| 685 | for (i = 0; i < so->num_outputs; i++) { |
| 686 | unsigned mask = ((1 << so->output[i].num_components) - 1) << |
| 687 | so->output[i].start_component; |
| 688 | fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n", |
| 689 | i, so->output[i].output_buffer, |
| 690 | so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1, |
| 691 | so->output[i].register_index, |
| 692 | mask & 1 ? "x" : "", |
| 693 | mask & 2 ? "y" : "", |
| 694 | mask & 4 ? "z" : "", |
| 695 | mask & 8 ? "w" : ""); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4. |
| 700 | * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2), |
| 701 | * or v4i32 (num_channels=3,4). */ |
| 702 | static void build_tbuffer_store(struct si_shader_context *shader, |
| 703 | LLVMValueRef rsrc, |
| 704 | LLVMValueRef vdata, |
| 705 | unsigned num_channels, |
| 706 | LLVMValueRef vaddr, |
| 707 | LLVMValueRef soffset, |
| 708 | unsigned inst_offset, |
| 709 | unsigned dfmt, |
| 710 | unsigned nfmt, |
| 711 | unsigned offen, |
| 712 | unsigned idxen, |
| 713 | unsigned glc, |
| 714 | unsigned slc, |
| 715 | unsigned tfe) |
| 716 | { |
| 717 | struct gallivm_state *gallivm = &shader->radeon_bld.gallivm; |
| 718 | LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context); |
| 719 | LLVMValueRef args[] = { |
| 720 | rsrc, |
| 721 | vdata, |
| 722 | LLVMConstInt(i32, num_channels, 0), |
| 723 | vaddr, |
| 724 | soffset, |
| 725 | LLVMConstInt(i32, inst_offset, 0), |
| 726 | LLVMConstInt(i32, dfmt, 0), |
| 727 | LLVMConstInt(i32, nfmt, 0), |
| 728 | LLVMConstInt(i32, offen, 0), |
| 729 | LLVMConstInt(i32, idxen, 0), |
| 730 | LLVMConstInt(i32, glc, 0), |
| 731 | LLVMConstInt(i32, slc, 0), |
| 732 | LLVMConstInt(i32, tfe, 0) |
| 733 | }; |
| 734 | |
| 735 | /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */ |
| 736 | unsigned func = CLAMP(num_channels, 1, 3) - 1; |
| 737 | const char *types[] = {"i32", "v2i32", "v4i32"}; |
| 738 | char name[256]; |
| 739 | snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]); |
| 740 | |
| 741 | lp_build_intrinsic(gallivm->builder, name, |
| 742 | LLVMVoidTypeInContext(gallivm->context), |
| 743 | args, Elements(args)); |
| 744 | } |
| 745 | |
| 746 | static void build_streamout_store(struct si_shader_context *shader, |
| 747 | LLVMValueRef rsrc, |
| 748 | LLVMValueRef vdata, |
| 749 | unsigned num_channels, |
| 750 | LLVMValueRef vaddr, |
| 751 | LLVMValueRef soffset, |
| 752 | unsigned inst_offset) |
| 753 | { |
| 754 | static unsigned dfmt[] = { |
| 755 | V_008F0C_BUF_DATA_FORMAT_32, |
| 756 | V_008F0C_BUF_DATA_FORMAT_32_32, |
| 757 | V_008F0C_BUF_DATA_FORMAT_32_32_32, |
| 758 | V_008F0C_BUF_DATA_FORMAT_32_32_32_32 |
| 759 | }; |
| 760 | assert(num_channels >= 1 && num_channels <= 4); |
| 761 | |
| 762 | build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset, |
| 763 | inst_offset, dfmt[num_channels-1], |
| 764 | V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0); |
| 765 | } |
| 766 | |
| 767 | /* On SI, the vertex shader is responsible for writing streamout data |
| 768 | * to buffers. */ |
| 769 | static void si_llvm_emit_streamout(struct si_shader_context *shader) |
| 770 | { |
| 771 | struct pipe_stream_output_info *so = &shader->shader->selector->so; |
| 772 | struct gallivm_state *gallivm = &shader->radeon_bld.gallivm; |
| 773 | LLVMBuilderRef builder = gallivm->builder; |
| 774 | int i, j; |
| 775 | struct lp_build_if_state if_ctx; |
| 776 | |
| 777 | LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context); |
| 778 | |
| 779 | LLVMValueRef so_param = |
| 780 | LLVMGetParam(shader->radeon_bld.main_fn, |
| 781 | shader->param_streamout_config); |
| 782 | |
| 783 | /* Get bits [22:16], i.e. (so_param >> 16) & 127; */ |
| 784 | LLVMValueRef so_vtx_count = |
| 785 | LLVMBuildAnd(builder, |
| 786 | LLVMBuildLShr(builder, so_param, |
| 787 | LLVMConstInt(i32, 16, 0), ""), |
| 788 | LLVMConstInt(i32, 127, 0), ""); |
| 789 | |
| 790 | LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32, |
| 791 | NULL, 0, LLVMReadNoneAttribute); |
| 792 | |
| 793 | /* can_emit = tid < so_vtx_count; */ |
| 794 | LLVMValueRef can_emit = |
| 795 | LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, ""); |
| 796 | |
| 797 | /* Emit the streamout code conditionally. This actually avoids |
| 798 | * out-of-bounds buffer access. The hw tells us via the SGPR |
| 799 | * (so_vtx_count) which threads are allowed to emit streamout data. */ |
| 800 | lp_build_if(&if_ctx, gallivm, can_emit); |
| 801 | { |
| 802 | /* The buffer offset is computed as follows: |
| 803 | * ByteOffset = streamout_offset[buffer_id]*4 + |
| 804 | * (streamout_write_index + thread_id)*stride[buffer_id] + |
| 805 | * attrib_offset |
| 806 | */ |
| 807 | |
| 808 | LLVMValueRef so_write_index = |
| 809 | LLVMGetParam(shader->radeon_bld.main_fn, |
| 810 | shader->param_streamout_write_index); |
| 811 | |
| 812 | /* Compute (streamout_write_index + thread_id). */ |
| 813 | so_write_index = LLVMBuildAdd(builder, so_write_index, tid, ""); |
| 814 | |
| 815 | /* Compute the write offset for each enabled buffer. */ |
| 816 | LLVMValueRef so_write_offset[4] = {}; |
| 817 | for (i = 0; i < 4; i++) { |
| 818 | if (!so->stride[i]) |
| 819 | continue; |
| 820 | |
| 821 | LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn, |
| 822 | shader->param_streamout_offset[i]); |
| 823 | so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), ""); |
| 824 | |
| 825 | so_write_offset[i] = LLVMBuildMul(builder, so_write_index, |
| 826 | LLVMConstInt(i32, so->stride[i]*4, 0), ""); |
| 827 | so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, ""); |
| 828 | } |
| 829 | |
| 830 | LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS] = shader->radeon_bld.soa.outputs; |
| 831 | |
| 832 | /* Write streamout data. */ |
| 833 | for (i = 0; i < so->num_outputs; i++) { |
| 834 | unsigned buf_idx = so->output[i].output_buffer; |
| 835 | unsigned reg = so->output[i].register_index; |
| 836 | unsigned start = so->output[i].start_component; |
| 837 | unsigned num_comps = so->output[i].num_components; |
| 838 | LLVMValueRef out[4]; |
| 839 | |
| 840 | assert(num_comps && num_comps <= 4); |
| 841 | if (!num_comps || num_comps > 4) |
| 842 | continue; |
| 843 | |
| 844 | /* Load the output as int. */ |
| 845 | for (j = 0; j < num_comps; j++) { |
| 846 | out[j] = LLVMBuildLoad(builder, outputs[reg][start+j], ""); |
| 847 | out[j] = LLVMBuildBitCast(builder, out[j], i32, ""); |
| 848 | } |
| 849 | |
| 850 | /* Pack the output. */ |
| 851 | LLVMValueRef vdata = NULL; |
| 852 | |
| 853 | switch (num_comps) { |
| 854 | case 1: /* as i32 */ |
| 855 | vdata = out[0]; |
| 856 | break; |
| 857 | case 2: /* as v2i32 */ |
| 858 | case 3: /* as v4i32 (aligned to 4) */ |
| 859 | case 4: /* as v4i32 */ |
| 860 | vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps))); |
| 861 | for (j = 0; j < num_comps; j++) { |
| 862 | vdata = LLVMBuildInsertElement(builder, vdata, out[j], |
| 863 | LLVMConstInt(i32, j, 0), ""); |
| 864 | } |
| 865 | break; |
| 866 | } |
| 867 | |
| 868 | build_streamout_store(shader, shader->so_buffers[buf_idx], |
| 869 | vdata, num_comps, |
| 870 | so_write_offset[buf_idx], |
| 871 | LLVMConstInt(i32, 0, 0), |
| 872 | so->output[i].dst_offset*4); |
| 873 | } |
| 874 | } |
| 875 | lp_build_endif(&if_ctx); |
| 876 | } |
| 877 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 878 | /* XXX: This is partially implemented for VS only at this point. It is not complete */ |
| 879 | static void si_llvm_emit_epilogue(struct lp_build_tgsi_context * bld_base) |
| 880 | { |
| 881 | 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] | 882 | struct si_shader * shader = &si_shader_ctx->shader->shader; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 883 | struct lp_build_context * base = &bld_base->base; |
| 884 | struct lp_build_context * uint = |
| 885 | &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld; |
| 886 | struct tgsi_parse_context *parse = &si_shader_ctx->parse; |
Michel Dänzer | 1a616c1 | 2012-11-13 17:35:09 +0100 | [diff] [blame] | 887 | LLVMValueRef args[9]; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 888 | LLVMValueRef last_args[9] = { 0 }; |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 889 | LLVMValueRef pos_args[4][9] = { { 0 } }; |
Michel Dänzer | 0afeea5 | 2013-05-02 14:53:17 +0200 | [diff] [blame] | 890 | unsigned semantic_name; |
Christian König | 3508815 | 2012-08-01 22:35:24 +0200 | [diff] [blame] | 891 | unsigned param_count = 0; |
Marek Olšák | 053606d | 2013-11-19 22:07:30 +0100 | [diff] [blame] | 892 | int depth_index = -1, stencil_index = -1, psize_index = -1, edgeflag_index = -1; |
Marek Olšák | d52791a | 2013-11-21 15:21:38 +0100 | [diff] [blame] | 893 | int layer_index = -1; |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 894 | int i; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 895 | |
Marek Olšák | 8d03d92 | 2013-09-01 23:59:06 +0200 | [diff] [blame] | 896 | if (si_shader_ctx->shader->selector->so.num_outputs) { |
| 897 | si_llvm_emit_streamout(si_shader_ctx); |
| 898 | } |
| 899 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 900 | while (!tgsi_parse_end_of_tokens(parse)) { |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 901 | struct tgsi_full_declaration *d = |
| 902 | &parse->FullToken.FullDeclaration; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 903 | unsigned target; |
| 904 | unsigned index; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 905 | |
| 906 | tgsi_parse_token(parse); |
Michel Dänzer | c840270 | 2013-02-12 18:37:22 +0100 | [diff] [blame] | 907 | |
| 908 | if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY && |
| 909 | parse->FullToken.FullProperty.Property.PropertyName == |
| 910 | TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS) |
| 911 | shader->fs_write_all = TRUE; |
| 912 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 913 | if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION) |
| 914 | continue; |
| 915 | |
| 916 | switch (d->Declaration.File) { |
| 917 | case TGSI_FILE_INPUT: |
| 918 | i = shader->ninput++; |
Marek Olšák | 2eac0aa | 2013-05-14 19:37:17 +0200 | [diff] [blame] | 919 | assert(i < Elements(shader->input)); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 920 | shader->input[i].name = d->Semantic.Name; |
| 921 | shader->input[i].sid = d->Semantic.Index; |
Francisco Jerez | 1279923 | 2012-04-30 18:27:52 +0200 | [diff] [blame] | 922 | shader->input[i].interpolate = d->Interp.Interpolate; |
| 923 | shader->input[i].centroid = d->Interp.Centroid; |
Christian König | 3508815 | 2012-08-01 22:35:24 +0200 | [diff] [blame] | 924 | continue; |
| 925 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 926 | case TGSI_FILE_OUTPUT: |
| 927 | i = shader->noutput++; |
Marek Olšák | 2eac0aa | 2013-05-14 19:37:17 +0200 | [diff] [blame] | 928 | assert(i < Elements(shader->output)); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 929 | shader->output[i].name = d->Semantic.Name; |
| 930 | shader->output[i].sid = d->Semantic.Index; |
Francisco Jerez | 1279923 | 2012-04-30 18:27:52 +0200 | [diff] [blame] | 931 | shader->output[i].interpolate = d->Interp.Interpolate; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 932 | break; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 933 | |
Christian König | 3508815 | 2012-08-01 22:35:24 +0200 | [diff] [blame] | 934 | default: |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 935 | continue; |
Christian König | 3508815 | 2012-08-01 22:35:24 +0200 | [diff] [blame] | 936 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 937 | |
Michel Dänzer | 0afeea5 | 2013-05-02 14:53:17 +0200 | [diff] [blame] | 938 | semantic_name = d->Semantic.Name; |
| 939 | handle_semantic: |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 940 | for (index = d->Range.First; index <= d->Range.Last; index++) { |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 941 | /* Select the correct target */ |
Michel Dänzer | 0afeea5 | 2013-05-02 14:53:17 +0200 | [diff] [blame] | 942 | switch(semantic_name) { |
Tom Stellard | c3c323a | 2012-08-30 10:35:36 -0400 | [diff] [blame] | 943 | case TGSI_SEMANTIC_PSIZE: |
Marek Olšák | 053606d | 2013-11-19 22:07:30 +0100 | [diff] [blame] | 944 | shader->vs_out_misc_write = true; |
| 945 | shader->vs_out_point_size = true; |
| 946 | psize_index = index; |
| 947 | continue; |
| 948 | case TGSI_SEMANTIC_EDGEFLAG: |
| 949 | shader->vs_out_misc_write = true; |
| 950 | shader->vs_out_edgeflag = true; |
| 951 | edgeflag_index = index; |
| 952 | continue; |
Marek Olšák | d52791a | 2013-11-21 15:21:38 +0100 | [diff] [blame] | 953 | case TGSI_SEMANTIC_LAYER: |
| 954 | shader->vs_out_misc_write = true; |
| 955 | shader->vs_out_layer = true; |
| 956 | layer_index = index; |
| 957 | continue; |
Michel Dänzer | 1a616c1 | 2012-11-13 17:35:09 +0100 | [diff] [blame] | 958 | case TGSI_SEMANTIC_POSITION: |
| 959 | if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) { |
| 960 | target = V_008DFC_SQ_EXP_POS; |
| 961 | break; |
| 962 | } else { |
| 963 | depth_index = index; |
| 964 | continue; |
| 965 | } |
| 966 | case TGSI_SEMANTIC_STENCIL: |
| 967 | stencil_index = index; |
| 968 | continue; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 969 | case TGSI_SEMANTIC_COLOR: |
| 970 | if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) { |
Michel Dänzer | 691f08d | 2012-09-06 18:03:38 +0200 | [diff] [blame] | 971 | case TGSI_SEMANTIC_BCOLOR: |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 972 | target = V_008DFC_SQ_EXP_PARAM + param_count; |
Michel Dänzer | dd9d619 | 2012-05-18 15:01:10 +0200 | [diff] [blame] | 973 | shader->output[i].param_offset = param_count; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 974 | param_count++; |
| 975 | } else { |
Marek Olšák | 9471513 | 2013-10-22 22:05:35 +0200 | [diff] [blame] | 976 | target = V_008DFC_SQ_EXP_MRT + shader->output[i].sid; |
Marek Olšák | 6d4755a | 2013-07-30 22:29:29 +0200 | [diff] [blame] | 977 | if (si_shader_ctx->shader->key.ps.alpha_to_one) { |
| 978 | si_alpha_to_one(bld_base, index); |
| 979 | } |
Marek Olšák | 9471513 | 2013-10-22 22:05:35 +0200 | [diff] [blame] | 980 | if (shader->output[i].sid == 0 && |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 981 | si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS) |
Michel Dänzer | 7708a86 | 2012-11-02 15:57:30 +0100 | [diff] [blame] | 982 | si_alpha_test(bld_base, index); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 983 | } |
| 984 | break; |
Michel Dänzer | 0afeea5 | 2013-05-02 14:53:17 +0200 | [diff] [blame] | 985 | case TGSI_SEMANTIC_CLIPDIST: |
Michel Dänzer | 2f98dc2 | 2013-08-08 16:58:00 +0200 | [diff] [blame] | 986 | if (!(si_shader_ctx->shader->key.vs.ucps_enabled & |
| 987 | (1 << d->Semantic.Index))) |
| 988 | continue; |
Michel Dänzer | 0afeea5 | 2013-05-02 14:53:17 +0200 | [diff] [blame] | 989 | shader->clip_dist_write |= |
| 990 | d->Declaration.UsageMask << (d->Semantic.Index << 2); |
| 991 | target = V_008DFC_SQ_EXP_POS + 2 + d->Semantic.Index; |
| 992 | break; |
Michel Dänzer | e3befbc | 2013-05-15 18:09:50 +0200 | [diff] [blame] | 993 | case TGSI_SEMANTIC_CLIPVERTEX: |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 994 | si_llvm_emit_clipvertex(bld_base, pos_args, index); |
Michel Dänzer | e3befbc | 2013-05-15 18:09:50 +0200 | [diff] [blame] | 995 | continue; |
Michel Dänzer | 30b3037 | 2012-09-06 17:53:04 +0200 | [diff] [blame] | 996 | case TGSI_SEMANTIC_FOG: |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 997 | case TGSI_SEMANTIC_GENERIC: |
| 998 | target = V_008DFC_SQ_EXP_PARAM + param_count; |
Michel Dänzer | dd9d619 | 2012-05-18 15:01:10 +0200 | [diff] [blame] | 999 | shader->output[i].param_offset = param_count; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1000 | param_count++; |
| 1001 | break; |
| 1002 | default: |
| 1003 | target = 0; |
| 1004 | fprintf(stderr, |
| 1005 | "Warning: SI unhandled output type:%d\n", |
Michel Dänzer | 0afeea5 | 2013-05-02 14:53:17 +0200 | [diff] [blame] | 1006 | semantic_name); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1007 | } |
| 1008 | |
Michel Dänzer | 26c7139 | 2012-08-24 12:03:11 +0200 | [diff] [blame] | 1009 | si_llvm_init_export_args(bld_base, d, index, target, args); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1010 | |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 1011 | if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX && |
| 1012 | target >= V_008DFC_SQ_EXP_POS && |
| 1013 | target <= (V_008DFC_SQ_EXP_POS + 3)) { |
| 1014 | memcpy(pos_args[target - V_008DFC_SQ_EXP_POS], |
| 1015 | args, sizeof(args)); |
| 1016 | } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT && |
| 1017 | semantic_name == TGSI_SEMANTIC_COLOR) { |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1018 | if (last_args[0]) { |
| 1019 | lp_build_intrinsic(base->gallivm->builder, |
| 1020 | "llvm.SI.export", |
| 1021 | LLVMVoidTypeInContext(base->gallivm->context), |
| 1022 | last_args, 9); |
| 1023 | } |
| 1024 | |
| 1025 | memcpy(last_args, args, sizeof(args)); |
| 1026 | } else { |
| 1027 | lp_build_intrinsic(base->gallivm->builder, |
| 1028 | "llvm.SI.export", |
| 1029 | LLVMVoidTypeInContext(base->gallivm->context), |
| 1030 | args, 9); |
| 1031 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1032 | } |
Michel Dänzer | 0afeea5 | 2013-05-02 14:53:17 +0200 | [diff] [blame] | 1033 | |
| 1034 | if (semantic_name == TGSI_SEMANTIC_CLIPDIST) { |
| 1035 | semantic_name = TGSI_SEMANTIC_GENERIC; |
| 1036 | goto handle_semantic; |
| 1037 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1038 | } |
| 1039 | |
Michel Dänzer | 1a616c1 | 2012-11-13 17:35:09 +0100 | [diff] [blame] | 1040 | if (depth_index >= 0 || stencil_index >= 0) { |
| 1041 | LLVMValueRef out_ptr; |
| 1042 | unsigned mask = 0; |
| 1043 | |
| 1044 | /* Specify the target we are exporting */ |
| 1045 | args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ); |
| 1046 | |
| 1047 | if (depth_index >= 0) { |
| 1048 | out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2]; |
| 1049 | args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, ""); |
| 1050 | mask |= 0x1; |
| 1051 | |
| 1052 | if (stencil_index < 0) { |
| 1053 | args[6] = |
| 1054 | args[7] = |
| 1055 | args[8] = args[5]; |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | if (stencil_index >= 0) { |
| 1060 | out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1]; |
| 1061 | args[7] = |
| 1062 | args[8] = |
| 1063 | args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, ""); |
Michel Dänzer | 46fd81e | 2013-08-23 14:55:45 +0200 | [diff] [blame] | 1064 | /* Only setting the stencil component bit (0x2) here |
| 1065 | * breaks some stencil piglit tests |
| 1066 | */ |
| 1067 | mask |= 0x3; |
Michel Dänzer | 1a616c1 | 2012-11-13 17:35:09 +0100 | [diff] [blame] | 1068 | |
| 1069 | if (depth_index < 0) |
| 1070 | args[5] = args[6]; |
| 1071 | } |
| 1072 | |
| 1073 | /* Specify which components to enable */ |
| 1074 | args[0] = lp_build_const_int32(base->gallivm, mask); |
| 1075 | |
| 1076 | args[1] = |
| 1077 | args[2] = |
| 1078 | args[4] = uint->zero; |
| 1079 | |
| 1080 | if (last_args[0]) |
| 1081 | lp_build_intrinsic(base->gallivm->builder, |
| 1082 | "llvm.SI.export", |
| 1083 | LLVMVoidTypeInContext(base->gallivm->context), |
| 1084 | args, 9); |
| 1085 | else |
| 1086 | memcpy(last_args, args, sizeof(args)); |
| 1087 | } |
| 1088 | |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 1089 | if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) { |
| 1090 | unsigned pos_idx = 0; |
Christian König | f18fd25 | 2012-07-25 21:58:46 +0200 | [diff] [blame] | 1091 | |
Marek Olšák | 48784f3 | 2013-10-23 16:10:38 +0200 | [diff] [blame] | 1092 | /* We need to add the position output manually if it's missing. */ |
| 1093 | if (!pos_args[0][0]) { |
| 1094 | pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */ |
| 1095 | pos_args[0][1] = uint->zero; /* EXEC mask */ |
| 1096 | pos_args[0][2] = uint->zero; /* last export? */ |
| 1097 | pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS); |
| 1098 | pos_args[0][4] = uint->zero; /* COMPR flag */ |
| 1099 | pos_args[0][5] = base->zero; /* X */ |
| 1100 | pos_args[0][6] = base->zero; /* Y */ |
| 1101 | pos_args[0][7] = base->zero; /* Z */ |
| 1102 | pos_args[0][8] = base->one; /* W */ |
| 1103 | } |
| 1104 | |
Marek Olšák | 053606d | 2013-11-19 22:07:30 +0100 | [diff] [blame] | 1105 | /* Write the misc vector (point size, edgeflag, layer, viewport). */ |
| 1106 | if (shader->vs_out_misc_write) { |
| 1107 | pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */ |
| 1108 | shader->vs_out_point_size | |
Marek Olšák | d52791a | 2013-11-21 15:21:38 +0100 | [diff] [blame] | 1109 | (shader->vs_out_edgeflag << 1) | |
| 1110 | (shader->vs_out_layer << 2)); |
Marek Olšák | 053606d | 2013-11-19 22:07:30 +0100 | [diff] [blame] | 1111 | pos_args[1][1] = uint->zero; /* EXEC mask */ |
| 1112 | pos_args[1][2] = uint->zero; /* last export? */ |
| 1113 | pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1); |
| 1114 | pos_args[1][4] = uint->zero; /* COMPR flag */ |
| 1115 | pos_args[1][5] = base->zero; /* X */ |
| 1116 | pos_args[1][6] = base->zero; /* Y */ |
| 1117 | pos_args[1][7] = base->zero; /* Z */ |
| 1118 | pos_args[1][8] = base->zero; /* W */ |
| 1119 | |
| 1120 | if (shader->vs_out_point_size) { |
| 1121 | pos_args[1][5] = LLVMBuildLoad(base->gallivm->builder, |
| 1122 | si_shader_ctx->radeon_bld.soa.outputs[psize_index][0], ""); |
| 1123 | } |
| 1124 | |
| 1125 | if (shader->vs_out_edgeflag) { |
| 1126 | LLVMValueRef output = LLVMBuildLoad(base->gallivm->builder, |
| 1127 | si_shader_ctx->radeon_bld.soa.outputs[edgeflag_index][0], ""); |
| 1128 | |
| 1129 | /* The output is a float, but the hw expects an integer |
| 1130 | * with the first bit containing the edge flag. */ |
| 1131 | output = LLVMBuildFPToUI(base->gallivm->builder, output, |
| 1132 | bld_base->uint_bld.elem_type, ""); |
| 1133 | |
| 1134 | output = lp_build_min(&bld_base->int_bld, output, bld_base->int_bld.one); |
| 1135 | |
| 1136 | /* The LLVM intrinsic expects a float. */ |
| 1137 | pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder, output, |
| 1138 | base->elem_type, ""); |
| 1139 | } |
Marek Olšák | d52791a | 2013-11-21 15:21:38 +0100 | [diff] [blame] | 1140 | |
| 1141 | if (shader->vs_out_layer) { |
| 1142 | pos_args[1][7] = LLVMBuildLoad(base->gallivm->builder, |
| 1143 | si_shader_ctx->radeon_bld.soa.outputs[layer_index][0], ""); |
| 1144 | } |
Marek Olšák | 053606d | 2013-11-19 22:07:30 +0100 | [diff] [blame] | 1145 | } |
| 1146 | |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 1147 | for (i = 0; i < 4; i++) |
| 1148 | if (pos_args[i][0]) |
| 1149 | shader->nr_pos_exports++; |
Christian König | f18fd25 | 2012-07-25 21:58:46 +0200 | [diff] [blame] | 1150 | |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 1151 | for (i = 0; i < 4; i++) { |
| 1152 | if (!pos_args[i][0]) |
| 1153 | continue; |
Christian König | f18fd25 | 2012-07-25 21:58:46 +0200 | [diff] [blame] | 1154 | |
Michel Dänzer | c840270 | 2013-02-12 18:37:22 +0100 | [diff] [blame] | 1155 | /* Specify the target we are exporting */ |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 1156 | pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++); |
| 1157 | |
| 1158 | if (pos_idx == shader->nr_pos_exports) |
| 1159 | /* Specify that this is the last export */ |
| 1160 | pos_args[i][2] = uint->one; |
Michel Dänzer | c840270 | 2013-02-12 18:37:22 +0100 | [diff] [blame] | 1161 | |
| 1162 | lp_build_intrinsic(base->gallivm->builder, |
| 1163 | "llvm.SI.export", |
| 1164 | LLVMVoidTypeInContext(base->gallivm->context), |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 1165 | pos_args[i], 9); |
| 1166 | } |
| 1167 | } else { |
| 1168 | if (!last_args[0]) { |
| 1169 | /* Specify which components to enable */ |
| 1170 | last_args[0] = lp_build_const_int32(base->gallivm, 0x0); |
| 1171 | |
| 1172 | /* Specify the target we are exporting */ |
| 1173 | last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT); |
| 1174 | |
| 1175 | /* Set COMPR flag to zero to export data as 32-bit */ |
| 1176 | last_args[4] = uint->zero; |
| 1177 | |
| 1178 | /* dummy bits */ |
| 1179 | last_args[5]= uint->zero; |
| 1180 | last_args[6]= uint->zero; |
| 1181 | last_args[7]= uint->zero; |
| 1182 | last_args[8]= uint->zero; |
Michel Dänzer | c840270 | 2013-02-12 18:37:22 +0100 | [diff] [blame] | 1183 | |
| 1184 | si_shader_ctx->shader->spi_shader_col_format |= |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 1185 | V_028714_SPI_SHADER_32_ABGR; |
| 1186 | 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] | 1187 | } |
| 1188 | |
Michel Dänzer | b00269a | 2013-08-07 18:14:16 +0200 | [diff] [blame] | 1189 | /* Specify whether the EXEC mask represents the valid mask */ |
| 1190 | last_args[1] = uint->one; |
| 1191 | |
| 1192 | if (shader->fs_write_all && shader->nr_cbufs > 1) { |
| 1193 | int i; |
| 1194 | |
| 1195 | /* Specify that this is not yet the last export */ |
| 1196 | last_args[2] = lp_build_const_int32(base->gallivm, 0); |
| 1197 | |
| 1198 | for (i = 1; i < shader->nr_cbufs; i++) { |
| 1199 | /* Specify the target we are exporting */ |
| 1200 | last_args[3] = lp_build_const_int32(base->gallivm, |
| 1201 | V_008DFC_SQ_EXP_MRT + i); |
| 1202 | |
| 1203 | lp_build_intrinsic(base->gallivm->builder, |
| 1204 | "llvm.SI.export", |
| 1205 | LLVMVoidTypeInContext(base->gallivm->context), |
| 1206 | last_args, 9); |
| 1207 | |
| 1208 | si_shader_ctx->shader->spi_shader_col_format |= |
| 1209 | si_shader_ctx->shader->spi_shader_col_format << 4; |
| 1210 | si_shader_ctx->shader->cb_shader_mask |= |
| 1211 | si_shader_ctx->shader->cb_shader_mask << 4; |
| 1212 | } |
| 1213 | |
| 1214 | last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT); |
| 1215 | } |
| 1216 | |
| 1217 | /* Specify that this is the last export */ |
| 1218 | last_args[2] = lp_build_const_int32(base->gallivm, 1); |
| 1219 | |
| 1220 | lp_build_intrinsic(base->gallivm->builder, |
| 1221 | "llvm.SI.export", |
| 1222 | LLVMVoidTypeInContext(base->gallivm->context), |
| 1223 | last_args, 9); |
Michel Dänzer | c840270 | 2013-02-12 18:37:22 +0100 | [diff] [blame] | 1224 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1225 | /* XXX: Look up what this function does */ |
| 1226 | /* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/ |
| 1227 | } |
| 1228 | |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame] | 1229 | static const struct lp_build_tgsi_action txf_action; |
| 1230 | |
| 1231 | static void build_tex_intrinsic(const struct lp_build_tgsi_action * action, |
| 1232 | struct lp_build_tgsi_context * bld_base, |
| 1233 | struct lp_build_emit_data * emit_data); |
| 1234 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1235 | static void tex_fetch_args( |
| 1236 | struct lp_build_tgsi_context * bld_base, |
| 1237 | struct lp_build_emit_data * emit_data) |
| 1238 | { |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 1239 | 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] | 1240 | struct gallivm_state *gallivm = bld_base->base.gallivm; |
Michel Dänzer | c2bae6b | 2012-08-02 17:19:22 +0200 | [diff] [blame] | 1241 | const struct tgsi_full_instruction * inst = emit_data->inst; |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 1242 | unsigned opcode = inst->Instruction.Opcode; |
| 1243 | unsigned target = inst->Texture.Texture; |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 1244 | LLVMValueRef coords[4]; |
| 1245 | LLVMValueRef address[16]; |
Michel Dänzer | beaa5eb | 2013-05-24 13:23:26 +0100 | [diff] [blame] | 1246 | int ref_pos; |
| 1247 | 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] | 1248 | unsigned count = 0; |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 1249 | unsigned chan; |
Marek Olšák | dbeedbb | 2013-10-31 15:08:49 +0100 | [diff] [blame] | 1250 | unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1; |
| 1251 | unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index; |
| 1252 | |
| 1253 | if (target == TGSI_TEXTURE_BUFFER) { |
| 1254 | LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128); |
| 1255 | LLVMTypeRef v2i128 = LLVMVectorType(i128, 2); |
| 1256 | LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context); |
| 1257 | LLVMTypeRef v16i8 = LLVMVectorType(i8, 16); |
| 1258 | |
| 1259 | /* Truncate v32i8 to v16i8. */ |
| 1260 | LLVMValueRef res = si_shader_ctx->resources[sampler_index]; |
| 1261 | res = LLVMBuildBitCast(gallivm->builder, res, v2i128, ""); |
| 1262 | res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, ""); |
| 1263 | res = LLVMBuildBitCast(gallivm->builder, res, v16i8, ""); |
| 1264 | |
| 1265 | emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4); |
| 1266 | emit_data->args[0] = res; |
| 1267 | emit_data->args[1] = bld_base->uint_bld.zero; |
| 1268 | emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0); |
| 1269 | emit_data->arg_count = 3; |
| 1270 | return; |
| 1271 | } |
Tom Stellard | 467f516 | 2012-05-16 15:15:35 -0400 | [diff] [blame] | 1272 | |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 1273 | /* Fetch and project texture coordinates */ |
| 1274 | 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] | 1275 | for (chan = 0; chan < 3; chan++ ) { |
| 1276 | coords[chan] = lp_build_emit_fetch(bld_base, |
| 1277 | emit_data->inst, 0, |
| 1278 | chan); |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 1279 | if (opcode == TGSI_OPCODE_TXP) |
Michel Dänzer | c2bae6b | 2012-08-02 17:19:22 +0200 | [diff] [blame] | 1280 | coords[chan] = lp_build_emit_llvm_binary(bld_base, |
| 1281 | TGSI_OPCODE_DIV, |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 1282 | coords[chan], |
| 1283 | coords[3]); |
| 1284 | } |
| 1285 | |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 1286 | if (opcode == TGSI_OPCODE_TXP) |
| 1287 | coords[3] = bld_base->base.one; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1288 | |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 1289 | /* Pack LOD bias value */ |
| 1290 | if (opcode == TGSI_OPCODE_TXB) |
| 1291 | address[count++] = coords[3]; |
Vadim Girlin | 8cf552b | 2012-12-18 17:39:19 +0400 | [diff] [blame] | 1292 | |
Michel Dänzer | 0495adb | 2013-05-06 12:45:14 +0200 | [diff] [blame] | 1293 | if (target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE) |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 1294 | radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords); |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 1295 | |
| 1296 | /* Pack depth comparison value */ |
| 1297 | switch (target) { |
| 1298 | case TGSI_TEXTURE_SHADOW1D: |
| 1299 | case TGSI_TEXTURE_SHADOW1D_ARRAY: |
| 1300 | case TGSI_TEXTURE_SHADOW2D: |
| 1301 | case TGSI_TEXTURE_SHADOWRECT: |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 1302 | case TGSI_TEXTURE_SHADOWCUBE: |
| 1303 | case TGSI_TEXTURE_SHADOW2D_ARRAY: |
Michel Dänzer | beaa5eb | 2013-05-24 13:23:26 +0100 | [diff] [blame] | 1304 | assert(ref_pos >= 0); |
| 1305 | address[count++] = coords[ref_pos]; |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 1306 | break; |
| 1307 | case TGSI_TEXTURE_SHADOWCUBE_ARRAY: |
| 1308 | address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0); |
Michel Dänzer | e0f2ffc | 2012-12-03 12:46:30 +0100 | [diff] [blame] | 1309 | } |
| 1310 | |
Michel Dänzer | a6b83c0 | 2013-02-21 16:10:55 +0100 | [diff] [blame] | 1311 | /* Pack user derivatives */ |
| 1312 | if (opcode == TGSI_OPCODE_TXD) { |
| 1313 | for (chan = 0; chan < 2; chan++) { |
| 1314 | address[count++] = lp_build_emit_fetch(bld_base, inst, 1, chan); |
| 1315 | if (num_coords > 1) |
| 1316 | address[count++] = lp_build_emit_fetch(bld_base, inst, 2, chan); |
| 1317 | } |
| 1318 | } |
| 1319 | |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 1320 | /* Pack texture coordinates */ |
| 1321 | address[count++] = coords[0]; |
Michel Dänzer | beaa5eb | 2013-05-24 13:23:26 +0100 | [diff] [blame] | 1322 | if (num_coords > 1) |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 1323 | address[count++] = coords[1]; |
Michel Dänzer | beaa5eb | 2013-05-24 13:23:26 +0100 | [diff] [blame] | 1324 | if (num_coords > 2) |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 1325 | address[count++] = coords[2]; |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 1326 | |
Marek Olšák | d2bd634 | 2013-09-18 15:40:21 +0200 | [diff] [blame] | 1327 | /* Pack LOD or sample index */ |
Michel Dänzer | 3623111 | 2013-05-02 09:44:45 +0200 | [diff] [blame] | 1328 | if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF) |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 1329 | address[count++] = coords[3]; |
| 1330 | |
| 1331 | if (count > 16) { |
| 1332 | assert(!"Cannot handle more than 16 texture address parameters"); |
| 1333 | count = 16; |
| 1334 | } |
| 1335 | |
| 1336 | for (chan = 0; chan < count; chan++ ) { |
| 1337 | address[chan] = LLVMBuildBitCast(gallivm->builder, |
| 1338 | address[chan], |
| 1339 | LLVMInt32TypeInContext(gallivm->context), |
| 1340 | ""); |
| 1341 | } |
| 1342 | |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame] | 1343 | /* Adjust the sample index according to FMASK. |
| 1344 | * |
| 1345 | * For uncompressed MSAA surfaces, FMASK should return 0x76543210, |
| 1346 | * which is the identity mapping. Each nibble says which physical sample |
| 1347 | * should be fetched to get that sample. |
| 1348 | * |
| 1349 | * For example, 0x11111100 means there are only 2 samples stored and |
| 1350 | * the second sample covers 3/4 of the pixel. When reading samples 0 |
| 1351 | * and 1, return physical sample 0 (determined by the first two 0s |
| 1352 | * in FMASK), otherwise return physical sample 1. |
| 1353 | * |
| 1354 | * The sample index should be adjusted as follows: |
| 1355 | * sample_index = (fmask >> (sample_index * 4)) & 0xF; |
| 1356 | */ |
| 1357 | if (target == TGSI_TEXTURE_2D_MSAA || |
| 1358 | target == TGSI_TEXTURE_2D_ARRAY_MSAA) { |
| 1359 | struct lp_build_context *uint_bld = &bld_base->uint_bld; |
| 1360 | struct lp_build_emit_data txf_emit_data = *emit_data; |
Marek Olšák | d2bd634 | 2013-09-18 15:40:21 +0200 | [diff] [blame] | 1361 | LLVMValueRef txf_address[4]; |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame] | 1362 | unsigned txf_count = count; |
| 1363 | |
Marek Olšák | d2bd634 | 2013-09-18 15:40:21 +0200 | [diff] [blame] | 1364 | memcpy(txf_address, address, sizeof(txf_address)); |
| 1365 | |
| 1366 | if (target == TGSI_TEXTURE_2D_MSAA) { |
| 1367 | txf_address[2] = bld_base->uint_bld.zero; |
| 1368 | } |
| 1369 | txf_address[3] = bld_base->uint_bld.zero; |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame] | 1370 | |
| 1371 | /* Pad to a power-of-two size. */ |
| 1372 | while (txf_count < util_next_power_of_two(txf_count)) |
| 1373 | txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context)); |
| 1374 | |
| 1375 | /* Read FMASK using TXF. */ |
| 1376 | txf_emit_data.chan = 0; |
| 1377 | txf_emit_data.dst_type = LLVMVectorType( |
| 1378 | LLVMInt32TypeInContext(bld_base->base.gallivm->context), 4); |
| 1379 | txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count); |
| 1380 | txf_emit_data.args[1] = si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index]; |
Marek Olšák | d2bd634 | 2013-09-18 15:40:21 +0200 | [diff] [blame] | 1381 | txf_emit_data.args[2] = lp_build_const_int32(bld_base->base.gallivm, |
| 1382 | target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY); |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame] | 1383 | txf_emit_data.arg_count = 3; |
| 1384 | |
| 1385 | build_tex_intrinsic(&txf_action, bld_base, &txf_emit_data); |
| 1386 | |
| 1387 | /* Initialize some constants. */ |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame] | 1388 | LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0); |
| 1389 | LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0); |
| 1390 | |
| 1391 | /* Apply the formula. */ |
| 1392 | LLVMValueRef fmask = |
| 1393 | LLVMBuildExtractElement(gallivm->builder, |
| 1394 | txf_emit_data.output[0], |
| 1395 | uint_bld->zero, ""); |
| 1396 | |
Marek Olšák | d2bd634 | 2013-09-18 15:40:21 +0200 | [diff] [blame] | 1397 | unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3; |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame] | 1398 | |
| 1399 | LLVMValueRef sample_index4 = |
Marek Olšák | d2bd634 | 2013-09-18 15:40:21 +0200 | [diff] [blame] | 1400 | LLVMBuildMul(gallivm->builder, address[sample_chan], four, ""); |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame] | 1401 | |
| 1402 | LLVMValueRef shifted_fmask = |
| 1403 | LLVMBuildLShr(gallivm->builder, fmask, sample_index4, ""); |
| 1404 | |
| 1405 | LLVMValueRef final_sample = |
| 1406 | LLVMBuildAnd(gallivm->builder, shifted_fmask, F, ""); |
| 1407 | |
| 1408 | /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK |
| 1409 | * resource descriptor is 0 (invalid), |
| 1410 | */ |
| 1411 | LLVMValueRef fmask_desc = |
| 1412 | LLVMBuildBitCast(gallivm->builder, |
| 1413 | si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index], |
| 1414 | LLVMVectorType(uint_bld->elem_type, 8), ""); |
| 1415 | |
| 1416 | LLVMValueRef fmask_word1 = |
| 1417 | LLVMBuildExtractElement(gallivm->builder, fmask_desc, |
| 1418 | uint_bld->one, ""); |
| 1419 | |
| 1420 | LLVMValueRef word1_is_nonzero = |
| 1421 | LLVMBuildICmp(gallivm->builder, LLVMIntNE, |
| 1422 | fmask_word1, uint_bld->zero, ""); |
| 1423 | |
Marek Olšák | d2bd634 | 2013-09-18 15:40:21 +0200 | [diff] [blame] | 1424 | /* Replace the MSAA sample index. */ |
| 1425 | address[sample_chan] = |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame] | 1426 | LLVMBuildSelect(gallivm->builder, word1_is_nonzero, |
Marek Olšák | d2bd634 | 2013-09-18 15:40:21 +0200 | [diff] [blame] | 1427 | final_sample, address[sample_chan], ""); |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame] | 1428 | } |
Michel Dänzer | a6b83c0 | 2013-02-21 16:10:55 +0100 | [diff] [blame] | 1429 | |
Michel Dänzer | 3623111 | 2013-05-02 09:44:45 +0200 | [diff] [blame] | 1430 | /* Resource */ |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame] | 1431 | emit_data->args[1] = si_shader_ctx->resources[sampler_index]; |
Michel Dänzer | 3623111 | 2013-05-02 09:44:45 +0200 | [diff] [blame] | 1432 | |
| 1433 | if (opcode == TGSI_OPCODE_TXF) { |
| 1434 | /* add tex offsets */ |
| 1435 | if (inst->Texture.NumOffsets) { |
| 1436 | struct lp_build_context *uint_bld = &bld_base->uint_bld; |
| 1437 | struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base); |
| 1438 | const struct tgsi_texture_offset * off = inst->TexOffsets; |
| 1439 | |
| 1440 | assert(inst->Texture.NumOffsets == 1); |
| 1441 | |
Marek Olšák | defedc0 | 2013-09-18 15:36:38 +0200 | [diff] [blame] | 1442 | switch (target) { |
| 1443 | case TGSI_TEXTURE_3D: |
| 1444 | address[2] = lp_build_add(uint_bld, address[2], |
| 1445 | bld->immediates[off->Index][off->SwizzleZ]); |
| 1446 | /* fall through */ |
| 1447 | case TGSI_TEXTURE_2D: |
| 1448 | case TGSI_TEXTURE_SHADOW2D: |
| 1449 | case TGSI_TEXTURE_RECT: |
| 1450 | case TGSI_TEXTURE_SHADOWRECT: |
| 1451 | case TGSI_TEXTURE_2D_ARRAY: |
| 1452 | case TGSI_TEXTURE_SHADOW2D_ARRAY: |
Michel Dänzer | 3623111 | 2013-05-02 09:44:45 +0200 | [diff] [blame] | 1453 | address[1] = |
| 1454 | lp_build_add(uint_bld, address[1], |
Marek Olšák | defedc0 | 2013-09-18 15:36:38 +0200 | [diff] [blame] | 1455 | bld->immediates[off->Index][off->SwizzleY]); |
| 1456 | /* fall through */ |
| 1457 | case TGSI_TEXTURE_1D: |
| 1458 | case TGSI_TEXTURE_SHADOW1D: |
| 1459 | case TGSI_TEXTURE_1D_ARRAY: |
| 1460 | case TGSI_TEXTURE_SHADOW1D_ARRAY: |
| 1461 | address[0] = |
| 1462 | lp_build_add(uint_bld, address[0], |
| 1463 | bld->immediates[off->Index][off->SwizzleX]); |
| 1464 | break; |
| 1465 | /* texture offsets do not apply to other texture targets */ |
| 1466 | } |
Michel Dänzer | 3623111 | 2013-05-02 09:44:45 +0200 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | emit_data->dst_type = LLVMVectorType( |
| 1470 | LLVMInt32TypeInContext(bld_base->base.gallivm->context), |
| 1471 | 4); |
| 1472 | |
| 1473 | emit_data->arg_count = 3; |
| 1474 | } else { |
| 1475 | /* Sampler */ |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame] | 1476 | emit_data->args[2] = si_shader_ctx->samplers[sampler_index]; |
Michel Dänzer | 3623111 | 2013-05-02 09:44:45 +0200 | [diff] [blame] | 1477 | |
| 1478 | emit_data->dst_type = LLVMVectorType( |
| 1479 | LLVMFloatTypeInContext(bld_base->base.gallivm->context), |
| 1480 | 4); |
| 1481 | |
| 1482 | emit_data->arg_count = 4; |
| 1483 | } |
| 1484 | |
| 1485 | /* Dimensions */ |
| 1486 | emit_data->args[emit_data->arg_count - 1] = |
| 1487 | lp_build_const_int32(bld_base->base.gallivm, target); |
| 1488 | |
Michel Dänzer | 120efee | 2013-01-25 12:10:11 +0100 | [diff] [blame] | 1489 | /* Pad to power of two vector */ |
| 1490 | while (count < util_next_power_of_two(count)) |
| 1491 | address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context)); |
| 1492 | |
Christian König | ccf3e8f | 2013-03-26 15:09:27 +0100 | [diff] [blame] | 1493 | emit_data->args[0] = lp_build_gather_values(gallivm, address, count); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1494 | } |
| 1495 | |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 1496 | static void build_tex_intrinsic(const struct lp_build_tgsi_action * action, |
| 1497 | struct lp_build_tgsi_context * bld_base, |
| 1498 | struct lp_build_emit_data * emit_data) |
| 1499 | { |
| 1500 | struct lp_build_context * base = &bld_base->base; |
Kai Wasserbäch | bbb77fc | 2013-10-27 19:36:07 +0100 | [diff] [blame] | 1501 | char intr_name[127]; |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 1502 | |
Marek Olšák | dbeedbb | 2013-10-31 15:08:49 +0100 | [diff] [blame] | 1503 | if (emit_data->inst->Texture.Texture == TGSI_TEXTURE_BUFFER) { |
| 1504 | emit_data->output[emit_data->chan] = build_intrinsic( |
| 1505 | base->gallivm->builder, |
| 1506 | "llvm.SI.vs.load.input", emit_data->dst_type, |
| 1507 | emit_data->args, emit_data->arg_count, |
| 1508 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
| 1509 | return; |
| 1510 | } |
| 1511 | |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 1512 | sprintf(intr_name, "%sv%ui32", action->intr_name, |
Christian König | ccf3e8f | 2013-03-26 15:09:27 +0100 | [diff] [blame] | 1513 | LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0]))); |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 1514 | |
Christian König | 44e3224 | 2013-03-20 12:10:35 +0100 | [diff] [blame] | 1515 | emit_data->output[emit_data->chan] = build_intrinsic( |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 1516 | base->gallivm->builder, intr_name, emit_data->dst_type, |
Christian König | 44e3224 | 2013-03-20 12:10:35 +0100 | [diff] [blame] | 1517 | emit_data->args, emit_data->arg_count, |
| 1518 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 1519 | } |
| 1520 | |
Michel Dänzer | 0495adb | 2013-05-06 12:45:14 +0200 | [diff] [blame] | 1521 | static void txq_fetch_args( |
| 1522 | struct lp_build_tgsi_context * bld_base, |
| 1523 | struct lp_build_emit_data * emit_data) |
| 1524 | { |
| 1525 | struct si_shader_context *si_shader_ctx = si_shader_context(bld_base); |
| 1526 | const struct tgsi_full_instruction *inst = emit_data->inst; |
Marek Olšák | dbeedbb | 2013-10-31 15:08:49 +0100 | [diff] [blame] | 1527 | struct gallivm_state *gallivm = bld_base->base.gallivm; |
| 1528 | |
| 1529 | if (inst->Texture.Texture == TGSI_TEXTURE_BUFFER) { |
| 1530 | LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context); |
| 1531 | LLVMTypeRef v8i32 = LLVMVectorType(i32, 8); |
| 1532 | |
| 1533 | /* Read the size from the buffer descriptor directly. */ |
| 1534 | LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index]; |
| 1535 | size = LLVMBuildBitCast(gallivm->builder, size, v8i32, ""); |
| 1536 | size = LLVMBuildExtractElement(gallivm->builder, size, |
| 1537 | lp_build_const_int32(gallivm, 2), ""); |
| 1538 | emit_data->args[0] = size; |
| 1539 | return; |
| 1540 | } |
Michel Dänzer | 0495adb | 2013-05-06 12:45:14 +0200 | [diff] [blame] | 1541 | |
| 1542 | /* Mip level */ |
| 1543 | emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X); |
| 1544 | |
| 1545 | /* Resource */ |
| 1546 | emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index]; |
| 1547 | |
| 1548 | /* Dimensions */ |
| 1549 | emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm, |
| 1550 | inst->Texture.Texture); |
| 1551 | |
| 1552 | emit_data->arg_count = 3; |
| 1553 | |
| 1554 | emit_data->dst_type = LLVMVectorType( |
| 1555 | LLVMInt32TypeInContext(bld_base->base.gallivm->context), |
| 1556 | 4); |
| 1557 | } |
| 1558 | |
Marek Olšák | dbeedbb | 2013-10-31 15:08:49 +0100 | [diff] [blame] | 1559 | static void build_txq_intrinsic(const struct lp_build_tgsi_action * action, |
| 1560 | struct lp_build_tgsi_context * bld_base, |
| 1561 | struct lp_build_emit_data * emit_data) |
| 1562 | { |
| 1563 | if (emit_data->inst->Texture.Texture == TGSI_TEXTURE_BUFFER) { |
| 1564 | /* Just return the buffer size. */ |
| 1565 | emit_data->output[emit_data->chan] = emit_data->args[0]; |
| 1566 | return; |
| 1567 | } |
| 1568 | |
| 1569 | build_tgsi_intrinsic_nomem(action, bld_base, emit_data); |
| 1570 | } |
| 1571 | |
Michel Dänzer | a06ee5a | 2013-06-19 18:14:01 +0200 | [diff] [blame] | 1572 | #if HAVE_LLVM >= 0x0304 |
| 1573 | |
| 1574 | static void si_llvm_emit_ddxy( |
| 1575 | const struct lp_build_tgsi_action * action, |
| 1576 | struct lp_build_tgsi_context * bld_base, |
| 1577 | struct lp_build_emit_data * emit_data) |
| 1578 | { |
| 1579 | struct si_shader_context *si_shader_ctx = si_shader_context(bld_base); |
| 1580 | struct gallivm_state *gallivm = bld_base->base.gallivm; |
| 1581 | struct lp_build_context * base = &bld_base->base; |
| 1582 | const struct tgsi_full_instruction *inst = emit_data->inst; |
| 1583 | unsigned opcode = inst->Instruction.Opcode; |
| 1584 | LLVMValueRef indices[2]; |
| 1585 | LLVMValueRef store_ptr, load_ptr0, load_ptr1; |
| 1586 | LLVMValueRef tl, trbl, result[4]; |
| 1587 | LLVMTypeRef i32; |
| 1588 | unsigned swizzle[4]; |
| 1589 | unsigned c; |
| 1590 | |
| 1591 | i32 = LLVMInt32TypeInContext(gallivm->context); |
| 1592 | |
| 1593 | indices[0] = bld_base->uint_bld.zero; |
| 1594 | indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32, |
| 1595 | NULL, 0, LLVMReadNoneAttribute); |
| 1596 | store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds, |
| 1597 | indices, 2, ""); |
| 1598 | |
| 1599 | indices[1] = LLVMBuildAnd(gallivm->builder, indices[1], |
| 1600 | lp_build_const_int32(gallivm, 0xfffffffc), ""); |
| 1601 | load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds, |
| 1602 | indices, 2, ""); |
| 1603 | |
| 1604 | indices[1] = LLVMBuildAdd(gallivm->builder, indices[1], |
| 1605 | lp_build_const_int32(gallivm, |
| 1606 | opcode == TGSI_OPCODE_DDX ? 1 : 2), |
| 1607 | ""); |
| 1608 | load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds, |
| 1609 | indices, 2, ""); |
| 1610 | |
| 1611 | for (c = 0; c < 4; ++c) { |
| 1612 | unsigned i; |
| 1613 | |
| 1614 | swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c); |
| 1615 | for (i = 0; i < c; ++i) { |
| 1616 | if (swizzle[i] == swizzle[c]) { |
| 1617 | result[c] = result[i]; |
| 1618 | break; |
| 1619 | } |
| 1620 | } |
| 1621 | if (i != c) |
| 1622 | continue; |
| 1623 | |
| 1624 | LLVMBuildStore(gallivm->builder, |
| 1625 | LLVMBuildBitCast(gallivm->builder, |
| 1626 | lp_build_emit_fetch(bld_base, inst, 0, c), |
| 1627 | i32, ""), |
| 1628 | store_ptr); |
| 1629 | |
| 1630 | tl = LLVMBuildLoad(gallivm->builder, load_ptr0, ""); |
| 1631 | tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, ""); |
| 1632 | |
| 1633 | trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, ""); |
| 1634 | trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, ""); |
| 1635 | |
| 1636 | result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, ""); |
| 1637 | } |
| 1638 | |
| 1639 | emit_data->output[0] = lp_build_gather_values(gallivm, result, 4); |
| 1640 | } |
| 1641 | |
| 1642 | #endif /* HAVE_LLVM >= 0x0304 */ |
| 1643 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1644 | static const struct lp_build_tgsi_action tex_action = { |
| 1645 | .fetch_args = tex_fetch_args, |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 1646 | .emit = build_tex_intrinsic, |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 1647 | .intr_name = "llvm.SI.sample." |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1648 | }; |
| 1649 | |
Michel Dänzer | 3e20513 | 2012-11-06 17:39:01 +0100 | [diff] [blame] | 1650 | static const struct lp_build_tgsi_action txb_action = { |
| 1651 | .fetch_args = tex_fetch_args, |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 1652 | .emit = build_tex_intrinsic, |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 1653 | .intr_name = "llvm.SI.sampleb." |
Michel Dänzer | 3e20513 | 2012-11-06 17:39:01 +0100 | [diff] [blame] | 1654 | }; |
| 1655 | |
Michel Dänzer | a6b83c0 | 2013-02-21 16:10:55 +0100 | [diff] [blame] | 1656 | #if HAVE_LLVM >= 0x0304 |
| 1657 | static const struct lp_build_tgsi_action txd_action = { |
| 1658 | .fetch_args = tex_fetch_args, |
| 1659 | .emit = build_tex_intrinsic, |
| 1660 | .intr_name = "llvm.SI.sampled." |
| 1661 | }; |
| 1662 | #endif |
| 1663 | |
Michel Dänzer | 3623111 | 2013-05-02 09:44:45 +0200 | [diff] [blame] | 1664 | static const struct lp_build_tgsi_action txf_action = { |
| 1665 | .fetch_args = tex_fetch_args, |
| 1666 | .emit = build_tex_intrinsic, |
| 1667 | .intr_name = "llvm.SI.imageload." |
| 1668 | }; |
| 1669 | |
Michel Dänzer | 56ae9be | 2012-11-06 17:41:50 +0100 | [diff] [blame] | 1670 | static const struct lp_build_tgsi_action txl_action = { |
| 1671 | .fetch_args = tex_fetch_args, |
Michel Dänzer | 07eddc4 | 2013-02-06 15:43:10 +0100 | [diff] [blame] | 1672 | .emit = build_tex_intrinsic, |
Michel Dänzer | e5fb734 | 2013-01-24 18:54:51 +0100 | [diff] [blame] | 1673 | .intr_name = "llvm.SI.samplel." |
Michel Dänzer | 56ae9be | 2012-11-06 17:41:50 +0100 | [diff] [blame] | 1674 | }; |
| 1675 | |
Michel Dänzer | 0495adb | 2013-05-06 12:45:14 +0200 | [diff] [blame] | 1676 | static const struct lp_build_tgsi_action txq_action = { |
| 1677 | .fetch_args = txq_fetch_args, |
Marek Olšák | dbeedbb | 2013-10-31 15:08:49 +0100 | [diff] [blame] | 1678 | .emit = build_txq_intrinsic, |
Michel Dänzer | 0495adb | 2013-05-06 12:45:14 +0200 | [diff] [blame] | 1679 | .intr_name = "llvm.SI.resinfo" |
| 1680 | }; |
| 1681 | |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 1682 | static void create_meta_data(struct si_shader_context *si_shader_ctx) |
| 1683 | { |
| 1684 | struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm; |
| 1685 | LLVMValueRef args[3]; |
| 1686 | |
| 1687 | args[0] = LLVMMDStringInContext(gallivm->context, "const", 5); |
| 1688 | args[1] = 0; |
| 1689 | args[2] = lp_build_const_int32(gallivm, 1); |
| 1690 | |
| 1691 | si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3); |
| 1692 | } |
| 1693 | |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 1694 | static void create_function(struct si_shader_context *si_shader_ctx) |
| 1695 | { |
Michel Dänzer | a06ee5a | 2013-06-19 18:14:01 +0200 | [diff] [blame] | 1696 | struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base; |
| 1697 | struct gallivm_state *gallivm = bld_base->base.gallivm; |
Vadim Girlin | 453ea2d | 2013-10-13 19:53:54 +0400 | [diff] [blame] | 1698 | LLVMTypeRef params[21], f32, i8, i32, v2i32, v3i32; |
Marek Olšák | 13a1a8b | 2013-08-18 01:57:40 +0200 | [diff] [blame] | 1699 | unsigned i, last_sgpr, num_params; |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 1700 | |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 1701 | i8 = LLVMInt8TypeInContext(gallivm->context); |
Christian König | c497321 | 2013-03-05 12:14:02 +0100 | [diff] [blame] | 1702 | i32 = LLVMInt32TypeInContext(gallivm->context); |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 1703 | f32 = LLVMFloatTypeInContext(gallivm->context); |
| 1704 | v2i32 = LLVMVectorType(i32, 2); |
| 1705 | v3i32 = LLVMVectorType(i32, 3); |
Christian König | c497321 | 2013-03-05 12:14:02 +0100 | [diff] [blame] | 1706 | |
Vincent Lejeune | 6e51c2a | 2013-10-05 16:04:48 +0200 | [diff] [blame] | 1707 | params[SI_PARAM_CONST] = LLVMPointerType( |
| 1708 | LLVMArrayType(LLVMVectorType(i8, 16), NUM_CONST_BUFFERS), CONST_ADDR_SPACE); |
| 1709 | /* We assume at most 16 textures per program at the moment. |
| 1710 | * This need probably need to be changed to support bindless textures */ |
| 1711 | params[SI_PARAM_SAMPLER] = LLVMPointerType( |
| 1712 | LLVMArrayType(LLVMVectorType(i8, 16), NUM_SAMPLER_VIEWS), CONST_ADDR_SPACE); |
| 1713 | params[SI_PARAM_RESOURCE] = LLVMPointerType( |
| 1714 | LLVMArrayType(LLVMVectorType(i8, 32), NUM_SAMPLER_STATES), CONST_ADDR_SPACE); |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 1715 | |
Marek Olšák | 13a1a8b | 2013-08-18 01:57:40 +0200 | [diff] [blame] | 1716 | switch (si_shader_ctx->type) { |
| 1717 | case TGSI_PROCESSOR_VERTEX: |
| 1718 | params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_CONST]; |
Marek Olšák | 2993cca | 2013-08-18 02:34:23 +0200 | [diff] [blame] | 1719 | params[SI_PARAM_SO_BUFFER] = params[SI_PARAM_CONST]; |
Christian König | cf9b31f | 2013-03-21 18:30:23 +0100 | [diff] [blame] | 1720 | params[SI_PARAM_START_INSTANCE] = i32; |
Marek Olšák | 8d03d92 | 2013-09-01 23:59:06 +0200 | [diff] [blame] | 1721 | num_params = SI_PARAM_START_INSTANCE+1; |
| 1722 | |
| 1723 | /* The locations of the other parameters are assigned dynamically. */ |
| 1724 | |
| 1725 | /* Streamout SGPRs. */ |
| 1726 | if (si_shader_ctx->shader->selector->so.num_outputs) { |
| 1727 | params[si_shader_ctx->param_streamout_config = num_params++] = i32; |
| 1728 | params[si_shader_ctx->param_streamout_write_index = num_params++] = i32; |
| 1729 | } |
| 1730 | /* A streamout buffer offset is loaded if the stride is non-zero. */ |
| 1731 | for (i = 0; i < 4; i++) { |
| 1732 | if (!si_shader_ctx->shader->selector->so.stride[i]) |
| 1733 | continue; |
| 1734 | |
| 1735 | params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32; |
| 1736 | } |
| 1737 | |
| 1738 | last_sgpr = num_params-1; |
| 1739 | |
| 1740 | /* VGPRs */ |
| 1741 | params[si_shader_ctx->param_vertex_id = num_params++] = i32; |
| 1742 | params[num_params++] = i32; /* unused*/ |
| 1743 | params[num_params++] = i32; /* unused */ |
| 1744 | params[si_shader_ctx->param_instance_id = num_params++] = i32; |
Marek Olšák | 13a1a8b | 2013-08-18 01:57:40 +0200 | [diff] [blame] | 1745 | break; |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 1746 | |
Marek Olšák | 13a1a8b | 2013-08-18 01:57:40 +0200 | [diff] [blame] | 1747 | case TGSI_PROCESSOR_FRAGMENT: |
Vadim Girlin | 453ea2d | 2013-10-13 19:53:54 +0400 | [diff] [blame] | 1748 | params[SI_PARAM_ALPHA_REF] = f32; |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 1749 | params[SI_PARAM_PRIM_MASK] = i32; |
Marek Olšák | 13a1a8b | 2013-08-18 01:57:40 +0200 | [diff] [blame] | 1750 | last_sgpr = SI_PARAM_PRIM_MASK; |
Christian König | 0666ffd | 2013-03-05 15:07:39 +0100 | [diff] [blame] | 1751 | params[SI_PARAM_PERSP_SAMPLE] = v2i32; |
| 1752 | params[SI_PARAM_PERSP_CENTER] = v2i32; |
| 1753 | params[SI_PARAM_PERSP_CENTROID] = v2i32; |
| 1754 | params[SI_PARAM_PERSP_PULL_MODEL] = v3i32; |
| 1755 | params[SI_PARAM_LINEAR_SAMPLE] = v2i32; |
| 1756 | params[SI_PARAM_LINEAR_CENTER] = v2i32; |
| 1757 | params[SI_PARAM_LINEAR_CENTROID] = v2i32; |
| 1758 | params[SI_PARAM_LINE_STIPPLE_TEX] = f32; |
| 1759 | params[SI_PARAM_POS_X_FLOAT] = f32; |
| 1760 | params[SI_PARAM_POS_Y_FLOAT] = f32; |
| 1761 | params[SI_PARAM_POS_Z_FLOAT] = f32; |
| 1762 | params[SI_PARAM_POS_W_FLOAT] = f32; |
| 1763 | params[SI_PARAM_FRONT_FACE] = f32; |
| 1764 | params[SI_PARAM_ANCILLARY] = f32; |
| 1765 | params[SI_PARAM_SAMPLE_COVERAGE] = f32; |
| 1766 | params[SI_PARAM_POS_FIXED_PT] = f32; |
Marek Olšák | 13a1a8b | 2013-08-18 01:57:40 +0200 | [diff] [blame] | 1767 | num_params = SI_PARAM_POS_FIXED_PT+1; |
| 1768 | break; |
| 1769 | |
| 1770 | default: |
| 1771 | assert(0 && "unimplemented shader"); |
| 1772 | return; |
Christian König | c497321 | 2013-03-05 12:14:02 +0100 | [diff] [blame] | 1773 | } |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 1774 | |
Marek Olšák | 13a1a8b | 2013-08-18 01:57:40 +0200 | [diff] [blame] | 1775 | assert(num_params <= Elements(params)); |
| 1776 | radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params); |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 1777 | radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type); |
Christian König | cf9b31f | 2013-03-21 18:30:23 +0100 | [diff] [blame] | 1778 | |
Marek Olšák | 13a1a8b | 2013-08-18 01:57:40 +0200 | [diff] [blame] | 1779 | for (i = 0; i <= last_sgpr; ++i) { |
| 1780 | LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i); |
Vincent Lejeune | 6e51c2a | 2013-10-05 16:04:48 +0200 | [diff] [blame] | 1781 | switch (i) { |
| 1782 | default: |
| 1783 | LLVMAddAttribute(P, LLVMInRegAttribute); |
| 1784 | break; |
| 1785 | #if HAVE_LLVM >= 0x0304 |
| 1786 | /* We tell llvm that array inputs are passed by value to allow Sinking pass |
| 1787 | * to move load. Inputs are constant so this is fine. */ |
| 1788 | case SI_PARAM_CONST: |
| 1789 | case SI_PARAM_SAMPLER: |
| 1790 | case SI_PARAM_RESOURCE: |
| 1791 | LLVMAddAttribute(P, LLVMByValAttribute); |
| 1792 | break; |
| 1793 | #endif |
| 1794 | } |
Christian König | cf9b31f | 2013-03-21 18:30:23 +0100 | [diff] [blame] | 1795 | } |
Michel Dänzer | a06ee5a | 2013-06-19 18:14:01 +0200 | [diff] [blame] | 1796 | |
| 1797 | #if HAVE_LLVM >= 0x0304 |
| 1798 | if (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 || |
| 1799 | bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0) |
| 1800 | si_shader_ctx->ddxy_lds = |
| 1801 | LLVMAddGlobalInAddressSpace(gallivm->module, |
| 1802 | LLVMArrayType(i32, 64), |
| 1803 | "ddxy_lds", |
| 1804 | LOCAL_ADDR_SPACE); |
| 1805 | #endif |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 1806 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1807 | |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 1808 | static void preload_constants(struct si_shader_context *si_shader_ctx) |
| 1809 | { |
| 1810 | struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base; |
| 1811 | struct gallivm_state * gallivm = bld_base->base.gallivm; |
| 1812 | const struct tgsi_shader_info * info = bld_base->info; |
Marek Olšák | 2fd4200 | 2013-10-25 11:45:47 +0200 | [diff] [blame] | 1813 | unsigned buf; |
| 1814 | LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST); |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 1815 | |
Marek Olšák | 2fd4200 | 2013-10-25 11:45:47 +0200 | [diff] [blame] | 1816 | for (buf = 0; buf < NUM_CONST_BUFFERS; buf++) { |
| 1817 | unsigned i, num_const = info->const_file_max[buf] + 1; |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 1818 | |
Marek Olšák | 2fd4200 | 2013-10-25 11:45:47 +0200 | [diff] [blame] | 1819 | if (num_const == 0) |
| 1820 | continue; |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 1821 | |
Marek Olšák | 2fd4200 | 2013-10-25 11:45:47 +0200 | [diff] [blame] | 1822 | /* Allocate space for the constant values */ |
| 1823 | si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef)); |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 1824 | |
Marek Olšák | 2fd4200 | 2013-10-25 11:45:47 +0200 | [diff] [blame] | 1825 | /* Load the resource descriptor */ |
| 1826 | si_shader_ctx->const_resource[buf] = |
| 1827 | build_indexed_load(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf)); |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 1828 | |
Marek Olšák | 2fd4200 | 2013-10-25 11:45:47 +0200 | [diff] [blame] | 1829 | /* Load the constants, we rely on the code sinking to do the rest */ |
| 1830 | for (i = 0; i < num_const * 4; ++i) { |
| 1831 | LLVMValueRef args[2] = { |
| 1832 | si_shader_ctx->const_resource[buf], |
| 1833 | lp_build_const_int32(gallivm, i * 4) |
| 1834 | }; |
| 1835 | si_shader_ctx->constants[buf][i] = |
| 1836 | build_intrinsic(gallivm->builder, "llvm.SI.load.const", |
| 1837 | bld_base->base.elem_type, args, 2, |
| 1838 | LLVMReadNoneAttribute | LLVMNoUnwindAttribute); |
| 1839 | } |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 1840 | } |
| 1841 | } |
| 1842 | |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 1843 | static void preload_samplers(struct si_shader_context *si_shader_ctx) |
| 1844 | { |
| 1845 | struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base; |
| 1846 | struct gallivm_state * gallivm = bld_base->base.gallivm; |
| 1847 | const struct tgsi_shader_info * info = bld_base->info; |
| 1848 | |
| 1849 | unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1; |
| 1850 | |
| 1851 | LLVMValueRef res_ptr, samp_ptr; |
| 1852 | LLVMValueRef offset; |
| 1853 | |
| 1854 | if (num_samplers == 0) |
| 1855 | return; |
| 1856 | |
| 1857 | /* Allocate space for the values */ |
Marek Olšák | 4855acd | 2013-08-06 15:08:54 +0200 | [diff] [blame] | 1858 | si_shader_ctx->resources = CALLOC(NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef)); |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 1859 | si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef)); |
| 1860 | |
| 1861 | res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE); |
| 1862 | samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER); |
| 1863 | |
| 1864 | /* Load the resources and samplers, we rely on the code sinking to do the rest */ |
| 1865 | for (i = 0; i < num_samplers; ++i) { |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 1866 | /* Resource */ |
| 1867 | offset = lp_build_const_int32(gallivm, i); |
| 1868 | si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset); |
| 1869 | |
| 1870 | /* Sampler */ |
| 1871 | offset = lp_build_const_int32(gallivm, i); |
| 1872 | 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] | 1873 | |
| 1874 | /* FMASK resource */ |
| 1875 | if (info->is_msaa_sampler[i]) { |
| 1876 | offset = lp_build_const_int32(gallivm, FMASK_TEX_OFFSET + i); |
| 1877 | si_shader_ctx->resources[FMASK_TEX_OFFSET + i] = |
| 1878 | build_indexed_load(si_shader_ctx, res_ptr, offset); |
| 1879 | } |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 1880 | } |
| 1881 | } |
| 1882 | |
Marek Olšák | 8d03d92 | 2013-09-01 23:59:06 +0200 | [diff] [blame] | 1883 | static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx) |
| 1884 | { |
| 1885 | struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base; |
| 1886 | struct gallivm_state * gallivm = bld_base->base.gallivm; |
| 1887 | unsigned i; |
| 1888 | |
| 1889 | if (!si_shader_ctx->shader->selector->so.num_outputs) |
| 1890 | return; |
| 1891 | |
| 1892 | LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, |
| 1893 | SI_PARAM_SO_BUFFER); |
| 1894 | |
| 1895 | /* Load the resources, we rely on the code sinking to do the rest */ |
| 1896 | for (i = 0; i < 4; ++i) { |
| 1897 | if (si_shader_ctx->shader->selector->so.stride[i]) { |
| 1898 | LLVMValueRef offset = lp_build_const_int32(gallivm, i); |
| 1899 | |
| 1900 | si_shader_ctx->so_buffers[i] = build_indexed_load(si_shader_ctx, buf_ptr, offset); |
| 1901 | } |
| 1902 | } |
| 1903 | } |
| 1904 | |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1905 | int si_compile_llvm(struct r600_context *rctx, struct si_pipe_shader *shader, |
| 1906 | LLVMModuleRef mod) |
| 1907 | { |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1908 | unsigned i; |
| 1909 | uint32_t *ptr; |
Tom Stellard | 7782d19 | 2013-04-04 09:57:13 -0700 | [diff] [blame] | 1910 | struct radeon_llvm_binary binary; |
Tom Stellard | b280516 | 2013-10-03 17:39:59 -0400 | [diff] [blame] | 1911 | bool dump = r600_can_dump_shader(&rctx->screen->b, |
| 1912 | shader->selector ? shader->selector->tokens : NULL); |
Tom Stellard | 7782d19 | 2013-04-04 09:57:13 -0700 | [diff] [blame] | 1913 | memset(&binary, 0, sizeof(binary)); |
| 1914 | radeon_llvm_compile(mod, &binary, |
Marek Olšák | a81c3e0 | 2013-08-14 01:04:39 +0200 | [diff] [blame] | 1915 | r600_get_llvm_processor_name(rctx->screen->b.family), dump); |
Jay Cornwall | d7d539a | 2013-10-10 20:06:48 -0500 | [diff] [blame] | 1916 | if (dump && ! binary.disassembled) { |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1917 | fprintf(stderr, "SI CODE:\n"); |
Tom Stellard | 7782d19 | 2013-04-04 09:57:13 -0700 | [diff] [blame] | 1918 | for (i = 0; i < binary.code_size; i+=4 ) { |
| 1919 | fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3], |
| 1920 | binary.code[i + 2], binary.code[i + 1], |
| 1921 | binary.code[i]); |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1922 | } |
| 1923 | } |
| 1924 | |
Tom Stellard | d50343d | 2013-04-04 16:21:06 -0400 | [diff] [blame] | 1925 | /* XXX: We may be able to emit some of these values directly rather than |
| 1926 | * extracting fields to be emitted later. |
| 1927 | */ |
| 1928 | for (i = 0; i < binary.config_size; i+= 8) { |
| 1929 | unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i)); |
| 1930 | unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4)); |
| 1931 | switch (reg) { |
| 1932 | case R_00B028_SPI_SHADER_PGM_RSRC1_PS: |
| 1933 | case R_00B128_SPI_SHADER_PGM_RSRC1_VS: |
| 1934 | case R_00B228_SPI_SHADER_PGM_RSRC1_GS: |
| 1935 | case R_00B848_COMPUTE_PGM_RSRC1: |
| 1936 | shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8; |
| 1937 | shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4; |
| 1938 | break; |
Michel Dänzer | a06ee5a | 2013-06-19 18:14:01 +0200 | [diff] [blame] | 1939 | case R_00B02C_SPI_SHADER_PGM_RSRC2_PS: |
| 1940 | shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value); |
| 1941 | break; |
| 1942 | case R_00B84C_COMPUTE_PGM_RSRC2: |
| 1943 | shader->lds_size = G_00B84C_LDS_SIZE(value); |
| 1944 | break; |
Tom Stellard | d50343d | 2013-04-04 16:21:06 -0400 | [diff] [blame] | 1945 | case R_0286CC_SPI_PS_INPUT_ENA: |
| 1946 | shader->spi_ps_input_ena = value; |
| 1947 | break; |
| 1948 | default: |
| 1949 | fprintf(stderr, "Warning: Compiler emitted unknown " |
| 1950 | "config register: 0x%x\n", reg); |
| 1951 | break; |
| 1952 | } |
| 1953 | } |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1954 | |
| 1955 | /* copy new shader */ |
Marek Olšák | a81c3e0 | 2013-08-14 01:04:39 +0200 | [diff] [blame] | 1956 | r600_resource_reference(&shader->bo, NULL); |
| 1957 | shader->bo = r600_resource_create_custom(rctx->b.b.screen, PIPE_USAGE_IMMUTABLE, |
Tom Stellard | d50343d | 2013-04-04 16:21:06 -0400 | [diff] [blame] | 1958 | binary.code_size); |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1959 | if (shader->bo == NULL) { |
| 1960 | return -ENOMEM; |
| 1961 | } |
| 1962 | |
Marek Olšák | a81c3e0 | 2013-08-14 01:04:39 +0200 | [diff] [blame] | 1963 | ptr = (uint32_t*)rctx->b.ws->buffer_map(shader->bo->cs_buf, rctx->b.rings.gfx.cs, PIPE_TRANSFER_WRITE); |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1964 | if (0 /*R600_BIG_ENDIAN*/) { |
Tom Stellard | d50343d | 2013-04-04 16:21:06 -0400 | [diff] [blame] | 1965 | for (i = 0; i < binary.code_size / 4; ++i) { |
| 1966 | ptr[i] = util_bswap32(*(uint32_t*)(binary.code + i*4)); |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1967 | } |
| 1968 | } else { |
Tom Stellard | d50343d | 2013-04-04 16:21:06 -0400 | [diff] [blame] | 1969 | memcpy(ptr, binary.code, binary.code_size); |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1970 | } |
Marek Olšák | a81c3e0 | 2013-08-14 01:04:39 +0200 | [diff] [blame] | 1971 | rctx->b.ws->buffer_unmap(shader->bo->cs_buf); |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1972 | |
Tom Stellard | 7782d19 | 2013-04-04 09:57:13 -0700 | [diff] [blame] | 1973 | free(binary.code); |
| 1974 | free(binary.config); |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1975 | |
| 1976 | return 0; |
| 1977 | } |
| 1978 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1979 | int si_pipe_shader_create( |
| 1980 | struct pipe_context *ctx, |
Christian König | a0dca44 | 2013-03-22 15:59:22 +0100 | [diff] [blame] | 1981 | struct si_pipe_shader *shader) |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1982 | { |
| 1983 | struct r600_context *rctx = (struct r600_context*)ctx; |
Michel Dänzer | d1e40b3 | 2012-08-23 17:10:37 +0200 | [diff] [blame] | 1984 | struct si_pipe_shader_selector *sel = shader->selector; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1985 | struct si_shader_context si_shader_ctx; |
| 1986 | struct tgsi_shader_info shader_info; |
| 1987 | struct lp_build_tgsi_context * bld_base; |
| 1988 | LLVMModuleRef mod; |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 1989 | int r = 0; |
Marek Olšák | 0cb9de1 | 2013-09-22 15:34:12 +0200 | [diff] [blame] | 1990 | bool dump = r600_can_dump_shader(&rctx->screen->b, shader->selector->tokens); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1991 | |
Michel Dänzer | 82e38ac | 2012-09-27 16:39:26 +0200 | [diff] [blame] | 1992 | assert(shader->shader.noutput == 0); |
| 1993 | assert(shader->shader.ninterp == 0); |
| 1994 | assert(shader->shader.ninput == 0); |
| 1995 | |
Michel Dänzer | cfebaf9 | 2012-08-31 19:04:08 +0200 | [diff] [blame] | 1996 | memset(&si_shader_ctx, 0, sizeof(si_shader_ctx)); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1997 | radeon_llvm_context_init(&si_shader_ctx.radeon_bld); |
| 1998 | bld_base = &si_shader_ctx.radeon_bld.soa.bld_base; |
| 1999 | |
Michel Dänzer | d1e40b3 | 2012-08-23 17:10:37 +0200 | [diff] [blame] | 2000 | tgsi_scan_shader(sel->tokens, &shader_info); |
Michel Dänzer | a06ee5a | 2013-06-19 18:14:01 +0200 | [diff] [blame] | 2001 | |
Michel Dänzer | e44dfd4 | 2012-11-07 17:33:08 +0100 | [diff] [blame] | 2002 | shader->shader.uses_kill = shader_info.uses_kill; |
Christian König | e4ed587 | 2013-03-21 18:02:52 +0100 | [diff] [blame] | 2003 | shader->shader.uses_instanceid = shader_info.uses_instanceid; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 2004 | bld_base->info = &shader_info; |
| 2005 | bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 2006 | bld_base->emit_epilogue = si_llvm_emit_epilogue; |
| 2007 | |
| 2008 | bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action; |
Michel Dänzer | 3e20513 | 2012-11-06 17:39:01 +0100 | [diff] [blame] | 2009 | bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action; |
Michel Dänzer | a6b83c0 | 2013-02-21 16:10:55 +0100 | [diff] [blame] | 2010 | #if HAVE_LLVM >= 0x0304 |
| 2011 | bld_base->op_actions[TGSI_OPCODE_TXD] = txd_action; |
| 2012 | #endif |
Michel Dänzer | 3623111 | 2013-05-02 09:44:45 +0200 | [diff] [blame] | 2013 | bld_base->op_actions[TGSI_OPCODE_TXF] = txf_action; |
Michel Dänzer | 56ae9be | 2012-11-06 17:41:50 +0100 | [diff] [blame] | 2014 | bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action; |
Michel Dänzer | c2bae6b | 2012-08-02 17:19:22 +0200 | [diff] [blame] | 2015 | bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action; |
Michel Dänzer | 0495adb | 2013-05-06 12:45:14 +0200 | [diff] [blame] | 2016 | bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 2017 | |
Michel Dänzer | a06ee5a | 2013-06-19 18:14:01 +0200 | [diff] [blame] | 2018 | #if HAVE_LLVM >= 0x0304 |
| 2019 | bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy; |
| 2020 | bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy; |
| 2021 | #endif |
| 2022 | |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 2023 | si_shader_ctx.radeon_bld.load_input = declare_input; |
Christian König | e4ed587 | 2013-03-21 18:02:52 +0100 | [diff] [blame] | 2024 | si_shader_ctx.radeon_bld.load_system_value = declare_system_value; |
Michel Dänzer | d1e40b3 | 2012-08-23 17:10:37 +0200 | [diff] [blame] | 2025 | si_shader_ctx.tokens = sel->tokens; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 2026 | tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens); |
| 2027 | si_shader_ctx.shader = shader; |
| 2028 | si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 2029 | |
Christian König | 206f059 | 2013-03-20 14:37:21 +0100 | [diff] [blame] | 2030 | create_meta_data(&si_shader_ctx); |
Christian König | 55fe5cc | 2013-03-04 16:30:06 +0100 | [diff] [blame] | 2031 | create_function(&si_shader_ctx); |
Christian König | 0f6cf2b | 2013-03-15 15:53:25 +0100 | [diff] [blame] | 2032 | preload_constants(&si_shader_ctx); |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 2033 | preload_samplers(&si_shader_ctx); |
Marek Olšák | 8d03d92 | 2013-09-01 23:59:06 +0200 | [diff] [blame] | 2034 | preload_streamout_buffers(&si_shader_ctx); |
Christian König | b8f4ca3 | 2013-03-04 15:35:30 +0100 | [diff] [blame] | 2035 | |
Christian König | 835098a | 2012-07-17 21:28:10 +0200 | [diff] [blame] | 2036 | shader->shader.nr_cbufs = rctx->framebuffer.nr_cbufs; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 2037 | |
Tom Stellard | 185fc9a | 2012-07-12 10:40:47 -0400 | [diff] [blame] | 2038 | /* Dump TGSI code before doing TGSI->LLVM conversion in case the |
| 2039 | * conversion fails. */ |
| 2040 | if (dump) { |
Michel Dänzer | d1e40b3 | 2012-08-23 17:10:37 +0200 | [diff] [blame] | 2041 | tgsi_dump(sel->tokens, 0); |
Marek Olšák | 8d03d92 | 2013-09-01 23:59:06 +0200 | [diff] [blame] | 2042 | si_dump_streamout(&sel->so); |
Tom Stellard | 185fc9a | 2012-07-12 10:40:47 -0400 | [diff] [blame] | 2043 | } |
| 2044 | |
Michel Dänzer | d1e40b3 | 2012-08-23 17:10:37 +0200 | [diff] [blame] | 2045 | if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) { |
Michel Dänzer | 82cd9c0 | 2012-08-08 15:35:42 +0200 | [diff] [blame] | 2046 | fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n"); |
Marek Olšák | 2fd4200 | 2013-10-25 11:45:47 +0200 | [diff] [blame] | 2047 | for (int i = 0; i < NUM_CONST_BUFFERS; i++) |
| 2048 | FREE(si_shader_ctx.constants[i]); |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 2049 | FREE(si_shader_ctx.resources); |
| 2050 | FREE(si_shader_ctx.samplers); |
Michel Dänzer | 82cd9c0 | 2012-08-08 15:35:42 +0200 | [diff] [blame] | 2051 | return -EINVAL; |
| 2052 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 2053 | |
| 2054 | radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld); |
| 2055 | |
| 2056 | mod = bld_base->base.gallivm->module; |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 2057 | r = si_compile_llvm(rctx, shader, mod); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 2058 | |
Michel Dänzer | 4b64fa2 | 2012-08-15 18:22:46 +0200 | [diff] [blame] | 2059 | radeon_llvm_dispose(&si_shader_ctx.radeon_bld); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 2060 | tgsi_parse_free(&si_shader_ctx.parse); |
| 2061 | |
Marek Olšák | 2fd4200 | 2013-10-25 11:45:47 +0200 | [diff] [blame] | 2062 | for (int i = 0; i < NUM_CONST_BUFFERS; i++) |
| 2063 | FREE(si_shader_ctx.constants[i]); |
Christian König | 1c10018 | 2013-03-17 16:02:42 +0100 | [diff] [blame] | 2064 | FREE(si_shader_ctx.resources); |
| 2065 | FREE(si_shader_ctx.samplers); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 2066 | |
Tom Stellard | 302f53d | 2012-10-25 13:50:10 -0400 | [diff] [blame] | 2067 | return r; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader) |
| 2071 | { |
Marek Olšák | a81c3e0 | 2013-08-14 01:04:39 +0200 | [diff] [blame] | 2072 | r600_resource_reference(&shader->bo, NULL); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 2073 | } |