llvmpipe: make min/max lod and lod bias dynamic state
[mesa.git] / src / gallium / drivers / llvmpipe / lp_tex_sample.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 * 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_type.h"
47 #include "gallivm/lp_bld_sample.h"
48 #include "gallivm/lp_bld_tgsi.h"
49 #include "lp_jit.h"
50 #include "lp_tex_sample.h"
51 #include "lp_debug.h"
52
53
54 /**
55 * This provides the bridge between the sampler state store in
56 * lp_jit_context and lp_jit_texture and the sampler code
57 * generator. It provides the texture layout information required by
58 * the texture sampler code generator in terms of the state stored in
59 * lp_jit_context and lp_jit_texture in runtime.
60 */
61 struct llvmpipe_sampler_dynamic_state
62 {
63 struct lp_sampler_dynamic_state base;
64
65 const struct lp_sampler_static_state *static_state;
66
67 LLVMValueRef context_ptr;
68 };
69
70
71 /**
72 * This is the bridge between our sampler and the TGSI translator.
73 */
74 struct lp_llvm_sampler_soa
75 {
76 struct lp_build_sampler_soa base;
77
78 struct llvmpipe_sampler_dynamic_state dynamic_state;
79 };
80
81
82 /**
83 * Fetch the specified member of the lp_jit_texture structure.
84 * \param emit_load if TRUE, emit the LLVM load instruction to actually
85 * fetch the field's value. Otherwise, just emit the
86 * GEP code to address the field.
87 *
88 * @sa http://llvm.org/docs/GetElementPtr.html
89 */
90 static LLVMValueRef
91 lp_llvm_texture_member(const struct lp_sampler_dynamic_state *base,
92 LLVMBuilderRef builder,
93 unsigned unit,
94 unsigned member_index,
95 const char *member_name,
96 boolean emit_load)
97 {
98 struct llvmpipe_sampler_dynamic_state *state =
99 (struct llvmpipe_sampler_dynamic_state *)base;
100 LLVMValueRef indices[4];
101 LLVMValueRef ptr;
102 LLVMValueRef res;
103
104 assert(unit < PIPE_MAX_SAMPLERS);
105
106 /* context[0] */
107 indices[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
108 /* context[0].textures */
109 indices[1] = LLVMConstInt(LLVMInt32Type(), LP_JIT_CTX_TEXTURES, 0);
110 /* context[0].textures[unit] */
111 indices[2] = LLVMConstInt(LLVMInt32Type(), unit, 0);
112 /* context[0].textures[unit].member */
113 indices[3] = LLVMConstInt(LLVMInt32Type(), member_index, 0);
114
115 ptr = LLVMBuildGEP(builder, state->context_ptr, indices, Elements(indices), "");
116
117 if (emit_load)
118 res = LLVMBuildLoad(builder, ptr, "");
119 else
120 res = ptr;
121
122 lp_build_name(res, "context.texture%u.%s", unit, member_name);
123
124 return res;
125 }
126
127
128 /**
129 * Helper macro to instantiate the functions that generate the code to
130 * fetch the members of lp_jit_texture to fulfill the sampler code
131 * generator requests.
132 *
133 * This complexity is the price we have to pay to keep the texture
134 * sampler code generator a reusable module without dependencies to
135 * llvmpipe internals.
136 */
137 #define LP_LLVM_TEXTURE_MEMBER(_name, _index, _emit_load) \
138 static LLVMValueRef \
139 lp_llvm_texture_##_name( const struct lp_sampler_dynamic_state *base, \
140 LLVMBuilderRef builder, \
141 unsigned unit) \
142 { \
143 return lp_llvm_texture_member(base, builder, unit, _index, #_name, _emit_load ); \
144 }
145
146
147 LP_LLVM_TEXTURE_MEMBER(width, LP_JIT_TEXTURE_WIDTH, TRUE)
148 LP_LLVM_TEXTURE_MEMBER(height, LP_JIT_TEXTURE_HEIGHT, TRUE)
149 LP_LLVM_TEXTURE_MEMBER(depth, LP_JIT_TEXTURE_DEPTH, TRUE)
150 LP_LLVM_TEXTURE_MEMBER(last_level, LP_JIT_TEXTURE_LAST_LEVEL, TRUE)
151 LP_LLVM_TEXTURE_MEMBER(row_stride, LP_JIT_TEXTURE_ROW_STRIDE, FALSE)
152 LP_LLVM_TEXTURE_MEMBER(img_stride, LP_JIT_TEXTURE_IMG_STRIDE, FALSE)
153 LP_LLVM_TEXTURE_MEMBER(data_ptr, LP_JIT_TEXTURE_DATA, FALSE)
154 LP_LLVM_TEXTURE_MEMBER(min_lod, LP_JIT_TEXTURE_MIN_LOD, TRUE)
155 LP_LLVM_TEXTURE_MEMBER(max_lod, LP_JIT_TEXTURE_MAX_LOD, TRUE)
156 LP_LLVM_TEXTURE_MEMBER(lod_bias, LP_JIT_TEXTURE_LOD_BIAS, TRUE)
157
158
159 static void
160 lp_llvm_sampler_soa_destroy(struct lp_build_sampler_soa *sampler)
161 {
162 FREE(sampler);
163 }
164
165
166 /**
167 * Fetch filtered values from texture.
168 * The 'texel' parameter returns four vectors corresponding to R, G, B, A.
169 */
170 static void
171 lp_llvm_sampler_soa_emit_fetch_texel(const struct lp_build_sampler_soa *base,
172 LLVMBuilderRef builder,
173 struct lp_type type,
174 unsigned unit,
175 unsigned num_coords,
176 const LLVMValueRef *coords,
177 const LLVMValueRef *ddx,
178 const LLVMValueRef *ddy,
179 LLVMValueRef lod_bias, /* optional */
180 LLVMValueRef explicit_lod, /* optional */
181 LLVMValueRef *texel)
182 {
183 struct lp_llvm_sampler_soa *sampler = (struct lp_llvm_sampler_soa *)base;
184
185 assert(unit < PIPE_MAX_SAMPLERS);
186
187 if (LP_PERF & PERF_NO_TEX) {
188 lp_build_sample_nop(type, texel);
189 return;
190 }
191
192 lp_build_sample_soa(builder,
193 &sampler->dynamic_state.static_state[unit],
194 &sampler->dynamic_state.base,
195 type,
196 unit,
197 num_coords, coords,
198 ddx, ddy,
199 lod_bias, explicit_lod,
200 texel);
201 }
202
203
204 struct lp_build_sampler_soa *
205 lp_llvm_sampler_soa_create(const struct lp_sampler_static_state *static_state,
206 LLVMValueRef context_ptr)
207 {
208 struct lp_llvm_sampler_soa *sampler;
209
210 sampler = CALLOC_STRUCT(lp_llvm_sampler_soa);
211 if(!sampler)
212 return NULL;
213
214 sampler->base.destroy = lp_llvm_sampler_soa_destroy;
215 sampler->base.emit_fetch_texel = lp_llvm_sampler_soa_emit_fetch_texel;
216 sampler->dynamic_state.base.width = lp_llvm_texture_width;
217 sampler->dynamic_state.base.height = lp_llvm_texture_height;
218 sampler->dynamic_state.base.depth = lp_llvm_texture_depth;
219 sampler->dynamic_state.base.last_level = lp_llvm_texture_last_level;
220 sampler->dynamic_state.base.row_stride = lp_llvm_texture_row_stride;
221 sampler->dynamic_state.base.img_stride = lp_llvm_texture_img_stride;
222 sampler->dynamic_state.base.data_ptr = lp_llvm_texture_data_ptr;
223 sampler->dynamic_state.base.min_lod = lp_llvm_texture_min_lod;
224 sampler->dynamic_state.base.max_lod = lp_llvm_texture_max_lod;
225 sampler->dynamic_state.base.lod_bias = lp_llvm_texture_lod_bias;
226
227 sampler->dynamic_state.static_state = static_state;
228 sampler->dynamic_state.context_ptr = context_ptr;
229
230 return &sampler->base;
231 }
232