public static byte[] CompileHLSL(
            ShaderResult shaderResult, string shaderFunction, string shaderProfile,
            ref string errorsAndWarnings)
        {
            try
            {
                SharpDX.D3DCompiler.ShaderFlags shaderFlags = 0;

                // While we never allow preshaders, this flag is invalid for
                // the DX11 shader compiler which doesn't allow preshaders
                // in the first place.
                //shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.NoPreshader;

                if (shaderResult.Profile == ShaderProfile.DirectX_11)
                {
                    shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.EnableBackwardsCompatibility;
                }

                if (shaderResult.Debug)
                {
                    shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.SkipOptimization;
                    shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.Debug;
                }
                else
                {
                    shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.OptimizationLevel3;
                }

                // Compile the shader into bytecode.
                var result = SharpDX.D3DCompiler.ShaderBytecode.Compile(
                    shaderResult.FileContent,
                    shaderFunction,
                    shaderProfile,
                    shaderFlags,
                    0,
                    null,
                    null,
                    shaderResult.FilePath);

                // Store all the errors and warnings to log out later.
                errorsAndWarnings += result.Message;

                if (result.Bytecode == null)
                {
                    throw new ShaderCompilerException();
                }

                var shaderByteCode = result.Bytecode;
                //var source = shaderByteCode.Disassemble();

                // Return a copy of the shader bytecode.
                return(shaderByteCode.Data);
            }
            catch (SharpDX.CompilationException ex)
            {
                errorsAndWarnings += ex.Message;
                throw new ShaderCompilerException();
            }
        }
Exemple #2
0
        /// <summary>
        /// Compiles the effect10 from memory.
        /// </summary>
        /// <param name="dataRef">The data ref.</param>
        /// <param name="dataLength">Length of the data.</param>
        /// <param name="srcFileNameRef">The SRC file name ref.</param>
        /// <param name="definesRef">The defines ref.</param>
        /// <param name="includeRef">The include ref.</param>
        /// <param name="hlslFlags">The h LSL flags.</param>
        /// <param name="fxFlags">The f X flags.</param>
        /// <param name="compiledEffectOut">The compiled effect out.</param>
        /// <param name="errorsOut">The errors out.</param>
        /// <returns>Result code.</returns>
        /// <unmanaged>HRESULT D3D10CompileEffectFromMemory([In] void* pData,[In] SIZE_T DataLength,[In] const char* pSrcFileName,[In, Buffer, Optional] const D3D_SHADER_MACRO* pDefines,[In] ID3DInclude* pInclude,[In] D3DCOMPILE_SHADER_FLAGS HLSLFlags,[In] D3DCOMPILE_EFFECT_FLAGS FXFlags,[In] ID3D10Blob** ppCompiledEffect,[In] ID3D10Blob** ppErrors)</unmanaged>
        public static SharpDX.Result CompileEffect10FromMemory(
            System.IntPtr dataRef,
            SharpDX.PointerSize dataLength,
            string srcFileNameRef,
            SharpDX.Direct3D.ShaderMacro[] definesRef,
            System.IntPtr includeRef,
            SharpDX.D3DCompiler.ShaderFlags hlslFlags,
            SharpDX.D3DCompiler.EffectFlags fxFlags,
            out SharpDX.Direct3D.Blob compiledEffectOut,
            out SharpDX.Direct3D.Blob errorsOut)
        {
            unsafe
            {
                IntPtr srcFileNameRef_ = Marshal.StringToHGlobalAnsi(srcFileNameRef);
                SharpDX.Direct3D.ShaderMacro.__Native[] definesRef__ = (definesRef == null)
                                                                           ? null
                                                                           : new SharpDX.Direct3D.ShaderMacro.__Native[definesRef.Length];
                if (definesRef != null)
                {
                    for (int i = 0; i < definesRef.Length; i++)
                    {
                        definesRef[i].__MarshalTo(ref definesRef__[i]);
                    }
                }
                IntPtr         compiledEffectOut_ = IntPtr.Zero;
                IntPtr         errorsOut_         = IntPtr.Zero;
                SharpDX.Result __result__;
                fixed(void *definesRef_ = definesRef__)
                {
                    __result__ = SharpDX.D3DCompiler.LocalInterop.CalliFuncint(
                        (void *)dataRef,
                        (void *)dataLength,
                        (void *)srcFileNameRef_,
                        definesRef_,
                        (void *)includeRef,
                        unchecked ((int)hlslFlags),
                        unchecked ((int)fxFlags),
                        &compiledEffectOut_,
                        &errorsOut_,
                        (void *)D3D10CompileEffectFromMemory_);
                }

                Marshal.FreeHGlobal(srcFileNameRef_);
                if (definesRef != null)
                {
                    for (int i = 0; i < definesRef.Length; i++)
                    {
                        definesRef[i].__MarshalFree(ref definesRef__[i]);
                    }
                }
                compiledEffectOut = (compiledEffectOut_ == IntPtr.Zero) ? null : new SharpDX.Direct3D.Blob(compiledEffectOut_);
                errorsOut         = (errorsOut_ == IntPtr.Zero) ? null : new SharpDX.Direct3D.Blob(errorsOut_);
                __result__.CheckError();
                return(__result__);
            }
        }
        private static byte[] CompileHLSL(ShaderInfo shaderInfo, string shaderFunction, string shaderProfile)
        {
            SharpDX.D3DCompiler.ShaderBytecode shaderByteCode;
            try
            {
                SharpDX.D3DCompiler.ShaderFlags shaderFlags = 0;

                // While we never allow preshaders, this flag is invalid for
                // the DX11 shader compiler which doesn't allow preshaders
                // in the first place.
                //shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.NoPreshader;

                if (shaderInfo.Profile == ShaderProfile.DirectX_11)
                {
                    shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.EnableBackwardsCompatibility;
                }

                if (shaderInfo.Debug)
                {
                    shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.SkipOptimization;
                    shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.Debug;
                }
                else
                {
                    shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.OptimizationLevel3;
                }

                // Compile the shader into bytecode.
                var result = SharpDX.D3DCompiler.ShaderBytecode.Compile(
                    shaderInfo.fileContent,
                    shaderFunction,
                    shaderProfile,
                    shaderFlags,
                    0,
                    null,
                    null,
                    shaderInfo.fileName);

                if (result.HasErrors)
                {
                    throw new Exception(result.Message);
                }

                shaderByteCode = result.Bytecode;
                //var source = shaderByteCode.Disassemble();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            // Return a copy of the shader bytecode.
            return(shaderByteCode.Data.ToArray());
        }
Exemple #4
0
        private static DX11Effect CompileSharpDX(string content, bool isfile, Include include, ShaderMacro[] defines, bool library = false)
        {
            DX11Effect shader = new DX11Effect();

            var profile = library ? "lib_5_0" : "fx_5_0";

            SharpDX.D3DCompiler.Include sdxInclude = include != null ? new SharpDXIncludeWrapper(include) : null;
            var sdxDefines = defines != null?defines.AsSharpDXMacro() : null;

            string errors;

            try
            {
                SharpDX.D3DCompiler.ShaderFlags flags = SharpDX.D3DCompiler.ShaderFlags.OptimizationLevel1;

                if (isfile)
                {
                    SharpDX.D3DCompiler.CompilationResult result = SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile(content, profile, flags, SharpDX.D3DCompiler.EffectFlags.None, sdxDefines, sdxInclude);

                    if (result.Bytecode != null)
                    {
                        SlimDX.DataStream ds = new SlimDX.DataStream(result.Bytecode, true, true);
                        shader.ByteCode = new ShaderBytecode(ds);
                        shader.Preprocess();
                    }
                    errors = result.Message;
                }
                else
                {
                    //shader.ByteCode = ShaderBytecode.Compile(content, "fx_5_0", flags, EffectFlags.None, defines, include, out errors);
                    SharpDX.D3DCompiler.CompilationResult result = SharpDX.D3DCompiler.ShaderBytecode.Compile(content, profile, flags, SharpDX.D3DCompiler.EffectFlags.None, sdxDefines, sdxInclude);

                    if (result.Bytecode != null)
                    {
                        SlimDX.DataStream ds = new SlimDX.DataStream(result.Bytecode, true, true);
                        shader.ByteCode = new ShaderBytecode(ds);
                        shader.Preprocess();
                    }
                    errors = result.Message;
                }

                //Compilation worked, but we can still have warning
                shader.IsCompiled   = shader.ByteCode != null;
                shader.ErrorMessage = errors;
            }
            catch (Exception ex)
            {
                shader.IsCompiled    = false;
                shader.ErrorMessage  = ex.Message;
                shader.DefaultEffect = null;
            }
            return(shader);
        }
        public static ComputeShader GetComputeShader(SharpDX.Direct3D11.Device _dx11Device, string sourse, SharpDX.Direct3D.ShaderMacro[] defines)
        {
            SharpDX.D3DCompiler.ShaderFlags shaderFlags = SharpDX.D3DCompiler.ShaderFlags.None;
#if DEBUG
            shaderFlags = SharpDX.D3DCompiler.ShaderFlags.Debug;
#endif
            using (var horizBC = SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile(sourse,
                                                                                    "CS",
                                                                                    "cs_5_0",
                                                                                    shaderFlags,
                                                                                    SharpDX.D3DCompiler.EffectFlags.None,
                                                                                    defines, null))
                return(new ComputeShader(_dx11Device, horizBC));
        }
Exemple #6
0
        private d3dx_state CreateShader(TwoMGFX.ShaderInfo shaderInfo, string shaderFunction, string shaderProfile, bool isVertexShader)
        {
            // Compile the shader.
            SharpDX.D3DCompiler.ShaderBytecode shaderByteCode;
            try
            {
                SharpDX.D3DCompiler.ShaderFlags shaderFlags = 0;

                // While we never allow preshaders, this flag is invalid for
                // the DX11 shader compiler which doesn't allow preshaders
                // in the first place.
                //shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.NoPreshader;

                if (shaderInfo.DX11Profile)
                {
                    shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.EnableBackwardsCompatibility;
                }

                if (shaderInfo.Debug)
                {
                    shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.SkipOptimization;
                    shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.Debug;
                }
                else
                {
                    shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.OptimizationLevel3;
                }

                // Compile the shader into bytecode.
                var result = SharpDX.D3DCompiler.ShaderBytecode.Compile(
                    shaderInfo.fileContent,
                    shaderFunction,
                    shaderProfile,
                    shaderFlags,
                    0,
                    null,
                    null,
                    shaderInfo.fileName);

                if (result.HasErrors)
                {
                    throw new Exception(result.Message);
                }

                shaderByteCode = result.Bytecode;
                //var source = shaderByteCode.Disassemble();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            // Get a copy of the shader bytecode.
            var bytecode = shaderByteCode.Data.ToArray();

            // First look to see if we already created this same shader.
            DXShaderData dxShader = null;

            foreach (var shader in Shaders)
            {
                if (bytecode.SequenceEqual(shader.Bytecode))
                {
                    dxShader = shader;
                    break;
                }
            }

            // Create a new shader.
            if (dxShader == null)
            {
                if (shaderInfo.DX11Profile)
                {
                    dxShader = DXShaderData.CreateHLSL(bytecode, isVertexShader, ConstantBuffers, Shaders.Count, shaderInfo.Debug);
                }
                else
                {
                    dxShader = DXShaderData.CreateGLSL(bytecode, ConstantBuffers, Shaders.Count);
                }

                Shaders.Add(dxShader);
            }

            //var assmbly = desc.Bytecode.Disassemble();
            //var buffer = reflection.GetConstantBuffer(0);

            var state = new d3dx_state();

            state.index     = 0;
            state.type      = STATE_TYPE.CONSTANT;
            state.operation = isVertexShader ? (uint)146 : (uint)147;

            state.parameter          = new d3dx_parameter();
            state.parameter.name     = string.Empty;
            state.parameter.semantic = string.Empty;
            state.parameter.class_   = D3DXPARAMETER_CLASS.OBJECT;
            state.parameter.type     = isVertexShader ? D3DXPARAMETER_TYPE.VERTEXSHADER : D3DXPARAMETER_TYPE.PIXELSHADER;
            state.parameter.rows     = 0;
            state.parameter.columns  = 0;
            state.parameter.data     = dxShader.SharedIndex;

            return(state);
        }