[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 __OptimisedUtil_H__ |
---|
| 29 | #define __OptimisedUtil_H__ |
---|
| 30 | |
---|
| 31 | #include "OgrePrerequisites.h" |
---|
| 32 | #include "OgreEdgeListBuilder.h" |
---|
| 33 | #include <cstddef> |
---|
| 34 | |
---|
| 35 | namespace Ogre { |
---|
| 36 | |
---|
| 37 | /** \addtogroup Core |
---|
| 38 | * @{ |
---|
| 39 | */ |
---|
| 40 | /** \addtogroup Math |
---|
| 41 | * @{ |
---|
| 42 | */ |
---|
| 43 | /** Utility class for provides optimised functions. |
---|
| 44 | @note |
---|
| 45 | This class are supposed used by internal engine only. |
---|
| 46 | */ |
---|
| 47 | class _OgreExport OptimisedUtil |
---|
| 48 | { |
---|
| 49 | private: |
---|
| 50 | /// Privated copy constructor, to prevent misuse |
---|
| 51 | OptimisedUtil(const OptimisedUtil& rhs); /* do nothing, should not use */ |
---|
| 52 | /// Privated operator=, to prevent misuse |
---|
| 53 | OptimisedUtil& operator=(const OptimisedUtil& rhs); /* do not use */ |
---|
| 54 | |
---|
| 55 | protected: |
---|
| 56 | /// Store a pointer to the implementation |
---|
| 57 | static OptimisedUtil* msImplementation; |
---|
| 58 | |
---|
| 59 | /// Detect best implementation based on run-time environment |
---|
| 60 | static OptimisedUtil* _detectImplementation(void); |
---|
| 61 | |
---|
| 62 | public: |
---|
| 63 | // Default constructor |
---|
| 64 | OptimisedUtil(void) {} |
---|
| 65 | // Destructor |
---|
| 66 | virtual ~OptimisedUtil() {} |
---|
| 67 | |
---|
| 68 | /** Gets the implementation of this class. |
---|
| 69 | @note |
---|
| 70 | Don't cache the pointer returned by this function, it'll change due |
---|
| 71 | run-time environment detection to pick up the best implementation. |
---|
| 72 | */ |
---|
| 73 | static OptimisedUtil* getImplementation(void) { return msImplementation; } |
---|
| 74 | |
---|
| 75 | /** Performs software vertex skinning. |
---|
| 76 | @param srcPosPtr Pointer to source position buffer. |
---|
| 77 | @param destPosPtr Pointer to destination position buffer. |
---|
| 78 | @param srcNormPtr Pointer to source normal buffer, if NULL, |
---|
| 79 | means blend position only. |
---|
| 80 | @param destNormPtr Pointer to destination normal buffer, it's |
---|
| 81 | ignored if srcNormPtr is NULL. |
---|
| 82 | @param blendWeightPtr Pointer to blend weight buffer. |
---|
| 83 | @param blendIndexPtr Pointer to blend index buffer. |
---|
| 84 | @param blendMatrices An array of pointer of blend matrix, the matrix |
---|
| 85 | must be aligned to SIMD alignment, but not necessary for the array |
---|
| 86 | itself. |
---|
| 87 | @param srcPosStride The stride of source position in bytes. |
---|
| 88 | @param destPosStride The stride of destination position in bytes. |
---|
| 89 | @param srcNormStride The stride of source normal in bytes, |
---|
| 90 | it's ignored if srcNormPtr is NULL. |
---|
| 91 | @param destNormStride The stride of destination normal in bytes, |
---|
| 92 | it's ignored if srcNormPtr is NULL. |
---|
| 93 | @param blendWeightStride The stride of blend weight buffer in bytes. |
---|
| 94 | @param blendIndexStride The stride of blend index buffer in bytes. |
---|
| 95 | @param numWeightsPerVertex Number of blend weights per-vertex, as well |
---|
| 96 | as for blend indices. |
---|
| 97 | @param numVertices Number of vertices to blend. |
---|
| 98 | */ |
---|
| 99 | virtual void softwareVertexSkinning( |
---|
| 100 | const float *srcPosPtr, float *destPosPtr, |
---|
| 101 | const float *srcNormPtr, float *destNormPtr, |
---|
| 102 | const float *blendWeightPtr, const unsigned char* blendIndexPtr, |
---|
| 103 | const Matrix4* const* blendMatrices, |
---|
| 104 | size_t srcPosStride, size_t destPosStride, |
---|
| 105 | size_t srcNormStride, size_t destNormStride, |
---|
| 106 | size_t blendWeightStride, size_t blendIndexStride, |
---|
| 107 | size_t numWeightsPerVertex, |
---|
| 108 | size_t numVertices) = 0; |
---|
| 109 | |
---|
| 110 | /** Performs a software vertex morph, of the kind used for |
---|
| 111 | morph animation although it can be used for other purposes. |
---|
| 112 | @remarks |
---|
| 113 | This function will linearly interpolate positions between two |
---|
| 114 | source buffers, into a third buffer. |
---|
| 115 | @param t Parametric distance between the start and end positions |
---|
| 116 | @param srcPos1 Pointer to buffer for the start positions |
---|
| 117 | @param srcPos2 Pointer to buffer for the end positions |
---|
| 118 | @param dstPos Pointer to buffer for the destination positions |
---|
| 119 | @param pos1VSize, pos2VSize, dstVSize Vertex sizes in bytes of each of the 3 buffers referenced |
---|
| 120 | @param numVertices Number of vertices to morph, which agree with |
---|
| 121 | the number in start, end and destination buffer. Bear in mind |
---|
| 122 | three floating-point values per vertex |
---|
| 123 | */ |
---|
| 124 | virtual void softwareVertexMorph( |
---|
| 125 | Real t, |
---|
| 126 | const float *srcPos1, const float *srcPos2, |
---|
| 127 | float *dstPos, |
---|
| 128 | size_t pos1VSize, size_t pos2VSize, size_t dstVSize, |
---|
| 129 | size_t numVertices, |
---|
| 130 | bool morphNormals) = 0; |
---|
| 131 | |
---|
| 132 | /** Concatenate an affine matrix to an array of affine matrices. |
---|
| 133 | @note |
---|
| 134 | An affine matrix is a 4x4 matrix with row 3 equal to (0, 0, 0, 1), |
---|
| 135 | e.g. no projective coefficients. |
---|
| 136 | @param baseMatrix The matrix used as first operand. |
---|
| 137 | @param srcMatrices An array of matrix used as second operand. |
---|
| 138 | @param dstMatrices An array of matrix to store matrix concatenate results. |
---|
| 139 | @param numMatrices Number of matrices in the array. |
---|
| 140 | */ |
---|
| 141 | virtual void concatenateAffineMatrices( |
---|
| 142 | const Matrix4& baseMatrix, |
---|
| 143 | const Matrix4* srcMatrices, |
---|
| 144 | Matrix4* dstMatrices, |
---|
| 145 | size_t numMatrices) = 0; |
---|
| 146 | |
---|
| 147 | /** Calculate the face normals for the triangles based on position |
---|
| 148 | information. |
---|
| 149 | @param positions Pointer to position information, which packed in |
---|
| 150 | (x, y, z) format, indexing by vertex index in the triangle. No |
---|
| 151 | alignment requests. |
---|
| 152 | @param triangles The triangles need to calculate face normal, the vertex |
---|
| 153 | positions is indexed by vertex index to position information. |
---|
| 154 | @param faceNormals The array of Vector4 used to store triangles face normal, |
---|
| 155 | Must be aligned to SIMD alignment. |
---|
| 156 | @param numTriangles Number of triangles to calculate face normal. |
---|
| 157 | */ |
---|
| 158 | virtual void calculateFaceNormals( |
---|
| 159 | const float *positions, |
---|
| 160 | const EdgeData::Triangle *triangles, |
---|
| 161 | Vector4 *faceNormals, |
---|
| 162 | size_t numTriangles) = 0; |
---|
| 163 | |
---|
| 164 | /** Calculate the light facing state of the triangle's face normals |
---|
| 165 | @remarks |
---|
| 166 | This is normally the first stage of calculating a silhouette, i.e. |
---|
| 167 | establishing which tris are facing the light and which are facing |
---|
| 168 | away. |
---|
| 169 | @param lightPos 4D position of the light in object space, note that |
---|
| 170 | for directional lights (which have no position), the w component |
---|
| 171 | is 0 and the x/y/z position are the direction. |
---|
| 172 | @param faceNormals An array of face normals for the triangles, the face |
---|
| 173 | normal are unit vector orthogonal to the triangles, plus distance |
---|
| 174 | from origin. This array must be aligned to SIMD alignment. |
---|
| 175 | @param lightFacings An array of flags for store light facing state |
---|
| 176 | results, the result flag is true if corresponding face normal facing |
---|
| 177 | the light, false otherwise. This array no alignment requires. |
---|
| 178 | @param numFaces Number of face normals to calculate. |
---|
| 179 | */ |
---|
| 180 | virtual void calculateLightFacing( |
---|
| 181 | const Vector4& lightPos, |
---|
| 182 | const Vector4* faceNormals, |
---|
| 183 | char* lightFacings, |
---|
| 184 | size_t numFaces) = 0; |
---|
| 185 | |
---|
| 186 | /** Extruding vertices by a fixed distance based on light position. |
---|
| 187 | @param lightPos 4D light position, when w=0.0f this represents a |
---|
| 188 | directional light, otherwise, w must be equal to 1.0f, which |
---|
| 189 | represents a point light. |
---|
| 190 | @param extrudeDist The distance to extrude. |
---|
| 191 | @param srcPositions Pointer to source vertex's position buffer, which |
---|
| 192 | the position is a 3D vector packed in xyz format. No SIMD alignment |
---|
| 193 | requirement but loss performance for unaligned data. |
---|
| 194 | @param destPositions Pointer to destination vertex's position buffer, |
---|
| 195 | which the position is a 3D vector packed in xyz format. No SIMD |
---|
| 196 | alignment requirement but loss performance for unaligned data. |
---|
| 197 | @param numVertices Number of vertices need to extruding, which agree |
---|
| 198 | with source and destination buffers. |
---|
| 199 | */ |
---|
| 200 | virtual void extrudeVertices( |
---|
| 201 | const Vector4& lightPos, |
---|
| 202 | Real extrudeDist, |
---|
| 203 | const float* srcPositions, |
---|
| 204 | float* destPositions, |
---|
| 205 | size_t numVertices) = 0; |
---|
| 206 | }; |
---|
| 207 | |
---|
| 208 | /** Returns raw offseted of the given pointer. |
---|
| 209 | @note |
---|
| 210 | The offset are in bytes, no matter what type of the pointer. |
---|
| 211 | */ |
---|
| 212 | template <class T> |
---|
| 213 | static FORCEINLINE const T* rawOffsetPointer(const T* ptr, ptrdiff_t offset) |
---|
| 214 | { |
---|
| 215 | return (const T*)((const char*)(ptr) + offset); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | template <class T> |
---|
| 219 | static FORCEINLINE T* rawOffsetPointer(T* ptr, ptrdiff_t offset) |
---|
| 220 | { |
---|
| 221 | return (T*)((char*)(ptr) + offset); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | /** Advance the pointer with raw offset. |
---|
| 225 | @note |
---|
| 226 | The offset are in bytes, no matter what type of the pointer. |
---|
| 227 | */ |
---|
| 228 | template <class T> |
---|
| 229 | static FORCEINLINE void advanceRawPointer(const T*& ptr, ptrdiff_t offset) |
---|
| 230 | { |
---|
| 231 | ptr = rawOffsetPointer(ptr, offset); |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | template <class T> |
---|
| 235 | static FORCEINLINE void advanceRawPointer(T*& ptr, ptrdiff_t offset) |
---|
| 236 | { |
---|
| 237 | ptr = rawOffsetPointer(ptr, offset); |
---|
| 238 | } |
---|
| 239 | /** @} */ |
---|
| 240 | /** @} */ |
---|
| 241 | |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | #endif // __OptimisedUtil_H__ |
---|