4using System.Collections.Generic;
12 private const int sectionLength = 1000;
13 private const int totalSections = 100;
15 private class VideoSection
17 public readonly
long start;
18 public readonly
long end;
20 public VideoSection(
long start,
long end)
27 private List<VideoSection> sections;
29 private bool loadingVideo;
31 private void FillSectionList(
long numFrames)
33 this.sections =
new List<VideoSection>();
35 long numSections = numFrames / sectionLength;
36 Assert(numSections > 0,
"Video file has no sections.");
38 long sectionStart = 0;
39 sectionStart + sectionLength <= numFrames && sections.Count < totalSections;
40 sectionStart += sectionLength
43 sections.Add(
new VideoSection(start: sectionStart, end: sectionStart + sectionLength));
45 for (
int i = 0; sections.Count < totalSections; i++)
47 sections.Add(sections[i]);
51 private readonly ComputeShader csHighlightRemoval;
52 private readonly
int kernelShowResult;
56 private UnityEngine.Video.VideoPlayer videoPlayer;
57 private readonly
long frameStart;
58 private readonly
long? frameEndOrNull;
65 private long? lastProcessedFrame;
67 public string paramsJSON => JsonUtility.ToJson(this.launchParameters.GetSerializable());
69 public int warningCounter => this.launchParameters.dispatcher.warningCounter;
76 UnityEngine.Video.VideoPlayer videoPlayer,
80 ComputeShader csHighlightRemoval
83 this.csHighlightRemoval = csHighlightRemoval;
84 this.kernelShowResult = csHighlightRemoval.FindKernel(
"ShowResult");
85 this.logType = logType;
86 this.launchParameters = launchParameters;
87 this.lastProcessedFrame =
null;
90 this.frameEndOrNull = frameEnd;
91 this.frameStart = frameStart ?? 0;
92 this.finished =
false;
93 this.loadingVideo =
true;
95 if (this.launchParameters.dispatcher.clusteringRTsAndBuffers.isAllocated ==
false)
97 this.launchParameters.dispatcher.clusteringRTsAndBuffers.Allocate();
100 this.SetTextureSize();
101 this.InitVideoPlayer(videoPlayer, frameStart);
103 FillSectionList((
long)this.videoPlayer.frameCount);
106 private void InitVideoPlayer(UnityEngine.Video.VideoPlayer videoPlayer,
long? frameStart)
108 this.videoPlayer = videoPlayer;
109 this.videoPlayer.playbackSpeed = 0;
110 this.videoPlayer.clip = this.launchParameters.video;
111 this.videoPlayer.Play();
119 this.videoPlayer.frame != 0,
120 "Video player set to frame 0. Normally this does not happen."
122 this.videoPlayer.frame = frameStart ?? 0;
125 private void SetTextureSize()
127 int workingTextureSize = this.launchParameters
129 .clusteringRTsAndBuffers
132 int fullTextureSize = this.launchParameters.dispatcher.clusteringRTsAndBuffers.fullSize;
136 (workingTextureSize & (workingTextureSize - 1)) == 0
137 && workingTextureSize > 0,
138 $
"Invalid texture size provided to {System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.Name + System.Reflection.MethodBase.GetCurrentMethod().Name}"
141 workingTextureSize <= fullTextureSize,
142 "Full texture size can not be smaller than working texture size."
145 this.csHighlightRemoval.SetInt(
"texture_size", workingTextureSize);
151 switch (this.logType)
154 measurement = this.benchmarkMeasurementVariance;
157 measurement = this.benchmarkMeasurementFrameTime;
161 new System.NotImplementedException($
"Log type not implemented: {this.logType}")
167 measurement: measurement,
168 serializableLaunchParameters: this.launchParameters.GetSerializable(),
169 logType:
this.logType
173 private void ProcessFrame_VarianceMode()
175 this.RunDispatcher();
177 if (this.videoPlayer.frame !=
this.sections[0].start)
179 this.MakeVarianceLogEntry();
183 private void ProcessFrame_FrameTimeMode()
199 this.launchParameters.dispatcher.clusteringRTsAndBuffers.GetClusterCenters()
202 const int numRepetitions = 10;
208 for (
int i = 0; i < numRepetitions; i++)
210 this.launchParameters.dispatcher.clusteringRTsAndBuffers.SetClusterCenters(
213 this.RunDispatcher();
219 this.launchParameters.dispatcher.clusteringRTsAndBuffers.GetClusterCenters()
228 this.benchmarkMeasurementFrameTime.frameTimeRecords.Add(
230 frameIndex:
this.videoPlayer.frame,
237 private bool IsFrameReady()
241 this.videoPlayer.frame == -1
248 ||
this.videoPlayer.frame != 0 &&
this.loadingVideo
250 ||
this.lastProcessedFrame ==
this.videoPlayer.frame
256 this.loadingVideo =
false;
265 Graphics.Blit(src, dst);
274 this.finished ==
false,
275 $
"{System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.Name + System.Reflection.MethodBase.GetCurrentMethod().Name} should not be called after measurements were finished"
282 if (this.lastProcessedFrame !=
null)
284 if (this.lastProcessedFrame + 1 != this.videoPlayer.frame)
299 this.videoPlayer.frame ==
this.sections[0].start,
300 $
"Incorrect frame processing order. Current frame: {this.videoPlayer.frame}. Previous frame: {this.lastProcessedFrame}"
304 this.lastProcessedFrame = this.videoPlayer.frame;
307 this.videoPlayer.texture,
308 this.launchParameters.dispatcher.clusteringRTsAndBuffers.texturesFullRes.rtInput
311 this.launchParameters.dispatcher.clusteringRTsAndBuffers.Downsample(
312 this.csHighlightRemoval,
313 staggeredJitter: this.launchParameters.staggeredJitter,
314 doDownscale:
this.launchParameters.doDownscale
317 switch (this.logType)
320 this.ProcessFrame_VarianceMode();
323 ProcessFrame_FrameTimeMode();
326 new System.NotImplementedException($
"Log type not implemented: {this.logType}");
335 private void AdvanceFrame()
338 if (this.videoPlayer.frame ==
this.sections[0].end - 1)
341 this.sections.RemoveAt(0);
344 if (this.sections.Count == 0)
346 this.finished =
true;
351 this.videoPlayer.frame = this.sections[0].start;
361#pragma warning disable 162
364 this.launchParameters.dispatcher.clusteringRTsAndBuffers.RandomizeClusterCenters();
368 this.launchParameters.dispatcher.clusteringRTsAndBuffers.SetDeterministicClusterCenters();
370#pragma warning restore 162
376 this.videoPlayer.frame++;
383 private void MakeVarianceLogEntry()
385 this.benchmarkMeasurementVariance.frameVarianceRecords.Add(
387 frameIndex:
this.videoPlayer.frame,
388 variance:
this.launchParameters.dispatcher.GetVariance()
396 private void RunDispatcher()
398 this.launchParameters.dispatcher.RunClustering(
399 this.launchParameters.dispatcher.clusteringRTsAndBuffers.texturesWorkRes
410 this.launchParameters.dispatcher.AttributeClustersKM(
411 this.launchParameters.dispatcher.clusteringRTsAndBuffers.texturesWorkRes
417 this.csHighlightRemoval.SetTexture(
418 this.kernelShowResult,
419 "tex_arr_clusters_r",
420 this.launchParameters.dispatcher.clusteringRTsAndBuffers.texturesWorkRes.rtArr
423 this.csHighlightRemoval.SetTexture(
424 this.kernelShowResult,
426 this.launchParameters.dispatcher.clusteringRTsAndBuffers.rtResult
429 this.csHighlightRemoval.SetBuffer(
430 this.kernelShowResult,
431 "cbuf_cluster_centers",
432 this.launchParameters.dispatcher.clusteringRTsAndBuffers.cbufClusterCenters
435 this.csHighlightRemoval.SetTexture(
436 this.kernelShowResult,
438 this.launchParameters.dispatcher.clusteringRTsAndBuffers.texturesWorkRes.rtInput
441 this.csHighlightRemoval.Dispatch(
442 this.kernelShowResult,
444 this.launchParameters.dispatcher.clusteringRTsAndBuffers.rtResult.width
449 this.launchParameters.dispatcher.clusteringRTsAndBuffers.rtResult.height
456 Graphics.Blit(this.launchParameters.dispatcher.clusteringRTsAndBuffers.rtResult, target);
458 this.launchParameters.dispatcher.clusteringRTsAndBuffers.rtResult.DiscardContents();
463 this.launchParameters.Dispose();
Call Dispose after using.
static long MeasureTime(Action action)
Runs the provided action and measures the time it required to finish. Ensures GC will not trigger wh...
Call Dispose after using.
Call Allocate before using and Dispose after using.
const int kernelSize
Must match the shader.
Call Dispose after using.
BenchmarkReport GetReport()
MeasurementRunner(BenchmarkGeneration.LaunchParameters launchParameters, UnityEngine.Video.VideoPlayer videoPlayer, long? frameStart, long? frameEnd, ClusteringTest.LogType logType, ComputeShader csHighlightRemoval)
Takes ownership of launchParameters
void ProcessNextFrame(RenderTexture src, RenderTexture dst)
void RenderResult(RenderTexture target)