Compute shaders
This commit is contained in:
66
shaders/31_shader_compute.comp.slang
Normal file
66
shaders/31_shader_compute.comp.slang
Normal file
@@ -0,0 +1,66 @@
|
||||
struct VSInput {
|
||||
float2 inPosition;
|
||||
float3 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;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user