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

ShVariable.cpp

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 #include <iostream>
00028 
00029 #include "ShVariable.hpp"
00030 #include "ShProgram.hpp"
00031 
00032 namespace SH {
00033 
00034 ShVariable::ShVariable()
00035   : ShMetaForwarder(0),
00036     m_node(0), m_swizzle(0), m_neg(false)
00037 {
00038 }
00039 
00040 ShVariable::ShVariable(const ShVariableNodePtr& node)
00041   : ShMetaForwarder(node.object()),
00042     m_node(node), m_swizzle(node ? node->size() : 0), m_neg(false)
00043 {
00044 }
00045 
00046 ShVariable::ShVariable(const ShVariableNodePtr& node,
00047                        const ShSwizzle& swizzle,
00048                        bool neg)
00049   : ShMetaForwarder(node.object()),
00050     m_node(node), m_swizzle(swizzle), m_neg(neg)
00051 {
00052 }
00053 
00054 ShVariable& ShVariable::operator=(const ShProgram& prgc)
00055 {
00056   ShProgram prg(prgc);
00057   if (prg.node()->finished()) {
00058     m_node->attach(prg.node());
00059   } else {
00060     prg.node()->assign(m_node);
00061   }
00062   return *this;
00063 }
00064 
00065 bool ShVariable::null() const
00066 {
00067   return !m_node;
00068 }
00069 
00070 bool ShVariable::uniform() const
00071 {
00072   return m_node->uniform();
00073 }
00074 
00075 bool ShVariable::hasValues() const
00076 {
00077   return m_node->hasValues();
00078 }
00079 
00080 int ShVariable::size() const
00081 {
00082   return m_swizzle.size();
00083 }
00084 
00085 ShValueType ShVariable::valueType() const 
00086 {
00087   if(!m_node) return SH_VALUETYPE_END;
00088   return m_node->valueType();
00089 }
00090 
00091 void ShVariable::rangeVariant(const ShVariant* low, const ShVariant* high)
00092 {
00093   m_node->rangeVariant(low, high, m_neg, m_swizzle);
00094 }
00095 
00096 ShVariantPtr ShVariable::lowBoundVariant() const
00097 {
00098   return m_node->lowBoundVariant()->get(m_neg, m_swizzle);
00099 }
00100 
00101 ShVariantPtr ShVariable::highBoundVariant() const
00102 {
00103   return m_node->highBoundVariant()->get(m_neg, m_swizzle);
00104 }
00105 
00106 const ShSwizzle& ShVariable::swizzle() const
00107 {
00108   return m_swizzle;
00109 }
00110 
00111 const ShVariableNodePtr& ShVariable::node() const
00112 {
00113   return m_node;
00114 }
00115 
00116 bool ShVariable::neg() const
00117 {
00118   return m_neg;
00119 }
00120 
00121 bool& ShVariable::neg()
00122 {
00123   return m_neg;
00124 }
00125 
00126 ShVariantPtr ShVariable::getVariant() const
00127 {
00128   return m_node->getVariant()->get(m_neg, m_swizzle);
00129 }
00130 
00131 ShVariantPtr ShVariable::getVariant(int index) const
00132 {
00133   return m_node->getVariant()->get(m_neg, m_swizzle * ShSwizzle(size(), index)); 
00134 }
00135 
00136 
00137 bool ShVariable::loadVariant(ShVariant *&result) const
00138 {
00139   if((!m_neg) && m_swizzle.identity()) {
00140     result = const_cast<ShVariant*>(m_node->getVariant());
00141     return false;
00142   }
00143   // @todo type figure out a nicer, but just as efficient way than this hackery without
00144   // exposing unwanted functionality in ShVariant or ShVariableNode. 
00145   ShVariantPtr temp = getVariant();
00146   temp->acquireRef();
00147   result = temp.object();
00148   return true;
00149 }
00150 
00151 void ShVariable::updateVariant()
00152 {
00153   m_node->update_all();
00154 }
00155 
00156 void ShVariable::setVariant(const ShVariant* other, bool neg, const ShSwizzle &writemask)
00157 {
00158   m_node->setVariant(other, neg ^ m_neg, m_swizzle * writemask);
00159 }
00160 
00161 void ShVariable::setVariant(ShVariantCPtr other, bool neg, const ShSwizzle &writemask)
00162 {
00163   setVariant(other.object(), neg, writemask); 
00164 }
00165 
00166 void ShVariable::setVariant(const ShVariant* other, int index) 
00167 {
00168   m_node->setVariant(other, m_neg, m_swizzle * ShSwizzle(size(), index));
00169 }
00170 
00171 void ShVariable::setVariant(ShVariantCPtr other, int index) 
00172 {
00173   setVariant(other.object(), index); 
00174 }
00175 
00176 void ShVariable::setVariant(const ShVariant* other)
00177 {
00178   m_node->setVariant(other, m_neg, m_swizzle);
00179 }
00180 
00181 void ShVariable::setVariant(ShVariantCPtr other)
00182 {
00183   setVariant(other.object());
00184 }
00185 
00186 ShVariable ShVariable::operator()() const
00187 {
00188   return ShVariable(m_node, m_swizzle, m_neg);
00189 }
00190 
00191 ShVariable ShVariable::operator()(int i1) const
00192 {
00193   return ShVariable(m_node, m_swizzle * ShSwizzle(size(), i1), m_neg);
00194 }
00195 
00196 ShVariable ShVariable::operator()(int i1, int i2) const
00197 {
00198   return ShVariable(m_node, m_swizzle * ShSwizzle(size(), i1, i2), m_neg);
00199 }
00200 
00201 ShVariable ShVariable::operator()(int i1, int i2, int i3) const
00202 {
00203   return ShVariable(m_node, m_swizzle * ShSwizzle(size(), i1, i2, i3), m_neg);
00204 }
00205 
00206 ShVariable ShVariable::operator()(int i1, int i2, int i3, int i4) const
00207 {
00208   return ShVariable(m_node, m_swizzle * ShSwizzle(size(), i1, i2, i3, i4), m_neg);
00209 }
00210 
00211 ShVariable ShVariable::operator()(int n, int indices[]) const
00212 {
00213   return ShVariable(m_node, m_swizzle * ShSwizzle(size(), n, indices), m_neg);
00214 }
00215 
00216 std::ostream& operator<<(std::ostream& out, const ShVariable& v)
00217 {
00218   if (!v.m_node){
00219     out << "[null]";
00220     return out;
00221   }
00222   if (!v.hasValues()){
00223     out << "[not uniform]";
00224     return out;
00225   }
00226 
00227   out << '[';
00228   out << v.getVariant()->encodeArray();
00229   out<<']';
00230   return out;
00231 }
00232 
00233 
00234 ShVariable ShVariable::operator-() const
00235 {
00236   return ShVariable(m_node, m_swizzle, !m_neg);
00237 }
00238 
00239 bool ShVariable::operator==(const ShVariable& other) const
00240 {
00241   return m_node == other.m_node && m_swizzle == other.m_swizzle && m_neg == other.m_neg;
00242 }
00243 
00244 void ShVariable::clone(const ShVariable& other)
00245 {
00246   m_node = other.m_node;
00247   m_swizzle = other.m_swizzle;
00248   m_neg = other.m_neg;
00249 }
00250 
00251 
00252 }

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