[148] | 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-2013 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 __VertexIndexData_H__ |
---|
| 29 | #define __VertexIndexData_H__ |
---|
| 30 | |
---|
| 31 | #include "OgrePrerequisites.h" |
---|
| 32 | #include "OgreHardwareVertexBuffer.h" |
---|
| 33 | #include "OgreHardwareIndexBuffer.h" |
---|
| 34 | #include "OgreHeaderPrefix.h" |
---|
| 35 | |
---|
| 36 | namespace Ogre { |
---|
| 37 | /** \addtogroup Core |
---|
| 38 | * @{ |
---|
| 39 | */ |
---|
| 40 | /** \addtogroup RenderSystem |
---|
| 41 | * @{ |
---|
| 42 | */ |
---|
| 43 | |
---|
| 44 | /// Define a list of usage flags |
---|
| 45 | typedef vector<HardwareBuffer::Usage>::type BufferUsageList; |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | /** Summary class collecting together vertex source information. */ |
---|
| 49 | class _OgreExport VertexData : public VertexDataAlloc |
---|
| 50 | { |
---|
| 51 | private: |
---|
| 52 | /// Protected copy constructor, to prevent misuse |
---|
| 53 | VertexData(const VertexData& rhs); /* do nothing, should not use */ |
---|
| 54 | /// Protected operator=, to prevent misuse |
---|
| 55 | VertexData& operator=(const VertexData& rhs); /* do not use */ |
---|
| 56 | |
---|
| 57 | HardwareBufferManagerBase* mMgr; |
---|
| 58 | public: |
---|
| 59 | /** Constructor. |
---|
| 60 | @note |
---|
| 61 | This constructor creates the VertexDeclaration and VertexBufferBinding |
---|
| 62 | automatically, and arranges for their deletion afterwards. |
---|
| 63 | @param mgr Optional HardwareBufferManager from which to create resources |
---|
| 64 | */ |
---|
| 65 | VertexData(HardwareBufferManagerBase* mgr = 0); |
---|
| 66 | /** Constructor. |
---|
| 67 | @note |
---|
| 68 | This constructor receives the VertexDeclaration and VertexBufferBinding |
---|
| 69 | from the caller, and as such does not arrange for their deletion afterwards, |
---|
| 70 | the caller remains responsible for that. |
---|
| 71 | @param dcl The VertexDeclaration to use |
---|
| 72 | @param bind The VertexBufferBinding to use |
---|
| 73 | */ |
---|
| 74 | VertexData(VertexDeclaration* dcl, VertexBufferBinding* bind); |
---|
| 75 | ~VertexData(); |
---|
| 76 | |
---|
| 77 | /** Declaration of the vertex to be used in this operation. |
---|
| 78 | @remarks Note that this is created for you on construction. |
---|
| 79 | */ |
---|
| 80 | VertexDeclaration* vertexDeclaration; |
---|
| 81 | /** The vertex buffer bindings to be used. |
---|
| 82 | @remarks Note that this is created for you on construction. |
---|
| 83 | */ |
---|
| 84 | VertexBufferBinding* vertexBufferBinding; |
---|
| 85 | /// Whether this class should delete the declaration and binding |
---|
| 86 | bool mDeleteDclBinding; |
---|
| 87 | /// The base vertex index to start from |
---|
| 88 | size_t vertexStart; |
---|
| 89 | /// The number of vertices used in this operation |
---|
| 90 | size_t vertexCount; |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | /// Struct used to hold hardware morph / pose vertex data information |
---|
| 94 | struct HardwareAnimationData |
---|
| 95 | { |
---|
| 96 | unsigned short targetBufferIndex; |
---|
| 97 | Real parametric; |
---|
| 98 | }; |
---|
| 99 | typedef vector<HardwareAnimationData>::type HardwareAnimationDataList; |
---|
| 100 | /// VertexElements used for hardware morph / pose animation |
---|
| 101 | HardwareAnimationDataList hwAnimationDataList; |
---|
| 102 | /// Number of hardware animation data items used |
---|
| 103 | size_t hwAnimDataItemsUsed; |
---|
| 104 | |
---|
| 105 | /** Clones this vertex data, potentially including replicating any vertex buffers. |
---|
| 106 | @param copyData Whether to create new vertex buffers too or just reference the existing ones |
---|
| 107 | @param mgr If supplied, the buffer manager through which copies should be made |
---|
| 108 | @remarks The caller is expected to delete the returned pointer when ready |
---|
| 109 | */ |
---|
| 110 | VertexData* clone(bool copyData = true, HardwareBufferManagerBase* mgr = 0) const; |
---|
| 111 | |
---|
| 112 | /** Modifies the vertex data to be suitable for use for rendering shadow geometry. |
---|
| 113 | @remarks |
---|
| 114 | Preparing vertex data to generate a shadow volume involves firstly ensuring that the |
---|
| 115 | vertex buffer containing the positions is a standalone vertex buffer, |
---|
| 116 | with no other components in it. This method will therefore break apart any existing |
---|
| 117 | vertex buffers if position is sharing a vertex buffer. |
---|
| 118 | Secondly, it will double the size of this vertex buffer so that there are 2 copies of |
---|
| 119 | the position data for the mesh. The first half is used for the original, and the second |
---|
| 120 | half is used for the 'extruded' version. The vertex count used to render will remain |
---|
| 121 | the same though, so as not to add any overhead to regular rendering of the object. |
---|
| 122 | Both copies of the position are required in one buffer because shadow volumes stretch |
---|
| 123 | from the original mesh to the extruded version. |
---|
| 124 | @par |
---|
| 125 | It's important to appreciate that this method can fundamentally change the structure of your |
---|
| 126 | vertex buffers, although in reality they will be new buffers. As it happens, if other |
---|
| 127 | objects are using the original buffers then they will be unaffected because the reference |
---|
| 128 | counting will keep them intact. However, if you have made any assumptions about the |
---|
| 129 | structure of the vertex data in the buffers of this object, you may have to rethink them. |
---|
| 130 | */ |
---|
| 131 | void prepareForShadowVolume(void); |
---|
| 132 | |
---|
| 133 | /** Additional shadow volume vertex buffer storage. |
---|
| 134 | @remarks |
---|
| 135 | This additional buffer is only used where we have prepared this VertexData for |
---|
| 136 | use in shadow volume construction, and where the current render system supports |
---|
| 137 | vertex programs. This buffer contains the 'w' vertex position component which will |
---|
| 138 | be used by that program to differentiate between extruded and non-extruded vertices. |
---|
| 139 | This 'w' component cannot be included in the original position buffer because |
---|
| 140 | DirectX does not allow 4-component positions in the fixed-function pipeline, and the original |
---|
| 141 | position buffer must still be usable for fixed-function rendering. |
---|
| 142 | @par |
---|
| 143 | Note that we don't store any vertex declaration or vertex buffer binding here because this |
---|
| 144 | can be reused in the shadow algorithm. |
---|
| 145 | */ |
---|
| 146 | HardwareVertexBufferSharedPtr hardwareShadowVolWBuffer; |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | /** Reorganises the data in the vertex buffers according to the |
---|
| 150 | new vertex declaration passed in. Note that new vertex buffers |
---|
| 151 | are created and written to, so if the buffers being referenced |
---|
| 152 | by this vertex data object are also used by others, then the |
---|
| 153 | original buffers will not be damaged by this operation. |
---|
| 154 | Once this operation has completed, the new declaration |
---|
| 155 | passed in will overwrite the current one. |
---|
| 156 | @param newDeclaration The vertex declaration which will be used |
---|
| 157 | for the reorganised buffer state. Note that the new declaration |
---|
| 158 | must not include any elements which do not already exist in the |
---|
| 159 | current declaration; you can drop elements by |
---|
| 160 | excluding them from the declaration if you wish, however. |
---|
| 161 | @param bufferUsage Vector of usage flags which indicate the usage options |
---|
| 162 | for each new vertex buffer created. The indexes of the entries must correspond |
---|
| 163 | to the buffer binding values referenced in the declaration. |
---|
| 164 | @param mgr Optional pointer to the manager to use to create new declarations |
---|
| 165 | and buffers etc. If not supplied, the HardwareBufferManager singleton will be used |
---|
| 166 | */ |
---|
| 167 | void reorganiseBuffers(VertexDeclaration* newDeclaration, const BufferUsageList& bufferUsage, |
---|
| 168 | HardwareBufferManagerBase* mgr = 0); |
---|
| 169 | |
---|
| 170 | /** Reorganises the data in the vertex buffers according to the |
---|
| 171 | new vertex declaration passed in. Note that new vertex buffers |
---|
| 172 | are created and written to, so if the buffers being referenced |
---|
| 173 | by this vertex data object are also used by others, then the |
---|
| 174 | original buffers will not be damaged by this operation. |
---|
| 175 | Once this operation has completed, the new declaration |
---|
| 176 | passed in will overwrite the current one. |
---|
| 177 | This version of the method derives the buffer usages from the existing |
---|
| 178 | buffers, by using the 'most flexible' usage from the equivalent sources. |
---|
| 179 | @param newDeclaration The vertex declaration which will be used |
---|
| 180 | for the reorganised buffer state. Note that the new delcaration |
---|
| 181 | must not include any elements which do not already exist in the |
---|
| 182 | current declaration; you can drop elements by |
---|
| 183 | excluding them from the declaration if you wish, however. |
---|
| 184 | @param mgr Optional pointer to the manager to use to create new declarations |
---|
| 185 | and buffers etc. If not supplied, the HardwareBufferManager singleton will be used |
---|
| 186 | */ |
---|
| 187 | void reorganiseBuffers(VertexDeclaration* newDeclaration, HardwareBufferManagerBase* mgr = 0); |
---|
| 188 | |
---|
| 189 | /** Remove any gaps in the vertex buffer bindings. |
---|
| 190 | @remarks |
---|
| 191 | This is useful if you've removed elements and buffers from this vertex |
---|
| 192 | data and want to remove any gaps in the vertex buffer bindings. This |
---|
| 193 | method is mainly useful when reorganising vertex data manually. |
---|
| 194 | @note |
---|
| 195 | This will cause binding index of the elements in the vertex declaration |
---|
| 196 | to be altered to new binding index. |
---|
| 197 | */ |
---|
| 198 | void closeGapsInBindings(void); |
---|
| 199 | |
---|
| 200 | /** Remove all vertex buffers that never used by the vertex declaration. |
---|
| 201 | @remarks |
---|
| 202 | This is useful if you've removed elements from the vertex declaration |
---|
| 203 | and want to unreference buffers that never used any more. This method |
---|
| 204 | is mainly useful when reorganising vertex data manually. |
---|
| 205 | @note |
---|
| 206 | This also remove any gaps in the vertex buffer bindings. |
---|
| 207 | */ |
---|
| 208 | void removeUnusedBuffers(void); |
---|
| 209 | |
---|
| 210 | /** Convert all packed colour values (VET_COLOUR_*) in buffers used to |
---|
| 211 | another type. |
---|
| 212 | @param srcType The source colour type to assume if the ambiguous VET_COLOUR |
---|
| 213 | is encountered. |
---|
| 214 | @param destType The destination colour type, must be VET_COLOUR_ABGR or |
---|
| 215 | VET_COLOUR_ARGB. |
---|
| 216 | */ |
---|
| 217 | void convertPackedColour(VertexElementType srcType, VertexElementType destType); |
---|
| 218 | |
---|
| 219 | |
---|
| 220 | /** Allocate elements to serve a holder of morph / pose target data |
---|
| 221 | for hardware morphing / pose blending. |
---|
| 222 | @remarks |
---|
| 223 | This method will allocate the given number of 3D texture coordinate |
---|
| 224 | sets for use as a morph target or target pose offset (3D position). |
---|
| 225 | These elements will be saved in hwAnimationDataList. |
---|
| 226 | It will also assume that the source of these new elements will be new |
---|
| 227 | buffers which are not bound at this time, so will start the sources to |
---|
| 228 | 1 higher than the current highest binding source. The caller is |
---|
| 229 | expected to bind these new buffers when appropriate. For morph animation |
---|
| 230 | the original position buffer will be the 'from' keyframe data, whilst |
---|
| 231 | for pose animation it will be the original vertex data. |
---|
| 232 | If normals are animated, then twice the number of 3D texture coordinates are required |
---|
| 233 | @return The number of sets that were supported |
---|
| 234 | */ |
---|
| 235 | ushort allocateHardwareAnimationElements(ushort count, bool animateNormals); |
---|
| 236 | |
---|
| 237 | |
---|
| 238 | |
---|
| 239 | }; |
---|
| 240 | |
---|
| 241 | /** Summary class collecting together index data source information. */ |
---|
| 242 | class _OgreExport IndexData : public IndexDataAlloc |
---|
| 243 | { |
---|
| 244 | protected: |
---|
| 245 | /// Protected copy constructor, to prevent misuse |
---|
| 246 | IndexData(const IndexData& rhs); /* do nothing, should not use */ |
---|
| 247 | /// Protected operator=, to prevent misuse |
---|
| 248 | IndexData& operator=(const IndexData& rhs); /* do not use */ |
---|
| 249 | public: |
---|
| 250 | IndexData(); |
---|
| 251 | ~IndexData(); |
---|
| 252 | /// Pointer to the HardwareIndexBuffer to use, must be specified if useIndexes = true |
---|
| 253 | HardwareIndexBufferSharedPtr indexBuffer; |
---|
| 254 | |
---|
| 255 | /// Index in the buffer to start from for this operation |
---|
| 256 | size_t indexStart; |
---|
| 257 | |
---|
| 258 | /// The number of indexes to use from the buffer |
---|
| 259 | size_t indexCount; |
---|
| 260 | |
---|
| 261 | /** Clones this index data, potentially including replicating the index buffer. |
---|
| 262 | @param copyData Whether to create new buffers too or just reference the existing ones |
---|
| 263 | @param mgr If supplied, the buffer manager through which copies should be made |
---|
| 264 | @remarks The caller is expected to delete the returned pointer when finished |
---|
| 265 | */ |
---|
| 266 | IndexData* clone(bool copyData = true, HardwareBufferManagerBase* mgr = 0) const; |
---|
| 267 | |
---|
| 268 | /** Re-order the indexes in this index data structure to be more |
---|
| 269 | vertex cache friendly; that is to re-use the same vertices as close |
---|
| 270 | together as possible. |
---|
| 271 | @remarks |
---|
| 272 | Can only be used for index data which consists of triangle lists. |
---|
| 273 | It would in fact be pointless to use it on triangle strips or fans |
---|
| 274 | in any case. |
---|
| 275 | */ |
---|
| 276 | void optimiseVertexCacheTriList(void); |
---|
| 277 | |
---|
| 278 | }; |
---|
| 279 | |
---|
| 280 | /** Vertex cache profiler. |
---|
| 281 | @remarks |
---|
| 282 | Utility class for evaluating the effectiveness of the use of the vertex |
---|
| 283 | cache by a given index buffer. |
---|
| 284 | */ |
---|
| 285 | class _OgreExport VertexCacheProfiler : public BufferAlloc |
---|
| 286 | { |
---|
| 287 | public: |
---|
| 288 | enum CacheType { |
---|
| 289 | FIFO, LRU |
---|
| 290 | }; |
---|
| 291 | |
---|
| 292 | VertexCacheProfiler(unsigned int cachesize = 16, CacheType cachetype = FIFO ) |
---|
| 293 | : size ( cachesize ), tail (0), buffersize (0), hit (0), miss (0) |
---|
| 294 | { |
---|
| 295 | cache = OGRE_ALLOC_T(uint32, size, MEMCATEGORY_GEOMETRY); |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | ~VertexCacheProfiler() |
---|
| 299 | { |
---|
| 300 | OGRE_FREE(cache, MEMCATEGORY_GEOMETRY); |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | void profile(const HardwareIndexBufferSharedPtr& indexBuffer); |
---|
| 304 | void reset() { hit = 0; miss = 0; tail = 0; buffersize = 0; } |
---|
| 305 | void flush() { tail = 0; buffersize = 0; } |
---|
| 306 | |
---|
| 307 | unsigned int getHits() { return hit; } |
---|
| 308 | unsigned int getMisses() { return miss; } |
---|
| 309 | unsigned int getSize() { return size; } |
---|
| 310 | private: |
---|
| 311 | unsigned int size; |
---|
| 312 | uint32 *cache; |
---|
| 313 | |
---|
| 314 | unsigned int tail, buffersize; |
---|
| 315 | unsigned int hit, miss; |
---|
| 316 | |
---|
| 317 | bool inCache(unsigned int index); |
---|
| 318 | }; |
---|
| 319 | /** @} */ |
---|
| 320 | /** @} */ |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | #include "OgreHeaderSuffix.h" |
---|
| 324 | |
---|
| 325 | #endif |
---|
| 326 | |
---|