-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRenderEngine.cs
567 lines (478 loc) · 16.4 KB
/
RenderEngine.cs
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/**
Copyright 2014-2024 Robert McNeel and Associates
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
**/
using ccl;
using Rhino;
using Rhino.Display;
using Rhino.DocObjects;
using Rhino.Render;
using Rhino.Render.PostEffects;
using RhinoCyclesCore.Core;
using RhinoCyclesCore.Database;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
namespace RhinoCyclesCore
{
public class CyclesPostEffectExecutionControl : PostEffectExecutionControl
{
public int CurrentSample { get; set; } = 0;
public int TriggerSample { get; set; } = int.MaxValue;
public override bool ReadyToExecutePostEffect(Guid pep_id)
{
return CurrentSample >= TriggerSample;
}
}
public enum State
{
Unset,
Waiting,
Uploading,
Rendering,
Stopping,
Stopped
}
/// <summary>
/// The actual render engine, ready for asynchronous work in Rhino.
/// </summary>
public partial class RenderEngine : IDisposable
{
protected CreatePreviewEventArgs PreviewEventArgs { get; set; }
public RenderWindow RenderWindow { get; set; }
public Session Session { get; set; } = null;
/// <summary>
/// True when State.Rendering
/// </summary>
public bool IsRendering => State == State.Rendering;
/// <summary>
/// True when State.Uploading
/// </summary>
public bool IsUploading => State == State.Uploading;
/// <summary>
/// True when State.Waiting
/// </summary>
public bool IsWaiting => State == State.Waiting;
/// <summary>
/// True when State.IsStopped
/// </summary>
public bool IsStopped => State == State.Stopped;
/// <summary>
/// Current render engine state.
/// </summary>
public State State { get; set; }
/// <summary>
/// Reference to the bitmap we're rendering into.
///
/// This is used when rendering material previews.
/// </summary>
public Bitmap RenderBitmap { get; set; }
/// <summary>
/// Set to true when the render session should be cancelled - used for preview job cancellation
/// </summary>
public bool CancelRender { get; set; }
public bool ShouldBreak => CancelRender || State == State.Stopped || State == State.Stopping;
public int RenderedSamples { get; set; }
public int RenderedTiles { get; set; }
public bool Finished { get; set; } = false;
public string TimeString;
protected CSycles.LoggerCallback m_logger_callback;
protected bool m_flush;
/// <summary>
/// Flag set to true when a flush on the changequeue is needed.
///
/// Setting of Flush is protected with a lock. Getting is not.
/// </summary>
public bool Flush
{
get
{
return m_flush;
}
set
{
m_flush = value;
}
}
public void TriggerBeginChangesNotified()
{
BeginChangesNotified?.Invoke(this, EventArgs.Empty);
}
public event EventHandler<EventArgs> BeginChangesNotified;
/// <summary>
/// Our instance of the change queue. This is our access point for all
/// data. The ChangeQueue mechanism will push data to it, record it
/// with all necessary book keeping to track the data relations between
/// Rhino and Cycles.
/// </summary>
public ChangeDatabase Database { get; set; }
/// <summary>
/// Return true if any change has been received through the changequeue
/// </summary>
/// <returns>true if any changes have been received.</returns>
protected bool HasSceneChanges()
{
return Database.HasChanges();
}
protected readonly uint m_doc_serialnumber;
private readonly bool m_interactive;
public RhinoDoc Doc => RhinoDoc.FromRuntimeSerialNumber(m_doc_serialnumber);
/// <summary>
/// Render engine implementations that need to keep track of views
/// for instance to signal when a frame is ready for that particular
/// view.
///
/// Generally such engines want to register an event handler to
/// Database.ViewChanged to record the new ViewInfo here.
/// </summary>
public ViewInfo View { get; set; }
public bool ViewSet => View != null;
public Rectangle BufferRectangle { get; set; }
public Size FullSize { get; set; }
public CyclesPostEffectExecutionControl PEEController { get; set; }
#region CONSTRUCTORS
private void RegisterEventHandler()
{
Database.MaterialShaderChanged += Database_MaterialShaderChanged;
Database.LightShaderChanged += Database_LightShaderChanged;
Database.FilmUpdateTagged += Database_FilmUpdateTagged;
}
protected Converters.BitmapConverter _bitmapConverter = new Converters.BitmapConverter();
public DisplayPipelineAttributes Attributes => Database?.DisplayPipelineAttributes ?? null;
public RenderEngine(Guid pluginId, uint docRuntimeSerialnumber, ViewInfo view, ViewportInfo vp, DisplayPipelineAttributes attributes, bool interactive)
{
RcCore.It.AddLogString("RenderEngine constructor entry");
m_doc_serialnumber = docRuntimeSerialnumber;
View = view;
m_interactive = interactive;
var doc = RhinoDoc.FromRuntimeSerialNumber(m_doc_serialnumber);
RcCore.It.AddLogString("RenderEngine create ChangeDatabase");
Database = new ChangeDatabase(pluginId, this, m_doc_serialnumber, View, attributes, !m_interactive, _bitmapConverter)
{
ModelAbsoluteTolerance = doc.ModelAbsoluteTolerance,
ModelAngleToleranceRadians = doc.ModelAngleToleranceRadians,
ModelUnitSystem = doc.ModelUnitSystem
};
RcCore.It.AddLogString("RenderEngine ChangeDatabase created");
RegisterEventHandler();
PEEController = new CyclesPostEffectExecutionControl()
{
CurrentSample = 0,
TriggerSample = RcCore.It.AllSettings.TriggerPostEffectsSample
};
RcCore.It.AddLogString("RenderEngine constructor exit");
}
public RenderEngine(Guid pluginId, CreatePreviewEventArgs previewEventArgs, bool interactive, uint docsrn)
{
PEEController = new CyclesPostEffectExecutionControl()
{
CurrentSample = 0,
TriggerSample = int.MaxValue
};
PreviewEventArgs = previewEventArgs;
m_doc_serialnumber = docsrn;
Database = new ChangeDatabase(pluginId, this, PreviewEventArgs, _bitmapConverter, docsrn);
RegisterEventHandler();
}
public ccl.Shader _Shader { get; set; } = null;
// Create a placeholder shader for new meshes to use before they get assigned
// their actual shaders
protected void CreateSimpShader()
{
var _sh = _Shader;
if(_sh == null)
{
_Shader = new ccl.Shader(Session.Scene);
}
}
#endregion
/// <summary>
/// Tell our changequeue instance to initialise world.
/// </summary>
public void CreateWorld()
{
Database.CreateWorld(RcCore.It.AllSettings.FlushAtEndOfCreateWorld);
}
/// <summary>
/// True if rendering for preview
/// </summary>
/// <returns></returns>
public bool IsPreview()
{
return Database.IsPreview;
}
public void TestCancel(IntPtr sid)
{
if (IsStopped) return;
if (PreviewEventArgs != null)
{
if (PreviewEventArgs.Cancel)
{
Session.QuickCancel();
State = State.Stopping;
CancelRender = true;
}
}
}
public class StatusTextEventArgs
{
public StatusTextEventArgs(string s, float progress, int samples, bool finished)
{
StatusText = s;
Progress = progress;
Samples = samples;
Finished = finished;
}
public string StatusText { get; private set; }
public float Progress { get; private set; }
public int Samples { get; private set; }
public bool Finished { get; private set; }
}
public event EventHandler<StatusTextEventArgs> StatusTextUpdated;
/// <summary>
/// Tell engine to fire StatusTextEvent with given arguments
/// </summary>
/// <param name="e"></param>
public void TriggerStatusTextUpdated(StatusTextEventArgs e)
{
StatusTextUpdated?.Invoke(this, e);
}
/// <summary>
/// Handle status updates
/// </summary>
/// <param name="sid"></param>
string _previouStatusMessage = "";
public void UpdateCallback(IntPtr sid)
{
if (IsStopped) return;
RcCore.It.AddLogStringIfVerbose($"RenderEngine.UpdateCallback (ptr {sid}) entry");
var status = CSycles.progress_get_status(sid);
var substatus = CSycles.progress_get_substatus(sid);
RenderedSamples = CSycles.progress_get_sample(sid);
RenderedTiles = CSycles.progress_get_rendered_tiles(sid);
//Debug.WriteLine("Current sample: {0}", RenderedSamples);
float progress;
double total_time, sample_time;
CSycles.progress_get_time(sid, out total_time, out sample_time);
CSycles.progress_get_progress(sid, out progress);
int hr = ((int)total_time) / (60 * 60);
int min = (((int)total_time) / 60) % 60;
int sec = ((int)total_time) % 60;
int hun = ((int)(total_time * 100.0)) % 100;
if (!substatus.Equals(string.Empty)) status = status + ": " + substatus;
bool finished = status.Contains("Finished") || status.Contains("Rendering Done");
if (finished) RenderedSamples = MaxSamples;
if(!Finished && !status.Equals(_previouStatusMessage)) RcCore.It.AddLogStringIfVerbose($"RenderEngine.UpdateCallback {status}");
_previouStatusMessage = status;
TimeString = $"{hr}h {min}m {sec}.{hun}s";
status = $"{status} {TimeString}";
Finished = finished;
// don't set full 100% progress here yet, because that signals the renderwindow the end of async render
if (progress >= 0.9999f) progress = 1.0f;
if (MaxSamples == int.MaxValue) progress = -1.0f;
RenderWindow?.SetProgress(status, progress);
TriggerStatusTextUpdated(new StatusTextEventArgs(status, progress, RenderedSamples, finished));
RcCore.It.AddLogStringIfVerbose($"RenderEngine.UpdateCallback (ptr {sid}) exit");
}
/// <summary>
/// Clamp color so we get valid values for system bitmap
/// </summary>
/// <param name="ch"></param>
/// <returns></returns>
public static int ColorClamp(int ch)
{
if (ch < 0) return 0;
return ch > 255 ? 255 : ch;
}
/// <summary>
/// Return resolution size divided by PixelSize
/// </summary>
/// <returns>width / pixelsize, height / pixelsize</returns>
protected Size CalculateNativeRenderSize()
{
Size size = RenderWindow.Size();
return new Size(
width: size.Width / PixelSize,
height: size.Height / PixelSize
);
}
public void BlitPixelsToRenderWindowChannel()
{
RcCore.It.AddLogStringIfVerbose("BlitPixelsToRenderWindowChannel entry");
var renderWindowSize = RenderWindow.Size();
int width = renderWindowSize.Width;
int height = renderWindowSize.Height;
var rect = new Rectangle(0, 0, width, height);
foreach (var pass in Session.Passes)
{
IntPtr pixel_buffer = IntPtr.Zero;
int pixelSizeFromCycles = 1;
var channel = StandardChannelForPassType(pass);
RcCore.It.AddLogStringIfVerbose($"BlitPixelsToRenderWindowChannel RetainPixelBuffer {pass} start");
Session.RetainPixelBuffer(pass, width, height, ref pixel_buffer, ref pixelSizeFromCycles);
RcCore.It.AddLogStringIfVerbose($"BlitPixelsToRenderWindowChannel RetainPixelBuffer {pass} end");
if (pixel_buffer != IntPtr.Zero && pixelSizeFromCycles > 0)
{
RenderWindow.SetRenderOutputRect(
new Rectangle(
x: 0,
y: 0,
width: width / pixelSizeFromCycles,
height: height / pixelSizeFromCycles
)
);
using (var rgba = RenderWindow.OpenChannel(channel))
{
PixelBuffer pb = new PixelBuffer(pixel_buffer);
RcCore.It.AddLogStringIfVerbose($"BlitPixelsToRenderWindowChannel rgba.SetValues {pass} start");
rgba?.SetValues(rect, rect.Size, pb);
RcCore.It.AddLogStringIfVerbose($"BlitPixelsToRenderWindowChannel rgba.SetValues {pass} end");
}
Session.ReleasePixelBuffer(pass);
}
}
RcCore.It.AddLogStringIfVerbose("BlitPixelsToRenderWindowChannel exit");
}
/// <summary>
/// Callback for debug logging facility. Will be called only for Debug builds of ccycles.dll
/// </summary>
/// <param name="msg"></param>
public static void LoggerCallback(string msg)
{
RcCore.It.AddLogStringIfVerbose($"Cycles: {msg}");
}
/// <summary>
/// Call to stop render thread.
/// </summary>
public void StopRendering()
{
StopTheRenderer();
RcCore.It.AddLogStringIfVerbose($"C# Cycles render thread join start\n");
RenderThread?.Join();
RcCore.It.AddLogStringIfVerbose($"C# Cycles render thread join end\n");
RenderThread = null;
}
public void Pause()
{
State = State.Waiting;
}
public void Continue()
{
State = State.Rendering;
}
/// <summary>
/// This device is set for the modal render engine case when the session
/// is used to compile the OpenCL kernels off the main thread
/// when we do so we should just destroy the session without cancelling
/// the session
/// </summary>
public Thread RenderThread { get; set; } = null;
public bool StartRenderThread(ThreadStart threadStart, string threadName)
{
RenderThread = new Thread(threadStart)
{
Name = threadName
};
RenderThread.Start();
return true;
}
// Object to lock while uploading data to the session
// This is needed to ensure we don't pull out the session
// while still uploading the data. So this is used in data upload
// and is tried to be acquired in StopTheRenderer.
protected object UploadDataLock = new();
/// <summary>
/// Call to cancel rendering, stop and destroy the session, and change state to Stopped. At the start
/// state is changed to Stopping
/// </summary>
public void StopTheRenderer()
{
RcCore.It.AddLogStringIfVerbose("StopTheRenderer entry");
State = State.Stopping;
RcCore.It.AddLogStringIfVerbose("StopTheRenderer, acquire UploadData lock...");
// Try to get the lock. If we can't get it, we're still uploading data, so wait
// for the upload to finish. Once we can get the lock, we know that no other actor
// is uploading data, so we can just unlock and continue with the teardown.
lock(UploadDataLock)
{
RcCore.It.AddLogStringIfVerbose("StopTheRenderer, UploadData lock acquired");
}
// try to get scene lock. Necessary since UploadData might still be writing to
// the session. Wait for it to react to state being set to Stopping.
// Once we can lock we know there is no other actor accessing the session, so
// we can just unlock and continue with the teardown.
Debug.Assert(Session != null);
if (Session != null)
{
RcCore.It.AddLogStringIfVerbose("Session Wait, lock and unlock start");
Session.WaitUntilLocked();
Session.Unlock();
RcCore.It.AddLogStringIfVerbose("Session Wait, lock and unlock end");
RcCore.It.AddLogStringIfVerbose("Session QuickCancel start");
Session.QuickCancel();
RcCore.It.AddLogStringIfVerbose("Session QuickCancel end");
Session.Cancel("StopTheRenderer");
Thread.Sleep(500);
RcCore.It.AddLogStringIfVerbose("Session Dispose start");
Session.Dispose();
RcCore.It.AddLogStringIfVerbose("Session Dispose end");
}
CancelRender = true;
State = State.Stopped;
RcCore.It.AddLogStringIfVerbose("StopTheRenderer exit");
}
/// <summary>
/// Set progress to HUD if exists. Also set to RenderWindow, if it is not null.
/// </summary>
/// <param name="rw"></param>
/// <param name="msg"></param>
/// <param name="progress"></param>
public void SetProgress(RenderWindow rw, string msg, float progress)
{
TriggerStatusTextUpdated(new StatusTextEventArgs(msg, progress, progress < 0 ? -1 : 0, false));
rw?.SetProgress(msg, progress);
}
// handle material shader updates
protected void Database_MaterialShaderChanged(object sender, MaterialShaderUpdatedEventArgs e)
{
RecreateMaterialShader(e.RcShader, e.CclShader);
e.CclShader.Tag();
}
// handle light shader updates
protected void Database_LightShaderChanged(object sender, LightShaderUpdatedEventArgs e)
{
ReCreateSimpleEmissionShader(e.RcLightShader, e.CclShader);
e.CclShader.Tag();
}
protected void Database_FilmUpdateTagged(object sender, EventArgs e)
{
Session.Scene.Film.Update();
}
protected void Database_LinearWorkflowChanged(object sender, LinearWorkflowChangedEventArgs e)
{
}
private bool disposedValue = false;
public virtual void Dispose() {
Dispose(true);
}
public virtual void Dispose(bool isDisposing) {
if(!disposedValue)
{
if(isDisposing)
{
_bitmapConverter?.Dispose();
Database?.Dispose();
}
}
}
}
}