started working on ini parser
This commit is contained in:
0
materialsystem/vulkan/computeshader.cpp
Normal file
0
materialsystem/vulkan/computeshader.cpp
Normal file
2
materialsystem/vulkan/device_extensions.h
Normal file
2
materialsystem/vulkan/device_extensions.h
Normal file
@@ -0,0 +1,2 @@
|
||||
REQUIRED_EXTENSION(VK_KHR_SWAPCHAIN)
|
||||
OPTIONAL_EXTENSION(VK_KHR_RAY_TRACING_PIPELINE)
|
||||
@@ -1,40 +0,0 @@
|
||||
#include "materialsystem/imaterialsystem.h"
|
||||
|
||||
class CVkRenderingContext
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
CVkRenderingContext g_vkRenderingContext;
|
||||
|
||||
class CVkMaterialSystem: public IMaterialSystem
|
||||
{
|
||||
public:
|
||||
virtual void Init();
|
||||
virtual void Frame( float fDeltaTime );
|
||||
virtual void Shutdown();
|
||||
|
||||
IRenderContext *GetRenderContext();
|
||||
};
|
||||
|
||||
CVkMaterialSystem g_vkMaterialSystem;
|
||||
IMaterialSystem *materials = &g_vkMaterialSystem;
|
||||
|
||||
|
||||
void CVkMaterialSystem::Init()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void CVkMaterialSystem::Frame( float fDeltaTime )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void CVkMaterialSystem::Shutdown()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
0
materialsystem/vulkan/raytracingshader.cpp
Normal file
0
materialsystem/vulkan/raytracingshader.cpp
Normal file
274
materialsystem/vulkan/rendercontext.cpp
Normal file
274
materialsystem/vulkan/rendercontext.cpp
Normal file
@@ -0,0 +1,274 @@
|
||||
#include "SDL3/SDL_vulkan.h"
|
||||
#include "materialsystem/materialsystem.h"
|
||||
#include "tier0/platform.h"
|
||||
#include "tier1/utlvector.h"
|
||||
#define VK_NO_PROTOTYPES
|
||||
#include "vulkan/vulkan_core.h"
|
||||
#include "vulkan_state.h"
|
||||
#include "igamewindow.h"
|
||||
|
||||
|
||||
#define REQUIRED_EXTENSION(ext) ext##_EXTENSION_NAME,
|
||||
#define OPTIONAL_EXTENSION(ext) ext##_EXTENSION_NAME,
|
||||
const char *g_vkDeviceExtensions[] = {
|
||||
#include "device_extensions.h"
|
||||
};
|
||||
#undef REQUIRED_EXTENSION
|
||||
#undef OPTIONAL_EXTENSION
|
||||
|
||||
|
||||
|
||||
class CVkRenderContext: public IRenderContext
|
||||
{
|
||||
public:
|
||||
virtual void Init() override;
|
||||
virtual void Frame( float fDeltaTime ) override;
|
||||
virtual void Shutdown() override;
|
||||
|
||||
virtual IVertexBuffer *CreateVertexBuffer( uint32_t nSize ) override;
|
||||
virtual IIndexBuffer *CreateIndexBuffer( uint32_t nSize ) override;
|
||||
virtual IImage *CreateRenderTarget( uint32_t x, uint32_t y, EImageFormat eFormat, EMultisampleType eMultisampleType ) override;
|
||||
virtual IImage *CreateStorageImage( uint32_t x, uint32_t y, EImageFormat eFormat, EMultisampleType eMultisampleType ) override;
|
||||
|
||||
virtual void DestroyBuffer( IBuffer *pBuffer ) override;
|
||||
virtual void DestroyImage( IImage *pImage ) override;
|
||||
private:
|
||||
IBuffer *CreateBuffer( uint32_t nSize, VkBufferUsageFlags2 eUsage );
|
||||
IBuffer *CreateBufferAligned( uint32_t nSize, uint32_t nAlignment, VkBufferUsageFlags2 eUsage );
|
||||
|
||||
VkPhysicalDevice SelectPhysicalDevice( CUtlVector<VkPhysicalDevice> physicalDevices );
|
||||
|
||||
void CreateSwapchain();
|
||||
void DestroySwapchain();
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Creates vertex buffer. Wrapper over CreateBuffer
|
||||
//-----------------------------------------------------------------------------
|
||||
IVertexBuffer *CVkRenderContext::CreateVertexBuffer( uint32_t nSize )
|
||||
{
|
||||
return (IVertexBuffer*)CreateBuffer(nSize, VK_BUFFER_USAGE_2_VERTEX_BUFFER_BIT);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Creates index buffer. Wrapper over CreateBuffer
|
||||
//-----------------------------------------------------------------------------
|
||||
IIndexBuffer *CVkRenderContext::CreateIndexBuffer( uint32_t nSize )
|
||||
{
|
||||
return (IIndexBuffer*)CreateBuffer(nSize, VK_BUFFER_USAGE_2_INDEX_BUFFER_BIT);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Creates basic vulkan buffer
|
||||
//-----------------------------------------------------------------------------
|
||||
IBuffer *CreateBuffer( uint32_t nSize, VkBufferUsageFlags2 eUsage )
|
||||
{
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Creates vulkan buffer aligned to the nAlignment
|
||||
// Useful for everything eg: ray tracing, which requires them to be aligned
|
||||
// to the groupBaseAlignment.
|
||||
//-----------------------------------------------------------------------------
|
||||
IBuffer *CreateBufferAligned( uint32_t nSize, uint32_t nAlignment, VkBufferUsageFlags2 eUsage )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
IImage *CVkRenderContext::CreateRenderTarget( uint32_t x, uint32_t y, EImageFormat eFormat, EMultisampleType eMultisampleType )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
IImage *CVkRenderContext::CreateStorageImage( uint32_t x, uint32_t y, EImageFormat eFormat, EMultisampleType eMultisampleType )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
void CVkRenderContext::DestroyBuffer( IBuffer *pBuffer )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void CVkRenderContext::DestroyImage( IImage *pImage )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
CVkRenderContext s_vkRenderContext;
|
||||
IRenderContext *g_pVkRenderContext = &s_vkRenderContext;
|
||||
|
||||
|
||||
uint32_t g_iDrawFamily;
|
||||
uint32_t g_iPresentFamily;
|
||||
|
||||
VkInstance g_vkInstance;
|
||||
VkPhysicalDevice g_vkPhysicalDevice;
|
||||
VkDevice g_vkDevice;
|
||||
VkSwapchainKHR g_vkSwapchain;
|
||||
|
||||
|
||||
void CVkRenderContext::Init()
|
||||
{
|
||||
VkResult r;
|
||||
|
||||
int nExtensionCount;
|
||||
CUtlVector<const char *> extensions = {};
|
||||
|
||||
uint32_t nPhysicalDevicesCount;
|
||||
CUtlVector<VkPhysicalDevice> physicalDevices;
|
||||
|
||||
uint32_t nNumQueueFamilies = 0;
|
||||
CUtlVector<VkQueueFamilyProperties> queueFamilyProperties;
|
||||
|
||||
VkApplicationInfo stApplicationInfo = {};
|
||||
VkInstanceCreateInfo stInstanceCreateInfo = {};
|
||||
|
||||
VkDeviceQueueCreateInfo stDeviceQueueCreateInfo = {};
|
||||
VkDeviceCreateInfo stDeviceCreateInfo = {};
|
||||
|
||||
float fPriority = 1.0;
|
||||
|
||||
r = volkInitialize();
|
||||
VULKAN_RESULT_PRINT(r, volkInitialize)
|
||||
|
||||
// Get extensions required by game window
|
||||
nExtensionCount = gamewindow->GetVulkanInstanceExtensionCount();
|
||||
extensions.Resize(nExtensionCount);
|
||||
V_memcpy(extensions.GetData(), gamewindow->GetVulkanInstanceExtensions(), extensions.GetSize()*sizeof(const char*));
|
||||
|
||||
// Create instance
|
||||
stApplicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
||||
stApplicationInfo.apiVersion = VK_API_VERSION_1_4;
|
||||
stApplicationInfo.pApplicationName = "funny";
|
||||
stApplicationInfo.pEngineName = "funny";
|
||||
|
||||
stInstanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
||||
stInstanceCreateInfo.pApplicationInfo = &stApplicationInfo;
|
||||
stInstanceCreateInfo.enabledExtensionCount = extensions.GetSize();
|
||||
stInstanceCreateInfo.ppEnabledExtensionNames = extensions.GetData();
|
||||
|
||||
r = vkCreateInstance(&stInstanceCreateInfo, NULL, &g_vkInstance);
|
||||
VULKAN_RESULT_PRINT(r, vkCreateInstance)
|
||||
|
||||
// volk requires to load instance this way
|
||||
volkLoadInstance(g_vkInstance);
|
||||
|
||||
|
||||
// Get amount of physical devices
|
||||
r = vkEnumeratePhysicalDevices(g_vkInstance, &nPhysicalDevicesCount, NULL);
|
||||
VULKAN_RESULT_PRINT(r, vkEnumeratePhysicalDevices)
|
||||
physicalDevices.Resize(nPhysicalDevicesCount);
|
||||
|
||||
// Read all physical devices
|
||||
r = vkEnumeratePhysicalDevices(g_vkInstance, &nPhysicalDevicesCount, physicalDevices.GetData());
|
||||
VULKAN_RESULT_PRINT(r, vkEnumeratePhysicalDevices)
|
||||
|
||||
g_vkPhysicalDevice = SelectPhysicalDevice(physicalDevices);
|
||||
|
||||
|
||||
// Get all queues
|
||||
vkGetPhysicalDeviceQueueFamilyProperties(g_vkPhysicalDevice, &nNumQueueFamilies, NULL);
|
||||
queueFamilyProperties.Resize(nNumQueueFamilies);
|
||||
|
||||
uint32_t i = 0;
|
||||
vkGetPhysicalDeviceQueueFamilyProperties(g_vkPhysicalDevice, &nNumQueueFamilies, queueFamilyProperties.GetData());
|
||||
|
||||
for (auto &family: queueFamilyProperties)
|
||||
{
|
||||
if (family.queueFlags & VK_QUEUE_GRAPHICS_BIT)
|
||||
{
|
||||
g_iDrawFamily = i;
|
||||
g_iPresentFamily = i;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
|
||||
// Create device
|
||||
stDeviceQueueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
||||
stDeviceQueueCreateInfo.queueCount = 1;
|
||||
stDeviceQueueCreateInfo.pQueuePriorities = &fPriority;
|
||||
stDeviceQueueCreateInfo.queueFamilyIndex = g_iDrawFamily;
|
||||
|
||||
stDeviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
|
||||
stDeviceCreateInfo.queueCreateInfoCount = 1;
|
||||
stDeviceCreateInfo.pQueueCreateInfos = &stDeviceQueueCreateInfo;
|
||||
vkCreateDevice(g_vkPhysicalDevice, &stDeviceCreateInfo, NULL, &g_vkDevice);
|
||||
|
||||
CreateSwapchain();
|
||||
}
|
||||
|
||||
void CVkRenderContext::Frame( float fDeltaTime )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void CVkRenderContext::CreateSwapchain()
|
||||
{
|
||||
gamewindow->CreateVulkanSurface(g_vkInstance);
|
||||
|
||||
}
|
||||
|
||||
void CVkRenderContext::DestroySwapchain()
|
||||
{
|
||||
vkDestroySwapchainKHR(g_vkDevice, g_vkSwapchain, NULL);
|
||||
gamewindow->DestroyVulkanSurface(g_vkInstance);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// TODO: Move to the rendering context
|
||||
//-----------------------------------------------------------------------------
|
||||
void CVkRenderContext::Shutdown()
|
||||
{
|
||||
vkDestroyInstance(g_vkInstance, NULL);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Selects best device to be used in rendering.
|
||||
// We want to get the best perfomance out of the GPU, so we need to select the
|
||||
// best driver.
|
||||
//-----------------------------------------------------------------------------
|
||||
VkPhysicalDevice CVkRenderContext::SelectPhysicalDevice( CUtlVector<VkPhysicalDevice> physicalDevices )
|
||||
{
|
||||
uint32_t uMaxScore = 0;
|
||||
VkPhysicalDevice selectedDevice = 0;
|
||||
VkResult r;
|
||||
|
||||
for (auto &device: physicalDevices)
|
||||
{
|
||||
uint32_t nExtensionCount;
|
||||
CUtlVector<VkExtensionProperties> extensions;
|
||||
VkPhysicalDeviceFeatures stPhysicalDeviceFeatures;
|
||||
VkPhysicalDeviceProperties stPhysicalDeviceProperties;
|
||||
uint32_t uScore = 0;
|
||||
|
||||
r = vkEnumerateDeviceExtensionProperties(device, NULL, &nExtensionCount, NULL);
|
||||
VULKAN_RESULT_PRINT(r, vkEnumeratePhysicalDevices)
|
||||
extensions.Resize(nExtensionCount);
|
||||
|
||||
r = vkEnumerateDeviceExtensionProperties(device, NULL, &nExtensionCount, extensions.GetData());
|
||||
VULKAN_RESULT_PRINT(r, vkEnumeratePhysicalDevices)
|
||||
|
||||
vkGetPhysicalDeviceProperties(device, &stPhysicalDeviceProperties);
|
||||
vkGetPhysicalDeviceFeatures(device, &stPhysicalDeviceFeatures);
|
||||
|
||||
if (stPhysicalDeviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
|
||||
uScore += 1000;
|
||||
if (stPhysicalDeviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU)
|
||||
uScore += 500;
|
||||
|
||||
if (uScore >= uMaxScore)
|
||||
{
|
||||
selectedDevice = device;
|
||||
uMaxScore = uScore;
|
||||
}
|
||||
}
|
||||
|
||||
return selectedDevice;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
#ifndef VULKAN_STATE_H
|
||||
#define VULKAN_STATE_H
|
||||
|
||||
|
||||
#include "volk.h"
|
||||
#include "vk_mem_alloc.h"
|
||||
#include "tier0/platform.h"
|
||||
#include "iappsystem.h"
|
||||
#include "materialsystem/materialsystem.h"
|
||||
|
||||
#define REQUIRED_EXTENSION(ext) bool bIsSupported_##ext;
|
||||
#define OPTIONAL_EXTENSION(ext) bool bIsSupported_##ext;
|
||||
struct SupportedVulkanExtensions_t
|
||||
{
|
||||
#include "device_extensions.h"
|
||||
};
|
||||
#undef REQUIRED_EXTENSION
|
||||
#undef OPTIONAL_EXTENSION
|
||||
|
||||
enum EDependencyMode
|
||||
{
|
||||
DEPENDENCY_MODE_SHADER_IMAGE,
|
||||
DEPENDENCY_MODE_SHADER_BUFFER,
|
||||
DEPENDENCY_MODE_SHADER_ACCELERATION_STRUCTURE,
|
||||
|
||||
|
||||
DEPENDENCY_MODE_DRAWCALL_VERTEX_BUFFER,
|
||||
DEPENDENCY_MODE_DRAWCALL_INDEX_BUFFER,
|
||||
DEPENDENCY_MODE_DRAWCALL_OUTPUT_IMAGE,
|
||||
};
|
||||
|
||||
abstract_class IVkCommand
|
||||
{
|
||||
public:
|
||||
virtual void Call() = 0;
|
||||
void AddDependency( IRenderingObject *pObject, EDependencyMode eDependencyMode );
|
||||
};
|
||||
|
||||
abstract_class IVkCommandBuffer: public IAppSystem
|
||||
{
|
||||
public:
|
||||
void PushCommand( IVkCommand *pCommand );
|
||||
|
||||
void Submit();
|
||||
};
|
||||
|
||||
extern IVkCommandBuffer *vkcommandbuffer;
|
||||
|
||||
#undef __cplusplus
|
||||
#include "vulkan/vk_enum_string_helper.h"
|
||||
#define __cplusplus
|
||||
|
||||
#define VULKAN_RESULT_PRINT(r, func) \
|
||||
if (r != VK_SUCCESS) \
|
||||
Plat_FatalErrorFunc(#func " failed: %s\n", string_VkResult(r));
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user