Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 1 | |
| 2 | #include "gallivm/lp_bld_tgsi_action.h" |
| 3 | #include "gallivm/lp_bld_const.h" |
| 4 | #include "gallivm/lp_bld_intr.h" |
| 5 | #include "gallivm/lp_bld_tgsi.h" |
| 6 | #include "radeon_llvm.h" |
Tom Stellard | 509ddb0 | 2012-04-16 17:48:44 -0400 | [diff] [blame] | 7 | #include "radeon_llvm_emit.h" |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 8 | #include "tgsi/tgsi_info.h" |
| 9 | #include "tgsi/tgsi_parse.h" |
| 10 | #include "tgsi/tgsi_scan.h" |
| 11 | #include "tgsi/tgsi_dump.h" |
| 12 | |
| 13 | #include "radeonsi_pipe.h" |
| 14 | #include "radeonsi_shader.h" |
| 15 | #include "sid.h" |
| 16 | |
| 17 | #include <assert.h> |
| 18 | #include <errno.h> |
| 19 | #include <stdio.h> |
| 20 | |
| 21 | /* |
| 22 | static ps_remap_inputs( |
| 23 | struct tgsi_llvm_context * tl_ctx, |
| 24 | unsigned tgsi_index, |
| 25 | unsigned tgsi_chan) |
| 26 | { |
| 27 | : |
| 28 | } |
| 29 | |
| 30 | struct si_input |
| 31 | { |
| 32 | struct list_head head; |
| 33 | unsigned tgsi_index; |
| 34 | unsigned tgsi_chan; |
| 35 | unsigned order; |
| 36 | }; |
| 37 | */ |
| 38 | |
| 39 | |
| 40 | struct si_shader_context |
| 41 | { |
| 42 | struct radeon_llvm_context radeon_bld; |
| 43 | struct r600_context *rctx; |
| 44 | struct tgsi_parse_context parse; |
| 45 | struct tgsi_token * tokens; |
| 46 | struct si_pipe_shader *shader; |
| 47 | unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */ |
| 48 | /* unsigned num_inputs; */ |
| 49 | /* struct list_head inputs; */ |
| 50 | /* unsigned * input_mappings *//* From TGSI to SI hw */ |
| 51 | /* struct tgsi_shader_info info;*/ |
| 52 | }; |
| 53 | |
| 54 | static struct si_shader_context * si_shader_context( |
| 55 | struct lp_build_tgsi_context * bld_base) |
| 56 | { |
| 57 | return (struct si_shader_context *)bld_base; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | #define PERSPECTIVE_BASE 0 |
| 62 | #define LINEAR_BASE 9 |
| 63 | |
| 64 | #define SAMPLE_OFFSET 0 |
| 65 | #define CENTER_OFFSET 2 |
| 66 | #define CENTROID_OFSET 4 |
| 67 | |
| 68 | #define USE_SGPR_MAX_SUFFIX_LEN 5 |
| 69 | |
| 70 | enum sgpr_type { |
| 71 | SGPR_I32, |
| 72 | SGPR_I64, |
| 73 | SGPR_PTR_V4I32, |
| 74 | SGPR_PTR_V8I32 |
| 75 | }; |
| 76 | |
| 77 | static LLVMValueRef use_sgpr( |
| 78 | struct gallivm_state * gallivm, |
| 79 | enum sgpr_type type, |
| 80 | unsigned sgpr) |
| 81 | { |
| 82 | LLVMValueRef sgpr_index; |
| 83 | LLVMValueRef sgpr_value; |
| 84 | LLVMTypeRef ret_type; |
| 85 | |
| 86 | sgpr_index = lp_build_const_int32(gallivm, sgpr); |
| 87 | |
| 88 | if (type == SGPR_I32) { |
| 89 | ret_type = LLVMInt32TypeInContext(gallivm->context); |
| 90 | return lp_build_intrinsic_unary(gallivm->builder, |
| 91 | "llvm.SI.use.sgpr.i32", |
| 92 | ret_type, sgpr_index); |
| 93 | } |
| 94 | |
| 95 | ret_type = LLVMInt64TypeInContext(gallivm->context); |
| 96 | sgpr_value = lp_build_intrinsic_unary(gallivm->builder, |
| 97 | "llvm.SI.use.sgpr.i64", |
| 98 | ret_type, sgpr_index); |
| 99 | |
| 100 | switch (type) { |
| 101 | case SGPR_I64: |
| 102 | return sgpr_value; |
| 103 | case SGPR_PTR_V4I32: |
| 104 | ret_type = LLVMInt32TypeInContext(gallivm->context); |
| 105 | ret_type = LLVMVectorType(ret_type, 4); |
| 106 | ret_type = LLVMPointerType(ret_type, |
| 107 | 0 /*XXX: Specify address space*/); |
| 108 | return LLVMBuildIntToPtr(gallivm->builder, sgpr_value, |
| 109 | ret_type, ""); |
| 110 | case SGPR_PTR_V8I32: |
| 111 | ret_type = LLVMInt32TypeInContext(gallivm->context); |
| 112 | ret_type = LLVMVectorType(ret_type, 8); |
| 113 | ret_type = LLVMPointerType(ret_type, |
| 114 | 0 /*XXX: Specify address space*/); |
| 115 | return LLVMBuildIntToPtr(gallivm->builder, sgpr_value, |
| 116 | ret_type, ""); |
| 117 | default: |
| 118 | assert(!"Unsupported SGPR type in use_sgpr()"); |
| 119 | return NULL; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | static void declare_input_vs( |
| 124 | struct si_shader_context * si_shader_ctx, |
| 125 | unsigned input_index, |
| 126 | const struct tgsi_full_declaration *decl) |
| 127 | { |
| 128 | LLVMValueRef t_list_ptr; |
| 129 | LLVMValueRef t_offset; |
| 130 | LLVMValueRef attribute_offset; |
| 131 | LLVMValueRef buffer_index_reg; |
| 132 | LLVMValueRef args[4]; |
| 133 | LLVMTypeRef vec4_type; |
| 134 | LLVMValueRef input; |
| 135 | struct lp_build_context * uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld; |
| 136 | struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base; |
| 137 | struct r600_context *rctx = si_shader_ctx->rctx; |
| 138 | struct pipe_vertex_element *velem = &rctx->vertex_elements->elements[input_index]; |
| 139 | unsigned chan; |
| 140 | |
| 141 | /* XXX: Communicate with the rest of the driver about which SGPR the T# |
Michel Dänzer | 9918fbd | 2012-04-19 11:41:48 +0200 | [diff] [blame] | 142 | * list pointer is going to be stored in. Hard code to SGPR[6:7] for |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 143 | * now */ |
Michel Dänzer | 9918fbd | 2012-04-19 11:41:48 +0200 | [diff] [blame] | 144 | t_list_ptr = use_sgpr(base->gallivm, SGPR_I64, 3); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 145 | |
| 146 | t_offset = lp_build_const_int32(base->gallivm, |
| 147 | 4 * velem->vertex_buffer_index); |
| 148 | attribute_offset = lp_build_const_int32(base->gallivm, velem->src_offset); |
| 149 | |
| 150 | /* Load the buffer index is always, which is always stored in VGPR0 |
| 151 | * for Vertex Shaders */ |
| 152 | buffer_index_reg = lp_build_intrinsic(base->gallivm->builder, |
| 153 | "llvm.SI.vs.load.buffer.index", uint->elem_type, NULL, 0); |
| 154 | |
| 155 | vec4_type = LLVMVectorType(base->elem_type, 4); |
| 156 | args[0] = t_list_ptr; |
| 157 | args[1] = t_offset; |
| 158 | args[2] = attribute_offset; |
| 159 | args[3] = buffer_index_reg; |
| 160 | input = lp_build_intrinsic(base->gallivm->builder, |
| 161 | "llvm.SI.vs.load.input", vec4_type, args, 4); |
| 162 | |
| 163 | /* Break up the vec4 into individual components */ |
| 164 | for (chan = 0; chan < 4; chan++) { |
| 165 | LLVMValueRef llvm_chan = lp_build_const_int32(base->gallivm, chan); |
| 166 | /* XXX: Use a helper function for this. There is one in |
| 167 | * tgsi_llvm.c. */ |
| 168 | si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] = |
| 169 | LLVMBuildExtractElement(base->gallivm->builder, |
| 170 | input, llvm_chan, ""); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | static void declare_input_fs( |
| 175 | struct si_shader_context * si_shader_ctx, |
| 176 | unsigned input_index, |
| 177 | const struct tgsi_full_declaration *decl) |
| 178 | { |
| 179 | const char * intr_name; |
| 180 | unsigned chan; |
| 181 | struct lp_build_context * base = |
| 182 | &si_shader_ctx->radeon_bld.soa.bld_base.base; |
| 183 | struct gallivm_state * gallivm = base->gallivm; |
| 184 | |
| 185 | /* This value is: |
| 186 | * [15:0] NewPrimMask (Bit mask for each quad. It is set it the |
| 187 | * quad begins a new primitive. Bit 0 always needs |
| 188 | * to be unset) |
| 189 | * [32:16] ParamOffset |
| 190 | * |
| 191 | */ |
Michel Dänzer | 9918fbd | 2012-04-19 11:41:48 +0200 | [diff] [blame] | 192 | /* XXX: This register number must be identical to the S_00B02C_USER_SGPR |
| 193 | * register field value |
| 194 | */ |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 195 | LLVMValueRef params = use_sgpr(base->gallivm, SGPR_I32, 6); |
| 196 | |
| 197 | |
| 198 | /* XXX: Is this the input_index? */ |
| 199 | LLVMValueRef attr_number = lp_build_const_int32(gallivm, input_index); |
| 200 | |
| 201 | /* XXX: Handle all possible interpolation modes */ |
Francisco Jerez | 1279923 | 2012-04-30 18:27:52 +0200 | [diff] [blame] | 202 | switch (decl->Interp.Interpolate) { |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 203 | case TGSI_INTERPOLATE_COLOR: |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame^] | 204 | if (si_shader_ctx->rctx->rasterizer->flatshade) { |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 205 | intr_name = "llvm.SI.fs.interp.constant"; |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame^] | 206 | } else { |
| 207 | if (decl->Interp.Centroid) |
| 208 | intr_name = "llvm.SI.fs.interp.persp.centroid"; |
| 209 | else |
| 210 | intr_name = "llvm.SI.fs.interp.persp.center"; |
| 211 | } |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 212 | break; |
| 213 | case TGSI_INTERPOLATE_CONSTANT: |
| 214 | intr_name = "llvm.SI.fs.interp.constant"; |
| 215 | break; |
| 216 | case TGSI_INTERPOLATE_LINEAR: |
Michel Dänzer | 1deb2be | 2012-05-14 16:26:19 +0200 | [diff] [blame^] | 217 | if (decl->Interp.Centroid) |
| 218 | intr_name = "llvm.SI.fs.interp.linear.centroid"; |
| 219 | else |
| 220 | intr_name = "llvm.SI.fs.interp.linear.center"; |
| 221 | break; |
| 222 | case TGSI_INTERPOLATE_PERSPECTIVE: |
| 223 | if (decl->Interp.Centroid) |
| 224 | intr_name = "llvm.SI.fs.interp.persp.centroid"; |
| 225 | else |
| 226 | intr_name = "llvm.SI.fs.interp.persp.center"; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 227 | break; |
| 228 | default: |
| 229 | fprintf(stderr, "Warning: Unhandled interpolation mode.\n"); |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */ |
| 234 | for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) { |
| 235 | LLVMValueRef args[3]; |
| 236 | LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan); |
| 237 | unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan); |
| 238 | LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context); |
| 239 | args[0] = llvm_chan; |
| 240 | args[1] = attr_number; |
| 241 | args[2] = params; |
| 242 | si_shader_ctx->radeon_bld.inputs[soa_index] = |
| 243 | lp_build_intrinsic(gallivm->builder, intr_name, |
| 244 | input_type, args, 3); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | static void declare_input( |
| 249 | struct radeon_llvm_context * radeon_bld, |
| 250 | unsigned input_index, |
| 251 | const struct tgsi_full_declaration *decl) |
| 252 | { |
| 253 | struct si_shader_context * si_shader_ctx = |
| 254 | si_shader_context(&radeon_bld->soa.bld_base); |
| 255 | if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) { |
| 256 | declare_input_vs(si_shader_ctx, input_index, decl); |
| 257 | } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) { |
| 258 | declare_input_fs(si_shader_ctx, input_index, decl); |
| 259 | } else { |
| 260 | fprintf(stderr, "Warning: Unsupported shader type,\n"); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | static LLVMValueRef fetch_constant( |
| 265 | struct lp_build_tgsi_context * bld_base, |
| 266 | const struct tgsi_full_src_register *reg, |
| 267 | enum tgsi_opcode_type type, |
| 268 | unsigned swizzle) |
| 269 | { |
| 270 | struct lp_build_context * base = &bld_base->base; |
| 271 | |
| 272 | LLVMValueRef const_ptr; |
| 273 | LLVMValueRef offset; |
| 274 | |
| 275 | /* XXX: Assume the pointer to the constant buffer is being stored in |
Michel Dänzer | 9918fbd | 2012-04-19 11:41:48 +0200 | [diff] [blame] | 276 | * SGPR[0:1] */ |
| 277 | const_ptr = use_sgpr(base->gallivm, SGPR_I64, 0); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 278 | |
| 279 | /* XXX: This assumes that the constant buffer is not packed, so |
| 280 | * CONST[0].x will have an offset of 0 and CONST[1].x will have an |
| 281 | * offset of 4. */ |
| 282 | offset = lp_build_const_int32(base->gallivm, |
| 283 | (reg->Register.Index * 4) + swizzle); |
| 284 | |
| 285 | return lp_build_intrinsic_binary(base->gallivm->builder, |
| 286 | "llvm.SI.load.const", base->elem_type, const_ptr, offset); |
| 287 | } |
| 288 | |
| 289 | |
| 290 | /* Declare some intrinsics with the correct attributes */ |
| 291 | static void si_llvm_emit_prologue(struct lp_build_tgsi_context * bld_base) |
| 292 | { |
| 293 | LLVMValueRef function; |
| 294 | struct gallivm_state * gallivm = bld_base->base.gallivm; |
| 295 | |
| 296 | LLVMTypeRef i64 = LLVMInt64TypeInContext(gallivm->context); |
| 297 | LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context); |
| 298 | |
| 299 | /* declare i32 @llvm.SI.use.sgpr.i32(i32) */ |
| 300 | function = lp_declare_intrinsic(gallivm->module, "llvm.SI.use.sgpr.i32", |
| 301 | i32, &i32, 1); |
| 302 | LLVMAddFunctionAttr(function, LLVMReadNoneAttribute); |
| 303 | |
| 304 | /* declare i64 @llvm.SI.use.sgpr.i64(i32) */ |
| 305 | function = lp_declare_intrinsic(gallivm->module, "llvm.SI.use.sgpr.i64", |
| 306 | i64, &i32, 1); |
| 307 | LLVMAddFunctionAttr(function, LLVMReadNoneAttribute); |
| 308 | } |
| 309 | |
| 310 | /* XXX: This is partially implemented for VS only at this point. It is not complete */ |
| 311 | static void si_llvm_emit_epilogue(struct lp_build_tgsi_context * bld_base) |
| 312 | { |
| 313 | struct si_shader_context * si_shader_ctx = si_shader_context(bld_base); |
| 314 | struct r600_shader * shader = &si_shader_ctx->shader->shader; |
| 315 | struct lp_build_context * base = &bld_base->base; |
| 316 | struct lp_build_context * uint = |
| 317 | &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld; |
| 318 | struct tgsi_parse_context *parse = &si_shader_ctx->parse; |
| 319 | LLVMValueRef last_args[9] = { 0 }; |
| 320 | |
| 321 | while (!tgsi_parse_end_of_tokens(parse)) { |
| 322 | /* XXX: component_bits controls which components of the output |
| 323 | * registers actually get exported. (e.g bit 0 means export |
| 324 | * X component, bit 1 means export Y component, etc.) I'm |
| 325 | * hard coding this to 0xf for now. In the future, we might |
| 326 | * want to do something else. */ |
| 327 | unsigned component_bits = 0xf; |
| 328 | unsigned chan; |
| 329 | struct tgsi_full_declaration *d = |
| 330 | &parse->FullToken.FullDeclaration; |
| 331 | LLVMValueRef args[9]; |
| 332 | unsigned target; |
| 333 | unsigned index; |
| 334 | unsigned color_count = 0; |
| 335 | unsigned param_count = 0; |
| 336 | int i; |
| 337 | |
| 338 | tgsi_parse_token(parse); |
| 339 | if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION) |
| 340 | continue; |
| 341 | |
| 342 | switch (d->Declaration.File) { |
| 343 | case TGSI_FILE_INPUT: |
| 344 | i = shader->ninput++; |
| 345 | shader->input[i].name = d->Semantic.Name; |
| 346 | shader->input[i].sid = d->Semantic.Index; |
Francisco Jerez | 1279923 | 2012-04-30 18:27:52 +0200 | [diff] [blame] | 347 | shader->input[i].interpolate = d->Interp.Interpolate; |
| 348 | shader->input[i].centroid = d->Interp.Centroid; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 349 | break; |
| 350 | case TGSI_FILE_OUTPUT: |
| 351 | i = shader->noutput++; |
| 352 | shader->output[i].name = d->Semantic.Name; |
| 353 | shader->output[i].sid = d->Semantic.Index; |
Francisco Jerez | 1279923 | 2012-04-30 18:27:52 +0200 | [diff] [blame] | 354 | shader->output[i].interpolate = d->Interp.Interpolate; |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 355 | break; |
| 356 | } |
| 357 | |
| 358 | if (d->Declaration.File != TGSI_FILE_OUTPUT) |
| 359 | continue; |
| 360 | |
| 361 | for (index = d->Range.First; index <= d->Range.Last; index++) { |
| 362 | for (chan = 0; chan < 4; chan++ ) { |
| 363 | LLVMValueRef out_ptr = |
| 364 | si_shader_ctx->radeon_bld.soa.outputs |
| 365 | [index][chan]; |
| 366 | /* +5 because the first output value will be |
| 367 | * the 6th argument to the intrinsic. */ |
| 368 | args[chan + 5]= LLVMBuildLoad( |
| 369 | base->gallivm->builder, out_ptr, ""); |
| 370 | } |
| 371 | |
| 372 | /* XXX: We probably need to keep track of the output |
| 373 | * values, so we know what we are passing to the next |
| 374 | * stage. */ |
| 375 | |
| 376 | /* Select the correct target */ |
| 377 | switch(d->Semantic.Name) { |
| 378 | case TGSI_SEMANTIC_POSITION: |
| 379 | target = V_008DFC_SQ_EXP_POS; |
| 380 | break; |
| 381 | case TGSI_SEMANTIC_COLOR: |
| 382 | if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) { |
| 383 | target = V_008DFC_SQ_EXP_PARAM + param_count; |
| 384 | param_count++; |
| 385 | } else { |
| 386 | target = V_008DFC_SQ_EXP_MRT + color_count; |
| 387 | color_count++; |
| 388 | } |
| 389 | break; |
| 390 | case TGSI_SEMANTIC_GENERIC: |
| 391 | target = V_008DFC_SQ_EXP_PARAM + param_count; |
| 392 | param_count++; |
| 393 | break; |
| 394 | default: |
| 395 | target = 0; |
| 396 | fprintf(stderr, |
| 397 | "Warning: SI unhandled output type:%d\n", |
| 398 | d->Semantic.Name); |
| 399 | } |
| 400 | |
| 401 | /* Specify which components to enable */ |
| 402 | args[0] = lp_build_const_int32(base->gallivm, |
| 403 | component_bits); |
| 404 | |
| 405 | /* Specify whether the EXEC mask represents the valid mask */ |
| 406 | args[1] = lp_build_const_int32(base->gallivm, 0); |
| 407 | |
| 408 | /* Specify whether this is the last export */ |
| 409 | args[2] = lp_build_const_int32(base->gallivm, 0); |
| 410 | |
| 411 | /* Specify the target we are exporting */ |
| 412 | args[3] = lp_build_const_int32(base->gallivm, target); |
| 413 | |
| 414 | /* Set COMPR flag to zero to export data as 32-bit */ |
| 415 | args[4] = uint->zero; |
| 416 | |
| 417 | if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX ? |
| 418 | (d->Semantic.Name == TGSI_SEMANTIC_POSITION) : |
| 419 | (d->Semantic.Name == TGSI_SEMANTIC_COLOR)) { |
| 420 | if (last_args[0]) { |
| 421 | lp_build_intrinsic(base->gallivm->builder, |
| 422 | "llvm.SI.export", |
| 423 | LLVMVoidTypeInContext(base->gallivm->context), |
| 424 | last_args, 9); |
| 425 | } |
| 426 | |
| 427 | memcpy(last_args, args, sizeof(args)); |
| 428 | } else { |
| 429 | lp_build_intrinsic(base->gallivm->builder, |
| 430 | "llvm.SI.export", |
| 431 | LLVMVoidTypeInContext(base->gallivm->context), |
| 432 | args, 9); |
| 433 | } |
| 434 | |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /* Specify whether the EXEC mask represents the valid mask */ |
| 439 | last_args[1] = lp_build_const_int32(base->gallivm, |
| 440 | si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT); |
| 441 | |
| 442 | /* Specify that this is the last export */ |
| 443 | last_args[2] = lp_build_const_int32(base->gallivm, 1); |
| 444 | |
| 445 | lp_build_intrinsic(base->gallivm->builder, |
| 446 | "llvm.SI.export", |
| 447 | LLVMVoidTypeInContext(base->gallivm->context), |
| 448 | last_args, 9); |
| 449 | |
| 450 | /* XXX: Look up what this function does */ |
| 451 | /* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/ |
| 452 | } |
| 453 | |
| 454 | static void tex_fetch_args( |
| 455 | struct lp_build_tgsi_context * bld_base, |
| 456 | struct lp_build_emit_data * emit_data) |
| 457 | { |
| 458 | /* WriteMask */ |
| 459 | emit_data->args[0] = lp_build_const_int32(bld_base->base.gallivm, |
| 460 | emit_data->inst->Dst[0].Register.WriteMask); |
| 461 | |
| 462 | /* Coordinates */ |
| 463 | /* XXX: Not all sample instructions need 4 address arguments. */ |
| 464 | emit_data->args[1] = lp_build_emit_fetch(bld_base, emit_data->inst, |
| 465 | 0, LP_CHAN_ALL); |
| 466 | |
| 467 | /* Resource */ |
| 468 | emit_data->args[2] = use_sgpr(bld_base->base.gallivm, SGPR_I64, 2); |
| 469 | emit_data->args[3] = lp_build_const_int32(bld_base->base.gallivm, |
Michel Dänzer | de12c6c | 2012-04-19 10:35:31 +0200 | [diff] [blame] | 470 | 8 * emit_data->inst->Src[1].Register.Index); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 471 | |
| 472 | /* Sampler */ |
| 473 | emit_data->args[4] = use_sgpr(bld_base->base.gallivm, SGPR_I64, 1); |
| 474 | emit_data->args[5] = lp_build_const_int32(bld_base->base.gallivm, |
Michel Dänzer | de12c6c | 2012-04-19 10:35:31 +0200 | [diff] [blame] | 475 | 4 * emit_data->inst->Src[1].Register.Index); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 476 | |
| 477 | /* Dimensions */ |
| 478 | /* XXX: We might want to pass this information to the shader at some. */ |
| 479 | /* emit_data->args[4] = lp_build_const_int32(bld_base->base.gallivm, |
| 480 | emit_data->inst->Texture.Texture); |
| 481 | */ |
| 482 | |
| 483 | emit_data->arg_count = 6; |
| 484 | /* XXX: To optimize, we could use a float or v2f32, if the last bits of |
| 485 | * the writemask are clear */ |
| 486 | emit_data->dst_type = LLVMVectorType( |
| 487 | LLVMFloatTypeInContext(bld_base->base.gallivm->context), |
| 488 | 4); |
| 489 | } |
| 490 | |
| 491 | static const struct lp_build_tgsi_action tex_action = { |
| 492 | .fetch_args = tex_fetch_args, |
| 493 | .emit = lp_build_tgsi_intrinsic, |
| 494 | .intr_name = "llvm.SI.sample" |
| 495 | }; |
| 496 | |
| 497 | |
| 498 | int si_pipe_shader_create( |
| 499 | struct pipe_context *ctx, |
| 500 | struct si_pipe_shader *shader) |
| 501 | { |
| 502 | struct r600_context *rctx = (struct r600_context*)ctx; |
| 503 | struct si_shader_context si_shader_ctx; |
| 504 | struct tgsi_shader_info shader_info; |
| 505 | struct lp_build_tgsi_context * bld_base; |
| 506 | LLVMModuleRef mod; |
| 507 | unsigned char * inst_bytes; |
| 508 | unsigned inst_byte_count; |
| 509 | unsigned i; |
| 510 | |
| 511 | radeon_llvm_context_init(&si_shader_ctx.radeon_bld); |
| 512 | bld_base = &si_shader_ctx.radeon_bld.soa.bld_base; |
| 513 | |
| 514 | tgsi_scan_shader(shader->tokens, &shader_info); |
| 515 | bld_base->info = &shader_info; |
| 516 | bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant; |
| 517 | bld_base->emit_prologue = si_llvm_emit_prologue; |
| 518 | bld_base->emit_epilogue = si_llvm_emit_epilogue; |
| 519 | |
| 520 | bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action; |
| 521 | |
| 522 | si_shader_ctx.radeon_bld.load_input = declare_input; |
| 523 | si_shader_ctx.tokens = shader->tokens; |
| 524 | tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens); |
| 525 | si_shader_ctx.shader = shader; |
| 526 | si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor; |
| 527 | si_shader_ctx.rctx = rctx; |
| 528 | |
| 529 | shader->shader.nr_cbufs = rctx->nr_cbufs; |
| 530 | |
| 531 | lp_build_tgsi_llvm(bld_base, shader->tokens); |
| 532 | |
| 533 | radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld); |
| 534 | |
| 535 | mod = bld_base->base.gallivm->module; |
| 536 | tgsi_dump(shader->tokens, 0); |
| 537 | LLVMDumpModule(mod); |
| 538 | radeon_llvm_compile(mod, &inst_bytes, &inst_byte_count, "SI", 1 /* dump */); |
| 539 | fprintf(stderr, "SI CODE:\n"); |
| 540 | for (i = 0; i < inst_byte_count; i+=4 ) { |
| 541 | fprintf(stderr, "%02x%02x%02x%02x\n", inst_bytes[i + 3], |
| 542 | inst_bytes[i + 2], inst_bytes[i + 1], |
| 543 | inst_bytes[i]); |
| 544 | } |
| 545 | |
| 546 | shader->num_sgprs = util_le32_to_cpu(*(uint32_t*)inst_bytes); |
| 547 | shader->num_vgprs = util_le32_to_cpu(*(uint32_t*)(inst_bytes + 4)); |
| 548 | shader->spi_ps_input_ena = util_le32_to_cpu(*(uint32_t*)(inst_bytes + 8)); |
| 549 | |
| 550 | tgsi_parse_free(&si_shader_ctx.parse); |
| 551 | |
| 552 | /* copy new shader */ |
| 553 | if (shader->bo == NULL) { |
| 554 | uint32_t *ptr; |
| 555 | |
| 556 | shader->bo = (struct r600_resource*) |
| 557 | pipe_buffer_create(ctx->screen, PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE, inst_byte_count); |
| 558 | if (shader->bo == NULL) { |
| 559 | return -ENOMEM; |
| 560 | } |
Marek Olšák | 0a61202 | 2012-04-26 12:02:31 +0200 | [diff] [blame] | 561 | ptr = (uint32_t*)rctx->ws->buffer_map(shader->bo->cs_buf, rctx->cs, PIPE_TRANSFER_WRITE); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 562 | if (0 /*R600_BIG_ENDIAN*/) { |
| 563 | for (i = 0; i < (inst_byte_count-12)/4; ++i) { |
| 564 | ptr[i] = util_bswap32(*(uint32_t*)(inst_bytes+12 + i*4)); |
| 565 | } |
| 566 | } else { |
| 567 | memcpy(ptr, inst_bytes + 12, inst_byte_count - 12); |
| 568 | } |
Marek Olšák | 0a61202 | 2012-04-26 12:02:31 +0200 | [diff] [blame] | 569 | rctx->ws->buffer_unmap(shader->bo->cs_buf); |
Tom Stellard | a75c616 | 2012-01-06 17:38:37 -0500 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | free(inst_bytes); |
| 573 | |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader) |
| 578 | { |
| 579 | pipe_resource_reference((struct pipe_resource**)&shader->bo, NULL); |
| 580 | |
| 581 | memset(&shader->shader,0,sizeof(struct r600_shader)); |
| 582 | } |