Пример #1
0
        protected virtual bool init(IDisposableResource parent, Image image, int width, int height, bool generateMipmaps, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, RenderTargetUsage renderTargetUsage, BufferUsages usage, bool isRenderTarget, bool lockable, Loader.LoadedCallbackMethod loadedCallback)
        {
            byte[][] mipmaps = null;
            int[] mipmapSizes = null, mipmapPitches = null;
            try
            {
                video = parent.FindParentOrSelfWithException<Video>();
                if (isRenderTarget) generateMipmaps = false;

                // load image data
                if (image != null)
                {
                    mipmaps = new byte[image.Mipmaps.Length][];
                    mipmapSizes = new int[image.Mipmaps.Length];
                    mipmapPitches = new int[image.Mipmaps.Length];
                    for (int i = 0; i != mipmaps.Length; ++i)
                    {
                        var imageMipmap = image.Mipmaps[i];
                        mipmaps[i] = image.Compressed ? imageMipmap.Data : imageMipmap.SwapRBColorChannels();
                        mipmapSizes[i] = imageMipmap.Data.Length;
                        mipmapPitches[i] = imageMipmap.Pitch;
                    }

                    Size = image.Size;
                    surfaceFormat = image.SurfaceFormat;
                    PixelByteSize = image.CalculatePixelByteSize();
                }
                else
                {
                    if (width == 0 || height == 0) Debug.ThrowError("Texture2D", "Width or Height cannot be 0");
                    Size = new Size2(width, height);
                    PixelByteSize = Image.CalculatePixelByteSize((surfaceFormat == SurfaceFormats.Defualt ? Video.DefaultSurfaceFormat() : surfaceFormat), width, height);
                }
                TexelOffset = (1 / Size.ToVector2()) * .5f;
                SizeF = Size.ToVector2();

                // init texture
                REIGN_D3DUSAGE nativeUsage = isRenderTarget ? REIGN_D3DUSAGE.RENDERTARGET : REIGN_D3DUSAGE.NONE;
                REIGN_D3DPOOL nativePool = (mipmaps != null && !video.Caps.ExDevice) ? REIGN_D3DPOOL.MANAGED : REIGN_D3DPOOL.DEFAULT;
                if (usage == BufferUsages.Read)
                {
                    if (!isRenderTarget) Debug.ThrowError("Texture2D", "Only RenderTargets may be readable");
                    // NOTE: Staging texture and states will be created in the RenderTarget
                }
                if (usage == BufferUsages.Write)
                {
                    if (mipmaps != null)
                    {
                        if (video.Caps.ExDevice) nativeUsage |= REIGN_D3DUSAGE.DYNAMIC;
                    }
                    else
                    {
                        nativeUsage |= REIGN_D3DUSAGE.DYNAMIC;
                    }
                }
                com = new Texture2DCom();
                var error = com.Init(video.com, Size.Width, Size.Height, generateMipmaps, mipmaps, mipmapSizes, mipmapPitches, 0, nativePool, nativeUsage, video.surfaceFormat(surfaceFormat), isRenderTarget);

                switch (error)
                {
                    case TextureError.Texture: Debug.ThrowError("Texture2D", "Failed to create Texture2D"); break;
                    case TextureError.SystemTexture: Debug.ThrowError("Texture2D", "Failed to create System Texture2D"); break;
                }

                if (!video.Caps.ExDevice && nativePool != REIGN_D3DPOOL.MANAGED)
                {
                    LostDevice_image = image;
                    LostDevice_width = width;
                    LostDevice_height = height;
                    LostDevice_generateMipmaps = generateMipmaps;
                    LostDevice_multiSampleType = multiSampleType;
                    LostDevice_surfaceFormat = surfaceFormat;
                    LostDevice_renderTargetUsage = renderTargetUsage;
                    LostDevice_usage = usage;
                    LostDevice_isRenderTarget = isRenderTarget;
                    LostDevice_lockable = lockable;
                    LostDevice_pool = nativePool;
                }

                if (nativePool == REIGN_D3DPOOL.DEFAULT && !video.Caps.ExDevice && !video.deviceReseting)
                {
                    video.DeviceLost += deviceLost;
                    video.DeviceReset += deviceReset;
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                Dispose();
                if (loadedCallback != null) loadedCallback(this, false);
                return false;
            }

            if (!isRenderTarget)
            {
                Loaded = true;
                if (loadedCallback != null) loadedCallback(this, true);
            }
            return true;
        }
Пример #2
0
        protected virtual bool init(IDisposableResource parent, Image image, int width, int height, bool generateMipmaps, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, RenderTargetUsage renderTargetUsage, BufferUsages usage, bool isRenderTarget, Loader.LoadedCallbackMethod loadedCallback)
        {
            long[] mipmaps = null;
            int[] mipmapSizes = null, mipmapPitches = null;
            try
            {
                video = parent.FindParentOrSelfWithException<Video>();
                if (isRenderTarget) generateMipmaps = false;

                // load image data
                if (image != null)
                {
                    mipmaps = new long[image.Mipmaps.Length];
                    mipmapSizes = new int[image.Mipmaps.Length];
                    mipmapPitches = new int[image.Mipmaps.Length];
                    for (int i = 0; i != mipmaps.Length; ++i)
                    {
                        var imageMipmap = image.Mipmaps[i];
                        IntPtr mipmapPtr = Marshal.AllocHGlobal(imageMipmap.Data.Length);
                        Marshal.Copy(imageMipmap.Data, 0, mipmapPtr, imageMipmap.Data.Length);
                        mipmapSizes[i] = imageMipmap.Data.Length;
                        mipmapPitches[i] = imageMipmap.Pitch;
                        mipmaps[i] = mipmapPtr.ToInt64();
                    }

                    Size = image.Size;
                    surfaceFormat = image.SurfaceFormat;
                    PixelByteSize = image.CalculatePixelByteSize();
                }
                else
                {
                    if (width == 0 || height == 0) Debug.ThrowError("Texture2D", "Width or Height cannot be 0");
                    Size = new Size2(width, height);
                    PixelByteSize = Image.CalculatePixelByteSize((surfaceFormat == SurfaceFormats.Defualt ? Video.DefaultSurfaceFormat() : surfaceFormat), width, height);
                }
                SizeF = Size.ToVector2();

                // init texture
                REIGN_D3D11_USAGE usageType = REIGN_D3D11_USAGE.DEFAULT;
                REIGN_D3D11_CPU_ACCESS_FLAG cpuAccessFlags = (REIGN_D3D11_CPU_ACCESS_FLAG)0;
                if (usage == BufferUsages.Read)
                {
                    if (!isRenderTarget) Debug.ThrowError("Texture2D", "Only RenderTargets may be readable");
                    // NOTE: Staging texture and states will be created in the RenderTarget
                    //usageType = REIGN_D3D11_USAGE.STAGING;
                    //cpuAccessFlags = REIGN_D3D11_CPU_ACCESS_FLAG.READ;
                }
                if (usage == BufferUsages.Write)
                {
                    usageType = REIGN_D3D11_USAGE.DYNAMIC;
                    cpuAccessFlags = REIGN_D3D11_CPU_ACCESS_FLAG.WRITE;
                }
                com = new Texture2DCom();
                var error = com.Init(video.com, Size.Width, Size.Height, generateMipmaps, mipmaps != null, mipmaps, mipmapSizes, mipmapPitches, 0, video.surfaceFormat(surfaceFormat), usageType, cpuAccessFlags, isRenderTarget);

                switch (error)
                {
                    case TextureError.Texture: Debug.ThrowError("Texture2D", "Failed to create Texture2D"); break;
                    case TextureError.ShaderResourceView: Debug.ThrowError("Texture2D", "Failed to create ShaderResourceView"); break;
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                Dispose();
                if (loadedCallback != null) loadedCallback(this, false);
                return false;
            }
            finally
            {
                if (mipmaps != null)
                {
                    for (int i = 0; i != mipmaps.Length; ++i)
                    {
                        if (mipmaps[i] != 0) Marshal.FreeHGlobal(new IntPtr(mipmaps[i]));
                    }
                }
            }

            if (!isRenderTarget)
            {
                Loaded = true;
                if (loadedCallback != null) loadedCallback(this, true);
            }
            return true;
        }
Пример #3
0
        protected virtual bool init(IDisposableResource parent, Image image, int width, int height, bool generateMipmaps, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, RenderTargetUsage renderTargetUsage, BufferUsages usage, bool isRenderTarget, bool lockable, Loader.LoadedCallbackMethod loadedCallback)
        {
            byte[][] mipmaps     = null;
            int[]    mipmapSizes = null, mipmapPitches = null;
            try
            {
                video = parent.FindParentOrSelfWithException <Video>();
                if (isRenderTarget)
                {
                    generateMipmaps = false;
                }

                // load image data
                if (image != null)
                {
                    mipmaps       = new byte[image.Mipmaps.Length][];
                    mipmapSizes   = new int[image.Mipmaps.Length];
                    mipmapPitches = new int[image.Mipmaps.Length];
                    for (int i = 0; i != mipmaps.Length; ++i)
                    {
                        var imageMipmap = image.Mipmaps[i];
                        mipmaps[i]       = image.Compressed ? imageMipmap.Data : imageMipmap.SwapRBColorChannels();
                        mipmapSizes[i]   = imageMipmap.Data.Length;
                        mipmapPitches[i] = imageMipmap.Pitch;
                    }

                    Size          = image.Size;
                    surfaceFormat = image.SurfaceFormat;
                    PixelByteSize = image.CalculatePixelByteSize();
                }
                else
                {
                    if (width == 0 || height == 0)
                    {
                        Debug.ThrowError("Texture2D", "Width or Height cannot be 0");
                    }
                    Size          = new Size2(width, height);
                    PixelByteSize = Image.CalculatePixelByteSize((surfaceFormat == SurfaceFormats.Defualt ? Video.DefaultSurfaceFormat() : surfaceFormat), width, height);
                }
                TexelOffset = (1 / Size.ToVector2()) * .5f;
                SizeF       = Size.ToVector2();

                // init texture
                REIGN_D3DUSAGE nativeUsage = isRenderTarget ? REIGN_D3DUSAGE.RENDERTARGET : REIGN_D3DUSAGE.NONE;
                REIGN_D3DPOOL  nativePool  = (mipmaps != null && !video.Caps.ExDevice) ? REIGN_D3DPOOL.MANAGED : REIGN_D3DPOOL.DEFAULT;
                if (usage == BufferUsages.Read)
                {
                    if (!isRenderTarget)
                    {
                        Debug.ThrowError("Texture2D", "Only RenderTargets may be readable");
                    }
                    // NOTE: Staging texture and states will be created in the RenderTarget
                }
                if (usage == BufferUsages.Write)
                {
                    if (mipmaps != null)
                    {
                        if (video.Caps.ExDevice)
                        {
                            nativeUsage |= REIGN_D3DUSAGE.DYNAMIC;
                        }
                    }
                    else
                    {
                        nativeUsage |= REIGN_D3DUSAGE.DYNAMIC;
                    }
                }
                com = new Texture2DCom();
                var error = com.Init(video.com, Size.Width, Size.Height, generateMipmaps, mipmaps, mipmapSizes, mipmapPitches, 0, nativePool, nativeUsage, video.surfaceFormat(surfaceFormat), isRenderTarget);

                switch (error)
                {
                case TextureError.Texture: Debug.ThrowError("Texture2D", "Failed to create Texture2D"); break;

                case TextureError.SystemTexture: Debug.ThrowError("Texture2D", "Failed to create System Texture2D"); break;
                }

                if (!video.Caps.ExDevice && nativePool != REIGN_D3DPOOL.MANAGED)
                {
                    LostDevice_image             = image;
                    LostDevice_width             = width;
                    LostDevice_height            = height;
                    LostDevice_generateMipmaps   = generateMipmaps;
                    LostDevice_multiSampleType   = multiSampleType;
                    LostDevice_surfaceFormat     = surfaceFormat;
                    LostDevice_renderTargetUsage = renderTargetUsage;
                    LostDevice_usage             = usage;
                    LostDevice_isRenderTarget    = isRenderTarget;
                    LostDevice_lockable          = lockable;
                    LostDevice_pool = nativePool;
                }

                if (nativePool == REIGN_D3DPOOL.DEFAULT && !video.Caps.ExDevice && !video.deviceReseting)
                {
                    video.DeviceLost  += deviceLost;
                    video.DeviceReset += deviceReset;
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                Dispose();
                if (loadedCallback != null)
                {
                    loadedCallback(this, false);
                }
                return(false);
            }

            if (!isRenderTarget)
            {
                Loaded = true;
                if (loadedCallback != null)
                {
                    loadedCallback(this, true);
                }
            }
            return(true);
        }
Пример #4
0
        protected virtual bool init(IDisposableResource parent, Image image, int width, int height, bool generateMipmaps, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, RenderTargetUsage renderTargetUsage, BufferUsages usage, bool isRenderTarget, Loader.LoadedCallbackMethod loadedCallback)
        {
            long[] mipmaps     = null;
            int[]  mipmapSizes = null, mipmapPitches = null;
            try
            {
                video = parent.FindParentOrSelfWithException <Video>();
                if (isRenderTarget)
                {
                    generateMipmaps = false;
                }

                // load image data
                if (image != null)
                {
                    mipmaps       = new long[image.Mipmaps.Length];
                    mipmapSizes   = new int[image.Mipmaps.Length];
                    mipmapPitches = new int[image.Mipmaps.Length];
                    for (int i = 0; i != mipmaps.Length; ++i)
                    {
                        var    imageMipmap = image.Mipmaps[i];
                        IntPtr mipmapPtr   = Marshal.AllocHGlobal(imageMipmap.Data.Length);
                        Marshal.Copy(imageMipmap.Data, 0, mipmapPtr, imageMipmap.Data.Length);
                        mipmapSizes[i]   = imageMipmap.Data.Length;
                        mipmapPitches[i] = imageMipmap.Pitch;
                        mipmaps[i]       = mipmapPtr.ToInt64();
                    }

                    Size          = image.Size;
                    surfaceFormat = image.SurfaceFormat;
                    PixelByteSize = image.CalculatePixelByteSize();
                }
                else
                {
                    if (width == 0 || height == 0)
                    {
                        Debug.ThrowError("Texture2D", "Width or Height cannot be 0");
                    }
                    Size          = new Size2(width, height);
                    PixelByteSize = Image.CalculatePixelByteSize((surfaceFormat == SurfaceFormats.Defualt ? Video.DefaultSurfaceFormat() : surfaceFormat), width, height);
                }
                SizeF = Size.ToVector2();

                // init texture
                REIGN_D3D11_USAGE           usageType      = REIGN_D3D11_USAGE.DEFAULT;
                REIGN_D3D11_CPU_ACCESS_FLAG cpuAccessFlags = (REIGN_D3D11_CPU_ACCESS_FLAG)0;
                if (usage == BufferUsages.Read)
                {
                    if (!isRenderTarget)
                    {
                        Debug.ThrowError("Texture2D", "Only RenderTargets may be readable");
                    }
                    // NOTE: Staging texture and states will be created in the RenderTarget
                    //usageType = REIGN_D3D11_USAGE.STAGING;
                    //cpuAccessFlags = REIGN_D3D11_CPU_ACCESS_FLAG.READ;
                }
                if (usage == BufferUsages.Write)
                {
                    usageType      = REIGN_D3D11_USAGE.DYNAMIC;
                    cpuAccessFlags = REIGN_D3D11_CPU_ACCESS_FLAG.WRITE;
                }
                com = new Texture2DCom();
                var error = com.Init(video.com, Size.Width, Size.Height, generateMipmaps, mipmaps != null, mipmaps, mipmapSizes, mipmapPitches, 0, video.surfaceFormat(surfaceFormat), usageType, cpuAccessFlags, isRenderTarget);

                switch (error)
                {
                case TextureError.Texture: Debug.ThrowError("Texture2D", "Failed to create Texture2D"); break;

                case TextureError.ShaderResourceView: Debug.ThrowError("Texture2D", "Failed to create ShaderResourceView"); break;
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                Dispose();
                if (loadedCallback != null)
                {
                    loadedCallback(this, false);
                }
                return(false);
            }
            finally
            {
                if (mipmaps != null)
                {
                    for (int i = 0; i != mipmaps.Length; ++i)
                    {
                        if (mipmaps[i] != 0)
                        {
                            Marshal.FreeHGlobal(new IntPtr(mipmaps[i]));
                        }
                    }
                }
            }

            if (!isRenderTarget)
            {
                Loaded = true;
                if (loadedCallback != null)
                {
                    loadedCallback(this, true);
                }
            }
            return(true);
        }