Home | History | Annotate | Download | only in gallivm
      1 /**************************************************************************
      2  *
      3  * Copyright 2012 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 #include "lp_bld_const.h"
     29 #include "lp_bld_struct.h"
     30 #include "lp_bld_format.h"
     31 #include "lp_bld_debug.h"
     32 #include "lp_bld_type.h"
     33 #include "lp_bld_conv.h"
     34 #include "lp_bld_pack.h"
     35 
     36 #include "util/u_memory.h"
     37 #include "util/u_format.h"
     38 #include "pipe/p_state.h"
     39 
     40 /**
     41  * @brief lp_build_fetch_rgba_aos_array
     42  *
     43  * \param format_desc   describes format of the image we're fetching from
     44  * \param dst_type      output type
     45  * \param base_ptr      address of the pixel block (or the texel if uncompressed)
     46  * \param offset        ptr offset
     47  */
     48 LLVMValueRef
     49 lp_build_fetch_rgba_aos_array(struct gallivm_state *gallivm,
     50                               const struct util_format_description *format_desc,
     51                               struct lp_type dst_type,
     52                               LLVMValueRef base_ptr,
     53                               LLVMValueRef offset)
     54 {
     55    struct lp_build_context bld;
     56    LLVMBuilderRef builder = gallivm->builder;
     57    LLVMTypeRef src_vec_type;
     58    LLVMValueRef ptr, res = NULL;
     59    struct lp_type src_type;
     60 
     61    memset(&src_type, 0, sizeof src_type);
     62    src_type.floating = format_desc->channel[0].type == UTIL_FORMAT_TYPE_FLOAT;
     63    src_type.fixed    = format_desc->channel[0].type == UTIL_FORMAT_TYPE_FIXED;
     64    src_type.sign     = format_desc->channel[0].type != UTIL_FORMAT_TYPE_UNSIGNED;
     65    src_type.norm     = format_desc->channel[0].normalized;
     66    src_type.width    = format_desc->channel[0].size;
     67    src_type.length   = format_desc->nr_channels;
     68 
     69    assert(src_type.length <= dst_type.length);
     70 
     71    src_vec_type  = lp_build_vec_type(gallivm,  src_type);
     72 
     73    /* Read whole vector from memory, unaligned */
     74    if (!res) {
     75       ptr = LLVMBuildGEP(builder, base_ptr, &offset, 1, "");
     76       ptr = LLVMBuildPointerCast(builder, ptr, LLVMPointerType(src_vec_type, 0), "");
     77       res = LLVMBuildLoad(builder, ptr, "");
     78       lp_set_load_alignment(res, src_type.width / 8);
     79    }
     80 
     81    /* Truncate doubles to float */
     82    if (src_type.floating && src_type.width == 64) {
     83       src_type.width = 32;
     84       src_vec_type  = lp_build_vec_type(gallivm,  src_type);
     85 
     86       res = LLVMBuildFPTrunc(builder, res, src_vec_type, "");
     87    }
     88 
     89    /* Expand to correct length */
     90    if (src_type.length < dst_type.length) {
     91       res = lp_build_pad_vector(gallivm, res, src_type, dst_type.length);
     92       src_type.length = dst_type.length;
     93    }
     94 
     95    /* Convert to correct format */
     96    lp_build_conv(gallivm, src_type, dst_type, &res, 1, &res, 1);
     97 
     98    /* Swizzle it */
     99    lp_build_context_init(&bld, gallivm, dst_type);
    100    return lp_build_format_swizzle_aos(format_desc, &bld, res);
    101 }
    102