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
, first_level
,
75 gallivm
->target
, texture_type
,
76 LP_JIT_TEXTURE_FIRST_LEVEL
);
77 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, last_level
,
78 gallivm
->target
, texture_type
,
79 LP_JIT_TEXTURE_LAST_LEVEL
);
80 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, base
,
81 gallivm
->target
, texture_type
,
83 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, row_stride
,
84 gallivm
->target
, texture_type
,
85 LP_JIT_TEXTURE_ROW_STRIDE
);
86 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture
, img_stride
,
87 gallivm
->target
, texture_type
,
88 LP_JIT_TEXTURE_IMG_STRIDE
);
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 lp_jit_create_types(struct lp_fragment_shader_variant
*lp
)
132 struct gallivm_state
*gallivm
= lp
->gallivm
;
133 LLVMContextRef lc
= gallivm
->context
;
134 LLVMTypeRef viewport_type
, texture_type
, sampler_type
;
136 /* struct lp_jit_viewport */
138 LLVMTypeRef elem_types
[LP_JIT_VIEWPORT_NUM_FIELDS
];
140 elem_types
[LP_JIT_VIEWPORT_MIN_DEPTH
] =
141 elem_types
[LP_JIT_VIEWPORT_MAX_DEPTH
] = LLVMFloatTypeInContext(lc
);
143 viewport_type
= LLVMStructTypeInContext(lc
, elem_types
,
144 ARRAY_SIZE(elem_types
), 0);
146 LP_CHECK_MEMBER_OFFSET(struct lp_jit_viewport
, min_depth
,
147 gallivm
->target
, viewport_type
,
148 LP_JIT_VIEWPORT_MIN_DEPTH
);
149 LP_CHECK_MEMBER_OFFSET(struct lp_jit_viewport
, max_depth
,
150 gallivm
->target
, viewport_type
,
151 LP_JIT_VIEWPORT_MAX_DEPTH
);
152 LP_CHECK_STRUCT_SIZE(struct lp_jit_viewport
,
153 gallivm
->target
, viewport_type
);
156 texture_type
= create_jit_texture_type(gallivm
);
157 sampler_type
= create_jit_sampler_type(gallivm
);
159 /* struct lp_jit_context */
161 LLVMTypeRef elem_types
[LP_JIT_CTX_COUNT
];
162 LLVMTypeRef context_type
;
164 elem_types
[LP_JIT_CTX_CONSTANTS
] =
165 LLVMArrayType(LLVMPointerType(LLVMFloatTypeInContext(lc
), 0), LP_MAX_TGSI_CONST_BUFFERS
);
166 elem_types
[LP_JIT_CTX_NUM_CONSTANTS
] =
167 LLVMArrayType(LLVMInt32TypeInContext(lc
), LP_MAX_TGSI_CONST_BUFFERS
);
168 elem_types
[LP_JIT_CTX_ALPHA_REF
] = LLVMFloatTypeInContext(lc
);
169 elem_types
[LP_JIT_CTX_STENCIL_REF_FRONT
] =
170 elem_types
[LP_JIT_CTX_STENCIL_REF_BACK
] = LLVMInt32TypeInContext(lc
);
171 elem_types
[LP_JIT_CTX_U8_BLEND_COLOR
] = LLVMPointerType(LLVMInt8TypeInContext(lc
), 0);
172 elem_types
[LP_JIT_CTX_F_BLEND_COLOR
] = LLVMPointerType(LLVMFloatTypeInContext(lc
), 0);
173 elem_types
[LP_JIT_CTX_VIEWPORTS
] = LLVMPointerType(viewport_type
, 0);
174 elem_types
[LP_JIT_CTX_TEXTURES
] = LLVMArrayType(texture_type
,
175 PIPE_MAX_SHADER_SAMPLER_VIEWS
);
176 elem_types
[LP_JIT_CTX_SAMPLERS
] = LLVMArrayType(sampler_type
,
178 elem_types
[LP_JIT_CTX_SSBOS
] =
179 LLVMArrayType(LLVMPointerType(LLVMInt32TypeInContext(lc
), 0), LP_MAX_TGSI_SHADER_BUFFERS
);
180 elem_types
[LP_JIT_CTX_NUM_SSBOS
] =
181 LLVMArrayType(LLVMInt32TypeInContext(lc
), LP_MAX_TGSI_SHADER_BUFFERS
);
182 context_type
= LLVMStructTypeInContext(lc
, elem_types
,
183 ARRAY_SIZE(elem_types
), 0);
185 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, constants
,
186 gallivm
->target
, context_type
,
187 LP_JIT_CTX_CONSTANTS
);
188 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, num_constants
,
189 gallivm
->target
, context_type
,
190 LP_JIT_CTX_NUM_CONSTANTS
);
191 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, alpha_ref_value
,
192 gallivm
->target
, context_type
,
193 LP_JIT_CTX_ALPHA_REF
);
194 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, stencil_ref_front
,
195 gallivm
->target
, context_type
,
196 LP_JIT_CTX_STENCIL_REF_FRONT
);
197 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, stencil_ref_back
,
198 gallivm
->target
, context_type
,
199 LP_JIT_CTX_STENCIL_REF_BACK
);
200 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, u8_blend_color
,
201 gallivm
->target
, context_type
,
202 LP_JIT_CTX_U8_BLEND_COLOR
);
203 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, f_blend_color
,
204 gallivm
->target
, context_type
,
205 LP_JIT_CTX_F_BLEND_COLOR
);
206 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, viewports
,
207 gallivm
->target
, context_type
,
208 LP_JIT_CTX_VIEWPORTS
);
209 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, textures
,
210 gallivm
->target
, context_type
,
211 LP_JIT_CTX_TEXTURES
);
212 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, samplers
,
213 gallivm
->target
, context_type
,
214 LP_JIT_CTX_SAMPLERS
);
215 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, ssbos
,
216 gallivm
->target
, context_type
,
218 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context
, num_ssbos
,
219 gallivm
->target
, context_type
,
220 LP_JIT_CTX_NUM_SSBOS
);
221 LP_CHECK_STRUCT_SIZE(struct lp_jit_context
,
222 gallivm
->target
, context_type
);
224 lp
->jit_context_ptr_type
= LLVMPointerType(context_type
, 0);
227 /* struct lp_jit_thread_data */
229 LLVMTypeRef elem_types
[LP_JIT_THREAD_DATA_COUNT
];
230 LLVMTypeRef thread_data_type
;
232 elem_types
[LP_JIT_THREAD_DATA_CACHE
] =
233 LLVMPointerType(lp_build_format_cache_type(gallivm
), 0);
234 elem_types
[LP_JIT_THREAD_DATA_COUNTER
] = LLVMInt64TypeInContext(lc
);
235 elem_types
[LP_JIT_THREAD_DATA_INVOCATIONS
] = LLVMInt64TypeInContext(lc
);
236 elem_types
[LP_JIT_THREAD_DATA_RASTER_STATE_VIEWPORT_INDEX
] =
237 LLVMInt32TypeInContext(lc
);
239 thread_data_type
= LLVMStructTypeInContext(lc
, elem_types
,
240 ARRAY_SIZE(elem_types
), 0);
242 lp
->jit_thread_data_ptr_type
= LLVMPointerType(thread_data_type
, 0);
245 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
246 #if HAVE_LLVM >= 0x304
247 char *str
= LLVMPrintModuleToString(gallivm
->module
);
248 fprintf(stderr
, "%s", str
);
249 LLVMDisposeMessage(str
);
251 LLVMDumpModule(gallivm
->module
);
258 lp_jit_screen_cleanup(struct llvmpipe_screen
*screen
)
265 lp_jit_screen_init(struct llvmpipe_screen
*screen
)
267 return lp_build_init();
272 lp_jit_init_types(struct lp_fragment_shader_variant
*lp
)
274 if (!lp
->jit_context_ptr_type
)
275 lp_jit_create_types(lp
);