1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
32 * @author Jose Fonseca <jfonseca@vmware.com>
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"
44 create_jit_texture_type(struct gallivm_state
*gallivm
)
46 LLVMContextRef lc
= gallivm
->context
;
47 LLVMTypeRef texture_type
;
48 LLVMTypeRef elem_types
[LP_JIT_TEXTURE_NUM_FIELDS
];
50 /* struct lp_jit_texture */
51 elem_types
[LP_JIT_TEXTURE_WIDTH
] =
52 elem_types
[LP_JIT_TEXTURE_HEIGHT
] =
53 elem_types
[LP_JIT_TEXTURE_DEPTH
] =
54 elem_types
[LP_JIT_TEXTURE_FIRST_LEVEL
] =
55 elem_types
[LP_JIT_TEXTURE_LAST_LEVEL
] = LLVMInt32TypeInContext(lc
);
56 elem_types
[LP_JIT_TEXTURE_BASE
] = LLVMPointerType(LLVMInt8TypeInContext(lc
), 0);
57 elem_types
[LP_JIT_TEXTURE_ROW_STRIDE
] =
58 elem_types
[LP_JIT_TEXTURE_IMG_STRIDE
] =
59 elem_types
[LP_JIT_TEXTURE_MIP_OFFSETS
] =
60 LLVMArrayType(LLVMInt32TypeInContext(lc
), LP_MAX_TEXTURE_LEVELS
);
62 texture_type
= LLVMStructTypeInContext(lc
, elem_types
,
63 ARRAY_SIZE(elem_types
), 0);
65 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, width
,
66 gallivm
->target
, texture_type
,
67 LP_JIT_TEXTURE_WIDTH
);
68 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, height
,
69 gallivm
->target
, texture_type
,
70 LP_JIT_TEXTURE_HEIGHT
);
71 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, depth
,
72 gallivm
->target
, texture_type
,
73 LP_JIT_TEXTURE_DEPTH
);
74 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, base
,
75 gallivm
->target
, texture_type
,
77 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, row_stride
,
78 gallivm
->target
, texture_type
,
79 LP_JIT_TEXTURE_ROW_STRIDE
);
80 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, img_stride
,
81 gallivm
->target
, texture_type
,
82 LP_JIT_TEXTURE_IMG_STRIDE
);
83 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, first_level
,
84 gallivm
->target
, texture_type
,
85 LP_JIT_TEXTURE_FIRST_LEVEL
);
86 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, last_level
,
87 gallivm
->target
, texture_type
,
88 LP_JIT_TEXTURE_LAST_LEVEL
);
89 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, mip_offsets
,
90 gallivm
->target
, texture_type
,
91 LP_JIT_TEXTURE_MIP_OFFSETS
);
92 LP_CHECK_STRUCT_SIZE(struct lp_jit_texture
,
93 gallivm
->target
, texture_type
);
98 create_jit_sampler_type(struct gallivm_state
*gallivm
)
100 LLVMContextRef lc
= gallivm
->context
;
101 LLVMTypeRef sampler_type
;
102 LLVMTypeRef elem_types
[LP_JIT_SAMPLER_NUM_FIELDS
];
103 elem_types
[LP_JIT_SAMPLER_MIN_LOD
] =
104 elem_types
[LP_JIT_SAMPLER_MAX_LOD
] =
105 elem_types
[LP_JIT_SAMPLER_LOD_BIAS
] = LLVMFloatTypeInContext(lc
);
106 elem_types
[LP_JIT_SAMPLER_BORDER_COLOR
] =
107 LLVMArrayType(LLVMFloatTypeInContext(lc
), 4);
109 sampler_type
= LLVMStructTypeInContext(lc
, elem_types
,
110 ARRAY_SIZE(elem_types
), 0);
112 LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler
, min_lod
,
113 gallivm
->target
, sampler_type
,
114 LP_JIT_SAMPLER_MIN_LOD
);
115 LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler
, max_lod
,
116 gallivm
->target
, sampler_type
,
117 LP_JIT_SAMPLER_MAX_LOD
);
118 LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler
, lod_bias
,
119 gallivm
->target
, sampler_type
,
120 LP_JIT_SAMPLER_LOD_BIAS
);
121 LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler
, border_color
,
122 gallivm
->target
, sampler_type
,
123 LP_JIT_SAMPLER_BORDER_COLOR
);
124 LP_CHECK_STRUCT_SIZE(struct lp_jit_sampler
,
125 gallivm
->target
, sampler_type
);
130 create_jit_image_type(struct gallivm_state
*gallivm
)
132 LLVMContextRef lc
= gallivm
->context
;
133 LLVMTypeRef image_type
;
134 LLVMTypeRef elem_types
[LP_JIT_IMAGE_NUM_FIELDS
];
135 elem_types
[LP_JIT_IMAGE_WIDTH
] =
136 elem_types
[LP_JIT_IMAGE_HEIGHT
] =
137 elem_types
[LP_JIT_IMAGE_DEPTH
] = LLVMInt32TypeInContext(lc
);
138 elem_types
[LP_JIT_IMAGE_BASE
] = LLVMPointerType(LLVMInt8TypeInContext(lc
), 0);
139 elem_types
[LP_JIT_IMAGE_ROW_STRIDE
] =
140 elem_types
[LP_JIT_IMAGE_IMG_STRIDE
] = LLVMInt32TypeInContext(lc
);
142 image_type
= LLVMStructTypeInContext(lc
, elem_types
,
143 ARRAY_SIZE(elem_types
), 0);
144 LP_CHECK_MEMBER_OFFSET(struct lp_jit_image
, width
,
145 gallivm
->target
, image_type
,
147 LP_CHECK_MEMBER_OFFSET(struct lp_jit_image
, height
,
148 gallivm
->target
, image_type
,
149 LP_JIT_IMAGE_HEIGHT
);
150 LP_CHECK_MEMBER_OFFSET(struct lp_jit_image
, depth
,
151 gallivm
->target
, image_type
,
153 LP_CHECK_MEMBER_OFFSET(struct lp_jit_image
, base
,
154 gallivm
->target
, image_type
,
156 LP_CHECK_MEMBER_OFFSET(struct lp_jit_image
, row_stride
,
157 gallivm
->target
, image_type
,
158 LP_JIT_IMAGE_ROW_STRIDE
);
159 LP_CHECK_MEMBER_OFFSET(struct lp_jit_image
, img_stride
,
160 gallivm
->target
, image_type
,
161 LP_JIT_IMAGE_IMG_STRIDE
);
166 lp_jit_create_types(struct lp_fragment_shader_variant
*lp
)
168 struct gallivm_state
*gallivm
= lp
->gallivm
;
169 LLVMContextRef lc
= gallivm
->context
;
170 LLVMTypeRef viewport_type
, texture_type
, sampler_type
, image_type
;
172 /* struct lp_jit_viewport */
174 LLVMTypeRef elem_types
[LP_JIT_VIEWPORT_NUM_FIELDS
];
176 elem_types
[LP_JIT_VIEWPORT_MIN_DEPTH
] =
177 elem_types
[LP_JIT_VIEWPORT_MAX_DEPTH
] = LLVMFloatTypeInContext(lc
);
179 viewport_type
= LLVMStructTypeInContext(lc
, elem_types
,
180 ARRAY_SIZE(elem_types
), 0);
182 LP_CHECK_MEMBER_OFFSET(struct lp_jit_viewport
, min_depth
,
183 gallivm
->target
, viewport_type
,
184 LP_JIT_VIEWPORT_MIN_DEPTH
);
185 LP_CHECK_MEMBER_OFFSET(struct lp_jit_viewport
, max_depth
,
186 gallivm
->target
, viewport_type
,
187 LP_JIT_VIEWPORT_MAX_DEPTH
);
188 LP_CHECK_STRUCT_SIZE(struct lp_jit_viewport
,
189 gallivm
->target
, viewport_type
);
192 texture_type
= create_jit_texture_type(gallivm
);
193 sampler_type
= create_jit_sampler_type(gallivm
);
194 image_type
= create_jit_image_type(gallivm
);
196 /* struct lp_jit_context */
198 LLVMTypeRef elem_types
[LP_JIT_CTX_COUNT
];
199 LLVMTypeRef context_type
;
201 elem_types
[LP_JIT_CTX_CONSTANTS
] =
202 LLVMArrayType(LLVMPointerType(LLVMFloatTypeInContext(lc
), 0), LP_MAX_TGSI_CONST_BUFFERS
);
203 elem_types
[LP_JIT_CTX_NUM_CONSTANTS
] =
204 LLVMArrayType(LLVMInt32TypeInContext(lc
), LP_MAX_TGSI_CONST_BUFFERS
);
205 elem_types
[LP_JIT_CTX_TEXTURES
] = LLVMArrayType(texture_type
,
206 PIPE_MAX_SHADER_SAMPLER_VIEWS
);
207 elem_types
[LP_JIT_CTX_SAMPLERS
] = LLVMArrayType(sampler_type
,
209 elem_types
[LP_JIT_CTX_IMAGES
] = LLVMArrayType(image_type
,
210 PIPE_MAX_SHADER_IMAGES
);
211 elem_types
[LP_JIT_CTX_ALPHA_REF
] = LLVMFloatTypeInContext(lc
);
212 elem_types
[LP_JIT_CTX_STENCIL_REF_FRONT
] =
213 elem_types
[LP_JIT_CTX_STENCIL_REF_BACK
] = LLVMInt32TypeInContext(lc
);
214 elem_types
[LP_JIT_CTX_U8_BLEND_COLOR
] = LLVMPointerType(LLVMInt8TypeInContext(lc
), 0);
215 elem_types
[LP_JIT_CTX_F_BLEND_COLOR
] = LLVMPointerType(LLVMFloatTypeInContext(lc
), 0);
216 elem_types
[LP_JIT_CTX_VIEWPORTS
] = LLVMPointerType(viewport_type
, 0);
217 elem_types
[LP_JIT_CTX_SSBOS
] =
218 LLVMArrayType(LLVMPointerType(LLVMInt32TypeInContext(lc
), 0), LP_MAX_TGSI_SHADER_BUFFERS
);
219 elem_types
[LP_JIT_CTX_NUM_SSBOS
] =
220 LLVMArrayType(LLVMInt32TypeInContext(lc
), LP_MAX_TGSI_SHADER_BUFFERS
);
221 context_type
= LLVMStructTypeInContext(lc
, elem_types
,
222 ARRAY_SIZE(elem_types
), 0);
224 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, constants
,
225 gallivm
->target
, context_type
,
226 LP_JIT_CTX_CONSTANTS
);
227 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, num_constants
,
228 gallivm
->target
, context_type
,
229 LP_JIT_CTX_NUM_CONSTANTS
);
230 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, textures
,
231 gallivm
->target
, context_type
,
232 LP_JIT_CTX_TEXTURES
);
233 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, samplers
,
234 gallivm
->target
, context_type
,
235 LP_JIT_CTX_SAMPLERS
);
236 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, images
,
237 gallivm
->target
, context_type
,
239 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, alpha_ref_value
,
240 gallivm
->target
, context_type
,
241 LP_JIT_CTX_ALPHA_REF
);
242 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, stencil_ref_front
,
243 gallivm
->target
, context_type
,
244 LP_JIT_CTX_STENCIL_REF_FRONT
);
245 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, stencil_ref_back
,
246 gallivm
->target
, context_type
,
247 LP_JIT_CTX_STENCIL_REF_BACK
);
248 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, u8_blend_color
,
249 gallivm
->target
, context_type
,
250 LP_JIT_CTX_U8_BLEND_COLOR
);
251 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, f_blend_color
,
252 gallivm
->target
, context_type
,
253 LP_JIT_CTX_F_BLEND_COLOR
);
254 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, viewports
,
255 gallivm
->target
, context_type
,
256 LP_JIT_CTX_VIEWPORTS
);
257 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, ssbos
,
258 gallivm
->target
, context_type
,
260 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, num_ssbos
,
261 gallivm
->target
, context_type
,
262 LP_JIT_CTX_NUM_SSBOS
);
263 LP_CHECK_STRUCT_SIZE(struct lp_jit_context
,
264 gallivm
->target
, context_type
);
266 lp
->jit_context_ptr_type
= LLVMPointerType(context_type
, 0);
269 /* struct lp_jit_thread_data */
271 LLVMTypeRef elem_types
[LP_JIT_THREAD_DATA_COUNT
];
272 LLVMTypeRef thread_data_type
;
274 elem_types
[LP_JIT_THREAD_DATA_CACHE
] =
275 LLVMPointerType(lp_build_format_cache_type(gallivm
), 0);
276 elem_types
[LP_JIT_THREAD_DATA_COUNTER
] = LLVMInt64TypeInContext(lc
);
277 elem_types
[LP_JIT_THREAD_DATA_INVOCATIONS
] = LLVMInt64TypeInContext(lc
);
278 elem_types
[LP_JIT_THREAD_DATA_RASTER_STATE_VIEWPORT_INDEX
] =
279 LLVMInt32TypeInContext(lc
);
281 thread_data_type
= LLVMStructTypeInContext(lc
, elem_types
,
282 ARRAY_SIZE(elem_types
), 0);
284 lp
->jit_thread_data_ptr_type
= LLVMPointerType(thread_data_type
, 0);
287 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
288 #if HAVE_LLVM >= 0x304
289 char *str
= LLVMPrintModuleToString(gallivm
->module
);
290 fprintf(stderr
, "%s", str
);
291 LLVMDisposeMessage(str
);
293 LLVMDumpModule(gallivm
->module
);
300 lp_jit_screen_cleanup(struct llvmpipe_screen
*screen
)
307 lp_jit_screen_init(struct llvmpipe_screen
*screen
)
309 return lp_build_init();
314 lp_jit_init_types(struct lp_fragment_shader_variant
*lp
)
316 if (!lp
->jit_context_ptr_type
)
317 lp_jit_create_types(lp
);
321 lp_jit_create_cs_types(struct lp_compute_shader_variant
*lp
)
323 struct gallivm_state
*gallivm
= lp
->gallivm
;
324 LLVMContextRef lc
= gallivm
->context
;
325 LLVMTypeRef texture_type
, sampler_type
, image_type
;
327 texture_type
= create_jit_texture_type(gallivm
);
328 sampler_type
= create_jit_sampler_type(gallivm
);
329 image_type
= create_jit_image_type(gallivm
);
331 /* struct lp_jit_cs_thread_data */
333 LLVMTypeRef elem_types
[LP_JIT_CS_THREAD_DATA_COUNT
];
334 LLVMTypeRef thread_data_type
;
336 elem_types
[LP_JIT_CS_THREAD_DATA_CACHE
] =
337 LLVMPointerType(lp_build_format_cache_type(gallivm
), 0);
339 elem_types
[LP_JIT_CS_THREAD_DATA_SHARED
] = LLVMPointerType(LLVMInt32TypeInContext(lc
), 0);
340 thread_data_type
= LLVMStructTypeInContext(lc
, elem_types
,
341 ARRAY_SIZE(elem_types
), 0);
343 lp
->jit_cs_thread_data_ptr_type
= LLVMPointerType(thread_data_type
, 0);
346 /* struct lp_jit_cs_context */
348 LLVMTypeRef elem_types
[LP_JIT_CS_CTX_COUNT
];
349 LLVMTypeRef cs_context_type
;
351 elem_types
[LP_JIT_CS_CTX_CONSTANTS
] =
352 LLVMArrayType(LLVMPointerType(LLVMFloatTypeInContext(lc
), 0), LP_MAX_TGSI_CONST_BUFFERS
);
353 elem_types
[LP_JIT_CS_CTX_NUM_CONSTANTS
] =
354 LLVMArrayType(LLVMInt32TypeInContext(lc
), LP_MAX_TGSI_CONST_BUFFERS
);
355 elem_types
[LP_JIT_CS_CTX_TEXTURES
] = LLVMArrayType(texture_type
,
356 PIPE_MAX_SHADER_SAMPLER_VIEWS
);
357 elem_types
[LP_JIT_CS_CTX_SAMPLERS
] = LLVMArrayType(sampler_type
,
359 elem_types
[LP_JIT_CS_CTX_IMAGES
] = LLVMArrayType(image_type
,
360 PIPE_MAX_SHADER_IMAGES
);
361 elem_types
[LP_JIT_CS_CTX_SSBOS
] =
362 LLVMArrayType(LLVMPointerType(LLVMInt32TypeInContext(lc
), 0), LP_MAX_TGSI_SHADER_BUFFERS
);
363 elem_types
[LP_JIT_CS_CTX_NUM_SSBOS
] =
364 LLVMArrayType(LLVMInt32TypeInContext(lc
), LP_MAX_TGSI_SHADER_BUFFERS
);
366 elem_types
[LP_JIT_CS_CTX_SHARED_SIZE
] = LLVMInt32TypeInContext(lc
);
368 cs_context_type
= LLVMStructTypeInContext(lc
, elem_types
,
369 ARRAY_SIZE(elem_types
), 0);
371 LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context
, constants
,
372 gallivm
->target
, cs_context_type
,
373 LP_JIT_CS_CTX_CONSTANTS
);
374 LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context
, num_constants
,
375 gallivm
->target
, cs_context_type
,
376 LP_JIT_CS_CTX_NUM_CONSTANTS
);
377 LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context
, textures
,
378 gallivm
->target
, cs_context_type
,
379 LP_JIT_CS_CTX_TEXTURES
);
380 LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context
, samplers
,
381 gallivm
->target
, cs_context_type
,
382 LP_JIT_CS_CTX_SAMPLERS
);
383 LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context
, images
,
384 gallivm
->target
, cs_context_type
,
385 LP_JIT_CS_CTX_IMAGES
);
386 LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context
, ssbos
,
387 gallivm
->target
, cs_context_type
,
388 LP_JIT_CS_CTX_SSBOS
);
389 LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context
, num_ssbos
,
390 gallivm
->target
, cs_context_type
,
391 LP_JIT_CS_CTX_NUM_SSBOS
);
392 LP_CHECK_MEMBER_OFFSET(struct lp_jit_cs_context
, shared_size
,
393 gallivm
->target
, cs_context_type
,
394 LP_JIT_CS_CTX_SHARED_SIZE
);
395 LP_CHECK_STRUCT_SIZE(struct lp_jit_cs_context
,
396 gallivm
->target
, cs_context_type
);
398 lp
->jit_cs_context_ptr_type
= LLVMPointerType(cs_context_type
, 0);
401 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
402 #if HAVE_LLVM >= 0x304
403 char *str
= LLVMPrintModuleToString(gallivm
->module
);
404 fprintf(stderr
, "%s", str
);
405 LLVMDisposeMessage(str
);
407 LLVMDumpModule(gallivm
->module
);
413 lp_jit_init_cs_types(struct lp_compute_shader_variant
*lp
)
415 if (!lp
->jit_cs_context_ptr_type
)
416 lp_jit_create_cs_types(lp
);