llvmpipe: clamp fragment shader depth write to the current viewport depth range.
[mesa.git] / src / gallium / drivers / llvmpipe / lp_jit.c
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 "lp_context.h"
40 #include "lp_jit.h"
41
42
43 static void
44 lp_jit_create_types(struct lp_fragment_shader_variant *lp)
45 {
46 struct gallivm_state *gallivm = lp->gallivm;
47 LLVMContextRef lc = gallivm->context;
48 LLVMTypeRef viewport_type, texture_type, sampler_type;
49
50 /* struct lp_jit_viewport */
51 {
52 LLVMTypeRef elem_types[LP_JIT_VIEWPORT_NUM_FIELDS];
53
54 elem_types[LP_JIT_VIEWPORT_MIN_DEPTH] =
55 elem_types[LP_JIT_VIEWPORT_MAX_DEPTH] = LLVMFloatTypeInContext(lc);
56
57 viewport_type = LLVMStructTypeInContext(lc, elem_types,
58 Elements(elem_types), 0);
59
60 #if HAVE_LLVM < 0x0300
61 LLVMAddTypeName(gallivm->module, "viewport", viewport_type);
62
63 LLVMInvalidateStructLayout(gallivm->target, viewport_type);
64 #endif
65
66 LP_CHECK_MEMBER_OFFSET(struct lp_jit_viewport, min_depth,
67 gallivm->target, viewport_type,
68 LP_JIT_VIEWPORT_MIN_DEPTH);
69 LP_CHECK_MEMBER_OFFSET(struct lp_jit_viewport, max_depth,
70 gallivm->target, viewport_type,
71 LP_JIT_VIEWPORT_MAX_DEPTH);
72 LP_CHECK_STRUCT_SIZE(struct lp_jit_viewport,
73 gallivm->target, viewport_type);
74 }
75
76 /* struct lp_jit_texture */
77 {
78 LLVMTypeRef elem_types[LP_JIT_TEXTURE_NUM_FIELDS];
79
80 elem_types[LP_JIT_TEXTURE_WIDTH] =
81 elem_types[LP_JIT_TEXTURE_HEIGHT] =
82 elem_types[LP_JIT_TEXTURE_DEPTH] =
83 elem_types[LP_JIT_TEXTURE_FIRST_LEVEL] =
84 elem_types[LP_JIT_TEXTURE_LAST_LEVEL] = LLVMInt32TypeInContext(lc);
85 elem_types[LP_JIT_TEXTURE_BASE] = LLVMPointerType(LLVMInt8TypeInContext(lc), 0);
86 elem_types[LP_JIT_TEXTURE_ROW_STRIDE] =
87 elem_types[LP_JIT_TEXTURE_IMG_STRIDE] =
88 elem_types[LP_JIT_TEXTURE_MIP_OFFSETS] =
89 LLVMArrayType(LLVMInt32TypeInContext(lc), LP_MAX_TEXTURE_LEVELS);
90
91 texture_type = LLVMStructTypeInContext(lc, elem_types,
92 Elements(elem_types), 0);
93 #if HAVE_LLVM < 0x0300
94 LLVMAddTypeName(gallivm->module, "texture", texture_type);
95
96 LLVMInvalidateStructLayout(gallivm->target, texture_type);
97 #endif
98
99 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, width,
100 gallivm->target, texture_type,
101 LP_JIT_TEXTURE_WIDTH);
102 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, height,
103 gallivm->target, texture_type,
104 LP_JIT_TEXTURE_HEIGHT);
105 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, depth,
106 gallivm->target, texture_type,
107 LP_JIT_TEXTURE_DEPTH);
108 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, first_level,
109 gallivm->target, texture_type,
110 LP_JIT_TEXTURE_FIRST_LEVEL);
111 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, last_level,
112 gallivm->target, texture_type,
113 LP_JIT_TEXTURE_LAST_LEVEL);
114 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, base,
115 gallivm->target, texture_type,
116 LP_JIT_TEXTURE_BASE);
117 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, row_stride,
118 gallivm->target, texture_type,
119 LP_JIT_TEXTURE_ROW_STRIDE);
120 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, img_stride,
121 gallivm->target, texture_type,
122 LP_JIT_TEXTURE_IMG_STRIDE);
123 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, mip_offsets,
124 gallivm->target, texture_type,
125 LP_JIT_TEXTURE_MIP_OFFSETS);
126 LP_CHECK_STRUCT_SIZE(struct lp_jit_texture,
127 gallivm->target, texture_type);
128 }
129
130 /* struct lp_jit_sampler */
131 {
132 LLVMTypeRef elem_types[LP_JIT_SAMPLER_NUM_FIELDS];
133 elem_types[LP_JIT_SAMPLER_MIN_LOD] =
134 elem_types[LP_JIT_SAMPLER_MAX_LOD] =
135 elem_types[LP_JIT_SAMPLER_LOD_BIAS] = LLVMFloatTypeInContext(lc);
136 elem_types[LP_JIT_SAMPLER_BORDER_COLOR] =
137 LLVMArrayType(LLVMFloatTypeInContext(lc), 4);
138
139 sampler_type = LLVMStructTypeInContext(lc, elem_types,
140 Elements(elem_types), 0);
141 #if HAVE_LLVM < 0x0300
142 LLVMAddTypeName(gallivm->module, "sampler", sampler_type);
143
144 LLVMInvalidateStructLayout(gallivm->target, sampler_type);
145 #endif
146
147 LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler, min_lod,
148 gallivm->target, sampler_type,
149 LP_JIT_SAMPLER_MIN_LOD);
150 LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler, max_lod,
151 gallivm->target, sampler_type,
152 LP_JIT_SAMPLER_MAX_LOD);
153 LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler, lod_bias,
154 gallivm->target, sampler_type,
155 LP_JIT_SAMPLER_LOD_BIAS);
156 LP_CHECK_MEMBER_OFFSET(struct lp_jit_sampler, border_color,
157 gallivm->target, sampler_type,
158 LP_JIT_SAMPLER_BORDER_COLOR);
159 LP_CHECK_STRUCT_SIZE(struct lp_jit_sampler,
160 gallivm->target, sampler_type);
161 }
162
163 /* struct lp_jit_context */
164 {
165 LLVMTypeRef elem_types[LP_JIT_CTX_COUNT];
166 LLVMTypeRef context_type;
167
168 elem_types[LP_JIT_CTX_CONSTANTS] =
169 LLVMArrayType(LLVMPointerType(LLVMFloatTypeInContext(lc), 0), LP_MAX_TGSI_CONST_BUFFERS);
170 elem_types[LP_JIT_CTX_ALPHA_REF] = LLVMFloatTypeInContext(lc);
171 elem_types[LP_JIT_CTX_STENCIL_REF_FRONT] =
172 elem_types[LP_JIT_CTX_STENCIL_REF_BACK] = LLVMInt32TypeInContext(lc);
173 elem_types[LP_JIT_CTX_U8_BLEND_COLOR] = LLVMPointerType(LLVMInt8TypeInContext(lc), 0);
174 elem_types[LP_JIT_CTX_F_BLEND_COLOR] = LLVMPointerType(LLVMFloatTypeInContext(lc), 0);
175 elem_types[LP_JIT_CTX_VIEWPORTS] = LLVMPointerType(viewport_type, 0);
176 elem_types[LP_JIT_CTX_TEXTURES] = LLVMArrayType(texture_type,
177 PIPE_MAX_SHADER_SAMPLER_VIEWS);
178 elem_types[LP_JIT_CTX_SAMPLERS] = LLVMArrayType(sampler_type,
179 PIPE_MAX_SAMPLERS);
180
181 context_type = LLVMStructTypeInContext(lc, elem_types,
182 Elements(elem_types), 0);
183
184 #if HAVE_LLVM < 0x0300
185 LLVMInvalidateStructLayout(gallivm->target, context_type);
186
187 LLVMAddTypeName(gallivm->module, "context", context_type);
188 #endif
189
190 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, constants,
191 gallivm->target, context_type,
192 LP_JIT_CTX_CONSTANTS);
193 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, alpha_ref_value,
194 gallivm->target, context_type,
195 LP_JIT_CTX_ALPHA_REF);
196 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, stencil_ref_front,
197 gallivm->target, context_type,
198 LP_JIT_CTX_STENCIL_REF_FRONT);
199 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, stencil_ref_back,
200 gallivm->target, context_type,
201 LP_JIT_CTX_STENCIL_REF_BACK);
202 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, u8_blend_color,
203 gallivm->target, context_type,
204 LP_JIT_CTX_U8_BLEND_COLOR);
205 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, f_blend_color,
206 gallivm->target, context_type,
207 LP_JIT_CTX_F_BLEND_COLOR);
208 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, viewports,
209 gallivm->target, context_type,
210 LP_JIT_CTX_VIEWPORTS);
211 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, textures,
212 gallivm->target, context_type,
213 LP_JIT_CTX_TEXTURES);
214 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, samplers,
215 gallivm->target, context_type,
216 LP_JIT_CTX_SAMPLERS);
217 LP_CHECK_STRUCT_SIZE(struct lp_jit_context,
218 gallivm->target, context_type);
219
220 lp->jit_context_ptr_type = LLVMPointerType(context_type, 0);
221 }
222
223 /* struct lp_jit_thread_data */
224 {
225 LLVMTypeRef elem_types[LP_JIT_THREAD_DATA_COUNT];
226 LLVMTypeRef thread_data_type;
227
228 elem_types[LP_JIT_THREAD_DATA_COUNTER] = LLVMInt64TypeInContext(lc);
229 elem_types[LP_JIT_THREAD_DATA_RASTER_STATE_VIEWPORT_INDEX] =
230 LLVMInt32TypeInContext(lc);
231
232 thread_data_type = LLVMStructTypeInContext(lc, elem_types,
233 Elements(elem_types), 0);
234
235 #if HAVE_LLVM < 0x0300
236 LLVMInvalidateStructLayout(gallivm->target, thread_data_type);
237
238 LLVMAddTypeName(gallivm->module, "thread_data", thread_data_type);
239 #endif
240
241 lp->jit_thread_data_ptr_type = LLVMPointerType(thread_data_type, 0);
242 }
243
244 if (gallivm_debug & GALLIVM_DEBUG_IR) {
245 LLVMDumpModule(gallivm->module);
246 }
247 }
248
249
250 void
251 lp_jit_screen_cleanup(struct llvmpipe_screen *screen)
252 {
253 /* nothing */
254 }
255
256
257 void
258 lp_jit_screen_init(struct llvmpipe_screen *screen)
259 {
260 lp_build_init();
261 }
262
263
264 void
265 lp_jit_init_types(struct lp_fragment_shader_variant *lp)
266 {
267 if (!lp->jit_context_ptr_type)
268 lp_jit_create_types(lp);
269 }