1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Texture sampling -- common code.
32 * @author Jose Fonseca <jfonseca@vmware.com>
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"
40 #include "lp_bld_const.h"
41 #include "lp_bld_arit.h"
42 #include "lp_bld_type.h"
43 #include "lp_bld_format.h"
44 #include "lp_bld_sample.h"
48 * Initialize lp_sampler_static_state object with the gallium sampler
50 * The former is considered to be static and the later dynamic.
53 lp_sampler_static_state(struct lp_sampler_static_state
*state
,
54 const struct pipe_sampler_view
*view
,
55 const struct pipe_sampler_state
*sampler
)
57 const struct pipe_resource
*texture
= view
->texture
;
59 memset(state
, 0, sizeof *state
);
68 * We don't copy sampler state over unless it is actually enabled, to avoid
69 * spurious recompiles, as the sampler static state is part of the shader
72 * Ideally the state tracker or cso_cache module would make all state
73 * canonical, but until that happens it's better to be safe than sorry here.
75 * XXX: Actually there's much more than can be done here, especially
76 * regarding 1D/2D/3D/CUBE textures, wrap modes, etc.
79 state
->format
= view
->format
;
80 state
->swizzle_r
= view
->swizzle_r
;
81 state
->swizzle_g
= view
->swizzle_g
;
82 state
->swizzle_b
= view
->swizzle_b
;
83 state
->swizzle_a
= view
->swizzle_a
;
85 state
->target
= texture
->target
;
86 state
->pot_width
= util_is_pot(texture
->width0
);
87 state
->pot_height
= util_is_pot(texture
->height0
);
88 state
->pot_depth
= util_is_pot(texture
->depth0
);
90 state
->wrap_s
= sampler
->wrap_s
;
91 state
->wrap_t
= sampler
->wrap_t
;
92 state
->wrap_r
= sampler
->wrap_r
;
93 state
->min_img_filter
= sampler
->min_img_filter
;
94 state
->mag_img_filter
= sampler
->mag_img_filter
;
95 if (view
->last_level
) {
96 state
->min_mip_filter
= sampler
->min_mip_filter
;
98 state
->min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
101 state
->compare_mode
= sampler
->compare_mode
;
102 if (sampler
->compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
103 state
->compare_func
= sampler
->compare_func
;
106 state
->normalized_coords
= sampler
->normalized_coords
;
107 state
->lod_bias
= sampler
->lod_bias
;
108 if (!view
->last_level
&&
109 sampler
->min_img_filter
== sampler
->mag_img_filter
) {
110 state
->min_lod
= 0.0f
;
111 state
->max_lod
= 0.0f
;
113 state
->min_lod
= MAX2(sampler
->min_lod
, 0.0f
);
114 state
->max_lod
= sampler
->max_lod
;
116 state
->border_color
[0] = sampler
->border_color
[0];
117 state
->border_color
[1] = sampler
->border_color
[1];
118 state
->border_color
[2] = sampler
->border_color
[2];
119 state
->border_color
[3] = sampler
->border_color
[3];
122 * FIXME: Handle the remainder of pipe_sampler_view.
128 * Compute the offset of a pixel block.
130 * x, y, z, y_stride, z_stride are vectors, and they refer to pixels.
132 * Returns the relative offset and i,j sub-block coordinates
135 lp_build_sample_offset(struct lp_build_context
*bld
,
136 const struct util_format_description
*format_desc
,
140 LLVMValueRef y_stride
,
141 LLVMValueRef z_stride
,
142 LLVMValueRef
*out_offset
,
146 LLVMValueRef x_stride
;
152 * Describe the coordinates in terms of pixel blocks.
154 * TODO: pixel blocks are power of two. LLVM should convert rem/div to
155 * bit arithmetic. Verify this.
158 if (format_desc
->block
.width
== 1) {
162 LLVMValueRef block_width
= lp_build_const_int_vec(bld
->type
, format_desc
->block
.width
);
163 i
= LLVMBuildURem(bld
->builder
, x
, block_width
, "");
164 x
= LLVMBuildUDiv(bld
->builder
, x
, block_width
, "");
167 if (format_desc
->block
.height
== 1) {
171 LLVMValueRef block_height
= lp_build_const_int_vec(bld
->type
, format_desc
->block
.height
);
172 j
= LLVMBuildURem(bld
->builder
, y
, block_height
, "");
173 y
= LLVMBuildUDiv(bld
->builder
, y
, block_height
, "");
176 x_stride
= lp_build_const_vec(bld
->type
, format_desc
->block
.bits
/8);
177 offset
= lp_build_mul(bld
, x
, x_stride
);
180 LLVMValueRef y_offset
= lp_build_mul(bld
, y
, y_stride
);
181 offset
= lp_build_add(bld
, offset
, y_offset
);
185 LLVMValueRef z_offset
= lp_build_mul(bld
, z
, z_stride
);
186 offset
= lp_build_add(bld
, offset
, z_offset
);
189 *out_offset
= offset
;