示例#1
0
        private void ExtractIncludeSource(ref bool useVfetch)
        {
            if (filename != null)
            {
                //parse the hs, look for #include's in the root level only

                foreach (HlslStructure hs in this.hlslShader.Children)
                {
                    if (hs.Elements.Length > 4)
                    {
                        if (hs.Elements[0] == "#" &&
                            hs.Elements[1] == "include")
                        {
                            bool          global = hs.Elements[2] == "<";
                            StringBuilder file   = new StringBuilder();

                            for (int i = 3; i < hs.Elements.Length - 1; i++)
                            {
                                if ((global && hs.Elements[i] == ">") || (!global && hs.Elements[i] == "\""))
                                {
                                    break;
                                }

                                file.Append(hs.Elements[i]);
                            }

                            string includeName = file.ToString();

                            if (includeName == VFetchIncludeHandler.IncludeSymbol)
                            {
                                //vfetch requires shaders are built separately
                                useVfetch = true;
                            }
                            else
                            {
                                //find the file
                                string path = VFetchIncludeHandler.ResolveIncludePath(file.ToString(), this.filename);

                                if (File.Exists(path))
                                {
                                    //load the file and parse it as well
                                    SourceShader include = new SourceShader(File.ReadAllText(path), path, false);
                                    includedSource.Add(include);
                                }
                            }
                        }
                    }
                }
            }
        }
示例#2
0
        public DecompiledEffect(SourceShader source, Platform platform)
        {
            CompilerMacro[] macros = null;
            if (platform == Platform.Xbox)
            {
                macros = XboxCompileMacros;
            }

            CompilerIncludeHandler include = null;
            TargetPlatform         target  = TargetPlatform.Windows;

            this.techniqueDefaults = new Dictionary <string, TechniqueExtraData>();

            if (platform != Platform.Both)
            {
                include = new VFetchIncludeHandler(source.FileName, true);                 //ALWAYS target the PC for the vfetch macro
            }
            else
            {
                include = new VFetchIncludeHandler(source.FileName);                 //Acts as a generic handler
            }
            CompiledEffect compiledEffect = Effect.CompileEffectFromSource(source.ShaderSource, macros, include, source.CompilerOptions, target);

            if (!compiledEffect.Success)
            {
                Common.ThrowError(compiledEffect.ErrorsAndWarnings, source.ShaderSource);
            }

            //now pull the good stuff out.
            using (EffectPool pool = new EffectPool())
                using (Effect effect = new Effect(Graphics.GraphicsDevice, compiledEffect.GetEffectCode(), CompilerOptions.None, pool))
                {
                    Register[]      registers = new Register[effect.Parameters.Count];
                    List <Register> textures  = new List <Register>();

                    for (int i = 0; i < registers.Length; i++)
                    {
                        if (effect.Parameters[i].ParameterType == EffectParameterType.Single ||
                            effect.Parameters[i].ParameterType == EffectParameterType.Int32 ||
                            effect.Parameters[i].ParameterType == EffectParameterType.Bool)
                        {
                            registers[i].Name     = effect.Parameters[i].Name;
                            registers[i].Semantic = effect.Parameters[i].Semantic;
                        }

                        if (effect.Parameters[i].ParameterType >= EffectParameterType.Texture &&
                            effect.Parameters[i].ParameterType <= EffectParameterType.TextureCube)
                        {
                            EffectParameterType type = effect.Parameters[i].ParameterType;
                            if (type == EffectParameterType.Texture1D)
                            {
                                type = EffectParameterType.Texture2D;
                            }

                            registers[i].Name     = effect.Parameters[i].Name;
                            registers[i].Semantic = effect.Parameters[i].Semantic;
                            registers[i].Type     = type.ToString();
                            textures.Add(registers[i]);
                        }
                    }

                    this.effectRegisters = new RegisterSet(registers);
                    this.decompiledAsm   = Effect.Disassemble(effect, false);

                    ExtractEffectDefaults(effect, textures, source, platform);
                }
        }