Skip to content

Commit

Permalink
Hacked in synchronous shape texture readback for comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
huwb committed May 30, 2018
1 parent 7421ce2 commit 8fa07a5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/unity/Assets/Crest-Examples/Scripts/OceanDebugGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ void OnGUI()
_showSimTargets = GUI.Toggle(new Rect(x, y, w, h), _showSimTargets, "Show shape data"); y += h;
WaveDataCam._shapeCombinePass = GUI.Toggle(new Rect(x, y, w, h), WaveDataCam._shapeCombinePass, "Shape combine pass"); y += h;
WaveDataCam._readbackCollData = GUI.Toggle(new Rect(x, y, w, h), WaveDataCam._readbackCollData, "Readback coll data"); y += h;
WaveDataCam._useAsyncReadback = GUI.Toggle(new Rect(x, y, w, h), WaveDataCam._useAsyncReadback, "Use Async Readback"); y += h;

OceanRenderer._acceptLargeWavelengthsInLastLOD = GUI.Toggle(new Rect(x, y, w, h), OceanRenderer._acceptLargeWavelengthsInLastLOD, "Large waves in last LOD"); y += h;

Expand Down
51 changes: 51 additions & 0 deletions src/unity/Assets/Crest/Scripts/WaveDataCam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class WaveDataCam : MonoBehaviour
// debug use
public static bool _shapeCombinePass = true;
public static bool _readbackCollData = true;
public static bool _useAsyncReadback = true;

// read shape textures back to the CPU for collision purposes
public bool _readbackShapeForCollision = true;
Expand All @@ -37,6 +38,8 @@ struct CollisionRequest
// collision data
NativeArray<ushort> _collDataNative;
RenderData _collRenderData;
RenderTexture _dispTexCopy;
Texture2D _syncReadbackTex;

Material _matOceanDepth;
RenderTexture _rtOceanDepth;
Expand Down Expand Up @@ -93,6 +96,24 @@ private void Update()

void UpdateShapeReadback()
{
if(!_useAsyncReadback)
{
if (!_readbackCollData)
return;

if (_syncReadbackTex == null)
{
_syncReadbackTex = new Texture2D(_dispTexCopy.width, _dispTexCopy.height, TextureFormat.RGBAHalf, false);
}

var oldRT = RenderTexture.active;
RenderTexture.active = _dispTexCopy;
_syncReadbackTex.ReadPixels(new Rect(0, 0, _dispTexCopy.width, _dispTexCopy.height), 0, 0, false);
RenderTexture.active = oldRT;

return;
}

// shape textures are read back to the CPU for collision purposes. this uses an experimental API which
// will hopefully be settled in future unity versions.
// queue pattern inspired by: https://github.com/keijiro/AsyncCaptureTest
Expand Down Expand Up @@ -178,6 +199,19 @@ public bool SampleDisplacement(ref Vector3 worldPos, ref Vector3 displacement)
var rt = cam.targetTexture;
var x = Mathf.FloorToInt(u * rt.width);
var y = Mathf.FloorToInt(v * rt.height);

if (!_useAsyncReadback)
{
if (_syncReadbackTex == null)
return false;

Color sample = _syncReadbackTex.GetPixel(x, y);
displacement.x = sample.r;
displacement.y = sample.g;
displacement.z = sample.b;
return true;
}

var idx = 4 * (y * rt.width + x);

displacement.x = Mathf.HalfToFloat(_collDataNative[idx + 0]);
Expand Down Expand Up @@ -385,6 +419,23 @@ void UpdateCmdBufShapeCombine()
var mat = OceanRenderer.Instance.Builder._shapeWDCs[L]._combineMaterial;
_bufCombineShapes.Blit(cams[L + 1].targetTexture, cams[L].targetTexture, mat);
}

// this code currently only runs once (on init), so need to copy out shape regardless of whether we'll need it later.
//if (!_useAsyncReadback)
{
// now that all the shape is accumulated up, take a copy for readback later
var wdcs = OceanRenderer.Instance.Builder._shapeWDCs;
for (int i = 0; i < cams.Length; i++)
{
if (wdcs[i]._dispTexCopy == null)
{
wdcs[i]._dispTexCopy = new RenderTexture(cams[i].targetTexture);
wdcs[i]._dispTexCopy.name = cams[i].targetTexture.name + "_COPY";
}

_bufCombineShapes.Blit(cams[i].targetTexture, wdcs[i]._dispTexCopy);
}
}
}
}

Expand Down

0 comments on commit 8fa07a5

Please sign in to comment.