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 __InstanceBatchShader_H__ |
---|
29 | #define __InstanceBatchShader_H__ |
---|
30 | |
---|
31 | #include "OgreInstanceBatch.h" |
---|
32 | |
---|
33 | namespace Ogre |
---|
34 | { |
---|
35 | /** \addtogroup Core |
---|
36 | * @{ |
---|
37 | */ |
---|
38 | /** \addtogroup Scene |
---|
39 | * @{ |
---|
40 | */ |
---|
41 | |
---|
42 | /** This is the same technique the old "InstancedGeometry" implementation used (with improvements). |
---|
43 | Basically it creates a large vertex buffer with many repeating entities, and sends per instance |
---|
44 | data through shader constants. Because SM 2.0 & 3.0 have up to 256 shader constant registers, |
---|
45 | this means there can be approx up to 84 instances per batch, assuming they're not skinned |
---|
46 | But using shader constants for other stuff (i.e. lighting) also affects negatively this number |
---|
47 | A mesh with skeletally animated 2 bones reduces the number 84 to 42 instances per batch. |
---|
48 | @par |
---|
49 | The main advantage of this technique is that it's supported on a high variety of hardware |
---|
50 | (SM 2.0 cards are required) and the same shader can be used for both skeletally animated |
---|
51 | normal entities and instanced entities without a single change required. |
---|
52 | @par |
---|
53 | Unlike the old InstancedGeometry implementation, the developer doesn't need to worry about |
---|
54 | reaching the 84 instances limit, the InstanceManager automatically takes care of splitting |
---|
55 | and creating new batches. But beware internally, this means less performance improvement. |
---|
56 | Another improvement is that vertex buffers are shared between batches, which significantly |
---|
57 | reduces GPU VRAM usage. |
---|
58 | |
---|
59 | @remarks |
---|
60 | Design discussion webpage: http://www.ogre3d.org/forums/viewtopic.php?f=4&t=59902 |
---|
61 | @author |
---|
62 | Matias N. Goldberg ("dark_sylinc") |
---|
63 | @version |
---|
64 | 1.0 |
---|
65 | */ |
---|
66 | class _OgreExport InstanceBatchShader : public InstanceBatch |
---|
67 | { |
---|
68 | unsigned short mNumWorldMatrices; |
---|
69 | |
---|
70 | void setupVertices( const SubMesh* baseSubMesh ); |
---|
71 | void setupIndices( const SubMesh* baseSubMesh ); |
---|
72 | |
---|
73 | /** When the mesh is (hardware) skinned, a different code path is called so that |
---|
74 | we reuse the index buffers and modify them in place. For example Instance #2 |
---|
75 | with reference to bone #5 would have BlendIndex = 2 + 5 = 7 |
---|
76 | Everything is copied identically except the VES_BLEND_INDICES semantic |
---|
77 | */ |
---|
78 | void setupHardwareSkinned( const SubMesh* baseSubMesh, VertexData *thisVertexData, |
---|
79 | VertexData *baseVertexData ); |
---|
80 | |
---|
81 | public: |
---|
82 | InstanceBatchShader( InstanceManager *creator, MeshPtr &meshReference, const MaterialPtr &material, |
---|
83 | size_t instancesPerBatch, const Mesh::IndexMap *indexToBoneMap, |
---|
84 | const String &batchName ); |
---|
85 | virtual ~InstanceBatchShader(); |
---|
86 | |
---|
87 | /** @see InstanceBatch::calculateMaxNumInstances */ |
---|
88 | size_t calculateMaxNumInstances( const SubMesh *baseSubMesh, uint16 flags ) const; |
---|
89 | |
---|
90 | /** @see InstanceBatch::buildFrom */ |
---|
91 | void buildFrom( const SubMesh *baseSubMesh, const RenderOperation &renderOperation ); |
---|
92 | |
---|
93 | //Renderable overloads |
---|
94 | void getWorldTransforms( Matrix4* xform ) const; |
---|
95 | unsigned short getNumWorldTransforms(void) const; |
---|
96 | }; |
---|
97 | } |
---|
98 | |
---|
99 | #endif |
---|