Beispiel #1
0
        /// <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)));
            }
        }
Beispiel #2
0
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (blob != null)
                {
                    blob.Dispose();
                    blob = null;
                }

                if (constantTable != null)
                {
                    constantTable.Dispose();
                    constantTable = null;
                }
            }
            if (isOwner && BufferPointer != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(BufferPointer);
                BufferPointer = IntPtr.Zero;
                BufferSize    = 0;
            }
        }