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