示例#1
0
文件: Program.cs 项目: mrommel/dx11
        private void BuildFX()
        {
            var shaderFlags = ShaderFlags.None;

#if DEBUG
            shaderFlags |= ShaderFlags.Debug;
            shaderFlags |= ShaderFlags.SkipOptimization;
#endif
            string         errors         = null;
            ShaderBytecode compiledShader = null;
            try
            {
                //compiledShader = new ShaderBytecode(new DataStream(File.ReadAllBytes("fx/Lighting.fxo"), false, false));
                //_fx = new Effect(Device, compiledShader);
                Core.FX.IncludeFX includeFX = new Core.FX.IncludeFX();
                compiledShader = ShaderBytecode.CompileFromFile("FX/Lighting.fx", null, "fx_5_0", shaderFlags, EffectFlags.None, null, includeFX, out errors);
                _fx            = new Effect(Device, compiledShader);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            finally
            {
                Util.ReleaseCom(ref compiledShader);
            }

            _tech         = _fx.GetTechniqueByName("LightTech");
            _fxWVP        = _fx.GetVariableByName("gWorldViewProj").AsMatrix();
            _fxWorld      = _fx.GetVariableByName("gWorld").AsMatrix();
            _fxWIT        = _fx.GetVariableByName("gWorldInvTranspose").AsMatrix();
            _fxEyePosW    = _fx.GetVariableByName("gEyePosW").AsVector();
            _fxDirLight   = _fx.GetVariableByName("gDirLight");
            _fxPointLight = _fx.GetVariableByName("gPointLight");
            _fxSpotLight  = _fx.GetVariableByName("gSpotLight");
            _fxMaterial   = _fx.GetVariableByName("gMaterial");
        }
示例#2
0
        public void TestFullscreenQuad()
        {
            BasicShader    shader = null;
            FullScreenQuad quad   = null;
            var            game   = new DX11Game();

            game.InitDirectX();

            var device = game.Device;

            var bytecode  = ShaderBytecode.CompileFromFile("../../Common.Core/Shaders/FullScreenQuad.fx", "fx_5_0", ShaderFlags.None, EffectFlags.None);
            var effect    = new Effect(game.Device, bytecode);
            var technique = effect.GetTechniqueByName("TestQuadTextured");
            var pass      = technique.GetPassByIndex(0);

            quad = new FullScreenQuad(game.Device);

            var diffuseShaderVariable = effect.GetVariableByName("txDiffuse").AsResource();

            var texturePath = Wallpaper001_png;

            var diffuseTexture = Texture2D.FromFile(device, texturePath);

            var diffuseTextureRv = new ShaderResourceView(device, diffuseTexture);

            diffuseShaderVariable.SetResource(diffuseTextureRv);

            var inputLayout = FullScreenQuad.CreateInputLayout(device, pass);

            game.GameLoopEvent += delegate
            {
                pass.Apply(game.Device.ImmediateContext);

                quad.Draw(inputLayout);
            };
            game.Run();
        }
示例#3
0
文件: Program.cs 项目: virus-404/dx11
        private void BuildFX()
        {
            ShaderBytecode compiledShader = null;

            try {
                compiledShader = new ShaderBytecode(new DataStream(File.ReadAllBytes("fx/Lighting.fxo"), false, false));
                _fx            = new Effect(Device, compiledShader);
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return;
            } finally {
                Util.ReleaseCom(ref compiledShader);
            }

            _tech         = _fx.GetTechniqueByName("LightTech");
            _fxWVP        = _fx.GetVariableByName("gWorldViewProj").AsMatrix();
            _fxWorld      = _fx.GetVariableByName("gWorld").AsMatrix();
            _fxWIT        = _fx.GetVariableByName("gWorldInvTranspose").AsMatrix();
            _fxEyePosW    = _fx.GetVariableByName("gEyePosW").AsVector();
            _fxDirLight   = _fx.GetVariableByName("gDirLight");
            _fxPointLight = _fx.GetVariableByName("gPointLight");
            _fxSpotLight  = _fx.GetVariableByName("gSpotLight");
            _fxMaterial   = _fx.GetVariableByName("gMaterial");
        }
示例#4
0
        private void BuildFX() {
            ShaderBytecode compiledShader = null;
            try {
                compiledShader = new ShaderBytecode(new DataStream(File.ReadAllBytes("fx/Lighting.fxo"), false, false));
                _fx = new Effect(Device, compiledShader);
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return;
            } finally {
                Util.ReleaseCom(ref compiledShader);
            }

            _tech = _fx.GetTechniqueByName("LightTech");
            _fxWVP = _fx.GetVariableByName("gWorldViewProj").AsMatrix();
            _fxWorld = _fx.GetVariableByName("gWorld").AsMatrix();
            _fxWIT = _fx.GetVariableByName("gWorldInvTranspose").AsMatrix();
            _fxEyePosW = _fx.GetVariableByName("gEyePosW").AsVector();
            _fxDirLight = _fx.GetVariableByName("gDirLight");
            _fxPointLight = _fx.GetVariableByName("gPointLight");
            _fxSpotLight = _fx.GetVariableByName("gSpotLight");
            _fxMaterial = _fx.GetVariableByName("gMaterial");
        }
示例#5
0
        public void TestDirectX11SpecaterCamera()
        {
            var keyboard = new SlimDXTWKeyboard();
            var dev      = new SlimDX.DirectInput.DirectInput();
            var kb       = new SlimDX.DirectInput.Keyboard(dev);

            kb.Acquire();

            var mouse = new TWMouse();
            var m     = new SlimDX.DirectInput.Mouse(dev);

            m.Acquire();



            var game = new DX11Form();

            game.InitDirectX();
            var device    = game.Device;
            var bytecode  = ShaderBytecode.CompileFromFile("../../Common.Core/Shaders/MiniTri.fx", "fx_5_0", ShaderFlags.None, EffectFlags.None);
            var effect    = new Effect(device, bytecode);
            var technique = effect.GetTechniqueByName("RenderTransform");
            var pass      = technique.GetPassByIndex(0);
            var layout    = new InputLayout(device, pass.Description.Signature,
                                            new[] {
                new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
                new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0),
                new InputElement("TEXCOORD", 0, Format.R32G32_Float, 32, 0)
            });

            var vertexStride = (16 + 16 + 8);
            var stream       = new DataStream(3 * vertexStride, true, true);

            stream.WriteRange(new[] {
                new VertexCustom(new Vector4(-1.0f, 0, 0, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector2(0.5f, 0)),
                new VertexCustom(new Vector4(0f, 1f, 0, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), new Vector2(0f, 1f)),
                new VertexCustom(new Vector4(1f, 0f, 0, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), new Vector2(1f, 1f))
            });
            stream.Position = 0;



            var vertices = new SlimDX.Direct3D11.Buffer(device, stream, new BufferDescription()
            {
                BindFlags      = BindFlags.VertexBuffer,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags    = ResourceOptionFlags.None,
                SizeInBytes    = 3 * vertexStride,
                Usage          = ResourceUsage.Default
            });

            stream.Dispose();

            //var world = Matrix.Translation(MathHelper.Forward);

            /*var viewProjection = Matrix.LookAtRH(Vector3.UnitZ * 5, -Vector3.UnitZ, MathHelper.Up)
             * Matrix.PerspectiveFovRH(MathHelper.PiOver4, 4f / 3f, 0.1f, 1000f);*/


            var diffuseShaderVariable = effect.GetVariableByName("txDiffuse").AsResource();
            var worldParam            = effect.GetVariableByName("world").AsMatrix();
            //worldParam.AsMatrix().SetMatrix(world);


            var viewProjParam = effect.GetVariableBySemantic("viewprojection").AsMatrix();

            /*viewProjParam.SetMatrix(
             *  viewProjection);*/

            //worldParam.SetMatrix(Matrix.Identity);
            //effect.GetVariableBySemantic("viewprojection").AsMatrix().SetMatrix(Matrix.Identity);

            var texturePath = Wallpaper001_png;

            var diffuseTexture = Texture2D.FromFile(device, texturePath);

            var diffuseTextureRv = new ShaderResourceView(device, diffuseTexture);


            diffuseShaderVariable.SetResource(diffuseTextureRv);


            var rasterizerState = RasterizerState.FromDescription(device, new RasterizerStateDescription()
            {
                CullMode = CullMode.None,
                FillMode = FillMode.Solid
            });

            device.ImmediateContext.Rasterizer.State = rasterizerState;


            var cam = new SpectaterCamera();

            game.GameLoopEvent += delegate
            {
                mouse.UpdateMouseState(m.GetCurrentState());
                keyboard.UpdateKeyboardState(kb.GetCurrentState());
                cam.Update(0.001f, keyboard, mouse);

                device.ImmediateContext.InputAssembler.InputLayout       = layout;
                device.ImmediateContext.InputAssembler.PrimitiveTopology =
                    PrimitiveTopology.TriangleList;
                device.ImmediateContext.InputAssembler.SetVertexBuffers(0,
                                                                        new VertexBufferBinding
                                                                            (vertices,
                                                                            vertexStride, 0));

                for (int i = 0; i < technique.Description.PassCount; ++i)
                {
                    pass.Apply(device.ImmediateContext);
                    device.ImmediateContext.Draw(3, 0);
                }

                viewProjParam.SetMatrix(cam.ViewProjection);
                worldParam.SetMatrix(Matrix.Identity);
                if (keyboard.IsKeyDown(Key.Escape))
                {
                    game.Exit();
                }
            };

            game.Run();

            bytecode.Dispose();
            effect.Dispose();
            layout.Dispose();
            vertices.Dispose();
            diffuseTexture.Dispose();
            diffuseTextureRv.Dispose();
            rasterizerState.Dispose();
            kb.Dispose();
            m.Dispose();
            dev.Dispose();
        }
示例#6
0
        public void TestDirectX11SimpleShader()
        {
            var game = new DX11Form();

            game.InitDirectX();
            var device    = game.Device;
            var bytecode  = ShaderBytecode.CompileFromFile("../../Common.Core/Shaders/MiniTri.fx", "fx_5_0", ShaderFlags.None, EffectFlags.None);
            var effect    = new Effect(device, bytecode);
            var technique = effect.GetTechniqueByName("Render");
            var pass      = technique.GetPassByIndex(0);
            var layout    = new InputLayout(device, pass.Description.Signature,
                                            new[] {
                new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
                new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0),
                new InputElement("TEXCOORD", 0, Format.R32G32_Float, 32, 0)
            });

            var vertexStride = (16 + 16 + 8);
            var stream       = new DataStream(3 * vertexStride, true, true);

            stream.WriteRange(new[] {
                new VertexCustom(new Vector4(0.0f, 0.5f, 0.5f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector2(0.5f, 0)),
                new VertexCustom(new Vector4(0.5f, -0.5f, 0.5f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), new Vector2(0f, 1f)),
                new VertexCustom(new Vector4(-0.5f, -0.5f, 0.5f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), new Vector2(1f, 1f))
            });
            stream.Position = 0;

            var vertices = new SlimDX.Direct3D11.Buffer(device, stream, new BufferDescription()
            {
                BindFlags      = BindFlags.VertexBuffer,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags    = ResourceOptionFlags.None,
                SizeInBytes    = 3 * vertexStride,
                Usage          = ResourceUsage.Default
            });

            stream.Dispose();


            var diffuseShaderVariable = effect.GetVariableByName("txDiffuse").AsResource();

            var texturePath = Wallpaper001_png;

            var diffuseTexture = Texture2D.FromFile(device, texturePath);

            var diffuseTextureRv = new ShaderResourceView(device, diffuseTexture);


            diffuseShaderVariable.SetResource(diffuseTextureRv);


            game.GameLoopEvent += delegate
            {
                device.ImmediateContext.InputAssembler.InputLayout       = layout;
                device.ImmediateContext.InputAssembler.PrimitiveTopology =
                    PrimitiveTopology.TriangleList;
                device.ImmediateContext.InputAssembler.SetVertexBuffers(0,
                                                                        new VertexBufferBinding
                                                                            (vertices,
                                                                            vertexStride, 0));

                for (int i = 0; i < technique.Description.PassCount; ++i)
                {
                    pass.Apply(device.ImmediateContext);
                    device.ImmediateContext.Draw(3, 0);
                }
            };

            game.Run();


            bytecode.Dispose();
            effect.Dispose();
            layout.Dispose();
            vertices.Dispose();
            diffuseTexture.Dispose();
            diffuseTextureRv.Dispose();
        }
示例#7
0
        private void ParseStandardGlobal(SlimDX.Direct3D11.Effect effect, EffectVariable sg)
        {
            if (!sg.Description.Name.ToLower().Equals("script"))
            {
                throw new InvalidMMEEffectShaderException(
                          string.Format("STANDARDGLOBALセマンティクスの指定される変数名は\"Script\"である必要があります、指定された変数名は\"{0}\"でした。",
                                        sg.Description.Name));
            }
            if (!sg.GetVariableType().Description.TypeName.ToLower().Equals("float"))
            {
                throw new InvalidMMEEffectShaderException(
                          string.Format("STANDARDGLOBALセマンティクスの指定される変数型は\"float\"である必要があります、指定された変数名は\"{0}\"でした。",
                                        sg.GetVariableType().Description.TypeName.ToLower()));
            }
            if (sg.AsScalar().GetFloat() != 0.8f)
            {
                throw new InvalidMMEEffectShaderException(
                          string.Format("STANDARDGLOBALセマンティクスの指定される値は\"0.8\"である必要があります、指定された値は\"{0}\"でした。",
                                        sg.AsScalar().GetFloat()));
            }
            //ScriptOutputの解析
            EffectVariable soVal = EffectParseHelper.getAnnotation(sg, "ScriptOutput", "string");

            if (soVal != null)
            {
                if (!soVal.AsString().GetString().Equals("color"))
                {
                    throw new InvalidMMEEffectShaderException(
                              string.Format(
                                  "STANDARDGLOBALセマンティクスの指定される変数のアノテーション「string ScriptOutput」は、\"color\"でなくてはなりません。指定された値は\"{0}\"でした。",
                                  soVal.AsString().GetString().ToLower()));
                }
            }
            EffectVariable scVal = EffectParseHelper.getAnnotation(sg, "ScriptClass", "string");

            if (scVal != null)
            {
                string sc = scVal.AsString().GetString();
                switch (sc.ToLower())
                {
                case "object":
                    ScriptClass = ScriptClass.Object;
                    break;

                case "scene":
                    ScriptClass = ScriptClass.Scene;
                    break;

                case "sceneorobject":
                    ScriptClass = ScriptClass.SceneOrObject;
                    break;

                default:
                    throw new InvalidMMEEffectShaderException(
                              string.Format(
                                  "STANDARDGLOBALセマンティクスの指定される変数のアノテーション「string ScriptClass」は、\"object\",\"scene\",\"sceneorobject\"でなくてはなりません。指定された値は\"{0}\"でした。(スペルミス?)",
                                  sc.ToLower()));
                }
            }
            EffectVariable sorVal = EffectParseHelper.getAnnotation(sg, "ScriptOrder", "string");

            if (sorVal != null)
            {
                string sor = sorVal.AsString().GetString();
                switch (sor.ToLower())
                {
                case "standard":
                    ScriptOrder = ScriptOrder.Standard;
                    break;

                case "preprocess":
                    ScriptOrder = ScriptOrder.Preprocess;
                    break;

                case "postprocess":
                    ScriptOrder = ScriptOrder.Postprocess;
                    break;

                default:
                    throw new InvalidMMEEffectShaderException(
                              string.Format(
                                  "STANDARDGLOBALセマンティクスの指定される変数のアノテーション「string ScriptOrder」は、\"standard\",\"preprocess\",\"postprocess\"でなくてはなりません。指定された値は\"{0}\"でした。(スペルミス?)",
                                  sor.ToLower()));
                }
            }
            EffectVariable scrVal = EffectParseHelper.getAnnotation(sg, "Script", "string");

            if (scrVal != null)
            {
                StandardGlobalScript = scrVal.AsString().GetString();
                if (string.IsNullOrEmpty(StandardGlobalScript))
                {
                    for (int i = 0; i < effect.Description.TechniqueCount; i++)
                    {
                        SortedTechnique.Add(effect.GetTechniqueByIndex(i));
                    }
                }
                else
                {
                    string[] scriptChunks = StandardGlobalScript.Split(';');
                    if (scriptChunks.Length == 1)
                    {
                        throw new InvalidMMEEffectShaderException(
                                  string.Format("STANDARDGLOBALセマンティクスの指定される変数のスクリプト「{0}」は読み込めませんでした。\";\"が足りません。",
                                                StandardGlobalScript));
                    }
                    string targetScript = scriptChunks[scriptChunks.Length - 2]; //最後のセミコロンが付いているスクリプト以外は無視
                    if (StandardGlobalScript.IndexOf("?") == -1)                 //見つからない時
                    {
                        string[] args = targetScript.Split('=');
                        if (args.Length > 2)
                        {
                            throw new InvalidMMEEffectShaderException(
                                      string.Format("STANDARDGLOBALセマンティクスの指定される変数のスクリプト「{0}」は読み込めませんでした。\"=\"の数が多すぎます。",
                                                    targetScript));
                        }
                        if (!args[0].ToLower().Equals("technique"))
                        {
                            throw new InvalidMMEEffectShaderException(
                                      string.Format(
                                          "STANDARDGLOBALセマンティクスの指定される変数のスクリプト「{0}」は読み込めませんでした。\"{1}\"は\"Technique\"であるべきです。(スペルミス?)",
                                          targetScript, args[0]));
                        }
                        EffectTechnique technique = effect.GetTechniqueByName(args[1]);
                        if (technique != null)
                        {
                            SortedTechnique.Add(technique);
                        }
                        else
                        {
                            throw new InvalidMMEEffectShaderException(
                                      string.Format(
                                          "STANDARDGLOBALセマンティクスの指定される変数のスクリプト「{0}」は読み込めませんでした。テクニック\"{1}\"は存在しません。(スペルミス?)",
                                          targetScript, args[1]));
                        }
                    }
                    else //?が見つかるとき
                    {
                        string[] args = targetScript.Split('?');
                        if (args.Length == 2)
                        {
                            string[] techniques = args[1].Split(':');
                            foreach (string technique in techniques)
                            {
                                EffectTechnique effectTechnique = effect.GetTechniqueByName(technique);
                                if (effectTechnique != null)
                                {
                                    SortedTechnique.Add(effectTechnique);
                                }
                                else
                                {
                                    throw new InvalidMMEEffectShaderException(
                                              string.Format(
                                                  "STANDARDGLOBALセマンティクスの指定される変数のスクリプト「{0}」は読み込めませんでした。テクニック\"{1}\"は見つかりません。(スペルミス?)",
                                                  targetScript, technique));
                                }
                            }
                        }
                        else if (args.Length > 2)
                        {
                            throw new InvalidMMEEffectShaderException(
                                      string.Format("STANDARDGLOBALセマンティクスの指定される変数のスクリプト「{0}」は読み込めませんでした。\"?\"の数が多すぎます。",
                                                    targetScript));
                        }
                    }
                    if (scriptChunks.Length > 2)
                    {
                        Debug.WriteLine(
                            string.Format(
                                "STANDARDGLOBALセマンティクスの指定される変数のスクリプト「{0}」では、複数回Techniqueの代入が行われていますが、最後の代入以外は無視されます。",
                                StandardGlobalScript));
                    }
                }
            }
        }
示例#8
0
文件: Program.cs 项目: remy22/dx11
        private void BuildFX()
        {
            ShaderBytecode compiledShader = null;
            try {
                compiledShader = new ShaderBytecode(new DataStream(File.ReadAllBytes("fx/color.fxo"), false, false));
                _fx = new Effect(Device, compiledShader);
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return;
            } finally {
                Util.ReleaseCom(ref compiledShader);
            }

            _tech = _fx.GetTechniqueByName("ColorTech");
            _fxWVP = _fx.GetVariableByName("gWorldViewProj").AsMatrix();
        }