/// <summary> /// Initializes a new instance of the <see cref="EffectCompiler"/> class. /// </summary> /// <param name="data">The data.</param> /// <param name="defines">The defines.</param> /// <param name="includeFile">The include file.</param> /// <param name="flags">The flags.</param> /// <unmanaged>HRESULT D3DXCreateEffectCompiler([In] const char* pSrcData,[In] unsigned int SrcDataLen,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] unsigned int Flags,[In] ID3DXEffectCompiler** ppCompiler,[In] ID3DXBuffer** ppParseErrors)</unmanaged> public EffectCompiler(string data, Macro[] defines, Include includeFile, ShaderFlags flags) : base(IntPtr.Zero) { IntPtr dataPtr = Marshal.StringToHGlobalAnsi(data); try { CreateEffectCompiler(dataPtr, data.Length, defines, includeFile, flags, this); } finally { Marshal.FreeHGlobal(dataPtr); } }
internal static Macro[] PrepareMacros(Macro[] macros) { if (macros == null) { return(null); } if (macros.Length == 0) { return(null); } if (macros[macros.Length - 1].Name == null && macros[macros.Length - 1].Definition == null) { return(macros); } var macroArray = new Macro[macros.Length + 1]; Array.Copy(macros, macroArray, macros.Length); macroArray[macros.Length] = new Macro(null, null); return(macroArray); }
public static void Initialize(ref Device device, ref RenderForm form) { Macro macro = new Macro("nblights", 2.ToString()); Basic_Effect = Effect.FromFile(device, "Effect.fx", new Macro[] { macro }, null, "", ShaderFlags.OptimizationLevel3); Basic_Effect.Technique = Basic_Effect.GetTechnique(0); Basic_Effect.SetValue("AmbientLightColor", new Vector4(0f, 0f, 0f, 0f)); Matrix proj = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, form.ClientSize.Width / (float)form.ClientSize.Height, 0.1f, 7000.0f); }
/// <summary> /// Creates an effect compiler from a memory buffer containing an ASCII effect description . /// </summary> /// <param name="data">The data.</param> /// <param name="defines">The defines.</param> /// <param name="includeFile">The include file.</param> /// <param name="flags">The flags.</param> /// <returns> /// An instance of <see cref="EffectCompiler"/> /// </returns> /// <unmanaged>HRESULT D3DXCreateEffectCompiler([In] const void* pSrcData,[In] unsigned int SrcDataLen,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] unsigned int Flags,[Out, Fast] ID3DXEffectCompiler** ppCompiler,[In] ID3DXBuffer** ppParseErrors)</unmanaged> public static EffectCompiler FromMemory(byte[] data, Macro[] defines, Include includeFile, ShaderFlags flags) { unsafe { var compiler = new EffectCompiler(IntPtr.Zero); fixed (void* pData = data) CreateEffectCompiler((IntPtr)pData, data.Length, defines, includeFile, flags, compiler); return compiler; } }
private static void CreateEffectCompiler(IntPtr data, int length, Macro[] defines, Include includeFile, ShaderFlags flags, EffectCompiler instance) { Blob blobForErrors = null; try { D3DX9.CreateEffectCompiler(data, length, defines, IncludeShadow.ToIntPtr(includeFile), (int)flags, instance, out blobForErrors); } catch (SharpDXException ex) { if (blobForErrors != null) throw new CompilationException(ex.ResultCode, Utilities.BlobToString(blobForErrors)); throw; } }
internal static Macro[] PrepareMacros(Macro[] macros) { if (macros == null) return null; if (macros.Length == 0) return null; if (macros[macros.Length - 1].Name == null && macros[macros.Length - 1].Definition == null) return macros; var macroArray = new Macro[macros.Length + 1]; Array.Copy(macros, macroArray, macros.Length); macroArray[macros.Length] = new Macro(null, null); return macroArray; }
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(); } }
/// <summary> /// Compiles an effect from a memory buffer. /// </summary> /// <param name="device">The device.</param> /// <param name="memory">The buffer.</param> /// <param name="preprocessorDefines">The preprocessor defines.</param> /// <param name="includeFile">The include file.</param> /// <param name="skipConstants">The skip constants.</param> /// <param name="flags">The flags.</param> /// <param name="pool">The pool.</param> /// <returns>An <see cref="Effect"/></returns> /// <unmanaged>HRESULT D3DXCreateEffectEx([In] IDirect3DDevice9* pDevice,[In] const void* pSrcData,[In] unsigned int SrcDataLen,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] const char* pSkipConstants,[In] unsigned int Flags,[In] ID3DXEffectPool* pPool,[In] ID3DXEffect** ppEffect,[In] ID3DXBuffer** ppCompilationErrors)</unmanaged> public static Effect FromMemory(Device device, byte[] memory, Macro[] preprocessorDefines, Include includeFile, string skipConstants, ShaderFlags flags, EffectPool pool) { unsafe { Effect effect = null; Blob blobForErrors = null; try { fixed (void* pData = memory) D3DX9.CreateEffectEx( device, (IntPtr)pData, memory.Length, PrepareMacros(preprocessorDefines), IncludeShadow.ToIntPtr(includeFile), skipConstants, (int)flags, pool, out effect, out blobForErrors); } catch (SharpDXException ex) { if (blobForErrors != null) throw new CompilationException(ex.ResultCode, Utilities.BlobToString(blobForErrors)); throw; } return effect; } }
/// <summary> /// Compiles an effect from a string. /// </summary> /// <param name="device">The device.</param> /// <param name="sourceData">The source data.</param> /// <param name="preprocessorDefines">The preprocessor defines.</param> /// <param name="includeFile">The include file.</param> /// <param name="skipConstants">The skip constants.</param> /// <param name="flags">The flags.</param> /// <returns> /// An <see cref="Effect"/> /// </returns> /// <unmanaged>HRESULT D3DXCreateEffectEx([In] IDirect3DDevice9* pDevice,[In] const void* pSrcData,[In] unsigned int SrcDataLen,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] const char* pSkipConstants,[In] unsigned int Flags,[In] ID3DXEffectPool* pPool,[In] ID3DXEffect** ppEffect,[In] ID3DXBuffer** ppCompilationErrors)</unmanaged> public static Effect FromString(Device device, string sourceData, Macro[] preprocessorDefines, Include includeFile, string skipConstants, ShaderFlags flags) { return FromString(device, sourceData, preprocessorDefines, includeFile, skipConstants, flags, null); }
/// <summary> /// Compiles a shader or effect from a file on disk. /// </summary> /// <param name="fileName">The name of the source file to compile.</param> /// <param name="profile">The shader target or set of shader features to compile against.</param> /// <param name="shaderFlags">Shader compilation options.</param> /// <param name="defines">A set of macros to define during compilation.</param> /// <param name="include">An interface for handling include files.</param> /// <returns> /// The compiled shader bytecode, or <c>null</c> if the method fails. /// </returns> /// <unmanaged>HRESULT D3DXCompileShader([In] const char* pSrcData,[In] unsigned int SrcDataLen,[In] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] const char* pFunctionName,[In] const char* pProfile,[In] unsigned int Flags,[In] ID3DXBuffer** ppShader,[In] ID3DXBuffer** ppErrorMsgs,[In] ID3DXConstantTable** ppConstantTable)</unmanaged> public static CompilationResult CompileFromFile(string fileName, string profile, ShaderFlags shaderFlags = ShaderFlags.None, Macro[] defines = null, Include include = null) { return CompileFromFile(fileName, null, profile, shaderFlags, defines, include); }
/// <summary> /// Compiles a shader or effect from a file on disk. /// </summary> /// <param name="fileName">The name of the source file to compile.</param> /// <param name="entryPoint">The name of the shader entry-point function, or <c>null</c> for an effect file.</param> /// <param name="profile">The shader target or set of shader features to compile against.</param> /// <param name="shaderFlags">Shader compilation options.</param> /// <param name="defines">A set of macros to define during compilation.</param> /// <param name="include">An interface for handling include files.</param> /// <returns> /// The compiled shader bytecode, or <c>null</c> if the method fails. /// </returns> /// <unmanaged>HRESULT D3DXCompileShader([In] const char* pSrcData,[In] unsigned int SrcDataLen,[In] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] const char* pFunctionName,[In] const char* pProfile,[In] unsigned int Flags,[In] ID3DXBuffer** ppShader,[In] ID3DXBuffer** ppErrorMsgs,[In] ID3DXConstantTable** ppConstantTable)</unmanaged> public static CompilationResult CompileFromFile(string fileName, string entryPoint, string profile, ShaderFlags shaderFlags = ShaderFlags.None, Macro[] defines = null, Include include = null) { if (fileName == null) { throw new ArgumentNullException("fileName"); } if (profile == null) { throw new ArgumentNullException("profile"); } if (!File.Exists(fileName)) { throw new FileNotFoundException("Could not open the shader or effect file.", fileName); } return Compile(File.ReadAllText(fileName), entryPoint, profile, shaderFlags, PrepareMacros(defines), include); }
/// <summary> /// Compiles the provided shader or effect source. /// </summary> /// <param name="shaderSource">An array of bytes containing the raw source of the shader or effect to compile.</param> /// <param name="profile">The shader target or set of shader features to compile against.</param> /// <param name="shaderFlags">Shader compilation options.</param> /// <param name="defines">A set of macros to define during compilation.</param> /// <param name="include">An interface for handling include files.</param> /// <returns> /// The compiled shader bytecode, or <c>null</c> if the method fails. /// </returns> /// <unmanaged>HRESULT D3DXCompileShader([In] const char* pSrcData,[In] unsigned int SrcDataLen,[In] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] const char* pFunctionName,[In] const char* pProfile,[In] unsigned int Flags,[In] ID3DXBuffer** ppShader,[In] ID3DXBuffer** ppErrorMsgs,[In] ID3DXConstantTable** ppConstantTable)</unmanaged> public static CompilationResult Compile(byte[] shaderSource, string profile, ShaderFlags shaderFlags, Macro[] defines, Include include) { return Compile(shaderSource, null, profile, shaderFlags, defines, include); }
/// <summary> /// Compiles the provided shader or effect source. /// </summary> /// <param name="shaderSource">A string containing the source of the shader or effect to compile.</param> /// <param name="entryPoint">The name of the shader entry-point function, or <c>null</c> for an effect file.</param> /// <param name="profile">The shader target or set of shader features to compile against.</param> /// <param name="shaderFlags">Shader compilation options.</param> /// <param name="defines">A set of macros to define during compilation.</param> /// <param name="include">An interface for handling include files.</param> /// <returns> /// The compiled shader bytecode, or <c>null</c> if the method fails. /// </returns> /// <unmanaged>HRESULT D3DXCompileShader([In] const char* pSrcData,[In] unsigned int SrcDataLen,[In] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] const char* pFunctionName,[In] const char* pProfile,[In] unsigned int Flags,[In] ID3DXBuffer** ppShader,[In] ID3DXBuffer** ppErrorMsgs,[In] ID3DXConstantTable** ppConstantTable)</unmanaged> public static CompilationResult Compile(string shaderSource, string entryPoint, string profile, ShaderFlags shaderFlags, Macro[] defines, Include include) { if (string.IsNullOrEmpty(shaderSource)) { throw new ArgumentNullException("shaderSource"); } return Compile(Encoding.ASCII.GetBytes(shaderSource), entryPoint, profile, shaderFlags, defines, include); }
/// <summary> /// Assembles a shader from file. /// </summary> /// <param name="fileName">Name of the shader file.</param> /// <param name="defines">Macro definitions.</param> /// <param name="includeFile">An <see cref="SharpDX.Direct3D9.Include"/> interface to use for handling #include directives.</param> /// <param name="flags">Compilation options.</param> /// <returns> /// A <see cref="SharpDX.Direct3D9.CompilationResult"/> object representing the raw shader stream. /// </returns> public static CompilationResult AssembleFromFile(string fileName, Macro[] defines, Include includeFile, ShaderFlags flags) { return Assemble(File.ReadAllText(fileName), defines, includeFile, flags); }
/// <summary> /// Assembles a shader from the given source data. /// </summary> /// <param name="sourceData">The source shader data.</param> /// <param name="defines">Macro definitions.</param> /// <param name="includeFile">An <see cref="SharpDX.Direct3D9.Include" /> interface to use for handling #include directives.</param> /// <param name="flags">Compilation options.</param> /// <returns>A <see cref="SharpDX.Direct3D9.CompilationResult" /> object representing the raw shader stream.</returns> /// <unmanaged>HRESULT D3DXAssembleShader([In] const void* pSrcData,[In] unsigned int SrcDataLen,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] unsigned int Flags,[In] ID3DXBuffer** ppShader,[In] ID3DXBuffer** ppErrorMsgs)</unmanaged> public static CompilationResult Assemble(byte[] sourceData, Macro[] defines, Include includeFile, ShaderFlags flags) { unsafe { var resultCode = Result.Ok; Blob blobForCode = null; Blob blobForErrors = null; try { fixed (void* pData = sourceData) D3DX9.AssembleShader( (IntPtr)pData, sourceData.Length, PrepareMacros(defines), IncludeShadow.ToIntPtr(includeFile), (int)flags, out blobForCode, out blobForErrors); } catch (SharpDXException ex) { if (blobForErrors != null) { resultCode = ex.ResultCode; if (Configuration.ThrowOnShaderCompileError) throw new CompilationException(ex.ResultCode, Utilities.BlobToString(blobForErrors)); } else { throw; } } return new CompilationResult(blobForCode != null ? new ShaderBytecode(blobForCode) : null, resultCode, Utilities.BlobToString(blobForErrors)); } }
/// <summary> /// Assembles a shader from the given source data. /// </summary> /// <param name="sourceData">The source shader data.</param> /// <param name="defines">Macro definitions.</param> /// <param name="includeFile">An <see cref="SharpDX.Direct3D9.Include" /> interface to use for handling #include directives.</param> /// <param name="flags">Compilation options.</param> /// <returns>A <see cref="SharpDX.Direct3D9.CompilationResult" /> object representing the raw shader stream.</returns> /// <unmanaged>HRESULT D3DXAssembleShader([In] const void* pSrcData,[In] unsigned int SrcDataLen,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] unsigned int Flags,[In] ID3DXBuffer** ppShader,[In] ID3DXBuffer** ppErrorMsgs)</unmanaged> public static CompilationResult Assemble(string sourceData, Macro[] defines, Include includeFile, ShaderFlags flags) { return Assemble(Encoding.ASCII.GetBytes(sourceData), defines, includeFile, flags); }
/// <summary> /// Compiles an effect from a file. /// </summary> /// <param name="device">The device.</param> /// <param name="fileName">Name of the file.</param> /// <param name="preprocessorDefines">The preprocessor defines.</param> /// <param name="includeFile">The include file.</param> /// <param name="skipConstants">The skip constants.</param> /// <param name="flags">The flags.</param> /// <param name="pool">The pool.</param> /// <returns> /// An <see cref="Effect"/> /// </returns> /// <unmanaged>HRESULT D3DXCreateEffectEx([In] IDirect3DDevice9* pDevice,[In] const void* pSrcData,[In] unsigned int SrcDataLen,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] const char* pSkipConstants,[In] unsigned int Flags,[In] ID3DXEffectPool* pPool,[In] ID3DXEffect** ppEffect,[In] ID3DXBuffer** ppCompilationErrors)</unmanaged> public static Effect FromFile(Device device, string fileName, Macro[] preprocessorDefines, Include includeFile, string skipConstants, ShaderFlags flags, EffectPool pool) { return FromString(device, File.ReadAllText(fileName), preprocessorDefines, includeFile, skipConstants, flags, pool); }
/// <summary> /// Compiles the provided shader or effect source. /// </summary> /// <param name="shaderSource">An array of bytes containing the raw source of the shader or effect to compile.</param> /// <param name="entryPoint">The name of the shader entry-point function, or <c>null</c> for an effect file.</param> /// <param name="profile">The shader target or set of shader features to compile against.</param> /// <param name="shaderFlags">Shader compilation options.</param> /// <param name="defines">A set of macros to define during compilation.</param> /// <param name="include">An interface for handling include files.</param> /// <returns> /// The compiled shader bytecode, or <c>null</c> if the method fails. /// </returns> /// <unmanaged>HRESULT D3DXCompileShader([In] const char* pSrcData,[In] unsigned int SrcDataLen,[In] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] const char* pFunctionName,[In] const char* pProfile,[In] unsigned int Flags,[In] ID3DXBuffer** ppShader,[In] ID3DXBuffer** ppErrorMsgs,[In] ID3DXConstantTable** ppConstantTable)</unmanaged> public static CompilationResult Compile(byte[] shaderSource, string entryPoint, string profile, ShaderFlags shaderFlags, Macro[] defines, Include include) { unsafe { var resultCode = Result.Ok; Blob blobForCode = null; Blob blobForErrors = null; ConstantTable constantTable = null; try { fixed (void* pData = &shaderSource[0]) D3DX9.CompileShader( (IntPtr)pData, shaderSource.Length, PrepareMacros(defines), IncludeShadow.ToIntPtr(include), entryPoint, profile, (int)shaderFlags, out blobForCode, out blobForErrors, out constantTable); } catch (SharpDXException ex) { if (blobForErrors != null) { resultCode = ex.ResultCode; if (Configuration.ThrowOnShaderCompileError) throw new CompilationException(ex.ResultCode, Utilities.BlobToString(blobForErrors)); } else { throw; } } finally { if (constantTable != null) constantTable.Dispose(); } return new CompilationResult(blobForCode != null ? new ShaderBytecode(blobForCode) : null, resultCode, Utilities.BlobToString(blobForErrors)); } }
/// <summary> /// Compiles an effect from a memory buffer. /// </summary> /// <param name="device">The device.</param> /// <param name="memory">The buffer.</param> /// <param name="preprocessorDefines">The preprocessor defines.</param> /// <param name="includeFile">The include file.</param> /// <param name="skipConstants">The skip constants.</param> /// <param name="flags">The flags.</param> /// <returns> /// An <see cref="Effect"/> /// </returns> /// <unmanaged>HRESULT D3DXCreateEffectEx([In] IDirect3DDevice9* pDevice,[In] const void* pSrcData,[In] unsigned int SrcDataLen,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] const char* pSkipConstants,[In] unsigned int Flags,[In] ID3DXEffectPool* pPool,[In] ID3DXEffect** ppEffect,[In] ID3DXBuffer** ppCompilationErrors)</unmanaged> public static Effect FromMemory(Device device, byte[] memory, Macro[] preprocessorDefines, Include includeFile, string skipConstants, ShaderFlags flags) { return FromMemory(device, memory, preprocessorDefines, includeFile, skipConstants, flags, null); }
/// <summary> /// Preprocesses the provided shader or effect source. /// </summary> /// <param name = "shaderSource">An array of bytes containing the raw source of the shader or effect to preprocess.</param> /// <param name = "defines">A set of macros to define during preprocessing.</param> /// <param name = "include">An interface for handling include files.</param> /// <returns>The preprocessed shader source.</returns> /// <unmanaged>HRESULT D3DXPreprocessShader([In] const void* pSrcData,[In] unsigned int SrcDataSize,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] ID3DXBuffer** ppShaderText,[In] ID3DXBuffer** ppErrorMsgs)</unmanaged> public static string Preprocess(byte[] shaderSource, Macro[] defines = null, Include include = null) { string errors = null; return Preprocess(shaderSource, defines, include, out errors); }
/// <summary> /// Compiles an effect from a stream. /// </summary> /// <param name="device">The device.</param> /// <param name="stream">The stream.</param> /// <param name="preprocessorDefines">The preprocessor defines.</param> /// <param name="includeFile">The include file.</param> /// <param name="skipConstants">The skip constants.</param> /// <param name="flags">The flags.</param> /// <param name="pool">The pool.</param> /// <returns>An <see cref="Effect"/></returns> /// <unmanaged>HRESULT D3DXCreateEffectEx([In] IDirect3DDevice9* pDevice,[In] const void* pSrcData,[In] unsigned int SrcDataLen,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] const char* pSkipConstants,[In] unsigned int Flags,[In] ID3DXEffectPool* pPool,[In] ID3DXEffect** ppEffect,[In] ID3DXBuffer** ppCompilationErrors)</unmanaged> public static Effect FromStream(Device device, Stream stream, Macro[] preprocessorDefines, Include includeFile, string skipConstants, ShaderFlags flags, EffectPool pool) { unsafe { Effect effect = null; Blob blobForErrors = null; try { if (stream is DataStream) { D3DX9.CreateEffectEx( device, ((DataStream)stream).PositionPointer, (int)(stream.Length - stream.Position), PrepareMacros(preprocessorDefines), IncludeShadow.ToIntPtr(includeFile), skipConstants, (int)flags, pool, out effect, out blobForErrors); } else { var data = Utilities.ReadStream(stream); fixed (void* pData = data) D3DX9.CreateEffectEx( device, (IntPtr)pData, data.Length, PrepareMacros(preprocessorDefines), IncludeShadow.ToIntPtr(includeFile), skipConstants, (int)flags, pool, out effect, out blobForErrors); } stream.Position = stream.Length; } catch (SharpDXException ex) { if (blobForErrors != null) throw new CompilationException(ex.ResultCode, Utilities.BlobToString(blobForErrors)); throw; } return effect; } }
/// <summary> /// Preprocesses the provided shader or effect source. /// </summary> /// <param name = "shaderSource">An array of bytes containing the raw source of the shader or effect to preprocess.</param> /// <param name = "defines">A set of macros to define during preprocessing.</param> /// <param name = "include">An interface for handling include files.</param> /// <param name = "compilationErrors">When the method completes, contains a string of compilation errors, or an empty string if preprocessing succeeded.</param> /// <returns>The preprocessed shader source.</returns> /// <unmanaged>HRESULT D3DXPreprocessShader([In] const void* pSrcData,[In] unsigned int SrcDataSize,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] ID3DXBuffer** ppShaderText,[In] ID3DXBuffer** ppErrorMsgs)</unmanaged> public static string Preprocess(byte[] shaderSource, Macro[] defines, Include include, out string compilationErrors) { unsafe { fixed (void* pData = &shaderSource[0]) return Preprocess((IntPtr)pData, shaderSource.Length, defines, include, out compilationErrors); } }
/// <summary> /// Compiles an effect from a string. /// </summary> /// <param name="device">The device.</param> /// <param name="sourceData">The source data.</param> /// <param name="preprocessorDefines">The preprocessor defines.</param> /// <param name="includeFile">The include file.</param> /// <param name="skipConstants">The skip constants.</param> /// <param name="flags">The flags.</param> /// <param name="pool">The pool.</param> /// <returns> /// An <see cref="Effect"/> /// </returns> /// <unmanaged>HRESULT D3DXCreateEffectEx([In] IDirect3DDevice9* pDevice,[In] const void* pSrcData,[In] unsigned int SrcDataLen,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] const char* pSkipConstants,[In] unsigned int Flags,[In] ID3DXEffectPool* pPool,[In] ID3DXEffect** ppEffect,[In] ID3DXBuffer** ppCompilationErrors)</unmanaged> public static Effect FromString(Device device, string sourceData, Macro[] preprocessorDefines, Include includeFile, string skipConstants, ShaderFlags flags, EffectPool pool) { Effect effect = null; Blob blobForErrors = null; var buffer = Marshal.StringToHGlobalAnsi(sourceData); try { D3DX9.CreateEffectEx( device, buffer, sourceData.Length, PrepareMacros(preprocessorDefines), IncludeShadow.ToIntPtr(includeFile), skipConstants, (int)flags, pool, out effect, out blobForErrors); } catch (SharpDXException ex) { if (blobForErrors != null) throw new CompilationException(ex.ResultCode, Utilities.BlobToString(blobForErrors)); throw; } finally { Marshal.FreeHGlobal(buffer); } return effect; }
/// <summary> /// Preprocesses the provided shader or effect source. /// </summary> /// <param name="shaderSourcePtr">The shader source PTR.</param> /// <param name="shaderSourceLength">Length of the shader source.</param> /// <param name="defines">A set of macros to define during preprocessing.</param> /// <param name="include">An interface for handling include files.</param> /// <param name="compilationErrors">When the method completes, contains a string of compilation errors, or an empty string if preprocessing succeeded.</param> /// <returns> /// The preprocessed shader source. /// </returns> /// <unmanaged>HRESULT D3DXPreprocessShader([In] const void* pSrcData,[In] unsigned int SrcDataSize,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] ID3DXBuffer** ppShaderText,[In] ID3DXBuffer** ppErrorMsgs)</unmanaged> public static string Preprocess(IntPtr shaderSourcePtr, int shaderSourceLength, Macro[] defines, Include include, out string compilationErrors) { unsafe { Blob blobForText = null; Blob blobForErrors = null; compilationErrors = null; try { D3DX9.PreprocessShader(shaderSourcePtr, shaderSourceLength, PrepareMacros(defines), IncludeShadow.ToIntPtr(include), out blobForText, out blobForErrors); } catch (SharpDXException ex) { if (blobForErrors != null) { compilationErrors = Utilities.BlobToString(blobForErrors); throw new CompilationException(ex.ResultCode, compilationErrors); } throw; } return Utilities.BlobToString(blobForText); } }
/// <summary> /// Compiles an effect from a file. /// </summary> /// <param name="device">The device.</param> /// <param name="fileName">Name of the file.</param> /// <param name="preprocessorDefines">The preprocessor defines.</param> /// <param name="includeFile">The include file.</param> /// <param name="skipConstants">The skip constants.</param> /// <param name="flags">The flags.</param> /// <returns> /// An <see cref="Effect"/> /// </returns> /// <unmanaged>HRESULT D3DXCreateEffectEx([In] IDirect3DDevice9* pDevice,[In] const void* pSrcData,[In] unsigned int SrcDataLen,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] const char* pSkipConstants,[In] unsigned int Flags,[In] ID3DXEffectPool* pPool,[In] ID3DXEffect** ppEffect,[In] ID3DXBuffer** ppCompilationErrors)</unmanaged> public static Effect FromFile(Device device, string fileName, Macro[] preprocessorDefines, Include includeFile, string skipConstants, ShaderFlags flags) { return FromFile(device, fileName, preprocessorDefines, includeFile, skipConstants, flags, null); }
/// <summary> /// Preprocesses the provided shader or effect source. /// </summary> /// <param name = "shaderSource">A string containing the source of the shader or effect to preprocess.</param> /// <param name = "defines">A set of macros to define during preprocessing.</param> /// <param name = "include">An interface for handling include files.</param> /// <param name = "compilationErrors">When the method completes, contains a string of compilation errors, or an empty string if preprocessing succeeded.</param> /// <returns>The preprocessed shader source.</returns> /// <unmanaged>HRESULT D3DXPreprocessShader([In] const void* pSrcData,[In] unsigned int SrcDataSize,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] ID3DXBuffer** ppShaderText,[In] ID3DXBuffer** ppErrorMsgs)</unmanaged> public static string Preprocess(string shaderSource, Macro[] defines, Include include, out string compilationErrors) { if (string.IsNullOrEmpty(shaderSource)) { throw new ArgumentNullException("shaderSource"); } var shaderSourcePtr = Marshal.StringToHGlobalAnsi(shaderSource); try { return Preprocess(shaderSourcePtr, shaderSource.Length, defines, include, out compilationErrors); } finally { if (shaderSourcePtr != IntPtr.Zero) Marshal.FreeHGlobal(shaderSourcePtr); } }
/// <summary> /// Creates an effect compiler from a file on disk containing an ASCII effect description . /// </summary> /// <param name="fileName">Name of the file.</param> /// <param name="defines">The defines.</param> /// <param name="includeFile">The include file.</param> /// <param name="flags">The flags.</param> /// <returns> /// An instance of <see cref="EffectCompiler"/> /// </returns> /// <unmanaged>HRESULT D3DXCreateEffectCompiler([In] const void* pSrcData,[In] unsigned int SrcDataLen,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] unsigned int Flags,[Out, Fast] ID3DXEffectCompiler** ppCompiler,[In] ID3DXBuffer** ppParseErrors)</unmanaged> public static EffectCompiler FromFile(string fileName, Macro[] defines, Include includeFile, ShaderFlags flags) { return new EffectCompiler(File.ReadAllText(fileName), defines, includeFile, flags); }
private static void Main(string[] args) { #region Variables if (args.Length == 3 && args[0] == "-r" && int.TryParse(args[1], out resolution[0]) && int.TryParse(args[2], out resolution[1])) { } int menu_running = 1; // 0 -> jeu en cours // 1 -> menu de demarrage // 2 -> menu de jeu #endregion #region Fenêtre form = new RenderForm("Underground - Horror game"); form.Width = resolution[0]; form.Height = resolution[1]; // Intro panel1 = new Panel(); panel1.Name = "panel1"; panel1.Size = new System.Drawing.Size(resolution[0], resolution[1]); panel1.TabIndex = 1; form.Controls.Add(panel1); m_play = new DxPlay(panel1, @"Ressources\Video\UndergroundPS.avi"); //m_play = new DxPlay(panel1, @"Ressources\Video\UndergrounFinal_Introduction_vidonly.avi"); // Fonction à éxécuter après m_play.StopPlay += new DxPlay.DxPlayEvent(MediaPlayer.Fin_intro); Direct3D direct3D = new Direct3D(); PresentParameters Parametres = new PresentParameters( form.Width, form.Height, Format.X8R8G8B8, 1, MultisampleType.None, 0, SwapEffect.Discard, IntPtr.Zero, true, true, Format.D24X8, PresentFlags.None, 0, PresentInterval.Immediate); input = new Input(form); device = new Device(direct3D, 0, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing, Parametres); vertexElems3D = new[] { new VertexElement(0, // POSITION 0, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.Position, 0), new VertexElement(0, // TEXCOORD0 Convert.ToInt16(Utilities.SizeOf<Vector4>()), DeclarationType.Float2, DeclarationMethod.Default,DeclarationUsage.TextureCoordinate,0), new VertexElement(0, // COLOR0 Convert.ToInt16(Utilities.SizeOf<Vector4>()+Utilities.SizeOf<Vector2>()), DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.Color, 0), new VertexElement(0, // NORMAL0 Convert.ToInt16(Utilities.SizeOf<Vector4>()+Utilities.SizeOf<Vector2>()+Utilities.SizeOf<Vector4>()), DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.Normal, 0), new VertexElement(0, // TANGENT Convert.ToInt16(Utilities.SizeOf<Vector4>()+Utilities.SizeOf<Vector2>()+Utilities.SizeOf<Vector4>()+Utilities.SizeOf<Vector4>()), DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.Tangent, 0), VertexElement.VertexDeclarationEnd, new VertexElement(0, // booléen de bump mapping Convert.ToInt16(Utilities.SizeOf<Vector4>()+Utilities.SizeOf<Vector2>()+Utilities.SizeOf<Vector4>()+Utilities.SizeOf<Vector4>()+Utilities.SizeOf<Vector4>()), DeclarationType.Float1, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0), VertexElement.VertexDeclarationEnd, }; vertexElems2D = new[] { new VertexElement(0,0,DeclarationType.Float4,DeclarationMethod.Default,DeclarationUsage.PositionTransformed,0), new VertexElement(0,16,DeclarationType.Float2,DeclarationMethod.Default,DeclarationUsage.TextureCoordinate,0), VertexElement.VertexDeclarationEnd }; VertexDeclaration3D = new VertexDeclaration(Program.device, Program.vertexElems3D); VertexDeclaration2D = new VertexDeclaration(Program.device, Program.vertexElems2D); VertexBufferZero = new VertexBuffer(IntPtr.Zero); device.VertexDeclaration = VertexDeclaration3D; Program.device.SetRenderState(RenderState.AlphaBlendEnable, true); // graphics.GraphicsDevice.RenderState.AlphaBlendEnable = true; Program.device.SetRenderState(RenderState.DestinationBlend, Blend.InverseSourceAlpha); // graphics.GraphicsDevice.RenderState.DestinationBlend = Blend.One; Program.device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha); // graphics.GraphicsDevice.RenderState.SourceBlend = Blend.One; Program.device.SetRenderState(RenderState.AlphaFunc, BlendOperation.Maximum); // graphics.GraphicsDevice.RenderState.BlendFunction = BlendFunction.Add; Program.device.SetRenderState(RenderState.AlphaTestEnable, true); Program.device.SetRenderState(RenderState.MinTessellationLevel, 8); #endregion Liste_textures = new List<structTexture>(); Liste_binaires = new List<structBinary>(); Liste_OBJ = new List<structOBJ>(); Liste_Lights = new Light[2]; Liste_Lights[0].Type = LightType.Point; Liste_Lights[0].Position = new Vector3(0, 0, 0); Liste_Lights[0].Ambient = new Color4(0.5f, 0.5f, 0.5f, 1); Liste_Lights[0].Range = 30f * 70; Liste_Lights[1].Type = LightType.Point; Liste_Lights[1].Position = new Vector3(-100, -100, -100); Liste_Lights[1].Ambient = new Color4(0.5f,0,0,1); Liste_Lights[1].Range = 5f * 70; Liste_textures.Add(new structTexture("null.bmp", Texture.FromFile(device, "null.bmp"))); //Ingame.Slender.doit_etre_recharge = true; // Creation du fichier effect de référence Macro macro = new Macro("nblights", 2.ToString()); BaseEffect = Effect.FromFile(Program.device, "Underground.fx", new Macro[] { macro }, null, "", ShaderFlags.OptimizationLevel3); BaseEffect.Technique = BaseEffect.GetTechnique(0); BaseEffect.SetValue("AmbientLightColor", new Vector4(0f, 0f, 0f, 0f)); Matrix proj = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, Program.form.ClientSize.Width / (float)Program.form.ClientSize.Height, 0.1f, 7000.0f); BaseEffect.SetValue("Projection", proj); BaseEffect.SetValue("LightDiffuseColor[0]", Liste_Lights[0].Ambient); BaseEffect.SetValue("LightDiffuseColor[1]", Liste_Lights[1].Ambient); BaseEffect.SetValue("LightDistanceSquared[1]", Liste_Lights[1].Range); Thread ThSound = new Thread(Sound.main); Thread ThEvents = new Thread(Ingame.fevents); ThSound.Start(); ThEvents.Start(); Menu.Initialize_Menu(); ObjLoader.Initialize(); newmaze = new Maze(10, 10); newmaze.Initecelles(); newmaze.Generate(newmaze.maze[0, 0]); newmaze.impefectGenerate(); newmaze.Setaffichage(); Ingame.ingame(); Menu.Dispose(); ThSound.Abort(); ThEvents.Abort(); VertexBufferZero.Dispose(); VertexDeclaration3D.Dispose(); VertexDeclaration2D.Dispose(); device.Dispose(); direct3D.Dispose(); }
/// <summary> /// Creates an effect compiler from a stream containing an ASCII effect description . /// </summary> /// <param name="stream">The stream.</param> /// <param name="defines">The defines.</param> /// <param name="includeFile">The include file.</param> /// <param name="flags">The flags.</param> /// <returns>An instance of <see cref="EffectCompiler"/></returns> /// <unmanaged>HRESULT D3DXCreateEffectCompiler([In] const void* pSrcData,[In] unsigned int SrcDataLen,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] unsigned int Flags,[Out, Fast] ID3DXEffectCompiler** ppCompiler,[In] ID3DXBuffer** ppParseErrors)</unmanaged> public static EffectCompiler FromStream(Stream stream, Macro[] defines, Include includeFile, ShaderFlags flags) { if (stream is DataStream) { var compiler = new EffectCompiler(IntPtr.Zero); CreateEffectCompiler(((DataStream)stream).PositionPointer, (int)(stream.Length - stream.Position), defines, includeFile, flags, compiler); return compiler; } return FromMemory(Utilities.ReadStream(stream), defines, includeFile, flags); }
/// <summary> /// Preprocesses a shader or effect from a file on disk. /// </summary> /// <param name = "fileName">The name of the source file to compile.</param> /// <param name = "defines">A set of macros to define during preprocessing.</param> /// <param name = "include">An interface for handling include files.</param> /// <param name = "compilationErrors">When the method completes, contains a string of compilation errors, or an empty string if preprocessing succeeded.</param> /// <returns>The preprocessed shader source.</returns> /// <unmanaged>HRESULT D3DXPreprocessShader([In] const void* pSrcData,[In] unsigned int SrcDataSize,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] ID3DXBuffer** ppShaderText,[In] ID3DXBuffer** ppErrorMsgs)</unmanaged> public static string PreprocessFromFile(string fileName, Macro[] defines, Include include, out string compilationErrors) { if (fileName == null) { throw new ArgumentNullException("fileName"); } if (!File.Exists(fileName)) { throw new FileNotFoundException("Could not open the shader or effect file.", fileName); } return Preprocess(File.ReadAllText(fileName), defines, include, out compilationErrors); }
/// <summary> /// Preprocesses a shader or effect from a file on disk. /// </summary> /// <param name = "fileName">The name of the source file to compile.</param> /// <param name = "defines">A set of macros to define during preprocessing.</param> /// <param name = "include">An interface for handling include files.</param> /// <returns>The preprocessed shader source.</returns> /// <unmanaged>HRESULT D3DXPreprocessShader([In] const void* pSrcData,[In] unsigned int SrcDataSize,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] ID3DXBuffer** ppShaderText,[In] ID3DXBuffer** ppErrorMsgs)</unmanaged> public static string PreprocessFromFile(string fileName, Macro[] defines, Include include) { string errors = null; return PreprocessFromFile(fileName, defines, include, out errors); }
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(); } }