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