Example #1
0
 private void CreateDevice()
 {
     try
     {
         // Create device
         device = D3DDevice1.CreateDevice1(
             null,
             DriverType.Hardware,
             null,
             CreateDeviceOptions.SupportBgra,
             FeatureLevel.Ten);
     }
     catch (Exception)
     {
         // if we can't create a hardware device,
         // try the warp one
     }
     if (device == null)
     {
         device = D3DDevice1.CreateDevice1(
             null,
             DriverType.Software,
             "d3d10warp.dll",
             CreateDeviceOptions.SupportBgra,
             FeatureLevel.Ten);
     }
 }
Example #2
0
        private static D3DDevice1 TryCreateDevice1(DriverType type)
        {
            // We'll try to create the device that supports any of these feature levels
            FeatureLevel[] levels =
            {
                FeatureLevel.Ten,
                FeatureLevel.NinePointThree,
                FeatureLevel.NinePointTwo,
                FeatureLevel.NinePointOne
            };

            foreach (FeatureLevel level in levels)
            {
                try
                {
                    return(D3DDevice1.CreateDevice1(null, type, null,
                                                    CreateDeviceOptions.SupportBgra, level));
                }
                catch (ArgumentException)                 // E_INVALIDARG
                {
                }
                catch (OutOfMemoryException)                 // E_OUTOFMEMORY
                {
                }
                catch (DirectXException)                 // D3DERR_INVALIDCALL or E_FAIL
                {
                }
            }
            return(null);            // We failed to create a device at any required feature level
        }
        /// <summary>
        /// Create Direct3D device and swap chain
        /// </summary>
        protected void InitDevice()
        {
            device    = D3DDevice1.CreateDeviceAndSwapChain1(host.Handle);
            swapChain = device.SwapChain;

            SetViews();

            // Create the effect
            using (FileStream effectStream = File.OpenRead("Tutorial07.fxo"))
            {
                effect = device.CreateEffectFromCompiledBinary(new BinaryReader(effectStream));
            }

            // Obtain the technique
            technique = effect.GetTechniqueByName("Render");

            // Obtain the variables
            worldVariable      = effect.GetVariableByName("World").AsMatrix;
            viewVariable       = effect.GetVariableByName("View").AsMatrix;
            projectionVariable = effect.GetVariableByName("Projection").AsMatrix;
            meshColorVariable  = effect.GetVariableByName("vMeshColor").AsVector;
            diffuseVariable    = effect.GetVariableByName("txDiffuse").AsShaderResource;

            InitVertexLayout();
            InitVertexBuffer();
            InitIndexBuffer();

            // Set primitive topology
            device.IA.PrimitiveTopology = PrimitiveTopology.TriangleList;

            // Load the Texture
            using (FileStream stream = File.OpenRead("seafloor.png"))
            {
                textureRV = TextureLoader.LoadTexture(device, stream);
            }

            InitMatrices();

            diffuseVariable.Resource = textureRV;
            needsResizing            = false;
        }
Example #4
0
        /// <summary>
        ///         Creates a DirectX 10 device and related device specific resources.
        /// </summary>
        /// <exception cref="InvalidOperationException">
        ///         A previous call to CreateResources has not been followed by a call to
        ///         <see cref="FreeResources" />.
        /// </exception>
        /// <exception cref="ObjectDisposedException">
        ///         <see cref="Dispose()" /> has been called on this instance.
        /// </exception>
        /// <exception cref="Microsoft.WindowsAPICodePack.DirectX.DirectXException">
        ///         Unable to create a DirectX 10 device or an error occured creating
        ///         device dependent resources.
        /// </exception>
        public void CreateResources()
        {
            MustNotDisposed();
            if (_device != null)
            {
                throw new InvalidOperationException(
                          "A previous call to CreateResources has not been followed by a call to FreeResources.");
            }

            // Try to create a hardware device first and fall back to a
            // software (WARP doens't let us share resources)
            D3DDevice1 device1 = TryCreateDevice1(DriverType.Hardware);

            if (device1 == null)
            {
                device1 = TryCreateDevice1(DriverType.Software);
                if (device1 == null)
                {
                    throw new DirectXException("Unable to create a DirectX 10 device.");
                }
            }
            _device = device1.QueryInterface <D3DDevice>();
            device1.Dispose();
        }
Example #5
0
 private void CreateDevice()
 {
     try
     {
         // Create device
         device = D3DDevice1.CreateDevice1(
             null,
             DriverType.Hardware,
             null,
             CreateDeviceFlag.SupportBGRA,
             FeatureLevel.FeatureLevel_10_0);
     }
     catch (Exception)
     {
         // if we can't create a hardware device,
         // try the warp one
     }
     if (device == null)
     {
         device = D3DDevice1.CreateDevice1(
             null,
             DriverType.Software,
             "d3d10warp.dll",
             CreateDeviceFlag.SupportBGRA,
             FeatureLevel.FeatureLevel_10_0);
     }
 }
Example #6
0
        /// <summary>
        /// Create Direct3D device and swap chain
        /// </summary>
        protected void InitDevice()
        {
            device = D3DDevice1.CreateDeviceAndSwapChain1(directControl.Handle);
            swapChain = device.SwapChain;

            SetViews();

            // Create the effect
            using (FileStream effectStream = File.OpenRead("Tutorial07.fxo"))
            {
                effect = device.CreateEffectFromCompiledBinary(effectStream);
            }

            // Obtain the technique
            technique = effect.GetTechniqueByName("Render");

            // Obtain the variables
            worldVariable = effect.GetVariableByName("World").AsMatrix;
            viewVariable = effect.GetVariableByName("View").AsMatrix;
            projectionVariable = effect.GetVariableByName("Projection").AsMatrix;
            meshColorVariable = effect.GetVariableByName("vMeshColor").AsVector;
            diffuseVariable = effect.GetVariableByName("txDiffuse").AsShaderResource;

            InitVertexLayout();
            InitVertexBuffer();
            InitIndexBuffer();

            // Set primitive topology
            device.IA.PrimitiveTopology = PrimitiveTopology.TriangleList;

            // Load the Texture
            textureRV = TextureLoader.LoadTexture(device, "seafloor.png");

            InitMatrices();

            diffuseVariable.Resource = textureRV;
            needsResizing = false;
        }
Example #7
0
        void CreateDeviceResources()
        {
            uint nWidth  = (uint)host.ActualWidth;
            uint nHeight = (uint)host.ActualHeight;

            // Create D3D device and swap chain
            SwapChainDescription swapDesc = new SwapChainDescription
            {
                BufferDescription = new ModeDescription
                {
                    Width       = nWidth, Height = nHeight,
                    Format      = Format.R8G8B8A8UNorm,
                    RefreshRate = new Rational {
                        Numerator = 60, Denominator = 1
                    }
                },
                SampleDescription = new SampleDescription {
                    Count = 1, Quality = 0
                },
                BufferUsage        = UsageOptions.RenderTargetOutput,
                BufferCount        = 1,
                OutputWindowHandle = host.Handle,
                Windowed           = true
            };

            device = D3DDevice1.CreateDeviceAndSwapChain1(
                null,
                DriverType.Hardware,
                null,
                CreateDeviceOptions.SupportBgra,
                FeatureLevel.NinePointThree,
                swapDesc
                );
            swapChain = device.SwapChain;

            using (Texture2D pBuffer = swapChain.GetBuffer <Texture2D>(0))
            {
                renderTargetView = device.CreateRenderTargetView(pBuffer);
            }

            MakeBothSidesRendered();
            InitializeDepthStencil(nWidth, nHeight);

            device.OM.RenderTargets = new OutputMergerRenderTargets(new[] { renderTargetView }, depthStencilView);

            // Set a new viewport based on the new dimensions
            SetViewport(nWidth, nHeight);

            // Load pixel shader
            shader = LoadResourceShader(device, "Microsoft.WindowsAPICodePack.DirectX.Samples.dxgisample.fxo");

            // Obtain the technique
            technique = shader.GetTechniqueByName("Render");

            // Create the input layout
            InitializeGeometryBuffers();

            // Obtain the variables
            Initialize3DTransformations(nWidth, nHeight);

            // Allocate a offscreen D3D surface for D2D to render our 2D content into
            InitializeTextureRenderTarget();

            // Create a D2D render target which can draw into the surface in the swap chain
            CreateD2DRenderTargets();
        }
Example #8
0
        void CreateDeviceResources()
        {
            uint nWidth = (uint)host.ActualWidth;
            uint nHeight = (uint)host.ActualHeight;

            // Create D3D device and swap chain
            SwapChainDescription swapDesc = new SwapChainDescription
            {
                BufferDescription = new ModeDescription
                {
                    Width = nWidth, Height = nHeight,
                    Format = Format.R8G8B8A8UNorm,
                    RefreshRate = new Rational { Numerator = 60, Denominator = 1 }
                },
                SampleDescription = new SampleDescription { Count = 1, Quality = 0 },
                BufferUsage = UsageOptions.RenderTargetOutput,
                BufferCount = 1,
                OutputWindowHandle = host.Handle,
                Windowed = true
            };

            device = D3DDevice1.CreateDeviceAndSwapChain1(
                null,
                DriverType.Hardware,
                null,
                CreateDeviceOptions.SupportBgra,
                FeatureLevel.NinePointThree,
                swapDesc
                );
            swapChain = device.SwapChain;

            using (Texture2D pBuffer = swapChain.GetBuffer<Texture2D>(0))
            {
                renderTargetView = device.CreateRenderTargetView(pBuffer);
            }

            MakeBothSidesRendered();
            InitializeDepthStencil(nWidth, nHeight);

            device.OM.RenderTargets = new OutputMergerRenderTargets(new[] { renderTargetView }, depthStencilView);

            // Set a new viewport based on the new dimensions
            SetViewport(nWidth, nHeight);

            // Load pixel shader
            shader = LoadResourceShader(device, "Microsoft.WindowsAPICodePack.DirectX.Samples.dxgisample.fxo");

            // Obtain the technique
            technique = shader.GetTechniqueByName("Render");

            // Create the input layout
            InitializeGeometryBuffers();

            // Obtain the variables
            Initialize3DTransformations(nWidth, nHeight);

            // Allocate a offscreen D3D surface for D2D to render our 2D content into
            InitializeTextureRenderTarget();

            // Create a D2D render target which can draw into the surface in the swap chain
            CreateD2DRenderTargets();
        }
Example #9
0
        void CreateDeviceResources()
        {
            uint nWidth = (uint)host.ActualWidth;
            uint nHeight = (uint)host.ActualHeight;

            // Create D3D device and swap chain
            SwapChainDescription swapDesc = new SwapChainDescription();
            swapDesc.BufferDescription.Width = nWidth;
            swapDesc.BufferDescription.Height = nHeight;
            swapDesc.BufferDescription.Format = Format.R8G8B8A8_UNORM;
            swapDesc.BufferDescription.RefreshRate.Numerator = 60;
            swapDesc.BufferDescription.RefreshRate.Denominator = 1;
            swapDesc.SampleDescription.Count = 1;
            swapDesc.SampleDescription.Quality = 0;
            swapDesc.BufferUsage = UsageOption.RenderTargetOutput;
            swapDesc.BufferCount = 1;
            swapDesc.OutputWindowHandle = host.Handle;
            swapDesc.Windowed = true;
            device = D3DDevice1.CreateDeviceAndSwapChain1(
                null,
                DriverType.Hardware,
                null,
                CreateDeviceFlag.SupportBGRA,
                FeatureLevel.FeatureLevel_10_0,
                swapDesc,
                out swapChain
                );

            using (Texture2D pBuffer = swapChain.GetBuffer<Texture2D>(0))
            {
                renderTargetView = device.CreateRenderTargetView(pBuffer);
            }

            MakeBothSidesRendered();
            InitializeDepthStencil(nWidth, nHeight);

            device.OM.SetRenderTargets(new[] { renderTargetView }, depthStencilView);

            // Set a new viewport based on the new dimensions
            SetViewport(nWidth, nHeight);

            // Load pixel shader
            shader = LoadResourceShader(device, "Microsoft.WindowsAPICodePack.DirectX.Samples.dxgisample.fxo");

            // Obtain the technique
            technique = shader.GetTechniqueByName("Render");

            // Create the input layout
            InitializeGeometryBuffers();

            // Obtain the variables
            Initialize3DTransformations(nWidth, nHeight);

            // Allocate a offscreen D3D surface for D2D to render our 2D content into
            InitializeTextureRenderTarget();

            // Create a D2D render target which can draw into the surface in the swap chain
            CreateD2DRenderTargets();
        }