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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
|
// Copyright 2018 Blender Foundation. All rights reserved.
//
// 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.
//
// Author: Sergey Sharybin
#include "internal/opensubdiv_evaluator_internal.h"
#include <cassert>
#include <cstdio>
#include <vector>
#ifdef _MSC_VER
# include <iso646.h>
#endif
#include <opensubdiv/far/patchMap.h>
#include <opensubdiv/far/patchTable.h>
#include <opensubdiv/far/patchTableFactory.h>
#include <opensubdiv/osd/cpuEvaluator.h>
#include <opensubdiv/osd/cpuPatchTable.h>
#include <opensubdiv/osd/cpuVertexBuffer.h>
#include <opensubdiv/osd/mesh.h>
#include <opensubdiv/osd/types.h>
#include "MEM_guardedalloc.h"
#include "internal/opensubdiv_topology_refiner_internal.h"
#include "opensubdiv_topology_refiner_capi.h"
using OpenSubdiv::Osd::BufferDescriptor;
using OpenSubdiv::Osd::CpuEvaluator;
using OpenSubdiv::Osd::CpuPatchTable;
using OpenSubdiv::Osd::CpuVertexBuffer;
using OpenSubdiv::Osd::PatchCoord;
using OpenSubdiv::Far::PatchMap;
using OpenSubdiv::Far::PatchTable;
using OpenSubdiv::Far::PatchTableFactory;
using OpenSubdiv::Far::StencilTable;
using OpenSubdiv::Far::StencilTableFactory;
using OpenSubdiv::Far::TopologyRefiner;
namespace opensubdiv_capi {
namespace {
// Helper class to wrap numerous of patch coordinates into a buffer.
// Used to pass coordinates to the CPU evaluator. Other evaluators are not
// supported.
class PatchCoordBuffer : public std::vector<PatchCoord> {
public:
static PatchCoordBuffer* Create(int size) {
PatchCoordBuffer* buffer = new PatchCoordBuffer();
buffer->resize(size);
return buffer;
}
PatchCoord* BindCpuBuffer() {
return reinterpret_cast<PatchCoord*>(&(*this)[0]);
}
int GetNumVertices() {
return size();
}
void UpdateData(const PatchCoord* patch_coords, int num_patch_coords) {
memcpy(&(*this)[0],
reinterpret_cast<const void*>(patch_coords),
sizeof(PatchCoord) * num_patch_coords);
}
};
// Helper class to wrap single of patch coord into a buffer. Used to pass
// coordinates to the CPU evaluator. Other evaluators are not supported.
class SinglePatchCoordBuffer {
public:
static SinglePatchCoordBuffer* Create() {
return new SinglePatchCoordBuffer();
}
SinglePatchCoordBuffer() {
}
explicit SinglePatchCoordBuffer(const PatchCoord &patch_coord)
: patch_coord_(patch_coord) {
}
PatchCoord* BindCpuBuffer() {
return &patch_coord_;
}
int GetNumVertices() {
return 1;
}
void UpdateData(const PatchCoord &patch_coord) {
patch_coord_ = patch_coord;
}
protected:
PatchCoord patch_coord_;
};
// Helper class which is aimed to be used in cases when buffer is small enough
// and better to be allocated in stack rather than in heap.
//
// TODO(sergey): Check if bare arrays could be used by CPU evaluator.
template <int element_size, int num_vertices>
class StackAllocatedBuffer {
public:
static PatchCoordBuffer* Create(int /*size*/) {
// TODO(sergey): Validate that requested dize is smaller than static
// stack memory size.
return new StackAllocatedBuffer<element_size, num_vertices>();
}
float* BindCpuBuffer() {
return &data_[0];
}
int GetNumVertices() {
return num_vertices;
}
// TODO(sergey): Support UpdateData().
protected:
float data_[element_size * num_vertices];
};
// Volatile evaluator which can be used from threads.
//
// TODO(sergey): Make it possible to evaluate coordinates in chunks.
// TODO(sergey): Make it possible to evaluate multiple face varying layers.
// (or maybe, it's cheap to create new evaluator for existing
// topology to evaluate all needed face varying layers?)
template <typename SRC_VERTEX_BUFFER,
typename EVAL_VERTEX_BUFFER,
typename STENCIL_TABLE,
typename PATCH_TABLE,
typename EVALUATOR,
typename DEVICE_CONTEXT = void>
class VolatileEvalOutput {
public:
typedef OpenSubdiv::Osd::EvaluatorCacheT<EVALUATOR> EvaluatorCache;
VolatileEvalOutput(const StencilTable* vertex_stencils,
const StencilTable* varying_stencils,
const StencilTable* face_varying_stencils,
const int face_varying_channel,
const int face_varying_width,
const PatchTable* patch_table,
EvaluatorCache* evaluator_cache = NULL,
DEVICE_CONTEXT* device_context = NULL)
: src_desc_(0, 3, 3),
src_varying_desc_(0, 3, 3),
src_face_varying_desc_(0, face_varying_width, face_varying_width),
face_varying_channel_(face_varying_channel),
face_varying_width_(face_varying_width),
evaluator_cache_(evaluator_cache),
device_context_(device_context) {
// Total number of vertices = coarse points + refined points + local points.
int num_total_vertices = vertex_stencils->GetNumControlVertices() +
vertex_stencils->GetNumStencils();
num_coarse_vertices_ = vertex_stencils->GetNumControlVertices();
using OpenSubdiv::Osd::convertToCompatibleStencilTable;
src_data_ =
SRC_VERTEX_BUFFER::Create(3, num_total_vertices, device_context_);
src_varying_data_ =
SRC_VERTEX_BUFFER::Create(3, num_total_vertices, device_context_);
patch_table_ = PATCH_TABLE::Create(patch_table, device_context_);
patch_coords_ = NULL;
vertex_stencils_ = convertToCompatibleStencilTable<STENCIL_TABLE>(
vertex_stencils, device_context_);
varying_stencils_ = convertToCompatibleStencilTable<STENCIL_TABLE>(
varying_stencils, device_context_);
if (face_varying_stencils != NULL) {
num_coarse_face_varying_vertices_ =
face_varying_stencils->GetNumControlVertices();
const int num_total_face_varying_vertices =
face_varying_stencils->GetNumControlVertices() +
face_varying_stencils->GetNumStencils();
src_face_varying_data_ =
EVAL_VERTEX_BUFFER::Create(2,
num_total_face_varying_vertices,
device_context_);
face_varying_stencils_ = convertToCompatibleStencilTable<STENCIL_TABLE>(
face_varying_stencils, device_context_);
} else {
num_coarse_face_varying_vertices_ = 0;
src_face_varying_data_ = NULL;
face_varying_stencils_ = NULL;
}
}
~VolatileEvalOutput() {
delete src_data_;
delete src_varying_data_;
delete src_face_varying_data_;
delete patch_table_;
delete vertex_stencils_;
delete varying_stencils_;
delete face_varying_stencils_;
}
// TODO(sergey): Implement binding API.
void updateData(const float* src, int start_vertex, int num_vertices) {
src_data_->UpdateData(src, start_vertex, num_vertices, device_context_);
}
void updateVaryingData(const float* src, int start_vertex, int num_vertices) {
src_varying_data_->UpdateData(src,
start_vertex,
num_vertices,
device_context_);
}
void updateFaceVaryingData(const float* src,
int start_vertex,
int num_vertices) {
src_face_varying_data_->UpdateData(src,
start_vertex,
num_vertices,
device_context_);
}
bool hasVaryingData() const {
// return varying_stencils_ != NULL;
// TODO(sergey): Check this based on actual topology.
return false;
}
bool hasFaceVaryingData() const {
return face_varying_stencils_ != NULL;
}
void refine() {
// Evaluate vertex positions.
BufferDescriptor dst_desc = src_desc_;
dst_desc.offset += num_coarse_vertices_ * src_desc_.stride;
const EVALUATOR* eval_instance =
OpenSubdiv::Osd::GetEvaluator<EVALUATOR>(evaluator_cache_,
src_desc_,
dst_desc,
device_context_);
EVALUATOR::EvalStencils(src_data_, src_desc_,
src_data_, dst_desc,
vertex_stencils_,
eval_instance,
device_context_);
// Evaluate varying data.
if (hasVaryingData()) {
BufferDescriptor dst_varying_desc = src_varying_desc_;
dst_varying_desc.offset +=
num_coarse_vertices_ * src_varying_desc_.stride;
eval_instance = OpenSubdiv::Osd::GetEvaluator<EVALUATOR>(
evaluator_cache_,
src_varying_desc_,
dst_varying_desc,
device_context_);
EVALUATOR::EvalStencils(src_varying_data_, src_varying_desc_,
src_varying_data_, dst_varying_desc,
varying_stencils_,
eval_instance, device_context_);
}
// Evaluate face-varying data.
if (hasFaceVaryingData()) {
BufferDescriptor dst_face_varying_desc = src_face_varying_desc_;
dst_face_varying_desc.offset += num_coarse_face_varying_vertices_ *
src_face_varying_desc_.stride;
eval_instance = OpenSubdiv::Osd::GetEvaluator<EVALUATOR>(
evaluator_cache_,
src_face_varying_desc_,
dst_face_varying_desc,
device_context_);
EVALUATOR::EvalStencils(src_face_varying_data_, src_face_varying_desc_,
src_face_varying_data_, dst_face_varying_desc,
face_varying_stencils_,
eval_instance,
device_context_);
}
}
void evalPatchCoord(const PatchCoord& patch_coord, float P[3]) {
StackAllocatedBuffer<6, 1> vertex_data;
// TODO(sergey): Varying data is interleaved in vertex array, so need to
// adjust stride if there is a varying data.
// BufferDescriptor vertex_desc(0, 3, 6);
BufferDescriptor vertex_desc(0, 3, 3);
SinglePatchCoordBuffer patch_coord_buffer(patch_coord);
const EVALUATOR* eval_instance =
OpenSubdiv::Osd::GetEvaluator<EVALUATOR>(evaluator_cache_,
src_desc_,
vertex_desc,
device_context_);
EVALUATOR::EvalPatches(src_data_,
src_desc_,
&vertex_data,
vertex_desc,
patch_coord_buffer.GetNumVertices(),
&patch_coord_buffer,
patch_table_,
eval_instance,
device_context_);
const float* refined_vertices = vertex_data.BindCpuBuffer();
memcpy(P, refined_vertices, sizeof(float) * 3);
}
void evalPatchesWithDerivatives(const PatchCoord& patch_coord,
float P[3],
float dPdu[3], float dPdv[3]) {
StackAllocatedBuffer<6, 1> vertex_data, derivatives;
// TODO(sergey): Varying data is interleaved in vertex array, so need to
// adjust stride if there is a varying data.
// BufferDescriptor vertex_desc(0, 3, 6);
BufferDescriptor vertex_desc(0, 3, 3);
BufferDescriptor du_desc(0, 3, 6), dv_desc(3, 3, 6);
SinglePatchCoordBuffer patch_coord_buffer(patch_coord);
const EVALUATOR* eval_instance =
OpenSubdiv::Osd::GetEvaluator<EVALUATOR>(evaluator_cache_,
src_desc_,
vertex_desc,
du_desc, dv_desc,
device_context_);
EVALUATOR::EvalPatches(src_data_, src_desc_,
&vertex_data, vertex_desc,
&derivatives, du_desc,
&derivatives, dv_desc,
patch_coord_buffer.GetNumVertices(),
&patch_coord_buffer,
patch_table_,
eval_instance,
device_context_);
const float* refined_vertices = vertex_data.BindCpuBuffer();
memcpy(P, refined_vertices, sizeof(float) * 3);
if (dPdu != NULL || dPdv != NULL) {
const float* refined_derivatives = derivatives.BindCpuBuffer();
if (dPdu != NULL) {
memcpy(dPdu, refined_derivatives, sizeof(float) * 3);
}
if (dPdv != NULL) {
memcpy(dPdv, refined_derivatives + 3, sizeof(float) * 3);
}
}
}
void evalPatchVarying(const PatchCoord& patch_coord, float varying[3]) {
StackAllocatedBuffer<6, 1> varying_data;
BufferDescriptor varying_desc(3, 3, 6);
SinglePatchCoordBuffer patch_coord_buffer(patch_coord);
const EVALUATOR* eval_instance =
OpenSubdiv::Osd::GetEvaluator<EVALUATOR>(evaluator_cache_,
src_varying_desc_,
varying_desc,
device_context_);
EVALUATOR::EvalPatchesVarying(src_varying_data_, src_varying_desc_,
&varying_data, varying_desc,
patch_coord_buffer.GetNumVertices(),
&patch_coord_buffer,
patch_table_,
eval_instance,
device_context_);
const float* refined_varying = varying_data.BindCpuBuffer();
memcpy(varying, refined_varying, sizeof(float) * 3);
}
void evalPatchFaceVarying(const PatchCoord& patch_coord,
float face_varying[2]) {
StackAllocatedBuffer<2, 1> face_varying_data;
BufferDescriptor face_varying_desc(0, 2, 2);
SinglePatchCoordBuffer patch_coord_buffer(patch_coord);
const EVALUATOR* eval_instance =
OpenSubdiv::Osd::GetEvaluator<EVALUATOR>(evaluator_cache_,
src_face_varying_desc_,
face_varying_desc,
device_context_);
EVALUATOR::EvalPatchesFaceVarying(
src_face_varying_data_, src_face_varying_desc_,
&face_varying_data, face_varying_desc,
patch_coord_buffer.GetNumVertices(),
&patch_coord_buffer,
patch_table_,
face_varying_channel_,
eval_instance,
device_context_);
const float* refined_face_varying = face_varying_data.BindCpuBuffer();
memcpy(face_varying, refined_face_varying, sizeof(float) * 2);
}
private:
SRC_VERTEX_BUFFER* src_data_;
SRC_VERTEX_BUFFER* src_varying_data_;
EVAL_VERTEX_BUFFER* src_face_varying_data_;
PatchCoordBuffer* patch_coords_;
PATCH_TABLE* patch_table_;
BufferDescriptor src_desc_;
BufferDescriptor src_varying_desc_;
BufferDescriptor src_face_varying_desc_;
int num_coarse_vertices_;
int face_varying_channel_;
int face_varying_width_;
int num_coarse_face_varying_vertices_;
const STENCIL_TABLE* vertex_stencils_;
const STENCIL_TABLE* varying_stencils_;
const STENCIL_TABLE* face_varying_stencils_;
EvaluatorCache* evaluator_cache_;
DEVICE_CONTEXT* device_context_;
};
} // namespace
// Note: Define as a class instead of typedcef to make it possible
// to have anonymous class in opensubdiv_evaluator_internal.h
class CpuEvalOutput : public VolatileEvalOutput<CpuVertexBuffer,
CpuVertexBuffer,
StencilTable,
CpuPatchTable,
CpuEvaluator> {
public:
CpuEvalOutput(const StencilTable* vertex_stencils,
const StencilTable* varying_stencils,
const StencilTable* face_varying_stencils,
const int face_varying_channel,
const int face_varying_width,
const PatchTable* patch_table,
EvaluatorCache* evaluator_cache = NULL)
: VolatileEvalOutput<CpuVertexBuffer,
CpuVertexBuffer,
StencilTable,
CpuPatchTable,
CpuEvaluator> (vertex_stencils,
varying_stencils,
face_varying_stencils,
face_varying_channel,
face_varying_width,
patch_table,
evaluator_cache) {
}
};
////////////////////////////////////////////////////////////////////////////////
// Evaluator wrapper for anonymous API.
CpuEvalOutputAPI::CpuEvalOutputAPI(CpuEvalOutput* implementation,
OpenSubdiv::Far::PatchMap* patch_map)
: implementation_(implementation),
patch_map_(patch_map) {
}
CpuEvalOutputAPI::~CpuEvalOutputAPI() {
delete implementation_;
}
void CpuEvalOutputAPI::setCoarsePositions(const float* positions,
const int start_vertex_index,
const int num_vertices) {
// TODO(sergey): Add sanity check on indices.
implementation_->updateData(positions, start_vertex_index, num_vertices);
}
void CpuEvalOutputAPI::setVaryingData(const float* varying_data,
const int start_vertex_index,
const int num_vertices) {
// TODO(sergey): Add sanity check on indices.
implementation_->updateVaryingData(varying_data,
start_vertex_index,
num_vertices);
}
void CpuEvalOutputAPI::setFaceVaryingData(const float* face_varying_data,
const int start_vertex_index,
const int num_vertices) {
// TODO(sergey): Add sanity check on indices.
implementation_->updateFaceVaryingData(face_varying_data,
start_vertex_index,
num_vertices);
}
void CpuEvalOutputAPI::setCoarsePositionsFromBuffer(
const void* buffer,
const int start_offset,
const int stride,
const int start_vertex_index,
const int num_vertices) {
// TODO(sergey): Add sanity check on indices.
const unsigned char* current_buffer = (unsigned char *)buffer;
current_buffer += start_offset;
for (int i = 0; i < num_vertices; ++i) {
const int current_vertex_index = start_vertex_index + i;
implementation_->updateData(reinterpret_cast<const float*>(current_buffer),
current_vertex_index, 1);
current_buffer += stride;
}
}
void CpuEvalOutputAPI::setVaryingDataFromBuffer(
const void* buffer,
const int start_offset,
const int stride,
const int start_vertex_index,
const int num_vertices) {
// TODO(sergey): Add sanity check on indices.
const unsigned char* current_buffer = (unsigned char *)buffer;
current_buffer += start_offset;
for (int i = 0; i < num_vertices; ++i) {
const int current_vertex_index = start_vertex_index + i;
implementation_->updateVaryingData(
reinterpret_cast<const float*>(current_buffer),
current_vertex_index, 1);
current_buffer += stride;
}
}
void CpuEvalOutputAPI::setFaceVaryingDataFromBuffer(
const void* buffer,
const int start_offset,
const int stride,
const int start_vertex_index,
const int num_vertices) {
// TODO(sergey): Add sanity check on indices.
const unsigned char* current_buffer = (unsigned char *)buffer;
current_buffer += start_offset;
for (int i = 0; i < num_vertices; ++i) {
const int current_vertex_index = start_vertex_index + i;
implementation_->updateFaceVaryingData(
reinterpret_cast<const float*>(current_buffer),
current_vertex_index, 1);
current_buffer += stride;
}
}
void CpuEvalOutputAPI::refine() {
implementation_->refine();
}
void CpuEvalOutputAPI::evaluateLimit(const int ptex_face_index,
float face_u, float face_v,
float P[3], float dPdu[3], float dPdv[3]) {
assert(face_u >= 0.0f);
assert(face_u <= 1.0f);
assert(face_v >= 0.0f);
assert(face_v <= 1.0f);
const PatchTable::PatchHandle* handle =
patch_map_->FindPatch(ptex_face_index, face_u, face_v);
PatchCoord patch_coord(*handle, face_u, face_v);
if (dPdu != NULL || dPdv != NULL) {
implementation_->evalPatchesWithDerivatives(patch_coord, P, dPdu, dPdv);
} else {
implementation_->evalPatchCoord(patch_coord, P);
}}
void CpuEvalOutputAPI::evaluateVarying(const int ptex_face_index,
float face_u, float face_v,
float varying[3]) {
assert(face_u >= 0.0f);
assert(face_u <= 1.0f);
assert(face_v >= 0.0f);
assert(face_v <= 1.0f);
const PatchTable::PatchHandle* handle =
patch_map_->FindPatch(ptex_face_index, face_u, face_v);
PatchCoord patch_coord(*handle, face_u, face_v);
implementation_->evalPatchVarying(patch_coord, varying);
}
void CpuEvalOutputAPI::evaluateFaceVarying(const int ptex_face_index,
float face_u, float face_v,
float face_varying[2]) {
assert(face_u >= 0.0f);
assert(face_u <= 1.0f);
assert(face_v >= 0.0f);
assert(face_v <= 1.0f);
const PatchTable::PatchHandle* handle =
patch_map_->FindPatch(ptex_face_index, face_u, face_v);
PatchCoord patch_coord(*handle, face_u, face_v);
implementation_->evalPatchFaceVarying(patch_coord, face_varying);
}
} // namespace opensubdiv_capi
OpenSubdiv_EvaluatorInternal::OpenSubdiv_EvaluatorInternal()
: eval_output(NULL),
patch_map(NULL),
patch_table(NULL) {
}
OpenSubdiv_EvaluatorInternal::~OpenSubdiv_EvaluatorInternal() {
delete eval_output;
delete patch_map;
delete patch_table;
}
OpenSubdiv_EvaluatorInternal* openSubdiv_createEvaluatorInternal(
OpenSubdiv_TopologyRefiner* topology_refiner) {
TopologyRefiner* refiner = topology_refiner->internal->osd_topology_refiner;
if (refiner == NULL) {
// Happens on bad topology.
return NULL;
}
// TODO(sergey): Base this on actual topology.
// const bool bas_varying_data = false;
const bool has_face_varying_data =
(refiner->GetNumFVarChannels() != 0);
const int level = topology_refiner->getSubdivisionLevel(topology_refiner);
// TODO(sergey): Query from topology refiner.
const bool is_adaptive = topology_refiner->getIsAdaptive(topology_refiner);
// Refine the topology with given settings.
// TODO(sergey): What if topology is already refined?
if (is_adaptive) {
TopologyRefiner::AdaptiveOptions options(level);
options.considerFVarChannels = has_face_varying_data;
options.useInfSharpPatch = false;
refiner->RefineAdaptive(options);
} else {
TopologyRefiner::UniformOptions options(level);
refiner->RefineUniform(options);
}
// Generate stencil table to update the bi-cubic patches control vertices
// after they have been re-posed (both for vertex & varying interpolation).
//
// Vertex stencils.
StencilTableFactory::Options vertex_stencil_options;
vertex_stencil_options.generateOffsets = true;
vertex_stencil_options.generateIntermediateLevels = is_adaptive;
const StencilTable* vertex_stencils =
StencilTableFactory::Create(*refiner, vertex_stencil_options);
// Varying stencils.
//
// TODO(sergey): Seems currently varying stencils are always required in
// OpenSubdiv itself.
const StencilTable* varying_stencils = NULL;
StencilTableFactory::Options varying_stencil_options;
varying_stencil_options.generateOffsets = true;
varying_stencil_options.generateIntermediateLevels = is_adaptive;
varying_stencil_options.interpolationMode =
StencilTableFactory::INTERPOLATE_VARYING;
varying_stencils =
StencilTableFactory::Create(*refiner, varying_stencil_options);
// Face warying stencil.
const StencilTable* face_varying_stencils = NULL;
if (has_face_varying_data) {
StencilTableFactory::Options face_varying_stencil_options;
face_varying_stencil_options.generateOffsets = true;
face_varying_stencil_options.generateIntermediateLevels = is_adaptive;
face_varying_stencil_options.interpolationMode =
StencilTableFactory::INTERPOLATE_FACE_VARYING;
// TODO(sergey): Make it configurable which face varying channel is being
// interpolated.
face_varying_stencil_options.fvarChannel = 0;
face_varying_stencils =
StencilTableFactory::Create(*refiner, face_varying_stencil_options);
}
// Generate bi-cubic patch table for the limit surface.
// TODO(sergey): Ideally we would want to expose end-cap settings via
// C-API to make it more generic. Currently it matches old Blender's
// subsurf code.
PatchTableFactory::Options patch_options(level);
patch_options.SetEndCapType(PatchTableFactory::Options::ENDCAP_BSPLINE_BASIS);
patch_options.useInfSharpPatch = false;
patch_options.generateFVarTables = has_face_varying_data;
patch_options.generateFVarLegacyLinearPatches = false;
const PatchTable* patch_table = PatchTableFactory::Create(
*refiner, patch_options);
// Append local points stencils.
const StencilTable* local_point_stencil_table =
patch_table->GetLocalPointStencilTable();
if (local_point_stencil_table != NULL) {
const StencilTable* table =
StencilTableFactory::AppendLocalPointStencilTable(
*refiner, vertex_stencils, local_point_stencil_table);
delete vertex_stencils;
vertex_stencils = table;
}
const StencilTable* local_point_varying_stencil_table =
patch_table->GetLocalPointVaryingStencilTable();
if (local_point_varying_stencil_table != NULL) {
const StencilTable* table =
StencilTableFactory::AppendLocalPointStencilTable(
*refiner, varying_stencils, local_point_varying_stencil_table);
delete varying_stencils;
varying_stencils = table;
}
const StencilTable* local_point_face_varying_stencil_table =
patch_table->GetLocalPointFaceVaryingStencilTable();
if (local_point_face_varying_stencil_table != NULL) {
const StencilTable* table =
StencilTableFactory::AppendLocalPointStencilTableFaceVarying(
*refiner,
face_varying_stencils,
local_point_face_varying_stencil_table);
delete face_varying_stencils;
face_varying_stencils = table;
}
// Create OpenSubdiv's CPU side evaluator.
// TODO(sergey): Make it possible to use different evaluators.
opensubdiv_capi::CpuEvalOutput* eval_output =
new opensubdiv_capi::CpuEvalOutput(vertex_stencils,
varying_stencils,
face_varying_stencils,
0, 2,
patch_table);
OpenSubdiv::Far::PatchMap* patch_map = new PatchMap(*patch_table);
// Wrap everything we need into an object which we control from our side.
OpenSubdiv_EvaluatorInternal* evaluator_descr;
evaluator_descr = OBJECT_GUARDED_NEW(OpenSubdiv_EvaluatorInternal);
evaluator_descr->eval_output =
new opensubdiv_capi::CpuEvalOutputAPI(eval_output, patch_map);
evaluator_descr->patch_map = patch_map;
evaluator_descr->patch_table = patch_table;
// TOOD(sergey): Look into whether we've got duplicated stencils arrays.
delete vertex_stencils;
delete varying_stencils;
delete face_varying_stencils;
return evaluator_descr;
}
void openSubdiv_deleteEvaluatorInternal(
OpenSubdiv_EvaluatorInternal* evaluator) {
OBJECT_GUARDED_DELETE(evaluator, OpenSubdiv_EvaluatorInternal);
}
|