llvmpipe: make texture border_color dynamic state
[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 <llvm-c/Transforms/Scalar.h>
37
38 #include "util/u_memory.h"
39 #include "util/u_cpu_detect.h"
40 #include "gallivm/lp_bld_init.h"
41 #include "gallivm/lp_bld_debug.h"
42 #include "lp_screen.h"
43 #include "gallivm/lp_bld_intr.h"
44 #include "lp_jit.h"
45
46
47 static void
48 lp_jit_init_globals(struct llvmpipe_screen *screen)
49 {
50 LLVMTypeRef texture_type;
51
52 /* struct lp_jit_texture */
53 {
54 LLVMTypeRef elem_types[LP_JIT_TEXTURE_NUM_FIELDS];
55
56 elem_types[LP_JIT_TEXTURE_WIDTH] = LLVMInt32Type();
57 elem_types[LP_JIT_TEXTURE_HEIGHT] = LLVMInt32Type();
58 elem_types[LP_JIT_TEXTURE_DEPTH] = LLVMInt32Type();
59 elem_types[LP_JIT_TEXTURE_LAST_LEVEL] = LLVMInt32Type();
60 elem_types[LP_JIT_TEXTURE_ROW_STRIDE] =
61 LLVMArrayType(LLVMInt32Type(), LP_MAX_TEXTURE_LEVELS);
62 elem_types[LP_JIT_TEXTURE_IMG_STRIDE] =
63 LLVMArrayType(LLVMInt32Type(), LP_MAX_TEXTURE_LEVELS);
64 elem_types[LP_JIT_TEXTURE_DATA] =
65 LLVMArrayType(LLVMPointerType(LLVMInt8Type(), 0),
66 LP_MAX_TEXTURE_LEVELS);
67 elem_types[LP_JIT_TEXTURE_MIN_LOD] = LLVMFloatType();
68 elem_types[LP_JIT_TEXTURE_MAX_LOD] = LLVMFloatType();
69 elem_types[LP_JIT_TEXTURE_LOD_BIAS] = LLVMFloatType();
70 elem_types[LP_JIT_TEXTURE_BORDER_COLOR] =
71 LLVMArrayType(LLVMFloatType(), 4);
72
73 texture_type = LLVMStructType(elem_types, Elements(elem_types), 0);
74
75 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, width,
76 screen->target, texture_type,
77 LP_JIT_TEXTURE_WIDTH);
78 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, height,
79 screen->target, texture_type,
80 LP_JIT_TEXTURE_HEIGHT);
81 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, depth,
82 screen->target, texture_type,
83 LP_JIT_TEXTURE_DEPTH);
84 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, last_level,
85 screen->target, texture_type,
86 LP_JIT_TEXTURE_LAST_LEVEL);
87 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, row_stride,
88 screen->target, texture_type,
89 LP_JIT_TEXTURE_ROW_STRIDE);
90 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, img_stride,
91 screen->target, texture_type,
92 LP_JIT_TEXTURE_IMG_STRIDE);
93 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, data,
94 screen->target, texture_type,
95 LP_JIT_TEXTURE_DATA);
96 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, min_lod,
97 screen->target, texture_type,
98 LP_JIT_TEXTURE_MIN_LOD);
99 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, max_lod,
100 screen->target, texture_type,
101 LP_JIT_TEXTURE_MAX_LOD);
102 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, lod_bias,
103 screen->target, texture_type,
104 LP_JIT_TEXTURE_LOD_BIAS);
105 LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, border_color,
106 screen->target, texture_type,
107 LP_JIT_TEXTURE_BORDER_COLOR);
108
109 LP_CHECK_STRUCT_SIZE(struct lp_jit_texture,
110 screen->target, texture_type);
111
112 LLVMAddTypeName(screen->module, "texture", texture_type);
113 }
114
115 /* struct lp_jit_context */
116 {
117 LLVMTypeRef elem_types[LP_JIT_CTX_COUNT];
118 LLVMTypeRef context_type;
119
120 elem_types[LP_JIT_CTX_CONSTANTS] = LLVMPointerType(LLVMFloatType(), 0);
121 elem_types[LP_JIT_CTX_ALPHA_REF] = LLVMFloatType();
122 elem_types[LP_JIT_CTX_STENCIL_REF_FRONT] = LLVMInt32Type();
123 elem_types[LP_JIT_CTX_STENCIL_REF_BACK] = LLVMInt32Type();
124 elem_types[LP_JIT_CTX_BLEND_COLOR] = LLVMPointerType(LLVMInt8Type(), 0);
125 elem_types[LP_JIT_CTX_TEXTURES] = LLVMArrayType(texture_type,
126 PIPE_MAX_SAMPLERS);
127
128 context_type = LLVMStructType(elem_types, Elements(elem_types), 0);
129
130 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, constants,
131 screen->target, context_type,
132 LP_JIT_CTX_CONSTANTS);
133 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, alpha_ref_value,
134 screen->target, context_type,
135 LP_JIT_CTX_ALPHA_REF);
136 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, stencil_ref_front,
137 screen->target, context_type,
138 LP_JIT_CTX_STENCIL_REF_FRONT);
139 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, stencil_ref_back,
140 screen->target, context_type,
141 LP_JIT_CTX_STENCIL_REF_BACK);
142 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, blend_color,
143 screen->target, context_type,
144 LP_JIT_CTX_BLEND_COLOR);
145 LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, textures,
146 screen->target, context_type,
147 LP_JIT_CTX_TEXTURES);
148 LP_CHECK_STRUCT_SIZE(struct lp_jit_context,
149 screen->target, context_type);
150
151 LLVMAddTypeName(screen->module, "context", context_type);
152
153 screen->context_ptr_type = LLVMPointerType(context_type, 0);
154 }
155
156 if (gallivm_debug & GALLIVM_DEBUG_IR) {
157 LLVMDumpModule(screen->module);
158 }
159 }
160
161
162 void
163 lp_jit_screen_cleanup(struct llvmpipe_screen *screen)
164 {
165 if(screen->engine)
166 LLVMDisposeExecutionEngine(screen->engine);
167
168 if(screen->pass)
169 LLVMDisposePassManager(screen->pass);
170 }
171
172
173 void
174 lp_jit_screen_init(struct llvmpipe_screen *screen)
175 {
176 lp_build_init();
177
178 screen->module = lp_build_module;
179 screen->provider = lp_build_provider;
180 screen->engine = lp_build_engine;
181 screen->target = lp_build_target;
182
183 screen->pass = LLVMCreateFunctionPassManager(screen->provider);
184 LLVMAddTargetData(screen->target, screen->pass);
185
186 if ((gallivm_debug & GALLIVM_DEBUG_NO_OPT) == 0) {
187 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
188 * but there are more on SVN. */
189 /* TODO: Add more passes */
190 LLVMAddCFGSimplificationPass(screen->pass);
191 LLVMAddPromoteMemoryToRegisterPass(screen->pass);
192 LLVMAddConstantPropagationPass(screen->pass);
193 if(util_cpu_caps.has_sse4_1) {
194 /* FIXME: There is a bug in this pass, whereby the combination of fptosi
195 * and sitofp (necessary for trunc/floor/ceil/round implementation)
196 * somehow becomes invalid code.
197 */
198 LLVMAddInstructionCombiningPass(screen->pass);
199 }
200 LLVMAddGVNPass(screen->pass);
201 } else {
202 /* We need at least this pass to prevent the backends to fail in
203 * unexpected ways.
204 */
205 LLVMAddPromoteMemoryToRegisterPass(screen->pass);
206 }
207
208 lp_jit_init_globals(screen);
209 }