1 // Copyright 2009-2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #define _CRT_SECURE_NO_WARNINGS
7 
8 /* size of screen tiles */
9 #define TILE_SIZE_X 8
10 #define TILE_SIZE_Y 8
11 
12 /* vertex and triangle layout */
13 struct Vertex   { float x,y,z,r;  }; // FIXME: rename to Vertex4f
14 struct Triangle { int v0, v1, v2; };
15 
16 /* include embree API */
17 #include "../../../include/embree3/rtcore.h"
18 RTC_NAMESPACE_USE
19 
20 /* include optional vector library */
21 #include "../math/math.h"
22 #include "../math/vec.h"
23 #include "../math/affinespace.h"
24 #include "../core/ray.h"
25 #include "camera.h"
26 #include "scene_device.h"
27 #include "noise.h"
28 #if !defined(ISPC)
29 #include "../../../common/algorithms/parallel_for.h"
30 
31 namespace embree {
32 #endif
33 
34 enum Mode {
35   MODE_NORMAL = 0,
36   MODE_STREAM = 1
37 };
38 
39 extern "C" RTCDevice g_device;
40 extern "C" Mode g_mode;
41 extern "C" RTCIntersectContextFlags g_iflags_coherent;
42 extern "C" RTCIntersectContextFlags g_iflags_incoherent;
43 
44 /* error reporting function */
45 void error_handler(void* userPtr, RTCError code, const char* str = nullptr);
46 
47 /* returns time stamp counter */
48 extern "C" int64_t get_tsc();
49 
50 /* declare some standard library functions */
51 extern "C" void abort ();
52 extern "C" void exit(int);
53 extern "C" int puts ( const char* str );
54 extern "C" int putchar ( int character );
55 
56 /* face forward for shading normals */
faceforward(const Vec3fa & N,const Vec3fa & I,const Vec3fa & Ng)57 inline Vec3fa faceforward( const Vec3fa& N, const Vec3fa& I, const Vec3fa& Ng ) {
58   Vec3fa NN = N; return dot(I, Ng) < 0 ? NN : neg(NN);
59 }
60 
61 /* GLFW keys codes */
62 #if !defined(GLFW_KEY_F1)
63 #define GLFW_KEY_F1                 290
64 #define GLFW_KEY_F2                 291
65 #define GLFW_KEY_F3                 292
66 #define GLFW_KEY_F4                 293
67 #define GLFW_KEY_F5                 294
68 #define GLFW_KEY_F6                 295
69 #define GLFW_KEY_F7                 296
70 #define GLFW_KEY_F8                 297
71 #define GLFW_KEY_F9                 298
72 #define GLFW_KEY_F10                299
73 #define GLFW_KEY_F11                300
74 #define GLFW_KEY_F12                301
75 #endif
76 
77 extern "C" void device_key_pressed_default(int key);
78 extern "C" void (* key_pressed_handler)(int key);
79 
80 extern "C" void renderFrameStandard(int* pixels,
81                          const unsigned int width,
82                          const unsigned int height,
83                          const float time,
84                          const ISPCCamera& camera);
85 
86 unsigned int getNumHWThreads();
87 
88 #if defined(ISPC)
89 #define ALIGNED_STRUCT_(x)
90 #define __aligned(x)
91 #define MAYBE_UNUSED
92 #endif
93 
94 /* draws progress bar */
95 extern "C" void progressStart();
96 extern "C" bool progressMonitor(void* ptr, const double n);
97 extern "C" void progressEnd();
98 
99 Vec2f  getTextureCoordinatesSubdivMesh(void* mesh, const unsigned int primID, const float u, const float v);
100 
101 float  getTextureTexel1f(const Texture* texture, float u, float v);
102 Vec3fa  getTextureTexel3f(const Texture* texture, float u, float v);
103 
104 enum ISPCInstancingMode { ISPC_INSTANCING_NONE, ISPC_INSTANCING_GEOMETRY, ISPC_INSTANCING_GROUP };
105 
106 /* ray statistics */
107 #if !defined(TASKING_PPL) // not supported with PPL because threadIndex is not unique and atomics are too expensive
108 #define RAY_STATS
109 #endif
110 
111 struct RayStats
112 {
113   int numRays;
114   int pad[32-1];
115 };
116 
117 #if defined(RAY_STATS)
118 #if defined(ISPC)
RayStats_addRay(RayStats & stats)119 inline void RayStats_addRay(RayStats& stats)       { stats.numRays += popcnt(1); }
RayStats_addShadowRay(RayStats & stats)120 inline void RayStats_addShadowRay(RayStats& stats) { stats.numRays += popcnt(1); }
121 #else // C++
RayStats_addRay(RayStats & stats)122 __forceinline void RayStats_addRay(RayStats& stats)        { stats.numRays++; }
RayStats_addShadowRay(RayStats & stats)123 __forceinline void RayStats_addShadowRay(RayStats& stats)  { stats.numRays++; }
124 #endif
125 #else // disabled
RayStats_addRay(RayStats & stats)126 inline void RayStats_addRay(RayStats& stats)       {}
RayStats_addShadowRay(RayStats & stats)127 inline void RayStats_addShadowRay(RayStats& stats) {}
128 #endif
129 
130 extern "C" RayStats* g_stats;
131 
nativePacketSupported(RTCDevice device)132 inline bool nativePacketSupported(RTCDevice device)
133 {
134   if (sizeof(float) == 1*4) return true;
135   else if (sizeof(float) == 4*4) return rtcGetDeviceProperty(device,RTC_DEVICE_PROPERTY_NATIVE_RAY4_SUPPORTED);
136   else if (sizeof(float) == 8*4) return rtcGetDeviceProperty(device,RTC_DEVICE_PROPERTY_NATIVE_RAY8_SUPPORTED);
137   else if (sizeof(float) == 16*4) return rtcGetDeviceProperty(device,RTC_DEVICE_PROPERTY_NATIVE_RAY16_SUPPORTED);
138   else return false;
139 }
140 
141 } // namespace embree
142