Skip to content

Commit

Permalink
Add a work-in-progress Vulkan backend (#236).
Browse files Browse the repository at this point in the history
Currently, only the SDL platform is supported together with the Vulkan renderer. To use this backend, set the CMake configuration `SAMPLES_BACKEND=SDL_Vulkan`.

Development commits by @wh1t3lord (#236):

- Started Vulkan implementation
- Keep codebase simple, but easy to use.
- ADD: instance creation
- ADD: physical device and device properties
- ADD: The commentaries about ShellRenderInterfaceVulkan and trying to create Surface
- ADD: Destroying functions but get HWND from RMLUI
- ADD: obtaining HWND
- FIX: compilation
- ADD: standard way to initialize and shutdown
- ADD: Queues and Queues Indecies
- ADD: device creation
- ADD: swapchain
- ADD: OnResize method
- ADD: correct swapchain creation
- ADD: sync primitives
- ADD: presenting, but without resources
- ADD: start implementing resource management, creating descriptor set layout
- You must compile shaders by your own. So engine operates with SPV format only, not raw strings, not compiling files.
- ADD: creating shaders, but I need to implement that
- ADD: creating VkShaderModule
- ADD: remove not capable method in that arch
- ADD: Physical device wrapper, pipeline layout creation
- ADD: continue creating VkFramebuffer for swapchain
- FIX: compilation thing and waiting before destroying everything
- ADD: continue work
- FIX: swapchain creation
- FIX: formatting for commentary
- ADD: something new
- ADD: command list ring
- ADD: implemented command list ring
- ADD: added explanation commentary about using multiple logical frames for rendering
- ADD: added allocator for another allocator, but I need to implement it first
- ADD: added final allocator for dynamic buffer
- ADD: ring buffer pool implementation
- FIX: compilation fixes
- FIX: vma uses implementation in cpp
- ADD: successful creation and deletion
- ADD: started rendering begin and end scopes
- ADD: stable rendering
- ADD: correct rendering body
- ADD: clearing back buffer is working well
- ADD: something new
- ADD: texture data wrapper
- ADD: writing thing for uploading
- ADD: need to create separate queue for uploading resources
- ADD: continue work, added upload manager
- ADD: many stuff
- ADD: uploading textures is fine, but think about better memory handling, now it is just simple and trivial
- ADD: successful init/destroy state for app
- ADD: shaders for Vulkan, they are compiled with SPIR-V otherwise you can't load your shaders...
- ADD: successful init/destroy
- FIX: inserting data into descriptor set layout bindings
- FIX: shaders, binding must unique!
- ADD: continue at home
- ADD: Descriptors
- ADD: continue
- ADD: continue
- FIX: obtaining texture from handle
- ADD: continue work about pipeline creation, use VkDescriptorSetLayoutBinding info from CreateDescriptorSetLayout method.
- ADD: creating pipelines, continue testing later
- ADD: successful creation pipelines
- ADD: continue work, because for every compile geometry you need to have own descriptor set, so we cache all vkCmds after all CompileGeometry->RenderCompiledGeometry
- ADD: allocated more than one descriptor set, but still I need to test my thing
- ADD: well it seems drawing functions are recorded, but nothing to show on screen. It is strange. Have to fix it and analyze what is wrong with it.
- ADD: cached compiled geometry_handle_t
- FIX: data uploading was incorrect
- FIX: commentary
- FIX: correct format for color
- ADD: something, but geometry is discarded somehow...
- ADD: Now this renders the scene, but font textures are not valid
- ADD: something new
- ADD: FINAL
- ADD: commentary
- ADD: in order to test you have to uncomment those lines
- FIX: removed pointless commentary due to release of backends branch
- ADD: all necessary commentaries
- ADD: vulkan renderer
- ADD: continue work about releasing stuff
- ADD: continue work, use one large Descriptor with offsets
- ADD: updated out-dated VMA allocator version, deleted some todos, need to finish ring pool
- ADD: using virtual allocator
- ADD: FINAL, but I didn't test the other scenes. Reusing descriptors, Resizing is working
- FIX: message
- ADD: stuff, but WE REALLY need to solve the thing with Descriptors
- ADD: continue at home
- ADD: test at home
- FIX: if we have descriptors, but remove that functionality after tests
- FIX: descriptor set picking
- FIX: descriptor set creation
- ADD: continue testing
- ADD: continue at home
- ADD: one descriptor set for geometry and descriptors per texture
- ADD: something
- ADD: resizing works
- ADD: need for tests
- FIX: with allocation
- ADD: possibly final things

Development by @mikke89:

- Ported the Vulkan shell code to the new backend architecture for RmlUi 5.0.
- Made a script to compile the GLSL shaders to SPIR-V binary C character arrays. This allows directly including the shaders in the renderer source code.

Co-authored-by: wh1t3lord <git.matvey@yandex.ru>
  • Loading branch information
mikke89 and wh1t3lord committed Jun 9, 2022
1 parent 35b1132 commit 531b2c9
Show file tree
Hide file tree
Showing 15 changed files with 33,322 additions and 2 deletions.
193 changes: 193 additions & 0 deletions Backends/RmlUi_Backend_SDL_Vulkan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/*
* This source file is part of RmlUi, the HTML/CSS Interface Middleware
*
* For the latest information, see http://github.com/mikke89/RmlUi
*
* Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
* Copyright (c) 2019 The RmlUi Team, and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

#include "RmlUi_Backend.h"
#include "RmlUi_Platform_SDL.h"
#include "RmlUi_Renderer_Vulkan.h"
#include <RmlUi/Core/Context.h>
#include <RmlUi/Core/Core.h>
#include <RmlUi/Core/FileInterface.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_vulkan.h>

#if !SDL_VIDEO_VULKAN
#error "Only the Vulkan SDL backend is supported."
#endif

/**
Global data used by this backend.
Lifetime governed by the calls to Backend::Initialize() and Backend::Shutdown().
*/
struct BackendData {
SystemInterface_SDL system_interface;
RenderInterface_Vulkan render_interface;

SDL_Window* window = nullptr;

bool running = true;
};
static Rml::UniquePtr<BackendData> data;

bool Backend::Initialize(const char* window_name, int width, int height, bool allow_resize)
{
RMLUI_ASSERT(!data);

if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0)
return false;

const Uint32 window_flags = (SDL_WINDOW_VULKAN | (allow_resize ? SDL_WINDOW_RESIZABLE : 0));

SDL_Window* window = SDL_CreateWindow(window_name, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, window_flags);
if (!window)
{
fprintf(stderr, "SDL error on create window: %s\n", SDL_GetError());
return false;
}

data = Rml::MakeUnique<BackendData>();
data->window = window;

if (!data->render_interface.Initialize(
[](VkInstance instance, VkSurfaceKHR* out_surface) { return (bool)SDL_Vulkan_CreateSurface(data->window, instance, out_surface); }))
{
data.reset();
fprintf(stderr, "Could not initialize Vulkan render interface.");
return false;
}

data->system_interface.SetWindow(window);
data->render_interface.SetViewport(width, height);

return true;
}

void Backend::Shutdown()
{
RMLUI_ASSERT(data);

data->render_interface.Shutdown();

SDL_DestroyWindow(data->window);

data.reset();

SDL_Quit();
}

Rml::SystemInterface* Backend::GetSystemInterface()
{
RMLUI_ASSERT(data);
return &data->system_interface;
}

Rml::RenderInterface* Backend::GetRenderInterface()
{
RMLUI_ASSERT(data);
return &data->render_interface;
}

bool Backend::ProcessEvents(Rml::Context* context, KeyDownCallback key_down_callback)
{
RMLUI_ASSERT(data && context);

bool result = data->running;
SDL_Event ev;

while (SDL_PollEvent(&ev))
{
switch (ev.type)
{
case SDL_QUIT:
{
result = false;
}
break;
case SDL_KEYDOWN:
{
const Rml::Input::KeyIdentifier key = RmlSDL::ConvertKey(ev.key.keysym.sym);
const int key_modifier = RmlSDL::GetKeyModifierState();
const float native_dp_ratio = 1.f;

// See if we have any global shortcuts that take priority over the context.
if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, true))
break;
// Otherwise, hand the event over to the context by calling the input handler as normal.
if (!RmlSDL::InputEventHandler(context, ev))
break;
// The key was not consumed by the context either, try keyboard shortcuts of lower priority.
if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, false))
break;
}
break;
case SDL_WINDOWEVENT:
{
switch (ev.window.event)
{
case SDL_WINDOWEVENT_SIZE_CHANGED:
{
Rml::Vector2i dimensions(ev.window.data1, ev.window.data2);
context->SetDimensions(dimensions);
data->render_interface.SetViewport(dimensions.x, dimensions.y);
}
break;
}
}
break;
default:
{
RmlSDL::InputEventHandler(context, ev);
}
break;
}
}

return result;
}

void Backend::RequestExit()
{
RMLUI_ASSERT(data);

data->running = false;
}

void Backend::BeginFrame()
{
RMLUI_ASSERT(data);

data->render_interface.BeginFrame();
}

void Backend::PresentFrame()
{
RMLUI_ASSERT(data);

data->render_interface.EndFrame();
SDL_GL_SwapWindow(data->window);
}
Loading

0 comments on commit 531b2c9

Please sign in to comment.