Material Clustering Benchmark
WrapperStopCondition.cs
Go to the documentation of this file.
1using UnityEngine.Pool;
2using static Diagnostics;
3
5{
7 {
8 private readonly ASimpleDispatcer wrappedDispatcher;
9
10 public WrapperStopCondition(ASimpleDispatcer wrappedDispatcher)
11 {
12 this.wrappedDispatcher = wrappedDispatcher;
13 if (wrappedDispatcher.usesStopCondition)
14 {
15 Throw(
16 new System.InvalidOperationException(
17 "WrapperStopCondition must be given a dispatcher, which does not use stop condition."
18 )
19 );
20 }
21 }
22
23 public void Dispose()
24 {
25 this.wrappedDispatcher.Dispose();
26 }
27
28 /*
29 impossible to use stop condition without readback
30 */
31 public bool doesReadback => true;
32 public bool usesStopCondition => true;
33 public int warningCounter => this.wrappedDispatcher.warningCounter;
34
35 public virtual string name => this.wrappedDispatcher.name;
36 public bool doRandomizeEmptyClusters => this.wrappedDispatcher.doRandomizeEmptyClusters;
37 public int numIterations => this.wrappedDispatcher.numIterations;
39 this.wrappedDispatcher.clusteringRTsAndBuffers;
40 public DispatcherParameters abstractParameters => this.wrappedDispatcher.abstractParameters;
41
42 public void UpdateClusterCenters(ClusteringTextures clusteringTextures, bool rejectOld)
43 {
44 this.wrappedDispatcher.UpdateClusterCenters(
45 clusteringTextures: clusteringTextures,
46 rejectOld: rejectOld
47 );
48 }
49
50 public void AttributeClustersKM(ClusteringTextures clusteringTextures)
51 {
52 this.wrappedDispatcher.AttributeClustersKM(clusteringTextures: clusteringTextures);
53 }
54
55 public bool useFullResTexRef => this.wrappedDispatcher.useFullResTexRef;
56
57 public float? GetVariance()
58 {
59 return this.wrappedDispatcher.GetVariance();
60 }
61
62 public class RunUntilConvergesResult : System.IDisposable
63 {
65 public bool converged;
66
67 public static readonly IObjectPool<RunUntilConvergesResult> pool =
69 createFunc: () => new RunUntilConvergesResult(),
70 maxActive: 2
71 );
72
73 private RunUntilConvergesResult() { }
74
75 public void Dispose()
76 {
77 this.clusterCenters.Dispose();
78 pool.Release(this);
79 }
80
82 {
83 RunUntilConvergesResult obj = pool.Get();
84 obj.converged = converged;
85 obj.clusterCenters = clusterCenters;
86
87 return obj;
88 }
89 }
90
91 public virtual void RunClustering(ClusteringTextures clusteringTextures)
92 {
93 this.RunUntilConverges(clusteringTextures).Dispose();
94 }
95
97 {
98 this.wrappedDispatcher.SingleIteration(clusteringTextures);
99
100 /*
101 direcrly runing getVariance is not an option
102 because its implementation is not optimized
103 */
104
105 ClusterCenters clusterCenters = null;
106 ClusterCenters newClusterCenters =
107 this.wrappedDispatcher.clusteringRTsAndBuffers.GetClusterCenters();
108
109 /*
110 start at 2
111 iteration #1 was already performed
112 iteration #2 is the current one
113
114 we need variance change between 2 iterations
115 thus we can't check stop condition after the first iteration
116
117 variance check between last iteration of the previous frame
118 and the first iteration of the current frame
119 makes no sense
120 because the input texture changed
121 */
122
123 for (int kmIteration = 2; kmIteration < StopCondition.maxIterations; kmIteration++)
124 {
125 /*
126 * dispose previous cluster centers
127 * (only hold one instance at a time)
128 */
129 clusterCenters?.Dispose();
130 clusterCenters = newClusterCenters;
131
132 this.wrappedDispatcher.SingleIteration(clusteringTextures);
133
134 newClusterCenters =
135 this.wrappedDispatcher.clusteringRTsAndBuffers.GetClusterCenters();
136
137 if (
138 clusterCenters.variance - newClusterCenters.variance
140 )
141 {
142 // * dispose latest cluster centers
143 clusterCenters.Dispose();
145 converged: true,
146 clusterCenters: newClusterCenters
147 );
148 }
149 }
150
151 // * dispose latest cluster centers
152 clusterCenters.Dispose();
153 return RunUntilConvergesResult.Get(converged: false, clusterCenters: newClusterCenters);
154 }
155 }
156}
Call Dispose after using.
Call Allocate before using and Dispose after using.
static readonly IObjectPool< RunUntilConvergesResult > pool
static RunUntilConvergesResult Get(ClusterCenters clusterCenters, bool converged)
virtual void RunClustering(ClusteringTextures clusteringTextures)
WrapperStopCondition(ASimpleDispatcer wrappedDispatcher)
void UpdateClusterCenters(ClusteringTextures clusteringTextures, bool rejectOld)
void AttributeClustersKM(ClusteringTextures clusteringTextures)
float? GetVariance()
Computes variance for the current cluster centers. Depending on this.useFullResTexRef uses either wor...
RunUntilConvergesResult RunUntilConverges(ClusteringTextures clusteringTextures)
Call Dispose after using.
Call Dispose after using.
Definition: IDispatcher.cs:9