Permalink
Browse files

Unity: use NativeArray for backbuffer, 5.27 -> 5.30 Mray/s on WinPC

  • Loading branch information...
aras-p committed Mar 23, 2018
1 parent 6ea3d58 commit 6e0f9e3d0268a6fed041a98923a77770ee8bafbb
Showing with 21 additions and 18 deletions.
  1. +8 −9 Unity/Assets/Test.cs
  2. +12 −8 Unity/Assets/TestScript.cs
  3. +1 −1 Unity/ProjectSettings/ProjectSettings.asset
@@ -10,6 +10,7 @@
using System.Threading;
using UnityEngine;
using Debug = System.Diagnostics.Debug;
using Unity.Collections;
struct Material
{
@@ -211,9 +212,9 @@ float3 Trace(Ray r, int depth, ref int inoutRayCount)
}
}
int TraceRowJob(int y, int screenWidth, int screenHeight, int frameCount, float[] backbuffer, ref Camera cam)
int TraceRowJob(int y, int screenWidth, int screenHeight, int frameCount, NativeArray<Color> backbuffer, ref Camera cam)
{
int backbufferIdx = y * screenWidth * 4;
int backbufferIdx = y * screenWidth;
float invWidth = 1.0f / screenWidth;
float invHeight = 1.0f / screenHeight;
float lerpFac = (float)frameCount / (float)(frameCount + 1);
@@ -236,19 +237,17 @@ int TraceRowJob(int y, int screenWidth, int screenHeight, int frameCount, float[
col *= 1.0f / (float)DO_SAMPLES_PER_PIXEL;
col = new float3(Mathf.Sqrt(col.x), Mathf.Sqrt(col.y), Mathf.Sqrt(col.z));
float3 prev = new float3(backbuffer[backbufferIdx + 0], backbuffer[backbufferIdx + 1], backbuffer[backbufferIdx + 2]);
col = prev * lerpFac + col * (1 - lerpFac);
backbuffer[backbufferIdx + 0] = col.x;
backbuffer[backbufferIdx + 1] = col.y;
backbuffer[backbufferIdx + 2] = col.z;
backbufferIdx += 4;
Color prev = backbuffer[backbufferIdx];
col = new float3(prev.r, prev.g, prev.b) * lerpFac + col * (1 - lerpFac);
backbuffer[backbufferIdx] = new Color(col.x, col.y, col.z, 1);
backbufferIdx++;
}
}
return rayCount;
}
public void DrawTest(float time, int frameCount, int screenWidth, int screenHeight, float[] backbuffer, out int outRayCount)
public void DrawTest(float time, int frameCount, int screenWidth, int screenHeight, NativeArray<Color> backbuffer, out int outRayCount)
{
int rayCount = 0;
#if DO_ANIMATE
@@ -2,14 +2,16 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using UnityEngine;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
public class TestScript : MonoBehaviour
{
public UnityEngine.UI.Text m_UIPerfText;
public UnityEngine.UI.RawImage m_UIImage;
Texture2D m_BackbufferTex;
float[] m_Backbuffer;
NativeArray<Color> m_Backbuffer;
Test m_Test;
Stopwatch m_Stopwatch = new Stopwatch();
@@ -21,13 +23,18 @@ void Start ()
{
int width = 1280, height = 720;
m_BackbufferTex = new Texture2D(width, height, TextureFormat.RGBAFloat, false);
m_Backbuffer = new float[width * height * 4];
for (int i = 0; i < m_Backbuffer.Length; i += 4)
m_Backbuffer[i + 3] = 1.0f;
m_Backbuffer = new NativeArray<Color>(width * height, Allocator.Persistent);
for (int i = 0; i < m_Backbuffer.Length; i++)
m_Backbuffer[i] = new Color(0,0,0,1);
m_UIImage.texture = m_BackbufferTex;
m_Test = new Test();
}
private void OnDestroy()
{
m_Backbuffer.Dispose();
}
void UpdateLoop()
{
m_Stopwatch.Start();
@@ -53,10 +60,7 @@ void Update ()
m_Stopwatch.Reset();
}
GCHandle pinnedArray = GCHandle.Alloc(m_Backbuffer, GCHandleType.Pinned);
IntPtr pointer = pinnedArray.AddrOfPinnedObject();
m_BackbufferTex.LoadRawTextureData(pinnedArray.AddrOfPinnedObject(), m_Backbuffer.Length * 4);
pinnedArray.Free();
unsafe { m_BackbufferTex.LoadRawTextureData((IntPtr)m_Backbuffer.GetUnsafeReadOnlyPtr(), m_Backbuffer.Length * 16); }
m_BackbufferTex.Apply();
}
}
@@ -536,7 +536,7 @@ PlayerSettings:
Standalone: 0
il2cppCompilerConfiguration: {}
incrementalIl2cppBuild: {}
allowUnsafeCode: 0
allowUnsafeCode: 1
additionalIl2CppArgs:
scriptingRuntimeVersion: 1
apiCompatibilityLevelPerPlatform: {}

0 comments on commit 6e0f9e3

Please sign in to comment.