Hello,
mainly as a learning experience, but also to aid my further developement, I want to enchance my particle system using SIMD, as featured in this article: http://software.intel.com/en-us/articles/creating-a-particle-system-with-streaming-simd-extensions.
Now my main problem is that, unlike them, I want my particles to being sorted. This is a problem, since SIMD seems to rely on the data being accessed in a linear manner (from i to i+3), which isn't working all that well with my current approach. This is how depth updating looks currently:
const D3DXVECTOR3& vPos(camera.GetPosition());
for(DepthVector::iterator ii = m_vDepth.begin(); ii != m_vDepth.end(); ++ii)
{
//get index of currect particle to keep temporal coherency
const unsigned int index = ii->index;
ii->depth = abs(m_particles.x[index] - vPos.x);
ii->depth += abs(m_particles.y[index] - vPos.y);
ii->depth += abs(m_particles.z[index] - vPos.z);
}
std::sort(m_vDepth.begin(), m_vDepth.end(), ParticleSorter());
As you can see, I perform an index sort, later on I'll use that index to identify the particle. Note that since I'm using an SoA I can't really sort that itself... can I? Anyway, now this is the code I've written so far in SIMD:
void ParticleEmitter::SortParticles(const D3DXVECTOR3& vCameraPos)
{
//load camera position and create depth register
__m128 cameraPosX = _mm_set1_ps(vCameraPos.x), cameraPosY = _mm_set1_ps(vCameraPos.y), cameraPosZ = _mm_set1_ps(vCameraPos.z), depthRegister;
//register for abs()-ing
__m128 dummy = _mm_set1_ps(1.0f);
__declspec(align(16)) float depth[4];
// number of 4 elements we can process
const unsigned int remainingParticles = m_vDepth.size() % 4;
//get vector iterator
DepthVector::iterator ii = m_vDepth.begin();
//
for(unsigned int i = 0; i < m_cParticles - remainingParticles; i+=4)
{
//set register to X-component of camera distance -> abs( ParticleX - CameraX )
depthRegister = _mm_andnot_ps(dummy, _mm_sub_ps( _mm_load_ps(&m_particles.x), cameraPosX) );
//add Y-component -> abs( ParticleY - CameraY)
_mm_add_ps(depthRegister, _mm_andnot_ps(dummy, _mm_sub_ps( _mm_load_ps(&m_particles.y), cameraPosY) ) );
//add Z-component -> abs( ParticleZ - CameraZ)
_mm_add_ps(depthRegister, _mm_andnot_ps(dummy, _mm_sub_ps( _mm_load_ps(&m_particles.z), cameraPosZ) ) );
//load depth
_mm_store_ps(depth, depthRegister);
//set depth
ii->depth = depth[0];
ii++;
ii->depth = depth[1];
ii++;
ii->depth = depth[2];
ii++;
ii->depth = depth[3];
ii++;
}
std::sort(m_vDepth.begin(), m_vDepth.end(), ParticleSorter());
}
Now there are two problems with the code:
- Number one, I don't see how to keep temporal coherence, as I did before. This is very bad, as re-sorting the previous-frame sorted depth list is almost twice as fast as the unordered one. Is there any way to perform index sorting, or some other way of sorting particles that is compatible with SIMD?
- Number two, this code is only running half of the time. Sometimes it crashes on the first "ii->depth = depth" - line, stating something about a read access violation at a non-null memory adress. It always crashes at another "i", so there really is no pattern, looking at the debugger doesn't tell me anything, since all values seem to be okay, and the adress the error message tells isn't anywhere I can look at in the MSVC debugger. Since I'm new to SIMD, maybe someone can see a mistake in the code I cannot see? Nevermind, shouldn't have done depth, depth[i+1] ... but depth[0], depth[1]... fixed the crash, but the first problem still resides - the SIMD code performs far worst er (0.0055 ms instead of 0.003 ms), and profiling tells me that all the performance I gain from quatering the work needed for depth calculation is more than lost lost because the depth vector needs to be sorted frame after frame again from scratch. Any ideas?
In case you wonder, this is how I declare the regarding structs:
struct Particles
{
#define BOUNDARY_ALIGNMENT 16
Particles(unsigned int count)
{
/*x = new float[count];
y = new float[count];
z = new float[count];*/
x = (float*)_mm_malloc(count * sizeof(float), BOUNDARY_ALIGNMENT);
y = (float*)_mm_malloc(count * sizeof(float), BOUNDARY_ALIGNMENT);
z = (float*)_mm_malloc(count * sizeof(float), BOUNDARY_ALIGNMENT);
}
~Particles(void)
{
_mm_free(x);
_mm_free(y);
_mm_free(z);
/*delete[] x;
delete[] y;
delete[] z;*/
}
float* x, *y, *z;
};
struct ParticleDepth
{
int index;
float depth;
};
Thanks in advance!