1/**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28/**
29 * @file
30 * C - JIT interfaces
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
35
36#include "util/u_memory.h"
37#include "gallivm/lp_bld_init.h"
38#include "gallivm/lp_bld_debug.h"
39#include "gallivm/lp_bld_format.h"
40#include "lp_context.h"
41#include "lp_jit.h"
42
43
44static void
45lp_jit_create_types(struct lp_fragment_shader_variant *lp)
46{
47   struct gallivm_state *gallivm = lp->gallivm;
48   LLVMContextRef lc = gallivm->context;
49   LLVMTypeRef viewport_type, texture_type, sampler_type;
50
51   /* struct lp_jit_viewport */
52   {
53      LLVMTypeRef elem_types[LP_JIT_VIEWPORT_NUM_FIELDS];
54
55      elem_types[LP_JIT_VIEWPORT_MIN_DEPTH] =
56      elem_types[LP_JIT_VIEWPORT_MAX_DEPTH] = LLVMFloatTypeInContext(lc);
57
58      viewport_type = LLVMStructTypeInContext(lc, elem_types,
59                                              ARRAY_SIZE(elem_types), 0);
60
61      LP_CHECK_MEMBER_OFFSET(struct lp_jit_viewport, min_depth,
62                             gallivm->target, viewport_type,
63                             LP_JIT_VIEWPORT_MIN_DEPTH);
64      LP_CHECK_MEMBER_OFFSET(struct lp_jit_viewport, max_depth,
65                             gallivm->target, viewport_type,
66                             LP_JIT_VIEWPORT_MAX_DEPTH);
67      LP_CHECK_STRUCT_SIZE(struct lp_jit_viewport,
68                           gallivm->target, viewport_type);
69   }
70
71   /* struct lp_jit_texture */
72   {
73      LLVMTypeRef elem_types[LP_JIT_TEXTURE_NUM_FIELDS];
74
75      elem_types[LP_JIT_TEXTURE_WIDTH]  =
76      elem_types[LP_JIT_TEXTURE_HEIGHT] =
77      elem_types[LP_JIT_TEXTURE_DEPTH] =
78      elem_types[LP_JIT_TEXTURE_FIRST_LEVEL] =
79      elem_types[LP_JIT_TEXTURE_LAST_LEVEL] = LLVMInt32TypeInContext(lc);
80      elem_types[LP_JIT_TEXTURE_BASE] = LLVMPointerType(LLVMInt8TypeInContext(lc), 0);
81      elem_types[LP_JIT_TEXTURE_ROW_STRIDE] =
82      elem_types[LP_JIT_TEXTURE_IMG_STRIDE] =
83      elem_types[LP_JIT_TEXTURE_MIP_OFFSETS] =
84         LLVMArrayType(LLVMInt32TypeInContext(lc), LP_MAX_TEXTURE_LEVELS);
85
86      texture_type = LLVMStructTypeInContext(lc, elem_types,
87                                             ARRAY_SIZE(elem_types), 0);
88
89      LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, width,
90                             gallivm->target, texture_type,
91                             LP_JIT_TEXTURE_WIDTH);
92      LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, height,
93                             gallivm->target, texture_type,
94                             LP_JIT_TEXTURE_HEIGHT);
95      LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, depth,
96                             gallivm->target, texture_type,
97                             LP_JIT_TEXTURE_DEPTH);
98      LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, first_level,
99                             gallivm->target, texture_type,
100                             LP_JIT_TEXTURE_FIRST_LEVEL);
101      LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, last_level,
102                             gallivm->target, texture_type,
103                             LP_JIT_TEXTURE_LAST_LEVEL);
104      LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, base,
105                             gallivm->target, texture_type,
106                             LP_JIT_TEXTURE_BASE);
107      LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, row_stride,
108                             gallivm->target, texture_type,
109                             LP_JIT_TEXTURE_ROW_STRIDE);
110      LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, img_stride,
111                             gallivm->target, texture_type,
112                             LP_JIT_TEXTURE_IMG_STRIDE);
113      LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, mip_offsets,
114                             gallivm->target, texture_type,
115                             LP_JIT_TEXTURE_MIP_OFFSETS);
116      LP_CHECK_STRUCT_SIZE(struct lp_jit_texture,
117                           gallivm->target, texture_type);
118   }
119
120   /* struct lp_jit_sampler */
121   {
122      LLVMTypeRef elem_types[LP_JIT_SAMPLER_NUM_FIELDS];
123      elem_types[LP_JIT_SAMPLER_MIN_LOD] =
124      elem_types[LP_JIT_SAMPLER_MAX_LOD] =
125      elem_types[LP_JIT_SAMPLER_LOD_BIAS] = LLVMFloatTypeInContext(lc);
126      elem_types[LP_JIT_SAMPLER_BORDER_COLOR] =
127         LLVMArrayType(LLVMFloatTypeInContext(lc), 4);
128
129      sampler_type = LLVMStructTypeInContext(lc, elem_types,
130                                             ARRAY_SIZE(elem_types), 0);
131
132      LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler, min_lod,
133                             gallivm->target, sampler_type,
134                             LP_JIT_SAMPLER_MIN_LOD);
135      LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler, max_lod,
136                             gallivm->target, sampler_type,
137                             LP_JIT_SAMPLER_MAX_LOD);
138      LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler, lod_bias,
139                             gallivm->target, sampler_type,
140                             LP_JIT_SAMPLER_LOD_BIAS);
141      LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler, border_color,
142                             gallivm->target, sampler_type,
143                             LP_JIT_SAMPLER_BORDER_COLOR);
144      LP_CHECK_STRUCT_SIZE(struct lp_jit_sampler,
145                           gallivm->target, sampler_type);
146   }
147
148   /* struct lp_jit_context */
149   {
150      LLVMTypeRef elem_types[LP_JIT_CTX_COUNT];
151      LLVMTypeRef context_type;
152
153      elem_types[LP_JIT_CTX_CONSTANTS] =
154         LLVMArrayType(LLVMPointerType(LLVMFloatTypeInContext(lc), 0), LP_MAX_TGSI_CONST_BUFFERS);
155      elem_types[LP_JIT_CTX_NUM_CONSTANTS] =
156            LLVMArrayType(LLVMInt32TypeInContext(lc), LP_MAX_TGSI_CONST_BUFFERS);
157      elem_types[LP_JIT_CTX_ALPHA_REF] = LLVMFloatTypeInContext(lc);
158      elem_types[LP_JIT_CTX_STENCIL_REF_FRONT] =
159      elem_types[LP_JIT_CTX_STENCIL_REF_BACK] = LLVMInt32TypeInContext(lc);
160      elem_types[LP_JIT_CTX_U8_BLEND_COLOR] = LLVMPointerType(LLVMInt8TypeInContext(lc), 0);
161      elem_types[LP_JIT_CTX_F_BLEND_COLOR] = LLVMPointerType(LLVMFloatTypeInContext(lc), 0);
162      elem_types[LP_JIT_CTX_VIEWPORTS] = LLVMPointerType(viewport_type, 0);
163      elem_types[LP_JIT_CTX_TEXTURES] = LLVMArrayType(texture_type,
164                                                      PIPE_MAX_SHADER_SAMPLER_VIEWS);
165      elem_types[LP_JIT_CTX_SAMPLERS] = LLVMArrayType(sampler_type,
166                                                      PIPE_MAX_SAMPLERS);
167
168      context_type = LLVMStructTypeInContext(lc, elem_types,
169                                             ARRAY_SIZE(elem_types), 0);
170
171      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, constants,
172                             gallivm->target, context_type,
173                             LP_JIT_CTX_CONSTANTS);
174      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, num_constants,
175                             gallivm->target, context_type,
176                             LP_JIT_CTX_NUM_CONSTANTS);
177      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, alpha_ref_value,
178                             gallivm->target, context_type,
179                             LP_JIT_CTX_ALPHA_REF);
180      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, stencil_ref_front,
181                             gallivm->target, context_type,
182                             LP_JIT_CTX_STENCIL_REF_FRONT);
183      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, stencil_ref_back,
184                             gallivm->target, context_type,
185                             LP_JIT_CTX_STENCIL_REF_BACK);
186      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, u8_blend_color,
187                             gallivm->target, context_type,
188                             LP_JIT_CTX_U8_BLEND_COLOR);
189      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, f_blend_color,
190                             gallivm->target, context_type,
191                             LP_JIT_CTX_F_BLEND_COLOR);
192      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, viewports,
193                             gallivm->target, context_type,
194                             LP_JIT_CTX_VIEWPORTS);
195      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, textures,
196                             gallivm->target, context_type,
197                             LP_JIT_CTX_TEXTURES);
198      LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, samplers,
199                             gallivm->target, context_type,
200                             LP_JIT_CTX_SAMPLERS);
201      LP_CHECK_STRUCT_SIZE(struct lp_jit_context,
202                           gallivm->target, context_type);
203
204      lp->jit_context_ptr_type = LLVMPointerType(context_type, 0);
205   }
206
207   /* struct lp_jit_thread_data */
208   {
209      LLVMTypeRef elem_types[LP_JIT_THREAD_DATA_COUNT];
210      LLVMTypeRef thread_data_type;
211
212      elem_types[LP_JIT_THREAD_DATA_CACHE] =
213            LLVMPointerType(lp_build_format_cache_type(gallivm), 0);
214      elem_types[LP_JIT_THREAD_DATA_COUNTER] = LLVMInt64TypeInContext(lc);
215      elem_types[LP_JIT_THREAD_DATA_INVOCATIONS] = LLVMInt64TypeInContext(lc);
216      elem_types[LP_JIT_THREAD_DATA_RASTER_STATE_VIEWPORT_INDEX] =
217            LLVMInt32TypeInContext(lc);
218
219      thread_data_type = LLVMStructTypeInContext(lc, elem_types,
220                                                 ARRAY_SIZE(elem_types), 0);
221
222      lp->jit_thread_data_ptr_type = LLVMPointerType(thread_data_type, 0);
223   }
224
225   if (gallivm_debug & GALLIVM_DEBUG_IR) {
226#if HAVE_LLVM >= 0x304
227      char *str = LLVMPrintModuleToString(gallivm->module);
228      fprintf(stderr, "%s", str);
229      LLVMDisposeMessage(str);
230#else
231      LLVMDumpModule(gallivm->module);
232#endif
233   }
234}
235
236
237void
238lp_jit_screen_cleanup(struct llvmpipe_screen *screen)
239{
240   /* nothing */
241}
242
243
244boolean
245lp_jit_screen_init(struct llvmpipe_screen *screen)
246{
247   return lp_build_init();
248}
249
250
251void
252lp_jit_init_types(struct lp_fragment_shader_variant *lp)
253{
254   if (!lp->jit_context_ptr_type)
255      lp_jit_create_types(lp);
256}
257