Ejemplo n.º 1
0
        private D3D11.ShaderResourceView CreateShaderResourceView(D3D11.Device device, Format format, D3D11.Texture2D texture)
        {
            var srvDesc = new D3D11.ShaderResourceViewDescription();

            srvDesc.Format    = format;
            srvDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.TextureCube;
            srvDesc.TextureCube.MostDetailedMip = 0;
            srvDesc.TextureCube.MipLevels       = -1;
            return(new D3D11.ShaderResourceView(device, texture, srvDesc));
        }
Ejemplo n.º 2
0
        public void CreateResources(D3D11.Device device, int sampleCount, int sampleQuality, int width, int height)
        {
            FieldOfView = width / (float)height;
            // render target
            D3D11.Texture2DDescription targetTextureDesc = new D3D11.Texture2DDescription()
            {
                Format            = DXGI.Format.R8G8B8A8_UNorm,
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = width,
                Height            = height,
                SampleDescription = new DXGI.SampleDescription(sampleCount, sampleQuality),
                Usage             = D3D11.ResourceUsage.Default,
                BindFlags         = D3D11.BindFlags.RenderTarget | D3D11.BindFlags.ShaderResource,
                CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                OptionFlags       = D3D11.ResourceOptionFlags.None
            };
            using (D3D11.Texture2D target = new D3D11.Texture2D(device, targetTextureDesc)) {
                renderTargetResource = new D3D11.ShaderResourceView(device, target);
                renderTargetView     = new D3D11.RenderTargetView(device, target);
            }

            // depth buffer
            D3D11.Texture2DDescription depthTextureDesc = new D3D11.Texture2DDescription()
            {
                Format            = DXGI.Format.R32_Typeless,
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = width,
                Height            = height,
                SampleDescription = new DXGI.SampleDescription(sampleCount, sampleQuality),
                Usage             = D3D11.ResourceUsage.Default,
                BindFlags         = D3D11.BindFlags.DepthStencil | D3D11.BindFlags.ShaderResource,
                CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                OptionFlags       = D3D11.ResourceOptionFlags.None
            };
            D3D11.DepthStencilViewDescription depthViewDesc = new D3D11.DepthStencilViewDescription()
            {
                Flags     = D3D11.DepthStencilViewFlags.None,
                Dimension = D3D11.DepthStencilViewDimension.Texture2D,
                Format    = DXGI.Format.D32_Float,
            };
            D3D11.ShaderResourceViewDescription depthResourceDesc = new D3D11.ShaderResourceViewDescription()
            {
                Format    = DXGI.Format.R32_Float,
                Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D
            };
            depthResourceDesc.Texture2D.MipLevels = 1;
            using (D3D11.Texture2D depthTexture = new D3D11.Texture2D(device, depthTextureDesc)) {
                depthStencilView     = new D3D11.DepthStencilView(device, depthTexture, depthViewDesc);
                depthStencilResource = new D3D11.ShaderResourceView(device, depthTexture, depthResourceDesc);
            }
        }
Ejemplo n.º 3
0
        public void CreateDepthStencil(
            D3D11.Device device,
            D3D11.Texture2DDescription textureDesc,
            D3D11.DepthStencilViewDescription viewDesc,
            D3D11.ShaderResourceViewDescription resourceViewDesc)
        {
            depthStencil     = new D3D11.Texture2D(device, textureDesc);
            depthStencilSwap = new D3D11.Texture2D(device, textureDesc);


            defaultRenderViews.DSV = new D3D11.DepthStencilView(device, depthStencil, viewDesc);
            swapRenderViews.DSV    = new D3D11.DepthStencilView(device, depthStencil, viewDesc);

            defaultRenderViews.DS_SRVT = new D3D11.ShaderResourceView(device, depthStencil, resourceViewDesc);
            swapRenderViews.DS_SRVT    = new D3D11.ShaderResourceView(device, depthStencil, resourceViewDesc);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Function to perform initialization of the shader view resource.
        /// </summary>
        protected override void OnInitialize()
        {
            Gorgon.Log.Print("Creating buffer shader view for {0}.", LoggingLevel.Verbose, Resource.Name);

            var desc = new D3D.ShaderResourceViewDescription
            {
                BufferEx = new D3D.ShaderResourceViewDescription.ExtendedBufferResource
                {
                    FirstElement = ElementStart,
                    ElementCount = ElementCount,
                    Flags        = IsRaw ? D3D.ShaderResourceViewExtendedBufferFlags.Raw : D3D.ShaderResourceViewExtendedBufferFlags.None
                },
                Dimension = DXCommon.ShaderResourceViewDimension.ExtendedBuffer,
                Format    = (GI.Format)Format
            };

            D3DView = new D3D.ShaderResourceView(Resource.Graphics.D3DDevice, Resource.D3DResource, desc)
            {
                DebugName = "Gorgon Shader View for " + Resource.GetType().FullName
            };
        }
Ejemplo n.º 5
0
        private void BuildBuffers()
        {
            D3D11.BufferDescription buf_desc = new D3D11.BufferDescription();
            buf_desc.BindFlags           = D3D11.BindFlags.ShaderResource | D3D11.BindFlags.UnorderedAccess;
            buf_desc.Usage               = D3D11.ResourceUsage.Default;
            buf_desc.StructureByteStride = WaveSolution.Stride;
            buf_desc.SizeInBytes         = WaveSolution.Stride * VertexCount;
            buf_desc.OptionFlags         = D3D11.ResourceOptionFlags.BufferStructured;
            buf_desc.CpuAccessFlags      = D3D11.CpuAccessFlags.Write | D3D11.CpuAccessFlags.Read;

            _inputBuf1 = new D3D11.Buffer(Device, buf_desc);
            _inputBuf2 = new D3D11.Buffer(Device, buf_desc);

            D3D11.ShaderResourceViewDescription vdesc = new D3D11.ShaderResourceViewDescription();
            vdesc.Dimension           = D3D.ShaderResourceViewDimension.Buffer;
            vdesc.Format              = DXGI.Format.Unknown;
            vdesc.Buffer.FirstElement = 0;
            vdesc.Buffer.ElementCount = VertexCount;


            _inputBuf1View = new D3D11.ShaderResourceView(Device, _inputBuf1, vdesc);
            _inputBuf2View = new D3D11.ShaderResourceView(Device, _inputBuf2, vdesc);



            D3D11.UnorderedAccessViewDescription uavdesc = new D3D11.UnorderedAccessViewDescription();
            uavdesc.Format              = DXGI.Format.Unknown;
            uavdesc.Dimension           = D3D11.UnorderedAccessViewDimension.Buffer;
            uavdesc.Buffer.FirstElement = 0;
            uavdesc.Buffer.ElementCount = VertexCount;

            _inputBuf1UAV = new D3D11.UnorderedAccessView(Device, _inputBuf1, uavdesc);
            _inputBuf2UAV = new D3D11.UnorderedAccessView(Device, _inputBuf2, uavdesc);


            return;
        }
Ejemplo n.º 6
0
        private void InitializeDeviceResources(DXGI.SwapChain swapChain)
        {
            backbufferTexture = swapChain.GetBackBuffer <D3D11.Texture2D>(0);
            backbufferRTV     = new D3D11.RenderTargetView(device, backbufferTexture);

            width  = backbufferTexture.Description.Width;
            height = backbufferTexture.Description.Height;

            D3D11.Texture2DDescription sceneTextureDesc = new D3D11.Texture2DDescription
            {
                CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                BindFlags         = D3D11.BindFlags.RenderTarget | D3D11.BindFlags.ShaderResource,
                Format            = DXGI.Format.R8G8B8A8_UNorm,
                Width             = width,
                Height            = height,
                OptionFlags       = D3D11.ResourceOptionFlags.None,
                MipLevels         = 1,
                ArraySize         = 1,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage             = D3D11.ResourceUsage.Default
            };

            sceneTexture = new D3D11.Texture2D(device, sceneTextureDesc);
            sceneRTV     = new D3D11.RenderTargetView(device, sceneTexture);
            sceneSRV     = new D3D11.ShaderResourceView(device, sceneTexture);

            var depthBufferDesc = new D3D11.Texture2DDescription()
            {
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = DXGI.Format.R32_Typeless,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage             = D3D11.ResourceUsage.Default,
                BindFlags         = D3D11.BindFlags.DepthStencil | D3D11.BindFlags.ShaderResource,
                CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                OptionFlags       = D3D11.ResourceOptionFlags.None
            };

            using (var depthStencilBufferTexture = new D3D11.Texture2D(device, depthBufferDesc))
            {
                var depthStencilViewDesc = new D3D11.DepthStencilViewDescription()
                {
                    Format    = DXGI.Format.D32_Float,
                    Dimension = D3D11.DepthStencilViewDimension.Texture2D,
                    Texture2D = { MipSlice = 0 }
                };

                depthDSV = new D3D11.DepthStencilView(device, depthStencilBufferTexture, depthStencilViewDesc);

                var shaderResourceViewDesc = new D3D11.ShaderResourceViewDescription()
                {
                    Format    = DXGI.Format.R32_Float,
                    Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D,
                    Texture2D = { MipLevels = 1, MostDetailedMip = 0 }
                };

                depthSRV = new D3D11.ShaderResourceView(device, depthStencilBufferTexture, shaderResourceViewDesc);
            }

            var depthStencilDesc = new D3D11.DepthStencilStateDescription()
            {
                IsDepthEnabled   = true,
                DepthWriteMask   = D3D11.DepthWriteMask.All,
                DepthComparison  = D3D11.Comparison.Less,
                IsStencilEnabled = false,
                StencilReadMask  = 0xFF,
                StencilWriteMask = 0xFF,

                FrontFace = new D3D11.DepthStencilOperationDescription()
                {
                    FailOperation      = D3D11.StencilOperation.Keep,
                    DepthFailOperation = D3D11.StencilOperation.Keep,
                    PassOperation      = D3D11.StencilOperation.Keep,
                    Comparison         = D3D11.Comparison.Always
                },

                BackFace = new D3D11.DepthStencilOperationDescription()
                {
                    FailOperation      = D3D11.StencilOperation.Keep,
                    DepthFailOperation = D3D11.StencilOperation.Keep,
                    PassOperation      = D3D11.StencilOperation.Keep,
                    Comparison         = D3D11.Comparison.Always
                }
            };

            depthStencilState = new D3D11.DepthStencilState(device, depthStencilDesc);

            var rasterDesc = new D3D11.RasterizerStateDescription()
            {
                IsAntialiasedLineEnabled = false,
                CullMode                = D3D11.CullMode.Back,
                DepthBias               = 0,
                DepthBiasClamp          = 0.0f,
                IsDepthClipEnabled      = true,
                FillMode                = D3D11.FillMode.Solid,
                IsFrontCounterClockwise = false,
                IsMultisampleEnabled    = false,
                IsScissorEnabled        = false,
                SlopeScaledDepthBias    = 0.0f
            };

            rasterizerState = new D3D11.RasterizerState(device, rasterDesc);
        }
Ejemplo n.º 7
0
        public void InitRenderTargets()
        {
            D3D11.DeviceContext deviceContext = deviceResources.DeviceContext;

            var renderTargetDesc = new D3D11.Texture2DDescription()
            {
                Format            = Format.R16G16B16A16_Float,
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = deviceResources.BackBuffer.Description.Width,
                Height            = deviceResources.BackBuffer.Description.Height,
                SampleDescription = sampleDescription,
                Usage             = D3D11.ResourceUsage.Default,
                BindFlags         = D3D11.BindFlags.RenderTarget | D3D11.BindFlags.ShaderResource,
                CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                OptionFlags       = D3D11.ResourceOptionFlags.None
            };

            var depthStencilDesc = new D3D11.Texture2DDescription()
            {
                Format            = Format.R24G8_Typeless,
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = deviceResources.BackBuffer.Description.Width,
                Height            = deviceResources.BackBuffer.Description.Height,
                SampleDescription = sampleDescription,
                Usage             = D3D11.ResourceUsage.Default,
                BindFlags         = D3D11.BindFlags.DepthStencil | D3D11.BindFlags.ShaderResource,
                CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                OptionFlags       = D3D11.ResourceOptionFlags.None
            };

            var depthStencilStateDesc = new D3D11.DepthStencilStateDescription
            {
                IsDepthEnabled  = true,
                DepthComparison = D3D11.Comparison.LessEqual,
                DepthWriteMask  = D3D11.DepthWriteMask.All,
            };


            // Create Descripptions for DepthStencil
            // RenderTarget works without Desc
            D3D11.DepthStencilViewDescription dsvDesc = new D3D11.DepthStencilViewDescription();
            dsvDesc.Format             = Format.D24_UNorm_S8_UInt;
            dsvDesc.Dimension          = D3D11.DepthStencilViewDimension.Texture2D;
            dsvDesc.Texture2D.MipSlice = 0;
            dsvDesc.Flags = 0;

            D3D11.ShaderResourceViewDescription dsSrvtDesc = new D3D11.ShaderResourceViewDescription();
            dsSrvtDesc.Texture2D.MipLevels = 1;
            dsSrvtDesc.Dimension           = ShaderResourceViewDimension.Texture2D;
            dsSrvtDesc.Format = Format.R24_UNorm_X8_Typeless; // TODO: Check Format


            // Create RenderTarget Textures
            renderTargetHandler.CreateRenderTarget(deviceResources.Device, renderTargetDesc);
            renderTargetHandler.CreateDepthStencil(deviceResources.Device, depthStencilDesc, dsvDesc, dsSrvtDesc);


            deviceContext.OutputMerger.SetRenderTargets(
                renderTargetHandler.GetDepthStencilView(),
                renderTargetHandler.GetRenderTargetView()
                );



            // Create DepthStencilState
            var depthStencilState = new D3D11.DepthStencilState(deviceResources.Device, depthStencilStateDesc);

            deviceContext.OutputMerger.SetDepthStencilState(depthStencilState);

            // Create Sampler
            sampler = CreateSampler();
            deviceContext.VertexShader.SetSampler(0, sampler);
            deviceContext.PixelShader.SetSampler(0, sampler);

            deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;

            Logger.Write(LogType.Info, "Renderer Created");
        }