Merge branch 'mesa_7_6_branch'
[mesa.git] / src / gallium / drivers / llvmpipe / lp_tex_sample_llvm.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 "lp_bld_debug.h"
46 #include "lp_bld_type.h"
47 #include "lp_bld_intr.h"
48 #include "lp_bld_sample.h"
49 #include "lp_bld_tgsi.h"
50 #include "lp_state.h"
51 #include "lp_tex_sample.h"
52
53
54 /**
55 * This provides the bridge between the sampler state store in lp_jit_context
56 * and lp_jit_texture and the sampler code generator. It provides the
57 * texture layout information required by the texture sampler code generator
58 * in terms of the state stored in lp_jit_context and lp_jit_texture in runtime.
59 */
60 struct llvmpipe_sampler_dynamic_state
61 {
62 struct lp_sampler_dynamic_state base;
63
64 const struct lp_sampler_static_state *static_state;
65
66 LLVMValueRef context_ptr;
67 };
68
69
70 /**
71 * This is the bridge between our sampler and the TGSI translator.
72 */
73 struct lp_llvm_sampler_soa
74 {
75 struct lp_build_sampler_soa base;
76
77 struct llvmpipe_sampler_dynamic_state dynamic_state;
78 };
79
80
81 /**
82 * Fetch the specified member of the lp_jit_texture structure.
83 *
84 * @sa http://llvm.org/docs/GetElementPtr.html
85 */
86 static LLVMValueRef
87 lp_llvm_texture_member(struct lp_sampler_dynamic_state *base,
88 LLVMBuilderRef builder,
89 unsigned unit,
90 unsigned member_index,
91 const char *member_name)
92 {
93 struct llvmpipe_sampler_dynamic_state *state = (struct llvmpipe_sampler_dynamic_state *)base;
94 LLVMValueRef indices[4];
95 LLVMValueRef ptr;
96 LLVMValueRef res;
97
98 assert(unit < PIPE_MAX_SAMPLERS);
99
100 /* context[0] */
101 indices[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
102 /* context[0].textures */
103 indices[1] = LLVMConstInt(LLVMInt32Type(), LP_JIT_CONTEXT_TEXTURES_INDEX, 0);
104 /* context[0].textures[unit] */
105 indices[2] = LLVMConstInt(LLVMInt32Type(), unit, 0);
106 /* context[0].textures[unit].member */
107 indices[3] = LLVMConstInt(LLVMInt32Type(), member_index, 0);
108
109 ptr = LLVMBuildGEP(builder, state->context_ptr, indices, Elements(indices), "");
110
111 res = LLVMBuildLoad(builder, ptr, "");
112
113 lp_build_name(res, "context.texture%u.%s", unit, member_name);
114
115 return res;
116 }
117
118
119 /**
120 * Helper macro to instantiate the functions that generate the code to fetch
121 * the members of lp_jit_texture to fulfill the sampler code generator requests.
122 *
123 * This complexity is the price we have to pay to keep the texture sampler code
124 * generator a reusable module without dependencies to llvmpipe internals.
125 */
126 #define LP_LLVM_TEXTURE_MEMBER(_name, _index) \
127 static LLVMValueRef \
128 lp_llvm_texture_##_name( struct lp_sampler_dynamic_state *base, \
129 LLVMBuilderRef builder, \
130 unsigned unit) \
131 { \
132 return lp_llvm_texture_member(base, builder, unit, _index, #_name ); \
133 }
134
135
136 LP_LLVM_TEXTURE_MEMBER(width, LP_JIT_TEXTURE_WIDTH)
137 LP_LLVM_TEXTURE_MEMBER(height, LP_JIT_TEXTURE_HEIGHT)
138 LP_LLVM_TEXTURE_MEMBER(stride, LP_JIT_TEXTURE_STRIDE)
139 LP_LLVM_TEXTURE_MEMBER(data_ptr, LP_JIT_TEXTURE_DATA)
140
141
142 static void
143 lp_llvm_sampler_soa_destroy(struct lp_build_sampler_soa *sampler)
144 {
145 FREE(sampler);
146 }
147
148
149 static void
150 lp_llvm_sampler_soa_emit_fetch_texel(struct lp_build_sampler_soa *base,
151 LLVMBuilderRef builder,
152 struct lp_type type,
153 unsigned unit,
154 unsigned num_coords,
155 const LLVMValueRef *coords,
156 LLVMValueRef lodbias,
157 LLVMValueRef *texel)
158 {
159 struct lp_llvm_sampler_soa *sampler = (struct lp_llvm_sampler_soa *)base;
160
161 assert(unit < PIPE_MAX_SAMPLERS);
162
163 lp_build_sample_soa(builder,
164 &sampler->dynamic_state.static_state[unit],
165 &sampler->dynamic_state.base,
166 type,
167 unit,
168 num_coords,
169 coords,
170 lodbias,
171 texel);
172 }
173
174
175 struct lp_build_sampler_soa *
176 lp_llvm_sampler_soa_create(const struct lp_sampler_static_state *static_state,
177 LLVMValueRef context_ptr)
178 {
179 struct lp_llvm_sampler_soa *sampler;
180
181 sampler = CALLOC_STRUCT(lp_llvm_sampler_soa);
182 if(!sampler)
183 return NULL;
184
185 sampler->base.destroy = lp_llvm_sampler_soa_destroy;
186 sampler->base.emit_fetch_texel = lp_llvm_sampler_soa_emit_fetch_texel;
187 sampler->dynamic_state.base.width = lp_llvm_texture_width;
188 sampler->dynamic_state.base.height = lp_llvm_texture_height;
189 sampler->dynamic_state.base.stride = lp_llvm_texture_stride;
190 sampler->dynamic_state.base.data_ptr = lp_llvm_texture_data_ptr;
191 sampler->dynamic_state.static_state = static_state;
192 sampler->dynamic_state.context_ptr = context_ptr;
193
194 return &sampler->base;
195 }
196