// ======================================================================== // // Copyright 2009-2014 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 "../common/tutorial/tutorial_device.h" #include "../common/tutorial/scene_device.h" /* scene data */ extern "C" ISPCScene* g_ispc_scene; RTCScene g_scene = NULL; /* render function to use */ renderPixelFunc renderPixel; /* error reporting function */ void error_handler(const RTCError code, const int8* str) { printf("Embree: "); switch (code) { case RTC_UNKNOWN_ERROR : printf("RTC_UNKNOWN_ERROR"); break; case RTC_INVALID_ARGUMENT : printf("RTC_INVALID_ARGUMENT"); break; case RTC_INVALID_OPERATION: printf("RTC_INVALID_OPERATION"); break; case RTC_OUT_OF_MEMORY : printf("RTC_OUT_OF_MEMORY"); break; case RTC_UNSUPPORTED_CPU : printf("RTC_UNSUPPORTED_CPU"); break; default : printf("invalid error code"); break; } if (str) { printf(" ("); while (*str) putchar(*str++); printf(")\n"); } abort(); } Vec3fa renderPixelEyeLight(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p); /* called by the C++ code for initialization */ extern "C" void device_init (int8* cfg) { /* initialize ray tracing core */ rtcInit(cfg); /* set error handler */ rtcSetErrorFunction(error_handler); /* set start render mode */ renderPixel = renderPixelStandard; //renderPixel = renderPixelEyeLight; } RTCScene convertScene(ISPCScene* scene_in) { /* create scene */ RTCScene scene_out = rtcNewScene(RTC_SCENE_STATIC,RTC_INTERSECT1); /* add all meshes to the scene */ for (int i=0; inumMeshes; i++) { /* get ith mesh */ ISPCMesh* mesh = scene_in->meshes[i]; /* create a triangle mesh */ unsigned int geometry = rtcNewTriangleMesh (scene_out, RTC_GEOMETRY_STATIC, mesh->numTriangles, mesh->numVertices); /* share vertex buffer */ rtcSetBuffer(scene_out, geometry, RTC_VERTEX_BUFFER, mesh->positions, 0, sizeof(Vec3fa )); rtcSetBuffer(scene_out, geometry, RTC_INDEX_BUFFER, mesh->triangles, 0, sizeof(ISPCTriangle)); } /* commit changes to scene */ return scene_out; } /* task that renders a single screen tile */ Vec3fa renderPixelStandard(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 = 0; /* intersect ray with scene */ rtcIntersect(g_scene,ray); /* shade background black */ if (ray.geomID == RTC_INVALID_GEOMETRY_ID) return Vec3fa(0.0f); /* shade all rays that hit something */ Vec3fa color = Vec3fa(0.0f); Vec3fa Ns = Vec3fa(0.0f); #if 1 // FIXME: pointer gather not implemented on ISPC for Xeon Phi ISPCMesh* mesh = g_ispc_scene->meshes[ray.geomID]; ISPCTriangle* tri = &mesh->triangles[ray.primID]; /* load material ID */ int materialID = tri->materialID; /* interpolate shading normal */ if (mesh->normals) { Vec3fa n0 = Vec3fa(mesh->normals[tri->v0]); Vec3fa n1 = Vec3fa(mesh->normals[tri->v1]); Vec3fa n2 = Vec3fa(mesh->normals[tri->v2]); float u = ray.u, v = ray.v, w = 1.0f-ray.u-ray.v; Ns = normalize(w*n0 + u*n1 + v*n2); } else { Ns = normalize(ray.Ng); } #else int materialID = 0; foreach_unique (geomID in ray.geomID) { ISPCMesh* mesh = g_ispc_scene->meshes[geomID]; foreach_unique (primID in ray.primID) { ISPCTriangle* tri = &mesh->triangles[primID]; /* load material ID */ materialID = tri->materialID; /* interpolate shading normal */ if (mesh->normals) { Vec3fa n0 = Vec3fa(mesh->normals[tri->v0]); Vec3fa n1 = Vec3fa(mesh->normals[tri->v1]); Vec3fa n2 = Vec3fa(mesh->normals[tri->v2]); float u = ray.u, v = ray.v, w = 1.0f-ray.u-ray.v; Ns = w*n0 + u*n1 + v*n2; } else { Ns = normalize(ray.Ng); } } } Ns = normalize(Ns); #endif OBJMaterial* material = (OBJMaterial*) &g_ispc_scene->materials[materialID]; color = Vec3fa(material->Kd); /* apply ambient light */ Vec3fa Nf = faceforward(Ns,neg(ray.dir),Ns); //Vec3fa Ng = normalize(ray.Ng); //Vec3fa Nf = dot(ray.dir,Ng) < 0.0f ? Ng : neg(Ng); color = color*dot(ray.dir,Nf); // FIXME: *= return color; } /* task that renders a single screen tile */ 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) { const int tileY = taskIndex / numTilesX; const int tileX = taskIndex - tileY * numTilesX; const int x0 = tileX * TILE_SIZE_X; const int x1 = min(x0+TILE_SIZE_X,width); const int y0 = tileY * TILE_SIZE_Y; const int y1 = min(y0+TILE_SIZE_Y,height); for (int y = y0; y