init
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
/* Copyright (c) 2024, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
Texture2D textureColorMap : register(t0);
|
||||
SamplerState samplerColorMap : register(s0);
|
||||
Texture2D textureGradientRamp : register(t1);
|
||||
SamplerState samplerGradientRamp : register(s1);
|
||||
|
||||
struct VSOutput
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
[[vk::location(0)]] float GradientPos : POSITION0;
|
||||
[[vk::location(1)]] float2 CenterPos : POSITION1;
|
||||
[[vk::builtin("PointSize")]] float PSize : PSIZE;
|
||||
[[vk::location(2)]] float PointSize : TEXCOORD0;
|
||||
};
|
||||
|
||||
float4 main(VSOutput input) : SV_TARGET
|
||||
{
|
||||
float3 color = textureGradientRamp.Sample(samplerGradientRamp, float2(input.GradientPos, 0.0)).rgb;
|
||||
float2 PointCoord = (input.Pos.xy - input.CenterPos.xy) / input.PointSize + 0.5;
|
||||
return float4(textureColorMap.Sample(samplerColorMap, PointCoord).rgb * color, 1);
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,56 @@
|
||||
/* Copyright (c) 2024, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
struct VSInput
|
||||
{
|
||||
[[vk::location(0)]] float4 Pos : POSITION0;
|
||||
[[vk::location(1)]] float4 Vel : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct VSOutput
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
[[vk::location(0)]] float GradientPos : POSITION3;
|
||||
[[vk::location(1)]] float2 CenterPos : POSITION1;
|
||||
[[vk::builtin("PointSize")]] float PSize : PSIZE;
|
||||
[[vk::location(2)]] float PointSize : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct UBO
|
||||
{
|
||||
float4x4 projection;
|
||||
float4x4 modelview;
|
||||
float2 screendim;
|
||||
};
|
||||
[[vk::binding(2, 0)]]
|
||||
ConstantBuffer<UBO> ubo : register(b2);
|
||||
|
||||
VSOutput main(VSInput input)
|
||||
{
|
||||
VSOutput output = (VSOutput) 0;
|
||||
const float spriteSize = 0.005 * input.Pos.w; // Point size influenced by mass (stored in input.Pos.w);
|
||||
|
||||
float4 eyePos = mul(ubo.modelview, float4(input.Pos.x, input.Pos.y, input.Pos.z, 1.0));
|
||||
float4 projectedCorner = mul(ubo.projection, float4(0.5 * spriteSize, 0.5 * spriteSize, eyePos.z, eyePos.w));
|
||||
output.PSize = output.PointSize = clamp(ubo.screendim.x * projectedCorner.x / projectedCorner.w, 1.0, 128.0);
|
||||
|
||||
output.Pos = mul(ubo.projection, eyePos);
|
||||
output.CenterPos = ((output.Pos.xy / output.Pos.w) + 1.0) * 0.5 * ubo.screendim;
|
||||
|
||||
output.GradientPos = input.Vel.w;
|
||||
return output;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,92 @@
|
||||
/* Copyright (c) 2024, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
// Copyright 2020 Google LLC
|
||||
// Copyright 2023 Sascha Willems
|
||||
|
||||
struct Particle
|
||||
{
|
||||
float4 pos;
|
||||
float4 vel;
|
||||
};
|
||||
[[vk::binding(0, 0)]]
|
||||
RWStructuredBuffer<Particle> particles : register(u0);
|
||||
|
||||
struct UBO
|
||||
{
|
||||
float deltaT;
|
||||
int particleCount;
|
||||
};
|
||||
[[vk::binding(1, 0)]]
|
||||
ConstantBuffer<UBO> ubo : register(b1);
|
||||
|
||||
// We need to define an upper bound as HLSl doesn't support variable length arrays
|
||||
#define MAX_SHARED_DATA_SIZE 1024
|
||||
[[vk::constant_id(1)]] const int SHARED_DATA_SIZE = 512;
|
||||
[[vk::constant_id(2)]] const float GRAVITY = 0.002;
|
||||
[[vk::constant_id(3)]] const float POWER = 0.75;
|
||||
[[vk::constant_id(4)]] const float SOFTEN = 0.05;
|
||||
|
||||
// Share data between computer shader invocations to speed up caluclations
|
||||
groupshared float4 sharedData[MAX_SHARED_DATA_SIZE];
|
||||
|
||||
#define TIME_FACTOR 0.05
|
||||
|
||||
[numthreads(256, 1, 1)]
|
||||
void main(uint3 GlobalInvocationID : SV_DispatchThreadID, uint3 LocalInvocationID : SV_GroupThreadID)
|
||||
{
|
||||
// Current SSBO index
|
||||
uint index = GlobalInvocationID.x;
|
||||
if (index >= ubo.particleCount)
|
||||
return;
|
||||
|
||||
float4 position = particles[index].pos;
|
||||
float4 velocity = particles[index].vel;
|
||||
float4 acceleration = float4(0, 0, 0, 0);
|
||||
|
||||
for (int i = 0; i < ubo.particleCount; i += SHARED_DATA_SIZE)
|
||||
{
|
||||
if (i + LocalInvocationID.x < ubo.particleCount)
|
||||
{
|
||||
sharedData[LocalInvocationID.x] = particles[i + LocalInvocationID.x].pos;
|
||||
}
|
||||
else
|
||||
{
|
||||
sharedData[LocalInvocationID.x] = float4(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
GroupMemoryBarrierWithGroupSync();
|
||||
|
||||
for (int j = 0; j < 256; j++)
|
||||
{
|
||||
float4 other = sharedData[j];
|
||||
float3 len = other.xyz - position.xyz;
|
||||
acceleration.xyz += GRAVITY * len * other.w / pow(dot(len, len) + SOFTEN, POWER);
|
||||
}
|
||||
|
||||
GroupMemoryBarrierWithGroupSync();
|
||||
}
|
||||
|
||||
particles[index].vel.xyz += ubo.deltaT * TIME_FACTOR * acceleration.xyz;
|
||||
|
||||
// Gradient texture position
|
||||
particles[index].vel.w += 0.1 * TIME_FACTOR * ubo.deltaT;
|
||||
if (particles[index].vel.w > 1.0)
|
||||
{
|
||||
particles[index].vel.w -= 1.0;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,44 @@
|
||||
/* Copyright (c) 2024, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
struct Particle
|
||||
{
|
||||
float4 pos;
|
||||
float4 vel;
|
||||
};
|
||||
[[vk::binding(0, 0)]]
|
||||
RWStructuredBuffer<Particle> particles : register(u0);
|
||||
|
||||
struct UBO
|
||||
{
|
||||
float deltaT;
|
||||
int particleCount;
|
||||
};
|
||||
[[vk::binding(1, 0)]]
|
||||
ConstantBuffer<UBO> ubo : register(b1);
|
||||
|
||||
#define TIME_FACTOR 0.05
|
||||
|
||||
[numthreads(256, 1, 1)]
|
||||
void main(uint3 GlobalInvocationID : SV_DispatchThreadID)
|
||||
{
|
||||
int index = int(GlobalInvocationID.x);
|
||||
float4 position = particles[index].pos;
|
||||
float4 velocity = particles[index].vel;
|
||||
position += ubo.deltaT * TIME_FACTOR * velocity;
|
||||
particles[index].pos = position;
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user