14a49301eSmrg/**************************************************************************
24a49301eSmrg *
34a49301eSmrg * Copyright 2009 VMware, Inc.
44a49301eSmrg * All Rights Reserved.
54a49301eSmrg *
64a49301eSmrg * Permission is hereby granted, free of charge, to any person obtaining a
74a49301eSmrg * copy of this software and associated documentation files (the
84a49301eSmrg * "Software"), to deal in the Software without restriction, including
94a49301eSmrg * without limitation the rights to use, copy, modify, merge, publish,
104a49301eSmrg * distribute, sub license, and/or sell copies of the Software, and to
114a49301eSmrg * permit persons to whom the Software is furnished to do so, subject to
124a49301eSmrg * the following conditions:
134a49301eSmrg *
144a49301eSmrg * The above copyright notice and this permission notice (including the
154a49301eSmrg * next paragraph) shall be included in all copies or substantial portions
164a49301eSmrg * of the Software.
174a49301eSmrg *
184a49301eSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
194a49301eSmrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
204a49301eSmrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
214a49301eSmrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
224a49301eSmrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
234a49301eSmrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
244a49301eSmrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
254a49301eSmrg *
264a49301eSmrg **************************************************************************/
274a49301eSmrg
284a49301eSmrg/**
294a49301eSmrg * @file
304a49301eSmrg * C - JIT interfaces
314a49301eSmrg *
324a49301eSmrg * @author Jose Fonseca <jfonseca@vmware.com>
334a49301eSmrg */
344a49301eSmrg
357ec681f3Smrg#include <llvm/Config/llvm-config.h>
364a49301eSmrg
374a49301eSmrg#include "util/u_memory.h"
38cdc920a0Smrg#include "gallivm/lp_bld_init.h"
393464ebd5Sriastradh#include "gallivm/lp_bld_debug.h"
4001e04c3fSmrg#include "gallivm/lp_bld_format.h"
413464ebd5Sriastradh#include "lp_context.h"
427ec681f3Smrg#include "lp_screen.h"
434a49301eSmrg#include "lp_jit.h"
444a49301eSmrg
457ec681f3Smrgstatic LLVMTypeRef
467ec681f3Smrgcreate_jit_texture_type(struct gallivm_state *gallivm)
477ec681f3Smrg{
487ec681f3Smrg   LLVMContextRef lc = gallivm->context;
497ec681f3Smrg   LLVMTypeRef texture_type;
507ec681f3Smrg   LLVMTypeRef elem_types[LP_JIT_TEXTURE_NUM_FIELDS];
517ec681f3Smrg
527ec681f3Smrg   /* struct lp_jit_texture */
537ec681f3Smrg   elem_types[LP_JIT_TEXTURE_WIDTH]  =
547ec681f3Smrg   elem_types[LP_JIT_TEXTURE_HEIGHT] =
557ec681f3Smrg   elem_types[LP_JIT_TEXTURE_DEPTH] =
567ec681f3Smrg   elem_types[LP_JIT_TEXTURE_NUM_SAMPLES] =
577ec681f3Smrg   elem_types[LP_JIT_TEXTURE_SAMPLE_STRIDE] =
587ec681f3Smrg   elem_types[LP_JIT_TEXTURE_FIRST_LEVEL] =
597ec681f3Smrg   elem_types[LP_JIT_TEXTURE_LAST_LEVEL] = LLVMInt32TypeInContext(lc);
607ec681f3Smrg   elem_types[LP_JIT_TEXTURE_BASE] = LLVMPointerType(LLVMInt8TypeInContext(lc), 0);
617ec681f3Smrg   elem_types[LP_JIT_TEXTURE_ROW_STRIDE] =
627ec681f3Smrg   elem_types[LP_JIT_TEXTURE_IMG_STRIDE] =
637ec681f3Smrg   elem_types[LP_JIT_TEXTURE_MIP_OFFSETS] =
647ec681f3Smrg      LLVMArrayType(LLVMInt32TypeInContext(lc), LP_MAX_TEXTURE_LEVELS);
657ec681f3Smrg
667ec681f3Smrg   texture_type = LLVMStructTypeInContext(lc, elem_types,
677ec681f3Smrg                                          ARRAY_SIZE(elem_types), 0);
687ec681f3Smrg
697ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, width,
707ec681f3Smrg                          gallivm->target, texture_type,
717ec681f3Smrg                          LP_JIT_TEXTURE_WIDTH);
727ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, height,
737ec681f3Smrg                          gallivm->target, texture_type,
747ec681f3Smrg                          LP_JIT_TEXTURE_HEIGHT);
757ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, depth,
767ec681f3Smrg                          gallivm->target, texture_type,
777ec681f3Smrg                          LP_JIT_TEXTURE_DEPTH);
787ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, base,
797ec681f3Smrg                          gallivm->target, texture_type,
807ec681f3Smrg                          LP_JIT_TEXTURE_BASE);
817ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, row_stride,
827ec681f3Smrg                          gallivm->target, texture_type,
837ec681f3Smrg                          LP_JIT_TEXTURE_ROW_STRIDE);
847ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, img_stride,
857ec681f3Smrg                          gallivm->target, texture_type,
867ec681f3Smrg                          LP_JIT_TEXTURE_IMG_STRIDE);
877ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, first_level,
887ec681f3Smrg                          gallivm->target, texture_type,
897ec681f3Smrg                          LP_JIT_TEXTURE_FIRST_LEVEL);
907ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, last_level,
917ec681f3Smrg                          gallivm->target, texture_type,
927ec681f3Smrg                          LP_JIT_TEXTURE_LAST_LEVEL);
937ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, mip_offsets,
947ec681f3Smrg                          gallivm->target, texture_type,
957ec681f3Smrg                          LP_JIT_TEXTURE_MIP_OFFSETS);
967ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, num_samples,
977ec681f3Smrg                          gallivm->target, texture_type,
987ec681f3Smrg                          LP_JIT_TEXTURE_NUM_SAMPLES);
997ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, sample_stride,
1007ec681f3Smrg                          gallivm->target, texture_type,
1017ec681f3Smrg                          LP_JIT_TEXTURE_SAMPLE_STRIDE);
1027ec681f3Smrg   LP_CHECK_STRUCT_SIZE(struct lp_jit_texture,
1037ec681f3Smrg                        gallivm->target, texture_type);
1047ec681f3Smrg   return texture_type;
1057ec681f3Smrg}
1067ec681f3Smrg
1077ec681f3Smrgstatic LLVMTypeRef
1087ec681f3Smrgcreate_jit_sampler_type(struct gallivm_state *gallivm)
1097ec681f3Smrg{
1107ec681f3Smrg   LLVMContextRef lc = gallivm->context;
1117ec681f3Smrg   LLVMTypeRef sampler_type;
1127ec681f3Smrg   LLVMTypeRef elem_types[LP_JIT_SAMPLER_NUM_FIELDS];
1137ec681f3Smrg   elem_types[LP_JIT_SAMPLER_MIN_LOD] =
1147ec681f3Smrg   elem_types[LP_JIT_SAMPLER_MAX_LOD] =
1157ec681f3Smrg   elem_types[LP_JIT_SAMPLER_LOD_BIAS] =
1167ec681f3Smrg   elem_types[LP_JIT_SAMPLER_MAX_ANISO] = LLVMFloatTypeInContext(lc);
1177ec681f3Smrg   elem_types[LP_JIT_SAMPLER_BORDER_COLOR] =
1187ec681f3Smrg      LLVMArrayType(LLVMFloatTypeInContext(lc), 4);
1197ec681f3Smrg
1207ec681f3Smrg   sampler_type = LLVMStructTypeInContext(lc, elem_types,
1217ec681f3Smrg                                          ARRAY_SIZE(elem_types), 0);
1227ec681f3Smrg
1237ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler, min_lod,
1247ec681f3Smrg                          gallivm->target, sampler_type,
1257ec681f3Smrg                          LP_JIT_SAMPLER_MIN_LOD);
1267ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler, max_lod,
1277ec681f3Smrg                          gallivm->target, sampler_type,
1287ec681f3Smrg                          LP_JIT_SAMPLER_MAX_LOD);
1297ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler, lod_bias,
1307ec681f3Smrg                          gallivm->target, sampler_type,
1317ec681f3Smrg                          LP_JIT_SAMPLER_LOD_BIAS);
1327ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler, border_color,
1337ec681f3Smrg                          gallivm->target, sampler_type,
1347ec681f3Smrg                          LP_JIT_SAMPLER_BORDER_COLOR);
1357ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler, max_aniso,
1367ec681f3Smrg                          gallivm->target, sampler_type,
1377ec681f3Smrg                          LP_JIT_SAMPLER_MAX_ANISO);
1387ec681f3Smrg   LP_CHECK_STRUCT_SIZE(struct lp_jit_sampler,
1397ec681f3Smrg                        gallivm->target, sampler_type);
1407ec681f3Smrg   return sampler_type;
1417ec681f3Smrg}
1427ec681f3Smrg
1437ec681f3Smrgstatic LLVMTypeRef
1447ec681f3Smrgcreate_jit_image_type(struct gallivm_state *gallivm)
1457ec681f3Smrg{
1467ec681f3Smrg   LLVMContextRef lc = gallivm->context;
1477ec681f3Smrg   LLVMTypeRef image_type;
1487ec681f3Smrg   LLVMTypeRef elem_types[LP_JIT_IMAGE_NUM_FIELDS];
1497ec681f3Smrg   elem_types[LP_JIT_IMAGE_WIDTH] =
1507ec681f3Smrg   elem_types[LP_JIT_IMAGE_HEIGHT] =
1517ec681f3Smrg   elem_types[LP_JIT_IMAGE_DEPTH] = LLVMInt32TypeInContext(lc);
1527ec681f3Smrg   elem_types[LP_JIT_IMAGE_BASE] = LLVMPointerType(LLVMInt8TypeInContext(lc), 0);
1537ec681f3Smrg   elem_types[LP_JIT_IMAGE_ROW_STRIDE] =
1547ec681f3Smrg   elem_types[LP_JIT_IMAGE_IMG_STRIDE] =
1557ec681f3Smrg   elem_types[LP_JIT_IMAGE_NUM_SAMPLES] =
1567ec681f3Smrg   elem_types[LP_JIT_IMAGE_SAMPLE_STRIDE] = LLVMInt32TypeInContext(lc);
1577ec681f3Smrg
1587ec681f3Smrg   image_type = LLVMStructTypeInContext(lc, elem_types,
1597ec681f3Smrg                                        ARRAY_SIZE(elem_types), 0);
1607ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_image, width,
1617ec681f3Smrg                          gallivm->target, image_type,
1627ec681f3Smrg                          LP_JIT_IMAGE_WIDTH);
1637ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_image, height,
1647ec681f3Smrg                          gallivm->target, image_type,
1657ec681f3Smrg                          LP_JIT_IMAGE_HEIGHT);
1667ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_image, depth,
1677ec681f3Smrg                          gallivm->target, image_type,
1687ec681f3Smrg                          LP_JIT_IMAGE_DEPTH);
1697ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_image, base,
1707ec681f3Smrg                          gallivm->target, image_type,
1717ec681f3Smrg                          LP_JIT_IMAGE_BASE);
1727ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_image, row_stride,
1737ec681f3Smrg                          gallivm->target, image_type,
1747ec681f3Smrg                          LP_JIT_IMAGE_ROW_STRIDE);
1757ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_image, img_stride,
1767ec681f3Smrg                          gallivm->target, image_type,
1777ec681f3Smrg                          LP_JIT_IMAGE_IMG_STRIDE);
1787ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_image, num_samples,
1797ec681f3Smrg                          gallivm->target, image_type,
1807ec681f3Smrg                          LP_JIT_IMAGE_NUM_SAMPLES);
1817ec681f3Smrg   LP_CHECK_MEMBER_OFFSET(struct lp_jit_image, sample_stride,
1827ec681f3Smrg                          gallivm->target, image_type,
1837ec681f3Smrg                          LP_JIT_IMAGE_SAMPLE_STRIDE);
1847ec681f3Smrg   return image_type;
1857ec681f3Smrg}
1864a49301eSmrg
1874a49301eSmrgstatic void
188af69d88dSmrglp_jit_create_types(struct lp_fragment_shader_variant *lp)
1894a49301eSmrg{
1903464ebd5Sriastradh   struct gallivm_state *gallivm = lp->gallivm;
1913464ebd5Sriastradh   LLVMContextRef lc = gallivm->context;
1927ec681f3Smrg   LLVMTypeRef viewport_type, texture_type, sampler_type, image_type;
1937ec681f3Smrg   LLVMTypeRef linear_elem_type;
194af69d88dSmrg
195af69d88dSmrg   /* struct lp_jit_viewport */
196af69d88dSmrg   {
197af69d88dSmrg      LLVMTypeRef elem_types[LP_JIT_VIEWPORT_NUM_FIELDS];
198af69d88dSmrg
199af69d88dSmrg      elem_types[LP_JIT_VIEWPORT_MIN_DEPTH] =
200af69d88dSmrg      elem_types[LP_JIT_VIEWPORT_MAX_DEPTH] = LLVMFloatTypeInContext(lc);
201af69d88dSmrg
202af69d88dSmrg      viewport_type = LLVMStructTypeInContext(lc, elem_types,
20301e04c3fSmrg                                              ARRAY_SIZE(elem_types), 0);
204af69d88dSmrg
205af69d88dSmrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_viewport, min_depth,
206af69d88dSmrg                             gallivm->target, viewport_type,
207af69d88dSmrg                             LP_JIT_VIEWPORT_MIN_DEPTH);
208af69d88dSmrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_viewport, max_depth,
209af69d88dSmrg                             gallivm->target, viewport_type,
210af69d88dSmrg                             LP_JIT_VIEWPORT_MAX_DEPTH);
211af69d88dSmrg      LP_CHECK_STRUCT_SIZE(struct lp_jit_viewport,
212af69d88dSmrg                           gallivm->target, viewport_type);
213af69d88dSmrg   }
2144a49301eSmrg
2157ec681f3Smrg   texture_type = create_jit_texture_type(gallivm);
2167ec681f3Smrg   sampler_type = create_jit_sampler_type(gallivm);
2177ec681f3Smrg   image_type = create_jit_image_type(gallivm);
2184a49301eSmrg
2194a49301eSmrg   /* struct lp_jit_context */
2204a49301eSmrg   {
2213464ebd5Sriastradh      LLVMTypeRef elem_types[LP_JIT_CTX_COUNT];
2224a49301eSmrg      LLVMTypeRef context_type;
2234a49301eSmrg
224af69d88dSmrg      elem_types[LP_JIT_CTX_CONSTANTS] =
225af69d88dSmrg         LLVMArrayType(LLVMPointerType(LLVMFloatTypeInContext(lc), 0), LP_MAX_TGSI_CONST_BUFFERS);
226af69d88dSmrg      elem_types[LP_JIT_CTX_NUM_CONSTANTS] =
227af69d88dSmrg            LLVMArrayType(LLVMInt32TypeInContext(lc), LP_MAX_TGSI_CONST_BUFFERS);
2287ec681f3Smrg      elem_types[LP_JIT_CTX_TEXTURES] = LLVMArrayType(texture_type,
2297ec681f3Smrg                                                      PIPE_MAX_SHADER_SAMPLER_VIEWS);
2307ec681f3Smrg      elem_types[LP_JIT_CTX_SAMPLERS] = LLVMArrayType(sampler_type,
2317ec681f3Smrg                                                      PIPE_MAX_SAMPLERS);
2327ec681f3Smrg      elem_types[LP_JIT_CTX_IMAGES] = LLVMArrayType(image_type,
2337ec681f3Smrg                                                    PIPE_MAX_SHADER_IMAGES);
2343464ebd5Sriastradh      elem_types[LP_JIT_CTX_ALPHA_REF] = LLVMFloatTypeInContext(lc);
2357ec681f3Smrg      elem_types[LP_JIT_CTX_SAMPLE_MASK] =
2363464ebd5Sriastradh      elem_types[LP_JIT_CTX_STENCIL_REF_FRONT] =
2373464ebd5Sriastradh      elem_types[LP_JIT_CTX_STENCIL_REF_BACK] = LLVMInt32TypeInContext(lc);
238af69d88dSmrg      elem_types[LP_JIT_CTX_U8_BLEND_COLOR] = LLVMPointerType(LLVMInt8TypeInContext(lc), 0);
239af69d88dSmrg      elem_types[LP_JIT_CTX_F_BLEND_COLOR] = LLVMPointerType(LLVMFloatTypeInContext(lc), 0);
240af69d88dSmrg      elem_types[LP_JIT_CTX_VIEWPORTS] = LLVMPointerType(viewport_type, 0);
2417ec681f3Smrg      elem_types[LP_JIT_CTX_ANISO_FILTER_TABLE] = LLVMPointerType(LLVMFloatTypeInContext(lc), 0);
2427ec681f3Smrg      elem_types[LP_JIT_CTX_SSBOS] =
2437ec681f3Smrg         LLVMArrayType(LLVMPointerType(LLVMInt32TypeInContext(lc), 0), LP_MAX_TGSI_SHADER_BUFFERS);
2447ec681f3Smrg      elem_types[LP_JIT_CTX_NUM_SSBOS] =
2457ec681f3Smrg            LLVMArrayType(LLVMInt32TypeInContext(lc), LP_MAX_TGSI_SHADER_BUFFERS);
2463464ebd5Sriastradh      context_type = LLVMStructTypeInContext(lc, elem_types,
24701e04c3fSmrg                                             ARRAY_SIZE(elem_types), 0);
2483464ebd5Sriastradh
2494a49301eSmrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, constants,
2503464ebd5Sriastradh                             gallivm->target, context_type,
2513464ebd5Sriastradh                             LP_JIT_CTX_CONSTANTS);
252af69d88dSmrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, num_constants,
253af69d88dSmrg                             gallivm->target, context_type,
254af69d88dSmrg                             LP_JIT_CTX_NUM_CONSTANTS);
2557ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, textures,
2567ec681f3Smrg                             gallivm->target, context_type,
2577ec681f3Smrg                             LP_JIT_CTX_TEXTURES);
2587ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, samplers,
2597ec681f3Smrg                             gallivm->target, context_type,
2607ec681f3Smrg                             LP_JIT_CTX_SAMPLERS);
2617ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, images,
2627ec681f3Smrg                             gallivm->target, context_type,
2637ec681f3Smrg                             LP_JIT_CTX_IMAGES);
2644a49301eSmrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, alpha_ref_value,
2653464ebd5Sriastradh                             gallivm->target, context_type,
2663464ebd5Sriastradh                             LP_JIT_CTX_ALPHA_REF);
2673464ebd5Sriastradh      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, stencil_ref_front,
2683464ebd5Sriastradh                             gallivm->target, context_type,
2693464ebd5Sriastradh                             LP_JIT_CTX_STENCIL_REF_FRONT);
2703464ebd5Sriastradh      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, stencil_ref_back,
2713464ebd5Sriastradh                             gallivm->target, context_type,
2723464ebd5Sriastradh                             LP_JIT_CTX_STENCIL_REF_BACK);
273af69d88dSmrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, u8_blend_color,
274af69d88dSmrg                             gallivm->target, context_type,
275af69d88dSmrg                             LP_JIT_CTX_U8_BLEND_COLOR);
276af69d88dSmrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, f_blend_color,
2773464ebd5Sriastradh                             gallivm->target, context_type,
278af69d88dSmrg                             LP_JIT_CTX_F_BLEND_COLOR);
279af69d88dSmrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, viewports,
280af69d88dSmrg                             gallivm->target, context_type,
281af69d88dSmrg                             LP_JIT_CTX_VIEWPORTS);
2827ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, ssbos,
2833464ebd5Sriastradh                             gallivm->target, context_type,
2847ec681f3Smrg                             LP_JIT_CTX_SSBOS);
2857ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, num_ssbos,
286af69d88dSmrg                             gallivm->target, context_type,
2877ec681f3Smrg                             LP_JIT_CTX_NUM_SSBOS);
2887ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, sample_mask,
2897ec681f3Smrg                             gallivm->target, context_type,
2907ec681f3Smrg                             LP_JIT_CTX_SAMPLE_MASK);
2917ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, aniso_filter_table,
2927ec681f3Smrg                             gallivm->target, context_type,
2937ec681f3Smrg                             LP_JIT_CTX_ANISO_FILTER_TABLE);
2944a49301eSmrg      LP_CHECK_STRUCT_SIZE(struct lp_jit_context,
2953464ebd5Sriastradh                           gallivm->target, context_type);
2964a49301eSmrg
2973464ebd5Sriastradh      lp->jit_context_ptr_type = LLVMPointerType(context_type, 0);
2984a49301eSmrg   }
2994a49301eSmrg
300af69d88dSmrg   /* struct lp_jit_thread_data */
301af69d88dSmrg   {
302af69d88dSmrg      LLVMTypeRef elem_types[LP_JIT_THREAD_DATA_COUNT];
303af69d88dSmrg      LLVMTypeRef thread_data_type;
304af69d88dSmrg
30501e04c3fSmrg      elem_types[LP_JIT_THREAD_DATA_CACHE] =
30601e04c3fSmrg            LLVMPointerType(lp_build_format_cache_type(gallivm), 0);
307af69d88dSmrg      elem_types[LP_JIT_THREAD_DATA_COUNTER] = LLVMInt64TypeInContext(lc);
30801e04c3fSmrg      elem_types[LP_JIT_THREAD_DATA_INVOCATIONS] = LLVMInt64TypeInContext(lc);
309af69d88dSmrg      elem_types[LP_JIT_THREAD_DATA_RASTER_STATE_VIEWPORT_INDEX] =
3107ec681f3Smrg      elem_types[LP_JIT_THREAD_DATA_RASTER_STATE_VIEW_INDEX] =
311af69d88dSmrg            LLVMInt32TypeInContext(lc);
312af69d88dSmrg
313af69d88dSmrg      thread_data_type = LLVMStructTypeInContext(lc, elem_types,
31401e04c3fSmrg                                                 ARRAY_SIZE(elem_types), 0);
315af69d88dSmrg
316af69d88dSmrg      lp->jit_thread_data_ptr_type = LLVMPointerType(thread_data_type, 0);
317af69d88dSmrg   }
318af69d88dSmrg
3197ec681f3Smrg   /*
3207ec681f3Smrg    * lp_linear_elem
3217ec681f3Smrg    *
3227ec681f3Smrg    * XXX: it can be instanced only once due to the use of opaque types, and
3237ec681f3Smrg    * the fact that screen->module is also a global.
3247ec681f3Smrg    */
3257ec681f3Smrg   {
3267ec681f3Smrg      LLVMTypeRef ret_type;
3277ec681f3Smrg      LLVMTypeRef arg_types[1];
3287ec681f3Smrg      LLVMTypeRef func_type;
3297ec681f3Smrg
3307ec681f3Smrg      ret_type = LLVMPointerType(LLVMVectorType(LLVMInt8TypeInContext(lc), 16), 0);
3317ec681f3Smrg
3327ec681f3Smrg      arg_types[0] = LLVMPointerType(LLVMInt8TypeInContext(lc), 0);
3337ec681f3Smrg
3347ec681f3Smrg      /* lp_linear_func */
3357ec681f3Smrg      func_type = LLVMFunctionType(ret_type, arg_types, ARRAY_SIZE(arg_types), 0);
3367ec681f3Smrg
3377ec681f3Smrg      /*
3387ec681f3Smrg       * We actually define lp_linear_elem not as a structure but simply as a
3397ec681f3Smrg       * lp_linear_func pointer
3407ec681f3Smrg       */
3417ec681f3Smrg      linear_elem_type = LLVMPointerType(func_type, 0);
3427ec681f3Smrg   }
3437ec681f3Smrg
3447ec681f3Smrg   /* struct lp_jit_linear_context */
3457ec681f3Smrg   {
3467ec681f3Smrg      LLVMTypeRef linear_elem_ptr_type = LLVMPointerType(linear_elem_type, 0);
3477ec681f3Smrg      LLVMTypeRef elem_types[LP_JIT_LINEAR_CTX_COUNT];
3487ec681f3Smrg      LLVMTypeRef linear_context_type;
3497ec681f3Smrg
3507ec681f3Smrg
3517ec681f3Smrg      elem_types[LP_JIT_LINEAR_CTX_CONSTANTS] = LLVMPointerType(LLVMInt8TypeInContext(lc), 0);
3527ec681f3Smrg      elem_types[LP_JIT_LINEAR_CTX_TEX] =
3537ec681f3Smrg            LLVMArrayType(linear_elem_ptr_type, LP_MAX_LINEAR_TEXTURES);
3547ec681f3Smrg      elem_types[LP_JIT_LINEAR_CTX_INPUTS] =
3557ec681f3Smrg            LLVMArrayType(linear_elem_ptr_type, LP_MAX_LINEAR_INPUTS);
3567ec681f3Smrg      elem_types[LP_JIT_LINEAR_CTX_COLOR0] = LLVMPointerType(LLVMInt8TypeInContext(lc), 0);
3577ec681f3Smrg      elem_types[LP_JIT_LINEAR_CTX_BLEND_COLOR] = LLVMInt32TypeInContext(lc);
3587ec681f3Smrg      elem_types[LP_JIT_LINEAR_CTX_ALPHA_REF] = LLVMInt8TypeInContext(lc);
3597ec681f3Smrg
3607ec681f3Smrg      linear_context_type = LLVMStructTypeInContext(lc, elem_types,
3617ec681f3Smrg                                                    ARRAY_SIZE(elem_types), 0);
3627ec681f3Smrg
3637ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_linear_context, constants,
3647ec681f3Smrg                             gallivm->target, linear_context_type,
3657ec681f3Smrg                             LP_JIT_LINEAR_CTX_CONSTANTS);
3667ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_linear_context, tex,
3677ec681f3Smrg                             gallivm->target, linear_context_type,
3687ec681f3Smrg                             LP_JIT_LINEAR_CTX_TEX);
3697ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_linear_context, inputs,
3707ec681f3Smrg                             gallivm->target, linear_context_type,
3717ec681f3Smrg                             LP_JIT_LINEAR_CTX_INPUTS);
3727ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_linear_context, color0,
3737ec681f3Smrg                             gallivm->target, linear_context_type,
3747ec681f3Smrg                             LP_JIT_LINEAR_CTX_COLOR0);
3757ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_linear_context, blend_color,
3767ec681f3Smrg                             gallivm->target, linear_context_type,
3777ec681f3Smrg                             LP_JIT_LINEAR_CTX_BLEND_COLOR);
3787ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_linear_context, alpha_ref_value,
3797ec681f3Smrg                             gallivm->target, linear_context_type,
3807ec681f3Smrg                             LP_JIT_LINEAR_CTX_ALPHA_REF);
3817ec681f3Smrg      LP_CHECK_STRUCT_SIZE(struct lp_jit_linear_context,
3827ec681f3Smrg                           gallivm->target, linear_context_type);
3837ec681f3Smrg
3847ec681f3Smrg      lp->jit_linear_context_ptr_type = LLVMPointerType(linear_context_type, 0);
3857ec681f3Smrg   }
3867ec681f3Smrg
3873464ebd5Sriastradh   if (gallivm_debug & GALLIVM_DEBUG_IR) {
38801e04c3fSmrg      char *str = LLVMPrintModuleToString(gallivm->module);
38901e04c3fSmrg      fprintf(stderr, "%s", str);
39001e04c3fSmrg      LLVMDisposeMessage(str);
3913464ebd5Sriastradh   }
3924a49301eSmrg}
3934a49301eSmrg
3944a49301eSmrg
3954a49301eSmrgvoid
3964a49301eSmrglp_jit_screen_cleanup(struct llvmpipe_screen *screen)
3974a49301eSmrg{
3983464ebd5Sriastradh   /* nothing */
3994a49301eSmrg}
4004a49301eSmrg
4014a49301eSmrg
40201e04c3fSmrgboolean
4034a49301eSmrglp_jit_screen_init(struct llvmpipe_screen *screen)
4044a49301eSmrg{
40501e04c3fSmrg   return lp_build_init();
4063464ebd5Sriastradh}
4074a49301eSmrg
4084a49301eSmrg
409af69d88dSmrgvoid
410af69d88dSmrglp_jit_init_types(struct lp_fragment_shader_variant *lp)
4113464ebd5Sriastradh{
4123464ebd5Sriastradh   if (!lp->jit_context_ptr_type)
4133464ebd5Sriastradh      lp_jit_create_types(lp);
4144a49301eSmrg}
4157ec681f3Smrg
4167ec681f3Smrgstatic void
4177ec681f3Smrglp_jit_create_cs_types(struct lp_compute_shader_variant *lp)
4187ec681f3Smrg{
4197ec681f3Smrg   struct gallivm_state *gallivm = lp->gallivm;
4207ec681f3Smrg   LLVMContextRef lc = gallivm->context;
4217ec681f3Smrg   LLVMTypeRef texture_type, sampler_type, image_type;
4227ec681f3Smrg
4237ec681f3Smrg   texture_type = create_jit_texture_type(gallivm);
4247ec681f3Smrg   sampler_type = create_jit_sampler_type(gallivm);
4257ec681f3Smrg   image_type = create_jit_image_type(gallivm);
4267ec681f3Smrg
4277ec681f3Smrg   /* struct lp_jit_cs_thread_data */
4287ec681f3Smrg   {
4297ec681f3Smrg      LLVMTypeRef elem_types[LP_JIT_CS_THREAD_DATA_COUNT];
4307ec681f3Smrg      LLVMTypeRef thread_data_type;
4317ec681f3Smrg
4327ec681f3Smrg      elem_types[LP_JIT_CS_THREAD_DATA_CACHE] =
4337ec681f3Smrg            LLVMPointerType(lp_build_format_cache_type(gallivm), 0);
4347ec681f3Smrg
4357ec681f3Smrg      elem_types[LP_JIT_CS_THREAD_DATA_SHARED] = LLVMPointerType(LLVMInt32TypeInContext(lc), 0);
4367ec681f3Smrg      thread_data_type = LLVMStructTypeInContext(lc, elem_types,
4377ec681f3Smrg                                                 ARRAY_SIZE(elem_types), 0);
4387ec681f3Smrg
4397ec681f3Smrg      lp->jit_cs_thread_data_ptr_type = LLVMPointerType(thread_data_type, 0);
4407ec681f3Smrg   }
4417ec681f3Smrg
4427ec681f3Smrg   /* struct lp_jit_cs_context */
4437ec681f3Smrg   {
4447ec681f3Smrg      LLVMTypeRef elem_types[LP_JIT_CS_CTX_COUNT];
4457ec681f3Smrg      LLVMTypeRef cs_context_type;
4467ec681f3Smrg
4477ec681f3Smrg      elem_types[LP_JIT_CS_CTX_CONSTANTS] =
4487ec681f3Smrg         LLVMArrayType(LLVMPointerType(LLVMFloatTypeInContext(lc), 0), LP_MAX_TGSI_CONST_BUFFERS);
4497ec681f3Smrg      elem_types[LP_JIT_CS_CTX_NUM_CONSTANTS] =
4507ec681f3Smrg            LLVMArrayType(LLVMInt32TypeInContext(lc), LP_MAX_TGSI_CONST_BUFFERS);
4517ec681f3Smrg      elem_types[LP_JIT_CS_CTX_TEXTURES] = LLVMArrayType(texture_type,
4527ec681f3Smrg                                                      PIPE_MAX_SHADER_SAMPLER_VIEWS);
4537ec681f3Smrg      elem_types[LP_JIT_CS_CTX_SAMPLERS] = LLVMArrayType(sampler_type,
4547ec681f3Smrg                                                      PIPE_MAX_SAMPLERS);
4557ec681f3Smrg      elem_types[LP_JIT_CS_CTX_IMAGES] = LLVMArrayType(image_type,
4567ec681f3Smrg                                                       PIPE_MAX_SHADER_IMAGES);
4577ec681f3Smrg      elem_types[LP_JIT_CS_CTX_SSBOS] =
4587ec681f3Smrg         LLVMArrayType(LLVMPointerType(LLVMInt32TypeInContext(lc), 0), LP_MAX_TGSI_SHADER_BUFFERS);
4597ec681f3Smrg      elem_types[LP_JIT_CS_CTX_NUM_SSBOS] =
4607ec681f3Smrg            LLVMArrayType(LLVMInt32TypeInContext(lc), LP_MAX_TGSI_SHADER_BUFFERS);
4617ec681f3Smrg
4627ec681f3Smrg      elem_types[LP_JIT_CS_CTX_SHARED_SIZE] = LLVMInt32TypeInContext(lc);
4637ec681f3Smrg
4647ec681f3Smrg      elem_types[LP_JIT_CS_CTX_KERNEL_ARGS] = LLVMPointerType(LLVMInt8TypeInContext(lc), 0);
4657ec681f3Smrg
4667ec681f3Smrg      elem_types[LP_JIT_CS_CTX_ANISO_FILTER_TABLE] = LLVMPointerType(LLVMFloatTypeInContext(lc), 0);
4677ec681f3Smrg
4687ec681f3Smrg      cs_context_type = LLVMStructTypeInContext(lc, elem_types,
4697ec681f3Smrg                                             ARRAY_SIZE(elem_types), 0);
4707ec681f3Smrg
4717ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context, constants,
4727ec681f3Smrg                             gallivm->target, cs_context_type,
4737ec681f3Smrg                             LP_JIT_CS_CTX_CONSTANTS);
4747ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context, num_constants,
4757ec681f3Smrg                             gallivm->target, cs_context_type,
4767ec681f3Smrg                             LP_JIT_CS_CTX_NUM_CONSTANTS);
4777ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context, textures,
4787ec681f3Smrg                             gallivm->target, cs_context_type,
4797ec681f3Smrg                             LP_JIT_CS_CTX_TEXTURES);
4807ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context, samplers,
4817ec681f3Smrg                             gallivm->target, cs_context_type,
4827ec681f3Smrg                             LP_JIT_CS_CTX_SAMPLERS);
4837ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context, images,
4847ec681f3Smrg                             gallivm->target, cs_context_type,
4857ec681f3Smrg                             LP_JIT_CS_CTX_IMAGES);
4867ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context, ssbos,
4877ec681f3Smrg                             gallivm->target, cs_context_type,
4887ec681f3Smrg                             LP_JIT_CS_CTX_SSBOS);
4897ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context, num_ssbos,
4907ec681f3Smrg                             gallivm->target, cs_context_type,
4917ec681f3Smrg                             LP_JIT_CS_CTX_NUM_SSBOS);
4927ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context, shared_size,
4937ec681f3Smrg                             gallivm->target, cs_context_type,
4947ec681f3Smrg                             LP_JIT_CS_CTX_SHARED_SIZE);
4957ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context, kernel_args,
4967ec681f3Smrg                             gallivm->target, cs_context_type,
4977ec681f3Smrg                             LP_JIT_CS_CTX_KERNEL_ARGS);
4987ec681f3Smrg      LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context, aniso_filter_table,
4997ec681f3Smrg                             gallivm->target, cs_context_type,
5007ec681f3Smrg                             LP_JIT_CS_CTX_ANISO_FILTER_TABLE);
5017ec681f3Smrg      LP_CHECK_STRUCT_SIZE(struct lp_jit_cs_context,
5027ec681f3Smrg                           gallivm->target, cs_context_type);
5037ec681f3Smrg
5047ec681f3Smrg      lp->jit_cs_context_ptr_type = LLVMPointerType(cs_context_type, 0);
5057ec681f3Smrg   }
5067ec681f3Smrg
5077ec681f3Smrg   if (gallivm_debug & GALLIVM_DEBUG_IR) {
5087ec681f3Smrg      char *str = LLVMPrintModuleToString(gallivm->module);
5097ec681f3Smrg      fprintf(stderr, "%s", str);
5107ec681f3Smrg      LLVMDisposeMessage(str);
5117ec681f3Smrg   }
5127ec681f3Smrg}
5137ec681f3Smrg
5147ec681f3Smrgvoid
5157ec681f3Smrglp_jit_init_cs_types(struct lp_compute_shader_variant *lp)
5167ec681f3Smrg{
5177ec681f3Smrg   if (!lp->jit_cs_context_ptr_type)
5187ec681f3Smrg      lp_jit_create_cs_types(lp);
5197ec681f3Smrg}
520