Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

Commit d31420b

Browse files
authored
Merge pull request #260 from Unity-Technologies/mobile_support
Linking shader works on OpenGL ES 3 on Android
2 parents 6ab57b4 + 533080f commit d31420b

15 files changed

Lines changed: 141 additions & 43 deletions

PostProcessing/Runtime/Effects/ColorGrading.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,9 @@ void RenderHDRPipeline(PostProcessRenderContext context)
224224
break;
225225
}
226226

227-
int groupSize = Mathf.CeilToInt(k_Lut3DSize / 8f);
227+
bool isAndroidOpenGL = Application.platform == RuntimePlatform.Android && SystemInfo.graphicsDeviceType != GraphicsDeviceType.Vulkan;
228+
int groupSizeXY = Mathf.CeilToInt(k_Lut3DSize / 8f);
229+
int groupSizeZ = Mathf.CeilToInt(k_Lut3DSize / (isAndroidOpenGL ? 2f : 8f));
228230
var cmd = context.command;
229231
cmd.SetComputeTextureParam(compute, kernel, "_Output", m_InternalLogLut);
230232
cmd.SetComputeVectorParam(compute, "_Size", new Vector4(k_Lut3DSize, 1f / (k_Lut3DSize - 1f), 0f, 0f));
@@ -287,7 +289,7 @@ void RenderHDRPipeline(PostProcessRenderContext context)
287289

288290
// Generate the lut
289291
context.command.BeginSample("HdrColorGradingLut");
290-
cmd.DispatchCompute(compute, kernel, groupSize, groupSize, groupSize);
292+
cmd.DispatchCompute(compute, kernel, groupSizeXY, groupSizeXY, groupSizeZ);
291293
context.command.EndSample("HdrColorGradingLut");
292294
}
293295

PostProcessing/Runtime/Monitors/HistogramMonitor.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,20 @@ public enum Channel
1818
public Channel channel = Channel.Master;
1919

2020
ComputeBuffer m_Data;
21-
const int k_Bins = 256;
22-
const int k_ThreadGroupSize = 16;
21+
private int numBins;
22+
private int threadGroupSizeX;
23+
private int threadGroupSizeY;
24+
25+
internal override void OnEnable()
26+
{
27+
base.OnEnable();
28+
29+
bool isAndroidOpenGL = Application.platform == RuntimePlatform.Android && SystemInfo.graphicsDeviceType != GraphicsDeviceType.Vulkan;
30+
31+
numBins = isAndroidOpenGL ? 128 : 256;
32+
threadGroupSizeX = isAndroidOpenGL ? 16 : 16;
33+
threadGroupSizeY = isAndroidOpenGL ? 8 : 16;
34+
}
2335

2436
internal override void OnDisable()
2537
{
@@ -41,7 +53,7 @@ internal override void Render(PostProcessRenderContext context)
4153
CheckOutput(width, height);
4254

4355
if (m_Data == null)
44-
m_Data = new ComputeBuffer(k_Bins, sizeof(uint));
56+
m_Data = new ComputeBuffer(numBins, sizeof(uint));
4557

4658
var compute = context.resources.computeShaders.gammaHistogram;
4759
var cmd = context.command;
@@ -50,7 +62,7 @@ internal override void Render(PostProcessRenderContext context)
5062
// Clear the buffer on every frame as we use it to accumulate values on every frame
5163
int kernel = compute.FindKernel("KHistogramClear");
5264
cmd.SetComputeBufferParam(compute, kernel, "_HistogramBuffer", m_Data);
53-
cmd.DispatchCompute(compute, kernel, Mathf.CeilToInt(k_Bins / (float)k_ThreadGroupSize), 1, 1);
65+
cmd.DispatchCompute(compute, kernel, Mathf.CeilToInt(numBins / (float)threadGroupSizeX), 1, 1);
5466

5567
// Gather all pixels and fill in our histogram
5668
kernel = compute.FindKernel("KHistogramGather");
@@ -65,8 +77,8 @@ internal override void Render(PostProcessRenderContext context)
6577
cmd.SetComputeTextureParam(compute, kernel, "_Source", ShaderIDs.HalfResFinalCopy);
6678
cmd.SetComputeBufferParam(compute, kernel, "_HistogramBuffer", m_Data);
6779
cmd.DispatchCompute(compute, kernel,
68-
Mathf.CeilToInt(parameters.x / k_ThreadGroupSize),
69-
Mathf.CeilToInt(parameters.y / k_ThreadGroupSize),
80+
Mathf.CeilToInt(parameters.x / threadGroupSizeX),
81+
Mathf.CeilToInt(parameters.y / threadGroupSizeY),
7082
1
7183
);
7284

PostProcessing/Runtime/Monitors/VectorscopeMonitor.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ public sealed class VectorscopeMonitor : Monitor
99
public float exposure = 0.12f;
1010

1111
ComputeBuffer m_Data;
12-
const int k_ThreadGroupSize = 16;
12+
private int threadGroupSizeX;
13+
private int threadGroupSizeY;
1314

1415
internal override void OnDisable()
1516
{
@@ -20,6 +21,16 @@ internal override void OnDisable()
2021

2122
m_Data = null;
2223
}
24+
25+
internal override void OnEnable()
26+
{
27+
base.OnEnable();
28+
29+
bool isAndroidOpenGL = Application.platform == RuntimePlatform.Android && SystemInfo.graphicsDeviceType != GraphicsDeviceType.Vulkan;
30+
31+
threadGroupSizeX = isAndroidOpenGL ? 16 : 16;
32+
threadGroupSizeY = isAndroidOpenGL ? 8 : 16;
33+
}
2334

2435
internal override bool NeedsHalfRes()
2536
{
@@ -56,8 +67,8 @@ internal override void Render(PostProcessRenderContext context)
5667
cmd.SetComputeBufferParam(compute, kernel, "_VectorscopeBuffer", m_Data);
5768
cmd.SetComputeVectorParam(compute, "_Params", parameters);
5869
cmd.DispatchCompute(compute, kernel,
59-
Mathf.CeilToInt(size / (float)k_ThreadGroupSize),
60-
Mathf.CeilToInt(size / (float)k_ThreadGroupSize),
70+
Mathf.CeilToInt(size / (float)threadGroupSizeX),
71+
Mathf.CeilToInt(size / (float)threadGroupSizeY),
6172
1
6273
);
6374

@@ -66,8 +77,8 @@ internal override void Render(PostProcessRenderContext context)
6677
cmd.SetComputeBufferParam(compute, kernel, "_VectorscopeBuffer", m_Data);
6778
cmd.SetComputeTextureParam(compute, kernel, "_Source", ShaderIDs.HalfResFinalCopy);
6879
cmd.DispatchCompute(compute, kernel,
69-
Mathf.CeilToInt(parameters.x / k_ThreadGroupSize),
70-
Mathf.CeilToInt(parameters.y / k_ThreadGroupSize),
80+
Mathf.CeilToInt(parameters.x / threadGroupSizeX),
81+
Mathf.CeilToInt(parameters.y / threadGroupSizeY),
7182
1
7283
);
7384

PostProcessing/Runtime/Monitors/WaveformMonitor.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ public sealed class WaveformMonitor : Monitor
1010

1111
ComputeBuffer m_Data;
1212

13+
private int threadGroupSize;
14+
private int threadGroupSizeX;
15+
private int threadGroupSizeY;
16+
1317
internal override void OnDisable()
1418
{
1519
base.OnDisable();
@@ -20,6 +24,17 @@ internal override void OnDisable()
2024
m_Data = null;
2125
}
2226

27+
internal override void OnEnable()
28+
{
29+
base.OnEnable();
30+
31+
bool isAndroidOpenGL = Application.platform == RuntimePlatform.Android && SystemInfo.graphicsDeviceType != GraphicsDeviceType.Vulkan;
32+
33+
threadGroupSize = isAndroidOpenGL ? 128 : 256;
34+
threadGroupSizeX = isAndroidOpenGL ? 16 : 16;
35+
threadGroupSizeY = isAndroidOpenGL ? 8 : 16;
36+
}
37+
2338
internal override bool NeedsHalfRes()
2439
{
2540
return true;
@@ -60,7 +75,7 @@ internal override void Render(PostProcessRenderContext context)
6075
int kernel = compute.FindKernel("KWaveformClear");
6176
cmd.SetComputeBufferParam(compute, kernel, "_WaveformBuffer", m_Data);
6277
cmd.SetComputeVectorParam(compute, "_Params", parameters);
63-
cmd.DispatchCompute(compute, kernel, Mathf.CeilToInt(width / 16f), Mathf.CeilToInt(height / 16f), 1);
78+
cmd.DispatchCompute(compute, kernel, Mathf.CeilToInt(width / (float)threadGroupSizeX), Mathf.CeilToInt(height / (float)threadGroupSizeY), 1);
6479

6580
// For performance reasons, especially on consoles, we'll just downscale the source
6681
// again to reduce VMEM stalls. Eventually the whole algorithm needs to be rewritten as
@@ -73,7 +88,7 @@ internal override void Render(PostProcessRenderContext context)
7388
cmd.SetComputeBufferParam(compute, kernel, "_WaveformBuffer", m_Data);
7489
cmd.SetComputeTextureParam(compute, kernel, "_Source", ShaderIDs.WaveformSource);
7590
cmd.SetComputeVectorParam(compute, "_Params", parameters);
76-
cmd.DispatchCompute(compute, kernel, width, Mathf.CeilToInt(height / 256f), 1);
91+
cmd.DispatchCompute(compute, kernel, width, Mathf.CeilToInt(height / (float)threadGroupSize), 1);
7792
cmd.ReleaseTemporaryRT(ShaderIDs.WaveformSource);
7893

7994
// Generate the waveform texture

PostProcessing/Runtime/Utils/LogHistogram.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@ public sealed class LogHistogram
77

88
// Don't forget to update 'ExposureHistogram.hlsl' if you change these values !
99
const int k_Bins = 128;
10-
const int k_ThreadX = 16;
11-
const int k_ThreadY = 16;
10+
int threadX;
11+
int threadY;
1212

1313
public ComputeBuffer data { get; private set; }
1414

1515
public void Generate(PostProcessRenderContext context)
1616
{
1717
if (data == null)
18-
data = new ComputeBuffer(k_Bins, sizeof(uint));
18+
{
19+
bool isAndroidOpenGL = Application.platform == RuntimePlatform.Android && SystemInfo.graphicsDeviceType != GraphicsDeviceType.Vulkan;
20+
threadX = isAndroidOpenGL ? 16 : 16;
21+
threadY = isAndroidOpenGL ? 8 : 16;
22+
data = new ComputeBuffer (k_Bins, sizeof(uint));
23+
}
1924

2025
var scaleOffsetRes = GetHistogramScaleOffsetRes(context);
2126
var compute = context.resources.computeShaders.exposureHistogram;
@@ -25,16 +30,16 @@ public void Generate(PostProcessRenderContext context)
2530
// Clear the buffer on every frame as we use it to accumulate luminance values on each frame
2631
int kernel = compute.FindKernel("KEyeHistogramClear");
2732
cmd.SetComputeBufferParam(compute, kernel, "_HistogramBuffer", data);
28-
cmd.DispatchCompute(compute, kernel, Mathf.CeilToInt(k_Bins / (float)k_ThreadX), 1, 1);
33+
cmd.DispatchCompute(compute, kernel, Mathf.CeilToInt(k_Bins / (float)threadX), 1, 1);
2934

3035
// Get a log histogram
3136
kernel = compute.FindKernel("KEyeHistogram");
3237
cmd.SetComputeBufferParam(compute, kernel, "_HistogramBuffer", data);
3338
cmd.SetComputeTextureParam(compute, kernel, "_Source", context.source);
3439
cmd.SetComputeVectorParam(compute, "_ScaleOffsetRes", scaleOffsetRes);
3540
cmd.DispatchCompute(compute, kernel,
36-
Mathf.CeilToInt(scaleOffsetRes.z / (float)k_ThreadX),
37-
Mathf.CeilToInt(scaleOffsetRes.w / (float)k_ThreadY),
41+
Mathf.CeilToInt(scaleOffsetRes.z / (float)threadX),
42+
Mathf.CeilToInt(scaleOffsetRes.w / (float)threadY),
3843
1
3944
);
4045

PostProcessing/Runtime/Utils/TextureLerper.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ internal Texture Lerp(Texture from, Texture to, float t)
107107

108108
RenderTexture rt = null;
109109

110+
bool isAndroidOpenGL = Application.platform == RuntimePlatform.Android && SystemInfo.graphicsDeviceType != GraphicsDeviceType.Vulkan;
111+
110112
if (is3d)
111113
{
112114
int size = to.width;
@@ -119,8 +121,9 @@ internal Texture Lerp(Texture from, Texture to, float t)
119121
m_Command.SetComputeTextureParam(compute, kernel, "_From", from);
120122
m_Command.SetComputeTextureParam(compute, kernel, "_To", to);
121123

122-
int groupSize = Mathf.CeilToInt(size / 8f);
123-
m_Command.DispatchCompute(compute, kernel, groupSize, groupSize, groupSize);
124+
int groupSizeXY = Mathf.CeilToInt(size / 8f);
125+
int groupSizeZ = Mathf.CeilToInt(size / (isAndroidOpenGL ? 2f : 8f));
126+
m_Command.DispatchCompute(compute, kernel, groupSizeXY, groupSizeXY, groupSizeZ);
124127
}
125128
else
126129
{

PostProcessing/Shaders/Builtins/ExposureHistogram.compute

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ CBUFFER_END
1515
groupshared uint gs_histogram[HISTOGRAM_BINS];
1616

1717
#pragma kernel KEyeHistogram
18-
[numthreads(HISTOGRAM_THREAD_X,HISTOGRAM_THREAD_Y,1)]
18+
[numthreads(HISTOGRAM_THREAD_X, HISTOGRAM_THREAD_Y, 1)]
1919
void KEyeHistogram(uint2 dispatchThreadId : SV_DispatchThreadID, uint2 groupThreadId : SV_GroupThreadID)
2020
{
2121
// Pretty straightforward implementation of histogram gathering using atomic ops.

PostProcessing/Shaders/Builtins/ExposureHistogram.hlsl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66
// Don't forget to update 'AutoExposureRenderer.cs' if you change these values !
77
#define HISTOGRAM_BINS 128
88
#define HISTOGRAM_TEXELS HISTOGRAM_BINS / 4
9-
#define HISTOGRAM_THREAD_X 16
10-
#define HISTOGRAM_THREAD_Y 16
9+
#if SHADER_API_GLES3
10+
#define HISTOGRAM_THREAD_X 16
11+
#define HISTOGRAM_THREAD_Y 8
12+
#else
13+
#define HISTOGRAM_THREAD_X 16
14+
#define HISTOGRAM_THREAD_Y 16
15+
#endif
1116

1217
float GetHistogramBinFromLuminance(float value, float2 scaleOffset)
1318
{

PostProcessing/Shaders/Builtins/Lut3DBaker.compute

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,22 @@ void Eval(uint3 id)
151151
}
152152
}
153153

154-
[numthreads(8,8,8)]
154+
#ifdef SHADER_API_GLES3
155+
#define GROUP_SIZE_XY 8
156+
#define GROUP_SIZE_Z 2
157+
#else
158+
#define GROUP_SIZE_XY 8
159+
#define GROUP_SIZE_Z 8
160+
#endif
161+
162+
[numthreads(GROUP_SIZE_XY, GROUP_SIZE_XY, GROUP_SIZE_Z)]
155163
void KGenLut3D_NoTonemap(uint3 id : SV_DispatchThreadID) { Eval(id); }
156164

157-
[numthreads(8,8,8)]
165+
[numthreads(GROUP_SIZE_XY, GROUP_SIZE_XY, GROUP_SIZE_Z)]
158166
void KGenLut3D_AcesTonemap(uint3 id : SV_DispatchThreadID) { Eval(id); }
159167

160-
[numthreads(8,8,8)]
168+
[numthreads(GROUP_SIZE_XY, GROUP_SIZE_XY, GROUP_SIZE_Z)]
161169
void KGenLut3D_NeutralTonemap(uint3 id : SV_DispatchThreadID) { Eval(id); }
162170

163-
[numthreads(8,8,8)]
171+
[numthreads(GROUP_SIZE_XY, GROUP_SIZE_XY, GROUP_SIZE_Z)]
164172
void KGenLut3D_CustomTonemap(uint3 id : SV_DispatchThreadID) { Eval(id); }

PostProcessing/Shaders/Builtins/Texture3DLerp.compute

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ CBUFFER_END
1111
Texture3D _From;
1212
Texture3D _To;
1313

14-
[numthreads(8, 8, 8)]
14+
#ifdef SHADER_API_GLES3
15+
#define GROUP_SIZE_XY 8
16+
#define GROUP_SIZE_Z 2
17+
#else
18+
#define GROUP_SIZE_XY 8
19+
#define GROUP_SIZE_Z 8
20+
#endif
21+
22+
[numthreads(GROUP_SIZE_XY, GROUP_SIZE_XY, GROUP_SIZE_Z)]
1523
void KTexture3DLerp(uint3 id : SV_DispatchThreadID)
1624
{
1725
if (float(id.x) < _Params.y && float(id.y) < _Params.y && float(id.z) < _Params.y)

0 commit comments

Comments
 (0)