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