1/**************************************************************************
2 *
3 * Copyright 2010 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#ifndef DRAW_LLVM_H
29#define DRAW_LLVM_H
30
31#include "draw/draw_private.h"
32
33#include "draw/draw_vs.h"
34#include "gallivm/lp_bld_sample.h"
35
36#include "pipe/p_context.h"
37#include "util/u_simple_list.h"
38
39
40struct draw_llvm;
41struct llvm_vertex_shader;
42
43struct draw_jit_texture
44{
45   uint32_t width;
46   uint32_t height;
47   uint32_t depth;
48   uint32_t first_level;
49   uint32_t last_level;
50   uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
51   uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
52   const void *data[PIPE_MAX_TEXTURE_LEVELS];
53   float min_lod;
54   float max_lod;
55   float lod_bias;
56   float border_color[4];
57};
58
59enum {
60   DRAW_JIT_TEXTURE_WIDTH = 0,
61   DRAW_JIT_TEXTURE_HEIGHT,
62   DRAW_JIT_TEXTURE_DEPTH,
63   DRAW_JIT_TEXTURE_FIRST_LEVEL,
64   DRAW_JIT_TEXTURE_LAST_LEVEL,
65   DRAW_JIT_TEXTURE_ROW_STRIDE,
66   DRAW_JIT_TEXTURE_IMG_STRIDE,
67   DRAW_JIT_TEXTURE_DATA,
68   DRAW_JIT_TEXTURE_MIN_LOD,
69   DRAW_JIT_TEXTURE_MAX_LOD,
70   DRAW_JIT_TEXTURE_LOD_BIAS,
71   DRAW_JIT_TEXTURE_BORDER_COLOR,
72   DRAW_JIT_TEXTURE_NUM_FIELDS  /* number of fields above */
73};
74
75enum {
76   DRAW_JIT_VERTEX_VERTEX_ID = 0,
77   DRAW_JIT_VERTEX_CLIP,
78   DRAW_JIT_VERTEX_PRE_CLIP_POS,
79   DRAW_JIT_VERTEX_DATA
80};
81
82/**
83 * This structure is passed directly to the generated vertex shader.
84 *
85 * It contains the derived state.
86 *
87 * Changes here must be reflected in the draw_jit_context_* macros.
88 * Changes to the ordering should be avoided.
89 *
90 * Only use types with a clear size and padding here, in particular prefer the
91 * stdint.h types to the basic integer types.
92 */
93struct draw_jit_context
94{
95   const float *vs_constants;
96   const float *gs_constants;
97   float (*planes) [DRAW_TOTAL_CLIP_PLANES][4];
98   float *viewport;
99
100   struct draw_jit_texture textures[PIPE_MAX_SAMPLERS];
101};
102
103
104#define draw_jit_context_vs_constants(_gallivm, _ptr) \
105   lp_build_struct_get(_gallivm, _ptr, 0, "vs_constants")
106
107#define draw_jit_context_gs_constants(_gallivm, _ptr) \
108   lp_build_struct_get(_gallivm, _ptr, 1, "gs_constants")
109
110#define draw_jit_context_planes(_gallivm, _ptr) \
111   lp_build_struct_get(_gallivm, _ptr, 2, "planes")
112
113#define draw_jit_context_viewport(_gallivm, _ptr) \
114   lp_build_struct_get(_gallivm, _ptr, 3, "viewport")
115
116#define DRAW_JIT_CTX_TEXTURES 4
117
118#define draw_jit_context_textures(_gallivm, _ptr) \
119   lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_TEXTURES, "textures")
120
121#define draw_jit_header_id(_gallivm, _ptr)              \
122   lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_VERTEX_ID, "id")
123
124#define draw_jit_header_clip(_gallivm, _ptr) \
125   lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_CLIP, "clip")
126
127#define draw_jit_header_pre_clip_pos(_gallivm, _ptr) \
128   lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_PRE_CLIP_POS, "pre_clip_pos")
129
130#define draw_jit_header_data(_gallivm, _ptr)            \
131   lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_DATA, "data")
132
133
134#define draw_jit_vbuffer_stride(_gallivm, _ptr)         \
135   lp_build_struct_get(_gallivm, _ptr, 0, "stride")
136
137#define draw_jit_vbuffer_offset(_gallivm, _ptr)         \
138   lp_build_struct_get(_gallivm, _ptr, 1, "buffer_offset")
139
140
141typedef int
142(*draw_jit_vert_func)(struct draw_jit_context *context,
143                      struct vertex_header *io,
144                      const char *vbuffers[PIPE_MAX_ATTRIBS],
145                      unsigned start,
146                      unsigned count,
147                      unsigned stride,
148                      struct pipe_vertex_buffer *vertex_buffers,
149                      unsigned instance_id);
150
151
152typedef int
153(*draw_jit_vert_func_elts)(struct draw_jit_context *context,
154                           struct vertex_header *io,
155                           const char *vbuffers[PIPE_MAX_ATTRIBS],
156                           const unsigned *fetch_elts,
157                           unsigned fetch_count,
158                           unsigned stride,
159                           struct pipe_vertex_buffer *vertex_buffers,
160                           unsigned instance_id);
161
162struct draw_llvm_variant_key
163{
164   unsigned nr_vertex_elements:8;
165   unsigned nr_samplers:8;
166   unsigned clamp_vertex_color:1;
167   unsigned clip_xy:1;
168   unsigned clip_z:1;
169   unsigned clip_user:1;
170   unsigned clip_halfz:1;
171   unsigned bypass_viewport:1;
172   unsigned need_edgeflags:1;
173   unsigned ucp_enable:PIPE_MAX_CLIP_PLANES;
174   unsigned pad:9-PIPE_MAX_CLIP_PLANES;
175
176   /* Variable number of vertex elements:
177    */
178   struct pipe_vertex_element vertex_element[1];
179
180   /* Followed by variable number of samplers:
181    */
182/*   struct lp_sampler_static_state sampler; */
183};
184
185#define DRAW_LLVM_MAX_VARIANT_KEY_SIZE \
186   (sizeof(struct draw_llvm_variant_key) +	\
187    PIPE_MAX_SAMPLERS * sizeof(struct lp_sampler_static_state) +	\
188    (PIPE_MAX_ATTRIBS-1) * sizeof(struct pipe_vertex_element))
189
190
191static INLINE size_t
192draw_llvm_variant_key_size(unsigned nr_vertex_elements,
193			   unsigned nr_samplers)
194{
195   return (sizeof(struct draw_llvm_variant_key) +
196	   nr_samplers * sizeof(struct lp_sampler_static_state) +
197	   (nr_vertex_elements - 1) * sizeof(struct pipe_vertex_element));
198}
199
200
201static INLINE struct lp_sampler_static_state *
202draw_llvm_variant_key_samplers(struct draw_llvm_variant_key *key)
203{
204   return (struct lp_sampler_static_state *)
205      &key->vertex_element[key->nr_vertex_elements];
206}
207
208
209
210struct draw_llvm_variant_list_item
211{
212   struct draw_llvm_variant *base;
213   struct draw_llvm_variant_list_item *next, *prev;
214};
215
216struct draw_llvm_variant
217{
218   struct gallivm_state *gallivm;
219
220   /* LLVM JIT builder types */
221   LLVMTypeRef context_ptr_type;
222   LLVMTypeRef buffer_ptr_type;
223   LLVMTypeRef vb_ptr_type;
224   LLVMTypeRef vertex_header_ptr_type;
225
226   LLVMValueRef function;
227   LLVMValueRef function_elts;
228   draw_jit_vert_func jit_func;
229   draw_jit_vert_func_elts jit_func_elts;
230
231   struct llvm_vertex_shader *shader;
232
233   struct draw_llvm *llvm;
234   struct draw_llvm_variant_list_item list_item_global;
235   struct draw_llvm_variant_list_item list_item_local;
236
237   /* key is variable-sized, must be last */
238   struct draw_llvm_variant_key key;
239};
240
241struct llvm_vertex_shader {
242   struct draw_vertex_shader base;
243
244   unsigned variant_key_size;
245   struct draw_llvm_variant_list_item variants;
246   unsigned variants_created;
247   unsigned variants_cached;
248};
249
250struct draw_llvm {
251   struct draw_context *draw;
252
253   struct draw_jit_context jit_context;
254
255   struct draw_llvm_variant_list_item vs_variants_list;
256   int nr_variants;
257};
258
259
260static INLINE struct llvm_vertex_shader *
261llvm_vertex_shader(struct draw_vertex_shader *vs)
262{
263   return (struct llvm_vertex_shader *)vs;
264}
265
266
267struct draw_llvm *
268draw_llvm_create(struct draw_context *draw);
269
270void
271draw_llvm_destroy(struct draw_llvm *llvm);
272
273struct draw_llvm_variant *
274draw_llvm_create_variant(struct draw_llvm *llvm,
275			 unsigned num_vertex_header_attribs,
276			 const struct draw_llvm_variant_key *key);
277
278void
279draw_llvm_destroy_variant(struct draw_llvm_variant *variant);
280
281struct draw_llvm_variant_key *
282draw_llvm_make_variant_key(struct draw_llvm *llvm, char *store);
283
284struct lp_build_sampler_soa *
285draw_llvm_sampler_soa_create(const struct lp_sampler_static_state *static_state,
286                             LLVMValueRef context_ptr);
287
288void
289draw_llvm_set_sampler_state(struct draw_context *draw);
290
291void
292draw_llvm_set_mapped_texture(struct draw_context *draw,
293                             unsigned sampler_idx,
294                             uint32_t width, uint32_t height, uint32_t depth,
295                             uint32_t first_level, uint32_t last_level,
296                             uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],
297                             uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],
298                             const void *data[PIPE_MAX_TEXTURE_LEVELS]);
299
300#endif
301