Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

CcTextures.hpp

Go to the documentation of this file.
00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright (c) 2003 University of Waterloo Computer Graphics Laboratory
00004 // Project administrator: Michael D. McCool
00005 // Authors: Zheng Qin, Stefanus Du Toit, Kevin Moule, Tiberiu S. Popa,
00006 //          Michael D. McCool
00007 // 
00008 // This software is provided 'as-is', without any express or implied
00009 // warranty. In no event will the authors be held liable for any damages
00010 // arising from the use of this software.
00011 // 
00012 // Permission is granted to anyone to use this software for any purpose,
00013 // including commercial applications, and to alter it and redistribute it
00014 // freely, subject to the following restrictions:
00015 // 
00016 // 1. The origin of this software must not be misrepresented; you must
00017 // not claim that you wrote the original software. If you use this
00018 // software in a product, an acknowledgment in the product documentation
00019 // would be appreciated but is not required.
00020 // 
00021 // 2. Altered source versions must be plainly marked as such, and must
00022 // not be misrepresented as being the original software.
00023 // 
00024 // 3. This notice may not be removed or altered from any source
00025 // distribution.
00027 
00043 // @todo type
00044 // add casting code for when MemoryType is not equivalent to the type used in computation
00045 
00046 // floor and clamp to [0, max) for aninteger lookup
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 

Generated on Mon Jan 24 18:36:29 2005 for Sh by  doxygen 1.4.1