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