Example #1
0
        public void CreateDeviceDependentResources(DeviceResources resources)
        {
            this.deviceResources = resources;

            this.fragmentCountPS = this.deviceResources.D3DDevice.CreatePixelShader(
                File.ReadAllBytes("FragmentCountPS.cso"),
                null);

            this.createPrefixSumPass0CS = this.deviceResources.D3DDevice.CreateComputeShader(
                File.ReadAllBytes("CreatePrefixSumPass0CS.cso"),
                null);

            this.createPrefixSumPass1CS = this.deviceResources.D3DDevice.CreateComputeShader(
                File.ReadAllBytes("CreatePrefixSumPass1CS.cso"),
                null);

            this.fillDeepBufferPS = this.deviceResources.D3DDevice.CreatePixelShader(
                File.ReadAllBytes("FillDeepBufferPS.cso"),
                null);

            this.sortAndRenderCS = this.deviceResources.D3DDevice.CreateComputeShader(
                File.ReadAllBytes("SortAndRenderCS.cso"),
                null);

            var depthStencilDesc = new D3D11DepthStencilDesc
            {
                IsDepthEnabled   = false,
                IsStencilEnabled = false
            };

            this.depthStencilState = this.deviceResources.D3DDevice.CreateDepthStencilState(depthStencilDesc);

            this.computeShaderConstantBuffer = this.deviceResources.D3DDevice.CreateBuffer(
                new D3D11BufferDesc(OitComputeShaderConstantBufferData.Size, D3D11BindOptions.ConstantBuffer));

            this.pixelShaderConstantBuffer = this.deviceResources.D3DDevice.CreateBuffer(
                new D3D11BufferDesc(OitPixelShaderConstantBufferData.Size, D3D11BindOptions.ConstantBuffer));
        }
Example #2
0
        public void CreateDeviceDependentResources(DeviceResources resources)
        {
            this.deviceResources = resources;

            var d3dDevice = this.deviceResources.D3DDevice;

            // Create the shaders
            byte[] renderParticlesVSBytecode = File.ReadAllBytes("ParticleDrawVS.cso");
            this.g_pRenderParticlesVS = d3dDevice.CreateVertexShader(renderParticlesVSBytecode, null);
            this.g_pRenderParticlesGS = d3dDevice.CreateGeometryShader(File.ReadAllBytes("ParticleDrawGS.cso"), null);
            this.g_pRenderParticlesPS = d3dDevice.CreatePixelShader(File.ReadAllBytes("ParticleDrawPS.cso"), null);
            this.g_pCalcCS            = d3dDevice.CreateComputeShader(File.ReadAllBytes("NBodyGravityCS.cso"), null);

            // Create our vertex input layout
            D3D11InputElementDesc[] layoutDesc = new D3D11InputElementDesc[]
            {
                new D3D11InputElementDesc
                {
                    SemanticName         = "COLOR",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32A32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 0,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                }
            };

            this.g_pParticleVertexLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, renderParticlesVSBytecode);

            this.CreateParticleBuffer();
            this.CreateParticlePosVeloBuffers();

            // Setup constant buffer
            this.g_pcbGS = d3dDevice.CreateBuffer(new D3D11BufferDesc(ConstantBufferGS.Size, D3D11BindOptions.ConstantBuffer));
            this.g_pcbCS = d3dDevice.CreateBuffer(new D3D11BufferDesc(ConstantBufferCS.Size, D3D11BindOptions.ConstantBuffer));

            // Load the Particle Texture
            DdsDirectX.CreateTexture(
                "Particle.dds",
                this.deviceResources.D3DDevice,
                this.deviceResources.D3DContext,
                out this.g_pParticleTexRV);

            D3D11SamplerDesc SamplerDesc = D3D11SamplerDesc.Default;

            SamplerDesc.AddressU      = D3D11TextureAddressMode.Clamp;
            SamplerDesc.AddressV      = D3D11TextureAddressMode.Clamp;
            SamplerDesc.AddressW      = D3D11TextureAddressMode.Clamp;
            SamplerDesc.Filter        = D3D11Filter.MinMagMipLinear;
            this.g_pSampleStateLinear = d3dDevice.CreateSamplerState(SamplerDesc);

            D3D11BlendDesc BlendStateDesc = D3D11BlendDesc.Default;

            D3D11RenderTargetBlendDesc[] BlendStateDescRenderTargets = BlendStateDesc.GetRenderTargets();
            BlendStateDescRenderTargets[0].IsBlendEnabled        = true;
            BlendStateDescRenderTargets[0].BlendOperation        = D3D11BlendOperation.Add;
            BlendStateDescRenderTargets[0].SourceBlend           = D3D11BlendValue.SourceAlpha;
            BlendStateDescRenderTargets[0].DestinationBlend      = D3D11BlendValue.One;
            BlendStateDescRenderTargets[0].BlendOperationAlpha   = D3D11BlendOperation.Add;
            BlendStateDescRenderTargets[0].SourceBlendAlpha      = D3D11BlendValue.Zero;
            BlendStateDescRenderTargets[0].DestinationBlendAlpha = D3D11BlendValue.Zero;
            BlendStateDescRenderTargets[0].RenderTargetWriteMask = D3D11ColorWriteEnables.All;
            BlendStateDesc.SetRenderTargets(BlendStateDescRenderTargets);
            this.g_pBlendingStateParticle = d3dDevice.CreateBlendState(BlendStateDesc);

            D3D11DepthStencilDesc DepthStencilDesc = D3D11DepthStencilDesc.Default;

            DepthStencilDesc.IsDepthEnabled = false;
            DepthStencilDesc.DepthWriteMask = D3D11DepthWriteMask.Zero;
            this.g_pDepthStencilState       = d3dDevice.CreateDepthStencilState(DepthStencilDesc);

            XMFloat3 eye = new XMFloat3(-Spread * 2, Spread * 4, -Spread * 3);
            XMFloat3 at  = new XMFloat3(0.0f, 0.0f, 0.0f);
            XMFloat3 up  = new XMFloat3(0.0f, 1.0f, 0.0f);

            this.ViewMatrix = XMMatrix.LookAtLH(eye, at, up);
        }
        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);
        }
Example #4
0
        public IDisplay Create(string title, int width, int height, IAdapter adapter)
        {
            const uint defaultRefreshRate = 144;
            const bool debug = true;

            var window = _windowCreator.CreateWindow(new CreateWindowArguments(title, width, height));

            // Create device
            var d3D11Device = _d3D11DeviceFactory.Create(new CreateDeviceArguments(window, defaultRefreshRate, adapter?.Handle ?? IntPtr.Zero, debug));

            // Created the backbuffer and the RenderTargetView
            using var backBuffer = d3D11Device.SwapChain.GetBuffer(0, D3D11Resources.D3D11Resource);
            var renderTarget = d3D11Device.CreateRenderTargetView(backBuffer);


            D3D11Texture2DDesc sTarget = default;

            sTarget.Height             = (uint)window.Height;
            sTarget.Width              = (uint)window.Width;
            sTarget.MipLevels          = 1u;
            sTarget.ArraySize          = 1u;
            sTarget.Format             = DxgiFormat.R8G8B8A8Unorm;
            sTarget.SampleDesc.Count   = 1;
            sTarget.SampleDesc.Quality = 0;
            sTarget.Usage              = D3D11Usage.Default;
            sTarget.BindFlags          = D3D11BindFlag.RenderTarget;

            //var d3D11Texture2D = d3D11Device.CreateTexture2D(sTarget); // TODO: this is getting leaked
            //var secondRenderTarget = d3D11Device.CreateRenderTargetView(d3D11Texture2D); // TODO: this is getting leaked


            // Create the DepthStencilView (z-buffering)
            D3D11DepthStencilDesc depthDesc = default;

            depthDesc.DepthEnable = true;
            //depthDesc.DepthWriteMask = D3D11DepthWriteMask.Zero;
            //depthDesc.DepthFunc = D3D11ComparisonFunc.LessEqual;  // this is needed to add alpha-blending to sprites, not sure why.
            depthDesc.DepthWriteMask    = D3D11DepthWriteMask.All;
            depthDesc.DepthFunc         = D3D11ComparisonFunc.LessEqual; // this is needed to add alpha-blending to sprites, not sure why.
            using var depthStencilState = d3D11Device.CreateDepthStencilState(depthDesc);
            d3D11Device.Context.OMSetDepthStencilState(depthStencilState, 1u);

            D3D11Texture2DDesc texture2DDesc = default;

            texture2DDesc.Height    = (uint)window.Height;
            texture2DDesc.Width     = (uint)window.Width;
            texture2DDesc.MipLevels = 1u;
            texture2DDesc.ArraySize = 1u;
            texture2DDesc.Format    = DxgiFormat.D32Float;
            //texture2DDesc.SampleDesc.Count = 4u;  // AA
            texture2DDesc.SampleDesc.Count   = 1;
            texture2DDesc.SampleDesc.Quality = 0;
            texture2DDesc.Usage     = D3D11Usage.Default;
            texture2DDesc.BindFlags = D3D11BindFlag.DepthStencil;
            using var depthStencil  = d3D11Device.CreateTexture2D(texture2DDesc);

            D3D11DepthStencilViewDesc viewDesc = default;

            viewDesc.Format        = DxgiFormat.D32Float;
            viewDesc.ViewDimension = D3D11DsvDimension.Texture2D;
            //viewDesc.ViewDimension = D3D11DsvDimension.Texture2Dms; // AA
            viewDesc.Texture2D.MipSlice = 0u;
            var depthStencilView = d3D11Device.CreateDepthStencilView(depthStencil, viewDesc);

            // Set the render target view and depthStencil
            unsafe
            {
                var renderTargets = stackalloc IntPtr[1];
                renderTargets[0] = renderTarget.Handle;
                d3D11Device.Context.OMSetRenderTargets(renderTargets, 1, depthStencilView.Handle);
            }



            // Set the default viewport
            D3D11Viewport viewport = default;

            viewport.Width    = window.Width;
            viewport.Height   = window.Height;
            viewport.MinDepth = 0;
            viewport.MaxDepth = 1;

            d3D11Device.Context.SetViewport(viewport);


            // Create the device abstraction
            var device = new Device(d3D11Device, renderTarget, depthStencilView, _compiler);

            return(new Display(device, window));
        }