Merge branch 'blender2.7'
[blender.git] / source / blender / blenkernel / intern / CCGSubSurf.h
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 #ifndef __CCGSUBSURF_H__
22 #define __CCGSUBSURF_H__
23
24 /** \file blender/blenkernel/intern/CCGSubSurf.h
25  *  \ingroup bke
26  */
27
28 typedef void* CCGMeshHDL;
29 typedef void* CCGVertHDL;
30 typedef void* CCGEdgeHDL;
31 typedef void* CCGFaceHDL;
32
33 typedef struct CCGSubSurf CCGSubSurf;
34 typedef struct CCGVert CCGVert;
35 typedef struct CCGEdge CCGEdge;
36 typedef struct CCGFace CCGFace;
37
38 typedef struct CCGMeshIFC {
39         int                     vertUserSize, edgeUserSize, faceUserSize;
40         int                     numLayers;
41         int                     vertDataSize;
42         int                     simpleSubdiv;
43 } CCGMeshIFC;
44
45 /***/
46
47 typedef void* CCGAllocatorHDL;
48
49 typedef struct CCGAllocatorIFC {
50         void*           (*alloc)                        (CCGAllocatorHDL a, int numBytes);
51         void*           (*realloc)                      (CCGAllocatorHDL a, void *ptr, int newSize, int oldSize);
52         void            (*free)                         (CCGAllocatorHDL a, void *ptr);
53         void            (*release)                      (CCGAllocatorHDL a);
54 } CCGAllocatorIFC;
55
56 /* private, so we can allocate on the stack */
57 typedef struct _EHashIterator {
58         struct _EHash *eh;
59         int curBucket;
60         struct _EHEntry *curEntry;
61 } EHashIterator;
62
63 /***/
64
65 typedef enum {
66         eCCGError_None = 0,
67
68         eCCGError_InvalidSyncState,
69         eCCGError_InvalidValue,
70 } CCGError;
71
72 /***/
73
74 #define CCG_OMP_LIMIT   1000000
75
76 /* TODO(sergey): This actually depends on subsurf level as well. */
77 #define CCG_TASK_LIMIT 16
78
79 /***/
80
81 CCGSubSurf*     ccgSubSurf_new  (CCGMeshIFC *ifc, int subdivisionLevels, CCGAllocatorIFC *allocatorIFC, CCGAllocatorHDL allocator);
82 void            ccgSubSurf_free (CCGSubSurf *ss);
83
84 CCGError        ccgSubSurf_initFullSync         (CCGSubSurf *ss);
85 CCGError        ccgSubSurf_initPartialSync      (CCGSubSurf *ss);
86 #ifdef WITH_OPENSUBDIV
87 CCGError        ccgSubSurf_initOpenSubdivSync   (CCGSubSurf *ss);
88 #endif
89
90 CCGError        ccgSubSurf_syncVert             (CCGSubSurf *ss, CCGVertHDL vHDL, const void *vertData, int seam, CCGVert **v_r);
91 CCGError        ccgSubSurf_syncEdge             (CCGSubSurf *ss, CCGEdgeHDL eHDL, CCGVertHDL e_vHDL0, CCGVertHDL e_vHDL1, float crease, CCGEdge **e_r);
92 CCGError        ccgSubSurf_syncFace             (CCGSubSurf *ss, CCGFaceHDL fHDL, int numVerts, CCGVertHDL *vHDLs, CCGFace **f_r);
93
94 CCGError        ccgSubSurf_syncVertDel  (CCGSubSurf *ss, CCGVertHDL vHDL);
95 CCGError        ccgSubSurf_syncEdgeDel  (CCGSubSurf *ss, CCGEdgeHDL eHDL);
96 CCGError        ccgSubSurf_syncFaceDel  (CCGSubSurf *ss, CCGFaceHDL fHDL);
97
98 CCGError        ccgSubSurf_processSync  (CCGSubSurf *ss);
99
100 CCGError        ccgSubSurf_updateFromFaces(CCGSubSurf *ss, int lvl, CCGFace **faces, int numFaces);
101 CCGError        ccgSubSurf_updateToFaces(CCGSubSurf *ss, int lvl, CCGFace **faces, int numFaces);
102 CCGError        ccgSubSurf_updateNormals(CCGSubSurf *ss, CCGFace **faces, int numFaces);
103 CCGError        ccgSubSurf_updateLevels(CCGSubSurf *ss, int lvl, CCGFace **faces, int numFaces);
104 CCGError        ccgSubSurf_stitchFaces(CCGSubSurf *ss, int lvl, CCGFace **faces, int numFaces);
105
106 CCGError        ccgSubSurf_setSubdivisionLevels         (CCGSubSurf *ss, int subdivisionLevels);
107
108 CCGError        ccgSubSurf_setAllowEdgeCreation         (CCGSubSurf *ss, int allowEdgeCreation, float defaultCreaseValue, void *defaultUserData);
109 void            ccgSubSurf_getAllowEdgeCreation         (CCGSubSurf *ss, int *allowEdgeCreation_r, float *defaultCreaseValue_r, void *defaultUserData_r);
110
111 void            ccgSubSurf_getUseAgeCounts                      (CCGSubSurf *ss, int *useAgeCounts_r, int *vertUserOffset_r, int *edgeUserOffset_r, int *faceUserOffset_r);
112 CCGError        ccgSubSurf_setUseAgeCounts                      (CCGSubSurf *ss, int useAgeCounts, int vertUserOffset, int edgeUserOffset, int faceUserOffset);
113
114 CCGError        ccgSubSurf_setCalcVertexNormals         (CCGSubSurf *ss, int useVertNormals, int normalDataOffset);
115 void            ccgSubSurf_setAllocMask                         (CCGSubSurf *ss, int allocMask, int maskOffset);
116
117 void            ccgSubSurf_setNumLayers                         (CCGSubSurf *ss, int numLayers);
118
119 /***/
120
121 int                     ccgSubSurf_getNumVerts                          (const CCGSubSurf *ss);
122 int                     ccgSubSurf_getNumEdges                          (const CCGSubSurf *ss);
123 int                     ccgSubSurf_getNumFaces                          (const CCGSubSurf *ss);
124
125 int                     ccgSubSurf_getSubdivisionLevels         (const CCGSubSurf *ss);
126 int                     ccgSubSurf_getEdgeSize                          (const CCGSubSurf *ss);
127 int                     ccgSubSurf_getEdgeLevelSize                     (const CCGSubSurf *ss, int level);
128 int                     ccgSubSurf_getGridSize                          (const CCGSubSurf *ss);
129 int                     ccgSubSurf_getGridLevelSize                     (const CCGSubSurf *ss, int level);
130 int                     ccgSubSurf_getSimpleSubdiv                      (const CCGSubSurf *ss);
131
132 CCGVert*        ccgSubSurf_getVert                                      (CCGSubSurf *ss, CCGVertHDL v);
133 CCGVertHDL      ccgSubSurf_getVertVertHandle            (CCGVert *v);
134 int                     ccgSubSurf_getVertNumFaces                      (CCGVert *v);
135 CCGFace*        ccgSubSurf_getVertFace                          (CCGVert *v, int index);
136 int                     ccgSubSurf_getVertNumEdges                      (CCGVert *v);
137 CCGEdge*        ccgSubSurf_getVertEdge                          (CCGVert *v, int index);
138
139 int                     ccgSubSurf_getVertAge                           (CCGSubSurf *ss, CCGVert *v);
140 void*           ccgSubSurf_getVertUserData                      (CCGSubSurf *ss, CCGVert *v);
141 void*           ccgSubSurf_getVertData                          (CCGSubSurf *ss, CCGVert *v);
142 void*           ccgSubSurf_getVertLevelData                     (CCGSubSurf *ss, CCGVert *v, int level);
143
144 CCGEdge*        ccgSubSurf_getEdge                                      (CCGSubSurf *ss, CCGEdgeHDL e);
145 CCGEdgeHDL      ccgSubSurf_getEdgeEdgeHandle            (CCGEdge *e);
146 int                     ccgSubSurf_getEdgeNumFaces                      (CCGEdge *e);
147 CCGFace*        ccgSubSurf_getEdgeFace                          (CCGEdge *e, int index);
148 CCGVert*        ccgSubSurf_getEdgeVert0                         (CCGEdge *e);
149 CCGVert*        ccgSubSurf_getEdgeVert1                         (CCGEdge *e);
150 float           ccgSubSurf_getEdgeCrease                        (CCGEdge *e);
151
152 int                     ccgSubSurf_getEdgeAge                           (CCGSubSurf *ss, CCGEdge *e);
153 void*           ccgSubSurf_getEdgeUserData                      (CCGSubSurf *ss, CCGEdge *e);
154 void*           ccgSubSurf_getEdgeDataArray                     (CCGSubSurf *ss, CCGEdge *e);
155 void*           ccgSubSurf_getEdgeData                          (CCGSubSurf *ss, CCGEdge *e, int x);
156 void*           ccgSubSurf_getEdgeLevelData                     (CCGSubSurf *ss, CCGEdge *e, int x, int level);
157
158 CCGFace*        ccgSubSurf_getFace                                      (CCGSubSurf *ss, CCGFaceHDL f);
159 CCGFaceHDL      ccgSubSurf_getFaceFaceHandle            (CCGFace *f);
160 int                     ccgSubSurf_getFaceNumVerts                      (CCGFace *f);
161 CCGVert*        ccgSubSurf_getFaceVert                          (CCGFace *f, int index);
162 CCGEdge*        ccgSubSurf_getFaceEdge                          (CCGFace *f, int index);
163 int                     ccgSubSurf_getFaceEdgeIndex                     (CCGFace *f, CCGEdge *e);
164
165 int                     ccgSubSurf_getFaceAge                           (CCGSubSurf *ss, CCGFace *f);
166 void*           ccgSubSurf_getFaceUserData                      (CCGSubSurf *ss, CCGFace *f);
167 void*           ccgSubSurf_getFaceCenterData            (CCGFace *f);
168 void*           ccgSubSurf_getFaceGridEdgeDataArray     (CCGSubSurf *ss, CCGFace *f, int gridIndex);
169 void*           ccgSubSurf_getFaceGridEdgeData          (CCGSubSurf *ss, CCGFace *f, int gridIndex, int x);
170 void*           ccgSubSurf_getFaceGridDataArray         (CCGSubSurf *ss, CCGFace *f, int gridIndex);
171 void*           ccgSubSurf_getFaceGridData                      (CCGSubSurf *ss, CCGFace *f, int gridIndex, int x, int y);
172
173 int                     ccgSubSurf_getNumFinalVerts             (const CCGSubSurf *ss);
174 int                     ccgSubSurf_getNumFinalEdges             (const CCGSubSurf *ss);
175 int                     ccgSubSurf_getNumFinalFaces             (const CCGSubSurf *ss);
176
177 /***/
178
179 typedef struct _EHashIterator CCGVertIterator;
180 typedef struct _EHashIterator CCGEdgeIterator;
181 typedef struct _EHashIterator CCGFaceIterator;
182
183 void            ccgSubSurf_initVertIterator(CCGSubSurf *ss, CCGVertIterator *viter);
184 void            ccgSubSurf_initEdgeIterator(CCGSubSurf *ss, CCGEdgeIterator *eiter);
185 void            ccgSubSurf_initFaceIterator(CCGSubSurf *ss, CCGFaceIterator *fiter);
186
187 CCGVert*                        ccgVertIterator_getCurrent      (CCGVertIterator *vi);
188 int                                     ccgVertIterator_isStopped       (CCGVertIterator *vi);
189 void                            ccgVertIterator_next            (CCGVertIterator *vi);
190
191 CCGEdge*                        ccgEdgeIterator_getCurrent      (CCGEdgeIterator *ei);
192 int                                     ccgEdgeIterator_isStopped       (CCGEdgeIterator *ei);
193 void                            ccgEdgeIterator_next            (CCGEdgeIterator *ei);
194
195 CCGFace*                        ccgFaceIterator_getCurrent      (CCGFaceIterator *fi);
196 int                                     ccgFaceIterator_isStopped       (CCGFaceIterator *fi);
197 void                            ccgFaceIterator_next            (CCGFaceIterator *fi);
198
199 #ifdef WITH_OPENSUBDIV
200 struct DerivedMesh;
201
202 /* Check if topology changed and evaluators are to be re-created. */
203 void ccgSubSurf_checkTopologyChanged(CCGSubSurf *ss, struct DerivedMesh *dm);
204
205 /* Create topology refiner from give derived mesh which then later will be
206  * used for GL mesh creation.
207  */
208 void ccgSubSurf_prepareTopologyRefiner(CCGSubSurf *ss, struct DerivedMesh *dm);
209
210 /* Make sure GL mesh exists, up to date and ready to draw. */
211 bool ccgSubSurf_prepareGLMesh(CCGSubSurf *ss, bool use_osd_glsl, int active_uv_index);
212
213 /* Draw given partitions of the GL mesh.
214  *
215  * TODO(sergey): fill_quads is actually an invariant and should be part
216  * of the prepare routine.
217  */
218 void ccgSubSurf_drawGLMesh(CCGSubSurf *ss, bool fill_quads,
219                            int start_partition, int num_partitions);
220
221 /* Get number of base faces in a particular GL mesh. */
222 int ccgSubSurf_getNumGLMeshBaseFaces(CCGSubSurf *ss);
223
224 /* Get number of vertices in base faces in a particular GL mesh. */
225 int ccgSubSurf_getNumGLMeshBaseFaceVerts(CCGSubSurf *ss, int face);
226
227 /* Controls whether CCG are needed (Cmeaning CPU evaluation) or fully GPU compute
228  * and draw is allowed.
229  */
230 void ccgSubSurf_setSkipGrids(CCGSubSurf *ss, bool skip_grids);
231 bool ccgSubSurf_needGrids(CCGSubSurf *ss);
232
233 /* Set evaluator's face varying data from UV coordinates.
234  * Used for CPU evaluation.
235  */
236 void ccgSubSurf_evaluatorSetFVarUV(CCGSubSurf *ss,
237                                    struct DerivedMesh *dm,
238                                    int layer_index);
239
240 /* TODO(sergey): Temporary call to test things. */
241 void ccgSubSurf_evaluatorFVarUV(CCGSubSurf *ss,
242                                 int face_index, int S,
243                                 float grid_u, float grid_v,
244                                 float uv[2]);
245
246 void ccgSubSurf_free_osd_mesh(CCGSubSurf *ss);
247
248 void ccgSubSurf_getMinMax(CCGSubSurf *ss, float r_min[3], float r_max[3]);
249
250 void ccgSubSurf__sync_subdivUvs(CCGSubSurf *ss, bool subsurf_uvs);
251
252 #endif
253
254 #endif  /* __CCGSUBSURF_H__ */