public static void CreateTexture(
     DdsFile dds,
     D3D11Device device,
     D3D11DeviceContext context,
     out D3D11Resource texture,
     out D3D11ShaderResourceView textureView)
 {
     CreateTexture(dds, device, context, 0, out texture, out textureView, out _);
 }
        public static void CreateTexture(
            Stream stream,
            D3D11Device device,
            D3D11DeviceContext context,
            out D3D11Resource texture,
            out D3D11ShaderResourceView textureView)
        {
            DdsFile dds = DdsFile.FromStream(stream);

            CreateTexture(dds, device, context, out texture, out textureView);
        }
        public static void CreateTexture(
            string fileName,
            D3D11Device device,
            D3D11DeviceContext context,
            out D3D11Resource texture,
            out D3D11ShaderResourceView textureView)
        {
            DdsFile dds = DdsFile.FromFile(fileName);

            CreateTexture(dds, device, context, out texture, out textureView);
        }
        public static void CreateTexture(
            DdsFile dds,
            D3D11Device device,
            D3D11DeviceContext context,
            int maxSize,
            D3D11Usage usage,
            D3D11BindOptions bindOptions,
            D3D11CpuAccessOptions cpuAccessOptions,
            D3D11ResourceMiscOptions miscOptions,
            bool forceSRGB,
            out D3D11Resource texture,
            out D3D11ShaderResourceView textureView,
            out DdsAlphaMode alphaMode)
        {
            if (dds == null)
            {
                throw new ArgumentNullException(nameof(dds));
            }

            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            CreateTextureFromDDS(
                device,
                context,
                dds,
                dds.Data,
                maxSize,
                usage,
                bindOptions,
                cpuAccessOptions,
                miscOptions,
                forceSRGB,
                out texture,
                out textureView);

            alphaMode = dds.AlphaMode;
        }
 public static void CreateTexture(
     DdsFile dds,
     D3D11Device device,
     D3D11DeviceContext context,
     int maxSize,
     out D3D11Resource texture,
     out D3D11ShaderResourceView textureView,
     out DdsAlphaMode alphaMode)
 {
     CreateTexture(
         dds,
         device,
         context,
         maxSize,
         D3D11Usage.Default,
         D3D11BindOptions.ShaderResource,
         D3D11CpuAccessOptions.None,
         D3D11ResourceMiscOptions.None,
         false,
         out texture,
         out textureView,
         out alphaMode);
 }
        private static void CreateTextureFromDDS(
            D3D11Device device,
            D3D11DeviceContext context,
            DdsFile dds,
            byte[] bitData,
            int maxSize,
            D3D11Usage usage,
            D3D11BindOptions bindOptions,
            D3D11CpuAccessOptions cpuAccessOptions,
            D3D11ResourceMiscOptions miscOptions,
            bool forceSRGB,
            out D3D11Resource texture,
            out D3D11ShaderResourceView textureView)
        {
            int width  = dds.Width;
            int height = dds.Height;
            int depth  = dds.Depth;

            D3D11ResourceDimension resDim = (D3D11ResourceDimension)dds.ResourceDimension;
            int        arraySize          = Math.Max(1, dds.ArraySize);
            DxgiFormat format             = (DxgiFormat)dds.Format;
            bool       isCubeMap          = false;

            if (dds.Format == DdsFormat.Unknown)
            {
                if (dds.PixelFormat.RgbBitCount == 32)
                {
                    if (IsBitMask(dds.PixelFormat, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000))
                    {
                        format = DxgiFormat.B8G8R8X8UNorm;
                        int length = bitData.Length / 4;
                        var bytes  = new byte[length * 4];

                        for (int i = 0; i < length; i++)
                        {
                            bytes[i * 4 + 0] = bitData[i * 4 + 2];
                            bytes[i * 4 + 1] = bitData[i * 4 + 1];
                            bytes[i * 4 + 2] = bitData[i * 4 + 0];
                        }

                        bitData = bytes;
                    }
                }
                else if (dds.PixelFormat.RgbBitCount == 24)
                {
                    if (IsBitMask(dds.PixelFormat, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000))
                    {
                        format = DxgiFormat.B8G8R8X8UNorm;
                        int length = bitData.Length / 3;
                        var bytes  = new byte[length * 4];

                        for (int i = 0; i < length; i++)
                        {
                            bytes[i * 4 + 0] = bitData[i * 3 + 0];
                            bytes[i * 4 + 1] = bitData[i * 3 + 1];
                            bytes[i * 4 + 2] = bitData[i * 3 + 2];
                        }

                        bitData = bytes;
                    }
                }
            }

            int mipCount = Math.Max(1, dds.MipmapCount);

            switch (format)
            {
            case DxgiFormat.AI44:
            case DxgiFormat.IA44:
            case DxgiFormat.P8:
            case DxgiFormat.A8P8:
                throw new NotSupportedException(format.ToString() + " format is not supported.");

            default:
                if (DdsHelpers.GetBitsPerPixel((DdsFormat)format) == 0)
                {
                    throw new NotSupportedException(format.ToString() + " format is not supported.");
                }

                break;
            }

            switch (resDim)
            {
            case D3D11ResourceDimension.Texture1D:
                // D3DX writes 1D textures with a fixed Height of 1
                if ((dds.Options & DdsOptions.Height) != 0 && height != 1)
                {
                    throw new InvalidDataException();
                }

                height = 1;
                depth  = 1;
                break;

            case D3D11ResourceDimension.Texture2D:
                if ((dds.ResourceMiscOptions & DdsResourceMiscOptions.TextureCube) != 0)
                {
                    arraySize *= 6;
                    isCubeMap  = true;
                }

                depth = 1;
                break;

            case D3D11ResourceDimension.Texture3D:
                if ((dds.Options & DdsOptions.Depth) == 0)
                {
                    throw new InvalidDataException();
                }

                if (arraySize > 1)
                {
                    throw new NotSupportedException();
                }
                break;

            default:
                if ((dds.Options & DdsOptions.Depth) != 0)
                {
                    resDim = D3D11ResourceDimension.Texture3D;
                }
                else
                {
                    if ((dds.Caps2 & DdsAdditionalCaps.CubeMap) != 0)
                    {
                        // We require all six faces to be defined
                        if ((dds.Caps2 & DdsAdditionalCaps.CubeMapAllFaces) != DdsAdditionalCaps.CubeMapAllFaces)
                        {
                            throw new NotSupportedException();
                        }

                        arraySize = 6;
                        isCubeMap = true;
                    }

                    depth  = 1;
                    resDim = D3D11ResourceDimension.Texture2D;

                    // Note there's no way for a legacy Direct3D 9 DDS to express a '1D' texture
                }

                break;
            }

            if ((miscOptions & D3D11ResourceMiscOptions.TextureCube) != 0 &&
                resDim == D3D11ResourceDimension.Texture2D &&
                (arraySize % 6 == 0))
            {
                isCubeMap = true;
            }

            // Bound sizes (for security purposes we don't trust DDS file metadata larger than the D3D 11.x hardware requirements)
            if (mipCount > D3D11Constants.ReqMipLevels)
            {
                throw new NotSupportedException();
            }

            switch (resDim)
            {
            case D3D11ResourceDimension.Texture1D:
                if (arraySize > D3D11Constants.ReqTexture1DArrayAxisDimension ||
                    width > D3D11Constants.ReqTexture1DDimension)
                {
                    throw new NotSupportedException();
                }

                break;

            case D3D11ResourceDimension.Texture2D:
                if (isCubeMap)
                {
                    // This is the right bound because we set arraySize to (NumCubes*6) above
                    if (arraySize > D3D11Constants.ReqTexture2DArrayAxisDimension ||
                        width > D3D11Constants.ReqTextureCubeDimension ||
                        height > D3D11Constants.ReqTextureCubeDimension)
                    {
                        throw new NotSupportedException();
                    }
                }
                else if (arraySize > D3D11Constants.ReqTexture2DArrayAxisDimension ||
                         width > D3D11Constants.ReqTexture2DDimension ||
                         height > D3D11Constants.ReqTexture2DDimension)
                {
                    throw new NotSupportedException();
                }

                break;

            case D3D11ResourceDimension.Texture3D:
                if (arraySize > 1 ||
                    width > D3D11Constants.ReqTexture3DDimension ||
                    height > D3D11Constants.ReqTexture3DDimension ||
                    depth > D3D11Constants.ReqTexture3DDimension)
                {
                    throw new NotSupportedException();
                }

                break;

            default:
                throw new NotSupportedException();
            }

            bool autogen = false;

            if (mipCount == 1)
            {
                // See if format is supported for auto-gen mipmaps (varies by feature level)
                if (!device.CheckFormatSupport(format, out D3D11FormatSupport fmtSupport) &&
                    (fmtSupport & D3D11FormatSupport.MipAutogen) != 0)
                {
                    // 10level9 feature levels do not support auto-gen mipgen for volume textures
                    if (resDim != D3D11ResourceDimension.Texture3D ||
                        device.FeatureLevel >= D3D11FeatureLevel.FeatureLevel100)
                    {
                        autogen = true;
                    }
                }
            }

            if (autogen)
            {
                // Create texture with auto-generated mipmaps
                CreateD3DResources(
                    device,
                    resDim,
                    width,
                    height,
                    depth,
                    0,
                    arraySize,
                    format,
                    usage,
                    bindOptions | D3D11BindOptions.RenderTarget,
                    cpuAccessOptions,
                    miscOptions | D3D11ResourceMiscOptions.GenerateMips,
                    forceSRGB,
                    isCubeMap,
                    null,
                    out texture,
                    out textureView);

                try
                {
                    DdsHelpers.GetSurfaceInfo(width, height, (DdsFormat)format, out int numBytes, out int rowBytes, out int numRows);

                    if (numBytes > bitData.Length)
                    {
                        throw new EndOfStreamException();
                    }

                    D3D11ShaderResourceViewDesc desc = textureView.Description;
                    uint mipLevels = 1;

                    switch (desc.ViewDimension)
                    {
                    case D3D11SrvDimension.Texture1D:
                        mipLevels = desc.Texture1D.MipLevels;
                        break;

                    case D3D11SrvDimension.Texture1DArray:
                        mipLevels = desc.Texture1DArray.MipLevels;
                        break;

                    case D3D11SrvDimension.Texture2D:
                        mipLevels = desc.Texture2D.MipLevels;
                        break;

                    case D3D11SrvDimension.Texture2DArray:
                        mipLevels = desc.Texture2DArray.MipLevels;
                        break;

                    case D3D11SrvDimension.TextureCube:
                        mipLevels = desc.TextureCube.MipLevels;
                        break;

                    case D3D11SrvDimension.TextureCubeArray:
                        mipLevels = desc.TextureCubeArray.MipLevels;
                        break;

                    case D3D11SrvDimension.Texture3D:
                        mipLevels = desc.Texture3D.MipLevels;
                        break;

                    default:
                        throw new InvalidDataException();
                    }

                    if (arraySize > 1)
                    {
                        int pSrcBits = 0;

                        for (uint item = 0; item < (uint)arraySize; item++)
                        {
                            if (pSrcBits + numBytes > bitData.Length)
                            {
                                throw new EndOfStreamException();
                            }

                            var data = new byte[numBytes];
                            Array.Copy(bitData, pSrcBits, data, 0, numBytes);

                            uint res = D3D11Utils.CalcSubresource(0, item, mipLevels);
                            context.UpdateSubresource(texture, res, null, data, (uint)rowBytes, (uint)numBytes);

                            pSrcBits += numBytes;
                        }
                    }
                    else
                    {
                        context.UpdateSubresource(texture, 0, null, bitData, (uint)rowBytes, (uint)numBytes);
                    }

                    context.GenerateMips(textureView);
                }
                catch
                {
                    D3D11Utils.DisposeAndNull(ref textureView);
                    D3D11Utils.DisposeAndNull(ref texture);
                    throw;
                }
            }
            else
            {
                // Create the texture

                if (!FillInitData(
                        width,
                        height,
                        depth,
                        mipCount,
                        arraySize,
                        format,
                        maxSize,
                        bitData,
                        out int twidth,
                        out int theight,
                        out int tdepth,
                        out int skipMip,
                        out D3D11SubResourceData[] initData))
        private static void CreateD3DResources(
            D3D11Device device,
            D3D11ResourceDimension resDim,
            int width,
            int height,
            int depth,
            int mipCount,
            int arraySize,
            DxgiFormat format,
            D3D11Usage usage,
            D3D11BindOptions bindFlags,
            D3D11CpuAccessOptions cpuAccessFlags,
            D3D11ResourceMiscOptions miscFlags,
            bool forceSRGB,
            bool isCubeMap,
            D3D11SubResourceData[] initData,
            out D3D11Resource texture,
            out D3D11ShaderResourceView textureView)
        {
            texture     = null;
            textureView = null;

            if (forceSRGB)
            {
                format = (DxgiFormat)DdsHelpers.MakeSrgb((DdsFormat)format);
            }

            switch (resDim)
            {
            case D3D11ResourceDimension.Texture1D:
            {
                var desc = new D3D11Texture1DDesc(
                    format,
                    (uint)width,
                    (uint)arraySize,
                    (uint)mipCount,
                    bindFlags,
                    usage,
                    cpuAccessFlags,
                    miscFlags & ~D3D11ResourceMiscOptions.TextureCube);

                texture = device.CreateTexture1D(desc, initData);

                try
                {
                    var SRVDesc = new D3D11ShaderResourceViewDesc
                    {
                        Format = format
                    };

                    if (arraySize > 1)
                    {
                        SRVDesc.ViewDimension  = D3D11SrvDimension.Texture1DArray;
                        SRVDesc.Texture1DArray = new D3D11Texture1DArraySrv
                        {
                            MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels,
                            ArraySize = (uint)arraySize
                        };
                    }
                    else
                    {
                        SRVDesc.ViewDimension = D3D11SrvDimension.Texture1D;
                        SRVDesc.Texture1D     = new D3D11Texture1DSrv
                        {
                            MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels
                        };
                    }

                    textureView = device.CreateShaderResourceView(texture, SRVDesc);
                }
                catch
                {
                    D3D11Utils.DisposeAndNull(ref texture);
                    throw;
                }

                break;
            }

            case D3D11ResourceDimension.Texture2D:
            {
                var desc = new D3D11Texture2DDesc(
                    format,
                    (uint)width,
                    (uint)height,
                    (uint)arraySize,
                    (uint)mipCount,
                    bindFlags,
                    usage,
                    cpuAccessFlags,
                    1,
                    0);

                if (isCubeMap)
                {
                    desc.MiscOptions = miscFlags | D3D11ResourceMiscOptions.TextureCube;
                }
                else
                {
                    desc.MiscOptions = miscFlags & ~D3D11ResourceMiscOptions.TextureCube;
                }

                if (format == DxgiFormat.BC1UNorm || format == DxgiFormat.BC2UNorm || format == DxgiFormat.BC3UNorm)
                {
                    if ((width & 3) != 0 || (height & 3) != 0)
                    {
                        desc.Width  = (uint)(width + 3) & ~3U;
                        desc.Height = (uint)(height + 3) & ~3U;
                    }
                }

                texture = device.CreateTexture2D(desc, initData);

                try
                {
                    var SRVDesc = new D3D11ShaderResourceViewDesc
                    {
                        Format = format
                    };

                    if (isCubeMap)
                    {
                        if (arraySize > 6)
                        {
                            SRVDesc.ViewDimension    = D3D11SrvDimension.TextureCubeArray;
                            SRVDesc.TextureCubeArray = new D3D11TextureCubeArraySrv
                            {
                                MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels,
                                // Earlier we set arraySize to (NumCubes * 6)
                                NumCubes = (uint)arraySize / 6
                            };
                        }
                        else
                        {
                            SRVDesc.ViewDimension = D3D11SrvDimension.TextureCube;
                            SRVDesc.TextureCube   = new D3D11TextureCubeSrv
                            {
                                MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels,
                            };
                        }
                    }
                    else if (arraySize > 1)
                    {
                        SRVDesc.ViewDimension  = D3D11SrvDimension.Texture2DArray;
                        SRVDesc.Texture2DArray = new D3D11Texture2DArraySrv
                        {
                            MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels,
                            ArraySize = (uint)arraySize
                        };
                    }
                    else
                    {
                        SRVDesc.ViewDimension = D3D11SrvDimension.Texture2D;
                        SRVDesc.Texture2D     = new D3D11Texture2DSrv
                        {
                            MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels,
                        };
                    }

                    textureView = device.CreateShaderResourceView(texture, SRVDesc);
                }
                catch
                {
                    D3D11Utils.DisposeAndNull(ref texture);
                    throw;
                }

                break;
            }

            case D3D11ResourceDimension.Texture3D:
            {
                var desc = new D3D11Texture3DDesc(
                    format,
                    (uint)width,
                    (uint)height,
                    (uint)depth,
                    (uint)mipCount,
                    bindFlags,
                    usage,
                    cpuAccessFlags,
                    miscFlags & ~D3D11ResourceMiscOptions.TextureCube);

                texture = device.CreateTexture3D(desc, initData);

                try
                {
                    var SRVDesc = new D3D11ShaderResourceViewDesc
                    {
                        Format        = format,
                        ViewDimension = D3D11SrvDimension.Texture3D,
                        Texture3D     = new D3D11Texture3DSrv
                        {
                            MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels,
                        }
                    };

                    textureView = device.CreateShaderResourceView(texture, SRVDesc);
                }
                catch
                {
                    D3D11Utils.DisposeAndNull(ref texture);
                    throw;
                }

                break;
            }
            }
        }
Beispiel #8
0
        // Use this for initialization
        void Start()
        {
            NewTextureForRender();
            if (Application.isPlaying)
            {
                if (MicroLightManager.Instance)
                {
                    switch (MicroLightManager.Instance.mRenderType)
                    {
                    case RendererType.MicroLightRender:
                    {
                        //Create Shared  Texture2D

                        LeftRT2D = MicroLightPlugin.D3D11Helper.CreateSharedView(MicroLightManager.Instance.m_UnityDevice,
                                                                                 DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM,
                                                                                 (int)TextureSize.x,
                                                                                 (int)TextureSize.y);

                        if (LeftRT2D == IntPtr.Zero)
                        {
                            Debug.Log(this + " LeftRT2D is null");
                        }
                        HANDLE LeftRTHandleSend = MicroLightPlugin.D3D11Helper.GetSharedHandle(MicroLightManager.Instance.m_UnityDevice, LeftRT2D);
                        if (LeftRTHandleSend == IntPtr.Zero)
                        {
                            Debug.Log(this + " LeftRTHandleSend is null");
                        }
                        D3D11Resource LeftRTHandleReceive = MicroLightPlugin.D3D11Helper.OpenSharedResource(MicroLightManager.Instance.m_Device, LeftRTHandleSend);
                        if (LeftRTHandleReceive == IntPtr.Zero)
                        {
                            Debug.Log(this + " LeftRTHandleReceive is null");
                        }

                        RightRT2D = MicroLightPlugin.D3D11Helper.CreateSharedView(MicroLightManager.Instance.m_UnityDevice,
                                                                                  DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM,
                                                                                  (int)TextureSize.x,
                                                                                  (int)TextureSize.y);
                        if (RightRT2D == IntPtr.Zero)
                        {
                            Debug.Log(this + " RightRT2D is null");
                        }
                        HANDLE RightRTHandleSend = MicroLightPlugin.D3D11Helper.GetSharedHandle(MicroLightManager.Instance.m_UnityDevice, RightRT2D);
                        if (RightRTHandleSend == IntPtr.Zero)
                        {
                            Debug.Log(this + " RightRTHandleSend is null");
                        }
                        D3D11Resource RightRTHandleReceive = MicroLightPlugin.D3D11Helper.OpenSharedResource(MicroLightManager.Instance.m_Device, RightRTHandleSend);
                        if (RightRTHandleSend == IntPtr.Zero)
                        {
                            Debug.Log(this + " RightRTHandleReceive is null");
                        }

                        leftView  = MicroLightPlugin.D3D11Helper.CreateShaderResourceView(MicroLightManager.Instance.m_Device, LeftRTHandleReceive);
                        rightView = MicroLightPlugin.D3D11Helper.CreateShaderResourceView(MicroLightManager.Instance.m_Device, RightRTHandleReceive);


                        if (leftView == IntPtr.Zero)
                        {
                            UnityEngine.Debug.Log("leftView is null");
                        }
                        if (rightView == IntPtr.Zero)
                        {
                            UnityEngine.Debug.Log("rightView is null");
                        }

                        switch (MicroLightManager.Instance.mRenderMode)
                        {
                        case RenderMode.LR3D:
                        {
                            MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_RenderAPI, ((int)id * 10 + (int)LeftEye.mEyeTye), leftView, rightView, 0.5f, 1.0f, -0.25f, 0);
                            MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_RenderAPI, ((int)id * 10 + (int)RightEye.mEyeTye), rightView, leftView, 0.5f, 1.0f, 0.25f, 0);
                        }
                        break;

                        case RenderMode.Stereo:
                        {
                            switch (id)
                            {
                            case TrackingSpaceId.Surface:
                                MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_RenderAPI, (int)id, leftView, rightView, 1, 1, 0, 0);
                                break;

                            case TrackingSpaceId.LTypeRoomFront:
                                MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_RenderAPI, (int)id, leftView, rightView, 0.5f, 1.0f, -0.25f, 0);
                                break;

                            case TrackingSpaceId.LTypeRoomFloor:
                                MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_RenderAPI, (int)id, leftView, rightView, 0.5f, 1.0f, 0.25f, 0);
                                break;

                            case TrackingSpaceId.TrapezoidRoomLeft:
                                MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_RenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, -0.375f, 0);
                                break;

                            case TrackingSpaceId.TrapezoidRoomFront:
                                MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_RenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, -0.125f, 0);
                                break;

                            case TrackingSpaceId.TrapezoidRoomRight:
                                MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_RenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, 0.125f, 0);
                                break;

#if MicroLightGridView
                            case TrackingSpaceId.TrapezoidRoomFloor:
                                MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_RenderAPI, (int)id + 1, leftView, rightView, 0.25f, 1.0f, 0.375f, 0);
                                break;
#else
                            case TrackingSpaceId.TrapezoidRoomFloor:
                                MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_RenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, 0.375f, 0);
                                break;
#endif
                            }
                        }
                        break;
                        }
                    }
                    break;

                    case RendererType.UnityRender:
                    {
                        leftView =
                            MicroLightPlugin.D3D11Helper.CreateShaderResourceViewFromRenderTexture(MicroLightManager.Instance.m_UnityDevice,
                                                                                                   LeftRT.GetNativeTexturePtr(),
                                                                                                   DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM);

                        rightView =
                            MicroLightPlugin.D3D11Helper.CreateShaderResourceViewFromRenderTexture(MicroLightManager.Instance.m_UnityDevice,
                                                                                                   RightRT.GetNativeTexturePtr(),
                                                                                                   DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM);

                        if (leftView == IntPtr.Zero)
                        {
                            UnityEngine.Debug.Log("leftView is null");
                        }
                        if (rightView == IntPtr.Zero)
                        {
                            UnityEngine.Debug.Log("rightView is null");
                        }

                        switch (MicroLightManager.Instance.mRenderMode)
                        {
                        case RenderMode.LR3D:
                            MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_UnityRenderAPI, ((int)id * 10 + (int)LeftEye.mEyeTye), leftView, rightView, 0.5f, 1.0f, -0.25f, 0);
                            MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_UnityRenderAPI, ((int)id * 10 + (int)RightEye.mEyeTye), rightView, leftView, 0.5f, 1.0f, 0.25f, 0);
                            break;

                        case RenderMode.Stereo:


                            switch (id)
                            {
                            case TrackingSpaceId.Surface:
                                UnityNativePlugin.Render.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 1, 1, 0, 0);
                                break;

                            case TrackingSpaceId.LTypeRoomFront:
                                UnityNativePlugin.Render.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.5f, 1.0f, -0.25f, 0);
                                break;

                            case TrackingSpaceId.LTypeRoomFloor:
                                UnityNativePlugin.Render.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.5f, 1.0f, 0.25f, 0);
                                break;

                            case TrackingSpaceId.TrapezoidRoomLeft:
                                UnityNativePlugin.Render.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, -0.375f, 0);
                                break;

                            case TrackingSpaceId.TrapezoidRoomFront:
                                UnityNativePlugin.Render.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, -0.125f, 0);
                                break;

                            case TrackingSpaceId.TrapezoidRoomRight:
                                UnityNativePlugin.Render.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, 0.125f, 0);
                                break;

#if MicroLightGridView
                            case TrackingSpaceId.TrapezoidRoomFloor:
                                UnityNativePlugin.Render.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id + 1, leftView, rightView, 0.25f, 1.0f, 0.375f, 0);
                                break;
#else
                            case TrackingSpaceId.TrapezoidRoomFloor:
                                UnityNativePlugin.Render.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, 0.375f, 0);
                                break;
#endif
                            }

                            break;
                        }
                    }
                    break;

                    case RendererType.UnityCoverRender:
                    {
                        leftView =
                            MicroLightPlugin.D3D11Helper.CreateShaderResourceViewFromRenderTexture(MicroLightManager.Instance.m_UnityDevice,
                                                                                                   LeftRT.GetNativeTexturePtr(),
                                                                                                   DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM);

                        rightView =
                            MicroLightPlugin.D3D11Helper.CreateShaderResourceViewFromRenderTexture(MicroLightManager.Instance.m_UnityDevice,
                                                                                                   RightRT.GetNativeTexturePtr(),
                                                                                                   DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM);


                        if (leftView == IntPtr.Zero)
                        {
                            UnityEngine.Debug.Log("leftView is null");
                        }
                        if (rightView == IntPtr.Zero)
                        {
                            UnityEngine.Debug.Log("rightView is null");
                        }
                        switch (MicroLightManager.Instance.mRenderMode)
                        {
                        case RenderMode.LR3D:
                            UnityNativePlugin.CoverRender.AddView(MicroLightManager.Instance.m_CoverRenderAPI, ((int)id * 10 + (int)LeftEye.mEyeTye), leftView, rightView, 0.5f, 1.0f, -0.25f, 0);
                            UnityNativePlugin.CoverRender.AddView(MicroLightManager.Instance.m_CoverRenderAPI, ((int)id * 10 + (int)RightEye.mEyeTye), rightView, leftView, 0.5f, 1.0f, 0.25f, 0);
                            break;

                        case RenderMode.Stereo:

                            switch (id)
                            {
                            case TrackingSpaceId.Surface:
                                UnityNativePlugin.CoverRender.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 1, 1, 0, 0);
                                break;

                            case TrackingSpaceId.LTypeRoomFront:
                                UnityNativePlugin.CoverRender.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.5f, 1.0f, -0.25f, 0);
                                break;

                            case TrackingSpaceId.LTypeRoomFloor:
                                UnityNativePlugin.CoverRender.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.5f, 1.0f, 0.25f, 0);
                                break;

                            case TrackingSpaceId.TrapezoidRoomLeft:
                                UnityNativePlugin.CoverRender.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, -0.375f, 0);
                                break;

                            case TrackingSpaceId.TrapezoidRoomFront:
                                UnityNativePlugin.CoverRender.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, -0.125f, 0);
                                break;

                            case TrackingSpaceId.TrapezoidRoomRight:
                                UnityNativePlugin.CoverRender.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, 0.125f, 0);
                                break;

#if MicroLightGridView
                            case TrackingSpaceId.TrapezoidRoomFloor:
                                UnityNativePlugin.CoverRender.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id + 1, leftView, rightView, 0.25f, 1.0f, 0.375f, 0);
                                break;
#else
                            case TrackingSpaceId.TrapezoidRoomFloor:
                                UnityNativePlugin.CoverRender.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, 0.375f, 0);
                                break;
#endif
                            }

                            break;
                        }
                    }
                    break;
                    }
                }
            }
        }
Beispiel #9
0
 public static extern D3D11ShaderResourceView CreateShaderResourceView(D3D11Device device, D3D11Resource resource);