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