Esempio n. 1
0
        /// <summary>
        /// Function to save the shader to a stream.
        /// </summary>
        /// <param name="stream">Stream to write into.</param>
        /// <param name="binary">[Optional] TRUE to save the binary version of the shader, FALSE to save the source.</param>
        /// <param name="saveDebug">[Optional] TRUE to save the debug information, FALSE to exclude it.</param>
        /// <remarks>The <paramref name="saveDebug"/> parameter is only applicable when the <paramref name="binary"/> parameter is set to TRUE.</remarks>
        /// <exception cref="System.ArgumentNullException">Thrown when the <paramref name="stream"/> parameter is NULL (Nothing in VB.Net).</exception>
        /// <exception cref="System.ArgumentException">Thrown when the shader is being saved as source code and the <see cref="GorgonLibrary.Graphics.GorgonShader.SourceCode">SourceCode</see> parameter is NULL (Nothing in VB.Net) or empty.</exception>
        /// <exception cref="GorgonLibrary.GorgonException">Thrown when the shader fails to compile.</exception>
        public void Save(Stream stream, bool binary = false, bool saveDebug = false)
        {
            Shaders.ShaderBytecode compiledShader = null;
            GorgonDebug.AssertNull(stream, "stream");

            if ((!binary) && (string.IsNullOrEmpty(SourceCode)))
            {
                throw new ArgumentException(Resources.GORGFX_SHADER_NO_CODE, "binary");
            }

            if (!binary)
            {
                byte[] shaderSource = Encoding.UTF8.GetBytes(SourceCode);
                stream.Write(shaderSource, 0, shaderSource.Length);

                return;
            }

            try
            {
                compiledShader = CompileFromSource(saveDebug);
                byte[] header = Encoding.UTF8.GetBytes(GorgonShaderBinding.BinaryShaderHeader);
                stream.Write(header, 0, header.Length);
                compiledShader.Save(stream);
            }
            finally
            {
                if (compiledShader != null)
                {
                    compiledShader.Dispose();
                }
            }
        }
Esempio n. 2
0
        //================================================================================================================//

        public static bool CompileShader(string baseSourceDirectory, string outputDirectory, string includeDirectory, HlslConfiguration global_, HlslFileConfiguration file)
        {
            try
            {
                string _path = Path.GetDirectoryName(file.FileNameRelative);

                IncludeFX include_ = new IncludeFX(baseSourceDirectory + _path + "\\");

                Console.WriteLine("Compiler Flags used: " + global_.GetShaderFlagsAsString().ToString());

                if (File.Exists(baseSourceDirectory + file.FileNameRelative))
                {
                    D3DCompiler.CompilationResult errors_ = D3DCompiler.ShaderBytecode.CompileFromFile(baseSourceDirectory + file.FileNameRelative,
                                                                                                       file.EntryPoint,
                                                                                                       file.PixelShaderVersion,
                                                                                                       global_.GetShaderFlags(),
                                                                                                       D3DCompiler.EffectFlags.None,
                                                                                                       null,
                                                                                                       include_);

                    D3DCompiler.ShaderBytecode shaderByteCode = errors_.Bytecode;

                    if (shaderByteCode != null)
                    {
                        string fileName_ = Path.GetFileNameWithoutExtension(Path.GetFileName(file.FileNameRelative));

                        string path_ = Path.GetFullPath(outputDirectory);

                        file.OutputName = path_ + fileName_ + ".cso";

                        System.IO.Directory.CreateDirectory(path_ + "\\");

                        FileStream write_ = new FileStream(file.OutputName, FileMode.Create, FileAccess.Write);

                        shaderByteCode.Save(write_);
                    }
                    else
                    {
                        throw new Exception(errors_.Message);
                    }

                    return(true);
                }
                else
                {
                    Console.WriteLine("File not found: " + baseSourceDirectory + file.FileNameRelative);
                }
            }
            catch (Exception ex_)
            {
                Console.WriteLine("Failed to compile file: " + ex_.Message);

                return(false);
            }

            return(false);
        }