From 287102dceab79ac70046767b600052ec58d3dc7a Mon Sep 17 00:00:00 2001 From: kotofyt Date: Sat, 20 Sep 2025 12:39:42 +0300 Subject: [PATCH] Vulkan stuff --- engine/engine.cpp | 2 +- materialsystem/__build.cpp | 1 + materialsystem/materialsystem.cpp | 2 +- materialsystem/vulkan/commandbuffer.cpp | 33 +++++++ materialsystem/vulkan/computeshader.cpp | 0 materialsystem/vulkan/raytracingshader.cpp | 0 materialsystem/vulkan/rendercontext.cpp | 2 +- materialsystem/vulkan/vulkan_state.h | 58 ++++++++++--- public/icvar.h | 2 +- public/igamewindow.h | 2 +- public/materialsystem/icomputeshader.h | 25 ------ public/materialsystem/imaterial.h | 0 .../{materialsystem.h => imaterialsystem.h} | 87 +++++++++++-------- public/materialsystem/ipipeline.h | 10 --- public/materialsystem/irastershader.h | 27 ------ public/materialsystem/iraytracingshader.h | 0 public/materialsystem/ishader.h | 36 -------- public/sv_dll.h | 2 +- public/{ => tier2}/iappsystem.h | 0 19 files changed, 137 insertions(+), 152 deletions(-) delete mode 100644 materialsystem/vulkan/computeshader.cpp delete mode 100644 materialsystem/vulkan/raytracingshader.cpp delete mode 100644 public/materialsystem/icomputeshader.h delete mode 100644 public/materialsystem/imaterial.h rename public/materialsystem/{materialsystem.h => imaterialsystem.h} (69%) delete mode 100644 public/materialsystem/ipipeline.h delete mode 100644 public/materialsystem/irastershader.h delete mode 100644 public/materialsystem/iraytracingshader.h delete mode 100644 public/materialsystem/ishader.h rename public/{ => tier2}/iappsystem.h (100%) diff --git a/engine/engine.cpp b/engine/engine.cpp index 22867e6..4fe3773 100644 --- a/engine/engine.cpp +++ b/engine/engine.cpp @@ -1,7 +1,7 @@ #include "tier2/ifilesystem.h" #include "igamewindow.h" -#include "materialsystem/materialsystem.h" +#include "materialsystem/imaterialsystem.h" #include "tier1/interface.h" #include "tier1/commandline.h" #include "tier0/mem.h" diff --git a/materialsystem/__build.cpp b/materialsystem/__build.cpp index a33d6d0..d5f4027 100644 --- a/materialsystem/__build.cpp +++ b/materialsystem/__build.cpp @@ -8,6 +8,7 @@ CUtlVector MaterialSystem_CompiledFiles = { "materialsystem/materialsystem.cpp", "materialsystem/vulkan/rendercontext.cpp", "materialsystem/vulkan/material.cpp", + "materialsystem/vulkan/commandbuffer.cpp", "external/volk/volk.c", }; CUtlString material_lib; diff --git a/materialsystem/materialsystem.cpp b/materialsystem/materialsystem.cpp index 5cdf297..8900fc7 100644 --- a/materialsystem/materialsystem.cpp +++ b/materialsystem/materialsystem.cpp @@ -1,4 +1,4 @@ -#include "materialsystem/materialsystem.h" +#include "materialsystem/imaterialsystem.h" class CMaterialSystem: public IMaterialSystem { diff --git a/materialsystem/vulkan/commandbuffer.cpp b/materialsystem/vulkan/commandbuffer.cpp index e69de29..809ede1 100644 --- a/materialsystem/vulkan/commandbuffer.cpp +++ b/materialsystem/vulkan/commandbuffer.cpp @@ -0,0 +1,33 @@ +#include "tier1/utlvector.h" +#include "vulkan_state.h" + +class CVkCommandBuffer: public IVkCommandBuffer +{ +public: + virtual void Init() override; + virtual void Shutdown() override; + virtual void AddCommand( EVulkanCommandType eType, CVkCommand *pCommand ) override; + virtual void Submit() override; +private: + CUtlSelfReferencingVector g_commands; +}; + +void CVkCommandBuffer::AddCommand( EVulkanCommandType eType, CVkCommand *pCommand ) +{ + g_commands.AppendTail(pCommand); +} + +void CVkCommandBuffer::Submit() +{ + for (auto &pCommand: g_commands) + { + pCommand->Execute(); + delete pCommand; + }; +}; + +static CVkCommandBuffer s_vkCommandBuffer; +IVkCommandBuffer *VulkanCommandBuffer() +{ + return &s_vkCommandBuffer; +} diff --git a/materialsystem/vulkan/computeshader.cpp b/materialsystem/vulkan/computeshader.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/materialsystem/vulkan/raytracingshader.cpp b/materialsystem/vulkan/raytracingshader.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/materialsystem/vulkan/rendercontext.cpp b/materialsystem/vulkan/rendercontext.cpp index 57f3889..163edf1 100644 --- a/materialsystem/vulkan/rendercontext.cpp +++ b/materialsystem/vulkan/rendercontext.cpp @@ -1,5 +1,5 @@ #include "SDL3/SDL_vulkan.h" -#include "materialsystem/materialsystem.h" +#include "materialsystem/imaterialsystem.h" #include "tier0/lib.h" #include "tier0/platform.h" #include "tier1/utlstring.h" diff --git a/materialsystem/vulkan/vulkan_state.h b/materialsystem/vulkan/vulkan_state.h index eb4286b..f1f308f 100644 --- a/materialsystem/vulkan/vulkan_state.h +++ b/materialsystem/vulkan/vulkan_state.h @@ -5,8 +5,9 @@ #include "volk.h" #include "vk_mem_alloc.h" #include "tier0/platform.h" -#include "iappsystem.h" -#include "materialsystem/materialsystem.h" +#include "tier1/utlvector.h" +#include "tier2/iappsystem.h" +#include "materialsystem/imaterialsystem.h" #define REQUIRED_EXTENSION(ext) bool bIsSupported_##ext; #define OPTIONAL_EXTENSION(ext) bool bIsSupported_##ext; @@ -17,38 +18,73 @@ extern struct SupportedVulkanExtensions_t #undef REQUIRED_EXTENSION #undef OPTIONAL_EXTENSION +enum EVulkanCommandType +{ + COMMAND_TYPE_GENERAL, + + COMMAND_TYPE_BEGIN, + COMMAND_TYPE_DRAW, + COMMAND_TYPE_END, +}; + enum EDependencyMode { - DEPENDENCY_MODE_SHADER_IMAGE, - DEPENDENCY_MODE_SHADER_BUFFER, + DEPENDENCY_MODE_SHADER_IMAGE_READ, + DEPENDENCY_MODE_SHADER_BUFFER_READ, + DEPENDENCY_MODE_SHADER_IMAGE_WRITE, + DEPENDENCY_MODE_SHADER_BUFFER_WRITE, DEPENDENCY_MODE_SHADER_ACCELERATION_STRUCTURE, - DEPENDENCY_MODE_DRAWCALL_VERTEX_BUFFER, DEPENDENCY_MODE_DRAWCALL_INDEX_BUFFER, DEPENDENCY_MODE_DRAWCALL_OUTPUT_IMAGE, + + DEPENDENCY_MODE_IMAGE_SOURCE, + DEPENDENCY_MODE_IMAGE_DESTINATION, }; -abstract_class IVkCommand +#define EXPOSE_VULKAN_COMMAND( name ) \ +class CVulkanCommand_##name; \ +CVulkanCommand_##name *name(); \ + +#define DECLARE_VULKAN_COMMAND( name ) \ +class CVulkanCommand_##name \ +{ \ +public: \ + virtual void Execute() override; \ +}; \ +CVulkanCommand_##name *name(); \ +{ \ + return new CVulkanCommand_##name \ +} \ +void CVulkanCommand_##name::Execute() \ + +abstract_class CVkCommand { public: - virtual void Call() = 0; void AddDependency( IRenderingObject *pObject, EDependencyMode eDependencyMode ); + virtual void Execute(); + + struct VulkanCommandDepenency_t { + IRenderingObject *m_pObject; + EDependencyMode m_eDependencyMode; + }; + CUtlVector m_depedencies; }; abstract_class IVkCommandBuffer: public IAppSystem { public: - void PushCommand( IVkCommand *pCommand ); + virtual void AddCommand( EVulkanCommandType eType, CVkCommand * ) = 0; - void Submit(); + virtual void Submit() = 0; }; -extern IVkCommandBuffer *vkcommandbuffer; +IVkCommandBuffer *VulkanCommandBuffer(); #undef __cplusplus #include "vulkan/vk_enum_string_helper.h" -#define __cplusplus +#define __cplusplus 202400L #define VULKAN_RESULT_PRINT(r, func) \ if (r != VK_SUCCESS) \ diff --git a/public/icvar.h b/public/icvar.h index 51fe502..c2c9960 100644 --- a/public/icvar.h +++ b/public/icvar.h @@ -1,7 +1,7 @@ #ifndef CONSOLE_H #define CONSOLE_H -#include "iappsystem.h" +#include "tier2/iappsystem.h" #include "tier0/platform.h" #include "tier1/utlstring.h" #include "tier1/utlvector.h" diff --git a/public/igamewindow.h b/public/igamewindow.h index 885cd75..9c16b85 100644 --- a/public/igamewindow.h +++ b/public/igamewindow.h @@ -1,7 +1,7 @@ #ifndef GAME_WINDOW_H #define GAME_WINDOW_H -#include "iappsystem.h" +#include "tier2/iappsystem.h" #include "tier0/platform.h" enum EGraphicsAPI diff --git a/public/materialsystem/icomputeshader.h b/public/materialsystem/icomputeshader.h deleted file mode 100644 index 28b7817..0000000 --- a/public/materialsystem/icomputeshader.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef MATERIAL_COMPUTE_SHADER_H -#define MATERIAL_COMPUTE_SHADER_H - -#include "ishader.h" - -enum EShaderOutput { - SHADER_TEXTURE0, - SHADER_TEXTURE1, - SHADER_TEXTURE2, - SHADER_TEXTURE3, - SHADER_TEXTURE4, - SHADER_TEXTURE5, - SHADER_TEXTURE6, - SHADER_TEXTURE7, - - SHADER_DEPTH, -}; - -abstract_class IComputeShader : public IShader -{ -public: - virtual void SetShaderSource( const char *szPath ) = 0; -}; - -#endif diff --git a/public/materialsystem/imaterial.h b/public/materialsystem/imaterial.h deleted file mode 100644 index e69de29..0000000 diff --git a/public/materialsystem/materialsystem.h b/public/materialsystem/imaterialsystem.h similarity index 69% rename from public/materialsystem/materialsystem.h rename to public/materialsystem/imaterialsystem.h index 7988d2e..13e79c2 100644 --- a/public/materialsystem/materialsystem.h +++ b/public/materialsystem/imaterialsystem.h @@ -1,44 +1,9 @@ #ifndef MATERIAL_SYSTEM_H #define MATERIAL_SYSTEM_H -#include "iappsystem.h" +#include "tier2/iappsystem.h" #include "tier0/platform.h" - -abstract_class IRenderingObject -{ -public: - virtual void SetDebugName( const char *szName ) = 0; -}; - -abstract_class IBuffer : public IRenderingObject -{ -public: - virtual void Lock() = 0; - virtual void Unlock() = 0; - virtual void *Map() = 0; - virtual void Unmap() = 0; - - virtual uint32_t GetSize() = 0; -}; - -abstract_class IVertexBuffer : public IBuffer -{ -public: -}; - -abstract_class IIndexBuffer : public IBuffer -{ -public: -}; - -abstract_class IMesh -{ -public: -}; - - - enum EImageFormat { IMAGE_FORMAT_R8_UINT, @@ -73,6 +38,26 @@ enum EMultisampleType MULTISAMPLE_TYPE_8_SAMPLES, }; +abstract_class IRenderingObject +{ +public: + virtual void SetDebugName( const char *szName ) = 0; +}; + +abstract_class IBuffer : public IRenderingObject +{ +public: + virtual void Lock() = 0; + virtual void Unlock() = 0; + virtual void *Map() = 0; + virtual void Unmap() = 0; + + virtual uint32_t GetSize() = 0; +}; + +typedef IBuffer IVertexBuffer; +typedef IBuffer IIndexBuffer; + abstract_class IImage : public IRenderingObject { public: @@ -81,6 +66,21 @@ public: virtual EImageFormat GetImageFormat() = 0; virtual EMultisampleType GetMultisampleType() = 0; }; + +abstract_class IShader +{ +public: +}; + +abstract_class IMaterial +{ +public: + virtual void SetConstants( void *pData ) = 0; + + virtual void SetTexture( const char *szName, IImage *pImage ) = 0; + virtual void SetBuffer( const char *szName, IImage *pImage ) = 0; +}; + abstract_class IRenderContext: public IAppSystem { public: @@ -93,14 +93,27 @@ public: virtual void DestroyBuffer( IBuffer *pBuffer ) = 0; virtual void DestroyImage( IImage *pImage ) = 0; + + virtual IShader *CreateShader( const char *szName ); + virtual void DestroyShader( IShader *pMaterial ) = 0; + + virtual IMaterial *CreateMaterial( IShader *pShader ) = 0; + virtual void DestroyMaterial( IMaterial *pMaterial ) = 0; + + virtual void SetMaterial( IMaterial *pMaterial ) = 0; + virtual void SetVertexBuffer( IVertexBuffer *pBuffer ) = 0; + virtual void SetIndexBuffer( IVertexBuffer *pBuffer ) = 0; + virtual void DrawPrimitives( ) = 0; + virtual void DrawPrimitivesIndexed( ) = 0; }; + abstract_class IMaterialSystem: public IAppSystem { public: virtual void Frame( float fTime ) = 0; - virtual IRenderContext *GetRenderContext( void) = 0; + virtual IRenderContext *GetRenderContext( void ) = 0; }; IMaterialSystem *Materials( void ); diff --git a/public/materialsystem/ipipeline.h b/public/materialsystem/ipipeline.h deleted file mode 100644 index a427426..0000000 --- a/public/materialsystem/ipipeline.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef MATERIAL_PIPELINE_H -#define MATERIAL_PIPELINE_H - -#include "tier0/platform.h" -abstract_class IPipeline -{ -public: -}; - -#endif diff --git a/public/materialsystem/irastershader.h b/public/materialsystem/irastershader.h deleted file mode 100644 index fc12401..0000000 --- a/public/materialsystem/irastershader.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef MATERIAL_RASTER_SHADER_H -#define MATERIAL_RASTER_SHADER_H - -#include "ishader.h" - -enum EShaderOutput { - SHADER_TEXTURE0, - SHADER_TEXTURE1, - SHADER_TEXTURE2, - SHADER_TEXTURE3, - SHADER_TEXTURE4, - SHADER_TEXTURE5, - SHADER_TEXTURE6, - SHADER_TEXTURE7, - - SHADER_DEPTH, -}; - -abstract_class IRasterShader : public IShader -{ -public: - virtual void SetVertexShader( const char *szPath ) = 0; - virtual void SetPixelShader( const char *szPath ) = 0; - virtual void EnableTexture( EShaderOutput eTextureID, bool bEnabled ) = 0; -}; - -#endif diff --git a/public/materialsystem/iraytracingshader.h b/public/materialsystem/iraytracingshader.h deleted file mode 100644 index e69de29..0000000 diff --git a/public/materialsystem/ishader.h b/public/materialsystem/ishader.h deleted file mode 100644 index 641508c..0000000 --- a/public/materialsystem/ishader.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef MATERIAL_SHADER_H -#define MATERIAL_SHADER_H - -#include "tier0/platform.h" - -enum EShaderInputType -{ - SHADER_INPUT_TYPE_POINTER, - - SHADER_INPUT_TYPE_INT, - - SHADER_INPUT_TYPE_FLOAT, - SHADER_INPUT_TYPE_FLOAT2, - SHADER_INPUT_TYPE_FLOAT3, - SHADER_INPUT_TYPE_FLOAT4, - - SHADER_INPUT_TYPE_MATRIX, - - SHADER_INPUT_TYPE_IMAGE, - SHADER_INPUT_TYPE_TEXTURE, - - SHADER_INPUT_TYPE_MODEL, -}; - -abstract_class IShader -{ -public: - virtual void CreateShaderParameter( const char *szName, uint32_t binding, EShaderInputType eType, bool bArray ) = 0; - virtual void CreateMaterialParameter( const char *szName, uint32_t binding, EShaderInputType eType, bool bArray ) = 0; - virtual void CreateConstants( uint32_t nSize ) = 0; - - virtual void SetShaderParameter( const char *szName, void *pData ) = 0; -}; - - -#endif diff --git a/public/sv_dll.h b/public/sv_dll.h index defd13a..4641dae 100644 --- a/public/sv_dll.h +++ b/public/sv_dll.h @@ -1,7 +1,7 @@ #ifndef SV_DLL_H #define SV_DLL_H -#include "iappsystem.h" +#include "tier2/iappsystem.h" #define SERVER_DLL_INTERFACE_NAME "ServerGameDLL001" diff --git a/public/iappsystem.h b/public/tier2/iappsystem.h similarity index 100% rename from public/iappsystem.h rename to public/tier2/iappsystem.h