public CompiledShader CompileShader(
            string funcname,
            string profile,
            CompilerOptions options)
        {
            var errorbuf = default(ID3DXBuffer);
            var compiler = default(ID3DXEffectCompiler);

            try
            {
                var len = Encoding.ASCII.GetByteCount(SourceCode);

                var hr = m_d3DXCreateEffectCompiler(
                    SourceCode,
                    (uint)len,
                    null,
                    IntPtr.Zero,
                    options.ToD3DXSHADER(),
                    out compiler,
                    out errorbuf);
                if (!D3DXUtil.Succeeded(hr))
                {
                    return(new CompiledShader
                    {
                        ErrorMessage =
                            errorbuf != null
                                  ? errorbuf.AsAnsiString()
                                  : string.Empty
                    });
                }
                return(compiler.CompileShader(
                           funcname,
                           profile,
                           options));
            }
            finally
            {
                if (errorbuf != null)
                {
                    Marshal.ReleaseComObject(errorbuf);
                }
                if (compiler != null)
                {
                    Marshal.ReleaseComObject(compiler);
                }
            }
        }
Esempio n. 2
0
        public static CompiledShader CompileShader(
            this ID3DXEffectCompilerCustom compiler,
            string function,
            string profile,
            CompilerOptions option)
        {
            VerifyNonNullArgument(
                compiler,
                "compiler");

            var buffer        = default(ID3DXBuffer);
            var msg           = default(ID3DXBuffer);
            var constantTable = default(ID3DXConstantTable);

            try
            {
                var ret = new CompiledShader();
                if (
                    Succeeded(
                        compiler.CompileShader(
                            function,
                            profile,
                            option.ToD3DXSHADER(),
                            out buffer,
                            out msg,
                            out constantTable)))
                {
                    var constantDesc = constantTable.GetDesc();
                    if (constantDesc.HasValue)
                    {
                        var constantsList =
                            from handle in constantTable.GetTopLevelConstantHandles()
                            let constatns =
                                from desc in constantTable.GetConstantDesc(handle)
                                select desc.ToConstantInfo()
                                select constatns.ToArray();

                        ret.TopLevelConstants    = constantsList.ToArray();
                        ret.ConstantTableCreator =
                            constantDesc.Value.Creator.ReadAsAnsiString();
                        ret.ConstantTableVersion = (int)constantDesc.Value.Version;
                    }
                    ret.Data = buffer.AsByteArray();
                }
                ret.ErrorMessage = msg != null?msg.AsAnsiString() : String.Empty;

                return(ret);
            }
            finally
            {
                if (buffer != null)
                {
                    Marshal.ReleaseComObject(buffer);
                    buffer = null;
                }
                if (msg != null)
                {
                    Marshal.ReleaseComObject(msg);
                    msg = null;
                }
                if (constantTable != null)
                {
                    Marshal.ReleaseComObject(constantTable);
                    constantTable = null;
                }
            }
        }
        CompiledEffect(
            UnmanagedLibrary dx9Handle,
            string source,
            CompilerOptions options)
        {
            m_dx9Handle = dx9Handle;
            m_d3DXCreateEffectCompiler = m_dx9Handle
                                         .GetUnmanagedFunction <D3DXCreateEffectCompilerProc>(
                EffectCompilerFuncName);

            SourceCode       = source;
            CompileFlags     = options;
            CompileSucceeded = false;


            var errorbuf = default(ID3DXBuffer);
            var compiler = default(ID3DXEffectCompiler);

            try
            {
                var len = Encoding.ASCII.GetByteCount(SourceCode);

                var hr = m_d3DXCreateEffectCompiler(
                    SourceCode,
                    (uint)len,
                    null,
                    IntPtr.Zero,
                    options.ToD3DXSHADER(),
                    out compiler,
                    out errorbuf);
                ErrorMessage = errorbuf != null
                              ? errorbuf.AsAnsiString()
                              : string.Empty;

                if (!D3DXUtil.Succeeded(hr))
                {
                    return;
                }

                CompileSucceeded = true;

                var maybeEffectDesc = compiler.GetDesc();
                if (!maybeEffectDesc.HasValue)
                {
                    return;
                }

                var effectDesc       = maybeEffectDesc.Value;
                var dxEffectCompiler = compiler;
                var functions        = from funcIndex in Enumerable.Range(
                    0,
                    (int)effectDesc.Functions)
                                       let funcHandle =
                    dxEffectCompiler.GetFunction((uint)funcIndex)
                    let funcDesc =
                        dxEffectCompiler.GetFunctionDesc(funcHandle)
                        // funcDesc.Annotations are always empty, so we ignore them
                        select
                        funcDesc.HasValue
                                  ? funcDesc.Value.Name.ReadAsAnsiString()
                                  : string.Empty;

                var effectCompiler = compiler;

                var topLevelParams =
                    from paramIndex in Enumerable.Range(
                        0,
                        (int)effectDesc.Parameters)
                    select effectCompiler.GetParameter(
                        IntPtr.Zero,
                        paramIndex);

                Creator            = effectDesc.Creator.ReadAsAnsiString();
                Functions          = functions.ToArray();
                TopLevelParameters = topLevelParams.ToArray();

                return;
            }
            finally
            {
                if (errorbuf != null)
                {
                    Marshal.ReleaseComObject(errorbuf);
                }
                if (compiler != null)
                {
                    Marshal.ReleaseComObject(compiler);
                }
            }
        }