José Fonseca | b4f6907 | 2009-10-22 20:42:16 +0100 | [diff] [blame] | 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 | * Texture sampling -- common code. |
| 31 | * |
| 32 | * @author Jose Fonseca <jfonseca@vmware.com> |
| 33 | */ |
| 34 | |
| 35 | #include "pipe/p_defines.h" |
| 36 | #include "pipe/p_state.h" |
| 37 | #include "util/u_format.h" |
| 38 | #include "util/u_math.h" |
| 39 | #include "lp_bld_debug.h" |
José Fonseca | bc93e91 | 2009-10-22 22:37:06 +0100 | [diff] [blame] | 40 | #include "lp_bld_const.h" |
| 41 | #include "lp_bld_arit.h" |
| 42 | #include "lp_bld_type.h" |
José Fonseca | b4f6907 | 2009-10-22 20:42:16 +0100 | [diff] [blame] | 43 | #include "lp_bld_format.h" |
| 44 | #include "lp_bld_sample.h" |
| 45 | |
| 46 | |
| 47 | void |
| 48 | lp_sampler_static_state(struct lp_sampler_static_state *state, |
| 49 | const struct pipe_texture *texture, |
| 50 | const struct pipe_sampler_state *sampler) |
| 51 | { |
| 52 | memset(state, 0, sizeof *state); |
| 53 | |
| 54 | if(!texture) |
| 55 | return; |
| 56 | |
| 57 | if(!sampler) |
| 58 | return; |
| 59 | |
| 60 | state->format = texture->format; |
| 61 | state->target = texture->target; |
Roland Scheidegger | d509f84 | 2009-11-26 22:49:58 +0100 | [diff] [blame] | 62 | state->pot_width = util_is_pot(texture->width0); |
| 63 | state->pot_height = util_is_pot(texture->height0); |
| 64 | state->pot_depth = util_is_pot(texture->depth0); |
José Fonseca | b4f6907 | 2009-10-22 20:42:16 +0100 | [diff] [blame] | 65 | |
| 66 | state->wrap_s = sampler->wrap_s; |
| 67 | state->wrap_t = sampler->wrap_t; |
| 68 | state->wrap_r = sampler->wrap_r; |
| 69 | state->min_img_filter = sampler->min_img_filter; |
| 70 | state->min_mip_filter = sampler->min_mip_filter; |
| 71 | state->mag_img_filter = sampler->mag_img_filter; |
Michal Krol | 4e014c0 | 2010-01-06 14:12:10 +0100 | [diff] [blame] | 72 | state->compare_mode = sampler->compare_mode; |
| 73 | if(sampler->compare_mode != PIPE_TEX_COMPARE_NONE) { |
José Fonseca | b4f6907 | 2009-10-22 20:42:16 +0100 | [diff] [blame] | 74 | state->compare_func = sampler->compare_func; |
| 75 | } |
| 76 | state->normalized_coords = sampler->normalized_coords; |
| 77 | state->prefilter = sampler->prefilter; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /** |
| 82 | * Gather elements from scatter positions in memory into a single vector. |
| 83 | * |
| 84 | * @param src_width src element width |
| 85 | * @param dst_width result element width (source will be expanded to fit) |
| 86 | * @param length length of the offsets, |
| 87 | * @param base_ptr base pointer, should be a i8 pointer type. |
| 88 | * @param offsets vector with offsets |
| 89 | */ |
| 90 | LLVMValueRef |
| 91 | lp_build_gather(LLVMBuilderRef builder, |
| 92 | unsigned length, |
| 93 | unsigned src_width, |
| 94 | unsigned dst_width, |
| 95 | LLVMValueRef base_ptr, |
| 96 | LLVMValueRef offsets) |
| 97 | { |
| 98 | LLVMTypeRef src_type = LLVMIntType(src_width); |
| 99 | LLVMTypeRef src_ptr_type = LLVMPointerType(src_type, 0); |
| 100 | LLVMTypeRef dst_elem_type = LLVMIntType(dst_width); |
| 101 | LLVMTypeRef dst_vec_type = LLVMVectorType(dst_elem_type, length); |
| 102 | LLVMValueRef res; |
| 103 | unsigned i; |
| 104 | |
| 105 | res = LLVMGetUndef(dst_vec_type); |
| 106 | for(i = 0; i < length; ++i) { |
| 107 | LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0); |
| 108 | LLVMValueRef elem_offset; |
| 109 | LLVMValueRef elem_ptr; |
| 110 | LLVMValueRef elem; |
| 111 | |
| 112 | elem_offset = LLVMBuildExtractElement(builder, offsets, index, ""); |
| 113 | elem_ptr = LLVMBuildGEP(builder, base_ptr, &elem_offset, 1, ""); |
| 114 | elem_ptr = LLVMBuildBitCast(builder, elem_ptr, src_ptr_type, ""); |
| 115 | elem = LLVMBuildLoad(builder, elem_ptr, ""); |
| 116 | |
| 117 | assert(src_width <= dst_width); |
| 118 | if(src_width > dst_width) |
| 119 | elem = LLVMBuildTrunc(builder, elem, dst_elem_type, ""); |
| 120 | if(src_width < dst_width) |
| 121 | elem = LLVMBuildZExt(builder, elem, dst_elem_type, ""); |
| 122 | |
| 123 | res = LLVMBuildInsertElement(builder, res, elem, index, ""); |
| 124 | } |
| 125 | |
| 126 | return res; |
| 127 | } |
José Fonseca | bc93e91 | 2009-10-22 22:37:06 +0100 | [diff] [blame] | 128 | |
| 129 | |
| 130 | /** |
| 131 | * Compute the offset of a pixel. |
| 132 | * |
| 133 | * x, y, y_stride are vectors |
| 134 | */ |
| 135 | LLVMValueRef |
| 136 | lp_build_sample_offset(struct lp_build_context *bld, |
| 137 | const struct util_format_description *format_desc, |
| 138 | LLVMValueRef x, |
| 139 | LLVMValueRef y, |
| 140 | LLVMValueRef y_stride, |
| 141 | LLVMValueRef data_ptr) |
| 142 | { |
| 143 | LLVMValueRef x_stride; |
| 144 | LLVMValueRef offset; |
| 145 | |
| 146 | x_stride = lp_build_const_scalar(bld->type, format_desc->block.bits/8); |
| 147 | |
| 148 | if(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) { |
| 149 | LLVMValueRef x_lo, x_hi; |
| 150 | LLVMValueRef y_lo, y_hi; |
| 151 | LLVMValueRef x_stride_lo, x_stride_hi; |
| 152 | LLVMValueRef y_stride_lo, y_stride_hi; |
| 153 | LLVMValueRef x_offset_lo, x_offset_hi; |
| 154 | LLVMValueRef y_offset_lo, y_offset_hi; |
| 155 | LLVMValueRef offset_lo, offset_hi; |
| 156 | |
| 157 | x_lo = LLVMBuildAnd(bld->builder, x, bld->one, ""); |
| 158 | y_lo = LLVMBuildAnd(bld->builder, y, bld->one, ""); |
| 159 | |
| 160 | x_hi = LLVMBuildLShr(bld->builder, x, bld->one, ""); |
| 161 | y_hi = LLVMBuildLShr(bld->builder, y, bld->one, ""); |
| 162 | |
| 163 | x_stride_lo = x_stride; |
| 164 | y_stride_lo = lp_build_const_scalar(bld->type, 2*format_desc->block.bits/8); |
| 165 | |
| 166 | x_stride_hi = lp_build_const_scalar(bld->type, 4*format_desc->block.bits/8); |
| 167 | y_stride_hi = LLVMBuildShl(bld->builder, y_stride, bld->one, ""); |
| 168 | |
| 169 | x_offset_lo = lp_build_mul(bld, x_lo, x_stride_lo); |
| 170 | y_offset_lo = lp_build_mul(bld, y_lo, y_stride_lo); |
| 171 | offset_lo = lp_build_add(bld, x_offset_lo, y_offset_lo); |
| 172 | |
| 173 | x_offset_hi = lp_build_mul(bld, x_hi, x_stride_hi); |
| 174 | y_offset_hi = lp_build_mul(bld, y_hi, y_stride_hi); |
| 175 | offset_hi = lp_build_add(bld, x_offset_hi, y_offset_hi); |
| 176 | |
| 177 | offset = lp_build_add(bld, offset_hi, offset_lo); |
| 178 | } |
| 179 | else { |
| 180 | LLVMValueRef x_offset; |
| 181 | LLVMValueRef y_offset; |
| 182 | |
| 183 | x_offset = lp_build_mul(bld, x, x_stride); |
| 184 | y_offset = lp_build_mul(bld, y, y_stride); |
| 185 | |
| 186 | offset = lp_build_add(bld, x_offset, y_offset); |
| 187 | } |
| 188 | |
| 189 | return offset; |
| 190 | } |