+
+
+/**
+ * Create LLVM types for various structures.
+ */
+static void
+create_gs_jit_types(struct draw_gs_llvm_variant *var)
+{
+ struct gallivm_state *gallivm = var->gallivm;
+ LLVMTypeRef texture_type, sampler_type, context_type;
+
+ texture_type = create_jit_texture_type(gallivm, "texture");
+ sampler_type = create_jit_sampler_type(gallivm, "sampler");
+
+ context_type = create_gs_jit_context_type(gallivm,
+ var->shader->base.vector_length,
+ texture_type, sampler_type,
+ "draw_gs_jit_context");
+ var->context_ptr_type = LLVMPointerType(context_type, 0);
+
+ var->input_array_type = create_gs_jit_input_type(gallivm);
+}
+
+static LLVMTypeRef
+get_gs_context_ptr_type(struct draw_gs_llvm_variant *variant)
+{
+ if (!variant->context_ptr_type)
+ create_gs_jit_types(variant);
+ return variant->context_ptr_type;
+}
+
+static LLVMValueRef
+generate_mask_value(struct draw_gs_llvm_variant *variant,
+ struct lp_type gs_type)
+{
+ struct gallivm_state *gallivm = variant->gallivm;
+ LLVMBuilderRef builder = gallivm->builder;
+ LLVMValueRef bits[16];
+ struct lp_type mask_type = lp_int_type(gs_type);
+ struct lp_type mask_elem_type = lp_elem_type(mask_type);
+ LLVMValueRef mask_val = lp_build_const_vec(gallivm,
+ mask_type,
+ 0);
+ unsigned i;
+
+ assert(gs_type.length <= Elements(bits));
+
+ for (i = gs_type.length; i >= 1; --i) {
+ int idx = i - 1;
+ LLVMValueRef ind = lp_build_const_int32(gallivm, i);
+ bits[idx] = lp_build_compare(gallivm,
+ mask_elem_type, PIPE_FUNC_GEQUAL,
+ variant->num_prims, ind);
+ }
+ for (i = 0; i < gs_type.length; ++i) {
+ LLVMValueRef ind = lp_build_const_int32(gallivm, i);
+ mask_val = LLVMBuildInsertElement(builder, mask_val, bits[i], ind, "");
+ }
+ mask_val = lp_build_compare(gallivm,
+ mask_type, PIPE_FUNC_NOTEQUAL,
+ mask_val,
+ lp_build_const_int_vec(gallivm, mask_type, 0));
+
+ return mask_val;
+}
+
+static void
+draw_gs_llvm_generate(struct draw_llvm *llvm,
+ struct draw_gs_llvm_variant *variant)
+{
+ struct gallivm_state *gallivm = variant->gallivm;
+ LLVMContextRef context = gallivm->context;
+ LLVMTypeRef int32_type = LLVMInt32TypeInContext(context);
+ LLVMTypeRef arg_types[5];
+ LLVMTypeRef func_type;
+ LLVMValueRef variant_func;
+ LLVMValueRef context_ptr;
+ LLVMBasicBlockRef block;
+ LLVMBuilderRef builder;
+ LLVMValueRef io_ptr, input_array, num_prims, mask_val;
+ struct lp_build_sampler_soa *sampler = 0;
+ struct lp_build_context bld;
+ struct lp_bld_tgsi_system_values system_values;
+ struct lp_type gs_type;
+ unsigned i;
+ struct lp_build_tgsi_gs_iface gs_iface;
+ const struct tgsi_token *tokens = variant->shader->base.state.tokens;
+ LLVMValueRef consts_ptr;
+ LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];
+ struct lp_build_mask_context mask;
+
+ memset(&system_values, 0, sizeof(system_values));
+
+ assert(variant->vertex_header_ptr_type);
+
+ arg_types[0] = get_gs_context_ptr_type(variant); /* context */
+ arg_types[1] = variant->input_array_type; /* input */
+ arg_types[2] = variant->vertex_header_ptr_type; /* vertex_header */
+ arg_types[3] = int32_type; /* num_prims */
+ arg_types[4] = int32_type; /* instance_id */
+
+ func_type = LLVMFunctionType(int32_type, arg_types, Elements(arg_types), 0);
+
+ variant_func = LLVMAddFunction(gallivm->module, "draw_geometry_shader",
+ func_type);
+ variant->function = variant_func;
+
+ LLVMSetFunctionCallConv(variant_func, LLVMCCallConv);
+
+ for (i = 0; i < Elements(arg_types); ++i)
+ if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind)
+ LLVMAddAttribute(LLVMGetParam(variant_func, i),
+ LLVMNoAliasAttribute);
+
+ context_ptr = LLVMGetParam(variant_func, 0);
+ input_array = LLVMGetParam(variant_func, 1);
+ io_ptr = LLVMGetParam(variant_func, 2);
+ num_prims = LLVMGetParam(variant_func, 3);
+ system_values.instance_id = LLVMGetParam(variant_func, 4);
+
+ lp_build_name(context_ptr, "context");
+ lp_build_name(input_array, "input");
+ lp_build_name(io_ptr, "io");
+ lp_build_name(io_ptr, "num_prims");
+ lp_build_name(system_values.instance_id, "instance_id");
+
+ variant->context_ptr = context_ptr;
+ variant->io_ptr = io_ptr;
+ variant->num_prims = num_prims;
+
+ gs_iface.input = input_array;
+ gs_iface.emit_vertex = draw_gs_llvm_emit_vertex;
+ gs_iface.end_primitive = draw_gs_llvm_end_primitive;
+ gs_iface.gs_epilogue = draw_gs_llvm_epilogue;
+ gs_iface.user_data = variant;
+
+ /*
+ * Function body
+ */
+
+ block = LLVMAppendBasicBlockInContext(gallivm->context, variant_func, "entry");
+ builder = gallivm->builder;
+ LLVMPositionBuilderAtEnd(builder, block);
+
+ lp_build_context_init(&bld, gallivm, lp_type_int(32));
+
+ memset(&gs_type, 0, sizeof gs_type);
+ gs_type.floating = TRUE; /* floating point values */
+ gs_type.sign = TRUE; /* values are signed */
+ gs_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */
+ gs_type.width = 32; /* 32-bit float */
+ gs_type.length = variant->shader->base.vector_length;
+
+ consts_ptr = draw_gs_jit_context_constants(variant->gallivm, context_ptr);
+
+ /* code generated texture sampling */
+ sampler = draw_llvm_sampler_soa_create(variant->key.samplers,
+ context_ptr);
+
+ mask_val = generate_mask_value(variant, gs_type);
+ lp_build_mask_begin(&mask, gallivm, gs_type, mask_val);
+
+ lp_build_tgsi_soa(variant->gallivm,
+ tokens,
+ gs_type,
+ &mask,
+ consts_ptr,
+ &system_values,
+ NULL /*pos*/,
+ NULL,
+ outputs,
+ sampler,
+ &llvm->draw->gs.geometry_shader->info,
+ &gs_iface);
+
+ lp_build_mask_end(&mask);
+
+ LLVMBuildRet(builder, lp_build_zero(gallivm, lp_type_uint(32)));
+
+ gallivm_verify_function(gallivm, variant_func);
+}
+
+
+struct draw_gs_llvm_variant *
+draw_gs_llvm_create_variant(struct draw_llvm *llvm,
+ unsigned num_outputs,
+ const struct draw_gs_llvm_variant_key *key)
+{
+ struct draw_gs_llvm_variant *variant;
+ struct llvm_geometry_shader *shader =
+ llvm_geometry_shader(llvm->draw->gs.geometry_shader);
+ LLVMTypeRef vertex_header;
+
+ variant = MALLOC(sizeof *variant +
+ shader->variant_key_size -
+ sizeof variant->key);
+ if (variant == NULL)
+ return NULL;
+
+ variant->llvm = llvm;
+ variant->shader = shader;
+
+ variant->gallivm = gallivm_create();
+
+ create_gs_jit_types(variant);
+
+ memcpy(&variant->key, key, shader->variant_key_size);
+
+ vertex_header = create_jit_vertex_header(variant->gallivm, num_outputs);
+
+ variant->vertex_header_ptr_type = LLVMPointerType(vertex_header, 0);
+
+ draw_gs_llvm_generate(llvm, variant);
+
+ gallivm_compile_module(variant->gallivm);
+
+ variant->jit_func = (draw_gs_jit_func)
+ gallivm_jit_function(variant->gallivm, variant->function);
+
+ variant->list_item_global.base = variant;
+ variant->list_item_local.base = variant;
+ /*variant->no = */shader->variants_created++;
+ variant->list_item_global.base = variant;
+
+ return variant;
+}
+
+void
+draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant *variant)
+{
+ struct draw_llvm *llvm = variant->llvm;
+
+ if (variant->function) {
+ gallivm_free_function(variant->gallivm,
+ variant->function, variant->jit_func);
+ }
+
+ gallivm_destroy(variant->gallivm);
+
+ remove_from_list(&variant->list_item_local);
+ variant->shader->variants_cached--;
+ remove_from_list(&variant->list_item_global);
+ llvm->nr_gs_variants--;
+ FREE(variant);
+}
+
+struct draw_gs_llvm_variant_key *
+draw_gs_llvm_make_variant_key(struct draw_llvm *llvm, char *store)
+{
+ unsigned i;
+ struct draw_gs_llvm_variant_key *key;
+ struct draw_sampler_static_state *draw_sampler;
+
+ key = (struct draw_gs_llvm_variant_key *)store;
+
+ /* All variants of this shader will have the same value for
+ * nr_samplers. Not yet trying to compact away holes in the
+ * sampler array.
+ */
+ key->nr_samplers = llvm->draw->gs.geometry_shader->info.file_max[TGSI_FILE_SAMPLER] + 1;
+ if (llvm->draw->gs.geometry_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] != -1) {
+ key->nr_sampler_views =
+ llvm->draw->gs.geometry_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] + 1;
+ }
+ else {
+ key->nr_sampler_views = key->nr_samplers;
+ }
+
+ draw_sampler = key->samplers;
+
+ memset(draw_sampler, 0, MAX2(key->nr_samplers, key->nr_sampler_views) * sizeof *draw_sampler);
+
+ for (i = 0 ; i < key->nr_samplers; i++) {
+ lp_sampler_static_sampler_state(&draw_sampler[i].sampler_state,
+ llvm->draw->samplers[PIPE_SHADER_GEOMETRY][i]);
+ }
+ for (i = 0 ; i < key->nr_sampler_views; i++) {
+ lp_sampler_static_texture_state(&draw_sampler[i].texture_state,
+ llvm->draw->sampler_views[PIPE_SHADER_GEOMETRY][i]);
+ }
+
+ return key;
+}
+
+void
+draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key *key)
+{
+ unsigned i;
+ struct draw_sampler_static_state *sampler = key->samplers;
+
+ for (i = 0 ; i < key->nr_sampler_views; i++) {
+ debug_printf("sampler[%i].src_format = %s\n", i,
+ util_format_name(sampler[i].texture_state.format));
+ }
+}