Пример #1
0
        public CShaderDesc CreateShaderDesc(RName shader, string entry, EShaderType type, CShaderDefinitions defines, EPlatformType platforms)
        {
            string cachedName = "";
            string sm         = "vs_5_0";
            var    shaderHash = UniHash.APHash(shader.Name + entry + defines.ToString());
            var    macro      = defines.GetHash64().ToString();

            switch (type)
            {
            case EShaderType.EST_ComputeShader:
                cachedName = $"cs/{shaderHash}_{macro}.cshader";
                sm         = "cs_5_0";
                if (ShaderModel == 3)
                {
                    return(null);
                }
                break;

            case EShaderType.EST_VertexShader:
                cachedName = $"vs/{shaderHash}_{macro}.vshader";
                sm         = "vs_5_0";
                break;

            case EShaderType.EST_PixelShader:
                cachedName = $"ps/{shaderHash}_{macro}.pshader";
                sm         = "ps_5_0";
                break;
            }
            var smStr      = CRenderContext.ShaderModelString;
            var shaderFile = CEngine.Instance.FileManager.DDCDirectory + smStr + "/" + cachedName;

            using (var xnd = IO.XndHolder.SyncLoadXND(shaderFile))
            {
                if (xnd != null)
                {
                    bool needCompile = false;
                    var  fileDesc    = CEngine.Instance.FileManager.HLSLFileDescManager.FindFileDesc(shader.Address);
                    if (fileDesc != null)
                    {
                        var hashAttr = xnd.Node.FindAttrib("HashCode");
                        if (hashAttr != null)
                        {
                            hashAttr.BeginRead();
                            string savedHash;
                            hashAttr.Read(out savedHash);
                            hashAttr.EndRead();
                            if (savedHash != fileDesc.HashCode.ToString())
                            {
                                needCompile = true;
                            }
                        }
                    }
                    if (needCompile == false)
                    {
                        CShaderDesc desc = new CShaderDesc(type);
                        desc.LoadXnd(xnd.Node);
                        return(desc);
                    }
                }

                {
                    var xml = IO.XmlHolder.NewXMLHolder($"{type.ToString()}", "");
                    xml.RootNode.AddAttrib("Shader", shader.ToString());
                    xml.RootNode.AddAttrib("Entry", entry);
                    var node = xml.RootNode.AddNode("Macro", "", xml);
                    foreach (var i in defines.mShaderMacroArray)
                    {
                        node.AddAttrib(i.Name, i.Definition);
                    }
                    var fileDesc = CEngine.Instance.FileManager.HLSLFileDescManager.FindFileDesc(shader.Address);
                    if (fileDesc != null)
                    {
                        xml.RootNode.AddAttrib("HashCode", fileDesc.HashCode.ToString());
                    }

                    var shaderDescFile = CEngine.Instance.FileManager.DDCDirectory + "shaderinfo/" + cachedName + ".shaderxml";
                    IO.XmlHolder.SaveXML(shaderDescFile, xml);

                    var desc  = CompileHLSLFromFile(shader.Address, entry, sm, defines, platforms);
                    var saved = IO.XndHolder.NewXNDHolder();

                    if (fileDesc != null)
                    {
                        var hashAttr = saved.Node.AddAttrib("HashCode");
                        hashAttr.BeginWrite();
                        hashAttr.Write(fileDesc.HashCode.ToString());
                        hashAttr.EndWrite();
                    }

                    desc.Save2Xnd(saved.Node, platforms);

                    IO.XndHolder.SaveXND(shaderFile, saved);

                    return(desc);
                }
            }
        }
Пример #2
0
        public void CookShaderDesc(RName shader, string entry, EShaderType type, CShaderDefinitions defines, int ShaderModel, EPlatformType platforms)
        {
            string cachedName = "";
            string sm         = "vs_5_0";
            var    shaderHash = UniHash.APHash(shader.Name + entry + defines.ToString());
            var    macro      = defines.GetHash64().ToString();

            switch (type)
            {
            case EShaderType.EST_ComputeShader:
                cachedName = $"cs/{shaderHash}_{macro}.cshader";
                sm         = "cs_5_0";
                if (ShaderModel == 3)
                {
                    return;
                }
                break;

            case EShaderType.EST_VertexShader:
                cachedName = $"vs/{shaderHash}_{macro}.vshader";
                sm         = "vs_5_0";
                break;

            case EShaderType.EST_PixelShader:
                cachedName = $"ps/{shaderHash}_{macro}.pshader";
                sm         = "ps_5_0";
                break;
            }
            var shaderFile = "";

            switch (CIPlatform.Instance.PlayMode)
            {
            case CIPlatform.enPlayMode.Cook:
                shaderFile = CEngine.Instance.FileManager.CookingRoot + "deriveddatacache/" + $"sm{ShaderModel}/" + cachedName;
                break;

            case CIPlatform.enPlayMode.Game:
            case CIPlatform.enPlayMode.Editor:
            case CIPlatform.enPlayMode.PlayerInEditor:
                shaderFile = CEngine.Instance.FileManager.DDCDirectory + $"sm{ShaderModel}/" + cachedName;
                break;
            }
            using (var xnd = IO.XndHolder.SyncLoadXND(shaderFile))
            {
                if (xnd != null)
                {
                    bool needCompile = false;
                    var  fileDesc    = CEngine.Instance.FileManager.HLSLFileDescManager.FindFileDesc(shader.Address);
                    if (fileDesc != null)
                    {
                        var hashAttr = xnd.Node.FindAttrib("HashCode");
                        if (hashAttr != null)
                        {
                            hashAttr.BeginRead();
                            string savedHash;
                            hashAttr.Read(out savedHash);
                            hashAttr.EndRead();
                            if (savedHash != fileDesc.HashCode.ToString())
                            {
                                needCompile = true;
                            }
                        }
                    }
                    if (needCompile == false)
                    {
                        CShaderDesc desc = new CShaderDesc(type);
                        desc.LoadXnd(xnd.Node);
                        return;
                    }
                }

                {
                    var desc  = CompileHLSLFromFile(shader.Address, entry, sm, defines, platforms);
                    var saved = IO.XndHolder.NewXNDHolder();

                    var fileDesc = CEngine.Instance.FileManager.HLSLFileDescManager.FindFileDesc(shader.Address);
                    if (fileDesc != null)
                    {
                        var hashAttr = saved.Node.AddAttrib("HashCode");
                        hashAttr.BeginWrite();
                        hashAttr.Write(fileDesc.HashCode.ToString());
                        hashAttr.EndWrite();
                        desc.Save2Xnd(saved.Node, platforms);
                    }

                    IO.XndHolder.SaveXND(shaderFile, saved);
                }
            }
        }