summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/llvmpipe/lp_tex_sample_llvm.c
blob: d2a6ae21f57ab5670aabe66785a2d42b3915196c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**************************************************************************
 * 
 * Copyright 2009 VMware, Inc.
 * All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 **************************************************************************/

/**
 * Texture sampling code generation
 *
 * This file is nothing more than ugly glue between three largely independent
 * entities:
 * - TGSI -> LLVM translation (i.e., lp_build_tgsi_soa)
 * - texture sampling code generation (i.e., lp_build_sample_soa)
 * - LLVM pipe driver
 *
 * All interesting code is in the functions mentioned above. There is really
 * nothing to see here.
 *
 * @author Jose Fonseca <jfonseca@vmware.com>
 */

#include "pipe/p_defines.h"
#include "pipe/p_shader_tokens.h"
#include "lp_bld_debug.h"
#include "lp_bld_type.h"
#include "lp_bld_intr.h"
#include "lp_bld_sample.h"
#include "lp_bld_tgsi.h"
#include "lp_state.h"
#include "lp_tex_sample.h"


/**
 * This provides the bridge between the sampler state store in lp_jit_context
 * and lp_jit_texture and the sampler code generator. It provides the
 * texture layout information required by the texture sampler code generator
 * in terms of the state stored in lp_jit_context and lp_jit_texture in runtime.
 */
struct llvmpipe_sampler_dynamic_state
{
   struct lp_sampler_dynamic_state base;

   const struct lp_sampler_static_state *static_state;

   LLVMValueRef context_ptr;
};


/**
 * This is the bridge between our sampler and the TGSI translator.
 */
struct lp_llvm_sampler_soa
{
   struct lp_build_sampler_soa base;

   struct llvmpipe_sampler_dynamic_state dynamic_state;
};


/**
 * Fetch the specified member of the lp_jit_texture structure.
 *
 * @sa http://llvm.org/docs/GetElementPtr.html
 */
static LLVMValueRef
lp_llvm_texture_member(struct lp_sampler_dynamic_state *base,
                       LLVMBuilderRef builder,
                       unsigned unit,
                       unsigned member_index,
                       const char *member_name)
{
   struct llvmpipe_sampler_dynamic_state *state = (struct llvmpipe_sampler_dynamic_state *)base;
   LLVMValueRef indices[4];
   LLVMValueRef ptr;
   LLVMValueRef res;

   assert(unit < PIPE_MAX_SAMPLERS);

   /* context[0] */
   indices[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
   /* context[0].textures */
   indices[1] = LLVMConstInt(LLVMInt32Type(), LP_JIT_CONTEXT_TEXTURES_INDEX, 0);
   /* context[0].textures[unit] */
   indices[2] = LLVMConstInt(LLVMInt32Type(), unit, 0);
   /* context[0].textures[unit].member */
   indices[3] = LLVMConstInt(LLVMInt32Type(), member_index, 0);

   ptr = LLVMBuildGEP(builder, state->context_ptr, indices, Elements(indices), "");

   res = LLVMBuildLoad(builder, ptr, "");

   lp_build_name(res, "context.texture%u.%s", unit, member_name);

   return res;
}


/**
 * Helper macro to instantiate the functions that generate the code to fetch
 * the members of lp_jit_texture to fulfill the sampler code generator requests.
 *
 * This complexity is the price we have to pay to keep the texture sampler code
 * generator a reusable module without dependencies to llvmpipe internals.
 */
#define LP_LLVM_TEXTURE_MEMBER(_name, _index) \
   static LLVMValueRef \
   lp_llvm_texture_##_name( struct lp_sampler_dynamic_state *base, \
                            LLVMBuilderRef builder, \
                            unsigned unit) \
   { \
      return lp_llvm_texture_member(base, builder, unit, _index, #_name ); \
   }


LP_LLVM_TEXTURE_MEMBER(width,    LP_JIT_TEXTURE_WIDTH)
LP_LLVM_TEXTURE_MEMBER(height,   LP_JIT_TEXTURE_HEIGHT)
LP_LLVM_TEXTURE_MEMBER(stride,   LP_JIT_TEXTURE_STRIDE)
LP_LLVM_TEXTURE_MEMBER(data_ptr, LP_JIT_TEXTURE_DATA)


static void
lp_llvm_sampler_soa_destroy(struct lp_build_sampler_soa *sampler)
{
   FREE(sampler);
}


static void
lp_llvm_sampler_soa_emit_fetch_texel(struct lp_build_sampler_soa *base,
                                     LLVMBuilderRef builder,
                                     struct lp_type type,
                                     unsigned unit,
                                     unsigned num_coords,
                                     const LLVMValueRef *coords,
                                     LLVMValueRef lodbias,
                                     LLVMValueRef *texel)
{
   struct lp_llvm_sampler_soa *sampler = (struct lp_llvm_sampler_soa *)base;

   assert(unit < PIPE_MAX_SAMPLERS);

   lp_build_sample_soa(builder,
                       &sampler->dynamic_state.static_state[unit],
                       &sampler->dynamic_state.base,
                       type,
                       unit,
                       num_coords,
                       coords,
                       lodbias,
                       texel);
}


struct lp_build_sampler_soa *
lp_llvm_sampler_soa_create(const struct lp_sampler_static_state *static_state,
                           LLVMValueRef context_ptr)
{
   struct lp_llvm_sampler_soa *sampler;

   sampler = CALLOC_STRUCT(lp_llvm_sampler_soa);
   if(!sampler)
      return NULL;

   sampler->base.destroy = lp_llvm_sampler_soa_destroy;
   sampler->base.emit_fetch_texel = lp_llvm_sampler_soa_emit_fetch_texel;
   sampler->dynamic_state.base.width = lp_llvm_texture_width;
   sampler->dynamic_state.base.height = lp_llvm_texture_height;
   sampler->dynamic_state.base.stride = lp_llvm_texture_stride;
   sampler->dynamic_state.base.data_ptr = lp_llvm_texture_data_ptr;
   sampler->dynamic_state.static_state = static_state;
   sampler->dynamic_state.context_ptr = context_ptr;

   return &sampler->base;
}