Skip to content

optimize the clear head method from SetData to a CS kernel #11

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

Merged
merged 3 commits into from
Sep 18, 2022
Merged
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
16 changes: 12 additions & 4 deletions Assets/OrderIndependentTransparency/Scripts/OitLinkedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ public class OitLinkedList : IOrderIndependentTransparency
private int bufferSize;
private int bufferStride;
private Material linkedListMaterial;
private uint[] resetTable;
private const int MAX_SORTED_PIXELS = 8;

private ComputeShader oitComputeUtils;
private int clearStartOffsetBufferKernel;
private int dispatchGroupSizeX, dispatchGroupSizeY;

public OitLinkedList(bool postProcess = false)
{
linkedListMaterial = new Material(Shader.Find("Hidden/LinkedListRendering"));
Expand All @@ -35,16 +38,21 @@ public OitLinkedList(bool postProcess = false)
startOffsetBuffer = new GraphicsBuffer(GraphicsBuffer.Target.Raw, bufferSizeHead, bufferStrideHead);
startOffsetBufferId = Shader.PropertyToID("StartOffsetBuffer");

resetTable = new uint[bufferSizeHead];
oitComputeUtils = Resources.Load<ComputeShader>("OitComputeUtils");
clearStartOffsetBufferKernel = oitComputeUtils.FindKernel("ClearStartOffsetBuffer");
oitComputeUtils.SetBuffer(clearStartOffsetBufferKernel, startOffsetBufferId, startOffsetBuffer);
oitComputeUtils.SetInt("bufferWidth", bufferWidth);
dispatchGroupSizeX = Mathf.CeilToInt(bufferWidth / 32.0f);
dispatchGroupSizeY = Mathf.CeilToInt(bufferHeight / 32.0f);
}

public void PreRender()
{
if (fragmentLinkBuffer == null || startOffsetBuffer == null || startOffsetBuffer.count != resetTable.Length)
if (fragmentLinkBuffer == null || startOffsetBuffer == null)
return;

//reset StartOffsetBuffer to zeros
startOffsetBuffer.SetData(resetTable);
oitComputeUtils.Dispatch(clearStartOffsetBufferKernel, dispatchGroupSizeX, dispatchGroupSizeY, 1);

// set buffers for rendering
Graphics.SetRandomWriteTarget(1, fragmentLinkBuffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ RWByteAddressBuffer StartOffsetBuffer : register(u2);

void createFragmentEntry(float4 col, float3 pos, uint uCoverage) {
//Retrieve current Pixel count and increase counter
uint uPixelCount = FLBuffer.IncrementCounter();
uint uPixelCount = FLBuffer.IncrementCounter()+1;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i leave first one empty for safer debug, otherwise it's easy to make a infinite loop and make my computer crash


//calculate bufferAddress
uint uStartOffsetAddress = 4 * (_ScreenParams.x * (pos.y - 0.5) + (pos.x - 0.5));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma kernel ClearStartOffsetBuffer

RWByteAddressBuffer StartOffsetBuffer;
int bufferWidth;

[numthreads(32, 32, 1)]
void ClearStartOffsetBuffer (uint3 id: SV_DispatchThreadID)
{
StartOffsetBuffer.Store(4 * (bufferWidth * id.y + id.x), 0);
}

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