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