Subversion

gpucv

[/] [experimental/] [trunk/] [gpucv/] [include/] [GPUCVCuda/] [DataDsc_CUDA_Buffer.h] - Rev 271 Go to most recent revision

Compare with Previous - Blame


//CVG_LicenseBegin==============================================================
//
//	Copyright@ GET 2005 (Groupe des Ecoles de Telecom)
//		http://www.get-telecom.fr/
//	
//	This software is a GPU accelerated library for computer-vision. It 
//	supports an OPENCV-like extensible interface for easily porting OPENCV 
//	applications.
//	
//	Contacts:
//		GpuCV core team: gpucv@picoforge.int-evry.fr
//		GpuCV developers newsgroup: gpucv-developers@picoforge.int-evry.fr
//	
//	Project's Home Page:
//		https://picoforge.int-evry.fr/cgi-bin/twiki/view/Gpucv/Web/WebHome
//	
//	This software is governed by the CeCILL  license under French law and
//	abiding by the rules of distribution of free software.  You can  use, 
//	modify and/ or redistribute the software under the terms of the CeCILL
//	license as circulated by CEA, CNRS and INRIA at the following URL
//	"http://www.cecill.info". 
//	
//================================================================CVG_LicenseEnd
#ifndef __GPUCV_TEXTURE_DATADSC_CUDA_H
#define __GPUCV_TEXTURE_DATADSC_CUDA_H


#include <GPUCVCuda/DataDsc_CUDA_Array.h>
#if _GPUCV_COMPILE_CUDA

/**	\brief DataDsc_CUDA_Buffer is the class to describe CUDA device memory objects.
*	\author Yannick Allusse
*	\version CUDA 1.1 and GpuCV 4.1 rev 261
*/
class _GPUCV_CUDA_EXPORT DataDsc_CUDA_Buffer
        :public DataDsc_CUDA_Base
{
public:
        enum CUDA_DEVICE_DATA_TYPE{
                CUDA_NO_TYPE,
                CUDA_GL_BUFFER,
                CUDA_DX_BUFFER,
                CUDA_CPU_BUFFER,
                CUDA_TEXTURE_ARRAY
        };

protected:
        size_t					m_pitch;		//!< See cudaMallocPitch().
        bool					m_glBufferMapped;	//!< Specify if the object is mapped to a buffer.
public:
        /** \brief Default constructor. */
                _GPUCV_CUDA_INLINE
                        DataDsc_CUDA_Buffer(void);
        /** \brief Default destructor. */
                _GPUCV_CUDA_INLINE virtual
                        ~DataDsc_CUDA_Buffer(void);	

        /** \brief Redefinition of exception logging function */
                virtual std::string LogException(void);

        //Redefinition of data manipulation
                _GPUCV_CUDA_INLINE virtual void Free();

        //Redefinition of DataDsc_Base interaction with other objects
                //access format, parameter are OpenGL enum format descriptor.
                virtual bool	CopyTo(DataDsc_Base* _destination, bool _datatransfer=true);
                virtual bool	CopyFrom(DataDsc_Base* _source, bool _datatransfer=true);
                virtual	DataDsc_Base * Clone(DataDsc_CUDA_Buffer * _src, bool _datatransfer=true);
                virtual	DataDsc_Base * CloneToNew(bool _datatransfer=true);

                void PostProcessUpdate(void);
                void PreProcessUpdate(void);

                virtual std::ostringstream & operator << (std::ostringstream & _stream)const;
                
        //local functions:
                /** \brief Allocate a CUDA device buffer.
                        \param _datatype => Type of data to allocate using CUDA type descriptors [CU_AD_FORMAT_UNSIGNED_INT8 | CU_AD_FORMAT_UNSIGNED_INT16 | CU_AD_FORMAT_FLOAT ...]
                */
                        void _AllocateDevice(unsigned int _datatype);

                /** \brief Allocate a CUDA device buffer(template function).
                        \param TType => Type of data to allocate using "C" data type [char|uchar|int|float...]
                        \return True if data allocation was succesfull.
                */
                template <typename TType>
                bool _AllocateDataPtr(void)
                {	
                        CLASS_FCT_SET_NAME_TPL(TType,"_AllocateDataPtr");
#if _GCU_DEBUG_MEMORY_ALLOC
                        unsigned int TotalMem=0;
                        unsigned int FreeMem=0;
                        cuMemGetInfo(&FreeMem,&TotalMem);
                        CLASS_NOTICE("");
                        GPUCV_NOTICE("BEFORE=>Cuda free memory: " << FreeMem << " (" << (double)FreeMem/TotalMem*100. <<"% )");
                        if(FreeMem==0 || TotalMem==0)
                        {
                                GPUCV_NOTICE("We don't have free memory..??");
                        }
#endif
                        m_memSize = GetDataSize();
#if 0
                                gcudaMalloc((void **)&m_data.m_deviceDataPtr, m_memSize);
#else
                                gcudaMallocPitch((void **)&m_data.m_deviceDataPtr, &m_pitch, _GetWidth()*_GetNChannels()*sizeof(TType), _GetHeight());
#endif
                                CLASS_DEBUG("gcudaMallocPitch(&m_deviceDataPtr, &m_pitch,"<< _GetWidth()*_GetNChannels()*sizeof(unsigned char) <<","<< _GetHeight() <<")");
                                CLASS_ASSERT(m_data.m_deviceDataPtr, "_DeviceAllocate()=> Could not allocate pointer");
#if _DEBUG
                                gcudaMemset(m_data.m_deviceDataPtr, 0, m_memSize);				
#endif
                                CLASS_DEBUG("cudaMemset(m_deviceDataPtr, 0,"<< m_memSize<<")");
                        gcudaCheckError("DataDsc_CUDA_Buffer::_DeviceAllocate() execution failed\n");
                        //how to get error code from CUDA..?
#if _GCU_DEBUG_MEMORY_ALLOC
                        cuMemGetInfo(&FreeMem,&TotalMem);
                        GPUCV_NOTICE("AFTER=>Cuda free memory: " << FreeMem << " (" << (double)FreeMem/TotalMem*100. <<"% )");
#endif
                        return true;
                }

                /** \brief Get CUDA memory pitch value.
                        \return CUDA memory pitch value.
                */
                        _GPUCV_CUDA_INLINE 
                                size_t _GetPitch(void)const
                                {
                                        return m_pitch;
                                }
};

#endif//_GPUCV_COMPILE_CUDA
#endif//__GPUCV_TEXTURE_DATADSC_CUDA_H

Powered by WebSVN v1.61