/// <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; }
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(); }