Exemple #1
0
            public void DoCompile()
            {
                if (!m_FxcCompile.m_CompileError) // don't compile more stuff if we already have a known error
                {
                    SharpDX.D3DCompiler.CompilationResult result = null;
                    string exception = null;

                    try
                    {
                        result = SharpDX.D3DCompiler.ShaderBytecode.Compile(
                            m_ShaderSource,
                            "main",
                            m_Profile,
                            SharpDX.D3DCompiler.ShaderFlags.OptimizationLevel3,
                            SharpDX.D3DCompiler.EffectFlags.None,
                            m_Defines,
                            this,
                            "shaderCombo_" + m_ComboId
                            );
                    }
                    catch (SharpDX.SharpDXException e)
                    {
                        exception = e.ToString();
                        result    = null;
                    }

                    if (null == result || result.HasErrors)
                    {
                        m_FxcCompile.m_CompileError = true;

                        string compileErrors =
                            "shaderCombo_" + m_ComboId + ": "
                            + "message: " + (null == result ? "[none]" : result.Message)
                            + ", exception: " + (null == exception ? "[none]" : exception)
                            + ", defines:"
                        ;

                        foreach (SharpDX.Direct3D.ShaderMacro define in m_Defines)
                        {
                            compileErrors += " /D" + define.Name.ToString() + "=" + define.Definition.ToString();
                        }

                        lock (m_FxcCompile.m_CompileErrors)
                        {
                            m_FxcCompile.m_CompileErrors.AddLast(
                                compileErrors
                                );
                        }

                        result = null;
                    }

                    lock (m_FxcCompile.m_CompilationResults)
                    {
                        m_FxcCompile.m_CompilationResults[m_ComboId] = result;
                    }
                }

                Interlocked.Decrement(ref m_FxcCompile.m_OutstandingCompiles);
            }
Exemple #2
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 Effect(SharpDX.Direct3D11.Device Device, string ShaderCode)
        {
            // Load vertex shader
            SharpDX.D3DCompiler.CompilationResult result = SharpDX.D3DCompiler.ShaderBytecode.Compile(ShaderCode, VertexShaderEntrypoint, "vs_4_0");
            SharpDX.D3DCompiler.ShaderBytecode    vsb    = result.Bytecode;
            VertexShader = new VertexShader(Device, vsb);

            // Load pixel shader
            result = SharpDX.D3DCompiler.ShaderBytecode.Compile(ShaderCode, PixelShaderEntrypoint, "ps_4_0");
            SharpDX.D3DCompiler.ShaderBytecode psb = result.Bytecode;
            PixelShader = new PixelShader(Device, psb);
            psb.Dispose();

            // Create constant buffer
            ConstantBuffer = new SharpDX.Direct3D11.Buffer(Device, Utilities.SizeOf <ConstantBufferData>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);

            // Create input layout. This tells the input-assembler stage how to map items from our vertex structures into vertices for the vertex shader.
            // It is validated against the vertex shader bytecode because it needs to match properly.
            InputLayout = new InputLayout(Device, vsb, new Vertex().InputElements);
            vsb.Dispose();
        }