lp_bld_tgsi.h revision 1d6f3543a063ab9e740fd0c149dcce26c282d773
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 * @file
30 * TGSI to LLVM IR translation.
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
35#ifndef LP_BLD_TGSI_H
36#define LP_BLD_TGSI_H
37
38#include "gallivm/lp_bld.h"
39#include "pipe/p_compiler.h"
40#include "pipe/p_state.h"
41#include "tgsi/tgsi_scan.h"
42
43
44struct tgsi_token;
45struct tgsi_shader_info;
46struct lp_type;
47struct lp_build_context;
48struct lp_build_mask_context;
49
50
51enum lp_build_tex_modifier {
52   LP_BLD_TEX_MODIFIER_NONE = 0,
53   LP_BLD_TEX_MODIFIER_PROJECTED,
54   LP_BLD_TEX_MODIFIER_LOD_BIAS,
55   LP_BLD_TEX_MODIFIER_EXPLICIT_LOD,
56   LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
57};
58
59
60/**
61 * Describe a channel of a register.
62 *
63 * The value can be a:
64 * - immediate value (i.e. derived from a IMM register)
65 * - CONST[n].x/y/z/w
66 * - IN[n].x/y/z/w
67 * - undetermined (when .file == TGSI_FILE_NULL)
68 *
69 * This is one of the analysis results, and is used to described
70 * the output color in terms of inputs.
71 */
72struct lp_tgsi_channel_info
73{
74   unsigned file:4; /* TGSI_FILE_* */
75   unsigned swizzle:3; /* PIPE_SWIZZLE_x */
76   union {
77      uint32_t index;
78      float value; /* for TGSI_FILE_IMMEDIATE */
79   } u;
80};
81
82
83/**
84 * Describe a texture sampler interpolator.
85 *
86 * The interpolation is described in terms of regular inputs.
87 */
88struct lp_tgsi_texture_info
89{
90   struct lp_tgsi_channel_info coord[4];
91   unsigned target:8; /* TGSI_TEXTURE_* */
92   unsigned unit:8;  /* Sampler unit */
93   unsigned modifier:8; /* LP_BLD_TEX_MODIFIER_* */
94};
95
96
97struct lp_tgsi_info
98{
99   struct tgsi_shader_info base;
100
101   /*
102    * Whether any of the texture opcodes access a register file other than
103    * TGSI_FILE_INPUT.
104    *
105    * We could also handle TGSI_FILE_CONST/IMMEDIATE here, but there is little
106    * benefit.
107    */
108   unsigned indirect_textures:1;
109
110   /*
111    * Texture opcode description. Aimed at detecting and described direct
112    * texture opcodes.
113    */
114   unsigned num_texs;
115   struct lp_tgsi_texture_info tex[PIPE_MAX_SAMPLERS];
116
117   /*
118    * Output description. Aimed at detecting and describing simple blit
119    * shaders.
120    */
121   struct lp_tgsi_channel_info output[PIPE_MAX_SHADER_OUTPUTS][4];
122
123   /*
124    * Shortcut pointers into the above (for fragment shaders).
125    */
126   const struct lp_tgsi_channel_info *cbuf[PIPE_MAX_COLOR_BUFS];
127};
128
129/**
130 * Sampler code generation interface.
131 *
132 * Although texture sampling is a requirement for TGSI translation, it is
133 * a very different problem with several different approaches to it. This
134 * structure establishes an interface for texture sampling code generation, so
135 * that we can easily use different texture sampling strategies.
136 */
137struct lp_build_sampler_soa
138{
139   void
140   (*destroy)( struct lp_build_sampler_soa *sampler );
141
142   void
143   (*emit_fetch_texel)( const struct lp_build_sampler_soa *sampler,
144                        LLVMBuilderRef builder,
145                        struct lp_type type,
146                        unsigned unit,
147                        unsigned num_coords,
148                        const LLVMValueRef *coords,
149                        const LLVMValueRef *ddx,
150                        const LLVMValueRef *ddy,
151                        LLVMValueRef lod_bias, /* optional */
152                        LLVMValueRef explicit_lod, /* optional */
153                        LLVMValueRef *texel);
154};
155
156
157struct lp_build_sampler_aos
158{
159   LLVMValueRef
160   (*emit_fetch_texel)( struct lp_build_sampler_aos *sampler,
161                        struct lp_build_context *bld,
162                        unsigned target, /* TGSI_TEXTURE_* */
163                        unsigned unit,
164                        LLVMValueRef coords,
165                        LLVMValueRef ddx,
166                        LLVMValueRef ddy,
167                        enum lp_build_tex_modifier modifier);
168};
169
170
171void
172lp_build_tgsi_info(const struct tgsi_token *tokens,
173                   struct lp_tgsi_info *info);
174
175
176void
177lp_build_tgsi_soa(LLVMBuilderRef builder,
178                  const struct tgsi_token *tokens,
179                  struct lp_type type,
180                  struct lp_build_mask_context *mask,
181                  LLVMValueRef consts_ptr,
182                  LLVMValueRef system_values_array,
183                  const LLVMValueRef *pos,
184                  const LLVMValueRef (*inputs)[4],
185                  LLVMValueRef (*outputs)[4],
186                  struct lp_build_sampler_soa *sampler,
187                  const struct tgsi_shader_info *info);
188
189
190void
191lp_build_tgsi_aos(LLVMBuilderRef builder,
192                  const struct tgsi_token *tokens,
193                  struct lp_type type,
194                  const unsigned char swizzles[4],
195                  LLVMValueRef consts_ptr,
196                  const LLVMValueRef *inputs,
197                  LLVMValueRef *outputs,
198                  struct lp_build_sampler_aos *sampler,
199                  const struct tgsi_shader_info *info);
200
201
202LLVMValueRef
203lp_build_system_values_array(LLVMBuilderRef builder,
204                             const struct tgsi_shader_info *info,
205                             LLVMValueRef instance_id,
206                             LLVMValueRef facing);
207
208
209#endif /* LP_BLD_TGSI_H */
210