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