예제 #1
0
        //public void ForceNeedUpdate()
        //{
        //	forceNeedUpdate = true;
        //}

        void Update()
        {
            if (TemporarilyDisableUpdate)
            {
                return;
            }

            //check disabled
            if (!Enabled)
            {
                Clear();
                return;
            }

            var newCode = Code.Value;

            //forceNeedUpdate = false;

            //no update when code is not changed
            if (newCode == compiledCode)
            {
                return;
            }
            ////no update in the editor when no need update and auto update is disabled
            //if( !forceNeedUpdate && EngineApp.ApplicationType == EngineApp.ApplicationTypeEnum.Editor && !EditorAutoUpdate )
            //	return;

            //do update

            Clear();
            compiledCode = newCode;
            //forceNeedUpdate = false;

            if (newCode != "")
            {
                compiledScript = GetOrCompileScript(compiledCode, out var error);
                if (!string.IsNullOrEmpty(error))
                {
                    Log.Warning($"Unable to compile shader script.\r\n\r\nComponent \"{GetPathFromRoot( true )}\".\r\n\r\n" + error);
                    return;
                }

                //One method mode
                if (compiledScript != null)                 //&& compiledMembers.Count == 1 )
                {
                    //compiledOneMethod = (CompiledMethodData)compiledMembers[ 0 ];
                    OneMethod_AddProperties();
                }
            }
        }
예제 #2
0
        void Clear()
        {
            compiledCode   = "";
            compiledScript = null;
            //compiledMembers.Clear();
            //compiledMemberBySignature.Clear();

            //One method mode
            //compiledOneMethod = null;
            properties.Clear();
            propertyBySignature.Clear();
            propertyMethodParameters = null;
            //propertyMethodReturnParameter = null;

            //!!!!
            //// clear cached texture for flow graph
            //cachedFlowGraphNodeImage?.Dispose();
            //cachedFlowGraphNodeImage = null;
        }
예제 #3
0
        static CompiledShaderScript GetOrCompileScript(string code, out string error)
        {
            error = "";

            lock ( compiledScriptCache )
            {
                if (compiledScriptCache.TryGetValue(code, out var script))
                {
                    return(script);
                }
            }

            var lines = code.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);

            var line = "";

            foreach (var line2 in lines)
            {
                if (line2.Contains(@"/*!public*/"))
                {
                    line = line2;
                    break;
                }
            }

            if (string.IsNullOrEmpty(line))
            {
                error = "No method is marked public.";
                return(null);
            }

            var result = new CompiledShaderScript();

            result.Body = code;

            try
            {
                // /*!public*/ vec3 Method(vec3 parameter1, vec3 parameter2)

                var str1 = line.Substring(line.IndexOf(@"/*!public*/") + @"/*!public*/".Length);

                int braceOpen  = str1.IndexOf('(');
                int braceClose = str1.IndexOf(')');

                var str2      = str1.Substring(0, braceOpen).Trim();
                var str2Split = str2.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (str2Split.Length != 2)
                {
                    throw new Exception("");
                }

                var parameters = new List <CompiledShaderScript.Parameter>();

                //get return type
                var returnTypeStr = str2Split[0];
                var returnType    = GetTypeByString(returnTypeStr, true);
                if (returnType == null)
                {
                    throw new Exception($"Unknown type \"{returnTypeStr}\".");
                }

                //get method name
                result.MethodName = str2Split[1];

                //parameters
                str2      = str1.Substring(braceOpen + 1, braceClose - braceOpen - 1).Trim();
                str2Split = str2.Split(new char[] { ',' });
                foreach (var str3 in str2Split)
                {
                    var str4 = str3.Trim();

                    var str4Split = str4.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (str4Split.Length < 2 || str4Split.Length > 3)
                    {
                        throw new Exception("");
                    }

                    //out, ref
                    bool isOut = false;
                    if (str4Split.Length == 3)
                    {
                        switch (str4Split[0])
                        {
                        case "out": throw new Exception("Output parameters are not supported.");

                        //case "out": isOut = true; break;
                        case "ref": throw new Exception("Parameters passed by reference are not supported.");

                        default: throw new Exception("");
                        }
                    }

                    //get type
                    var typeStr = str4Split.Length == 3 ? str4Split[1] : str4Split[0];
                    var type    = GetTypeByString(typeStr, false);
                    if (type == null)
                    {
                        throw new Exception($"Unknown type \"{typeStr}\".");
                    }

                    //get name
                    var name = str4Split.Length == 3 ? str4Split[2] : str4Split[1];

                    //add parameter
                    var p = new CompiledShaderScript.Parameter();
                    p.Name   = name;
                    p.Type   = MetadataManager.GetTypeOfNetType(type);
                    p.Output = isOut;
                    parameters.Add(p);
                }

                //add return parameter
                if (returnType != typeof(void))
                {
                    var p = new CompiledShaderScript.Parameter();
                    p.Name        = "ReturnValue";
                    p.Type        = MetadataManager.GetTypeOfNetType(returnType);
                    p.ReturnValue = true;
                    parameters.Add(p);
                }

                result.MethodParameters = parameters.ToArray();
            }
            catch (Exception e)
            {
                error = "Can't parse method signature.\r\n" + "\"" + line + "\"" + "\r\n\r\n" + e.Message;
                return(null);
            }

            //add to cache
            lock ( compiledScriptCache )
            {
                if (compiledScriptCache.Count > 300)
                {
                    compiledScriptCache.Clear();
                }
                compiledScriptCache[code] = result;
            }

            return(result);
        }