private void BuildForcefieldsBuffer( out TypedComputeBuffer <SwarmShaderForcefieldState> outPooledForcefieldsBuffer, out int outActiveForcefieldCount) { // Grab the oldest buffer off the queue, and move it back to mark it as the most recently touched buffer. TypedComputeBuffer <SwarmShaderForcefieldState> targetForcefieldsBuffer = forcefieldsBufferQueue.Dequeue(); forcefieldsBufferQueue.Enqueue(targetForcefieldsBuffer); swarmForcefieldCollector.CollectForcefields( transform.localToWorldMatrix, ref scratchForcefieldStateList); if (scratchForcefieldStateList.Count > targetForcefieldsBuffer.count) { Debug.LogWarningFormat( "Discarding some forcefields since [{0}] were wanted, but only [{1}] can be passed to the shader.", scratchForcefieldStateList.Count, targetForcefieldsBuffer.count); scratchForcefieldStateList.RemoveRange( targetForcefieldsBuffer.count, (scratchForcefieldStateList.Count - targetForcefieldsBuffer.count)); } targetForcefieldsBuffer.SetData(scratchForcefieldStateList.ToArray()); outPooledForcefieldsBuffer = targetForcefieldsBuffer; outActiveForcefieldCount = scratchForcefieldStateList.Count; }
private bool TryAllocateBuffers() { bool result = false; if (!SystemInfo.supportsComputeShaders) { Debug.LogError("Compute shaders are not supported on this machine. Is DX11 or later installed?"); } else { if (swarmerModelVerticesBuffer == null) { var swarmerModelVertices = new List <SwarmShaderSwarmerModelVertex>(); switch (ModelShape) { case ModelShapeType.SimpleFlatTriangle: { AppendFlatDoubleSidedTriangleVerticesToModel( 0.0f, // leftSegmentFraction 0.0f, // rightSegmentFraction ModelColoration, Matrix4x4.identity, ref swarmerModelVertices); break; } case ModelShapeType.TriamondWing: { AppendTriangularBifrustumVerticesToModel( 0.0f, // leftSegmentFraction 0.0f, // rightSegmentFraction ModelColoration, FacetType.Generic, // Front-left (before rotation). FacetType.Generic, // Front-right (before rotation). FacetType.Front, // Rear (before rotation). Matrix4x4.TRS( new Vector3( 0.0f, 0.0f, (1.0f - Mathf.Sin(30.0f * Mathf.Deg2Rad))), Quaternion.AngleAxis(180, Vector3.up), Vector3.one), ref swarmerModelVertices); AppendTriangularBifrustumVerticesToModel( 0.0f, // leftSegmentFraction 1.0f, // rightSegmentFraction ModelColoration, FacetType.Generic, // Front-left (before rotation). FacetType.Front, // Front-right (before rotation). FacetType.Rear, // Rear (before rotation). Matrix4x4.TRS( new Vector3( (1.0f * Mathf.Cos(30.0f * Mathf.Deg2Rad)), 0.0f, 0.0f), Quaternion.identity, Vector3.one), ref swarmerModelVertices); AppendTriangularBifrustumVerticesToModel( 1.0f, // leftSegmentFraction 0.0f, // rightSegmentFraction ModelColoration, FacetType.Front, // Front-left (before rotation). FacetType.Generic, // Front-right (before rotation). FacetType.Rear, // Rear (before rotation). Matrix4x4.TRS( new Vector3( (-1.0f * Mathf.Cos(30.0f * Mathf.Deg2Rad)), 0.0f, 0.0f), Quaternion.identity, Vector3.one), ref swarmerModelVertices); break; } default: throw new System.ComponentModel.InvalidEnumArgumentException(); } swarmerModelVerticesBuffer = new TypedComputeBuffer <SwarmShaderSwarmerModelVertex>(swarmerModelVertices.Count); swarmerModelVerticesBuffer.SetData(swarmerModelVertices.ToArray()); allocatedModelColorationType = ModelColoration; allocatedModelShapeType = ModelShape; } if (swarmerModelVerticesBuffer != null) { result = true; } else { // Abort any partial-allocations. ReleaseBuffers(); } } if (DebugLoggingEnabled) { Debug.LogFormat("Compute buffer allocation attempted. [Success={0}]", result); } return(result); }