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    uint32_t num_samples;
64    uint32_t sample_stride;
65 };
66 
67 
68 struct lp_jit_sampler
69 {
70    float min_lod;
71    float max_lod;
72    float lod_bias;
73    float border_color[4];
74    float max_aniso;
75 };
76 
77 
78 struct lp_jit_viewport
79 {
80    float min_depth;
81    float max_depth;
82 };
83 
84 
85 struct lp_jit_image
86 {
87    uint32_t width;        /* same as number of elements */
88    uint32_t height;
89    uint32_t depth;
90    const void *base;
91    uint32_t row_stride;
92    uint32_t img_stride;
93    uint32_t num_samples;
94    uint32_t sample_stride;
95 };
96 
97 enum {
98    LP_JIT_TEXTURE_WIDTH = 0,
99    LP_JIT_TEXTURE_HEIGHT,
100    LP_JIT_TEXTURE_DEPTH,
101    LP_JIT_TEXTURE_BASE,
102    LP_JIT_TEXTURE_ROW_STRIDE,
103    LP_JIT_TEXTURE_IMG_STRIDE,
104    LP_JIT_TEXTURE_FIRST_LEVEL,
105    LP_JIT_TEXTURE_LAST_LEVEL,
106    LP_JIT_TEXTURE_MIP_OFFSETS,
107    LP_JIT_TEXTURE_NUM_SAMPLES,
108    LP_JIT_TEXTURE_SAMPLE_STRIDE,
109    LP_JIT_TEXTURE_NUM_FIELDS  /* number of fields above */
110 };
111 
112 
113 enum {
114    LP_JIT_SAMPLER_MIN_LOD,
115    LP_JIT_SAMPLER_MAX_LOD,
116    LP_JIT_SAMPLER_LOD_BIAS,
117    LP_JIT_SAMPLER_BORDER_COLOR,
118    LP_JIT_SAMPLER_MAX_ANISO,
119    LP_JIT_SAMPLER_NUM_FIELDS  /* number of fields above */
120 };
121 
122 
123 enum {
124    LP_JIT_VIEWPORT_MIN_DEPTH,
125    LP_JIT_VIEWPORT_MAX_DEPTH,
126    LP_JIT_VIEWPORT_NUM_FIELDS /* number of fields above */
127 };
128 
129 enum {
130    LP_JIT_IMAGE_WIDTH = 0,
131    LP_JIT_IMAGE_HEIGHT,
132    LP_JIT_IMAGE_DEPTH,
133    LP_JIT_IMAGE_BASE,
134    LP_JIT_IMAGE_ROW_STRIDE,
135    LP_JIT_IMAGE_IMG_STRIDE,
136    LP_JIT_IMAGE_NUM_SAMPLES,
137    LP_JIT_IMAGE_SAMPLE_STRIDE,
138    LP_JIT_IMAGE_NUM_FIELDS  /* number of fields above */
139 };
140 /**
141  * This structure is passed directly to the generated fragment shader.
142  *
143  * It contains the derived state.
144  *
145  * Changes here must be reflected in the lp_jit_context_* macros and
146  * lp_jit_init_types function. Changes to the ordering should be avoided.
147  *
148  * Only use types with a clear size and padding here, in particular prefer the
149  * stdint.h types to the basic integer types.
150  */
151 struct lp_jit_context
152 {
153    const float *constants[LP_MAX_TGSI_CONST_BUFFERS];
154    int num_constants[LP_MAX_TGSI_CONST_BUFFERS];
155 
156    struct lp_jit_texture textures[PIPE_MAX_SHADER_SAMPLER_VIEWS];
157    struct lp_jit_sampler samplers[PIPE_MAX_SAMPLERS];
158    struct lp_jit_image images[PIPE_MAX_SHADER_IMAGES];
159 
160    float alpha_ref_value;
161 
162    uint32_t stencil_ref_front, stencil_ref_back;
163 
164    uint8_t *u8_blend_color;
165    float *f_blend_color;
166 
167    struct lp_jit_viewport *viewports;
168 
169    const uint32_t *ssbos[LP_MAX_TGSI_SHADER_BUFFERS];
170    int num_ssbos[LP_MAX_TGSI_SHADER_BUFFERS];
171 
172    uint32_t sample_mask;
173 
174    const float *aniso_filter_table;
175 };
176 
177 
178 /**
179  * These enum values must match the position of the fields in the
180  * lp_jit_context struct above.
181  */
182 enum {
183    LP_JIT_CTX_CONSTANTS = 0,
184    LP_JIT_CTX_NUM_CONSTANTS,
185    LP_JIT_CTX_TEXTURES,
186    LP_JIT_CTX_SAMPLERS,
187    LP_JIT_CTX_IMAGES,
188    LP_JIT_CTX_ALPHA_REF,
189    LP_JIT_CTX_STENCIL_REF_FRONT,
190    LP_JIT_CTX_STENCIL_REF_BACK,
191    LP_JIT_CTX_U8_BLEND_COLOR,
192    LP_JIT_CTX_F_BLEND_COLOR,
193    LP_JIT_CTX_VIEWPORTS,
194    LP_JIT_CTX_SSBOS,
195    LP_JIT_CTX_NUM_SSBOS,
196    LP_JIT_CTX_SAMPLE_MASK,
197    LP_JIT_CTX_ANISO_FILTER_TABLE,
198    LP_JIT_CTX_COUNT
199 };
200 
201 
202 #define lp_jit_context_constants(_gallivm, _ptr) \
203    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CTX_CONSTANTS, "constants")
204 
205 #define lp_jit_context_num_constants(_gallivm, _ptr) \
206    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CTX_NUM_CONSTANTS, "num_constants")
207 
208 #define lp_jit_context_textures(_gallivm, _ptr) \
209    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CTX_TEXTURES, "textures")
210 
211 #define lp_jit_context_samplers(_gallivm, _ptr) \
212    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CTX_SAMPLERS, "samplers")
213 
214 #define lp_jit_context_images(_gallivm, _ptr) \
215    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CTX_IMAGES, "images")
216 
217 #define lp_jit_context_alpha_ref_value(_gallivm, _ptr) \
218    lp_build_struct_get(_gallivm, _ptr, LP_JIT_CTX_ALPHA_REF, "alpha_ref_value")
219 
220 #define lp_jit_context_stencil_ref_front_value(_gallivm, _ptr) \
221    lp_build_struct_get(_gallivm, _ptr, LP_JIT_CTX_STENCIL_REF_FRONT, "stencil_ref_front")
222 
223 #define lp_jit_context_stencil_ref_back_value(_gallivm, _ptr) \
224    lp_build_struct_get(_gallivm, _ptr, LP_JIT_CTX_STENCIL_REF_BACK, "stencil_ref_back")
225 
226 #define lp_jit_context_u8_blend_color(_gallivm, _ptr) \
227    lp_build_struct_get(_gallivm, _ptr, LP_JIT_CTX_U8_BLEND_COLOR, "u8_blend_color")
228 
229 #define lp_jit_context_f_blend_color(_gallivm, _ptr) \
230    lp_build_struct_get(_gallivm, _ptr, LP_JIT_CTX_F_BLEND_COLOR, "f_blend_color")
231 
232 #define lp_jit_context_viewports(_gallivm, _ptr) \
233    lp_build_struct_get(_gallivm, _ptr, LP_JIT_CTX_VIEWPORTS, "viewports")
234 
235 #define lp_jit_context_ssbos(_gallivm, _ptr) \
236    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CTX_SSBOS, "ssbos")
237 
238 #define lp_jit_context_num_ssbos(_gallivm, _ptr) \
239    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CTX_NUM_SSBOS, "num_ssbos")
240 
241 #define lp_jit_context_sample_mask(_gallivm, _ptr) \
242    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CTX_SAMPLE_MASK, "sample_mask")
243 
244 #define lp_jit_context_aniso_filter_table(_gallivm, _ptr) \
245    lp_build_struct_get(_gallivm, _ptr, LP_JIT_CTX_ANISO_FILTER_TABLE, "aniso_filter_table")
246 
247 struct lp_jit_thread_data
248 {
249    struct lp_build_format_cache *cache;
250    uint64_t vis_counter;
251    uint64_t ps_invocations;
252 
253    /*
254     * Non-interpolated rasterizer state passed through to the fragment shader.
255     */
256    struct {
257       uint32_t viewport_index;
258       uint32_t view_index;
259    } raster_state;
260 };
261 
262 
263 enum {
264    LP_JIT_THREAD_DATA_CACHE = 0,
265    LP_JIT_THREAD_DATA_COUNTER,
266    LP_JIT_THREAD_DATA_INVOCATIONS,
267    LP_JIT_THREAD_DATA_RASTER_STATE_VIEWPORT_INDEX,
268    LP_JIT_THREAD_DATA_RASTER_STATE_VIEW_INDEX,
269    LP_JIT_THREAD_DATA_COUNT
270 };
271 
272 
273 #define lp_jit_thread_data_cache(_gallivm, _ptr) \
274    lp_build_struct_get(_gallivm, _ptr, LP_JIT_THREAD_DATA_CACHE, "cache")
275 
276 #define lp_jit_thread_data_counter(_gallivm, _ptr) \
277    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_THREAD_DATA_COUNTER, "counter")
278 
279 #define lp_jit_thread_data_invocations(_gallivm, _ptr) \
280    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_THREAD_DATA_INVOCATIONS, "invocs")
281 
282 #define lp_jit_thread_data_raster_state_viewport_index(_gallivm, _ptr) \
283    lp_build_struct_get(_gallivm, _ptr, \
284                        LP_JIT_THREAD_DATA_RASTER_STATE_VIEWPORT_INDEX, \
285                        "raster_state.viewport_index")
286 
287 #define lp_jit_thread_data_raster_state_view_index(_gallivm, _ptr) \
288    lp_build_struct_get(_gallivm, _ptr, \
289                        LP_JIT_THREAD_DATA_RASTER_STATE_VIEW_INDEX, \
290                        "raster_state.view_index")
291 
292 /**
293  * typedef for fragment shader function
294  *
295  * @param context       jit context
296  * @param x             block start x
297  * @param y             block start y
298  * @param facing        is front facing
299  * @param a0            shader input a0
300  * @param dadx          shader input dadx
301  * @param dady          shader input dady
302  * @param color         color buffer
303  * @param depth         depth buffer
304  * @param mask          mask of visible pixels in block (16-bits per sample)
305  * @param thread_data   task thread data
306  * @param stride        color buffer row stride in bytes
307  * @param depth_stride  depth buffer row stride in bytes
308  */
309 typedef void
310 (*lp_jit_frag_func)(const struct lp_jit_context *context,
311                     uint32_t x,
312                     uint32_t y,
313                     uint32_t facing,
314                     const void *a0,
315                     const void *dadx,
316                     const void *dady,
317                     uint8_t **color,
318                     uint8_t *depth,
319                     uint64_t mask,
320                     struct lp_jit_thread_data *thread_data,
321                     unsigned *stride,
322                     unsigned depth_stride,
323                     unsigned *color_sample_stride,
324                     unsigned depth_sample_stride);
325 
326 
327 #define LP_MAX_LINEAR_CONSTANTS 16
328 #define LP_MAX_LINEAR_TEXTURES 2
329 #define LP_MAX_LINEAR_INPUTS 8
330 
331 
332 /**
333  * This structure is passed directly to the generated fragment shader.
334  *
335  * It contains the derived state.
336  *
337  * Changes here must be reflected in the lp_jit_linear_context_* macros and
338  * lp_jit_init_types function. Changes to the ordering should be avoided.
339  *
340  * Only use types with a clear size and padding here, in particular prefer the
341  * stdint.h types to the basic integer types.
342  */
343 struct lp_jit_linear_context
344 {
345    /**
346     * Constants in 8bit unorm values.
347     */
348    const uint8_t (*constants)[4];
349    struct lp_linear_elem *tex[LP_MAX_LINEAR_TEXTURES];
350    struct lp_linear_elem *inputs[LP_MAX_LINEAR_INPUTS];
351 
352    uint8_t *color0;
353    uint32_t blend_color;
354 
355    uint8_t alpha_ref_value;
356 };
357 
358 
359 /**
360  * These enum values must match the position of the fields in the
361  * lp_jit_linear_context struct above.
362  */
363 enum {
364    LP_JIT_LINEAR_CTX_CONSTANTS = 0,
365    LP_JIT_LINEAR_CTX_TEX,
366    LP_JIT_LINEAR_CTX_INPUTS,
367    LP_JIT_LINEAR_CTX_COLOR0,
368    LP_JIT_LINEAR_CTX_BLEND_COLOR,
369    LP_JIT_LINEAR_CTX_ALPHA_REF,
370    LP_JIT_LINEAR_CTX_COUNT
371 };
372 
373 
374 #define lp_jit_linear_context_constants(_gallivm, _ptr) \
375    lp_build_struct_get(_gallivm, _ptr, LP_JIT_LINEAR_CTX_CONSTANTS, "constants")
376 
377 #define lp_jit_linear_context_tex(_gallivm, _ptr) \
378    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_LINEAR_CTX_TEX, "tex")
379 
380 #define lp_jit_linear_context_inputs(_gallivm, _ptr) \
381    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_LINEAR_CTX_INPUTS, "inputs")
382 
383 #define lp_jit_linear_context_color0(_gallivm, _ptr) \
384    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_LINEAR_CTX_COLOR0, "color0")
385 
386 #define lp_jit_linear_context_blend_color(_gallivm, _ptr) \
387    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_LINEAR_CTX_BLEND_COLOR, "blend_color")
388 
389 #define lp_jit_linear_context_alpha_ref(_gallivm, _ptr) \
390    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_LINEAR_CTX_ALPHA_REF, "alpha_ref_value")
391 
392 
393 typedef const uint8_t *
394 (*lp_jit_linear_llvm_func)(struct lp_jit_linear_context *context,
395                            uint32_t x,
396                            uint32_t y,
397                            uint32_t w);
398 
399 /* We're not really jitting this, but I need to get into the
400  * rast_state struct to call the function we actually are jitting.
401  */
402 struct lp_rast_state;
403 typedef boolean
404 (*lp_jit_linear_func)(const struct lp_rast_state *state,
405                       uint32_t x,
406                       uint32_t y,
407                       uint32_t w,
408                       uint32_t h,
409                       const float (*a0)[4],
410                       const float (*dadx)[4],
411                       const float (*dady)[4],
412                       uint8_t *color,
413                       uint32_t color_stride);
414 
415 struct lp_jit_cs_thread_data
416 {
417    struct lp_build_format_cache *cache;
418    void *shared;
419 };
420 
421 enum {
422    LP_JIT_CS_THREAD_DATA_CACHE = 0,
423    LP_JIT_CS_THREAD_DATA_SHARED = 1,
424    LP_JIT_CS_THREAD_DATA_COUNT
425 };
426 
427 
428 #define lp_jit_cs_thread_data_cache(_gallivm, _ptr) \
429    lp_build_struct_get(_gallivm, _ptr, LP_JIT_CS_THREAD_DATA_CACHE, "cache")
430 
431 #define lp_jit_cs_thread_data_shared(_gallivm, _ptr) \
432    lp_build_struct_get(_gallivm, _ptr, LP_JIT_CS_THREAD_DATA_SHARED, "shared")
433 
434 struct lp_jit_cs_context
435 {
436    const float *constants[LP_MAX_TGSI_CONST_BUFFERS];
437    int num_constants[LP_MAX_TGSI_CONST_BUFFERS];
438 
439    struct lp_jit_texture textures[PIPE_MAX_SHADER_SAMPLER_VIEWS];
440    struct lp_jit_sampler samplers[PIPE_MAX_SAMPLERS];
441    struct lp_jit_image images[PIPE_MAX_SHADER_IMAGES];
442 
443    const uint32_t *ssbos[LP_MAX_TGSI_SHADER_BUFFERS];
444    int num_ssbos[LP_MAX_TGSI_SHADER_BUFFERS];
445 
446    void *kernel_args;
447 
448    uint32_t shared_size;
449 
450    const float *aniso_filter_table;
451 };
452 
453 /**
454  * These enum values must match the position of the fields in the
455  * lp_jit_context struct above.
456  */
457 enum {
458    LP_JIT_CS_CTX_CONSTANTS = 0,
459    LP_JIT_CS_CTX_NUM_CONSTANTS,
460    LP_JIT_CS_CTX_TEXTURES, /* must match the LP_JIT_CTX_TEXTURES */
461    LP_JIT_CS_CTX_SAMPLERS,
462    LP_JIT_CS_CTX_IMAGES,
463    LP_JIT_CS_CTX_SSBOS,
464    LP_JIT_CS_CTX_NUM_SSBOS,
465    LP_JIT_CS_CTX_KERNEL_ARGS,
466    LP_JIT_CS_CTX_SHARED_SIZE,
467    LP_JIT_CS_CTX_ANISO_FILTER_TABLE,
468    LP_JIT_CS_CTX_COUNT
469 };
470 
471 #define lp_jit_cs_context_constants(_gallivm, _ptr) \
472    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CS_CTX_CONSTANTS, "constants")
473 
474 #define lp_jit_cs_context_num_constants(_gallivm, _ptr) \
475    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CS_CTX_NUM_CONSTANTS, "num_constants")
476 
477 #define lp_jit_cs_context_textures(_gallivm, _ptr) \
478    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CS_CTX_TEXTURES, "textures")
479 
480 #define lp_jit_cs_context_samplers(_gallivm, _ptr) \
481    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CS_CTX_SAMPLERS, "samplers")
482 
483 #define lp_jit_cs_context_images(_gallivm, _ptr) \
484    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CS_CTX_IMAGES, "images")
485 
486 #define lp_jit_cs_context_ssbos(_gallivm, _ptr) \
487    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CS_CTX_SSBOS, "ssbos")
488 
489 #define lp_jit_cs_context_num_ssbos(_gallivm, _ptr) \
490    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CS_CTX_NUM_SSBOS, "num_ssbos")
491 
492 #define lp_jit_cs_context_shared_size(_gallivm, _ptr) \
493    lp_build_struct_get_ptr(_gallivm, _ptr, LP_JIT_CS_CTX_SHARED_SIZE, "shared_size")
494 
495 #define lp_jit_cs_context_kernel_args(_gallivm, _ptr) \
496    lp_build_struct_get(_gallivm, _ptr, LP_JIT_CS_CTX_KERNEL_ARGS, "kernel_args")
497 
498 #define lp_jit_cs_context_aniso_filter_table(_gallivm, _ptr) \
499    lp_build_struct_get(_gallivm, _ptr, LP_JIT_CS_CTX_ANISO_FILTER_TABLE, "aniso_filter_table")
500 
501 
502 typedef void
503 (*lp_jit_cs_func)(const struct lp_jit_cs_context *context,
504                   uint32_t x,
505                   uint32_t y,
506                   uint32_t z,
507                   uint32_t grid_x,
508                   uint32_t grid_y,
509                   uint32_t grid_z,
510                   uint32_t grid_size_x,
511                   uint32_t grid_size_y,
512                   uint32_t grid_size_z,
513                   uint32_t work_dim,
514                   struct lp_jit_cs_thread_data *thread_data);
515 
516 void
517 lp_jit_screen_cleanup(struct llvmpipe_screen *screen);
518 
519 
520 boolean
521 lp_jit_screen_init(struct llvmpipe_screen *screen);
522 
523 
524 void
525 lp_jit_init_types(struct lp_fragment_shader_variant *lp);
526 
527 void
528 lp_jit_init_cs_types(struct lp_compute_shader_variant *lp);
529 #endif /* LP_JIT_H */
530