public Renderable(GraphicsDevice graphicsDevice, Sdl2Window contextWindow) { _graphicsDevice = graphicsDevice; _contextWindow = contextWindow; _factory = new DisposeCollectorResourceFactory(_graphicsDevice.ResourceFactory); _commandList = _factory.CreateCommandList(); _sceneRuntimeState = new SceneRuntimeDescriptor(); _modelPNTTBDescriptorList = new List <ModelRuntimeDescriptor <VertexPositionNormalTextureTangentBitangent> >(); _modelPNDescriptorList = new List <ModelRuntimeDescriptor <VertexPositionNormal> >(); _modelPTDescriptorList = new List <ModelRuntimeDescriptor <VertexPositionTexture> >(); _modelPCDescriptorList = new List <ModelRuntimeDescriptor <VertexPositionColor> >(); _modelPDescriptorList = new List <ModelRuntimeDescriptor <VertexPosition> >(); PNTTBRuntimeGeometry = new GeometryDescriptor <VertexPositionNormalTextureTangentBitangent>(); PNRuntimeGeometry = new GeometryDescriptor <VertexPositionNormal>(); PTRuntimeGeometry = new GeometryDescriptor <VertexPositionTexture>(); PCRuntimeGeometry = new GeometryDescriptor <VertexPositionColor>(); PRuntimeGeometry = new GeometryDescriptor <VertexPosition>(); // Tick every millisecond _frameTimer = new FrameTimer(1.0); _running = false; }
private static IEnumerable <RenderPass> _allPasses; // List of all RenderPasses in the RenderPass enum public static void Initialize(Sdl2Window window) { // ReSharper disable once UseObjectOrCollectionInitializer var options = new GraphicsDeviceOptions( debug: false, swapchainDepthFormat: PixelFormat.R16_UNorm, syncToVerticalBlank: true, resourceBindingModel: ResourceBindingModel.Improved, preferDepthRangeZeroToOne: true, preferStandardClipSpaceYDirection: true); #if DEBUG options.Debug = true; #endif GraphicsDevice = VeldridStartup.CreateGraphicsDevice(window, options); ResourceFactory = new DisposeCollectorResourceFactory(GraphicsDevice.ResourceFactory); _commandList = ResourceFactory.CreateCommandList(); _renderPasses = new Dictionary <RenderPass, List <RenderObject> >(); _allPasses = Enum.GetValues(typeof(RenderPass)).Cast <RenderPass>(); foreach (var pass in _allPasses) { _renderPasses.Add(pass, new List <RenderObject>()); } Console.WriteLine($"Initialized Veldrid with {GraphicsDevice.BackendType} backend"); }
public void Run() { _factory = new DisposeCollectorResourceFactory(_gd.ResourceFactory); CreateResources(_factory); Stopwatch sw = Stopwatch.StartNew(); double previousElapsed = sw.Elapsed.TotalSeconds; while (_window.Exists) { double newElapsed = sw.Elapsed.TotalSeconds; float deltaSeconds = (float)(newElapsed - previousElapsed); InputSnapshot inputSnapshot = _window.PumpEvents(); InputTracker.UpdateFrameInput(inputSnapshot); if (_window.Exists) { previousElapsed = newElapsed; if (_windowResized) { _gd.ResizeMainWindow((uint)_window.Width, (uint)_window.Height); HandleWindowResize(); } Draw(deltaSeconds); } } _gd.WaitForIdle(); _factory.DisposeCollector.DisposeAll(); _gd.Dispose(); }
public override void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc, ResourceScope scope) { if ((scope & ResourceScope.Map) == 0) { return; } var renderer = sc.Models.GetRenderer <SpriteModelRenderer>(); var disposeFactory = new DisposeCollectorResourceFactory(gd.ResourceFactory, _disposeCollector); (var atlasImage, var vertexBuffers) = SPRResourceUtils.CreateSpriteAtlas(SpriteModel.SpriteFile, gd, disposeFactory, sc.TextureLoader); //TODO: disable mipmapping for certain sprites? var texture = sc.MapResourceCache.AddTexture2D(gd, disposeFactory, new ImageSharpTexture(atlasImage, true), $"{SpriteModel.Name}_atlas"); VertexBuffers = vertexBuffers; IndexBuffer = SPRResourceUtils.CreateIndexBuffer(gd, disposeFactory); RenderColorBuffer = disposeFactory.CreateBuffer(new BufferDescription((uint)Marshal.SizeOf <Vector4>(), BufferUsage.UniformBuffer | BufferUsage.Dynamic)); var view = sc.MapResourceCache.GetTextureView(gd.ResourceFactory, texture); ResourceSet = disposeFactory.CreateResourceSet(new ResourceSetDescription( renderer.Layout, sc.ProjectionMatrixBuffer, sc.ViewMatrixBuffer, sc.WorldAndInverseBuffer, view, sc.MainSampler, sc.LightingInfoBuffer, RenderColorBuffer)); }
public void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc, ResourceScope scope) { var factory = new DisposeCollectorResourceFactory(gd.ResourceFactory, _disposeCollector); BonesBuffer = factory.CreateBuffer(new BufferDescription((uint)(Marshal.SizeOf <Matrix4x4>() * MDLConstants.MaxBones), BufferUsage.UniformBuffer | BufferUsage.Dynamic)); RenderArgumentsBuffer = factory.CreateBuffer(new BufferDescription((uint)Marshal.SizeOf <StudioRenderArguments>(), BufferUsage.UniformBuffer | BufferUsage.Dynamic)); TextureDataBuffer = factory.CreateBuffer(new BufferDescription((uint)Marshal.SizeOf <StudioTextureData>(), BufferUsage.UniformBuffer | BufferUsage.Dynamic)); _sharedLayout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("WorldAndInverse", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("Bones", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("RenderArguments", ResourceKind.UniformBuffer, ShaderStages.Vertex | ShaderStages.Fragment), new ResourceLayoutElementDescription("TextureData", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("Sampler", ResourceKind.Sampler, ShaderStages.Fragment), new ResourceLayoutElementDescription("LightingInfo", ResourceKind.UniformBuffer, ShaderStages.Fragment))); TextureLayout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("Texture", ResourceKind.TextureReadOnly, ShaderStages.Fragment))); var vertexLayouts = new[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3), new VertexElementDescription("TextureCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2), new VertexElementDescription("Normal", VertexElementSemantic.Normal, VertexElementFormat.Float3), new VertexElementDescription("VertexBoneIndex", VertexElementSemantic.TextureCoordinate, VertexElementFormat.UInt1), new VertexElementDescription("NormalBoneIndex", VertexElementSemantic.TextureCoordinate, VertexElementFormat.UInt1)) }; for (var cullMode = CullBack; cullMode < CullModeCount; ++cullMode) { for (var maskMode = MaskDisabled; maskMode < MaskModeCount; ++maskMode) { for (var additiveMode = AdditiveDisabled; additiveMode < AdditiveModeCount; ++additiveMode) { _pipelines[cullMode, maskMode, additiveMode] = CreatePipelines( gd, sc, vertexLayouts, _sharedLayout, TextureLayout, factory, cullMode == CullBack, maskMode == MaskEnabled, additiveMode == AdditiveEnabled); } } } SharedResourceSet = factory.CreateResourceSet(new ResourceSetDescription( _sharedLayout, sc.ProjectionMatrixBuffer, sc.ViewMatrixBuffer, sc.WorldAndInverseBuffer, BonesBuffer, RenderArgumentsBuffer, TextureDataBuffer, sc.MainSampler, sc.LightingInfoBuffer )); }
public void Run() { m_factory = new DisposeCollectorResourceFactory(m_gd.ResourceFactory); GraphicsDeviceCreated?.Invoke(m_gd, m_factory, m_gd.MainSwapchain); Stopwatch sw = Stopwatch.StartNew(); double previousElapsed = sw.Elapsed.TotalSeconds; while (m_window.Exists) { double newElapsed = sw.Elapsed.TotalSeconds; float deltaSeconds = (float)(newElapsed - previousElapsed); InputSnapshot inputSnapshot = m_window.PumpEvents(); InputTracker.UpdateFrameInput(inputSnapshot); if (m_window.Exists) { previousElapsed = newElapsed; if (m_windowResized) { m_windowResized = false; m_gd.ResizeMainWindow((uint)m_window.Width, (uint)m_window.Height); Resized?.Invoke(); } Rendering?.Invoke(deltaSeconds); } } m_gd.WaitForIdle(); m_factory.DisposeCollector.DisposeAll(); m_gd.Dispose(); GraphicsDeviceDestroyed?.Invoke(); }
public void Run() { GraphicsDeviceOptions options = new GraphicsDeviceOptions( debug: false, swapchainDepthFormat: PixelFormat.R16_UNorm, syncToVerticalBlank: true, resourceBindingModel: ResourceBindingModel.Improved, preferDepthRangeZeroToOne: true, preferStandardClipSpaceYDirection: true); #if DEBUG options.Debug = true; #endif // construct a graphics device using the default backend. _gd = VeldridStartup.CreateGraphicsDevice(_window, options); // to determine what the default backend is you can run: //VeldridStartup.GetPlatformDefaultBackend() // to specify a specific backend use: //_gd = VeldridStartup.CreateGraphicsDevice(_window, options, GraphicsBackend.Metal); _factory = new DisposeCollectorResourceFactory(_gd.ResourceFactory); GraphicsDeviceCreated?.Invoke(_gd, _factory, _gd.MainSwapchain); Stopwatch sw = Stopwatch.StartNew(); double previousElapsed = sw.Elapsed.TotalSeconds; while (IsWindowOpen()) { double newElapsed = sw.Elapsed.TotalSeconds; float deltaSeconds = (float)(newElapsed - previousElapsed); InputSnapshot inputSnapshot = _window.PumpEvents(); InputTracker.UpdateFrameInput(inputSnapshot); if (_window.Exists) { previousElapsed = newElapsed; if (_windowResized) { _windowResized = false; _gd.ResizeMainWindow((uint)_window.Width, (uint)_window.Height); Resized?.Invoke(); } Rendering?.Invoke(deltaSeconds); } } Shutdown?.Invoke(); _gd.WaitForIdle(); _factory.DisposeCollector.DisposeAll(); _gd.Dispose(); GraphicsDeviceDestroyed?.Invoke(); }
public Canvas(GraphicsDevice device) { // May need to implement Disposable Pattern to force the system // to actually dispose of the resources instead of waiting on the GC _device = device; _resources = new DisposeCollectorResourceFactory(device.ResourceFactory); }
public void Run() { var options = new GraphicsDeviceOptions( false, PixelFormat.R16_UNorm, true, ResourceBindingModel.Improved, true, true); #if DEBUG options.Debug = true; #endif GraphicsDevice gd; if (_options.GraphicsBackend.HasValue) { gd = VeldridStartup.CreateGraphicsDevice(_window, options, _options.GraphicsBackend.Value); } else { gd = VeldridStartup.CreateGraphicsDevice(_window, options); } var factory = new DisposeCollectorResourceFactory(gd.ResourceFactory); _veldrid = new VeldridContext(gd, factory, gd.MainSwapchain); GraphicsDeviceCreated?.Invoke(_veldrid); var sw = Stopwatch.StartNew(); var previousElapsed = sw.Elapsed.TotalSeconds; while (_window.Exists) { var newElapsed = sw.Elapsed.TotalSeconds; var deltaSeconds = (float)(newElapsed - previousElapsed); var inputSnapshot = _window.PumpEvents(); if (_window.Exists) { previousElapsed = newElapsed; if (_windowResized) { _windowResized = false; gd.ResizeMainWindow((uint)_window.Width, (uint)_window.Height); Resized?.Invoke(); } Rendering?.Invoke(deltaSeconds); } } gd.WaitForIdle(); factory.DisposeCollector.DisposeAll(); gd.Dispose(); GraphicsDeviceDestroyed?.Invoke(); }
public GraphicsDeviceTestBase() { if (Environment.GetEnvironmentVariable("VELDRID_TESTS_ENABLE_RENDERDOC") == "1" && RenderDoc.Load(out _renderDoc)) { _renderDoc.DebugOutputMute = false; } Activator.CreateInstance<T>().CreateGraphicsDevice(out _window, out _gd); _factory = new DisposeCollectorResourceFactory(_gd.ResourceFactory); }
public SubRenderable(GraphicsDevice graphicsDevice, Resolution resolution) { _graphicsDevice = graphicsDevice; _resolution = resolution; _factory = new DisposeCollectorResourceFactory(_graphicsDevice.ResourceFactory); _sceneRuntimeDescriptor = new SceneRuntimeDescriptor(); _commandList = _factory.CreateCommandList(); GenerateFramebuffer(); }
public void Shutdown() { _gd.WaitForIdle(); _factory.DisposeCollector.DisposeAll(); _factory = null; _gd.Dispose(); _gd = null; _window.Close(); _window = null; }
public void CreateDeviceObjects(IRendererContext context) { var c = (VeldridRendererContext)context; var cl = c.CommandList; var gd = c.GraphicsDevice; var sc = c.SceneContext; DisposeCollectorResourceFactory factory = new DisposeCollectorResourceFactory(gd.ResourceFactory); _disposeCollector = factory.DisposeCollector; ResourceLayout resourceLayout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("SourceTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("SourceSampler", ResourceKind.Sampler, ShaderStages.Fragment))); var shaderCache = Resolve <IShaderCache>(); _shaders = shaderCache.GetShaderPair(gd.ResourceFactory, VertexShaderName, FragmentShaderName, shaderCache.GetGlsl(VertexShaderName), shaderCache.GetGlsl(FragmentShaderName)); GraphicsPipelineDescription pd = new GraphicsPipelineDescription( new BlendStateDescription( RgbaFloat.Black, BlendAttachmentDescription.OverrideBlend, BlendAttachmentDescription.OverrideBlend), gd.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyGreaterEqual : DepthStencilStateDescription.DepthOnlyLessEqual, RasterizerStateDescription.Default, PrimitiveTopology.TriangleList, new ShaderSetDescription( new[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2), new VertexElementDescription("TexCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2)) }, _shaders, ShaderHelper.GetSpecializations(gd)), new[] { resourceLayout }, sc.DuplicatorFramebuffer.OutputDescription); _pipeline = factory.CreateGraphicsPipeline(ref pd); _pipeline.Name = "P_ScreenDuplicator"; float[] verts = CoreUtil.GetFullScreenQuadVerts(gd.IsClipSpaceYInverted); _vb = factory.CreateBuffer(new BufferDescription(verts.SizeInBytes() * sizeof(float), BufferUsage.VertexBuffer)); cl.UpdateBuffer(_vb, 0, verts); _ib = factory.CreateBuffer( new BufferDescription((uint)QuadIndices.Length * sizeof(ushort), BufferUsage.IndexBuffer)); cl.UpdateBuffer(_ib, 0, QuadIndices); }
public void UpdateShaders(DisposeCollectorResourceFactory factory) { if (passes.All(x => x.IsCached)) { return; } foreach (var pass in passes) { pass.Update(factory, GetVertexDescription()); } }
private void CreateDevice() { var options = new GraphicsDeviceOptions(true, PixelFormat.R32_Float, true, ResourceBindingModel.Improved); var logicalDpi = 96.0f * CompositionScaleX; var renderWidth = RenderSize.Width; var renderHeight = RenderSize.Height; _graphicsDevice = GraphicsDevice.CreateD3D11(options, this, renderWidth, renderHeight, logicalDpi); _resources = new DisposeCollectorResourceFactory(_graphicsDevice.ResourceFactory); GraphicsDeviceCreated?.Invoke(_graphicsDevice, _resources, _graphicsDevice.MainSwapchain); }
public void Run() { var options = new GraphicsDeviceOptions( debug: false, swapchainDepthFormat: PixelFormat.R16_UNorm, syncToVerticalBlank: true, resourceBindingModel: ResourceBindingModel.Improved, preferDepthRangeZeroToOne: true, preferStandardClipSpaceYDirection: true); #if DEBUG options.Debug = true; #endif graphicsDevice = VeldridStartup.CreateGraphicsDevice(window, options); resourceFactory = new DisposeCollectorResourceFactory(graphicsDevice.ResourceFactory); this.DrawingContext = new DrawingContext(graphicsDevice, resourceFactory); DrawingContextCreated?.Invoke(DrawingContext); var sw = Stopwatch.StartNew(); var previousElapsed = sw.Elapsed.TotalSeconds; while (window.Exists) { double newElapsed = sw.Elapsed.TotalSeconds; float deltaSeconds = (float)(newElapsed - previousElapsed); this.InputSnapshot = window.PumpEvents(); //InputTracker.UpdateFrameInput(inputSnapshot); if (window.Exists) { previousElapsed = newElapsed; if (windowResized) { windowResized = false; graphicsDevice.ResizeMainWindow((uint)window.Width, (uint)window.Height); Resized?.Invoke(); } Rendering?.Invoke(deltaSeconds); } } graphicsDevice.WaitForIdle(); resourceFactory.DisposeCollector.DisposeAll(); graphicsDevice.Dispose(); DrawingContextDestroyed?.Invoke(); }
public override void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc, ResourceScope scope) { if ((scope & ResourceScope.Global) == 0) { return; } DisposeCollectorResourceFactory factory = new DisposeCollectorResourceFactory(gd.ResourceFactory); _disposeCollector = factory.DisposeCollector; ResourceLayout resourceLayout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("SourceTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("SourceSampler", ResourceKind.Sampler, ShaderStages.Fragment))); var shaderName = gd.IsUvOriginTopLeft ? "FinalPass_inverted" : "FinalPass"; (var vs, var fs) = sc.GlobalResourceCache.GetShaders(gd, gd.ResourceFactory, shaderName); GraphicsPipelineDescription pd = new GraphicsPipelineDescription( new BlendStateDescription( RgbaFloat.Black, BlendAttachmentDescription.OverrideBlend), gd.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyGreaterEqual : DepthStencilStateDescription.DepthOnlyLessEqual, RasterizerStateDescription.Default, PrimitiveTopology.TriangleList, new ShaderSetDescription( new[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2), new VertexElementDescription("TexCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2)) }, new[] { vs, fs, }), new ResourceLayout[] { resourceLayout }, gd.SwapchainFramebuffer.OutputDescription); _pipeline = factory.CreateGraphicsPipeline(ref pd); float[] verts = Util.GetFullScreenQuadVerts(gd); _vb = factory.CreateBuffer(new BufferDescription(verts.SizeInBytes() * sizeof(float), BufferUsage.VertexBuffer)); cl.UpdateBuffer(_vb, 0, verts); _ib = factory.CreateBuffer( new BufferDescription((uint)s_quadIndices.Length * sizeof(ushort), BufferUsage.IndexBuffer)); cl.UpdateBuffer(_ib, 0, s_quadIndices); }
public void Run() { GraphicsDeviceOptions options = new GraphicsDeviceOptions( debug: false, swapchainDepthFormat: PixelFormat.R16_UNorm, syncToVerticalBlank: true, resourceBindingModel: ResourceBindingModel.Improved, preferDepthRangeZeroToOne: true, preferStandardClipSpaceYDirection: true); #if DEBUG options.Debug = true; #endif GD = VeldridStartup.CreateGraphicsDevice(Window, options, GraphicsBackend.Direct3D11); Factory = new DisposeCollectorResourceFactory(GD.ResourceFactory); GraphicsDeviceCreated?.Invoke(GD, Factory, GD.MainSwapchain); Stopwatch sw = Stopwatch.StartNew(); double previousElapsed = sw.Elapsed.TotalSeconds; long ticks = 0; while (Window.Exists) { double newElapsed = sw.Elapsed.TotalSeconds; float deltaSeconds = (float)(newElapsed - previousElapsed); InputSnapshot inputSnapshot = Window.PumpEvents(); InputTracker.UpdateFrameInput(inputSnapshot); if (Window.Exists) { previousElapsed = newElapsed; if (WindowResized) { WindowResized = false; GD.ResizeMainWindow((uint)Window.Width, (uint)Window.Height); Resized?.Invoke(); } Rendering?.Invoke(deltaSeconds, ticks++); } } GD.WaitForIdle(); Factory.DisposeCollector.DisposeAll(); GD.Dispose(); GraphicsDeviceDestroyed?.Invoke(); }
public override void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc, ResourceScope scope) { if ((scope & ResourceScope.Map) == 0) { return; } var renderer = sc.Models.GetRenderer <StudioModelRenderer>(); var disposeFactory = new DisposeCollectorResourceFactory(gd.ResourceFactory, _disposeCollector); var vertices = new List <StudioVertex>(); var indices = new List <uint>(); //Construct the meshes for each body part var bodyParts = new List <BodyPartData>(StudioModel.StudioFile.BodyParts.Count); foreach (var bodyPart in StudioModel.StudioFile.BodyParts) { bodyParts.Add(StudioResourceUtils.CreateBodyPart(StudioModel.StudioFile, bodyPart, vertices, indices)); } BodyParts = bodyParts.ToArray(); var verticesArray = vertices.ToArray(); var indicesArray = indices.ToArray(); VertexBuffer = disposeFactory.CreateBuffer(new BufferDescription(verticesArray.SizeInBytes(), BufferUsage.VertexBuffer)); gd.UpdateBuffer(VertexBuffer, 0, verticesArray); IndexBuffer = disposeFactory.CreateBuffer(new BufferDescription(indicesArray.SizeInBytes(), BufferUsage.IndexBuffer)); gd.UpdateBuffer(IndexBuffer, 0, indicesArray); var uploadedTextures = StudioResourceUtils.CreateTextures(StudioModel.Name, StudioModel.StudioFile, gd, sc.TextureLoader, sc.MapResourceCache); Textures = new ResourceSet[uploadedTextures.Count]; for (var i = 0; i < uploadedTextures.Count; ++i) { var view = sc.MapResourceCache.GetTextureView(gd.ResourceFactory, uploadedTextures[i]); Textures[i] = disposeFactory.CreateResourceSet(new ResourceSetDescription(renderer.TextureLayout, view)); } }
public void Run() { GraphicsDeviceOptions options = new GraphicsDeviceOptions( debug: false, swapchainDepthFormat: PixelFormat.R16_UNorm, syncToVerticalBlank: true, resourceBindingModel: ResourceBindingModel.Improved); #if DEBUG options.Debug = true; #endif _gd = VeldridStartup.CreateGraphicsDevice(_window, options); _factory = new DisposeCollectorResourceFactory(_gd.ResourceFactory); GraphicsDeviceCreated?.Invoke(_gd, _factory, _gd.MainSwapchain); Stopwatch sw = Stopwatch.StartNew(); double previousElapsed = sw.Elapsed.TotalSeconds; while (_window.Exists) { double newElapsed = sw.Elapsed.TotalSeconds; float deltaSeconds = (float)(newElapsed - previousElapsed); InputSnapshot inputSnapshot = _window.PumpEvents(); InputTracker.UpdateFrameInput(inputSnapshot); if (_window.Exists) { previousElapsed = newElapsed; if (_windowResized) { _windowResized = false; _gd.ResizeMainWindow((uint)_window.Width, (uint)_window.Height); Resized?.Invoke(); } Rendering?.Invoke(deltaSeconds); } } _gd.WaitForIdle(); _factory.DisposeCollector.DisposeAll(); _gd.Dispose(); GraphicsDeviceDestroyed?.Invoke(); }
public void CreateDeviceObjects(IRendererContext context) { var c = (VeldridRendererContext)context; var cl = c.CommandList; var gd = c.GraphicsDevice; var factory = new DisposeCollectorResourceFactory(gd.ResourceFactory); _disposeCollector = factory.DisposeCollector; var layout = factory.CreateResourceLayout(new ResourceLayoutDescription( ResourceLayoutHelper.Texture("SourceTexture"), ResourceLayoutHelper.Sampler("SourceSampler"))); var shaderCache = Resolve <IShaderCache>(); _shaders = shaderCache.GetShaderPair(gd.ResourceFactory, VertexShaderName, FragmentShaderName, shaderCache.GetGlsl(VertexShaderName), shaderCache.GetGlsl(FragmentShaderName)); var rasterizerState = new RasterizerStateDescription( FaceCullMode.Back, PolygonFillMode.Solid, FrontFace.Clockwise, true, false); var pd = new GraphicsPipelineDescription( new BlendStateDescription(RgbaFloat.Black, BlendAttachmentDescription.OverrideBlend), DepthStencilStateDescription.Disabled, rasterizerState, PrimitiveTopology.TriangleList, new ShaderSetDescription(new[] { VertexLayoutHelper.Vertex2DTextured }, _shaders, ShaderHelper.GetSpecializations(gd)), new[] { layout }, gd.SwapchainFramebuffer.OutputDescription); _pipeline = factory.CreateGraphicsPipeline(ref pd); _pipeline.Name = "P_FullScreenQuad"; float[] verts = CoreUtil.GetFullScreenQuadVerts(gd.IsClipSpaceYInverted); _vb = factory.CreateBuffer(new BufferDescription(verts.SizeInBytes() * sizeof(float), BufferUsage.VertexBuffer)); cl.UpdateBuffer(_vb, 0, verts); _ib = factory.CreateBuffer(new BufferDescription(QuadIndices.SizeInBytes(), BufferUsage.IndexBuffer)); cl.UpdateBuffer(_ib, 0, QuadIndices); }
public Renderable(string title, Resolution windowSize, GraphicsDeviceOptions graphicsDeviceOptions, RenderOptions renderOptions) { WindowCreateInfo windowCI = new WindowCreateInfo() { X = 100, Y = 100, WindowWidth = windowSize.Horizontal, WindowHeight = windowSize.Vertical, WindowTitle = title }; _contextWindow = VeldridStartup.CreateWindow(ref windowCI); if (renderOptions.UsePreferredGraphicsBackend) { _graphicsDevice = VeldridStartup.CreateGraphicsDevice(_contextWindow, graphicsDeviceOptions, renderOptions.PreferredGraphicsBackend); } else { _graphicsDevice = VeldridStartup.CreateGraphicsDevice(_contextWindow, graphicsDeviceOptions); } _renderOptions = renderOptions; _contextWindow.Title = $"{title} / {_graphicsDevice.BackendType.ToString()}"; _factory = new DisposeCollectorResourceFactory(_graphicsDevice.ResourceFactory); _commandList = _factory.CreateCommandList(); _sceneRuntimeState = new SceneRuntimeDescriptor(); _modelPNTTBDescriptorList = new List <ModelRuntimeDescriptor <VertexPositionNormalTextureTangentBitangent> >(); _modelPNDescriptorList = new List <ModelRuntimeDescriptor <VertexPositionNormal> >(); _modelPTDescriptorList = new List <ModelRuntimeDescriptor <VertexPositionTexture> >(); _modelPCDescriptorList = new List <ModelRuntimeDescriptor <VertexPositionColor> >(); _modelPDescriptorList = new List <ModelRuntimeDescriptor <VertexPosition> >(); PNTTBRuntimeGeometry = new GeometryDescriptor <VertexPositionNormalTextureTangentBitangent>(); PNRuntimeGeometry = new GeometryDescriptor <VertexPositionNormal>(); PTRuntimeGeometry = new GeometryDescriptor <VertexPositionTexture>(); PCRuntimeGeometry = new GeometryDescriptor <VertexPositionColor>(); PRuntimeGeometry = new GeometryDescriptor <VertexPosition>(); // Tick every millisecond _frameTimer = new FrameTimer(1.0); _running = false; }
public void Run() { DisposeCollectorResourceFactory factory = new DisposeCollectorResourceFactory(_gd.ResourceFactory); CreateResources(factory); while (_window.Exists) { _window.PumpEvents(); if (_window.Exists) { Draw(); } } factory.DisposeCollector.DisposeAll(); }
public override void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc) { DisposeCollectorResourceFactory factory = new DisposeCollectorResourceFactory(gd.ResourceFactory); _disposeCollector = factory.DisposeCollector; ResourceLayout resourceLayout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("SourceTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("SourceSampler", ResourceKind.Sampler, ShaderStages.Fragment))); (Shader vs, Shader fs) = StaticResourceCache.GetShaders(gd, gd.ResourceFactory, "ScreenDuplicator"); GraphicsPipelineDescription pd = new GraphicsPipelineDescription( new BlendStateDescription( RgbaFloat.Black, BlendAttachmentDescription.OverrideBlend, BlendAttachmentDescription.OverrideBlend), gd.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyGreaterEqual : DepthStencilStateDescription.DepthOnlyLessEqual, RasterizerStateDescription.Default, PrimitiveTopology.TriangleList, new ShaderSetDescription( new[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2), new VertexElementDescription("TexCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2)) }, new[] { vs, fs, }, ShaderHelper.GetSpecializations(gd)), new ResourceLayout[] { resourceLayout }, sc.DuplicatorFramebuffer.OutputDescription); _pipeline = factory.CreateGraphicsPipeline(ref pd); float[] verts = Util.GetFullScreenQuadVerts(gd); _vb = factory.CreateBuffer(new BufferDescription(verts.SizeInBytes() * sizeof(float), BufferUsage.VertexBuffer)); cl.UpdateBuffer(_vb, 0, verts); _ib = factory.CreateBuffer( new BufferDescription((uint)s_quadIndices.Length * sizeof(ushort), BufferUsage.IndexBuffer)); cl.UpdateBuffer(_ib, 0, s_quadIndices); }
public override void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc) { DisposeCollectorResourceFactory factory = new DisposeCollectorResourceFactory(gd.ResourceFactory); _disposeCollector = factory.DisposeCollector; ResourceLayout resourceLayout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("SourceTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("SourceSampler", ResourceKind.Sampler, ShaderStages.Fragment))); GraphicsPipelineDescription pd = new GraphicsPipelineDescription( new BlendStateDescription( RgbaFloat.Black, BlendAttachmentDescription.OverrideBlend, BlendAttachmentDescription.OverrideBlend), DepthStencilStateDescription.DepthOnlyLessEqual, RasterizerStateDescription.Default, PrimitiveTopology.TriangleList, new ShaderSetDescription( new[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float2), new VertexElementDescription("TexCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2)) }, new[] { ShaderHelper.LoadShader(gd, factory, "ScreenDuplicator", ShaderStages.Vertex, "VS"), ShaderHelper.LoadShader(gd, factory, "ScreenDuplicator", ShaderStages.Fragment, "FS"), }), new ResourceLayout[] { resourceLayout }, sc.DuplicatorFramebuffer.OutputDescription); _pipeline = factory.CreateGraphicsPipeline(ref pd); _vb = factory.CreateBuffer(new BufferDescription((uint)s_quadVerts.Length * sizeof(float), BufferUsage.VertexBuffer)); cl.UpdateBuffer(_vb, 0, s_quadVerts); _ib = factory.CreateBuffer( new BufferDescription((uint)s_quadIndices.Length * sizeof(ushort), BufferUsage.IndexBuffer)); cl.UpdateBuffer(_ib, 0, s_quadIndices); }
public void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, GraphicsSystem sc) { DisposeCollectorResourceFactory factory = new DisposeCollectorResourceFactory(gd.ResourceFactory); _disposeCollector = factory.DisposeCollector; ResourceLayout resourceLayout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("SourceTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("SourceSampler", ResourceKind.Sampler, ShaderStages.Fragment))); (Shader vs, Shader fs) = StaticResourceCache.GetShaders(gd, gd.ResourceFactory, "FullScreenQuad"); GraphicsPipelineDescription pd = new GraphicsPipelineDescription( new BlendStateDescription( RgbaFloat.Black, BlendAttachmentDescription.OverrideBlend), DepthStencilStateDescription.Disabled, new RasterizerStateDescription(FaceCullMode.Back, PolygonFillMode.Solid, FrontFace.Clockwise, true, false), PrimitiveTopology.TriangleList, new ShaderSetDescription( new[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2), new VertexElementDescription("TexCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2)) }, new[] { vs, fs }), new ResourceLayout[] { resourceLayout }, gd.SwapchainFramebuffer.OutputDescription); _pipeline = factory.CreateGraphicsPipeline(ref pd); float[] verts = Util.GetFullScreenQuadVerts(gd.BackendType); _vb = factory.CreateBuffer(new BufferDescription(verts.SizeInBytes() * sizeof(float), BufferUsage.VertexBuffer)); cl.UpdateBuffer(_vb, 0, verts); _ib = factory.CreateBuffer( new BufferDescription(s_quadIndices.SizeInBytes(), BufferUsage.IndexBuffer)); cl.UpdateBuffer(_ib, 0, s_quadIndices); }
public void CreateDeviceObjects(GraphicsDevice gd, CommandList cl) { DisposeCollectorResourceFactory factory = new DisposeCollectorResourceFactory(gd.ResourceFactory); _disposeCollector = factory.DisposeCollector; //ResourceLayout resourceLayout = factory.CreateResourceLayout(new ResourceLayoutDescription()); (Shader vs, Shader fs) = StaticResourceCache.GetShaders(gd, gd.ResourceFactory, "FullScreenQuad"); GraphicsPipelineDescription pd = new GraphicsPipelineDescription( new BlendStateDescription( RgbaFloat.Black, BlendAttachmentDescription.OverrideBlend), new DepthStencilStateDescription(true, true, ComparisonKind.Always), new RasterizerStateDescription(FaceCullMode.Back, PolygonFillMode.Solid, FrontFace.Clockwise, true, false), PrimitiveTopology.TriangleList, new ShaderSetDescription( new[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2), new VertexElementDescription("TexCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2)) }, new[] { vs, fs }, ShaderHelper.GetSpecializations(gd)), new ResourceLayout[] { }, gd.SwapchainFramebuffer.OutputDescription); _pipeline = factory.CreateGraphicsPipeline(ref pd); float[] verts = Utils.GetFullScreenQuadVerts(gd); _vb = factory.CreateBuffer(new BufferDescription((uint)verts.Length * sizeof(float), BufferUsage.VertexBuffer)); cl.UpdateBuffer(_vb, 0, verts); _ib = factory.CreateBuffer( new BufferDescription((uint)s_quadIndices.Length * sizeof(ushort), BufferUsage.IndexBuffer)); cl.UpdateBuffer(_ib, 0, s_quadIndices); }
public ResourceLayout[] FillEffectsResourceSet(DisposeCollectorResourceFactory factory, SceneRuntimeDescriptor sceneRuntimeDescriptor, List <SubRenderable> childrenPre) { var effectLayoutList = new List <ResourceLayout>(); //TODO: just iterate over children and process them as Subrenderable !! if (ShadowMapEnabled) { var effectCount = RenderFlags.GetSizeOfPreEffectFlag(PreEffectsFlag) * 2; // 1 for Vertex Stage 1 for Fragment EffectResourceSets[RenderFlags.NORMAL_ARRAY_INDEX] = new ResourceSet[effectCount]; var shadowMapRenderable = childrenPre[0] as ShadowMap; var shadowMapResourceLayout = ResourceGenerator.GenerateTextureResourceLayoutForShadowMapping(factory); effectLayoutList.Add(sceneRuntimeDescriptor.LightProvViewResourceLayout); effectLayoutList.Add(shadowMapResourceLayout); EffectResourceSets[RenderFlags.NORMAL_ARRAY_INDEX][RenderFlags.GetPreEffectArrayIndexForFlag(RenderFlags.SHADOW_MAP)] = sceneRuntimeDescriptor.LightProjViewResourceSet; EffectResourceSets[RenderFlags.NORMAL_ARRAY_INDEX][RenderFlags.GetPreEffectArrayIndexForFlag(RenderFlags.SHADOW_MAP) + 1] = ResourceGenerator.GenerateResourceSetForShadowMapping(shadowMapResourceLayout, shadowMapRenderable.ShadowMapTexView, factory); } if (OmniShadowMapEnabled) { var effectCount = 2; EffectResourceSets[RenderFlags.NORMAL_ARRAY_INDEX] = new ResourceSet[effectCount]; EffectResourceSets[RenderFlags.OMNI_SHADOW_MAPS] = new ResourceSet[2]; //var omniShadowMapRenderable = childrenPre[RenderFlags.GetPreEffectArrayIndexForFlag(RenderFlags.OMNI_SHADOW_MAPS)] as OmniShadowMap; var omniShadowMapRenderable = childrenPre[0] as OmniShadowMap; var shadowMapResourceLayout = ResourceGenerator.GenerateTextureResourceLayoutForOmniShadowMapping(factory); //TODO: this has to be an array effectLayoutList.Add(sceneRuntimeDescriptor.CameraInfoResourceLayout); effectLayoutList.Add(shadowMapResourceLayout); EffectResourceSets[RenderFlags.NORMAL_ARRAY_INDEX][0] = sceneRuntimeDescriptor.CameraInfoResourceSet; EffectResourceSets[RenderFlags.NORMAL_ARRAY_INDEX][1] = ResourceGenerator.GenerateResourceSetForShadowMapping(shadowMapResourceLayout, omniShadowMapRenderable.ShadowMapTexView, factory); EffectResourceSets[RenderFlags.OMNI_SHADOW_MAPS][0] = omniShadowMapRenderable.ShadowMatrices; EffectResourceSets[RenderFlags.OMNI_SHADOW_MAPS][1] = omniShadowMapRenderable.CameraInfo; } return(effectLayoutList.ToArray()); }
public void CreateDeviceObjects(GraphicsDevice gd, CommandList cl) { if (gd == null) { throw new ArgumentNullException(nameof(gd)); } if (cl == null) { throw new ArgumentNullException(nameof(cl)); } var factory = new DisposeCollectorResourceFactory(gd.ResourceFactory, _disposer); DeviceBuffer MakeBuffer(uint size, string name) { var buffer = factory.CreateBuffer( new BufferDescription(size, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); buffer.Name = name; return(buffer); } ProjectionMatrixBuffer = MakeBuffer(64, "M_Projection"); ModelViewMatrixBuffer = MakeBuffer(64, "M_View"); IdentityMatrixBuffer = MakeBuffer(64, "M_Id"); DepthLimitsBuffer = MakeBuffer((uint)Unsafe.SizeOf <DepthCascadeLimits>(), "B_DepthLimits"); CameraInfoBuffer = MakeBuffer((uint)Unsafe.SizeOf <CameraInfo>(), "B_CameraInfo"); cl.UpdateBuffer(IdentityMatrixBuffer, 0, Matrix4x4.Identity); var commonLayoutDescription = new ResourceLayoutDescription( ResourceLayoutHelper.Uniform("_Shared"), // CameraInfo / common data buffer ResourceLayoutHelper.UniformV("_Projection"), // Perspective Matrix ResourceLayoutHelper.UniformV("_View"), // View Matrix ResourceLayoutHelper.Texture("uPalette")); // PaletteTexture CommonResourceLayout = factory.CreateResourceLayout(commonLayoutDescription); CommonResourceLayout.Name = "RL_Common"; }
public void Start() { WindowCreateInfo wci = new WindowCreateInfo { X = 100, Y = 100, WindowWidth = 1280, WindowHeight = 720, WindowTitle = _runner.Name, }; _window = VeldridStartup.CreateWindow(ref wci); _window.Resized += () => { _windowResized = true; }; _ft = _runner.GetSubSystem <FrameTimer>(); _loader = _runner.GetSubSystem <ResourceLoader>(); GraphicsDeviceOptions options = new GraphicsDeviceOptions( debug: false, swapchainDepthFormat: PixelFormat.R16_UNorm, syncToVerticalBlank: true, resourceBindingModel: ResourceBindingModel.Improved); #if DEBUG options.Debug = true; #endif _gd = VeldridStartup.CreateGraphicsDevice(_window, options); _factory = new DisposeCollectorResourceFactory(_gd.ResourceFactory); ResourceLayout projViewLayout = _factory.CreateResourceLayout( new ResourceLayoutDescription( new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex))); ResourceLayout worldTextureLayout = _factory.CreateResourceLayout( new ResourceLayoutDescription( new ResourceLayoutElementDescription("World", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("SurfaceTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("SurfaceSampler", ResourceKind.Sampler, ShaderStages.Fragment))); ShaderSetDescription shaderSet = new ShaderSetDescription( new[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3), new VertexElementDescription("TexCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2)) }, new[] { _loader.LoadShader(_factory, "Cube", ShaderStages.Vertex, "VS"), _loader.LoadShader(_factory, "Cube", ShaderStages.Fragment, "FS") }); _pipeline = _factory.CreateGraphicsPipeline(new GraphicsPipelineDescription( BlendStateDescription.SingleOverrideBlend, DepthStencilStateDescription.DepthOnlyLessEqual, RasterizerStateDescription.Default, PrimitiveTopology.TriangleList, shaderSet, new[] { projViewLayout, worldTextureLayout }, MainSwapchain.Framebuffer.OutputDescription)); _cl = _factory.CreateCommandList(); LastInput = _window.PumpEvents(); }