Home | History | Annotate | Download | only in swr
      1 /****************************************************************************
      2  * Copyright (C) 2015 Intel Corporation.   All Rights Reserved.
      3  *
      4  * Permission is hereby granted, free of charge, to any person obtaining a
      5  * copy of this software and associated documentation files (the "Software"),
      6  * to deal in the Software without restriction, including without limitation
      7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8  * and/or sell copies of the Software, and to permit persons to whom the
      9  * Software is furnished to do so, subject to the following conditions:
     10  *
     11  * The above copyright notice and this permission notice (including the next
     12  * paragraph) shall be included in all copies or substantial portions of the
     13  * Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     21  * IN THE SOFTWARE.
     22  ***************************************************************************/
     23 
     24 #ifndef SWR_STATE_H
     25 #define SWR_STATE_H
     26 
     27 #include "pipe/p_defines.h"
     28 #include "tgsi/tgsi_scan.h"
     29 #include "tgsi/tgsi_parse.h"
     30 #include "tgsi/tgsi_dump.h"
     31 #include "gallivm/lp_bld_init.h"
     32 #include "gallivm/lp_bld_tgsi.h"
     33 #include "util/crc32.h"
     34 #include "api.h"
     35 #include "swr_tex_sample.h"
     36 #include "swr_shader.h"
     37 #include <unordered_map>
     38 #include <memory>
     39 
     40 template <typename T>
     41 struct ShaderVariant {
     42    struct gallivm_state *gallivm;
     43    T shader;
     44 
     45    ShaderVariant(struct gallivm_state *gs, T code) : gallivm(gs), shader(code) {}
     46    ~ShaderVariant() { gallivm_destroy(gallivm); }
     47 };
     48 
     49 typedef ShaderVariant<PFN_VERTEX_FUNC> VariantVS;
     50 typedef ShaderVariant<PFN_PIXEL_KERNEL> VariantFS;
     51 
     52 /* skeleton */
     53 struct swr_vertex_shader {
     54    struct pipe_shader_state pipe;
     55    struct lp_tgsi_info info;
     56    std::unordered_map<swr_jit_vs_key, std::unique_ptr<VariantVS>> map;
     57    SWR_STREAMOUT_STATE soState;
     58    PFN_SO_FUNC soFunc[PIPE_PRIM_MAX] {0};
     59 };
     60 
     61 struct swr_fragment_shader {
     62    struct pipe_shader_state pipe;
     63    struct lp_tgsi_info info;
     64    uint32_t constantMask;
     65    uint32_t flatConstantMask;
     66    uint32_t pointSpriteMask;
     67    std::unordered_map<swr_jit_fs_key, std::unique_ptr<VariantFS>> map;
     68 };
     69 
     70 /* Vertex element state */
     71 struct swr_vertex_element_state {
     72    FETCH_COMPILE_STATE fsState;
     73    PFN_FETCH_FUNC fsFunc;
     74    uint32_t stream_pitch[PIPE_MAX_ATTRIBS];
     75    uint32_t min_instance_div[PIPE_MAX_ATTRIBS];
     76    uint32_t instanced_bufs;
     77 };
     78 
     79 struct swr_blend_state {
     80    struct pipe_blend_state pipe;
     81    SWR_BLEND_STATE blendState;
     82    RENDER_TARGET_BLEND_COMPILE_STATE compileState[PIPE_MAX_COLOR_BUFS];
     83 };
     84 
     85 /*
     86  * Derived SWR API DrawState
     87  * For convenience of making simple changes without re-deriving state.
     88  */
     89 struct swr_derived_state {
     90    SWR_RASTSTATE rastState;
     91    SWR_VIEWPORT vp;
     92    SWR_VIEWPORT_MATRICES vpm;
     93 };
     94 
     95 void swr_update_derived(struct pipe_context *,
     96                         const struct pipe_draw_info * = nullptr);
     97 
     98 /*
     99  * Conversion functions: Convert mesa state defines to SWR.
    100  */
    101 
    102 static INLINE SWR_LOGIC_OP
    103 swr_convert_logic_op(const UINT op)
    104 {
    105    switch (op) {
    106    case PIPE_LOGICOP_CLEAR:
    107       return LOGICOP_CLEAR;
    108    case PIPE_LOGICOP_NOR:
    109       return LOGICOP_NOR;
    110    case PIPE_LOGICOP_AND_INVERTED:
    111       return LOGICOP_AND_INVERTED;
    112    case PIPE_LOGICOP_COPY_INVERTED:
    113       return LOGICOP_COPY_INVERTED;
    114    case PIPE_LOGICOP_AND_REVERSE:
    115       return LOGICOP_AND_REVERSE;
    116    case PIPE_LOGICOP_INVERT:
    117       return LOGICOP_INVERT;
    118    case PIPE_LOGICOP_XOR:
    119       return LOGICOP_XOR;
    120    case PIPE_LOGICOP_NAND:
    121       return LOGICOP_NAND;
    122    case PIPE_LOGICOP_AND:
    123       return LOGICOP_AND;
    124    case PIPE_LOGICOP_EQUIV:
    125       return LOGICOP_EQUIV;
    126    case PIPE_LOGICOP_NOOP:
    127       return LOGICOP_NOOP;
    128    case PIPE_LOGICOP_OR_INVERTED:
    129       return LOGICOP_OR_INVERTED;
    130    case PIPE_LOGICOP_COPY:
    131       return LOGICOP_COPY;
    132    case PIPE_LOGICOP_OR_REVERSE:
    133       return LOGICOP_OR_REVERSE;
    134    case PIPE_LOGICOP_OR:
    135       return LOGICOP_OR;
    136    case PIPE_LOGICOP_SET:
    137       return LOGICOP_SET;
    138    default:
    139       assert(0 && "Unsupported logic op");
    140       return LOGICOP_NOOP;
    141    }
    142 }
    143 
    144 static INLINE SWR_STENCILOP
    145 swr_convert_stencil_op(const UINT op)
    146 {
    147    switch (op) {
    148    case PIPE_STENCIL_OP_KEEP:
    149       return STENCILOP_KEEP;
    150    case PIPE_STENCIL_OP_ZERO:
    151       return STENCILOP_ZERO;
    152    case PIPE_STENCIL_OP_REPLACE:
    153       return STENCILOP_REPLACE;
    154    case PIPE_STENCIL_OP_INCR:
    155       return STENCILOP_INCRSAT;
    156    case PIPE_STENCIL_OP_DECR:
    157       return STENCILOP_DECRSAT;
    158    case PIPE_STENCIL_OP_INCR_WRAP:
    159       return STENCILOP_INCR;
    160    case PIPE_STENCIL_OP_DECR_WRAP:
    161       return STENCILOP_DECR;
    162    case PIPE_STENCIL_OP_INVERT:
    163       return STENCILOP_INVERT;
    164    default:
    165       assert(0 && "Unsupported stencil op");
    166       return STENCILOP_KEEP;
    167    }
    168 }
    169 
    170 static INLINE SWR_FORMAT
    171 swr_convert_index_type(const UINT index_size)
    172 {
    173    switch (index_size) {
    174    case sizeof(unsigned char):
    175       return R8_UINT;
    176    case sizeof(unsigned short):
    177       return R16_UINT;
    178    case sizeof(unsigned int):
    179       return R32_UINT;
    180    default:
    181       assert(0 && "Unsupported index type");
    182       return R32_UINT;
    183    }
    184 }
    185 
    186 
    187 static INLINE SWR_ZFUNCTION
    188 swr_convert_depth_func(const UINT pipe_func)
    189 {
    190    switch (pipe_func) {
    191    case PIPE_FUNC_NEVER:
    192       return ZFUNC_NEVER;
    193    case PIPE_FUNC_LESS:
    194       return ZFUNC_LT;
    195    case PIPE_FUNC_EQUAL:
    196       return ZFUNC_EQ;
    197    case PIPE_FUNC_LEQUAL:
    198       return ZFUNC_LE;
    199    case PIPE_FUNC_GREATER:
    200       return ZFUNC_GT;
    201    case PIPE_FUNC_NOTEQUAL:
    202       return ZFUNC_NE;
    203    case PIPE_FUNC_GEQUAL:
    204       return ZFUNC_GE;
    205    case PIPE_FUNC_ALWAYS:
    206       return ZFUNC_ALWAYS;
    207    default:
    208       assert(0 && "Unsupported depth func");
    209       return ZFUNC_ALWAYS;
    210    }
    211 }
    212 
    213 
    214 static INLINE SWR_CULLMODE
    215 swr_convert_cull_mode(const UINT cull_face)
    216 {
    217    switch (cull_face) {
    218    case PIPE_FACE_NONE:
    219       return SWR_CULLMODE_NONE;
    220    case PIPE_FACE_FRONT:
    221       return SWR_CULLMODE_FRONT;
    222    case PIPE_FACE_BACK:
    223       return SWR_CULLMODE_BACK;
    224    case PIPE_FACE_FRONT_AND_BACK:
    225       return SWR_CULLMODE_BOTH;
    226    default:
    227       assert(0 && "Invalid cull mode");
    228       return SWR_CULLMODE_NONE;
    229    }
    230 }
    231 
    232 static INLINE SWR_BLEND_OP
    233 swr_convert_blend_func(const UINT blend_func)
    234 {
    235    switch (blend_func) {
    236    case PIPE_BLEND_ADD:
    237       return BLENDOP_ADD;
    238    case PIPE_BLEND_SUBTRACT:
    239       return BLENDOP_SUBTRACT;
    240    case PIPE_BLEND_REVERSE_SUBTRACT:
    241       return BLENDOP_REVSUBTRACT;
    242    case PIPE_BLEND_MIN:
    243       return BLENDOP_MIN;
    244    case PIPE_BLEND_MAX:
    245       return BLENDOP_MAX;
    246    default:
    247       assert(0 && "Invalid blend func");
    248       return BLENDOP_ADD;
    249    }
    250 }
    251 
    252 static INLINE SWR_BLEND_FACTOR
    253 swr_convert_blend_factor(const UINT blend_factor)
    254 {
    255    switch (blend_factor) {
    256    case PIPE_BLENDFACTOR_ONE:
    257       return BLENDFACTOR_ONE;
    258    case PIPE_BLENDFACTOR_SRC_COLOR:
    259       return BLENDFACTOR_SRC_COLOR;
    260    case PIPE_BLENDFACTOR_SRC_ALPHA:
    261       return BLENDFACTOR_SRC_ALPHA;
    262    case PIPE_BLENDFACTOR_DST_ALPHA:
    263       return BLENDFACTOR_DST_ALPHA;
    264    case PIPE_BLENDFACTOR_DST_COLOR:
    265       return BLENDFACTOR_DST_COLOR;
    266    case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
    267       return BLENDFACTOR_SRC_ALPHA_SATURATE;
    268    case PIPE_BLENDFACTOR_CONST_COLOR:
    269       return BLENDFACTOR_CONST_COLOR;
    270    case PIPE_BLENDFACTOR_CONST_ALPHA:
    271       return BLENDFACTOR_CONST_ALPHA;
    272    case PIPE_BLENDFACTOR_SRC1_COLOR:
    273       return BLENDFACTOR_SRC1_COLOR;
    274    case PIPE_BLENDFACTOR_SRC1_ALPHA:
    275       return BLENDFACTOR_SRC1_ALPHA;
    276    case PIPE_BLENDFACTOR_ZERO:
    277       return BLENDFACTOR_ZERO;
    278    case PIPE_BLENDFACTOR_INV_SRC_COLOR:
    279       return BLENDFACTOR_INV_SRC_COLOR;
    280    case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
    281       return BLENDFACTOR_INV_SRC_ALPHA;
    282    case PIPE_BLENDFACTOR_INV_DST_ALPHA:
    283       return BLENDFACTOR_INV_DST_ALPHA;
    284    case PIPE_BLENDFACTOR_INV_DST_COLOR:
    285       return BLENDFACTOR_INV_DST_COLOR;
    286    case PIPE_BLENDFACTOR_INV_CONST_COLOR:
    287       return BLENDFACTOR_INV_CONST_COLOR;
    288    case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
    289       return BLENDFACTOR_INV_CONST_ALPHA;
    290    case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
    291       return BLENDFACTOR_INV_SRC1_COLOR;
    292    case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
    293       return BLENDFACTOR_INV_SRC1_ALPHA;
    294    default:
    295       assert(0 && "Invalid blend factor");
    296       return BLENDFACTOR_ONE;
    297    }
    298 }
    299 
    300 static INLINE enum SWR_SURFACE_TYPE
    301 swr_convert_target_type(const enum pipe_texture_target target)
    302 {
    303    switch (target) {
    304    case PIPE_BUFFER:
    305       return SURFACE_BUFFER;
    306    case PIPE_TEXTURE_1D:
    307    case PIPE_TEXTURE_1D_ARRAY:
    308       return SURFACE_1D;
    309    case PIPE_TEXTURE_2D:
    310    case PIPE_TEXTURE_2D_ARRAY:
    311    case PIPE_TEXTURE_RECT:
    312       return SURFACE_2D;
    313    case PIPE_TEXTURE_3D:
    314       return SURFACE_3D;
    315    case PIPE_TEXTURE_CUBE:
    316    case PIPE_TEXTURE_CUBE_ARRAY:
    317       return SURFACE_CUBE;
    318    default:
    319       assert(0);
    320       return SURFACE_NULL;
    321    }
    322 }
    323 #endif
    324