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 #include <llvm/Config/llvm-config.h>
36 
37 #include "util/u_memory.h"
38 #include "gallivm/lp_bld_init.h"
39 #include "gallivm/lp_bld_debug.h"
40 #include "gallivm/lp_bld_format.h"
41 #include "lp_context.h"
42 #include "lp_screen.h"
43 #include "lp_jit.h"
44 
45 static LLVMTypeRef
create_jit_texture_type(struct gallivm_state * gallivm)46 create_jit_texture_type(struct gallivm_state *gallivm)
47 {
48    LLVMContextRef lc = gallivm->context;
49    LLVMTypeRef texture_type;
50    LLVMTypeRef elem_types[LP_JIT_TEXTURE_NUM_FIELDS];
51 
52    /* struct lp_jit_texture */
53    elem_types[LP_JIT_TEXTURE_WIDTH]  =
54    elem_types[LP_JIT_TEXTURE_HEIGHT] =
55    elem_types[LP_JIT_TEXTURE_DEPTH] =
56    elem_types[LP_JIT_TEXTURE_NUM_SAMPLES] =
57    elem_types[LP_JIT_TEXTURE_SAMPLE_STRIDE] =
58    elem_types[LP_JIT_TEXTURE_FIRST_LEVEL] =
59    elem_types[LP_JIT_TEXTURE_LAST_LEVEL] = LLVMInt32TypeInContext(lc);
60    elem_types[LP_JIT_TEXTURE_BASE] = LLVMPointerType(LLVMInt8TypeInContext(lc), 0);
61    elem_types[LP_JIT_TEXTURE_ROW_STRIDE] =
62    elem_types[LP_JIT_TEXTURE_IMG_STRIDE] =
63    elem_types[LP_JIT_TEXTURE_MIP_OFFSETS] =
64       LLVMArrayType(LLVMInt32TypeInContext(lc), LP_MAX_TEXTURE_LEVELS);
65 
66    texture_type = LLVMStructTypeInContext(lc, elem_types,
67                                           ARRAY_SIZE(elem_types), 0);
68 
69    LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, width,
70                           gallivm->target, texture_type,
71                           LP_JIT_TEXTURE_WIDTH);
72    LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, height,
73                           gallivm->target, texture_type,
74                           LP_JIT_TEXTURE_HEIGHT);
75    LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, depth,
76                           gallivm->target, texture_type,
77                           LP_JIT_TEXTURE_DEPTH);
78    LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, base,
79                           gallivm->target, texture_type,
80                           LP_JIT_TEXTURE_BASE);
81    LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, row_stride,
82                           gallivm->target, texture_type,
83                           LP_JIT_TEXTURE_ROW_STRIDE);
84    LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, img_stride,
85                           gallivm->target, texture_type,
86                           LP_JIT_TEXTURE_IMG_STRIDE);
87    LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, first_level,
88                           gallivm->target, texture_type,
89                           LP_JIT_TEXTURE_FIRST_LEVEL);
90    LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, last_level,
91                           gallivm->target, texture_type,
92                           LP_JIT_TEXTURE_LAST_LEVEL);
93    LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, mip_offsets,
94                           gallivm->target, texture_type,
95                           LP_JIT_TEXTURE_MIP_OFFSETS);
96    LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, num_samples,
97                           gallivm->target, texture_type,
98                           LP_JIT_TEXTURE_NUM_SAMPLES);
99    LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, sample_stride,
100                           gallivm->target, texture_type,
101                           LP_JIT_TEXTURE_SAMPLE_STRIDE);
102    LP_CHECK_STRUCT_SIZE(struct lp_jit_texture,
103                         gallivm->target, texture_type);
104    return texture_type;
105 }
106 
107 static LLVMTypeRef
create_jit_sampler_type(struct gallivm_state * gallivm)108 create_jit_sampler_type(struct gallivm_state *gallivm)
109 {
110    LLVMContextRef lc = gallivm->context;
111    LLVMTypeRef sampler_type;
112    LLVMTypeRef elem_types[LP_JIT_SAMPLER_NUM_FIELDS];
113    elem_types[LP_JIT_SAMPLER_MIN_LOD] =
114    elem_types[LP_JIT_SAMPLER_MAX_LOD] =
115    elem_types[LP_JIT_SAMPLER_LOD_BIAS] =
116    elem_types[LP_JIT_SAMPLER_MAX_ANISO] = LLVMFloatTypeInContext(lc);
117    elem_types[LP_JIT_SAMPLER_BORDER_COLOR] =
118       LLVMArrayType(LLVMFloatTypeInContext(lc), 4);
119 
120    sampler_type = LLVMStructTypeInContext(lc, elem_types,
121                                           ARRAY_SIZE(elem_types), 0);
122 
123    LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler, min_lod,
124                           gallivm->target, sampler_type,
125                           LP_JIT_SAMPLER_MIN_LOD);
126    LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler, max_lod,
127                           gallivm->target, sampler_type,
128                           LP_JIT_SAMPLER_MAX_LOD);
129    LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler, lod_bias,
130                           gallivm->target, sampler_type,
131                           LP_JIT_SAMPLER_LOD_BIAS);
132    LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler, border_color,
133                           gallivm->target, sampler_type,
134                           LP_JIT_SAMPLER_BORDER_COLOR);
135    LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler, max_aniso,
136                           gallivm->target, sampler_type,
137                           LP_JIT_SAMPLER_MAX_ANISO);
138    LP_CHECK_STRUCT_SIZE(struct lp_jit_sampler,
139                         gallivm->target, sampler_type);
140    return sampler_type;
141 }
142 
143 static LLVMTypeRef
create_jit_image_type(struct gallivm_state * gallivm)144 create_jit_image_type(struct gallivm_state *gallivm)
145 {
146    LLVMContextRef lc = gallivm->context;
147    LLVMTypeRef image_type;
148    LLVMTypeRef elem_types[LP_JIT_IMAGE_NUM_FIELDS];
149    elem_types[LP_JIT_IMAGE_WIDTH] =
150    elem_types[LP_JIT_IMAGE_HEIGHT] =
151    elem_types[LP_JIT_IMAGE_DEPTH] = LLVMInt32TypeInContext(lc);
152    elem_types[LP_JIT_IMAGE_BASE] = LLVMPointerType(LLVMInt8TypeInContext(lc), 0);
153    elem_types[LP_JIT_IMAGE_ROW_STRIDE] =
154    elem_types[LP_JIT_IMAGE_IMG_STRIDE] =
155    elem_types[LP_JIT_IMAGE_NUM_SAMPLES] =
156    elem_types[LP_JIT_IMAGE_SAMPLE_STRIDE] = LLVMInt32TypeInContext(lc);
157 
158    image_type = LLVMStructTypeInContext(lc, elem_types,
159                                         ARRAY_SIZE(elem_types), 0);
160    LP_CHECK_MEMBER_OFFSET(struct lp_jit_image, width,
161                           gallivm->target, image_type,
162                           LP_JIT_IMAGE_WIDTH);
163    LP_CHECK_MEMBER_OFFSET(struct lp_jit_image, height,
164                           gallivm->target, image_type,
165                           LP_JIT_IMAGE_HEIGHT);
166    LP_CHECK_MEMBER_OFFSET(struct lp_jit_image, depth,
167                           gallivm->target, image_type,
168                           LP_JIT_IMAGE_DEPTH);
169    LP_CHECK_MEMBER_OFFSET(struct lp_jit_image, base,
170                           gallivm->target, image_type,
171                           LP_JIT_IMAGE_BASE);
172    LP_CHECK_MEMBER_OFFSET(struct lp_jit_image, row_stride,
173                           gallivm->target, image_type,
174                           LP_JIT_IMAGE_ROW_STRIDE);
175    LP_CHECK_MEMBER_OFFSET(struct lp_jit_image, img_stride,
176                           gallivm->target, image_type,
177                           LP_JIT_IMAGE_IMG_STRIDE);
178    LP_CHECK_MEMBER_OFFSET(struct lp_jit_image, num_samples,
179                           gallivm->target, image_type,
180                           LP_JIT_IMAGE_NUM_SAMPLES);
181    LP_CHECK_MEMBER_OFFSET(struct lp_jit_image, sample_stride,
182                           gallivm->target, image_type,
183                           LP_JIT_IMAGE_SAMPLE_STRIDE);
184    return image_type;
185 }
186 
187 static void
lp_jit_create_types(struct lp_fragment_shader_variant * lp)188 lp_jit_create_types(struct lp_fragment_shader_variant *lp)
189 {
190    struct gallivm_state *gallivm = lp->gallivm;
191    LLVMContextRef lc = gallivm->context;
192    LLVMTypeRef viewport_type, texture_type, sampler_type, image_type;
193    LLVMTypeRef linear_elem_type;
194 
195    /* struct lp_jit_viewport */
196    {
197       LLVMTypeRef elem_types[LP_JIT_VIEWPORT_NUM_FIELDS];
198 
199       elem_types[LP_JIT_VIEWPORT_MIN_DEPTH] =
200       elem_types[LP_JIT_VIEWPORT_MAX_DEPTH] = LLVMFloatTypeInContext(lc);
201 
202       viewport_type = LLVMStructTypeInContext(lc, elem_types,
203                                               ARRAY_SIZE(elem_types), 0);
204 
205       LP_CHECK_MEMBER_OFFSET(struct lp_jit_viewport, min_depth,
206                              gallivm->target, viewport_type,
207                              LP_JIT_VIEWPORT_MIN_DEPTH);
208       LP_CHECK_MEMBER_OFFSET(struct lp_jit_viewport, max_depth,
209                              gallivm->target, viewport_type,
210                              LP_JIT_VIEWPORT_MAX_DEPTH);
211       LP_CHECK_STRUCT_SIZE(struct lp_jit_viewport,
212                            gallivm->target, viewport_type);
213    }
214 
215    texture_type = create_jit_texture_type(gallivm);
216    sampler_type = create_jit_sampler_type(gallivm);
217    image_type = create_jit_image_type(gallivm);
218 
219    /* struct lp_jit_context */
220    {
221       LLVMTypeRef elem_types[LP_JIT_CTX_COUNT];
222       LLVMTypeRef context_type;
223 
224       elem_types[LP_JIT_CTX_CONSTANTS] =
225          LLVMArrayType(LLVMPointerType(LLVMFloatTypeInContext(lc), 0), LP_MAX_TGSI_CONST_BUFFERS);
226       elem_types[LP_JIT_CTX_NUM_CONSTANTS] =
227             LLVMArrayType(LLVMInt32TypeInContext(lc), LP_MAX_TGSI_CONST_BUFFERS);
228       elem_types[LP_JIT_CTX_TEXTURES] = LLVMArrayType(texture_type,
229                                                       PIPE_MAX_SHADER_SAMPLER_VIEWS);
230       elem_types[LP_JIT_CTX_SAMPLERS] = LLVMArrayType(sampler_type,
231                                                       PIPE_MAX_SAMPLERS);
232       elem_types[LP_JIT_CTX_IMAGES] = LLVMArrayType(image_type,
233                                                     PIPE_MAX_SHADER_IMAGES);
234       elem_types[LP_JIT_CTX_ALPHA_REF] = LLVMFloatTypeInContext(lc);
235       elem_types[LP_JIT_CTX_SAMPLE_MASK] =
236       elem_types[LP_JIT_CTX_STENCIL_REF_FRONT] =
237       elem_types[LP_JIT_CTX_STENCIL_REF_BACK] = LLVMInt32TypeInContext(lc);
238       elem_types[LP_JIT_CTX_U8_BLEND_COLOR] = LLVMPointerType(LLVMInt8TypeInContext(lc), 0);
239       elem_types[LP_JIT_CTX_F_BLEND_COLOR] = LLVMPointerType(LLVMFloatTypeInContext(lc), 0);
240       elem_types[LP_JIT_CTX_VIEWPORTS] = LLVMPointerType(viewport_type, 0);
241       elem_types[LP_JIT_CTX_ANISO_FILTER_TABLE] = LLVMPointerType(LLVMFloatTypeInContext(lc), 0);
242       elem_types[LP_JIT_CTX_SSBOS] =
243          LLVMArrayType(LLVMPointerType(LLVMInt32TypeInContext(lc), 0), LP_MAX_TGSI_SHADER_BUFFERS);
244       elem_types[LP_JIT_CTX_NUM_SSBOS] =
245             LLVMArrayType(LLVMInt32TypeInContext(lc), LP_MAX_TGSI_SHADER_BUFFERS);
246       context_type = LLVMStructTypeInContext(lc, elem_types,
247                                              ARRAY_SIZE(elem_types), 0);
248 
249       LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, constants,
250                              gallivm->target, context_type,
251                              LP_JIT_CTX_CONSTANTS);
252       LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, num_constants,
253                              gallivm->target, context_type,
254                              LP_JIT_CTX_NUM_CONSTANTS);
255       LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, textures,
256                              gallivm->target, context_type,
257                              LP_JIT_CTX_TEXTURES);
258       LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, samplers,
259                              gallivm->target, context_type,
260                              LP_JIT_CTX_SAMPLERS);
261       LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, images,
262                              gallivm->target, context_type,
263                              LP_JIT_CTX_IMAGES);
264       LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, alpha_ref_value,
265                              gallivm->target, context_type,
266                              LP_JIT_CTX_ALPHA_REF);
267       LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, stencil_ref_front,
268                              gallivm->target, context_type,
269                              LP_JIT_CTX_STENCIL_REF_FRONT);
270       LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, stencil_ref_back,
271                              gallivm->target, context_type,
272                              LP_JIT_CTX_STENCIL_REF_BACK);
273       LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, u8_blend_color,
274                              gallivm->target, context_type,
275                              LP_JIT_CTX_U8_BLEND_COLOR);
276       LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, f_blend_color,
277                              gallivm->target, context_type,
278                              LP_JIT_CTX_F_BLEND_COLOR);
279       LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, viewports,
280                              gallivm->target, context_type,
281                              LP_JIT_CTX_VIEWPORTS);
282       LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, ssbos,
283                              gallivm->target, context_type,
284                              LP_JIT_CTX_SSBOS);
285       LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, num_ssbos,
286                              gallivm->target, context_type,
287                              LP_JIT_CTX_NUM_SSBOS);
288       LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, sample_mask,
289                              gallivm->target, context_type,
290                              LP_JIT_CTX_SAMPLE_MASK);
291       LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, aniso_filter_table,
292                              gallivm->target, context_type,
293                              LP_JIT_CTX_ANISO_FILTER_TABLE);
294       LP_CHECK_STRUCT_SIZE(struct lp_jit_context,
295                            gallivm->target, context_type);
296 
297       lp->jit_context_ptr_type = LLVMPointerType(context_type, 0);
298    }
299 
300    /* struct lp_jit_thread_data */
301    {
302       LLVMTypeRef elem_types[LP_JIT_THREAD_DATA_COUNT];
303       LLVMTypeRef thread_data_type;
304 
305       elem_types[LP_JIT_THREAD_DATA_CACHE] =
306             LLVMPointerType(lp_build_format_cache_type(gallivm), 0);
307       elem_types[LP_JIT_THREAD_DATA_COUNTER] = LLVMInt64TypeInContext(lc);
308       elem_types[LP_JIT_THREAD_DATA_INVOCATIONS] = LLVMInt64TypeInContext(lc);
309       elem_types[LP_JIT_THREAD_DATA_RASTER_STATE_VIEWPORT_INDEX] =
310       elem_types[LP_JIT_THREAD_DATA_RASTER_STATE_VIEW_INDEX] =
311             LLVMInt32TypeInContext(lc);
312 
313       thread_data_type = LLVMStructTypeInContext(lc, elem_types,
314                                                  ARRAY_SIZE(elem_types), 0);
315 
316       lp->jit_thread_data_ptr_type = LLVMPointerType(thread_data_type, 0);
317    }
318 
319    /*
320     * lp_linear_elem
321     *
322     * XXX: it can be instanced only once due to the use of opaque types, and
323     * the fact that screen->module is also a global.
324     */
325    {
326       LLVMTypeRef ret_type;
327       LLVMTypeRef arg_types[1];
328       LLVMTypeRef func_type;
329 
330       ret_type = LLVMPointerType(LLVMVectorType(LLVMInt8TypeInContext(lc), 16), 0);
331 
332       arg_types[0] = LLVMPointerType(LLVMInt8TypeInContext(lc), 0);
333 
334       /* lp_linear_func */
335       func_type = LLVMFunctionType(ret_type, arg_types, ARRAY_SIZE(arg_types), 0);
336 
337       /*
338        * We actually define lp_linear_elem not as a structure but simply as a
339        * lp_linear_func pointer
340        */
341       linear_elem_type = LLVMPointerType(func_type, 0);
342    }
343 
344    /* struct lp_jit_linear_context */
345    {
346       LLVMTypeRef linear_elem_ptr_type = LLVMPointerType(linear_elem_type, 0);
347       LLVMTypeRef elem_types[LP_JIT_LINEAR_CTX_COUNT];
348       LLVMTypeRef linear_context_type;
349 
350 
351       elem_types[LP_JIT_LINEAR_CTX_CONSTANTS] = LLVMPointerType(LLVMInt8TypeInContext(lc), 0);
352       elem_types[LP_JIT_LINEAR_CTX_TEX] =
353             LLVMArrayType(linear_elem_ptr_type, LP_MAX_LINEAR_TEXTURES);
354       elem_types[LP_JIT_LINEAR_CTX_INPUTS] =
355             LLVMArrayType(linear_elem_ptr_type, LP_MAX_LINEAR_INPUTS);
356       elem_types[LP_JIT_LINEAR_CTX_COLOR0] = LLVMPointerType(LLVMInt8TypeInContext(lc), 0);
357       elem_types[LP_JIT_LINEAR_CTX_BLEND_COLOR] = LLVMInt32TypeInContext(lc);
358       elem_types[LP_JIT_LINEAR_CTX_ALPHA_REF] = LLVMInt8TypeInContext(lc);
359 
360       linear_context_type = LLVMStructTypeInContext(lc, elem_types,
361                                                     ARRAY_SIZE(elem_types), 0);
362 
363       LP_CHECK_MEMBER_OFFSET(struct lp_jit_linear_context, constants,
364                              gallivm->target, linear_context_type,
365                              LP_JIT_LINEAR_CTX_CONSTANTS);
366       LP_CHECK_MEMBER_OFFSET(struct lp_jit_linear_context, tex,
367                              gallivm->target, linear_context_type,
368                              LP_JIT_LINEAR_CTX_TEX);
369       LP_CHECK_MEMBER_OFFSET(struct lp_jit_linear_context, inputs,
370                              gallivm->target, linear_context_type,
371                              LP_JIT_LINEAR_CTX_INPUTS);
372       LP_CHECK_MEMBER_OFFSET(struct lp_jit_linear_context, color0,
373                              gallivm->target, linear_context_type,
374                              LP_JIT_LINEAR_CTX_COLOR0);
375       LP_CHECK_MEMBER_OFFSET(struct lp_jit_linear_context, blend_color,
376                              gallivm->target, linear_context_type,
377                              LP_JIT_LINEAR_CTX_BLEND_COLOR);
378       LP_CHECK_MEMBER_OFFSET(struct lp_jit_linear_context, alpha_ref_value,
379                              gallivm->target, linear_context_type,
380                              LP_JIT_LINEAR_CTX_ALPHA_REF);
381       LP_CHECK_STRUCT_SIZE(struct lp_jit_linear_context,
382                            gallivm->target, linear_context_type);
383 
384       lp->jit_linear_context_ptr_type = LLVMPointerType(linear_context_type, 0);
385    }
386 
387    if (gallivm_debug & GALLIVM_DEBUG_IR) {
388       char *str = LLVMPrintModuleToString(gallivm->module);
389       fprintf(stderr, "%s", str);
390       LLVMDisposeMessage(str);
391    }
392 }
393 
394 
395 void
lp_jit_screen_cleanup(struct llvmpipe_screen * screen)396 lp_jit_screen_cleanup(struct llvmpipe_screen *screen)
397 {
398    /* nothing */
399 }
400 
401 
402 boolean
lp_jit_screen_init(struct llvmpipe_screen * screen)403 lp_jit_screen_init(struct llvmpipe_screen *screen)
404 {
405    return lp_build_init();
406 }
407 
408 
409 void
lp_jit_init_types(struct lp_fragment_shader_variant * lp)410 lp_jit_init_types(struct lp_fragment_shader_variant *lp)
411 {
412    if (!lp->jit_context_ptr_type)
413       lp_jit_create_types(lp);
414 }
415 
416 static void
lp_jit_create_cs_types(struct lp_compute_shader_variant * lp)417 lp_jit_create_cs_types(struct lp_compute_shader_variant *lp)
418 {
419    struct gallivm_state *gallivm = lp->gallivm;
420    LLVMContextRef lc = gallivm->context;
421    LLVMTypeRef texture_type, sampler_type, image_type;
422 
423    texture_type = create_jit_texture_type(gallivm);
424    sampler_type = create_jit_sampler_type(gallivm);
425    image_type = create_jit_image_type(gallivm);
426 
427    /* struct lp_jit_cs_thread_data */
428    {
429       LLVMTypeRef elem_types[LP_JIT_CS_THREAD_DATA_COUNT];
430       LLVMTypeRef thread_data_type;
431 
432       elem_types[LP_JIT_CS_THREAD_DATA_CACHE] =
433             LLVMPointerType(lp_build_format_cache_type(gallivm), 0);
434 
435       elem_types[LP_JIT_CS_THREAD_DATA_SHARED] = LLVMPointerType(LLVMInt32TypeInContext(lc), 0);
436       thread_data_type = LLVMStructTypeInContext(lc, elem_types,
437                                                  ARRAY_SIZE(elem_types), 0);
438 
439       lp->jit_cs_thread_data_ptr_type = LLVMPointerType(thread_data_type, 0);
440    }
441 
442    /* struct lp_jit_cs_context */
443    {
444       LLVMTypeRef elem_types[LP_JIT_CS_CTX_COUNT];
445       LLVMTypeRef cs_context_type;
446 
447       elem_types[LP_JIT_CS_CTX_CONSTANTS] =
448          LLVMArrayType(LLVMPointerType(LLVMFloatTypeInContext(lc), 0), LP_MAX_TGSI_CONST_BUFFERS);
449       elem_types[LP_JIT_CS_CTX_NUM_CONSTANTS] =
450             LLVMArrayType(LLVMInt32TypeInContext(lc), LP_MAX_TGSI_CONST_BUFFERS);
451       elem_types[LP_JIT_CS_CTX_TEXTURES] = LLVMArrayType(texture_type,
452                                                       PIPE_MAX_SHADER_SAMPLER_VIEWS);
453       elem_types[LP_JIT_CS_CTX_SAMPLERS] = LLVMArrayType(sampler_type,
454                                                       PIPE_MAX_SAMPLERS);
455       elem_types[LP_JIT_CS_CTX_IMAGES] = LLVMArrayType(image_type,
456                                                        PIPE_MAX_SHADER_IMAGES);
457       elem_types[LP_JIT_CS_CTX_SSBOS] =
458          LLVMArrayType(LLVMPointerType(LLVMInt32TypeInContext(lc), 0), LP_MAX_TGSI_SHADER_BUFFERS);
459       elem_types[LP_JIT_CS_CTX_NUM_SSBOS] =
460             LLVMArrayType(LLVMInt32TypeInContext(lc), LP_MAX_TGSI_SHADER_BUFFERS);
461 
462       elem_types[LP_JIT_CS_CTX_SHARED_SIZE] = LLVMInt32TypeInContext(lc);
463 
464       elem_types[LP_JIT_CS_CTX_KERNEL_ARGS] = LLVMPointerType(LLVMInt8TypeInContext(lc), 0);
465 
466       elem_types[LP_JIT_CS_CTX_ANISO_FILTER_TABLE] = LLVMPointerType(LLVMFloatTypeInContext(lc), 0);
467 
468       cs_context_type = LLVMStructTypeInContext(lc, elem_types,
469                                              ARRAY_SIZE(elem_types), 0);
470 
471       LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context, constants,
472                              gallivm->target, cs_context_type,
473                              LP_JIT_CS_CTX_CONSTANTS);
474       LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context, num_constants,
475                              gallivm->target, cs_context_type,
476                              LP_JIT_CS_CTX_NUM_CONSTANTS);
477       LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context, textures,
478                              gallivm->target, cs_context_type,
479                              LP_JIT_CS_CTX_TEXTURES);
480       LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context, samplers,
481                              gallivm->target, cs_context_type,
482                              LP_JIT_CS_CTX_SAMPLERS);
483       LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context, images,
484                              gallivm->target, cs_context_type,
485                              LP_JIT_CS_CTX_IMAGES);
486       LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context, ssbos,
487                              gallivm->target, cs_context_type,
488                              LP_JIT_CS_CTX_SSBOS);
489       LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context, num_ssbos,
490                              gallivm->target, cs_context_type,
491                              LP_JIT_CS_CTX_NUM_SSBOS);
492       LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context, shared_size,
493                              gallivm->target, cs_context_type,
494                              LP_JIT_CS_CTX_SHARED_SIZE);
495       LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context, kernel_args,
496                              gallivm->target, cs_context_type,
497                              LP_JIT_CS_CTX_KERNEL_ARGS);
498       LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context, aniso_filter_table,
499                              gallivm->target, cs_context_type,
500                              LP_JIT_CS_CTX_ANISO_FILTER_TABLE);
501       LP_CHECK_STRUCT_SIZE(struct lp_jit_cs_context,
502                            gallivm->target, cs_context_type);
503 
504       lp->jit_cs_context_ptr_type = LLVMPointerType(cs_context_type, 0);
505    }
506 
507    if (gallivm_debug & GALLIVM_DEBUG_IR) {
508       char *str = LLVMPrintModuleToString(gallivm->module);
509       fprintf(stderr, "%s", str);
510       LLVMDisposeMessage(str);
511    }
512 }
513 
514 void
lp_jit_init_cs_types(struct lp_compute_shader_variant * lp)515 lp_jit_init_cs_types(struct lp_compute_shader_variant *lp)
516 {
517    if (!lp->jit_cs_context_ptr_type)
518       lp_jit_create_cs_types(lp);
519 }
520