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

ShManipulator.hpp

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 #ifndef SHMANIPULATOR_HPP
00028 #define SHMANIPULATOR_HPP
00029 
00030 #include "ShProgram.hpp"
00031 #include <vector>
00032 #include <string>
00033 #include <sstream>
00034 
00035 namespace SH {
00036 
00047 template<typename T> 
00048 struct storage_trait {
00049   typedef T StorageType;
00050 };
00051 
00052 template<>
00053 struct storage_trait<const char*> {
00054   typedef std::string StorageType; 
00055 };
00056 
00057 enum OffsetRangeErrors {
00058   OFFSET_RANGE_BAD_OFFSET = -1,
00059   OFFSET_RANGE_BAD_INDEX = -2
00060 };
00061 
00062 template<typename T>
00063 class OffsetRange {
00064   public:
00065     OffsetRange(); 
00066     OffsetRange( T start, T end );
00067     OffsetRange( T start, int startOffset, T end, int endOffset );
00068 
00075     int absStartIndex( const ShProgramNode::VarList vars ) const; 
00076     int absEndIndex( const ShProgramNode::VarList vars ) const; 
00077 
00078     std::string toString() const; 
00079 
00080   private:
00081     T start, end;
00082     int startOffset, endOffset;
00083     int absIndex( T index, int offset, const ShProgramNode::VarList &vars ) const;
00084 };
00085 
00086 
00098 template<typename T>
00099 class ShManipulator {
00100   public:
00101     typedef typename storage_trait<T>::StorageType StorageType;
00102     typedef OffsetRange<StorageType> IndexRange;
00103     typedef std::vector<IndexRange> IndexRangeVector;
00104 
00107     ShManipulator();
00108     ~ShManipulator();
00109 
00110     ShManipulator<T>& operator()(T i);
00111     ShManipulator<T>& operator()(T start, T end);
00112     ShManipulator<T>& operator()(const IndexRange &range); 
00113 
00114     // converts ranges to a sequence of integer ranges using given var list 
00115     IndexRangeVector getRanges() const; 
00116 
00117     // converts to string for debugging/error messages
00118     std::string toString() const;
00119     
00120   protected:
00121     IndexRangeVector m_ranges; 
00122 
00123     // converts indices to positive integer indices. 
00124     // If it cannot be found, raises AlgebraException.
00125     // if the index has an offset that makes it invalid, then valid is set to false
00126     OffsetRange<int> convertRange(IndexRange range, const ShProgramNode::VarList &v) const;
00127 };
00128 
00137 template<typename T>
00138 ShProgram operator<<(const ShProgram &p, const ShManipulator<T> &m); 
00139 
00146 template<typename T>
00147 ShProgram operator<<(const ShManipulator<T> &m, const ShProgram &p);
00148 
00149 
00151 // shader outputs based on given indices
00152 //
00162 template<typename T>
00163 ShManipulator<T> shSwizzle(T i0);
00164 
00165 template<typename T>
00166 ShManipulator<T> shSwizzle(T i0, T i1);
00167 
00168 template<typename T>
00169 ShManipulator<T> shSwizzle(T i0, T i1, T i2);
00170 
00171 template<typename T>
00172 ShManipulator<T> shSwizzle(T i0, T i1, T i2, T i3);
00173 
00174 template<typename T>
00175 ShManipulator<T> shSwizzle(T i0, T i1, T i2, T i3, T i4);
00176 
00177 template<typename T>
00178 ShManipulator<T> shSwizzle(T i0, T i1, T i2, T i3, T i4, T i5);
00179 
00180 template<typename T>
00181 ShManipulator<T> shSwizzle(T i0, T i1, T i2, T i3, T i4, T i5, T i6);
00182 
00183 template<typename T>
00184 ShManipulator<T> shSwizzle(T i0, T i1, T i2, T i3, T i4, T i5, T i6, T i7);
00185 
00186 template<typename T>
00187 ShManipulator<T> shSwizzle(T i0, T i1, T i2, T i3, T i4, T i5, T i6, T i7, T i8);
00188 
00189 template<typename T>
00190 ShManipulator<T> shSwizzle(T i0, T i1, T i2, T i3, T i4, T i5, T i6, T i7, T i8, T i9);
00191 
00192 template<typename T>
00193 ShManipulator<T> shSwizzle(std::vector<T> indices);
00194 
00196 // outputs based on given indices
00197 template<typename T>
00198 ShManipulator<T> shRange(T i);
00199 
00200 template<typename T>
00201 ShManipulator<T> shRange(T start, T end);
00202 
00213 template<typename T>
00214 ShManipulator<T> shExtract(T k); 
00215 
00227 template<typename T>
00228 ShManipulator<T> shInsert(T k); 
00229 
00238 template<typename T>
00239 ShManipulator<T> shDrop(T k);
00240 
00241 typedef ShManipulator<int> ShPositionManipulator;
00242 typedef ShManipulator<char*> ShNameManipulator;
00243 
00244 }
00245 
00246 #include "ShManipulatorImpl.hpp"
00247 
00248 #endif

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