Example #1
0
        private void Update(EvaluationContext context)
        {
            var resourceManager = ResourceManager.Instance();
            var device          = resourceManager.Device;
            var deviceContext   = device.ImmediateContext;
            var vsStage         = deviceContext.VertexShader;

            ConstantBuffers.GetValues(ref _constantBuffers, context);
            ShaderResources.GetValues(ref _shaderResourceViews, context);
            SamplerStates.GetValues(ref _samplerStates, context);

            _prevConstantBuffers     = vsStage.GetConstantBuffers(0, _constantBuffers.Length);
            _prevShaderResourceViews = vsStage.GetShaderResources(0, _shaderResourceViews.Length);
            _prevVertexShader        = vsStage.Get();

            var vs = VertexShader.GetValue(context);

            if (vs == null)
            {
                return;
            }

            vsStage.Set(vs);
            vsStage.SetConstantBuffers(0, _constantBuffers.Length, _constantBuffers);
            vsStage.SetShaderResources(0, _shaderResourceViews.Length, _shaderResourceViews);
        }
Example #2
0
        private void Update(EvaluationContext context)
        {
            var updateLive = UpdateLive.GetValue(context);

            if (_updatedOnce && !updateLive)
            {
                FilteredCubeMap.Value = _prefilteredCubeMap;
                return;
            }

            var exposure = Exposure.GetValue(context);

            //ConstantBuffers.GetValues(ref _constantBuffers, context);
            ShaderResources.GetValues(ref _shaderResourceViews, context);
            SamplerStates.GetValues(ref _samplerStates, context);

            var vs = VertexShader.GetValue(context);
            var gs = GeometryShader.GetValue(context);

            if (CubeMap.IsConnected && CubeMap.DirtyFlag.IsDirty)
            {
                //Log.Debug("Dirty");
            }

            var cubeMapSrc = CubeMap.GetValue(context); // Needs to be checked for null!

            if (cubeMapSrc == null)
            {
                FilteredCubeMap.Value = null;
                return;
            }

            var device        = ResourceManager.Instance().Device;
            var deviceContext = device.ImmediateContext;

            // Vertex shader stage
            var vsStage = deviceContext.VertexShader;

            _prevVsConstantBuffers     = vsStage.GetConstantBuffers(0, 1);
            _prevVsShaderResourceViews = vsStage.GetShaderResources(0, _shaderResourceViews.Length);
            _prevVertexShader          = vsStage.Get();

            if (vs == null)
            {
                Log.Warning($"{nameof(_SpecularPrefilter)} requires valid vertex shader", SymbolChildId);
                return;
            }
            vsStage.Set(vs);
            vsStage.SetShaderResources(0, _shaderResourceViews.Length, _shaderResourceViews);

            // Geometry shader stage
            var gsStage = deviceContext.GeometryShader;

            _prevGsConstantBuffers     = gsStage.GetConstantBuffers(0, 1);
            _prevGsShaderResourceViews = gsStage.GetShaderResources(0, _shaderResourceViews.Length);
            _prevGeometryShader        = gsStage.Get();

            if (gs == null)
            {
                Log.Warning($"{nameof(_SpecularPrefilter)} requires valid geometry shader", SymbolChildId);
                return;
            }

            gsStage.Set(gs);
            gsStage.SetShaderResources(0, _shaderResourceViews.Length, _shaderResourceViews);

            // Pixel shader stage
            var psStage = deviceContext.PixelShader;

            _prevPixelShader           = psStage.Get();
            _prevPsConstantBuffers     = psStage.GetConstantBuffers(0, 1);
            _prevPsShaderResourceViews = psStage.GetShaderResources(0, _shaderResourceViews.Length);
            _prevPsSamplerStates       = psStage.GetSamplers(0, _samplerStates.Length);

            var ps = PixelShader.GetValue(context);

            if (ps == null)
            {
                Log.Warning($"{nameof(_SpecularPrefilter)} requires valid pixel shader", SymbolChildId);
                return;
            }
            psStage.Set(ps);
            psStage.SetShaderResources(0, _shaderResourceViews.Length, _shaderResourceViews);
            psStage.SetSamplers(0, _samplerStates);


            // if (_prefilteredCubeMap != null && !Changed)
            // {
            //     context.Image = _prefilteredCubeMap;
            //     return context;
            // }

            Vector2 cubeMapSize = new Vector2(cubeMapSrc.Description.Width, cubeMapSrc.Description.Height);
            // Log.Debug($"source size: {cubeMapSrc.Description.Width} num mips in src: {cubeMapSrc.Description.MipLevels}");

            // if ( _prefilteredCubeMap == null )
            // {
            var cubeMapDesc = new Texture2DDescription
            {
                BindFlags         = BindFlags.ShaderResource | BindFlags.RenderTarget,
                Format            = cubeMapSrc.Description.Format,
                Width             = (int)cubeMapSize.X,
                Height            = (int)cubeMapSize.Y,
                MipLevels         = cubeMapSrc.Description.MipLevels,
                SampleDescription = cubeMapSrc.Description.SampleDescription,
                Usage             = ResourceUsage.Default,
                OptionFlags       = ResourceOptionFlags.TextureCube | ResourceOptionFlags.GenerateMipMaps,
                CpuAccessFlags    = CpuAccessFlags.None,
                ArraySize         = 6
            };

            Utilities.Dispose(ref _prefilteredCubeMap);
            try
            {
                _prefilteredCubeMap = new Texture2D(device, cubeMapDesc);
            }
            catch (SharpDXException e)
            {
                Log.Debug($"can't create CubeMap target {e.Message}");
                return;
            }

            var rastDesc = new RasterizerStateDescription
            {
                FillMode           = FillMode.Solid,
                CullMode           = CullMode.None,
                IsDepthClipEnabled = false
            };

            _rasterizerState = new RasterizerState(device, rastDesc);

            // Input Assembler
            var previousTopology = device.ImmediateContext.InputAssembler.PrimitiveTopology;

            device.ImmediateContext.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;

            _prevBlendState = device.ImmediateContext.OutputMerger.GetBlendState(out _prevBlendFactor, out _prevSampleMask);
            device.ImmediateContext.OutputMerger.BlendState        = DefaultRenderingStates.DisabledBlendState;
            device.ImmediateContext.OutputMerger.DepthStencilState = DefaultRenderingStates.DisabledDepthStencilState;

            _prevRenderTargetViews = device.ImmediateContext.OutputMerger.GetRenderTargets(1);
            device.ImmediateContext.OutputMerger.GetRenderTargets(out _prevDepthStencilView);

            var rtvDesc = new RenderTargetViewDescription()
            {
                Dimension      = RenderTargetViewDimension.Texture2DArray,
                Format         = cubeMapSrc.Description.Format,
                Texture2DArray = new RenderTargetViewDescription.Texture2DArrayResource()
                {
                    ArraySize       = 6,
                    FirstArraySlice = 0,
                    MipSlice        = 0
                }
            };

            int size = _prefilteredCubeMap.Description.Width;

            _prevViewports = device.ImmediateContext.Rasterizer.GetViewports <RawViewportF>();

            device.ImmediateContext.Rasterizer.State = _rasterizerState;

            int numMipLevels = _prefilteredCubeMap.Description.MipLevels;
            int mipSlice     = 0;

            while (mipSlice < numMipLevels)
            {
                // Log.Debug($"Update mipmap level {mipSlice} size: {size}");
                var viewport = new RawViewportF {
                    X = 0, Y = 0, Width = size, Height = size, MinDepth = 0, MaxDepth = 1
                };
                device.ImmediateContext.Rasterizer.SetViewports(new[] { viewport });


                Utilities.Dispose(ref _cubeMapRtv);
                rtvDesc.Texture2DArray.MipSlice = mipSlice;
                _cubeMapRtv = new RenderTargetView(device, _prefilteredCubeMap, rtvDesc);
                device.ImmediateContext.OutputMerger.SetTargets(_cubeMapRtv, null);

                var roughness = (float)mipSlice / (_prefilteredCubeMap.Description.MipLevels - 1);

                // Is this required?
                if (_settingsBuffer != null)
                {
                    Utilities.Dispose(ref _settingsBuffer);
                }

                for (int i = 0; i < _samplingParameters.Length; ++i)
                {
                    int indexToUse = -1;
                    if (Math.Abs(roughness - _samplingParameters[i].roughness) < 0.001f)
                    {
                        indexToUse = i;
                    }

                    if (indexToUse == -1 && roughness < _samplingParameters[i].roughness)
                    {
                        indexToUse = i - 1;
                    }

                    if (indexToUse != -1)
                    {
                        var parameterData = _samplingParameters[indexToUse];
                        parameterData.roughness = roughness;
                        parameterData.exposure  = exposure;
                        ResourceManager.Instance().SetupConstBuffer(parameterData, ref _settingsBuffer);
                        break;
                    }
                }

                var constantBuffers = new[] { _settingsBuffer };
                psStage.SetConstantBuffers(0, 1, constantBuffers);
                vsStage.SetConstantBuffers(0, 1, constantBuffers);
                gsStage.SetConstantBuffers(0, 1, constantBuffers);

                device.ImmediateContext.Draw(3, 0);
                size /= 2;
                ++mipSlice;
            }

            FilteredCubeMap.Value = _prefilteredCubeMap;
            Utilities.Dispose(ref _cubeMapRtv);

            //device.ImmediateContext.InputAssembler.PrimitiveTopology = previousTopology;
            Restore(context);
            _updatedOnce = true;
        }