1 //
2 //   Copyright 2015 Pixar
3 //
4 //   Licensed under the Apache License, Version 2.0 (the "Apache License")
5 //   with the following modification; you may not use this file except in
6 //   compliance with the Apache License and the following modification to it:
7 //   Section 6. Trademarks. is deleted and replaced with:
8 //
9 //   6. Trademarks. This License does not grant permission to use the trade
10 //      names, trademarks, service marks, or product names of the Licensor
11 //      and its affiliates, except as required to comply with Section 4(c) of
12 //      the License and to reproduce the content of the NOTICE file.
13 //
14 //   You may obtain a copy of the Apache License at
15 //
16 //       http://www.apache.org/licenses/LICENSE-2.0
17 //
18 //   Unless required by applicable law or agreed to in writing, software
19 //   distributed under the Apache License with the above modification is
20 //   distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 //   KIND, either express or implied. See the Apache License for the specific
22 //   language governing permissions and limitations under the Apache License.
23 //
24 
25 #include "../osd/cpuPatchTable.h"
26 #include "../far/patchDescriptor.h"
27 
28 #include <iostream>
29 
30 namespace OpenSubdiv {
31 namespace OPENSUBDIV_VERSION {
32 
33 namespace Osd {
34 
CpuPatchTable(const Far::PatchTable * farPatchTable)35 CpuPatchTable::CpuPatchTable(const Far::PatchTable *farPatchTable) {
36     int nPatchArrays = farPatchTable->GetNumPatchArrays();
37 
38     // count
39     int numPatches = 0;
40     int numIndices = 0;
41     for (int j = 0; j < nPatchArrays; ++j) {
42         int nPatch = farPatchTable->GetNumPatches(j);
43         int nCV = farPatchTable->GetPatchArrayDescriptor(j).GetNumControlVertices();
44         numPatches += nPatch;
45         numIndices += nPatch * nCV;
46     }
47     _patchArrays.reserve(nPatchArrays);
48     _indexBuffer.reserve(numIndices);
49 
50     _varyingPatchArrays.reserve(nPatchArrays);
51     _varyingIndexBuffer.reserve(
52         numPatches*farPatchTable->GetVaryingPatchDescriptor().GetNumControlVertices());
53 
54     _fvarPatchArrays.resize(farPatchTable->GetNumFVarChannels());
55     _fvarIndexBuffers.resize(farPatchTable->GetNumFVarChannels());
56     _fvarParamBuffers.resize(farPatchTable->GetNumFVarChannels());
57     for (int fvc=0; fvc<farPatchTable->GetNumFVarChannels(); ++fvc) {
58         _fvarPatchArrays[fvc].reserve(nPatchArrays);
59         _fvarIndexBuffers[fvc].reserve(
60             numPatches * farPatchTable->GetFVarValueStride(fvc));
61         _fvarParamBuffers[fvc].reserve(numPatches);
62     }
63     _patchParamBuffer.reserve(numPatches);
64 
65     // for each patchArray
66     for (int j = 0; j < nPatchArrays; ++j) {
67         int numPatchesThisArray = farPatchTable->GetNumPatches(j);
68 
69         // create vertex array and append indices to buffer:
70         PatchArray patchArray(
71             farPatchTable->GetPatchArrayDescriptor(j),
72             numPatchesThisArray,
73             (int)_indexBuffer.size(), (int)_patchParamBuffer.size());
74         _patchArrays.push_back(patchArray);
75 
76         Far::ConstIndexArray indices = farPatchTable->GetPatchArrayVertices(j);
77         _indexBuffer.insert(_indexBuffer.end(), indices.begin(), indices.end());
78 
79         // create varying array and append indices to buffer:
80         PatchArray varyingPatchArray(
81             farPatchTable->GetVaryingPatchDescriptor(),
82             numPatchesThisArray,
83             (int)_varyingIndexBuffer.size(), (int)_patchParamBuffer.size());
84         _varyingPatchArrays.push_back(varyingPatchArray);
85 
86         Far::ConstIndexArray
87             varyingIndices = farPatchTable->GetPatchArrayVaryingVertices(j);
88         _varyingIndexBuffer.insert(_varyingIndexBuffer.end(),
89                 varyingIndices.begin(), varyingIndices.end());
90 
91         // create face-varying arrays for each channel:
92         for (int fvc=0; fvc<farPatchTable->GetNumFVarChannels(); ++fvc) {
93             // create face-varying array and append indices to buffer:
94             PatchArray fvarPatchArray(
95                 farPatchTable->GetFVarPatchDescriptorRegular(fvc),
96                 farPatchTable->GetFVarPatchDescriptorIrregular(fvc),
97                 numPatchesThisArray,
98                 (int)_fvarIndexBuffers[fvc].size(), (int)_fvarParamBuffers[fvc].size());
99             _fvarPatchArrays[fvc].push_back(fvarPatchArray);
100 
101             Far::ConstIndexArray
102                 fvarIndices = farPatchTable->GetPatchArrayFVarValues(j, fvc);
103             _fvarIndexBuffers[fvc].insert(_fvarIndexBuffers[fvc].end(),
104                     fvarIndices.begin(), fvarIndices.end());
105 
106             // append face-varying patch params (converting Far PatchParams to Osd)
107             Far::ConstPatchParamArray
108                 fvarParam = farPatchTable->GetPatchArrayFVarPatchParams(j, fvc);
109 
110             for (int k = 0; k < numPatchesThisArray; ++k) {
111                 PatchParam param;
112                 //param.patchParam = patchParamTable[patchIndex];
113                 param.field0 = fvarParam[k].field0;
114                 param.field1 = fvarParam[k].field1;
115                 param.sharpness = 0.0f;
116                 _fvarParamBuffers[fvc].push_back(param);
117             }
118         }
119 
120         // patchParams bundling
121         // XXX: this process won't be needed if Far::PatchParam includes
122         // sharpness.
123 #if 0
124         // XXX: we need sharpness interface for patcharray or put sharpness
125         //      into patchParam.
126         Far::ConstPatchParamArray patchParams =
127             farPatchTable->GetPatchParams(j);
128         for (int k = 0; k < patchParams.size(); ++k) {
129             float sharpness = 0.0;
130             _patchParamBuffer.push_back(patchParams[k].field0);
131             _patchParamBuffer.push_back(patchParams[k].field1);
132             _patchParamBuffer.push_back(*((unsigned int *)&sharpness));
133         }
134 #else
135         // XXX: workaround. GetPatchParamTable() will be deprecated though.
136         Far::PatchParamTable const & patchParamTable =
137             farPatchTable->GetPatchParamTable();
138         std::vector<Far::Index> const &sharpnessIndexTable =
139             farPatchTable->GetSharpnessIndexTable();
140         int numPatchesJ = farPatchTable->GetNumPatches(j);
141         for (int k = 0; k < numPatchesJ; ++k) {
142             float sharpness = 0.0;
143             int patchIndex = (int)_patchParamBuffer.size();
144             if (patchIndex < (int)sharpnessIndexTable.size()) {
145                 int sharpnessIndex = sharpnessIndexTable[patchIndex];
146                 if (sharpnessIndex >= 0)
147                     sharpness = farPatchTable->GetSharpnessValues()[sharpnessIndex];
148             }
149             PatchParam param;
150             //param.patchParam = patchParamTable[patchIndex];
151             param.field0 = patchParamTable[patchIndex].field0;
152             param.field1 = patchParamTable[patchIndex].field1;
153             param.sharpness = sharpness;
154             _patchParamBuffer.push_back(param);
155         }
156 #endif
157     }
158 }
159 
160 }  // end namespace Osd
161 
162 }  // end namespace OPENSUBDIV_VERSION
163 }  // end namespace OpenSubdiv
164 
165