[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 __BaseInstanceBatchVTF_H__ |
---|
| 29 | #define __BaseInstanceBatchVTF_H__ |
---|
| 30 | |
---|
| 31 | #include "OgreInstanceBatch.h" |
---|
| 32 | #include "OgreTexture.h" |
---|
| 33 | |
---|
| 34 | namespace Ogre |
---|
| 35 | { |
---|
| 36 | /** \addtogroup Core |
---|
| 37 | * @{ |
---|
| 38 | */ |
---|
| 39 | /** \addtogroup Scene |
---|
| 40 | * @{ |
---|
| 41 | */ |
---|
| 42 | |
---|
| 43 | /** Instancing implementation using vertex texture through Vertex Texture Fetch (VTF) |
---|
| 44 | This implementation has the following advantages: |
---|
| 45 | * Supports huge amount of instances per batch |
---|
| 46 | * Supports skinning even with huge ammounts of instances per batch |
---|
| 47 | * Doesn't need shader constants registers. |
---|
| 48 | * Best suited for skinned entities |
---|
| 49 | |
---|
| 50 | But beware the disadvantages: |
---|
| 51 | * VTF is only fast on modern GPUs (ATI Radeon HD 2000+, GeForce 8+ series onwards) |
---|
| 52 | * On GeForce 6/7 series VTF is too slow |
---|
| 53 | * VTF isn't (controversely) supported on old ATI X1800 hardware |
---|
| 54 | * Only one bone weight per vertex is supported |
---|
| 55 | * GPUs with low memory bandwidth (i.e. laptops and integrated GPUs) |
---|
| 56 | may perform even worse than no instancing |
---|
| 57 | |
---|
| 58 | Whether this performs great or bad depends on the hardware. It improved up to 4x performance on |
---|
| 59 | a Intel Core 2 Quad Core X9650 GeForce 8600 GTS, and in an Intel Core 2 Duo P7350 ATI |
---|
| 60 | Mobility Radeon HD 4650, but went 0.75x slower on an AthlonX2 5000+ integrated nForce 6150 SE |
---|
| 61 | Each BaseInstanceBatchVTF has it's own texture, which occupies memory in VRAM. |
---|
| 62 | Approx VRAM usage can be computed by doing 12 bytes * 3 * numInstances * numBones |
---|
| 63 | Use flag IM_VTFBESTFIT to avoid wasting VRAM (but may reduce amount of instances per batch). |
---|
| 64 | @par |
---|
| 65 | The material requires at least a texture unit stage named "InstancingVTF" |
---|
| 66 | |
---|
| 67 | @remarks |
---|
| 68 | Design discussion webpage: http://www.ogre3d.org/forums/viewtopic.php?f=4&t=59902 |
---|
| 69 | @author |
---|
| 70 | Matias N. Goldberg ("dark_sylinc") |
---|
| 71 | @version |
---|
| 72 | 1.0 |
---|
| 73 | */ |
---|
| 74 | class _OgreExport BaseInstanceBatchVTF : public InstanceBatch |
---|
| 75 | { |
---|
| 76 | protected: |
---|
| 77 | typedef vector<uint8>::type HWBoneIdxVec; |
---|
| 78 | typedef vector<float>::type HWBoneWgtVec; |
---|
| 79 | typedef vector<Matrix4>::type Matrix4Vec; |
---|
| 80 | |
---|
| 81 | size_t mMatricesPerInstance; //number of bone matrices per instance |
---|
| 82 | size_t mNumWorldMatrices; //Num bones * num instances |
---|
| 83 | TexturePtr mMatrixTexture; //The VTF |
---|
| 84 | |
---|
| 85 | //Used when all matrices from each instance must be in the same row (i.e. HW Instancing). |
---|
| 86 | //A few pixels are wasted, but resizing the texture puts the danger of not sampling the |
---|
| 87 | //right pixel... (in theory it should work, but in practice doesn't) |
---|
| 88 | size_t mWidthFloatsPadding; |
---|
| 89 | size_t mMaxFloatsPerLine; |
---|
| 90 | |
---|
| 91 | size_t mRowLength; |
---|
| 92 | size_t mWeightCount; |
---|
| 93 | //Temporary array used to store 3x4 matrices before they are converted to dual quaternions |
---|
| 94 | float* mTempTransformsArray3x4; |
---|
| 95 | |
---|
| 96 | // The state of the usage of bone matrix lookup |
---|
| 97 | bool mUseBoneMatrixLookup; |
---|
| 98 | size_t mMaxLookupTableInstances; |
---|
| 99 | |
---|
| 100 | bool mUseBoneDualQuaternions; |
---|
| 101 | bool mForceOneWeight; |
---|
| 102 | bool mUseOneWeight; |
---|
| 103 | |
---|
| 104 | /** Clones the base material so it can have it's own vertex texture, and also |
---|
| 105 | clones it's shadow caster materials, if it has any |
---|
| 106 | */ |
---|
| 107 | void cloneMaterial( const MaterialPtr &material ); |
---|
| 108 | |
---|
| 109 | /** Retrieves bone data from the original sub mesh and puts it into an appropriate buffer, |
---|
| 110 | later to be read when creating the vertex semantics. |
---|
| 111 | Assumes outBoneIdx has enough space (base submesh vertex count) |
---|
| 112 | */ |
---|
| 113 | void retrieveBoneIdx( VertexData *baseVertexData, HWBoneIdxVec &outBoneIdx ); |
---|
| 114 | |
---|
| 115 | /** @see retrieveBoneIdx() |
---|
| 116 | Assumes outBoneIdx has enough space (twice the base submesh vertex count, one for each weight) |
---|
| 117 | Assumes outBoneWgt has enough space (twice the base submesh vertex count, one for each weight) |
---|
| 118 | */ |
---|
| 119 | void retrieveBoneIdxWithWeights(VertexData *baseVertexData, HWBoneIdxVec &outBoneIdx, HWBoneWgtVec &outBoneWgt); |
---|
| 120 | |
---|
| 121 | /** Setups the material to use a vertex texture */ |
---|
| 122 | void setupMaterialToUseVTF( TextureType textureType, MaterialPtr &material ); |
---|
| 123 | |
---|
| 124 | /** Creates the vertex texture */ |
---|
| 125 | void createVertexTexture( const SubMesh* baseSubMesh ); |
---|
| 126 | |
---|
| 127 | /** Creates 2 TEXCOORD semantics that will be used to sample the vertex texture */ |
---|
| 128 | virtual void createVertexSemantics( VertexData *thisVertexData, VertexData *baseVertexData, |
---|
| 129 | const HWBoneIdxVec &hwBoneIdx, const HWBoneWgtVec &hwBoneWgt) = 0; |
---|
| 130 | |
---|
| 131 | size_t convert3x4MatricesToDualQuaternions(float* matrices, size_t numOfMatrices, float* outDualQuaternions); |
---|
| 132 | |
---|
| 133 | /** Keeps filling the VTF with world matrix data */ |
---|
| 134 | void updateVertexTexture(void); |
---|
| 135 | |
---|
| 136 | /** Affects VTF texture's width dimension */ |
---|
| 137 | virtual bool matricesTogetherPerRow() const = 0; |
---|
| 138 | |
---|
| 139 | /** update the lookup numbers for entities with shared transforms */ |
---|
| 140 | virtual void updateSharedLookupIndexes(); |
---|
| 141 | |
---|
| 142 | /** @see InstanceBatch::generateInstancedEntity() */ |
---|
| 143 | virtual InstancedEntity* generateInstancedEntity(size_t num); |
---|
| 144 | |
---|
| 145 | public: |
---|
| 146 | BaseInstanceBatchVTF( InstanceManager *creator, MeshPtr &meshReference, const MaterialPtr &material, |
---|
| 147 | size_t instancesPerBatch, const Mesh::IndexMap *indexToBoneMap, |
---|
| 148 | const String &batchName); |
---|
| 149 | virtual ~BaseInstanceBatchVTF(); |
---|
| 150 | |
---|
| 151 | /** @see InstanceBatch::buildFrom */ |
---|
| 152 | void buildFrom( const SubMesh *baseSubMesh, const RenderOperation &renderOperation ); |
---|
| 153 | |
---|
| 154 | //Renderable overloads |
---|
| 155 | void getWorldTransforms( Matrix4* xform ) const; |
---|
| 156 | unsigned short getNumWorldTransforms(void) const; |
---|
| 157 | |
---|
| 158 | /** Overloaded to be able to updated the vertex texture */ |
---|
| 159 | void _updateRenderQueue(RenderQueue* queue); |
---|
| 160 | |
---|
| 161 | /** Sets the state of the usage of bone matrix lookup |
---|
| 162 | |
---|
| 163 | Under default condition each instance entity is assigned a specific area in the vertex |
---|
| 164 | texture for bone matrix data. When turned on the amount of area in the vertex texture |
---|
| 165 | assigned for bone matrix data will be relative to the amount of unique animation states. |
---|
| 166 | Instanced entities sharing the same animation state will share the same area in the matrix. |
---|
| 167 | The specific position of each entity is placed in the vertex data and added in a second phase |
---|
| 168 | in the shader. |
---|
| 169 | |
---|
| 170 | Note this feature only works in VTF_HW for now. |
---|
| 171 | This value needs to be set before adding any instanced entities |
---|
| 172 | */ |
---|
| 173 | void setBoneMatrixLookup(bool enable, size_t maxLookupTableInstances) { assert(mInstancedEntities.empty()); |
---|
| 174 | mUseBoneMatrixLookup = enable; mMaxLookupTableInstances = maxLookupTableInstances; } |
---|
| 175 | |
---|
| 176 | /** Tells whether to use bone matrix lookup |
---|
| 177 | @see setBoneMatrixLookup() |
---|
| 178 | */ |
---|
| 179 | bool useBoneMatrixLookup() const { return mUseBoneMatrixLookup; } |
---|
| 180 | |
---|
| 181 | void setBoneDualQuaternions(bool enable) { assert(mInstancedEntities.empty()); |
---|
| 182 | mUseBoneDualQuaternions = enable; mRowLength = (mUseBoneDualQuaternions ? 2 : 3); } |
---|
| 183 | |
---|
| 184 | bool useBoneDualQuaternions() const { return mUseBoneDualQuaternions; } |
---|
| 185 | |
---|
| 186 | void setForceOneWeight(bool enable) { assert(mInstancedEntities.empty()); |
---|
| 187 | mForceOneWeight = enable; } |
---|
| 188 | |
---|
| 189 | bool forceOneWeight() const { return mForceOneWeight; } |
---|
| 190 | |
---|
| 191 | void setUseOneWeight(bool enable) { assert(mInstancedEntities.empty()); |
---|
| 192 | mUseOneWeight = enable; } |
---|
| 193 | |
---|
| 194 | bool useOneWeight() const { return mUseOneWeight; } |
---|
| 195 | |
---|
| 196 | /** @see InstanceBatch::useBoneWorldMatrices() */ |
---|
| 197 | virtual bool useBoneWorldMatrices() const { return !mUseBoneMatrixLookup; } |
---|
| 198 | |
---|
| 199 | /** @return the maximum amount of shared transform entities when using lookup table*/ |
---|
| 200 | virtual size_t getMaxLookupTableInstances() const { return mMaxLookupTableInstances; } |
---|
| 201 | |
---|
| 202 | }; |
---|
| 203 | |
---|
| 204 | class _OgreExport InstanceBatchVTF : public BaseInstanceBatchVTF |
---|
| 205 | { |
---|
| 206 | |
---|
| 207 | void setupVertices( const SubMesh* baseSubMesh ); |
---|
| 208 | void setupIndices( const SubMesh* baseSubMesh ); |
---|
| 209 | |
---|
| 210 | /** Creates 2 TEXCOORD semantics that will be used to sample the vertex texture */ |
---|
| 211 | void createVertexSemantics( VertexData *thisVertexData, VertexData *baseVertexData, |
---|
| 212 | const HWBoneIdxVec &hwBoneIdx, const HWBoneWgtVec &hwBoneWgt ); |
---|
| 213 | |
---|
| 214 | virtual bool matricesTogetherPerRow() const { return false; } |
---|
| 215 | public: |
---|
| 216 | InstanceBatchVTF( InstanceManager *creator, MeshPtr &meshReference, const MaterialPtr &material, |
---|
| 217 | size_t instancesPerBatch, const Mesh::IndexMap *indexToBoneMap, |
---|
| 218 | const String &batchName); |
---|
| 219 | virtual ~InstanceBatchVTF(); |
---|
| 220 | |
---|
| 221 | /** @see InstanceBatch::calculateMaxNumInstances */ |
---|
| 222 | size_t calculateMaxNumInstances( const SubMesh *baseSubMesh, uint16 flags ) const; |
---|
| 223 | }; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | #endif |
---|