Exemple #1
0
        protected override void Initialize()
        {
            base.Initialize();
            var modelName = "sibenik.obj";
            var modelPath = Path.GetFullPath(@"..\..\..\..\..\Assets\Models\" + modelName);

            OptixContext = new Context();
            mTraversal   = new TraversalEngine(OptixContext, QueryType.ClosestHit, RayFormat.OriginDirectionInterleaved, TriFormat.Mesh,
                                               TraversalOutput.Normal, InitOptions.None);
            mTraversal.SetCpuThreadCount(1);
            //mTraversal.NumCpuThreads = Context.CpuNumThreads;


            var model = new OptixOBJLoader(modelPath, OptixContext, null, null, n => null);

            //OBJLoader normally automatically creates Geometry, GeometryInstances, and GeometryGroups for Optix
            //but the Traversal API doesn't use that, so turn that off
            model.ParseMaterials   = false;
            model.ParseNormals     = false;
            model.GenerateNormals  = false;
            model.GenerateGeometry = false;
            model.LoadContent();
            SetCamera(model.BBox);

            //we copy the data here or else the GC would clean up a model.Vertices.ToArray() for example
            var verts = new Vector3[model.Positions.Count];
            var tris  = new Int3[model.Groups[0].VIndices.Count];

            model.Positions.CopyTo(verts);
            model.Groups[0].VIndices.CopyTo(tris);
            var indexes = tris.SelectMany(c => new[] { c.X, c.Y, c.Z }).ToArray();

            mTraversal.SetMesh(verts, indexes);

            int numRays = Width * Height;
            var rays    = CreateRays();

            mTraversal.SetRayData <TraversalEngine.Ray>(rays);

            mTraversal.Preprocess();
            mDepths  = new float[numRays];
            mNormals = new Vector3[numRays];

            RaysTracedPerFrame = numRays;

            Console.WriteLine("Accel data size " + mTraversal.AccelDataSize);
            using (var accData = mTraversal.AccelData)
            {
                var size   = accData.Length;
                var buffer = new byte[size];
                accData.ReadRange(buffer, 0, (int)size);
                var nonZeros = buffer.Count(b => b != 0);
                Console.WriteLine("Non zeros in acc data " + nonZeros);
            }
        }
        protected override void Initialize()
        {
            base.Initialize();
            rayGenPath = GetScript("vpt.cu.ptx");
            shaderPath = GetScript("vpt.cu.ptx");
            var modelName = "cornell-dragon.obj";
            var modelPath = Path.GetFullPath(@"..\Assets\Models\" + modelName);

            /*-----------------------------------------------
             * Create the Optix context
             *-----------------------------------------------*/
            OptixContext = new Context();
            OptixContext.RayTypeCount        = 2;
            OptixContext.EntryPointCount     = 1;
            OptixContext.EnableAllExceptions = false;

            /*-----------------------------------------------
             * Create the material that will be executed when there is an intersection
             *-----------------------------------------------*/
            var material = new Material(OptixContext);

            material.Programs[0] = new SurfaceProgram(OptixContext, RayHitType.Closest, shaderPath, "diffuse");
            material.Programs[1] = new SurfaceProgram(OptixContext, RayHitType.Any, shaderPath, "shadow");

            /*-----------------------------------------------
             * Load the geometry
             *-----------------------------------------------*/
            var model = new OptixOBJLoader(modelPath, OptixContext, null, material, n => null);

            model.GeoGroup        = new GeometryGroup(OptixContext);
            model.ParseNormals    = false;
            model.GenerateNormals = false;

            string intersectPath = GetScript("triangle_mesh.cu.ptx");

            model.IntersecitonProgPath = intersectPath;
            model.BoundingBoxProgPath  = intersectPath;
            model.IntersecitonProgName = "mesh_intersect";
            model.BoundingBoxProgName  = "mesh_bounds";

            model.LoadContent(this.resolveMaterial);

            /*-----------------------------------------------
             * Create scene lights
             *-----------------------------------------------*/
            CreateLights();

            /*-----------------------------------------------
             * Create the output buffer
             *-----------------------------------------------*/
            CreateOutputBuffer(Format.Float4);

            /*-----------------------------------------------
             * Create the ray-generation and exception programs
             *-----------------------------------------------*/
            var rayGen    = new OptixProgram(OptixContext, rayGenPath, "pathtrace_camera");
            var exception = new OptixProgram(OptixContext, rayGenPath, "exception");
            var miss      = new OptixProgram(OptixContext, shaderPath, "miss");

            miss["bg_color"].Set(100 / 255.0f, 149 / 255.0f, 237 / 255.0f);

            OptixContext.SetRayGenerationProgram(0, rayGen);
            OptixContext.SetExceptionProgram(0, exception);
            OptixContext.SetRayMissProgram(0, miss);

            /*-----------------------------------------------
             * Finally compile the optix context, and build the accel tree
             *-----------------------------------------------*/
            SetCamera(model.BBox);


            OptixContext["top_object"].Set(model.GeoGroup);
            OptixContext["output_buffer"].Set(OutputBuffer);
            OptixContext["scene_epsilon"].Set(0.0003f);

            OptixContext["rr_begin_depth"].Set(rrBeginDepth);
            OptixContext["max_depth"].Set(maxDepth);
            OptixContext["sqrt_num_samples"].Set(sqrtSamples);
            OptixContext["frame_number"].Set(mFrame);

            OptixContext["pathtrace_ray_type"].Set(0u);
            OptixContext["pathtrace_shadow_ray_type"].Set(1u);

            OptixContext["bad_color"].Set(1.0f, 0.0f, 0.0f);



            Trace.Write("Compiling Optix... ");

            OptixContext.Compile();
            OptixContext.BuildAccelTree();

            //give aproximate here becuase we'll have somewhere between rrBeginDepth and maxDepth number of iterations per sample.
            var avgIteration = (int)(rrBeginDepth + maxDepth) / 2;

            RaysTracedPerFrame = Width * Height * 2 * ((int)sqrtSamples * (int)sqrtSamples) * avgIteration;
        }