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; }
protected override void Initialize() { string rayGenPath = shaderPath; /*----------------------------------------------- * Create the Optix context *-----------------------------------------------*/ OptixContext = new Context(); OptixContext.RayTypeCount = 2; OptixContext.EntryPointCount = 1; OptixContext.SetStackSize(4096); /* Create the ray-generation and exception programs *-----------------------------------------------*/ var rayGen = new OptixProgram(OptixContext, rayGenPath, mTutorial < 11 ? "pinhole_camera" : "env_camera"); var exception = new OptixProgram(OptixContext, rayGenPath, "exception"); var miss = new OptixProgram(OptixContext, rayGenPath, mTutorial < 5 ? "miss" : "envmap_miss"); OptixContext["bg_color"].Set(100 / 255.0f, 149 / 255.0f, 237 / 255.0f); OptixContext["bad_color"].Set(1.0f, 0.0f, 0.0f); OptixContext.SetRayGenerationProgram(0, rayGen); OptixContext.SetExceptionProgram(0, exception); OptixContext.SetRayMissProgram(0, miss); /*----------------------------------------------- * Create lights *-----------------------------------------------*/ BasicLight[] lights = new BasicLight[1]; lights[0].Position = new Vector3(-5.0f, 60.0f, -16.0f); lights[0].Color = new Vector3(1.0f, 1.0f, 1.0f); lights[0].CastsShadow = 1; BufferDesc desc = new BufferDesc() { Width = (uint)lights.Length, Format = Format.User, Type = BufferType.Input, ElemSize = (uint)Marshal.SizeOf(typeof(BasicLight)) }; Buffer lightsBuffer = new Buffer(OptixContext, desc); lightsBuffer.SetData <BasicLight>(lights); OptixContext["lights"].Set(lightsBuffer); /*----------------------------------------------- * Create noise texture *-----------------------------------------------*/ if (mTutorial >= 8) { uint noiseTexDim = 64; desc = new BufferDesc() { Width = noiseTexDim, Height = noiseTexDim, Depth = noiseTexDim, Format = Format.Float, Type = BufferType.Input }; Buffer noiseBuffer = new Buffer(OptixContext, desc); Random rand = new Random(); BufferStream stream = noiseBuffer.Map(); for (int i = 0; i < noiseTexDim * noiseTexDim * noiseTexDim; i++) { stream.Write <float>((float)rand.NextDouble()); } noiseBuffer.Unmap(); TextureSampler noiseTex = new TextureSampler(OptixContext, TextureSamplerDesc.GetDefault(WrapMode.Repeat)); noiseTex.SetBuffer(noiseBuffer); OptixContext["noise_texture"].Set(noiseTex); } /*----------------------------------------------- * Load enivronment map texture *-----------------------------------------------*/ LoadEnvMap(); /*----------------------------------------------- * Load the geometry *-----------------------------------------------*/ CreateGeometry(); /*----------------------------------------------- * Create the output buffer *-----------------------------------------------*/ CreateOutputBuffer(Format.UByte4); OptixContext["output_buffer"].Set(OutputBuffer); /*----------------------------------------------- * Finally compile the optix context, and build the accel tree *-----------------------------------------------*/ SetCamera(); OptixContext["max_depth"].Set(100); OptixContext["radiance_ray_type"].Set(0u); OptixContext["shadow_ray_type"].Set(1u); OptixContext["frame_number"].Set(0u); OptixContext["scene_epsilon"].Set(.001f); OptixContext["importance_cutoff"].Set(0.01f); OptixContext["ambient_light_color"].Set(0.31f, 0.33f, 0.28f); OptixContext.Compile(); OptixContext.BuildAccelTree(); //very loose calculation of number of rays float numSecondaryRays = 0; if (mTutorial >= 9) { numSecondaryRays = 2.5f; //only convex hull casts refraction rays } else if (mTutorial >= 8) { numSecondaryRays = 2; } else if (mTutorial >= 4) { numSecondaryRays = 1.5f; //only the floor casts reflection rays, so give aproximate } else if (mTutorial >= 3) { numSecondaryRays = 1; } RaysTracedPerFrame = (int)(Width * Height * (numSecondaryRays + 1)); }