00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00024
00040
00041
00042
00043
00044 template<typename T>
00045 inline int sh_cc_backend_nearest(T value)
00046 {
00047 return (int)(floor(static_cast<double>(value)));
00048 }
00049
00050 struct sh_gcc_backend_wrap_clamp
00051 {
00052 static inline int wrap(int src, int Max)
00053 {
00054 return src >= Max ? Max - 1 : (src < 0 ? 0 : src);
00055 }
00056 };
00057
00058 struct sh_gcc_backend_wrap_repeat
00059 {
00060 static int wrap(int src, int Max)
00061 {
00062 src %= Max;
00063 if(src < 0) src += Max;
00064 return src;
00065 }
00066 };
00067
00068 struct sh_gcc_backend_clamped
00069 {
00070 template<typename T>
00071 static inline T clamp(T dest)
00072 {
00073 return dest < 0 ? 0 : (dest > 1 ? 1 : dest);
00074 }
00075 };
00076
00077 struct sh_gcc_backend_unclamped
00078 {
00079 template<typename T>
00080 static inline T clamp(T dest)
00081 {
00082 return dest;
00083 }
00084 };
00085
00086 template<int TexDims, int TexSize, int TexWidth, int TexHeight, int TexDepth, typename TexType,
00087 typename SrcWrap, typename DestClamp,
00088 typename IndexType, typename MemoryType>
00089 void sh_cc_backend_lookupi(const void *texture, IndexType *src, MemoryType *dest)
00090 {
00091 const TexType* data = reinterpret_cast<const TexType*>(texture);
00092 int index = 0;
00093 if(TexDims == 3) index = SrcWrap::wrap(sh_cc_backend_nearest(src[2]), TexDepth);
00094 if(TexDims >= 2) index = SrcWrap::wrap(sh_cc_backend_nearest(src[1]), TexHeight)
00095 + TexHeight * index;
00096 index = SrcWrap::wrap(sh_cc_backend_nearest(src[0]), TexWidth)
00097 + TexWidth * index;
00098
00099 int start = index * TexSize;
00100 for(int i = 0; i < TexSize; ++i) {
00101 dest[i] = static_cast<MemoryType>(DestClamp::clamp(data[start + i]));
00102 }
00103 }
00104
00105 template<int TexDims, int TexSize, int TexWidth, int TexHeight, int TexDepth, typename TexType,
00106 typename SrcWrap, typename DestClamp,
00107 typename IndexType, typename MemoryType>
00108 void sh_cc_backend_lookup(const void *texture, IndexType *src, MemoryType *dest)
00109 {
00110 IndexType scaled_src[TexDims];
00111 scaled_src[0] = TexWidth * src[0];
00112 if(TexDims > 1) scaled_src[1] = TexHeight * src[1];
00113 if(TexDims > 2) scaled_src[2] = TexDepth * src[2];
00114
00115 sh_cc_backend_lookupi<TexDims, TexSize, TexWidth, TexHeight, TexDepth, TexType,
00116 SrcWrap, DestClamp>(texture, scaled_src, dest);
00117 }
00118