private static D3d.Device CreateDevice(System.Windows.Forms.Control renderWindow,
            D3d.PresentParameters presentParams,
            D3d.CreateFlags createFlags,
            Settings deviceSettings)
        {
            try
            {
                D3d.Device d3dDevice = new D3d.Device(deviceSettings.adapterIndex,
                                                        deviceSettings.deviceType,
                                                        renderWindow,
                                                        createFlags,
                                                        presentParams);
                return d3dDevice;
            }
            catch (D3d.DeviceLostException e)
            {
                log.Warning("Unable to create Direct3D device: {0}", e.Message);
            }
            catch (D3d.InvalidCallException e)
            {
                log.Warning("Unable to create Direct3D device: {0}", e.Message);
            }
            catch (D3d.NotAvailableException e)
            {
                log.Warning("Unable to create Direct3D device: {0}", e.Message);
            }
            catch (D3d.OutOfVideoMemoryException e)
            {
                log.Warning("Unable to create Direct3D device: {0}", e.Message);
            }

            return null;
        }
        private bool Initialize(TextureManager manager, D3d.Format format, int size, bool mipmap,int multisample )
        {
            D3d.Pool d3dPool = D3d.Pool.Default;
            D3d.Usage d3dUsage = manager.GetUsageFlags( false,mipmap,true );

            try
            {
                d3dTexture = new D3d.CubeTexture( manager.Device.D3dDevice,size,manager.GetMipLevelCount(mipmap),d3dUsage,format,d3dPool);

                // Create depth buffer.
                d3dDepthBuffer = manager.Device.D3dDevice.CreateDepthStencilSurface(size, size, manager.Device.Settings.depthBufferFormat, (D3d.MultiSampleType)multisample, 0, true);

                this.Initialize(manager, format, size,size,6, d3dTexture);

                return true;
            }
            catch (D3d.InvalidCallException e)
            {
                log.Warning("Unable to create render cube texture: {0}", e.Message);
            }
            catch (D3d.OutOfVideoMemoryException e)
            {
                log.Warning("Unable to create render cube texture: {0}", e.Message);
            }
            catch (OutOfMemoryException e)
            {
                log.Warning("Unable to create render cube texture: {0}", e.Message);
            }

            return false;
        }
        private bool Initialize(TextureManager manager, D3d.Format format, int width, int height, bool dynamic, bool mipmap)
        {
            D3d.Pool d3dPool = D3d.Pool.Managed;
            D3d.Usage d3dUsage = manager.GetUsageFlags( dynamic,mipmap,false );

            try
            {
                d3dTexture = new D3d.Texture(   manager.Device.D3dDevice,
                                                width,
                                                height,
                                                manager.GetMipLevelCount(mipmap),
                                                d3dUsage,
                                                format,
                                                d3dPool );
                this.Initialize(manager, format, width, height, 1, d3dTexture);

                return true;
            }
            catch (D3d.InvalidCallException e)
            {
                log.Warning("Unable to create texture: {0}", e.Message);
            }
            catch (D3d.OutOfVideoMemoryException e)
            {
                log.Warning("Unable to create texture: {0}", e.Message);
            }
            catch (OutOfMemoryException e)
            {
                log.Warning("Unable to create texture: {0}", e.Message);
            }

            return false;
        }
        public static int GetSize(D3d.Format format)
        {
            if ( formatSizes.ContainsKey(format) )
                return formatSizes[format];

            return 0;
        }
        public static TextureCube Create(TextureManager manager,D3d.Format format, int size, bool dynamic, bool mipmap)
        {
            TextureCube newTexture = new TextureCube();
            if ( newTexture.Initialize( manager,format,size,dynamic,mipmap ) )
                return newTexture;

            return null;
        }
        public static RenderTexture2d Create(TextureManager manager,D3d.Format format, int width, int height, bool mipmap,int multisample)
        {
            RenderTexture2d newTexture = new RenderTexture2d();
            if ( newTexture.Initialize( manager,format,width,height,mipmap,multisample ) )
                return newTexture;

            return null;
        }
        public static RenderTextureCube Create(TextureManager manager,D3d.Format format, int size, bool mipmap,int multisample )
        {
            RenderTextureCube newTexture = new RenderTextureCube();
            if ( newTexture.Initialize( manager,format,size,mipmap,multisample ) )
                return newTexture;

            return null;
        }
        public static Texture2d Create(TextureManager manager,D3d.Format format, int width, int height, bool dynamic, bool mipmap)
        {
            Texture2d newTexture = new Texture2d();
            if ( newTexture.Initialize( manager,format,width,height,dynamic,mipmap ) )
                return newTexture;

            return null;
        }
 protected void Initialize(TextureManager manager, D3d.Format format, int width, int height, int depth,D3d.BaseTexture d3dBase )
 {
     textureManager  = manager;
     textureFormat   = format;
     textureWidth    = width;
     textureHeight   = height;
     textureDepth    = depth;
     textureBase     = d3dBase;
 }
        public RenderTexture2d CreateRenderTexture2d(D3d.Format format, int width, int height, bool mipmap, int multisample)
        {
            int depth = 128;
            if (ModifyParameters(D3d.ResourceType.Textures, format, false, mipmap, true, ref width, ref height, ref depth))
            {
                RenderTexture2d newTexture = RenderTexture2d.Create(this, format, width, height, mipmap,multisample );
                if (newTexture != null)
                {
                    textures.Add(newTexture);
                    textureTargets.Add(newTexture);
                    return newTexture;
                }
            }

            return null;
        }
        public RenderTextureCube CreateRenderTextureCube(D3d.Format format, int size, bool mipmap, int multisample)
        {
            int height  = 128;
            int depth   = 128;
            if (ModifyParameters(D3d.ResourceType.CubeTexture, format, false, mipmap, true, ref size, ref height, ref depth))
            {
                RenderTextureCube newTexture = RenderTextureCube.Create(this, format, size, mipmap, multisample);
                if (newTexture != null)
                {
                    textures.Add(newTexture);
                    textureTargets.Add(newTexture);
                    return newTexture;
                }
            }

            return null;
        }
        public D3DHardwareIndexBuffer(IndexType type, int numIndices, BufferUsage usage, 
            D3D.Device device, bool useSystemMemory, bool useShadowBuffer)
            : base(type, numIndices, usage, useSystemMemory, useShadowBuffer)
        {
            #if !NO_OGRE_D3D_MANAGE_BUFFERS
            d3dPool = useSystemMemory? Pool.SystemMemory :
                // If not system mem, use managed pool UNLESS buffer is discardable
                // if discardable, keeping the software backing is expensive
                ((usage & BufferUsage.Discardable) != 0) ? Pool.Default : Pool.Managed;
            #else
            d3dPool = useSystemMemory ? Pool.SystemMemory : Pool.Default;
            #endif

            Type bufferType = (type == IndexType.Size16) ? typeof(short) : typeof(int);

            // create the buffer
            d3dBuffer = new IndexBuffer(
                bufferType,
                //sizeInBytes, // sizeInBytes is wrong, because the D3D API is expecting the number of indices
                numIndices,
                device,
                D3DHelper.ConvertEnum(usage),
                d3dPool);
        }
 /// <summary>
 ///		
 /// </summary>
 /// <param name="device"></param>
 public D3DHardwareBufferManager(D3D.Device device)
 {
     this.device = device;
 }
        //---------------------------------------------------------------------
        public bool RecreateIfDefaultPool(D3D.Device device)
        {
            if (d3dPool == Pool.Default)
            {
                Type bufferType = (type == IndexType.Size16) ? typeof(short) : typeof(int);
                // Create the Index buffer
                d3dBuffer = new IndexBuffer(
                    bufferType,
                    //sizeInBytes, // sizeInBytes is wrong, because the D3D API is expecting the number of indices
                    numIndices,
                    device,
                    D3DHelper.ConvertEnum(usage),
                    d3dPool);

                return true;
            }
            return false;
        }
        private bool ModifyParameters(  D3d.ResourceType type,
            D3d.Format format,
            bool dynamic,
            bool mipmap,
            bool isTarget,
            ref int width,
            ref int height,
            ref int depth)
        {
            // First fix any dimensional errors.
            width   = width     / textureDivider;
            height  = height    / textureDivider;
            depth   = depth     / textureDivider;

            // Make sure we stay within maximum allowed dimensions.
            if ( width > textureDevice.Capabilities.textureMaxWidth )
                width = textureDevice.Capabilities.textureMaxWidth;

            if ( height > textureDevice.Capabilities.textureMaxHeight )
                height = textureDevice.Capabilities.textureMaxHeight;

            if ( depth > textureDevice.Capabilities.textureMaxDepth )
                depth = textureDevice.Capabilities.textureMaxDepth;

            D3d.Usage d3dUsage = GetUsageFlags( dynamic,mipmap,isTarget );

            // Get the current display mode format for later use.
            D3d.Format displayFormat = D3d.Manager.Adapters[ Device.Settings.adapterIndex ].CurrentDisplayMode.Format;

            if (D3d.Manager.CheckDeviceFormat(  Device.Settings.adapterIndex,
                                                Device.Settings.deviceType,
                                                Device.Settings.backBufferFormat,
                                                d3dUsage, type, format))
            {
                if (isTarget)
                {
                    if (D3d.Manager.CheckDepthStencilMatch(Device.Settings.adapterIndex,
                                                            Device.Settings.deviceType,
                                                            displayFormat,
                                                            format,
                                                            Device.Settings.depthBufferFormat))
                    {
                        return true;
                    }
                }
                else
                {
                    return true;
                }
            }

            return false;
        }
 public static FrameBuffer Create(Settings settings, D3d.Device device, System.Windows.Forms.Control renderWindow)
 {
     D3d.SwapChain swapChain = new D3d.SwapChain( device,settings.GetPresentParameters(renderWindow) );
     return null;
 }
 public D3DVertexDeclaration(D3D.Device device)
 {
     this.device = device;
 }
        public TextureCube CreateTextureCube(D3d.Format format, int size, bool dynamic, bool mipmap)
        {
            int height = 128;
            int depth = 128;

            if (ModifyParameters(D3d.ResourceType.CubeTexture, format, dynamic, mipmap, false, ref  size, ref height, ref depth))
            {
                TextureCube newTexture = TextureCube.Create(this, format, size, dynamic, mipmap);
                if (newTexture != null)
                {
                    textures.Add(newTexture);
                    return newTexture;
                }
            }

            return null;
        }
        public Texture2d CreateTexture2d(D3d.Format format, int width, int height, bool dynamic,bool mipmap )
        {
            int depth = 128;

            if (ModifyParameters(D3d.ResourceType.Textures, format, dynamic, mipmap, false, ref width, ref height, ref depth))
            {
                Texture2d newTexture = Texture2d.Create(this, format, width, height, dynamic, mipmap);
                if (newTexture != null)
                {
                    textures.Add(newTexture);
                    return newTexture;
                }
            }

            return null;
        }