public static void Main(string[] args) { WindowCreateInfo windowCI = new WindowCreateInfo() { X = 100, Y = 100, WindowWidth = 960, WindowHeight = 540, WindowTitle = "Veldrid TinyDemo", }; RenderContextCreateInfo contextCI = new RenderContextCreateInfo(); VeldridStartup.CreateWindowAndRenderContext(ref windowCI, ref contextCI, out var window, out RenderContext rc); VertexBuffer vb = rc.ResourceFactory.CreateVertexBuffer(Cube.Vertices, new VertexDescriptor(VertexPositionColor.SizeInBytes, 2), false); IndexBuffer ib = rc.ResourceFactory.CreateIndexBuffer(Cube.Indices, false); string folder = rc.BackendType == GraphicsBackend.Direct3D11 ? "HLSL" : "GLSL"; string extension = rc.BackendType == GraphicsBackend.Direct3D11 ? "hlsl" : "glsl"; VertexInputLayout inputLayout = rc.ResourceFactory.CreateInputLayout(new VertexInputDescription[] { new VertexInputDescription( new VertexInputElement("Position", VertexSemanticType.Position, VertexElementFormat.Float3), new VertexInputElement("Color", VertexSemanticType.Color, VertexElementFormat.Float4)) }); string vsPath = Path.Combine(AppContext.BaseDirectory, folder, $"vertex.{extension}"); string fsPath = Path.Combine(AppContext.BaseDirectory, folder, $"fragment.{extension}"); Shader vs = rc.ResourceFactory.CreateShader(ShaderStages.Vertex, File.ReadAllText(vsPath)); Shader fs = rc.ResourceFactory.CreateShader(ShaderStages.Fragment, File.ReadAllText(fsPath)); ShaderSet shaderSet = rc.ResourceFactory.CreateShaderSet(inputLayout, vs, fs); ShaderResourceBindingSlots bindingSlots = rc.ResourceFactory.CreateShaderResourceBindingSlots( shaderSet, new ShaderResourceDescription("ViewProjectionMatrix", ShaderConstantType.Matrix4x4)); ConstantBuffer viewProjectionBuffer = rc.ResourceFactory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); while (window.Exists) { InputSnapshot snapshot = window.PumpEvents(); rc.ClearBuffer(); rc.SetViewport(0, 0, window.Width, window.Height); float timeFactor = Environment.TickCount / 1000f; viewProjectionBuffer.SetData( Matrix4x4.CreateLookAt( new Vector3(2 * (float)Math.Sin(timeFactor), (float)Math.Sin(timeFactor), 2 * (float)Math.Cos(timeFactor)), Vector3.Zero, Vector3.UnitY) * Matrix4x4.CreatePerspectiveFieldOfView(1.05f, (float)window.Width / window.Height, .5f, 10f)); rc.SetVertexBuffer(0, vb); rc.IndexBuffer = ib; rc.ShaderSet = shaderSet; rc.ShaderResourceBindingSlots = bindingSlots; rc.SetConstantBuffer(0, viewProjectionBuffer); rc.DrawIndexedPrimitives(Cube.Indices.Length); rc.SwapBuffers(); } }
private void ChangeRenderContext(GraphicsBackend backend) { _sc.DestroyDeviceObjects(); _scene.DestroyAllDeviceObjects(); CommonMaterials.DestroyAllDeviceObjects(); _rc.Dispose(); if (_recreateWindow) { WindowCreateInfo windowCI = new WindowCreateInfo { X = _window.X, Y = _window.Y, WindowWidth = _window.Width, WindowHeight = _window.Height, WindowInitialState = _window.WindowState, WindowTitle = "Veldrid NeoDemo" }; _window.Close(); _window = VeldridStartup.CreateWindow(ref windowCI); _window.Resized += () => _windowResized = true; } RenderContextCreateInfo rcCI = new RenderContextCreateInfo { Backend = backend }; _rc = VeldridStartup.CreateRenderContext(ref rcCI, _window); _sc.CreateDeviceObjects(_rc); _scene.CreateAllDeviceObjects(_rc); CommonMaterials.CreateAllDeviceObjects(_rc); }
public NeoDemo() { WindowCreateInfo windowCI = new WindowCreateInfo { X = 50, Y = 50, WindowWidth = 960, WindowHeight = 540, WindowInitialState = WindowState.Normal, WindowTitle = "Veldrid NeoDemo" }; RenderContextCreateInfo rcCI = new RenderContextCreateInfo(); rcCI.DebugContext = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); VeldridStartup.CreateWindowAndRenderContext(ref windowCI, ref rcCI, out _window, out _rc); _window.Resized += () => _windowResized = true; _sc.CreateDeviceObjects(_rc); CommonMaterials.CreateAllDeviceObjects(_rc); _scene = new Scene(_window.Width, _window.Height); _sc.SetCurrentScene(_scene); _igRenderable = new ImGuiRenderable(_window.Width, _window.Height); _resizeHandled += (w, h) => _igRenderable.WindowResized(w, h); _igRenderable.CreateDeviceObjects(_rc); _scene.AddRenderable(_igRenderable); _scene.AddUpdateable(_igRenderable); InfiniteGrid grid = new InfiniteGrid(); grid.CreateDeviceObjects(_rc); _scene.AddRenderable(grid); Skybox skybox = Skybox.LoadDefaultSkybox(); skybox.CreateDeviceObjects(_rc); _scene.AddRenderable(skybox); AddTexturedMesh( "Textures/spnza_bricks_a_diff.png", PrimitiveShapes.Box(10, 10, 10, 10), new Vector3(0, 0, -5), Quaternion.Identity, Vector3.One); AddTexturedMesh( "Textures/spnza_bricks_a_diff.png", PrimitiveShapes.Box(5, 5, 5, 5f), new Vector3(-3, -9, 2), Quaternion.Identity, Vector3.One); AddTexturedMesh( "Textures/spnza_bricks_a_diff.png", PrimitiveShapes.Box(27, 3, 27, 27f), new Vector3(-5, -16, 5), Quaternion.Identity, Vector3.One); AddTexturedMesh( "Textures/spnza_bricks_a_diff.png", PrimitiveShapes.Plane(100, 100, 5), new Vector3(0, -20, 0), Quaternion.Identity, Vector3.One); ShadowmapDrawer texDrawer = new ShadowmapDrawer(_window, () => _sc.NearShadowMapBinding); texDrawer.CreateDeviceObjects(_rc); texDrawer.Position = new Vector2(10, 25); _scene.AddRenderable(texDrawer); texDrawer = new ShadowmapDrawer(_window, () => _sc.MidShadowMapBinding); texDrawer.CreateDeviceObjects(_rc); texDrawer.Position = new Vector2(20 + texDrawer.Size.X, 25); _scene.AddRenderable(texDrawer); texDrawer = new ShadowmapDrawer(_window, () => _sc.FarShadowMapBinding); texDrawer.CreateDeviceObjects(_rc); texDrawer.Position = new Vector2(30 + (texDrawer.Size.X * 2), 25); _scene.AddRenderable(texDrawer); }