Live Long and Render (II)

This is the second part of my dev diary about implementing Vulkan support in Crimild. Check out the first part for a brief introduction if you haven’t read it yet.

Changes, changes, changes

I’m still struggling with the class hierarchy and responsibilities. I would like to use RAII as much as possible, but I’m not sure about the API design and who’s responsible for creating new objects yet.

For example, it feels natural that the Instance (basically a wrapper for VkInstance) creates the render devices and swapchain. But, since the surface is platform dependent, it must be created somewhere else which doesn’t feel right.

On the other hand, a render device should create new resources (like images or buffers) but that also means that such resources are coupled with that particular device. What if we have more than one device?

I know, I’m overthinking it as usual but, to be honest, defining the class hierarchy has proven to be the most challenging task so far.

As a side note, I decided to use exceptions for error reporting. Like when attempting to create a Vulkan objects and the process fails for some reason. This simplifies the code a lot and, although there’s an overhead in using exceptions, they’re only used in error paths so it’s not a big issue.

Initialization

The process of initializing Vulkan in Crimild can be described as follows:

  1. The VulkanSystem creates a Vulkan Instance and keeps a strong reference to it that lives for the rest of the simulation
  2. The VulkanSystem creates a surface where we’re going to render into. This is platform dependent mostly.
  3. The Instance creates a Render Device (see below)
  4. The Instance creates a Swapchain (see below)
  5. The Render Device creates resources (images, buffers, etc)
  6. The Swapchain request the Render Device to create Image Views for available Images (usually 2 in order to work with double buffering)
  7. Dark magic goes here (not implemented yet)
  8. Render!

Please keep in mind that this is still work in progress.

Render Device

I’ve been talking about render devices but I didn’t say what they are yet. RenderDevice is a new class that handles both Vulkan’s physical and logical devices. I know that we may have more than one logical device per physical one, but I’m not seeing that as a requirement for the moment. If the time comes where I need to make that distinction, it won’t be hard to split the class in two.

The goals is for RenderDevice to replace the Renderer interface which has become too big over the years.

I don’t have much code for the RenderDevice class at the moment. Well, there’s a lot of code, but it’s mostly for initialization. I’m expecting this class to get bigger and bigger as the time passes.

Swapchain

The Swapchain is kind of a new concept that I borrowed directly from Vulkan. It’s main responsibility is to handle images that need to be presented to the screen/surface.

For such reason, there are only two main functions for a Swapchain object: 1) acquire a new image for us to render to and 2) present that image to the screen once is ready.

The Swapchain class is pretty much completed and I don’t think it might get much bigger than what it is today.

Thinking out loud: Headless Vulkan

This is something that I would like to try out in future iterations. Unlike OpenGL, we can use Vulkan without having to create a visible window. This could prove useful in several scenarios, like when doing complex compute operations, image generation using procedural algorithms, computer vision… even unit tests. I do like the idea of having automatic tests for everything rendering-related that actually mean something as I do for other systems in the engine.

Again, this is not a priority right now, but I’ll definitely give it a try in the future.

Up Next!

Now that we have a window, a render device and a swapchain, I believe the next logical step is to actually render something. Therefore, I’ll be focusing on pipelines and commands next.

Advertisements

Live Long and Render (I)

320x240

Fun fact: I created a draft for this article in early 2016, while I was rounding up Metal support and shortly after Vulkan was announced. It took me three years, but here it is:

YES.

I’m finally working on a Vulkan-based rendering backend for Crimild.

This time it’s going to be much more than just making a Renderer class with Vulkan code inside as I did with Metal some time ago. This time, thought, I’m willing to go as far as I have to and make a modern rendering architecture.

It’s BIG refactor time!

Why now?

I started this year (2019, I think) talking about how render graphs and shader graphs and other rendering improvements were critical for the next major version of Crimild. But as time has passed, I come to realize that they were not enough.

Despite their benefits, I was headed towards a mixed solution. Neither old school nor modern. Using state of the art techniques, but bounded by OpenGL limitations. And, while it was indeed a step forward towards newer paradigms and APIs (Vulkan, Metal, etc), if I followed that road I was going to have to change most of it again in the near future. After all, there’s no gain in making Vulkan work like OpenGL.

Therefore, I started from scratch and decided to do it the right way from the beginning. I’m going to implement a whole new rendering system on top of Vulkan and it’s major concepts (render devices, swapchains, pipelines, render passes, etc).

What about OpenGL? Well, as much as I love it, I would like to get rid of it. The only environment in which I see OpenGL as still relevant is on web (as in WebGL). Being able to publish apps on browsers (throught Emscripten) is still a goal for Crimild, so I guess OpenGL is not going anywhere for now. But, it’s going to change based on whatever architecture I come up with after this refactor.

What about Metal? I am getting rid of the Metal renderer. Plain and simple. There’s no point in supporting both Vulkan and Metal (at least for the moment). And the current implementation is quite limited. The good size of this is that after having a Vulkan-based rendering system, implementing on in Metal should be straightforward. Provided I needed, of course.

What have I done so far?

I have read articles, books, even tweets. I have watched videos. I have completed the excellent Vulkan Tutorial and looked into several examples.

I recently started working on (that is, actually coding) the new rendering architecture on my spare time (I have other priorities at the moment). This is what I achieved so far:

Yeap, that’s just a window.

Well, it’s a bit more than that.

At the moment, I can create a Vulkan Instance and a Surface, which is not a small feat for Vulkan. I’m working on a Mac (using MoltenVK) with GLFW for window management, but the new implementation is supposed to work on multiple platforms.

I made changes to other parts of Crimild too, like simulations and systems. Nothing too big, just a few tools to better prioritize systems. I am planning to change them a lot once go full ECS in the not-so-distant future. But, one refactor at a time.

That’s it for now. I try and write more posts as I make progress. But I won’t make any promises 🙂

Bye!

Scripting improvements for UI

For my current project (not that one, the other one) I needed a mechanism to create user interfaces in a fast and easy way, without having to position elements explicitly.

In a recent post, I talked about a kind of constraint-based UI layout mechanism included in recent versions of Crimild that allow us to create interfaces in a declarative way:

Each element in the UI defines a set of constraints related with either its parent another element.

Surprisingly, I (almost) didn’t change anything about that (so far). I know, I know, it’s shocking to me as well.

Anyway, that mechanism is pretty powerful, indeed. Still, it requires us to recompile the game every time we make changes (because C++). Therefore, the next evolutionary step was quite obvious: move that feature to Lua.

Using Crimild’s Coding facilities introduced last year, I implemented decoding functions for all of the UI-related components. The result is a Lua script that looks like this:

local UIFrameConstraint = 'crimild.ui.UIFrameConstraint'
local makeConstraint = UIFrameConstraint.Maker
-- Other require calls remove for clarity

-- Create a Restart Button
local restartButton = Group(
{
components = {
UIBackground( { color = { 0.0, 0.0, 1.0, 1.0 } } ),
UIFrame(
{
constraints = {
makeConstraint.after( mainMenuButton ),
makeConstraint.bottom( 10 ),
makeConstraint.width( 80 ),
makeConstraint.height( 20 ),
},
}
),
UILabel( { text = 'Restart' } ),
UIRestartButton(),
},
}
)

Simple and fast. Nice!

BTW, that script is using a new organization for scripts based on Lua modules. But that’s a story for another time…