Skip to content

Examples: ComputeSpriteBatch: add uniform buffer #49

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Binary file modified Content/Shaders/Compiled/DXIL/SpriteBatch.comp.dxil
Binary file not shown.
48 changes: 27 additions & 21 deletions Content/Shaders/Compiled/MSL/SpriteBatch.comp.msl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

using namespace metal;

struct type_UBO
{
float time;
};

struct SpriteComputeData
{
packed_float3 position;
Expand All @@ -28,27 +33,28 @@ struct type_RWStructuredBuffer_SpriteVertex
SpriteVertex _m0[1];
};

kernel void main0(const device type_StructuredBuffer_SpriteComputeData& ComputeBuffer [[buffer(0)]], device type_RWStructuredBuffer_SpriteVertex& VertexBuffer [[buffer(1)]], uint3 gl_GlobalInvocationID [[thread_position_in_grid]])
kernel void main0(constant type_UBO& UBO [[buffer(0)]], const device type_StructuredBuffer_SpriteComputeData& ComputeBuffer [[buffer(1)]], device type_RWStructuredBuffer_SpriteVertex& VertexBuffer [[buffer(2)]], uint3 gl_GlobalInvocationID [[thread_position_in_grid]])
{
SpriteComputeData _52 = ComputeBuffer._m0[gl_GlobalInvocationID.x];
float _62 = cos(_52.rotation);
float _63 = sin(_52.rotation);
float4x4 _74 = (float4x4(float4(1.0, 0.0, 0.0, 0.0), float4(0.0, 1.0, 0.0, 0.0), float4(0.0, 0.0, 1.0, 0.0), float4(_52.position[0], _52.position[1], _52.position[2], 1.0)) * float4x4(float4(_62, _63, 0.0, 0.0), float4(-_63, _62, 0.0, 0.0), float4(0.0, 0.0, 1.0, 0.0), float4(0.0, 0.0, 0.0, 1.0))) * float4x4(float4(_52.scale.x, 0.0, 0.0, 0.0), float4(0.0, _52.scale.y, 0.0, 0.0), float4(0.0, 0.0, 1.0, 0.0), float4(0.0, 0.0, 0.0, 1.0));
uint _76 = gl_GlobalInvocationID.x * 4u;
VertexBuffer._m0[_76].position = _74 * float4(0.0, 0.0, 0.0, 1.0);
uint _79 = _76 + 1u;
VertexBuffer._m0[_79].position = _74 * float4(1.0, 0.0, 0.0, 1.0);
uint _82 = _76 + 2u;
VertexBuffer._m0[_82].position = _74 * float4(0.0, 1.0, 0.0, 1.0);
uint _85 = _76 + 3u;
VertexBuffer._m0[_85].position = _74 * float4(1.0, 1.0, 0.0, 1.0);
VertexBuffer._m0[_76].texcoord = float2(0.0);
VertexBuffer._m0[_79].texcoord = float2(1.0, 0.0);
VertexBuffer._m0[_82].texcoord = float2(0.0, 1.0);
VertexBuffer._m0[_85].texcoord = float2(1.0);
VertexBuffer._m0[_76].color = _52.color;
VertexBuffer._m0[_79].color = _52.color;
VertexBuffer._m0[_82].color = _52.color;
VertexBuffer._m0[_85].color = _52.color;
SpriteComputeData _56 = ComputeBuffer._m0[gl_GlobalInvocationID.x];
float _66 = cos(_56.rotation);
float _67 = sin(_56.rotation);
float4x4 _78 = (float4x4(float4(1.0, 0.0, 0.0, 0.0), float4(0.0, 1.0, 0.0, 0.0), float4(0.0, 0.0, 1.0, 0.0), float4(_56.position[0], _56.position[1], _56.position[2], 1.0)) * float4x4(float4(_66, _67, 0.0, 0.0), float4(-_67, _66, 0.0, 0.0), float4(0.0, 0.0, 1.0, 0.0), float4(0.0, 0.0, 0.0, 1.0))) * float4x4(float4(_56.scale.x, 0.0, 0.0, 0.0), float4(0.0, _56.scale.y, 0.0, 0.0), float4(0.0, 0.0, 1.0, 0.0), float4(0.0, 0.0, 0.0, 1.0));
uint _80 = gl_GlobalInvocationID.x * 4u;
VertexBuffer._m0[_80].position = _78 * float4(0.0, 0.0, 0.0, 1.0);
uint _83 = _80 + 1u;
VertexBuffer._m0[_83].position = _78 * float4(1.0, 0.0, 0.0, 1.0);
uint _86 = _80 + 2u;
VertexBuffer._m0[_86].position = _78 * float4(0.0, 1.0, 0.0, 1.0);
uint _89 = _80 + 3u;
VertexBuffer._m0[_89].position = _78 * float4(1.0, 1.0, 0.0, 1.0);
VertexBuffer._m0[_80].texcoord = float2(0.0);
VertexBuffer._m0[_83].texcoord = float2(1.0, 0.0);
VertexBuffer._m0[_86].texcoord = float2(0.0, 1.0);
VertexBuffer._m0[_89].texcoord = float2(1.0);
float4 _98 = _56.color * sin(UBO.time);
VertexBuffer._m0[_80].color = _98;
VertexBuffer._m0[_83].color = _98;
VertexBuffer._m0[_86].color = _98;
VertexBuffer._m0[_89].color = _98;
}

Binary file modified Content/Shaders/Compiled/SPIRV/SpriteBatch.comp.spv
Binary file not shown.
14 changes: 10 additions & 4 deletions Content/Shaders/Source/SpriteBatch.comp.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ struct SpriteVertex
float4 color;
};

cbuffer UBO : register(b0, space2)
{
float time : packoffset(c0);
};

StructuredBuffer<SpriteComputeData> ComputeBuffer : register(t0, space0);
RWStructuredBuffer<SpriteVertex> VertexBuffer : register(u0, space1);

Expand Down Expand Up @@ -64,8 +69,9 @@ void main(uint3 GlobalInvocationID : SV_DispatchThreadID)
VertexBuffer[n * 4u + 2].texcoord = float2(0.0f, 1.0f);
VertexBuffer[n * 4u + 3].texcoord = float2(1.0f, 1.0f);

VertexBuffer[n * 4u] .color = currentSpriteData.color;
VertexBuffer[n * 4u + 1].color = currentSpriteData.color;
VertexBuffer[n * 4u + 2].color = currentSpriteData.color;
VertexBuffer[n * 4u + 3].color = currentSpriteData.color;
float st = sin(time);
VertexBuffer[n * 4u] .color = currentSpriteData.color * st;
VertexBuffer[n * 4u + 1].color = currentSpriteData.color * st;
VertexBuffer[n * 4u + 2].color = currentSpriteData.color * st;
VertexBuffer[n * 4u + 3].color = currentSpriteData.color * st;
}
7 changes: 6 additions & 1 deletion Examples/ComputeSpriteBatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ typedef struct ComputeSpriteInstance
float r, g, b, a;
} ComputeSpriteInstance;

const Uint32 SPRITE_COUNT = 8192;
const Uint32 SPRITE_COUNT = 819200;

static float t;

static int Init(Context* context)
{
Expand Down Expand Up @@ -131,6 +133,7 @@ static int Init(Context* context)
&(SDL_GPUComputePipelineCreateInfo){
.num_readonly_storage_buffers = 1,
.num_readwrite_storage_buffers = 1,
.num_uniform_buffers = 1,
.threadcount_x = 64,
.threadcount_y = 1,
.threadcount_z = 1
Expand Down Expand Up @@ -292,6 +295,7 @@ static int Init(Context* context)

static int Update(Context* context)
{
t += 0.1f;
return 0;
}

Expand Down Expand Up @@ -383,6 +387,7 @@ static int Draw(Context* context)
},
1
);
SDL_PushGPUComputeUniformData(cmdBuf, 0, &t, sizeof(float));
SDL_DispatchGPUCompute(computePass, SPRITE_COUNT / 64, 1, 1);

SDL_EndGPUComputePass(computePass);
Expand Down