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 <[email protected]> 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 #include "gallivm/lp_bld_jit_types.h" 42 43 #include "pipe/p_state.h" 44 #include "lp_texture.h" 45 46 47 struct lp_build_format_cache; 48 struct lp_fragment_shader_variant; 49 struct lp_compute_shader_variant; 50 struct lp_rast_state; 51 struct llvmpipe_screen; 52 struct vertex_header; 53 54 struct lp_jit_viewport 55 { 56 float min_depth; 57 float max_depth; 58 }; 59 60 enum { 61 LP_JIT_VIEWPORT_MIN_DEPTH, 62 LP_JIT_VIEWPORT_MAX_DEPTH, 63 LP_JIT_VIEWPORT_NUM_FIELDS /* number of fields above */ 64 }; 65 66 /** 67 * This structure is passed directly to the generated fragment shader. 68 * 69 * It contains the derived state. 70 * 71 * Changes here must be reflected in the lp_jit_context_* macros and 72 * lp_jit_init_types function. Changes to the ordering should be avoided. 73 * 74 * Only use types with a clear size and padding here, in particular prefer the 75 * stdint.h types to the basic integer types. 76 */ 77 struct lp_jit_context 78 { 79 float alpha_ref_value; 80 81 uint32_t stencil_ref_front, stencil_ref_back; 82 83 uint8_t *u8_blend_color; 84 float *f_blend_color; 85 86 struct lp_jit_viewport *viewports; 87 88 uint32_t sample_mask; 89 }; 90 91 92 /** 93 * These enum values must match the position of the fields in the 94 * lp_jit_context struct above. 95 */ 96 enum { 97 LP_JIT_CTX_ALPHA_REF, 98 LP_JIT_CTX_STENCIL_REF_FRONT, 99 LP_JIT_CTX_STENCIL_REF_BACK, 100 LP_JIT_CTX_U8_BLEND_COLOR, 101 LP_JIT_CTX_F_BLEND_COLOR, 102 LP_JIT_CTX_VIEWPORTS, 103 LP_JIT_CTX_SAMPLE_MASK, 104 LP_JIT_CTX_COUNT 105 }; 106 107 #define lp_jit_context_alpha_ref_value(_gallivm, _type, _ptr) \ 108 lp_build_struct_get2(_gallivm, _type, _ptr, LP_JIT_CTX_ALPHA_REF, "alpha_ref_value") 109 110 #define lp_jit_context_stencil_ref_front_value(_gallivm, _type, _ptr) \ 111 lp_build_struct_get2(_gallivm, _type, _ptr, LP_JIT_CTX_STENCIL_REF_FRONT, "stencil_ref_front") 112 113 #define lp_jit_context_stencil_ref_back_value(_gallivm, _type, _ptr) \ 114 lp_build_struct_get2(_gallivm, _type, _ptr, LP_JIT_CTX_STENCIL_REF_BACK, "stencil_ref_back") 115 116 #define lp_jit_context_u8_blend_color(_gallivm, _type, _ptr) \ 117 lp_build_struct_get2(_gallivm, _type, _ptr, LP_JIT_CTX_U8_BLEND_COLOR, "u8_blend_color") 118 119 #define lp_jit_context_f_blend_color(_gallivm, _type, _ptr) \ 120 lp_build_struct_get2(_gallivm, _type, _ptr, LP_JIT_CTX_F_BLEND_COLOR, "f_blend_color") 121 122 #define lp_jit_context_viewports(_gallivm, _type, _ptr) \ 123 lp_build_struct_get2(_gallivm, _type, _ptr, LP_JIT_CTX_VIEWPORTS, "viewports") 124 125 #define lp_jit_context_sample_mask(_gallivm, _type, _ptr) \ 126 lp_build_struct_get_ptr2(_gallivm, _type, _ptr, LP_JIT_CTX_SAMPLE_MASK, "sample_mask") 127 128 129 struct lp_jit_thread_data 130 { 131 struct lp_build_format_cache *cache; 132 uint64_t vis_counter; // for occlusion query 133 uint64_t ps_invocations; // pixel shader invocations 134 135 /* 136 * Non-interpolated rasterizer state passed through to the fragment shader. 137 */ 138 struct { 139 uint32_t viewport_index; 140 uint32_t view_index; 141 } raster_state; 142 }; 143 144 145 enum { 146 LP_JIT_THREAD_DATA_CACHE = 0, 147 LP_JIT_THREAD_DATA_VIS_COUNTER, 148 LP_JIT_THREAD_DATA_PS_INVOCATIONS, 149 LP_JIT_THREAD_DATA_RASTER_STATE_VIEWPORT_INDEX, 150 LP_JIT_THREAD_DATA_RASTER_STATE_VIEW_INDEX, 151 LP_JIT_THREAD_DATA_COUNT 152 }; 153 154 155 #define lp_jit_thread_data_cache(_gallivm, _type, _ptr) \ 156 lp_build_struct_get2(_gallivm, _type, _ptr, LP_JIT_THREAD_DATA_CACHE, "cache") 157 158 #define lp_jit_thread_data_vis_counter(_gallivm, _type, _ptr) \ 159 lp_build_struct_get_ptr2(_gallivm, _type, _ptr, LP_JIT_THREAD_DATA_VIS_COUNTER, "viscounter") 160 161 #define lp_jit_thread_data_ps_invocations(_gallivm, _type, _ptr) \ 162 lp_build_struct_get_ptr2(_gallivm, _type, _ptr, LP_JIT_THREAD_DATA_PS_INVOCATIONS, "psinvocs") 163 164 #define lp_jit_thread_data_raster_state_viewport_index(_gallivm, _type, _ptr) \ 165 lp_build_struct_get2(_gallivm, _type, _ptr, \ 166 LP_JIT_THREAD_DATA_RASTER_STATE_VIEWPORT_INDEX, \ 167 "raster_state.viewport_index") 168 169 #define lp_jit_thread_data_raster_state_view_index(_gallivm, _type, _ptr) \ 170 lp_build_struct_get2(_gallivm, _type, _ptr, \ 171 LP_JIT_THREAD_DATA_RASTER_STATE_VIEW_INDEX, \ 172 "raster_state.view_index") 173 174 /** 175 * typedef for fragment shader function 176 * 177 * @param context jit context 178 * @param x block start x 179 * @param y block start y 180 * @param facing is front facing 181 * @param a0 shader input a0 182 * @param dadx shader input dadx 183 * @param dady shader input dady 184 * @param color color buffer 185 * @param depth depth buffer 186 * @param mask mask of visible pixels in block (16-bits per sample) 187 * @param thread_data task thread data 188 * @param stride color buffer row stride in bytes 189 * @param depth_stride depth buffer row stride in bytes 190 */ 191 typedef void 192 (*lp_jit_frag_func)(const struct lp_jit_context *context, 193 const struct lp_jit_resources *res, 194 uint32_t x, 195 uint32_t y, 196 uint32_t facing, 197 const void *a0, 198 const void *dadx, 199 const void *dady, 200 uint8_t **color, 201 uint8_t *depth, 202 uint64_t mask, 203 struct lp_jit_thread_data *thread_data, 204 unsigned *stride, 205 unsigned depth_stride, 206 unsigned *color_sample_stride, 207 unsigned depth_sample_stride); 208 209 210 #define LP_MAX_LINEAR_CONSTANTS 16 211 #define LP_MAX_LINEAR_TEXTURES 2 212 #define LP_MAX_LINEAR_INPUTS 8 213 214 215 /** 216 * This structure is passed directly to the generated fragment shader. 217 * 218 * It contains the derived state. 219 * 220 * Changes here must be reflected in the lp_jit_linear_context_* macros and 221 * lp_jit_init_types function. Changes to the ordering should be avoided. 222 * 223 * Only use types with a clear size and padding here, in particular prefer the 224 * stdint.h types to the basic integer types. 225 */ 226 struct lp_jit_linear_context 227 { 228 /** 229 * Constants in 8bit unorm values. 230 */ 231 const uint8_t (*constants)[4]; 232 struct lp_linear_elem *tex[LP_MAX_LINEAR_TEXTURES]; 233 struct lp_linear_elem *inputs[LP_MAX_LINEAR_INPUTS]; 234 235 uint8_t *color0; 236 uint32_t blend_color; 237 238 uint8_t alpha_ref_value; 239 }; 240 241 242 /** 243 * These enum values must match the position of the fields in the 244 * lp_jit_linear_context struct above. 245 */ 246 enum { 247 LP_JIT_LINEAR_CTX_CONSTANTS = 0, 248 LP_JIT_LINEAR_CTX_TEX, 249 LP_JIT_LINEAR_CTX_INPUTS, 250 LP_JIT_LINEAR_CTX_COLOR0, 251 LP_JIT_LINEAR_CTX_BLEND_COLOR, 252 LP_JIT_LINEAR_CTX_ALPHA_REF, 253 LP_JIT_LINEAR_CTX_COUNT 254 }; 255 256 257 #define lp_jit_linear_context_constants(_gallivm, _type, _ptr) \ 258 lp_build_struct_get2(_gallivm, _type, _ptr, LP_JIT_LINEAR_CTX_CONSTANTS, "constants") 259 260 #define lp_jit_linear_context_tex(_gallivm, _type, _ptr) \ 261 lp_build_struct_get_ptr2(_gallivm, _type, _ptr, LP_JIT_LINEAR_CTX_TEX, "tex") 262 263 #define lp_jit_linear_context_inputs(_gallivm, _type, _ptr) \ 264 lp_build_struct_get_ptr2(_gallivm, _type, _ptr, LP_JIT_LINEAR_CTX_INPUTS, "inputs") 265 266 #define lp_jit_linear_context_color0(_gallivm, _type, _ptr) \ 267 lp_build_struct_get_ptr2(_gallivm, _type, _ptr, LP_JIT_LINEAR_CTX_COLOR0, "color0") 268 269 #define lp_jit_linear_context_blend_color(_gallivm, _type, _ptr) \ 270 lp_build_struct_get_ptr2(_gallivm, _type, _ptr, LP_JIT_LINEAR_CTX_BLEND_COLOR, "blend_color") 271 272 #define lp_jit_linear_context_alpha_ref(_gallivm, _type, _ptr) \ 273 lp_build_struct_get_ptr2(_gallivm, _type, _ptr, LP_JIT_LINEAR_CTX_ALPHA_REF, "alpha_ref_value") 274 275 276 typedef const uint8_t * 277 (*lp_jit_linear_llvm_func)(struct lp_jit_linear_context *context, 278 uint32_t x, 279 uint32_t y, 280 uint32_t w); 281 282 /* We're not really jitting this, but I need to get into the 283 * rast_state struct to call the function we actually are jitting. 284 */ 285 typedef bool 286 (*lp_jit_linear_func)(const struct lp_rast_state *state, 287 uint32_t x, 288 uint32_t y, 289 uint32_t w, 290 uint32_t h, 291 const float (*a0)[4], 292 const float (*dadx)[4], 293 const float (*dady)[4], 294 uint8_t *color, 295 uint32_t color_stride); 296 297 298 struct lp_jit_cs_thread_data 299 { 300 struct lp_build_format_cache *cache; 301 void *shared; 302 void *payload; 303 }; 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_PAYLOAD = 2, 310 LP_JIT_CS_THREAD_DATA_COUNT 311 }; 312 313 314 #define lp_jit_cs_thread_data_cache(_gallivm, _type, _ptr) \ 315 lp_build_struct_get2(_gallivm, _type, _ptr, LP_JIT_CS_THREAD_DATA_CACHE, "cache") 316 317 #define lp_jit_cs_thread_data_shared(_gallivm, _type, _ptr) \ 318 lp_build_struct_get2(_gallivm, _type, _ptr, LP_JIT_CS_THREAD_DATA_SHARED, "shared") 319 320 #define lp_jit_cs_thread_data_payload(_gallivm, _type, _ptr) \ 321 lp_build_struct_get2(_gallivm, _type, _ptr, LP_JIT_CS_THREAD_DATA_PAYLOAD, "payload") 322 323 324 struct lp_jit_cs_context 325 { 326 const void *kernel_args; 327 328 uint32_t shared_size; 329 }; 330 331 /** 332 * These enum values must match the position of the fields in the 333 * lp_jit_context struct above. 334 */ 335 enum { 336 LP_JIT_CS_CTX_KERNEL_ARGS, 337 LP_JIT_CS_CTX_SHARED_SIZE, 338 LP_JIT_CS_CTX_COUNT 339 }; 340 341 #define lp_jit_cs_context_constants(_gallivm, _type, _ptr) \ 342 lp_build_struct_get_ptr2(_gallivm, _type, _ptr, LP_JIT_CS_CTX_CONSTANTS, "constants") 343 344 #define lp_jit_cs_context_ssbos(_gallivm, _type, _ptr) \ 345 lp_build_struct_get_ptr2(_gallivm, _type, _ptr, LP_JIT_CS_CTX_SSBOS, "ssbos") 346 347 #define lp_jit_cs_context_textures(_gallivm, _type, _ptr) \ 348 lp_build_struct_get_ptr2(_gallivm, _type, _ptr, LP_JIT_CS_CTX_TEXTURES, "textures") 349 350 #define lp_jit_cs_context_samplers(_gallivm, _type, _ptr) \ 351 lp_build_struct_get_ptr2(_gallivm, _type, _ptr, LP_JIT_CS_CTX_SAMPLERS, "samplers") 352 353 #define lp_jit_cs_context_images(_gallivm, _type, _ptr) \ 354 lp_build_struct_get_ptr2(_gallivm, _type, _ptr, LP_JIT_CS_CTX_IMAGES, "images") 355 356 #define lp_jit_cs_context_aniso_filter_table(_gallivm, _type, _ptr) \ 357 lp_build_struct_get2(_gallivm, _type, _ptr, LP_JIT_CS_CTX_ANISO_FILTER_TABLE, "aniso_filter_table") 358 359 #define lp_jit_cs_context_kernel_args(_gallivm, _type, _ptr) \ 360 lp_build_struct_get2(_gallivm, _type, _ptr, LP_JIT_CS_CTX_KERNEL_ARGS, "kernel_args") 361 362 #define lp_jit_cs_context_shared_size(_gallivm, _type, _ptr) \ 363 lp_build_struct_get_ptr2(_gallivm, _type, _ptr, LP_JIT_CS_CTX_SHARED_SIZE, "shared_size") 364 365 typedef void 366 (*lp_jit_cs_func)(const struct lp_jit_cs_context *context, 367 const struct lp_jit_resources *resources, 368 uint32_t x, 369 uint32_t y, 370 uint32_t z, 371 uint32_t grid_x, 372 uint32_t grid_y, 373 uint32_t grid_z, 374 uint32_t grid_size_x, 375 uint32_t grid_size_y, 376 uint32_t grid_size_z, 377 uint32_t work_dim, 378 uint32_t draw_id, 379 struct vertex_header *io, /* mesh shader only */ 380 struct lp_jit_cs_thread_data *thread_data); 381 382 void 383 lp_jit_screen_cleanup(struct llvmpipe_screen *screen); 384 385 bool 386 lp_jit_screen_init(struct llvmpipe_screen *screen); 387 388 void 389 lp_jit_init_types(struct lp_fragment_shader_variant *lp); 390 391 void 392 lp_jit_init_cs_types(struct lp_compute_shader_variant *lp); 393 394 /* Helpers for converting pipe_* to lp_jit_* resources. */ 395 void lp_jit_buffer_from_bda(struct lp_jit_buffer *jit, void *mem, size_t size); 396 void lp_jit_buffer_from_pipe(struct lp_jit_buffer *jit, const struct pipe_shader_buffer *buffer); 397 void lp_jit_buffer_from_pipe_const(struct lp_jit_buffer *jit, const struct pipe_constant_buffer *buffer, struct pipe_screen *screen); 398 void lp_jit_texture_from_pipe(struct lp_jit_texture *jit, const struct pipe_sampler_view *view); 399 void lp_jit_texture_buffer_from_bda(struct lp_jit_texture *jit, void *mem, size_t size, enum pipe_format format); 400 void lp_jit_sampler_from_pipe(struct lp_jit_sampler *jit, const struct pipe_sampler_state *sampler); 401 void lp_jit_image_from_pipe(struct lp_jit_image *jit, const struct pipe_image_view *view); 402 void lp_jit_image_buffer_from_bda(struct lp_jit_image *jit, void *mem, size_t size, enum pipe_format format); 403 404 #endif /* LP_JIT_H */ 405