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 2003-2005 Serious Hack Inc.
00004 // 
00005 // This software is provided 'as-is', without any express or implied
00006 // warranty. In no event will the authors be held liable for any damages
00007 // arising from the use of this software.
00008 // 
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it
00011 // freely, subject to the following restrictions:
00012 // 
00013 // 1. The origin of this software must not be misrepresented; you must
00014 // not claim that you wrote the original software. If you use this
00015 // software in a product, an acknowledgment in the product documentation
00016 // would be appreciated but is not required.
00017 // 
00018 // 2. Altered source versions must be plainly marked as such, and must
00019 // not be misrepresented as being the original software.
00020 // 
00021 // 3. This notice may not be removed or altered from any source
00022 // distribution.
00024 
00040 // @todo type
00041 // add casting code for when MemoryType is not equivalent to the type used in computation
00042 
00043 // floor and clamp to [0, max) for aninteger lookup
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 

Generated on Thu Apr 21 17:32:45 2005 for Sh by  doxygen 1.4.2