forked from timdecode/UnrealComputeShaderExample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComputeShaderTestComponent.h
105 lines (70 loc) · 2.52 KB
/
ComputeShaderTestComponent.h
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "GlobalShader.h"
#include "UniformBuffer.h"
#include "RHICommandList.h"
#include <atomic>
#include "ComputeShaderTestComponent.generated.h"
BEGIN_GLOBAL_SHADER_PARAMETER_STRUCT(FBoidPosition, )
SHADER_PARAMETER(FVector, position)
END_GLOBAL_SHADER_PARAMETER_STRUCT()
BEGIN_GLOBAL_SHADER_PARAMETER_STRUCT(FBoidVelocity, )
SHADER_PARAMETER(FVector, velocity)
END_GLOBAL_SHADER_PARAMETER_STRUCT()
class FComputeShaderDeclaration : public FGlobalShader
{
DECLARE_SHADER_TYPE(FComputeShaderDeclaration, Global);
FComputeShaderDeclaration() {}
explicit FComputeShaderDeclaration(const ShaderMetaType::CompiledShaderInitializerType& Initializer);
static bool ShouldCompilePermutation(const FGlobalShaderPermutationParameters& Parameters) {
return GetMaxSupportedFeatureLevel(Parameters.Platform) >= ERHIFeatureLevel::SM5;
};
static void ModifyCompilationEnvironment(const FGlobalShaderPermutationParameters& Parameters, FShaderCompilerEnvironment& OutEnvironment);
#if ENGINE_MINOR_VERSION < 25
virtual bool Serialize(FArchive& Ar) override
{
bool bShaderHasOutdatedParams = FGlobalShader::Serialize(Ar);
Ar << positions;
Ar << times;
return bShaderHasOutdatedParams;
}
#else
#endif
public:
#if ENGINE_MINOR_VERSION < 25
FShaderResourceParameter positions;
FShaderResourceParameter times;
#else
LAYOUT_FIELD(FShaderResourceParameter, positions);
LAYOUT_FIELD(FShaderResourceParameter, times);
#endif
};
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class COMPUTESHADEREXAMPLE_API UComputeShaderTestComponent : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UComputeShaderTestComponent();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int numBoids = 1000;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float spawnRadius = 600.0f;
UPROPERTY(EditAnywhere, BlueprintReadOnly)
TArray<FVector> outputPositions;
protected:
// GPU side
FStructuredBufferRHIRef _positionBuffer;
FUnorderedAccessViewRHIRef _positionBufferUAV; // we need a UAV for writing
FStructuredBufferRHIRef _timesBuffer;
FUnorderedAccessViewRHIRef _timesBufferUAV;
};