forked from jesterKing/CCSycles
/
Session.cs
323 lines (290 loc) · 8.44 KB
/
Session.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
/**
Copyright 2014 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 System;
using System.Collections.Generic;
namespace ccl
{
/// <summary>
/// The Cycles session representation.
///
/// The Session is used to manage the render process at the highest level. Here one can set
/// the different callbacks to retrieve render results and updates and statistics during a render.
///
/// A session is created using SessionParameters and a Scene.
///
/// Through Session the render process is started and ended.
/// </summary>
public class Session
{
/// <summary>
/// True if the session has already been destroyed.
/// </summary>
private bool Destroyed { get; set; }
private Scene sc;
/// <summary>
/// Get or set the Scene used for this Session
/// </summary>
public Scene Scene
{
get { return sc; }
set {
CSycles.session_set_scene(Client.Id, Id, value.Id);
sc = value;
}
}
/// <summary>
/// Get the SessionParams used for this Session
/// </summary>
public SessionParameters SessionParams { get; private set; }
/// <summary>
/// Get the ID for this session
/// </summary>
public uint Id { get; }
/// <summary>
/// Reference to client used for this session
/// </summary>
private Client Client { get; }
/// <summary>
/// Create a new session using the given SessionParameters and Scene
/// </summary>
/// <param name="sessionParams">Previously created SessionParameters to create Session with</param>
/// <param name="scene">Previously created Scene to create Session with</param>
public Session(Client client, SessionParameters sessionParams)
{
Client = client;
SessionParams = sessionParams;
Id = CSycles.session_create(Client.Id, sessionParams.Id);
}
/// <summary>
/// private reference to the update callback
/// </summary>
CSycles.UpdateCallback updateCallback;
/// <summary>
/// Set the CSycles.UpdateCallback to Cycles to receive progress updates
/// </summary>
public CSycles.UpdateCallback UpdateCallback
{
set
{
if (Destroyed) return;
updateCallback = value;
CSycles.session_set_update_callback(Client.Id, Id, value);
}
}
/// <summary>
/// Private reference to the test cancel callback
/// </summary>
CSycles.TestCancelCallback testCancelCallback;
/// <summary>
/// Set the CSycles.TestCancelCallback to Cycles to test for session cancel
/// </summary>
public CSycles.TestCancelCallback TestCancelCallback
{
set
{
if (Destroyed) return;
testCancelCallback = value;
CSycles.session_set_cancel_callback(Client.Id, Id, value);
}
}
/// <summary>
/// Private reference to the tile update callback.
/// </summary>
CSycles.RenderTileCallback updateTileCallback;
/// <summary>
/// Set the CSycles.RenderTileCallback to use for render tile updates
/// </summary>
public CSycles.RenderTileCallback UpdateTileCallback
{
set
{
if (Destroyed) return;
updateTileCallback = value;
CSycles.session_set_update_tile_callback(Client.Id, Id, value);
}
}
/// <summary>
/// Private reference to the tile write callback
/// </summary>
CSycles.RenderTileCallback writeTileCallback;
/// <summary>
/// Set the CSycles.RenderTileCallback to use for render tile writes
/// </summary>
public CSycles.RenderTileCallback WriteTileCallback
{
set
{
if (Destroyed) return;
writeTileCallback = value;
CSycles.session_set_write_tile_callback(Client.Id, Id, value);
}
}
private CSycles.DisplayUpdateCallback displayUpdateCallback;
/// <summary>
/// Set the CSycles.DisplayUpdateCallback to use for signalling rendered
/// sample pass
/// </summary>
public CSycles.DisplayUpdateCallback DisplayUpdateCallback
{
set
{
if (Destroyed) return;
displayUpdateCallback = value;
CSycles.session_set_display_update_callback(Client.Id, Id, value);
}
}
/// <summary>
/// Start the rendering session. After this one should Wait() for
/// the session to complete.
/// </summary>
public void Start()
{
if (Destroyed) return;
CSycles.progress_reset(Client.Id, Id);
CSycles.session_start(Client.Id, Id);
}
/// <summary>
/// Set up the session for running through the sampler.
/// </summary>
public void PrepareRun()
{
if(Destroyed) return;
CSycles.session_prepare_run(Client.Id, Id);
}
/// <summary>
/// Sample one pass. Return -1 when no pass was rendered, zero or positive when something was rendered.
/// </summary>
public int Sample()
{
if(Destroyed) return -1;
return CSycles.session_sample(Client.Id, Id);
}
/// <summary>
/// Tear down the set up run for sampling.
/// </summary>
public void EndRun()
{
if(Destroyed) return;
CSycles.session_end_run(Client.Id, Id);
}
/// <summary>
/// Wait for the rendering session to complete
/// </summary>
public void Wait()
{
if (Destroyed) return;
CSycles.session_wait(Client.Id, Id);
}
/// <summary>
/// Cancel the currently in progress render
/// </summary>
/// <param name="cancelMessage"></param>
public void Cancel(string cancelMessage)
{
if (Destroyed) return;
CSycles.session_cancel(Client.Id, Id, cancelMessage);
}
/// <summary>
/// Destroy the session and all related.
/// </summary>
public void Destroy()
{
if (Destroyed) return;
CSycles.session_destroy(Client.Id, Id, Scene.Id);
Destroyed = true;
}
public void GetPixelBuffer(PassType pt, ref IntPtr pixel_buffer)
{
if (Destroyed)
{
pixel_buffer = IntPtr.Zero;
}
else
{
CSycles.session_get_float_buffer(Client.Id, Id, pt, ref pixel_buffer);
}
}
/// <summary>
/// Reset a Session
/// </summary>
/// <param name="width">Width of the resolution to reset with</param>
/// <param name="height">Height of the resolutin to reset with</param>
/// <param name="samples">The amount of samples to reset with</param>
/// <returns>0 on success. -1 when the session is already destroyed. -13 when a crash happened.</returns>
public int Reset(uint width, uint height, uint samples, uint full_x, uint full_y, uint full_width, uint full_height )
{
if (Destroyed) return -1;
CSycles.progress_reset(Client.Id, Id);
return CSycles.session_reset(Client.Id, Id, width, height, samples, full_x, full_y, full_width, full_height);
}
public int Reset(int width, int height, int samples, int full_x, int full_y, int full_width, int full_height )
{
return Reset((uint)width, (uint)height, (uint)samples, (uint)full_x, (uint)full_y, (uint)full_width, (uint)full_height);
}
/// <summary>
/// Pause or un-pause a render session.
/// </summary>
/// <param name="pause"></param>
public void SetPause(bool pause)
{
if (Destroyed) return;
CSycles.session_set_pause(Client.Id, Id, pause);
}
public bool IsPaused()
{
if (Destroyed) return false;
return CSycles.session_is_paused(Client.Id, Id);
}
/// <summary>
/// Set sample count for session to render. This can be used to increase the sample
/// count for an interactive render session.
/// </summary>
/// <param name="samples"></param>
public void SetSamples(int samples)
{
if (Destroyed) return;
CSycles.session_set_samples(Client.Id, Id, samples);
}
public IEnumerable<PassType> Passes {
get {
foreach(var pass in _passes) {
yield return pass;
}
}
}
private List<PassType> _passes = new List<PassType>();
/// <summary>
/// Add given pass to output layers
/// </summary>
/// <param name="pass"></param>
public void AddPass(PassType pass)
{
if (Destroyed) return;
CSycles.session_add_pass(Client.Id, Id, pass);
if (!_passes.Contains(pass)) {
_passes.Add(pass);
}
}
/// <summary>
/// Clear all passes for session. Note, will always add Combined pass back
/// </summary>
public void ClearPasses()
{
if (Destroyed) return;
_passes.Clear();
CSycles.session_clear_passes(Client.Id, Id);
CSycles.session_add_pass(Client.Id, Id, PassType.Combined);
_passes.Add(PassType.Combined);
}
}
}