[opengtl-commits] [220] add a pixel visitor to access to pixel data

[ Thread Index | Date Index | More lists.tuxfamily.org/opengtl-commits Archives ]


Revision: 220
Author:   cyrille
Date:     2008-06-22 23:42:18 +0200 (Sun, 22 Jun 2008)

Log Message:
-----------
add a pixel visitor to access to pixel data

Modified Paths:
--------------
    trunk/OpenGTL/OpenGTL/GTLCore/AST/AccessorExpression.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/CodeGenerator_p.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.h
    trunk/OpenGTL/OpenShiva/OpenShiva/CMakeLists.txt
    trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.cpp
    trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.h
    trunk/OpenGTL/OpenShiva/OpenShiva/Wrapper_p.cpp
    trunk/OpenGTL/OpenShiva/tools/interpreter/Shiva.cpp

Added Paths:
-----------
    trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.cpp
    trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.h


Modified: trunk/OpenGTL/OpenGTL/GTLCore/AST/AccessorExpression.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/AST/AccessorExpression.cpp	2008-06-22 16:10:57 UTC (rev 219)
+++ trunk/OpenGTL/OpenGTL/GTLCore/AST/AccessorExpression.cpp	2008-06-22 21:42:18 UTC (rev 220)
@@ -126,7 +126,10 @@
 
 const GTLCore::Type* ArrayAccessorExpression::type() const
 {
-  return m_parent->type()->embeddedType();
+  const Visitor* visitor = Visitor::getVisitorFor( m_parent->type() );
+  const GTLCore::Type* type = visitor->pointerToIndexType( m_parent->type() );
+  GTL_ASSERT( type );
+  return type;
 }
 
 //------------------- VariableAccessorExpression -------------------//

Modified: trunk/OpenGTL/OpenGTL/GTLCore/CodeGenerator_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/CodeGenerator_p.cpp	2008-06-22 16:10:57 UTC (rev 219)
+++ trunk/OpenGTL/OpenGTL/GTLCore/CodeGenerator_p.cpp	2008-06-22 21:42:18 UTC (rev 220)
@@ -228,8 +228,8 @@
   { // Don't cast the value as it has allready the correct type
     return _constant;
   }
-  GTL_ASSERT(_targetType->d->type()->isFirstClassType());
-  GTL_ASSERT(_constant->getType()->isFirstClassType());
+  GTL_ASSERT( _targetType->d->type()->isFirstClassType() );
+  GTL_ASSERT( _constant->getType()->isFirstClassType() );
   return llvm::ConstantExpr::getCast(
                       (unsigned)llvm::CastInst::getCastOpcode(_constant, _constantType->isSigned(), _targetType->d->type(), _targetType->isSigned()), _constant, _targetType->d->type() );
 }

Modified: trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp	2008-06-22 16:10:57 UTC (rev 219)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp	2008-06-22 21:42:18 UTC (rev 220)
@@ -90,6 +90,11 @@
 {
 }
 
+const GTLCore::Type* PrimitiveVisitor::pointerToIndexType( const GTLCore::Type* ) const
+{
+  return 0;
+}
+
 llvm::Value* PrimitiveVisitor::pointerToIndex(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _type, llvm::Value* _index) const
 {
   GTL_ABORT("Primitive type doesn't allow access using indexes"); //TODO except vectors
@@ -125,6 +130,11 @@
 {
 }
 
+const GTLCore::Type* ArrayVisitor::pointerToIndexType( const GTLCore::Type* _type) const
+{
+  return _type->embeddedType();
+}
+
 llvm::Value* ArrayVisitor::pointerToIndex(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _type, llvm::Value* _index) const
 {
   return _generationContext.codeGenerator()->accessArrayValue( _currentBlock, _pointer, _index);
@@ -262,6 +272,11 @@
 {
 }
 
+const GTLCore::Type* StructureVisitor::pointerToIndexType( const GTLCore::Type* ) const
+{
+  return 0;
+}
+
 llvm::Value* StructureVisitor::pointerToValue( GenerationContext& /*_generationContext*/,
                                                 llvm::BasicBlock* _currentBlock,
                                                 llvm::Value* _pointer, int _index ) const

Modified: trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.h	2008-06-22 16:10:57 UTC (rev 219)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.h	2008-06-22 21:42:18 UTC (rev 220)
@@ -52,6 +52,10 @@
       Visitor();
       virtual ~Visitor();
       /**
+       * @return the type that will be returned by a call to \ref pointerToIndex
+       */
+      virtual const GTLCore::Type* pointerToIndexType( const GTLCore::Type* _type ) const = 0;
+      /**
        * Allow to access to the element of an object through an index, used for vectors,
        * arrays and some stucture (like pixels).
        */
@@ -105,6 +109,7 @@
       PrimitiveVisitor();
       virtual ~PrimitiveVisitor();
     public:
+      virtual const GTLCore::Type* pointerToIndexType( const GTLCore::Type* _type ) const;
       virtual llvm::Value* pointerToIndex(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _type, llvm::Value* _index) const;
       virtual llvm::Value* get( GenerationContext& _generationContext, llvm::BasicBlock* currentBlock,
                                 llvm::Value* pointer) const;
@@ -121,6 +126,7 @@
       ArrayVisitor();
       virtual ~ArrayVisitor();
     public:
+      virtual const GTLCore::Type* pointerToIndexType( const GTLCore::Type* _type ) const;
       virtual llvm::Value* pointerToIndex(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _type, llvm::Value* _index) const;
       virtual llvm::Value* get( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer) const;
       virtual llvm::BasicBlock* set(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, llvm::Value*, const GTLCore::Type* _valueType) const;
@@ -157,6 +163,7 @@
       StructureVisitor( );
       virtual ~StructureVisitor();
     public:
+      virtual const GTLCore::Type* pointerToIndexType( const GTLCore::Type* _type ) const;
       virtual llvm::Value* pointerToIndex(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _type, llvm::Value* _index) const;
       virtual llvm::Value* get( GenerationContext& _generationContext, llvm::BasicBlock* currentBlock, llvm::Value* pointer) const;
       virtual llvm::BasicBlock* set(GenerationContext& _generationContext, llvm::BasicBlock* currentBlock, llvm::Value* pointer, const GTLCore::Type* _pointerType, llvm::Value*, const GTLCore::Type* _valueType) const;

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/CMakeLists.txt
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/CMakeLists.txt	2008-06-22 16:10:57 UTC (rev 219)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/CMakeLists.txt	2008-06-22 21:42:18 UTC (rev 220)
@@ -12,6 +12,7 @@
   Lexer_p.cpp
   Parser_p.cpp
   Wrapper_p.cpp
+  PixelVisitor_p.cpp
 # Wrap
   wrappers/ImageWrap_p.cpp
   wrappers/PixelWrap_p.cpp

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.cpp	2008-06-22 16:10:57 UTC (rev 219)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.cpp	2008-06-22 21:42:18 UTC (rev 220)
@@ -93,6 +93,14 @@
       _pointerToPixelVector, llvm::PointerType::get(llvm::IntegerType::get(8), 0), "", _currentBlock);
 }
 
+llvm::Value* CodeGenerator::accessPixelDataAsF32Ptr( GTLCore::GenerationContext& _gc, llvm::BasicBlock* _currentBlock, llvm::Value* _pixel)
+{
+  llvm::Value* _pointerToPixelVector = accessPixelDataPtr( _gc, _currentBlock, _pixel) ;
+  // Cast the vector to a "char*"
+  return new llvm::BitCastInst(
+      _pointerToPixelVector, llvm::PointerType::get(llvm::Type::FloatTy, 0), "", _currentBlock);
+}
+
 llvm::Value* CodeGenerator::callMemcpy( GTLCore::GenerationContext& _gc, llvm::BasicBlock* _currentBlock, llvm::Value* _dst, llvm::Value* _src, llvm::Value* _n )
 {
   // Initialise llvm_memcpy_i32

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.h
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.h	2008-06-22 16:10:57 UTC (rev 219)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.h	2008-06-22 21:42:18 UTC (rev 220)
@@ -39,16 +39,26 @@
 
 namespace OpenShiva {
   class Kernel;
+  /**
+   * @internal
+   * Provide code generation function for specific features of OpenShiva
+   */
   class CodeGenerator {
     public:
       /**
-       * @internal
        * Copy a pixel from memory to the vector.
        */
       static llvm::BasicBlock* memToPixel( GTLCore::GenerationContext& _gc, llvm::BasicBlock* _currentBlock, llvm::Value* _dataPointer, llvm::Value* _pixel, int _size );
       static llvm::BasicBlock* pixelToMem( GTLCore::GenerationContext& _gc, llvm::BasicBlock* _currentBlock, llvm::Value* _pixel, llvm::Value* _dataPointer, int _size );
       static llvm::Function* generateEvaluatePixeles( const std::vector<const GTLCore::Type*>& _inputTypes, const GTLCore::Type* _outputType, Kernel* _kernel, GTLCore::ModuleData* _moduleData, const GTLCore::PixelDescription& _pixelDescription );
+      /**
+       * @return a pointer to the data pointer of a pixel
+       */
       static llvm::Value* accessPixelDataPtr( GTLCore::GenerationContext& _gc, llvm::BasicBlock* _currentBlock, llvm::Value* _pixel);
+      /**
+       * @return a pointer to the data pointer of a pixel cast to a (float*)
+       */
+      static llvm::Value* accessPixelDataAsF32Ptr( GTLCore::GenerationContext& _gc, llvm::BasicBlock* _currentBlock, llvm::Value* _pixel);
     private:
       static llvm::Function* createMemCpyFunction( llvm::Module* _module );
       static llvm::Value* accessPixelDataAsU8Ptr( GTLCore::GenerationContext& _gc, llvm::BasicBlock* _currentBlock, llvm::Value* _pixel);

Added: trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.cpp	                        (rev 0)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.cpp	2008-06-22 21:42:18 UTC (rev 220)
@@ -0,0 +1,88 @@
+/*
+ *  Copyright (c) 2008 Cyrille Berger <cberger@xxxxxxxxxxx>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "PixelVisitor_p.h"
+
+// LLVM
+#include <llvm/Instructions.h>
+
+#include "GTLCore/Type.h"
+
+#include "Debug.h"
+#include "CodeGenerator_p.h"
+#include "wrappers/PixelWrap_p.h"
+
+using namespace OpenShiva;
+
+PixelVisitor* pixelVisitor = 0;
+
+STATIC_INITIALISATION( PixelVisitor )
+{
+  pixelVisitor = new PixelVisitor;
+}
+
+
+PixelVisitor::PixelVisitor()
+{
+}
+
+PixelVisitor::~PixelVisitor()
+{
+}
+
+const GTLCore::Visitor* PixelVisitor::instance()
+{
+  return pixelVisitor;
+}
+
+const GTLCore::Type* PixelVisitor::pointerToIndexType( const GTLCore::Type* _type ) const
+{
+  return GTLCore::Type::Float;
+}
+
+llvm::Value* PixelVisitor::pointerToIndex( GTLCore::GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _type, llvm::Value* _index) const
+{
+  llvm::Value* ptr = CodeGenerator::accessPixelDataAsF32Ptr( _generationContext, _currentBlock, _pointer );
+  return new llvm::GetElementPtrInst( ptr, _index, "", _currentBlock);
+}
+
+llvm::Value* PixelVisitor::get( GTLCore::GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer) const
+{
+  llvm::Value* result = new llvm::LoadInst(
+      CodeGenerator::accessPixelDataPtr( _generationContext, _currentBlock, _pointer ),
+      "",
+      _currentBlock );
+  SHIVA_DEBUG( *_pointer << " " << *result );
+  return result;
+}
+
+llvm::BasicBlock* PixelVisitor::set( GTLCore::GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, llvm::Value* _value, const GTLCore::Type* _valueType) const
+{
+  new llvm::StoreInst( _value,
+                       CodeGenerator::accessPixelDataPtr( _generationContext, _currentBlock, _pointer ),
+                       "",
+                       _currentBlock );
+  return _currentBlock;
+}
+
+llvm::BasicBlock* PixelVisitor::initialise( GTLCore::GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, const std::list< llvm::Value*>& _sizes) const
+{
+  // Don't do nothing
+  return _currentBlock;
+}


Property changes on: trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.cpp
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.h
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.h	                        (rev 0)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.h	2008-06-22 21:42:18 UTC (rev 220)
@@ -0,0 +1,51 @@
+/*
+ *  Copyright (c) 2008 Cyrille Berger <cberger@xxxxxxxxxxx>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifndef _OPENSHIVA_PIXEL_VISITOR_H_
+#define _OPENSHIVA_PIXEL_VISITOR_H_
+
+class PixelVisitorFactory;
+
+#include "GTLCore/Visitor_p.h"
+
+namespace OpenShiva {
+  /**
+   * @internal
+   * This is a visitor specific to the pixels type of OpenShiva, it will return the
+   * vector.
+   *
+   * @ingroup OpenShiva
+   */
+  class PixelVisitor : public GTLCore::Visitor {
+      friend class ::PixelVisitorFactory;
+      PixelVisitor();
+    public:
+      static const Visitor* instance();
+      virtual ~PixelVisitor();
+      virtual const GTLCore::Type* pointerToIndexType( const GTLCore::Type* _type ) const;
+      virtual llvm::Value* pointerToIndex( GTLCore::GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _type, llvm::Value* _index) const;
+      virtual llvm::Value* get(GTLCore::GenerationContext& _generationContext, llvm::BasicBlock* currentBlock,
+                       llvm::Value* pointer) const;
+      virtual llvm::BasicBlock* set(GTLCore::GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, llvm::Value* _value, const GTLCore::Type* _valueType) const;
+      virtual llvm::BasicBlock* initialise(GTLCore::GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, const std::list< llvm::Value*>& _sizes) const;
+  };
+  
+}
+
+#endif


Property changes on: trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.h
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/Wrapper_p.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/Wrapper_p.cpp	2008-06-22 16:10:57 UTC (rev 219)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/Wrapper_p.cpp	2008-06-22 21:42:18 UTC (rev 220)
@@ -35,8 +35,8 @@
 #include "GTLCore/TypeManager_p.h"
 #include "GTLCore/Value.h"
 
-
 #include "Debug.h"
+#include "PixelVisitor_p.h"
 
 using namespace OpenShiva;
 
@@ -105,7 +105,8 @@
   _pixelDataMembers.push_back( GTLCore::Type::StructDataMember( "x", GTLCore::Type::Float ) );
   _pixelDataMembers.push_back( GTLCore::Type::StructDataMember( "y", GTLCore::Type::Float) );
   
-  _typeManager->d->createStructure( "pixel" + _suffix, _pixelDataMembers );
+  const GTLCore::Type* type = _typeManager->d->createStructure( "pixel" + _suffix, _pixelDataMembers );
+  type->d->setVisitor( PixelVisitor::instance() );
 }
 
 void Wrapper::createRegionType( llvm::Module* _module, GTLCore::TypeManager* _typeManager )

Modified: trunk/OpenGTL/OpenShiva/tools/interpreter/Shiva.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/tools/interpreter/Shiva.cpp	2008-06-22 16:10:57 UTC (rev 219)
+++ trunk/OpenGTL/OpenShiva/tools/interpreter/Shiva.cpp	2008-06-22 21:42:18 UTC (rev 220)
@@ -104,7 +104,7 @@
       source += "\n";
       std::getline(in,str);
     }
-    GTLCore::PixelDescription pixel( GTLCore::Type::UnsignedInteger8, 1 );
+    GTLCore::PixelDescription pixel( GTLCore::Type::Float, 1 );
     OpenShiva::Kernel p(fileName, pixel);
     p.setSource( source );
     p.compile();


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/