Esempio n. 1
0
        private static Texture2D Make3D(Texture2D stereoTexture)
        {
            NvStereoImageHeader header = new NvStereoImageHeader(0x4433564e, 3840, 1080, 4, 0x00000002);

            // stereoTexture contains a stereo image with the left eye image on the left half
            // and the right eye image on the right half
            // this staging texture will have an extra row to contain the stereo signature
            Texture2DDescription stagingDesc = new Texture2DDescription()
                                                   {
                                                       ArraySize = 1,
                                                       Width = 2*size.Width,
                                                       Height = size.Height + 1,
                                                       BindFlags = BindFlags.None,
                                                       CpuAccessFlags = CpuAccessFlags.Write,
                                                       Format = SlimDX.DXGI.Format.R8G8B8A8_UNorm,
                                                       OptionFlags = ResourceOptionFlags.None,
                                                       Usage = ResourceUsage.Staging,
                                                       MipLevels = 1,
                                                       SampleDescription = new SampleDescription(1, 0)
                                                   };
            Texture2D staging = new Texture2D(device, stagingDesc);

            // Identify the source texture region to copy (all of it)
            ResourceRegion stereoSrcBox = new ResourceRegion
                                              {Front = 0, Back = 1, Top = 0, Bottom = size.Height, Left = 0, Right = 2*size.Width};
            // Copy it to the staging texture
            device.CopySubresourceRegion(stereoTexture, 0, stereoSrcBox, staging, 0, 0, 0, 0);

            // Open the staging texture for reading
            DataRectangle box = staging.Map(0, MapMode.Write, SlimDX.Direct3D10.MapFlags.None);
            // Go to the last row
            //box.Data.Seek(stereoTexture.Description.Width*stereoTexture.Description.Height*4, System.IO.SeekOrigin.Begin);
            box.Data.Seek(box.Pitch*1080, System.IO.SeekOrigin.Begin);
            // Write the NVSTEREO header
            box.Data.Write(data, 0, data.Length);
            staging.Unmap(0);

            // Create the final stereoized texture
            Texture2DDescription finalDesc = new Texture2DDescription()
                                                 {
                                                     ArraySize = 1,
                                                     Width = 2 * size.Width,
                                                     Height = size.Height + 1,
                                                     BindFlags = BindFlags.ShaderResource,
                                                     CpuAccessFlags = CpuAccessFlags.Write,
                                                     Format = SlimDX.DXGI.Format.R8G8B8A8_UNorm,
                                                     OptionFlags = ResourceOptionFlags.None,
                                                     Usage = ResourceUsage.Dynamic,
                                                     MipLevels = 1,
                                                     SampleDescription = new SampleDescription(1, 0)
                                                 };

            // Copy the staging texture on a new texture to be used as a shader resource
            Texture2D final = new Texture2D(device, finalDesc);
            device.CopyResource(staging, final);
            staging.Dispose();
            return final;
        }
Esempio n. 2
0
        Texture2D Make3D(Texture2D tex)
        {
            NvStereoImageHeader header = new NvStereoImageHeader(0x4433564e, 3840, 1080, 4, 0x00000002);
               // DataBox box = DeviceContext.Immediate.MapSubresource(tex, 0,
               //     tex.Description.Width * tex.Description.Height * 4, MapMode.Read, MapFlags.None);
               // Console.WriteLine(box.RowPitch);
               // int size = tex.Description.Width * tex.Description.Height * 8;
               // byte[] buffer = new byte[size/2];
               // int val = box.Data.ReadRange<byte>(buffer, 0, size/2);
               // DeviceContext.Immediate.UnmapSubresource(tex, 0);
            Texture2DDescription desc = new Texture2DDescription()
                                             {
                                                 ArraySize = 1,
                                                 Width = 3840,
                                                 Height = 1081,
                                                 BindFlags = BindFlags.None,
                                                 CpuAccessFlags = CpuAccessFlags.Write,
                                                 Format = SlimDX.DXGI.Format.R8G8B8A8_UNorm,
                                                 OptionFlags = ResourceOptionFlags.None,
                                                 Usage = ResourceUsage.Staging,
                                                 MipLevels = 1,
                                                 SampleDescription = new SampleDescription(1, 0)
                                             };

            ResourceRegion stereoSrcBox = new ResourceRegion
                                              {Front = 0, Back = 1, Top = 0, Bottom = 1080, Left = 0, Right = 3840};

            Texture2D outp = new Texture2D(Game.Context.Device, desc);

            DeviceContext.Immediate.CopySubresourceRegion(tex, 0, stereoSrcBox, outp, 0, 0, 0, 0);
            //DeviceContext.Immediate.CopyResource(tex, outp);
            DataBox box = DeviceContext.Immediate.MapSubresource(outp, 0,
                outp.Description.Width * outp.Description.Height * 4, MapMode.Write, MapFlags.None);
            //var val = box.Data.ReadByte();
            box.Data.Seek(tex.Description.Width * tex.Description.Height * 4, SeekOrigin.Begin);

            //box.Data.Write(data, 0, data.Length);
            //byte[] color = BitConverter.GetBytes(Color.White.ToArgb());
            //box.Data.Write(color,0, color.Length);
            //box.Data.Write(data, 0, data.Length);
            byte[] headerData = StructureToByteArray(header);
            box.Data.Write(headerData, 0, headerData.Length);
            //box.Data.Write(color, 0, color.Length);
            DeviceContext.Immediate.UnmapSubresource(outp, 0);
              //box.Data.Write(buffer, 0, buffer.Length);

            //DeviceContext.Immediate.CopySubresourceRegion(tex, 0, stereoSrcBox, outp, 0, 0, 0, 0);
            ////return Texture2D.FromMemory(Game.Context.Device, buffer);
            //Texture2D.ToFile(Game.Context.Immediate, outp, ImageFileFormat.Bmp, "prova.bmp");

            Texture2DDescription finalDesc = new Texture2DDescription()
            {
                ArraySize = 1,
                Width = 3840,
                Height = 1081,
                BindFlags = BindFlags.ShaderResource,
                CpuAccessFlags = CpuAccessFlags.Write,
                Format = SlimDX.DXGI.Format.R8G8B8A8_UNorm,
                OptionFlags = ResourceOptionFlags.None,
                Usage = ResourceUsage.Dynamic,
                MipLevels = 1,
                SampleDescription = new SampleDescription(1, 0)
            };

            Texture2D finalT = new Texture2D(DeviceContext.Device, finalDesc);
            DeviceContext.Immediate.CopyResource(outp, finalT);

            //Texture2D.ToFile(DeviceContext.Immediate, finalT, ImageFileFormat.Bmp, "prova.bmp");

            return finalT;
        }