Skip to content

Commit

Permalink
Fix sprites don't get rendered before 1st reallocation
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoshidza committed Oct 4, 2023
1 parent bac19bf commit 9b8f41e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
9 changes: 5 additions & 4 deletions Rendering/Common/InstancedProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ internal class InstancedProperty : IDisposable
/// <summary> Cached component type + system to retrieve <see cref="DynamicComponentTypeHandle"/> </summary>
public ComponentType ComponentType { get; }

public InstancedProperty(in int propertyID, in int count, in int stride, in ComponentType componentType)
public InstancedProperty(in int propertyID, in int bufferLength, in int bufferStride, in ComponentType componentType, MaterialPropertyBlock materialPropertyBlock)
{
PropertyID = propertyID;
ComponentType = componentType;

ComputeBuffer = new ComputeBuffer(count, stride, ComputeBufferType.Default, ComputeBufferMode.SubUpdates);
ComputeBuffer = new ComputeBuffer(bufferLength, bufferStride, ComputeBufferType.Default, ComputeBufferMode.SubUpdates);
materialPropertyBlock.SetBuffer(PropertyID, ComputeBuffer);
}

#if !NSPRITES_REACTIVE_DISABLE || !NSPRITES_STATIC_DISABLE
Expand Down Expand Up @@ -153,11 +154,11 @@ public void Complete()
}

/// <summary> Reallocate <see cref="ComputeBuffer"/> with new size and previous stride (tmp) and assign buffer to passed <see cref="MaterialPropertyBlock"/> </summary>
public void Reallocate(in int length, MaterialPropertyBlock materialPropertyBlock)
public void Reallocate(in int bufferLength, MaterialPropertyBlock materialPropertyBlock)
{
var stride = ComputeBuffer.stride;
ComputeBuffer.Release();
ComputeBuffer = new ComputeBuffer(length, stride, ComputeBufferType.Default, ComputeBufferMode.SubUpdates);
ComputeBuffer = new ComputeBuffer(bufferLength, stride, ComputeBufferType.Default, ComputeBufferMode.SubUpdates);
materialPropertyBlock.SetBuffer(PropertyID, ComputeBuffer);
}

Expand Down
6 changes: 3 additions & 3 deletions Rendering/Common/RenderArchetype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public RenderArchetype(Material material, Mesh mesh, in Bounds bounds, IReadOnly
ReactiveAndStaticAllocationCounter.Allocated = preallocatedSpace;
_createdChunksIndexes_RNL = new ReusableNativeList<int>(0, Allocator.Persistent);
_reorderedChunksIndexes_RNL = new ReusableNativeList<int>(0, Allocator.Persistent);
PointersProperty = new InstancedProperty(Shader.PropertyToID(PropertyPointer.PropertyName), preallocatedSpace, sizeof(int), ComponentType.ReadOnly<PropertyPointer>());
PointersProperty = new InstancedProperty(Shader.PropertyToID(PropertyPointer.PropertyName), preallocatedSpace, sizeof(int), ComponentType.ReadOnly<PropertyPointer>(), _materialPropertyBlock);
#endif
#if !NSPRITES_EACH_UPDATE_DISABLE
_perEntityPropertiesSpaceCounter.Allocated = preallocatedSpace;
Expand All @@ -215,7 +215,7 @@ public RenderArchetype(Material material, Mesh mesh, in Bounds bounds, IReadOnly
{
var propData = propertyDataSet[propIndex];
var propType = propertyMap[propData.PropertyID];
var prop = new InstancedProperty(propData.PropertyID, preallocatedSpace, UnsafeUtility.SizeOf(propType.GetManagedType()), propType);
var prop = new InstancedProperty(propData.PropertyID, preallocatedSpace, UnsafeUtility.SizeOf(propType.GetManagedType()), propType, _materialPropertyBlock);

PropertiesContainer.AddProperty(prop, propData.UpdateMode);
}
Expand Down Expand Up @@ -527,7 +527,7 @@ void SyncManyByChunks(IEnumerable<InstancedProperty> props, in SystemData system
}
#endif
// TODO: remove this, seems like it is never used, because `used` and `allocated` is always the same in EachUpdate logic
_perEntityPropertiesSpaceCounter.Used = _entityCount;
//_perEntityPropertiesSpaceCounter.Used = _entityCount;
#endif
#endregion

Expand Down
2 changes: 1 addition & 1 deletion Rendering/Tests/PropertyContainerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void PropertyContainerTestsSimplePasses()
Assert.NotNull(container[PropertyUpdateMode.Reactive]);
Assert.NotNull(container[PropertyUpdateMode.Static]);

var mockProperty = new InstancedProperty(0, 1, 4, default);
var mockProperty = new InstancedProperty(0, 1, 4, default, new());

container.AddProperty(mockProperty, PropertyUpdateMode.EachUpdate);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.tonymax.nsprites",
"version": "3.1.6",
"version": "3.1.7",
"displayName": "NSprites",
"description": "Entities sprite rendering system. Uses GPU instancing with compute buffers to draw entities as sprites.",
"unity": "2022.2",
Expand Down

0 comments on commit 9b8f41e

Please sign in to comment.