39 #include <vtkImageData.h>
40 #include <vtkPointData.h>
41 #include <vtkUnsignedCharArray.h>
42 #include <vtkUnsignedShortArray.h>
43 #include <boost/cstdint.hpp>
49 #define GL_GLEXT_PROTOTYPES
53 #include <OpenGL/glu.h>
99 std::cout <<
"error: bad buffer initialization: null image" << std::endl;
121 std::cout <<
"error: bad volume buffer initialization" << std::endl;
125 glActiveTexture(GL_TEXTURE7);
127 glEnable(GL_TEXTURE_3D);
129 glDisable(GL_TEXTURE_3D);
142 GLenum size,internalType;
143 boost::uint32_t dimx =
mTexture ->GetDimensions( )[0];
144 boost::uint32_t dimy =
mTexture ->GetDimensions( )[1];
145 boost::uint32_t dimz =
mTexture ->GetDimensions( )[2];
148 glEnable( GL_TEXTURE_3D );
151 glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP );
152 glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP );
153 glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP );
154 glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
155 glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
158 case VTK_UNSIGNED_CHAR:
160 size = GL_UNSIGNED_BYTE;
161 internalType = GL_LUMINANCE;
164 case VTK_UNSIGNED_SHORT:
166 size = GL_UNSIGNED_SHORT;
167 internalType = GL_LUMINANCE16;
174 std::cout <<
"Bit size not supported!" << std::endl;
175 QString dataType(
mTexture->GetScalarTypeAsString());
176 CX_LOG_ERROR() << QString(
"Attempt to update 3D GL texture from type %1 failed. Only unsigned types supported").arg(dataType);
180 if (
mTexture->GetNumberOfScalarComponents()==1)
182 void* data =
mTexture->GetPointData()->GetScalars()->GetVoidPointer(0);
183 glTexImage3D(GL_TEXTURE_3D, 0, internalType, dimx, dimy, dimz, 0, GL_LUMINANCE, size, data);
185 else if (
mTexture->GetNumberOfScalarComponents()==3)
187 internalType = GL_RGB;
188 void* data =
mTexture->GetPointData()->GetScalars()->GetVoidPointer(0);
189 glTexImage3D(GL_TEXTURE_3D, 0, internalType, dimx, dimy, dimz, 0, GL_RGB, size, data);
194 std::cout <<
"unsupported number of image components" << std::endl;
197 glDisable(GL_TEXTURE_3D);
206 virtual void bind(
int textureUnitIndex)
211 std::cout <<
"error: called bind() on unallocated volume buffer" << std::endl;
226 switch (textureUnitIndex)
228 case 0:
return GL_TEXTURE0;
229 case 1:
return GL_TEXTURE2;
230 case 2:
return GL_TEXTURE4;
231 case 3:
return GL_TEXTURE6;
232 case 4:
return GL_TEXTURE8;
266 return mTable->GetNumberOfTuples() *
mTable->GetNumberOfComponents() *
sizeof(float);
285 std::cout <<
"error: bad lut buffer initialization" << std::endl;
291 glActiveTexture(GL_TEXTURE6);
306 glActiveTexture(GL_TEXTURE6);
313 std::vector<float> lut;
314 int lutSize =
mTable->GetNumberOfTuples();
315 int lutDataSize = lutSize *
mTable->GetNumberOfComponents();
316 lut.resize(lutDataSize);
317 unsigned char* ptr =
mTable->GetPointer(0);
319 for (
int i = 0; i < lut.size(); ++i)
321 lut[i] = ((float) *ptr) / 255.0;
326 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
327 glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
328 glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
331 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA,
333 GL_RGBA, GL_FLOAT, &(*lut.begin()));
337 virtual void bind(
int textureUnitIndex)
342 std::cout <<
"error: called bind() on unallocated lut buffer" << std::endl;
357 return mTable->GetNumberOfTuples();
370 switch (textureUnitIndex)
372 case 0:
return GL_TEXTURE1;
373 case 1:
return GL_TEXTURE3;
374 case 2:
return GL_TEXTURE5;
375 case 3:
return GL_TEXTURE7;
376 case 4:
return GL_TEXTURE9;
385 retval->SetImage(volume);
392 retval->SetColorMap(lut);
396 template<
class BUFFER,
class DATA_PTR>
417 GPUImageBufferRepository* GPUImageBufferRepository::mInstance = NULL;
420 template<
class DATA_PTR,
class BUFFER>
455 for (
typename BufferMap::iterator iter=mRemovedData.begin(); iter!=mRemovedData.end(); )
457 if (!iter->second.lock())
459 typename BufferMap::iterator temp = iter;
461 mRemovedData.erase(temp);
470 if (mRemovedData.count(data))
472 BufferPtr object = mRemovedData[data].lock();
475 mData.push_front(BufferStore(data,
object));
476 mRemovedData.erase(data);
482 for (
typename std::list<BufferStore>::iterator iter=mData.begin(); iter!=mData.end(); ++iter)
484 if (iter->mData==data)
486 retval = iter->mBuffer;
487 mData.push_front(*iter);
496 retval = createGPUImageBuffer<BUFFER>(data);
497 mData.push_front(BufferStore(data, retval));
501 while (mData.size()>mMaxBuffers)
503 mRemovedData[mData.back().mData] = mData.back().mBuffer;
514 *textures = mData.size();
516 for (
typename std::list<BufferStore>::iterator iter=mData.begin(); iter!=mData.end(); ++iter)
518 mem += iter->mBuffer->getMemorySize();
523 typedef std::map<DATA_PTR, BufferWeakPtr> BufferMap;
524 BufferMap mRemovedData;
525 std::list<BufferStore> mData;
526 unsigned mMaxBuffers;
545 GPUImageBufferRepository::GPUImageBufferRepository()
550 GPUImageBufferRepository::~GPUImageBufferRepository()
580 void GPUImageBufferRepository::tearDown()
608 void GPUImageBufferRepository::tearDown() {;}
virtual ~GPUImageDataBufferImpl()
Helper class for sharing GPU memory over several Views (GL contexts).
boost::shared_ptr< BUFFER > createGPUImageBuffer(DATA_PTR val)
BufferQueue< vtkImageDataPtr, GPUImageDataBuffer > mVolumeBuffer
boost::shared_ptr< GPUImageDataBuffer > createGPUImageBuffer< GPUImageDataBuffer, vtkImageDataPtr >(vtkImageDataPtr val)
GPUImageDataBufferPtr createGPUImageDataBuffer(vtkImageDataPtr volume)
virtual int getMemorySize()
vtkUnsignedCharArrayPtr mTable
virtual void SetColorMap(vtkUnsignedCharArrayPtr table)
virtual unsigned int getTextureUid() const
void setName(const QString &name)
BufferQueue< vtkUnsignedCharArrayPtr, GPUImageLutBuffer > mLutBuffer
Repository for GPU buffers.
vtkSmartPointer< class vtkUnsignedCharArray > vtkUnsignedCharArrayPtr
boost::shared_ptr< GPUImageDataBuffer > createGPUImageBuffer< GPUImageDataBuffer >(vtkImageDataPtr val)
virtual void bind(int textureUnitIndex)
GPUImageLutBufferPtr getGPUImageLutBuffer(vtkUnsignedCharArrayPtr lut)
virtual void updateTexture()
void setMaxBuffers(unsigned val)
virtual ~GPUImageLutBufferImpl()
virtual void SetImage(vtkImageDataPtr texture)
int getGLTextureForLut(int textureUnitIndex)
GPUImageLutBufferPtr createGPUImageLutBuffer(vtkUnsignedCharArrayPtr lut)
boost::shared_ptr< class GPUImageDataBuffer > GPUImageDataBufferPtr
boost::weak_ptr< BUFFER > BufferWeakPtr
virtual void updateTexture()
boost::shared_ptr< class GPUImageLutBuffer > GPUImageLutBufferPtr
int getGLTextureForVolume(int textureUnitIndex)
GPUImageDataBufferPtr getGPUImageDataBuffer(vtkImageDataPtr volume)
int getMemoryUsage(int *textures)
Helper class for sharing GPU memory over several Views (GL contexts).
virtual int getMemorySize()
static GPUImageBufferRepository * getInstance()
virtual void bind(int textureUnitIndex)
BufferStore(DATA_PTR data, BufferPtr buffer)
boost::shared_ptr< GPUImageLutBuffer > createGPUImageBuffer< GPUImageLutBuffer >(vtkUnsignedCharArrayPtr val)
boost::shared_ptr< BUFFER > BufferPtr
GPUImageBufferRepositoryInternal()
#define report_gl_error()
int getMemoryUsage(int *textures)