protected override void LoadFromSource() { // Populate preprocessor defines var defines = new List <D3D9.Macro>(); if (!string.IsNullOrEmpty(PreprocessorDefines)) { var tmp = PreprocessorDefines.Split(' ', ',', ';'); foreach (string define in tmp) { var macro = new D3D9.Macro(); if (define.Contains("=")) { var split = define.Split('='); macro.Name = split[0]; macro.Definition = split[1]; } else { macro.Name = define; macro.Definition = "1"; } if (!string.IsNullOrEmpty(macro.Name)) { defines.Add(macro); } } } // Populate compile flags var compileFlags = UseColumnMajorMatrices ? D3D9.ShaderFlags.PackMatrixColumnMajor : D3D9.ShaderFlags.PackMatrixRowMajor; #if DEBUG compileFlags |= D3D9.ShaderFlags.Debug; #endif switch (OptimizationLevel) { case OptimizationLevel.Default: compileFlags |= D3D9.ShaderFlags.OptimizationLevel1; break; case OptimizationLevel.None: compileFlags |= D3D9.ShaderFlags.SkipOptimization; break; case OptimizationLevel.LevelZero: compileFlags |= D3D9.ShaderFlags.OptimizationLevel0; break; case OptimizationLevel.LevelOne: compileFlags |= D3D9.ShaderFlags.OptimizationLevel1; break; case OptimizationLevel.LevelTwo: compileFlags |= D3D9.ShaderFlags.OptimizationLevel2; break; case OptimizationLevel.LevelThree: compileFlags |= D3D9.ShaderFlags.OptimizationLevel3; break; } var parseFlags = compileFlags; compileFlags ^= UseColumnMajorMatrices ? D3D9.ShaderFlags.PackMatrixColumnMajor : D3D9.ShaderFlags.PackMatrixRowMajor; // include handler var includeHandler = new HLSLIncludeHandler(this); // Compile & assemble into microcode var effectCompiler = new D3D9.EffectCompiler(Source, defines.ToArray(), includeHandler, parseFlags); var effectHandle = new D3D9.EffectHandle(EntryPoint); var errors = string.Empty; try { MicroCode = effectCompiler.CompileShader(effectHandle, Target, compileFlags, out this.constTable); } catch (DX.SharpDXException ex) { if (ex is DX.CompilationException) { errors = ex.Message; } // check for errors if (!string.IsNullOrEmpty(errors)) { if (MicroCode != null) { if (LogManager.Instance != null) { LogManager.Instance.Write("HLSL: Warnings while compiling high level shader {0}:\n{1}", Name, errors); } } else { throw new AxiomException("HLSL: Unable to compile high level shader {0}:\n{1}", Name, errors); } } } finally { effectCompiler.Dispose(); effectHandle.Dispose(); includeHandler.Dispose(); } }
protected override void LoadFromSource() { // Populate preprocessor defines var defines = new List<D3D9.Macro>(); if ( !string.IsNullOrEmpty( PreprocessorDefines ) ) { var tmp = PreprocessorDefines.Split( ' ', ',', ';' ); foreach ( string define in tmp ) { var macro = new D3D9.Macro(); if ( define.Contains( "=" ) ) { var split = define.Split( '=' ); macro.Name = split[ 0 ]; macro.Definition = split[ 1 ]; } else { macro.Name = define; macro.Definition = "1"; } if ( !string.IsNullOrEmpty( macro.Name ) ) { defines.Add( macro ); } } } // Populate compile flags var compileFlags = UseColumnMajorMatrices ? D3D9.ShaderFlags.PackMatrixColumnMajor : D3D9.ShaderFlags.PackMatrixRowMajor; #if DEBUG compileFlags |= D3D9.ShaderFlags.Debug; #endif switch ( OptimizationLevel ) { case OptimizationLevel.Default: compileFlags |= D3D9.ShaderFlags.OptimizationLevel1; break; case OptimizationLevel.None: compileFlags |= D3D9.ShaderFlags.SkipOptimization; break; case OptimizationLevel.LevelZero: compileFlags |= D3D9.ShaderFlags.OptimizationLevel0; break; case OptimizationLevel.LevelOne: compileFlags |= D3D9.ShaderFlags.OptimizationLevel1; break; case OptimizationLevel.LevelTwo: compileFlags |= D3D9.ShaderFlags.OptimizationLevel2; break; case OptimizationLevel.LevelThree: compileFlags |= D3D9.ShaderFlags.OptimizationLevel3; break; } var parseFlags = compileFlags; compileFlags ^= UseColumnMajorMatrices ? D3D9.ShaderFlags.PackMatrixColumnMajor : D3D9.ShaderFlags.PackMatrixRowMajor; // include handler var includeHandler = new HLSLIncludeHandler( this ); // Compile & assemble into microcode var effectCompiler = new D3D9.EffectCompiler( Source, defines.ToArray(), includeHandler, parseFlags ); var effectHandle = new D3D9.EffectHandle( EntryPoint ); var errors = string.Empty; try { MicroCode = effectCompiler.CompileShader( effectHandle, Target, compileFlags, out this.constTable ); } catch ( DX.SharpDXException ex ) { if ( ex is DX.CompilationException ) { errors = ex.Message; } // check for errors if ( !string.IsNullOrEmpty( errors ) ) { if ( MicroCode != null ) { if ( LogManager.Instance != null ) { LogManager.Instance.Write( "HLSL: Warnings while compiling high level shader {0}:\n{1}", Name, errors ); } } else { throw new AxiomException( "HLSL: Unable to compile high level shader {0}:\n{1}", Name, errors ); } } } finally { effectCompiler.Dispose(); effectHandle.Dispose(); includeHandler.Dispose(); } }
//protected int parametersMapSizeAsBuffer; #endregion Fields #region Construction and Destruction /// <summary> /// Creates a new instance of <see cref="HLSLProgram"/> /// </summary> /// <param name="parent">the ResourceManager that owns this resource</param> /// <param name="name">Name of the program</param> /// <param name="handle">The resource id of the program</param> /// <param name="group">the resource group</param> /// <param name="isManual">is the program manually loaded?</param> /// <param name="loader">the loader responsible for this program</param> public HLSLProgram( ResourceManager parent, string name, ResourceHandle handle, string group, bool isManual, IManualResourceLoader loader ) : base( parent, name, handle, group, isManual, loader ) { includeHandler = new HLSLIncludeHandler( this ); columnMajorMatrices = true; }