Esempio n. 1
0
        /// <summary>
        /// Initialize the graphic library.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();

            // - Create a Vulkan instance, this instance must be alive during all the game execution
            Library.Initialize(TargetSurface);

            // - Makes a new device manager
            PhysicalDevice.Initialize(TargetSurface);

            // - Initialize the physical device on this drawing area
            GraphicDevice.Initialize();

            // - Initialize index and vert manager
            VertexManager.Setup();
            IndexManager.Setup();

            // - Makes syncronization semaphores and fances CPU<->GPU
            for (int i = 0; i < MaxFrameInFlight; ++i)
            {
                CommandBuffers.Add(null);
                Commands.Add(GraphicDevice.Handle.CreateCommandPool(GraphicDevice.GraphicQueueIndex, CommandPoolCreateFlags.ResetCommandBuffer));
                ImageAvailableSemaphores.Add(GraphicDevice.Handle.CreateSemaphore());
                RenderFinishedSemaphores.Add(GraphicDevice.Handle.CreateSemaphore());
                InFlightFences.Add(GraphicDevice.Handle.CreateFence(FenceCreateFlags.Signaled));
            }
        }
Esempio n. 2
0
        public static void Initialize(IInputContext inputContext, GraphicDeviceDesc graphicDeviceDesc)
        {
            if (_initialized)
            {
                return;
            }

            var graphicFactory = Service.Require <GraphicDeviceFactory>();

            _graphics = graphicFactory.CreateDevice(graphicDeviceDesc);

            var input = Service.Get <InputManager>();

            if (input != null)
            {
                _keyboard = input.CreateKeyboard(inputContext);
                if (_keyboard != null)
                {
                    _keyboardController = new KeyBoardController(_keyboard);
                }

                _mouse = input.CreateMouse(inputContext);
                if (_mouse != null)
                {
                    _mouseController = new MouseController(_mouse);
                }
                _joysticks = input.CreateJoysticks(inputContext);
            }

            RenderManager.PushTechnique <DefaultTechnique>();
            _initialized = true;
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a new instance of <see cref="ShaderManager"/> class.
        /// </summary>
        public ShaderManager(GraphicDevice device)
        {
            GraphicDevice = device;

            // - Initialize the store
            ShaderStore = new Dictionary <Type, ShaderInstance>();
        }
        public bool Apply(int technique, GraphicDevice device)
        {
            if (_rState != null)
            {
                _oldrsState       = device.Rasterizer;
                device.Rasterizer = _rState;
            }
            if (_blendState != null)
            {
                _oldBlendState = device.Blend;
                device.Blend   = _blendState;
            }
            if (_zBufferState != null)
            {
                _oldzBufferState = device.DepthTest;
                device.DepthTest = _zBufferState;
            }

            if (device.Program != program)
            {
                //bind variables to the pipeline
                foreach (var v in _variables)
                {
                    var variable = v.Variable;
                    variable.Binder = v.Setter;
                    variable.SetValue();
                }

                device.Program = program;

                return(true);
            }

            return(false);
        }
Esempio n. 5
0
        /// <summary>
        /// This call configure the graphic library for the new device instance.
        /// </summary>
        public override void InvalidateDevice()
        {
            base.InvalidateDevice();

            // - Wait the GPU (we must operate only when GPU is idle)
            GraphicDevice.Handle.WaitIdle();

            // - Dispose render pass
            DefaultRenderPass.Dispose();

            // - Dispose the pipeline
            Pipeline.Dispose();

            // - Dipose current swapchain
            Swapchain.Dispose();

            // - Dispose current VK surface
            DrawingSurface.Dispose();

            // - Dispose vertex and index managers
            VertexManager.Dispose();
            IndexManager.Dispose();

            // - Release all VK shaders
            ShaderManager.Dispose();

            for (int i = 0; i < MaxFrameInFlight; ++i)
            {
                if (CommandBuffers[i] != null)
                {
                    foreach (var buffer in CommandBuffers[i])
                    {
                        buffer.Reset();
                    }
                    Commands[i].FreeCommandBuffers(CommandBuffers[i]);
                }
            }

            foreach (Fence fence in InFlightFences)
            {
                fence.Destroy();
            }
            foreach (Semaphore sem in ImageAvailableSemaphores)
            {
                sem.Destroy();
            }
            foreach (Semaphore sem in RenderFinishedSemaphores)
            {
                sem.Destroy();
            }

            // - Dispose device
            GraphicDevice.Dispose();

            // - Initialize device
            Initialize();

            // - Reconfigure the renderer
            ConfigureRendering();
        }
Esempio n. 6
0
 public WindowFont(GraphicDevice device, Font font)
     : base(device)
 {
     this._font = font;
     Family     = font.FontFamily.Name;
     FontHeight = font.Height;
 }
Esempio n. 7
0
        /// <summary>
        /// Implement IDisposable.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            GraphicDevice.Handle.WaitForFences(InFlightFences.ToArray(), true, UInt64.MaxValue);

            // - Wait the GPU (we must operate only when GPU is idle)
            GraphicDevice.Handle.WaitIdle();

            // - Dispose render pass
            DefaultRenderPass.Dispose();

            // - Dispose the pipeline
            Pipeline.Dispose();

            // - Dipose current swapchain
            Swapchain.Dispose();

            // - Dispose vertex and index managers
            VertexManager.Dispose();
            IndexManager.Dispose();

            // - Release all VK shaders
            ShaderManager.Dispose();

            for (int i = 0; i < MaxFrameInFlight; ++i)
            {
                if (CommandBuffers[i] != null)
                {
                    foreach (var buffer in CommandBuffers[i])
                    {
                        buffer.Reset();
                    }
                }
            }

            foreach (CommandPool pool in Commands)
            {
                pool?.Destroy();
            }
            foreach (Fence fence in InFlightFences)
            {
                fence?.Destroy();
            }
            foreach (Semaphore sem in ImageAvailableSemaphores)
            {
                sem?.Destroy();
            }
            foreach (Semaphore sem in RenderFinishedSemaphores)
            {
                sem?.Destroy();
            }

            // - Dispose device
            GraphicDevice.Dispose();

            // - Dispose vulkan
            Library.Dispose();
        }
        public void SetTexture(int slot = 0, GraphicDevice device = null)
        {
            if (device == null)
            {
                device = GraphicDeviceFactory.Device;
            }

            device.PS.SetResource(slot, _texture);
        }
Esempio n. 9
0
        public Effect(GraphicDevice device)
        {
            this._device = device;
            var techs = GetTechniques();

            if (techs != null)
            {
                _SetTechniques(techs);
            }
        }
Esempio n. 10
0
        private void DrawMesh(Mesh mesh, GraphicDevice device)
        {
            device.SetVertexBuffer(0, mesh.VertexBuffer, 0);
            device.SetIndexBuffer(mesh.IndexBuffer);
            var effect = Effect;

            foreach (var pass in effect.Passes())
            {
                effect.Apply(pass);
                device.DrawIndexed(mesh.Layers[0].IndexCount, 0, 0);
            }
        }
Esempio n. 11
0
        private void DrawMeshPart(Mesh mesh, MeshPart part, GraphicDevice device)
        {
            device.SetVertexBuffer(0, mesh.VertexBuffer, 0);
            device.SetIndexBuffer(mesh.IndexBuffer);
            var effect = Effect;

            foreach (var pass in effect.Passes())
            {
                effect.Apply(pass);
                device.DrawIndexed(part.IndexCount, part.StartIndex, 0);
            }
        }
Esempio n. 12
0
        public ActorRender(GraphicDevice device) : base(device)
        {
            _sphere   = Mesh.CreateSphere(16, 16, 1);
            _box      = Mesh.CreateBox(2, 2, 2);
            _cylindre = Mesh.CreateCylindre(1, 16, 1, 1, false);
            _capsule  = Mesh.CreateCapsule(1, 1, 16, 16, 1, 16);

            _rasterizer = GraphicDeviceFactory.Device.CreateRasterizerState(new RasterizerDesc(true)
            {
                Fill = FillMode.Wireframe,
            });
        }
Esempio n. 13
0
        public static Effect GetEffect <T>(GraphicDevice device)
            where T : Effect
        {
            var effect = Service.Get <T>();

            if (effect == null)
            {
                effect = (T)Activator.CreateInstance(typeof(T), device);
                Service.Set(effect);
            }
            return(effect);
        }
Esempio n. 14
0
        public void Load()
        {
            Debugger.WriteLog($"Loading...", LogType.Info, nameof(Engine));
            GraphicDevice.SetBufferSize(new Point2D(800, 600));
            AddComponent(new RessourceManager());
            AddComponent(new AudioManager());
            AddComponent(new InputManager(GraphicDevice));

            Game.Engine = this;
            Game.Load();
            GraphicDevice.SetTitle(Game.Name);
            Debugger.WriteLog($"Entering Game Loop!", LogType.Info, nameof(Engine));
        }
Esempio n. 15
0
        public void Draw(GraphicDevice device, Effect effect)
        {
            device.PrimitiveTopology = IAPrimitive.TriangleList;
            device.SetVertexBuffer(0, _vb, 0);
            device.SetIndexBuffer(_ib);

            foreach (var pass in effect.Passes())
            {
                effect.Apply(pass);
                foreach (var part in _layers)
                {
                    device.DrawIndexed(part.IndexCount, part.StartIndex, 0);
                }
            }
        }
Esempio n. 16
0
        private void RenderTexture(GraphicDevice device, ITexture texture, int x = 0, int y = 0, int width = 256, int height = 256)
        {
            var untranformed = Service.Require <RenderQuadEffect>();
            var sprite       = Service.Require <Sprite>();

            device.Ps.SetResource(0, texture);
            device.Ps.SetSampler(0, SamplerState.Linear);

            sprite.Begin();
            sprite.SetTrasform(untranformed, new Igneel.Rectangle(x, y, width, height), Matrix.Identity);
            sprite.DrawQuad(untranformed);
            sprite.End();

            device.Ps.SetResource(0, null);
        }
Esempio n. 17
0
 public void Clear(Effect effect, GraphicDevice device)
 {
     if (_oldrsState != null)
     {
         device.Rasterizer = _oldrsState;
     }
     if (_oldBlendState != null)
     {
         device.Blend = _oldBlendState;
     }
     if (_oldzBufferState != null)
     {
         device.DepthTest = _oldzBufferState;
     }
 }
        public void SetTarget(int face, GraphicDevice device = null)
        {
            if (device == null)
            {
                device = GraphicDeviceFactory.Device;
            }

            if (_depthStencil != null)
            {
                device.SetRenderTarget(_targets[face], _depthStencil);
            }
            else
            {
                device.SetRenderTarget(_targets[face]);
            }
        }
        private void RenderLayers(GraphicDevice device, MeshPart[] layers)
        {
            var effect = this.Effect;

            effect.OnRender(this);

            foreach (var pass in effect.Passes())
            {
                effect.Apply(pass);
                foreach (var layer in layers)
                {
                    device.DrawIndexed(layer.primitiveCount * 3, layer.startIndex, 0);
                }
            }
            effect.EndPasses();
        }
Esempio n. 20
0
        private void RenderLayers(GraphicDevice device,
                                  MeshSkin skin,
                                  Frame[] bones,
                                  Matrix[] boneOffsetMatrices,
                                  ref Matrix bindShapePose,
                                  MeshPart[] layers)
        {
            var effect = this.Effect;

            effect.OnRender(this);

            foreach (var pass in effect.Passes())
            {
                effect.Apply(pass);
                foreach (var layer in layers)
                {
                    #region Compute Bone Matrices

                    var bonesIDs = skin.GetBones(layer);
                    if (bonesIDs != null)
                    {
                        int paletteEntry = 0;
                        for (paletteEntry = 0; paletteEntry < bonesIDs.Length; paletteEntry++)
                        {
                            int boneIndex = bonesIDs[paletteEntry];

                            Matrix globalPose = bones[boneIndex].GlobalPose;

                            Matrix.Multiply(ref bindShapePose, ref boneOffsetMatrices[boneIndex], out _boneMatrices[paletteEntry]);
                            Matrix.Multiply(ref _boneMatrices[paletteEntry], ref globalPose, out _boneMatrices[paletteEntry]);
                            //boneMatrices[paletteEntry] = skin.BindShapePose * boneOffsetMatrices[boneIndex] * bones[boneIndex].GlobalPose;
                        }

                        SkinMap.WorldArray = new SArray <Matrix>(_boneMatrices, paletteEntry);
                        //mapping.WorldArray = boneMatrices;
                    }

                    #endregion

                    device.DrawIndexed(layer.primitiveCount * 3, layer.startIndex, 0);
                }
            }
            effect.EndPasses();
        }
Esempio n. 21
0
        private int CreateResources()
        {
            GraphicDevice device     = GraphicDeviceFactory.Device;
            var           backBuffer = device.BackBuffer;
            int           width      = backBuffer.Width;
            int           height     = backBuffer.Height;

            Format depthFormat = Format.UNKNOWN;

            _rtHdrScene                = new RenderTexture2D(width, height, _hdrFormat, device.BackDepthBuffer.SurfaceFormat, backBuffer.Sampling);
            _rtBrightPassFilter        = new RenderTexture2D(width / 2, height / 2, Format.R8G8B8A8_UNORM, depthFormat);
            _rtStartSource             = new RenderTexture2D(width / 4, height / 4, Format.R8G8B8A8_UNORM, depthFormat);
            _rtCurrentAdaptedLuminance = new RenderTexture2D(1, 1, _luminanceFormat, depthFormat);
            _rtLastAdaptedLuminance    = new RenderTexture2D(1, 1, _luminanceFormat, depthFormat);
            _rtStarFinal               = new RenderTexture2D(_rtStartSource.Width, _rtStartSource.Height, Format.R8G8B8A8_UNORM, depthFormat);

            int i;

            for (i = 0; i < Bloomtextures; i++)
            {
                _rtBloom[i] = new RenderTexture2D(width / 8, height / 8, Format.R8G8B8A8_UNORM);
            }

            for (i = 0; i < Starmaxlines; i++)
            {
                _rtStarLines[i] = new RenderTexture2D(_rtStartSource.Width, _rtStartSource.Height, _hdrFormat);
            }

            for (i = 0; i < Starmaxpasses; i++)
            {
                _rtStarPasses[i] = new RenderTexture2D(_rtStartSource.Width, _rtStartSource.Height, _hdrFormat);
            }

            int size = 1;

            for (i = 0; i < Tonemaptextures; i++)
            {
                _rtToneMap[i] = new RenderTexture2D(size, size, _luminanceFormat, depthFormat);
                size         *= 3;
            }
            return(i);
        }
Esempio n. 22
0
        public void RenderTexture(GraphicDevice device, Texture2D texture, int x = 0, int y = 0, int width = 256, int height = 256)
        {
            if (untranformed == null)
            {
                untranformed = Igneel.Rendering.Effect.GetEffect <RenderQuadEffect>(GraphicDeviceFactory.Device);
                input        = untranformed.Map <Igneel.Rendering.Sprite.IShaderInput>();
            }

            var sprite = Service.Require <Sprite>();

            device.PS.SetResource(0, texture);
            device.PS.SetSampler(0, SamplerState.Linear);

            sprite.Begin();
            sprite.SetTrasform(input, new Igneel.Rectangle(x, y, width, height), Igneel.Matrix.Identity);
            sprite.DrawQuad(untranformed);
            sprite.End();

            device.PS.SetResource(0, null);
        }
        public EffectPassDesc(GraphicDevice device,
                              RasterizerState rasterizer = null,
                              BlendState blend           = null,
                              DepthStencilState zbuffer  = null,
                              params string[] shaders)
        {
            if (shaders == null)
            {
                throw new ArgumentNullException("shaders");
            }

            RState       = rasterizer;
            BlendState   = blend;
            ZBufferState = zbuffer;

            Program = new ShaderProgramDesc(device);
            foreach (var item in shaders)
            {
                Program.LinkShader(item);
            }
        }
Esempio n. 24
0
        private void MensureLuminance(GraphicDevice device, IShaderStage stage)
        {
            effect.Technique = HdrEffect.SampleAvgLum;

            RenderTexture2D dest   = _rtToneMap[Tonemaptextures - 1];
            RenderTexture2D source = _rtHdrScene;

            dest.SetTarget(device);
            source.SetTexture(0, device);

            stage.SetSampler(0, _linearSampler);

            device.Clear(ClearFlags.Target, Color4.Black, 1, 0);
            _quadRender.DrawQuad(effect);

            effect.Technique = HdrEffect.ResampleAvgLum;
            stage.SetSampler(0, _pointSampler);

            for (int i = Tonemaptextures - 2; i > 0; i--)
            {
                dest   = _rtToneMap[i];
                source = _rtToneMap[i + 1];

                dest.SetTarget(device);
                source.SetTexture(0, device);
                device.Clear(ClearFlags.Target, Color4.Black, 1, 0);
                _quadRender.DrawQuad(effect);
            }

            effect.Technique = HdrEffect.ResampleAvgLumExp;
            source           = _rtToneMap[1];
            dest             = _rtToneMap[0];

            dest.SetTarget(device);
            source.SetTexture(0, device);

            device.Clear(ClearFlags.Target, Color4.Black, 1, 0);
            _quadRender.DrawQuad(effect);
        }
Esempio n. 25
0
        private void CreateDevice()
        {
            //Tell the API were the folder containing the shader's code are ,and the ShaderModel
            ShaderRepository.SetupD3D10_SM40("Shaders");

            //Instantiate GraphicDeviceFactory in this case we are using Direct3D10 so an instance of GraphicManager10 is required
            var devFactory = new IgneelD3D10.GraphicManager10();

            //Create de GraphicDevice passsing a GraphicDeviceDesc containing device configuration and
            //and the rendering environment. In this case we use the WindowContext because this is a WindowsForms application
            device = devFactory.CreateDevice(new GraphicDeviceDesc(new WindowContext(Handle)
            {
                BackBufferWidth    = Width,
                BackBufferHeight   = Height,
                BackBufferFormat   = Format.R8G8B8A8_UNORM_SRGB,
                DepthStencilFormat = Format.D24_UNORM_S8_UINT,
                FullScreen         = false,
                Sampling           = new Multisampling(1, 0),
                Presentation       = PresentionInterval.Default
            }
                                                                   ));

            //Creates a ShaderProgram passsing the files containing the shader's code for the VertexShader and PixelShader stages
            shaderProgram = device.CreateProgram <TriangleVertex>("VertexShaderVS", "PixelShaderPS");

            //Retrieve a type safe mapping for setting the uniforms variables
            input = shaderProgram.Map <ProgramMapping>();

            //Set pipeline states
            blendState       = device.CreateBlendState(new BlendDesc(blendEnable: false, srcBlend: Blend.SourceAlpha, destBlend: Blend.InverseSourceAlpha));
            depthStenciState = device.CreateDepthStencilState(new DepthStencilStateDesc(depthFunc: Comparison.Less));
            cullingState     = device.CreateRasterizerState(new RasterizerDesc(cull: CullMode.Back));

            device.Blend      = blendState;
            device.Rasterizer = cullingState;
            device.DepthTest  = depthStenciState;
        }
Esempio n. 26
0
        private void RenderCompose(ComposedShape <T> composeShape, Matrix world, GraphicDevice device)
        {
            //if (composeShape.Shapes != null)
            //{
            //    for (int i = 0; i < composeShape.Shapes.Length; i++)
            //    {
            //        ShapeBuilder<T> shape = composeShape.Shapes[i];
            //        var matrix = composeShape.Transforms[i];
            //        effect.SetWorldMatrix(matrix * world);
            //        if (composeShape.Colors != null)
            //            effect.SetValue(hColor, composeShape.Colors[i]);

            //        effect.Apply(() =>
            //          {
            //              if (shape.Indices != null)
            //              {
            //                  device.DrawIndexedUserPrimitives(shape.PrimitiveType, 0, shape.Vertices.Length,
            //                                     shape.Indices.Length / GetPrimitiveSize(shape.PrimitiveType), shape.Indices,
            //                                     Format.Index16, shape.Vertices, vd.Size);
            //              }
            //              else
            //                 device.DrawUserPrimitives(shape.PrimitiveType, shape.Vertices.Length / GetPrimitiveSize(shape.PrimitiveType), shape.Vertices);
            //          });
            //    }
            //}
            //else if (composeShape.Components != null)
            //{
            //    for (int i = 0; i < composeShape.Components.Length; i++)
            //    {
            //        var component = composeShape.Components[i];
            //        var matrix = composeShape.Transforms[i];
            //        if (composeShape.Colors != null)
            //            effect.SetValue(hColor, composeShape.Colors[i]);
            //        RenderCompose(component, matrix * world, device);
            //    }
            //}
        }
Esempio n. 27
0
        private void DrawShape(ActorShape shape, GraphicDevice device)
        {
            Matrix scale;

            if (shape is BoxShape)
            {
                BoxShape boxShape = (BoxShape)shape;
                scale = Matrix.Scale(boxShape.Dimensions);
                Effect.Input.World = scale * boxShape.GlobalPose;
                DrawMesh(_box, device);
            }
            else if (shape is SphereShape)
            {
                SphereShape sphereShape = (SphereShape)shape;
                scale = Matrix.Scale(new Vector3(sphereShape.Radius));
                Effect.Input.World = scale * shape.GlobalPose;
                DrawMesh(_sphere, device);
            }
            else if (shape is PlaneShape)
            {
                scale = Matrix.Scale(Engine.Scene.ActiveCamera.ZFar, 0, Engine.Scene.ActiveCamera.ZFar);
                Effect.Input.World = scale * shape.GlobalPose;
                DrawMesh(_box, device);
            }
            else if (shape is WheelShape)
            {
                WheelShape wheel = (WheelShape)shape;

                Effect.Input.World = Matrix.Scale(wheel.Radius, 0, wheel.Radius) *
                                     Matrix.RotationZ(Numerics.PIover2) *
                                     Matrix.RotationY(wheel.SteerAngle) *
                                     shape.GlobalPose;

                DrawMesh(_cylindre, device);
            }
            else if (shape is CapsuleShape)
            {
                CapsuleShape capsuleShape = (CapsuleShape)shape;
                float        radius       = capsuleShape.Radius;
                float        height       = capsuleShape.Height;
                Effect.Input.World = Matrix.Translate(0, -0.5f, 0) *
                                     Matrix.Scale(radius, radius, radius) *
                                     Matrix.Translate(0, height * 0.5f, 0) * shape.GlobalPose;

                DrawMeshPart(_capsule, _capsule.Layers[0], device);

                Effect.Input.World = Matrix.Translate(0, 0.5f, 0) *
                                     Matrix.Scale(radius, radius, radius) *
                                     Matrix.Translate(0, -(height * 0.5f), 0) * shape.GlobalPose;
                DrawMeshPart(_capsule, _capsule.Layers[2], device);

                Effect.Input.World = Matrix.Scale(radius, height, radius) * shape.GlobalPose;
                DrawMeshPart(_capsule, _capsule.Layers[1], device);
            }
            else if (shape is TriangleMeshShape)
            {
                TriangleMesh tmesh = ((TriangleMeshShape)shape).Mesh;
                var          mesh  = (Mesh)tmesh.GraphicMesh;
                if (mesh != null)
                {
                    Effect.Input.World = shape.GlobalPose;

                    DrawMesh(mesh, device);
                }
            }
        }
 public BillboardEffect(GraphicDevice device)
     : base(device)
 {
 }
Esempio n. 29
0
 public TechniqueDesc(GraphicDevice device)
 {
     this.Device = device;
 }
Esempio n. 30
0
 private void DownSample(GraphicDevice graphic, RenderTexture2D src, RenderTexture2D dest)
 {
     dest.SetTarget(graphic);
     src.SetTexture(0, graphic);
     _sprite.DrawQuad(_effect);
 }