1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
|
// Copyright 2009-2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include "user_geometry_device.isph"
RTCScene g_scene = NULL;
uniform TutorialData data;
const uniform int numPhi = 5;
const uniform int numTheta = 2*numPhi;
void renderTileStandardStream(uniform int taskIndex,
uniform int threadIndex,
uniform int* uniform pixels,
const uniform unsigned int width,
const uniform unsigned int height,
const uniform float time,
const uniform ISPCCamera& camera,
const uniform int numTilesX,
const uniform int numTilesY);
/*
* Safely invalidate a packet of rays.
*/
inline void invalidateRay(Ray& ray)
{
// Initialize the whole ray so that it is invalid. This is important because
// in streamed mode, active state of lanes is forgotten between ray
// generation and traversal.
unmasked {
ray.org = make_Vec3f_(0.f);
ray.dir = make_Vec3f_(0.f);
ray.tnear = pos_inf;
ray.tfar = neg_inf;
}
}
inline void pushInstanceId(uniform RTCIntersectContext* uniform ctx, uniform unsigned int id)
{
#if RTC_MAX_INSTANCE_LEVEL_COUNT > 1
ctx->instID[ctx->instStackSize++] = id;
#else
ctx->instID[0] = id;
#endif
}
inline void popInstanceId(uniform RTCIntersectContext* uniform ctx)
{
#if RTC_MAX_INSTANCE_LEVEL_COUNT > 1
ctx->instID[--ctx->instStackSize] = RTC_INVALID_GEOMETRY_ID;
#else
ctx->instID[0] = RTC_INVALID_GEOMETRY_ID;
#endif
}
inline void copyInstanceIdStack(uniform const RTCIntersectContext* uniform ctx, varying unsigned* uniform tgt)
{
tgt[0] = ctx->instID[0];
#if (RTC_MAX_INSTANCE_LEVEL_COUNT > 1)
for (unsigned l = 1; l < RTC_MAX_INSTANCE_LEVEL_COUNT && l < ctx->instStackSize; ++l)
tgt[l] = ctx->instID[l];
#endif
}
// ======================================================================== //
// User defined instancing //
// ======================================================================== //
unmasked void instanceBoundsFunc(const struct RTCBoundsFunctionArguments* uniform args)
{
const uniform Instance* uniform instance = (const uniform Instance* uniform) args->geometryUserPtr;
uniform RTCBounds* uniform bounds_o = args->bounds_o;
uniform Vec3f l = instance->lower;
uniform Vec3f u = instance->upper;
uniform Vec3f p000 = xfmPoint(instance->local2world,make_Vec3f(l.x,l.y,l.z));
uniform Vec3f p001 = xfmPoint(instance->local2world,make_Vec3f(l.x,l.y,u.z));
uniform Vec3f p010 = xfmPoint(instance->local2world,make_Vec3f(l.x,u.y,l.z));
uniform Vec3f p011 = xfmPoint(instance->local2world,make_Vec3f(l.x,u.y,u.z));
uniform Vec3f p100 = xfmPoint(instance->local2world,make_Vec3f(u.x,l.y,l.z));
uniform Vec3f p101 = xfmPoint(instance->local2world,make_Vec3f(u.x,l.y,u.z));
uniform Vec3f p110 = xfmPoint(instance->local2world,make_Vec3f(u.x,u.y,l.z));
uniform Vec3f p111 = xfmPoint(instance->local2world,make_Vec3f(u.x,u.y,u.z));
uniform Vec3f lower = min(min(min(p000,p001),min(p010,p011)),min(min(p100,p101),min(p110,p111)));
uniform Vec3f upper = max(max(max(p000,p001),max(p010,p011)),max(max(p100,p101),max(p110,p111)));
bounds_o->lower_x = lower.x;
bounds_o->lower_y = lower.y;
bounds_o->lower_z = lower.z;
bounds_o->upper_x = upper.x;
bounds_o->upper_y = upper.y;
bounds_o->upper_z = upper.z;
}
unmasked void instanceIntersectFunc(const RTCIntersectFunctionNArguments* uniform args)
{
const uniform int* uniform valid = args->valid;
void* uniform ptr = args->geometryUserPtr;
uniform RTCIntersectContext* uniform context = args->context;
RTCRayHitN* uniform rays = (RTCRayHitN* uniform)args->rayhit;
assert(args->N == programCount);
if (!valid[programIndex])
return;
varying Ray *uniform ray = (varying Ray*uniform)rays;
const uniform Instance* uniform instance = (const uniform Instance* uniform)ptr;
const Vec3f_ ray_org = ray->org;
const Vec3f_ ray_dir = ray->dir;
const float ray_tnear = ray->tnear;
const float ray_tfar = ray->tfar;
ray->org = make_Vec3f_(xfmPoint (instance->world2local,ray_org));
ray->dir = make_Vec3f_(xfmVector(instance->world2local,ray_dir));
ray->tnear = ray_tnear;
ray->tfar = ray_tfar;
pushInstanceId(context, instance->userID);
rtcIntersectV(instance->object,context,RTCRayHit_(*ray));
popInstanceId(context);
const float updated_tfar = ray->tfar;
ray->org = ray_org;
ray->dir = ray_dir;
ray->tfar = updated_tfar;
}
unmasked void instanceOccludedFunc(const RTCOccludedFunctionNArguments* uniform args)
{
const uniform int* uniform valid = args->valid;
void* uniform ptr = args->geometryUserPtr;
uniform RTCIntersectContext* uniform context = args->context;
RTCRayHitN* uniform rays = (RTCRayHitN* uniform)args->ray;
assert(args->N == programCount);
if (!valid[programIndex])
return;
varying Ray *uniform ray = (varying Ray*uniform)rays;
const uniform Instance* uniform instance = (const uniform Instance* uniform)ptr;
const Vec3f_ ray_org = ray->org;
const Vec3f_ ray_dir = ray->dir;
const float ray_tnear = ray->tnear;
const float ray_tfar = ray->tfar;
ray->org = make_Vec3f_(xfmPoint (instance->world2local,ray_org));
ray->dir = make_Vec3f_(xfmVector(instance->world2local,ray_dir));
ray->tnear = ray_tnear;
ray->tfar = ray_tfar;
pushInstanceId(context, instance->userID);
rtcOccludedV(instance->object,context,RTCRay_(*ray));
popInstanceId(context);
const float updated_tfar = ray->tfar;
ray->org = ray_org;
ray->dir = ray_dir;
ray->tnear = ray_tnear;
ray->tfar = updated_tfar;
}
unmasked void instanceIntersectFuncN(const RTCIntersectFunctionNArguments* uniform args)
{
/* avoid crashing when debug visualizations are used */
if (args->context == NULL)
return;
const uniform int* uniform valid = args->valid;
void* uniform ptr = args->geometryUserPtr;
uniform RTCIntersectContext* uniform context = args->context;
uniform unsigned int N = args->N;
RTCRayHitN* uniform rayhit = (RTCRayHitN* uniform)args->rayhit;
RTCRayN* uniform rays = RTCRayHitN_RayN(rayhit,N);
RTCHitN* uniform hits = RTCRayHitN_HitN(rayhit,N);
const uniform Instance* uniform instance = (const uniform Instance* uniform) ptr;
/* iterate over all rays in ray packet */
for (uniform unsigned int ui=0; ui<N; ui+=programCount)
{
/* calculate varying loop and execution mask */
unsigned int vi = ui+programIndex;
if (vi>=N) continue;
/* ignore inactive rays */
if (valid[vi] != -1) continue;
/* create transformed ray */
Ray ray;
invalidateRay(ray);
const Vec3f ray_org = make_Vec3f(RTCRayN_org_x(rays,N,ui),RTCRayN_org_y(rays,N,ui),RTCRayN_org_z(rays,N,ui));
const Vec3f ray_dir = make_Vec3f(RTCRayN_dir_x(rays,N,ui),RTCRayN_dir_y(rays,N,ui),RTCRayN_dir_z(rays,N,ui));
ray.org = make_Vec3f_(xfmPoint (instance->world2local,ray_org));
ray.dir = make_Vec3f_(xfmVector(instance->world2local,ray_dir));
ray.tnear = RTCRayN_tnear(rays,N,ui);
ray.tfar = RTCRayN_tfar(rays,N,ui);
ray.time = RTCRayN_time(rays,N,ui);
ray.mask = RTCRayN_mask(rays,N,ui);
ray.geomID = RTC_INVALID_GEOMETRY_ID;
/* trace ray through object */
pushInstanceId(context, instance->userID);
rtcIntersectV(instance->object,context,RTCRayHit_(ray));
popInstanceId(context);
if (ray.geomID == RTC_INVALID_GEOMETRY_ID) continue;
/* update hit */
RTCRayN_tfar(rays,N,ui) = ray.tfar;
rtcCopyHitToHitN(hits,RTCHit_(ray),N,ui);
}
}
unmasked void instanceOccludedFuncN(const RTCOccludedFunctionNArguments* uniform args)
{
/* avoid crashing when debug visualizations are used */
if (args->context == NULL)
return;
const uniform int* uniform valid = args->valid;
void* uniform ptr = args->geometryUserPtr;
uniform RTCIntersectContext* uniform context = args->context;
RTCRayN* uniform rays = (RTCRayN* uniform)args->ray;
uniform unsigned int N = args->N;
const uniform Instance* uniform instance = (const uniform Instance* uniform) ptr;
/* iterate over all rays in ray packet */
for (uniform unsigned int ui=0; ui<N; ui+=programCount)
{
/* calculate varying loop and execution mask */
unsigned int vi = ui+programIndex;
if (vi>=N) continue;
/* ignore inactive rays */
if (valid[vi] != -1) continue;
/* create transformed ray */
Ray ray;
invalidateRay(ray);
const Vec3f ray_org = make_Vec3f(RTCRayN_org_x(rays,N,ui),RTCRayN_org_y(rays,N,ui),RTCRayN_org_z(rays,N,ui));
const Vec3f ray_dir = make_Vec3f(RTCRayN_dir_x(rays,N,ui),RTCRayN_dir_y(rays,N,ui),RTCRayN_dir_z(rays,N,ui));
ray.org = make_Vec3f_(xfmPoint (instance->world2local,ray_org));
ray.dir = make_Vec3f_(xfmVector(instance->world2local,ray_dir));
ray.tnear = RTCRayN_tnear(rays,N,ui);
ray.tfar = RTCRayN_tfar(rays,N,ui);
ray.time = RTCRayN_time(rays,N,ui);
ray.mask = RTCRayN_mask(rays,N,ui);
ray.geomID = RTC_INVALID_GEOMETRY_ID;
/* trace ray through object */
pushInstanceId(context, instance->userID);
rtcOccludedV(instance->object,context,RTCRay_(ray));
popInstanceId(context);
if (ray.tfar >= 0.0f) continue;
/* update hit */
RTCRayN_tfar(rays,N,ui) = ray.tfar;
}
}
uniform Instance* uniform createInstance (RTCScene scene, RTCScene object, uniform int userID, const uniform Vec3f& lower, const uniform Vec3f& upper)
{
uniform Instance* uniform instance = uniform new uniform Instance;
instance->object = object;
instance->userID = userID;
instance->lower = lower;
instance->upper = upper;
instance->local2world.l.vx = make_Vec3f(1,0,0);
instance->local2world.l.vy = make_Vec3f(0,1,0);
instance->local2world.l.vz = make_Vec3f(0,0,1);
instance->local2world.p = make_Vec3f(0,0,0);
instance->geometry = rtcNewGeometry(g_device, RTC_GEOMETRY_TYPE_USER);
rtcSetGeometryUserPrimitiveCount(instance->geometry,1);
rtcSetGeometryUserData(instance->geometry,instance);
rtcSetGeometryBoundsFunction(instance->geometry,instanceBoundsFunc,NULL);
if (g_mode == MODE_NORMAL && nativePacketSupported(g_device))
{
rtcSetGeometryIntersectFunction(instance->geometry,instanceIntersectFunc);
rtcSetGeometryOccludedFunction (instance->geometry,instanceOccludedFunc);
}
else
{
rtcSetGeometryIntersectFunction(instance->geometry,instanceIntersectFuncN);
rtcSetGeometryOccludedFunction (instance->geometry,instanceOccludedFuncN);
}
rtcCommitGeometry(instance->geometry);
rtcAttachGeometry(scene,instance->geometry);
rtcReleaseGeometry(instance->geometry);
return instance;
}
void updateInstance (RTCScene scene, uniform Instance* uniform instance)
{
instance->world2local = rcp(instance->local2world);
instance->normal2world = transposed(rcp(instance->local2world.l));
rtcCommitGeometry(instance->geometry);
}
// ======================================================================== //
// User defined sphere geometry //
// ======================================================================== //
unmasked void sphereBoundsFunc(const struct RTCBoundsFunctionArguments* uniform args)
{
const uniform Sphere* uniform spheres = (const uniform Sphere* uniform) args->geometryUserPtr;
uniform RTCBounds* uniform bounds_o = args->bounds_o;
const uniform Sphere& sphere = spheres[args->primID];
bounds_o->lower_x = sphere.p.x-sphere.r;
bounds_o->lower_y = sphere.p.y-sphere.r;
bounds_o->lower_z = sphere.p.z-sphere.r;
bounds_o->upper_x = sphere.p.x+sphere.r;
bounds_o->upper_y = sphere.p.y+sphere.r;
bounds_o->upper_z = sphere.p.z+sphere.r;
}
unmasked void sphereIntersectFunc(const RTCIntersectFunctionNArguments* uniform args)
{
uniform int* uniform valid = args->valid;
void* uniform ptr = args->geometryUserPtr;
varying Ray *uniform ray = (varying Ray*uniform)args->rayhit;
varying RTCHit* uniform hit = (varying RTCHit *uniform)&ray->Ng.x;
uniform unsigned int primID = args->primID;
assert(args->N == programCount);
const uniform Sphere* uniform spheres = (const uniform Sphere* uniform)ptr;
const uniform Sphere& sphere = spheres[primID];
if (!valid[programIndex]) return;
const Vec3f v = ray->org-sphere.p;
const float A = dot(ray->dir,ray->dir);
const float B = 2.0f*dot(v,ray->dir);
const float C = dot(v,v) - sqr(sphere.r);
const float D = B*B - 4.0f*A*C;
if (D < 0.0f) return;
const float Q = sqrt(D);
const float rcpA = rcp(A);
const float t0 = 0.5f*rcpA*(-B-Q);
const float t1 = 0.5f*rcpA*(-B+Q);
varying RTCHit potentialHit;
potentialHit.u = 0.0f;
potentialHit.v = 0.0f;
copyInstanceIdStack(args->context, potentialHit.instID);
potentialHit.geomID = sphere.geomID;
potentialHit.primID = primID;
if ((ray->tnear < t0) & (t0 < ray->tfar))
{
varying int imask;
varying bool mask = __mask;
unmasked {
imask = mask ? -1 : 0;
}
const Vec3f Ng = ray->org+t0*ray->dir-sphere.p;
potentialHit.Ng_x = Ng.x;
potentialHit.Ng_y = Ng.y;
potentialHit.Ng_z = Ng.z;
uniform RTCFilterFunctionNArguments fargs;
fargs.valid = (int* uniform)&imask;
fargs.geometryUserPtr = ptr;
fargs.context = args->context;
fargs.ray = (RTCRayN *uniform)args->rayhit;
fargs.hit = (RTCHitN* uniform)&potentialHit;
fargs.N = programCount;
const float old_t = ray->tfar;
ray->tfar = t0;
rtcFilterIntersection(args,&fargs);
if (imask == -1)
*hit = potentialHit;
else
ray->tfar = old_t;
}
if ((ray->tnear < t1) & (t1 < ray->tfar))
{
varying int imask;
varying bool mask = __mask;
unmasked {
imask = mask ? -1 : 0;
}
const Vec3f Ng = ray->org+t1*ray->dir-sphere.p;
potentialHit.Ng_x = Ng.x;
potentialHit.Ng_y = Ng.y;
potentialHit.Ng_z = Ng.z;
uniform RTCFilterFunctionNArguments fargs;
fargs.valid = (int* uniform)&imask;
fargs.geometryUserPtr = ptr;
fargs.context = args->context;
fargs.ray = (RTCRayN *uniform)args->rayhit;
fargs.hit = (RTCHitN* uniform)&potentialHit;
fargs.N = programCount;
const float old_t = ray->tfar;
ray->tfar = t1;
rtcFilterIntersection(args,&fargs);
if (imask == -1)
*hit = potentialHit;
else
ray->tfar = old_t;
}
}
unmasked void sphereOccludedFunc(const RTCOccludedFunctionNArguments* uniform args)
{
uniform int* uniform valid = args->valid;
void* uniform ptr = args->geometryUserPtr;
varying Ray *uniform ray = (varying Ray*uniform)args->ray;
uniform unsigned int primID = args->primID;
assert(args->N == programCount);
const uniform Sphere* uniform spheres = (const uniform Sphere* uniform) ptr;
const uniform Sphere& sphere = spheres[primID];
if (!valid[programIndex])
return;
const Vec3f v = ray->org-sphere.p;
const float A = dot(ray->dir,ray->dir);
const float B = 2.0f*dot(v,ray->dir);
const float C = dot(v,v) - sqr(sphere.r);
const float D = B*B - 4.0f*A*C;
if (D < 0.0f) return;
const float Q = sqrt(D);
const float rcpA = rcp(A);
const float t0 = 0.5f*rcpA*(-B-Q);
const float t1 = 0.5f*rcpA*(-B+Q);
varying RTCHit potentialHit;
potentialHit.u = 0.0f;
potentialHit.v = 0.0f;
copyInstanceIdStack(args->context, potentialHit.instID);
potentialHit.geomID = sphere.geomID;
potentialHit.primID = primID;
if ((ray->tnear < t0) & (t0 < ray->tfar))
{
varying int imask;
varying bool mask = __mask;
unmasked {
imask = mask ? -1 : 0;
}
const Vec3f Ng = ray->org+t0*ray->dir-sphere.p;
potentialHit.Ng_x = Ng.x;
potentialHit.Ng_y = Ng.y;
potentialHit.Ng_z = Ng.z;
uniform RTCFilterFunctionNArguments fargs;
fargs.valid = (int* uniform)&imask;
fargs.geometryUserPtr = ptr;
fargs.context = args->context;
fargs.ray = args->ray;
fargs.hit = (RTCHitN* uniform)&potentialHit;
fargs.N = programCount;
const float old_t = ray->tfar;
ray->tfar = t0;
rtcFilterOcclusion(args,&fargs);
if (imask == -1)
ray->tfar = neg_inf;
else
ray->tfar = old_t;
}
if ((ray->tnear < t1) & (t1 < ray->tfar))
{
varying int imask;
varying bool mask = __mask;
unmasked {
imask = mask ? -1 : 0;
}
const Vec3f Ng = ray->org+t1*ray->dir-sphere.p;
potentialHit.Ng_x = Ng.x;
potentialHit.Ng_y = Ng.y;
potentialHit.Ng_z = Ng.z;
uniform RTCFilterFunctionNArguments fargs;
fargs.valid = (int* uniform)&imask;
fargs.geometryUserPtr = ptr;
fargs.context = args->context;
fargs.ray = args->ray;
fargs.hit = (RTCHitN* uniform)&potentialHit;
fargs.N = programCount;
const float old_t = ray->tfar;
ray->tfar = t1;
rtcFilterOcclusion(args,&fargs);
if (imask == -1)
ray->tfar = neg_inf;
else
ray->tfar = old_t;
}
}
unmasked void sphereIntersectFuncN(const RTCIntersectFunctionNArguments* uniform args)
{
uniform int* uniform valid = (uniform int*) args->valid;
void* uniform ptr = args->geometryUserPtr;
uniform unsigned int N = args->N;
RTCRayHitN* uniform rayhit = (RTCRayHitN* uniform)args->rayhit;
RTCRayN* uniform rays = RTCRayHitN_RayN(rayhit,N);
RTCHitN* uniform hits = RTCRayHitN_HitN(rayhit,N);
uniform unsigned int primID = args->primID;
const uniform Sphere* uniform spheres = (const uniform Sphere* uniform) ptr;
/* iterate over all rays in ray packet */
for (uniform unsigned int ui=0; ui<N; ui+=programCount)
{
/* calculate varying loop and execution mask */
unsigned int vi = ui+programIndex;
if (vi>=N) continue;
/* ignore inactive rays */
if (valid[vi] != -1) continue;
const Vec3f ray_org = make_Vec3f(RTCRayN_org_x(rays,N,ui),RTCRayN_org_y(rays,N,ui),RTCRayN_org_z(rays,N,ui));
const Vec3f ray_dir = make_Vec3f(RTCRayN_dir_x(rays,N,ui),RTCRayN_dir_y(rays,N,ui),RTCRayN_dir_z(rays,N,ui));
float& ray_tnear = RTCRayN_tnear(rays,N,ui);
float& ray_tfar = RTCRayN_tfar(rays,N,ui);
const uniform Sphere& sphere = spheres[primID];
const Vec3f v = ray_org-sphere.p;
const float A = dot(ray_dir,ray_dir);
const float B = 2.0f*dot(v,ray_dir);
const float C = dot(v,v) - sqr(sphere.r);
const float D = B*B - 4.0f*A*C;
if (D < 0.0f) continue;
const float Q = sqrt(D);
const float rcpA = rcp(A);
const float t0 = 0.5f*rcpA*(-B-Q);
const float t1 = 0.5f*rcpA*(-B+Q);
RTCRayHit rtc_ray = rtcGetRayHitFromRayHitN(rayhit,N,ui);
varying Ray *uniform ray = (varying Ray*uniform)&rtc_ray;
RTCHit potentialhit;
potentialhit.u = 0.0f;
potentialhit.v = 0.0f;
copyInstanceIdStack(args->context, potentialhit.instID);
potentialhit.geomID = sphere.geomID;
potentialhit.primID = primID;
if ((ray_tnear < t0) & (t0 < ray_tfar))
{
varying int imask;
varying bool mask = __mask;
unmasked {
imask = mask ? -1 : 0;
}
const Vec3f Ng = ray->org+t0*ray->dir-sphere.p;
potentialhit.Ng_x = Ng.x;
potentialhit.Ng_y = Ng.y;
potentialhit.Ng_z = Ng.z;
uniform RTCFilterFunctionNArguments fargs;
fargs.valid = (int* uniform)&imask;
fargs.geometryUserPtr = ptr;
fargs.context = args->context;
fargs.ray = (RTCRayN* uniform)ray;
fargs.hit = (RTCHitN* uniform)&potentialhit;
fargs.N = programCount;
ray->tfar = t0;
rtcFilterIntersection(args,&fargs);
if (imask == -1) {
ray_tfar = t0;
rtcCopyHitToHitN(hits,&potentialhit,N,ui);
}
}
if ((ray_tnear < t1) & (t1 < ray_tfar))
{
varying int imask;
varying bool mask = __mask;
unmasked {
imask = mask ? -1 : 0;
}
const Vec3f Ng = ray->org+t1*ray->dir-sphere.p;
potentialhit.Ng_x = Ng.x;
potentialhit.Ng_y = Ng.y;
potentialhit.Ng_z = Ng.z;
uniform RTCFilterFunctionNArguments fargs;
fargs.valid = (int* uniform)&imask;
fargs.geometryUserPtr = ptr;
fargs.context = args->context;
fargs.ray = (RTCRayN* uniform)ray;
fargs.hit = (RTCHitN* uniform)&potentialhit;
fargs.N = programCount;
ray->tfar = t1;
rtcFilterIntersection(args,&fargs);
if (imask == -1) {
ray_tfar = t1;
rtcCopyHitToHitN(hits,&potentialhit,N,ui);
}
}
}
}
unmasked void sphereOccludedFuncN(const RTCOccludedFunctionNArguments* uniform args)
{
uniform int* uniform valid = args->valid;
void* uniform ptr = args->geometryUserPtr;
RTCRayN* uniform rays = (RTCRayN* uniform)args->ray;
uniform unsigned int N = args->N;
uniform unsigned int primID = args->primID;
const uniform Sphere* uniform spheres = (const uniform Sphere* uniform) ptr;
/* iterate over all rays in ray packet */
for (uniform unsigned int ui=0; ui<N; ui+=programCount)
{
/* calculate varying loop and execution mask */
unsigned int vi = ui+programIndex;
if (vi>=N) continue;
/* ignore inactive rays */
if (valid[vi] != -1) continue;
const Vec3f ray_org = make_Vec3f(RTCRayN_org_x(rays,N,ui),RTCRayN_org_y(rays,N,ui),RTCRayN_org_z(rays,N,ui));
const Vec3f ray_dir = make_Vec3f(RTCRayN_dir_x(rays,N,ui),RTCRayN_dir_y(rays,N,ui),RTCRayN_dir_z(rays,N,ui));
float& ray_tnear = RTCRayN_tnear(rays,N,ui);
float& ray_tfar = RTCRayN_tfar(rays,N,ui);
const uniform Sphere& sphere = spheres[primID];
const Vec3f v = ray_org-sphere.p;
const float A = dot(ray_dir,ray_dir);
const float B = 2.0f*dot(v,ray_dir);
const float C = dot(v,v) - sqr(sphere.r);
const float D = B*B - 4.0f*A*C;
if (D < 0.0f) continue;
const float Q = sqrt(D);
const float rcpA = rcp(A);
const float t0 = 0.5f*rcpA*(-B-Q);
const float t1 = 0.5f*rcpA*(-B+Q);
RTCRay rtc_ray = rtcGetRayFromRayN(rays,N,ui);
varying Ray *uniform ray = (varying Ray*uniform)&rtc_ray;
RTCHit potentialhit;
potentialhit.u = 0.0f;
potentialhit.v = 0.0f;
copyInstanceIdStack(args->context, potentialhit.instID);
potentialhit.geomID = sphere.geomID;
potentialhit.primID = primID;
if ((ray_tnear < t0) & (t0 < ray_tfar))
{
varying int imask;
varying bool mask = __mask;
unmasked {
imask = mask ? -1 : 0;
}
const Vec3f Ng = ray->org+t0*ray->dir-sphere.p;
potentialhit.Ng_x = Ng.x;
potentialhit.Ng_y = Ng.y;
potentialhit.Ng_z = Ng.z;
uniform RTCFilterFunctionNArguments fargs;
fargs.valid = (int* uniform)&imask;
fargs.geometryUserPtr = ptr;
fargs.context = args->context;
fargs.ray = (RTCRayN* uniform)ray;
fargs.hit = (RTCHitN* uniform)&potentialhit;
fargs.N = programCount;
ray->tfar = t0;
rtcFilterOcclusion(args,&fargs);
if (imask == -1)
RTCRayN_tfar(rays,N,ui) = neg_inf;
}
/* ignore rays that have just found a hit */
if (RTCRayN_tfar(rays,N,ui) < 0.0f)
continue;
if ((ray_tnear < t1) & (t1 < ray_tfar))
{
varying int imask;
varying bool mask = __mask;
unmasked {
imask = mask ? -1 : 0;
}
const Vec3f Ng = ray_org+t1*ray_dir-sphere.p;
potentialhit.Ng_x = Ng.x;
potentialhit.Ng_y = Ng.y;
potentialhit.Ng_z = Ng.z;
uniform RTCFilterFunctionNArguments fargs;
fargs.valid = (int* uniform)&imask;
fargs.geometryUserPtr = ptr;
fargs.context = args->context;
fargs.ray = (RTCRayN* uniform)ray;
fargs.hit = (RTCHitN* uniform)&potentialhit;
fargs.N = programCount;
ray->tfar = t1;
rtcFilterOcclusion(args,&fargs);
if (imask == -1)
RTCRayN_tfar(rays,N,ui) = neg_inf;
}
}
}
/* intersection filter function */
unmasked void sphereFilterFunction(const RTCFilterFunctionNArguments* uniform args)
{
uniform int* uniform valid = args->valid;
const uniform IntersectContext* uniform context = (const uniform IntersectContext* uniform) args->context;
varying struct Ray* uniform ray = (varying struct Ray* uniform)args->ray;
//varying struct RTCHit* uniform hit = (varying struct RTCHit* uniform)args->hit;
const uniform unsigned int N = args->N;
assert(N == programCount);
/* avoid crashing when debug visualizations are used */
if (context == NULL)
return;
/* ignore inactive rays */
if (valid[programIndex] != -1) return;
/* carve out parts of the sphere */
const Vec3f h = ray->org+ray->dir*ray->tfar;
float v = abs(sin(10.0f*h.x)*cos(10.0f*h.y)*sin(10.0f*h.z));
float T = clamp((v-0.1f)*3.0f,0.0f,1.0f);
/* reject some hits */
if (T < 0.5f) valid[programIndex] = 0;
}
unmasked void sphereFilterFunctionN(const RTCFilterFunctionNArguments* uniform args)
{
uniform int* uniform valid = args->valid;
const uniform IntersectContext* uniform context = (const uniform IntersectContext* uniform) args->context;
struct RTCRayN* uniform ray = (RTCRayN* uniform)args->ray;
//struct RTCHitN* uniform hit = args->hit;
const uniform unsigned int N = args->N;
/* avoid crashing when debug visualizations are used */
if (context == NULL)
return;
/* iterate over all rays in ray packet */
for (uniform unsigned int ui=0; ui<N; ui+=programCount)
{
/* calculate varying loop and execution mask */
unsigned int vi = ui+programIndex;
if (vi>=N) continue;
/* ignore inactive rays */
if (valid[vi] != -1) continue;
/* calculate hit point */
Vec3f ray_org = make_Vec3f(RTCRayN_org_x(ray,N,ui),RTCRayN_org_y(ray,N,ui),RTCRayN_org_z(ray,N,ui));
Vec3f ray_dir = make_Vec3f(RTCRayN_dir_x(ray,N,ui),RTCRayN_dir_y(ray,N,ui),RTCRayN_dir_z(ray,N,ui));
float hit_t = RTCRayN_tfar(ray,N,ui);
/* carve out parts of the sphere */
const Vec3f h = ray_org+hit_t*ray_dir;
float v = abs(sin(10.0f*h.x)*cos(10.0f*h.y)*sin(10.0f*h.z));
float T = clamp((v-0.1f)*3.0f,0.0f,1.0f);
/* reject some hits */
if (T < 0.5f) valid[vi] = 0;
}
}
uniform Sphere* uniform createAnalyticalSphere (RTCScene scene, const uniform Vec3f& p, uniform float r)
{
RTCGeometry geom = rtcNewGeometry(g_device, RTC_GEOMETRY_TYPE_USER);
uniform Sphere* uniform sphere = uniform new uniform Sphere;
sphere->p = p;
sphere->r = r;
sphere->geometry = geom;
sphere->geomID = rtcAttachGeometry(scene,geom);
rtcSetGeometryUserPrimitiveCount(geom,1);
rtcSetGeometryUserData(geom,sphere);
rtcSetGeometryBoundsFunction(geom,sphereBoundsFunc,NULL);
if (g_mode == MODE_NORMAL && nativePacketSupported(g_device))
{
rtcSetGeometryIntersectFunction(geom,sphereIntersectFunc);
rtcSetGeometryOccludedFunction (geom,sphereOccludedFunc);
}
else
{
rtcSetGeometryIntersectFunction(geom,sphereIntersectFuncN);
rtcSetGeometryOccludedFunction (geom,sphereOccludedFuncN);
}
rtcCommitGeometry(geom);
rtcReleaseGeometry(geom);
return sphere;
}
uniform Sphere* uniform createAnalyticalSpheres (RTCScene scene, uniform unsigned int N)
{
RTCGeometry geom = rtcNewGeometry(g_device, RTC_GEOMETRY_TYPE_USER);
uniform Sphere* uniform spheres = uniform new uniform Sphere[N];
uniform unsigned int geomID = rtcAttachGeometry(scene,geom);
for (uniform unsigned int i=0; i<N; i++) {
spheres[i].geometry = geom;
spheres[i].geomID = geomID;
}
rtcSetGeometryUserPrimitiveCount(geom,N);
rtcSetGeometryUserData(geom,spheres);
rtcSetGeometryBoundsFunction(geom,sphereBoundsFunc,NULL);
if (g_mode == MODE_NORMAL && nativePacketSupported(g_device))
{
rtcSetGeometryIntersectFunction(geom,sphereIntersectFunc);
rtcSetGeometryOccludedFunction (geom,sphereOccludedFunc);
rtcSetGeometryIntersectFilterFunction(geom,sphereFilterFunction);
rtcSetGeometryOccludedFilterFunction(geom,sphereFilterFunction);
}
else
{
rtcSetGeometryIntersectFunction(geom,sphereIntersectFuncN);
rtcSetGeometryOccludedFunction (geom,sphereOccludedFuncN);
rtcSetGeometryIntersectFilterFunction(geom,sphereFilterFunctionN);
rtcSetGeometryOccludedFilterFunction(geom,sphereFilterFunctionN);
}
rtcCommitGeometry(geom);
rtcReleaseGeometry(geom);
return spheres;
}
// ======================================================================== //
// Triangular sphere geometry //
// ======================================================================== //
uniform unsigned int createTriangulatedSphere (RTCScene scene, const uniform Vec3f& p, uniform float r)
{
/* create triangle mesh */
RTCGeometry geom = rtcNewGeometry (g_device, RTC_GEOMETRY_TYPE_TRIANGLE);
/* map triangle and vertex buffers */
uniform Vertex* uniform vertices = (uniform Vertex* uniform) rtcSetNewGeometryBuffer(geom,RTC_BUFFER_TYPE_VERTEX,0,RTC_FORMAT_FLOAT3,sizeof(uniform Vertex),numTheta*(numPhi+1));
uniform Triangle* uniform triangles = (uniform Triangle* uniform) rtcSetNewGeometryBuffer(geom,RTC_BUFFER_TYPE_INDEX,0,RTC_FORMAT_UINT3,sizeof(uniform Triangle),2*numTheta*(numPhi-1));
/* create sphere */
uniform int tri = 0;
const uniform float rcpNumTheta = rcp((uniform float)numTheta);
const uniform float rcpNumPhi = rcp((uniform float)numPhi);
for (uniform int phi=0; phi<=numPhi; phi++)
{
for (uniform int theta=0; theta<numTheta; theta++)
{
const uniform float phif = phi*pi*rcpNumPhi;
const uniform float thetaf = theta*2.0f*pi*rcpNumTheta;
uniform Vertex& v = vertices[phi*numTheta+theta];
v.x = p.x + r*sin(phif)*sin(thetaf);
v.y = p.y + r*cos(phif);
v.z = p.z + r*sin(phif)*cos(thetaf);
}
if (phi == 0) continue;
for (uniform int theta=1; theta<=numTheta; theta++)
{
uniform int p00 = (phi-1)*numTheta+theta-1;
uniform int p01 = (phi-1)*numTheta+theta%numTheta;
uniform int p10 = phi*numTheta+theta-1;
uniform int p11 = phi*numTheta+theta%numTheta;
if (phi > 1) {
triangles[tri].v0 = p10;
triangles[tri].v1 = p00;
triangles[tri].v2 = p01;
tri++;
}
if (phi < numPhi) {
triangles[tri].v0 = p11;
triangles[tri].v1 = p10;
triangles[tri].v2 = p01;
tri++;
}
}
}
rtcCommitGeometry(geom);
uniform unsigned int geomID = rtcAttachGeometry(scene,geom);
rtcReleaseGeometry(geom);
return geomID;
}
/* creates a ground plane */
uniform unsigned int createGroundPlane (RTCScene scene)
{
/* create a triangulated plane with 2 triangles and 4 vertices */
RTCGeometry geom = rtcNewGeometry (g_device, RTC_GEOMETRY_TYPE_TRIANGLE);
/* set vertices */
uniform Vertex* uniform vertices = (uniform Vertex* uniform) rtcSetNewGeometryBuffer(geom,RTC_BUFFER_TYPE_VERTEX,0,RTC_FORMAT_FLOAT3,sizeof(uniform Vertex),4);
vertices[0].x = -10; vertices[0].y = -2; vertices[0].z = -10;
vertices[1].x = -10; vertices[1].y = -2; vertices[1].z = +10;
vertices[2].x = +10; vertices[2].y = -2; vertices[2].z = -10;
vertices[3].x = +10; vertices[3].y = -2; vertices[3].z = +10;
/* set triangles */
uniform Triangle* uniform triangles = (uniform Triangle* uniform) rtcSetNewGeometryBuffer(geom,RTC_BUFFER_TYPE_INDEX,0,RTC_FORMAT_UINT3,sizeof(uniform Triangle),2);
triangles[0].v0 = 0; triangles[0].v1 = 2; triangles[0].v2 = 1;
triangles[1].v0 = 1; triangles[1].v1 = 2; triangles[1].v2 = 3;
rtcCommitGeometry(geom);
uniform unsigned int geomID = rtcAttachGeometry(scene,geom);
rtcReleaseGeometry(geom);
return geomID;
}
/* called by the C++ code for initialization */
export void device_init (uniform int8* uniform cfg)
{
/* create scene */
TutorialData_Constructor(&data);
g_scene = data.g_scene = rtcNewScene(g_device);
/* create scene with 4 analytical spheres */
data.g_scene0 = rtcNewScene(g_device);
rtcSetSceneBuildQuality(data.g_scene0,RTC_BUILD_QUALITY_LOW);
data.g_spheres = createAnalyticalSpheres(data.g_scene0,4);
data.g_spheres[0].p = make_Vec3f( 0, 0,+1); data.g_spheres[0].r = 0.5f;
data.g_spheres[1].p = make_Vec3f(+1, 0, 0); data.g_spheres[1].r = 0.5f;
data.g_spheres[2].p = make_Vec3f( 0, 0,-1); data.g_spheres[2].r = 0.5f;
data.g_spheres[3].p = make_Vec3f(-1, 0, 0); data.g_spheres[3].r = 0.5f;
rtcCommitScene(data.g_scene0);
/* create scene with 4 triangulated spheres */
data.g_scene1 = rtcNewScene(g_device);
createTriangulatedSphere(data.g_scene1,make_Vec3f( 0, 0,+1),0.5f);
createTriangulatedSphere(data.g_scene1,make_Vec3f(+1, 0, 0),0.5f);
createTriangulatedSphere(data.g_scene1,make_Vec3f( 0, 0,-1),0.5f);
createTriangulatedSphere(data.g_scene1,make_Vec3f(-1, 0, 0),0.5f);
rtcCommitScene(data.g_scene1);
/* create scene with 2 triangulated and 2 analytical spheres */
data.g_scene2 = rtcNewScene(g_device);
createTriangulatedSphere(data.g_scene2,make_Vec3f( 0, 0,+1),0.5f);
data.g_sphere0 = createAnalyticalSphere (data.g_scene2,make_Vec3f(+1, 0, 0),0.5f);
createTriangulatedSphere(data.g_scene2,make_Vec3f( 0, 0,-1),0.5f);
data.g_sphere1 = createAnalyticalSphere (data.g_scene2,make_Vec3f(-1, 0, 0),0.5f);
rtcCommitScene(data.g_scene2);
/* instantiate geometry */
data.g_instance[0] = createInstance(data.g_scene,data.g_scene0,0,make_Vec3f(-2,-2,-2),make_Vec3f(+2,+2,+2));
data.g_instance[1] = createInstance(data.g_scene,data.g_scene1,1,make_Vec3f(-2,-2,-2),make_Vec3f(+2,+2,+2));
data.g_instance[2] = createInstance(data.g_scene,data.g_scene2,2,make_Vec3f(-2,-2,-2),make_Vec3f(+2,+2,+2));
data.g_instance[3] = createInstance(data.g_scene,data.g_scene2,3,make_Vec3f(-2,-2,-2),make_Vec3f(+2,+2,+2));
createGroundPlane(data.g_scene);
rtcCommitScene(data.g_scene);
/* set all colors */
data.colors[0][0] = make_Vec3f(0.25f, 0.00f, 0.00f);
data.colors[0][1] = make_Vec3f(0.50f, 0.00f, 0.00f);
data.colors[0][2] = make_Vec3f(0.75f, 0.00f, 0.00f);
data.colors[0][3] = make_Vec3f(1.00f, 0.00f, 0.00f);
data.colors[1][0] = make_Vec3f(0.00f, 0.25f, 0.00f);
data.colors[1][1] = make_Vec3f(0.00f, 0.50f, 0.00f);
data.colors[1][2] = make_Vec3f(0.00f, 0.75f, 0.00f);
data.colors[1][3] = make_Vec3f(0.00f, 1.00f, 0.00f);
data.colors[2][0] = make_Vec3f(0.00f, 0.00f, 0.25f);
data.colors[2][1] = make_Vec3f(0.00f, 0.00f, 0.50f);
data.colors[2][2] = make_Vec3f(0.00f, 0.00f, 0.75f);
data.colors[2][3] = make_Vec3f(0.00f, 0.00f, 1.00f);
data.colors[3][0] = make_Vec3f(0.25f, 0.25f, 0.00f);
data.colors[3][1] = make_Vec3f(0.50f, 0.50f, 0.00f);
data.colors[3][2] = make_Vec3f(0.75f, 0.75f, 0.00f);
data.colors[3][3] = make_Vec3f(1.00f, 1.00f, 0.00f);
data.colors[4][0] = make_Vec3f(1.0f, 1.0f, 1.0f);
data.colors[4][1] = make_Vec3f(1.0f, 1.0f, 1.0f);
data.colors[4][2] = make_Vec3f(1.0f, 1.0f, 1.0f);
data.colors[4][3] = make_Vec3f(1.0f, 1.0f, 1.0f);
}
inline Vec3f face_forward(const Vec3f& dir, const Vec3f& _Ng) {
const Vec3f Ng = _Ng;
return dot(dir,Ng) < 0.0f ? Ng : neg(Ng);
}
/* task that renders a single screen tile */
Vec3f renderPixelStandard(const uniform TutorialData& data,
float x, float y, const uniform ISPCCamera& camera,
uniform RayStats& stats)
{
uniform RTCIntersectContext context;
rtcInitIntersectContext(&context);
/* initialize ray */
Ray ray = make_Ray(make_Vec3f(camera.xfm.p),
make_Vec3f(normalize(x*camera.xfm.l.vx + y*camera.xfm.l.vy + camera.xfm.l.vz)),
0.0f, inf, 0.0f, -1,
RTC_INVALID_GEOMETRY_ID, RTC_INVALID_GEOMETRY_ID);
/* intersect ray with scene */
rtcIntersectV(data.g_scene,&context,RTCRayHit_(ray));
RayStats_addRay(stats);
/* shade pixels */
Vec3f color = make_Vec3f(0.0f);
if (ray.geomID != RTC_INVALID_GEOMETRY_ID)
{
/* calculate shading normal in world space */
Vec3f Ns = ray.Ng;
if (ray.instID[0] != RTC_INVALID_GEOMETRY_ID) {
Ns = xfmVector(data.g_instance[ray.instID[0]]->normal2world,make_Vec3f(Ns));
}
Ns = face_forward(ray.dir,normalize(Ns));
/* calculate diffuse color of geometries */
Vec3f diffuse = make_Vec3f(0.0f);
if (ray.instID[0] == 0) diffuse = data.colors[ray.instID[0]][ray.primID];
else if (ray.instID[0] == -1) diffuse = data.colors[4][ray.primID];
else diffuse = data.colors[ray.instID[0]][ray.geomID];
color = color + diffuse*0.5;
/* initialize shadow ray */
Vec3f lightDir = normalize(make_Vec3f(-1,-1,-1));
Ray shadow = make_Ray(ray.org + 0.999f*ray.tfar*ray.dir, neg(lightDir), 0.001f, inf);
/* trace shadow ray */
rtcOccludedV(data.g_scene,&context,RTCRay_(shadow));
RayStats_addShadowRay(stats);
/* add light contribution */
if (shadow.tfar >= 0.0f)
color = color + diffuse*clamp(-dot(lightDir,Ns),0.0f,1.0f);
}
return color;
}
void renderPixelStandard(const uniform TutorialData& data,
int x, int y,
uniform int* uniform pixels,
const uniform unsigned int width,
const uniform unsigned int height,
const float time,
const uniform ISPCCamera& camera, uniform RayStats& stats)
{
Vec3f color = renderPixelStandard(data,x,y,camera,stats);
/* write color to framebuffer */
unsigned int r = (unsigned int) (255.0f * clamp(color.x,0.0f,1.0f));
unsigned int g = (unsigned int) (255.0f * clamp(color.y,0.0f,1.0f));
unsigned int b = (unsigned int) (255.0f * clamp(color.z,0.0f,1.0f));
pixels[y*width+x] = (b << 16) + (g << 8) + r;
}
/* renders a single screen tile */
void renderTileStandard(uniform int taskIndex,
uniform int threadIndex,
uniform int* uniform pixels,
const uniform unsigned int width,
const uniform unsigned int height,
const uniform float time,
const uniform ISPCCamera& camera,
const uniform int numTilesX,
const uniform int numTilesY)
{
const uniform unsigned int tileY = taskIndex / numTilesX;
const uniform unsigned int tileX = taskIndex - tileY * numTilesX;
const uniform unsigned int x0 = tileX * TILE_SIZE_X;
const uniform unsigned int x1 = min(x0+TILE_SIZE_X,width);
const uniform unsigned int y0 = tileY * TILE_SIZE_Y;
const uniform unsigned int y1 = min(y0+TILE_SIZE_Y,height);
foreach_tiled (y = y0 ... y1, x = x0 ... x1)
{
renderPixelStandard(data,x,y,pixels,width,height,time,camera,g_stats[threadIndex]);
}
}
/* renders a single screen tile */
void renderTileStandardStream(uniform int taskIndex,
uniform int threadIndex,
uniform int* uniform pixels,
const uniform unsigned int width,
const uniform unsigned int height,
const uniform float time,
const uniform ISPCCamera& camera,
const uniform int numTilesX,
const uniform int numTilesY)
{
const uniform unsigned int tileY = taskIndex / numTilesX;
const uniform unsigned int tileX = taskIndex - tileY * numTilesX;
const uniform unsigned int x0 = tileX * TILE_SIZE_X;
const uniform unsigned int x1 = min(x0+TILE_SIZE_X,width);
const uniform unsigned int y0 = tileY * TILE_SIZE_Y;
const uniform unsigned int y1 = min(y0+TILE_SIZE_Y,height);
uniform RayStats& stats = g_stats[threadIndex];
Ray primary_stream[TILE_SIZE_X*TILE_SIZE_Y];
Ray shadow_stream[TILE_SIZE_X*TILE_SIZE_Y];
Vec3f color_stream[TILE_SIZE_X*TILE_SIZE_Y];
bool valid_stream[TILE_SIZE_X*TILE_SIZE_Y];
/* generate stream of primary rays */
uniform int N = 0;
foreach_tiled (y = y0 ... y1, x = x0 ... x1)
{
/* ISPC workaround for mask == 0 */
if (all(__mask == 0)) continue;
/* initialize variables */
color_stream[N] = make_Vec3f(0.0f);
bool mask = __mask; unmasked { valid_stream[N] = mask; }
/* initialize ray */
Ray& primary = primary_stream[N];
invalidateRay(primary);
init_Ray(primary, make_Vec3f(camera.xfm.p), make_Vec3f(normalize((float)x*camera.xfm.l.vx +
(float)y*camera.xfm.l.vy + camera.xfm.l.vz)), 0.f, pos_inf, 0.0f, -1,
RTC_INVALID_GEOMETRY_ID, RTC_INVALID_GEOMETRY_ID);
N++;
RayStats_addRay(stats);
}
Vec3f lightDir = normalize(make_Vec3f(-1,-1,-1));
/* trace rays */
uniform RTCIntersectContext primary_context;
rtcInitIntersectContext(&primary_context);
primary_context.flags = g_iflags_coherent;
rtcIntersectVM(g_scene,&primary_context,(varying RTCRayHit* uniform)&primary_stream,N,sizeof(Ray));
/* terminate rays and update color */
N = -1;
foreach_tiled (y = y0 ... y1, x = x0 ... x1)
{
N++;
/* ISPC workaround for mask == 0 */
if (all(__mask == 0)) continue;
/* invalidate shadow rays by default */
Ray& shadow = shadow_stream[N];
unmasked {
shadow.org = make_Vec3f_(0.f);
shadow.dir = make_Vec3f_(0.f);
shadow.tnear = (float)(pos_inf);
shadow.tfar = (float)(neg_inf);
}
Ray& primary = primary_stream[N];
/* ignore invalid rays */
if (valid_stream[N] == false) continue;
/* terminate rays that hit nothing */
if (primary_stream[N].geomID == RTC_INVALID_GEOMETRY_ID) {
valid_stream[N] = false;
continue;
}
/* calculate diffuse color of geometries */
Vec3f diffuse = make_Vec3f(0.0f);
if (primary.instID[0] == 0) diffuse = data.colors[primary.instID[0]][primary.primID];
else if (primary.instID[0] == -1) diffuse = data.colors[4][primary.primID];
else diffuse = data.colors[primary.instID[0]][primary.geomID];
color_stream[N] = color_stream[N] + diffuse*0.5;
/* initialize shadow ray */
init_Ray(shadow,primary.org + 0.999f*primary.tfar*primary.dir, neg(lightDir), 0.001f, pos_inf, 0.0f, N*programCount + programIndex);
RayStats_addShadowRay(stats);
}
N++;
/* trace shadow rays */
uniform RTCIntersectContext shadow_context;
rtcInitIntersectContext(&shadow_context);
shadow_context.flags = g_iflags_coherent;
rtcOccludedVM(g_scene,&shadow_context,(varying RTCRay* uniform)&shadow_stream,N,sizeof(Ray));
/* add light contribution */
N = -1;
foreach_tiled (y = y0 ... y1, x = x0 ... x1)
{
N++;
/* ISPC workaround for mask == 0 */
if (all(__mask == 0)) continue;
/* ignore invalid rays */
if (valid_stream[N] == false) continue;
/* calculate shading normal in world space */
Ray& primary = primary_stream[N];
Vec3f Ns = primary.Ng;
if (primary.instID[0] != RTC_INVALID_GEOMETRY_ID) {
Ns = xfmVector(data.g_instance[primary.instID[0]]->normal2world,make_Vec3f(Ns));
}
Ns = face_forward(primary.dir,normalize(Ns));
/* add light contrinution */
Vec3f diffuse = make_Vec3f(0.0f);
if (primary.instID[0] == 0) diffuse = data.colors[primary.instID[0]][primary.primID];
else if (primary.instID[0] == -1) diffuse = data.colors[4][primary.primID];
else diffuse = data.colors[primary.instID[0]][primary.geomID];
Ray& shadow = shadow_stream[N];
if (shadow.tfar >= 0.0f) {
color_stream[N] = color_stream[N] + diffuse*clamp(-dot(lightDir,Ns),0.0f,1.0f);
}
}
N++;
/* framebuffer writeback */
N = 0;
foreach_tiled (y = y0 ... y1, x = x0 ... x1)
{
/* ISPC workaround for mask == 0 */
if (all(__mask == 0)) continue;
/* write color to framebuffer */
unsigned int r = (unsigned int) (255.0f * clamp(color_stream[N].x,0.0f,1.0f));
unsigned int g = (unsigned int) (255.0f * clamp(color_stream[N].y,0.0f,1.0f));
unsigned int b = (unsigned int) (255.0f * clamp(color_stream[N].z,0.0f,1.0f));
pixels[y*width+x] = (b << 16) + (g << 8) + r;
N++;
}
}
/* task that renders a single screen tile */
task void renderTileTask(uniform int* uniform pixels,
const uniform unsigned int width,
const uniform unsigned int height,
const uniform float time,
const uniform ISPCCamera& camera,
const uniform int numTilesX,
const uniform int numTilesY)
{
if (g_mode == MODE_NORMAL)
renderTileStandard(taskIndex,threadIndex,pixels,width,height,time,camera,numTilesX,numTilesY);
else
renderTileStandardStream(taskIndex,threadIndex,pixels,width,height,time,camera,numTilesX,numTilesY);
}
export void renderFrameStandard (uniform int* uniform pixels,
const uniform unsigned int width,
const uniform unsigned int height,
const uniform float time,
const uniform ISPCCamera& camera)
{
/* render all pixels */
const uniform int numTilesX = (width +TILE_SIZE_X-1)/TILE_SIZE_X;
const uniform int numTilesY = (height+TILE_SIZE_Y-1)/TILE_SIZE_Y;
launch[numTilesX*numTilesY] renderTileTask(pixels,width,height,time,camera,numTilesX,numTilesY); sync;
}
/* called by the C++ code to render */
export void device_render (uniform int* uniform pixels,
const uniform unsigned int width,
const uniform unsigned int height,
const uniform float time,
const uniform ISPCCamera& camera)
{
uniform float t0 = 0.7f*time;
uniform float t1 = 1.5f*time;
/* rotate instances around themselves */
uniform LinearSpace3f xfm;
xfm.vx = make_Vec3f(cos(t1),0,sin(t1));
xfm.vy = make_Vec3f(0,1,0);
xfm.vz = make_Vec3f(-sin(t1),0,cos(t1));
/* calculate transformations to move instances in circles */
data.g_instance[0]->local2world = make_AffineSpace3f(xfm,2.2f*make_Vec3f(+cos(t0),0.0f,+sin(t0)));
data.g_instance[1]->local2world = make_AffineSpace3f(xfm,2.2f*make_Vec3f(-cos(t0),0.0f,-sin(t0)));
data.g_instance[2]->local2world = make_AffineSpace3f(xfm,2.2f*make_Vec3f(-sin(t0),0.0f,+cos(t0)));
data.g_instance[3]->local2world = make_AffineSpace3f(xfm,2.2f*make_Vec3f(+sin(t0),0.0f,-cos(t0)));
/* update scene */
updateInstance(data.g_scene,data.g_instance[0]);
updateInstance(data.g_scene,data.g_instance[1]);
updateInstance(data.g_scene,data.g_instance[2]);
updateInstance(data.g_scene,data.g_instance[3]);
rtcCommitScene (data.g_scene);
}
/* called by the C++ code for cleanup */
export void device_cleanup ()
{
TutorialData_Destructor(&data);
}
|