Skip to content

Commit

Permalink
Water simulation finally integrated in the data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
AIP21 committed May 9, 2023
1 parent ffb6dd4 commit 951d92b
Show file tree
Hide file tree
Showing 21 changed files with 1,231 additions and 434 deletions.
23 changes: 22 additions & 1 deletion Assets/DataStructure/Data/AbstractGridData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace SimDataStructure.Data
{
// Data for whole grid (one instance per grid).
// This is IMMUTABLE
[Serializable]
public abstract class AbstractGridData
{
Expand All @@ -14,6 +15,26 @@ public AbstractGridData()

}

public abstract void Release();
/**
<summary>
Copies the data from this AbstractGridData to the given object.
</summary>
**/
public abstract void GetData(object target);

/**
<summary>
Copies the data from the given object to this AbstractGridData.
</summary>
**/
public abstract void SetData(object source);

/**
<summary>
Dispose this data. (Dispose any buffers, textures, etc from memory)
This should ALWAYS be called before this object is destroyed or discarded.
</summary>
**/
public abstract void Dispose();
}
}
112 changes: 60 additions & 52 deletions Assets/DataStructure/Data/BufferGridData.cs
Original file line number Diff line number Diff line change
@@ -1,65 +1,73 @@
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
// using UnityEngine;
// using System;
// using System.Collections;
// using System.Collections.Generic;

namespace SimDataStructure.Data
{
public class BufferGridData : AbstractGridData
{
private int width;
private int height;
// namespace SimDataStructure.Data
// {
// public class BufferGridData : AbstractGridData
// {
// private int width;
// private int height;

private int contentSize;
// private int contentSize;

private ComputeBuffer computeBuffer;
// private ComputeBuffer computeBuffer;

public BufferGridData(int width, int height, int contentSize = sizeof(float) * 4)
{
this.width = width;
this.height = height;
this.contentSize = contentSize;
// public BufferGridData(int width, int height, int contentSize = sizeof(float) * 4)
// {
// this.width = width;
// this.height = height;
// this.contentSize = contentSize;

this.computeBuffer = new ComputeBuffer(width * height, contentSize, ComputeBufferType.Default, ComputeBufferMode.Immutable);
}
// this.computeBuffer = new ComputeBuffer(width * height, contentSize, ComputeBufferType.Default, ComputeBufferMode.Immutable);
// }

// Return a copy of the buffer
public ComputeBuffer GetData()
{
ComputeBuffer copy = new ComputeBuffer(width * height, contentSize, ComputeBufferType.Default, ComputeBufferMode.Immutable);
// // Return a copy of the buffer
// public ComputeBuffer GetData()
// {
// ComputeBuffer copy = new ComputeBuffer(width * height, contentSize, ComputeBufferType.Default, ComputeBufferMode.Immutable);

ComputeBuffer.CopyCount(this.computeBuffer, copy, 0);
// ComputeBuffer.(this.computeBuffer, copy, 0);

return copy;
}
// return copy;
// }

public void SetData(ComputeBuffer toSet)
{
this.computeBuffer = toSet;
}
// // public void SetData(ComputeBuffer toSet)
// // {
// // this.computeBuffer = toSet;
// // }

public override void Release()
{
if (this.computeBuffer != null)
{
this.computeBuffer.Release();
this.computeBuffer = null;
}
}

public int GetWidth()
{
return this.width;
}
// public override AbstractGridData Clone()
// {
// BufferGridData clone = new BufferGridData(this.width, this.height, this.contentSize);

// return new BufferGridData(this.width, this.height, this.contentSize);
// }

// public override void Release()
// {
// if (this.computeBuffer != null)
// {
// this.computeBuffer.Release();
// this.computeBuffer = null;
// }
// }

// public int GetWidth()
// {
// return this.width;
// }

public int GetHeight()
{
return this.height;
}
// public int GetHeight()
// {
// return this.height;
// }

public int GetContentSize()
{
return this.contentSize;
}
}
}
// public int GetContentSize()
// {
// return this.contentSize;
// }
// }
// }
47 changes: 38 additions & 9 deletions Assets/DataStructure/Data/TextureGridData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,51 @@ public TextureGridData(int resolution, RenderTextureFormat format, FilterMode fi
this.renderTexture = this.createTexture(format, filterMode);
}

// Return a copy of the texture
public RenderTexture GetData()
/**
<summary>
Copies the data from this TextureGridData to the given RenderTexture.
This just does Graphics.Blit() to copy over the data.
</summary>
**/
public override void GetData(object target)
{
RenderTexture copy = createTexture(this.renderTexture.format, this.renderTexture.filterMode);

Graphics.Blit(this.renderTexture, copy);
if (this.renderTexture == null)
{
Debug.LogError("TextureGridData.SetData: RenderTexture is null");
return;
}

return copy;
if (target != null && target is RenderTexture)
{
Graphics.Blit(this.renderTexture, (RenderTexture)target);
}
else
{
Debug.LogError("TextureGridData.GetData: target is null or not a RenderTexture");
}
}

public void SetData(RenderTexture toSet)
/**
<summary>
Copies the data from the given RenderTexture to this TextureGridData.
This just does Graphics.Blit() to copy over the data.
</summary>
**/
public override void SetData(object source)
{
this.renderTexture = (RenderTexture)toSet;
if (this.renderTexture == null)
{
Debug.LogError("TextureGridData.SetData: RenderTexture is null");
return;
}

if (source != null && source is RenderTexture)
{
Graphics.Blit((RenderTexture)source, this.renderTexture);
}
}

public override void Release()
public override void Dispose()
{
if (this.renderTexture != null)
{
Expand Down
9 changes: 5 additions & 4 deletions Assets/DataStructure/Interfaces/IReadDataStructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ namespace SimDataStructure.Interfaces
/**
<summary>
For a class to be able to read the data, it needs to implement the IReadDataStructure interface.
The implementing class can read data from only ONE grid level
</summary>
*/
public interface IReadDataStructure
{
int ReadLevel { get; } // The grid level to receive data from
List<string> ReadDataNames { get; } // The names of the data to receive from the data structure
Dictionary<string, int> ReadDataNames { get; } // The levels and names of the data to receive from the data structure

/**
<summary>
Called by the data structure at the beginning of every tick to send the requested list of data to the implementing class.
The receiving class should copy the data contained by the AbstractGridData objects in the list, as the data structure will reuse the same AbstractGridData objects for the next tick.
It is recommended for the implementing class to cache the received list of data for use only during the tick, to avoid memory bloat, but the received data list can also be cached for data deltas, etc.
</summary>
*/
void recieveData(List<AbstractGridData> sentData);
void receiveData(List<AbstractGridData> sentData);
}
}
34 changes: 34 additions & 0 deletions Assets/DataStructure/Interfaces/ISetupDataStructure.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SimDataStructure.Data;

namespace SimDataStructure.Interfaces
{
/**
<summary>
Initialize the data structure with data.
This is called by the data structure when it is initialized.
The implementing class should return a dictionary of data name to AbstractGridData objects.
An implementing class should initialize only the data that it or its corresponding system will use.
</summary>
*/
public interface ISetupDataStructure
{
/**
<summary>
Pass new data to the data structure for it to store.
This is called by the data structure only once when it is initialized.
The implementing class should return a dictionary of data name to AbstractGridData objects.
An implementing class should initialize only the data that it or its corresponding system will use.
</summary>
*/
Dictionary<Tuple<string, int>, AbstractGridData> initializeData();
}
}
11 changes: 11 additions & 0 deletions Assets/DataStructure/Interfaces/ISetupDataStructure.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions Assets/DataStructure/Interfaces/IWriteDataStructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@ namespace SimDataStructure.Interfaces
/**
<summary>
For a class to be able to write data to the data structure, it needs to implement the IWriteDataStructure interface.
The implementing class can write data to ONLY one grid level
</summary>
*/
public interface IWriteDataStructure
{
int WriteLevel { get; } // The grid level to write data to
List<string> WriteDataNames { get; } // The names of the data you are writing to the data structure
Dictionary<string, int> WriteDataNames { get; } // The levels and names of the data you are writing to the data structure

/**
<summary>
Called by the data structure at the end of every tick to write data to the data structure.
This function should return a list of the data that the implementing class wants to write to the data structure.
The returned list of data MUST be the same length as the writeDataNames list and the index of each item in the returned list MUST correspond to its respective index in the writeDataNames list.
</summary>
//TODO: If this is slow, then change this to just return a list of objects
*/
List<AbstractGridData> writeData();
Dictionary<Tuple<string, int>, object> writeData();
}
}
Loading

0 comments on commit 951d92b

Please sign in to comment.