public void CreateDeviceDependentResources(DeviceResources resources)
        {
            this.deviceResources = resources;

            var d3dDevice = this.deviceResources.D3DDevice;

            // Create the shaders
            byte[] vertexShaderBytecode = File.ReadAllBytes("VertexShader.cso");
            this.g_pVertexShader       = d3dDevice.CreateVertexShader(vertexShaderBytecode, null);
            this.g_pHullShaderInteger  = d3dDevice.CreateHullShader(File.ReadAllBytes("HullShaderInteger.cso"), null);
            this.g_pHullShaderFracEven = d3dDevice.CreateHullShader(File.ReadAllBytes("HullShaderFracEven.cso"), null);
            this.g_pHullShaderFracOdd  = d3dDevice.CreateHullShader(File.ReadAllBytes("HullShaderFracOdd.cso"), null);
            this.g_pDomainShader       = d3dDevice.CreateDomainShader(File.ReadAllBytes("DomainShader.cso"), null);
            this.g_pPixelShader        = d3dDevice.CreatePixelShader(File.ReadAllBytes("PixelShader.cso"), null);
            this.g_pSolidColorPS       = d3dDevice.CreatePixelShader(File.ReadAllBytes("SolidColorPS.cso"), null);

            // Create our vertex input layout - this matches the BEZIER_CONTROL_POINT structure
            D3D11InputElementDesc[] layoutDesc = new D3D11InputElementDesc[]
            {
                new D3D11InputElementDesc
                {
                    SemanticName         = "POSITION",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 0,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                }
            };

            this.g_pPatchLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, vertexShaderBytecode);

            // Create constant buffers
            this.g_pcbPerFrame = d3dDevice.CreateBuffer(new D3D11BufferDesc(ConstantBufferConstants.Size, D3D11BindOptions.ConstantBuffer));

            // Create solid and wireframe rasterizer state objects
            D3D11RasterizerDesc rasterDesc = D3D11RasterizerDesc.Default;

            rasterDesc.CullMode           = D3D11CullMode.None;
            rasterDesc.IsDepthClipEnabled = true;

            rasterDesc.FillMode          = D3D11FillMode.Solid;
            this.g_pRasterizerStateSolid = d3dDevice.CreateRasterizerState(rasterDesc);

            rasterDesc.FillMode = D3D11FillMode.WireFrame;
            this.g_pRasterizerStateWireframe = d3dDevice.CreateRasterizerState(rasterDesc);

            D3D11BufferDesc vbdesc = D3D11BufferDesc.From(MobiusStrip.Points, D3D11BindOptions.VertexBuffer);

            this.g_pControlPointVB = d3dDevice.CreateBuffer(vbdesc, MobiusStrip.Points, 0, 0);

            XMFloat3 vecEye = new XMFloat3(1.0f, 1.5f, -3.5f);
            XMFloat3 vecAt  = new XMFloat3(0.0f, 0.0f, 0.0f);
            XMFloat3 vecUp  = new XMFloat3(0.0f, 1.0f, 0.0f);

            this.ViewMatrix = XMMatrix.LookAtLH(vecEye, vecAt, vecUp);
            this.EyePt      = vecEye;
        }
        public void CreateDeviceDependentResources(DeviceResources resources)
        {
            this.deviceResources = resources;

            //string fileName = Path.GetDirectoryName(this.OptFileName) + "\\" + Path.GetFileNameWithoutExtension(this.OptFileName) + "Exterior.opt";

            //OptFile opt;
            //if (File.Exists(fileName))
            //{
            //    opt = OptFile.FromFile(fileName);
            //}
            //else
            //{
            //    opt = OptFile.FromFile(this.OptFileName);
            //}

            OptFile opt = OptFile.FromFile(this.OptFileName);

            this.OptSize     = opt.Size * OptFile.ScaleFactor;
            this.OptSpanSize = opt.SpanSize.Scale(OptFile.ScaleFactor, OptFile.ScaleFactor, OptFile.ScaleFactor);

            Vector max = opt.MaxSize;
            Vector min = opt.MinSize;

            this.OptCenter = new Vector()
            {
                X = (max.X + min.X) / 2,
                Y = (max.Y + min.Y) / 2,
                Z = (max.Z + min.Z) / 2
            }.Scale(OptFile.ScaleFactor, OptFile.ScaleFactor, OptFile.ScaleFactor);

            this.CreateTextures(opt);
            this.CreateMeshes(opt);

            byte[] vertexShaderBytecode = File.ReadAllBytes("VertexShader.cso");
            this.vertexShader = this.deviceResources.D3DDevice.CreateVertexShader(vertexShaderBytecode, null);

            D3D11InputElementDesc[] basicVertexLayoutDesc = new D3D11InputElementDesc[]
            {
                new D3D11InputElementDesc
                {
                    SemanticName         = "POSITION",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 0,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                },
                new D3D11InputElementDesc
                {
                    SemanticName         = "NORMAL",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 12,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                },
                new D3D11InputElementDesc
                {
                    SemanticName         = "TEXCOORD",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 24,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                }
            };

            this.inputLayout = this.deviceResources.D3DDevice.CreateInputLayout(basicVertexLayoutDesc, vertexShaderBytecode);

            byte[] pixelShaderBytecode = File.ReadAllBytes("PixelShader.cso");
            this.pixelShader = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null);

            var constantBufferDesc = new D3D11BufferDesc(ConstantBufferData.Size, D3D11BindOptions.ConstantBuffer);

            this.constantBuffer = this.deviceResources.D3DDevice.CreateBuffer(constantBufferDesc);

            D3D11SamplerDesc samplerDesc = new D3D11SamplerDesc(
                D3D11Filter.Anisotropic,
                D3D11TextureAddressMode.Wrap,
                D3D11TextureAddressMode.Wrap,
                D3D11TextureAddressMode.Wrap,
                0.0f,
                this.deviceResources.D3DFeatureLevel > D3D11FeatureLevel.FeatureLevel91 ? D3D11Constants.DefaultMaxAnisotropy : D3D11Constants.FeatureLevel91DefaultMaxAnisotropy,
                D3D11ComparisonFunction.Never,
                new float[] { 0.0f, 0.0f, 0.0f, 0.0f },
                0.0f,
                float.MaxValue);

            this.sampler = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc);

            D3D11RasterizerDesc rasterizerDesc = D3D11RasterizerDesc.Default;

            rasterizerDesc.CullMode = D3D11CullMode.None;
            this.rasterizerState    = this.deviceResources.D3DDevice.CreateRasterizerState(rasterizerDesc);

            this.depthStencilState0 = this.deviceResources.D3DDevice.CreateDepthStencilState(D3D11DepthStencilDesc.Default);

            D3D11DepthStencilDesc depthStencilDesc = D3D11DepthStencilDesc.Default;

            depthStencilDesc.DepthWriteMask = D3D11DepthWriteMask.Zero;
            this.depthStencilState1         = this.deviceResources.D3DDevice.CreateDepthStencilState(depthStencilDesc);

            this.blendState0 = this.deviceResources.D3DDevice.CreateBlendState(D3D11BlendDesc.Default);

            D3D11BlendDesc blendDesc = D3D11BlendDesc.Default;

            D3D11RenderTargetBlendDesc[] blendDescRenderTargets = blendDesc.GetRenderTargets();
            blendDescRenderTargets[0].IsBlendEnabled        = true;
            blendDescRenderTargets[0].SourceBlend           = D3D11BlendValue.SourceAlpha;
            blendDescRenderTargets[0].DestinationBlend      = D3D11BlendValue.InverseSourceAlpha;
            blendDescRenderTargets[0].BlendOperation        = D3D11BlendOperation.Add;
            blendDescRenderTargets[0].SourceBlendAlpha      = D3D11BlendValue.One;
            blendDescRenderTargets[0].DestinationBlendAlpha = D3D11BlendValue.InverseSourceAlpha;
            blendDescRenderTargets[0].BlendOperationAlpha   = D3D11BlendOperation.Add;
            blendDescRenderTargets[0].RenderTargetWriteMask = D3D11ColorWriteEnables.All;
            blendDesc.SetRenderTargets(blendDescRenderTargets);
            this.blendState1 = this.deviceResources.D3DDevice.CreateBlendState(blendDesc);
        }
예제 #3
0
        private void CreateWindowSizeDependentResources()
        {
            this.d3dContext.OutputMergerSetRenderTargets(new D3D11RenderTargetView[] { null }, null);

            D3D11Utils.DisposeAndNull(ref this.backBuffer);
            D3D11Utils.DisposeAndNull(ref this.offscreenBuffer);
            D3D11Utils.DisposeAndNull(ref this.d3dRenderTargetView);
            D3D11Utils.DisposeAndNull(ref this.d3dDepthStencilView);
            D2D1Utils.DisposeAndNull(ref this.d2dRenderTarget);

            this.d3dContext.Flush();

            var createdBackBuffer = this.OnCreateBackBuffer();

            if (createdBackBuffer == null)
            {
                return;
            }

            this.backBuffer = createdBackBuffer;

            var backBufferDesc = this.backBuffer.Description;

            this.backBufferWidth  = backBufferDesc.Width;
            this.backBufferHeight = backBufferDesc.Height;

            if (this.d3dSampleDesc.Count > 1)
            {
                D3D11Texture2DDesc desc = new D3D11Texture2DDesc(
                    DxgiFormat.B8G8R8A8UNorm,
                    this.backBufferWidth,
                    this.backBufferHeight,
                    1,
                    1,
                    D3D11BindOptions.RenderTarget,
                    D3D11Usage.Default,
                    D3D11CpuAccessOptions.None,
                    this.d3dSampleDesc.Count,
                    this.d3dSampleDesc.Quality,
                    D3D11ResourceMiscOptions.None);

                this.offscreenBuffer = this.D3DDevice.CreateTexture2D(desc);
            }

            if (this.d3dSampleDesc.Count > 1)
            {
                D3D11RenderTargetViewDesc renderTargetViewDesc = new D3D11RenderTargetViewDesc(D3D11RtvDimension.Texture2DMs);

                this.d3dRenderTargetView = this.d3dDevice.CreateRenderTargetView(this.offscreenBuffer, renderTargetViewDesc);
            }
            else
            {
                D3D11RenderTargetViewDesc renderTargetViewDesc = new D3D11RenderTargetViewDesc(D3D11RtvDimension.Texture2D);

                this.d3dRenderTargetView = this.d3dDevice.CreateRenderTargetView(this.backBuffer, renderTargetViewDesc);
            }

            D3D11Texture2DDesc depthStencilDesc = new D3D11Texture2DDesc
            {
                Width            = this.backBufferWidth,
                Height           = this.backBufferHeight,
                MipLevels        = 1,
                ArraySize        = 1,
                Format           = DxgiFormat.D24UNormS8UInt,
                SampleDesc       = this.d3dSampleDesc,
                Usage            = D3D11Usage.Default,
                BindOptions      = D3D11BindOptions.DepthStencil,
                CpuAccessOptions = D3D11CpuAccessOptions.None,
                MiscOptions      = D3D11ResourceMiscOptions.None
            };

            using (var depthStencil = this.d3dDevice.CreateTexture2D(depthStencilDesc))
            {
                D3D11DepthStencilViewDesc depthStencilViewDesc = new D3D11DepthStencilViewDesc(this.d3dSampleDesc.Count > 1 ? D3D11DsvDimension.Texture2DMs : D3D11DsvDimension.Texture2D);

                this.d3dDepthStencilView = this.d3dDevice.CreateDepthStencilView(depthStencil, depthStencilViewDesc);
            }

            this.screenViewport = new D3D11Viewport
            {
                TopLeftX = 0,
                TopLeftY = 0,
                Width    = this.backBufferWidth,
                Height   = this.backBufferHeight,
                MinDepth = 0.0f,
                MaxDepth = 1.0f
            };

            this.d3dContext.RasterizerStageSetViewports(new[] { this.screenViewport });

            using (var surface = new DxgiSurface2(this.d3dSampleDesc.Count > 1 ? this.offscreenBuffer.Handle : this.backBuffer.Handle))
            {
                float dpiX;
                float dpiY;
                this.d2dFactory.GetDesktopDpi(out dpiX, out dpiY);

                var properties = new D2D1RenderTargetProperties(
                    D2D1RenderTargetType.Default,
                    new D2D1PixelFormat(DxgiFormat.B8G8R8A8UNorm, D2D1AlphaMode.Premultiplied),
                    dpiX,
                    dpiY,
                    D2D1RenderTargetUsages.None,
                    D2D1FeatureLevel.Default);

                this.d2dRenderTarget = this.d2dFactory.CreateDxgiSurfaceRenderTarget(surface, properties);
            }

            this.d2dRenderTarget.AntialiasMode     = D2D1AntialiasMode.PerPrimitive;
            this.d2dRenderTarget.TextAntialiasMode = D2D1TextAntialiasMode.Grayscale;

            D3D11RasterizerDesc rasterizerStateDesc = new D3D11RasterizerDesc(D3D11FillMode.Solid, D3D11CullMode.Back, false, 0, 0.0f, 0.0f, true, false, true, false);

            using (var rasterizerState = this.d3dDevice.CreateRasterizerState(rasterizerStateDesc))
            {
                this.d3dContext.RasterizerStageSetState(rasterizerState);
            }
        }