Ejemplo n.º 1
0
        public GraphicsState(WindowState window)
        {
            Window    = window;
            Stopwatch = window.Stopwatch;

            var syswm = new SDL_SysWMinfo();

            if (SDL_GetWindowWMInfo(window.Handle, ref syswm) == SDL_bool.SDL_FALSE)
            {
                throw new SdlException($"Could not get window wminfo: {SDL_GetError()}");
            }

            SDL_GetWindowSize(window.Handle, out var width, out var height);
            Device = GraphicsDevice.CreateVulkan(
                new GraphicsDeviceOptions(true, null, false, ResourceBindingModel.Default,
                                          true, false, false),
                VkSurfaceSource.CreateWin32(syswm.info.win.hinstance, syswm.info.win.window),
                (uint)width, (uint)height);
            Log.Debug("main swapchain color format is {format}",
                      Device.MainSwapchain.Framebuffer.ColorTargets[0].Target.Format);

            SingleRectIndexBuffer = new ResourcelessArrayBuffer <uint>(this, BufferUsage.IndexBuffer,
                                                                       new uint[] { 0, 2, 3, 0, 3, 1 }.ToImmutableArray());

            RandomnessTexture = new RandomnessTexture(this);
        }
Ejemplo n.º 2
0
        private static unsafe RenderContext CreateVulkanRenderContext(Sdl2Window window)
        {
            IntPtr        sdlHandle = window.SdlWindowHandle;
            SDL_SysWMinfo sysWmInfo;

            Sdl2Native.SDL_GetVersion(&sysWmInfo.version);
            Sdl2Native.SDL_GetWMWindowInfo(sdlHandle, &sysWmInfo);
            VkSurfaceSource surfaceSource = GetSurfaceSource(sysWmInfo);

            return(new VkRenderContext(surfaceSource, window.Width, window.Height));
        }
Ejemplo n.º 3
0
        private static unsafe RenderContext CreateVulkanRenderContext(Sdl2Window window)
        {
            IntPtr        sdlHandle = window.SdlWindowHandle;
            SDL_SysWMinfo sysWmInfo;

            Sdl2Native.SDL_GetVersion(&sysWmInfo.version);
            Sdl2Native.SDL_GetWMWindowInfo(sdlHandle, &sysWmInfo);
            Win32WindowInfo w32Info = Unsafe.Read <Win32WindowInfo>(&sysWmInfo.info);

            return(new VkRenderContext(VkSurfaceSource.CreateWin32(w32Info.hinstance, w32Info.window), window.Width, window.Height));
        }
Ejemplo n.º 4
0
        public static unsafe GraphicsDevice CreateVulkanGraphicsDevice(ref GraphicsDeviceCreateInfo contextCI, Sdl2Window window)
        {
            IntPtr        sdlHandle = window.SdlWindowHandle;
            SDL_SysWMinfo sysWmInfo;

            Sdl2Native.SDL_GetVersion(&sysWmInfo.version);
            Sdl2Native.SDL_GetWMWindowInfo(sdlHandle, &sysWmInfo);
            VkSurfaceSource surfaceSource = GetSurfaceSource(sysWmInfo);
            GraphicsDevice  gd            = Hacks.CreateVulkan(surfaceSource, (uint)window.Width, (uint)window.Height, contextCI.DebugDevice);

            return(gd);
        }
Ejemplo n.º 5
0
        public static unsafe GraphicsDevice CreateVulkanGraphicsDevice(GraphicsDeviceOptions options, Sdl2Window window)
        {
            IntPtr        sdlHandle = window.SdlWindowHandle;
            SDL_SysWMinfo sysWmInfo;

            Sdl2Native.SDL_GetVersion(&sysWmInfo.version);
            Sdl2Native.SDL_GetWMWindowInfo(sdlHandle, &sysWmInfo);
            VkSurfaceSource surfaceSource = GetSurfaceSource(sysWmInfo);
            GraphicsDevice  gd            = GraphicsDevice.CreateVulkan(options, surfaceSource, (uint)window.Width, (uint)window.Height);

            return(gd);
        }
Ejemplo n.º 6
0
        public static unsafe RenderContext CreateVulkanRenderContext(ref RenderContextCreateInfo contextCI, Sdl2Window window)
        {
            IntPtr        sdlHandle = window.SdlWindowHandle;
            SDL_SysWMinfo sysWmInfo;

            Sdl2Native.SDL_GetVersion(&sysWmInfo.version);
            Sdl2Native.SDL_GetWMWindowInfo(sdlHandle, &sysWmInfo);
            VkSurfaceSource surfaceSource = GetSurfaceSource(sysWmInfo);
            VkRenderContext rc            = new VkRenderContext(surfaceSource, window.Width, window.Height);

            if (contextCI.DebugContext)
            {
                rc.EnableDebugCallback();
            }

            return(rc);
        }
Ejemplo n.º 7
0
        private static unsafe VkSurfaceSource GetSurfaceSource(SDL_SysWMinfo sysWmInfo)
        {
            switch (sysWmInfo.subsystem)
            {
            case SysWMType.Windows:
                Win32WindowInfo w32Info = Unsafe.Read <Win32WindowInfo>(&sysWmInfo.info);
                return(VkSurfaceSource.CreateWin32(w32Info.hinstance, w32Info.Sdl2Window));

            case SysWMType.X11:
                X11WindowInfo x11Info = Unsafe.Read <X11WindowInfo>(&sysWmInfo.info);
                return(VkSurfaceSource.CreateXlib(
                           (Vulkan.Xlib.Display *)x11Info.display,
                           new Vulkan.Xlib.Window()
                {
                    Value = x11Info.Sdl2Window
                }));

            default:
                throw new PlatformNotSupportedException("Cannot create a Vulkan surface for " + sysWmInfo.subsystem + ".");
            }
        }
Ejemplo n.º 8
0
 public static GraphicsDevice CreateVulkan(VkSurfaceSource surfaceSource, uint width, uint height, bool debugDevice)
 {
     return(new VkGraphicsDevice(surfaceSource, width, height, debugDevice));
 }
Ejemplo n.º 9
0
        private void InitVeldrid()
        {
            var options = new GraphicsDeviceOptions
            {
#if DEBUG
                Debug = true,
#endif
                HasMainSwapchain    = true,
                SyncToVerticalBlank = _vsync,
                PreferStandardClipSpaceYDirection = true,
                SwapchainSrgbFormat = true
            };

            GLFW.GetFramebufferSize(_window.WindowPtr, out var w, out var h);

            var hwnd      = GLFW.GetWin32Window(_window.WindowPtr);
            var hinstance = GetModuleHandleA(null);

            switch (_vdRenderer)
            {
            case VeldridRenderer.Vulkan:
                _vdGfxDevice = GraphicsDevice.CreateVulkan(
                    options,
                    VkSurfaceSource.CreateWin32((nint)hinstance, hwnd),
                    (uint)w, (uint)h);
                break;

            case VeldridRenderer.D3D11:
                _vdGfxDevice = GraphicsDevice.CreateD3D11(options, hwnd, (uint)w, (uint)h);
                break;

            case VeldridRenderer.OpenGL:
            {
                var platInfo = new OpenGLPlatformInfo(
                    (nint)_window.WindowPtr,
                    GLFW.GetProcAddress,
                    ptr => GLFW.MakeContextCurrent((Window *)ptr),
                    () => (nint)GLFW.GetCurrentContext(),
                    () => GLFW.MakeContextCurrent(null),
                    ptr => GLFW.DestroyWindow((Window *)ptr),
                    () => GLFW.SwapBuffers(_window.WindowPtr),
                    vsync => GLFW.SwapInterval(vsync ? 1 : 0));

                _vdGfxDevice = GraphicsDevice.CreateOpenGL(options, platInfo, (uint)w, (uint)h);
                break;
            }
            }


            var factory = _vdGfxDevice.ResourceFactory;

            _vdCommandList      = factory.CreateCommandList();
            _vdCommandList.Name = "Honk";

            var vtxLayout = new VertexLayoutDescription(
                new VertexElementDescription("Position", VertexElementFormat.Float2,
                                             VertexElementSemantic.TextureCoordinate),
                new VertexElementDescription("UV", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate),
                new VertexElementDescription("Color", VertexElementFormat.Byte4_Norm,
                                             VertexElementSemantic.TextureCoordinate));

            var vtxShaderDesc = new ShaderDescription(
                ShaderStages.Vertex,
                Encoding.UTF8.GetBytes(VDVertexShader),
                "main");

            var fragShaderDesc = new ShaderDescription(
                ShaderStages.Fragment,
                Encoding.UTF8.GetBytes(VDFragmentShader),
                "main");

            _vdShaders = factory.CreateFromSpirv(vtxShaderDesc, fragShaderDesc);

            _vdShaders[0].Name = "VertexShader";
            _vdShaders[1].Name = "FragmentShader";

            var layoutTexture = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                                 new ResourceLayoutElementDescription(
                                                                     "Texture",
                                                                     ResourceKind.TextureReadOnly,
                                                                     ShaderStages.Fragment),
                                                                 new ResourceLayoutElementDescription(
                                                                     "TextureSampler",
                                                                     ResourceKind.Sampler,
                                                                     ShaderStages.Fragment)));

            layoutTexture.Name = "LayoutTexture";

            var layoutProjMatrix = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                                    new ResourceLayoutElementDescription(
                                                                        "ProjMtx",
                                                                        ResourceKind.UniformBuffer,
                                                                        ShaderStages.Vertex)));

            layoutProjMatrix.Name = "LayoutProjMatrix";

            var pipelineDesc = new GraphicsPipelineDescription(
                new BlendStateDescription(
                    RgbaFloat.White,
                    new BlendAttachmentDescription(
                        true,
                        BlendFactor.SourceAlpha,
                        BlendFactor.InverseSourceAlpha,
                        BlendFunction.Add,
                        BlendFactor.One,
                        BlendFactor.InverseSourceAlpha,
                        BlendFunction.Add)
                    ),
                DepthStencilStateDescription.Disabled,
                new RasterizerStateDescription(
                    FaceCullMode.None,
                    PolygonFillMode.Solid,
                    FrontFace.Clockwise,
                    depthClipEnabled: false,
                    scissorTestEnabled: true),
                PrimitiveTopology.TriangleList,
                new ShaderSetDescription(new[] { vtxLayout }, _vdShaders),
                new[] { layoutProjMatrix, layoutTexture },
                new OutputDescription(
                    null,
                    new OutputAttachmentDescription(PixelFormat.B8_G8_R8_A8_UNorm_SRgb))
                );

            _vdPipeline      = factory.CreateGraphicsPipeline(pipelineDesc);
            _vdPipeline.Name = "MainPipeline";

            _vdProjMatrixUniformBuffer = factory.CreateBuffer(new BufferDescription(
                                                                  (uint)sizeof(Matrix4x4),
                                                                  BufferUsage.Dynamic | BufferUsage.UniformBuffer));
            _vdProjMatrixUniformBuffer.Name = "_vdProjMatrixUniformBuffer";

            _vdSetProjMatrix = factory.CreateResourceSet(new ResourceSetDescription(
                                                             layoutProjMatrix,
                                                             _vdProjMatrixUniformBuffer));
            _vdSetProjMatrix.Name = "_vdSetProjMatrix";
            var io = ImGui.GetIO();

            io.Fonts.GetTexDataAsRGBA32(out byte *pixels, out var width, out var height, out _);

            _vdTexture = factory.CreateTexture(TextureDescription.Texture2D(
                                                   (uint)width, (uint)height,
                                                   mipLevels: 1,
                                                   arrayLayers: 1,
                                                   PixelFormat.R8_G8_B8_A8_UNorm_SRgb,
                                                   TextureUsage.Sampled));

            _vdTexture.Name = "MainTexture";

            _vdSampler = factory.CreateSampler(SamplerDescription.Linear);

            _vdSampler.Name = "MainSampler";

            _vdGfxDevice.UpdateTexture(
                _vdTexture,
                (IntPtr)pixels,
                (uint)(width * height * 4),
                x: 0, y: 0, z: 0,
                (uint)width, (uint)height, depth: 1,
                mipLevel: 0,
                arrayLayer: 0);

            _vdSetTexture = factory.CreateResourceSet(new ResourceSetDescription(
                                                          layoutTexture,
                                                          _vdTexture,
                                                          _vdSampler));

            _vdSetTexture.Name = "SetTexture";

            io.Fonts.SetTexID((nint)0);
            io.Fonts.ClearTexData();

            _vdGfxDevice.ResizeMainWindow((uint)w, (uint)h);
            _vdGfxDevice.SwapBuffers();
        }