gallivm: add compute jit interface.
[mesa.git] / src / gallium / drivers / llvmpipe / lp_jit.h
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 * C - JIT interfaces
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
35 #ifndef LP_JIT_H
36 #define LP_JIT_H
37
38
39 #include "gallivm/lp_bld_struct.h"
40 #include "gallivm/lp_bld_limits.h"
41
42 #include "pipe/p_state.h"
43 #include "lp_texture.h"
44
45
46 struct lp_build_format_cache;
47 struct lp_fragment_shader_variant;
48 struct lp_compute_shader_variant;
49 struct llvmpipe_screen;
50
51
52 struct lp_jit_texture
53 {
54 uint32_t width; /* same as number of elements */
55 uint32_t height;
56 uint32_t depth; /* doubles as array size */
57 const void *base;
58 uint32_t row_stride[LP_MAX_TEXTURE_LEVELS];
59 uint32_t img_stride[LP_MAX_TEXTURE_LEVELS];
60 uint32_t first_level;
61 uint32_t last_level;
62 uint32_t mip_offsets[LP_MAX_TEXTURE_LEVELS];
63 };
64
65
66 struct lp_jit_sampler
67 {
68 float min_lod;
69 float max_lod;
70 float lod_bias;
71 float border_color[4];
72 };
73
74
75 struct lp_jit_viewport
76 {
77 float min_depth;
78 float max_depth;
79 };
80
81
82 struct lp_jit_image
83 {
84 uint32_t width; /* same as number of elements */
85 uint32_t height;
86 uint32_t depth;
87 const void *base;
88 uint32_t row_stride;
89 uint32_t img_stride;
90 };
91
92 enum {
93 LP_JIT_TEXTURE_WIDTH = 0,
94 LP_JIT_TEXTURE_HEIGHT,
95 LP_JIT_TEXTURE_DEPTH,
96 LP_JIT_TEXTURE_BASE,
97 LP_JIT_TEXTURE_ROW_STRIDE,
98 LP_JIT_TEXTURE_IMG_STRIDE,
99 LP_JIT_TEXTURE_FIRST_LEVEL,
100 LP_JIT_TEXTURE_LAST_LEVEL,
101 LP_JIT_TEXTURE_MIP_OFFSETS,
102 LP_JIT_TEXTURE_NUM_FIELDS /* number of fields above */
103 };
104
105
106 enum {
107 LP_JIT_SAMPLER_MIN_LOD,
108 LP_JIT_SAMPLER_MAX_LOD,
109 LP_JIT_SAMPLER_LOD_BIAS,
110 LP_JIT_SAMPLER_BORDER_COLOR,
111 LP_JIT_SAMPLER_NUM_FIELDS /* number of fields above */
112 };
113
114
115 enum {
116 LP_JIT_VIEWPORT_MIN_DEPTH,
117 LP_JIT_VIEWPORT_MAX_DEPTH,
118 LP_JIT_VIEWPORT_NUM_FIELDS /* number of fields above */
119 };
120
121 enum {
122 LP_JIT_IMAGE_WIDTH = 0,
123 LP_JIT_IMAGE_HEIGHT,
124 LP_JIT_IMAGE_DEPTH,
125 LP_JIT_IMAGE_BASE,
126 LP_JIT_IMAGE_ROW_STRIDE,
127 LP_JIT_IMAGE_IMG_STRIDE,
128 LP_JIT_IMAGE_NUM_FIELDS /* number of fields above */
129 };
130 /**
131 * This structure is passed directly to the generated fragment shader.
132 *
133 * It contains the derived state.
134 *
135 * Changes here must be reflected in the lp_jit_context_* macros and
136 * lp_jit_init_types function. Changes to the ordering should be avoided.
137 *
138 * Only use types with a clear size and padding here, in particular prefer the
139 * stdint.h types to the basic integer types.
140 */
141 struct lp_jit_context
142 {
143 const float *constants[LP_MAX_TGSI_CONST_BUFFERS];
144 int num_constants[LP_MAX_TGSI_CONST_BUFFERS];
145
146 struct lp_jit_texture textures[PIPE_MAX_SHADER_SAMPLER_VIEWS];
147 struct lp_jit_sampler samplers[PIPE_MAX_SAMPLERS];
148 struct lp_jit_image images[PIPE_MAX_SHADER_IMAGES];
149
150 float alpha_ref_value;
151
152 uint32_t stencil_ref_front, stencil_ref_back;
153
154 uint8_t *u8_blend_color;
155 float *f_blend_color;
156
157 struct lp_jit_viewport *viewports;
158
159 const uint32_t *ssbos[LP_MAX_TGSI_SHADER_BUFFERS];
160 int num_ssbos[LP_MAX_TGSI_SHADER_BUFFERS];
161 };
162
163
164 /**
165 * These enum values must match the position of the fields in the
166 * lp_jit_context struct above.
167 */
168 enum {
169 LP_JIT_CTX_CONSTANTS = 0,
170 LP_JIT_CTX_NUM_CONSTANTS,
171 LP_JIT_CTX_TEXTURES,
172 LP_JIT_CTX_SAMPLERS,
173 LP_JIT_CTX_IMAGES,
174 LP_JIT_CTX_ALPHA_REF,
175 LP_JIT_CTX_STENCIL_REF_FRONT,
176 LP_JIT_CTX_STENCIL_REF_BACK,
177 LP_JIT_CTX_U8_BLEND_COLOR,
178 LP_JIT_CTX_F_BLEND_COLOR,
179 LP_JIT_CTX_VIEWPORTS,
180 LP_JIT_CTX_SSBOS,
181 LP_JIT_CTX_NUM_SSBOS,
182 LP_JIT_CTX_COUNT
183 };
184
185
186 #define lp_jit_context_constants(_gallivm, _ptr) \
187 lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CTX_CONSTANTS, "constants")
188
189 #define lp_jit_context_num_constants(_gallivm, _ptr) \
190 lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CTX_NUM_CONSTANTS, "num_constants")
191
192 #define lp_jit_context_textures(_gallivm, _ptr) \
193 lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CTX_TEXTURES, "textures")
194
195 #define lp_jit_context_samplers(_gallivm, _ptr) \
196 lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CTX_SAMPLERS, "samplers")
197
198 #define lp_jit_context_images(_gallivm, _ptr) \
199 lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CTX_IMAGES, "images")
200
201 #define lp_jit_context_alpha_ref_value(_gallivm, _ptr) \
202 lp_build_struct_get(_gallivm, _ptr, LP_JIT_CTX_ALPHA_REF, "alpha_ref_value")
203
204 #define lp_jit_context_stencil_ref_front_value(_gallivm, _ptr) \
205 lp_build_struct_get(_gallivm, _ptr, LP_JIT_CTX_STENCIL_REF_FRONT, "stencil_ref_front")
206
207 #define lp_jit_context_stencil_ref_back_value(_gallivm, _ptr) \
208 lp_build_struct_get(_gallivm, _ptr, LP_JIT_CTX_STENCIL_REF_BACK, "stencil_ref_back")
209
210 #define lp_jit_context_u8_blend_color(_gallivm, _ptr) \
211 lp_build_struct_get(_gallivm, _ptr, LP_JIT_CTX_U8_BLEND_COLOR, "u8_blend_color")
212
213 #define lp_jit_context_f_blend_color(_gallivm, _ptr) \
214 lp_build_struct_get(_gallivm, _ptr, LP_JIT_CTX_F_BLEND_COLOR, "f_blend_color")
215
216 #define lp_jit_context_viewports(_gallivm, _ptr) \
217 lp_build_struct_get(_gallivm, _ptr, LP_JIT_CTX_VIEWPORTS, "viewports")
218
219 #define lp_jit_context_ssbos(_gallivm, _ptr) \
220 lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CTX_SSBOS, "ssbos")
221
222 #define lp_jit_context_num_ssbos(_gallivm, _ptr) \
223 lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CTX_NUM_SSBOS, "num_ssbos")
224
225 struct lp_jit_thread_data
226 {
227 struct lp_build_format_cache *cache;
228 uint64_t vis_counter;
229 uint64_t ps_invocations;
230
231 /*
232 * Non-interpolated rasterizer state passed through to the fragment shader.
233 */
234 struct {
235 uint32_t viewport_index;
236 } raster_state;
237 };
238
239
240 enum {
241 LP_JIT_THREAD_DATA_CACHE = 0,
242 LP_JIT_THREAD_DATA_COUNTER,
243 LP_JIT_THREAD_DATA_INVOCATIONS,
244 LP_JIT_THREAD_DATA_RASTER_STATE_VIEWPORT_INDEX,
245 LP_JIT_THREAD_DATA_COUNT
246 };
247
248
249 #define lp_jit_thread_data_cache(_gallivm, _ptr) \
250 lp_build_struct_get(_gallivm, _ptr, LP_JIT_THREAD_DATA_CACHE, "cache")
251
252 #define lp_jit_thread_data_counter(_gallivm, _ptr) \
253 lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_THREAD_DATA_COUNTER, "counter")
254
255 #define lp_jit_thread_data_invocations(_gallivm, _ptr) \
256 lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_THREAD_DATA_INVOCATIONS, "invocs")
257
258 #define lp_jit_thread_data_raster_state_viewport_index(_gallivm, _ptr) \
259 lp_build_struct_get(_gallivm, _ptr, \
260 LP_JIT_THREAD_DATA_RASTER_STATE_VIEWPORT_INDEX, \
261 "raster_state.viewport_index")
262
263 /**
264 * typedef for fragment shader function
265 *
266 * @param context jit context
267 * @param x block start x
268 * @param y block start y
269 * @param facing is front facing
270 * @param a0 shader input a0
271 * @param dadx shader input dadx
272 * @param dady shader input dady
273 * @param color color buffer
274 * @param depth depth buffer
275 * @param mask mask of visible pixels in block
276 * @param thread_data task thread data
277 * @param stride color buffer row stride in bytes
278 * @param depth_stride depth buffer row stride in bytes
279 */
280 typedef void
281 (*lp_jit_frag_func)(const struct lp_jit_context *context,
282 uint32_t x,
283 uint32_t y,
284 uint32_t facing,
285 const void *a0,
286 const void *dadx,
287 const void *dady,
288 uint8_t **color,
289 uint8_t *depth,
290 uint32_t mask,
291 struct lp_jit_thread_data *thread_data,
292 unsigned *stride,
293 unsigned depth_stride);
294
295
296 struct lp_jit_cs_thread_data
297 {
298 struct lp_build_format_cache *cache;
299 void *shared;
300 };
301
302 enum {
303 LP_JIT_CS_THREAD_DATA_CACHE = 0,
304 LP_JIT_CS_THREAD_DATA_SHARED = 1,
305 LP_JIT_CS_THREAD_DATA_COUNT
306 };
307
308
309 #define lp_jit_cs_thread_data_cache(_gallivm, _ptr) \
310 lp_build_struct_get(_gallivm, _ptr, LP_JIT_CS_THREAD_DATA_CACHE, "cache")
311
312 #define lp_jit_cs_thread_data_shared(_gallivm, _ptr) \
313 lp_build_struct_get(_gallivm, _ptr, LP_JIT_CS_THREAD_DATA_SHARED, "shared")
314
315 struct lp_jit_cs_context
316 {
317 const float *constants[LP_MAX_TGSI_CONST_BUFFERS];
318 int num_constants[LP_MAX_TGSI_CONST_BUFFERS];
319
320 struct lp_jit_texture textures[PIPE_MAX_SHADER_SAMPLER_VIEWS];
321 struct lp_jit_sampler samplers[PIPE_MAX_SAMPLERS];
322 struct lp_jit_image images[PIPE_MAX_SHADER_IMAGES];
323
324 const uint32_t *ssbos[LP_MAX_TGSI_SHADER_BUFFERS];
325 int num_ssbos[LP_MAX_TGSI_SHADER_BUFFERS];
326
327 uint32_t shared_size;
328 };
329
330 /**
331 * These enum values must match the position of the fields in the
332 * lp_jit_context struct above.
333 */
334 enum {
335 LP_JIT_CS_CTX_CONSTANTS = 0,
336 LP_JIT_CS_CTX_NUM_CONSTANTS,
337 LP_JIT_CS_CTX_TEXTURES, /* must match the LP_JIT_CTX_TEXTURES */
338 LP_JIT_CS_CTX_SAMPLERS,
339 LP_JIT_CS_CTX_IMAGES,
340 LP_JIT_CS_CTX_SSBOS,
341 LP_JIT_CS_CTX_NUM_SSBOS,
342 LP_JIT_CS_CTX_SHARED_SIZE,
343 LP_JIT_CS_CTX_COUNT
344 };
345
346 #define lp_jit_cs_context_constants(_gallivm, _ptr) \
347 lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CS_CTX_CONSTANTS, "constants")
348
349 #define lp_jit_cs_context_num_constants(_gallivm, _ptr) \
350 lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CS_CTX_NUM_CONSTANTS, "num_constants")
351
352 #define lp_jit_cs_context_textures(_gallivm, _ptr) \
353 lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CS_CTX_TEXTURES, "textures")
354
355 #define lp_jit_cs_context_samplers(_gallivm, _ptr) \
356 lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CS_CTX_SAMPLERS, "samplers")
357
358 #define lp_jit_cs_context_images(_gallivm, _ptr) \
359 lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CS_CTX_IMAGES, "images")
360
361 #define lp_jit_cs_context_ssbos(_gallivm, _ptr) \
362 lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CS_CTX_SSBOS, "ssbos")
363
364 #define lp_jit_cs_context_num_ssbos(_gallivm, _ptr) \
365 lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CS_CTX_NUM_SSBOS, "num_ssbos")
366
367 #define lp_jit_cs_context_shared_size(_gallivm, _ptr) \
368 lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CS_CTX_SHARED_SIZE, "shared_size")
369
370
371 typedef void
372 (*lp_jit_cs_func)(const struct lp_jit_cs_context *context,
373 uint32_t x,
374 uint32_t y,
375 uint32_t z,
376 uint32_t grid_x,
377 uint32_t grid_y,
378 uint32_t grid_z,
379 uint32_t grid_size_x,
380 uint32_t grid_size_y,
381 uint32_t grid_size_z,
382 struct lp_jit_cs_thread_data *thread_data);
383
384 void
385 lp_jit_screen_cleanup(struct llvmpipe_screen *screen);
386
387
388 boolean
389 lp_jit_screen_init(struct llvmpipe_screen *screen);
390
391
392 void
393 lp_jit_init_types(struct lp_fragment_shader_variant *lp);
394
395 void
396 lp_jit_init_cs_types(struct lp_compute_shader_variant *lp);
397 #endif /* LP_JIT_H */