-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy path31_shader_compute.slang
More file actions
73 lines (59 loc) · 1.87 KB
/
31_shader_compute.slang
File metadata and controls
73 lines (59 loc) · 1.87 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
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;
//output.pointCoord = (input.inPosition * float2(1.0, -1.0) + float2(1.0)) / 2.0;
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;
struct ParticleSSBO {
Particle particles;
};
StructuredBuffer<ParticleSSBO> particlesIn;
RWStructuredBuffer<ParticleSSBO> particlesOut;
[shader("compute")]
[numthreads(256,1,1)]
void compMain(uint3 threadId : SV_DispatchThreadID)
{
uint index = threadId.x;
particlesOut[index].particles.position = particlesIn[index].particles.position + particlesIn[index].particles.velocity.xy * ubo.deltaTime;
particlesOut[index].particles.velocity = particlesIn[index].particles.velocity;
// Flip movement at window border
if ((particlesOut[index].particles.position.x <= -1.0) || (particlesOut[index].particles.position.x >= 1.0)) {
particlesOut[index].particles.velocity.x = -particlesOut[index].particles.velocity.x;
}
if ((particlesOut[index].particles.position.y <= -1.0) || (particlesOut[index].particles.position.y >= 1.0)) {
particlesOut[index].particles.velocity.y = -particlesOut[index].particles.velocity.y;
}
}