ShaderInterpreter.Converter.ConvertShader C# (CSharp) Method

ConvertShader() public static method

Converts a source shader into a C#-runnable class
public static ConvertShader ( FileInfo _ShaderPath, string _ShaderSource, string _ClassName, string _EntryPointVS, string _EntryPointPS ) : string
_ShaderPath System.IO.FileInfo The path of the shader to convert (for includes resolution)
_ShaderSource string The shader code
_ClassName string
_EntryPointVS string The name of the Vertex Shader entry point function
_EntryPointPS string The name of the Vertex Shader entry point function
return string
        public static string ConvertShader( FileInfo _ShaderPath, string _ShaderSource, string _ClassName, string _EntryPointVS, string _EntryPointPS )
        {
            // First resolve includes so we have the complete source code
            string	FullSource = ResolveIncludes( _ShaderPath, _ShaderSource );

            // Next perform various conversions
            FullSource = RemovePreprocessorDirectives( FullSource );
            FullSource = ReplaceStaticConsts( FullSource );
            FullSource = ConvertConstantBuffers( FullSource );
            FullSource = MakeStructFieldsPublic( FullSource );
            FullSource = ConvertRegisters( FullSource );
            FullSource = ConvertSemantics( FullSource );
            FullSource = ConvertFloatToDouble( FullSource );
            FullSource = ConvertVectorConstructors( FullSource );
            FullSource = ConvertOutFunctions( FullSource );

            // Finish by including the source in the middle of our header and footer
            string	Header = "using System;\n"
                           + "using ShaderInterpreter.ShaderMath;\n"
                           + "using ShaderInterpreter.Textures;\n"
                           + "\n"
                           + "namespace ShaderInterpreter\n"
                           + "{\n"
                           + "	public class	" + _ClassName + " : Shader\n"
                           + "	{\n"
                           + "";

            string	Footer = "\n	}\n"	// End the class
                           + "\n}\n";		// End the namespace

            string	FinalSource = Header
                                + FullSource
                                + Footer;

            return FinalSource;
        }

Usage Example

示例#1
0
        private void convertShaderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string OldFileName = GetRegKey("LastShaderFilename", m_ApplicationPath);

            openFileDialogShader.InitialDirectory = Path.GetDirectoryName(OldFileName);
            openFileDialogShader.FileName         = Path.GetFileName(OldFileName);
//          if ( openFileDialogShader.ShowDialog( this ) != DialogResult.OK )
//              return;
//          SetRegKey( "LastShaderFilename", openFileDialogShader.FileName );

            openFileDialogShader.FileName = @"E:\[Workspaces]\GodComplex\Resources\Shaders\GIRenderCubeMap.hlsl";


            // Perform conversion
            string CSharpCode = null;

            try
            {
                // Load the shader code
                string   ShaderCode = null;
                FileInfo ShaderFile = new FileInfo(openFileDialogShader.FileName);
                using (StreamReader S = ShaderFile.OpenText())
                    ShaderCode = S.ReadToEnd();

                // Perform conversion
                CSharpCode = Converter.ConvertShader(ShaderFile, ShaderCode, "Test", "VS", "PS");
            }
            catch (Converter.ConverterException _e)
            {
                SourceErrorForm F = new SourceErrorForm("An error occurred while converting shader \"" + openFileDialogShader.FileName + "\" to C# class!\n\n", _e);
                F.ShowDialog(this);
                return;
            }
            catch (Exception _e)
            {
                MessageBox("An error occurred while converting shader to C# class!\n\n" + _e.Message);
                return;
            }

            // Save the result
            OldFileName = GetRegKey("LastCSharpFilename", m_ApplicationPath);
            saveFileDialogShader.InitialDirectory = Path.GetDirectoryName(OldFileName);
            saveFileDialogShader.FileName         = Path.GetFileName(OldFileName);
            if (saveFileDialogShader.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }
            SetRegKey("LastCSharpFilename", saveFileDialogShader.FileName);

            FileInfo CSharpFile = new FileInfo(saveFileDialogShader.FileName);

            using (StreamWriter S = CSharpFile.CreateText())
            {
                S.Write(CSharpCode);
            }
        }