18 #include <vtkImageData.h> 19 #include <vtkPointData.h> 20 #include <vtkUnsignedCharArray.h> 21 #include <vtkUnsignedShortArray.h> 22 #include <boost/cstdint.hpp> 28 #define GL_GLEXT_PROTOTYPES //Needed to compile on Linux? 32 #include <OpenGL/glu.h> 78 std::cout <<
"error: bad buffer initialization: null image" << std::endl;
100 std::cout <<
"error: bad volume buffer initialization" << std::endl;
111 glGenTextures(1, &textureId);
123 if (mMTime == mTexture->GetMTime())
127 mMTime = mTexture->GetMTime();
129 GLenum size,internalType;
130 boost::uint32_t dimx = mTexture ->GetDimensions( )[0];
131 boost::uint32_t dimy = mTexture ->GetDimensions( )[1];
132 boost::uint32_t dimz = mTexture ->GetDimensions( )[2];
133 mMemorySize = dimx * dimy * dimz;
139 glBindTexture(GL_TEXTURE_3D, textureId);
140 glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER );
141 glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER );
142 glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER );
143 glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
144 glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
146 switch (mTexture->GetScalarType())
148 case VTK_UNSIGNED_CHAR:
150 size = GL_UNSIGNED_BYTE;
151 internalType = GL_RED;
154 case VTK_UNSIGNED_SHORT:
156 size = GL_UNSIGNED_SHORT;
157 internalType = GL_RED;
164 std::cout <<
"Bit size not supported!" << std::endl;
165 QString dataType(mTexture->GetScalarTypeAsString());
166 CX_LOG_ERROR() << QString(
"Attempt to update 3D GL texture from type %1 failed. Only unsigned types supported").arg(dataType);
171 if (mTexture->GetNumberOfScalarComponents()==1)
173 void* data = mTexture->GetPointData()->GetScalars()->GetVoidPointer(0);
174 glTexImage3D(GL_TEXTURE_3D, 0, internalType, dimx, dimy, dimz, 0, GL_RED, size, data);
176 else if (mTexture->GetNumberOfScalarComponents()==3)
178 internalType = GL_RGB;
179 void* data = mTexture->GetPointData()->GetScalars()->GetVoidPointer(0);
180 glTexImage3D(GL_TEXTURE_3D, 0, internalType, dimx, dimy, dimz, 0, GL_RGB, size, data);
185 std::cout <<
"unsupported number of image components" << std::endl;
197 virtual void bind(
int textureUnitIndex)
203 std::cout <<
"error: called bind() on unallocated volume buffer" << std::endl;
206 glBindTexture(GL_TEXTURE_3D, textureId);
212 glDeleteTextures(1, &textureId);
217 switch (textureUnitIndex)
219 case 0:
return GL_TEXTURE0;
220 case 1:
return GL_TEXTURE2;
221 case 2:
return GL_TEXTURE4;
222 case 3:
return GL_TEXTURE6;
223 case 4:
return GL_TEXTURE8;
257 return mTable->GetNumberOfTuples() * mTable->GetNumberOfComponents() *
sizeof(float);
276 std::cout <<
"error: bad lut buffer initialization" << std::endl;
282 glActiveTexture(GL_TEXTURE6);
283 glGenTextures(1, &textureId);
291 if (mMTime == mTable->GetMTime())
295 mMTime = mTable->GetMTime();
297 glActiveTexture(GL_TEXTURE6);
298 this->sendDataToGL();
304 std::vector<float> lut;
305 int lutSize = mTable->GetNumberOfTuples();
306 int lutDataSize = lutSize * mTable->GetNumberOfComponents();
307 lut.resize(lutDataSize);
308 unsigned char* ptr = mTable->GetPointer(0);
310 for (
int i = 0; i < lut.size(); ++i)
312 lut[i] = ((float) *ptr) / 255.0;
316 glBindTexture(GL_TEXTURE_1D, textureId);
317 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
318 glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
319 glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
322 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA,
324 GL_RGBA, GL_FLOAT, &(*lut.begin()));
328 virtual void bind(
int textureUnitIndex)
333 std::cout <<
"error: called bind() on unallocated lut buffer" << std::endl;
336 glActiveTexture(getGLTextureForLut(textureUnitIndex));
337 this->bindDataToGL();
343 glBindTexture(GL_TEXTURE_1D, textureId);
348 return mTable->GetNumberOfTuples();
356 glDeleteTextures(1, &textureId);
361 switch (textureUnitIndex)
363 case 0:
return GL_TEXTURE1;
364 case 1:
return GL_TEXTURE3;
365 case 2:
return GL_TEXTURE5;
366 case 3:
return GL_TEXTURE7;
367 case 4:
return GL_TEXTURE9;
376 retval->SetImage(volume);
383 retval->SetColorMap(lut);
387 template<
class BUFFER,
class DATA_PTR>
411 template<
class DATA_PTR,
class BUFFER>
420 BufferStore(DATA_PTR data, BufferPtr buffer) : mData(data), mBuffer(buffer) {}
443 BufferPtr
get(DATA_PTR data)
446 for (
typename BufferMap::iterator iter=mRemovedData.begin(); iter!=mRemovedData.end(); )
448 if (!iter->second.lock())
450 typename BufferMap::iterator temp = iter;
452 mRemovedData.erase(temp);
461 if (mRemovedData.count(data))
463 BufferPtr
object = mRemovedData[data].lock();
466 mData.push_front(BufferStore(data,
object));
467 mRemovedData.erase(data);
473 for (
typename std::list<BufferStore>::iterator iter=mData.begin(); iter!=mData.end(); ++iter)
475 if (iter->mData==data)
477 retval = iter->mBuffer;
478 mData.push_front(*iter);
487 retval = createGPUImageBuffer<BUFFER>(data);
488 mData.push_front(BufferStore(data, retval));
492 while (mData.size()>mMaxBuffers)
494 mRemovedData[mData.back().mData] = mData.back().mBuffer;
505 *textures = mData.size();
507 for (
typename std::list<BufferStore>::iterator iter=mData.begin(); iter!=mData.end(); ++iter)
509 mem += iter->mBuffer->getMemorySize();
514 typedef std::map<DATA_PTR, BufferWeakPtr> BufferMap;
515 BufferMap mRemovedData;
516 std::list<BufferStore> mData;
517 unsigned mMaxBuffers;
526 mVolumeBuffer.setName(
"Volume");
527 mLutBuffer.setName(
"Lut");
529 mVolumeBuffer.setMaxBuffers(12);
530 mLutBuffer.setMaxBuffers(15);
536 GPUImageBufferRepository::GPUImageBufferRepository()
541 GPUImageBufferRepository::~GPUImageBufferRepository()
571 void GPUImageBufferRepository::tearDown()
579 return mInternal->mVolumeBuffer.getMemoryUsage(textures);
584 return mInternal->mVolumeBuffer.get(volume);
589 return mInternal->mLutBuffer.get(lut);
603 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.
virtual void allocate(int textureUnitIndex)
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)
GPUImageBufferRepository * getGPUImageBufferRepository()
Namespace for all CustusX production code.