driver_opengl_vertex_buffer_hard.h

Go to the documentation of this file.
00001 
00005 /* Copyright, 2000-2002 Nevrax Ltd.
00006  *
00007  * This file is part of NEVRAX NEL.
00008  * NEVRAX NEL is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2, or (at your option)
00011  * any later version.
00012 
00013  * NEVRAX NEL is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * General Public License for more details.
00017 
00018  * You should have received a copy of the GNU General Public License
00019  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00020  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00021  * MA 02111-1307, USA.
00022  */
00023 
00024 #ifndef NL_DRIVER_OPENGL_VERTEX_BUFFER_HARD_H
00025 #define NL_DRIVER_OPENGL_VERTEX_BUFFER_HARD_H
00026 
00027 #include "nel/misc/types_nl.h"
00028 
00029 
00030 namespace NL3D
00031 {
00032 
00033 
00034 class   CDriverGL;
00035 class   IVertexBufferHardGL;
00036 class   CVertexBufferInfo;
00037 class   CVertexBufferHardGLMapObjectATI;
00038 class   CVertexBufferHardARB;
00039 
00040 
00041 
00042 // ***************************************************************************
00043 // ***************************************************************************
00044 // VBHard interface for both NVidia / ATI extension.
00045 // ***************************************************************************
00046 // ***************************************************************************
00047 
00048 
00049 
00050 
00051 // ***************************************************************************
00054 class IVertexArrayRange
00055 {
00056 public:
00057     IVertexArrayRange(CDriverGL *drv);
00058     virtual ~IVertexArrayRange();
00059 
00061     virtual bool                    allocate(uint32 size, CVertexBuffer::TPreferredMemory vbType)= 0;
00063     virtual void                    free()= 0;
00065     virtual IVertexBufferHardGL     *createVBHardGL(uint size, CVertexBuffer *vb) =0;
00067     virtual uint                    sizeAllocated() const =0;
00068     // Check & invalidate lost buffers. Default assume they can't be lost
00069     virtual void updateLostBuffers() {}
00070     // Get driver
00071     CDriverGL   *getDriver() const { return _Driver; }
00072     // tmp, for debug
00073     #ifdef NL_DEBUG
00074         virtual void         dumpMappedBuffers() {}
00075     #endif
00076 protected:
00077     CDriverGL   *_Driver;
00078 };
00079 
00080 
00081 
00082 // ***************************************************************************
00086 class IVertexBufferHardGL
00087 {
00088 public:
00089 
00090     IVertexBufferHardGL(CDriverGL *drv, CVertexBuffer *vb);
00091     virtual ~IVertexBufferHardGL();
00092 
00093 
00094     // ATI and NVidia have their own methods.
00095     virtual void        *lock() = 0;
00096     virtual void        unlock() = 0;
00097     virtual void        unlock(uint start, uint end) = 0;
00098     virtual void        *getPointer() = 0;
00099 
00100     virtual void            enable() =0;
00101     virtual void            disable() =0;
00102 
00103     virtual void        setupVBInfos(CVertexBufferInfo &vb) = 0;
00104 
00105     enum TVBType { NVidiaVB, ATIVB, ATIMapObjectVB, ARBVB, UnknownVB };
00106     // true if NVidia vertex buffer hard.
00107     TVBType  VBType;
00108     // For Fence access. Ignored for ATI.
00109     bool                GPURenderingAfterFence;
00110 
00111     // test if buffer content is invalid. If so, no rendering should occurs (rendering should silently fail)
00112     bool                            isInvalid() { return _Invalid; }
00113 
00114 public:
00115 
00116     CVertexBuffer       *VB;
00117 
00118 protected:
00119     CDriverGL           *_Driver;
00120     bool         _Invalid;
00121 };
00122 
00123 
00124 
00125 // ***************************************************************************
00126 // ***************************************************************************
00127 // NVidia implementation
00128 // ***************************************************************************
00129 // ***************************************************************************
00130 
00131 
00132 // ***************************************************************************
00136 class CVertexArrayRangeNVidia : public IVertexArrayRange
00137 {
00138 public:
00139     CVertexArrayRangeNVidia(CDriverGL *drv);
00140 
00141 
00143     // @{
00145     virtual bool                    allocate(uint32 size, CVertexBuffer::TPreferredMemory vbType);
00147     virtual void                    free();
00149     virtual IVertexBufferHardGL     *createVBHardGL(uint size, CVertexBuffer *vb);
00151     virtual uint                    sizeAllocated() const;
00152     // @}
00153 
00154 
00155     // Those methods read/write in _Driver->_CurrentVertexArrayRange.
00157     void            enable();
00159     void            disable();
00160 
00162     void            freeVB(void *ptr);
00163 
00164 
00165 // *************************
00166 private:
00167     void        *_VertexArrayPtr;
00168     uint32      _VertexArraySize;
00169 
00170     // Allocator.
00171     NLMISC::CHeapMemory     _HeapMemory;
00172 
00174     bool            allocated() const {return _VertexArrayPtr!=NULL;}
00176     void            *allocateVB(uint32 size);
00177 
00178 };
00179 
00180 
00181 
00182 // ***************************************************************************
00184 class CVertexBufferHardGLNVidia : public IVertexBufferHardGL
00185 {
00186 public:
00187 
00188     CVertexBufferHardGLNVidia(CDriverGL *drv, CVertexBuffer *vb);
00189     virtual ~CVertexBufferHardGLNVidia();
00190 
00191 
00193     // @{
00194     virtual void        *lock();
00195     virtual void        unlock();
00196     virtual void        unlock(uint start, uint end);
00197     virtual void        *getPointer();
00198     virtual void        enable();
00199     virtual void        disable();
00200     virtual void        lockHintStatic(bool staticLock);
00201     virtual void        setupVBInfos(CVertexBufferInfo &vb);
00202     // @}
00203 
00204 
00205     // setup ptrs allocated by createVBHard(), and init Fence.
00206     void                    initGL(CVertexArrayRangeNVidia *var, void *vertexPtr);
00207 
00208 
00209     // true if a setFence() has been done, without a finishFence(). NB: not the same thing as nglTestFenceNV()!!
00210     bool            isFenceSet() const {return _FenceSet;}
00211     // set or re-set the fence, whatever isFenceSet().
00212     void            setFence();
00213     // if(isFenceSet()), finish the fence, else no-op
00214     void            finishFence();
00215     // if fence is not set, no-op, else test nglTestFenceNV(), and update local _FenceSet flag.
00216     void            testFence();
00217 
00218     bool            getLockHintStatic() const {return _LockHintStatic;}
00219 
00220 // *************************
00221 private:
00222     CVertexArrayRangeNVidia     *_VertexArrayRange;
00223     void                        *_VertexPtr;
00224 
00225     // The fence inserted in command stream
00226     GLuint              _Fence;
00227     // True if a setFence() has been done, without a finishFence().
00228     bool                _FenceSet;
00229 
00230     bool                _LockHintStatic;
00231 
00232 };
00233 
00234 
00235 
00236 // ***************************************************************************
00237 // ***************************************************************************
00238 // ATI implementation
00239 // ***************************************************************************
00240 // ***************************************************************************
00241 
00242 
00243 // ***************************************************************************
00244 // This is a fake value to simulate allocation on CHeapMemory for ATI mgt.
00245 // NB: can set 0, since won't work for multiple reasons....
00246 #define NL3D_DRV_ATI_FAKE_MEM_START 0x10000
00247 
00248 
00249 // ***************************************************************************
00253 class CVertexArrayRangeATI : public IVertexArrayRange
00254 {
00255 public:
00256     CVertexArrayRangeATI(CDriverGL *drv);
00257 
00258 
00260     // @{
00262     virtual bool                    allocate(uint32 size, CVertexBuffer::TPreferredMemory vbType);
00264     virtual void                    free();
00266     virtual IVertexBufferHardGL     *createVBHardGL(uint size, CVertexBuffer *vb);
00268     virtual uint                    sizeAllocated() const;
00269     // @}
00270 
00271 
00272     // Those methods read/write in _Driver->_CurrentVertexArrayRange.
00276     void            enable();
00280     void            disable();
00281 
00283     void            freeVB(void *ptr);
00284 
00285 
00287     uint            getATIVertexObjectId() const {return _VertexObjectId;}
00288 
00289 // *************************
00290 private:
00291     uint        _VertexObjectId;
00292     bool        _Allocated;
00293     uint32      _VertexArraySize;
00294 
00295     // Allocator. NB: We don't have any Mem Ptr For ATI extension, so use a Fake Ptr: NL3D_DRV_ATI_FAKE_MEM_START
00296     NLMISC::CHeapMemory     _HeapMemory;
00297 
00299     void            *allocateVB(uint32 size);
00300 
00301 };
00302 
00303 
00304 
00305 
00306 // ***************************************************************************
00308 class CVertexBufferHardGLATI : public IVertexBufferHardGL
00309 {
00310 public:
00311 
00312     CVertexBufferHardGLATI(CDriverGL *drv, CVertexBuffer *vb);
00313     virtual ~CVertexBufferHardGLATI();
00314 
00315 
00317     // @{
00318     virtual void        *lock();
00319     virtual void        unlock();
00320     virtual void        unlock(uint start, uint end);
00321     virtual void        *getPointer();
00322     virtual void        enable();
00323     virtual void        disable();
00324     virtual void        lockHintStatic(bool staticLock);
00325     virtual void        setupVBInfos(CVertexBufferInfo &vb);
00326     // @}
00327 
00328 
00330     bool                    createRAMMirror(uint memSize);
00331 
00334     void                    initGL(CVertexArrayRangeATI *var, void *vertexPtr);
00335 
00336 
00337 public:
00338 
00340     uint                    getATIVertexObjectId() const {return _VertexArrayRange->getATIVertexObjectId();}
00341 
00342 
00343 // *************************
00344 private:
00345     CVertexArrayRangeATI        *_VertexArrayRange;
00346     void                        *_VertexPtr;
00347     void                        *_RAMMirrorVertexPtr;
00348     uint                        _RAMMirrorVertexSize;
00349 
00350 };
00351 
00352 
00354 
00360 class CVertexArrayRangeMapObjectATI : public IVertexArrayRange
00361 {
00362 public:
00363     CVertexArrayRangeMapObjectATI(CDriverGL *drv);
00364 
00365 
00367     // @{
00371     virtual bool                    allocate(uint32 size, CVertexBuffer::TPreferredMemory vbType);
00373     virtual void                    free();
00375     virtual IVertexBufferHardGL     *createVBHardGL(uint size, CVertexBuffer *vb);
00377     virtual uint                    sizeAllocated() const { return _SizeAllocated; }
00378     // @}
00379 
00380 
00381     // Those methods read/write in _Driver->_CurrentVertexArrayRange.
00385     void            enable();
00389     void            disable();
00390 
00391     CVertexBuffer::TPreferredMemory getVBType() const { return _VBType; }
00392 
00393     // check & invalidate lost buffers
00394     void updateLostBuffers();
00395 
00396     // tmp for debug
00397     #ifdef NL_DEBUG
00398         void dumpMappedBuffers();
00399     #endif
00400 
00401 // *************************
00402 private:
00403     CVertexBuffer::TPreferredMemory _VBType;
00404     uint32               _SizeAllocated;
00405 public:
00406     // for use by CVertexBufferHardGLMapObjectATI
00407     std::list<CVertexBufferHardGLMapObjectATI *> _LostVBList;
00408     #ifdef NL_DEBUG
00409         std::list<CVertexBufferHardGLMapObjectATI *> _MappedVBList;
00410     #endif
00411 };
00412 
00413 
00416 class CVertexBufferHardGLMapObjectATI : public IVertexBufferHardGL
00417 {
00418 public:
00419 
00420     CVertexBufferHardGLMapObjectATI(CDriverGL *drv, CVertexBuffer *vb);
00421     virtual ~CVertexBufferHardGLMapObjectATI();
00422 
00423 
00425     // @{
00426     virtual void        *lock();
00427     virtual void        unlock();
00428     virtual void        unlock(uint start, uint end);
00429     virtual void        *getPointer();
00430     virtual void        enable();
00431     virtual void        disable();
00432     virtual void        lockHintStatic(bool staticLock);
00433     virtual void        setupVBInfos(CVertexBufferInfo &vb);
00434     // @}
00435 
00438     void                    initGL(CVertexArrayRangeMapObjectATI *var, uint vertexObjectID);
00439 
00440 
00441 public:
00442 
00444     uint                    getATIVertexObjectId() const { return _VertexObjectId;}
00445 
00446 
00447 // *************************
00448 private:
00449     void                           *_VertexPtr; // pointer on current datas. Null if not locked
00450     CVertexArrayRangeMapObjectATI  *_VertexArrayRange;
00451     // if buffer has been invalidated, returns a dummy memory block and silently fails rendering
00452     std::vector<uint8>              _DummyVB;
00453     // Invalidate the buffer (when it is lost, or when a lock fails)
00454     void                            invalidate();
00455 public:
00456     // for use by CVertexArrayRangeMapObjectATI
00457     std::list<CVertexBufferHardGLMapObjectATI *>::iterator _IteratorInLostVBList;
00458     uint                           _VertexObjectId;
00459     // tmp for debug
00460     #ifdef NL_DEBUG
00461         bool                            _Unmapping;
00462         std::list<CVertexBufferHardGLMapObjectATI *>::iterator _IteratorInMappedVBList;
00463     #endif
00464 };
00465 
00466 // ***************************************************************************
00467 // ***************************************************************************
00468 // ARB_vertex_buffer_object implementation
00469 // ***************************************************************************
00470 // ***************************************************************************
00471 class CVertexArrayRangeARB : public IVertexArrayRange
00472 {
00473 public:
00474     CVertexArrayRangeARB(CDriverGL *drv);
00475 
00476 
00478     // @{
00482     virtual bool                    allocate(uint32 size, CVertexBuffer::TPreferredMemory vbType);
00484     virtual void                    free();
00486     virtual IVertexBufferHardGL     *createVBHardGL(uint size, CVertexBuffer *vb);
00488     virtual uint                    sizeAllocated() const { return _SizeAllocated; }
00489     // @}
00490 
00491 
00492     // Those methods read/write in _Driver->_CurrentVertexArrayRange.
00496     void            enable();
00500     void            disable();
00501     // check & invalidate lost buffers
00502     void updateLostBuffers();
00503     //
00504     #ifdef NL_DEBUG
00505         virtual void         dumpMappedBuffers();
00506     #endif
00507 // *************************
00508 private:
00509     CVertexBuffer::TPreferredMemory _VBType;
00510     uint32                          _SizeAllocated;
00511     // for use by CVertexBufferHardARB
00512 public:
00513     std::list<CVertexBufferHardARB *> _LostVBList;
00514     #ifdef NL_DEBUG
00515         std::list<CVertexBufferHardARB *> _MappedVBList;
00516     #endif
00517 };
00518 
00519 
00522 class CVertexBufferHardARB : public IVertexBufferHardGL
00523 {
00524 public:
00525 
00526     CVertexBufferHardARB(CDriverGL *drv, CVertexBuffer *vb);
00527     virtual ~CVertexBufferHardARB();
00528 
00529 
00531     // @{
00532     virtual void        *lock();
00533     virtual void        unlock();
00534     virtual void        unlock(uint startVert, uint endVert);
00535     virtual void        *getPointer();
00536     virtual void        enable();
00537     virtual void        disable();
00538     virtual void        lockHintStatic(bool staticLock);
00539     virtual void        setupVBInfos(CVertexBufferInfo &vb);
00540     // @}
00541 
00544     void                    initGL(uint vertexObjectID, CVertexArrayRangeARB *var, CVertexBuffer::TPreferredMemory memType);
00545 
00546 
00547 public:
00548 
00550     uint                    getARBVertexObjectId() const { return _VertexObjectId;}
00551 
00552     // Invalidate the buffer (when it is lost, or when a lock fails)
00553     void                            invalidate();
00554 
00555     // tmp
00556     void checkMappedVBList();
00557 
00558 // *************************
00559 private:
00560     CVertexArrayRangeARB            *_VertexArrayRange;
00561     CVertexBuffer::TPreferredMemory _MemType;
00562     void                            *_VertexPtr; // pointer on current datas. Null if not locked
00563     // if buffer has been invalidated, returns a dummy memory block and silently fails rendering
00564     std::vector<uint8>              _DummyVB;
00565     // for use by CVertexArrayRangeARB
00566     std::list<CVertexBufferHardARB *>::iterator _IteratorInLostVBList;
00567 public:
00568     uint                            _VertexObjectId;
00569     // tmp for debug
00570     #ifdef NL_DEBUG
00571         bool _Unmapping;
00572         std::list<CVertexBufferHardARB *>::iterator _IteratorInMappedVBList;
00573     #endif
00574 };
00575 
00576 
00577 } // NL3D
00578 
00579 
00580 #endif // NL_DRIVER_OPENGL_VERTEX_BUFFER_HARD_H
00581 
00582 /* End of driver_opengl_vertex_buffer_hard.h */
00583 
00584 
00585 
00586 
00587 
00588 
00589 
00590 
00591 
00592 
00593 
00594 
00595 
00596 
00597 
00598 
00599 
00600 
00601 
00602 
00603 

Generated on Thu Jan 7 08:26:26 2010 for NeL by  doxygen 1.6.1