/// <summary>
        /// Create Direct3D device and swap chain
        /// </summary>
        protected void InitDevice()
        {
            device    = D3DDevice.CreateDeviceAndSwapChain(directControl.Handle);
            swapChain = device.SwapChain;

            SetViews();

            // Create the effect
            using (FileStream effectStream = File.OpenRead("Tutorial04.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;

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

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

            InitMatrices();
            needsResizing = false;
        }
        /// <summary>
        /// Create Direct3D device and swap chain
        /// </summary>
        public void InitDevice()
        {
            device    = D3DDevice.CreateDeviceAndSwapChain(host.Handle);
            swapChain = device.SwapChain;

            // Create a render target view
            using (Texture2D pBuffer = swapChain.GetBuffer <Texture2D>(0))
            {
                renderTargetView = device.CreateRenderTargetView(pBuffer);
            }

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

            // Setup the viewport
            Viewport vp = new Viewport()
            {
                Width    = (uint)host.ActualWidth,
                Height   = (uint)host.ActualHeight,
                MinDepth = 0.0f,
                MaxDepth = 1.0f,
                TopLeftX = 0,
                TopLeftY = 0
            };

            device.RS.Viewports = new Viewport[] { vp };
        }
Exemple #3
0
        /// <summary>
        /// Create Direct3D device and swap chain
        /// </summary>
        protected void InitDevice()
        {
            device    = D3DDevice.CreateDeviceAndSwapChain(directControl.Handle);
            swapChain = device.SwapChain;

            // Create a render target view
            using (Texture2D pBuffer = swapChain.GetBuffer <Texture2D>(0))
            {
                renderTargetView = device.CreateRenderTargetView(pBuffer);
            }

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

            // Setup the viewport
            Viewport vp = new Viewport()
            {
                Width    = (uint)directControl.ClientSize.Width,
                Height   = (uint)directControl.ClientSize.Height,
                MinDepth = 0.0f,
                MaxDepth = 1.0f,
                TopLeftX = 0,
                TopLeftY = 0
            };

            device.RS.Viewports = new Viewport[] { vp };
        }
        void InitDevice()
        {
            // create Direct 3D device
            device    = D3DDevice.CreateDeviceAndSwapChain(renderHost.Handle);
            swapChain = device.SwapChain;

            // Create a render target view
            using (Texture2D pBuffer = swapChain.GetBuffer <Texture2D>(0))
            {
                renderTargetView = device.CreateRenderTargetView(pBuffer);
            }

            // Create depth stencil texture
            Texture2DDescription descDepth = new Texture2DDescription()
            {
                Width             = (uint)renderHost.ActualWidth,
                Height            = (uint)renderHost.ActualHeight,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = Format.D32Float,
                SampleDescription = new SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                BindingOptions = BindingOptions.DepthStencil,
            };

            depthStencil = device.CreateTexture2D(descDepth);

            // Create the depth stencil view
            DepthStencilViewDescription depthStencilViewDesc = new DepthStencilViewDescription()
            {
                Format        = descDepth.Format,
                ViewDimension = DepthStencilViewDimension.Texture2D
            };

            depthStencilView = device.CreateDepthStencilView(depthStencil, depthStencilViewDesc);

            // bind the views to the device
            device.OM.RenderTargets = new OutputMergerRenderTargets(new RenderTargetView[] { renderTargetView }, depthStencilView);

            // Setup the viewport
            Viewport vp = new Viewport()
            {
                Width    = (uint)renderHost.ActualWidth,
                Height   = (uint)renderHost.ActualHeight,
                MinDepth = 0.0f,
                MaxDepth = 1.0f,
                TopLeftX = 0,
                TopLeftY = 0
            };

            device.RS.Viewports = new Viewport[] { vp };
        }
Exemple #5
0
        /// <summary>
        /// Create Direct3D device and swap chain
        /// </summary>
        protected void InitDevice()
        {
            device    = D3DDevice.CreateDeviceAndSwapChain(directControl.Handle);
            swapChain = device.SwapChain;

            SetViews();

            meshManager = new XMeshManager(device);

            InitMatrices();
        }
Exemple #6
0
        /// <summary>
        /// Create Direct3D device and swap chain
        /// </summary>
        public void InitDevice()
        {
            device    = D3DDevice.CreateDeviceAndSwapChain(host.Handle);
            swapChain = device.SwapChain;

            SetViews();

            meshManager = new XMeshManager(device);
            mesh        = meshManager.Open("Media\\Tiger\\tiger.x");

            InitMatrices();
            needsResizing = false;
        }
Exemple #7
0
        /// <summary>
        /// Creates Direct3D device and swap chain,
        /// Initializes buffers,
        /// Loads and initializes the shader
        /// </summary>
        protected void InitializeDevice()
        {
            device    = D3DDevice.CreateDeviceAndSwapChain(directControl.Handle);
            swapChain = device.SwapChain;

            SetViews();

            effects = new Effects(device);

            InitializeVertexLayout();
            InitializeVertexBuffer();
            InitializeIndexBuffer();

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

            effects.ViewMatrix       = DXUtil.Camera.MatrixLookAtLH(Eye, At, Up);
            effects.ProjectionMatrix = DXUtil.Camera.MatrixPerspectiveFovLH((float)Math.PI * 0.25f, ((float)this.ClientSize.Width / (float)this.ClientSize.Height), 0.1f, 4000.0f);
        }
Exemple #8
0
        /// <summary>
        /// Create Direct3D device and swap chain
        /// </summary>
        protected void InitDevice()
        {
            device    = D3DDevice.CreateDeviceAndSwapChain(this.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;
            active = true;
        }
        private void InitializeDX()
        {
            //perform DX Device and Context initialization
            HwndSource hwnd = new HwndSource(0, 0, 0, 0, 0, "dxPlotterElement", IntPtr.Zero);

            device        = D3DDevice.CreateDeviceAndSwapChain(hwnd.Handle);
            swapChain     = device.SwapChain;
            deviceContext = device.ImmediateContext;

            CompositionTarget.Rendering += OnRenderingProccessReady;

            Texture2DDescription tdesc = new Texture2DDescription
            {
                ArraySize         = 1,
                Width             = 1,
                Height            = 1,
                Format            = Format.B8G8R8A8UNorm,
                MipLevels         = 1,
                SampleDescription = new SampleDescription {
                    Count = 1, Quality = 0
                },
                Usage          = Usage.Default,
                BindingOptions = BindingOptions.RenderTarget | BindingOptions.ShaderResource,
                MiscellaneousResourceOptions = MiscellaneousResourceOptions.Shared,
                CpuAccessOptions             = CpuAccessOptions.None
            };

            using (Texture2D texture2D = device.CreateTexture2D(tdesc))
            {
                renderTargetView = device.CreateRenderTargetView(texture2D);
                deviceContext.OM.RenderTargets = new OutputMergerRenderTargets(new RenderTargetView[] { renderTargetView });

                d3dImage.SetBackBufferEx(D3DResourceTypeEx.ID3D11Texture2D, texture2D.NativeInterface);
            }

            dxInitialized = true;
            OnDeviceInitialized();
        }
Exemple #10
0
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            // device creation
            device        = D3DDevice.CreateDeviceAndSwapChain(Handle);
            swapChain     = device.SwapChain;
            deviceContext = device.ImmediateContext;

            SetViews();

            // Open precompiled vertex shader
            // This file was compiled using: fxc Render.hlsl /T vs_4_0 /EVertShader /FoRender.vs
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.WindowsAPICodePack.Samples.Direct3D11.Render.vs"))
            {
                vertexShader = device.CreateVertexShader(stream);
            }

            deviceContext.VS.SetShader(vertexShader, null);

            // input layout is for the vert shader
            InputElementDescription inputElementDescription = new InputElementDescription();

            inputElementDescription.SemanticName         = "POSITION";
            inputElementDescription.SemanticIndex        = 0;
            inputElementDescription.Format               = Format.R32G32B32Float;
            inputElementDescription.InputSlot            = 0;
            inputElementDescription.AlignedByteOffset    = 0;
            inputElementDescription.InputSlotClass       = InputClassification.PerVertexData;
            inputElementDescription.InstanceDataStepRate = 0;

            InputLayout inputLayout;

            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.WindowsAPICodePack.Samples.Direct3D11.Render.vs"))
            {
                inputLayout = device.CreateInputLayout(new InputElementDescription[] { inputElementDescription }, stream);
            }
            deviceContext.IA.InputLayout = inputLayout;

            // Open precompiled vertex shader
            // This file was compiled using: fxc Render.hlsl /T ps_4_0 /EPixShader /FoRender.ps
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.WindowsAPICodePack.Samples.Direct3D11.Render.ps"))
            {
                pixelShader = device.CreatePixelShader(stream);
            }
            deviceContext.PS.SetShader(pixelShader, null);


            // create some geometry to draw (1 triangle)
            SimpleVertexArray vertex = new SimpleVertexArray();

            // put the vertices into a vertex buffer

            BufferDescription bufferDescription = new BufferDescription();

            bufferDescription.Usage          = Usage.Default;
            bufferDescription.ByteWidth      = (uint)Marshal.SizeOf(vertex);
            bufferDescription.BindingOptions = BindingOptions.VertexBuffer;

            SubresourceData subresourceData = new SubresourceData();

            IntPtr vertexData = Marshal.AllocCoTaskMem(Marshal.SizeOf(vertex));

            Marshal.StructureToPtr(vertex, vertexData, false);

            subresourceData.SystemMemory = vertexData;
            vertexBuffer = device.CreateBuffer(bufferDescription, subresourceData);


            deviceContext.IA.SetVertexBuffers(0, new D3DBuffer[] { vertexBuffer }, new uint[] { 12 }, new uint[] { 0 });
            deviceContext.IA.PrimitiveTopology = PrimitiveTopology.TriangleList;

            Marshal.FreeCoTaskMem(vertexData);
        }
Exemple #11
0
        /// <summary>
        /// Create Direct3D device and swap chain
        /// </summary>
        protected void InitDevice()
        {
            device    = D3DDevice.CreateDeviceAndSwapChain(directControl.Handle);
            swapChain = device.SwapChain;

            SetViews();

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

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

            // Define the input layout
            InputElementDescription[] layout =
            {
                new InputElementDescription()
                {
                    SemanticName         = "POSITION",
                    SemanticIndex        = 0,
                    Format               = Format.R32G32B32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 0,
                    InputSlotClass       = InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                }
            };

            PassDescription passDesc = technique.GetPassByIndex(0).Description;

            vertexLayout = device.CreateInputLayout(
                layout,
                passDesc.InputAssemblerInputSignature,
                passDesc.InputAssemblerInputSignatureSize);

            device.IA.InputLayout = vertexLayout;

            SimpleVertexArray vertex = new SimpleVertexArray();

            BufferDescription bd = new BufferDescription()
            {
                Usage                        = Usage.Default,
                ByteWidth                    = (uint)Marshal.SizeOf(vertex),
                BindingOptions               = BindingOptions.VertexBuffer,
                CpuAccessOptions             = CpuAccessOptions.None,
                MiscellaneousResourceOptions = MiscellaneousResourceOptions.None
            };

            IntPtr vertexData = Marshal.AllocCoTaskMem(Marshal.SizeOf(vertex));

            Marshal.StructureToPtr(vertex, vertexData, false);

            SubresourceData InitData = new SubresourceData()
            {
                SystemMemory           = vertexData,
                SystemMemoryPitch      = 0,
                SystemMemorySlicePitch = 0
            };

            //D3DBuffer buffer = null;
            vertexBuffer = device.CreateBuffer(bd, InitData);

            // Set vertex buffer
            uint stride = (uint)Marshal.SizeOf(typeof(Vector3F));
            uint offset = 0;

            device.IA.SetVertexBuffers(0, new Collection <D3DBuffer>()
            {
                vertexBuffer
            },
                                       new uint[] { stride }, new uint[] { offset });

            // Set primitive topology
            device.IA.PrimitiveTopology = PrimitiveTopology.TriangleList;
            Marshal.FreeCoTaskMem(vertexData);
        }