private void LightCullingPass(StandardFrameData frameData)
        {
            CurrentPass = Pass.LightCulling;

            UpdateLights(frameData.LightsList);
            m_PerFrameConstBuffer.NumLights           = NonDirLightsCount;
            m_PerFrameConstBuffer.DirLightsNum        = DirLightsCount;
            m_PerFrameConstBuffer.MaxNumLightsPerTile = (uint)GetMaxNumLightsPerTile();
            GetContext.UpdateSubresource(ref m_PerFrameConstBuffer, PerFrameConstantBuffer);

            m_PerObjectConstBuffer = new CommonStructs.ConstBufferPerObjectStruct()
            {
                WorldMatrix         = Matrix.Identity,
                WorldViewMatrix     = Matrix.Identity * frameData.CamerasList[0].View,
                WorldViewProjMatrix = Matrix.Identity * frameData.CamerasList[0].ViewProjection,
            };
            GetContext.UpdateSubresource(ref m_PerObjectConstBuffer, PerObjConstantBuffer);
            GetContext.ComputeShader.SetConstantBuffer(0, PerObjConstantBuffer);
            GetContext.ComputeShader.SetConstantBuffer(1, PerFrameConstantBuffer);

            GetContext.OutputMerger.SetRenderTargets(null, (RenderTargetView)null);

            SetNullVertexShader();
            SetNullPixelShader();
            SetNullRasterizerState();
            SetBlendState(BlendStates.Opaque);

            GetContext.ComputeShader.Set(AssetsLoader.GetShader <ComputeShader>("LightCullingCS"));
            GetContext.ComputeShader.SetShaderResource(0, LightCenterAndRadiusSRV);
            GetContext.ComputeShader.SetShaderResource(1, GetDisplay.DepthStencilSRVRef);
            GetContext.ComputeShader.SetUnorderedAccessView(0, LightIndexURV);

            GetContext.Dispatch(GetNumTilesX(), GetNumTilesY(), 1);

            GetContext.ComputeShader.Set(null);
            GetContext.ComputeShader.SetShaderResource(0, null);
            GetContext.ComputeShader.SetShaderResource(1, null);
            GetContext.ComputeShader.SetUnorderedAccessView(0, null);
        }
        private void SetMaterial(string materialName, bool changeMergerStates)
        {
            if (materialName == "SkySphereMaterial")
            {
                CurrentMaterialInstance = Material.GetSkySphereMaterial();
            }
            else
            {
                CurrentMaterialInstance = AssetsLoader.LoadMaterial(materialName);
            }

            if (changeMergerStates)
            {
                SetMergerStates(CurrentMaterialInstance.MetaMaterial);
            }
            // TODO: shader selector

            m_PerObjectConstBuffer = new CommonStructs.ConstBufferPerObjectStruct
            {
                textureTiling = CurrentMaterialInstance.PropetyBlock.Tile,
                textureShift  = CurrentMaterialInstance.PropetyBlock.Shift,

                AlbedoColor    = new Vector4(CurrentMaterialInstance.PropetyBlock.AlbedoColor, CurrentMaterialInstance.PropetyBlock.AlphaValue),
                RoughnessValue = CurrentMaterialInstance.PropetyBlock.RoughnessValue,
                MetallicValue  = CurrentMaterialInstance.PropetyBlock.MetallicValue,

                optionsMask0 = floatMaskVal(CurrentMaterialInstance.HasAlbedoMap, CurrentMaterialInstance.HasNormalMap, CurrentMaterialInstance.HasRoughnessMap, CurrentMaterialInstance.HasMetallicMap),
                optionsMask1 = floatMaskVal(CurrentMaterialInstance.HasOcclusionMap, false, false, false),

                /*renderer.SpecificType == Renderer.SpecificTypeEnum.SkySphere || renderer.SpecificType == Renderer.SpecificTypeEnum.Unlit
                || renderer.SpecificType == Renderer.SpecificTypeEnum.Wireframe,
                || renderer.SpecificType == Renderer.SpecificTypeEnum.SkySphere, false),*/
                filler = Vector2.Zero,
            };

            if (materialName == "SkySphereMaterial")
            {
                m_PerObjectConstBuffer.optionsMask1 = floatMaskVal(CurrentMaterialInstance.HasOcclusionMap, true, true, false);
            }

            // TODO: Change textures binding
            if (CurrentMaterialInstance.HasSampler)
            {
                GetContext.PixelShader.SetSampler(0, GetSharedItems.GetSamplerState(CurrentMaterialInstance.GetSamplerType));
                ShaderResourceView[] textures = new ShaderResourceView[]
                {
                    CurrentMaterialInstance.HasAlbedoMap?GetSharedItems.LoadTextureSRV(CurrentMaterialInstance.AlbedoMapAsset) : null,
                        CurrentMaterialInstance.HasNormalMap ? GetSharedItems.LoadTextureSRV(CurrentMaterialInstance.NormalMapAsset) : null,
                        CurrentMaterialInstance.HasRoughnessMap ? GetSharedItems.LoadTextureSRV(CurrentMaterialInstance.RoughnessMapAsset) : null,
                        CurrentMaterialInstance.HasMetallicMap ? GetSharedItems.LoadTextureSRV(CurrentMaterialInstance.MetallicMapAsset) : null,
                        CurrentMaterialInstance.HasOcclusionMap ? GetSharedItems.LoadTextureSRV(CurrentMaterialInstance.OcclusionMapAsset) : null,
                };
                GetContext.PixelShader.SetShaderResources(0, 5, textures);
            }
            else
            {
                GetContext.PixelShader.SetSampler(0, null);
                GetContext.PixelShader.SetShaderResources(0, 5, (ShaderResourceView[])null);
            }
            // PreFiltered
            GetContext.PixelShader.SetShaderResource(5, GetSharedItems.PreFilteredMap);
            // Irradiance
            GetContext.PixelShader.SetShaderResource(6, RenderBackend.SharedRenderItems.IrradianceMap);

            //Shadow maps

            /*GetContext.PixelShader.SetSampler(1,
             *  Engine.Instance.MainLight.SamplerState);
             * GetContext.PixelShader.SetShaderResource(7,
             *  Engine.Instance.MainLight.ShaderResourceView);*/

            if (materialName == "SkySphereMaterial")
            {
                SetPixelShader("FwdSkySpherePS");
            }
            else if (CurrentMaterialInstance.MetaMaterial.blendMode == ShaderGraph.MetaMaterial.BlendMode.Translucent)
            {
                SetPixelShader("TestShader");
            }
            else
            {
                SetPixelShader("PBRForwardPS");
            }
        }
        private void SetMaterial(string materialName, bool changeStates = false)
        {
            if (materialName == "SkySphereMaterial")
            {
                CurrentMaterialInstance = Material.GetSkySphereMaterial();
            }
            else
            {
                CurrentMaterialInstance = AssetsLoader.LoadMaterial(materialName);
            }

            if (CurrentPass == Pass.DepthPrePass)
            {
                return;
            }

            m_PerObjectConstBuffer = new CommonStructs.ConstBufferPerObjectStruct
            {
                textureTiling = CurrentMaterialInstance.PropetyBlock.Tile,
                textureShift  = CurrentMaterialInstance.PropetyBlock.Shift,

                AlbedoColor    = new Vector4(CurrentMaterialInstance.PropetyBlock.AlbedoColor, CurrentMaterialInstance.PropetyBlock.AlphaValue),
                RoughnessValue = CurrentMaterialInstance.PropetyBlock.RoughnessValue,
                MetallicValue  = CurrentMaterialInstance.PropetyBlock.MetallicValue,

                optionsMask0 = floatMaskVal(CurrentMaterialInstance.HasAlbedoMap, CurrentMaterialInstance.HasNormalMap, CurrentMaterialInstance.HasRoughnessMap, CurrentMaterialInstance.HasMetallicMap),
                optionsMask1 = floatMaskVal(CurrentMaterialInstance.HasOcclusionMap, false, false, false),

                /*renderer.SpecificType == Renderer.SpecificTypeEnum.SkySphere || renderer.SpecificType == Renderer.SpecificTypeEnum.Unlit
                || renderer.SpecificType == Renderer.SpecificTypeEnum.Wireframe,
                || renderer.SpecificType == Renderer.SpecificTypeEnum.SkySphere, false),*/
                filler = Vector2.Zero,
            };

            if (changeStates)
            {
                SetMergerStates(CurrentMaterialInstance.MetaMaterial);
            }

            if (materialName == "SkySphereMaterial")
            {
                m_PerObjectConstBuffer.optionsMask1 = floatMaskVal(CurrentMaterialInstance.HasOcclusionMap, true, true, false);
            }

            if (CurrentMaterialInstance.HasSampler)
            {
                GetContext.PixelShader.SetSampler(0, GetSharedItems.GetSamplerState(CurrentMaterialInstance.GetSamplerType));
                ShaderResourceView[] textures = new ShaderResourceView[]
                {
                    CurrentMaterialInstance.HasAlbedoMap?GetSharedItems.LoadTextureSRV(CurrentMaterialInstance.AlbedoMapAsset) : null,
                        CurrentMaterialInstance.HasNormalMap ? GetSharedItems.LoadTextureSRV(CurrentMaterialInstance.NormalMapAsset) : null,
                        CurrentMaterialInstance.HasRoughnessMap ? GetSharedItems.LoadTextureSRV(CurrentMaterialInstance.RoughnessMapAsset) : null,
                        CurrentMaterialInstance.HasMetallicMap ? GetSharedItems.LoadTextureSRV(CurrentMaterialInstance.MetallicMapAsset) : null,
                        CurrentMaterialInstance.HasOcclusionMap ? GetSharedItems.LoadTextureSRV(CurrentMaterialInstance.OcclusionMapAsset) : null,
                };
                GetContext.PixelShader.SetShaderResources(0, 5, textures);
            }
            else
            {
                GetContext.PixelShader.SetSampler(0, null);
                GetContext.PixelShader.SetShaderResources(0, 5, (ShaderResourceView[])null);
            }

            if (materialName == "SkySphereMaterial")
            {
                SetPixelShader("FwdSkySpherePS");
            }
            else
            {
                if (CurrentMaterialInstance.MetaMaterial.blendMode >= ShaderGraph.MetaMaterial.BlendMode.Translucent)
                {
                    SetPixelShader("TestShader");
                }
                else
                {
                    SetPixelShader("ForwardPlusScenePS");
                }
            }
        }
예제 #4
0
        protected void SetMaterial(string materialName, bool changeStates = false)
        {
            if (materialName == "SkySphereMaterial")
            {
                m_CachedMaterial = Material.GetSkySphereMaterial();
            }
            else
            {
                m_CachedMaterial = AssetsLoader.LoadMaterial(materialName);
            }

            if (CurrentPass == Pass.DepthPrePass || CurrentPass == Pass.ShadowMapsPass)
            {
                return;
            }

            if (changeStates)
            {
                SetMergerStates(m_CachedMaterial.MetaMaterial);
            }

            // TODO: setup shader from material meta. bind buffers and textures by shader.
            if (materialName == "SkySphereMaterial")
            {
                SetPixelShader("FwdSkySpherePS");
            }
            else
            {
                if (m_CachedMaterial.MetaMaterial.blendMode >= ShaderGraph.MetaMaterial.BlendMode.Translucent)
                {
                    SetPixelShader("TestShader");
                }
                else
                {
                    if (SetPixelShader("PBRForwardPS"))
                    {
                        GetContext.PixelShader.SetShaderResource(5, GetSharedItems.PreFilteredMap);
                        GetContext.PixelShader.SetShaderResource(6, GetSharedItems.IrradianceMap);
                        GetContext.PixelShader.SetShaderResource(7, ShadowMapsAtlasTarget.ResourceView);
                        GetContext.PixelShader.SetSampler(1, GetSharedItems.GetSamplerState(SamplerType.ShadowMap));
                    }
                }
            }

            m_PerObjectConstBuffer = new CommonStructs.ConstBufferPerObjectStruct
            {
                textureTiling = m_CachedMaterial.PropetyBlock.Tile,
                textureShift  = m_CachedMaterial.PropetyBlock.Shift,

                AlbedoColor    = new Vector4(m_CachedMaterial.PropetyBlock.AlbedoColor, m_CachedMaterial.PropetyBlock.AlphaValue),
                RoughnessValue = m_CachedMaterial.PropetyBlock.RoughnessValue,
                MetallicValue  = m_CachedMaterial.PropetyBlock.MetallicValue,

                optionsMask0 = CommonStructs.FloatMaskValue(
                    m_CachedMaterial.HasAlbedoMap, m_CachedMaterial.HasNormalMap,
                    m_CachedMaterial.HasRoughnessMap, m_CachedMaterial.HasMetallicMap),
                optionsMask1 = CommonStructs.FloatMaskValue(
                    m_CachedMaterial.HasOcclusionMap, false, false, false),
                filler = Vector2.Zero,
            };

            if (materialName == "SkySphereMaterial")
            {
                m_PerObjectConstBuffer.optionsMask1 = CommonStructs.FloatMaskValue(
                    m_CachedMaterial.HasOcclusionMap, true, true, false);
            }

            if (m_CachedMaterial.HasSampler)
            {
                GetContext.PixelShader.SetSampler(0, GetSharedItems.GetSamplerState(m_CachedMaterial.GetSamplerType));
                ShaderResourceView[] textures = new ShaderResourceView[]
                {
                    m_CachedMaterial.HasAlbedoMap?GetSharedItems.LoadTextureSRV(m_CachedMaterial.AlbedoMapAsset) : null,
                        m_CachedMaterial.HasNormalMap ? GetSharedItems.LoadTextureSRV(m_CachedMaterial.NormalMapAsset) : null,
                        m_CachedMaterial.HasRoughnessMap ? GetSharedItems.LoadTextureSRV(m_CachedMaterial.RoughnessMapAsset) : null,
                        m_CachedMaterial.HasMetallicMap ? GetSharedItems.LoadTextureSRV(m_CachedMaterial.MetallicMapAsset) : null,
                        m_CachedMaterial.HasOcclusionMap ? GetSharedItems.LoadTextureSRV(m_CachedMaterial.OcclusionMapAsset) : null,
                };
                GetContext.PixelShader.SetShaderResources(0, 5, textures);
            }
            else
            {
                GetContext.PixelShader.SetSampler(0, null);
                GetContext.PixelShader.SetShaderResources(0, 5, new ShaderResourceView[5] {
                    null, null, null, null, null
                });
            }
        }