Home | History | Annotate | Download | only in draw
      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 /**
     29  * Texture sampling code generation
     30  * @author Jose Fonseca <jfonseca (at) vmware.com>
     31  */
     32 
     33 #include "pipe/p_defines.h"
     34 #include "pipe/p_shader_tokens.h"
     35 #include "gallivm/lp_bld_const.h"
     36 #include "gallivm/lp_bld_debug.h"
     37 #include "gallivm/lp_bld_type.h"
     38 #include "gallivm/lp_bld_sample.h"
     39 #include "gallivm/lp_bld_tgsi.h"
     40 
     41 
     42 #include "util/u_debug.h"
     43 #include "util/u_memory.h"
     44 #include "util/u_pointer.h"
     45 #include "util/u_string.h"
     46 
     47 #include "draw_llvm.h"
     48 
     49 
     50 /**
     51  * This provides the bridge between the sampler state store in
     52  * lp_jit_context and lp_jit_texture and the sampler code
     53  * generator. It provides the texture layout information required by
     54  * the texture sampler code generator in terms of the state stored in
     55  * lp_jit_context and lp_jit_texture in runtime.
     56  */
     57 struct draw_llvm_sampler_dynamic_state
     58 {
     59    struct lp_sampler_dynamic_state base;
     60 
     61    const struct lp_sampler_static_state *static_state;
     62 
     63    LLVMValueRef context_ptr;
     64 };
     65 
     66 
     67 /**
     68  * This is the bridge between our sampler and the TGSI translator.
     69  */
     70 struct draw_llvm_sampler_soa
     71 {
     72    struct lp_build_sampler_soa base;
     73 
     74    struct draw_llvm_sampler_dynamic_state dynamic_state;
     75 };
     76 
     77 
     78 /**
     79  * Fetch the specified member of the lp_jit_texture structure.
     80  * \param emit_load  if TRUE, emit the LLVM load instruction to actually
     81  *                   fetch the field's value.  Otherwise, just emit the
     82  *                   GEP code to address the field.
     83  *
     84  * @sa http://llvm.org/docs/GetElementPtr.html
     85  */
     86 static LLVMValueRef
     87 draw_llvm_texture_member(const struct lp_sampler_dynamic_state *base,
     88                          struct gallivm_state *gallivm,
     89                          unsigned unit,
     90                          unsigned member_index,
     91                          const char *member_name,
     92                          boolean emit_load)
     93 {
     94    LLVMBuilderRef builder = gallivm->builder;
     95    struct draw_llvm_sampler_dynamic_state *state =
     96       (struct draw_llvm_sampler_dynamic_state *)base;
     97    LLVMValueRef indices[4];
     98    LLVMValueRef ptr;
     99    LLVMValueRef res;
    100 
    101    debug_assert(unit < PIPE_MAX_SAMPLERS);
    102 
    103    /* context[0] */
    104    indices[0] = lp_build_const_int32(gallivm, 0);
    105    /* context[0].textures */
    106    indices[1] = lp_build_const_int32(gallivm, DRAW_JIT_CTX_TEXTURES);
    107    /* context[0].textures[unit] */
    108    indices[2] = lp_build_const_int32(gallivm, unit);
    109    /* context[0].textures[unit].member */
    110    indices[3] = lp_build_const_int32(gallivm, member_index);
    111 
    112    ptr = LLVMBuildGEP(builder, state->context_ptr, indices, Elements(indices), "");
    113 
    114    if (emit_load)
    115       res = LLVMBuildLoad(builder, ptr, "");
    116    else
    117       res = ptr;
    118 
    119    lp_build_name(res, "context.texture%u.%s", unit, member_name);
    120 
    121    return res;
    122 }
    123 
    124 
    125 /**
    126  * Helper macro to instantiate the functions that generate the code to
    127  * fetch the members of lp_jit_texture to fulfill the sampler code
    128  * generator requests.
    129  *
    130  * This complexity is the price we have to pay to keep the texture
    131  * sampler code generator a reusable module without dependencies to
    132  * llvmpipe internals.
    133  */
    134 #define DRAW_LLVM_TEXTURE_MEMBER(_name, _index, _emit_load)  \
    135    static LLVMValueRef \
    136    draw_llvm_texture_##_name( const struct lp_sampler_dynamic_state *base, \
    137                               struct gallivm_state *gallivm,               \
    138                               unsigned unit)                            \
    139    { \
    140       return draw_llvm_texture_member(base, gallivm, unit, _index, #_name, _emit_load ); \
    141    }
    142 
    143 
    144 DRAW_LLVM_TEXTURE_MEMBER(width,      DRAW_JIT_TEXTURE_WIDTH, TRUE)
    145 DRAW_LLVM_TEXTURE_MEMBER(height,     DRAW_JIT_TEXTURE_HEIGHT, TRUE)
    146 DRAW_LLVM_TEXTURE_MEMBER(depth,      DRAW_JIT_TEXTURE_DEPTH, TRUE)
    147 DRAW_LLVM_TEXTURE_MEMBER(first_level,DRAW_JIT_TEXTURE_FIRST_LEVEL, TRUE)
    148 DRAW_LLVM_TEXTURE_MEMBER(last_level, DRAW_JIT_TEXTURE_LAST_LEVEL, TRUE)
    149 DRAW_LLVM_TEXTURE_MEMBER(row_stride, DRAW_JIT_TEXTURE_ROW_STRIDE, FALSE)
    150 DRAW_LLVM_TEXTURE_MEMBER(img_stride, DRAW_JIT_TEXTURE_IMG_STRIDE, FALSE)
    151 DRAW_LLVM_TEXTURE_MEMBER(data_ptr,   DRAW_JIT_TEXTURE_DATA, FALSE)
    152 DRAW_LLVM_TEXTURE_MEMBER(min_lod,    DRAW_JIT_TEXTURE_MIN_LOD, TRUE)
    153 DRAW_LLVM_TEXTURE_MEMBER(max_lod,    DRAW_JIT_TEXTURE_MAX_LOD, TRUE)
    154 DRAW_LLVM_TEXTURE_MEMBER(lod_bias,   DRAW_JIT_TEXTURE_LOD_BIAS, TRUE)
    155 DRAW_LLVM_TEXTURE_MEMBER(border_color, DRAW_JIT_TEXTURE_BORDER_COLOR, FALSE)
    156 
    157 
    158 static void
    159 draw_llvm_sampler_soa_destroy(struct lp_build_sampler_soa *sampler)
    160 {
    161    FREE(sampler);
    162 }
    163 
    164 
    165 /**
    166  * Fetch filtered values from texture.
    167  * The 'texel' parameter returns four vectors corresponding to R, G, B, A.
    168  */
    169 static void
    170 draw_llvm_sampler_soa_emit_fetch_texel(const struct lp_build_sampler_soa *base,
    171                                        struct gallivm_state *gallivm,
    172                                        struct lp_type type,
    173                                        unsigned unit,
    174                                        unsigned num_coords,
    175                                        const LLVMValueRef *coords,
    176                                        const struct lp_derivatives *derivs,
    177                                        LLVMValueRef lod_bias, /* optional */
    178                                        LLVMValueRef explicit_lod, /* optional */
    179                                        LLVMValueRef *texel)
    180 {
    181    struct draw_llvm_sampler_soa *sampler = (struct draw_llvm_sampler_soa *)base;
    182 
    183    assert(unit < PIPE_MAX_SAMPLERS);
    184 
    185    lp_build_sample_soa(gallivm,
    186                        &sampler->dynamic_state.static_state[unit],
    187                        &sampler->dynamic_state.base,
    188                        type,
    189                        unit,
    190                        num_coords, coords,
    191                        derivs,
    192                        lod_bias, explicit_lod,
    193                        texel);
    194 }
    195 
    196 
    197 /**
    198  * Fetch the texture size.
    199  */
    200 static void
    201 draw_llvm_sampler_soa_emit_size_query(const struct lp_build_sampler_soa *base,
    202                                       struct gallivm_state *gallivm,
    203                                       struct lp_type type,
    204                                       unsigned unit,
    205                                       LLVMValueRef explicit_lod, /* optional */
    206                                       LLVMValueRef *sizes_out)
    207 {
    208    struct draw_llvm_sampler_soa *sampler = (struct draw_llvm_sampler_soa *)base;
    209 
    210    assert(unit < PIPE_MAX_SAMPLERS);
    211 
    212    lp_build_size_query_soa(gallivm,
    213                            &sampler->dynamic_state.static_state[unit],
    214                            &sampler->dynamic_state.base,
    215 			   type,
    216                            unit,
    217                            explicit_lod,
    218                            sizes_out);
    219 }
    220 
    221 struct lp_build_sampler_soa *
    222 draw_llvm_sampler_soa_create(const struct lp_sampler_static_state *static_state,
    223                              LLVMValueRef context_ptr)
    224 {
    225    struct draw_llvm_sampler_soa *sampler;
    226 
    227    sampler = CALLOC_STRUCT(draw_llvm_sampler_soa);
    228    if(!sampler)
    229       return NULL;
    230 
    231    sampler->base.destroy = draw_llvm_sampler_soa_destroy;
    232    sampler->base.emit_fetch_texel = draw_llvm_sampler_soa_emit_fetch_texel;
    233    sampler->base.emit_size_query = draw_llvm_sampler_soa_emit_size_query;
    234    sampler->dynamic_state.base.width = draw_llvm_texture_width;
    235    sampler->dynamic_state.base.height = draw_llvm_texture_height;
    236    sampler->dynamic_state.base.depth = draw_llvm_texture_depth;
    237    sampler->dynamic_state.base.first_level = draw_llvm_texture_first_level;
    238    sampler->dynamic_state.base.last_level = draw_llvm_texture_last_level;
    239    sampler->dynamic_state.base.row_stride = draw_llvm_texture_row_stride;
    240    sampler->dynamic_state.base.img_stride = draw_llvm_texture_img_stride;
    241    sampler->dynamic_state.base.data_ptr = draw_llvm_texture_data_ptr;
    242    sampler->dynamic_state.base.min_lod = draw_llvm_texture_min_lod;
    243    sampler->dynamic_state.base.max_lod = draw_llvm_texture_max_lod;
    244    sampler->dynamic_state.base.lod_bias = draw_llvm_texture_lod_bias;
    245    sampler->dynamic_state.base.border_color = draw_llvm_texture_border_color;
    246    sampler->dynamic_state.static_state = static_state;
    247    sampler->dynamic_state.context_ptr = context_ptr;
    248 
    249    return &sampler->base;
    250 }
    251 
    252