/
ProcessingShader.compute
63 lines (50 loc) · 1.81 KB
/
ProcessingShader.compute
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
#pragma kernel NormalizeImage
#pragma kernel CropImage
#pragma kernel FlipXAxis
// Input image texture
Texture2D<float4> _InputImage;
// Output image texture
RWTexture2D<float4> _OutputImage;
// Structured buffer to hold the mean values for each color channel (r, g, b)
RWStructuredBuffer<float> _Mean;
// Structured buffer to hold the standard deviation values for each color channel (r, g, b)
RWStructuredBuffer<float> _Std;
// Float variable that represents the scaling factor to apply to the normalized pixel values
float _Scale;
// The (x, y) coordinates of the top-left corner of the cropping region
int2 _CropOffset;
// The size (width, height) of the cropping region
int2 _CropSize;
// Normalize the input image
[numthreads(8, 8, 1)]
void NormalizeImage(uint3 id : SV_DispatchThreadID)
{
float4 inputPixel = _InputImage[id.xy];
// Create float4 variables for mean and standard deviation
float4 mean = float4(_Mean[0], _Mean[1], _Mean[2], 0.0);
float4 std = float4(_Std[0], _Std[1], _Std[2], 1.0);
float4 normalizedPixel = (inputPixel - mean) / std;
// Apply scaling and leave the alpha channel unchanged
_OutputImage[id.xy] = float4(normalizedPixel.rgb * _Scale, inputPixel.a);
}
// Crop the input image
[numthreads(8, 8, 1)]
void CropImage(uint3 id : SV_DispatchThreadID)
{
if (id.x < (uint)_CropSize.x && id.y < (uint)_CropSize.y)
{
int2 inputPos = id.xy + _CropOffset;
_OutputImage[id.xy] = _InputImage[inputPos];
}
}
// Flip the input image around the x-axis
[numthreads(8, 8, 1)]
void FlipXAxis(uint3 id : SV_DispatchThreadID)
{
uint width;
uint height;
_InputImage.GetDimensions(width, height);
// Compute the flipped pixel coordinates
int2 flippedCoords = int2(id.x, height - id.y - 1);
_OutputImage[id.xy] = _InputImage[flippedCoords];
}