00001 const char* cc_texture_string[] = {
00002 "// Sh: A GPU metaprogramming language.\n",
00003 "//\n",
00004 "// Copyright (c) 2003 University of Waterloo Computer Graphics Laboratory\n",
00005 "// Project administrator: Michael D. McCool\n",
00006 "// Authors: Zheng Qin, Stefanus Du Toit, Kevin Moule, Tiberiu S. Popa,\n",
00007 "// Michael D. McCool\n",
00008 "// \n",
00009 "// This software is provided 'as-is', without any express or implied\n",
00010 "// warranty. In no event will the authors be held liable for any damages\n",
00011 "// arising from the use of this software.\n",
00012 "// \n",
00013 "// Permission is granted to anyone to use this software for any purpose,\n",
00014 "// including commercial applications, and to alter it and redistribute it\n",
00015 "// freely, subject to the following restrictions:\n",
00016 "// \n",
00017 "// 1. The origin of this software must not be misrepresented; you must\n",
00018 "// not claim that you wrote the original software. If you use this\n",
00019 "// software in a product, an acknowledgment in the product documentation\n",
00020 "// would be appreciated but is not required.\n",
00021 "// \n",
00022 "// 2. Altered source versions must be plainly marked as such, and must\n",
00023 "// not be misrepresented as being the original software.\n",
00024 "// \n",
00025 "// 3. This notice may not be removed or altered from any source\n",
00026 "// distribution.\n",
00027 "
00028 "\n",
00029 "\n",
00043 "\n",
00044 "
00045 "// add casting code for when MemoryType is not equivalent to the type used in computation\n",
00046 "\n",
00047 "// floor and clamp to [0, max) for aninteger lookup\n",
00048 "template<typename T>\n",
00049 "inline int sh_cc_backend_nearest(T value)\n",
00050 "{\n",
00051 " return (int)(floor(static_cast<double>(value))); \n",
00052 "}\n",
00053 "\n",
00054 "struct sh_gcc_backend_wrap_clamp\n",
00055 "{\n",
00056 " static inline int wrap(int src, int Max) \n",
00057 " {\n",
00058 " return src >= Max ? Max - 1 : (src < 0 ? 0 : src);\n",
00059 " }\n",
00060 "};\n",
00061 "\n",
00062 "struct sh_gcc_backend_wrap_repeat\n",
00063 "{\n",
00064 " static inline int wrap(int src, int Max) \n",
00065 " {\n",
00066 " src %= Max;\n",
00067 " if(src < 0) src += Max;\n",
00068 " return src; \n",
00069 " }\n",
00070 "};\n",
00071 "\n",
00072 "struct sh_gcc_backend_clamped\n",
00073 "{\n",
00074 " template<typename T>\n",
00075 " static inline T clamp(T dest) \n",
00076 " {\n",
00077 " return dest < 0 ? 0 : (dest > 1 ? 1 : dest);\n",
00078 " }\n",
00079 "};\n",
00080 "\n",
00081 "struct sh_gcc_backend_unclamped\n",
00082 "{\n",
00083 " template<typename T>\n",
00084 " static inline T clamp(T dest) \n",
00085 " {\n",
00086 " return dest; \n",
00087 " }\n",
00088 "};\n",
00089 "\n",
00090 "template<int TexDims, int TexSize, int TexWidth, int TexHeight, int TexDepth, typename TexType,\n",
00091 " typename SrcWrap, typename DestClamp,\n",
00092 " typename IndexType, typename MemoryType> \n",
00093 "void sh_cc_backend_lookupi(const void *texture, IndexType *src, MemoryType *dest)\n",
00094 "{\n",
00095 " const TexType* data = reinterpret_cast<const TexType*>(texture);\n",
00096 " int index = 0;\n",
00097 " if(TexDims == 3) index = SrcWrap::wrap(sh_cc_backend_nearest(src[2]), TexDepth);\n",
00098 " if(TexDims >= 2) index = SrcWrap::wrap(sh_cc_backend_nearest(src[1]), TexHeight) \n",
00099 " + TexHeight * index;\n",
00100 " index = SrcWrap::wrap(sh_cc_backend_nearest(src[0]), TexWidth)\n",
00101 " + TexWidth * index;\n",
00102 "\n",
00103 " int start = index * TexSize; \n",
00104 " for(int i = 0; i < TexSize; ++i) {\n",
00105 " dest[i] = static_cast<MemoryType>(DestClamp::clamp(data[start + i])); \n",
00106 " }\n",
00107 "}\n",
00108 "\n",
00109 "template<int TexDims, int TexSize, int TexWidth, int TexHeight, int TexDepth, typename TexType,\n",
00110 " typename SrcWrap, typename DestClamp,\n",
00111 " typename IndexType, typename MemoryType> \n",
00112 "void sh_cc_backend_lookup(const void *texture, IndexType *src, MemoryType *dest)\n",
00113 "{\n",
00114 " IndexType scaled_src[TexDims];\n",
00115 " scaled_src[0] = TexWidth * src[0];\n",
00116 " if(TexDims > 1) scaled_src[1] = TexHeight * src[1];\n",
00117 " if(TexDims > 2) scaled_src[2] = TexDepth * src[2];\n",
00118 "\n",
00119 " sh_cc_backend_lookupi<TexDims, TexSize, TexWidth, TexHeight, TexDepth, TexType, \n",
00120 " SrcWrap, DestClamp>(texture, scaled_src, dest);\n",
00121 "}\n",
00122 "\n",
00123 ""};