Skip to content

Commit 77bd1ad

Browse files
committed
- renderer: nvrhi vulkan linux compilation, SDL window
1 parent 98cf28b commit 77bd1ad

File tree

9 files changed

+109
-27
lines changed

9 files changed

+109
-27
lines changed

core/materialsystem1/Renderers/NVRHI/NVRHILibraryD3D12.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ bool CNVRHIRenderLibD3D12::InitAPI(const ShaderAPIParams& params)
280280
// create default swap chain
281281
if (params.windowInfo.windowType != RHI_WINDOW_HANDLE_UNKNOWN)
282282
{
283-
HWND mainWindow = (HWND)params.windowInfo.get(params.windowInfo.userData, RenderWindowInfo::WINDOW);
283+
HWND mainWindow = (HWND)params.windowInfo.get(RenderWindowInfo::WINDOW);
284284

285285
m_defaultSwapChain = CRefPtr<CNVRHISwapChainDXGI>(static_cast<CNVRHISwapChainDXGI*>(CNVRHIRenderLibDXGIBase::CreateSwapChain(params.windowInfo).Ptr()));
286286

@@ -326,7 +326,7 @@ ISwapChainPtr CNVRHIRenderLibD3D12::CreateSwapChain(const RenderWindowInfo& wind
326326

327327
RefCountPtr<IDXGISwapChain1> pSwapChain1;
328328
HRESULT hr = m_dxgiFactory->CreateSwapChainForHwnd(m_rhiGraphicsQueue,
329-
(HWND)windowInfo.get(windowInfo.userData, RenderWindowInfo::WINDOW),
329+
(HWND)windowInfo.get(RenderWindowInfo::WINDOW),
330330
&swapChainImpl->m_dxgiSwapChainDesc, &m_dxgiFullScreenDesc, nullptr,
331331
&pSwapChain1);
332332

core/materialsystem1/Renderers/NVRHI/NVRHILibraryVK.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,18 @@ bool CNVRHIRenderLibVK::InitAPI(const ShaderAPIParams& params)
135135
// This is required for using the MoltenVK portability subset implementation on macOS
136136
m_enabledExtensions.device.append(VK_KHR_PORTABILITY_SUBSET_EXTENSION_NAME);
137137
#endif
138-
138+
139139
#ifdef VK_USE_PLATFORM_WIN32_KHR
140140
m_enabledExtensions.instance.append(VK_KHR_SURFACE_EXTENSION_NAME);
141141
m_enabledExtensions.instance.append(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
142+
#elif defined(VULKAN_USE_PLATFORM_SDL)
143+
const RenderWindowInfo& windowInfo = params.windowInfo;
144+
if(windowInfo.parent && windowInfo.parent->windowType == RHI_WINDOW_HANDLE_SDL)
145+
{
146+
Array<const char*>& instanceExts = *(Array<const char*>*)windowInfo.parent->get(RenderWindowInfo::EXTENSIONS);
147+
for(const char* ext : instanceExts )
148+
m_enabledExtensions.instance.insert(ext);
149+
}
142150
#endif
143151
}
144152

@@ -901,13 +909,27 @@ ISwapChainPtr CNVRHIRenderLibVK::CreateSwapChain(const RenderWindowInfo& windowI
901909
{
902910
// Create the platform-specific surface
903911
#if defined( VULKAN_USE_PLATFORM_SDL )
912+
VkResult surfaceCreateRes = VkResult::VK_ERROR_SURFACE_LOST_KHR;
913+
904914
// Support generic SDL platform for linux and macOS
905-
auto res = vk::Result(CreateSDLWindowSurface((VkInstance)m_vkInstance, (VkSurfaceKHR*)&swapChain->m_vkWindowSurface));
915+
if(windowInfo.parent && windowInfo.parent->windowType == RHI_WINDOW_HANDLE_SDL)
916+
{
917+
swapChain->m_vkWindowSurface = (vk::SurfaceKHR)windowInfo.parent->get(RenderWindowInfo::SURFACE, m_vkInstance);
918+
if(swapChain->m_vkWindowSurface)
919+
{
920+
surfaceCreateRes = VkResult::VK_SUCCESS;
921+
}
922+
}
923+
else
924+
{
925+
ASSERT_FAIL("Not supported window type");
926+
}
927+
auto res = vk::Result(surfaceCreateRes);
906928

907929
#elif defined( VK_USE_PLATFORM_WIN32_KHR )
908930
auto surfaceCreateInfo = vk::Win32SurfaceCreateInfoKHR()
909-
.setHinstance((HINSTANCE)windowInfo.get(windowInfo.userData, RenderWindowInfo::TOPLEVEL))
910-
.setHwnd((HWND)windowInfo.get(windowInfo.userData, RenderWindowInfo::WINDOW));
931+
.setHinstance((HINSTANCE)windowInfo.get(RenderWindowInfo::TOPLEVEL))
932+
.setHwnd((HWND)windowInfo.get(RenderWindowInfo::WINDOW));
911933

912934
auto res = m_vkInstance.createWin32SurfaceKHR(&surfaceCreateInfo, nullptr, &swapChain->m_vkWindowSurface);
913935
#endif

core/materialsystem1/Renderers/NVRHI/NVRHILibraryVK.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "../IRenderLibrary.h"
1414
#include "../RenderWorker.h"
15+
#include "NVRHISwapChainVK.h"
1516

1617
class CNVRHISwapChainVK;
1718

core/materialsystem1/Renderers/NVRHI/NVRHISwapChainVK.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ CNVRHISwapChainVK::CNVRHISwapChainVK(const RenderWindowInfo& windowInfo, ITextur
4242

4343
void* CNVRHISwapChainVK::GetWindow() const
4444
{
45-
return m_winInfo.get(m_winInfo.userData, RenderWindowInfo::WINDOW);
45+
return m_winInfo.get(RenderWindowInfo::WINDOW);
4646
}
4747

4848
ITexturePtr CNVRHISwapChainVK::GetBackbuffer() const

core/materialsystem1/Renderers/RenderManager.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ IRenderLibrary* CEqRenderManager::CreateRenderer(const ShaderAPIParams& params)
6262

6363
#if PLAT_WIN
6464
s_currentRenderLib = &s_NVRHIRenderLibD3D12;
65-
#endif
66-
6765
/*if (!backendName.CompareCaseIns("D3D11"))
6866
s_currentRenderLib = &s_NVRHIRenderLibD3D11;
6967
else*/ if (!backendName.CompareCaseIns("Vulkan"))
7068
s_currentRenderLib = &s_NVRHIRenderLibVK;
69+
#else
70+
s_currentRenderLib = &s_NVRHIRenderLibVK;
71+
#endif
7172

7273
#elif RENDERER_TYPE == RHI_WGPU
7374

core/materialsystem1/Renderers/WGPU/WGPUSwapChain.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ CWGPUSwapChain::~CWGPUSwapChain()
2828

2929
void* CWGPUSwapChain::GetWindow() const
3030
{
31-
return m_winInfo.get(m_winInfo.userData, RenderWindowInfo::WINDOW);
31+
return m_winInfo.get(RenderWindowInfo::WINDOW);
3232
}
3333

3434
ITexturePtr CWGPUSwapChain::GetBackbuffer() const
@@ -114,25 +114,25 @@ bool CWGPUSwapChain::UpdateResize()
114114
{
115115
case RHI_WINDOW_HANDLE_NATIVE_WINDOWS:
116116
windowsSurfDesc.chain.sType = WGPUSType_SurfaceSourceWindowsHWND;
117-
windowsSurfDesc.hinstance = m_winInfo.get(m_winInfo.userData, RenderWindowInfo::TOPLEVEL);
118-
windowsSurfDesc.hwnd = m_winInfo.get(m_winInfo.userData, RenderWindowInfo::WINDOW);
117+
windowsSurfDesc.hinstance = m_winInfo.get(RenderWindowInfo::TOPLEVEL);
118+
windowsSurfDesc.hwnd = m_winInfo.get(RenderWindowInfo::WINDOW);
119119
surfDesc.nextInChain = &windowsSurfDesc.chain;
120120
break;
121121
case RHI_WINDOW_HANDLE_NATIVE_X11:
122122
x11SurfDesc.chain.sType = WGPUSType_SurfaceSourceXlibWindow;
123-
x11SurfDesc.display = m_winInfo.get(m_winInfo.userData, RenderWindowInfo::DISPLAY);
124-
x11SurfDesc.window = (uint64_t)m_winInfo.get(m_winInfo.userData, RenderWindowInfo::WINDOW);
123+
x11SurfDesc.display = m_winInfo.get(RenderWindowInfo::DISPLAY);
124+
x11SurfDesc.window = (uint64_t)m_winInfo.get(RenderWindowInfo::WINDOW);
125125
surfDesc.nextInChain = &x11SurfDesc.chain;
126126
break;
127127
case RHI_WINDOW_HANDLE_NATIVE_WAYLAND:
128128
waylandSurfDesc.chain.sType = WGPUSType_SurfaceSourceWaylandSurface;
129-
waylandSurfDesc.display = m_winInfo.get(m_winInfo.userData, RenderWindowInfo::DISPLAY);
130-
waylandSurfDesc.surface = m_winInfo.get(m_winInfo.userData, RenderWindowInfo::SURFACE);
129+
waylandSurfDesc.display = m_winInfo.get(RenderWindowInfo::DISPLAY);
130+
waylandSurfDesc.surface = m_winInfo.get(RenderWindowInfo::SURFACE);
131131
surfDesc.nextInChain = &waylandSurfDesc.chain;
132132
break;
133133
case RHI_WINDOW_HANDLE_NATIVE_ANDROID:
134134
androidWindowSurfDesc.chain.sType = WGPUSType_SurfaceSourceAndroidNativeWindow;
135-
androidWindowSurfDesc.window = m_winInfo.get(m_winInfo.userData, RenderWindowInfo::WINDOW);
135+
androidWindowSurfDesc.window = m_winInfo.get(RenderWindowInfo::WINDOW);
136136
surfDesc.nextInChain = &androidWindowSurfDesc.chain;
137137
break;
138138
default:

core/premake5.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ project "eqNVRHI"
130130
defines{
131131
"EQRHI_NVRHI",
132132
"RENDERER_TYPE=1",
133-
"VK_USE_PLATFORM_WIN32_KHR"
134133
}
135134
files {
136135
"materialsystem1/Renderers/NVRHI/**.cpp",
@@ -143,7 +142,18 @@ project "eqNVRHI"
143142
"materialsystem1/Renderers/NVRHI/**D3D*.h"
144143
}
145144

145+
filter "system:Linux"
146+
defines {
147+
"VULKAN_USE_PLATFORM_SDL"
148+
--"VK_USE_PLATFORM_XLIB_KHR",
149+
--"VK_USE_PLATFORM_XCB_KHR",
150+
--"VK_USE_PLATFORM_WAYLAND_KHR"
151+
}
152+
146153
filter "system:Windows"
154+
defines {
155+
"VK_USE_PLATFORM_WIN32_KHR"
156+
}
147157
files {
148158
"materialsystem1/Renderers/NVRHI/**DXGI*.cpp",
149159
"materialsystem1/Renderers/NVRHI/**DXGI*.h",

public/materialsystem1/renderers/ShaderAPI_defs.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313
enum ERHIWindowType : int
1414
{
1515
RHI_WINDOW_HANDLE_UNKNOWN = -1,
16-
16+
17+
RHI_WINDOW_HANDLE_SDL,
1718
RHI_WINDOW_HANDLE_NATIVE_WINDOWS,
1819
RHI_WINDOW_HANDLE_NATIVE_X11,
1920
RHI_WINDOW_HANDLE_NATIVE_WAYLAND,
2021
RHI_WINDOW_HANDLE_NATIVE_COCOA,
2122
RHI_WINDOW_HANDLE_NATIVE_ANDROID,
2223
};
2324

25+
struct RenderWindowInfo;
26+
2427
// designed to be sent as windowHandle param
2528
struct RenderWindowInfo
2629
{
@@ -29,13 +32,17 @@ struct RenderWindowInfo
2932
DISPLAY,
3033
WINDOW,
3134
SURFACE,
32-
TOPLEVEL
35+
TOPLEVEL,
36+
EXTENSIONS,
3337
};
34-
using GetterFunc = void*(*)(void* userData, Attribute attrib);
38+
using GetterFunc = void*(RenderWindowInfo::*)(Attribute attrib, void* arg) const;
39+
40+
void* get(Attribute attrib, void* arg = nullptr) const { return (this->*getFunc)(attrib, arg); }
3541

36-
ERHIWindowType windowType{ RHI_WINDOW_HANDLE_UNKNOWN };
37-
GetterFunc get{ nullptr };
38-
void* userData{ nullptr };
42+
ERHIWindowType windowType{ RHI_WINDOW_HANDLE_UNKNOWN };
43+
GetterFunc getFunc{ nullptr };
44+
RenderWindowInfo* parent{ nullptr };
45+
void* userData{ nullptr };
3946
};
4047

4148
//---------------------------------------

shared/sys/sys_host.cpp

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <SDL.h>
99
#include <SDL_syswm.h>
10+
#include <SDL_vulkan.h>
1011
#undef far
1112
#undef near
1213

@@ -346,7 +347,41 @@ void CGameHost::GetVideoModes(Array<SysVideoMode>& displayModes) const
346347
#endif
347348
}
348349

349-
static void* Helper_GetWindowInfo(void* userData, RenderWindowInfo::Attribute attrib)
350+
struct HostRenderWindowInfoSDL : RenderWindowInfo
351+
{
352+
void* GetWindowInfo(RenderWindowInfo::Attribute attrib, void* arg) const;
353+
void* CreateVulkanSurface(RenderWindowInfo::Attribute attrib, void* arg) const;
354+
};
355+
356+
void* HostRenderWindowInfoSDL::CreateVulkanSurface(RenderWindowInfo::Attribute attrib, void* arg) const
357+
{
358+
SDL_Window* window = reinterpret_cast<SDL_Window*>(userData);
359+
if(attrib == RenderWindowInfo::SURFACE)
360+
{
361+
VkSurfaceKHR surface = nullptr;
362+
if(!SDL_Vulkan_CreateSurface( window, (VkInstance)arg, &surface ))
363+
{
364+
MsgError( "SDL_Vulkan_CreateSurface failed: %s\n", SDL_GetError() );
365+
return nullptr;
366+
}
367+
return surface;
368+
}
369+
else if(attrib == RenderWindowInfo::EXTENSIONS)
370+
{
371+
uint32_t sdlCount = 0;
372+
static Array<const char*> sdlInstanceExtensions(PP_SL);
373+
374+
SDL_Vulkan_GetInstanceExtensions( window, &sdlCount, nullptr );
375+
sdlInstanceExtensions.setNum( sdlCount );
376+
SDL_Vulkan_GetInstanceExtensions( window, &sdlCount, sdlInstanceExtensions.ptr() );
377+
378+
return &sdlInstanceExtensions;
379+
}
380+
ASSERT_FAIL("Unsupported attribute %d", attrib);
381+
return nullptr;
382+
}
383+
384+
void* HostRenderWindowInfoSDL::GetWindowInfo(RenderWindowInfo::Attribute attrib, void* arg) const
350385
{
351386
// set window info
352387
SDL_Window* window = reinterpret_cast<SDL_Window*>(userData);
@@ -405,7 +440,7 @@ static void* Helper_GetWindowInfo(void* userData, RenderWindowInfo::Attribute at
405440
}
406441
#endif // PLAT_WIN
407442
default:
408-
ASSERT_FAIL("Not supported window type - %d", winfo.subsystem);
443+
ASSERT_FAIL("Unsupported window type %d or attribute %d", winfo.subsystem, attrib);
409444
}
410445

411446
return nullptr;
@@ -469,9 +504,15 @@ bool CGameHost::InitSystems()
469504
else
470505
rhiParams.screenFormat = FORMAT_RGB8;
471506

507+
RenderWindowInfo sdlWinInfo;
508+
sdlWinInfo.getFunc = (RenderWindowInfo::GetterFunc)&HostRenderWindowInfoSDL::CreateVulkanSurface;
509+
sdlWinInfo.userData = m_window;
510+
sdlWinInfo.windowType = RHI_WINDOW_HANDLE_SDL;
511+
472512
RenderWindowInfo& winInfo = rhiParams.windowInfo;
513+
winInfo.parent = &sdlWinInfo;
473514
winInfo.userData = m_window;
474-
winInfo.get = Helper_GetWindowInfo;
515+
winInfo.getFunc = (RenderWindowInfo::GetterFunc)&HostRenderWindowInfoSDL::GetWindowInfo;
475516

476517
// needed for initialization
477518
switch (winfo.subsystem)

0 commit comments

Comments
 (0)