partial void OptimizeGLES300_Windows(CShaderDesc desc) { switch (desc.ShaderType) { case EShaderType.EST_VertexShader: { using (var optimizer = new GLSLOptimizerSharp.GLSLOptimizer(GLSLOptimizerSharp.Target.OpenGLES30)) { var result = optimizer.Optimize(GLSLOptimizerSharp.ShaderType.Vertex, desc.GLCode, GLSLOptimizerSharp.OptimizationOptions.None); desc.SetGLCode(result.OutputCode); } break; } case EShaderType.EST_PixelShader: { using (var optimizer = new GLSLOptimizerSharp.GLSLOptimizer(GLSLOptimizerSharp.Target.OpenGLES30)) { var result = optimizer.Optimize(GLSLOptimizerSharp.ShaderType.Fragment, desc.GLCode, GLSLOptimizerSharp.OptimizationOptions.None); desc.SetGLCode(result.OutputCode); } break; } } }
public CConstantBuffer CreateConstantBuffer(CShaderDesc desc, UInt32 index) { var obj = SDK_IRenderContext_CreateConstantBuffer2(CoreObject, desc.CoreObject, (int)index); if (obj.Pointer == IntPtr.Zero) { return(null); } return(new CConstantBuffer(obj)); }
public CComputeShader CreateComputeShader(CShaderDesc desc) { unsafe { var obj = SDK_IRenderContext_CreateComputeShader(CoreObject, desc.CoreObject); if (obj.Pointer == IntPtr.Zero) { return(null); } return(new CComputeShader(obj)); } }
public void SetShaderDesc(CShaderDesc shader) { SDK_IInputLayoutDesc_SetShaderDesc(CoreObject, shader.CoreObject); }
partial void OptimizeGLES300_Windows(CShaderDesc desc);
public void OptimizeGLES300(CShaderDesc desc) { OptimizeGLES300_Windows(desc); }
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); } } }
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); } } }
private static void OnShaderTranslated(CShaderDesc.NativePointer coreObj) { var desc = new CShaderDesc(coreObj); desc.Core_AddRef(); if (bTestSlot) { string pattern = "layout\\(binding = ([0-9]+), std140\\)"; // @"(?<=19)\d{2}\b"; foreach (Match match in Regex.Matches(desc.GLCode, pattern)) { int slot = System.Convert.ToInt32(match.Groups[1].Value); if (slot > 16) { Console.WriteLine(match.Value); } } } ////var reg = new System.Text.RegularExpressions.Regex("^(-?\\d+)(\\.\\d+)?$hf"); //var reg = new System.Text.RegularExpressions.Regex("[0-9]*\\.[0-9]+([Ee][+\\-]?[0-9]+)?hf"); //var srcCode = desc.GLCode; //var result = reg.Matches(srcCode); //var hfNums = new List<string>(); //foreach (System.Text.RegularExpressions.Match i in result) //{ // var src = i.ToString(); // hfNums.Add(src); //} //if (hfNums.Count > 0) //{ // foreach (var i in hfNums) // { // var tar = i.Substring(0, i.Length - 2) + 'f'; // srcCode = srcCode.Replace(i, tar); // } // desc.SetGLCode(srcCode); //} if (desc.GLCode.Contains("precision mediump float;")) { var code = desc.GLCode.Replace("precision mediump float;", "precision highp float;"); desc.SetGLCode(code); } if (desc.GLCode.Contains("#extension GL_NV_gpu_shader5 : require")) { const string GLES_Define = "#extension GL_NV_gpu_shader5 : require\r\n" + "#define f16mat2 mat2\r\n" + "#define f16mat3 mat3\r\n" + "#define f16mat4 mat4\r\n"; var code = desc.GLCode.Replace("#extension GL_NV_gpu_shader5 : require", GLES_Define); desc.SetGLCode(code); } if (desc.GLCode.Contains("#else\n#error No extension available for FP16.")) { const string GLES_Define = "#elif defined(EXT_shader_16bit_storage)\r\n" + "#extension EXT_shader_16bit_storage : require\r\n" + "#define f16mat2 mat2\r\n" + "#define f16mat3 mat3\r\n" + "#define f16mat4 mat4\r\n" + "#else\r\n" + "#define float16_t float\r\n" + "#define f16vec2 vec2\r\n" + "#define f16vec3 vec3\r\n" + "#define f16vec4 vec4\r\n" + "#define float16_t float\r\n" + "#define f16mat2 mat2\r\n" + "#define f16mat3 mat3\r\n" + "#define f16mat4 mat4\r\n"; var code = desc.GLCode.Replace("#else\n#error No extension available for FP16.", GLES_Define); desc.SetGLCode(code); } if (desc.GLCode.Contains("#else\n#error No extension available for Int16.")) { const string GLES_Define = "#else\r\n" + "#define int16_t int\r\n" + "#define uint16_t uint\r\n"; var code = desc.GLCode.Replace("#else\n#error No extension available for Int16.", GLES_Define); desc.SetGLCode(code); } if (desc.ShaderType == EShaderType.EST_VertexShader) { if (desc.GLCode.IndexOf("#extension") < 0) { if (desc.GLCode.StartsWith("#version 310 es")) { const string GLES_DefaultPrecision = "#version 310 es\n" + "precision highp float;\n"; var code = desc.GLCode.Replace("#version 310 es", GLES_DefaultPrecision); desc.SetGLCode(code); } else if (desc.GLCode.StartsWith("#version 300 es")) { const string GLES_DefaultPrecision = "#version 300 es\n" + "precision highp float;\n"; var code = desc.GLCode.Replace("#version 300 es", GLES_DefaultPrecision); desc.SetGLCode(code); } } else { const string GLES_DefaultPrecision = "precision highp float;\n" + "void main()"; var code = desc.GLCode.Replace("void main()", GLES_DefaultPrecision); desc.SetGLCode(code); } } }