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
568
569
570
571
572
573
574
575
576
577
578
579
|
/*
* Copyright 2011, Blender Foundation.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributor:
* Jeroen Bakker
* Monique Dewanchand
*/
#include <algorithm>
#include <math.h>
#include <sstream>
#include <stdlib.h>
#include "COM_ExecutionGroup.h"
#include "COM_InputSocket.h"
#include "COM_SocketConnection.h"
#include "COM_defines.h"
#include "COM_ExecutionSystem.h"
#include "COM_ReadBufferOperation.h"
#include "COM_WriteBufferOperation.h"
#include "COM_ReadBufferOperation.h"
#include "COM_WorkScheduler.h"
#include "COM_ViewerOperation.h"
#include "COM_ChunkOrder.h"
#include "COM_ExecutionSystemHelper.h"
#include "BLI_math.h"
#include "PIL_time.h"
#include "WM_api.h"
#include "WM_types.h"
ExecutionGroup::ExecutionGroup()
{
this->m_isOutput = false;
this->m_complex = false;
this->m_chunkExecutionStates = NULL;
this->m_bTree = NULL;
this->m_height = 0;
this->m_width = 0;
this->m_cachedMaxReadBufferOffset = 0;
this->m_numberOfXChunks = 0;
this->m_numberOfYChunks = 0;
this->m_numberOfChunks = 0;
this->m_initialized = false;
this->m_openCL = false;
this->m_singleThreaded = false;
this->m_chunksFinished = 0;
}
CompositorPriority ExecutionGroup::getRenderPriotrity()
{
return this->getOutputNodeOperation()->getRenderPriority();
}
bool ExecutionGroup::containsOperation(NodeOperation *operation)
{
for (vector<NodeOperation *>::const_iterator iterator = this->m_operations.begin(); iterator != this->m_operations.end(); ++iterator) {
NodeOperation *inListOperation = *iterator;
if (inListOperation == operation) {
return true;
}
}
return false;
}
const bool ExecutionGroup::isComplex() const
{
return this->m_complex;
}
bool ExecutionGroup::canContainOperation(NodeOperation *operation)
{
if (!this->m_initialized) { return true; }
if (operation->isReadBufferOperation()) { return true; }
if (operation->isWriteBufferOperation()) { return false; }
if (operation->isSetOperation()) { return true; }
if (!this->isComplex()) {
return (!operation->isComplex());
}
else {
return false;
}
}
void ExecutionGroup::addOperation(ExecutionSystem *system, NodeOperation *operation)
{
/* should never happen but in rare cases it can - it causes confusing crashes */
BLI_assert(operation->isOperation() == true);
if (containsOperation(operation)) return;
if (canContainOperation(operation)) {
if (!operation->isBufferOperation()) {
this->m_complex = operation->isComplex();
this->m_openCL = operation->isOpenCL();
this->m_singleThreaded = operation->isSingleThreaded();
this->m_initialized = true;
}
this->m_operations.push_back(operation);
if (operation->isReadBufferOperation()) {
ReadBufferOperation *readOperation = (ReadBufferOperation *)operation;
WriteBufferOperation *writeOperation = readOperation->getMemoryProxy()->getWriteBufferOperation();
this->addOperation(system, writeOperation);
}
else {
unsigned int index;
for (index = 0; index < operation->getNumberOfInputSockets(); index++) {
InputSocket *inputSocket = operation->getInputSocket(index);
if (inputSocket->isConnected()) {
NodeOperation *node = (NodeOperation *)inputSocket->getConnection()->getFromNode();
this->addOperation(system, node);
}
}
}
}
else {
if (operation->isWriteBufferOperation()) {
WriteBufferOperation *writeoperation = (WriteBufferOperation *)operation;
if (writeoperation->getMemoryProxy()->getExecutor() == NULL) {
ExecutionGroup *newGroup = new ExecutionGroup();
writeoperation->getMemoryProxy()->setExecutor(newGroup);
newGroup->addOperation(system, operation);
ExecutionSystemHelper::addExecutionGroup(system->getExecutionGroups(), newGroup);
}
}
}
}
NodeOperation *ExecutionGroup::getOutputNodeOperation() const
{
return this->m_operations[0]; // the first operation of the group is always the output operation.
}
void ExecutionGroup::initExecution()
{
if (this->m_chunkExecutionStates != NULL) {
delete[] this->m_chunkExecutionStates;
}
unsigned int index;
determineNumberOfChunks();
this->m_chunkExecutionStates = NULL;
if (this->m_numberOfChunks != 0) {
this->m_chunkExecutionStates = new ChunkExecutionState[this->m_numberOfChunks];
for (index = 0; index < this->m_numberOfChunks; index++) {
this->m_chunkExecutionStates[index] = COM_ES_NOT_SCHEDULED;
}
}
unsigned int maxNumber = 0;
for (index = 0; index < this->m_operations.size(); index++) {
NodeOperation *operation = this->m_operations[index];
if (operation->isReadBufferOperation()) {
ReadBufferOperation *readOperation = (ReadBufferOperation *)operation;
this->m_cachedReadOperations.push_back(readOperation);
maxNumber = max(maxNumber, readOperation->getOffset());
}
}
maxNumber++;
this->m_cachedMaxReadBufferOffset = maxNumber;
}
void ExecutionGroup::deinitExecution()
{
if (this->m_chunkExecutionStates != NULL) {
delete[] this->m_chunkExecutionStates;
this->m_chunkExecutionStates = NULL;
}
this->m_numberOfChunks = 0;
this->m_numberOfXChunks = 0;
this->m_numberOfYChunks = 0;
this->m_cachedReadOperations.clear();
this->m_bTree = NULL;
}
void ExecutionGroup::determineResolution(unsigned int resolution[2])
{
NodeOperation *operation = this->getOutputNodeOperation();
resolution[0] = operation->getWidth();
resolution[1] = operation->getHeight();
this->setResolution(resolution);
}
void ExecutionGroup::determineNumberOfChunks()
{
if (this->m_singleThreaded) {
this->m_numberOfXChunks = 1;
this->m_numberOfYChunks = 1;
this->m_numberOfChunks = 1;
}
else {
const float chunkSizef = this->m_chunkSize;
this->m_numberOfXChunks = ceil(this->m_width / chunkSizef);
this->m_numberOfYChunks = ceil(this->m_height / chunkSizef);
this->m_numberOfChunks = this->m_numberOfXChunks * this->m_numberOfYChunks;
}
}
/**
* this method is called for the top execution groups. containing the compositor node or the preview node or the viewer node)
*/
void ExecutionGroup::execute(ExecutionSystem *graph)
{
CompositorContext& context = graph->getContext();
const bNodeTree *bTree = context.getbNodeTree();
if (this->m_width == 0 || this->m_height == 0) {return; } /// @note: break out... no pixels to calculate.
if (bTree->test_break && bTree->test_break(bTree->tbh)) {return; } /// @note: early break out for blur and preview nodes
if (this->m_numberOfChunks == 0) {return; } /// @note: early break out
unsigned int chunkNumber;
this->m_chunksFinished = 0;
this->m_bTree = bTree;
unsigned int index;
unsigned int *chunkOrder = new unsigned int[this->m_numberOfChunks];
for (chunkNumber = 0; chunkNumber < this->m_numberOfChunks; chunkNumber++) {
chunkOrder[chunkNumber] = chunkNumber;
}
NodeOperation *operation = this->getOutputNodeOperation();
float centerX = 0.5;
float centerY = 0.5;
OrderOfChunks chunkorder = COM_ORDER_OF_CHUNKS_DEFAULT;
if (operation->isViewerOperation()) {
ViewerBaseOperation *viewer = (ViewerBaseOperation *)operation;
centerX = viewer->getCenterX();
centerY = viewer->getCenterY();
chunkorder = viewer->getChunkOrder();
}
switch (chunkorder) {
case COM_TO_RANDOM:
for (index = 0; index < 2 * this->m_numberOfChunks; index++) {
int index1 = rand() % this->m_numberOfChunks;
int index2 = rand() % this->m_numberOfChunks;
int s = chunkOrder[index1];
chunkOrder[index1] = chunkOrder[index2];
chunkOrder[index2] = s;
}
break;
case COM_TO_CENTER_OUT:
{
ChunkOrderHotspot **hotspots = new ChunkOrderHotspot *[1];
hotspots[0] = new ChunkOrderHotspot(this->m_width * centerX, this->m_height * centerY, 0.0f);
rcti rect;
ChunkOrder *chunkOrders = new ChunkOrder[this->m_numberOfChunks];
for (index = 0; index < this->m_numberOfChunks; index++) {
determineChunkRect(&rect, index);
chunkOrders[index].setChunkNumber(index);
chunkOrders[index].setX(rect.xmin);
chunkOrders[index].setY(rect.ymin);
chunkOrders[index].determineDistance(hotspots, 1);
}
sort(&chunkOrders[0], &chunkOrders[this->m_numberOfChunks - 1]);
for (index = 0; index < this->m_numberOfChunks; index++) {
chunkOrder[index] = chunkOrders[index].getChunkNumber();
}
delete hotspots[0];
delete[] hotspots;
delete[] chunkOrders;
}
break;
case COM_TO_RULE_OF_THIRDS:
{
ChunkOrderHotspot **hotspots = new ChunkOrderHotspot *[9];
unsigned int tx = this->m_width / 6;
unsigned int ty = this->m_height / 6;
unsigned int mx = this->m_width / 2;
unsigned int my = this->m_height / 2;
unsigned int bx = mx + 2 * tx;
unsigned int by = my + 2 * ty;
float addition = this->m_numberOfChunks / COM_RULE_OF_THIRDS_DIVIDER;
hotspots[0] = new ChunkOrderHotspot(mx, my, addition * 0);
hotspots[1] = new ChunkOrderHotspot(tx, my, addition * 1);
hotspots[2] = new ChunkOrderHotspot(bx, my, addition * 2);
hotspots[3] = new ChunkOrderHotspot(bx, by, addition * 3);
hotspots[4] = new ChunkOrderHotspot(tx, ty, addition * 4);
hotspots[5] = new ChunkOrderHotspot(bx, ty, addition * 5);
hotspots[6] = new ChunkOrderHotspot(tx, by, addition * 6);
hotspots[7] = new ChunkOrderHotspot(mx, ty, addition * 7);
hotspots[8] = new ChunkOrderHotspot(mx, by, addition * 8);
rcti rect;
ChunkOrder *chunkOrders = new ChunkOrder[this->m_numberOfChunks];
for (index = 0; index < this->m_numberOfChunks; index++) {
determineChunkRect(&rect, index);
chunkOrders[index].setChunkNumber(index);
chunkOrders[index].setX(rect.xmin);
chunkOrders[index].setY(rect.ymin);
chunkOrders[index].determineDistance(hotspots, 9);
}
sort(&chunkOrders[0], &chunkOrders[this->m_numberOfChunks]);
for (index = 0; index < this->m_numberOfChunks; index++) {
chunkOrder[index] = chunkOrders[index].getChunkNumber();
}
delete hotspots[0];
delete hotspots[1];
delete hotspots[2];
delete hotspots[3];
delete hotspots[4];
delete hotspots[5];
delete hotspots[6];
delete hotspots[7];
delete hotspots[8];
delete[] hotspots;
delete[] chunkOrders;
}
break;
case COM_TO_TOP_DOWN:
default:
break;
}
bool breaked = false;
bool finished = false;
unsigned int startIndex = 0;
const int maxNumberEvaluated = BLI_system_thread_count() * 2;
while (!finished && !breaked) {
bool startEvaluated = false;
finished = true;
int numberEvaluated = 0;
for (index = startIndex; index < this->m_numberOfChunks && numberEvaluated < maxNumberEvaluated; index++) {
chunkNumber = chunkOrder[index];
int yChunk = chunkNumber / this->m_numberOfXChunks;
int xChunk = chunkNumber - (yChunk * this->m_numberOfXChunks);
const ChunkExecutionState state = this->m_chunkExecutionStates[chunkNumber];
if (state == COM_ES_NOT_SCHEDULED) {
scheduleChunkWhenPossible(graph, xChunk, yChunk);
finished = false;
startEvaluated = true;
numberEvaluated++;
WM_main_add_notifier(NC_WINDOW | ND_DRAW, NULL);
}
else if (state == COM_ES_SCHEDULED) {
finished = false;
startEvaluated = true;
numberEvaluated++;
}
else if (state == COM_ES_EXECUTED && !startEvaluated) {
startIndex = index + 1;
}
}
WorkScheduler::finish();
if (bTree->test_break && bTree->test_break(bTree->tbh)) {
breaked = true;
}
}
delete[] chunkOrder;
}
MemoryBuffer **ExecutionGroup::getInputBuffersOpenCL(int chunkNumber)
{
rcti rect;
vector<MemoryProxy *> memoryproxies;
unsigned int index;
determineChunkRect(&rect, chunkNumber);
this->determineDependingMemoryProxies(&memoryproxies);
MemoryBuffer **memoryBuffers = new MemoryBuffer *[this->m_cachedMaxReadBufferOffset];
for (index = 0; index < this->m_cachedMaxReadBufferOffset; index++) {
memoryBuffers[index] = NULL;
}
rcti output;
for (index = 0; index < this->m_cachedReadOperations.size(); index++) {
ReadBufferOperation *readOperation = (ReadBufferOperation *)this->m_cachedReadOperations[index];
MemoryProxy *memoryProxy = readOperation->getMemoryProxy();
this->determineDependingAreaOfInterest(&rect, readOperation, &output);
MemoryBuffer *memoryBuffer = memoryProxy->getExecutor()->constructConsolidatedMemoryBuffer(memoryProxy, &output);
memoryBuffers[readOperation->getOffset()] = memoryBuffer;
}
return memoryBuffers;
}
MemoryBuffer *ExecutionGroup::constructConsolidatedMemoryBuffer(MemoryProxy *memoryProxy, rcti *rect)
{
MemoryBuffer *imageBuffer = memoryProxy->getBuffer();
MemoryBuffer *result = new MemoryBuffer(memoryProxy, rect);
result->copyContentFrom(imageBuffer);
return result;
}
void ExecutionGroup::finalizeChunkExecution(int chunkNumber, MemoryBuffer **memoryBuffers)
{
if (this->m_chunkExecutionStates[chunkNumber] == COM_ES_SCHEDULED)
this->m_chunkExecutionStates[chunkNumber] = COM_ES_EXECUTED;
this->m_chunksFinished++;
if (memoryBuffers) {
for (unsigned int index = 0; index < this->m_cachedMaxReadBufferOffset; index++) {
MemoryBuffer *buffer = memoryBuffers[index];
if (buffer) {
if (buffer->isTemporarily()) {
memoryBuffers[index] = NULL;
delete buffer;
}
}
}
delete[] memoryBuffers;
}
if (this->m_bTree) {
// status report is only performed for top level Execution Groups.
float progress = this->m_chunksFinished;
progress /= this->m_numberOfChunks;
this->m_bTree->progress(this->m_bTree->prh, progress);
}
}
inline void ExecutionGroup::determineChunkRect(rcti *rect, const unsigned int xChunk, const unsigned int yChunk) const
{
if (this->m_singleThreaded) {
BLI_rcti_init(rect, 0, this->m_width, 0, this->m_height);
}
else {
const unsigned int minx = xChunk * this->m_chunkSize;
const unsigned int miny = yChunk * this->m_chunkSize;
BLI_rcti_init(rect, minx, min(minx + this->m_chunkSize, this->m_width), miny, min(miny + this->m_chunkSize, this->m_height));
}
}
void ExecutionGroup::determineChunkRect(rcti *rect, const unsigned int chunkNumber) const
{
const unsigned int yChunk = chunkNumber / this->m_numberOfXChunks;
const unsigned int xChunk = chunkNumber - (yChunk * this->m_numberOfXChunks);
determineChunkRect(rect, xChunk, yChunk);
}
MemoryBuffer *ExecutionGroup::allocateOutputBuffer(int chunkNumber, rcti *rect)
{
// we asume that this method is only called from complex execution groups.
NodeOperation *operation = this->getOutputNodeOperation();
if (operation->isWriteBufferOperation()) {
WriteBufferOperation *writeOperation = (WriteBufferOperation *)operation;
MemoryBuffer *buffer = new MemoryBuffer(writeOperation->getMemoryProxy(), rect);
return buffer;
}
return NULL;
}
bool ExecutionGroup::scheduleAreaWhenPossible(ExecutionSystem *graph, rcti *area)
{
if (this->m_singleThreaded) {
return scheduleChunkWhenPossible(graph, 0, 0);
}
// find all chunks inside the rect
// determine minxchunk, minychunk, maxxchunk, maxychunk where x and y are chunknumbers
float chunkSizef = this->m_chunkSize;
int indexx, indexy;
int minxchunk = floor(area->xmin / chunkSizef);
int maxxchunk = ceil((area->xmax - 1) / chunkSizef);
int minychunk = floor(area->ymin / chunkSizef);
int maxychunk = ceil((area->ymax - 1) / chunkSizef);
minxchunk = MAX2(minxchunk, 0);
minychunk = MAX2(minychunk, 0);
maxxchunk = MIN2(maxxchunk, this->m_numberOfXChunks);
maxychunk = MIN2(maxychunk, this->m_numberOfYChunks);
bool result = true;
for (indexx = minxchunk; indexx < maxxchunk; indexx++) {
for (indexy = minychunk; indexy < maxychunk; indexy++) {
if (!scheduleChunkWhenPossible(graph, indexx, indexy)) {
result = false;
}
}
}
return result;
}
bool ExecutionGroup::scheduleChunk(unsigned int chunkNumber)
{
if (this->m_chunkExecutionStates[chunkNumber] == COM_ES_NOT_SCHEDULED) {
this->m_chunkExecutionStates[chunkNumber] = COM_ES_SCHEDULED;
WorkScheduler::schedule(this, chunkNumber);
return true;
}
return false;
}
bool ExecutionGroup::scheduleChunkWhenPossible(ExecutionSystem *graph, int xChunk, int yChunk)
{
if (xChunk < 0 || xChunk >= (int)this->m_numberOfXChunks) {
return true;
}
if (yChunk < 0 || yChunk >= (int)this->m_numberOfYChunks) {
return true;
}
int chunkNumber = yChunk * this->m_numberOfXChunks + xChunk;
// chunk is already executed
if (this->m_chunkExecutionStates[chunkNumber] == COM_ES_EXECUTED) {
return true;
}
// chunk is scheduled, but not executed
if (this->m_chunkExecutionStates[chunkNumber] == COM_ES_SCHEDULED) {
return false;
}
// chunk is nor executed nor scheduled.
vector<MemoryProxy *> memoryProxies;
this->determineDependingMemoryProxies(&memoryProxies);
rcti rect;
determineChunkRect(&rect, xChunk, yChunk);
unsigned int index;
bool canBeExecuted = true;
rcti area;
for (index = 0; index < this->m_cachedReadOperations.size(); index++) {
ReadBufferOperation *readOperation = (ReadBufferOperation *)this->m_cachedReadOperations[index];
BLI_rcti_init(&area, 0, 0, 0, 0);
MemoryProxy *memoryProxy = memoryProxies[index];
determineDependingAreaOfInterest(&rect, readOperation, &area);
ExecutionGroup *group = memoryProxy->getExecutor();
if (group != NULL) {
if (!group->scheduleAreaWhenPossible(graph, &area)) {
canBeExecuted = false;
}
}
else {
throw "ERROR";
}
}
if (canBeExecuted) {
scheduleChunk(chunkNumber);
}
return false;
}
void ExecutionGroup::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
{
this->getOutputNodeOperation()->determineDependingAreaOfInterest(input, readOperation, output);
}
void ExecutionGroup::determineDependingMemoryProxies(vector<MemoryProxy *> *memoryProxies)
{
unsigned int index;
for (index = 0; index < this->m_cachedReadOperations.size(); index++) {
ReadBufferOperation *readOperation = (ReadBufferOperation *) this->m_cachedReadOperations[index];
memoryProxies->push_back(readOperation->getMemoryProxy());
}
}
bool ExecutionGroup::isOpenCL()
{
return this->m_openCL;
}
|