mesh.cpp 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376
  1. /*
  2. * Copyright 2011-2013 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "bvh/bvh.h"
  17. #include "bvh/bvh_build.h"
  18. #include "render/camera.h"
  19. #include "render/curves.h"
  20. #include "device/device.h"
  21. #include "render/graph.h"
  22. #include "render/shader.h"
  23. #include "render/light.h"
  24. #include "render/mesh.h"
  25. #include "render/nodes.h"
  26. #include "render/object.h"
  27. #include "render/scene.h"
  28. #include "render/stats.h"
  29. #include "kernel/osl/osl_globals.h"
  30. #include "subd/subd_split.h"
  31. #include "subd/subd_patch_table.h"
  32. #include "util/util_foreach.h"
  33. #include "util/util_logging.h"
  34. #include "util/util_progress.h"
  35. #include "util/util_set.h"
  36. #ifdef WITH_EMBREE
  37. # include "bvh/bvh_embree.h"
  38. #endif
  39. CCL_NAMESPACE_BEGIN
  40. /* Triangle */
  41. void Mesh::Triangle::bounds_grow(const float3 *verts, BoundBox &bounds) const
  42. {
  43. bounds.grow(verts[v[0]]);
  44. bounds.grow(verts[v[1]]);
  45. bounds.grow(verts[v[2]]);
  46. }
  47. void Mesh::Triangle::motion_verts(const float3 *verts,
  48. const float3 *vert_steps,
  49. size_t num_verts,
  50. size_t num_steps,
  51. float time,
  52. float3 r_verts[3]) const
  53. {
  54. /* Figure out which steps we need to fetch and their interpolation factor. */
  55. const size_t max_step = num_steps - 1;
  56. const size_t step = min((int)(time * max_step), max_step - 1);
  57. const float t = time * max_step - step;
  58. /* Fetch vertex coordinates. */
  59. float3 curr_verts[3];
  60. float3 next_verts[3];
  61. verts_for_step(verts, vert_steps, num_verts, num_steps, step, curr_verts);
  62. verts_for_step(verts, vert_steps, num_verts, num_steps, step + 1, next_verts);
  63. /* Interpolate between steps. */
  64. r_verts[0] = (1.0f - t) * curr_verts[0] + t * next_verts[0];
  65. r_verts[1] = (1.0f - t) * curr_verts[1] + t * next_verts[1];
  66. r_verts[2] = (1.0f - t) * curr_verts[2] + t * next_verts[2];
  67. }
  68. void Mesh::Triangle::verts_for_step(const float3 *verts,
  69. const float3 *vert_steps,
  70. size_t num_verts,
  71. size_t num_steps,
  72. size_t step,
  73. float3 r_verts[3]) const
  74. {
  75. const size_t center_step = ((num_steps - 1) / 2);
  76. if (step == center_step) {
  77. /* Center step: regular vertex location. */
  78. r_verts[0] = verts[v[0]];
  79. r_verts[1] = verts[v[1]];
  80. r_verts[2] = verts[v[2]];
  81. }
  82. else {
  83. /* Center step not stored in the attribute array array. */
  84. if (step > center_step) {
  85. step--;
  86. }
  87. size_t offset = step * num_verts;
  88. r_verts[0] = vert_steps[offset + v[0]];
  89. r_verts[1] = vert_steps[offset + v[1]];
  90. r_verts[2] = vert_steps[offset + v[2]];
  91. }
  92. }
  93. float3 Mesh::Triangle::compute_normal(const float3 *verts) const
  94. {
  95. const float3 &v0 = verts[v[0]];
  96. const float3 &v1 = verts[v[1]];
  97. const float3 &v2 = verts[v[2]];
  98. const float3 norm = cross(v1 - v0, v2 - v0);
  99. const float normlen = len(norm);
  100. if (normlen == 0.0f) {
  101. return make_float3(1.0f, 0.0f, 0.0f);
  102. }
  103. return norm / normlen;
  104. }
  105. bool Mesh::Triangle::valid(const float3 *verts) const
  106. {
  107. return isfinite3_safe(verts[v[0]]) && isfinite3_safe(verts[v[1]]) && isfinite3_safe(verts[v[2]]);
  108. }
  109. /* Curve */
  110. void Mesh::Curve::bounds_grow(const int k,
  111. const float3 *curve_keys,
  112. const float *curve_radius,
  113. BoundBox &bounds) const
  114. {
  115. float3 P[4];
  116. P[0] = curve_keys[max(first_key + k - 1, first_key)];
  117. P[1] = curve_keys[first_key + k];
  118. P[2] = curve_keys[first_key + k + 1];
  119. P[3] = curve_keys[min(first_key + k + 2, first_key + num_keys - 1)];
  120. float3 lower;
  121. float3 upper;
  122. curvebounds(&lower.x, &upper.x, P, 0);
  123. curvebounds(&lower.y, &upper.y, P, 1);
  124. curvebounds(&lower.z, &upper.z, P, 2);
  125. float mr = max(curve_radius[first_key + k], curve_radius[first_key + k + 1]);
  126. bounds.grow(lower, mr);
  127. bounds.grow(upper, mr);
  128. }
  129. void Mesh::Curve::bounds_grow(const int k,
  130. const float3 *curve_keys,
  131. const float *curve_radius,
  132. const Transform &aligned_space,
  133. BoundBox &bounds) const
  134. {
  135. float3 P[4];
  136. P[0] = curve_keys[max(first_key + k - 1, first_key)];
  137. P[1] = curve_keys[first_key + k];
  138. P[2] = curve_keys[first_key + k + 1];
  139. P[3] = curve_keys[min(first_key + k + 2, first_key + num_keys - 1)];
  140. P[0] = transform_point(&aligned_space, P[0]);
  141. P[1] = transform_point(&aligned_space, P[1]);
  142. P[2] = transform_point(&aligned_space, P[2]);
  143. P[3] = transform_point(&aligned_space, P[3]);
  144. float3 lower;
  145. float3 upper;
  146. curvebounds(&lower.x, &upper.x, P, 0);
  147. curvebounds(&lower.y, &upper.y, P, 1);
  148. curvebounds(&lower.z, &upper.z, P, 2);
  149. float mr = max(curve_radius[first_key + k], curve_radius[first_key + k + 1]);
  150. bounds.grow(lower, mr);
  151. bounds.grow(upper, mr);
  152. }
  153. void Mesh::Curve::bounds_grow(float4 keys[4], BoundBox &bounds) const
  154. {
  155. float3 P[4] = {
  156. float4_to_float3(keys[0]),
  157. float4_to_float3(keys[1]),
  158. float4_to_float3(keys[2]),
  159. float4_to_float3(keys[3]),
  160. };
  161. float3 lower;
  162. float3 upper;
  163. curvebounds(&lower.x, &upper.x, P, 0);
  164. curvebounds(&lower.y, &upper.y, P, 1);
  165. curvebounds(&lower.z, &upper.z, P, 2);
  166. float mr = max(keys[1].w, keys[2].w);
  167. bounds.grow(lower, mr);
  168. bounds.grow(upper, mr);
  169. }
  170. void Mesh::Curve::motion_keys(const float3 *curve_keys,
  171. const float *curve_radius,
  172. const float3 *key_steps,
  173. size_t num_curve_keys,
  174. size_t num_steps,
  175. float time,
  176. size_t k0,
  177. size_t k1,
  178. float4 r_keys[2]) const
  179. {
  180. /* Figure out which steps we need to fetch and their interpolation factor. */
  181. const size_t max_step = num_steps - 1;
  182. const size_t step = min((int)(time * max_step), max_step - 1);
  183. const float t = time * max_step - step;
  184. /* Fetch vertex coordinates. */
  185. float4 curr_keys[2];
  186. float4 next_keys[2];
  187. keys_for_step(
  188. curve_keys, curve_radius, key_steps, num_curve_keys, num_steps, step, k0, k1, curr_keys);
  189. keys_for_step(
  190. curve_keys, curve_radius, key_steps, num_curve_keys, num_steps, step + 1, k0, k1, next_keys);
  191. /* Interpolate between steps. */
  192. r_keys[0] = (1.0f - t) * curr_keys[0] + t * next_keys[0];
  193. r_keys[1] = (1.0f - t) * curr_keys[1] + t * next_keys[1];
  194. }
  195. void Mesh::Curve::cardinal_motion_keys(const float3 *curve_keys,
  196. const float *curve_radius,
  197. const float3 *key_steps,
  198. size_t num_curve_keys,
  199. size_t num_steps,
  200. float time,
  201. size_t k0,
  202. size_t k1,
  203. size_t k2,
  204. size_t k3,
  205. float4 r_keys[4]) const
  206. {
  207. /* Figure out which steps we need to fetch and their interpolation factor. */
  208. const size_t max_step = num_steps - 1;
  209. const size_t step = min((int)(time * max_step), max_step - 1);
  210. const float t = time * max_step - step;
  211. /* Fetch vertex coordinates. */
  212. float4 curr_keys[4];
  213. float4 next_keys[4];
  214. cardinal_keys_for_step(curve_keys,
  215. curve_radius,
  216. key_steps,
  217. num_curve_keys,
  218. num_steps,
  219. step,
  220. k0,
  221. k1,
  222. k2,
  223. k3,
  224. curr_keys);
  225. cardinal_keys_for_step(curve_keys,
  226. curve_radius,
  227. key_steps,
  228. num_curve_keys,
  229. num_steps,
  230. step + 1,
  231. k0,
  232. k1,
  233. k2,
  234. k3,
  235. next_keys);
  236. /* Interpolate between steps. */
  237. r_keys[0] = (1.0f - t) * curr_keys[0] + t * next_keys[0];
  238. r_keys[1] = (1.0f - t) * curr_keys[1] + t * next_keys[1];
  239. r_keys[2] = (1.0f - t) * curr_keys[2] + t * next_keys[2];
  240. r_keys[3] = (1.0f - t) * curr_keys[3] + t * next_keys[3];
  241. }
  242. void Mesh::Curve::keys_for_step(const float3 *curve_keys,
  243. const float *curve_radius,
  244. const float3 *key_steps,
  245. size_t num_curve_keys,
  246. size_t num_steps,
  247. size_t step,
  248. size_t k0,
  249. size_t k1,
  250. float4 r_keys[2]) const
  251. {
  252. k0 = max(k0, 0);
  253. k1 = min(k1, num_keys - 1);
  254. const size_t center_step = ((num_steps - 1) / 2);
  255. if (step == center_step) {
  256. /* Center step: regular key location. */
  257. /* TODO(sergey): Consider adding make_float4(float3, float)
  258. * function.
  259. */
  260. r_keys[0] = make_float4(curve_keys[first_key + k0].x,
  261. curve_keys[first_key + k0].y,
  262. curve_keys[first_key + k0].z,
  263. curve_radius[first_key + k0]);
  264. r_keys[1] = make_float4(curve_keys[first_key + k1].x,
  265. curve_keys[first_key + k1].y,
  266. curve_keys[first_key + k1].z,
  267. curve_radius[first_key + k1]);
  268. }
  269. else {
  270. /* Center step is not stored in this array. */
  271. if (step > center_step) {
  272. step--;
  273. }
  274. const size_t offset = first_key + step * num_curve_keys;
  275. r_keys[0] = make_float4(key_steps[offset + k0].x,
  276. key_steps[offset + k0].y,
  277. key_steps[offset + k0].z,
  278. curve_radius[first_key + k0]);
  279. r_keys[1] = make_float4(key_steps[offset + k1].x,
  280. key_steps[offset + k1].y,
  281. key_steps[offset + k1].z,
  282. curve_radius[first_key + k1]);
  283. }
  284. }
  285. void Mesh::Curve::cardinal_keys_for_step(const float3 *curve_keys,
  286. const float *curve_radius,
  287. const float3 *key_steps,
  288. size_t num_curve_keys,
  289. size_t num_steps,
  290. size_t step,
  291. size_t k0,
  292. size_t k1,
  293. size_t k2,
  294. size_t k3,
  295. float4 r_keys[4]) const
  296. {
  297. k0 = max(k0, 0);
  298. k3 = min(k3, num_keys - 1);
  299. const size_t center_step = ((num_steps - 1) / 2);
  300. if (step == center_step) {
  301. /* Center step: regular key location. */
  302. r_keys[0] = make_float4(curve_keys[first_key + k0].x,
  303. curve_keys[first_key + k0].y,
  304. curve_keys[first_key + k0].z,
  305. curve_radius[first_key + k0]);
  306. r_keys[1] = make_float4(curve_keys[first_key + k1].x,
  307. curve_keys[first_key + k1].y,
  308. curve_keys[first_key + k1].z,
  309. curve_radius[first_key + k1]);
  310. r_keys[2] = make_float4(curve_keys[first_key + k2].x,
  311. curve_keys[first_key + k2].y,
  312. curve_keys[first_key + k2].z,
  313. curve_radius[first_key + k2]);
  314. r_keys[3] = make_float4(curve_keys[first_key + k3].x,
  315. curve_keys[first_key + k3].y,
  316. curve_keys[first_key + k3].z,
  317. curve_radius[first_key + k3]);
  318. }
  319. else {
  320. /* Center step is not stored in this array. */
  321. if (step > center_step) {
  322. step--;
  323. }
  324. const size_t offset = first_key + step * num_curve_keys;
  325. r_keys[0] = make_float4(key_steps[offset + k0].x,
  326. key_steps[offset + k0].y,
  327. key_steps[offset + k0].z,
  328. curve_radius[first_key + k0]);
  329. r_keys[1] = make_float4(key_steps[offset + k1].x,
  330. key_steps[offset + k1].y,
  331. key_steps[offset + k1].z,
  332. curve_radius[first_key + k1]);
  333. r_keys[2] = make_float4(key_steps[offset + k2].x,
  334. key_steps[offset + k2].y,
  335. key_steps[offset + k2].z,
  336. curve_radius[first_key + k2]);
  337. r_keys[3] = make_float4(key_steps[offset + k3].x,
  338. key_steps[offset + k3].y,
  339. key_steps[offset + k3].z,
  340. curve_radius[first_key + k3]);
  341. }
  342. }
  343. /* SubdFace */
  344. float3 Mesh::SubdFace::normal(const Mesh *mesh) const
  345. {
  346. float3 v0 = mesh->verts[mesh->subd_face_corners[start_corner + 0]];
  347. float3 v1 = mesh->verts[mesh->subd_face_corners[start_corner + 1]];
  348. float3 v2 = mesh->verts[mesh->subd_face_corners[start_corner + 2]];
  349. return safe_normalize(cross(v1 - v0, v2 - v0));
  350. }
  351. /* Mesh */
  352. NODE_DEFINE(Mesh)
  353. {
  354. NodeType *type = NodeType::add("mesh", create);
  355. SOCKET_UINT(motion_steps, "Motion Steps", 3);
  356. SOCKET_BOOLEAN(use_motion_blur, "Use Motion Blur", false);
  357. SOCKET_INT_ARRAY(triangles, "Triangles", array<int>());
  358. SOCKET_POINT_ARRAY(verts, "Vertices", array<float3>());
  359. SOCKET_INT_ARRAY(shader, "Shader", array<int>());
  360. SOCKET_BOOLEAN_ARRAY(smooth, "Smooth", array<bool>());
  361. SOCKET_POINT_ARRAY(curve_keys, "Curve Keys", array<float3>());
  362. SOCKET_FLOAT_ARRAY(curve_radius, "Curve Radius", array<float>());
  363. SOCKET_INT_ARRAY(curve_first_key, "Curve First Key", array<int>());
  364. SOCKET_INT_ARRAY(curve_shader, "Curve Shader", array<int>());
  365. return type;
  366. }
  367. Mesh::Mesh() : Node(node_type)
  368. {
  369. need_update = true;
  370. need_update_rebuild = false;
  371. transform_applied = false;
  372. transform_negative_scaled = false;
  373. transform_normal = transform_identity();
  374. bounds = BoundBox::empty;
  375. bvh = NULL;
  376. tri_offset = 0;
  377. vert_offset = 0;
  378. curve_offset = 0;
  379. curvekey_offset = 0;
  380. patch_offset = 0;
  381. face_offset = 0;
  382. corner_offset = 0;
  383. attr_map_offset = 0;
  384. num_subd_verts = 0;
  385. attributes.triangle_mesh = this;
  386. curve_attributes.curve_mesh = this;
  387. subd_attributes.subd_mesh = this;
  388. geometry_flags = GEOMETRY_NONE;
  389. volume_isovalue = 0.001f;
  390. has_volume = false;
  391. has_surface_bssrdf = false;
  392. num_ngons = 0;
  393. subdivision_type = SUBDIVISION_NONE;
  394. subd_params = NULL;
  395. patch_table = NULL;
  396. }
  397. Mesh::~Mesh()
  398. {
  399. delete bvh;
  400. delete patch_table;
  401. delete subd_params;
  402. }
  403. void Mesh::resize_mesh(int numverts, int numtris)
  404. {
  405. verts.resize(numverts);
  406. triangles.resize(numtris * 3);
  407. shader.resize(numtris);
  408. smooth.resize(numtris);
  409. if (subd_faces.size()) {
  410. triangle_patch.resize(numtris);
  411. vert_patch_uv.resize(numverts);
  412. }
  413. attributes.resize();
  414. }
  415. void Mesh::reserve_mesh(int numverts, int numtris)
  416. {
  417. /* reserve space to add verts and triangles later */
  418. verts.reserve(numverts);
  419. triangles.reserve(numtris * 3);
  420. shader.reserve(numtris);
  421. smooth.reserve(numtris);
  422. if (subd_faces.size()) {
  423. triangle_patch.reserve(numtris);
  424. vert_patch_uv.reserve(numverts);
  425. }
  426. attributes.resize(true);
  427. }
  428. void Mesh::resize_curves(int numcurves, int numkeys)
  429. {
  430. curve_keys.resize(numkeys);
  431. curve_radius.resize(numkeys);
  432. curve_first_key.resize(numcurves);
  433. curve_shader.resize(numcurves);
  434. curve_attributes.resize();
  435. }
  436. void Mesh::reserve_curves(int numcurves, int numkeys)
  437. {
  438. curve_keys.reserve(numkeys);
  439. curve_radius.reserve(numkeys);
  440. curve_first_key.reserve(numcurves);
  441. curve_shader.reserve(numcurves);
  442. curve_attributes.resize(true);
  443. }
  444. void Mesh::resize_subd_faces(int numfaces, int num_ngons_, int numcorners)
  445. {
  446. subd_faces.resize(numfaces);
  447. subd_face_corners.resize(numcorners);
  448. num_ngons = num_ngons_;
  449. subd_attributes.resize();
  450. }
  451. void Mesh::reserve_subd_faces(int numfaces, int num_ngons_, int numcorners)
  452. {
  453. subd_faces.reserve(numfaces);
  454. subd_face_corners.reserve(numcorners);
  455. num_ngons = num_ngons_;
  456. subd_attributes.resize(true);
  457. }
  458. void Mesh::clear(bool preserve_voxel_data)
  459. {
  460. /* clear all verts and triangles */
  461. verts.clear();
  462. triangles.clear();
  463. shader.clear();
  464. smooth.clear();
  465. triangle_patch.clear();
  466. vert_patch_uv.clear();
  467. curve_keys.clear();
  468. curve_radius.clear();
  469. curve_first_key.clear();
  470. curve_shader.clear();
  471. subd_faces.clear();
  472. subd_face_corners.clear();
  473. num_subd_verts = 0;
  474. subd_creases.clear();
  475. curve_attributes.clear();
  476. subd_attributes.clear();
  477. attributes.clear(preserve_voxel_data);
  478. used_shaders.clear();
  479. if (!preserve_voxel_data) {
  480. geometry_flags = GEOMETRY_NONE;
  481. }
  482. transform_applied = false;
  483. transform_negative_scaled = false;
  484. transform_normal = transform_identity();
  485. delete patch_table;
  486. patch_table = NULL;
  487. }
  488. void Mesh::add_vertex(float3 P)
  489. {
  490. verts.push_back_reserved(P);
  491. if (subd_faces.size()) {
  492. vert_patch_uv.push_back_reserved(make_float2(0.0f, 0.0f));
  493. }
  494. }
  495. void Mesh::add_vertex_slow(float3 P)
  496. {
  497. verts.push_back_slow(P);
  498. if (subd_faces.size()) {
  499. vert_patch_uv.push_back_slow(make_float2(0.0f, 0.0f));
  500. }
  501. }
  502. void Mesh::add_triangle(int v0, int v1, int v2, int shader_, bool smooth_)
  503. {
  504. triangles.push_back_reserved(v0);
  505. triangles.push_back_reserved(v1);
  506. triangles.push_back_reserved(v2);
  507. shader.push_back_reserved(shader_);
  508. smooth.push_back_reserved(smooth_);
  509. if (subd_faces.size()) {
  510. triangle_patch.push_back_reserved(-1);
  511. }
  512. }
  513. void Mesh::add_curve_key(float3 co, float radius)
  514. {
  515. curve_keys.push_back_reserved(co);
  516. curve_radius.push_back_reserved(radius);
  517. }
  518. void Mesh::add_curve(int first_key, int shader)
  519. {
  520. curve_first_key.push_back_reserved(first_key);
  521. curve_shader.push_back_reserved(shader);
  522. }
  523. void Mesh::add_subd_face(int *corners, int num_corners, int shader_, bool smooth_)
  524. {
  525. int start_corner = subd_face_corners.size();
  526. for (int i = 0; i < num_corners; i++) {
  527. subd_face_corners.push_back_reserved(corners[i]);
  528. }
  529. int ptex_offset = 0;
  530. if (subd_faces.size()) {
  531. SubdFace &s = subd_faces[subd_faces.size() - 1];
  532. ptex_offset = s.ptex_offset + s.num_ptex_faces();
  533. }
  534. SubdFace face = {start_corner, num_corners, shader_, smooth_, ptex_offset};
  535. subd_faces.push_back_reserved(face);
  536. }
  537. void Mesh::compute_bounds()
  538. {
  539. BoundBox bnds = BoundBox::empty;
  540. size_t verts_size = verts.size();
  541. size_t curve_keys_size = curve_keys.size();
  542. if (verts_size + curve_keys_size > 0) {
  543. for (size_t i = 0; i < verts_size; i++)
  544. bnds.grow(verts[i]);
  545. for (size_t i = 0; i < curve_keys_size; i++)
  546. bnds.grow(curve_keys[i], curve_radius[i]);
  547. Attribute *attr = attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
  548. if (use_motion_blur && attr) {
  549. size_t steps_size = verts.size() * (motion_steps - 1);
  550. float3 *vert_steps = attr->data_float3();
  551. for (size_t i = 0; i < steps_size; i++)
  552. bnds.grow(vert_steps[i]);
  553. }
  554. Attribute *curve_attr = curve_attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
  555. if (use_motion_blur && curve_attr) {
  556. size_t steps_size = curve_keys.size() * (motion_steps - 1);
  557. float3 *key_steps = curve_attr->data_float3();
  558. for (size_t i = 0; i < steps_size; i++)
  559. bnds.grow(key_steps[i]);
  560. }
  561. if (!bnds.valid()) {
  562. bnds = BoundBox::empty;
  563. /* skip nan or inf coordinates */
  564. for (size_t i = 0; i < verts_size; i++)
  565. bnds.grow_safe(verts[i]);
  566. for (size_t i = 0; i < curve_keys_size; i++)
  567. bnds.grow_safe(curve_keys[i], curve_radius[i]);
  568. if (use_motion_blur && attr) {
  569. size_t steps_size = verts.size() * (motion_steps - 1);
  570. float3 *vert_steps = attr->data_float3();
  571. for (size_t i = 0; i < steps_size; i++)
  572. bnds.grow_safe(vert_steps[i]);
  573. }
  574. if (use_motion_blur && curve_attr) {
  575. size_t steps_size = curve_keys.size() * (motion_steps - 1);
  576. float3 *key_steps = curve_attr->data_float3();
  577. for (size_t i = 0; i < steps_size; i++)
  578. bnds.grow_safe(key_steps[i]);
  579. }
  580. }
  581. }
  582. if (!bnds.valid()) {
  583. /* empty mesh */
  584. bnds.grow(make_float3(0.0f, 0.0f, 0.0f));
  585. }
  586. bounds = bnds;
  587. }
  588. void Mesh::add_face_normals()
  589. {
  590. /* don't compute if already there */
  591. if (attributes.find(ATTR_STD_FACE_NORMAL))
  592. return;
  593. /* get attributes */
  594. Attribute *attr_fN = attributes.add(ATTR_STD_FACE_NORMAL);
  595. float3 *fN = attr_fN->data_float3();
  596. /* compute face normals */
  597. size_t triangles_size = num_triangles();
  598. if (triangles_size) {
  599. float3 *verts_ptr = verts.data();
  600. for (size_t i = 0; i < triangles_size; i++) {
  601. fN[i] = get_triangle(i).compute_normal(verts_ptr);
  602. }
  603. }
  604. /* expected to be in local space */
  605. if (transform_applied) {
  606. Transform ntfm = transform_inverse(transform_normal);
  607. for (size_t i = 0; i < triangles_size; i++)
  608. fN[i] = normalize(transform_direction(&ntfm, fN[i]));
  609. }
  610. }
  611. void Mesh::add_vertex_normals()
  612. {
  613. bool flip = transform_negative_scaled;
  614. size_t verts_size = verts.size();
  615. size_t triangles_size = num_triangles();
  616. /* static vertex normals */
  617. if (!attributes.find(ATTR_STD_VERTEX_NORMAL) && triangles_size) {
  618. /* get attributes */
  619. Attribute *attr_fN = attributes.find(ATTR_STD_FACE_NORMAL);
  620. Attribute *attr_vN = attributes.add(ATTR_STD_VERTEX_NORMAL);
  621. float3 *fN = attr_fN->data_float3();
  622. float3 *vN = attr_vN->data_float3();
  623. /* compute vertex normals */
  624. memset(vN, 0, verts.size() * sizeof(float3));
  625. for (size_t i = 0; i < triangles_size; i++) {
  626. for (size_t j = 0; j < 3; j++) {
  627. vN[get_triangle(i).v[j]] += fN[i];
  628. }
  629. }
  630. for (size_t i = 0; i < verts_size; i++) {
  631. vN[i] = normalize(vN[i]);
  632. if (flip) {
  633. vN[i] = -vN[i];
  634. }
  635. }
  636. }
  637. /* motion vertex normals */
  638. Attribute *attr_mP = attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
  639. Attribute *attr_mN = attributes.find(ATTR_STD_MOTION_VERTEX_NORMAL);
  640. if (has_motion_blur() && attr_mP && !attr_mN && triangles_size) {
  641. /* create attribute */
  642. attr_mN = attributes.add(ATTR_STD_MOTION_VERTEX_NORMAL);
  643. for (int step = 0; step < motion_steps - 1; step++) {
  644. float3 *mP = attr_mP->data_float3() + step * verts.size();
  645. float3 *mN = attr_mN->data_float3() + step * verts.size();
  646. /* compute */
  647. memset(mN, 0, verts.size() * sizeof(float3));
  648. for (size_t i = 0; i < triangles_size; i++) {
  649. for (size_t j = 0; j < 3; j++) {
  650. float3 fN = get_triangle(i).compute_normal(mP);
  651. mN[get_triangle(i).v[j]] += fN;
  652. }
  653. }
  654. for (size_t i = 0; i < verts_size; i++) {
  655. mN[i] = normalize(mN[i]);
  656. if (flip) {
  657. mN[i] = -mN[i];
  658. }
  659. }
  660. }
  661. }
  662. /* subd vertex normals */
  663. if (!subd_attributes.find(ATTR_STD_VERTEX_NORMAL) && subd_faces.size()) {
  664. /* get attributes */
  665. Attribute *attr_vN = subd_attributes.add(ATTR_STD_VERTEX_NORMAL);
  666. float3 *vN = attr_vN->data_float3();
  667. /* compute vertex normals */
  668. memset(vN, 0, verts.size() * sizeof(float3));
  669. for (size_t i = 0; i < subd_faces.size(); i++) {
  670. SubdFace &face = subd_faces[i];
  671. float3 fN = face.normal(this);
  672. for (size_t j = 0; j < face.num_corners; j++) {
  673. size_t corner = subd_face_corners[face.start_corner + j];
  674. vN[corner] += fN;
  675. }
  676. }
  677. for (size_t i = 0; i < verts_size; i++) {
  678. vN[i] = normalize(vN[i]);
  679. if (flip) {
  680. vN[i] = -vN[i];
  681. }
  682. }
  683. }
  684. }
  685. void Mesh::add_undisplaced()
  686. {
  687. AttributeSet &attrs = (subdivision_type == SUBDIVISION_NONE) ? attributes : subd_attributes;
  688. /* don't compute if already there */
  689. if (attrs.find(ATTR_STD_POSITION_UNDISPLACED)) {
  690. return;
  691. }
  692. /* get attribute */
  693. Attribute *attr = attrs.add(ATTR_STD_POSITION_UNDISPLACED);
  694. attr->flags |= ATTR_SUBDIVIDED;
  695. float3 *data = attr->data_float3();
  696. /* copy verts */
  697. size_t size = attr->buffer_size(
  698. this, (subdivision_type == SUBDIVISION_NONE) ? ATTR_PRIM_TRIANGLE : ATTR_PRIM_SUBD);
  699. /* Center points for ngons aren't stored in Mesh::verts but are included in size since they will
  700. * be calculated later, we subtract them from size here so we don't have an overflow while
  701. * copying.
  702. */
  703. size -= num_ngons * attr->data_sizeof();
  704. if (size) {
  705. memcpy(data, verts.data(), size);
  706. }
  707. }
  708. void Mesh::pack_shaders(Scene *scene, uint *tri_shader)
  709. {
  710. uint shader_id = 0;
  711. uint last_shader = -1;
  712. bool last_smooth = false;
  713. size_t triangles_size = num_triangles();
  714. int *shader_ptr = shader.data();
  715. for (size_t i = 0; i < triangles_size; i++) {
  716. if (shader_ptr[i] != last_shader || last_smooth != smooth[i]) {
  717. last_shader = shader_ptr[i];
  718. last_smooth = smooth[i];
  719. Shader *shader = (last_shader < used_shaders.size()) ? used_shaders[last_shader] :
  720. scene->default_surface;
  721. shader_id = scene->shader_manager->get_shader_id(shader, last_smooth);
  722. }
  723. tri_shader[i] = shader_id;
  724. }
  725. }
  726. void Mesh::pack_normals(float4 *vnormal)
  727. {
  728. Attribute *attr_vN = attributes.find(ATTR_STD_VERTEX_NORMAL);
  729. if (attr_vN == NULL) {
  730. /* Happens on objects with just hair. */
  731. return;
  732. }
  733. bool do_transform = transform_applied;
  734. Transform ntfm = transform_normal;
  735. float3 *vN = attr_vN->data_float3();
  736. size_t verts_size = verts.size();
  737. for (size_t i = 0; i < verts_size; i++) {
  738. float3 vNi = vN[i];
  739. if (do_transform)
  740. vNi = safe_normalize(transform_direction(&ntfm, vNi));
  741. vnormal[i] = make_float4(vNi.x, vNi.y, vNi.z, 0.0f);
  742. }
  743. }
  744. void Mesh::pack_verts(const vector<uint> &tri_prim_index,
  745. uint4 *tri_vindex,
  746. uint *tri_patch,
  747. float2 *tri_patch_uv,
  748. size_t vert_offset,
  749. size_t tri_offset)
  750. {
  751. size_t verts_size = verts.size();
  752. if (verts_size && subd_faces.size()) {
  753. float2 *vert_patch_uv_ptr = vert_patch_uv.data();
  754. for (size_t i = 0; i < verts_size; i++) {
  755. tri_patch_uv[i] = vert_patch_uv_ptr[i];
  756. }
  757. }
  758. size_t triangles_size = num_triangles();
  759. for (size_t i = 0; i < triangles_size; i++) {
  760. Triangle t = get_triangle(i);
  761. tri_vindex[i] = make_uint4(t.v[0] + vert_offset,
  762. t.v[1] + vert_offset,
  763. t.v[2] + vert_offset,
  764. tri_prim_index[i + tri_offset]);
  765. tri_patch[i] = (!subd_faces.size()) ? -1 : (triangle_patch[i] * 8 + patch_offset);
  766. }
  767. }
  768. void Mesh::pack_curves(Scene *scene,
  769. float4 *curve_key_co,
  770. float4 *curve_data,
  771. size_t curvekey_offset)
  772. {
  773. size_t curve_keys_size = curve_keys.size();
  774. /* pack curve keys */
  775. if (curve_keys_size) {
  776. float3 *keys_ptr = curve_keys.data();
  777. float *radius_ptr = curve_radius.data();
  778. for (size_t i = 0; i < curve_keys_size; i++)
  779. curve_key_co[i] = make_float4(keys_ptr[i].x, keys_ptr[i].y, keys_ptr[i].z, radius_ptr[i]);
  780. }
  781. /* pack curve segments */
  782. size_t curve_num = num_curves();
  783. for (size_t i = 0; i < curve_num; i++) {
  784. Curve curve = get_curve(i);
  785. int shader_id = curve_shader[i];
  786. Shader *shader = (shader_id < used_shaders.size()) ? used_shaders[shader_id] :
  787. scene->default_surface;
  788. shader_id = scene->shader_manager->get_shader_id(shader, false);
  789. curve_data[i] = make_float4(__int_as_float(curve.first_key + curvekey_offset),
  790. __int_as_float(curve.num_keys),
  791. __int_as_float(shader_id),
  792. 0.0f);
  793. }
  794. }
  795. void Mesh::pack_patches(uint *patch_data, uint vert_offset, uint face_offset, uint corner_offset)
  796. {
  797. size_t num_faces = subd_faces.size();
  798. int ngons = 0;
  799. for (size_t f = 0; f < num_faces; f++) {
  800. SubdFace face = subd_faces[f];
  801. if (face.is_quad()) {
  802. int c[4];
  803. memcpy(c, &subd_face_corners[face.start_corner], sizeof(int) * 4);
  804. *(patch_data++) = c[0] + vert_offset;
  805. *(patch_data++) = c[1] + vert_offset;
  806. *(patch_data++) = c[2] + vert_offset;
  807. *(patch_data++) = c[3] + vert_offset;
  808. *(patch_data++) = f + face_offset;
  809. *(patch_data++) = face.num_corners;
  810. *(patch_data++) = face.start_corner + corner_offset;
  811. *(patch_data++) = 0;
  812. }
  813. else {
  814. for (int i = 0; i < face.num_corners; i++) {
  815. int c[4];
  816. c[0] = subd_face_corners[face.start_corner + mod(i + 0, face.num_corners)];
  817. c[1] = subd_face_corners[face.start_corner + mod(i + 1, face.num_corners)];
  818. c[2] = verts.size() - num_subd_verts + ngons;
  819. c[3] = subd_face_corners[face.start_corner + mod(i - 1, face.num_corners)];
  820. *(patch_data++) = c[0] + vert_offset;
  821. *(patch_data++) = c[1] + vert_offset;
  822. *(patch_data++) = c[2] + vert_offset;
  823. *(patch_data++) = c[3] + vert_offset;
  824. *(patch_data++) = f + face_offset;
  825. *(patch_data++) = face.num_corners | (i << 16);
  826. *(patch_data++) = face.start_corner + corner_offset;
  827. *(patch_data++) = subd_face_corners.size() + ngons + corner_offset;
  828. }
  829. ngons++;
  830. }
  831. }
  832. }
  833. void Mesh::compute_bvh(
  834. Device *device, DeviceScene *dscene, SceneParams *params, Progress *progress, int n, int total)
  835. {
  836. if (progress->get_cancel())
  837. return;
  838. compute_bounds();
  839. if (need_build_bvh()) {
  840. string msg = "Updating Mesh BVH ";
  841. if (name == "")
  842. msg += string_printf("%u/%u", (uint)(n + 1), (uint)total);
  843. else
  844. msg += string_printf("%s %u/%u", name.c_str(), (uint)(n + 1), (uint)total);
  845. Object object;
  846. object.mesh = this;
  847. vector<Object *> objects;
  848. objects.push_back(&object);
  849. if (bvh && !need_update_rebuild) {
  850. progress->set_status(msg, "Refitting BVH");
  851. bvh->objects = objects;
  852. bvh->refit(*progress);
  853. }
  854. else {
  855. progress->set_status(msg, "Building BVH");
  856. BVHParams bparams;
  857. bparams.use_spatial_split = params->use_bvh_spatial_split;
  858. bparams.bvh_layout = BVHParams::best_bvh_layout(params->bvh_layout,
  859. device->get_bvh_layout_mask());
  860. bparams.use_unaligned_nodes = dscene->data.bvh.have_curves &&
  861. params->use_bvh_unaligned_nodes;
  862. bparams.num_motion_triangle_steps = params->num_bvh_time_steps;
  863. bparams.num_motion_curve_steps = params->num_bvh_time_steps;
  864. bparams.bvh_type = params->bvh_type;
  865. bparams.curve_flags = dscene->data.curve.curveflags;
  866. bparams.curve_subdivisions = dscene->data.curve.subdivisions;
  867. delete bvh;
  868. bvh = BVH::create(bparams, objects);
  869. MEM_GUARDED_CALL(progress, bvh->build, *progress);
  870. }
  871. }
  872. need_update = false;
  873. need_update_rebuild = false;
  874. }
  875. void Mesh::tag_update(Scene *scene, bool rebuild)
  876. {
  877. need_update = true;
  878. if (rebuild) {
  879. need_update_rebuild = true;
  880. scene->light_manager->need_update = true;
  881. }
  882. else {
  883. foreach (Shader *shader, used_shaders)
  884. if (shader->has_surface_emission)
  885. scene->light_manager->need_update = true;
  886. }
  887. scene->mesh_manager->need_update = true;
  888. scene->object_manager->need_update = true;
  889. }
  890. bool Mesh::has_motion_blur() const
  891. {
  892. return (use_motion_blur && (attributes.find(ATTR_STD_MOTION_VERTEX_POSITION) ||
  893. curve_attributes.find(ATTR_STD_MOTION_VERTEX_POSITION)));
  894. }
  895. bool Mesh::has_true_displacement() const
  896. {
  897. foreach (Shader *shader, used_shaders) {
  898. if (shader->has_displacement && shader->displacement_method != DISPLACE_BUMP) {
  899. return true;
  900. }
  901. }
  902. return false;
  903. }
  904. float Mesh::motion_time(int step) const
  905. {
  906. return (motion_steps > 1) ? 2.0f * step / (motion_steps - 1) - 1.0f : 0.0f;
  907. }
  908. int Mesh::motion_step(float time) const
  909. {
  910. if (motion_steps > 1) {
  911. int attr_step = 0;
  912. for (int step = 0; step < motion_steps; step++) {
  913. float step_time = motion_time(step);
  914. if (step_time == time) {
  915. return attr_step;
  916. }
  917. /* Center step is stored in a separate attribute. */
  918. if (step != motion_steps / 2) {
  919. attr_step++;
  920. }
  921. }
  922. }
  923. return -1;
  924. }
  925. bool Mesh::need_build_bvh() const
  926. {
  927. return !transform_applied || has_surface_bssrdf;
  928. }
  929. bool Mesh::is_instanced() const
  930. {
  931. /* Currently we treat subsurface objects as instanced.
  932. *
  933. * While it might be not very optimal for ray traversal, it avoids having
  934. * duplicated BVH in the memory, saving quite some space.
  935. */
  936. return !transform_applied || has_surface_bssrdf;
  937. }
  938. /* Mesh Manager */
  939. MeshManager::MeshManager()
  940. {
  941. need_update = true;
  942. need_flags_update = true;
  943. }
  944. MeshManager::~MeshManager()
  945. {
  946. }
  947. void MeshManager::update_osl_attributes(Device *device,
  948. Scene *scene,
  949. vector<AttributeRequestSet> &mesh_attributes)
  950. {
  951. #ifdef WITH_OSL
  952. /* for OSL, a hash map is used to lookup the attribute by name. */
  953. OSLGlobals *og = (OSLGlobals *)device->osl_memory();
  954. og->object_name_map.clear();
  955. og->attribute_map.clear();
  956. og->object_names.clear();
  957. og->attribute_map.resize(scene->objects.size() * ATTR_PRIM_TYPES);
  958. for (size_t i = 0; i < scene->objects.size(); i++) {
  959. /* set object name to object index map */
  960. Object *object = scene->objects[i];
  961. og->object_name_map[object->name] = i;
  962. og->object_names.push_back(object->name);
  963. /* set object attributes */
  964. foreach (ParamValue &attr, object->attributes) {
  965. OSLGlobals::Attribute osl_attr;
  966. osl_attr.type = attr.type();
  967. osl_attr.desc.element = ATTR_ELEMENT_OBJECT;
  968. osl_attr.value = attr;
  969. osl_attr.desc.offset = 0;
  970. osl_attr.desc.flags = 0;
  971. og->attribute_map[i * ATTR_PRIM_TYPES + ATTR_PRIM_TRIANGLE][attr.name()] = osl_attr;
  972. og->attribute_map[i * ATTR_PRIM_TYPES + ATTR_PRIM_CURVE][attr.name()] = osl_attr;
  973. og->attribute_map[i * ATTR_PRIM_TYPES + ATTR_PRIM_SUBD][attr.name()] = osl_attr;
  974. }
  975. /* find mesh attributes */
  976. size_t j;
  977. for (j = 0; j < scene->meshes.size(); j++)
  978. if (scene->meshes[j] == object->mesh)
  979. break;
  980. AttributeRequestSet &attributes = mesh_attributes[j];
  981. /* set object attributes */
  982. foreach (AttributeRequest &req, attributes.requests) {
  983. OSLGlobals::Attribute osl_attr;
  984. if (req.triangle_desc.element != ATTR_ELEMENT_NONE) {
  985. osl_attr.desc = req.triangle_desc;
  986. if (req.triangle_type == TypeDesc::TypeFloat)
  987. osl_attr.type = TypeDesc::TypeFloat;
  988. else if (req.triangle_type == TypeDesc::TypeMatrix)
  989. osl_attr.type = TypeDesc::TypeMatrix;
  990. else if (req.triangle_type == TypeFloat2)
  991. osl_attr.type = TypeFloat2;
  992. else
  993. osl_attr.type = TypeDesc::TypeColor;
  994. if (req.std != ATTR_STD_NONE) {
  995. /* if standard attribute, add lookup by geom: name convention */
  996. ustring stdname(string("geom:") + string(Attribute::standard_name(req.std)));
  997. og->attribute_map[i * ATTR_PRIM_TYPES + ATTR_PRIM_TRIANGLE][stdname] = osl_attr;
  998. }
  999. else if (req.name != ustring()) {
  1000. /* add lookup by mesh attribute name */
  1001. og->attribute_map[i * ATTR_PRIM_TYPES + ATTR_PRIM_TRIANGLE][req.name] = osl_attr;
  1002. }
  1003. }
  1004. if (req.curve_desc.element != ATTR_ELEMENT_NONE) {
  1005. osl_attr.desc = req.curve_desc;
  1006. if (req.curve_type == TypeDesc::TypeFloat)
  1007. osl_attr.type = TypeDesc::TypeFloat;
  1008. else if (req.curve_type == TypeDesc::TypeMatrix)
  1009. osl_attr.type = TypeDesc::TypeMatrix;
  1010. else if (req.curve_type == TypeFloat2)
  1011. osl_attr.type = TypeFloat2;
  1012. else
  1013. osl_attr.type = TypeDesc::TypeColor;
  1014. if (req.std != ATTR_STD_NONE) {
  1015. /* if standard attribute, add lookup by geom: name convention */
  1016. ustring stdname(string("geom:") + string(Attribute::standard_name(req.std)));
  1017. og->attribute_map[i * ATTR_PRIM_TYPES + ATTR_PRIM_CURVE][stdname] = osl_attr;
  1018. }
  1019. else if (req.name != ustring()) {
  1020. /* add lookup by mesh attribute name */
  1021. og->attribute_map[i * ATTR_PRIM_TYPES + ATTR_PRIM_CURVE][req.name] = osl_attr;
  1022. }
  1023. }
  1024. if (req.subd_desc.element != ATTR_ELEMENT_NONE) {
  1025. osl_attr.desc = req.subd_desc;
  1026. if (req.subd_type == TypeDesc::TypeFloat)
  1027. osl_attr.type = TypeDesc::TypeFloat;
  1028. else if (req.subd_type == TypeDesc::TypeMatrix)
  1029. osl_attr.type = TypeDesc::TypeMatrix;
  1030. else if (req.subd_type == TypeFloat2)
  1031. osl_attr.type = TypeFloat2;
  1032. else
  1033. osl_attr.type = TypeDesc::TypeColor;
  1034. if (req.std != ATTR_STD_NONE) {
  1035. /* if standard attribute, add lookup by geom: name convention */
  1036. ustring stdname(string("geom:") + string(Attribute::standard_name(req.std)));
  1037. og->attribute_map[i * ATTR_PRIM_TYPES + ATTR_PRIM_SUBD][stdname] = osl_attr;
  1038. }
  1039. else if (req.name != ustring()) {
  1040. /* add lookup by mesh attribute name */
  1041. og->attribute_map[i * ATTR_PRIM_TYPES + ATTR_PRIM_SUBD][req.name] = osl_attr;
  1042. }
  1043. }
  1044. }
  1045. }
  1046. #else
  1047. (void)device;
  1048. (void)scene;
  1049. (void)mesh_attributes;
  1050. #endif
  1051. }
  1052. void MeshManager::update_svm_attributes(Device *,
  1053. DeviceScene *dscene,
  1054. Scene *scene,
  1055. vector<AttributeRequestSet> &mesh_attributes)
  1056. {
  1057. /* for SVM, the attributes_map table is used to lookup the offset of an
  1058. * attribute, based on a unique shader attribute id. */
  1059. /* compute array stride */
  1060. int attr_map_size = 0;
  1061. for (size_t i = 0; i < scene->meshes.size(); i++) {
  1062. Mesh *mesh = scene->meshes[i];
  1063. mesh->attr_map_offset = attr_map_size;
  1064. attr_map_size += (mesh_attributes[i].size() + 1) * ATTR_PRIM_TYPES;
  1065. }
  1066. if (attr_map_size == 0)
  1067. return;
  1068. /* create attribute map */
  1069. uint4 *attr_map = dscene->attributes_map.alloc(attr_map_size);
  1070. memset(attr_map, 0, dscene->attributes_map.size() * sizeof(uint));
  1071. for (size_t i = 0; i < scene->meshes.size(); i++) {
  1072. Mesh *mesh = scene->meshes[i];
  1073. AttributeRequestSet &attributes = mesh_attributes[i];
  1074. /* set object attributes */
  1075. int index = mesh->attr_map_offset;
  1076. foreach (AttributeRequest &req, attributes.requests) {
  1077. uint id;
  1078. if (req.std == ATTR_STD_NONE)
  1079. id = scene->shader_manager->get_attribute_id(req.name);
  1080. else
  1081. id = scene->shader_manager->get_attribute_id(req.std);
  1082. if (mesh->num_triangles()) {
  1083. attr_map[index].x = id;
  1084. attr_map[index].y = req.triangle_desc.element;
  1085. attr_map[index].z = as_uint(req.triangle_desc.offset);
  1086. if (req.triangle_type == TypeDesc::TypeFloat)
  1087. attr_map[index].w = NODE_ATTR_FLOAT;
  1088. else if (req.triangle_type == TypeDesc::TypeMatrix)
  1089. attr_map[index].w = NODE_ATTR_MATRIX;
  1090. else if (req.triangle_type == TypeFloat2)
  1091. attr_map[index].w = NODE_ATTR_FLOAT2;
  1092. else
  1093. attr_map[index].w = NODE_ATTR_FLOAT3;
  1094. attr_map[index].w |= req.triangle_desc.flags << 8;
  1095. }
  1096. index++;
  1097. if (mesh->num_curves()) {
  1098. attr_map[index].x = id;
  1099. attr_map[index].y = req.curve_desc.element;
  1100. attr_map[index].z = as_uint(req.curve_desc.offset);
  1101. if (req.curve_type == TypeDesc::TypeFloat)
  1102. attr_map[index].w = NODE_ATTR_FLOAT;
  1103. else if (req.curve_type == TypeDesc::TypeMatrix)
  1104. attr_map[index].w = NODE_ATTR_MATRIX;
  1105. else if (req.curve_type == TypeFloat2)
  1106. attr_map[index].w = NODE_ATTR_FLOAT2;
  1107. else
  1108. attr_map[index].w = NODE_ATTR_FLOAT3;
  1109. attr_map[index].w |= req.curve_desc.flags << 8;
  1110. }
  1111. index++;
  1112. if (mesh->subd_faces.size()) {
  1113. attr_map[index].x = id;
  1114. attr_map[index].y = req.subd_desc.element;
  1115. attr_map[index].z = as_uint(req.subd_desc.offset);
  1116. if (req.subd_type == TypeDesc::TypeFloat)
  1117. attr_map[index].w = NODE_ATTR_FLOAT;
  1118. else if (req.subd_type == TypeDesc::TypeMatrix)
  1119. attr_map[index].w = NODE_ATTR_MATRIX;
  1120. else if (req.subd_type == TypeFloat2)
  1121. attr_map[index].w = NODE_ATTR_FLOAT2;
  1122. else
  1123. attr_map[index].w = NODE_ATTR_FLOAT3;
  1124. attr_map[index].w |= req.subd_desc.flags << 8;
  1125. }
  1126. index++;
  1127. }
  1128. /* terminator */
  1129. for (int j = 0; j < ATTR_PRIM_TYPES; j++) {
  1130. attr_map[index].x = ATTR_STD_NONE;
  1131. attr_map[index].y = 0;
  1132. attr_map[index].z = 0;
  1133. attr_map[index].w = 0;
  1134. index++;
  1135. }
  1136. }
  1137. /* copy to device */
  1138. dscene->attributes_map.copy_to_device();
  1139. }
  1140. static void update_attribute_element_size(Mesh *mesh,
  1141. Attribute *mattr,
  1142. AttributePrimitive prim,
  1143. size_t *attr_float_size,
  1144. size_t *attr_float2_size,
  1145. size_t *attr_float3_size,
  1146. size_t *attr_uchar4_size)
  1147. {
  1148. if (mattr) {
  1149. size_t size = mattr->element_size(mesh, prim);
  1150. if (mattr->element == ATTR_ELEMENT_VOXEL) {
  1151. /* pass */
  1152. }
  1153. else if (mattr->element == ATTR_ELEMENT_CORNER_BYTE) {
  1154. *attr_uchar4_size += size;
  1155. }
  1156. else if (mattr->type == TypeDesc::TypeFloat) {
  1157. *attr_float_size += size;
  1158. }
  1159. else if (mattr->type == TypeFloat2) {
  1160. *attr_float2_size += size;
  1161. }
  1162. else if (mattr->type == TypeDesc::TypeMatrix) {
  1163. *attr_float3_size += size * 4;
  1164. }
  1165. else {
  1166. *attr_float3_size += size;
  1167. }
  1168. }
  1169. }
  1170. static void update_attribute_element_offset(Mesh *mesh,
  1171. device_vector<float> &attr_float,
  1172. size_t &attr_float_offset,
  1173. device_vector<float2> &attr_float2,
  1174. size_t &attr_float2_offset,
  1175. device_vector<float4> &attr_float3,
  1176. size_t &attr_float3_offset,
  1177. device_vector<uchar4> &attr_uchar4,
  1178. size_t &attr_uchar4_offset,
  1179. Attribute *mattr,
  1180. AttributePrimitive prim,
  1181. TypeDesc &type,
  1182. AttributeDescriptor &desc)
  1183. {
  1184. if (mattr) {
  1185. /* store element and type */
  1186. desc.element = mattr->element;
  1187. desc.flags = mattr->flags;
  1188. type = mattr->type;
  1189. /* store attribute data in arrays */
  1190. size_t size = mattr->element_size(mesh, prim);
  1191. AttributeElement &element = desc.element;
  1192. int &offset = desc.offset;
  1193. if (mattr->element == ATTR_ELEMENT_VOXEL) {
  1194. /* store slot in offset value */
  1195. VoxelAttribute *voxel_data = mattr->data_voxel();
  1196. offset = voxel_data->slot;
  1197. }
  1198. else if (mattr->element == ATTR_ELEMENT_CORNER_BYTE) {
  1199. uchar4 *data = mattr->data_uchar4();
  1200. offset = attr_uchar4_offset;
  1201. assert(attr_uchar4.size() >= offset + size);
  1202. for (size_t k = 0; k < size; k++) {
  1203. attr_uchar4[offset + k] = data[k];
  1204. }
  1205. attr_uchar4_offset += size;
  1206. }
  1207. else if (mattr->type == TypeDesc::TypeFloat) {
  1208. float *data = mattr->data_float();
  1209. offset = attr_float_offset;
  1210. assert(attr_float.size() >= offset + size);
  1211. for (size_t k = 0; k < size; k++) {
  1212. attr_float[offset + k] = data[k];
  1213. }
  1214. attr_float_offset += size;
  1215. }
  1216. else if (mattr->type == TypeFloat2) {
  1217. float2 *data = mattr->data_float2();
  1218. offset = attr_float2_offset;
  1219. assert(attr_float2.size() >= offset + size);
  1220. for (size_t k = 0; k < size; k++) {
  1221. attr_float2[offset + k] = data[k];
  1222. }
  1223. attr_float2_offset += size;
  1224. }
  1225. else if (mattr->type == TypeDesc::TypeMatrix) {
  1226. Transform *tfm = mattr->data_transform();
  1227. offset = attr_float3_offset;
  1228. assert(attr_float3.size() >= offset + size * 3);
  1229. for (size_t k = 0; k < size * 3; k++) {
  1230. attr_float3[offset + k] = (&tfm->x)[k];
  1231. }
  1232. attr_float3_offset += size * 3;
  1233. }
  1234. else {
  1235. float4 *data = mattr->data_float4();
  1236. offset = attr_float3_offset;
  1237. assert(attr_float3.size() >= offset + size);
  1238. for (size_t k = 0; k < size; k++) {
  1239. attr_float3[offset + k] = data[k];
  1240. }
  1241. attr_float3_offset += size;
  1242. }
  1243. /* mesh vertex/curve index is global, not per object, so we sneak
  1244. * a correction for that in here */
  1245. if (mesh->subdivision_type == Mesh::SUBDIVISION_CATMULL_CLARK &&
  1246. desc.flags & ATTR_SUBDIVIDED) {
  1247. /* indices for subdivided attributes are retrieved
  1248. * from patch table so no need for correction here*/
  1249. }
  1250. else if (element == ATTR_ELEMENT_VERTEX)
  1251. offset -= mesh->vert_offset;
  1252. else if (element == ATTR_ELEMENT_VERTEX_MOTION)
  1253. offset -= mesh->vert_offset;
  1254. else if (element == ATTR_ELEMENT_FACE) {
  1255. if (prim == ATTR_PRIM_TRIANGLE)
  1256. offset -= mesh->tri_offset;
  1257. else
  1258. offset -= mesh->face_offset;
  1259. }
  1260. else if (element == ATTR_ELEMENT_CORNER || element == ATTR_ELEMENT_CORNER_BYTE) {
  1261. if (prim == ATTR_PRIM_TRIANGLE)
  1262. offset -= 3 * mesh->tri_offset;
  1263. else
  1264. offset -= mesh->corner_offset;
  1265. }
  1266. else if (element == ATTR_ELEMENT_CURVE)
  1267. offset -= mesh->curve_offset;
  1268. else if (element == ATTR_ELEMENT_CURVE_KEY)
  1269. offset -= mesh->curvekey_offset;
  1270. else if (element == ATTR_ELEMENT_CURVE_KEY_MOTION)
  1271. offset -= mesh->curvekey_offset;
  1272. }
  1273. else {
  1274. /* attribute not found */
  1275. desc.element = ATTR_ELEMENT_NONE;
  1276. desc.offset = 0;
  1277. }
  1278. }
  1279. void MeshManager::device_update_attributes(Device *device,
  1280. DeviceScene *dscene,
  1281. Scene *scene,
  1282. Progress &progress)
  1283. {
  1284. progress.set_status("Updating Mesh", "Computing attributes");
  1285. /* gather per mesh requested attributes. as meshes may have multiple
  1286. * shaders assigned, this merges the requested attributes that have
  1287. * been set per shader by the shader manager */
  1288. vector<AttributeRequestSet> mesh_attributes(scene->meshes.size());
  1289. for (size_t i = 0; i < scene->meshes.size(); i++) {
  1290. Mesh *mesh = scene->meshes[i];
  1291. scene->need_global_attributes(mesh_attributes[i]);
  1292. foreach (Shader *shader, mesh->used_shaders) {
  1293. mesh_attributes[i].add(shader->attributes);
  1294. }
  1295. }
  1296. /* mesh attribute are stored in a single array per data type. here we fill
  1297. * those arrays, and set the offset and element type to create attribute
  1298. * maps next */
  1299. /* Pre-allocate attributes to avoid arrays re-allocation which would
  1300. * take 2x of overall attribute memory usage.
  1301. */
  1302. size_t attr_float_size = 0;
  1303. size_t attr_float2_size = 0;
  1304. size_t attr_float3_size = 0;
  1305. size_t attr_uchar4_size = 0;
  1306. for (size_t i = 0; i < scene->meshes.size(); i++) {
  1307. Mesh *mesh = scene->meshes[i];
  1308. AttributeRequestSet &attributes = mesh_attributes[i];
  1309. foreach (AttributeRequest &req, attributes.requests) {
  1310. Attribute *triangle_mattr = mesh->attributes.find(req);
  1311. Attribute *curve_mattr = mesh->curve_attributes.find(req);
  1312. Attribute *subd_mattr = mesh->subd_attributes.find(req);
  1313. update_attribute_element_size(mesh,
  1314. triangle_mattr,
  1315. ATTR_PRIM_TRIANGLE,
  1316. &attr_float_size,
  1317. &attr_float2_size,
  1318. &attr_float3_size,
  1319. &attr_uchar4_size);
  1320. update_attribute_element_size(mesh,
  1321. curve_mattr,
  1322. ATTR_PRIM_CURVE,
  1323. &attr_float_size,
  1324. &attr_float2_size,
  1325. &attr_float3_size,
  1326. &attr_uchar4_size);
  1327. update_attribute_element_size(mesh,
  1328. subd_mattr,
  1329. ATTR_PRIM_SUBD,
  1330. &attr_float_size,
  1331. &attr_float2_size,
  1332. &attr_float3_size,
  1333. &attr_uchar4_size);
  1334. }
  1335. }
  1336. dscene->attributes_float.alloc(attr_float_size);
  1337. dscene->attributes_float2.alloc(attr_float2_size);
  1338. dscene->attributes_float3.alloc(attr_float3_size);
  1339. dscene->attributes_uchar4.alloc(attr_uchar4_size);
  1340. size_t attr_float_offset = 0;
  1341. size_t attr_float2_offset = 0;
  1342. size_t attr_float3_offset = 0;
  1343. size_t attr_uchar4_offset = 0;
  1344. /* Fill in attributes. */
  1345. for (size_t i = 0; i < scene->meshes.size(); i++) {
  1346. Mesh *mesh = scene->meshes[i];
  1347. AttributeRequestSet &attributes = mesh_attributes[i];
  1348. /* todo: we now store std and name attributes from requests even if
  1349. * they actually refer to the same mesh attributes, optimize */
  1350. foreach (AttributeRequest &req, attributes.requests) {
  1351. Attribute *triangle_mattr = mesh->attributes.find(req);
  1352. Attribute *curve_mattr = mesh->curve_attributes.find(req);
  1353. Attribute *subd_mattr = mesh->subd_attributes.find(req);
  1354. update_attribute_element_offset(mesh,
  1355. dscene->attributes_float,
  1356. attr_float_offset,
  1357. dscene->attributes_float2,
  1358. attr_float2_offset,
  1359. dscene->attributes_float3,
  1360. attr_float3_offset,
  1361. dscene->attributes_uchar4,
  1362. attr_uchar4_offset,
  1363. triangle_mattr,
  1364. ATTR_PRIM_TRIANGLE,
  1365. req.triangle_type,
  1366. req.triangle_desc);
  1367. update_attribute_element_offset(mesh,
  1368. dscene->attributes_float,
  1369. attr_float_offset,
  1370. dscene->attributes_float2,
  1371. attr_float2_offset,
  1372. dscene->attributes_float3,
  1373. attr_float3_offset,
  1374. dscene->attributes_uchar4,
  1375. attr_uchar4_offset,
  1376. curve_mattr,
  1377. ATTR_PRIM_CURVE,
  1378. req.curve_type,
  1379. req.curve_desc);
  1380. update_attribute_element_offset(mesh,
  1381. dscene->attributes_float,
  1382. attr_float_offset,
  1383. dscene->attributes_float2,
  1384. attr_float2_offset,
  1385. dscene->attributes_float3,
  1386. attr_float3_offset,
  1387. dscene->attributes_uchar4,
  1388. attr_uchar4_offset,
  1389. subd_mattr,
  1390. ATTR_PRIM_SUBD,
  1391. req.subd_type,
  1392. req.subd_desc);
  1393. if (progress.get_cancel())
  1394. return;
  1395. }
  1396. }
  1397. /* create attribute lookup maps */
  1398. if (scene->shader_manager->use_osl())
  1399. update_osl_attributes(device, scene, mesh_attributes);
  1400. update_svm_attributes(device, dscene, scene, mesh_attributes);
  1401. if (progress.get_cancel())
  1402. return;
  1403. /* copy to device */
  1404. progress.set_status("Updating Mesh", "Copying Attributes to device");
  1405. if (dscene->attributes_float.size()) {
  1406. dscene->attributes_float.copy_to_device();
  1407. }
  1408. if (dscene->attributes_float2.size()) {
  1409. dscene->attributes_float2.copy_to_device();
  1410. }
  1411. if (dscene->attributes_float3.size()) {
  1412. dscene->attributes_float3.copy_to_device();
  1413. }
  1414. if (dscene->attributes_uchar4.size()) {
  1415. dscene->attributes_uchar4.copy_to_device();
  1416. }
  1417. if (progress.get_cancel())
  1418. return;
  1419. /* After mesh attributes and patch tables have been copied to device memory,
  1420. * we need to update offsets in the objects. */
  1421. scene->object_manager->device_update_mesh_offsets(device, dscene, scene);
  1422. }
  1423. void MeshManager::mesh_calc_offset(Scene *scene)
  1424. {
  1425. size_t vert_size = 0;
  1426. size_t tri_size = 0;
  1427. size_t curve_key_size = 0;
  1428. size_t curve_size = 0;
  1429. size_t patch_size = 0;
  1430. size_t face_size = 0;
  1431. size_t corner_size = 0;
  1432. foreach (Mesh *mesh, scene->meshes) {
  1433. mesh->vert_offset = vert_size;
  1434. mesh->tri_offset = tri_size;
  1435. mesh->curvekey_offset = curve_key_size;
  1436. mesh->curve_offset = curve_size;
  1437. mesh->patch_offset = patch_size;
  1438. mesh->face_offset = face_size;
  1439. mesh->corner_offset = corner_size;
  1440. vert_size += mesh->verts.size();
  1441. tri_size += mesh->num_triangles();
  1442. curve_key_size += mesh->curve_keys.size();
  1443. curve_size += mesh->num_curves();
  1444. if (mesh->subd_faces.size()) {
  1445. Mesh::SubdFace &last = mesh->subd_faces[mesh->subd_faces.size() - 1];
  1446. patch_size += (last.ptex_offset + last.num_ptex_faces()) * 8;
  1447. /* patch tables are stored in same array so include them in patch_size */
  1448. if (mesh->patch_table) {
  1449. mesh->patch_table_offset = patch_size;
  1450. patch_size += mesh->patch_table->total_size();
  1451. }
  1452. }
  1453. face_size += mesh->subd_faces.size();
  1454. corner_size += mesh->subd_face_corners.size();
  1455. }
  1456. }
  1457. void MeshManager::device_update_mesh(
  1458. Device *, DeviceScene *dscene, Scene *scene, bool for_displacement, Progress &progress)
  1459. {
  1460. /* Count. */
  1461. size_t vert_size = 0;
  1462. size_t tri_size = 0;
  1463. size_t curve_key_size = 0;
  1464. size_t curve_size = 0;
  1465. size_t patch_size = 0;
  1466. foreach (Mesh *mesh, scene->meshes) {
  1467. vert_size += mesh->verts.size();
  1468. tri_size += mesh->num_triangles();
  1469. curve_key_size += mesh->curve_keys.size();
  1470. curve_size += mesh->num_curves();
  1471. if (mesh->subd_faces.size()) {
  1472. Mesh::SubdFace &last = mesh->subd_faces[mesh->subd_faces.size() - 1];
  1473. patch_size += (last.ptex_offset + last.num_ptex_faces()) * 8;
  1474. /* patch tables are stored in same array so include them in patch_size */
  1475. if (mesh->patch_table) {
  1476. mesh->patch_table_offset = patch_size;
  1477. patch_size += mesh->patch_table->total_size();
  1478. }
  1479. }
  1480. }
  1481. /* Create mapping from triangle to primitive triangle array. */
  1482. vector<uint> tri_prim_index(tri_size);
  1483. if (for_displacement) {
  1484. /* For displacement kernels we do some trickery to make them believe
  1485. * we've got all required data ready. However, that data is different
  1486. * from final render kernels since we don't have BVH yet, so can't
  1487. * really use same semantic of arrays.
  1488. */
  1489. foreach (Mesh *mesh, scene->meshes) {
  1490. for (size_t i = 0; i < mesh->num_triangles(); ++i) {
  1491. tri_prim_index[i + mesh->tri_offset] = 3 * (i + mesh->tri_offset);
  1492. }
  1493. }
  1494. }
  1495. else {
  1496. for (size_t i = 0; i < dscene->prim_index.size(); ++i) {
  1497. if ((dscene->prim_type[i] & PRIMITIVE_ALL_TRIANGLE) != 0) {
  1498. tri_prim_index[dscene->prim_index[i]] = dscene->prim_tri_index[i];
  1499. }
  1500. }
  1501. }
  1502. /* Fill in all the arrays. */
  1503. if (tri_size != 0) {
  1504. /* normals */
  1505. progress.set_status("Updating Mesh", "Computing normals");
  1506. uint *tri_shader = dscene->tri_shader.alloc(tri_size);
  1507. float4 *vnormal = dscene->tri_vnormal.alloc(vert_size);
  1508. uint4 *tri_vindex = dscene->tri_vindex.alloc(tri_size);
  1509. uint *tri_patch = dscene->tri_patch.alloc(tri_size);
  1510. float2 *tri_patch_uv = dscene->tri_patch_uv.alloc(vert_size);
  1511. foreach (Mesh *mesh, scene->meshes) {
  1512. mesh->pack_shaders(scene, &tri_shader[mesh->tri_offset]);
  1513. mesh->pack_normals(&vnormal[mesh->vert_offset]);
  1514. mesh->pack_verts(tri_prim_index,
  1515. &tri_vindex[mesh->tri_offset],
  1516. &tri_patch[mesh->tri_offset],
  1517. &tri_patch_uv[mesh->vert_offset],
  1518. mesh->vert_offset,
  1519. mesh->tri_offset);
  1520. if (progress.get_cancel())
  1521. return;
  1522. }
  1523. /* vertex coordinates */
  1524. progress.set_status("Updating Mesh", "Copying Mesh to device");
  1525. dscene->tri_shader.copy_to_device();
  1526. dscene->tri_vnormal.copy_to_device();
  1527. dscene->tri_vindex.copy_to_device();
  1528. dscene->tri_patch.copy_to_device();
  1529. dscene->tri_patch_uv.copy_to_device();
  1530. }
  1531. if (curve_size != 0) {
  1532. progress.set_status("Updating Mesh", "Copying Strands to device");
  1533. float4 *curve_keys = dscene->curve_keys.alloc(curve_key_size);
  1534. float4 *curves = dscene->curves.alloc(curve_size);
  1535. foreach (Mesh *mesh, scene->meshes) {
  1536. mesh->pack_curves(scene,
  1537. &curve_keys[mesh->curvekey_offset],
  1538. &curves[mesh->curve_offset],
  1539. mesh->curvekey_offset);
  1540. if (progress.get_cancel())
  1541. return;
  1542. }
  1543. dscene->curve_keys.copy_to_device();
  1544. dscene->curves.copy_to_device();
  1545. }
  1546. if (patch_size != 0) {
  1547. progress.set_status("Updating Mesh", "Copying Patches to device");
  1548. uint *patch_data = dscene->patches.alloc(patch_size);
  1549. foreach (Mesh *mesh, scene->meshes) {
  1550. mesh->pack_patches(&patch_data[mesh->patch_offset],
  1551. mesh->vert_offset,
  1552. mesh->face_offset,
  1553. mesh->corner_offset);
  1554. if (mesh->patch_table) {
  1555. mesh->patch_table->copy_adjusting_offsets(&patch_data[mesh->patch_table_offset],
  1556. mesh->patch_table_offset);
  1557. }
  1558. if (progress.get_cancel())
  1559. return;
  1560. }
  1561. dscene->patches.copy_to_device();
  1562. }
  1563. if (for_displacement) {
  1564. float4 *prim_tri_verts = dscene->prim_tri_verts.alloc(tri_size * 3);
  1565. foreach (Mesh *mesh, scene->meshes) {
  1566. for (size_t i = 0; i < mesh->num_triangles(); ++i) {
  1567. Mesh::Triangle t = mesh->get_triangle(i);
  1568. size_t offset = 3 * (i + mesh->tri_offset);
  1569. prim_tri_verts[offset + 0] = float3_to_float4(mesh->verts[t.v[0]]);
  1570. prim_tri_verts[offset + 1] = float3_to_float4(mesh->verts[t.v[1]]);
  1571. prim_tri_verts[offset + 2] = float3_to_float4(mesh->verts[t.v[2]]);
  1572. }
  1573. }
  1574. dscene->prim_tri_verts.copy_to_device();
  1575. }
  1576. }
  1577. void MeshManager::device_update_bvh(Device *device,
  1578. DeviceScene *dscene,
  1579. Scene *scene,
  1580. Progress &progress)
  1581. {
  1582. /* bvh build */
  1583. progress.set_status("Updating Scene BVH", "Building");
  1584. BVHParams bparams;
  1585. bparams.top_level = true;
  1586. bparams.bvh_layout = BVHParams::best_bvh_layout(scene->params.bvh_layout,
  1587. device->get_bvh_layout_mask());
  1588. bparams.use_spatial_split = scene->params.use_bvh_spatial_split;
  1589. bparams.use_unaligned_nodes = dscene->data.bvh.have_curves &&
  1590. scene->params.use_bvh_unaligned_nodes;
  1591. bparams.num_motion_triangle_steps = scene->params.num_bvh_time_steps;
  1592. bparams.num_motion_curve_steps = scene->params.num_bvh_time_steps;
  1593. bparams.bvh_type = scene->params.bvh_type;
  1594. bparams.curve_flags = dscene->data.curve.curveflags;
  1595. bparams.curve_subdivisions = dscene->data.curve.subdivisions;
  1596. VLOG(1) << "Using " << bvh_layout_name(bparams.bvh_layout) << " layout.";
  1597. #ifdef WITH_EMBREE
  1598. if (bparams.bvh_layout == BVH_LAYOUT_EMBREE) {
  1599. if (dscene->data.bvh.scene) {
  1600. BVHEmbree::destroy(dscene->data.bvh.scene);
  1601. }
  1602. }
  1603. #endif
  1604. BVH *bvh = BVH::create(bparams, scene->objects);
  1605. bvh->build(progress, &device->stats);
  1606. if (progress.get_cancel()) {
  1607. #ifdef WITH_EMBREE
  1608. if (bparams.bvh_layout == BVH_LAYOUT_EMBREE) {
  1609. if (dscene->data.bvh.scene) {
  1610. BVHEmbree::destroy(dscene->data.bvh.scene);
  1611. }
  1612. }
  1613. #endif
  1614. delete bvh;
  1615. return;
  1616. }
  1617. /* copy to device */
  1618. progress.set_status("Updating Scene BVH", "Copying BVH to device");
  1619. PackedBVH &pack = bvh->pack;
  1620. if (pack.nodes.size()) {
  1621. dscene->bvh_nodes.steal_data(pack.nodes);
  1622. dscene->bvh_nodes.copy_to_device();
  1623. }
  1624. if (pack.leaf_nodes.size()) {
  1625. dscene->bvh_leaf_nodes.steal_data(pack.leaf_nodes);
  1626. dscene->bvh_leaf_nodes.copy_to_device();
  1627. }
  1628. if (pack.object_node.size()) {
  1629. dscene->object_node.steal_data(pack.object_node);
  1630. dscene->object_node.copy_to_device();
  1631. }
  1632. if (pack.prim_tri_index.size()) {
  1633. dscene->prim_tri_index.steal_data(pack.prim_tri_index);
  1634. dscene->prim_tri_index.copy_to_device();
  1635. }
  1636. if (pack.prim_tri_verts.size()) {
  1637. dscene->prim_tri_verts.steal_data(pack.prim_tri_verts);
  1638. dscene->prim_tri_verts.copy_to_device();
  1639. }
  1640. if (pack.prim_type.size()) {
  1641. dscene->prim_type.steal_data(pack.prim_type);
  1642. dscene->prim_type.copy_to_device();
  1643. }
  1644. if (pack.prim_visibility.size()) {
  1645. dscene->prim_visibility.steal_data(pack.prim_visibility);
  1646. dscene->prim_visibility.copy_to_device();
  1647. }
  1648. if (pack.prim_index.size()) {
  1649. dscene->prim_index.steal_data(pack.prim_index);
  1650. dscene->prim_index.copy_to_device();
  1651. }
  1652. if (pack.prim_object.size()) {
  1653. dscene->prim_object.steal_data(pack.prim_object);
  1654. dscene->prim_object.copy_to_device();
  1655. }
  1656. if (pack.prim_time.size()) {
  1657. dscene->prim_time.steal_data(pack.prim_time);
  1658. dscene->prim_time.copy_to_device();
  1659. }
  1660. dscene->data.bvh.root = pack.root_index;
  1661. dscene->data.bvh.bvh_layout = bparams.bvh_layout;
  1662. dscene->data.bvh.use_bvh_steps = (scene->params.num_bvh_time_steps != 0);
  1663. #ifdef WITH_EMBREE
  1664. if (bparams.bvh_layout == BVH_LAYOUT_EMBREE) {
  1665. dscene->data.bvh.scene = ((BVHEmbree *)bvh)->scene;
  1666. }
  1667. else {
  1668. dscene->data.bvh.scene = NULL;
  1669. }
  1670. #endif
  1671. delete bvh;
  1672. }
  1673. void MeshManager::device_update_preprocess(Device *device, Scene *scene, Progress &progress)
  1674. {
  1675. if (!need_update && !need_flags_update) {
  1676. return;
  1677. }
  1678. progress.set_status("Updating Meshes Flags");
  1679. /* Update flags. */
  1680. bool volume_images_updated = false;
  1681. foreach (Mesh *mesh, scene->meshes) {
  1682. mesh->has_volume = false;
  1683. foreach (const Shader *shader, mesh->used_shaders) {
  1684. if (shader->has_volume) {
  1685. mesh->has_volume = true;
  1686. }
  1687. if (shader->has_surface_bssrdf) {
  1688. mesh->has_surface_bssrdf = true;
  1689. }
  1690. }
  1691. if (need_update && mesh->has_volume) {
  1692. /* Create volume meshes if there is voxel data. */
  1693. bool has_voxel_attributes = false;
  1694. foreach (Attribute &attr, mesh->attributes.attributes) {
  1695. if (attr.element == ATTR_ELEMENT_VOXEL) {
  1696. has_voxel_attributes = true;
  1697. }
  1698. }
  1699. if (has_voxel_attributes) {
  1700. if (!volume_images_updated) {
  1701. progress.set_status("Updating Meshes Volume Bounds");
  1702. device_update_volume_images(device, scene, progress);
  1703. volume_images_updated = true;
  1704. }
  1705. create_volume_mesh(scene, mesh, progress);
  1706. }
  1707. }
  1708. }
  1709. need_flags_update = false;
  1710. }
  1711. void MeshManager::device_update_displacement_images(Device *device,
  1712. Scene *scene,
  1713. Progress &progress)
  1714. {
  1715. progress.set_status("Updating Displacement Images");
  1716. TaskPool pool;
  1717. ImageManager *image_manager = scene->image_manager;
  1718. set<int> bump_images;
  1719. foreach (Mesh *mesh, scene->meshes) {
  1720. if (mesh->need_update) {
  1721. foreach (Shader *shader, mesh->used_shaders) {
  1722. if (!shader->has_displacement || shader->displacement_method == DISPLACE_BUMP) {
  1723. continue;
  1724. }
  1725. foreach (ShaderNode *node, shader->graph->nodes) {
  1726. if (node->special_type != SHADER_SPECIAL_TYPE_IMAGE_SLOT) {
  1727. continue;
  1728. }
  1729. ImageSlotTextureNode *image_node = static_cast<ImageSlotTextureNode *>(node);
  1730. int slot = image_node->slot;
  1731. if (slot != -1) {
  1732. bump_images.insert(slot);
  1733. }
  1734. }
  1735. }
  1736. }
  1737. }
  1738. foreach (int slot, bump_images) {
  1739. pool.push(function_bind(
  1740. &ImageManager::device_update_slot, image_manager, device, scene, slot, &progress));
  1741. }
  1742. pool.wait_work();
  1743. }
  1744. void MeshManager::device_update_volume_images(Device *device, Scene *scene, Progress &progress)
  1745. {
  1746. progress.set_status("Updating Volume Images");
  1747. TaskPool pool;
  1748. ImageManager *image_manager = scene->image_manager;
  1749. set<int> volume_images;
  1750. foreach (Mesh *mesh, scene->meshes) {
  1751. if (!mesh->need_update) {
  1752. continue;
  1753. }
  1754. foreach (Attribute &attr, mesh->attributes.attributes) {
  1755. if (attr.element != ATTR_ELEMENT_VOXEL) {
  1756. continue;
  1757. }
  1758. VoxelAttribute *voxel = attr.data_voxel();
  1759. if (voxel->slot != -1) {
  1760. volume_images.insert(voxel->slot);
  1761. }
  1762. }
  1763. }
  1764. foreach (int slot, volume_images) {
  1765. pool.push(function_bind(
  1766. &ImageManager::device_update_slot, image_manager, device, scene, slot, &progress));
  1767. }
  1768. pool.wait_work();
  1769. }
  1770. void MeshManager::device_update(Device *device,
  1771. DeviceScene *dscene,
  1772. Scene *scene,
  1773. Progress &progress)
  1774. {
  1775. if (!need_update)
  1776. return;
  1777. VLOG(1) << "Total " << scene->meshes.size() << " meshes.";
  1778. bool true_displacement_used = false;
  1779. size_t total_tess_needed = 0;
  1780. foreach (Mesh *mesh, scene->meshes) {
  1781. foreach (Shader *shader, mesh->used_shaders) {
  1782. if (shader->need_update_mesh)
  1783. mesh->need_update = true;
  1784. }
  1785. if (mesh->need_update) {
  1786. /* Update normals. */
  1787. mesh->add_face_normals();
  1788. mesh->add_vertex_normals();
  1789. if (mesh->need_attribute(scene, ATTR_STD_POSITION_UNDISPLACED)) {
  1790. mesh->add_undisplaced();
  1791. }
  1792. /* Test if we need tessellation. */
  1793. if (mesh->subdivision_type != Mesh::SUBDIVISION_NONE && mesh->num_subd_verts == 0 &&
  1794. mesh->subd_params) {
  1795. total_tess_needed++;
  1796. }
  1797. /* Test if we need displacement. */
  1798. if (mesh->has_true_displacement()) {
  1799. true_displacement_used = true;
  1800. }
  1801. if (progress.get_cancel())
  1802. return;
  1803. }
  1804. }
  1805. /* Tessellate meshes that are using subdivision */
  1806. if (total_tess_needed) {
  1807. Camera *dicing_camera = scene->dicing_camera;
  1808. dicing_camera->update(scene);
  1809. size_t i = 0;
  1810. foreach (Mesh *mesh, scene->meshes) {
  1811. if (mesh->need_update && mesh->subdivision_type != Mesh::SUBDIVISION_NONE &&
  1812. mesh->num_subd_verts == 0 && mesh->subd_params) {
  1813. string msg = "Tessellating ";
  1814. if (mesh->name == "")
  1815. msg += string_printf("%u/%u", (uint)(i + 1), (uint)total_tess_needed);
  1816. else
  1817. msg += string_printf(
  1818. "%s %u/%u", mesh->name.c_str(), (uint)(i + 1), (uint)total_tess_needed);
  1819. progress.set_status("Updating Mesh", msg);
  1820. mesh->subd_params->camera = dicing_camera;
  1821. DiagSplit dsplit(*mesh->subd_params);
  1822. mesh->tessellate(&dsplit);
  1823. i++;
  1824. if (progress.get_cancel())
  1825. return;
  1826. }
  1827. }
  1828. }
  1829. /* Update images needed for true displacement. */
  1830. bool old_need_object_flags_update = false;
  1831. if (true_displacement_used) {
  1832. VLOG(1) << "Updating images used for true displacement.";
  1833. device_update_displacement_images(device, scene, progress);
  1834. old_need_object_flags_update = scene->object_manager->need_flags_update;
  1835. scene->object_manager->device_update_flags(device, dscene, scene, progress, false);
  1836. }
  1837. /* Device update. */
  1838. device_free(device, dscene);
  1839. mesh_calc_offset(scene);
  1840. if (true_displacement_used) {
  1841. device_update_mesh(device, dscene, scene, true, progress);
  1842. }
  1843. if (progress.get_cancel())
  1844. return;
  1845. device_update_attributes(device, dscene, scene, progress);
  1846. if (progress.get_cancel())
  1847. return;
  1848. /* Update displacement. */
  1849. bool displacement_done = false;
  1850. size_t num_bvh = 0;
  1851. foreach (Mesh *mesh, scene->meshes) {
  1852. if (mesh->need_update) {
  1853. if (displace(device, dscene, scene, mesh, progress)) {
  1854. displacement_done = true;
  1855. }
  1856. if (mesh->need_build_bvh()) {
  1857. num_bvh++;
  1858. }
  1859. }
  1860. if (progress.get_cancel())
  1861. return;
  1862. }
  1863. /* Device re-update after displacement. */
  1864. if (displacement_done) {
  1865. device_free(device, dscene);
  1866. device_update_attributes(device, dscene, scene, progress);
  1867. if (progress.get_cancel())
  1868. return;
  1869. }
  1870. TaskPool pool;
  1871. size_t i = 0;
  1872. foreach (Mesh *mesh, scene->meshes) {
  1873. if (mesh->need_update) {
  1874. pool.push(function_bind(
  1875. &Mesh::compute_bvh, mesh, device, dscene, &scene->params, &progress, i, num_bvh));
  1876. if (mesh->need_build_bvh()) {
  1877. i++;
  1878. }
  1879. }
  1880. }
  1881. TaskPool::Summary summary;
  1882. pool.wait_work(&summary);
  1883. VLOG(2) << "Objects BVH build pool statistics:\n" << summary.full_report();
  1884. foreach (Shader *shader, scene->shaders) {
  1885. shader->need_update_mesh = false;
  1886. }
  1887. Scene::MotionType need_motion = scene->need_motion();
  1888. bool motion_blur = need_motion == Scene::MOTION_BLUR;
  1889. /* Update objects. */
  1890. vector<Object *> volume_objects;
  1891. foreach (Object *object, scene->objects) {
  1892. object->compute_bounds(motion_blur);
  1893. }
  1894. if (progress.get_cancel())
  1895. return;
  1896. device_update_bvh(device, dscene, scene, progress);
  1897. if (progress.get_cancel())
  1898. return;
  1899. device_update_mesh(device, dscene, scene, false, progress);
  1900. if (progress.get_cancel())
  1901. return;
  1902. need_update = false;
  1903. if (true_displacement_used) {
  1904. /* Re-tag flags for update, so they're re-evaluated
  1905. * for meshes with correct bounding boxes.
  1906. *
  1907. * This wouldn't cause wrong results, just true
  1908. * displacement might be less optimal ot calculate.
  1909. */
  1910. scene->object_manager->need_flags_update = old_need_object_flags_update;
  1911. }
  1912. }
  1913. void MeshManager::device_free(Device *device, DeviceScene *dscene)
  1914. {
  1915. dscene->bvh_nodes.free();
  1916. dscene->bvh_leaf_nodes.free();
  1917. dscene->object_node.free();
  1918. dscene->prim_tri_verts.free();
  1919. dscene->prim_tri_index.free();
  1920. dscene->prim_type.free();
  1921. dscene->prim_visibility.free();
  1922. dscene->prim_index.free();
  1923. dscene->prim_object.free();
  1924. dscene->prim_time.free();
  1925. dscene->tri_shader.free();
  1926. dscene->tri_vnormal.free();
  1927. dscene->tri_vindex.free();
  1928. dscene->tri_patch.free();
  1929. dscene->tri_patch_uv.free();
  1930. dscene->curves.free();
  1931. dscene->curve_keys.free();
  1932. dscene->patches.free();
  1933. dscene->attributes_map.free();
  1934. dscene->attributes_float.free();
  1935. dscene->attributes_float2.free();
  1936. dscene->attributes_float3.free();
  1937. dscene->attributes_uchar4.free();
  1938. /* Signal for shaders like displacement not to do ray tracing. */
  1939. dscene->data.bvh.bvh_layout = BVH_LAYOUT_NONE;
  1940. #ifdef WITH_OSL
  1941. OSLGlobals *og = (OSLGlobals *)device->osl_memory();
  1942. if (og) {
  1943. og->object_name_map.clear();
  1944. og->attribute_map.clear();
  1945. og->object_names.clear();
  1946. }
  1947. #else
  1948. (void)device;
  1949. #endif
  1950. }
  1951. void MeshManager::tag_update(Scene *scene)
  1952. {
  1953. need_update = true;
  1954. scene->object_manager->need_update = true;
  1955. }
  1956. void MeshManager::collect_statistics(const Scene *scene, RenderStats *stats)
  1957. {
  1958. foreach (Mesh *mesh, scene->meshes) {
  1959. stats->mesh.geometry.add_entry(
  1960. NamedSizeEntry(string(mesh->name.c_str()), mesh->get_total_size_in_bytes()));
  1961. }
  1962. }
  1963. bool Mesh::need_attribute(Scene *scene, AttributeStandard std)
  1964. {
  1965. if (std == ATTR_STD_NONE)
  1966. return false;
  1967. if (scene->need_global_attribute(std))
  1968. return true;
  1969. foreach (Shader *shader, used_shaders)
  1970. if (shader->attributes.find(std))
  1971. return true;
  1972. return false;
  1973. }
  1974. bool Mesh::need_attribute(Scene * /*scene*/, ustring name)
  1975. {
  1976. if (name == ustring())
  1977. return false;
  1978. foreach (Shader *shader, used_shaders)
  1979. if (shader->attributes.find(name))
  1980. return true;
  1981. return false;
  1982. }
  1983. CCL_NAMESPACE_END