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  * Texture sampling code generation
30  *
31  * This file is nothing more than ugly glue between three largely independent
32  * entities:
33  * - TGSI -> LLVM translation (i.e., lp_build_tgsi_soa)
34  * - texture sampling code generation (i.e., lp_build_sample_soa)
35  * - LLVM pipe driver
36  *
37  * All interesting code is in the functions mentioned above. There is really
38  * nothing to see here.
39  *
40  * @author Jose Fonseca <jfonseca@vmware.com>
41  */
42 
43 #include "pipe/p_defines.h"
44 #include "pipe/p_shader_tokens.h"
45 #include "gallivm/lp_bld_debug.h"
46 #include "gallivm/lp_bld_const.h"
47 #include "gallivm/lp_bld_type.h"
48 #include "gallivm/lp_bld_sample.h"
49 #include "gallivm/lp_bld_tgsi.h"
50 #include "lp_jit.h"
51 #include "lp_tex_sample.h"
52 #include "lp_debug.h"
53 
54 
55 /**
56  * This provides the bridge between the sampler state store in
57  * lp_jit_context and lp_jit_texture and the sampler code
58  * generator. It provides the texture layout information required by
59  * the texture sampler code generator in terms of the state stored in
60  * lp_jit_context and lp_jit_texture in runtime.
61  */
62 struct llvmpipe_sampler_dynamic_state
63 {
64    struct lp_sampler_dynamic_state base;
65 
66    const struct lp_sampler_static_state *static_state;
67 
68    LLVMValueRef context_ptr;
69 };
70 
71 
72 /**
73  * This is the bridge between our sampler and the TGSI translator.
74  */
75 struct lp_llvm_sampler_soa
76 {
77    struct lp_build_sampler_soa base;
78 
79    struct llvmpipe_sampler_dynamic_state dynamic_state;
80 };
81 
82 
83 /**
84  * Fetch the specified member of the lp_jit_texture structure.
85  * \param emit_load  if TRUE, emit the LLVM load instruction to actually
86  *                   fetch the field's value.  Otherwise, just emit the
87  *                   GEP code to address the field.
88  *
89  * @sa http://llvm.org/docs/GetElementPtr.html
90  */
91 static LLVMValueRef
lp_llvm_texture_member(const struct lp_sampler_dynamic_state * base,struct gallivm_state * gallivm,unsigned unit,unsigned member_index,const char * member_name,boolean emit_load)92 lp_llvm_texture_member(const struct lp_sampler_dynamic_state *base,
93                        struct gallivm_state *gallivm,
94                        unsigned unit,
95                        unsigned member_index,
96                        const char *member_name,
97                        boolean emit_load)
98 {
99    struct llvmpipe_sampler_dynamic_state *state =
100       (struct llvmpipe_sampler_dynamic_state *)base;
101    LLVMBuilderRef builder = gallivm->builder;
102    LLVMValueRef indices[4];
103    LLVMValueRef ptr;
104    LLVMValueRef res;
105 
106    assert(unit < PIPE_MAX_SAMPLERS);
107 
108    /* context[0] */
109    indices[0] = lp_build_const_int32(gallivm, 0);
110    /* context[0].textures */
111    indices[1] = lp_build_const_int32(gallivm, LP_JIT_CTX_TEXTURES);
112    /* context[0].textures[unit] */
113    indices[2] = lp_build_const_int32(gallivm, unit);
114    /* context[0].textures[unit].member */
115    indices[3] = lp_build_const_int32(gallivm, member_index);
116 
117    ptr = LLVMBuildGEP(builder, state->context_ptr, indices, Elements(indices), "");
118 
119    if (emit_load)
120       res = LLVMBuildLoad(builder, ptr, "");
121    else
122       res = ptr;
123 
124    lp_build_name(res, "context.texture%u.%s", unit, member_name);
125 
126    return res;
127 }
128 
129 
130 /**
131  * Helper macro to instantiate the functions that generate the code to
132  * fetch the members of lp_jit_texture to fulfill the sampler code
133  * generator requests.
134  *
135  * This complexity is the price we have to pay to keep the texture
136  * sampler code generator a reusable module without dependencies to
137  * llvmpipe internals.
138  */
139 #define LP_LLVM_TEXTURE_MEMBER(_name, _index, _emit_load)  \
140    static LLVMValueRef \
141    lp_llvm_texture_##_name( const struct lp_sampler_dynamic_state *base, \
142                             struct gallivm_state *gallivm, \
143                             unsigned unit) \
144    { \
145       return lp_llvm_texture_member(base, gallivm, unit, _index, #_name, _emit_load ); \
146    }
147 
148 
LP_LLVM_TEXTURE_MEMBER(width,LP_JIT_TEXTURE_WIDTH,TRUE)149 LP_LLVM_TEXTURE_MEMBER(width,      LP_JIT_TEXTURE_WIDTH, TRUE)
150 LP_LLVM_TEXTURE_MEMBER(height,     LP_JIT_TEXTURE_HEIGHT, TRUE)
151 LP_LLVM_TEXTURE_MEMBER(depth,      LP_JIT_TEXTURE_DEPTH, TRUE)
152 LP_LLVM_TEXTURE_MEMBER(first_level, LP_JIT_TEXTURE_FIRST_LEVEL, TRUE)
153 LP_LLVM_TEXTURE_MEMBER(last_level, LP_JIT_TEXTURE_LAST_LEVEL, TRUE)
154 LP_LLVM_TEXTURE_MEMBER(row_stride, LP_JIT_TEXTURE_ROW_STRIDE, FALSE)
155 LP_LLVM_TEXTURE_MEMBER(img_stride, LP_JIT_TEXTURE_IMG_STRIDE, FALSE)
156 LP_LLVM_TEXTURE_MEMBER(data_ptr,   LP_JIT_TEXTURE_DATA, FALSE)
157 LP_LLVM_TEXTURE_MEMBER(min_lod,    LP_JIT_TEXTURE_MIN_LOD, TRUE)
158 LP_LLVM_TEXTURE_MEMBER(max_lod,    LP_JIT_TEXTURE_MAX_LOD, TRUE)
159 LP_LLVM_TEXTURE_MEMBER(lod_bias,   LP_JIT_TEXTURE_LOD_BIAS, TRUE)
160 LP_LLVM_TEXTURE_MEMBER(border_color, LP_JIT_TEXTURE_BORDER_COLOR, FALSE)
161 
162 
163 static void
164 lp_llvm_sampler_soa_destroy(struct lp_build_sampler_soa *sampler)
165 {
166    FREE(sampler);
167 }
168 
169 
170 /**
171  * Fetch filtered values from texture.
172  * The 'texel' parameter returns four vectors corresponding to R, G, B, A.
173  */
174 static void
lp_llvm_sampler_soa_emit_fetch_texel(const struct lp_build_sampler_soa * base,struct gallivm_state * gallivm,struct lp_type type,unsigned unit,unsigned num_coords,const LLVMValueRef * coords,const struct lp_derivatives * derivs,LLVMValueRef lod_bias,LLVMValueRef explicit_lod,LLVMValueRef * texel)175 lp_llvm_sampler_soa_emit_fetch_texel(const struct lp_build_sampler_soa *base,
176                                      struct gallivm_state *gallivm,
177                                      struct lp_type type,
178                                      unsigned unit,
179                                      unsigned num_coords,
180                                      const LLVMValueRef *coords,
181                                      const struct lp_derivatives *derivs,
182                                      LLVMValueRef lod_bias, /* optional */
183                                      LLVMValueRef explicit_lod, /* optional */
184                                      LLVMValueRef *texel)
185 {
186    struct lp_llvm_sampler_soa *sampler = (struct lp_llvm_sampler_soa *)base;
187 
188    assert(unit < PIPE_MAX_SAMPLERS);
189 
190    if (LP_PERF & PERF_NO_TEX) {
191       lp_build_sample_nop(gallivm, type, num_coords, coords, texel);
192       return;
193    }
194 
195    lp_build_sample_soa(gallivm,
196                        &sampler->dynamic_state.static_state[unit],
197                        &sampler->dynamic_state.base,
198                        type,
199                        unit,
200                        num_coords, coords,
201                        derivs,
202                        lod_bias, explicit_lod,
203                        texel);
204 }
205 
206 /**
207  * Fetch the texture size.
208  */
209 static void
lp_llvm_sampler_soa_emit_size_query(const struct lp_build_sampler_soa * base,struct gallivm_state * gallivm,struct lp_type type,unsigned unit,LLVMValueRef explicit_lod,LLVMValueRef * sizes_out)210 lp_llvm_sampler_soa_emit_size_query(const struct lp_build_sampler_soa *base,
211                                     struct gallivm_state *gallivm,
212                                     struct lp_type type,
213                                     unsigned unit,
214                                     LLVMValueRef explicit_lod, /* optional */
215                                     LLVMValueRef *sizes_out)
216 {
217    struct lp_llvm_sampler_soa *sampler = (struct lp_llvm_sampler_soa *)base;
218 
219    assert(unit < PIPE_MAX_SAMPLERS);
220 
221    lp_build_size_query_soa(gallivm,
222 			   &sampler->dynamic_state.static_state[unit],
223 			   &sampler->dynamic_state.base,
224                            type,
225 			   unit,
226 			   explicit_lod,
227 			   sizes_out);
228 }
229 
230 
231 struct lp_build_sampler_soa *
lp_llvm_sampler_soa_create(const struct lp_sampler_static_state * static_state,LLVMValueRef context_ptr)232 lp_llvm_sampler_soa_create(const struct lp_sampler_static_state *static_state,
233                            LLVMValueRef context_ptr)
234 {
235    struct lp_llvm_sampler_soa *sampler;
236 
237    sampler = CALLOC_STRUCT(lp_llvm_sampler_soa);
238    if(!sampler)
239       return NULL;
240 
241    sampler->base.destroy = lp_llvm_sampler_soa_destroy;
242    sampler->base.emit_fetch_texel = lp_llvm_sampler_soa_emit_fetch_texel;
243    sampler->base.emit_size_query = lp_llvm_sampler_soa_emit_size_query;
244    sampler->dynamic_state.base.width = lp_llvm_texture_width;
245    sampler->dynamic_state.base.height = lp_llvm_texture_height;
246    sampler->dynamic_state.base.depth = lp_llvm_texture_depth;
247    sampler->dynamic_state.base.first_level = lp_llvm_texture_first_level;
248    sampler->dynamic_state.base.last_level = lp_llvm_texture_last_level;
249    sampler->dynamic_state.base.row_stride = lp_llvm_texture_row_stride;
250    sampler->dynamic_state.base.img_stride = lp_llvm_texture_img_stride;
251    sampler->dynamic_state.base.data_ptr = lp_llvm_texture_data_ptr;
252    sampler->dynamic_state.base.min_lod = lp_llvm_texture_min_lod;
253    sampler->dynamic_state.base.max_lod = lp_llvm_texture_max_lod;
254    sampler->dynamic_state.base.lod_bias = lp_llvm_texture_lod_bias;
255    sampler->dynamic_state.base.border_color = lp_llvm_texture_border_color;
256 
257    sampler->dynamic_state.static_state = static_state;
258    sampler->dynamic_state.context_ptr = context_ptr;
259 
260    return &sampler->base;
261 }
262 
263