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 template<int TexDims, int TexSize, int TexWidth, int TexHeight, int TexDepth, typename TexType,
00069   typename SrcWrap, typename IndexType, typename MemoryType> 
00070 void sh_cc_backend_lookupi(const void *texture, IndexType *src, MemoryType *dest)
00071 {
00072   const TexType* data = reinterpret_cast<const TexType*>(texture);
00073   int index = 0;
00074   if(TexDims == 3) index = SrcWrap::wrap(sh_cc_backend_nearest(src[2]), TexDepth);
00075   if(TexDims >= 2) index = SrcWrap::wrap(sh_cc_backend_nearest(src[1]), TexHeight) 
00076       + TexHeight * index;
00077   index = SrcWrap::wrap(sh_cc_backend_nearest(src[0]), TexWidth)
00078           + TexWidth * index;
00079 
00080   int start = index * TexSize; 
00081   for(int i = 0; i < TexSize; ++i) {
00082     dest[i] = static_cast<MemoryType>(data[start + i]); 
00083   }
00084 }
00085 
00086 template<int TexDims, int TexSize, int TexWidth, int TexHeight, int TexDepth, typename TexType,
00087   typename SrcWrap, typename IndexType, typename MemoryType> 
00088 void sh_cc_backend_lookup(const void *texture, IndexType *src, MemoryType *dest)
00089 {
00090   IndexType scaled_src[TexDims];
00091   scaled_src[0] = TexWidth * src[0];
00092   if(TexDims > 1) scaled_src[1] = TexHeight * src[1];
00093   if(TexDims > 2) scaled_src[2] = TexDepth * src[2];
00094 
00095   sh_cc_backend_lookupi<TexDims, TexSize, TexWidth, TexHeight, TexDepth, TexType, 
00096     SrcWrap>(texture, scaled_src, dest);
00097 }
00098 

Generated on Wed Jun 15 18:12:37 2005 for Sh by  doxygen 1.4.3-20050530