Blender  V2.93
lineart_util.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2019 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include <stdio.h>
25 #include <stdlib.h>
26 /* #include <time.h> */
27 
28 #include <math.h>
29 
30 #include "MEM_guardedalloc.h"
31 
32 #include "MOD_lineart.h"
33 
34 #include "BLI_math.h"
35 
36 #include "lineart_intern.h"
37 
38 /* Line art memory and list helper */
39 
41 {
42  LinkData *lip;
43  if (h == NULL) {
44  return 0;
45  }
46  lip = lineart_mem_aquire(smp, sizeof(LinkData));
47  lip->data = data;
48  BLI_addtail(h, lip);
49  return lip;
50 }
53  void *data,
54  int size)
55 {
56  LinkData *lip;
57  if (h == NULL) {
58  return 0;
59  }
60  lip = lineart_mem_aquire(smp, size);
61  lip->data = data;
62  BLI_addtail(h, lip);
63  return lip;
64 }
65 
67 {
68  LinkData *lip;
69  void *rev = 0;
70  if (h == NULL) {
71  return 0;
72  }
73  lip = BLI_pophead(h);
74  rev = lip ? lip->data : 0;
75  return rev;
76 }
78 {
79  BLI_remlink(h, (void *)lip);
80 }
81 
83 {
84  size_t set_size = size;
85  if (set_size < LRT_MEMORY_POOL_64MB) {
86  set_size = LRT_MEMORY_POOL_64MB; /* Prevent too many small allocations. */
87  }
88  size_t total_size = size + sizeof(LineartStaticMemPoolNode);
89  LineartStaticMemPoolNode *smpn = MEM_callocN(total_size, "mempool");
90  smpn->size = total_size;
91  smpn->used_byte = sizeof(LineartStaticMemPoolNode);
92  BLI_addhead(&smp->pools, smpn);
93  return smpn;
94 }
96 {
98  void *ret;
99 
100  if (!smpn || (smpn->used_byte + size) > smpn->size) {
101  smpn = lineart_mem_new_static_pool(smp, size);
102  }
103 
104  ret = ((unsigned char *)smpn) + smpn->used_byte;
105 
106  smpn->used_byte += size;
107 
108  return ret;
109 }
111 {
112  void *ret;
113 
114  BLI_spin_lock(&smp->lock_mem);
115 
116  LineartStaticMemPoolNode *smpn = smp->pools.first;
117 
118  if (!smpn || (smpn->used_byte + size) > smpn->size) {
119  smpn = lineart_mem_new_static_pool(smp, size);
120  }
121 
122  ret = ((unsigned char *)smpn) + smpn->used_byte;
123 
124  smpn->used_byte += size;
125 
126  BLI_spin_unlock(&smp->lock_mem);
127 
128  return ret;
129 }
131 {
133  while ((smpn = BLI_pophead(&smp->pools)) != NULL) {
134  MEM_freeN(smpn);
135  }
136 }
137 
139 {
140  LineartEdge *e_n = (LineartEdge *)node;
141  e_n->next = (*first);
142  (*first) = e_n;
143 }
144 
145 void lineart_prepend_pool(LinkNode **first, LineartStaticMemPool *smp, void *link)
146 {
147  LinkNode *ln = lineart_mem_aquire_thread(smp, sizeof(LinkNode));
148  ln->next = (*first);
149  ln->link = link;
150  (*first) = ln;
151 }
152 
153 /* =======================================================================[str] */
154 
156  double (*mProjection)[4], double fFov_rad, double fAspect, double zMin, double zMax)
157 {
158  double yMax;
159  double yMin;
160  double xMin;
161  double xMax;
162 
163  if (fAspect < 1) {
164  yMax = zMin * tan(fFov_rad * 0.5f);
165  yMin = -yMax;
166  xMin = yMin * fAspect;
167  xMax = -xMin;
168  }
169  else {
170  xMax = zMin * tan(fFov_rad * 0.5f);
171  xMin = -xMax;
172  yMin = xMin / fAspect;
173  yMax = -yMin;
174  }
175 
176  unit_m4_db(mProjection);
177 
178  mProjection[0][0] = (2.0f * zMin) / (xMax - xMin);
179  mProjection[1][1] = (2.0f * zMin) / (yMax - yMin);
180  mProjection[2][0] = (xMax + xMin) / (xMax - xMin);
181  mProjection[2][1] = (yMax + yMin) / (yMax - yMin);
182  mProjection[2][2] = -((zMax + zMin) / (zMax - zMin));
183  mProjection[2][3] = -1.0f;
184  mProjection[3][2] = -((2.0f * (zMax * zMin)) / (zMax - zMin));
185  mProjection[3][3] = 0.0f;
186 }
187 void lineart_matrix_ortho_44d(double (*mProjection)[4],
188  double xMin,
189  double xMax,
190  double yMin,
191  double yMax,
192  double zMin,
193  double zMax)
194 {
195  unit_m4_db(mProjection);
196 
197  mProjection[0][0] = 2.0f / (xMax - xMin);
198  mProjection[1][1] = 2.0f / (yMax - yMin);
199  mProjection[2][2] = -2.0f / (zMax - zMin);
200  mProjection[3][0] = -((xMax + xMin) / (xMax - xMin));
201  mProjection[3][1] = -((yMax + yMin) / (yMax - yMin));
202  mProjection[3][2] = -((zMax + zMin) / (zMax - zMin));
203  mProjection[3][3] = 1.0f;
204 }
205 
207 {
208  size_t total = 0;
209  size_t sum_this = 0;
210  size_t count_this = 0;
211 
213  count_this++;
214  sum_this += LRT_MEMORY_POOL_64MB;
215  }
216  printf("LANPR Memory allocated %zu Standalone nodes, total %zu Bytes.\n", count_this, sum_this);
217  total += sum_this;
218  sum_this = 0;
219  count_this = 0;
220 
222  count_this++;
223  sum_this += reln->element_count * sizeof(LineartEdge);
224  }
225  printf(" allocated %zu edge blocks, total %zu Bytes.\n", count_this, sum_this);
226  total += sum_this;
227  sum_this = 0;
228  count_this = 0;
229 
231  count_this++;
232  sum_this += reln->element_count * rb->triangle_size;
233  }
234  printf(" allocated %zu triangle blocks, total %zu Bytes.\n", count_this, sum_this);
235  total += sum_this;
236  sum_this = 0;
237  count_this = 0;
238 }
void * BLI_pophead(ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:257
void BLI_addhead(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:87
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:133
void unit_m4_db(double m[4][4])
Definition: math_matrix.c:75
void BLI_spin_unlock(SpinLock *spin)
Definition: threads.cc:480
void BLI_spin_lock(SpinLock *spin)
Definition: threads.cc:461
Read Guarded memory(de)allocation.
struct LineartEdge LineartEdge
struct LineartStaticMemPoolNode LineartStaticMemPoolNode
#define LRT_MEMORY_POOL_64MB
Definition: MOD_lineart.h:314
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
OperationNode * node
void lineart_count_and_print_render_buffer_memory(LineartRenderBuffer *rb)
Definition: lineart_util.c:206
void * lineart_list_pop_pointer_no_free(ListBase *h)
Definition: lineart_util.c:66
void * lineart_mem_aquire_thread(LineartStaticMemPool *smp, size_t size)
Definition: lineart_util.c:110
void * lineart_list_append_pointer_pool(ListBase *h, LineartStaticMemPool *smp, void *data)
Definition: lineart_util.c:40
void * lineart_mem_aquire(LineartStaticMemPool *smp, size_t size)
Definition: lineart_util.c:95
void lineart_prepend_pool(LinkNode **first, LineartStaticMemPool *smp, void *link)
Definition: lineart_util.c:145
void lineart_matrix_perspective_44d(double(*mProjection)[4], double fFov_rad, double fAspect, double zMin, double zMax)
Definition: lineart_util.c:155
LineartStaticMemPoolNode * lineart_mem_new_static_pool(LineartStaticMemPool *smp, size_t size)
Definition: lineart_util.c:82
void lineart_prepend_edge_direct(LineartEdge **first, void *node)
Definition: lineart_util.c:138
void * lineart_list_append_pointer_pool_sized(ListBase *h, LineartStaticMemPool *smp, void *data, int size)
Definition: lineart_util.c:51
void lineart_list_remove_pointer_item_no_free(ListBase *h, LinkData *lip)
Definition: lineart_util.c:77
void lineart_mem_destroy(LineartStaticMemPool *smp)
Definition: lineart_util.c:130
void lineart_matrix_ortho_44d(double(*mProjection)[4], double xMin, double xMax, double yMin, double yMax, double zMin, double zMax)
Definition: lineart_util.c:187
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
INLINE Rall1d< T, V, S > tan(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:327
return ret
struct LineartEdge * next
Definition: MOD_lineart.h:142
ListBase line_buffer_pointers
Definition: MOD_lineart.h:223
ListBase triangle_buffer_pointers
Definition: MOD_lineart.h:224
LineartStaticMemPool render_data_pool
Definition: MOD_lineart.h:231
void * data
Definition: DNA_listBase.h:42
void * link
Definition: BLI_linklist.h:40
struct LinkNode * next
Definition: BLI_linklist.h:39
void * first
Definition: DNA_listBase.h:47