Refactor: Expand tabs to 4 spaces

This commit is contained in:
Richard
2015-05-18 21:57:13 -06:00
parent a96a595a7a
commit 83de707587
324 changed files with 78951 additions and 78956 deletions

View File

@@ -62,324 +62,324 @@ using namespace Assimp;
// ------------------------------------------------------------------------------------------------
// Constructor to be privately used by Importer
ImproveCacheLocalityProcess::ImproveCacheLocalityProcess() {
configCacheDepth = PP_ICL_PTCACHE_SIZE;
configCacheDepth = PP_ICL_PTCACHE_SIZE;
}
// ------------------------------------------------------------------------------------------------
// Destructor, private as well
ImproveCacheLocalityProcess::~ImproveCacheLocalityProcess()
{
// nothing to do here
// nothing to do here
}
// ------------------------------------------------------------------------------------------------
// Returns whether the processing step is present in the given flag field.
bool ImproveCacheLocalityProcess::IsActive( unsigned int pFlags) const
{
return (pFlags & aiProcess_ImproveCacheLocality) != 0;
return (pFlags & aiProcess_ImproveCacheLocality) != 0;
}
// ------------------------------------------------------------------------------------------------
// Setup configuration
void ImproveCacheLocalityProcess::SetupProperties(const Importer* pImp)
{
// AI_CONFIG_PP_ICL_PTCACHE_SIZE controls the target cache size for the optimizer
configCacheDepth = pImp->GetPropertyInteger(AI_CONFIG_PP_ICL_PTCACHE_SIZE,PP_ICL_PTCACHE_SIZE);
// AI_CONFIG_PP_ICL_PTCACHE_SIZE controls the target cache size for the optimizer
configCacheDepth = pImp->GetPropertyInteger(AI_CONFIG_PP_ICL_PTCACHE_SIZE,PP_ICL_PTCACHE_SIZE);
}
// ------------------------------------------------------------------------------------------------
// Executes the post processing step on the given imported data.
void ImproveCacheLocalityProcess::Execute( aiScene* pScene)
{
if (!pScene->mNumMeshes) {
DefaultLogger::get()->debug("ImproveCacheLocalityProcess skipped; there are no meshes");
return;
}
if (!pScene->mNumMeshes) {
DefaultLogger::get()->debug("ImproveCacheLocalityProcess skipped; there are no meshes");
return;
}
DefaultLogger::get()->debug("ImproveCacheLocalityProcess begin");
DefaultLogger::get()->debug("ImproveCacheLocalityProcess begin");
float out = 0.f;
unsigned int numf = 0, numm = 0;
for( unsigned int a = 0; a < pScene->mNumMeshes; a++){
const float res = ProcessMesh( pScene->mMeshes[a],a);
if (res) {
numf += pScene->mMeshes[a]->mNumFaces;
out += res;
++numm;
}
}
if (!DefaultLogger::isNullLogger()) {
char szBuff[128]; // should be sufficiently large in every case
::sprintf(szBuff,"Cache relevant are %u meshes (%u faces). Average output ACMR is %f",
numm,numf,out/numf);
float out = 0.f;
unsigned int numf = 0, numm = 0;
for( unsigned int a = 0; a < pScene->mNumMeshes; a++){
const float res = ProcessMesh( pScene->mMeshes[a],a);
if (res) {
numf += pScene->mMeshes[a]->mNumFaces;
out += res;
++numm;
}
}
if (!DefaultLogger::isNullLogger()) {
char szBuff[128]; // should be sufficiently large in every case
::sprintf(szBuff,"Cache relevant are %u meshes (%u faces). Average output ACMR is %f",
numm,numf,out/numf);
DefaultLogger::get()->info(szBuff);
DefaultLogger::get()->debug("ImproveCacheLocalityProcess finished. ");
}
DefaultLogger::get()->info(szBuff);
DefaultLogger::get()->debug("ImproveCacheLocalityProcess finished. ");
}
}
// ------------------------------------------------------------------------------------------------
// Improves the cache coherency of a specific mesh
float ImproveCacheLocalityProcess::ProcessMesh( aiMesh* pMesh, unsigned int meshNum)
{
// TODO: rewrite this to use std::vector or boost::shared_array
ai_assert(NULL != pMesh);
// TODO: rewrite this to use std::vector or boost::shared_array
ai_assert(NULL != pMesh);
// Check whether the input data is valid
// - there must be vertices and faces
// - all faces must be triangulated or we can't operate on them
if (!pMesh->HasFaces() || !pMesh->HasPositions())
return 0.f;
// Check whether the input data is valid
// - there must be vertices and faces
// - all faces must be triangulated or we can't operate on them
if (!pMesh->HasFaces() || !pMesh->HasPositions())
return 0.f;
if (pMesh->mPrimitiveTypes != aiPrimitiveType_TRIANGLE) {
DefaultLogger::get()->error("This algorithm works on triangle meshes only");
return 0.f;
}
if (pMesh->mPrimitiveTypes != aiPrimitiveType_TRIANGLE) {
DefaultLogger::get()->error("This algorithm works on triangle meshes only");
return 0.f;
}
if(pMesh->mNumVertices <= configCacheDepth) {
return 0.f;
}
if(pMesh->mNumVertices <= configCacheDepth) {
return 0.f;
}
float fACMR = 3.f;
const aiFace* const pcEnd = pMesh->mFaces+pMesh->mNumFaces;
float fACMR = 3.f;
const aiFace* const pcEnd = pMesh->mFaces+pMesh->mNumFaces;
// Input ACMR is for logging purposes only
if (!DefaultLogger::isNullLogger()) {
// Input ACMR is for logging purposes only
if (!DefaultLogger::isNullLogger()) {
unsigned int* piFIFOStack = new unsigned int[configCacheDepth];
memset(piFIFOStack,0xff,configCacheDepth*sizeof(unsigned int));
unsigned int* piCur = piFIFOStack;
const unsigned int* const piCurEnd = piFIFOStack + configCacheDepth;
unsigned int* piFIFOStack = new unsigned int[configCacheDepth];
memset(piFIFOStack,0xff,configCacheDepth*sizeof(unsigned int));
unsigned int* piCur = piFIFOStack;
const unsigned int* const piCurEnd = piFIFOStack + configCacheDepth;
// count the number of cache misses
unsigned int iCacheMisses = 0;
for (const aiFace* pcFace = pMesh->mFaces;pcFace != pcEnd;++pcFace) {
// count the number of cache misses
unsigned int iCacheMisses = 0;
for (const aiFace* pcFace = pMesh->mFaces;pcFace != pcEnd;++pcFace) {
for (unsigned int qq = 0; qq < 3;++qq) {
bool bInCache = false;
for (unsigned int qq = 0; qq < 3;++qq) {
bool bInCache = false;
for (unsigned int* pp = piFIFOStack;pp < piCurEnd;++pp) {
if (*pp == pcFace->mIndices[qq]) {
// the vertex is in cache
bInCache = true;
break;
}
}
if (!bInCache) {
++iCacheMisses;
if (piCurEnd == piCur) {
piCur = piFIFOStack;
}
*piCur++ = pcFace->mIndices[qq];
}
}
}
delete[] piFIFOStack;
fACMR = (float)iCacheMisses / pMesh->mNumFaces;
if (3.0 == fACMR) {
char szBuff[128]; // should be sufficiently large in every case
for (unsigned int* pp = piFIFOStack;pp < piCurEnd;++pp) {
if (*pp == pcFace->mIndices[qq]) {
// the vertex is in cache
bInCache = true;
break;
}
}
if (!bInCache) {
++iCacheMisses;
if (piCurEnd == piCur) {
piCur = piFIFOStack;
}
*piCur++ = pcFace->mIndices[qq];
}
}
}
delete[] piFIFOStack;
fACMR = (float)iCacheMisses / pMesh->mNumFaces;
if (3.0 == fACMR) {
char szBuff[128]; // should be sufficiently large in every case
// the JoinIdenticalVertices process has not been executed on this
// mesh, otherwise this value would normally be at least minimally
// smaller than 3.0 ...
sprintf(szBuff,"Mesh %u: Not suitable for vcache optimization",meshNum);
DefaultLogger::get()->warn(szBuff);
return 0.f;
}
}
// the JoinIdenticalVertices process has not been executed on this
// mesh, otherwise this value would normally be at least minimally
// smaller than 3.0 ...
sprintf(szBuff,"Mesh %u: Not suitable for vcache optimization",meshNum);
DefaultLogger::get()->warn(szBuff);
return 0.f;
}
}
// first we need to build a vertex-triangle adjacency list
VertexTriangleAdjacency adj(pMesh->mFaces,pMesh->mNumFaces, pMesh->mNumVertices,true);
// first we need to build a vertex-triangle adjacency list
VertexTriangleAdjacency adj(pMesh->mFaces,pMesh->mNumFaces, pMesh->mNumVertices,true);
// build a list to store per-vertex caching time stamps
unsigned int* const piCachingStamps = new unsigned int[pMesh->mNumVertices];
memset(piCachingStamps,0x0,pMesh->mNumVertices*sizeof(unsigned int));
// build a list to store per-vertex caching time stamps
unsigned int* const piCachingStamps = new unsigned int[pMesh->mNumVertices];
memset(piCachingStamps,0x0,pMesh->mNumVertices*sizeof(unsigned int));
// allocate an empty output index buffer. We store the output indices in one large array.
// Since the number of triangles won't change the input faces can be reused. This is how
// we save thousands of redundant mini allocations for aiFace::mIndices
const unsigned int iIdxCnt = pMesh->mNumFaces*3;
unsigned int* const piIBOutput = new unsigned int[iIdxCnt];
unsigned int* piCSIter = piIBOutput;
// allocate an empty output index buffer. We store the output indices in one large array.
// Since the number of triangles won't change the input faces can be reused. This is how
// we save thousands of redundant mini allocations for aiFace::mIndices
const unsigned int iIdxCnt = pMesh->mNumFaces*3;
unsigned int* const piIBOutput = new unsigned int[iIdxCnt];
unsigned int* piCSIter = piIBOutput;
// allocate the flag array to hold the information
// whether a face has already been emitted or not
std::vector<bool> abEmitted(pMesh->mNumFaces,false);
// allocate the flag array to hold the information
// whether a face has already been emitted or not
std::vector<bool> abEmitted(pMesh->mNumFaces,false);
// dead-end vertex index stack
std::stack<unsigned int, std::vector<unsigned int> > sDeadEndVStack;
// dead-end vertex index stack
std::stack<unsigned int, std::vector<unsigned int> > sDeadEndVStack;
// create a copy of the piNumTriPtr buffer
unsigned int* const piNumTriPtr = adj.mLiveTriangles;
const std::vector<unsigned int> piNumTriPtrNoModify(piNumTriPtr, piNumTriPtr + pMesh->mNumVertices);
// create a copy of the piNumTriPtr buffer
unsigned int* const piNumTriPtr = adj.mLiveTriangles;
const std::vector<unsigned int> piNumTriPtrNoModify(piNumTriPtr, piNumTriPtr + pMesh->mNumVertices);
// get the largest number of referenced triangles and allocate the "candidate buffer"
unsigned int iMaxRefTris = 0; {
const unsigned int* piCur = adj.mLiveTriangles;
const unsigned int* const piCurEnd = adj.mLiveTriangles+pMesh->mNumVertices;
for (;piCur != piCurEnd;++piCur) {
iMaxRefTris = std::max(iMaxRefTris,*piCur);
}
}
unsigned int* piCandidates = new unsigned int[iMaxRefTris*3];
unsigned int iCacheMisses = 0;
// get the largest number of referenced triangles and allocate the "candidate buffer"
unsigned int iMaxRefTris = 0; {
const unsigned int* piCur = adj.mLiveTriangles;
const unsigned int* const piCurEnd = adj.mLiveTriangles+pMesh->mNumVertices;
for (;piCur != piCurEnd;++piCur) {
iMaxRefTris = std::max(iMaxRefTris,*piCur);
}
}
unsigned int* piCandidates = new unsigned int[iMaxRefTris*3];
unsigned int iCacheMisses = 0;
// ...................................................................................
/** PSEUDOCODE for the algorithm
// ...................................................................................
/** PSEUDOCODE for the algorithm
A = Build-Adjacency(I) Vertex-triangle adjacency
L = Get-Triangle-Counts(A) Per-vertex live triangle counts
C = Zero(Vertex-Count(I)) Per-vertex caching time stamps
D = Empty-Stack() Dead-end vertex stack
E = False(Triangle-Count(I)) Per triangle emitted flag
O = Empty-Index-Buffer() Empty output buffer
f = 0 Arbitrary starting vertex
s = k+1, i = 1 Time stamp and cursor
while f >= 0 For all valid fanning vertices
N = Empty-Set() 1-ring of next candidates
for each Triangle t in Neighbors(A, f)
if !Emitted(E,t)
for each Vertex v in t
Append(O,v) Output vertex
Push(D,v) Add to dead-end stack
Insert(N,v) Register as candidate
L[v] = L[v]-1 Decrease live triangle count
if s-C[v] > k If not in cache
C[v] = s Set time stamp
s = s+1 Increment time stamp
E[t] = true Flag triangle as emitted
Select next fanning vertex
f = Get-Next-Vertex(I,i,k,N,C,s,L,D)
return O
*/
// ...................................................................................
A = Build-Adjacency(I) Vertex-triangle adjacency
L = Get-Triangle-Counts(A) Per-vertex live triangle counts
C = Zero(Vertex-Count(I)) Per-vertex caching time stamps
D = Empty-Stack() Dead-end vertex stack
E = False(Triangle-Count(I)) Per triangle emitted flag
O = Empty-Index-Buffer() Empty output buffer
f = 0 Arbitrary starting vertex
s = k+1, i = 1 Time stamp and cursor
while f >= 0 For all valid fanning vertices
N = Empty-Set() 1-ring of next candidates
for each Triangle t in Neighbors(A, f)
if !Emitted(E,t)
for each Vertex v in t
Append(O,v) Output vertex
Push(D,v) Add to dead-end stack
Insert(N,v) Register as candidate
L[v] = L[v]-1 Decrease live triangle count
if s-C[v] > k If not in cache
C[v] = s Set time stamp
s = s+1 Increment time stamp
E[t] = true Flag triangle as emitted
Select next fanning vertex
f = Get-Next-Vertex(I,i,k,N,C,s,L,D)
return O
*/
// ...................................................................................
int ivdx = 0;
int ics = 1;
int iStampCnt = configCacheDepth+1;
while (ivdx >= 0) {
int ivdx = 0;
int ics = 1;
int iStampCnt = configCacheDepth+1;
while (ivdx >= 0) {
unsigned int icnt = piNumTriPtrNoModify[ivdx];
unsigned int* piList = adj.GetAdjacentTriangles(ivdx);
unsigned int* piCurCandidate = piCandidates;
unsigned int icnt = piNumTriPtrNoModify[ivdx];
unsigned int* piList = adj.GetAdjacentTriangles(ivdx);
unsigned int* piCurCandidate = piCandidates;
// get all triangles in the neighborhood
for (unsigned int tri = 0; tri < icnt;++tri) {
// get all triangles in the neighborhood
for (unsigned int tri = 0; tri < icnt;++tri) {
// if they have not yet been emitted, add them to the output IB
const unsigned int fidx = *piList++;
if (!abEmitted[fidx]) {
// if they have not yet been emitted, add them to the output IB
const unsigned int fidx = *piList++;
if (!abEmitted[fidx]) {
// so iterate through all vertices of the current triangle
const aiFace* pcFace = &pMesh->mFaces[ fidx ];
for (unsigned int* p = pcFace->mIndices, *p2 = pcFace->mIndices+3;p != p2;++p) {
const unsigned int dp = *p;
// so iterate through all vertices of the current triangle
const aiFace* pcFace = &pMesh->mFaces[ fidx ];
for (unsigned int* p = pcFace->mIndices, *p2 = pcFace->mIndices+3;p != p2;++p) {
const unsigned int dp = *p;
// the current vertex won't have any free triangles after this step
if (ivdx != (int)dp) {
// append the vertex to the dead-end stack
sDeadEndVStack.push(dp);
// the current vertex won't have any free triangles after this step
if (ivdx != (int)dp) {
// append the vertex to the dead-end stack
sDeadEndVStack.push(dp);
// register as candidate for the next step
*piCurCandidate++ = dp;
// register as candidate for the next step
*piCurCandidate++ = dp;
// decrease the per-vertex triangle counts
piNumTriPtr[dp]--;
}
// decrease the per-vertex triangle counts
piNumTriPtr[dp]--;
}
// append the vertex to the output index buffer
*piCSIter++ = dp;
// append the vertex to the output index buffer
*piCSIter++ = dp;
// if the vertex is not yet in cache, set its cache count
if (iStampCnt-piCachingStamps[dp] > configCacheDepth) {
piCachingStamps[dp] = iStampCnt++;
++iCacheMisses;
}
}
// flag triangle as emitted
abEmitted[fidx] = true;
}
}
// if the vertex is not yet in cache, set its cache count
if (iStampCnt-piCachingStamps[dp] > configCacheDepth) {
piCachingStamps[dp] = iStampCnt++;
++iCacheMisses;
}
}
// flag triangle as emitted
abEmitted[fidx] = true;
}
}
// the vertex has now no living adjacent triangles anymore
piNumTriPtr[ivdx] = 0;
// the vertex has now no living adjacent triangles anymore
piNumTriPtr[ivdx] = 0;
// get next fanning vertex
ivdx = -1;
int max_priority = -1;
for (unsigned int* piCur = piCandidates;piCur != piCurCandidate;++piCur) {
const unsigned int dp = *piCur;
// get next fanning vertex
ivdx = -1;
int max_priority = -1;
for (unsigned int* piCur = piCandidates;piCur != piCurCandidate;++piCur) {
const unsigned int dp = *piCur;
// must have live triangles
if (piNumTriPtr[dp] > 0) {
int priority = 0;
// must have live triangles
if (piNumTriPtr[dp] > 0) {
int priority = 0;
// will the vertex be in cache, even after fanning occurs?
unsigned int tmp;
if ((tmp = iStampCnt-piCachingStamps[dp]) + 2*piNumTriPtr[dp] <= configCacheDepth) {
priority = tmp;
}
// will the vertex be in cache, even after fanning occurs?
unsigned int tmp;
if ((tmp = iStampCnt-piCachingStamps[dp]) + 2*piNumTriPtr[dp] <= configCacheDepth) {
priority = tmp;
}
// keep best candidate
if (priority > max_priority) {
max_priority = priority;
ivdx = dp;
}
}
}
// did we reach a dead end?
if (-1 == ivdx) {
// need to get a non-local vertex for which we have a good chance that it is still
// in the cache ...
while (!sDeadEndVStack.empty()) {
unsigned int iCachedIdx = sDeadEndVStack.top();
sDeadEndVStack.pop();
if (piNumTriPtr[ iCachedIdx ] > 0) {
ivdx = iCachedIdx;
break;
}
}
// keep best candidate
if (priority > max_priority) {
max_priority = priority;
ivdx = dp;
}
}
}
// did we reach a dead end?
if (-1 == ivdx) {
// need to get a non-local vertex for which we have a good chance that it is still
// in the cache ...
while (!sDeadEndVStack.empty()) {
unsigned int iCachedIdx = sDeadEndVStack.top();
sDeadEndVStack.pop();
if (piNumTriPtr[ iCachedIdx ] > 0) {
ivdx = iCachedIdx;
break;
}
}
if (-1 == ivdx) {
// well, there isn't such a vertex. Simply get the next vertex in input order and
// hope it is not too bad ...
while (ics < (int)pMesh->mNumVertices) {
++ics;
if (piNumTriPtr[ics] > 0) {
ivdx = ics;
break;
}
}
}
}
}
float fACMR2 = 0.0f;
if (!DefaultLogger::isNullLogger()) {
fACMR2 = (float)iCacheMisses / pMesh->mNumFaces;
if (-1 == ivdx) {
// well, there isn't such a vertex. Simply get the next vertex in input order and
// hope it is not too bad ...
while (ics < (int)pMesh->mNumVertices) {
++ics;
if (piNumTriPtr[ics] > 0) {
ivdx = ics;
break;
}
}
}
}
}
float fACMR2 = 0.0f;
if (!DefaultLogger::isNullLogger()) {
fACMR2 = (float)iCacheMisses / pMesh->mNumFaces;
// very intense verbose logging ... prepare for much text if there are many meshes
if ( DefaultLogger::get()->getLogSeverity() == Logger::VERBOSE) {
char szBuff[128]; // should be sufficiently large in every case
// very intense verbose logging ... prepare for much text if there are many meshes
if ( DefaultLogger::get()->getLogSeverity() == Logger::VERBOSE) {
char szBuff[128]; // should be sufficiently large in every case
::sprintf(szBuff,"Mesh %u | ACMR in: %f out: %f | ~%.1f%%",meshNum,fACMR,fACMR2,
((fACMR - fACMR2) / fACMR) * 100.f);
DefaultLogger::get()->debug(szBuff);
}
::sprintf(szBuff,"Mesh %u | ACMR in: %f out: %f | ~%.1f%%",meshNum,fACMR,fACMR2,
((fACMR - fACMR2) / fACMR) * 100.f);
DefaultLogger::get()->debug(szBuff);
}
fACMR2 *= pMesh->mNumFaces;
}
// sort the output index buffer back to the input array
piCSIter = piIBOutput;
for (aiFace* pcFace = pMesh->mFaces; pcFace != pcEnd;++pcFace) {
pcFace->mIndices[0] = *piCSIter++;
pcFace->mIndices[1] = *piCSIter++;
pcFace->mIndices[2] = *piCSIter++;
}
fACMR2 *= pMesh->mNumFaces;
}
// sort the output index buffer back to the input array
piCSIter = piIBOutput;
for (aiFace* pcFace = pMesh->mFaces; pcFace != pcEnd;++pcFace) {
pcFace->mIndices[0] = *piCSIter++;
pcFace->mIndices[1] = *piCSIter++;
pcFace->mIndices[2] = *piCSIter++;
}
// delete temporary storage
delete[] piCachingStamps;
delete[] piIBOutput;
delete[] piCandidates;
// delete temporary storage
delete[] piCachingStamps;
delete[] piIBOutput;
delete[] piCandidates;
return fACMR2;
return fACMR2;
}