<commit_before>// ======================================================================== //
// Copyright 2009-2015 Intel Corporation                                    //
//                                                                          //
// Licensed under the Apache License, Version 2.0 (the "License");          //
// you may not use this file except in compliance with the License.         //
// You may obtain a copy of the License at                                  //
//                                                                          //
//     http://www.apache.org/licenses/LICENSE-2.0                           //
//                                                                          //
// Unless required by applicable law or agreed to in writing, software      //
// distributed under the License is distributed on an "AS IS" BASIS,        //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and      //
// limitations under the License.                                           //
// ======================================================================== //

#include "tutorial_device.h"
#include "kernels/algorithms/parallel_for.h"
#include "sys/sysinfo.h"
#include "scene_device.h"

#if defined(USE_PTEX)
#include "Ptexture.h"
#endif

/* the scene to render */
extern RTCScene g_scene;

/* global subdivision level for subdivision geometry */
unsigned int g_subdivision_levels = 0;

/* intensity scaling for traversal cost visualization */
//float scale = 0.001f;
float scale = 1.0f / 1000000.0f;

extern "C" bool g_changed = false;

/* stores pointer to currently used rendePixel function */
extern renderPixelFunc renderPixel;

extern float g_debug;

/* standard rendering function for each tutorial */
Vec3fa renderPixelStandard(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p);

/* renders a single pixel with eyelight shading */
Vec3fa renderPixelEyeLight(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p)
{
  /* initialize ray */
  RTCRay ray;
  ray.org = p;
  ray.dir = normalize(x*vx + y*vy + vz);
  ray.tnear = 0.0f;
  ray.tfar = inf;
  ray.geomID = RTC_INVALID_GEOMETRY_ID;
  ray.primID = RTC_INVALID_GEOMETRY_ID;
  ray.mask = -1;
  ray.time = g_debug;

  /* intersect ray with scene */
  rtcIntersect(g_scene,ray);

  /* shade pixel */
  if (ray.geomID == RTC_INVALID_GEOMETRY_ID) return Vec3fa(0.0f);
  else return Vec3fa(embree::abs(dot(ray.dir,normalize(ray.Ng))));
}

__noinline void setray(RTCRay& ray)
{
  ray.u = ray.v = 0.001f;
  ray.Ng = Vec3fa(0,1,0);
  ray.geomID = 0;
  ray.primID = 0;
}

/* renders a single pixel with wireframe shading */
Vec3fa renderPixelWireframe(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p)
{
  /* initialize ray */
  RTCRay ray;
  ray.org = p;
  ray.dir = normalize(x*vx + y*vy + vz);
  ray.tnear = 0.0f;
  ray.tfar = inf;
  ray.geomID = RTC_INVALID_GEOMETRY_ID;
  ray.primID = RTC_INVALID_GEOMETRY_ID;
  ray.mask = -1;
  ray.time = g_debug;

  /* intersect ray with scene */
  rtcIntersect(g_scene,ray);

  /* return black if nothing hit */
  if (ray.geomID == RTC_INVALID_GEOMETRY_ID) return Vec3fa(1.0f);

  /* calculate wireframe around triangles */
  const float border = 0.05f;
  Vec3fa color = Vec3fa(1.0f);
  if (ray.u < border) color = Vec3fa(0.0f);
  if (ray.v < border) color = Vec3fa(0.0f);
  if (1.0f-ray.u-ray.v < border) color = Vec3fa(0.0f);

  /* perform eyelight shading */
  return color*Vec3fa(embree::abs(dot(ray.dir,normalize(ray.Ng))));
}

/* renders a single pixel with UV shading */
Vec3fa renderPixelUV(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p)
{
  /* initialize ray */
  RTCRay ray;
  ray.org = p;
  ray.dir = normalize(x*vx + y*vy + vz);
  ray.tnear = 0.0f;
  ray.tfar = inf;
  ray.geomID = RTC_INVALID_GEOMETRY_ID;
  ray.primID = RTC_INVALID_GEOMETRY_ID;
  ray.mask = -1;
  ray.time = g_debug;

  /* intersect ray with scene */
  rtcIntersect(g_scene,ray);

  /* shade pixel */
  if (ray.geomID == RTC_INVALID_GEOMETRY_ID) return Vec3fa(0.0f);
  else return Vec3fa(ray.u,ray.v,1.0f-ray.u-ray.v);
}

/* renders a single pixel with geometry normal shading */
Vec3fa renderPixelNg(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p)
{
  /* initialize ray */
  RTCRay ray;
  ray.org = p;
  ray.dir = normalize(x*vx + y*vy + vz);
  ray.tnear = 0.0f;
  ray.tfar = inf;
  ray.geomID = RTC_INVALID_GEOMETRY_ID;
  ray.primID = RTC_INVALID_GEOMETRY_ID;
  ray.mask = -1;
  ray.time = g_debug;

  /* intersect ray with scene */
  rtcIntersect(g_scene,ray);

  /* shade pixel */
  if (ray.geomID == RTC_INVALID_GEOMETRY_ID) return Vec3fa(0.0f);
  else {
    //if (dot(ray.dir,ray.Ng) > 0.0f) return Vec3fa(zero); else
    return abs(normalize(Vec3fa(ray.Ng.x,ray.Ng.y,ray.Ng.z)));
  }
}

Vec3fa randomColor(const int ID) 
{
  int r = ((ID+13)*17*23) & 255;
  int g = ((ID+15)*11*13) & 255;
  int b = ((ID+17)* 7*19) & 255;
  const float oneOver255f = 1.f/255.f;
  return Vec3fa(r*oneOver255f,g*oneOver255f,b*oneOver255f);
}

/* geometry ID shading */
Vec3fa renderPixelGeomID(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p)
{
  /* initialize ray */
  RTCRay ray;
  ray.org = p;
  ray.dir = normalize(x*vx + y*vy + vz);
  ray.tnear = 0.0f;
  ray.tfar = inf;
  ray.geomID = RTC_INVALID_GEOMETRY_ID;
  ray.primID = RTC_INVALID_GEOMETRY_ID;
  ray.mask = -1;
  ray.time = g_debug;

  /* intersect ray with scene */
  rtcIntersect(g_scene,ray);

  /* shade pixel */
  if (ray.geomID == RTC_INVALID_GEOMETRY_ID) return Vec3fa(0.0f);
  else return randomColor(ray.geomID);
}

/* geometry ID and primitive ID shading */
Vec3fa renderPixelGeomIDPrimID(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p)
{
  /* initialize ray */
  RTCRay ray;
  ray.org = p;
  ray.dir = normalize(x*vx + y*vy + vz);
  ray.tnear = 0.0f;
  ray.tfar = inf;
  ray.geomID = RTC_INVALID_GEOMETRY_ID;
  ray.primID = RTC_INVALID_GEOMETRY_ID;
  ray.mask = -1;
  ray.time = g_debug;

  /* intersect ray with scene */
  rtcIntersect(g_scene,ray);

  /* shade pixel */
  if (ray.geomID == RTC_INVALID_GEOMETRY_ID) return Vec3fa(0.0f);
  else return randomColor(ray.geomID ^ ray.primID);
}

/* vizualizes the traversal cost of a pixel */
Vec3fa renderPixelCycles(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p)
{
  /* initialize ray */
  RTCRay ray;
  ray.org = p;
  ray.dir = normalize(x*vx + y*vy + vz);
  ray.tnear = 0.0f;
  ray.tfar = inf;
  ray.geomID = RTC_INVALID_GEOMETRY_ID;
  ray.primID = RTC_INVALID_GEOMETRY_ID;
  ray.mask = -1;
  ray.time = g_debug;

  /* intersect ray with scene */
  int64 c0 = get_tsc();
  rtcIntersect(g_scene,ray);
  int64 c1 = get_tsc();
  /* shade pixel */
  return Vec3fa((float)(c1-c0)*scale,0.0f,0.0f);
}

/* renders a single pixel with UV shading */
Vec3fa renderPixelUV16(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p)
{
  /* initialize ray */
  RTCRay ray;
  ray.org = p;
  ray.dir = normalize(x*vx + y*vy + vz);
  ray.tnear = 0.0f;
  ray.tfar = inf;
  ray.geomID = RTC_INVALID_GEOMETRY_ID;
  ray.primID = RTC_INVALID_GEOMETRY_ID;
  ray.mask = -1;
  ray.time = g_debug;

  /* intersect ray with scene */
  for (int i=0; i<16; i++) {
    ray.tfar = inf;
    rtcIntersect(g_scene,ray);
  }

  /* shade pixel */
  if (ray.geomID == RTC_INVALID_GEOMETRY_ID) return Vec3fa(0.0f);
  else return Vec3fa(ray.u,ray.v,1.0f-ray.u-ray.v);
}

/* renders a single pixel casting with ambient occlusion */
Vec3fa renderPixelAmbientOcclusion(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p)
{
  /* initialize ray */
  RTCRay ray;
  ray.org = p;
  ray.dir = normalize(x*vx + y*vy + vz);
  ray.tnear = 0.0f;
  ray.tfar = inf;
  ray.geomID = RTC_INVALID_GEOMETRY_ID;
  ray.primID = RTC_INVALID_GEOMETRY_ID;
  ray.mask = -1;
  ray.time = g_debug;

  /* intersect ray with scene */
  rtcIntersect(g_scene,ray);

  /* shade pixel */
  if (ray.geomID == RTC_INVALID_GEOMETRY_ID) return Vec3fa(0.0f);

  Vec3fa Ng = normalize(ray.Ng);
  Vec3fa col = Vec3fa(min(1.f,.3f+.8f*abs(dot(Ng,normalize(ray.dir)))));

  /* calculate hit point */
  float intensity = 0;
  Vec3fa hitPos = ray.org + ray.tfar * ray.dir;

#define AMBIENT_OCCLUSION_SAMPLES 16
  /* trace some ambient occlusion rays */
  int seed = 34*x+12*y;
  for (int i=0; i<AMBIENT_OCCLUSION_SAMPLES; i++) 
     {
      Vec3fa dir; 
      const float oneOver10000f = 1.f/10000.f;
      seed = 1103515245 * seed + 12345;
      dir.x = (seed%10000)*oneOver10000f;
      seed = 1103515245 * seed + 12345;
      dir.y = (seed%10000)*oneOver10000f;
      seed = 1103515245 * seed + 12345;
      dir.z = (seed%10000)*oneOver10000f;
    
      /* initialize shadow ray */
      RTCRay shadow;
      shadow.org = hitPos;
      shadow.dir = dir;
      shadow.tnear = 0.001f;
      shadow.tfar = inf;
      shadow.geomID = RTC_INVALID_GEOMETRY_ID;
      shadow.primID = RTC_INVALID_GEOMETRY_ID;
      shadow.mask = -1;
      shadow.time = 0;
    
      /* trace shadow ray */
      rtcOccluded(g_scene,shadow);
    
      /* add light contribution */
      if (shadow.geomID == RTC_INVALID_GEOMETRY_ID)
        intensity += 1.0f;   
     }
  intensity *= 1.0f/AMBIENT_OCCLUSION_SAMPLES;

  /* shade pixel */
  return col * intensity;
}

/* returns the point seen through specified pixel */
extern "C" bool device_pick(const float x,
                            const float y, 
                            const Vec3fa& vx, 
                            const Vec3fa& vy, 
                            const Vec3fa& vz, 
                            const Vec3fa& p,
                            Vec3fa& hitPos)
{
  /* initialize ray */
  RTCRay ray;
  ray.org = p;
  ray.dir = normalize(x*vx + y*vy + vz);
  ray.tnear = 0.0f;
  ray.tfar = inf;
  ray.geomID = RTC_INVALID_GEOMETRY_ID;
  ray.primID = RTC_INVALID_GEOMETRY_ID;
  ray.mask = -1;
  ray.time = g_debug;

  /* intersect ray with scene */
  rtcIntersect(g_scene,ray);
  PRINT(ray.geomID);
  PRINT(ray.primID);

  /* shade pixel */
  if (ray.geomID == RTC_INVALID_GEOMETRY_ID) {
    hitPos = Vec3fa(0.0f,0.0f,0.0f);
    return false;
  }
  else {
    hitPos = ray.org + ray.tfar*ray.dir;
    return true;
  }
}

/* called when a key is pressed */
extern "C" void device_key_pressed(int key)
{
  if (key == GLUT_KEY_F1) {
    renderPixel = renderPixelStandard;
    g_changed = true;
  }
  else if (key == GLUT_KEY_F2) {
    renderPixel = renderPixelEyeLight;
    g_changed = true;
  }    
  else if (key == GLUT_KEY_F3) {
    renderPixel = renderPixelWireframe;
    g_changed = true;
  }
  else if (key == GLUT_KEY_F4) {
    renderPixel = renderPixelUV;
    g_changed = true;
  }
  else if (key == GLUT_KEY_F5) {
    renderPixel = renderPixelNg;
    g_changed = true;
  }
  else if (key == GLUT_KEY_F6) {
    renderPixel = renderPixelGeomID;
    g_changed = true;
  }
  else if (key == GLUT_KEY_F7) {
    renderPixel = renderPixelGeomIDPrimID;
    g_changed = true;
  }
  else if (key == GLUT_KEY_F8) {
    renderPixel = renderPixelUV16;
    g_changed = true;
  }
  else if (key == GLUT_KEY_F9) {
    if (renderPixel == renderPixelCycles) scale *= 2.0f;
    PRINT(scale);
    renderPixel = renderPixelCycles;
    g_changed = true;
  }
  else if (key == GLUT_KEY_F10) {
    if (renderPixel == renderPixelCycles) scale *= 0.5f;
    PRINT(scale);
    renderPixel = renderPixelCycles;
    g_changed = true;
  }
  else if (key == GLUT_KEY_F11) {
    renderPixel = renderPixelAmbientOcclusion;
    g_changed = true;
  }
  else if (key == GLUT_KEY_F12) {
    g_changed = true;
  }

}

void renderTile(int taskIndex,
                int* pixels,
                const int width,
                const int height, 
                const float time,
                const Vec3fa& vx, 
                const Vec3fa& vy, 
                const Vec3fa& vz, 
                const Vec3fa& p,
                const int numTilesX, 
                const int numTilesY);

void launch_renderTile (int numTiles, 
                        int* pixels, const int width, const int height, const float time, 
                        const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p, const int numTilesX, const int numTilesY)
{
#if 1
  atomic_t tileID = 0;
  parallel_for(size_t(0),size_t(32),[&] (const range<size_t>& r) {
      for (size_t tid=r.begin(); tid<r.end(); tid++) {<commit_msg>bugfix in rendering task scheduler for tutorials, number of threads were limited to 32<commit_after>// ======================================================================== //
// Copyright 2009-2015 Intel Corporation                                    //
//                                                                          //
// Licensed under the Apache License, Version 2.0 (the "License");          //
// you may not use this file except in compliance with the License.         //
// You may obtain a copy of the License at                                  //
//                                                                          //
//     http://www.apache.org/licenses/LICENSE-2.0                           //
//                                                                          //
// Unless required by applicable law or agreed to in writing, software      //
// distributed under the License is distributed on an "AS IS" BASIS,        //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and      //
// limitations under the License.                                           //
// ======================================================================== //

#include "tutorial_device.h"
#include "kernels/algorithms/parallel_for.h"
#include "sys/sysinfo.h"
#include "scene_device.h"

#if defined(USE_PTEX)
#include "Ptexture.h"
#endif

/* the scene to render */
extern RTCScene g_scene;

/* global subdivision level for subdivision geometry */
unsigned int g_subdivision_levels = 0;

/* intensity scaling for traversal cost visualization */
//float scale = 0.001f;
float scale = 1.0f / 1000000.0f;

extern "C" bool g_changed = false;

/* stores pointer to currently used rendePixel function */
extern renderPixelFunc renderPixel;

extern float g_debug;

/* standard rendering function for each tutorial */
Vec3fa renderPixelStandard(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p);

/* renders a single pixel with eyelight shading */
Vec3fa renderPixelEyeLight(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p)
{
  /* initialize ray */
  RTCRay ray;
  ray.org = p;
  ray.dir = normalize(x*vx + y*vy + vz);
  ray.tnear = 0.0f;
  ray.tfar = inf;
  ray.geomID = RTC_INVALID_GEOMETRY_ID;
  ray.primID = RTC_INVALID_GEOMETRY_ID;
  ray.mask = -1;
  ray.time = g_debug;

  /* intersect ray with scene */
  rtcIntersect(g_scene,ray);

  /* shade pixel */
  if (ray.geomID == RTC_INVALID_GEOMETRY_ID) return Vec3fa(0.0f);
  else return Vec3fa(embree::abs(dot(ray.dir,normalize(ray.Ng))));
}

__noinline void setray(RTCRay& ray)
{
  ray.u = ray.v = 0.001f;
  ray.Ng = Vec3fa(0,1,0);
  ray.geomID = 0;
  ray.primID = 0;
}

/* renders a single pixel with wireframe shading */
Vec3fa renderPixelWireframe(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p)
{
  /* initialize ray */
  RTCRay ray;
  ray.org = p;
  ray.dir = normalize(x*vx + y*vy + vz);
  ray.tnear = 0.0f;
  ray.tfar = inf;
  ray.geomID = RTC_INVALID_GEOMETRY_ID;
  ray.primID = RTC_INVALID_GEOMETRY_ID;
  ray.mask = -1;
  ray.time = g_debug;

  /* intersect ray with scene */
  rtcIntersect(g_scene,ray);

  /* return black if nothing hit */
  if (ray.geomID == RTC_INVALID_GEOMETRY_ID) return Vec3fa(1.0f);

  /* calculate wireframe around triangles */
  const float border = 0.05f;
  Vec3fa color = Vec3fa(1.0f);
  if (ray.u < border) color = Vec3fa(0.0f);
  if (ray.v < border) color = Vec3fa(0.0f);
  if (1.0f-ray.u-ray.v < border) color = Vec3fa(0.0f);

  /* perform eyelight shading */
  return color*Vec3fa(embree::abs(dot(ray.dir,normalize(ray.Ng))));
}

/* renders a single pixel with UV shading */
Vec3fa renderPixelUV(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p)
{
  /* initialize ray */
  RTCRay ray;
  ray.org = p;
  ray.dir = normalize(x*vx + y*vy + vz);
  ray.tnear = 0.0f;
  ray.tfar = inf;
  ray.geomID = RTC_INVALID_GEOMETRY_ID;
  ray.primID = RTC_INVALID_GEOMETRY_ID;
  ray.mask = -1;
  ray.time = g_debug;

  /* intersect ray with scene */
  rtcIntersect(g_scene,ray);

  /* shade pixel */
  if (ray.geomID == RTC_INVALID_GEOMETRY_ID) return Vec3fa(0.0f);
  else return Vec3fa(ray.u,ray.v,1.0f-ray.u-ray.v);
}

/* renders a single pixel with geometry normal shading */
Vec3fa renderPixelNg(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p)
{
  /* initialize ray */
  RTCRay ray;
  ray.org = p;
  ray.dir = normalize(x*vx + y*vy + vz);
  ray.tnear = 0.0f;
  ray.tfar = inf;
  ray.geomID = RTC_INVALID_GEOMETRY_ID;
  ray.primID = RTC_INVALID_GEOMETRY_ID;
  ray.mask = -1;
  ray.time = g_debug;

  /* intersect ray with scene */
  rtcIntersect(g_scene,ray);

  /* shade pixel */
  if (ray.geomID == RTC_INVALID_GEOMETRY_ID) return Vec3fa(0.0f);
  else {
    //if (dot(ray.dir,ray.Ng) > 0.0f) return Vec3fa(zero); else
    return abs(normalize(Vec3fa(ray.Ng.x,ray.Ng.y,ray.Ng.z)));
  }
}

Vec3fa randomColor(const int ID) 
{
  int r = ((ID+13)*17*23) & 255;
  int g = ((ID+15)*11*13) & 255;
  int b = ((ID+17)* 7*19) & 255;
  const float oneOver255f = 1.f/255.f;
  return Vec3fa(r*oneOver255f,g*oneOver255f,b*oneOver255f);
}

/* geometry ID shading */
Vec3fa renderPixelGeomID(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p)
{
  /* initialize ray */
  RTCRay ray;
  ray.org = p;
  ray.dir = normalize(x*vx + y*vy + vz);
  ray.tnear = 0.0f;
  ray.tfar = inf;
  ray.geomID = RTC_INVALID_GEOMETRY_ID;
  ray.primID = RTC_INVALID_GEOMETRY_ID;
  ray.mask = -1;
  ray.time = g_debug;

  /* intersect ray with scene */
  rtcIntersect(g_scene,ray);

  /* shade pixel */
  if (ray.geomID == RTC_INVALID_GEOMETRY_ID) return Vec3fa(0.0f);
  else return randomColor(ray.geomID);
}

/* geometry ID and primitive ID shading */
Vec3fa renderPixelGeomIDPrimID(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p)
{
  /* initialize ray */
  RTCRay ray;
  ray.org = p;
  ray.dir = normalize(x*vx + y*vy + vz);
  ray.tnear = 0.0f;
  ray.tfar = inf;
  ray.geomID = RTC_INVALID_GEOMETRY_ID;
  ray.primID = RTC_INVALID_GEOMETRY_ID;
  ray.mask = -1;
  ray.time = g_debug;

  /* intersect ray with scene */
  rtcIntersect(g_scene,ray);

  /* shade pixel */
  if (ray.geomID == RTC_INVALID_GEOMETRY_ID) return Vec3fa(0.0f);
  else return randomColor(ray.geomID ^ ray.primID);
}

/* vizualizes the traversal cost of a pixel */
Vec3fa renderPixelCycles(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p)
{
  /* initialize ray */
  RTCRay ray;
  ray.org = p;
  ray.dir = normalize(x*vx + y*vy + vz);
  ray.tnear = 0.0f;
  ray.tfar = inf;
  ray.geomID = RTC_INVALID_GEOMETRY_ID;
  ray.primID = RTC_INVALID_GEOMETRY_ID;
  ray.mask = -1;
  ray.time = g_debug;

  /* intersect ray with scene */
  int64 c0 = get_tsc();
  rtcIntersect(g_scene,ray);
  int64 c1 = get_tsc();
  /* shade pixel */
  return Vec3fa((float)(c1-c0)*scale,0.0f,0.0f);
}

/* renders a single pixel with UV shading */
Vec3fa renderPixelUV16(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p)
{
  /* initialize ray */
  RTCRay ray;
  ray.org = p;
  ray.dir = normalize(x*vx + y*vy + vz);
  ray.tnear = 0.0f;
  ray.tfar = inf;
  ray.geomID = RTC_INVALID_GEOMETRY_ID;
  ray.primID = RTC_INVALID_GEOMETRY_ID;
  ray.mask = -1;
  ray.time = g_debug;

  /* intersect ray with scene */
  for (int i=0; i<16; i++) {
    ray.tfar = inf;
    rtcIntersect(g_scene,ray);
  }

  /* shade pixel */
  if (ray.geomID == RTC_INVALID_GEOMETRY_ID) return Vec3fa(0.0f);
  else return Vec3fa(ray.u,ray.v,1.0f-ray.u-ray.v);
}

/* renders a single pixel casting with ambient occlusion */
Vec3fa renderPixelAmbientOcclusion(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p)
{
  /* initialize ray */
  RTCRay ray;
  ray.org = p;
  ray.dir = normalize(x*vx + y*vy + vz);
  ray.tnear = 0.0f;
  ray.tfar = inf;
  ray.geomID = RTC_INVALID_GEOMETRY_ID;
  ray.primID = RTC_INVALID_GEOMETRY_ID;
  ray.mask = -1;
  ray.time = g_debug;

  /* intersect ray with scene */
  rtcIntersect(g_scene,ray);

  /* shade pixel */
  if (ray.geomID == RTC_INVALID_GEOMETRY_ID) return Vec3fa(0.0f);

  Vec3fa Ng = normalize(ray.Ng);
  Vec3fa col = Vec3fa(min(1.f,.3f+.8f*abs(dot(Ng,normalize(ray.dir)))));

  /* calculate hit point */
  float intensity = 0;
  Vec3fa hitPos = ray.org + ray.tfar * ray.dir;

#define AMBIENT_OCCLUSION_SAMPLES 16
  /* trace some ambient occlusion rays */
  int seed = 34*x+12*y;
  for (int i=0; i<AMBIENT_OCCLUSION_SAMPLES; i++) 
     {
      Vec3fa dir; 
      const float oneOver10000f = 1.f/10000.f;
      seed = 1103515245 * seed + 12345;
      dir.x = (seed%10000)*oneOver10000f;
      seed = 1103515245 * seed + 12345;
      dir.y = (seed%10000)*oneOver10000f;
      seed = 1103515245 * seed + 12345;
      dir.z = (seed%10000)*oneOver10000f;
    
      /* initialize shadow ray */
      RTCRay shadow;
      shadow.org = hitPos;
      shadow.dir = dir;
      shadow.tnear = 0.001f;
      shadow.tfar = inf;
      shadow.geomID = RTC_INVALID_GEOMETRY_ID;
      shadow.primID = RTC_INVALID_GEOMETRY_ID;
      shadow.mask = -1;
      shadow.time = 0;
    
      /* trace shadow ray */
      rtcOccluded(g_scene,shadow);
    
      /* add light contribution */
      if (shadow.geomID == RTC_INVALID_GEOMETRY_ID)
        intensity += 1.0f;   
     }
  intensity *= 1.0f/AMBIENT_OCCLUSION_SAMPLES;

  /* shade pixel */
  return col * intensity;
}

/* returns the point seen through specified pixel */
extern "C" bool device_pick(const float x,
                            const float y, 
                            const Vec3fa& vx, 
                            const Vec3fa& vy, 
                            const Vec3fa& vz, 
                            const Vec3fa& p,
                            Vec3fa& hitPos)
{
  /* initialize ray */
  RTCRay ray;
  ray.org = p;
  ray.dir = normalize(x*vx + y*vy + vz);
  ray.tnear = 0.0f;
  ray.tfar = inf;
  ray.geomID = RTC_INVALID_GEOMETRY_ID;
  ray.primID = RTC_INVALID_GEOMETRY_ID;
  ray.mask = -1;
  ray.time = g_debug;

  /* intersect ray with scene */
  rtcIntersect(g_scene,ray);
  PRINT(ray.geomID);
  PRINT(ray.primID);

  /* shade pixel */
  if (ray.geomID == RTC_INVALID_GEOMETRY_ID) {
    hitPos = Vec3fa(0.0f,0.0f,0.0f);
    return false;
  }
  else {
    hitPos = ray.org + ray.tfar*ray.dir;
    return true;
  }
}

/* called when a key is pressed */
extern "C" void device_key_pressed(int key)
{
  if (key == GLUT_KEY_F1) {
    renderPixel = renderPixelStandard;
    g_changed = true;
  }
  else if (key == GLUT_KEY_F2) {
    renderPixel = renderPixelEyeLight;
    g_changed = true;
  }    
  else if (key == GLUT_KEY_F3) {
    renderPixel = renderPixelWireframe;
    g_changed = true;
  }
  else if (key == GLUT_KEY_F4) {
    renderPixel = renderPixelUV;
    g_changed = true;
  }
  else if (key == GLUT_KEY_F5) {
    renderPixel = renderPixelNg;
    g_changed = true;
  }
  else if (key == GLUT_KEY_F6) {
    renderPixel = renderPixelGeomID;
    g_changed = true;
  }
  else if (key == GLUT_KEY_F7) {
    renderPixel = renderPixelGeomIDPrimID;
    g_changed = true;
  }
  else if (key == GLUT_KEY_F8) {
    renderPixel = renderPixelUV16;
    g_changed = true;
  }
  else if (key == GLUT_KEY_F9) {
    if (renderPixel == renderPixelCycles) scale *= 2.0f;
    PRINT(scale);
    renderPixel = renderPixelCycles;
    g_changed = true;
  }
  else if (key == GLUT_KEY_F10) {
    if (renderPixel == renderPixelCycles) scale *= 0.5f;
    PRINT(scale);
    renderPixel = renderPixelCycles;
    g_changed = true;
  }
  else if (key == GLUT_KEY_F11) {
    renderPixel = renderPixelAmbientOcclusion;
    g_changed = true;
  }
  else if (key == GLUT_KEY_F12) {
    g_changed = true;
  }

}

void renderTile(int taskIndex,
                int* pixels,
                const int width,
                const int height, 
                const float time,
                const Vec3fa& vx, 
                const Vec3fa& vy, 
                const Vec3fa& vz, 
                const Vec3fa& p,
                const int numTilesX, 
                const int numTilesY);

void launch_renderTile (int numTiles, 
                        int* pixels, const int width, const int height, const float time, 
                        const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p, const int numTilesX, const int numTilesY)
{
#if 1
  atomic_t tileID = 0;
  parallel_for(size_t(0),size_t(getNumberOfLogicalThreads()),[&] (const range<size_t>& r) {
      for (size_t tid=r.begin(); tid<r.end(); tid++) {
{'fraction_non_alphanumeric': 0.13425271098538424, 'fraction_numerical': 0.030724501021530723, 'mean_word_length': 2.577874613438291, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2550063', 'n_tokens_mistral': 10071, 'n_tokens_neox': 9380, 'n_words': 2327}