public CreateVertexArray AllocateCreateVertexArray(int size) { Debug.Assert(size > 0); //Console.WriteLine("AllocateCreateVertexArray."); // if the desired array is of a size not managed... return a new list which will never be // managed and later automatically garbage collected if (size > CreateVertexArraySize) { Console.WriteLine("exception case a: {0}", size); return(new CreateVertexArray(size, size)); } if (createVertexArrays.Count > 0) { CreateVertexArray array = createVertexArrays[createVertexArrays.Count - 1]; createVertexArrays.RemoveAt(createVertexArrays.Count - 1); array.OccupiedSize = size; return(array); } else { Console.WriteLine("exception case b: {0}", size); return(new CreateVertexArray(CreateVertexArraySize, size)); } }
public void FreeCreateVertexArray(CreateVertexArray array) { if (array.Array.Length > CreateVertexArraySize) { return; } if (createVertexArrays.Count < CreateVertexArrayMaxPoolSize) { createVertexArrays.Add(array); } }
private void CreateParticles( double lastFrameTime, double currentFrameTime, double dt ) { Game.Instance.Profiler.BeginSection("create_particles"); PIXHelper.BeginEvent("Create particles"); Game.Instance.Profiler.BeginSection("count"); Vector2 positionHalfPixel = new Vector2(1.0f / (2.0f * positionTextures[0].Width), 1.0f / (2.0f * positionTextures[0].Height)); Vector2 positionPixel = new Vector2(1.0f / (positionTextures[0].Width), 1.0f / (positionTextures[0].Height)); int[] particleCount = new int[emitters.Count]; int sumParticleCount = 0; for (int emitterIndex = 0; emitterIndex < emitters.Count; ++emitterIndex) { ParticleEmitter emitter = emitters[emitterIndex]; particleCount[emitterIndex] = emitter.CalculateParticleCount(lastFrameTime, currentFrameTime); sumParticleCount += particleCount[emitterIndex]; } Game.Instance.Profiler.EndSection("count"); // nothing to do if there are no new particles! if (sumParticleCount == 0) { PIXHelper.EndEvent(); Game.Instance.Profiler.EndSection("create_particles"); return; } Game.Instance.Profiler.BeginSection("allocate"); CreateVertexArray vertices = renderer.StatefulParticleResourceManager.AllocateCreateVertexArray(sumParticleCount * 3); Game.Instance.Profiler.EndSection("allocate"); Game.Instance.Profiler.BeginSection("create"); int maxindex = positionTextures[0].Width * positionTextures[0].Height; for (int i = 0; i < sumParticleCount; ++i) { if (nextParticleIndex >= maxindex) { nextParticleIndex = 0; } int textureSize = textureSizes[(int)GetSystemSize()]; int x = nextParticleIndex % textureSize; int y = nextParticleIndex / textureSize; Debug.Assert(textureSize == positionTextures[0].Width); Debug.Assert(textureSize == positionTextures[0].Height); Vector2[] offsets = { new Vector2(-1, -1), new Vector2(1, -1), new Vector2(0, 1) }; for (int j = 0; j < 3; ++j) { vertices.Array[i * 3 + j] = new CreateVertex( Vector3.Zero, Vector3.Zero, new Vector2( -1.0f + positionPixel.X + 2.0f * x * positionPixel.X + 2 * offsets[j].X * positionPixel.X, -1.0f + positionPixel.Y + 2.0f * y * positionPixel.Y + 2 * offsets[j].Y * positionPixel.Y ), new Short2( x, y ), 0 // emitterIndex ); } ++nextParticleIndex; } Game.Instance.Profiler.EndSection("create"); Game.Instance.Profiler.BeginSection("emit"); int vertexIndex = 0; for (int emitterIndex = 0; emitterIndex < emitters.Count; ++emitterIndex) { ParticleEmitter emitter = emitters[emitterIndex]; emitter.CreateParticles(lastFrameTime, currentFrameTime, vertices.Array, vertexIndex, particleCount[emitterIndex]); vertexIndex += particleCount[emitterIndex]; } Game.Instance.Profiler.EndSection("emit"); Game.Instance.Profiler.BeginSection("render"); int localCreateVerticesIndex = 0; int verticesCopied = 0; while (verticesCopied < vertices.OccupiedSize) { int passVerticesCount = vertices.OccupiedSize - verticesCopied; int passVerticesAvailable = createVertexBufferSize - localCreateVerticesIndex; if (passVerticesCount > passVerticesAvailable) { passVerticesCount = passVerticesAvailable; } for (int j = 0; j < passVerticesCount; ++j) { localCreateVertices[localCreateVerticesIndex + j] = vertices.Array[verticesCopied + j]; } verticesCopied += passVerticesCount; localCreateVerticesIndex += passVerticesCount; if (localCreateVerticesIndex >= createVertexBufferSize - createVertexBufferIndex) { FlushCreateParticles(verticesCopied, currentFrameTime, dt); localCreateVerticesIndex = 0; createVertexBufferIndex = 0; } } renderer.StatefulParticleResourceManager.FreeCreateVertexArray(vertices); if (localCreateVerticesIndex > 0) { FlushCreateParticles(localCreateVerticesIndex, currentFrameTime, dt); } Game.Instance.Profiler.EndSection("render"); PIXHelper.EndEvent(); Game.Instance.Profiler.EndSection("create_particles"); }