-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy path37_shader_compute.slang
More file actions
81 lines (69 loc) · 2.31 KB
/
37_shader_compute.slang
File metadata and controls
81 lines (69 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
struct VSInput {
float2 inPosition;
float4 inColor;
};
struct VSOutput
{
float4 pos : SV_Position;
float pointSize : SV_PointSize;
float3 fragColor : COLOR0;
};
struct PSInput
{
float4 pos : SV_POSITION;
float3 fragColor : COLOR0;
float2 pointCoord : SV_PointCoord;
};
[shader("vertex")]
VSOutput vertMain(VSInput input) {
VSOutput output;
output.pointSize = 14.0;
output.pos = float4(input.inPosition, 1.0, 1.0);
output.fragColor = input.inColor.rgb;
return output;
}
[shader("fragment")]
float4 fragMain(PSInput input) : SV_TARGET {
float2 coord = input.pointCoord - float2(0.5);
return float4(input.fragColor, 0.5 - length(coord));
}
struct Particle {
float2 position;
float2 velocity;
float4 color;
};
struct UniformBuffer {
float deltaTime;
};
ConstantBuffer<UniformBuffer> ubo;
// Push constants for particle group information
struct PushConstants {
uint startIndex;
uint count;
};
[[vk::push_constant]] PushConstants pushConstants;
struct ParticleSSBO {
Particle particles;
};
StructuredBuffer<ParticleSSBO> particlesIn;
RWStructuredBuffer<ParticleSSBO> particlesOut;
[shader("compute")]
[numthreads(256,1,1)]
void compMain(uint3 threadId : SV_DispatchThreadID)
{
// Calculate the global particle index by adding the thread ID to the start index
uint localIndex = threadId.x;
// Only process particles within the assigned range
if (localIndex < pushConstants.count) {
uint globalIndex = pushConstants.startIndex + localIndex;
particlesOut[globalIndex].particles.position = particlesIn[globalIndex].particles.position + particlesIn[globalIndex].particles.velocity.xy * ubo.deltaTime;
particlesOut[globalIndex].particles.velocity = particlesIn[globalIndex].particles.velocity;
// Flip movement at window border
if ((particlesOut[globalIndex].particles.position.x <= -1.0) || (particlesOut[globalIndex].particles.position.x >= 1.0)) {
particlesOut[globalIndex].particles.velocity.x = -particlesOut[globalIndex].particles.velocity.x;
}
if ((particlesOut[globalIndex].particles.position.y <= -1.0) || (particlesOut[globalIndex].particles.position.y >= 1.0)) {
particlesOut[globalIndex].particles.velocity.y = -particlesOut[globalIndex].particles.velocity.y;
}
}
}