ShTextureNode.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2006 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 #ifndef SHTEXTURENODE_HPP
00021 #define SHTEXTURENODE_HPP
00022 
00023 #include "ShDllExport.hpp"
00024 #include "ShVariableNode.hpp"
00025 #include "ShMemory.hpp"
00026 #include "ShRefCount.hpp"
00027 #include "ShVariable.hpp"
00028 
00029 namespace SH {
00030 
00034 enum ShTextureDims {
00035   SH_TEXTURE_1D,   // Power of two
00036   SH_TEXTURE_2D,   // Power of two
00037   SH_TEXTURE_RECT, // Non power of two
00038   SH_TEXTURE_3D,   // Power of two, but depth may not be
00039   SH_TEXTURE_CUBE // 6 "2D" memory objects, power of two
00040 };
00041 
00045 enum ShCubeDirection {
00046   SH_CUBE_POS_X = 0,
00047   SH_CUBE_NEG_X = 1,
00048   SH_CUBE_POS_Y = 2,
00049   SH_CUBE_NEG_Y = 3,
00050   SH_CUBE_POS_Z = 4,
00051   SH_CUBE_NEG_Z = 5
00052 };
00053 
00058 class 
00059 SH_DLLEXPORT ShTextureTraits {
00060 public:
00061   enum Filtering {
00062     SH_FILTER_NONE,
00063     SH_FILTER_MIPMAP_NEAREST,
00064     SH_FILTER_MIPMAP_LINEAR
00065   };
00066   
00067   enum Wrapping {
00068     SH_WRAP_CLAMP,
00069     SH_WRAP_CLAMP_TO_EDGE,
00070     SH_WRAP_REPEAT
00071   };
00072 
00073   ShTextureTraits(unsigned int interpolation,
00074                   Filtering filtering,
00075                   Wrapping wrapping)
00076     : m_interpolation(interpolation),
00077       m_filtering(filtering),
00078       m_wrapping(wrapping)
00079   {
00080   }
00081 
00082   bool operator==(const ShTextureTraits& other) const
00083   {
00084     return m_interpolation == other.m_interpolation
00085       && m_filtering == other.m_filtering
00086       && m_wrapping == other.m_wrapping;
00087   }
00088 
00089   bool operator!=(const ShTextureTraits& other) const { return !(*this == other); }
00090   
00091   unsigned int interpolation() const { return m_interpolation; }
00092   ShTextureTraits& interpolation(unsigned int interp) { m_interpolation = interp; return *this; }
00093   
00094   Filtering filtering() const { return m_filtering; }
00095   ShTextureTraits& filtering(Filtering filtering) { m_filtering = filtering; return *this; }
00096   
00097   Wrapping wrapping() const { return m_wrapping; }
00098   ShTextureTraits& wrapping(Wrapping wrapping) { m_wrapping = wrapping; return *this; }
00099   
00100 
00101 private:
00102   unsigned int m_interpolation;
00103   Filtering m_filtering;
00104   Wrapping m_wrapping;
00105 };
00106 
00107 class 
00108 SH_DLLEXPORT ShTextureNode : public ShVariableNode {
00109 public:
00110   ShTextureNode(ShTextureDims dims,
00111                 int size, // scalars per tuple 
00112                 ShValueType valueType, // type index 
00113                 const ShTextureTraits&,
00114                 int width, int height = 1, int depth = 1, int max_nb_elements = -1);
00115   virtual ~ShTextureNode();
00116 
00117   ShTextureDims dims() const;
00118 
00119   // Memory
00120   ShPointer<const ShMemory> memory(int n) const;
00121   ShPointer<const ShMemory> memory(ShCubeDirection dir, int n) const;
00122   ShMemoryPtr memory(int n);
00123   ShMemoryPtr memory(ShCubeDirection dir, int n);
00124   void memory(const ShMemoryPtr& memory, int n);
00125   void memory(const ShMemoryPtr& memory, ShCubeDirection dir, int n);
00126 
00127   // Basic properties - not all may be valid for all types
00128   const ShTextureTraits& traits() const; // valid for all texture nodes
00129   ShTextureTraits& traits(); // valid for all texture nodes
00130   int width() const; // valid for all texture nodes
00131   int height() const; // 1 for SH_TEXTURE_1D
00132   int depth() const; // 1 unless SH_TEXTURE_3D
00133   int count() const; // number of elements  
00134   int mipmap_levels();
00135 
00136   void setTexSize(int w);
00137   void setTexSize(int w, int h);
00138   void setTexSize(int w, int h, int d);
00139   const ShVariable& texSizeVar() const;
00140 
00141   void count(int n);
00142 
00144   bool build_mipmaps(ShCubeDirection dir = SH_CUBE_POS_X);
00145   
00146 private:
00147   int m_count; // max nb of elements sent to the GPU or -1 if unknown (used by the stream backend)
00148 
00149   ShTextureDims m_dims;
00150   
00151   ShMemoryPtr* m_memory; // array
00152   int m_nb_memories; // size of the array
00153   int m_mipmap_levels;
00154   int m_mipmap_generation_timestamp[6]; // -1 if they were never generated
00155   ShMemory* m_mipmap_generation_basemem[6]; // NULL if never generated
00156 
00157   ShTextureTraits m_traits;
00158   int m_width, m_height, m_depth;
00159 
00160   ShVariable m_texSizeVar;
00161   ShTextureTraits::Filtering m_old_filtering;
00162 
00163   void initialize_memories(bool force_initialization);
00164   float interpolate1D(float* base_data, int scale, int x, int component);
00165   float interpolate2D(float* base_data, int scale, int x, int y, int component);
00166   float interpolate3D(float* base_data, int scale, int x, int y, int z, int component);
00167   ShMemoryPtr memory_error(int n) const;
00168 
00169   // NOT IMPLEMENTED
00170   ShTextureNode(const ShTextureNode& other);
00171   ShTextureNode& operator=(const ShTextureNode& other);
00172 };
00173 
00174 typedef ShPointer<ShTextureNode> ShTextureNodePtr;
00175 typedef ShPointer<const ShTextureNode> ShTextureNodeCPtr;
00176 
00177 }
00178 #endif

Generated on Thu Feb 16 14:51:38 2006 for Sh by  doxygen 1.4.6