Exemple #1
0
        public static void InitContext(Context context)
        {

            var pbDesc = new BufferDesc()
            {
                Format = Format.Int,
                Width = (ulong)primes.Length,
                Type = BufferType.Input
            };
            var bp = new Buffer(context, pbDesc);
            var ps = bp.Map();
            for (int i = 0; i < primes.Length; i++)
            {
                ps.Write(primes[i]);
            }
            bp.Unmap();
            context["primes"].Set(bp);
        }
        private void CreateLights(IEnumerable<TriangleLight> lights)
        {

            BufferDesc desc = new BufferDesc() { Width = (uint)(lights.Count()), Format = Format.User, Type = BufferType.Input, ElemSize = (uint)Marshal.SizeOf(typeof(TriangleLight)) };
            OptixDotNet.Buffer lightBuffer = new OptixDotNet.Buffer(Context, desc);
            BufferStream stream = lightBuffer.Map();
            {
                foreach (var triangleLight in lights)
                {
                    triangleLight.normal.Normalize();
                    stream.Write(triangleLight);
                }
            }
            lightBuffer.Unmap();

            Context["lights"].Set(lightBuffer);
        }
Exemple #3
0
 protected uint[] GetImageUint(Buffer buffer)
 {
     var bufferStream = buffer.Map();
     uint[] data = new uint[bufferStream.Length / 4];
     int index = 0;
     while (bufferStream.CanRead && index <= data.Length - 1)
     {
         data[index++] = bufferStream.Read<uint>();
     }
     buffer.Unmap();
     return data;
 }
Exemple #4
0
 protected Vector4[] GetImage4(Buffer buffer)
 {
     var bufferStream = buffer.Map();
     Vector4[] data = new Vector4[bufferStream.Length / 16];
     int index = 0;
     while (bufferStream.CanRead && index <= data.Length - 1)
     {
         data[index++] = bufferStream.Read<Vector4>();
     }
     buffer.Unmap();
     return data;
 }
Exemple #5
0
        public static void InitNoise(Context context)
        {
            uint tex_width = 64;
            uint tex_height = 64;
            uint tex_depth = 64;
            var texBuf = new Buffer(context, new BufferDesc() { Width = tex_width, Depth = tex_depth, Height = tex_height, Format = Format.Float, Type = BufferType.Input });
            var ts = texBuf.Map();
            var rnd = new Random();
            float[] b = new float[tex_depth * tex_height * tex_width];
            Voronoi_repeat(16, (int)tex_width, (int)tex_height, (int)tex_depth, b);
            for (int i = 0; i < tex_depth * tex_height * tex_width; i++)
            {
                ts.Write(b[i]);
                //((float)rnd.NextDouble());
            }
            texBuf.Unmap();
            var noiseSampler = new TextureSampler(context,
                new TextureSamplerDesc()

                {
                    Filter = new FilterMinMagMip() { Mag = FilterMode.Linear, Min = FilterMode.Linear, Mip = FilterMode.None },
                    Wrap = new WrapUVW() { WrapU = WrapMode.Repeat, WrapV = WrapMode.Repeat },
                    Index = TextureIndexMode.NormalizeCoords,
                    Read = TextureReadMode.NormalizedFloat,
                    MaxAnisotropy = 1.0f,
                    MipLevels = 1
                }
            );
            noiseSampler.SetBuffer(texBuf, 0);
            context["noise_texture"].Set(noiseSampler);
            context["origin"].SetFloat3(new Vector3(0, 0, 1));
            context["diameter"].Set(0.85f);
            context["turbulence"].Set(3.5f);

            /*
              int tex_width  = 64;
              int tex_height = 64;
              int tex_depth  = 64;
              Buffer noiseBuffer = m_context->createBuffer(RT_BUFFER_INPUT, RT_FORMAT_FLOAT, tex_width, tex_height, tex_depth);
              float *tex_data = (float *) noiseBuffer->map();

              if (m_parameters.proc == PROCEDURE_VORONOI)
              {
            // Distances to Voronoi control points (repeated version, taking the 26 surrounding cubes into account!)
            Voronoi_repeat(16, tex_width, tex_height, tex_depth, tex_data);
              }
              else
              {
            // Random noise in range [0, 1]
            for (int i = tex_width * tex_height * tex_depth;  i > 0; i--)
            {
              // One channel 3D noise in [0.0, 1.0] range.
              *tex_data++ = rand_range(0.0f, 1.0f);
            }
              }
              noiseBuffer->unmap();

              // Noise texture sampler
              TextureSampler noiseSampler = m_context->createTextureSampler();

              noiseSampler->setWrapMode(0, RT_WRAP_REPEAT);
              noiseSampler->setWrapMode(1, RT_WRAP_REPEAT);
              noiseSampler->setFilteringModes(RT_FILTER_LINEAR, RT_FILTER_LINEAR, RT_FILTER_NONE);
              noiseSampler->setIndexingMode(RT_TEXTURE_INDEX_NORMALIZED_COORDINATES);
              noiseSampler->setReadMode(RT_TEXTURE_READ_NORMALIZED_FLOAT);
              noiseSampler->setMaxAnisotropy(1.0f);
              noiseSampler->setMipLevelCount(1);
              noiseSampler->setArraySize(1);
              noiseSampler->setBuffer(0, 0, noiseBuffer);

              m_context["noise_texture"]->setTextureSampler(noiseSampler);

              // 1D color ramp buffer, 4 float channels, all entries in the range [0.0, 1.0].
              Buffer colorRampBuffer = m_context->createBuffer(RT_BUFFER_INPUT, RT_FORMAT_FLOAT4, 1024);
              tex_data = (float *) colorRampBuffer->map();
              create_color_ramp(m_parameters.ramp, 1024, tex_data);
              colorRampBuffer->unmap();

             */
        }
        private void BuildLights(SceneLightsource[] sceneLights)
        {
            var lightInfos = new List<LightInfo>();
            foreach (var sceneLightsource in sceneLights)
            {
                var lightInfo = sceneLightsource.CreateInfo();
                if (lightInfo.type == 5)
                {
                    EnvMapPath = sceneLightsource.Get("EnvMap.Image", string.Empty);
                }
                if (lightInfo.type != 2)
                {
                    lightInfos.Add(lightInfo);
                }
                else
                {
                    var li = ((Dictionary<string, List<int>>)BuildCache["LightTrianglesIndexes"])[((SceneMeshEntity)sceneLightsource["AreaLight.Mesh"]).Name];
                    lightInfos.Add(new LightInfo()
                    {
                        type = 2,
                        emission = sceneLightsource.Color,
                        startTriangle = li.Min(),
                        endTriangle = li.Max(),
                    });
                }
            }

            var triangleInfos = ((List<TriangleLight>)BuildCache["LightTriangles"]).Select(tr => new LightTriangleInfo()
            {
                v0 = tr.v1,
                v1 = tr.v2,
                v2 = tr.v3,
                n = tr.normal,
                area = CalcTriangleArea(tr)
            }).ToList();

            //TODO Index from 1!!! First triangle is fake
            triangleInfos.Insert(0, new LightTriangleInfo()
            {
                area = 1,
                n = new Vector3(1, 1, 1),
                v1 = new Vector3(100, 100, 100),
                v2 = new Vector3(101, 101, 101),
                v0 = new Vector3(102, 102, 102),
            });

            var triBufferDesc = new BufferDesc
            {
                Width = (uint)triangleInfos.Count,
                Format = Format.User,
                Type = BufferType.Input,
                ElemSize = (uint)Marshal.SizeOf(typeof(LightTriangleInfo))
            };
            var triangleBuffer = new Buffer(Session.OptixContext, triBufferDesc);
            var stream = triangleBuffer.Map();
            stream.WriteRange(triangleInfos.ToArray());
            triangleBuffer.Unmap();

            if (!string.IsNullOrWhiteSpace(EnvMapPath))
            {
                Tracer.Print("BuildLights", "Loading environment map " + EnvMapPath);
                var sceneEnvMapPath = EnvMapPath;
                var envmap = TextureCache.Instance.Load(Session.OptixContext, sceneEnvMapPath); //KC_outside_hi.hdr
                var id = envmap.GetId();
                Session.OptixContext["envmapId"].Set((uint)id);
                Session.OptixContext["use_envmap"].Set(1);

                var elt = lightInfos.Find(lt => lt.type == 5);
                var index = lightInfos.IndexOf(elt);
                lightInfos[index] = new LightInfo
                {
                    type = 5,
                    samplerId = id,
                    emission = elt.emission
                };
            }
            else
            {
                Session.OptixContext["use_envmap"].Set(0);
            }

            var ltBufferDesc = new BufferDesc
            {
                Width = (uint)lightInfos.Count,
                Format = Format.User,
                Type = BufferType.Input,
                ElemSize = (uint)Marshal.SizeOf(typeof(LightInfo))
            };
            var lightBuffer = new OptixDotNet.Buffer(Session.OptixContext, ltBufferDesc);
            var ltStream = lightBuffer.Map();
            //LightsCount.LValue += lts.Count;

            foreach (var lightinfo in lightInfos)
            {
                ltStream.Write(lightinfo);
            }
            lightBuffer.Unmap();

            Session.Frame.LightTrianglesBuffer = triangleBuffer;
            Session.Frame.LightBuffer = lightBuffer;
        }