1 // Copyright 2009-2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #include "../common/tutorial/tutorial_device.h"
5 
6 namespace embree {
7 
8 /* configuration */
9 #define EDGE_LEVEL 256.0f
10 #define ENABLE_SMOOTH_NORMALS 0
11 
12 /* scene data */
13 RTCScene g_scene = nullptr;
14 
15 /* previous camera position */
16 Vec3fa old_p;
17 
18 __aligned(16) float cube_vertices[8][4] =
19 {
20   { -1.0f, -1.0f, -1.0f, 0.0f },
21   {  1.0f, -1.0f, -1.0f, 0.0f },
22   {  1.0f, -1.0f,  1.0f, 0.0f },
23   { -1.0f, -1.0f,  1.0f, 0.0f },
24   { -1.0f,  1.0f, -1.0f, 0.0f },
25   {  1.0f,  1.0f, -1.0f, 0.0f },
26   {  1.0f,  1.0f,  1.0f, 0.0f },
27   { -1.0f,  1.0f,  1.0f, 0.0f }
28 };
29 
30 #if 1
31 
32 #define NUM_INDICES 24
33 #define NUM_FACES 6
34 #define FACE_SIZE 4
35 
36 unsigned int cube_indices[24] = {
37   0, 4, 5, 1,
38   1, 5, 6, 2,
39   2, 6, 7, 3,
40   0, 3, 7, 4,
41   4, 7, 6, 5,
42   0, 1, 2, 3,
43 };
44 
45 unsigned int cube_faces[6] = {
46   4, 4, 4, 4, 4, 4
47 };
48 
49 #else
50 
51 #define NUM_INDICES 36
52 #define NUM_FACES 12
53 #define FACE_SIZE 3
54 
55 unsigned int cube_indices[36] = {
56   1, 4, 5,  0, 4, 1,
57   2, 5, 6,  1, 5, 2,
58   3, 6, 7,  2, 6, 3,
59   4, 3, 7,  0, 3, 4,
60   5, 7, 6,  4, 7, 5,
61   3, 1, 2,  0, 1, 3
62 };
63 
64 unsigned int cube_faces[12] = {
65   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3
66 };
67 
68 #endif
69 
displacement(const Vec3fa & P)70 float displacement(const Vec3fa& P)
71 {
72   float dN = 0.0f;
73   for (float freq = 1.0f; freq<40.0f; freq*= 2) {
74     float n = abs(noise(freq*P));
75     dN += 1.4f*n*n/freq;
76   }
77   return dN;
78 }
79 
displacement_du(const Vec3fa & P,const Vec3fa & dPdu)80 float displacement_du(const Vec3fa& P, const Vec3fa& dPdu)
81 {
82   const float du = 0.001f;
83   return (displacement(P+du*dPdu)-displacement(P))/du;
84 }
85 
displacement_dv(const Vec3fa & P,const Vec3fa & dPdv)86 float displacement_dv(const Vec3fa& P, const Vec3fa& dPdv)
87 {
88   const float dv = 0.001f;
89   return (displacement(P+dv*dPdv)-displacement(P))/dv;
90 }
91 
displacementFunction(const struct RTCDisplacementFunctionNArguments * args)92 void displacementFunction(const struct RTCDisplacementFunctionNArguments* args)
93 {
94   const float* nx = args->Ng_x;
95   const float* ny = args->Ng_y;
96   const float* nz = args->Ng_z;
97   float* px = args->P_x;
98   float* py = args->P_y;
99   float* pz = args->P_z;
100   unsigned int N = args->N;
101 
102   for (unsigned int i=0; i<N; i++) {
103     const Vec3fa P = Vec3fa(px[i],py[i],pz[i]);
104     const Vec3fa Ng = Vec3fa(nx[i],ny[i],nz[i]);
105     const Vec3fa dP = displacement(P)*Ng;
106     px[i] += dP.x; py[i] += dP.y; pz[i] += dP.z;
107   }
108 }
109 
110 /* adds a cube to the scene */
addCube(RTCScene scene_i)111 unsigned int addCube (RTCScene scene_i)
112 {
113   /* create a triangulated cube with 6 quads and 8 vertices */
114   RTCGeometry geom = rtcNewGeometry(g_device, RTC_GEOMETRY_TYPE_SUBDIVISION);
115 
116   rtcSetSharedGeometryBuffer(geom, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, cube_vertices, 0, sizeof(Vec3fa),       8);
117   rtcSetSharedGeometryBuffer(geom, RTC_BUFFER_TYPE_INDEX,  0, RTC_FORMAT_UINT,   cube_indices,  0, sizeof(unsigned int), NUM_INDICES);
118   rtcSetSharedGeometryBuffer(geom, RTC_BUFFER_TYPE_FACE,   0, RTC_FORMAT_UINT,   cube_faces,    0, sizeof(unsigned int), NUM_FACES);
119 
120   float* level = (float*) rtcSetNewGeometryBuffer(geom, RTC_BUFFER_TYPE_LEVEL, 0, RTC_FORMAT_FLOAT, sizeof(float), NUM_INDICES);
121   for (size_t i=0; i<NUM_INDICES; i++) level[i] = EDGE_LEVEL;
122 
123   rtcSetGeometryDisplacementFunction(geom,displacementFunction);
124 
125   rtcCommitGeometry(geom);
126   unsigned int geomID = rtcAttachGeometry(scene_i,geom);
127   rtcReleaseGeometry(geom);
128   return geomID;
129 }
130 
131 /* adds a ground plane to the scene */
addGroundPlane(RTCScene scene_i)132 unsigned int addGroundPlane (RTCScene scene_i)
133 {
134   /* create a triangulated plane with 2 triangles and 4 vertices */
135   RTCGeometry geom = rtcNewGeometry (g_device, RTC_GEOMETRY_TYPE_TRIANGLE);
136 
137   /* set vertices */
138   Vertex* vertices = (Vertex*) rtcSetNewGeometryBuffer(geom,RTC_BUFFER_TYPE_VERTEX,0,RTC_FORMAT_FLOAT3,sizeof(Vertex),4);
139   vertices[0].x = -10; vertices[0].y = -2; vertices[0].z = -10;
140   vertices[1].x = -10; vertices[1].y = -2; vertices[1].z = +10;
141   vertices[2].x = +10; vertices[2].y = -2; vertices[2].z = -10;
142   vertices[3].x = +10; vertices[3].y = -2; vertices[3].z = +10;
143 
144   /* set triangles */
145   Triangle* triangles = (Triangle*) rtcSetNewGeometryBuffer(geom,RTC_BUFFER_TYPE_INDEX,0,RTC_FORMAT_UINT3,sizeof(Triangle),2);
146   triangles[0].v0 = 0; triangles[0].v1 = 1; triangles[0].v2 = 2;
147   triangles[1].v0 = 1; triangles[1].v1 = 3; triangles[1].v2 = 2;
148 
149   rtcCommitGeometry(geom);
150   unsigned int geomID = rtcAttachGeometry(scene_i,geom);
151   rtcReleaseGeometry(geom);
152   return geomID;
153 }
154 
155 /* called by the C++ code for initialization */
device_init(char * cfg)156 extern "C" void device_init (char* cfg)
157 {
158   /* create scene */
159   g_scene = rtcNewScene(g_device);
160   rtcSetSceneFlags(g_scene,RTC_SCENE_FLAG_ROBUST);
161 
162   /* add ground plane */
163   addGroundPlane(g_scene);
164 
165   /* add cube */
166   addCube(g_scene);
167 
168   /* commit changes to scene */
169   rtcCommitScene (g_scene);
170 }
171 
172 /* task that renders a single screen tile */
renderPixelStandard(float x,float y,const ISPCCamera & camera,RayStats & stats)173 Vec3fa renderPixelStandard(float x, float y, const ISPCCamera& camera, RayStats& stats)
174 {
175   RTCIntersectContext context;
176   rtcInitIntersectContext(&context);
177 
178   /* initialize ray */
179   Ray ray(Vec3fa(camera.xfm.p), Vec3fa(normalize(x*camera.xfm.l.vx + y*camera.xfm.l.vy + camera.xfm.l.vz)), 0.0f, inf);
180 
181   /* intersect ray with scene */
182   rtcIntersect1(g_scene,&context,RTCRayHit_(ray));
183   RayStats_addRay(stats);
184 
185   /* shade pixels */
186   Vec3fa color = Vec3fa(0.0f);
187   if (ray.geomID != RTC_INVALID_GEOMETRY_ID)
188   {
189     Vec3fa diffuse = ray.geomID != 0 ? Vec3fa(0.9f,0.6f,0.5f) : Vec3fa(0.8f,0.0f,0.0f);
190     color = color + diffuse*0.5f;
191     Vec3fa lightDir = normalize(Vec3fa(-1,-1,-1));
192 
193     Vec3fa Ng = normalize(ray.Ng);
194 #if ENABLE_SMOOTH_NORMALS
195     Vec3fa P = ray.org + ray.tfar*ray.dir;
196     if (ray.geomID > 0) {
197       Vec3fa dPdu,dPdv;
198       auto geomID = ray.geomID; {
199         rtcInterpolate1(rtcGetGeometry(g_scene,geomID),ray.primID,ray.u,ray.v,RTC_BUFFER_TYPE_VERTEX,0,nullptr,&dPdu.x,&dPdv.x,3);
200       }
201       Ng = normalize(cross(dPdu,dPdv));
202       dPdu = dPdu + Ng*displacement_du(P,dPdu);
203       dPdv = dPdv + Ng*displacement_dv(P,dPdv);
204       Ng = normalize(cross(dPdu,dPdv));
205     }
206 #endif
207 
208     /* initialize shadow ray */
209     Ray shadow(ray.org + ray.tfar*ray.dir, neg(lightDir), 0.001f, inf, 0.0f);
210 
211     /* trace shadow ray */
212     rtcOccluded1(g_scene,&context,RTCRay_(shadow));
213     RayStats_addShadowRay(stats);
214 
215     /* add light contribution */
216     if (shadow.tfar >= 0.0f)
217       color = color + diffuse*clamp(-(dot(lightDir,Ng)),0.0f,1.0f);
218   }
219   return color;
220 }
221 
222 /* renders a single screen tile */
renderTileStandard(int taskIndex,int threadIndex,int * pixels,const unsigned int width,const unsigned int height,const float time,const ISPCCamera & camera,const int numTilesX,const int numTilesY)223 void renderTileStandard(int taskIndex,
224                         int threadIndex,
225                         int* pixels,
226                         const unsigned int width,
227                         const unsigned int height,
228                         const float time,
229                         const ISPCCamera& camera,
230                         const int numTilesX,
231                         const int numTilesY)
232 {
233   const unsigned int tileY = taskIndex / numTilesX;
234   const unsigned int tileX = taskIndex - tileY * numTilesX;
235   const unsigned int x0 = tileX * TILE_SIZE_X;
236   const unsigned int x1 = min(x0+TILE_SIZE_X,width);
237   const unsigned int y0 = tileY * TILE_SIZE_Y;
238   const unsigned int y1 = min(y0+TILE_SIZE_Y,height);
239 
240   for (unsigned int y=y0; y<y1; y++) for (unsigned int x=x0; x<x1; x++)
241   {
242     /* calculate pixel color */
243     Vec3fa color = renderPixelStandard((float)x,(float)y,camera,g_stats[threadIndex]);
244 
245     /* write color to framebuffer */
246     unsigned int r = (unsigned int) (255.0f * clamp(color.x,0.0f,1.0f));
247     unsigned int g = (unsigned int) (255.0f * clamp(color.y,0.0f,1.0f));
248     unsigned int b = (unsigned int) (255.0f * clamp(color.z,0.0f,1.0f));
249     pixels[y*width+x] = (b << 16) + (g << 8) + r;
250   }
251 }
252 
253 /* task that renders a single screen tile */
renderTileTask(int taskIndex,int threadIndex,int * pixels,const unsigned int width,const unsigned int height,const float time,const ISPCCamera & camera,const int numTilesX,const int numTilesY)254 void renderTileTask (int taskIndex, int threadIndex, int* pixels,
255                          const unsigned int width,
256                          const unsigned int height,
257                          const float time,
258                          const ISPCCamera& camera,
259                          const int numTilesX,
260                          const int numTilesY)
261 {
262   renderTileStandard(taskIndex,threadIndex,pixels,width,height,time,camera,numTilesX,numTilesY);
263 }
264 
renderFrameStandard(int * pixels,const unsigned int width,const unsigned int height,const float time,const ISPCCamera & camera)265 extern "C" void renderFrameStandard (int* pixels,
266                           const unsigned int width,
267                           const unsigned int height,
268                           const float time,
269                           const ISPCCamera& camera)
270 {
271   /* render image */
272   const int numTilesX = (width +TILE_SIZE_X-1)/TILE_SIZE_X;
273   const int numTilesY = (height+TILE_SIZE_Y-1)/TILE_SIZE_Y;
274   parallel_for(size_t(0),size_t(numTilesX*numTilesY),[&](const range<size_t>& range) {
275     const int threadIndex = (int)TaskScheduler::threadIndex();
276     for (size_t i=range.begin(); i<range.end(); i++)
277       renderTileTask((int)i,threadIndex,pixels,width,height,time,camera,numTilesX,numTilesY);
278   });
279 }
280 
281 /* called by the C++ code to render */
device_render(int * pixels,const unsigned int width,const unsigned int height,const float time,const ISPCCamera & camera)282 extern "C" void device_render (int* pixels,
283                            const unsigned int width,
284                            const unsigned int height,
285                            const float time,
286                            const ISPCCamera& camera)
287 {
288 }
289 
290 /* called by the C++ code for cleanup */
device_cleanup()291 extern "C" void device_cleanup ()
292 {
293   rtcReleaseScene (g_scene); g_scene = nullptr;
294 }
295 
296 } // namespace embree
297