Jump to content

Building a Zero-Overhead Renderer

Josh

986 views

The Leadwerks 4 renderer was built for maximum flexibility. The Leadwerks 5 renderer is being built first and foremost for great graphics with maximum speed. This is the fundamental difference between the two designs. VR is the main driving force for this direction, but all games will benefit.

Multithreaded Design

Leadwerks 4 does make use of multithreading in some places but it is fairly simplistic. In Leadwerks 5 the entire architecture is based around separate threads, which is challenging but a lot of fun for me to develop. I worked out a way to create a command buffer on the main thread that stores a list of commands for the rendering thread to perform during the next rendering frame. (Thanks for the tip on Lambda functions @Crazycarpet) Each object in the main thread has a simplified object it is associated with that lives in the rendering thread. For example, each Camera has a RenderCamera object that corresponds to it. Here's how changes in the main thread get added to a command buffer to be executed when the rendering thread is ready:

void Camera::SetClearColor(const float r,const float g,const float b,const float a)
{
	clearcolor.x = r; clearcolor.y = g; clearcolor.z = b; clearcolor.w = a;
#ifdef LEADWERKS_5
	GameEngine::cullingthreadcommandbuffer.push_back( [this->rendercamera, this->clearcolor]() { rendercamera->clearcolor = clearcolor; } );
#endif
}

The World::Render() command is still there for conceptual consistency, but what it really does it add all the accumulated commands onto a stack of command buffers for the rendering thread to evaluate whenever it's ready:

void World::Render(shared_ptr<Buffer> buffer)
{
	//Add render call onto command buffer
	GameEngine::cullingthreadcommandbuffer.push_back(std::bind(&RenderWorld::AddToRenderQueue, this->renderworld));

	//Copy command buffer onto culling command buffer stack
	GameEngine::CullingThreadCommandBufferMutex->Lock();
	GameEngine::cullingthreadcommandbufferstack.push_back(GameEngine::cullingthreadcommandbuffer);
	GameEngine::CullingThreadCommandBufferMutex->Unlock();
  
	//Clear the command buffer and start over
	GameEngine::cullingthreadcommandbuffer.clear();
}

The rendering thread is running in a loop inside a function that looks something like this:

shared_ptr<SharedObject> GameEngine::CullingThreadEntryPoint(shared_ptr<SharedObject> o)
{
	while (true)
	{
		//Get the number of command stacks that are queued
		CullingThreadCommandBufferMutex->Lock();
		int count = cullingthreadcommandbufferstack.size();
		CullingThreadCommandBufferMutex->Unlock();

		//For each command stack
		for (int i = 0; i < count; ++i)
		{
			//For each command
			for (int n = 0; n < cullingthreadcommandbufferstack[i].size(); ++n)
			{
				//Execute command
				cullingthreadcommandbufferstack[i][n]();
			}
		}

		//Remove executed command stacks
		CullingThreadCommandBufferMutex->Lock();
		int newcount = cullingthreadcommandbufferstack.size();
		if (newcount == count)
		{
			cullingthreadcommandbufferstack.clear();
		}
		else
		{
			memcpy(&cullingthreadcommandbufferstack[0], &cullingthreadcommandbufferstack[count], sizeof(sizeof(cullingthreadcommandbufferstack[0])) * (newcount - count));
			cullingthreadcommandbufferstack.resize(newcount);
		}
		CullingThreadCommandBufferMutex->Unlock();

		//Render queued worlds
		for (auto it = RenderWorld::renderqueue.begin(); it != RenderWorld::renderqueue.end(); ++it)
		{
			(it->first)->Render(nullptr);
		}
	}
	return nullptr;
}

I am trying to design the system for maximum flexibility with the thread speeds so that we can experiment with different frequencies for each stage. This is why the rendering thread goes through and executes all commands an all accumulated command buffers before going on to actually render any queued world. This prevents the rendering thread from rendering an extra frame when another one has already been received (which shouldn't really happen, but we will see).

As you can see, the previously expensive World::Render() command now does almost nothing before returning to your game loop. I am also going to experiment with running the game loop and the rendering loop at different speeds. So let's say previously your game was running at 60 FPS and 1/3 of that time was spent rendering the world. This left you without about 11 milliseconds to execute your game code, or things would start to slow down. With the new design your game code could have up to 33 milliseconds to execute without compromising the framerate. That means your code could be three times more complex, and you would not have to worry so much about efficiency, since the rendering thread will keep blazing away at a much faster rate.

The game loop is a lot simpler now with just two command you need to update and render the world. This gives you a chance to adjust some objects after physics and before rendering. A basic Leadwerks 5 program is really simple:

#include "Leadwerks.h"

using namespace Leadwerks;

int main(int argc, const char *argv[])
{
	auto window = CreateWindow("MyGame");
	auto context = CreateContext(window);
	auto world = CreateWorld();
	auto camera = CreateCamera(world);

	while (true)
	{
		if (window->KeyHit(KEY_ESCAPE) or window->Closed()) return 0;
		world->Update();
		world->Render(context);
	}
}

This may cause problems if you try to do something fancy like render a world to a buffer and then use that buffer as a texture in another world. We might lose some flexibility there, and if we do I will prioritize speed over having lots of options.

Clustered Forward Rendering

Leadwerks has used a deferred renderer since version 2.1. Version 2.0 was a forward renderer with shadowmaps, and it didn't work very well. At the time, GPUs were not very good at branching logic. If you had an if / else statement, the GPU would perform BOTH branches (including expensive texture lookups) and take the result of the "true" one. To get around this problem, the engine would generate a new version of a shader each time a new combination of lights were onscreen, causing period microfreezes when a new shader was loaded. In 2.1 we switched to a deferred renderer which eliminated these problems. Due to increasingly smart graphics hardware and more flexible modern APIs a new technique called clustered forward rendering is now possible, offering flexibility similar to a deferred renderer, with the increased speed of a forward renderer. Here is a nice article that describes the technique:
http://www.adriancourreges.com/blog/2016/09/09/doom-2016-graphics-study/

51_grain_pre.jpg.b5b4718bc5962e13f7c200c9454ebca3.jpg

This approach is also more scalable. Extra renders to the normal buffer and other details can be skipped for better scaling on integrated graphics and slower hardware. I'm not really targeting slow hardware as a priority, but I wouldn't be surprised if it ran extremely fast on integrated graphics when the settings are turned down. Of course, the system requirements will be the same because we need modern API features to do this.

I'm still a little foggy on how custom post-processing effects will be implemented. There will definitely be more standard features built into the renderer. For example, SSR will be mixed with probe reflections and a quality setting (off, static, dynamic) will determine how much processing power is used for reflections. If improved performance and integration comes at the cost of reduced flexibility in the post-process shaders, I will choose that option, but so far I don't foresee any problems.

Vulkan Graphics

The new renderer is being developed with OpenGL 4.1 so that I can make a more gradual progression, but I am very interested in moving to Vulkan once I have the OpenGL build worked out. Valve made an agreement with the developers of MoltenVK to release the SDK for free. This code translates Vulkan API calls into Apple's Metal API, so you basically have Vulkan running on Mac (sort of). I previously contacted the MoltenVK team about a special license for Leadwerks that would allow you guys to release your games on Mac without buying a MoltenVK license, but we did not reach any agreement and at the time the whole prospect seemed pretty shaky. With Valve supporting this I feel more confident going in this direction. In fact, due to the design of our engine, it would be possible to outsource the task of a Vulkan renderer without granting any source code access or complicating the internals of the engine one bit.

  • Like 5


5 Comments


Recommended Comments

Will this feature be released in Leadwerks 4.X or just in 5.0?

If it's just in 5.0, will we get a Linux build soon?

Share this comment


Link to comment

Can't wait to see what the future holds for Leadwerks. You will be able to make way better use of the CPU's threads with Vulkan so that'll be fun (if it happens).

Don't forget to always use RenderDoc when you're changing up the renderer. Best tool ever made, I swear... although I'm sure you've used it already :)

Share this comment


Link to comment

@martyj This is the architecture change stuff I was talking about in Leadwerks 5. It also introduces breaking API changes. I will continue to develop this on Windows and only release a Linux or Mac build when it is further along. This will make a huge difference for the type of game you are working on, since I think your game logic is pretty intensive.

@Crazycarpet I have never heard of that program before but I will definitely try it out. It looks cool!

 

  • Like 1

Share this comment


Link to comment

I have very very basic multithreaded rendering working now. Literally all it does it clear the screen but all OpenGL calls are occurring on a second thread.

Share this comment


Link to comment

I think it's important to keep trudging forward with newer hardware.  AMD's affordable core act (RYZEN) means 6 and 8 core CPUs (with 12 and 16 threads) are going to be the norm in the coming years, because intel has had to adapt to their pricing and offering, so are likewise offering more cores for cheaper.  We already saw them move coffee lake to 6 cores after a decade of quad cores.  So to me, offering to leverage this in your core engine is going to be a strong selling point and get developer attention.

AMD has a 15w, mobile 4 core 8 thread CPU.  It's getting heated out there on the CPU front.  As soon as miners stop buying all the GPUs, I think we're going to see some trading blows over the next year starting from this summer.

Offering Vulkan support out of the box is going to turn a lot of heads as well, especially if it's well done.

Good work Josh.  Looking forward to playing with Leadwerks on my 8 core AMD and RX Vega =)

Share this comment


Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Blog Entries

    • By Marcousik in Marcousik's Creations Blog 2
      I updated the moto I was working on and now the physics let having fun with this.
       
    • By Josh in Josh's Dev Blog 24
      Current generation graphics hardware only supports up to a 32-bit floating point depth buffer, and that isn't adequate for large-scale rendering because there isn't enough precision to make objects appear in the correct order and prevent z-fighting.

      After trying out a few different approaches I found that the best way to support large-scale rendering is to allow the user to create several cameras. The first camera should have a range of 0.1-1000 meters, the second would use the same near / far ratio and start where the first one left off, with a depth range of 1000-10,000 meters. Because the ratio of near to far ranges is what matters, not the actual distance, the numbers can get very big very fast. A third camera could be added with a range out to 100,000 kilometers!
      The trick is to set the new Camera::SetClearMode() command to make it so only the furthest-range camera clears the color buffer. Additional cameras clear the depth buffer and then render on top of the previous draw. You can use the new Camera::SetOrder() command to ensure that they are drawn in the order you want.
      auto camera1 = CreateCamera(world); camera1->SetRange(0.1,1000); camera1->SetClearMode(CLEAR_DEPTH); camera1->SetOrder(1); auto camera2 = CreateCamera(world); camera2->SetRange(1000,10000); camera2->SetClearMode(CLEAR_DEPTH); camera2->SetOrder(2); auto camera3 = CreateCamera(world); camera3->SetRange(10000,100000000); camera3->SetClearMode(CLEAR_COLOR | CLEAR_DEPTH); camera3->SetOrder(3); Using this technique I was able to render the Earth, sun, and moon to-scale. The three objects are actually sized correctly, at the correct distance. You can see that from Earth orbit the sun and moon appear roughly the same size. The sun is much bigger, but also much further away, so this is exactly what we would expect.

      You can also use these features to render several cameras in one pass to show different views. For example, we can create a rear-view mirror easily with a second camera:
      auto mirrorcam = CreateCamera(world); mirrorcam->SetParent(maincamera); mirrorcam->SetRotation(0,180,0); mirrorcam=>SetClearMode(CLEAR_COLOR | CLEAR_DEPTH); //Set the camera viewport to only render to a small rectangle at the top of the screen: mirrorcam->SetViewport(framebuffer->GetSize().x/2-200,10,400,50); This creates a "picture-in-picture" effect like what is shown in the image below:

      Want to render some 3D HUD elements on top of your scene? This can be done with an orthographic camera:
      auto uicam = CreateCamera(world); uicam=>SetClearMode(CLEAR_DEPTH); uicam->SetProjectionMode(PROJECTION_ORTHOGRAPHIC); This will make 3D elements appear on top of your scene without clearing the previous render result. You would probably want to move the UI camera far away from the scene so only your HUD elements appear in the last pass.
    • By Josh in Josh's Dev Blog 6
      A new build is available on the beta branch on Steam.
      Updated to Visual Studio 2019. Updated to latest version of OpenVR and Steamworks SDK. Fixed object tracking with seated VR mode. Note that the way seated VR works now is a little different, you need to use the VR orientation instead of just positioning the camera (see below). Added VR:SetRotation() so you can rotate the world around in VR. The VRPlayer script has rotation added to the left controller. Press the touchpad to turn left and right. Any arbitrary rotation will work, including roll and pitch. Here are the offset commands:
      static void VR::SetOffset(const Vec3& position); static void VR::SetOffset(const float x, const float y, const float z); static void VR::SetOffset(const float x, const float y, const float z, const float pitch, const float yaw, const float roll); static void VR::SetOffset(const Vec3& position, const Vec3& rotation); static void VR::SetOffset(const Vec3& position, const Quat& rotation); static Vec3 VR::GetOffset(); static Vec3 VR::GetRotation(); static Quat VR::GetQuaternion();  
×
×
  • Create New...