/src/ogre/OgreMain/include/OgreHardwareVertexBuffer.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ----------------------------------------------------------------------------- |
3 | | This source file is part of OGRE |
4 | | (Object-oriented Graphics Rendering Engine) |
5 | | For the latest info, see http://www.ogre3d.org/ |
6 | | |
7 | | Copyright (c) 2000-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | #ifndef __HardwareVertexBuffer__ |
29 | | #define __HardwareVertexBuffer__ |
30 | | |
31 | | // Precompiler options |
32 | | #include "OgrePrerequisites.h" |
33 | | #include "OgreHardwareBuffer.h" |
34 | | #include "OgreSharedPtr.h" |
35 | | #include "OgreColourValue.h" |
36 | | #include "OgreHeaderPrefix.h" |
37 | | |
38 | | namespace Ogre { |
39 | | class HardwareBufferManagerBase; |
40 | | |
41 | | /** \addtogroup Core |
42 | | * @{ |
43 | | */ |
44 | | /** \addtogroup RenderSystem |
45 | | * @{ |
46 | | */ |
47 | | /** Specialisation of HardwareBuffer for a vertex buffer. */ |
48 | | class _OgreExport HardwareVertexBuffer final : public HardwareBuffer |
49 | | { |
50 | | bool mIsInstanceData; |
51 | | HardwareBufferManagerBase* mMgr; |
52 | | uint32 mNumVertices; |
53 | | uint32 mVertexSize; |
54 | | uint32 mInstanceDataStepRate; |
55 | | |
56 | | public: |
57 | | /// Should be called by HardwareBufferManager |
58 | | HardwareVertexBuffer(HardwareBufferManagerBase* mgr, size_t vertexSize, size_t numVertices, |
59 | | Usage usage, bool useShadowBuffer); |
60 | | HardwareVertexBuffer(HardwareBufferManagerBase* mgr, size_t vertexSize, size_t numVertices, |
61 | | HardwareBuffer* delegate); |
62 | | ~HardwareVertexBuffer(); |
63 | | /// Return the manager of this buffer, if any |
64 | 0 | HardwareBufferManagerBase* getManager() const { return mMgr; } |
65 | | /// Gets the size in bytes of a single vertex in this buffer |
66 | 0 | uint32 getVertexSize(void) const { return mVertexSize; } |
67 | | /// Get the number of vertices in this buffer |
68 | 0 | uint32 getNumVertices(void) const { return mNumVertices; } |
69 | | /// Get if this vertex buffer is an "instance data" buffer (per instance) |
70 | 0 | bool isInstanceData() const { return mIsInstanceData; } |
71 | | /// Set if this vertex buffer is an "instance data" buffer (per instance) |
72 | | void setIsInstanceData(const bool val); |
73 | | /// Get the number of instances to draw using the same per-instance data before advancing in the buffer by one element. |
74 | | uint32 getInstanceDataStepRate() const; |
75 | | /// Set the number of instances to draw using the same per-instance data before advancing in the buffer by one element. |
76 | | void setInstanceDataStepRate(const size_t val); |
77 | | |
78 | | |
79 | | // NB subclasses should override lock, unlock, readData, writeData |
80 | | |
81 | | }; |
82 | | |
83 | | /// @deprecated use HardwareBufferLockGuard directly |
84 | | OGRE_DEPRECATED typedef HardwareBufferLockGuard HardwareVertexBufferLockGuard; |
85 | | |
86 | | /// Vertex element semantics, used to identify the meaning of vertex buffer contents |
87 | | enum VertexElementSemantic : uint8 |
88 | | { |
89 | | /// Position, typically VET_FLOAT3 |
90 | | VES_POSITION = 1, |
91 | | /// Blending weights |
92 | | VES_BLEND_WEIGHTS = 2, |
93 | | /// Blending indices |
94 | | VES_BLEND_INDICES = 3, |
95 | | /// Normal, typically VET_FLOAT3 |
96 | | VES_NORMAL = 4, |
97 | | /// Colour, typically VET_UBYTE4 |
98 | | VES_COLOUR = 5, |
99 | | /// Secondary colour. Generally free for custom data. Means specular with OpenGL FFP. |
100 | | VES_COLOUR2 = 6, |
101 | | /// Texture coordinates, typically VET_FLOAT2 |
102 | | VES_TEXTURE_COORDINATES = 7, |
103 | | /// Binormal (Y axis if normal is Z) |
104 | | VES_BINORMAL = 8, |
105 | | /// Tangent (X axis if normal is Z) |
106 | | VES_TANGENT = 9, |
107 | | /// The number of VertexElementSemantic elements (note - the first value VES_POSITION is 1) |
108 | | VES_COUNT = 9, |
109 | | /// @deprecated use VES_COLOUR |
110 | | VES_DIFFUSE = VES_COLOUR, |
111 | | /// @deprecated use VES_COLOUR2 |
112 | | VES_SPECULAR = VES_COLOUR2 |
113 | | }; |
114 | | |
115 | | /** |
116 | | * Vertex element type, used to identify the base types of the vertex contents |
117 | | * |
118 | | * VET_SHORT1, VET_SHORT3, VET_USHORT1 and VET_USHORT3 should be used with caution |
119 | | * because they aren't supported on D3D9 and D3D11 - they are unaligned as their size |
120 | | * is not a multiple of 4 bytes. Therefore drivers usually add padding on upload. |
121 | | */ |
122 | | enum VertexElementType : uint8 |
123 | | { |
124 | | VET_FLOAT1 = 0, |
125 | | VET_FLOAT2 = 1, |
126 | | VET_FLOAT3 = 2, |
127 | | VET_FLOAT4 = 3, |
128 | | |
129 | | VET_SHORT1 = 5, ///< not supported on D3D9 |
130 | | VET_SHORT2 = 6, |
131 | | VET_SHORT3 = 7, ///< not supported on D3D9 and D3D11 |
132 | | VET_SHORT4 = 8, |
133 | | VET_UBYTE4 = 9, |
134 | | _DETAIL_SWAP_RB = 10, |
135 | | |
136 | | // the following are not universally supported on all hardware: |
137 | | VET_DOUBLE1 = 12, |
138 | | VET_DOUBLE2 = 13, |
139 | | VET_DOUBLE3 = 14, |
140 | | VET_DOUBLE4 = 15, |
141 | | VET_USHORT1 = 16, ///< not supported on D3D9 |
142 | | VET_USHORT2 = 17, |
143 | | VET_USHORT3 = 18, ///< not supported on D3D9 and D3D11 |
144 | | VET_USHORT4 = 19, |
145 | | VET_INT1 = 20, |
146 | | VET_INT2 = 21, |
147 | | VET_INT3 = 22, |
148 | | VET_INT4 = 23, |
149 | | VET_UINT1 = 24, |
150 | | VET_UINT2 = 25, |
151 | | VET_UINT3 = 26, |
152 | | VET_UINT4 = 27, |
153 | | VET_BYTE4 = 28, ///< signed bytes |
154 | | VET_BYTE4_NORM = 29, ///< signed bytes (normalized to -1..1) |
155 | | VET_UBYTE4_NORM = 30, ///< unsigned bytes (normalized to 0..1) |
156 | | VET_SHORT2_NORM = 31, ///< signed shorts (normalized to -1..1) |
157 | | VET_SHORT4_NORM = 32, |
158 | | VET_USHORT2_NORM = 33, ///< unsigned shorts (normalized to 0..1) |
159 | | VET_USHORT4_NORM = 34, |
160 | | VET_INT_10_10_10_2_NORM = 35, ///< signed int (normalized to 0..1) |
161 | | VET_HALF1 = 36, ///< not supported on D3D9 |
162 | | VET_HALF2 = 37, |
163 | | VET_HALF3 = 38, ///< not supported on D3D9 and D3D11 |
164 | | VET_HALF4 = 39, |
165 | | VET_COLOUR = VET_UBYTE4_NORM, ///< @deprecated use VET_UBYTE4_NORM |
166 | | VET_COLOUR_ARGB = VET_UBYTE4_NORM, ///< @deprecated use VET_UBYTE4_NORM |
167 | | VET_COLOUR_ABGR = VET_UBYTE4_NORM, ///< @deprecated use VET_UBYTE4_NORM |
168 | | }; |
169 | | |
170 | | /** This class declares the usage of a single vertex buffer as a component |
171 | | of a complete VertexDeclaration. |
172 | | |
173 | | Several vertex buffers can be used to supply the input geometry for a |
174 | | rendering operation, and in each case a vertex buffer can be used in |
175 | | different ways for different operations; the buffer itself does not |
176 | | define the semantics (position, normal etc), the VertexElement |
177 | | class does. |
178 | | */ |
179 | | class _OgreExport VertexElement : public VertexDataAlloc |
180 | | { |
181 | | private: |
182 | | /// The offset in the buffer that this element starts at |
183 | | size_t mOffset; |
184 | | /// The source vertex buffer, as bound to an index using VertexBufferBinding |
185 | | unsigned short mSource; |
186 | | /// Index of the item, only applicable for some elements like texture coords |
187 | | unsigned short mIndex; |
188 | | /// The type of element |
189 | | VertexElementType mType; |
190 | | /// The meaning of the element |
191 | | VertexElementSemantic mSemantic; |
192 | | public: |
193 | | /// Constructor, should not be called directly, only needed because of list |
194 | 0 | VertexElement() {} |
195 | | /// Constructor, should not be called directly, call VertexDeclaration::addElement |
196 | | VertexElement(unsigned short source, size_t offset, VertexElementType theType, |
197 | | VertexElementSemantic semantic, unsigned short index = 0); |
198 | | /// Gets the vertex buffer index from where this element draws it's values |
199 | 0 | unsigned short getSource(void) const { return mSource; } |
200 | | /// Gets the offset into the buffer where this element starts |
201 | 0 | size_t getOffset(void) const { return mOffset; } |
202 | | /// Gets the data format of this element |
203 | 0 | VertexElementType getType(void) const { return mType; } |
204 | | /// Gets the meaning of this element |
205 | 0 | VertexElementSemantic getSemantic(void) const { return mSemantic; } |
206 | | /// Gets the index of this element, only applicable for repeating elements |
207 | 0 | unsigned short getIndex(void) const { return mIndex; } |
208 | | /// Gets the size of this element in bytes |
209 | | size_t getSize(void) const; |
210 | | /// Utility method for helping to calculate offsets |
211 | | static size_t getTypeSize(VertexElementType etype); |
212 | | /// Utility method which returns the count of values in a given type (result for colors may be counter-intuitive) |
213 | | static unsigned short getTypeCount(VertexElementType etype); |
214 | | /** Simple converter function which will return a type large enough to hold 'count' values |
215 | | of the same type as the values in 'baseType'. The 'baseType' parameter should have the |
216 | | smallest count available. The return type may have the count rounded up to the next multiple |
217 | | of 4 bytes. Byte types will always return a 4-count type, while short types will return either |
218 | | a 2-count or 4-count type. |
219 | | */ |
220 | | static VertexElementType multiplyTypeCount(VertexElementType baseType, unsigned short count); |
221 | | /** Simple converter function which will turn a type into it's single-value (or lowest multiple-value) |
222 | | equivalent - makes switches on type easier. May give counter-intuitive results with bytes or shorts. |
223 | | */ |
224 | | static VertexElementType getBaseType(VertexElementType multiType); |
225 | | |
226 | | /// @deprecated do not use |
227 | | OGRE_DEPRECATED static void convertColourValue(VertexElementType srcType, VertexElementType dstType, uint32* ptr); |
228 | | |
229 | | /// @deprecated use ColourValue::getAsABGR() |
230 | | OGRE_DEPRECATED static uint32 convertColourValue(const ColourValue& src, VertexElementType) |
231 | 0 | { |
232 | 0 | return src.getAsABGR(); |
233 | 0 | } |
234 | | |
235 | | /// @deprecated use VET_UBYTE4_NORM |
236 | 0 | OGRE_DEPRECATED static VertexElementType getBestColourVertexElementType() { return VET_UBYTE4_NORM; } |
237 | | |
238 | | inline bool operator== (const VertexElement& rhs) const |
239 | 0 | { |
240 | 0 | if (mType != rhs.mType || |
241 | 0 | mIndex != rhs.mIndex || |
242 | 0 | mOffset != rhs.mOffset || |
243 | 0 | mSemantic != rhs.mSemantic || |
244 | 0 | mSource != rhs.mSource) |
245 | 0 | return false; |
246 | 0 | else |
247 | 0 | return true; |
248 | 0 |
|
249 | 0 | } |
250 | | /** Adjusts a pointer to the base of a vertex to point at this element. |
251 | | |
252 | | Pointers are passed as a parameter because we can't |
253 | | rely on covariant return types. |
254 | | @param pBase Pointer to the start of a vertex in this buffer. |
255 | | @param pElem Pointer to a pointer which will be set to the start of this element. |
256 | | */ |
257 | | template<typename T> |
258 | | void baseVertexPointerToElement(void* pBase, T** pElem) const |
259 | | { |
260 | | // The only way we can do this is to cast to char* in order to use byte offset |
261 | | // then cast back to T*. |
262 | | *pElem = reinterpret_cast<T*>(static_cast<char*>(pBase) + mOffset); |
263 | | } |
264 | | }; |
265 | | /** This class declares the format of a set of vertex inputs, which |
266 | | can be issued to the rendering API through a RenderOperation. |
267 | | |
268 | | The ordering is important on Direct3D9 with Direct3D 7 grade cards. |
269 | | Calling closeGapsInSource() will format this VertexDeclaration accordingly. |
270 | | |
271 | | Whilst GL and more modern graphics cards in D3D will allow you to defy these rules, |
272 | | sticking to them will reduce state changes and improve performance on modern APIs as well. |
273 | | |
274 | | Like the other classes in this functional area, these declarations should be created and |
275 | | destroyed using the HardwareBufferManager. |
276 | | */ |
277 | | class _OgreExport VertexDeclaration : public VertexDataAlloc |
278 | | { |
279 | | public: |
280 | | /// Defines the list of vertex elements that makes up this declaration |
281 | | typedef std::list<VertexElement> VertexElementList; |
282 | | protected: |
283 | | VertexElementList mElementList; |
284 | | |
285 | | /** Notify derived class that it is time to invalidate cached state, such as VAO or ID3D11InputLayout */ |
286 | 0 | virtual void notifyChanged() {} |
287 | | public: |
288 | | /// Standard constructor, not you should use HardwareBufferManager::createVertexDeclaration |
289 | | VertexDeclaration(); |
290 | | virtual ~VertexDeclaration(); |
291 | | |
292 | | /** Get the number of elements in the declaration. */ |
293 | 0 | size_t getElementCount(void) const { return mElementList.size(); } |
294 | | /** Gets read-only access to the list of vertex elements. */ |
295 | | const VertexElementList& getElements(void) const; |
296 | | /** Get a single element. */ |
297 | | const VertexElement* getElement(unsigned short index) const; |
298 | | |
299 | | /** Sorts the elements in this list to be compatible with D3D7 graphics cards |
300 | | |
301 | | the order is as follows: position, blending weights, normals, diffuse colours, specular colours, |
302 | | texture coordinates |
303 | | */ |
304 | | void sort(void); |
305 | | |
306 | | /** Remove any gaps in the source buffer list used by this declaration. |
307 | | |
308 | | This is useful if you've modified a declaration and want to remove |
309 | | any gaps in the list of buffers being used. Note, however, that if this |
310 | | declaration is already being used with a VertexBufferBinding, you will |
311 | | need to alter that too. This method is mainly useful when reorganising |
312 | | buffers based on an altered declaration. |
313 | | |
314 | | Whilst in theory you have completely full reign over the format of you vertices, in reality |
315 | | there are some restrictions. D3D7 grade hardware imposes a fixed ordering on the elements which are |
316 | | pulled from each buffer: |
317 | | |
318 | | - VertexElements should be added in the following order, and the order of the elements within any shared |
319 | | buffer should be as follows: |
320 | | 1. Positions |
321 | | 2. Blending weights |
322 | | 3. Normals |
323 | | 4. Diffuse colours |
324 | | 5. Specular colours |
325 | | 6. Texture coordinates (starting at 0, listed in order, with no gaps) |
326 | | - You must not have unused gaps in your buffers which are not referenced by any VertexElement |
327 | | - You must not cause the buffer & offset settings of 2 VertexElements to overlap |
328 | | |
329 | | OpenGL and D3D9 compatible hardware are not required to follow these strict limitations, so you might |
330 | | find, for example that if you broke these rules your application would run under OpenGL and under DirectX on |
331 | | recent cards, but it is not guaranteed to run on older hardware under DirectX unless you stick to the above |
332 | | rules. |
333 | | @note |
334 | | This will also call sort() |
335 | | */ |
336 | | void closeGapsInSource(void); |
337 | | |
338 | | /** Generates a new VertexDeclaration for optimal usage based on the current |
339 | | vertex declaration, which can be used with VertexData::reorganiseBuffers later |
340 | | if you wish, or simply used as a template. |
341 | | |
342 | | Different buffer organisations and buffer usages will be returned |
343 | | depending on the parameters passed to this method. |
344 | | @param skeletalAnimation Whether this vertex data is going to be |
345 | | skeletally animated |
346 | | @param vertexAnimation Whether this vertex data is going to be vertex animated |
347 | | @param vertexAnimationNormals Whether vertex data animation is going to include normals animation |
348 | | */ |
349 | | VertexDeclaration* getAutoOrganisedDeclaration(bool skeletalAnimation, |
350 | | bool vertexAnimation, bool vertexAnimationNormals) const; |
351 | | |
352 | | /** Gets the index of the highest source value referenced by this declaration. */ |
353 | | unsigned short getMaxSource(void) const; |
354 | | |
355 | | |
356 | | |
357 | | /** Adds a new VertexElement to this declaration. |
358 | | |
359 | | This method adds a single element (positions, normals etc) to the end of the |
360 | | vertex declaration. <b>Please read the information in VertexDeclaration about |
361 | | the importance of ordering and structure for compatibility with older D3D drivers</b>. |
362 | | @param source The binding index of HardwareVertexBuffer which will provide the source for this element. |
363 | | See VertexBufferBinding for full information. |
364 | | @param offset The offset in bytes where this element is located in the buffer |
365 | | @param theType The data format of the element (3 floats, a colour etc) |
366 | | @param semantic The meaning of the data (position, normal, diffuse colour etc) |
367 | | @param index Optional index for multi-input elements like texture coordinates |
368 | | @return A reference to the VertexElement added. |
369 | | */ |
370 | | const VertexElement& addElement(unsigned short source, size_t offset, VertexElementType theType, |
371 | | VertexElementSemantic semantic, unsigned short index = 0); |
372 | | /** Inserts a new VertexElement at a given position in this declaration. |
373 | | |
374 | | This method adds a single element (positions, normals etc) at a given position in this |
375 | | vertex declaration. <b>Please read the information in VertexDeclaration about |
376 | | the importance of ordering and structure for compatibility with older D3D drivers</b>. |
377 | | @param atPosition Position where the new element is inserted |
378 | | @param source The binding index of HardwareVertexBuffer which will provide the source for this element. |
379 | | See VertexBufferBinding for full information. |
380 | | @param offset The offset in bytes where this element is located in the buffer |
381 | | @param theType The data format of the element (3 floats, a colour etc) |
382 | | @param semantic The meaning of the data (position, normal, diffuse colour etc) |
383 | | @param index Optional index for multi-input elements like texture coordinates |
384 | | @return A reference to the VertexElement added. |
385 | | */ |
386 | | const VertexElement& insertElement(unsigned short atPosition, |
387 | | unsigned short source, size_t offset, VertexElementType theType, |
388 | | VertexElementSemantic semantic, unsigned short index = 0); |
389 | | |
390 | | /** Remove the element at the given index from this declaration. */ |
391 | | void removeElement(unsigned short elem_index); |
392 | | |
393 | | /** Remove the element with the given semantic and usage index. |
394 | | |
395 | | In this case 'index' means the usage index for repeating elements such |
396 | | as texture coordinates. For other elements this will always be 0 and does |
397 | | not refer to the index in the vector. |
398 | | */ |
399 | | void removeElement(VertexElementSemantic semantic, unsigned short index = 0); |
400 | | |
401 | | /** Remove all elements. */ |
402 | | void removeAllElements(void); |
403 | | |
404 | | /** Modify an element in-place, params as addElement. |
405 | | |
406 | | <b>Please read the information in VertexDeclaration about |
407 | | the importance of ordering and structure for compatibility with older D3D drivers</b>. |
408 | | */ |
409 | | void modifyElement(unsigned short elem_index, unsigned short source, size_t offset, VertexElementType theType, |
410 | | VertexElementSemantic semantic, unsigned short index = 0); |
411 | | |
412 | | /** Finds a VertexElement with the given semantic and index |
413 | | |
414 | | @return The VertexElement or null, if the element is not found |
415 | | */ |
416 | | const VertexElement* findElementBySemantic(VertexElementSemantic sem, unsigned short index = 0) const; |
417 | | /** Based on the current elements, gets the size of the vertex for a given buffer source. |
418 | | @param source The buffer binding index for which to get the vertex size. |
419 | | */ |
420 | | |
421 | | /** Gets a list of elements which use a given source. |
422 | | |
423 | | Note that the list of elements is returned by value therefore is separate from |
424 | | the declaration as soon as this method returns. |
425 | | */ |
426 | | VertexElementList findElementsBySource(unsigned short source) const; |
427 | | |
428 | | /** Gets the vertex size defined by this declaration for a given source. */ |
429 | | size_t getVertexSize(unsigned short source) const; |
430 | | |
431 | | /** Return the index of the next free texture coordinate set which may be added |
432 | | to this declaration. |
433 | | */ |
434 | | unsigned short getNextFreeTextureCoordinate() const; |
435 | | |
436 | | /** Clones this declaration. |
437 | | @param mgr Optional HardwareBufferManager to use for creating the clone |
438 | | (if null, use the current default). |
439 | | */ |
440 | | VertexDeclaration* clone(HardwareBufferManagerBase* mgr = 0) const OGRE_NODISCARD; |
441 | | |
442 | | inline bool operator== (const VertexDeclaration& rhs) const |
443 | 0 | { |
444 | 0 | if (mElementList.size() != rhs.mElementList.size()) |
445 | 0 | return false; |
446 | 0 |
|
447 | 0 | VertexElementList::const_iterator i, iend, rhsi, rhsiend; |
448 | 0 | iend = mElementList.end(); |
449 | 0 | rhsiend = rhs.mElementList.end(); |
450 | 0 | rhsi = rhs.mElementList.begin(); |
451 | 0 | for (i = mElementList.begin(); i != iend && rhsi != rhsiend; ++i, ++rhsi) |
452 | 0 | { |
453 | 0 | if ( !(*i == *rhsi) ) |
454 | 0 | return false; |
455 | 0 | } |
456 | 0 |
|
457 | 0 | return true; |
458 | 0 | } |
459 | | inline bool operator!= (const VertexDeclaration& rhs) const |
460 | 0 | { |
461 | 0 | return !(*this == rhs); |
462 | 0 | } |
463 | | |
464 | | }; |
465 | | |
466 | | /** Records the state of all the vertex buffer bindings required to provide a vertex declaration |
467 | | with the input data it needs for the vertex elements. |
468 | | |
469 | | Why do we have this binding list rather than just have VertexElement referring to the |
470 | | vertex buffers direct? Well, in the underlying APIs, binding the vertex buffers to an |
471 | | index (or 'stream') is the way that vertex data is linked, so this structure better |
472 | | reflects the realities of that. In addition, by separating the vertex declaration from |
473 | | the list of vertex buffer bindings, it becomes possible to reuse bindings between declarations |
474 | | and vice versa, giving opportunities to reduce the state changes required to perform rendering. |
475 | | @par |
476 | | Like the other classes in this functional area, these binding maps should be created and |
477 | | destroyed using the HardwareBufferManager. |
478 | | */ |
479 | | class _OgreExport VertexBufferBinding : public VertexDataAlloc |
480 | | { |
481 | | public: |
482 | | /// Defines the vertex buffer bindings used as source for vertex declarations |
483 | | typedef std::map<unsigned short, HardwareVertexBufferSharedPtr> VertexBufferBindingMap; |
484 | | private: |
485 | | VertexBufferBindingMap mBindingMap; |
486 | | mutable unsigned short mHighIndex; |
487 | | public: |
488 | | /// Constructor, should not be called direct, use HardwareBufferManager::createVertexBufferBinding |
489 | | VertexBufferBinding(); |
490 | | ~VertexBufferBinding(); |
491 | | /** Set a binding, associating a vertex buffer with a given index. |
492 | | |
493 | | If the index is already associated with a vertex buffer, |
494 | | the association will be replaced. This may cause the old buffer |
495 | | to be destroyed if nothing else is referring to it. |
496 | | You should assign bindings from 0 and not leave gaps, although you can |
497 | | bind them in any order. |
498 | | */ |
499 | | void setBinding(unsigned short index, const HardwareVertexBufferSharedPtr& buffer); |
500 | | /** Removes an existing binding. */ |
501 | | void unsetBinding(unsigned short index); |
502 | | |
503 | | /** Removes all the bindings. */ |
504 | | void unsetAllBindings(void); |
505 | | |
506 | | /// Gets a read-only version of the buffer bindings |
507 | | const VertexBufferBindingMap& getBindings(void) const; |
508 | | |
509 | | /// Gets the buffer bound to the given source index |
510 | | const HardwareVertexBufferSharedPtr& getBuffer(unsigned short index) const; |
511 | | /// Gets whether a buffer is bound to the given source index |
512 | | bool isBufferBound(unsigned short index) const; |
513 | | |
514 | 0 | size_t getBufferCount(void) const { return mBindingMap.size(); } |
515 | | |
516 | | /** Gets the highest index which has already been set, plus 1. |
517 | | |
518 | | This is to assist in binding the vertex buffers such that there are |
519 | | not gaps in the list. |
520 | | */ |
521 | 0 | unsigned short getNextIndex(void) const { return mHighIndex++; } |
522 | | |
523 | | /** Gets the last bound index. |
524 | | */ |
525 | | unsigned short getLastBoundIndex(void) const; |
526 | | |
527 | | typedef std::map<ushort, ushort> BindingIndexMap; |
528 | | |
529 | | /** Check whether any gaps in the bindings. |
530 | | */ |
531 | | bool hasGaps(void) const; |
532 | | |
533 | | /** Remove any gaps in the bindings. |
534 | | |
535 | | This is useful if you've removed vertex buffer from this vertex buffer |
536 | | bindings and want to remove any gaps in the bindings. Note, however, |
537 | | that if this bindings is already being used with a VertexDeclaration, |
538 | | you will need to alter that too. This method is mainly useful when |
539 | | reorganising buffers manually. |
540 | | @param |
541 | | bindingIndexMap To be retrieve the binding index map that used to |
542 | | translation old index to new index; will be cleared by this method |
543 | | before fill-in. |
544 | | */ |
545 | | void closeGaps(BindingIndexMap& bindingIndexMap); |
546 | | |
547 | | /// Returns true if this binding has an element that contains instance data |
548 | | bool hasInstanceData() const |
549 | 0 | { |
550 | 0 | for (const auto& b : mBindingMap) |
551 | 0 | if (b.second->isInstanceData()) |
552 | 0 | return true; |
553 | 0 | return false; |
554 | 0 | } |
555 | | }; |
556 | | /** @} */ |
557 | | /** @} */ |
558 | | |
559 | | |
560 | | |
561 | | } |
562 | | |
563 | | #include "OgreHeaderSuffix.h" |
564 | | |
565 | | #endif |
566 | | |