Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/ogre_src_v1-9-0/OgreMain/include/OgreHardwareBuffer.h @ 148

Last change on this file since 148 was 148, checked in by patricwi, 6 years ago

Added new dependencies for ogre1.9 and cegui0.8

File size: 15.9 KB
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2013 Torus Knot Software Ltd
8
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in
17all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25THE SOFTWARE.
26-----------------------------------------------------------------------------
27*/
28#ifndef __HardwareBuffer__
29#define __HardwareBuffer__
30
31// Precompiler options
32#include "OgrePrerequisites.h"
33#include "OgreException.h"
34
35namespace Ogre {
36
37        /** \addtogroup Core
38        *  @{
39        */
40        /** \addtogroup RenderSystem
41        *  @{
42        */
43        /** Abstract class defining common features of hardware buffers.
44    @remarks
45            A 'hardware buffer' is any area of memory held outside of core system ram,
46            and in our case refers mostly to video ram, although in theory this class
47            could be used with other memory areas such as sound card memory, custom
48            coprocessor memory etc.
49    @par
50            This reflects the fact that memory held outside of main system RAM must
51            be interacted with in a more formal fashion in order to promote
52            cooperative and optimal usage of the buffers between the various
53            processing units which manipulate them.
54    @par
55            This abstract class defines the core interface which is common to all
56            buffers, whether it be vertex buffers, index buffers, texture memory
57            or framebuffer memory etc.
58        @par
59                Buffers have the ability to be 'shadowed' in system memory, this is because
60                the kinds of access allowed on hardware buffers is not always as flexible as
61                that allowed for areas of system memory - for example it is often either
62                impossible, or extremely undesirable from a performance standpoint to read from
63                a hardware buffer; when writing to hardware buffers, you should also write every
64                byte and do it sequentially. In situations where this is too restrictive,
65                it is possible to create a hardware, write-only buffer (the most efficient kind)
66                and to back it with a system memory 'shadow' copy which can be read and updated arbitrarily.
67                Ogre handles synchronising this buffer with the real hardware buffer (which should still be
68                created with the HBU_DYNAMIC flag if you intend to update it very frequently). Whilst this
69                approach does have it's own costs, such as increased memory overhead, these costs can
70                often be outweighed by the performance benefits of using a more hardware efficient buffer.
71                You should look for the 'useShadowBuffer' parameter on the creation methods used to create
72                the buffer of the type you require (see HardwareBufferManager) to enable this feature.
73    */
74        class _OgreExport HardwareBuffer : public BufferAlloc
75    {
76
77            public:
78                    /// Enums describing buffer usage; not mutually exclusive
79                    enum Usage
80                    {
81                /** Static buffer which the application rarely modifies once created. Modifying
82                the contents of this buffer will involve a performance hit.
83                */
84                HBU_STATIC = 1,
85                            /** Indicates the application would like to modify this buffer with the CPU
86                            fairly often.
87                            Buffers created with this flag will typically end up in AGP memory rather
88                            than video memory.
89                            */
90                            HBU_DYNAMIC = 2,
91                            /** Indicates the application will never read the contents of the buffer back,
92                            it will only ever write data. Locking a buffer with this flag will ALWAYS
93                            return a pointer to new, blank memory rather than the memory associated
94                            with the contents of the buffer; this avoids DMA stalls because you can
95                            write to a new memory area while the previous one is being used.
96                            */
97                            HBU_WRITE_ONLY = 4,
98                /** Indicates that the application will be refilling the contents
99                of the buffer regularly (not just updating, but generating the
100                contents from scratch), and therefore does not mind if the contents
101                of the buffer are lost somehow and need to be recreated. This
102                allows and additional level of optimisation on the buffer.
103                This option only really makes sense when combined with
104                HBU_DYNAMIC_WRITE_ONLY.
105                */
106                HBU_DISCARDABLE = 8,
107                                /// Combination of HBU_STATIC and HBU_WRITE_ONLY
108                                HBU_STATIC_WRITE_ONLY = 5, 
109                                /** Combination of HBU_DYNAMIC and HBU_WRITE_ONLY. If you use
110                this, strongly consider using HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE
111                instead if you update the entire contents of the buffer very
112                regularly.
113                */
114                                HBU_DYNAMIC_WRITE_ONLY = 6,
115                /// Combination of HBU_DYNAMIC, HBU_WRITE_ONLY and HBU_DISCARDABLE
116                HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE = 14
117
118
119                    };
120                    /// Locking options
121                    enum LockOptions
122                    {
123                /** Normal mode, ie allows read/write and contents are preserved. */
124                HBL_NORMAL,
125                            /** Discards the <em>entire</em> buffer while locking; this allows optimisation to be
126                                performed because synchronisation issues are relaxed. Only allowed on buffers
127                            created with the HBU_DYNAMIC flag.
128                            */
129                            HBL_DISCARD,
130                            /** Lock the buffer for reading only. Not allowed in buffers which are created with HBU_WRITE_ONLY.
131                                Mandatory on static buffers, i.e. those created without the HBU_DYNAMIC flag.
132                                */ 
133                            HBL_READ_ONLY,
134                /** As HBL_DISCARD, except the application guarantees not to overwrite any
135                region of the buffer which has already been used in this frame, can allow
136                some optimisation on some APIs. */
137                HBL_NO_OVERWRITE,
138                                /** Lock the buffer for writing only.*/
139                                HBL_WRITE_ONLY
140                       
141                    };
142            protected:
143                    size_t mSizeInBytes;
144                    Usage mUsage;
145                    bool mIsLocked;
146                        size_t mLockStart;
147                        size_t mLockSize;
148                        bool mSystemMemory;
149            bool mUseShadowBuffer;
150            HardwareBuffer* mShadowBuffer;
151            bool mShadowUpdated;
152            bool mSuppressHardwareUpdate;
153               
154            /// Internal implementation of lock()
155                    virtual void* lockImpl(size_t offset, size_t length, LockOptions options) = 0;
156            /// Internal implementation of unlock()
157                    virtual void unlockImpl(void) = 0;
158
159    public:
160                    /// Constructor, to be called by HardwareBufferManager only
161            HardwareBuffer(Usage usage, bool systemMemory, bool useShadowBuffer) 
162                                : mUsage(usage), mIsLocked(false), mLockStart(0), mLockSize(0), mSystemMemory(systemMemory), 
163                mUseShadowBuffer(useShadowBuffer), mShadowBuffer(NULL), mShadowUpdated(false), 
164                mSuppressHardwareUpdate(false) 
165            {
166                // If use shadow buffer, upgrade to WRITE_ONLY on hardware side
167                if (useShadowBuffer && usage == HBU_DYNAMIC)
168                {
169                    mUsage = HBU_DYNAMIC_WRITE_ONLY;
170                }
171                else if (useShadowBuffer && usage == HBU_STATIC)
172                {
173                    mUsage = HBU_STATIC_WRITE_ONLY;
174                }
175            }
176            virtual ~HardwareBuffer() {}
177                    /** Lock the buffer for (potentially) reading / writing.
178                    @param offset The byte offset from the start of the buffer to lock
179                    @param length The size of the area to lock, in bytes
180                    @param options Locking options
181                    @return Pointer to the locked memory
182                    */
183                    virtual void* lock(size_t offset, size_t length, LockOptions options)
184            {
185                assert(!isLocked() && "Cannot lock this buffer, it is already locked!");
186
187                                void* ret = NULL;
188                                if ((length + offset) > mSizeInBytes)
189                                {
190                                        OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
191                                                "Lock request out of bounds.",
192                                                "HardwareBuffer::lock");
193                                }
194                                else if (mUseShadowBuffer)
195                {
196                                        if (options != HBL_READ_ONLY)
197                                        {
198                                                // we have to assume a read / write lock so we use the shadow buffer
199                                                // and tag for sync on unlock()
200                        mShadowUpdated = true;
201                    }
202
203                    ret = mShadowBuffer->lock(offset, length, options);
204                }
205                else
206                {
207                                        // Lock the real buffer if there is no shadow buffer
208                    ret = lockImpl(offset, length, options);
209                    mIsLocked = true;
210                }
211                                mLockStart = offset;
212                                mLockSize = length;
213                return ret;
214            }
215
216            /** Lock the entire buffer for (potentially) reading / writing.
217                    @param options Locking options
218                    @return Pointer to the locked memory
219            */
220            void* lock(LockOptions options)
221            {
222                return this->lock(0, mSizeInBytes, options);
223            }
224                    /** Releases the lock on this buffer.
225            @remarks
226                Locking and unlocking a buffer can, in some rare circumstances such as
227                switching video modes whilst the buffer is locked, corrupt the
228                contents of a buffer. This is pretty rare, but if it occurs,
229                this method will throw an exception, meaning you
230                must re-upload the data.
231            @par
232                Note that using the 'read' and 'write' forms of updating the buffer does not
233                suffer from this problem, so if you want to be 100% sure your
234                data will not be lost, use the 'read' and 'write' forms instead.
235            */
236                    virtual void unlock(void)
237            {
238                assert(isLocked() && "Cannot unlock this buffer, it is not locked!");
239
240                                // If we used the shadow buffer this time...
241                if (mUseShadowBuffer && mShadowBuffer->isLocked())
242                {
243                    mShadowBuffer->unlock();
244                    // Potentially update the 'real' buffer from the shadow buffer
245                    _updateFromShadow();
246                }
247                else
248                {
249                                        // Otherwise, unlock the real one
250                    unlockImpl();
251                    mIsLocked = false;
252                }
253
254            }
255
256            /** Reads data from the buffer and places it in the memory pointed to by pDest.
257                    @param offset The byte offset from the start of the buffer to read
258                    @param length The size of the area to read, in bytes
259            @param pDest The area of memory in which to place the data, must be large enough to
260                accommodate the data!
261            */
262            virtual void readData(size_t offset, size_t length, void* pDest) = 0;
263            /** Writes data to the buffer from an area of system memory; note that you must
264                ensure that your buffer is big enough.
265                    @param offset The byte offset from the start of the buffer to start writing
266                    @param length The size of the data to write to, in bytes
267            @param pSource The source of the data to be written
268                        @param discardWholeBuffer If true, this allows the driver to discard the entire buffer when writing,
269                                such that DMA stalls can be avoided; use if you can.
270            */
271            virtual void writeData(size_t offset, size_t length, const void* pSource,
272                                        bool discardWholeBuffer = false) = 0;
273
274                        /** Copy data from another buffer into this one.
275                        @remarks
276                                Note that the source buffer must not be created with the
277                usage HBU_WRITE_ONLY otherwise this will fail.
278                        @param srcBuffer The buffer from which to read the copied data
279                        @param srcOffset Offset in the source buffer at which to start reading
280                        @param dstOffset Offset in the destination buffer to start writing
281                        @param length Length of the data to copy, in bytes.
282                        @param discardWholeBuffer If true, will discard the entire contents of this buffer before copying
283                        */
284                        virtual void copyData(HardwareBuffer& srcBuffer, size_t srcOffset, 
285                                size_t dstOffset, size_t length, bool discardWholeBuffer = false)
286                        {
287                                const void *srcData = srcBuffer.lock(
288                                        srcOffset, length, HBL_READ_ONLY);
289                                this->writeData(dstOffset, length, srcData, discardWholeBuffer);
290                                srcBuffer.unlock();
291                        }
292
293                        /** Copy all data from another buffer into this one.
294                        @remarks
295                                Normally these buffers should be of identical size, but if they're
296                                not, the routine will use the smallest of the two sizes.
297                        */
298                        virtual void copyData(HardwareBuffer& srcBuffer)
299                        {
300                                size_t sz = std::min(getSizeInBytes(), srcBuffer.getSizeInBytes()); 
301                                copyData(srcBuffer, 0, 0, sz, true);
302                        }
303                       
304                        /// Updates the real buffer from the shadow buffer, if required
305            virtual void _updateFromShadow(void)
306            {
307                if (mUseShadowBuffer && mShadowUpdated && !mSuppressHardwareUpdate)
308                {
309                    // Do this manually to avoid locking problems
310                    const void *srcData = mShadowBuffer->lockImpl(
311                                        mLockStart, mLockSize, HBL_READ_ONLY);
312                                        // Lock with discard if the whole buffer was locked, otherwise normal
313                                        LockOptions lockOpt;
314                                        if (mLockStart == 0 && mLockSize == mSizeInBytes)
315                                                lockOpt = HBL_DISCARD;
316                                        else
317                                                lockOpt = HBL_NORMAL;
318                                       
319                    void *destData = this->lockImpl(
320                                        mLockStart, mLockSize, lockOpt);
321                                        // Copy shadow to real
322                    memcpy(destData, srcData, mLockSize);
323                    this->unlockImpl();
324                    mShadowBuffer->unlockImpl();
325                    mShadowUpdated = false;
326                }
327            }
328
329            /// Returns the size of this buffer in bytes
330            size_t getSizeInBytes(void) const { return mSizeInBytes; }
331            /// Returns the Usage flags with which this buffer was created
332            Usage getUsage(void) const { return mUsage; }
333                        /// Returns whether this buffer is held in system memory
334                        bool isSystemMemory(void) const { return mSystemMemory; }
335                        /// Returns whether this buffer has a system memory shadow for quicker reading
336                        bool hasShadowBuffer(void) const { return mUseShadowBuffer; }
337            /// Returns whether or not this buffer is currently locked.
338            bool isLocked(void) const { 
339                return mIsLocked || (mUseShadowBuffer && mShadowBuffer->isLocked()); 
340            }
341            /// Pass true to suppress hardware upload of shadow buffer changes
342            void suppressHardwareUpdate(bool suppress) {
343                mSuppressHardwareUpdate = suppress;
344                if (!suppress)
345                    _updateFromShadow();
346            }
347
348
349
350
351               
352    };
353        /** @} */
354        /** @} */
355
356        /** Locking helper. Guaranteed unlocking even in case of exception. */
357    template <typename T> struct HardwareBufferLockGuard
358    {
359        HardwareBufferLockGuard(const T& p, HardwareBuffer::LockOptions options)
360            : pBuf(p)
361        {
362            pData = pBuf->lock(options);
363        }
364        HardwareBufferLockGuard(const T& p, size_t offset, size_t length, HardwareBuffer::LockOptions options)
365            : pBuf(p)
366        {
367            pData = pBuf->lock(offset, length, options);
368        }               
369        ~HardwareBufferLockGuard()
370        {
371            pBuf->unlock();
372        }
373       
374        const T& pBuf;
375        void* pData;
376    };
377}
378#endif
379
380
Note: See TracBrowser for help on using the repository browser.