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
lp_jit_create_types(struct lp_fragment_shader_variant * lp)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 texture_type;
49 
50    /* struct lp_jit_texture */
51    {
52       LLVMTypeRef elem_types[LP_JIT_TEXTURE_NUM_FIELDS];
53 
54       elem_types[LP_JIT_TEXTURE_WIDTH]  =
55       elem_types[LP_JIT_TEXTURE_HEIGHT] =
56       elem_types[LP_JIT_TEXTURE_DEPTH] =
57       elem_types[LP_JIT_TEXTURE_FIRST_LEVEL] =
58       elem_types[LP_JIT_TEXTURE_LAST_LEVEL] =  LLVMInt32TypeInContext(lc);
59       elem_types[LP_JIT_TEXTURE_ROW_STRIDE] =
60       elem_types[LP_JIT_TEXTURE_IMG_STRIDE] =
61          LLVMArrayType(LLVMInt32TypeInContext(lc), LP_MAX_TEXTURE_LEVELS);
62       elem_types[LP_JIT_TEXTURE_DATA] =
63          LLVMArrayType(LLVMPointerType(LLVMInt8TypeInContext(lc), 0),
64                        LP_MAX_TEXTURE_LEVELS);
65       elem_types[LP_JIT_TEXTURE_MIN_LOD] =
66       elem_types[LP_JIT_TEXTURE_MAX_LOD] =
67       elem_types[LP_JIT_TEXTURE_LOD_BIAS] = LLVMFloatTypeInContext(lc);
68       elem_types[LP_JIT_TEXTURE_BORDER_COLOR] =
69          LLVMArrayType(LLVMFloatTypeInContext(lc), 4);
70 
71       texture_type = LLVMStructTypeInContext(lc, elem_types,
72                                              Elements(elem_types), 0);
73 #if HAVE_LLVM < 0x0300
74       LLVMAddTypeName(gallivm->module, "texture", texture_type);
75 
76       LLVMInvalidateStructLayout(gallivm->target, texture_type);
77 #endif
78 
79       LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, width,
80                              gallivm->target, texture_type,
81                              LP_JIT_TEXTURE_WIDTH);
82       LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, height,
83                              gallivm->target, texture_type,
84                              LP_JIT_TEXTURE_HEIGHT);
85       LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, depth,
86                              gallivm->target, texture_type,
87                              LP_JIT_TEXTURE_DEPTH);
88       LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, first_level,
89                              gallivm->target, texture_type,
90                              LP_JIT_TEXTURE_FIRST_LEVEL);
91       LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, last_level,
92                              gallivm->target, texture_type,
93                              LP_JIT_TEXTURE_LAST_LEVEL);
94       LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, row_stride,
95                              gallivm->target, texture_type,
96                              LP_JIT_TEXTURE_ROW_STRIDE);
97       LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, img_stride,
98                              gallivm->target, texture_type,
99                              LP_JIT_TEXTURE_IMG_STRIDE);
100       LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, data,
101                              gallivm->target, texture_type,
102                              LP_JIT_TEXTURE_DATA);
103       LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, min_lod,
104                              gallivm->target, texture_type,
105                              LP_JIT_TEXTURE_MIN_LOD);
106       LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, max_lod,
107                              gallivm->target, texture_type,
108                              LP_JIT_TEXTURE_MAX_LOD);
109       LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, lod_bias,
110                              gallivm->target, texture_type,
111                              LP_JIT_TEXTURE_LOD_BIAS);
112       LP_CHECK_MEMBER_OFFSET(struct lp_jit_texture, border_color,
113                              gallivm->target, texture_type,
114                              LP_JIT_TEXTURE_BORDER_COLOR);
115 
116       LP_CHECK_STRUCT_SIZE(struct lp_jit_texture,
117                            gallivm->target, texture_type);
118    }
119 
120    /* struct lp_jit_context */
121    {
122       LLVMTypeRef elem_types[LP_JIT_CTX_COUNT];
123       LLVMTypeRef context_type;
124 
125       elem_types[LP_JIT_CTX_CONSTANTS] = LLVMPointerType(LLVMFloatTypeInContext(lc), 0);
126       elem_types[LP_JIT_CTX_ALPHA_REF] = LLVMFloatTypeInContext(lc);
127       elem_types[LP_JIT_CTX_STENCIL_REF_FRONT] =
128       elem_types[LP_JIT_CTX_STENCIL_REF_BACK] = LLVMInt32TypeInContext(lc);
129       elem_types[LP_JIT_CTX_BLEND_COLOR] = LLVMPointerType(LLVMInt8TypeInContext(lc), 0);
130       elem_types[LP_JIT_CTX_TEXTURES] = LLVMArrayType(texture_type,
131                                                       PIPE_MAX_SAMPLERS);
132 
133       context_type = LLVMStructTypeInContext(lc, elem_types,
134                                              Elements(elem_types), 0);
135 
136 #if HAVE_LLVM < 0x0300
137       LLVMInvalidateStructLayout(gallivm->target, context_type);
138 
139       LLVMAddTypeName(gallivm->module, "context", context_type);
140 #endif
141 
142       LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, constants,
143                              gallivm->target, context_type,
144                              LP_JIT_CTX_CONSTANTS);
145       LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, alpha_ref_value,
146                              gallivm->target, context_type,
147                              LP_JIT_CTX_ALPHA_REF);
148       LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, stencil_ref_front,
149                              gallivm->target, context_type,
150                              LP_JIT_CTX_STENCIL_REF_FRONT);
151       LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, stencil_ref_back,
152                              gallivm->target, context_type,
153                              LP_JIT_CTX_STENCIL_REF_BACK);
154       LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, blend_color,
155                              gallivm->target, context_type,
156                              LP_JIT_CTX_BLEND_COLOR);
157       LP_CHECK_MEMBER_OFFSET(struct lp_jit_context, textures,
158                              gallivm->target, context_type,
159                              LP_JIT_CTX_TEXTURES);
160       LP_CHECK_STRUCT_SIZE(struct lp_jit_context,
161                            gallivm->target, context_type);
162 
163       lp->jit_context_ptr_type = LLVMPointerType(context_type, 0);
164    }
165 
166    if (gallivm_debug & GALLIVM_DEBUG_IR) {
167       LLVMDumpModule(gallivm->module);
168    }
169 }
170 
171 
172 void
lp_jit_screen_cleanup(struct llvmpipe_screen * screen)173 lp_jit_screen_cleanup(struct llvmpipe_screen *screen)
174 {
175    /* nothing */
176 }
177 
178 
179 void
lp_jit_screen_init(struct llvmpipe_screen * screen)180 lp_jit_screen_init(struct llvmpipe_screen *screen)
181 {
182    lp_build_init();
183 }
184 
185 
186 void
lp_jit_init_types(struct lp_fragment_shader_variant * lp)187 lp_jit_init_types(struct lp_fragment_shader_variant *lp)
188 {
189    if (!lp->jit_context_ptr_type)
190       lp_jit_create_types(lp);
191 }
192