From 130e198c496382d5fb01b0a36423e0161421abf5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marek=20Ol=C5=A1=C3=A1k?= Date: Wed, 15 Mar 2017 23:06:59 +0100 Subject: [PATCH] radeonsi: separate out TGSI initialization of si_shader_context MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit so that we can put multiple different TGSI shaders into one module. Reviewed-by: Nicolai Hähnle --- src/gallium/drivers/radeonsi/si_shader.c | 15 ++-- src/gallium/drivers/radeonsi/si_shader_internal.h | 7 +- .../drivers/radeonsi/si_shader_tgsi_setup.c | 93 ++++++++++++++-------- 3 files changed, 72 insertions(+), 43 deletions(-) diff --git a/src/gallium/drivers/radeonsi/si_shader.c b/src/gallium/drivers/radeonsi/si_shader.c index 125affb..19155fc 100644 --- a/src/gallium/drivers/radeonsi/si_shader.c +++ b/src/gallium/drivers/radeonsi/si_shader.c @@ -63,7 +63,6 @@ struct si_shader_output_values static void si_init_shader_ctx(struct si_shader_context *ctx, struct si_screen *sscreen, - struct si_shader *shader, LLVMTargetMachineRef tm); static void si_llvm_emit_barrier(const struct lp_build_tgsi_action *action, @@ -6523,7 +6522,8 @@ si_generate_gs_copy_shader(struct si_screen *sscreen, shader->selector = gs_selector; shader->is_gs_copy_shader = true; - si_init_shader_ctx(&ctx, sscreen, shader, tm); + si_init_shader_ctx(&ctx, sscreen, tm); + ctx.shader = shader; ctx.type = PIPE_SHADER_VERTEX; builder = gallivm->builder; @@ -6722,15 +6722,12 @@ static void si_dump_shader_key(unsigned shader, struct si_shader_key *key, static void si_init_shader_ctx(struct si_shader_context *ctx, struct si_screen *sscreen, - struct si_shader *shader, LLVMTargetMachineRef tm) { struct lp_build_tgsi_context *bld_base; struct lp_build_tgsi_action tmpl = {}; - si_llvm_context_init(ctx, sscreen, shader, tm, - (shader && shader->selector) ? &shader->selector->info : NULL, - (shader && shader->selector) ? shader->selector->tokens : NULL); + si_llvm_context_init(ctx, sscreen, tm); bld_base = &ctx->bld_base; bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant; @@ -7396,7 +7393,8 @@ int si_compile_tgsi_shader(struct si_screen *sscreen, si_dump_streamout(&sel->so); } - si_init_shader_ctx(&ctx, sscreen, shader, tm); + si_init_shader_ctx(&ctx, sscreen, tm); + si_llvm_context_set_tgsi(&ctx, shader); ctx.separate_prolog = !is_monolithic; memset(shader->info.vs_output_param_offset, AC_EXP_PARAM_UNDEFINED, @@ -7644,7 +7642,8 @@ si_get_shader_part(struct si_screen *sscreen, struct si_shader_context ctx; struct gallivm_state *gallivm = &ctx.gallivm; - si_init_shader_ctx(&ctx, sscreen, &shader, tm); + si_init_shader_ctx(&ctx, sscreen, tm); + ctx.shader = &shader; ctx.type = type; switch (type) { diff --git a/src/gallium/drivers/radeonsi/si_shader_internal.h b/src/gallium/drivers/radeonsi/si_shader_internal.h index fd7deec..3f856c4 100644 --- a/src/gallium/drivers/radeonsi/si_shader_internal.h +++ b/src/gallium/drivers/radeonsi/si_shader_internal.h @@ -182,10 +182,9 @@ LLVMValueRef si_llvm_bound_index(struct si_shader_context *ctx, void si_llvm_context_init(struct si_shader_context *ctx, struct si_screen *sscreen, - struct si_shader *shader, - LLVMTargetMachineRef tm, - const struct tgsi_shader_info *info, - const struct tgsi_token *tokens); + LLVMTargetMachineRef tm); +void si_llvm_context_set_tgsi(struct si_shader_context *ctx, + struct si_shader *shader); void si_llvm_create_func(struct si_shader_context *ctx, const char *name, diff --git a/src/gallium/drivers/radeonsi/si_shader_tgsi_setup.c b/src/gallium/drivers/radeonsi/si_shader_tgsi_setup.c index 7218d2d..c733f5a 100644 --- a/src/gallium/drivers/radeonsi/si_shader_tgsi_setup.c +++ b/src/gallium/drivers/radeonsi/si_shader_tgsi_setup.c @@ -1256,10 +1256,7 @@ static void emit_immediate(struct lp_build_tgsi_context *bld_base, void si_llvm_context_init(struct si_shader_context *ctx, struct si_screen *sscreen, - struct si_shader *shader, - LLVMTargetMachineRef tm, - const struct tgsi_shader_info *info, - const struct tgsi_token *tokens) + LLVMTargetMachineRef tm) { struct lp_type type; @@ -1269,10 +1266,8 @@ void si_llvm_context_init(struct si_shader_context *ctx, * helper functions in the gallivm module. */ memset(ctx, 0, sizeof(*ctx)); - ctx->shader = shader; ctx->screen = sscreen; ctx->tm = tm; - ctx->type = info ? info->processor : -1; ctx->gallivm.context = LLVMContextCreate(); ctx->gallivm.module = LLVMModuleCreateWithNameInContext("tgsi", @@ -1301,24 +1296,6 @@ void si_llvm_context_init(struct si_shader_context *ctx, struct lp_build_tgsi_context *bld_base = &ctx->bld_base; - bld_base->info = info; - - if (info && info->array_max[TGSI_FILE_TEMPORARY] > 0) { - int size = info->array_max[TGSI_FILE_TEMPORARY]; - - ctx->temp_arrays = CALLOC(size, sizeof(ctx->temp_arrays[0])); - ctx->temp_array_allocas = CALLOC(size, sizeof(ctx->temp_array_allocas[0])); - - if (tokens) - tgsi_scan_arrays(tokens, TGSI_FILE_TEMPORARY, size, - ctx->temp_arrays); - } - - if (info && info->file_max[TGSI_FILE_IMMEDIATE] >= 0) { - int size = info->file_max[TGSI_FILE_IMMEDIATE] + 1; - ctx->imms = MALLOC(size * TGSI_NUM_CHANNELS * sizeof(LLVMValueRef)); - } - type.floating = true; type.fixed = false; type.sign = true; @@ -1335,17 +1312,10 @@ void si_llvm_context_init(struct si_shader_context *ctx, lp_build_context_init(&ctx->bld_base.int64_bld, &ctx->gallivm, lp_int_type(type)); bld_base->soa = 1; - bld_base->emit_store = si_llvm_emit_store; bld_base->emit_swizzle = emit_swizzle; bld_base->emit_declaration = emit_declaration; bld_base->emit_immediate = emit_immediate; - bld_base->emit_fetch_funcs[TGSI_FILE_IMMEDIATE] = si_llvm_emit_fetch; - bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = si_llvm_emit_fetch; - bld_base->emit_fetch_funcs[TGSI_FILE_TEMPORARY] = si_llvm_emit_fetch; - bld_base->emit_fetch_funcs[TGSI_FILE_OUTPUT] = si_llvm_emit_fetch; - bld_base->emit_fetch_funcs[TGSI_FILE_SYSTEM_VALUE] = fetch_system_value; - /* metadata allowing 2.5 ULP */ ctx->fpmath_md_kind = LLVMGetMDKindIDInContext(ctx->gallivm.context, "fpmath", 6); @@ -1381,6 +1351,67 @@ void si_llvm_context_init(struct si_shader_context *ctx, ctx->i32_1 = LLVMConstInt(ctx->i32, 1, 0); } +/* Set the context to a certain TGSI shader. Can be called repeatedly + * to change the shader. */ +void si_llvm_context_set_tgsi(struct si_shader_context *ctx, + struct si_shader *shader) +{ + const struct tgsi_shader_info *info = NULL; + const struct tgsi_token *tokens = NULL; + + if (shader && shader->selector) { + info = &shader->selector->info; + tokens = shader->selector->tokens; + } + + ctx->shader = shader; + ctx->type = info ? info->processor : -1; + ctx->bld_base.info = info; + + /* Clean up the old contents. */ + FREE(ctx->temp_arrays); + ctx->temp_arrays = NULL; + FREE(ctx->temp_array_allocas); + ctx->temp_array_allocas = NULL; + + FREE(ctx->imms); + ctx->imms = NULL; + ctx->imms_num = 0; + + FREE(ctx->temps); + ctx->temps = NULL; + ctx->temps_count = 0; + + if (!info || !tokens) + return; + + if (info->array_max[TGSI_FILE_TEMPORARY] > 0) { + int size = info->array_max[TGSI_FILE_TEMPORARY]; + + ctx->temp_arrays = CALLOC(size, sizeof(ctx->temp_arrays[0])); + ctx->temp_array_allocas = CALLOC(size, sizeof(ctx->temp_array_allocas[0])); + + tgsi_scan_arrays(tokens, TGSI_FILE_TEMPORARY, size, + ctx->temp_arrays); + } + if (info->file_max[TGSI_FILE_IMMEDIATE] >= 0) { + int size = info->file_max[TGSI_FILE_IMMEDIATE] + 1; + ctx->imms = MALLOC(size * TGSI_NUM_CHANNELS * sizeof(LLVMValueRef)); + } + + /* Re-set these to start with a clean slate. */ + ctx->bld_base.num_instructions = 0; + ctx->bld_base.pc = 0; + memset(ctx->outputs, 0, sizeof(ctx->outputs)); + + ctx->bld_base.emit_store = si_llvm_emit_store; + ctx->bld_base.emit_fetch_funcs[TGSI_FILE_IMMEDIATE] = si_llvm_emit_fetch; + ctx->bld_base.emit_fetch_funcs[TGSI_FILE_INPUT] = si_llvm_emit_fetch; + ctx->bld_base.emit_fetch_funcs[TGSI_FILE_TEMPORARY] = si_llvm_emit_fetch; + ctx->bld_base.emit_fetch_funcs[TGSI_FILE_OUTPUT] = si_llvm_emit_fetch; + ctx->bld_base.emit_fetch_funcs[TGSI_FILE_SYSTEM_VALUE] = fetch_system_value; +} + void si_llvm_create_func(struct si_shader_context *ctx, const char *name, LLVMTypeRef *return_types, unsigned num_return_elems, -- 2.7.4