Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Burstify queries #1143

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// This file is subject to the MIT License as seen in the root of this folder structure (LICENSE)

using Unity.Collections;
using UnityEngine;

namespace Crest.Examples
Expand All @@ -27,13 +28,38 @@ public class OceanSampleDisplacementDemo : MonoBehaviour
public float _minGridSize = 0f;

GameObject[] _markerObjects = new GameObject[3];
#if CREST_BURST_QUERY
NativeArray<Vector3> _markerPos;
NativeArray<Vector3> _resultDisps;
NativeArray<Vector3> _resultNorms;
NativeArray<Vector3> _resultVels;
#else
Vector3[] _markerPos = new Vector3[3];
Vector3[] _resultDisps = new Vector3[3];
Vector3[] _resultNorms = new Vector3[3];
Vector3[] _resultVels = new Vector3[3];
#endif

float _samplesRadius = 5f;

#if CREST_BURST_QUERY
void OnEnable()
{
_markerPos = new NativeArray<Vector3>(3, Allocator.Persistent);
_resultDisps = new NativeArray<Vector3>(3, Allocator.Persistent);
_resultNorms = new NativeArray<Vector3>(3, Allocator.Persistent);
_resultVels = new NativeArray<Vector3>(3, Allocator.Persistent);
}

void OnDisable()
{
_markerPos.Dispose();
_resultDisps.Dispose();
_resultNorms.Dispose();
_resultVels.Dispose();
}
#endif

void Update()
{
if (OceanRenderer.Instance == null)
Expand All @@ -53,7 +79,11 @@ void Update()

var collProvider = OceanRenderer.Instance.CollisionProvider;

#if CREST_BURST_QUERY
var status = collProvider.Query(GetHashCode(), _minGridSize, ref _markerPos, ref _resultDisps, ref _resultNorms, ref _resultVels);
#else
var status = collProvider.Query(GetHashCode(), _minGridSize, _markerPos, _resultDisps, _resultNorms, _resultVels);
#endif

if (collProvider.RetrieveSucceeded(status))
{
Expand Down
9 changes: 9 additions & 0 deletions crest/Assets/Crest/Crest/Scripts/Collision/CollProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// NOTE: DWP2 depends on this file. Any API changes need to be communicated to the DWP2 authors in advance.

using Unity.Collections;
using UnityEngine;

namespace Crest
Expand All @@ -22,7 +23,11 @@ public interface ICollProvider
/// <param name="o_resultHeights">Float array of water heights at the query positions. Pass null if this information is not required.</param>
/// <param name="o_resultNorms">Water normals at the query positions. Pass null if this information is not required.</param>
/// <param name="o_resultVels">Water surface velocities at the query positions. Pass null if this information is not required.</param>
#if CREST_BURST_QUERY
int Query(int i_ownerHash, float i_minSpatialLength, ref NativeArray<Vector3> i_queryPoints, ref NativeArray<float> o_resultHeights, ref NativeArray<Vector3> o_resultNorms, ref NativeArray<Vector3> o_resultVels);
#else
int Query(int i_ownerHash, float i_minSpatialLength, Vector3[] i_queryPoints, float[] o_resultHeights, Vector3[] o_resultNorms, Vector3[] o_resultVels);
#endif

/// <summary>
/// Query water physical data at a set of points. Pass in null to any out parameters that are not required.
Expand All @@ -33,7 +38,11 @@ public interface ICollProvider
/// <param name="o_resultDisps">Displacement vectors for water surface points that will displace to the XZ coordinates of the query points. Water heights are given by sea level plus the y component of the displacement.</param>
/// <param name="o_resultNorms">Water normals at the query positions. Pass null if this information is not required.</param>
/// <param name="o_resultVels">Water surface velocities at the query positions. Pass null if this information is not required.</param>
#if CREST_BURST_QUERY
int Query(int i_ownerHash, float i_minSpatialLength, ref NativeArray<Vector3> i_queryPoints, ref NativeArray<Vector3> o_resultDisps, ref NativeArray<Vector3> o_resultNorms, ref NativeArray<Vector3> o_resultVels);
#else
int Query(int i_ownerHash, float i_minSpatialLength, Vector3[] i_queryPoints, Vector3[] o_resultDisps, Vector3[] o_resultNorms, Vector3[] o_resultVels);
#endif

/// <summary>
/// Check if query results could be retrieved successfully using return code from Query() function
Expand Down
118 changes: 96 additions & 22 deletions crest/Assets/Crest/Crest/Scripts/Collision/CollProviderBakedFFT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ class QueryData
/// position data will be created. This will force any running jobs to complete. The jobs will be kicked off in LateUpdate,
/// so this should be called before the kick-off, such as from Update.
/// </summary>
#if CREST_BURST_QUERY
public int RegisterQueryPoints(int ownerHash, ref NativeArray<Vector3> queryPoints, int dataToWriteThisFrame)
#else
public int RegisterQueryPoints(int ownerHash, Vector3[] queryPoints, int dataToWriteThisFrame)
#endif
{
var numQuads = (queryPoints.Length + 3) / 4;

Expand Down Expand Up @@ -222,10 +226,14 @@ public CollProviderBakedFFT(FFTBakedData data)
}
}

#if CREST_BURST_QUERY
bool RetrieveHeights(int i_ownerHash, NativeArray<float> o_resultHeights)
#else
bool RetrieveHeights(int i_ownerHash, float[] o_resultHeights)
#endif
{
// Return data - get segment from finished jobs
if (o_resultHeights != null && _queryDataHeights._segmentRegistryQueriesResults.TryGetValue(i_ownerHash, out var computedQuerySegment))
if (_queryDataHeights._segmentRegistryQueriesResults.TryGetValue(i_ownerHash, out var computedQuerySegment))
{
// Copy results to output. Could be avoided if query api was changed to NAs.
for (int i = 0; i < o_resultHeights.Length; i++)
Expand All @@ -238,10 +246,14 @@ bool RetrieveHeights(int i_ownerHash, float[] o_resultHeights)
return false;
}

#if CREST_BURST_QUERY
bool RetrieveDisps(int i_ownerHash, NativeArray<Vector3> o_resultDisps)
#else
bool RetrieveDisps(int i_ownerHash, Vector3[] o_resultDisps)
#endif
{
// Return data - get segment from finished jobs
if (o_resultDisps != null && _queryDataDisps._segmentRegistryQueriesResults.TryGetValue(i_ownerHash, out var computedQuerySegment))
if (_queryDataDisps._segmentRegistryQueriesResults.TryGetValue(i_ownerHash, out var computedQuerySegment))
{
// Copy results to output. Could be avoided if query api was changed to NAs.
Vector3 disp;
Expand All @@ -258,9 +270,13 @@ bool RetrieveDisps(int i_ownerHash, Vector3[] o_resultDisps)
return false;
}

#if CREST_BURST_QUERY
bool RetrieveNorms(int i_ownerHash, NativeArray<Vector3> o_resultNorms)
#else
bool RetrieveNorms(int i_ownerHash, Vector3[] o_resultNorms)
#endif
{
if (o_resultNorms != null && _queryDataNorms._segmentRegistryQueriesResults.TryGetValue(i_ownerHash, out var computedQuerySegment))
if (_queryDataNorms._segmentRegistryQueriesResults.TryGetValue(i_ownerHash, out var computedQuerySegment))
{
// Copy results to output. Could be avoided if query api was changed to NAs.
Vector3 norm;
Expand All @@ -277,9 +293,13 @@ bool RetrieveNorms(int i_ownerHash, Vector3[] o_resultNorms)
return false;
}

#if CREST_BURST_QUERY
bool RetrieveVels(int i_ownerHash, NativeArray<Vector3> o_resultVels)
#else
bool RetrieveVels(int i_ownerHash, Vector3[] o_resultVels)
#endif
{
if (o_resultVels != null && _queryDataVels._segmentRegistryQueriesResults.TryGetValue(i_ownerHash, out var computedQuerySegment))
if (_queryDataVels._segmentRegistryQueriesResults.TryGetValue(i_ownerHash, out var computedQuerySegment))
{
// Copy results to output. Could be avoided if query api was changed to NAs.
Vector3 vel = Vector3.zero;
Expand All @@ -297,65 +317,119 @@ bool RetrieveVels(int i_ownerHash, Vector3[] o_resultVels)
public int Query(
int i_ownerHash,
float i_minSpatialLength,
#if CREST_BURST_QUERY
ref NativeArray<Vector3> i_queryPoints,
ref NativeArray<float> o_resultHeights,
ref NativeArray<Vector3> o_resultNorms,
ref NativeArray<Vector3> o_resultVels
#else
Vector3[] i_queryPoints,
float[] o_resultHeights,
Vector3[] o_resultNorms,
Vector3[] o_resultVels
#endif
)
{
var dataCopiedOutHeights = RetrieveHeights(i_ownerHash, o_resultHeights);
var dataCopiedOutNorms = RetrieveNorms(i_ownerHash, o_resultNorms);
var dataCopiedOutVels = RetrieveVels(i_ownerHash, o_resultVels);
#if CREST_BURST_QUERY
var useHeight = o_resultHeights.Length > 0;
var useNormal = o_resultNorms.Length > 0;
var useVelocity = o_resultVels.Length > 0;
#else
var useHeight = o_resultHeights?.Length > 0;
var useNormal = o_resultNorms?.Length > 0;
var useVelocity = o_resultVels?.Length > 0;
#endif

if (o_resultHeights != null)
var dataCopiedOutHeights = !useHeight || RetrieveHeights(i_ownerHash, o_resultHeights);
var dataCopiedOutNorms = !useNormal || RetrieveNorms(i_ownerHash, o_resultNorms);
var dataCopiedOutVels = !useVelocity || RetrieveVels(i_ownerHash, o_resultVels);

if (useHeight)
{
#if CREST_BURST_QUERY
_queryDataHeights.RegisterQueryPoints(i_ownerHash, ref i_queryPoints, 1 - _dataBeingUsedByJobs);
#else
_queryDataHeights.RegisterQueryPoints(i_ownerHash, i_queryPoints, 1 - _dataBeingUsedByJobs);
#endif
}
if (o_resultNorms != null)
if (useNormal)
{
#if CREST_BURST_QUERY
_queryDataNorms.RegisterQueryPoints(i_ownerHash, ref i_queryPoints, 1 - _dataBeingUsedByJobs);
#else
_queryDataNorms.RegisterQueryPoints(i_ownerHash, i_queryPoints, 1 - _dataBeingUsedByJobs);
#endif
}
if (o_resultVels != null)
if (useVelocity)
{
#if CREST_BURST_QUERY
_queryDataVels.RegisterQueryPoints(i_ownerHash, ref i_queryPoints, 1 - _dataBeingUsedByJobs);
#else
_queryDataVels.RegisterQueryPoints(i_ownerHash, i_queryPoints, 1 - _dataBeingUsedByJobs);
#endif
}

var allCopied = (dataCopiedOutHeights || o_resultHeights == null)
&& (dataCopiedOutNorms || o_resultNorms == null)
&& (dataCopiedOutVels || o_resultVels == null);
var allCopied = dataCopiedOutHeights && dataCopiedOutNorms && dataCopiedOutVels;

return allCopied ? (int)QueryStatus.Success : (int)QueryStatus.ResultsNotReadyYet;
}

public int Query(
int i_ownerHash,
float i_minSpatialLength,
#if CREST_BURST_QUERY
ref NativeArray<Vector3> i_queryPoints,
ref NativeArray<Vector3> o_resultDisps,
ref NativeArray<Vector3> o_resultNorms,
ref NativeArray<Vector3> o_resultVels
#else
Vector3[] i_queryPoints,
Vector3[] o_resultDisps,
Vector3[] o_resultNorms,
Vector3[] o_resultVels
#endif
)
{
var dataCopiedOutDisps = RetrieveDisps(i_ownerHash, o_resultDisps);
var dataCopiedOutNorms = RetrieveNorms(i_ownerHash, o_resultNorms);
var dataCopiedOutVels = RetrieveVels(i_ownerHash, o_resultVels);
#if CREST_BURST_QUERY
var useDisplacement = o_resultDisps.Length > 0;
var useNormal = o_resultNorms.Length > 0;
var useVelocity = o_resultVels.Length > 0;
#else
var useDisplacement = o_resultDisps?.Length > 0;
var useNormal = o_resultNorms?.Length > 0;
var useVelocity = o_resultVels?.Length > 0;
#endif

if (o_resultDisps != null)
var dataCopiedOutDisps = !useDisplacement || RetrieveDisps(i_ownerHash, o_resultDisps);
var dataCopiedOutNorms = !useNormal || RetrieveNorms(i_ownerHash, o_resultNorms);
var dataCopiedOutVels = !useVelocity || RetrieveVels(i_ownerHash, o_resultVels);

if (useDisplacement)
{
#if CREST_BURST_QUERY
_queryDataDisps.RegisterQueryPoints(i_ownerHash, ref i_queryPoints, 1 - _dataBeingUsedByJobs);
#else
_queryDataDisps.RegisterQueryPoints(i_ownerHash, i_queryPoints, 1 - _dataBeingUsedByJobs);
#endif
}
if (o_resultNorms != null)
if (useNormal)
{
#if CREST_BURST_QUERY
_queryDataNorms.RegisterQueryPoints(i_ownerHash, ref i_queryPoints, 1 - _dataBeingUsedByJobs);
#else
_queryDataNorms.RegisterQueryPoints(i_ownerHash, i_queryPoints, 1 - _dataBeingUsedByJobs);
#endif
}
if (o_resultVels != null)
if (useVelocity)
{
#if CREST_BURST_QUERY
_queryDataVels.RegisterQueryPoints(i_ownerHash, ref i_queryPoints, 1 - _dataBeingUsedByJobs);
#else
_queryDataVels.RegisterQueryPoints(i_ownerHash, i_queryPoints, 1 - _dataBeingUsedByJobs);
#endif
}

var allCopied = (dataCopiedOutDisps || o_resultDisps == null)
&& (dataCopiedOutNorms || o_resultNorms == null)
&& (dataCopiedOutVels || o_resultVels == null);
var allCopied = dataCopiedOutDisps && dataCopiedOutNorms && dataCopiedOutVels;

return allCopied ? (int)QueryStatus.Success : (int)QueryStatus.ResultsNotReadyYet;
}
Expand Down
72 changes: 25 additions & 47 deletions crest/Assets/Crest/Crest/Scripts/Collision/CollProviderNull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// This file is subject to the MIT License as seen in the root of this folder structure (LICENSE)

using Unity.Collections;
using UnityEngine;

namespace Crest
Expand All @@ -11,63 +12,40 @@ namespace Crest
/// </summary>
public class CollProviderNull : ICollProvider
{
public int Query(int i_ownerHash, float i_minSpatialLength, Vector3[] i_queryPoints, Vector3[] o_resultDisps, Vector3[] o_resultNorms, Vector3[] o_resultVels)
#if CREST_BURST_QUERY
public int Query(int i_ownerHash, float i_minSpatialLength, ref NativeArray<Vector3> i_queryPoints, ref NativeArray<Vector3> o_resultDisps, ref NativeArray<Vector3> o_resultNorms, ref NativeArray<Vector3> o_resultVels)
{
if (o_resultDisps != null)
{
for (int i = 0; i < o_resultDisps.Length; i++)
{
o_resultDisps[i] = Vector3.zero;
}
}

if (o_resultNorms != null)
{
for (int i = 0; i < o_resultNorms.Length; i++)
{
o_resultNorms[i] = Vector3.up;
}
}
for (int i = 0; i < o_resultDisps.Length; i++) o_resultDisps[i] = Vector3.zero;
for (int i = 0; i < o_resultNorms.Length; i++) o_resultNorms[i] = Vector3.up;
for (int i = 0; i < o_resultVels.Length; i++) o_resultVels[i] = Vector3.zero;
return 0;
}

if (o_resultVels != null)
{
for (int i = 0; i < o_resultVels.Length; i++)
{
o_resultVels[i] = Vector3.zero;
}
}
public int Query(int i_ownerHash, float i_minSpatialLength, ref NativeArray<Vector3> i_queryPoints, ref NativeArray<float> o_resultHeights, ref NativeArray<Vector3> o_resultNorms, ref NativeArray<Vector3> o_resultVels)
{
for (int i = 0; i < o_resultHeights.Length; i++) o_resultHeights[i] = 0f;
for (int i = 0; i < o_resultNorms.Length; i++) o_resultNorms[i] = Vector3.up;
for (int i = 0; i < o_resultVels.Length; i++) o_resultVels[i] = Vector3.zero;
return 0;
}
#else
public int Query(int i_ownerHash, float i_minSpatialLength, Vector3[] i_queryPoints, Vector3[] o_resultDisps, Vector3[] o_resultNorms, Vector3[] o_resultVels)

{
for (int i = 0; i < o_resultDisps?.Length; i++) o_resultDisps[i] = Vector3.zero;
for (int i = 0; i < o_resultNorms?.Length; i++) o_resultNorms[i] = Vector3.up;
for (int i = 0; i < o_resultVels?.Length; i++) o_resultVels[i] = Vector3.zero;
return 0;
}

public int Query(int i_ownerHash, float i_minSpatialLength, Vector3[] i_queryPoints, float[] o_resultHeights, Vector3[] o_resultNorms, Vector3[] o_resultVels)
{
if (o_resultHeights != null)
{
for (int i = 0; i < o_resultHeights.Length; i++)
{
o_resultHeights[i] = 0f;
}
}

if (o_resultNorms != null)
{
for (int i = 0; i < o_resultNorms.Length; i++)
{
o_resultNorms[i] = Vector3.up;
}
}

if (o_resultVels != null)
{
for (int i = 0; i < o_resultVels.Length; i++)
{
o_resultVels[i] = Vector3.zero;
}
}

for (int i = 0; i < o_resultHeights?.Length; i++) o_resultHeights[i] = 0f;
for (int i = 0; i < o_resultNorms?.Length; i++) o_resultNorms[i] = Vector3.up;
for (int i = 0; i < o_resultVels?.Length; i++) o_resultVels[i] = Vector3.zero;
return 0;
}
#endif

public bool RetrieveSucceeded(int queryStatus)
{
Expand Down
Loading