Exemplo n.º 1
0
            protected void _translateUnifiedGpuProgram(ScriptCompiler compiler, ObjectAbstractNode obj)
            {
                var          customParameters = new NameValuePairList();
                AbstractNode parameters       = null;

                foreach (var i in obj.Children)
                {
                    if (i is PropertyAbstractNode)
                    {
                        var prop = (PropertyAbstractNode)i;
                        if (prop.Name == "delegate")
                        {
                            var value = string.Empty;
                            if (prop.Values.Count != 0 && prop.Values[0] is AtomAbstractNode)
                            {
                                value = ((AtomAbstractNode)prop.Values[0]).Value;
                            }

                            ScriptCompilerEvent evt =
                                new ProcessResourceNameScriptCompilerEvent(ProcessResourceNameScriptCompilerEvent.ResourceType.GpuProgram,
                                                                           value);

                            compiler._fireEvent(ref evt);
                            customParameters["delegate"] = ((ProcessResourceNameScriptCompilerEvent)evt).Name;
                        }
                        else
                        {
                            var name  = prop.Name;
                            var value = string.Empty;
                            var first = true;
                            foreach (var it in prop.Values)
                            {
                                if (it is AtomAbstractNode)
                                {
                                    if (!first)
                                    {
                                        value += " ";
                                    }
                                    else
                                    {
                                        first = false;
                                    }
                                    value += ((AtomAbstractNode)it).Value;
                                }
                            }
                            customParameters.Add(name, value);
                        }
                    }
                    else if (i is ObjectAbstractNode)
                    {
                        if (((ObjectAbstractNode)i).Id == (uint)Keywords.ID_DEFAULT_PARAMS)
                        {
                            parameters = i;
                        }
                        else
                        {
                            processNode(compiler, i);
                        }
                    }
                }

                // Allocate the program
                Object progObj;
                HighLevelGpuProgram prog = null;

                ScriptCompilerEvent evnt = new CreateHighLevelGpuProgramScriptCompilerEvent(obj.File, obj.Name,
                                                                                            compiler.ResourceGroup, string.Empty,
                                                                                            "unified",
                                                                                            _translateIDToGpuProgramType(obj.Id));

                var processed = compiler._fireEvent(ref evnt, out progObj);

                if (!processed)
                {
                    prog =
                        (HighLevelGpuProgram)
                        (HighLevelGpuProgramManager.Instance.CreateProgram(obj.Name, compiler.ResourceGroup, "unified",
                                                                           _translateIDToGpuProgramType(obj.Id)));
                }
                else
                {
                    prog = (HighLevelGpuProgram)progObj;
                }

                // Check that allocation worked
                if (prog == null)
                {
                    compiler.AddError(CompileErrorCode.ObjectAllocationError, obj.File, obj.Line,
                                      "gpu program \"" + obj.Name + "\" could not be created");
                    return;
                }

                obj.Context = prog;

                prog.IsMorphAnimationIncluded     = false;
                prog.PoseAnimationCount           = 0;
                prog.IsSkeletalAnimationIncluded  = false;
                prog.IsVertexTextureFetchRequired = false;
                prog.Origin = obj.File;

                // Set the custom parameters
                prog.SetParameters(customParameters);

                // Set up default parameters
                if (prog.IsSupported && parameters != null)
                {
                    var ptr = prog.DefaultParameters;
                    GpuProgramTranslator.TranslateProgramParameters(compiler, ptr, (ObjectAbstractNode)parameters);
                }
            }
Exemplo n.º 2
0
            protected void _translateGpuProgram(ScriptCompiler compiler, ObjectAbstractNode obj)
            {
                var          customParameters = new NameValuePairList();
                string       syntax = string.Empty, source = string.Empty;
                AbstractNode parameters = null;

                foreach (var i in obj.Children)
                {
                    if (i is PropertyAbstractNode)
                    {
                        var prop = (PropertyAbstractNode)i;
                        if (prop.Id == (uint)Keywords.ID_SOURCE)
                        {
                            if (prop.Values.Count != 0)
                            {
                                if (prop.Values[0] is AtomAbstractNode)
                                {
                                    source = ((AtomAbstractNode)prop.Values[0]).Value;
                                }
                                else
                                {
                                    compiler.AddError(CompileErrorCode.InvalidParameters, prop.File, prop.Line, "source file expected");
                                }
                            }
                            else
                            {
                                compiler.AddError(CompileErrorCode.StringExpected, prop.File, prop.Line, "source file expected");
                            }
                        }
                        else if (prop.Id == (uint)Keywords.ID_SYNTAX)
                        {
                            if (prop.Values.Count != 0)
                            {
                                if (prop.Values[0] is AtomAbstractNode)
                                {
                                    syntax = ((AtomAbstractNode)prop.Values[0]).Value;
                                }
                                else
                                {
                                    compiler.AddError(CompileErrorCode.InvalidParameters, prop.File, prop.Line, "syntax string expected");
                                }
                            }
                            else
                            {
                                compiler.AddError(CompileErrorCode.StringExpected, prop.File, prop.Line, "syntax string expected");
                            }
                        }
                        else
                        {
                            string name = prop.Name, value = string.Empty;
                            var    first = true;
                            foreach (var it in prop.Values)
                            {
                                if (it is AtomAbstractNode)
                                {
                                    if (!first)
                                    {
                                        value += " ";
                                    }
                                    else
                                    {
                                        first = false;
                                    }

                                    value += ((AtomAbstractNode)it).Value;
                                }
                            }
                            customParameters.Add(name, value);
                        }
                    }
                    else if (i is ObjectAbstractNode)
                    {
                        if (((ObjectAbstractNode)i).Id == (uint)Keywords.ID_DEFAULT_PARAMS)
                        {
                            parameters = i;
                        }
                        else
                        {
                            processNode(compiler, i);
                        }
                    }
                }

                if (!GpuProgramManager.Instance.IsSyntaxSupported(syntax))
                {
                    compiler.AddError(CompileErrorCode.UnsupportedByRenderSystem, obj.File, obj.Line);
                    //Register the unsupported program so that materials that use it know that
                    //it exists but is unsupported
                    var unsupportedProg = GpuProgramManager.Instance.Create(obj.Name, compiler.ResourceGroup,
                                                                            _translateIDToGpuProgramType(obj.Id), syntax);

                    return;
                }

                // Allocate the program
                object     progObj;
                GpuProgram prog = null;

                ScriptCompilerEvent evt = new CreateGpuProgramScriptCompilerEvent(obj.File, obj.Name, compiler.ResourceGroup,
                                                                                  source, syntax,
                                                                                  _translateIDToGpuProgramType(obj.Id));

                var processed = compiler._fireEvent(ref evt, out progObj);

                if (!processed)
                {
                    prog =
                        (GpuProgram)
                        GpuProgramManager.Instance.CreateProgram(obj.Name, compiler.ResourceGroup, source,
                                                                 _translateIDToGpuProgramType(obj.Id), syntax);
                }
                else
                {
                    prog = (GpuProgram)progObj;
                }

                // Check that allocation worked
                if (prog == null)
                {
                    compiler.AddError(CompileErrorCode.ObjectAllocationError, obj.File, obj.Line,
                                      "gpu program \"" + obj.Name + "\" could not be created");
                    return;
                }

                obj.Context = prog;

                prog.IsMorphAnimationIncluded     = false;
                prog.PoseAnimationCount           = 0;
                prog.IsSkeletalAnimationIncluded  = false;
                prog.IsVertexTextureFetchRequired = false;
                prog.Origin = obj.File;

                // Set the custom parameters
                prog.SetParameters(customParameters);

                // Set up default parameters
                if (prog.IsSupported && parameters != null)
                {
                    var ptr = prog.DefaultParameters;
                    GpuProgramTranslator.TranslateProgramParameters(compiler, ptr, (ObjectAbstractNode)parameters);
                }
            }