Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How does Unity target multiple platform Graphics APIs

Discussion in 'General Graphics' started by VictorKs, Mar 9, 2023.

  1. VictorKs

    VictorKs

    Joined:
    Jun 2, 2013
    Posts:
    242
    So I'm creating my first DirectX 11 engine as a learning project. While the standard features are easy to implement, achieving high performance is a really tough job! So how can Unity support so many platforms?
    Do they have multiple teams for each graphics API? Or are there tools that help port APIs?
     
  2. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    551
    I don't know how Unity does it as I don't work for them but as someone who has worked on several proprietary multi-platform engines, I can say that it is usually done with some form of interface/adapter/polymorphic classes. So for example, you'd have a VertexBuffer class with different implementations for different platforms. The rest of the engine doesn't interact with each platform API directly - it uses the interface instead.

    One tool that comes to mind is a cross-compiler for shaders. Unity has their own cross-compiler for ShaderLab code. DXC also supports compiling HLSL to SPIR-V for Vulkan.

    Debugging has always been the biggest problem in my personal experience because it's hard to create a common tool for that. Usually you have to use a separate debugger for each platform. However, Visual Studio has some gdb support by now, I believe. Especially iOS debugging on Windows is difficult.

    PS: There are also cross-compilers for C++ code, of course.
     
    Last edited: Mar 9, 2023
    VictorKs likes this.
  3. VictorKs

    VictorKs

    Joined:
    Jun 2, 2013
    Posts:
    242
    So technically you introduce an abstraction layer between the engine and the APIs. Yeah this makes total sense (Although not planning to learn anything else besides DX11 for now)

    Btw in your experience do engines write their own GUI system for their editor through APIs or use frameworks like imGUI and QT? And have you ever encountered a 3D engine that was not built with C++?
     
  4. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    551
    Writing your own GUI is a lot of work which people often underestimate. Text rendering alone is hard (think of kerning, non-latin scripts, right to left text, vector based rendering, ...). Games can cut some corners because they don't always need all of that but for the editor, you can't. So I'd highly recommend to use an existing UI framework for the editor.

    You can have an in-game editor but a separate editor is more common. The game can either run on the same process/thread as the editor like Unity does or separately.

    Yes (Java), but that was a specific use case. I'd say, in professional game development, the vast majority of game engines are written in C++ but there are certainly some pioneers out there that use Rust, for example.
     
    VictorKs likes this.
  5. VictorKs

    VictorKs

    Joined:
    Jun 2, 2013
    Posts:
    242
    Hehe I just tried and realized it for myself :D
    It seems a game engine is a much harder task than I had anticipated. I mean its not just the rendering but also the general lifecycle and scene management. And C++ is not that friendly as C#, so I'm going for a software rasterizer built with C#. But the funny twist is that it will be built as a console application! So I believe this will be the first ASCII game engine.
     
    c0d3_m0nk3y likes this.