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