/// <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> /// 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 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> /// 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))); } }
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; } }
/// <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> /// 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)); } }