pipeline.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file pipeline.cxx
  10. * @author drose
  11. * @date 2002-02-21
  12. */
  13. #include "pipeline.h"
  14. #include "pipelineCyclerTrueImpl.h"
  15. #include "configVariableInt.h"
  16. #include "config_pipeline.h"
  17. Pipeline *Pipeline::_render_pipeline = nullptr;
  18. /**
  19. *
  20. */
  21. Pipeline::
  22. Pipeline(const std::string &name, int num_stages) :
  23. Namable(name),
  24. #ifdef THREADED_PIPELINE
  25. _num_stages(num_stages),
  26. _cycle_lock("Pipeline cycle"),
  27. _lock("Pipeline"),
  28. _next_cycle_seq(1)
  29. #else
  30. _num_stages(1)
  31. #endif
  32. {
  33. #ifdef THREADED_PIPELINE
  34. // We maintain all of the cyclers in the world on one of two linked
  35. // lists. Cyclers that are "clean", which is to say, they have the
  36. // same value across all pipeline stages, are stored on the _clean
  37. // list. Cyclers that are "dirty", which have different values
  38. // across some pipeline stages, are stored instead on the _dirty
  39. // list. Cyclers can move themselves from clean to dirty by calling
  40. // add_dirty_cycler(), and cyclers get moved from dirty to clean
  41. // during cycle().
  42. // To visit each cycler once requires traversing both lists.
  43. _clean.make_head();
  44. _dirty.make_head();
  45. // We also store the total count of all cyclers, clean and dirty, in
  46. // _num_cyclers; and the count of only dirty cyclers in _num_dirty_cyclers.
  47. _num_cyclers = 0;
  48. _num_dirty_cyclers = 0;
  49. // This flag is true only during the call to cycle().
  50. _cycling = false;
  51. #else
  52. if (num_stages != 1) {
  53. pipeline_cat.warning()
  54. << "Requested " << num_stages
  55. << " pipeline stages but multithreaded render pipelines not enabled in build.\n";
  56. }
  57. #endif // THREADED_PIPELINE
  58. nassertv(num_stages >= 1);
  59. }
  60. /**
  61. *
  62. */
  63. Pipeline::
  64. ~Pipeline() {
  65. #ifdef THREADED_PIPELINE
  66. nassertv(_num_cyclers == 0);
  67. nassertv(_num_dirty_cyclers == 0);
  68. _clean.clear_head();
  69. _dirty.clear_head();
  70. nassertv(!_cycling);
  71. #endif // THREADED_PIPELINE
  72. }
  73. /**
  74. * Flows all the pipeline data down to the next stage.
  75. */
  76. void Pipeline::
  77. cycle() {
  78. #ifdef THREADED_PIPELINE
  79. if (pipeline_cat.is_spam()) {
  80. pipeline_cat.spam()
  81. << "Beginning the pipeline cycle\n";
  82. }
  83. pvector< PT(CycleData) > saved_cdatas;
  84. {
  85. ReMutexHolder cycle_holder(_cycle_lock);
  86. unsigned int prev_seq, next_seq;
  87. PipelineCyclerLinks prev_dirty;
  88. {
  89. // We can't hold the lock protecting the linked lists during the cycling
  90. // itself, since it could cause a deadlock.
  91. MutexHolder holder(_lock);
  92. if (_num_stages == 1) {
  93. // No need to cycle if there's only one stage.
  94. nassertv(_dirty._next == &_dirty);
  95. return;
  96. }
  97. nassertv(!_cycling);
  98. _cycling = true;
  99. // Increment the cycle sequence number, which is used by this method to
  100. // communicate with remove_cycler() about the status of dirty cyclers.
  101. prev_seq = next_seq = _next_cycle_seq;
  102. if (++next_seq == 0) {
  103. // Skip 0, which is a reserved number used to indicate a clean cycler.
  104. ++next_seq;
  105. }
  106. _next_cycle_seq = next_seq;
  107. // Move the dirty list to prev_dirty, for processing.
  108. prev_dirty.make_head();
  109. prev_dirty.take_list(_dirty);
  110. saved_cdatas.reserve(_num_dirty_cyclers);
  111. _num_dirty_cyclers = 0;
  112. }
  113. // This is duplicated for different number of stages, as an optimization.
  114. switch (_num_stages) {
  115. case 2:
  116. while (prev_dirty._next != &prev_dirty) {
  117. PipelineCyclerLinks *link = prev_dirty._next;
  118. while (link != &prev_dirty) {
  119. PipelineCyclerTrueImpl *cycler = (PipelineCyclerTrueImpl *)link;
  120. if (!cycler->_lock.try_lock()) {
  121. // No big deal, just move on to the next one for now, and we'll
  122. // come back around to it. It's important not to block here in
  123. // order to prevent one cycler from deadlocking another.
  124. if (link->_prev != &prev_dirty || link->_next != &prev_dirty) {
  125. link = cycler->_next;
  126. continue;
  127. } else {
  128. // Well, we are the last cycler left, so we might as well wait.
  129. // This is necessary to trigger the deadlock detection code.
  130. cycler->_lock.lock();
  131. }
  132. }
  133. MutexHolder holder(_lock);
  134. cycler->remove_from_list();
  135. // We save the result of cycle(), so that we can defer the side-
  136. // effects that might occur when CycleDatas destruct, at least until
  137. // the end of this loop.
  138. saved_cdatas.push_back(cycler->cycle_2());
  139. // cycle_2() won't leave a cycler dirty. Add it to the clean list.
  140. nassertd(!cycler->_dirty) break;
  141. cycler->insert_before(&_clean);
  142. #ifdef DEBUG_THREADS
  143. inc_cycler_type(_dirty_cycler_types, cycler->get_parent_type(), -1);
  144. #endif
  145. cycler->_lock.unlock();
  146. break;
  147. }
  148. }
  149. break;
  150. case 3:
  151. while (prev_dirty._next != &prev_dirty) {
  152. PipelineCyclerLinks *link = prev_dirty._next;
  153. while (link != &prev_dirty) {
  154. PipelineCyclerTrueImpl *cycler = (PipelineCyclerTrueImpl *)link;
  155. if (!cycler->_lock.try_lock()) {
  156. // No big deal, just move on to the next one for now, and we'll
  157. // come back around to it. It's important not to block here in
  158. // order to prevent one cycler from deadlocking another.
  159. if (link->_prev != &prev_dirty || link->_next != &prev_dirty) {
  160. link = cycler->_next;
  161. continue;
  162. } else {
  163. // Well, we are the last cycler left, so we might as well wait.
  164. // This is necessary to trigger the deadlock detection code.
  165. cycler->_lock.lock();
  166. }
  167. }
  168. MutexHolder holder(_lock);
  169. cycler->remove_from_list();
  170. saved_cdatas.push_back(cycler->cycle_3());
  171. if (cycler->_dirty) {
  172. // The cycler is still dirty. Add it back to the dirty list.
  173. nassertd(cycler->_dirty == prev_seq) break;
  174. cycler->insert_before(&_dirty);
  175. cycler->_dirty = next_seq;
  176. ++_num_dirty_cyclers;
  177. } else {
  178. // The cycler is now clean. Add it back to the clean list.
  179. cycler->insert_before(&_clean);
  180. #ifdef DEBUG_THREADS
  181. inc_cycler_type(_dirty_cycler_types, cycler->get_parent_type(), -1);
  182. #endif
  183. }
  184. cycler->_lock.unlock();
  185. break;
  186. }
  187. }
  188. break;
  189. default:
  190. while (prev_dirty._next != &prev_dirty) {
  191. PipelineCyclerLinks *link = prev_dirty._next;
  192. while (link != &prev_dirty) {
  193. PipelineCyclerTrueImpl *cycler = (PipelineCyclerTrueImpl *)link;
  194. if (!cycler->_lock.try_lock()) {
  195. // No big deal, just move on to the next one for now, and we'll
  196. // come back around to it. It's important not to block here in
  197. // order to prevent one cycler from deadlocking another.
  198. if (link->_prev != &prev_dirty || link->_next != &prev_dirty) {
  199. link = cycler->_next;
  200. continue;
  201. } else {
  202. // Well, we are the last cycler left, so we might as well wait.
  203. // This is necessary to trigger the deadlock detection code.
  204. cycler->_lock.lock();
  205. }
  206. }
  207. MutexHolder holder(_lock);
  208. cycler->remove_from_list();
  209. saved_cdatas.push_back(cycler->cycle());
  210. if (cycler->_dirty) {
  211. // The cycler is still dirty. Add it back to the dirty list.
  212. nassertd(cycler->_dirty == prev_seq) break;
  213. cycler->insert_before(&_dirty);
  214. cycler->_dirty = next_seq;
  215. ++_num_dirty_cyclers;
  216. } else {
  217. // The cycler is now clean. Add it back to the clean list.
  218. cycler->insert_before(&_clean);
  219. #ifdef DEBUG_THREADS
  220. inc_cycler_type(_dirty_cycler_types, cycler->get_parent_type(), -1);
  221. #endif
  222. }
  223. cycler->_lock.unlock();
  224. break;
  225. }
  226. }
  227. break;
  228. }
  229. // Now we're ready for the next frame.
  230. prev_dirty.clear_head();
  231. _cycling = false;
  232. }
  233. // And now it's safe to let the CycleData pointers in saved_cdatas destruct,
  234. // which may cause cascading deletes, and which will in turn cause
  235. // PipelineCyclers to remove themselves from (or add themselves to) the
  236. // _dirty list.
  237. saved_cdatas.clear();
  238. if (pipeline_cat.is_debug()) {
  239. pipeline_cat.debug()
  240. << "Finished the pipeline cycle\n";
  241. }
  242. #endif // THREADED_PIPELINE
  243. }
  244. /**
  245. * Specifies the number of stages required for the pipeline.
  246. */
  247. void Pipeline::
  248. set_num_stages(int num_stages) {
  249. nassertv(num_stages >= 1);
  250. #ifdef THREADED_PIPELINE
  251. // Make sure it's not currently cycling.
  252. ReMutexHolder cycle_holder(_cycle_lock);
  253. MutexHolder holder(_lock);
  254. if (num_stages != _num_stages) {
  255. // We need to lock every PipelineCycler object attached to this pipeline
  256. // before we can adjust the number of stages.
  257. PipelineCyclerLinks *links;
  258. for (links = _clean._next; links != &_clean; links = links->_next) {
  259. PipelineCyclerTrueImpl *cycler = (PipelineCyclerTrueImpl *)links;
  260. cycler->_lock.lock();
  261. }
  262. for (links = _dirty._next; links != &_dirty; links = links->_next) {
  263. PipelineCyclerTrueImpl *cycler = (PipelineCyclerTrueImpl *)links;
  264. cycler->_lock.lock();
  265. }
  266. _num_stages = num_stages;
  267. for (links = _clean._next; links != &_clean; links = links->_next) {
  268. PipelineCyclerTrueImpl *cycler = (PipelineCyclerTrueImpl *)links;
  269. cycler->set_num_stages(num_stages);
  270. }
  271. for (links = _dirty._next; links != &_dirty; links = links->_next) {
  272. PipelineCyclerTrueImpl *cycler = (PipelineCyclerTrueImpl *)links;
  273. cycler->set_num_stages(num_stages);
  274. }
  275. // Now release them all.
  276. int count = 0;
  277. for (links = _clean._next; links != &_clean; links = links->_next) {
  278. PipelineCyclerTrueImpl *cycler = (PipelineCyclerTrueImpl *)links;
  279. cycler->_lock.unlock();
  280. ++count;
  281. }
  282. for (links = _dirty._next; links != &_dirty; links = links->_next) {
  283. PipelineCyclerTrueImpl *cycler = (PipelineCyclerTrueImpl *)links;
  284. cycler->_lock.unlock();
  285. ++count;
  286. }
  287. nassertv(count == _num_cyclers);
  288. }
  289. #else // THREADED_PIPELINE
  290. if (num_stages != 1) {
  291. pipeline_cat.warning()
  292. << "Requested " << num_stages
  293. << " pipeline stages but multithreaded render pipelines not enabled in build.\n";
  294. }
  295. _num_stages = 1;
  296. #endif // THREADED_PIPELINE
  297. }
  298. #ifdef THREADED_PIPELINE
  299. /**
  300. * Adds the indicated cycler to the list of cyclers associated with the
  301. * pipeline. This method only exists when true pipelining is configured on.
  302. */
  303. void Pipeline::
  304. add_cycler(PipelineCyclerTrueImpl *cycler) {
  305. // It's safe to add it to the list while cycling, since the _clean list is
  306. // not touched during the cycle loop.
  307. MutexHolder holder(_lock);
  308. nassertv(!cycler->_dirty);
  309. cycler->insert_before(&_clean);
  310. ++_num_cyclers;
  311. #ifdef DEBUG_THREADS
  312. inc_cycler_type(_all_cycler_types, cycler->get_parent_type(), 1);
  313. #endif
  314. }
  315. #endif // THREADED_PIPELINE
  316. #ifdef THREADED_PIPELINE
  317. /**
  318. * Marks the indicated cycler as "dirty", meaning it will need to be cycled
  319. * next frame. This both adds it to the "dirty" set and also sets the "dirty"
  320. * flag within the cycler. This method only exists when true pipelining is
  321. * configured on.
  322. */
  323. void Pipeline::
  324. add_dirty_cycler(PipelineCyclerTrueImpl *cycler) {
  325. nassertv(cycler->_lock.debug_is_locked());
  326. // It's safe to add it to the list while cycling, since it's not currently
  327. // on the dirty list.
  328. MutexHolder holder(_lock);
  329. nassertv(!cycler->_dirty);
  330. nassertv(_num_stages != 1);
  331. // Remove it from the "clean" list and add it to the "dirty" list.
  332. cycler->remove_from_list();
  333. cycler->insert_before(&_dirty);
  334. cycler->_dirty = _next_cycle_seq;
  335. ++_num_dirty_cyclers;
  336. #ifdef DEBUG_THREADS
  337. inc_cycler_type(_dirty_cycler_types, cycler->get_parent_type(), 1);
  338. #endif
  339. }
  340. #endif // THREADED_PIPELINE
  341. #ifdef THREADED_PIPELINE
  342. /**
  343. * Removes the indicated cycler from the list of cyclers associated with the
  344. * pipeline. This method only exists when true pipelining is configured on.
  345. */
  346. void Pipeline::
  347. remove_cycler(PipelineCyclerTrueImpl *cycler) {
  348. nassertv(cycler->_lock.debug_is_locked());
  349. MutexHolder holder(_lock);
  350. // If it's dirty, it may currently be processed by cycle(), so we need to be
  351. // careful not to cause a race condition. It's safe for us to remove it
  352. // during cycle only if it's 0 (clean) or _next_cycle_seq (scheduled for the
  353. // next cycle, so not owned by the current one).
  354. while (cycler->_dirty != 0 && cycler->_dirty != _next_cycle_seq) {
  355. if (_cycle_lock.try_lock()) {
  356. // OK, great, we got the lock, so it finished cycling already.
  357. nassertv(!_cycling);
  358. --_num_cyclers;
  359. cycler->remove_from_list();
  360. cycler->_dirty = false;
  361. --_num_dirty_cyclers;
  362. #ifdef DEBUG_THREADS
  363. inc_cycler_type(_all_cycler_types, cycler->get_parent_type(), -1);
  364. inc_cycler_type(_dirty_cycler_types, cycler->get_parent_type(), -1);
  365. #endif
  366. _cycle_lock.unlock();
  367. return;
  368. } else {
  369. // It's possibly currently being cycled. We will wait for the cycler
  370. // to be done with it, so that we can safely remove it.
  371. _lock.unlock();
  372. cycler->_lock.unlock();
  373. Thread::force_yield();
  374. cycler->_lock.lock();
  375. _lock.lock();
  376. }
  377. }
  378. // It's not being owned by a cycle operation, so it's fair game.
  379. --_num_cyclers;
  380. cycler->remove_from_list();
  381. #ifdef DEBUG_THREADS
  382. inc_cycler_type(_all_cycler_types, cycler->get_parent_type(), -1);
  383. #endif
  384. if (cycler->_dirty) {
  385. cycler->_dirty = 0;
  386. --_num_dirty_cyclers;
  387. #ifdef DEBUG_THREADS
  388. inc_cycler_type(_dirty_cycler_types, cycler->get_parent_type(), -1);
  389. #endif
  390. }
  391. }
  392. #endif // THREADED_PIPELINE
  393. #if defined(THREADED_PIPELINE) && defined(DEBUG_THREADS)
  394. /**
  395. * Walks through the list of all the different PipelineCycler types in the
  396. * universe. For each one, calls the indicated callback function with the
  397. * TypeHandle of the respective type (actually, the result of
  398. * cycler::get_parent_type()) and the count of pipeline cyclers of that type.
  399. * Mainly used for PStats reporting.
  400. */
  401. void Pipeline::
  402. iterate_all_cycler_types(CallbackFunc *func, void *data) const {
  403. // Make sure it's not currently cycling.
  404. ReMutexHolder cycle_holder(_cycle_lock);
  405. MutexHolder holder(_lock);
  406. TypeCount::const_iterator ci;
  407. for (ci = _all_cycler_types.begin(); ci != _all_cycler_types.end(); ++ci) {
  408. func((*ci).first, (*ci).second, data);
  409. }
  410. }
  411. #endif // THREADED_PIPELINE && DEBUG_THREADS
  412. #if defined(THREADED_PIPELINE) && defined(DEBUG_THREADS)
  413. /**
  414. * Walks through the list of all the different PipelineCycler types, for only
  415. * the dirty PipelineCyclers. See also iterate_all_cycler_types().
  416. */
  417. void Pipeline::
  418. iterate_dirty_cycler_types(CallbackFunc *func, void *data) const {
  419. // Make sure it's not currently cycling.
  420. ReMutexHolder cycle_holder(_cycle_lock);
  421. MutexHolder holder(_lock);
  422. TypeCount::const_iterator ci;
  423. for (ci = _dirty_cycler_types.begin(); ci != _dirty_cycler_types.end(); ++ci) {
  424. func((*ci).first, (*ci).second, data);
  425. }
  426. }
  427. #endif // THREADED_PIPELINE && DEBUG_THREADS
  428. /**
  429. *
  430. */
  431. void Pipeline::
  432. make_render_pipeline() {
  433. ConfigVariableInt pipeline_stages
  434. ("pipeline-stages", 1,
  435. PRC_DESC("The initial number of stages in the render pipeline. This is "
  436. "only meaningful if threaded pipelining is compiled into "
  437. "Panda. In most cases, you should not set this at all anyway, "
  438. "since the pipeline can automatically grow stages as needed, "
  439. "but it will not remove stages automatically, and having more "
  440. "pipeline stages than your application requires will incur "
  441. "additional runtime overhead."));
  442. nassertv(_render_pipeline == nullptr);
  443. _render_pipeline = new Pipeline("render", pipeline_stages);
  444. }
  445. #if defined(THREADED_PIPELINE) && defined(DEBUG_THREADS)
  446. /**
  447. * Increments (or decrements, according to added) the value for TypeHandle in
  448. * the indicated TypeCount map. This is used in DEBUG_THREADS mode to track
  449. * the types of PipelineCyclers that are coming and going, mainly for PStats
  450. * reporting.
  451. *
  452. * It is assumed the lock is held during this call.
  453. */
  454. void Pipeline::
  455. inc_cycler_type(TypeCount &count, TypeHandle type, int addend) {
  456. TypeCount::iterator ci = count.find(type);
  457. if (ci == count.end()) {
  458. ci = count.insert(TypeCount::value_type(type, 0)).first;
  459. }
  460. (*ci).second += addend;
  461. nassertv((*ci).second >= 0);
  462. }
  463. #endif // THREADED_PIPELINE && DEBUG_THREADS