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 library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
00018 // MA  02110-1301, USA
00020 
00036 // @todo type
00037 // add casting code for when MemoryType is not equivalent to the type used in computation
00038 
00039 // floor and clamp to [0, max) for aninteger lookup
00040 template<typename T>
00041 inline int sh_cc_backend_nearest(T value)
00042 {
00043   return (int)(floor(static_cast<double>(value))); 
00044 }
00045 
00046 struct sh_gcc_backend_wrap_clamp
00047 {
00048   static inline int wrap(int src, int Max) 
00049   {
00050     return src >= Max ? Max - 1 : (src < 0 ? 0 : src);
00051   }
00052 };
00053 
00054 struct sh_gcc_backend_wrap_repeat
00055 {
00056   static int wrap(int src, int Max) 
00057   {
00058     src %= Max;
00059     if(src < 0) src += Max;
00060     return src; 
00061   }
00062 };
00063 
00064 template<int TexDims, int TexSize, int TexWidth, int TexHeight, int TexDepth, typename TexType,
00065   typename SrcWrap, typename IndexType, typename MemoryType> 
00066 void sh_cc_backend_lookupi(const void *texture, IndexType *src, MemoryType *dest)
00067 {
00068   const TexType* data = reinterpret_cast<const TexType*>(texture);
00069   int index = 0;
00070   if(TexDims == 3) index = SrcWrap::wrap(sh_cc_backend_nearest(src[2]), TexDepth);
00071   if(TexDims >= 2) index = SrcWrap::wrap(sh_cc_backend_nearest(src[1]), TexHeight) 
00072       + TexHeight * index;
00073   index = SrcWrap::wrap(sh_cc_backend_nearest(src[0]), TexWidth)
00074           + TexWidth * index;
00075 
00076   int start = index * TexSize; 
00077   for(int i = 0; i < TexSize; ++i) {
00078     dest[i] = static_cast<MemoryType>(data[start + i]); 
00079   }
00080 }
00081 
00082 template<int TexDims, int TexSize, int TexWidth, int TexHeight, int TexDepth, typename TexType,
00083   typename SrcWrap, typename IndexType, typename MemoryType> 
00084 void sh_cc_backend_lookup(const void *texture, IndexType *src, MemoryType *dest)
00085 {
00086   IndexType scaled_src[TexDims];
00087   scaled_src[0] = TexWidth * src[0];
00088   if(TexDims > 1) scaled_src[1] = TexHeight * src[1];
00089   if(TexDims > 2) scaled_src[2] = TexDepth * src[2];
00090 
00091   sh_cc_backend_lookupi<TexDims, TexSize, TexWidth, TexHeight, TexDepth, TexType, 
00092     SrcWrap>(texture, scaled_src, dest);
00093 }
00094 

Generated on Thu Jul 28 17:33:00 2005 for Sh by  doxygen 1.4.3-20050530