Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /**************************************************************************
  2.  *
  3.  * Copyright 2010 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. #ifndef DRAW_LLVM_H
  29. #define DRAW_LLVM_H
  30.  
  31. #include "draw/draw_private.h"
  32.  
  33. #include "draw/draw_vs.h"
  34. #include "draw/draw_gs.h"
  35.  
  36. #include "gallivm/lp_bld_sample.h"
  37. #include "gallivm/lp_bld_limits.h"
  38.  
  39. #include "pipe/p_context.h"
  40. #include "util/u_simple_list.h"
  41.  
  42.  
  43. struct draw_llvm;
  44. struct llvm_vertex_shader;
  45. struct llvm_geometry_shader;
  46.  
  47. struct draw_jit_texture
  48. {
  49.    uint32_t width;
  50.    uint32_t height;
  51.    uint32_t depth;
  52.    uint32_t first_level;
  53.    uint32_t last_level;
  54.    const void *base;
  55.    uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
  56.    uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
  57.    uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS];
  58. };
  59.  
  60.  
  61. struct draw_sampler_static_state
  62. {
  63.    /*
  64.     * These attributes are effectively interleaved for more sane key handling.
  65.     * However, there might be lots of null space if the amount of samplers and
  66.     * textures isn't the same.
  67.     */
  68.    struct lp_static_sampler_state sampler_state;
  69.    struct lp_static_texture_state texture_state;
  70. };
  71.  
  72.  
  73. struct draw_jit_sampler
  74. {
  75.    float min_lod;
  76.    float max_lod;
  77.    float lod_bias;
  78.    float border_color[4];
  79. };
  80.  
  81.  
  82. enum {
  83.    DRAW_JIT_TEXTURE_WIDTH = 0,
  84.    DRAW_JIT_TEXTURE_HEIGHT,
  85.    DRAW_JIT_TEXTURE_DEPTH,
  86.    DRAW_JIT_TEXTURE_FIRST_LEVEL,
  87.    DRAW_JIT_TEXTURE_LAST_LEVEL,
  88.    DRAW_JIT_TEXTURE_BASE,
  89.    DRAW_JIT_TEXTURE_ROW_STRIDE,
  90.    DRAW_JIT_TEXTURE_IMG_STRIDE,
  91.    DRAW_JIT_TEXTURE_MIP_OFFSETS,
  92.    DRAW_JIT_TEXTURE_NUM_FIELDS  /* number of fields above */
  93. };
  94.  
  95.  
  96. enum {
  97.    DRAW_JIT_SAMPLER_MIN_LOD,
  98.    DRAW_JIT_SAMPLER_MAX_LOD,
  99.    DRAW_JIT_SAMPLER_LOD_BIAS,
  100.    DRAW_JIT_SAMPLER_BORDER_COLOR,
  101.    DRAW_JIT_SAMPLER_NUM_FIELDS  /* number of fields above */
  102. };
  103.  
  104.  
  105. enum {
  106.    DRAW_JIT_VERTEX_VERTEX_ID = 0,
  107.    DRAW_JIT_VERTEX_CLIP,
  108.    DRAW_JIT_VERTEX_PRE_CLIP_POS,
  109.    DRAW_JIT_VERTEX_DATA
  110. };
  111.  
  112. /**
  113.  * This structure is passed directly to the generated vertex shader.
  114.  *
  115.  * It contains the derived state.
  116.  *
  117.  * Changes here must be reflected in the draw_jit_context_* macros.
  118.  * Changes to the ordering should be avoided.
  119.  *
  120.  * Only use types with a clear size and padding here, in particular prefer the
  121.  * stdint.h types to the basic integer types.
  122.  */
  123. struct draw_jit_context
  124. {
  125.    const float *vs_constants[LP_MAX_TGSI_CONST_BUFFERS];
  126.    float (*planes) [DRAW_TOTAL_CLIP_PLANES][4];
  127.    float *viewport;
  128.  
  129.    struct draw_jit_texture textures[PIPE_MAX_SHADER_SAMPLER_VIEWS];
  130.    struct draw_jit_sampler samplers[PIPE_MAX_SAMPLERS];
  131. };
  132.  
  133. enum {
  134.    DRAW_JIT_CTX_CONSTANTS   = 0,
  135.    DRAW_JIT_CTX_PLANES      = 1,
  136.    DRAW_JIT_CTX_VIEWPORT    = 2,
  137.    DRAW_JIT_CTX_TEXTURES    = 3,
  138.    DRAW_JIT_CTX_SAMPLERS    = 4,
  139.    DRAW_JIT_CTX_NUM_FIELDS
  140. };
  141.  
  142. #define draw_jit_context_vs_constants(_gallivm, _ptr) \
  143.    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_CONSTANTS, "vs_constants")
  144.  
  145. #define draw_jit_context_planes(_gallivm, _ptr) \
  146.    lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_CTX_PLANES, "planes")
  147.  
  148. #define draw_jit_context_viewport(_gallivm, _ptr) \
  149.    lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_CTX_VIEWPORT, "viewport")
  150.  
  151. #define draw_jit_context_textures(_gallivm, _ptr) \
  152.    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_TEXTURES, "textures")
  153.  
  154. #define draw_jit_context_samplers(_gallivm, _ptr) \
  155.    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_SAMPLERS, "samplers")
  156.  
  157. #define draw_jit_header_id(_gallivm, _ptr)              \
  158.    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_VERTEX_ID, "id")
  159.  
  160. #define draw_jit_header_clip(_gallivm, _ptr) \
  161.    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_CLIP, "clip")
  162.  
  163. #define draw_jit_header_pre_clip_pos(_gallivm, _ptr) \
  164.    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_PRE_CLIP_POS, "pre_clip_pos")
  165.  
  166. #define draw_jit_header_data(_gallivm, _ptr)            \
  167.    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_DATA, "data")
  168.  
  169.  
  170. #define draw_jit_vbuffer_stride(_gallivm, _ptr)         \
  171.    lp_build_struct_get(_gallivm, _ptr, 0, "stride")
  172.  
  173. #define draw_jit_vbuffer_offset(_gallivm, _ptr)         \
  174.    lp_build_struct_get(_gallivm, _ptr, 1, "buffer_offset")
  175.  
  176. enum {
  177.    DRAW_JIT_DVBUFFER_MAP = 0,
  178.    DRAW_JIT_DVBUFFER_SIZE,
  179.    DRAW_JIT_DVBUFFER_NUM_FIELDS  /* number of fields above */
  180. };
  181.  
  182. #define draw_jit_dvbuffer_map(_gallivm, _ptr)         \
  183.    lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_DVBUFFER_MAP, "map")
  184.  
  185. #define draw_jit_dvbuffer_size(_gallivm, _ptr)        \
  186.    lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_DVBUFFER_SIZE, "size")
  187.  
  188.  
  189. /**
  190.  * This structure is passed directly to the generated geometry shader.
  191.  *
  192.  * It contains the derived state.
  193.  *
  194.  * Changes here must be reflected in the draw_gs_jit_context_* macros.
  195.  * Changes to the ordering should be avoided.
  196.  *
  197.  * Only use types with a clear size and padding here, in particular prefer the
  198.  * stdint.h types to the basic integer types.
  199.  */
  200. struct draw_gs_jit_context
  201. {
  202.    const float *constants[LP_MAX_TGSI_CONST_BUFFERS];
  203.    float (*planes) [DRAW_TOTAL_CLIP_PLANES][4];
  204.    float *viewport;
  205.  
  206.    /* There two need to be exactly at DRAW_JIT_CTX_TEXTURES and
  207.     * DRAW_JIT_CTX_SAMPLERS positions in the struct */
  208.    struct draw_jit_texture textures[PIPE_MAX_SHADER_SAMPLER_VIEWS];
  209.    struct draw_jit_sampler samplers[PIPE_MAX_SAMPLERS];
  210.    
  211.    int **prim_lengths;
  212.    int *emitted_vertices;
  213.    int *emitted_prims;
  214. };
  215.  
  216. enum {
  217.    DRAW_GS_JIT_CTX_CONSTANTS = 0,
  218.    DRAW_GS_JIT_CTX_PLANES = 1,
  219.    DRAW_GS_JIT_CTX_VIEWPORT = 2,
  220.    /* Textures and samples are reserved for DRAW_JIT_CTX_TEXTURES
  221.     * and DRAW_JIT_CTX_SAMPLERS, because they both need
  222.     * to be at exactly the same locations as they are in the
  223.     * VS ctx structure for sampling to work. */
  224.    DRAW_GS_JIT_CTX_TEXTURES = DRAW_JIT_CTX_TEXTURES,
  225.    DRAW_GS_JIT_CTX_SAMPLERS = DRAW_JIT_CTX_SAMPLERS,
  226.    DRAW_GS_JIT_CTX_PRIM_LENGTHS = 5,
  227.    DRAW_GS_JIT_CTX_EMITTED_VERTICES = 6,
  228.    DRAW_GS_JIT_CTX_EMITTED_PRIMS = 7,
  229.    DRAW_GS_JIT_CTX_NUM_FIELDS = 8
  230. };
  231.  
  232. #define draw_gs_jit_context_constants(_gallivm, _ptr) \
  233.    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_CONSTANTS, "constants")
  234.  
  235. #define draw_gs_jit_context_planes(_gallivm, _ptr) \
  236.    lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_PLANES, "planes")
  237.  
  238. #define draw_gs_jit_context_viewport(_gallivm, _ptr) \
  239.    lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_VIEWPORT, "viewport")
  240.  
  241. #define draw_gs_jit_context_textures(_gallivm, _ptr) \
  242.    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_TEXTURES, "textures")
  243.  
  244. #define draw_gs_jit_context_samplers(_gallivm, _ptr) \
  245.    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_SAMPLERS, "samplers")
  246.  
  247. #define draw_gs_jit_prim_lengths(_gallivm, _ptr) \
  248.    lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_PRIM_LENGTHS, "prim_lengths")
  249.  
  250. #define draw_gs_jit_emitted_vertices(_gallivm, _ptr) \
  251.    lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_EMITTED_VERTICES, "emitted_vertices")
  252.  
  253. #define draw_gs_jit_emitted_prims(_gallivm, _ptr) \
  254.    lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_EMITTED_PRIMS, "emitted_prims")
  255.  
  256.  
  257.  
  258. typedef int
  259. (*draw_jit_vert_func)(struct draw_jit_context *context,
  260.                       struct vertex_header *io,
  261.                       const struct draw_vertex_buffer vbuffers[PIPE_MAX_ATTRIBS],
  262.                       unsigned start,
  263.                       unsigned count,
  264.                       unsigned stride,
  265.                       struct pipe_vertex_buffer *vertex_buffers,
  266.                       unsigned instance_id);
  267.  
  268.  
  269. typedef int
  270. (*draw_jit_vert_func_elts)(struct draw_jit_context *context,
  271.                            struct vertex_header *io,
  272.                            const struct draw_vertex_buffer vbuffers[PIPE_MAX_ATTRIBS],
  273.                            const unsigned *fetch_elts,
  274.                            unsigned fetch_max_elt,
  275.                            unsigned fetch_count,
  276.                            unsigned stride,
  277.                            struct pipe_vertex_buffer *vertex_buffers,
  278.                            unsigned instance_id);
  279.  
  280.  
  281. typedef int
  282. (*draw_gs_jit_func)(struct draw_gs_jit_context *context,
  283.                     float inputs[6][PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS][TGSI_NUM_CHANNELS],
  284.                     struct vertex_header *output,
  285.                     unsigned num_prims,
  286.                     unsigned instance_id,
  287.                     int *prim_ids);
  288.  
  289. struct draw_llvm_variant_key
  290. {
  291.    unsigned nr_vertex_elements:8;
  292.    unsigned nr_samplers:8;
  293.    unsigned nr_sampler_views:8;
  294.    unsigned clamp_vertex_color:1;
  295.    unsigned clip_xy:1;
  296.    unsigned clip_z:1;
  297.    unsigned clip_user:1;
  298.    unsigned clip_halfz:1;
  299.    unsigned bypass_viewport:1;
  300.    unsigned need_edgeflags:1;
  301.    unsigned has_gs:1;
  302.    /*
  303.     * it is important there are no holes in this struct
  304.     * (and all padding gets zeroed).
  305.     */
  306.    unsigned ucp_enable:PIPE_MAX_CLIP_PLANES;
  307.    unsigned pad1:32-PIPE_MAX_CLIP_PLANES;
  308.  
  309.    /* Variable number of vertex elements:
  310.     */
  311.    struct pipe_vertex_element vertex_element[1];
  312.  
  313.    /* Followed by variable number of samplers:
  314.     */
  315. /*   struct draw_sampler_static_state sampler; */
  316. };
  317.  
  318. struct draw_gs_llvm_variant_key
  319. {
  320.    unsigned nr_samplers:8;
  321.    unsigned nr_sampler_views:8;
  322.  
  323.    struct draw_sampler_static_state samplers[1];
  324. };
  325.  
  326. #define DRAW_LLVM_MAX_VARIANT_KEY_SIZE \
  327.    (sizeof(struct draw_llvm_variant_key) +      \
  328.     PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state) +  \
  329.     (PIPE_MAX_ATTRIBS-1) * sizeof(struct pipe_vertex_element))
  330.  
  331. #define DRAW_GS_LLVM_MAX_VARIANT_KEY_SIZE \
  332.    (sizeof(struct draw_gs_llvm_variant_key) +   \
  333.     PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state))
  334.  
  335.  
  336. static INLINE size_t
  337. draw_llvm_variant_key_size(unsigned nr_vertex_elements,
  338.                            unsigned nr_samplers)
  339. {
  340.    return (sizeof(struct draw_llvm_variant_key) +
  341.            nr_samplers * sizeof(struct draw_sampler_static_state) +
  342.            (nr_vertex_elements - 1) * sizeof(struct pipe_vertex_element));
  343. }
  344.  
  345.  
  346. static INLINE size_t
  347. draw_gs_llvm_variant_key_size(unsigned nr_samplers)
  348. {
  349.    return (sizeof(struct draw_gs_llvm_variant_key) +
  350.            (nr_samplers - 1) * sizeof(struct draw_sampler_static_state));
  351. }
  352.  
  353.  
  354. static INLINE struct draw_sampler_static_state *
  355. draw_llvm_variant_key_samplers(struct draw_llvm_variant_key *key)
  356. {
  357.    return (struct draw_sampler_static_state *)
  358.       &key->vertex_element[key->nr_vertex_elements];
  359. }
  360.  
  361.  
  362. struct draw_llvm_variant_list_item
  363. {
  364.    struct draw_llvm_variant *base;
  365.    struct draw_llvm_variant_list_item *next, *prev;
  366. };
  367.  
  368. struct draw_gs_llvm_variant_list_item
  369. {
  370.    struct draw_gs_llvm_variant *base;
  371.    struct draw_gs_llvm_variant_list_item *next, *prev;
  372. };
  373.  
  374.  
  375. struct draw_llvm_variant
  376. {
  377.    struct gallivm_state *gallivm;
  378.  
  379.    /* LLVM JIT builder types */
  380.    LLVMTypeRef context_ptr_type;
  381.    LLVMTypeRef buffer_ptr_type;
  382.    LLVMTypeRef vb_ptr_type;
  383.    LLVMTypeRef vertex_header_ptr_type;
  384.  
  385.    LLVMValueRef function;
  386.    LLVMValueRef function_elts;
  387.    draw_jit_vert_func jit_func;
  388.    draw_jit_vert_func_elts jit_func_elts;
  389.  
  390.    struct llvm_vertex_shader *shader;
  391.  
  392.    struct draw_llvm *llvm;
  393.    struct draw_llvm_variant_list_item list_item_global;
  394.    struct draw_llvm_variant_list_item list_item_local;
  395.  
  396.    /* key is variable-sized, must be last */
  397.    struct draw_llvm_variant_key key;
  398. };
  399.  
  400.  
  401. struct draw_gs_llvm_variant
  402. {
  403.    struct gallivm_state *gallivm;
  404.  
  405.    /* LLVM JIT builder types */
  406.    LLVMTypeRef context_ptr_type;
  407.    LLVMTypeRef vertex_header_ptr_type;
  408.    LLVMTypeRef input_array_type;
  409.  
  410.    LLVMValueRef context_ptr;
  411.    LLVMValueRef io_ptr;
  412.    LLVMValueRef num_prims;
  413.    LLVMValueRef function;
  414.    draw_gs_jit_func jit_func;
  415.  
  416.    struct llvm_geometry_shader *shader;
  417.  
  418.    struct draw_llvm *llvm;
  419.    struct draw_gs_llvm_variant_list_item list_item_global;
  420.    struct draw_gs_llvm_variant_list_item list_item_local;
  421.  
  422.    /* key is variable-sized, must be last */
  423.    struct draw_gs_llvm_variant_key key;
  424. };
  425.  
  426. struct llvm_vertex_shader {
  427.    struct draw_vertex_shader base;
  428.  
  429.    unsigned variant_key_size;
  430.    struct draw_llvm_variant_list_item variants;
  431.    unsigned variants_created;
  432.    unsigned variants_cached;
  433. };
  434.  
  435. struct llvm_geometry_shader {
  436.    struct draw_geometry_shader base;
  437.  
  438.    unsigned variant_key_size;
  439.    struct draw_gs_llvm_variant_list_item variants;
  440.    unsigned variants_created;
  441.    unsigned variants_cached;
  442. };
  443.  
  444.  
  445. struct draw_llvm {
  446.    struct draw_context *draw;
  447.  
  448.    struct draw_jit_context jit_context;
  449.    struct draw_gs_jit_context gs_jit_context;
  450.  
  451.    struct draw_llvm_variant_list_item vs_variants_list;
  452.    int nr_variants;
  453.  
  454.    struct draw_gs_llvm_variant_list_item gs_variants_list;
  455.    int nr_gs_variants;
  456. };
  457.  
  458.  
  459. static INLINE struct llvm_vertex_shader *
  460. llvm_vertex_shader(struct draw_vertex_shader *vs)
  461. {
  462.    return (struct llvm_vertex_shader *)vs;
  463. }
  464.  
  465. static INLINE struct llvm_geometry_shader *
  466. llvm_geometry_shader(struct draw_geometry_shader *gs)
  467. {
  468.    return (struct llvm_geometry_shader *)gs;
  469. }
  470.  
  471.  
  472.  
  473.  
  474. struct draw_llvm *
  475. draw_llvm_create(struct draw_context *draw);
  476.  
  477. void
  478. draw_llvm_destroy(struct draw_llvm *llvm);
  479.  
  480. struct draw_llvm_variant *
  481. draw_llvm_create_variant(struct draw_llvm *llvm,
  482.                          unsigned num_vertex_header_attribs,
  483.                          const struct draw_llvm_variant_key *key);
  484.  
  485. void
  486. draw_llvm_destroy_variant(struct draw_llvm_variant *variant);
  487.  
  488. struct draw_llvm_variant_key *
  489. draw_llvm_make_variant_key(struct draw_llvm *llvm, char *store);
  490.  
  491. void
  492. draw_llvm_dump_variant_key(struct draw_llvm_variant_key *key);
  493.  
  494.  
  495. struct draw_gs_llvm_variant *
  496. draw_gs_llvm_create_variant(struct draw_llvm *llvm,
  497.                             unsigned num_vertex_header_attribs,
  498.                             const struct draw_gs_llvm_variant_key *key);
  499.  
  500. void
  501. draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant *variant);
  502.  
  503. struct draw_gs_llvm_variant_key *
  504. draw_gs_llvm_make_variant_key(struct draw_llvm *llvm, char *store);
  505.  
  506. void
  507. draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key *key);
  508.  
  509. struct lp_build_sampler_soa *
  510. draw_llvm_sampler_soa_create(const struct draw_sampler_static_state *static_state,
  511.                              LLVMValueRef context_ptr);
  512.  
  513. void
  514. draw_llvm_set_sampler_state(struct draw_context *draw, unsigned shader_stage);
  515.  
  516. void
  517. draw_llvm_set_mapped_texture(struct draw_context *draw,
  518.                              unsigned shader_stage,
  519.                              unsigned sview_idx,
  520.                              uint32_t width, uint32_t height, uint32_t depth,
  521.                              uint32_t first_level, uint32_t last_level,
  522.                              const void *base_ptr,
  523.                              uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],
  524.                              uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],
  525.                              uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS]);
  526.  
  527. #endif
  528.