72f386bf246051e5091b41bd960967c0c6e78533
[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 /***/
77
78 CCGSubSurf*     ccgSubSurf_new  (CCGMeshIFC *ifc, int subdivisionLevels, CCGAllocatorIFC *allocatorIFC, CCGAllocatorHDL allocator);
79 void            ccgSubSurf_free (CCGSubSurf *ss);
80
81 CCGError        ccgSubSurf_initFullSync         (CCGSubSurf *ss);
82 CCGError        ccgSubSurf_initPartialSync      (CCGSubSurf *ss);
83 #ifdef WITH_OPENSUBDIV
84 CCGError        ccgSubSurf_initOpenSubdivSync   (CCGSubSurf *ss);
85 #endif
86
87 CCGError        ccgSubSurf_syncVert             (CCGSubSurf *ss, CCGVertHDL vHDL, const void *vertData, int seam, CCGVert **v_r);
88 CCGError        ccgSubSurf_syncEdge             (CCGSubSurf *ss, CCGEdgeHDL eHDL, CCGVertHDL e_vHDL0, CCGVertHDL e_vHDL1, float crease, CCGEdge **e_r);
89 CCGError        ccgSubSurf_syncFace             (CCGSubSurf *ss, CCGFaceHDL fHDL, int numVerts, CCGVertHDL *vHDLs, CCGFace **f_r);
90
91 CCGError        ccgSubSurf_syncVertDel  (CCGSubSurf *ss, CCGVertHDL vHDL);
92 CCGError        ccgSubSurf_syncEdgeDel  (CCGSubSurf *ss, CCGEdgeHDL eHDL);
93 CCGError        ccgSubSurf_syncFaceDel  (CCGSubSurf *ss, CCGFaceHDL fHDL);
94
95 CCGError        ccgSubSurf_processSync  (CCGSubSurf *ss);
96
97 CCGError        ccgSubSurf_updateFromFaces(CCGSubSurf *ss, int lvl, CCGFace **faces, int numFaces);
98 CCGError        ccgSubSurf_updateToFaces(CCGSubSurf *ss, int lvl, CCGFace **faces, int numFaces);
99 CCGError        ccgSubSurf_updateNormals(CCGSubSurf *ss, CCGFace **faces, int numFaces);
100 CCGError        ccgSubSurf_updateLevels(CCGSubSurf *ss, int lvl, CCGFace **faces, int numFaces);
101 CCGError        ccgSubSurf_stitchFaces(CCGSubSurf *ss, int lvl, CCGFace **faces, int numFaces);
102
103 CCGError        ccgSubSurf_setSubdivisionLevels         (CCGSubSurf *ss, int subdivisionLevels);
104
105 CCGError        ccgSubSurf_setAllowEdgeCreation         (CCGSubSurf *ss, int allowEdgeCreation, float defaultCreaseValue, void *defaultUserData);
106 void            ccgSubSurf_getAllowEdgeCreation         (CCGSubSurf *ss, int *allowEdgeCreation_r, float *defaultCreaseValue_r, void *defaultUserData_r);
107
108 void            ccgSubSurf_getUseAgeCounts                      (CCGSubSurf *ss, int *useAgeCounts_r, int *vertUserOffset_r, int *edgeUserOffset_r, int *faceUserOffset_r);
109 CCGError        ccgSubSurf_setUseAgeCounts                      (CCGSubSurf *ss, int useAgeCounts, int vertUserOffset, int edgeUserOffset, int faceUserOffset);
110
111 CCGError        ccgSubSurf_setCalcVertexNormals         (CCGSubSurf *ss, int useVertNormals, int normalDataOffset);
112 void            ccgSubSurf_setAllocMask                         (CCGSubSurf *ss, int allocMask, int maskOffset);
113
114 void            ccgSubSurf_setNumLayers                         (CCGSubSurf *ss, int numLayers);
115
116 /***/
117
118 int                     ccgSubSurf_getNumVerts                          (const CCGSubSurf *ss);
119 int                     ccgSubSurf_getNumEdges                          (const CCGSubSurf *ss);
120 int                     ccgSubSurf_getNumFaces                          (const CCGSubSurf *ss);
121
122 int                     ccgSubSurf_getSubdivisionLevels         (const CCGSubSurf *ss);
123 int                     ccgSubSurf_getEdgeSize                          (const CCGSubSurf *ss);
124 int                     ccgSubSurf_getEdgeLevelSize                     (const CCGSubSurf *ss, int level);
125 int                     ccgSubSurf_getGridSize                          (const CCGSubSurf *ss);
126 int                     ccgSubSurf_getGridLevelSize                     (const CCGSubSurf *ss, int level);
127 int                     ccgSubSurf_getSimpleSubdiv                      (const CCGSubSurf *ss);
128
129 CCGVert*        ccgSubSurf_getVert                                      (CCGSubSurf *ss, CCGVertHDL v);
130 CCGVertHDL      ccgSubSurf_getVertVertHandle            (CCGVert *v);
131 int                     ccgSubSurf_getVertNumFaces                      (CCGVert *v);
132 CCGFace*        ccgSubSurf_getVertFace                          (CCGVert *v, int index);
133 int                     ccgSubSurf_getVertNumEdges                      (CCGVert *v);
134 CCGEdge*        ccgSubSurf_getVertEdge                          (CCGVert *v, int index);
135
136 int                     ccgSubSurf_getVertAge                           (CCGSubSurf *ss, CCGVert *v);
137 void*           ccgSubSurf_getVertUserData                      (CCGSubSurf *ss, CCGVert *v);
138 void*           ccgSubSurf_getVertData                          (CCGSubSurf *ss, CCGVert *v);
139 void*           ccgSubSurf_getVertLevelData                     (CCGSubSurf *ss, CCGVert *v, int level);
140
141 CCGEdge*        ccgSubSurf_getEdge                                      (CCGSubSurf *ss, CCGEdgeHDL e);
142 CCGEdgeHDL      ccgSubSurf_getEdgeEdgeHandle            (CCGEdge *e);
143 int                     ccgSubSurf_getEdgeNumFaces                      (CCGEdge *e);
144 CCGFace*        ccgSubSurf_getEdgeFace                          (CCGEdge *e, int index);
145 CCGVert*        ccgSubSurf_getEdgeVert0                         (CCGEdge *e);
146 CCGVert*        ccgSubSurf_getEdgeVert1                         (CCGEdge *e);
147 float           ccgSubSurf_getEdgeCrease                        (CCGEdge *e);
148
149 int                     ccgSubSurf_getEdgeAge                           (CCGSubSurf *ss, CCGEdge *e);
150 void*           ccgSubSurf_getEdgeUserData                      (CCGSubSurf *ss, CCGEdge *e);
151 void*           ccgSubSurf_getEdgeDataArray                     (CCGSubSurf *ss, CCGEdge *e);
152 void*           ccgSubSurf_getEdgeData                          (CCGSubSurf *ss, CCGEdge *e, int x);
153 void*           ccgSubSurf_getEdgeLevelData                     (CCGSubSurf *ss, CCGEdge *e, int x, int level);
154
155 CCGFace*        ccgSubSurf_getFace                                      (CCGSubSurf *ss, CCGFaceHDL f);
156 CCGFaceHDL      ccgSubSurf_getFaceFaceHandle            (CCGFace *f);
157 int                     ccgSubSurf_getFaceNumVerts                      (CCGFace *f);
158 CCGVert*        ccgSubSurf_getFaceVert                          (CCGFace *f, int index);
159 CCGEdge*        ccgSubSurf_getFaceEdge                          (CCGFace *f, int index);
160 int                     ccgSubSurf_getFaceEdgeIndex                     (CCGFace *f, CCGEdge *e);
161
162 int                     ccgSubSurf_getFaceAge                           (CCGSubSurf *ss, CCGFace *f);
163 void*           ccgSubSurf_getFaceUserData                      (CCGSubSurf *ss, CCGFace *f);
164 void*           ccgSubSurf_getFaceCenterData            (CCGFace *f);
165 void*           ccgSubSurf_getFaceGridEdgeDataArray     (CCGSubSurf *ss, CCGFace *f, int gridIndex);
166 void*           ccgSubSurf_getFaceGridEdgeData          (CCGSubSurf *ss, CCGFace *f, int gridIndex, int x);
167 void*           ccgSubSurf_getFaceGridDataArray         (CCGSubSurf *ss, CCGFace *f, int gridIndex);
168 void*           ccgSubSurf_getFaceGridData                      (CCGSubSurf *ss, CCGFace *f, int gridIndex, int x, int y);
169
170 int                     ccgSubSurf_getNumFinalVerts             (const CCGSubSurf *ss);
171 int                     ccgSubSurf_getNumFinalEdges             (const CCGSubSurf *ss);
172 int                     ccgSubSurf_getNumFinalFaces             (const CCGSubSurf *ss);
173
174 /***/
175
176 typedef struct _EHashIterator CCGVertIterator;
177 typedef struct _EHashIterator CCGEdgeIterator;
178 typedef struct _EHashIterator CCGFaceIterator;
179
180 void            ccgSubSurf_initVertIterator(CCGSubSurf *ss, CCGVertIterator *viter);
181 void            ccgSubSurf_initEdgeIterator(CCGSubSurf *ss, CCGEdgeIterator *eiter);
182 void            ccgSubSurf_initFaceIterator(CCGSubSurf *ss, CCGFaceIterator *fiter);
183
184 CCGVert*                        ccgVertIterator_getCurrent      (CCGVertIterator *vi);
185 int                                     ccgVertIterator_isStopped       (CCGVertIterator *vi);
186 void                            ccgVertIterator_next            (CCGVertIterator *vi);
187
188 CCGEdge*                        ccgEdgeIterator_getCurrent      (CCGEdgeIterator *ei);
189 int                                     ccgEdgeIterator_isStopped       (CCGEdgeIterator *ei);
190 void                            ccgEdgeIterator_next            (CCGEdgeIterator *ei);
191
192 CCGFace*                        ccgFaceIterator_getCurrent      (CCGFaceIterator *fi);
193 int                                     ccgFaceIterator_isStopped       (CCGFaceIterator *fi);
194 void                            ccgFaceIterator_next            (CCGFaceIterator *fi);
195
196 #ifdef WITH_OPENSUBDIV
197 struct DerivedMesh;
198
199 /* Check if topology changed and evaluators are to be re-created. */
200 void ccgSubSurf_checkTopologyChanged(CCGSubSurf *ss, struct DerivedMesh *dm);
201
202 /* Create topology refiner from give derived mesh which then later will be
203  * used for GL mesh creation.
204  */
205 void ccgSubSurf_prepareTopologyRefiner(CCGSubSurf *ss, struct DerivedMesh *dm);
206
207 /* Make sure GL mesh exists, up to date and ready to draw. */
208 bool ccgSubSurf_prepareGLMesh(CCGSubSurf *ss, bool use_osd_glsl, int active_uv_index);
209
210 /* Draw given partitions of the GL mesh.
211  *
212  * TODO(sergey): fill_quads is actually an invariant and should be part
213  * of the prepare routine.
214  */
215 void ccgSubSurf_drawGLMesh(CCGSubSurf *ss, bool fill_quads,
216                            int start_partition, int num_partitions);
217
218 /* Get number of base faces in a particular GL mesh. */
219 int ccgSubSurf_getNumGLMeshBaseFaces(CCGSubSurf *ss);
220
221 /* Get number of vertices in base faces in a particular GL mesh. */
222 int ccgSubSurf_getNumGLMeshBaseFaceVerts(CCGSubSurf *ss, int face);
223
224 /* Controls whether CCG are needed (Cmeaning CPU evaluation) or fully GPU compute
225  * and draw is allowed.
226  */
227 void ccgSubSurf_setSkipGrids(CCGSubSurf *ss, bool skip_grids);
228 bool ccgSubSurf_needGrids(CCGSubSurf *ss);
229
230 /* Set evaluator's face varying data from UV coordinates.
231  * Used for CPU evaluation.
232  */
233 void ccgSubSurf_evaluatorSetFVarUV(CCGSubSurf *ss,
234                                    struct DerivedMesh *dm,
235                                    int layer_index);
236
237 /* TODO(sergey): Temporary call to test things. */
238 void ccgSubSurf_evaluatorFVarUV(CCGSubSurf *ss,
239                                 int face_index, int S,
240                                 float grid_u, float grid_v,
241                                 float uv[2]);
242
243 void ccgSubSurf_free_osd_mesh(CCGSubSurf *ss);
244
245 void ccgSubSurf_getMinMax(CCGSubSurf *ss, float r_min[3], float r_max[3]);
246
247 #endif
248
249 #endif  /* __CCGSUBSURF_H__ */