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