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