Skip to content

Commit

Permalink
Culling Version 2
Browse files Browse the repository at this point in the history
  • Loading branch information
PodeCaradox committed Jun 3, 2022
1 parent a50baa7 commit bccd982
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
7 changes: 2 additions & 5 deletions VFRZInstancing/Content/instance_effect.fx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ struct Tile

StructuredBuffer<Tile> AllTiles;
RWStructuredBuffer<Tile> VisibleTiles;
RWStructuredBuffer<uint> CountData;
int StartPosX;
int StartPosY;
int Columns;
int MapSizeX;
int MapSizeY;

Expand All @@ -42,10 +42,7 @@ void InstancingCS(uint3 localID : SV_GroupThreadID, uint3 groupID : SV_GroupID,


if(index.x < 0 || index.y < 0 || index.y >= MapSizeY || index.x >= MapSizeX) return;

uint outID;
InterlockedAdd(CountData[0], 1, outID); // increment the instance count in the indirect draw buffer (starts at byte 4)
VisibleTiles[outID] = AllTiles[index.y * MapSizeX + index.x];
VisibleTiles[globalID.x + globalID.y * Columns] = AllTiles[index.y * MapSizeX + index.x];
}


Expand Down
28 changes: 9 additions & 19 deletions VFRZInstancing/TileMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ public class TileMap : DrawableGameComponent

private StructuredBuffer _allTiles;
private StructuredBuffer _visibleTiles;
private StructuredBuffer _counter;
private VertexBuffer _geometryBuffer;
private Instances[] _instances;
private uint _instancesVertexBuffer;
const int _computeGroupSize = 32;
private uint[] counterValue = new uint[1];
#endregion

#region TileMapData
Expand Down Expand Up @@ -221,7 +219,6 @@ protected override void LoadContent()
//the instances can change so we have a StructuredBuffer buffer
_allTiles = new StructuredBuffer(GraphicsDevice, typeof(Instances), InstanceCount, BufferUsage.WriteOnly, ShaderAccess.Read);
_visibleTiles = new StructuredBuffer(GraphicsDevice, typeof(Instances), InstanceCount, BufferUsage.WriteOnly, ShaderAccess.ReadWrite);
_counter = new StructuredBuffer(GraphicsDevice, typeof(uint), 1, BufferUsage.None, ShaderAccess.ReadWrite);
InitializeInstances();
_allTiles.SetData(_instances);

Expand Down Expand Up @@ -288,22 +285,19 @@ public void Draw(GameTime gameTime)
{
// Set the effect technique and parameters
_effect.CurrentTechnique = _effect.Techniques["Instancing"];

ComputeCulling();

if (_instancesVertexBuffer <= 0) return;


_effect.Parameters["WorldViewProjection"].SetValue(_camera.Transform * _camera.Projection);
_effect.Parameters["TileBuffer"].SetValue(_visibleTiles);

// Apply the current technique pass.
_effect.CurrentTechnique.Passes[0].Apply();

GraphicsDevice.BlendState = BlendState.NonPremultiplied;
GraphicsDevice.RasterizerState = _raster;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;


GraphicsDevice.SamplerStates[0] = SS_PointBorder;

GraphicsDevice.SetVertexBuffer(_geometryBuffer);
Expand All @@ -316,28 +310,24 @@ public void Draw(GameTime gameTime)
private void ComputeCulling()
{
var drawingArea = _camera.CalculateDrawingArea(_tileSize);


//calculate how many tiles will be drawn.;
int tileCountX = drawingArea.Height + (_computeGroupSize - drawingArea.Height % _computeGroupSize);
int tileCountY = drawingArea.Width + (_computeGroupSize - drawingArea.Width % _computeGroupSize);

_effect.Parameters["StartPosX"].SetValue(drawingArea.X);
_effect.Parameters["StartPosY"].SetValue(drawingArea.Y);
_effect.Parameters["MapSizeX"].SetValue(_size.X);
_effect.Parameters["MapSizeY"].SetValue(_size.Y);
_effect.Parameters["AllTiles"].SetValue(_allTiles);
_effect.Parameters["VisibleTiles"].SetValue(_visibleTiles);
_effect.Parameters["CountData"].SetValue(_counter);

_effect.Parameters["Columns"].SetValue(tileCountX);

counterValue[0] = 0;
_counter.SetData(counterValue);

//calculate how many tiles will be drawn. Could also use that so we dont need a counter. Just use in shader the globalID.x + globalID.y * drawingArea.Height instead of outId;
int tileCountX = drawingArea.Height + (_computeGroupSize - drawingArea.Height % _computeGroupSize);
int tileCountY = drawingArea.Width + (_computeGroupSize - drawingArea.Width % _computeGroupSize);

_effect.CurrentTechnique.Passes[0].ApplyCompute();
GraphicsDevice.DispatchCompute(tileCountX / _computeGroupSize, tileCountY / _computeGroupSize, 1);

_counter.GetData(counterValue);
_instancesVertexBuffer = counterValue[0];//here tileCountX + tileCountY * drawingArea.Height
_instancesVertexBuffer = (uint)(tileCountX + tileCountY * tileCountX);
}
#endregion
}
Expand Down

0 comments on commit bccd982

Please sign in to comment.