コード例 #1
0
            public Program(CompiledEffectContent compiledEffect)
            {
                var code = compiledEffect.GetEffectCode();

                _effect = new Effect(SLSharp.Device, code);
                _pass   = _effect.Techniques.First().Passes.First();
            }
コード例 #2
0
        /// <summary>
        /// Processes a PaletteInfo object into a PaletteMaterialContent object.
        /// </summary>
        /// <param name="input">The PaletteInfo to process.</param>
        /// <param name="context">The processor context.</param>
        /// <returns>The processed PaletteMaterialContent</returns>
        public override PaletteMaterialContent Process(PaletteInfo input,
                                                       ContentProcessorContext context)
        {
            // Set all the variables based on the input.
            EffectProcessor effectProcessor = new EffectProcessor();
            EffectContent   effectContent   = new EffectContent();

            effectContent.EffectCode = input.SourceCode;
            CompiledEffectContent  compiled = effectProcessor.Process(effectContent, context);
            PaletteMaterialContent content  = new PaletteMaterialContent();

            content.PaletteSize = input.PaletteSize;
            content.ByteCode    = compiled.GetEffectCode();
            BasicMaterialContent basic = input.BasicContent;

            content.Alpha              = basic.Alpha;
            content.DiffuseColor       = basic.DiffuseColor;
            content.EmissiveColor      = basic.EmissiveColor;
            content.Name               = basic.Name;
            content.SpecularColor      = basic.SpecularColor;
            content.SpecularPower      = basic.SpecularPower;
            content.Texture            = basic.Texture;
            content.VertexColorEnabled = basic.VertexColorEnabled;
            return(content);
        }
コード例 #3
0
        public static byte[] GetEffectCodeBytes(string filename)
        {
            EffectContent         content  = importer.Import("./" + filename, new CustomImporterContext( ));
            CompiledEffectContent compiled = processor.Process(content, new CustomProcessorContext( ));

            return(compiled.GetEffectCode( ));
        }
コード例 #4
0
        static byte[] GetEffectCode(string filename)
        {
            var basePath       = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "Content");
            var effectFileName = System.IO.Path.Combine(basePath, filename + ".fx");

            EffectContent effectSource = new EffectContent
            {
                Identity   = new ContentIdentity(effectFileName),
                EffectCode = File.ReadAllText(effectFileName),
            };
            EffectProcessor       processor      = new EffectProcessor();
            CompiledEffectContent compiledEffect = processor.Process(effectSource, new ProcessorContext());

            return(compiledEffect.GetEffectCode());

#if DEBUG_SHADER_CODE
            // NOTE: We may need to implement a CompilerIncludeHandler here if we ever use #include in our shaders.
            var compiledEffect = Effect.CompileEffectFromFile(effectFileName, null, null, CompilerOptions.Debug, TargetPlatform.Windows);
            if (!compiledEffect.Success)
            {
                throw new InvalidOperationException(compiledEffect.ErrorsAndWarnings);
            }
            return(compiledEffect.GetEffectCode());
#else
            // We have to use a file stream instead of passing the file name directly because the latter method just botches up non-ASCII paths. :(
            //using (var effectFileStream = File.OpenRead(effectFileName))
            //{
            //    // NOTE: We may need to implement a CompilerIncludeHandler here if we ever use #include in our shaders.
            //    var compiledEffect = Effect.CompileEffectFromFile(effectFileStream, null, null, CompilerOptions.None, TargetPlatform.Windows);
            //    if (!compiledEffect.Success)
            //        throw new InvalidOperationException(compiledEffect.ErrorsAndWarnings);
            //    return compiledEffect.GetEffectCode();
            //}
#endif
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: nusisschad/XNAGameStudio
        static int Main(string[] args)
        {
            // Make sure we have the right number of commandline arguments.
            if (args.Length != 4)
            {
                Console.Error.WriteLine("Usage: CompileEffect <targetPlatform> <targetProfile> <input.fx> <output.bin>");
                return(1);
            }

            // Parse the commandline arguments.
            TargetPlatform targetPlatform;

            if (!Enum.TryParse(args[0], true, out targetPlatform))
            {
                Console.Error.WriteLine("Invalid target platform {0}. Valid options are {1}.", args[0], GetEnumValues <TargetPlatform>());
                return(1);
            }

            GraphicsProfile targetProfile;

            if (!Enum.TryParse(args[1], true, out targetProfile))
            {
                Console.Error.WriteLine("Invalid target profile {0}. Valid options are {1}.", args[1], GetEnumValues <GraphicsProfile>());
                return(1);
            }

            string inputFilename  = args[2];
            string outputFilename = args[3];

            try
            {
                Console.WriteLine("Compiling {0} -> {1} for {2}, {3}", Path.GetFileName(inputFilename), outputFilename, targetPlatform, targetProfile);

                ContentBuildLogger logger = new CustomLogger();

                // Import the effect source code.
                EffectImporter         importer        = new EffectImporter();
                ContentImporterContext importerContext = new CustomImporterContext(logger);
                EffectContent          sourceEffect    = importer.Import(inputFilename, importerContext);

                // Compile the effect.
                EffectProcessor         processor        = new EffectProcessor();
                ContentProcessorContext processorContext = new CustomProcessorContext(targetPlatform, targetProfile, logger);
                CompiledEffectContent   compiledEffect   = processor.Process(sourceEffect, processorContext);

                // Write out the compiled effect code.
                File.WriteAllBytes(outputFilename, compiledEffect.GetEffectCode());
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Error: {0}", e.Message);
                return(1);
            }

            return(0);
        }
コード例 #6
0
        public static byte[] ReadBytesFromSourceFile(string fileName)
        {
            EffectImporter  importer  = new EffectImporter( );
            EffectProcessor processor = new EffectProcessor( );

            EffectContent         content  = importer.Import(fileName, new ShaderImporterContext( ));
            CompiledEffectContent compiled = processor.Process(content, new ShaderProcessorContext( ));

            return(compiled.GetEffectCode( ));
        }
コード例 #7
0
        public override CompiledShaderContent Process(ShaderContent input, ContentProcessorContext context)
        {
            // Build new .fx file.
            string effectCode = BuildFxFile(input);

            // Save to temporary .stitchedeffect file.
            string tempFxFile = GetTempFileName(input, ".fx");

            File.WriteAllText(tempFxFile, effectCode, Encoding.GetEncoding(1252));

            // Run standard effect processor.
            CompiledEffectContent compiledEffect = context.BuildAndLoadAsset <EffectContent, CompiledEffectContent>(
                new ExternalReference <EffectContent>(tempFxFile),
                "EffectProcessor");

            return(new CompiledShaderContent {
                Effect = compiledEffect
            });
        }
コード例 #8
0
ファイル: XnaEffectLoader.cs プロジェクト: chuz/tesla-engine
        public override Effect Load(IResource resource, LoaderParameters parameters)
        {
            FileInfo info = new FileInfo(resource.FullName);

            if (!info.Exists)
            {
                throw new FileNotFoundException("Effect file does not exist in directory");
            }
            EffectContent content = new EffectContent();

            content.Identity   = new ContentIdentity(resource.FullName);
            content.EffectCode = File.ReadAllText(resource.FullName);

            CompiledEffectContent compiled = _processor.Process(content, _xnaContext);

            Effect effect = new Effect(compiled.GetEffectCode());

            effect.Name = resource.Name;
            return(effect);
        }
コード例 #9
0
        public override CompiledEffectContent Process(EffectContent input, ContentProcessorContext context)
        {
            //System.Diagnostics.Debugger.Launch();

            // If this isn't a MonoGame platform then do the default processing.
            var platform = ContentHelper.GetMonoGamePlatform();

            if (platform == MonoGamePlatform.None)
            {
                return(base.Process(input, context));
            }

            var options = new Options();

            options.SourceFile  = input.Identity.SourceFilename;
            options.DX11Profile = platform == MonoGamePlatform.Windows8 ? true : false;
            options.Debug       = DebugMode == EffectProcessorDebugMode.Debug;
            options.OutputFile  = context.OutputFilename;

            // Parse the MGFX file expanding includes, macros, and returning the techniques.
            ShaderInfo shaderInfo;

            try
            {
                shaderInfo = ShaderInfo.FromFile(options.SourceFile, options);
            }
            catch (Exception ex)
            {
                // TODO: Extract good line numbers from mgfx parser!
                throw new InvalidContentException(ex.Message, input.Identity, ex);
            }

            // Create the effect object.
            DXEffectObject effect = null;

            try
            {
                effect = DXEffectObject.FromShaderInfo(shaderInfo);
            }
            catch (Exception ex)
            {
                throw ProcessErrorsAndWarnings(ex.Message, input, context);
            }

            // Write out the effect to a runtime format.
            CompiledEffectContent result;

            try
            {
                using (var stream = new MemoryStream())
                {
                    using (var writer = new BinaryWriter(stream))
                        effect.Write(writer, options);

                    result = new CompiledEffectContent(stream.GetBuffer());
                }
            }
            catch (Exception ex)
            {
                throw new InvalidContentException("Failed to serialize the effect!", input.Identity, ex);
            }

            return(result);
        }
コード例 #10
0
        public override TOutput Process(TInput input, ContentProcessorContext context)
        {
            Dictionary <string, Q3BSPMaterialContent> processedDictionary = new Dictionary <string, Q3BSPMaterialContent>();

            #region Initialize StreamWriter, if necessary
#if DEBUG
            StreamWriter sw = new StreamWriter(context.OutputFilename.Substring(0, context.OutputFilename.LastIndexOf('.')) + ".compiled.txt");
#else
            StreamWriter sw = null;
#endif
            #endregion

            for (int i = 0; i < input.Count; i++)
            {
                Q3BSPMaterialContent shader = input[i];

                #region Throw any errors in the parsed shader
                if (shader.stages.Count > 8)
                {
                    throw new InvalidContentException(shader.shaderName + " has " + shader.stages.Count + " stages, but the maximum supported is 8.");
                }
                if (processedDictionary.ContainsKey(shader.shaderName))
                {
                    context.Logger.LogWarning("", new ContentIdentity(), "Material " + shader.shaderName + " is defined more than once.");
                    continue;
                    //throw new InvalidContentException("Material " + shader.shaderName + " is defined more than once.");
                }
                #endregion

                #region Log any needed warnings
                if (shader.stages.Count > 4 && shaderModel == ShaderModel.ShaderModel1)
                {
                    context.Logger.LogWarning("", new ContentIdentity(), shader.shaderName + " has more than 4 stages, Shader Model 2.0 is required.");
                }
                #endregion
                EffectProcessor processor = new EffectProcessor();
                processor.DebugMode = EffectProcessorDebugMode.Debug;
                EffectContent effectContent = new EffectContent();
                effectContent.EffectCode = GenerateEffectFromShader(shader, sw);
                #region Compile the Effect
#if DEBUG
                CompiledEffectContent compiledEffect = processor.Process(effectContent, context);
                //CompiledEffect compiledEffect = Effect.CompileEffectFromSource(GenerateEffectFromShader(shader, sw), null, null, CompilerOptions.Debug, TargetPlatform.Windows);
#else
                processor.DebugMode = EffectProcessorDebugMode.Auto;
                CompiledEffectContent compiledEffect = processor.Process(effectContent, context);
                //CompiledEffect compiledEffect = Effect.CompileEffectFromSource(GenerateEffectFromShader(shader, sw), null, null, CompilerOptions.None, TargetPlatform.Windows);
#endif
                #endregion

                /*
                 * if (compiledEffect.ErrorsAndWarnings.Contains("error"))
                 * {
                 *  throw new InvalidContentException(shader.shaderName + ": " + compiledEffect.ErrorsAndWarnings);
                 * }
                 */
                shader.compiledEffect = compiledEffect;
                shader.ShaderCode     = compiledEffect.GetEffectCode();
                processedDictionary.Add(shader.shaderName, shader);
            }
#if DEBUG
            sw.Flush();
            sw.Dispose();
#endif

            return(processedDictionary);
        }