diff --git a/TestProjects/VisualEffectGraph_URP/Assets/ReferenceImages/Linear/WindowsEditor/Direct3D11/None/015_GraphicsBuffer.png b/TestProjects/VisualEffectGraph_URP/Assets/ReferenceImages/Linear/WindowsEditor/Direct3D11/None/015_GraphicsBuffer.png new file mode 100644 index 00000000000..b0ef7e2c54d --- /dev/null +++ b/TestProjects/VisualEffectGraph_URP/Assets/ReferenceImages/Linear/WindowsEditor/Direct3D11/None/015_GraphicsBuffer.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1e940dcc7b7045c177387f2cb4ba2a02f1b21c8eed20a415ca1e30260cb90ea2 +size 9858 diff --git a/TestProjects/VisualEffectGraph_URP/Assets/ReferenceImages/Linear/WindowsEditor/Direct3D11/None/015_GraphicsBuffer.png.meta b/TestProjects/VisualEffectGraph_URP/Assets/ReferenceImages/Linear/WindowsEditor/Direct3D11/None/015_GraphicsBuffer.png.meta new file mode 100644 index 00000000000..04905fee710 --- /dev/null +++ b/TestProjects/VisualEffectGraph_URP/Assets/ReferenceImages/Linear/WindowsEditor/Direct3D11/None/015_GraphicsBuffer.png.meta @@ -0,0 +1,97 @@ +fileFormatVersion: 2 +guid: 2955094ef4df1b74db8c6a802472f4d1 +TextureImporter: + internalIDToNameTable: [] + externalObjects: {} + serializedVersion: 11 + mipmaps: + mipMapMode: 0 + enableMipMap: 0 + sRGBTexture: 1 + linearTexture: 0 + fadeOut: 0 + borderMipMap: 0 + mipMapsPreserveCoverage: 0 + alphaTestReferenceValue: 0.5 + mipMapFadeDistanceStart: 1 + mipMapFadeDistanceEnd: 3 + bumpmap: + convertToNormalMap: 0 + externalNormalMap: 0 + heightScale: 0.25 + normalMapFilter: 0 + isReadable: 1 + streamingMipmaps: 0 + streamingMipmapsPriority: 0 + vTOnly: 0 + grayScaleToAlpha: 0 + generateCubemap: 6 + cubemapConvolution: 0 + seamlessCubemap: 0 + textureFormat: 1 + maxTextureSize: 2048 + textureSettings: + serializedVersion: 2 + filterMode: 1 + aniso: 1 + mipBias: 0 + wrapU: 0 + wrapV: 0 + wrapW: 0 + nPOTScale: 0 + lightmap: 0 + compressionQuality: 50 + spriteMode: 0 + spriteExtrude: 1 + spriteMeshType: 1 + alignment: 0 + spritePivot: {x: 0.5, y: 0.5} + spritePixelsToUnits: 100 + spriteBorder: {x: 0, y: 0, z: 0, w: 0} + spriteGenerateFallbackPhysicsShape: 1 + alphaUsage: 1 + alphaIsTransparency: 0 + spriteTessellationDetail: -1 + textureType: 0 + textureShape: 1 + singleChannelComponent: 0 + flipbookRows: 1 + flipbookColumns: 1 + maxTextureSizeSet: 0 + compressionQualitySet: 0 + textureFormatSet: 0 + ignorePngGamma: 0 + applyGammaDecoding: 0 + platformSettings: + - serializedVersion: 3 + buildTarget: DefaultTexturePlatform + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 0 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + androidETC2FallbackOverride: 0 + forceMaximumCompressionQuality_BC6H_BC7: 0 + spriteSheet: + serializedVersion: 2 + sprites: [] + outline: [] + physicsShape: [] + bones: [] + spriteID: + internalID: 0 + vertices: [] + indices: + edges: [] + weights: [] + secondaryTextures: [] + nameFileIdTable: {} + spritePackingTag: + pSDRemoveMatte: 0 + pSDShowRemoveMatteOption: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/com.unity.testing.visualeffectgraph/Runtime/CreateAndBindGraphicsBuffer.cs b/com.unity.testing.visualeffectgraph/Runtime/CreateAndBindGraphicsBuffer.cs index d21e60ea96f..85ee9c1f4f8 100644 --- a/com.unity.testing.visualeffectgraph/Runtime/CreateAndBindGraphicsBuffer.cs +++ b/com.unity.testing.visualeffectgraph/Runtime/CreateAndBindGraphicsBuffer.cs @@ -37,19 +37,22 @@ static IEnumerable PrefixSum(IEnumerable entries) } } - static readonly Tuple[] s_Colors = new Tuple[] + static readonly Tuple[] s_Colors = GenerateColorPrefixSum(); + static Tuple[] GenerateColorPrefixSum() { - new Tuple(new Vector3(1,1,1), 4.0f), - new Tuple(new Vector3(0,0,0), 1.0f), - new Tuple(new Vector3(1,0,0), 1.0f), - new Tuple(new Vector3(0,0,1), 1.0f), - new Tuple(new Vector3(1,1,0), 1.0f), - }; - - //TODOPAUL DO A FUNCTION :-) - static readonly float[] s_PrefixSum = PrefixSum(s_Colors.Select(o => o.Item2)).ToArray(); - static readonly float[] s_PrefixSumNormalized = s_PrefixSum.Select(o => o / s_PrefixSum.Last()).ToArray(); - static readonly Tuple[] s_ColorsPrefixSumNormalized = s_Colors.Zip(s_PrefixSumNormalized, (a, b) => new Tuple(a.Item1, b)).ToArray(); + var colors = new Tuple[] + { + new Tuple(new Vector3(1,1,1), 4.0f), + new Tuple(new Vector3(0,0,0), 1.0f), + new Tuple(new Vector3(1,0,0), 1.0f), + new Tuple(new Vector3(0,0,1), 1.0f), + new Tuple(new Vector3(1,1,0), 1.0f), + }; + + var prefixSum = PrefixSum(colors.Select(o => o.Item2)).ToArray(); + var prefixSumNormalized = prefixSum.Select(o => o / prefixSum.Last()).ToArray(); + return colors.Zip(prefixSumNormalized, (a, b) => new Tuple(a.Item1, b)).ToArray(); + } private static void ProcessMondrian(List rectangles, System.Random rand) { @@ -86,8 +89,8 @@ private static void ProcessMondrian(List rectangles, System.Random r } randValue = (float)rand.NextDouble(); - var newColor = s_ColorsPrefixSumNormalized.Last().Item1; - foreach (var color in s_ColorsPrefixSumNormalized) + var newColor = s_Colors.Last().Item1; + foreach (var color in s_Colors) { if (randValue < color.Item2) { @@ -102,7 +105,7 @@ private static void ProcessMondrian(List rectangles, System.Random r } private static readonly int s_BufferID = Shader.PropertyToID("buffer"); - private GraphicsBuffer m_buffer; + private GraphicsBuffer m_Buffer; private static readonly uint s_MaxIteration = 12; private static readonly float s_WaitTime = 1.0f / (float)s_MaxIteration; @@ -113,22 +116,26 @@ private static void ProcessMondrian(List rectangles, System.Random r private List m_Data; - void Start() + void Reallocate(int newSize) { - if (m_buffer != null) - m_buffer.Release(); + if (m_Buffer != null) + m_Buffer.Release(); - m_buffer = new GraphicsBuffer(GraphicsBuffer.Target.Structured, 4096, Marshal.SizeOf(typeof(CustomData))); - m_buffer.SetData(new CustomData[4096]); //Why we need to clear ? TODOPAUL : try to reallocate + m_Buffer = new GraphicsBuffer(GraphicsBuffer.Target.Structured, newSize, Marshal.SizeOf(typeof(CustomData))); + m_Buffer.SetData(new CustomData[newSize]); + } + void Start() + { + Reallocate(4); m_Data = new List() { new CustomData() { position = new Vector3(0, 0, 0), rectangle = new Rectangle() { color = new Vector3(1, 1, 1), size = new Vector2(1024, 1024) }} }; - m_buffer.SetData(m_Data); + m_Buffer.SetData(m_Data); var vfx = GetComponent(); - vfx.SetGraphicsBuffer(s_BufferID, m_buffer); + vfx.SetGraphicsBuffer(s_BufferID, m_Buffer); m_Random = new System.Random(1245); } @@ -143,11 +150,19 @@ void Update() { m_MaxIteration--; ProcessMondrian(m_Data, m_Random); - m_buffer.SetData(m_Data); + + if (m_Data.Count > m_Buffer.count) + { + int newCount = m_Buffer.count; + while (newCount < m_Data.Count) + newCount *= 2; + Reallocate(newCount); + } + + m_Buffer.SetData(m_Data); if (m_MaxIteration == 0) m_Wait = 1.0f; - } else { @@ -159,10 +174,10 @@ void Update() public void OnDisable() { - if (m_buffer != null) + if (m_Buffer != null) { - m_buffer.Release(); - m_buffer = null; + m_Buffer.Release(); + m_Buffer = null; } } }