32 #include "texture_reference.h" 34 #include <cugar/basic/types.h> 35 #include <cugar/linalg/vector.h> 37 #define BILINEAR_TEXTURE_LOOKUPS 1 45 FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
46 float4 texture_load(
const float4* tex)
48 #ifdef FERMAT_DEVICE_COMPILATION 59 FERMAT_HOST_DEVICE float4& operator()(
const uint32 pixel) {
return c[pixel]; }
60 FERMAT_HOST_DEVICE
const float4& operator()(
const uint32 pixel)
const {
return texture_load(c + pixel); }
61 FERMAT_HOST_DEVICE float4& operator()(
const uint32 x,
const uint32 y) {
return c[y*res_x + x]; }
62 FERMAT_HOST_DEVICE
const float4& operator()(
const uint32 x,
const uint32 y)
const {
return texture_load(c + y*res_x + x); }
63 FERMAT_HOST_DEVICE
const float4* ptr()
const {
return c; }
64 FERMAT_HOST_DEVICE float4* ptr() {
return c; }
75 FERMAT_HOST_DEVICE float4& operator()(
const uint32 pixel,
const uint32 lod) {
return levels[lod](pixel); }
76 FERMAT_HOST_DEVICE
const float4& operator()(
const uint32 pixel,
const uint32 lod)
const {
return reinterpret_cast<const TextureView*
>(levels)[lod](pixel); }
77 FERMAT_HOST_DEVICE float4& operator()(
const uint32 x,
const uint32 y,
const uint32 lod) {
return levels[lod](x,y); }
78 FERMAT_HOST_DEVICE
const float4& operator()(
const uint32 x,
const uint32 y,
const uint32 lod)
const {
return reinterpret_cast<const TextureView*
>(levels)[lod](x,y); }
86 FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
89 if (texture_ref.texture == uint32(-1) || textures[texture_ref.texture].n_levels == 0)
92 st.x *= texture_ref.scaling.x;
93 st.y *= texture_ref.scaling.y;
98 const MipMapView texture = textures[texture_ref.texture];
99 if (texture.n_levels > 0)
101 const uint32 x = cugar::min(uint32(st.x * texture.res_x), texture.res_x - 1);
102 const uint32 y = cugar::min(uint32(st.y * texture.res_y), texture.res_y - 1);
103 return texture(x, y, 0);
106 return default_value;
109 FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
110 void unchecked_quad_texture_lookup(uint2 texel,
const MipMapView texture,
const uint32 lod,
const float4 default_value,
cugar::Vector4f quad[4])
112 const uint32 x = texel.x;
113 const uint32 y = texel.y;
114 const uint32 xx = (x + 1) % texture.res_x;
115 const uint32 yy = (y + 1) % texture.res_y;
117 quad[0] = texture(x, y, lod);
118 quad[1] = texture(xx, y, lod);
119 quad[2] = texture(x, yy, lod);
120 quad[3] = texture(xx, yy, lod);
123 FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
126 if (texture_ref.texture == uint32(-1) || textures[texture_ref.texture].n_levels == 0)
128 quad[0] = quad[1] = quad[2] = quad[3] = default_value;
132 const MipMapView texture = textures[texture_ref.texture];
133 if (texture.n_levels > lod)
136 unchecked_quad_texture_lookup( texel, texture, lod, default_value, quad );
139 quad[0] = quad[1] = quad[2] = quad[3] = default_value;
142 FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
145 if (texture_ref.texture == uint32(-1) || textures[texture_ref.texture].n_levels == 0)
146 return default_value;
148 const MipMapView texture = textures[texture_ref.texture];
149 if (texture.n_levels > 0)
151 const uint32 x = uint32(st.x) % texture.res_x;
152 const uint32 y = uint32(st.y) % texture.res_y;
156 const uint32 lod = 0u;
157 unchecked_quad_texture_lookup( make_uint2(x,y), texture, lod, default_value, quad );
163 (quad[0] * (1-u) + quad[1] * u) * (1-v) +
164 (quad[2] * (1-u) + quad[3] * u) * v;
167 return default_value;
170 FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
173 if (texture_ref.texture == uint32(-1) || textures[texture_ref.texture].n_levels == 0)
174 return default_value;
176 st.x *= texture_ref.scaling.x;
177 st.y *= texture_ref.scaling.y;
182 const MipMapView texture = textures[texture_ref.texture];
183 if (texture.n_levels > 0)
185 const uint32 x = cugar::min(uint32(st.x * texture.res_x), texture.res_x - 1);
186 const uint32 y = cugar::min(uint32(st.y * texture.res_y), texture.res_y - 1);
190 const uint32 lod = 0u;
191 unchecked_quad_texture_lookup( make_uint2(x,y), texture, lod, default_value, quad );
193 const float u =
cugar::mod(st.x * texture.res_x, 1.0f);
194 const float v =
cugar::mod(st.y * texture.res_y, 1.0f);
197 (quad[0] * (1-u) + quad[1] * u) * (1-v) +
198 (quad[2] * (1-u) + quad[3] * u) * v;
201 return default_value;
204 FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
207 #if BILINEAR_TEXTURE_LOOKUPS 208 return bilinear_texture_lookup( st, texture_ref, textures, default_value );
210 return unfiltered_texture_lookup( st, texture_ref, textures, default_value );
214 FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
217 if (texture_ref.texture == uint32(-1) || textures[texture_ref.texture].n_levels == 0)
218 return default_value;
220 st.x *= texture_ref.scaling.x;
221 st.y *= texture_ref.scaling.y;
226 const MipMapView texture = textures[texture_ref.texture];
227 if (texture.n_levels > 0)
230 st *=
cugar::Vector4f(
float(texture.res_x),
float(texture.res_y), 1, 1);
236 const float i = (c.x + c.y + c.z);
237 const float iu = (cu.x + cu.y + cu.z);
238 const float iv = (cv.x + cv.y + cv.z);
242 return default_value;
Definition: texture_reference.h:41
float CUGAR_HOST_DEVICE mod(const float x, const float m)
Definition: numbers.h:606
Definition: texture_view.h:57
Definition: texture_view.h:73