diff --git a/Content/Shaders/Compiled/DXIL/SpriteBatch.comp.dxil b/Content/Shaders/Compiled/DXIL/SpriteBatch.comp.dxil index a787535..285804d 100644 Binary files a/Content/Shaders/Compiled/DXIL/SpriteBatch.comp.dxil and b/Content/Shaders/Compiled/DXIL/SpriteBatch.comp.dxil differ diff --git a/Content/Shaders/Compiled/MSL/SpriteBatch.comp.msl b/Content/Shaders/Compiled/MSL/SpriteBatch.comp.msl index a033b5b..d02192a 100644 --- a/Content/Shaders/Compiled/MSL/SpriteBatch.comp.msl +++ b/Content/Shaders/Compiled/MSL/SpriteBatch.comp.msl @@ -3,6 +3,11 @@ using namespace metal; +struct type_UBO +{ + float time; +}; + struct SpriteComputeData { packed_float3 position; @@ -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; } diff --git a/Content/Shaders/Compiled/SPIRV/SpriteBatch.comp.spv b/Content/Shaders/Compiled/SPIRV/SpriteBatch.comp.spv index 2924b62..c540c02 100644 Binary files a/Content/Shaders/Compiled/SPIRV/SpriteBatch.comp.spv and b/Content/Shaders/Compiled/SPIRV/SpriteBatch.comp.spv differ diff --git a/Content/Shaders/Source/SpriteBatch.comp.hlsl b/Content/Shaders/Source/SpriteBatch.comp.hlsl index 9c70d49..42c56b5 100644 --- a/Content/Shaders/Source/SpriteBatch.comp.hlsl +++ b/Content/Shaders/Source/SpriteBatch.comp.hlsl @@ -13,6 +13,11 @@ struct SpriteVertex float4 color; }; +cbuffer UBO : register(b0, space2) +{ + float time : packoffset(c0); +}; + StructuredBuffer ComputeBuffer : register(t0, space0); RWStructuredBuffer VertexBuffer : register(u0, space1); @@ -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; } diff --git a/Examples/ComputeSpriteBatch.c b/Examples/ComputeSpriteBatch.c index d204a35..9d0d171 100644 --- a/Examples/ComputeSpriteBatch.c +++ b/Examples/ComputeSpriteBatch.c @@ -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) { @@ -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 @@ -292,6 +295,7 @@ static int Init(Context* context) static int Update(Context* context) { + t += 0.1f; return 0; } @@ -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);