Blender V5.3
lineart_util.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdio>
10#include <cstdlib>
11/* #include <time.h> */
12
13#include <cmath>
14
15#include "MEM_guardedalloc.h"
16
17#include "BLI_listbase.hh"
18#include "BLI_math_matrix_c.hh"
19
20#include "MOD_lineart.hh"
21
22#include "lineart_intern.hh"
23
24namespace blender {
25
26/* Line art memory and list helper */
27
29{
30 LinkData *lip;
31 if (h == nullptr) {
32 return nullptr;
33 }
34 lip = static_cast<LinkData *>(lineart_mem_acquire(smp, sizeof(LinkData)));
35 lip->data = data;
36 BLI_addtail(h, lip);
37 return lip;
38}
41 void *data,
42 int size)
43{
44 LinkData *lip;
45 if (h == nullptr) {
46 return nullptr;
47 }
48 lip = static_cast<LinkData *>(lineart_mem_acquire(smp, size));
49 lip->data = data;
50 BLI_addtail(h, lip);
51 return lip;
52}
54{
55 LinkData *lip;
56 if (h == nullptr) {
57 return nullptr;
58 }
59 lip = static_cast<LinkData *>(lineart_mem_acquire_thread(smp, sizeof(LinkData)));
60 lip->data = data;
61 BLI_addtail(h, lip);
62 return lip;
63}
66 void *data,
67 int size)
68{
69 LinkData *lip;
70 if (h == nullptr) {
71 return nullptr;
72 }
73 lip = static_cast<LinkData *>(lineart_mem_acquire_thread(smp, size));
74 lip->data = data;
75 BLI_addtail(h, lip);
76 return lip;
77}
78
80{
81 LinkData *lip;
82 void *rev = nullptr;
83 if (h == nullptr) {
84 return nullptr;
85 }
86 lip = static_cast<LinkData *>(BLI_pophead(h));
87 rev = lip ? lip->data : nullptr;
88 return rev;
89}
91{
92 BLI_remlink(h, static_cast<void *>(lip));
93}
94
96{
97 size_t set_size = size;
98 if (set_size < LRT_MEMORY_POOL_1MB) {
99 set_size = LRT_MEMORY_POOL_1MB; /* Prevent too many small allocations. */
100 }
101 size_t total_size = set_size + sizeof(LineartStaticMemPoolNode);
103 MEM_new_zeroed(total_size, "mempool"));
104 smpn->size = total_size;
105 smpn->used_byte = sizeof(LineartStaticMemPoolNode);
106 BLI_addhead(&smp->pools, smpn);
107 return smpn;
108}
110{
111 LineartStaticMemPoolNode *smpn = smp->pools.first();
112 void *ret;
113
114 if (!smpn || (smpn->used_byte + size) > smpn->size) {
115 smpn = lineart_mem_new_static_pool(smp, size);
116 }
117
118 ret = (reinterpret_cast<uchar *>(smpn)) + smpn->used_byte;
119
120 smpn->used_byte += size;
121
122 return ret;
123}
125{
126 void *ret;
127
128 BLI_spin_lock(&smp->lock_mem);
129
130 LineartStaticMemPoolNode *smpn = smp->pools.first();
131
132 if (!smpn || (smpn->used_byte + size) > smpn->size) {
133 smpn = lineart_mem_new_static_pool(smp, size);
134 }
135
136 ret = (reinterpret_cast<uchar *>(smpn)) + smpn->used_byte;
137
138 smpn->used_byte += size;
139
141
142 return ret;
143}
145{
146 while (LineartStaticMemPoolNode *smpn = static_cast<LineartStaticMemPoolNode *>(
147 BLI_pophead(&smp->pools)))
148 {
149 MEM_delete(smpn);
150 }
151}
152
154{
155 LinkNode *ln = static_cast<LinkNode *>(lineart_mem_acquire_thread(smp, sizeof(LinkNode)));
156 ln->next = (*first);
157 ln->link = link;
158 (*first) = ln;
159}
160
161/* =======================================================================[str] */
162
164 double (*mProjection)[4], double fFov_rad, double fAspect, double zMin, double zMax)
165{
166 double yMax;
167 double yMin;
168 double xMin;
169 double xMax;
170
171 if (fAspect < 1) {
172 yMax = zMin * tan(fFov_rad * 0.5f);
173 yMin = -yMax;
174 xMin = yMin * fAspect;
175 xMax = -xMin;
176 }
177 else {
178 xMax = zMin * tan(fFov_rad * 0.5f);
179 xMin = -xMax;
180 yMin = xMin / fAspect;
181 yMax = -yMin;
182 }
183
184 unit_m4_db(mProjection);
185
186 mProjection[0][0] = (2.0f * zMin) / (xMax - xMin);
187 mProjection[1][1] = (2.0f * zMin) / (yMax - yMin);
188 mProjection[2][0] = (xMax + xMin) / (xMax - xMin);
189 mProjection[2][1] = (yMax + yMin) / (yMax - yMin);
190 mProjection[2][2] = -((zMax + zMin) / (zMax - zMin));
191 mProjection[2][3] = -1.0f;
192 mProjection[3][2] = -((2.0f * (zMax * zMin)) / (zMax - zMin));
193 mProjection[3][3] = 0.0f;
194}
195void lineart_matrix_ortho_44d(double (*mProjection)[4],
196 double xMin,
197 double xMax,
198 double yMin,
199 double yMax,
200 double zMin,
201 double zMax)
202{
203 unit_m4_db(mProjection);
204
205 mProjection[0][0] = 2.0f / (xMax - xMin);
206 mProjection[1][1] = 2.0f / (yMax - yMin);
207 mProjection[2][2] = -2.0f / (zMax - zMin);
208 mProjection[3][0] = -((xMax + xMin) / (xMax - xMin));
209 mProjection[3][1] = -((yMax + yMin) / (yMax - yMin));
210 mProjection[3][2] = -((zMax + zMin) / (zMax - zMin));
211 mProjection[3][3] = 1.0f;
212}
213
215{
216 size_t total = 0;
217 size_t count_this = ld->render_data_pool.pools.count();
218 size_t sum_this = LRT_MEMORY_POOL_1MB * count_this;
219
220 printf("LANPR Memory allocated %zu Standalone nodes, total %zu Bytes.\n", count_this, sum_this);
221 total += sum_this;
222 sum_this = 0;
223 count_this = 0;
224
226 count_this++;
227 sum_this += reln.element_count * sizeof(LineartEdge);
228 }
229 printf(" allocated %zu edge blocks, total %zu Bytes.\n", count_this, sum_this);
230 total += sum_this;
231 sum_this = 0;
232 count_this = 0;
233
235 count_this++;
236 sum_this += reln.element_count * ld->sizeof_triangle;
237 }
238 printf(" allocated %zu triangle blocks, total %zu Bytes.\n", count_this, sum_this);
239 total += sum_this;
240 sum_this = 0;
241 count_this = 0;
242
243 (void)total; /* Ignored. */
244}
245
246} // namespace blender
Read Blender memory allocation and freeing.
#define LRT_MEMORY_POOL_1MB
#define tan
BMesh Iterators.
void lineart_list_remove_pointer_item_no_free(ListBase *h, LinkData *lip)
void BLI_addhead(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:98
void lineart_count_and_print_render_buffer_memory(struct LineartData *ld)
void * lineart_list_append_pointer_pool_sized(ListBase *h, struct LineartStaticMemPool *smp, void *data, int size)
void lineart_matrix_perspective_44d(double(*mProjection)[4], double fFov_rad, double fAspect, double zMin, double zMax)
void * lineart_list_append_pointer_pool_sized_thread(ListBase *h, LineartStaticMemPool *smp, void *data, int size)
void * BLI_pophead(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:259
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
unsigned char uchar
Definition BKE_text.h:58
void * lineart_list_append_pointer_pool_thread(ListBase *h, struct LineartStaticMemPool *smp, void *data)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void lineart_matrix_ortho_44d(double(*mProjection)[4], double xMin, double xMax, double yMin, double yMax, double zMin, double zMax)
BMesh const char void * data
void unit_m4_db(double m[4][4])
void BLI_spin_unlock(SpinLock *spin)
Definition threads.cc:433
void * lineart_mem_acquire(struct LineartStaticMemPool *smp, size_t size)
struct LineartStaticMemPoolNode * lineart_mem_new_static_pool(struct LineartStaticMemPool *smp, size_t size)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void lineart_mem_destroy(struct LineartStaticMemPool *smp)
void * lineart_mem_acquire_thread(struct LineartStaticMemPool *smp, size_t size)
void * lineart_list_pop_pointer_no_free(ListBase *h)
void * lineart_list_append_pointer_pool(ListBase *h, struct LineartStaticMemPool *smp, void *data)
void BLI_spin_lock(SpinLock *spin)
Definition threads.cc:408
void lineart_prepend_pool(LinkNode **first, struct LineartStaticMemPool *smp, void *link)
ListBaseT< LineartElementLinkNode > line_buffer_pointers
ListBaseT< LineartElementLinkNode > triangle_buffer_pointers
struct blender::LineartData::_geom geom
LineartStaticMemPool render_data_pool
ListBaseT< LineartStaticMemPoolNode > pools
struct LinkNode * next