00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #if !defined SOBUFFEROBJECT_H
00024 #define SOBUFFEROBJECT_H
00025
00026 #include <Inventor/SbBase.h>
00027 #include <Inventor/misc/SoRefCounter.h>
00028 #include <Inventor/SoTypedObject.h>
00029 #include <Inventor/SoSubTypedClass.h>
00030 #include <Inventor/devices/SoDeviceContext.h>
00031 #include <Inventor/threads/SbThreadSpinlock.h>
00032 #include <Inventor/STL/limits>
00033
00034 #ifdef _WIN32
00035 #pragma warning(push)
00036 #pragma warning(disable:4251)
00037 #endif
00038
00039 class SoCpuBufferObject;
00040 class SoDeviceContext;
00041 class SoCpuBufferObjectCache;
00042
00046 #define SO_BUFFER_SIZE_ALL ((std::numeric_limits<size_t>::max)())
00047
00193 class SoBufferObject: public SoRefCounter, public SoTypedObject
00194 {
00195 SO_TYPED_CLASS_ABSTRACT_HEADER()
00196
00197
00198 public:
00199
00201 enum AccessMode
00202 {
00205 READ_ONLY,
00206
00211 SET,
00212
00217 READ_WRITE
00218 };
00219
00225 void lockBuffer();
00226
00232 void unlockBuffer();
00233
00240 virtual bool setSize( size_t size );
00241
00247 virtual size_t getSize() const;
00248
00261 virtual void map( SoBufferObject* targetBufferObject, AccessMode accessMode, size_t startPosition = 0, size_t mappingSize = SO_BUFFER_SIZE_ALL ) = 0;
00262
00268 virtual void map( SoCpuBufferObject* targetBufferObject, AccessMode accessMode, size_t startPosition = 0, size_t mappingSize = SO_BUFFER_SIZE_ALL ) = 0;
00269
00278 virtual void unmap( SoBufferObject* bufferObject ) = 0;
00279
00285 virtual void unmap( SoCpuBufferObject* bufferObject ) = 0;
00286
00299 virtual void memcpy( SoBufferObject* sourceBufferObject, size_t destOffset = 0, size_t sourceOffset = 0, size_t copySize = SO_BUFFER_SIZE_ALL ) = 0;
00300
00301
00307 virtual void memcpy( SoCpuBufferObject* sourceBufferObject, size_t destOffset = 0, size_t sourceOffset = 0, size_t copySize = SO_BUFFER_SIZE_ALL ) = 0;
00308
00341 virtual void memset( void* value, size_t valueSize = 1, size_t offset = 0, size_t count = SO_BUFFER_SIZE_ALL ) = 0;
00342
00347 virtual SoBufferObject* createInstance() const = 0;
00348
00349
00350
00354 virtual void clearInstance() = 0;
00355
00362 SoDeviceContext* getContext() const;
00363
00367 SoBufferObject* getMappedBufferObject();
00368
00369
00376 void setMappedBufferObject( SoBufferObject* bufferObject );
00377
00383 void setMappingAccessMode( AccessMode accessMode );
00384
00385
00389 AccessMode getMappingAccessMode();
00390
00395 void setMappingZoneInformation( size_t startPosition, size_t size );
00396
00402 size_t getMappingStartPosition() const;
00403
00409 size_t getMappingSize() const;
00410
00411
00420 static SoCpuBufferObjectCache* getBufferObjectCache();
00421
00422 private:
00423
00424
00425 void forceSize(size_t size);
00426
00427
00428 virtual void copyToCpuBuffer( SoCpuBufferObject* targetBufferObject, size_t destOffset = 0, size_t sourceOffset = 0, size_t copySize = SO_BUFFER_SIZE_ALL );
00429
00431 virtual SoBufferObject* clone() const;
00432
00437 uint64_t getModificationsCounter() const;
00438
00442 void incrementModificationsCounter() const;
00443
00444
00445 private:
00452 SoBufferObject();
00453
00458 virtual ~SoBufferObject();
00459
00460
00466 void setDeviceContext( SoDeviceContext* context );
00467
00471 bool checkCopyConditions( size_t sourceSize, size_t sourceOffset,
00472 size_t targetSize, size_t targetOffset, size_t copySize );
00473
00474
00479 void touch();
00480
00484 uint64_t getNextModificationId() const;
00485
00486
00487
00488 private:
00489
00490
00491
00492 private:
00494 size_t m_size;
00495
00499 SoBufferObject(const SoBufferObject&)
00500 : SoRefCounter()
00501 , SoTypedObject() {}
00502
00504 SoRef<SoDeviceContext> m_deviceContext;
00505
00507 SbThreadSpinlock m_bufferMutex;
00508
00510 SoBufferObject* m_mappedBufferObject;
00511
00513 AccessMode m_mappingAccessMode;
00514
00515 size_t m_mappingStartPosition;
00516
00517 size_t m_mappingSize;
00518
00519 mutable uint64_t m_modificationsCounter;
00520
00521
00522 static SoCpuBufferObjectCache* s_bufferObjectCache;
00523
00524 };
00525
00526
00527
00528
00529
00530
00531 inline SoBufferObject*
00532 SoBufferObject::getMappedBufferObject()
00533 {
00534 return m_mappedBufferObject;
00535 }
00536
00537
00538
00539 inline void
00540 SoBufferObject::setMappingAccessMode( SoBufferObject::AccessMode accessMode )
00541 {
00542 m_mappingAccessMode = accessMode;
00543 }
00544
00545
00546
00547 inline SoBufferObject::AccessMode
00548 SoBufferObject::getMappingAccessMode()
00549 {
00550 return m_mappingAccessMode;
00551 }
00552
00553
00554
00555 inline void
00556 SoBufferObject::setMappingZoneInformation( size_t startPosition, size_t size )
00557 {
00558 m_mappingStartPosition = startPosition;
00559 m_mappingSize = size;
00560 }
00561
00562
00563
00564 inline size_t
00565 SoBufferObject::getMappingStartPosition() const
00566 {
00567 return m_mappingStartPosition;
00568 }
00569
00570
00571
00572 inline size_t
00573 SoBufferObject::getMappingSize() const
00574 {
00575 return m_mappingSize;
00576 }
00577
00578
00579
00580 inline bool
00581 SoBufferObject::setSize( size_t size )
00582 {
00583 m_size = size;
00584 incrementModificationsCounter();
00585 return true;
00586 }
00587
00588
00589 inline size_t
00590 SoBufferObject::getSize() const
00591 {
00592 return m_size;
00593 }
00594
00595
00596 inline bool
00597 SoBufferObject::checkCopyConditions( size_t sourceSize, size_t sourceOffset,
00598 size_t targetSize, size_t targetOffset, size_t copySize )
00599 {
00600 if ( copySize == SO_BUFFER_SIZE_ALL )
00601 {
00602 if ( targetOffset >= getSize() )
00603 return false;
00604 else
00605 copySize = sourceSize - sourceOffset;
00606 }
00607
00608 if ( sourceOffset + copySize > sourceSize )
00609 return false;
00610
00611 if ( targetOffset + copySize > targetSize )
00612 return false;
00613
00614 return true;
00615 }
00616
00617
00618
00619 inline SoDeviceContext*
00620 SoBufferObject::getContext() const
00621 {
00622 return m_deviceContext.ptr();
00623 }
00624
00625
00626
00627
00628 inline void
00629 SoBufferObject::setDeviceContext( SoDeviceContext* context )
00630 {
00631 m_deviceContext = context;
00632 }
00633
00634
00635 inline uint64_t
00636 SoBufferObject::getModificationsCounter() const
00637 {
00638 return m_modificationsCounter;
00639 }
00640
00641
00642 inline void
00643 SoBufferObject::incrementModificationsCounter() const
00644 {
00645 m_modificationsCounter = getNextModificationId();
00646 }
00647
00648 #ifdef _WIN32
00649 #pragma warning(pop)
00650 #endif
00651
00652 #endif //SOBUFFEROBJECT_H
00653
00654
00655