Exemple #1
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);
        }
Exemple #2
0
            public Program(CompiledEffectContent compiledEffect)
            {
                var code = compiledEffect.GetEffectCode();

                _effect = new Effect(SLSharp.Device, code);
                _pass   = _effect.Techniques.First().Passes.First();
            }
Exemple #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( ));
        }
Exemple #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
        }
Exemple #5
0
        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);
        }
Exemple #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( ));
        }
Exemple #7
0
        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);
        }
        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);
        }