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