Skip to content

Commit

Permalink
Hook up the 3d viewport rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
dpjudas committed Jul 21, 2024
1 parent 7ec2c28 commit a7bfed4
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 52 deletions.
16 changes: 8 additions & 8 deletions SurrealEngine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,7 @@ Engine::Engine(GameLaunchInfo launchinfo) : LaunchInfo(launchinfo)

//packages = std::make_unique<PackageManager>(LaunchInfo.folder, LaunchInfo.engineVersion, LaunchInfo.gameName);
packages = std::make_unique<PackageManager>(LaunchInfo);
}

Engine::~Engine()
{
engine = nullptr;
}

void Engine::Run()
{
std::srand((unsigned int)std::time(nullptr));

gameengine = UObject::Cast<UGameEngine>(packages->NewObject("gameengine", "Engine", "GameEngine"));
Expand All @@ -63,7 +55,15 @@ void Engine::Run()
console->Viewport() = viewport;
canvas->Viewport() = viewport;
viewport->Console() = console;
}

Engine::~Engine()
{
engine = nullptr;
}

void Engine::Run()
{
LoadEngineSettings();
LoadKeybindings();

Expand Down
3 changes: 3 additions & 0 deletions SurrealEngine/Render/RenderScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ void RenderSubsystem::DrawScene()
Scene.Clipper.numSurfs = 0;
Scene.Clipper.numTris = 0;

if (!engine->Level)
return;

// Make sure all actors are at the right location in the BSP
for (UActor* actor : engine->Level->Actors)
{
Expand Down
41 changes: 25 additions & 16 deletions SurrealEngine/RenderDevice/RenderDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
#include "Vulkan/VulkanRenderDevice.h"
#include "UObject/ULevel.h"
#include <zwidget/core/colorf.h>
#include <ZWidget/core/widget.h>

std::unique_ptr<RenderDevice> RenderDevice::Create(GameWindow* viewport, std::shared_ptr<VulkanSurface> surface)
std::unique_ptr<RenderDevice> RenderDevice::Create(Widget* viewport, std::shared_ptr<VulkanSurface> surface)
{
return std::make_unique<VulkanRenderDevice>(viewport, surface);
}
Expand All @@ -19,6 +20,25 @@ RenderDeviceCanvas::RenderDeviceCanvas(RenderDevice* device) : device(device)
void RenderDeviceCanvas::begin(const Colorf& color)
{
device->Lock(vec4(0.0f), vec4(0.0f), vec4(color.r, color.g, color.b, color.a));

frame.XB = 0;
frame.YB = 0;
frame.X = device->Viewport->GetNativePixelWidth();
frame.Y = device->Viewport->GetNativePixelHeight();
frame.FX = (float)device->Viewport->GetNativePixelWidth();
frame.FY = (float)device->Viewport->GetNativePixelHeight();
frame.FX2 = (float)device->Viewport->GetNativePixelWidth() * 0.5f;
frame.FY2 = (float)device->Viewport->GetNativePixelHeight() * 0.5f;
frame.Viewport = device->Viewport;
frame.FovAngle = 90.0f;
frame.ObjectToWorld = mat4::identity();
frame.WorldToView = mat4::identity();
float Aspect = frame.FY / frame.FX;
float RProjZ = (float)std::tan(radians(frame.FovAngle) * 0.5f);
float RFX2 = 2.0f * RProjZ / frame.FX;
float RFY2 = 2.0f * RProjZ * Aspect / frame.FY;
frame.Projection = mat4::frustum(-RProjZ, RProjZ, -Aspect * RProjZ, Aspect * RProjZ, 1.0f, 32768.0f, handedness::left, clipzrange::zero_positive_w);
device->SetSceneNode(&frame);
}

void RenderDeviceCanvas::end()
Expand All @@ -28,13 +48,12 @@ void RenderDeviceCanvas::end()

void RenderDeviceCanvas::begin3d()
{
CheckFrame();
device->ClearZ(&frame);
}

void RenderDeviceCanvas::end3d()
{
CheckFrame();
device->SetSceneNode(&frame);
device->ClearZ(&frame);
}

Expand All @@ -44,7 +63,7 @@ std::unique_ptr<CanvasTexture> RenderDeviceCanvas::createTexture(int width, int
texture->Width = width;
texture->Height = height;
texture->Info.CacheID = (uint64_t)(ptrdiff_t)texture.get();
texture->Info.Format = TextureFormat::ARGB8;
texture->Info.Format = TextureFormat::BGRA8;
texture->Info.USize = width;
texture->Info.VSize = height;
texture->Info.NumMips = 1;
Expand All @@ -58,30 +77,20 @@ std::unique_ptr<CanvasTexture> RenderDeviceCanvas::createTexture(int width, int

void RenderDeviceCanvas::drawLineAntialiased(float x0, float y0, float x1, float y1, Colorf color)
{
CheckFrame();
device->Draw2DLine(&frame, vec4(color.r, color.g, color.b, color.a), vec3(x0, y0, 1.0f), vec3(x1, y1, 1.0f));
}

void RenderDeviceCanvas::fillTile(float x, float y, float width, float height, Colorf color)
{
CheckFrame();
device->DrawTile(&frame, static_cast<RenderDeviceTexture*>(whiteTexture.get())->Info, x, y, width, height, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, vec4(color.r, color.g, color.b, color.a), vec4(0.0f), 0);
device->DrawTile(&frame, static_cast<RenderDeviceTexture*>(whiteTexture.get())->Info, x, y, width, height, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, vec4(color.r, color.g, color.b, color.a), vec4(0.0f), PF_Highlighted);
}

void RenderDeviceCanvas::drawTile(CanvasTexture* texture, float x, float y, float width, float height, float u, float v, float uvwidth, float uvheight, Colorf color)
{
CheckFrame();
device->DrawTile(&frame, static_cast<RenderDeviceTexture*>(texture)->Info, x, y, width, height, u, v, uvwidth, uvheight, 1.0f, vec4(color.r, color.g, color.b, color.a), vec4(0.0f), 0);
device->DrawTile(&frame, static_cast<RenderDeviceTexture*>(texture)->Info, x, y, width, height, u, v, uvwidth, uvheight, 1.0f, vec4(color.r, color.g, color.b, color.a), vec4(0.0f), PF_Highlighted);
}

void RenderDeviceCanvas::drawGlyph(CanvasTexture* texture, float x, float y, float width, float height, float u, float v, float uvwidth, float uvheight, Colorf color)
{
CheckFrame();
device->DrawTile(&frame, static_cast<RenderDeviceTexture*>(texture)->Info, x, y, width, height, u, v, uvwidth, uvheight, 1.0f, vec4(color.r, color.g, color.b, color.a), vec4(0.0f), PF_SubpixelFont);
}

void RenderDeviceCanvas::CheckFrame()
{
// To do: check if frame is up to date and update it if not
// device->SetSceneNode(&frame);
}
12 changes: 6 additions & 6 deletions SurrealEngine/RenderDevice/RenderDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
#include <zwidget/core/canvas.h>
#include <zwidget/core/rect.h>

class GameWindow;
class UTexture;
class UActor;
class VulkanSurface;
class Widget;

struct FSceneNode
{
int XB, YB; // viewport top left
int X, Y; // viewport size
float FX, FY;
float FX2, FY2;
GameWindow* Viewport = nullptr;
Widget* Viewport = nullptr;
float FovAngle;

mat4 ObjectToWorld;
Expand Down Expand Up @@ -91,7 +91,7 @@ class OutputDevice
class RenderDevice
{
public:
static std::unique_ptr<RenderDevice> Create(GameWindow* viewport, std::shared_ptr<VulkanSurface> surface);
static std::unique_ptr<RenderDevice> Create(Widget* viewport, std::shared_ptr<VulkanSurface> surface);

virtual ~RenderDevice() = default;

Expand All @@ -115,7 +115,7 @@ class RenderDevice

bool ParseCommand(std::string* cmd, const std::string& keyword) { return false; }

GameWindow* Viewport = nullptr;
Widget* Viewport = nullptr;
bool PrecacheOnFlip = false;
float Brightness = 0.5f;
};
Expand All @@ -138,6 +138,8 @@ class RenderDeviceCanvas : public Canvas
void begin3d() override;
void end3d() override;

RenderDevice* GetRenderDevice() { return device; }

protected:
std::unique_ptr<CanvasTexture> createTexture(int width, int height, const void* pixels, ImageFormat format) override;
void drawLineAntialiased(float x0, float y0, float x1, float y1, Colorf color) override;
Expand All @@ -146,8 +148,6 @@ class RenderDeviceCanvas : public Canvas
void drawGlyph(CanvasTexture* texture, float x, float y, float width, float height, float u, float v, float uvwidth, float uvheight, Colorf color) override;

private:
void CheckFrame();

RenderDevice* device = nullptr;
FSceneNode frame;
};
2 changes: 1 addition & 1 deletion SurrealEngine/RenderDevice/Vulkan/RenderPassManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void RenderPassManager::CreatePipelines()
}
else // PF_SubpixelFont
{
builder.BlendMode(VK_BLEND_OP_ADD, VK_BLEND_FACTOR_CONSTANT_COLOR, VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR/*, VK_BLEND_FACTOR_ZERO, VK_BLEND_FACTOR_ONE*/);
builder.BlendMode(VK_BLEND_OP_ADD, VK_BLEND_FACTOR_CONSTANT_COLOR, VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR);
builder.AddFragmentShader(fragShaderAlphaTest[type]);
}

Expand Down
12 changes: 6 additions & 6 deletions SurrealEngine/RenderDevice/Vulkan/VulkanRenderDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void VulkanError(const char* text)
Exception::Throw(text);
}

VulkanRenderDevice::VulkanRenderDevice(GameWindow* InViewport, std::shared_ptr<VulkanSurface> surface)
VulkanRenderDevice::VulkanRenderDevice(Widget* InViewport, std::shared_ptr<VulkanSurface> surface)
{
Viewport = InViewport;

Expand Down Expand Up @@ -286,8 +286,8 @@ void VulkanRenderDevice::Lock(vec4 InFlashScale, vec4 InFlashFog, vec4 ScreenCle
FlashScale = InFlashScale;
FlashFog = InFlashFog;

int width = Viewport->GetPixelWidth();
int height = Viewport->GetPixelHeight();
int width = Viewport->GetNativePixelWidth();
int height = Viewport->GetNativePixelHeight();

if (!Textures->Scene || Textures->Scene->width != width || Textures->Scene->height != height)
{
Expand Down Expand Up @@ -384,7 +384,7 @@ void VulkanRenderDevice::Unlock(bool Blit)
RenderPasses->EndScene(Commands->GetDrawCommands());

BlitSceneToPostprocess();
SubmitAndWait(Blit, Viewport->GetPixelWidth(), Viewport->GetPixelHeight());
SubmitAndWait(Blit, Viewport->GetNativePixelWidth(), Viewport->GetNativePixelHeight());

IsLocked = false;
}
Expand Down Expand Up @@ -858,8 +858,8 @@ void VulkanRenderDevice::ClearZ(FSceneNode* Frame)

void VulkanRenderDevice::ReadPixels(FColor* Pixels)
{
int w = Viewport->GetPixelWidth();
int h = Viewport->GetPixelHeight();
int w = Viewport->GetNativePixelWidth();
int h = Viewport->GetNativePixelHeight();
void* data = Pixels;

auto dstimage = ImageBuilder()
Expand Down
2 changes: 1 addition & 1 deletion SurrealEngine/RenderDevice/Vulkan/VulkanRenderDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CachedTexture;
class VulkanRenderDevice : public RenderDevice
{
public:
VulkanRenderDevice(GameWindow* InViewport, std::shared_ptr<VulkanSurface> surface);
VulkanRenderDevice(Widget* InViewport, std::shared_ptr<VulkanSurface> surface);
~VulkanRenderDevice();

void Flush(bool AllowPrecache) override;
Expand Down
15 changes: 8 additions & 7 deletions SurrealEngine/UI/Editor/Editor3DViewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Editor3DViewport.h"
#include "Engine.h"
#include "Render/RenderSubsystem.h"
#include "RenderDevice/RenderDevice.h"
#include <zwidget/core/colorf.h>

Editor3DViewport::Editor3DViewport(Widget* parent) : EditorViewport(parent)
Expand All @@ -24,16 +25,16 @@ void Editor3DViewport::OnPaint(Canvas* canvas)

canvas->begin3d();

//if (!engine->render)
// engine->render = std::make_unique<RenderSubsystem>(Window()->GetRenderDevice());
if (!engine->render)
engine->render = std::make_unique<RenderSubsystem>(static_cast<RenderDeviceCanvas*>(canvas)->GetRenderDevice());

engine->CameraLocation = Location;
engine->CameraRotation = Rotation;
engine->ViewportX = (int)std::round(topLeft.x);
engine->ViewportY = (int)std::round(topLeft.y);
engine->ViewportWidth = (int)std::round(GetWidth());
engine->ViewportHeight = (int)std::round(GetHeight());
//engine->render->DrawEditorViewport();
engine->ViewportX = (int)std::round(topLeft.x * GetDpiScale());
engine->ViewportY = (int)std::round(topLeft.y * GetDpiScale());
engine->ViewportWidth = (int)std::round(GetWidth() * GetDpiScale());
engine->ViewportHeight = (int)std::round(GetHeight() * GetDpiScale());
engine->render->DrawEditorViewport();

canvas->end3d();
}
Expand Down
18 changes: 17 additions & 1 deletion SurrealEngine/UI/Editor/EditorMainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@
#include "UObject/UActor.h"
#include "UObject/ULevel.h"
#include "Utils/File.h"
#include "RenderDevice/RenderDevice.h"
#include <zvulkan/vulkansurface.h>
#include <zvulkan/vulkanbuilders.h>
#include <zwidget/window/zvulkanwidget.h>
#include <zwidget/widgets/menubar/menubar.h>

EditorMainWindow::EditorMainWindow()
EditorMainWindow::EditorMainWindow() : MainWindow(RenderAPI::Vulkan)
{
std::shared_ptr<VulkanInstance> instance = CreateZVulkanInstanceBuilder(this)
.OptionalSwapchainColorspace()
.DebugLayer(false)
.Create();

std::shared_ptr<VulkanSurface> surface = CreateZVulkanSurface(this, instance);
if (!surface)
throw std::runtime_error("No vulkan surface found");

device = RenderDevice::Create(this, surface);
SetCanvas(std::make_unique<RenderDeviceCanvas>(device.get()));

GetMenubar()->AddItem("File", [this](Menu* menu) { OnFileMenu(menu); });
GetMenubar()->AddItem("Edit", [this](Menu* menu) { OnEditMenu(menu); });
GetMenubar()->AddItem("View", [this](Menu* menu) { OnViewMenu(menu); });
Expand Down
2 changes: 2 additions & 0 deletions SurrealEngine/UI/Editor/EditorMainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class EditorWorkspace;
class Menu;
class RenderDevice;

class EditorMainWindow : public MainWindow
{
Expand Down Expand Up @@ -52,5 +53,6 @@ class EditorMainWindow : public MainWindow

private:
void LoadMap(std::string& mapName);
std::unique_ptr<RenderDevice> device;
std::unique_ptr<OpenFileDialog> openFileDialog = nullptr;
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Statusbar;
class MainWindow : public Widget
{
public:
MainWindow();
MainWindow(RenderAPI api = RenderAPI::Unspecified);
~MainWindow();

Menubar* GetMenubar() const { return MenubarWidget; }
Expand Down
2 changes: 1 addition & 1 deletion Thirdparty/ZWidget/src/widgets/mainwindow/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "widgets/toolbar/toolbar.h"
#include "widgets/statusbar/statusbar.h"

MainWindow::MainWindow() : Widget(nullptr, WidgetType::Window)
MainWindow::MainWindow(RenderAPI api) : Widget(nullptr, WidgetType::Window, api)
{
MenubarWidget = new Menubar(this);
// ToolbarWidget = new Toolbar(this);
Expand Down
37 changes: 33 additions & 4 deletions Thirdparty/ZWidget/src/window/win32/win32_open_file_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include "win32_display_window.h"
#include "core/widget.h"
#include <stdexcept>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>

Win32OpenFileDialog::Win32OpenFileDialog(Win32DisplayWindow* owner) : owner(owner)
{
Expand Down Expand Up @@ -88,10 +92,35 @@ bool Win32OpenFileDialog::Show()
}
}

if (owner)
result = open_dialog->Show(owner->WindowHandle.hwnd);
else
result = open_dialog->Show(0);
// For some reason this can hang deep inside Win32 if we do it on the calling thread!
{
bool done = false;
std::mutex mutex;
std::condition_variable condvar;

std::thread thread([&]() {

if (owner)
result = open_dialog->Show(owner->WindowHandle.hwnd);
else
result = open_dialog->Show(0);

std::unique_lock lock(mutex);
done = true;
condvar.notify_all();

});

std::unique_lock lock(mutex);
while (!done)
{
DisplayBackend::Get()->ProcessEvents();
using namespace std::chrono_literals;
condvar.wait_for(lock, 50ms, [&]() { return done; });
}
lock.unlock();
thread.join();
}

if (SUCCEEDED(result))
{
Expand Down

0 comments on commit a7bfed4

Please sign in to comment.