-
Notifications
You must be signed in to change notification settings - Fork 422
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
Add support for shader storage buffer objects (SSBOs) #5950
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
c371ec2
Initial implementation of SSBOs for GL and Veldrid
smoogipoo 3b1bf4b
Add unit test
smoogipoo 3ba6ecb
Add shader support and visual test showcasing raw SSBO usage
smoogipoo 300afed
Merge branch 'master' into shader-storage-buffer-objects
smoogipoo ee35088
Remove unused buffer binding method
smoogipoo e11a4fc
Refactor/fix GL ubo/ssbo binding
smoogipoo 5e9276d
Add implementation demonstrating stack-based usage
smoogipoo 43a898e
Prevent pipeline flushes when rebinding UBOs
smoogipoo f3bff98
Convert indentation to spaces
smoogipoo 5ea8e82
Fix failing edge case when transitioning from empty stack
smoogipoo 5648bd7
Rename CurrentIndex -> CurrentOffset
smoogipoo c39f4c7
Add new flush batch source for SSBO stack
smoogipoo 1996ca9
Log value of UseStructuredBuffers
smoogipoo 2ca906e
Apply refactorings from code review
smoogipoo 730cc38
Add comment on Clear() about preventing runaway VRAM usage
smoogipoo 1e91ef7
Add missing .Clear() call in test scene
smoogipoo b4ee7dd
Update packages
9ef363e
Remove unused field
bdach 15a35c3
Merge branch 'master' into shader-storage-buffer-objects
bdach 7cc4dbe
Fix SSBO usages crashing on Direct3D11
bdach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
258 changes: 258 additions & 0 deletions
258
osu.Framework.Tests/Graphics/ShaderStorageBufferObjectStackTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,258 @@ | ||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using System; | ||
using NUnit.Framework; | ||
using osu.Framework.Graphics.Rendering; | ||
using osu.Framework.Graphics.Rendering.Dummy; | ||
|
||
namespace osu.Framework.Tests.Graphics | ||
{ | ||
public class ShaderStorageBufferObjectStackTest | ||
{ | ||
private const int size = 10; | ||
|
||
private ShaderStorageBufferObjectStack<int> stack = null!; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
stack = new ShaderStorageBufferObjectStack<int>(new DummyRenderer(), 2, size); | ||
} | ||
|
||
[Test] | ||
public void TestBufferMustBeAtLeast2Elements() | ||
{ | ||
Assert.Throws<ArgumentOutOfRangeException>(() => _ = new ShaderStorageBufferObjectStack<int>(new DummyRenderer(), 1, 100)); | ||
Assert.Throws<ArgumentOutOfRangeException>(() => _ = new ShaderStorageBufferObjectStack<int>(new DummyRenderer(), 100, 1)); | ||
Assert.DoesNotThrow(() => _ = new ShaderStorageBufferObjectStack<int>(new DummyRenderer(), 2, 100)); | ||
Assert.DoesNotThrow(() => _ = new ShaderStorageBufferObjectStack<int>(new DummyRenderer(), 100, 2)); | ||
} | ||
|
||
[Test] | ||
public void TestInitialState() | ||
{ | ||
Assert.That(stack.CurrentOffset, Is.Zero); | ||
Assert.That(stack.CurrentBuffer, Is.Not.Null); | ||
Assert.That(stack.CurrentBuffer[stack.CurrentOffset], Is.EqualTo(0)); | ||
} | ||
|
||
[Test] | ||
public void TestPopWithNoItems() | ||
{ | ||
Assert.Throws<InvalidOperationException>(() => stack.Pop()); | ||
} | ||
|
||
[Test] | ||
public void TestAddInitialItem() | ||
{ | ||
var firstBuffer = stack.CurrentBuffer; | ||
|
||
stack.Push(1); | ||
|
||
Assert.That(stack.CurrentOffset, Is.Zero); | ||
Assert.That(stack.CurrentBuffer, Is.EqualTo(firstBuffer)); | ||
Assert.That(stack.CurrentBuffer[stack.CurrentOffset], Is.EqualTo(1)); | ||
} | ||
|
||
[Test] | ||
public void TestPushToFillOneBuffer() | ||
{ | ||
var firstBuffer = stack.CurrentBuffer; | ||
int expectedIndex = 0; | ||
|
||
for (int i = 0; i < size; i++) | ||
{ | ||
stack.Push(i); | ||
Assert.That(stack.CurrentOffset, Is.EqualTo(expectedIndex++)); | ||
Assert.That(stack.CurrentBuffer, Is.EqualTo(firstBuffer)); | ||
Assert.That(stack.CurrentBuffer[stack.CurrentOffset], Is.EqualTo(i)); | ||
} | ||
} | ||
|
||
[Test] | ||
public void TestPopEntireBuffer() | ||
{ | ||
for (int i = 0; i < size; i++) | ||
stack.Push(i); | ||
|
||
var firstBuffer = stack.CurrentBuffer; | ||
|
||
for (int i = size - 1; i >= 0; i--) | ||
{ | ||
Assert.That(stack.CurrentOffset, Is.EqualTo(i)); | ||
Assert.That(stack.CurrentBuffer, Is.EqualTo(firstBuffer)); | ||
Assert.That(stack.CurrentBuffer[stack.CurrentOffset], Is.EqualTo(i)); | ||
stack.Pop(); | ||
} | ||
} | ||
|
||
[Test] | ||
public void TestTransitionToBufferOnPush() | ||
{ | ||
for (int i = 0; i < size; i++) | ||
stack.Push(i); | ||
|
||
var firstBuffer = stack.CurrentBuffer; | ||
int copiedItem = stack.CurrentBuffer[stack.CurrentOffset]; | ||
|
||
// Transition to a new buffer... | ||
stack.Push(size); | ||
Assert.That(stack.CurrentBuffer, Is.Not.EqualTo(firstBuffer)); | ||
|
||
// ... where the "hack" employed by the queue means that after a transition, the new item is added at index 1... | ||
Assert.That(stack.CurrentOffset, Is.EqualTo(1)); | ||
Assert.That(stack.CurrentBuffer[1], Is.EqualTo(size)); | ||
|
||
// ... and the first item in the new buffer is a copy of the last referenced item before the push. | ||
Assert.That(stack.CurrentBuffer[0], Is.EqualTo(copiedItem)); | ||
} | ||
|
||
[Test] | ||
public void TestTransitionToBufferOnPop() | ||
{ | ||
for (int i = 0; i < size; i++) | ||
stack.Push(i); | ||
|
||
var firstBuffer = stack.CurrentBuffer; | ||
int copiedItem = stack.CurrentBuffer[stack.CurrentOffset]; | ||
|
||
// Transition to the new buffer. | ||
stack.Push(size); | ||
|
||
// The "hack" employed means that on the first pop, the index moves to the 0th index in the new buffer. | ||
stack.Pop(); | ||
Assert.That(stack.CurrentBuffer, Is.Not.EqualTo(firstBuffer)); | ||
Assert.That(stack.CurrentOffset, Is.Zero); | ||
Assert.That(stack.CurrentBuffer[stack.CurrentOffset], Is.EqualTo(copiedItem)); | ||
|
||
// After a subsequent pop, we transition to the previous buffer and move to the index prior to the copied item. | ||
// We've already seen the copied item in the new buffer with the above pop, so we should not see it again here. | ||
stack.Pop(); | ||
Assert.That(stack.CurrentBuffer, Is.EqualTo(firstBuffer)); | ||
Assert.That(stack.CurrentOffset, Is.EqualTo(copiedItem - 1)); | ||
Assert.That(stack.CurrentBuffer[stack.CurrentOffset], Is.EqualTo(copiedItem - 1)); | ||
|
||
// Popping once again should move the index further backwards. | ||
stack.Pop(); | ||
Assert.That(stack.CurrentBuffer, Is.EqualTo(firstBuffer)); | ||
Assert.That(stack.CurrentOffset, Is.EqualTo(copiedItem - 2)); | ||
} | ||
|
||
[Test] | ||
public void TestTransitionToAndFromNewBufferFromMiddle() | ||
{ | ||
for (int i = 0; i < size; i++) | ||
stack.Push(i); | ||
|
||
// Move to the middle of the current buffer (it can not take up any new items at this point). | ||
stack.Pop(); | ||
stack.Pop(); | ||
|
||
var firstBuffer = stack.CurrentBuffer; | ||
int copiedItem = stack.CurrentOffset; | ||
|
||
// Transition to the new buffer... | ||
stack.Push(size); | ||
|
||
// ... and as above, we arrive at index 1 in the new buffer. | ||
Assert.That(stack.CurrentBuffer, Is.Not.EqualTo(firstBuffer)); | ||
Assert.That(stack.CurrentOffset, Is.EqualTo(1)); | ||
Assert.That(stack.CurrentBuffer[1], Is.EqualTo(size)); | ||
Assert.That(stack.CurrentBuffer[0], Is.EqualTo(copiedItem)); | ||
|
||
// Transition to the previous buffer... | ||
stack.Pop(); | ||
stack.Pop(); | ||
|
||
// ... noting that this is the same as the above "normal" pop case, except that item arrived at is in the middle of the previous buffer. | ||
Assert.That(stack.CurrentBuffer, Is.EqualTo(firstBuffer)); | ||
Assert.That(stack.CurrentOffset, Is.EqualTo(copiedItem - 1)); | ||
Assert.That(stack.CurrentBuffer[stack.CurrentOffset], Is.EqualTo(copiedItem - 1)); | ||
|
||
// Popping once again from this state should move further backwards. | ||
stack.Pop(); | ||
Assert.That(stack.CurrentBuffer, Is.EqualTo(firstBuffer)); | ||
Assert.That(stack.CurrentOffset, Is.EqualTo(copiedItem - 2)); | ||
} | ||
|
||
[Test] | ||
public void TestMoveToAndFromMiddleOfNewBuffer() | ||
{ | ||
for (int i = 0; i < size; i++) | ||
stack.Push(i); | ||
|
||
var lastBuffer = stack.CurrentBuffer; | ||
int copiedItem1 = stack.CurrentBuffer[stack.CurrentOffset]; | ||
|
||
// Transition to the middle of the new buffer. | ||
stack.Push(size); | ||
stack.Push(size + 1); | ||
Assert.That(stack.CurrentBuffer, Is.Not.EqualTo(lastBuffer)); | ||
Assert.That(stack.CurrentOffset, Is.EqualTo(2)); | ||
Assert.That(stack.CurrentBuffer[2], Is.EqualTo(size + 1)); | ||
Assert.That(stack.CurrentBuffer[1], Is.EqualTo(size)); | ||
Assert.That(stack.CurrentBuffer[0], Is.EqualTo(copiedItem1)); | ||
|
||
// Transition to the previous buffer. | ||
stack.Pop(); | ||
stack.Pop(); | ||
stack.Pop(); | ||
Assert.That(stack.CurrentBuffer, Is.EqualTo(lastBuffer)); | ||
|
||
// The item that will be copied into the new buffer. | ||
int copiedItem2 = stack.CurrentBuffer[stack.CurrentOffset]; | ||
|
||
// Transition to the new buffer... | ||
stack.Push(size + 2); | ||
Assert.That(stack.CurrentBuffer, Is.Not.EqualTo(lastBuffer)); | ||
|
||
// ... noting that this is the same as the normal case of transitioning to a new buffer, except arriving in the middle of it... | ||
Assert.That(stack.CurrentOffset, Is.EqualTo(4)); | ||
Assert.That(stack.CurrentBuffer[4], Is.EqualTo(size + 2)); | ||
|
||
// ... where this is the copied item as a result of the immediate push... | ||
Assert.That(stack.CurrentBuffer[3], Is.EqualTo(copiedItem2)); | ||
|
||
// ... and these are the same items from the first pushes above. | ||
Assert.That(stack.CurrentBuffer[2], Is.EqualTo(size + 1)); | ||
Assert.That(stack.CurrentBuffer[1], Is.EqualTo(size)); | ||
Assert.That(stack.CurrentBuffer[0], Is.EqualTo(copiedItem1)); | ||
|
||
// Transition to the previous buffer... | ||
stack.Pop(); | ||
stack.Pop(); | ||
Assert.That(stack.CurrentBuffer, Is.EqualTo(lastBuffer)); | ||
|
||
// ... but this one's a little tricky. The entire process up to this point is: | ||
// 1. From index N-1 -> transition to new buffer. | ||
// 2. Transition to old buffer, arrive at index N-2 (N-1 was copied into the new buffer). | ||
// 3. From index N-2 -> transition to new buffer. | ||
// 4. Transition to old buffer, arrive at index N-3 (N-2 was copied into the new buffer). | ||
Assert.That(stack.CurrentOffset, Is.EqualTo(size - 3)); | ||
Assert.That(stack.CurrentBuffer[stack.CurrentOffset], Is.EqualTo(size - 3)); | ||
} | ||
|
||
[Test] | ||
public void TestTransitionFromEmptyStack() | ||
{ | ||
for (int i = 0; i < size * 2; i++) | ||
{ | ||
var lastBuffer = stack.CurrentBuffer; | ||
|
||
// Push one item. | ||
stack.Push(i); | ||
|
||
// On a buffer transition, test that the item at the 0-th index of the first buffer was correct copied to the new buffer. | ||
if (stack.CurrentBuffer != lastBuffer) | ||
Assert.That(stack.CurrentBuffer[stack.CurrentOffset - 1], Is.EqualTo(0)); | ||
|
||
// Test that the item was correctly placed in the new buffer | ||
Assert.That(stack.CurrentBuffer[stack.CurrentOffset], Is.EqualTo(i)); | ||
|
||
// Return to an empty stack. | ||
stack.Pop(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#ifndef SSBO_TEST_FS | ||
#define SSBO_TEST_FS | ||
|
||
#extension GL_ARB_shader_storage_buffer_object : enable | ||
|
||
struct ColourData | ||
{ | ||
vec4 Colour; | ||
}; | ||
|
||
#ifndef OSU_GRAPHICS_NO_SSBO | ||
|
||
layout(std140, set = 0, binding = 0) readonly buffer g_ColourBuffer | ||
{ | ||
ColourData Data[]; | ||
} ColourBuffer; | ||
|
||
#else // OSU_GRAPHICS_NO_SSBO | ||
|
||
layout(std140, set = 0, binding = 0) uniform g_ColourBuffer | ||
{ | ||
ColourData Data[64]; | ||
} ColourBuffer; | ||
|
||
#endif // OSU_GRAPHICS_NO_SSBO | ||
|
||
layout(location = 0) flat in int v_ColourIndex; | ||
layout(location = 0) out vec4 o_Colour; | ||
|
||
void main(void) | ||
{ | ||
o_Colour = ColourBuffer.Data[v_ColourIndex].Colour; | ||
} | ||
|
||
#endif // SSBO_TEST_FS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef SSBO_TEST_VS | ||
#define SSBO_TEST_VS | ||
|
||
layout(location = 0) in highp vec2 m_Position; | ||
layout(location = 1) in int m_ColourIndex; | ||
|
||
layout(location = 0) flat out int v_ColourIndex; | ||
|
||
void main(void) | ||
{ | ||
v_ColourIndex = m_ColourIndex; | ||
gl_Position = g_ProjMatrix * vec4(m_Position, 1.0, 1.0); | ||
} | ||
|
||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, I don't get this. These items were popped, so why are they still in the buffer in any way? I'd expect them to get overwritten?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SSBOStack never overwrites data because it doesn't know if a draw call has been launched for the previous data. I'm also unsure whether it would be more expensive to overwrite said data while it's in use by the GPU, in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay I guess I somehow didn't read xmldoc so that's my bad.
I do feel like the current xmldoc is leaving things half-stated, because if I'm understanding correctly, you would generally call
.Clear()
at the start of every draw call to reclaim all of the buffer space and prevent runaway VRAM usage, right? I'd spell that out explicitly there.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that sounds about right... I do wonder if this is yet another one of those things that should be "automagically" handled for you like every other instance of
ResetCounters()
or whatever (VBOs/UBOs...).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've also added some XMLDoc to
.Clear()
, but if you have any better suggestions then I'm all ears. Proper documentation without requiring developers to have white-box knowledge about everything, is still one of the most difficult things for me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably okay as is. The main reference as to how the thing should be used is the test, and it spells things out pretty clearly.
There are things you could do to force the consumers to do the clear every frame by API design but I'm not sure it's worth the hassle and implementation overheads.