diff --git a/build.cpp b/build.cpp index f62a852..fb172e3 100755 --- a/build.cpp +++ b/build.cpp @@ -27,6 +27,7 @@ DECLARE_BUILD_STAGE(install_game) filesystem2->CopyFile(szOutputDir, GET_PROJECT_OBJECT(shadercompiler, "fs")); filesystem2->CopyFile(szOutputDir, GET_PROJECT_OBJECT(Server, "server")); filesystem2->CopyFile(szOutputDir, GET_PROJECT_OBJECT(Client, "client")); + filesystem2->CopyDirectory(CUtlString("%s/core",szOutputDir.GetString()), "funnyassets/meshes"); filesystem2->CopyDirectory(CUtlString("%s/core",szOutputDir.GetString()), "build/funnygame/assets/shaders"); if (Target_t::DefaultTarget().kernel == TARGET_KERNEL_WINDOWS) { diff --git a/engine/cl_dll.cpp b/engine/cl_dll.cpp index 58dc8e4..bc7eeb0 100644 --- a/engine/cl_dll.cpp +++ b/engine/cl_dll.cpp @@ -2,6 +2,7 @@ #include "tier1/interface.h" #include "tier0/platform.h" #include "icvar.h" +#include "tier2/ifilesystem.h" void CClientGameDLL::Init() { @@ -19,6 +20,7 @@ void CClientGameDLL::Init() pEngineBridge->ConnectInterface(RENDER_CONTEXT_INTERFACE_VERSION, m_pRenderContext); pEngineBridge->ConnectInterface("MainWindow", m_pGameWindow); + pEngineBridge->ConnectInterface(FILESYSTEM_INTERFACE_VERSION, filesystem); pEngineBridge->Init(); m_pBridge = pEngineBridge; } diff --git a/engine/engine.cpp b/engine/engine.cpp index 98ee308..453928c 100644 --- a/engine/engine.cpp +++ b/engine/engine.cpp @@ -53,12 +53,16 @@ extern "C" void FunnyMain( int argc, char **argv ) g_pClientGame->Init(); + double fPrevious = Plat_GetTime(); for (;;) { - g_pWindowManager->Frame(0); - g_pServerGame->m_pBridge->Frame(0); - g_pClientGame->m_pBridge->Frame(0); + double fCurrent = Plat_GetTime(); + double fDelta = fCurrent-fPrevious; + fPrevious = fCurrent; + g_pWindowManager->Frame(fDelta); + g_pServerGame->m_pBridge->Frame(fDelta); + g_pClientGame->m_pBridge->Frame(fDelta); - g_pRenderContext->Frame(0); + g_pRenderContext->Frame(fDelta); }; }; diff --git a/funnyassets/meshes/cube.fmesh_c b/funnyassets/meshes/cube.fmesh_c index bb8348e..1be759f 100644 Binary files a/funnyassets/meshes/cube.fmesh_c and b/funnyassets/meshes/cube.fmesh_c differ diff --git a/funnyassets/shaders/brdf.hlsl b/funnyassets/shaders/brdf.hlsl index ec76d0b..a21f4ae 100644 --- a/funnyassets/shaders/brdf.hlsl +++ b/funnyassets/shaders/brdf.hlsl @@ -3,7 +3,7 @@ float GetSpecularThreshold( float m_fMetalness ) return 0; } -struct BRDF_Data +struct BRDF_t { float3 m_vRayIn; float3 m_vRayOut; @@ -23,7 +23,7 @@ struct BRDF_Data float fSineNormalView = saturate(dot(m_vNormal, m_vRayIn)); float fFresnelLight = FresnelDiffuseTerm(fSineNormalLight); float fFresnelView = FresnelDiffuseTerm(fSineNormalView); - return fFresnelView * fFresnelLight; + return saturate(dot(m_vNormal,m_vRayOut)); } float3 GetOutGoingDirection() diff --git a/funnyassets/shaders/fgui/text.shader b/funnyassets/shaders/fgui/text.shader deleted file mode 100644 index 43ea659..0000000 --- a/funnyassets/shaders/fgui/text.shader +++ /dev/null @@ -1,12 +0,0 @@ -#include "../macros.hlsl" -COMMON -{ - -} -VS -{ - -} -PS -{ -} diff --git a/funnyassets/shaders/mesh_raster.shader b/funnyassets/shaders/mesh_raster.shader index dad0a2e..9d3acf0 100644 --- a/funnyassets/shaders/mesh_raster.shader +++ b/funnyassets/shaders/mesh_raster.shader @@ -4,16 +4,20 @@ COMMON { cbuffer CameraInfo { float4x4 g_matViewProjection; + float4 g_vViewPosition; }; struct PerModelData { float4x4 m_matTranslation; + float4x4 m_matRotation; } StructuredBuffer g_modelData; struct PS_INPUT { float4 m_vScreenPosition: SV_POSITION; float4 m_vWorldPosition: POSITION; + float4 m_vTexCoord: TEXCOORD0; + float4 m_vNormal: NORMAL0; } } VS @@ -22,6 +26,8 @@ VS struct VS_INPUT { float3 m_vPosition: POSITION; + float2 m_vTexCoord: TEXCOORD0; + float3 m_vNormal: NORMAL0; } PS_INPUT vsMain( VS_INPUT input ) @@ -36,7 +42,15 @@ VS output.m_vScreenPosition, g_matViewProjection ); - output.m_vWorldPosition = float4(input.m_vPosition, 1); + output.m_vWorldPosition = mul( + float4(input.m_vPosition, 1), + g_modelData[0].m_matTranslation + ); + output.m_vNormal = mul( + float4(input.m_vNormal, 1), + g_modelData[0].m_matRotation + ); + output.m_vTexCoord = float4(input.m_vTexCoord, 0, 0); return output; } } @@ -47,8 +61,15 @@ PS float4 psMain( PS_INPUT input ) { - float4 vWorldSpacePosition = input.m_vWorldPosition; - return float4(vWorldSpacePosition); + float4 vWorldSpacePosition = input.m_vNormal; + BRDF_t brdf = {}; + brdf.m_vRayIn = normalize(g_vViewPosition.xyz-input.m_vWorldPosition.xyz); + brdf.m_vRayOut = normalize(-input.m_vWorldPosition.xyz); + brdf.m_vNormal = input.m_vNormal.xyz; + brdf.m_fRoughness = 1; + brdf.m_fMetalness = 1; + + return float4(brdf.BurleyDiffuse(), 1); } } diff --git a/game/client/game.cpp b/game/client/game.cpp index 72fa035..12c0fcd 100644 --- a/game/client/game.cpp +++ b/game/client/game.cpp @@ -16,6 +16,8 @@ class CFunnyGameBridge: public IEngineBridge virtual void Frame( float fDelta ) override; virtual void Shutdown() override; virtual void ConnectInterface( const char *psz, void *pInterface ) override; + IMeshInstance *m_pMeshInstance; + float m_fTime = 0; }; IEngineBridge *EngineBridge() @@ -35,26 +37,17 @@ void CFunnyGameBridge::Init() g_pWorldRenderer->Init(); pShader = g_pRenderContext->CreateShader("game/core/shaders/mesh_raster.shader_c"); - float cubeVertices[][3] = { - {-1, -1, 1}, { 1, -1, 1}, { 1, 1, 1}, - {-1, -1, 1}, { 1, 1, 1}, {-1, 1, 1}, - {-1, -1, -1}, {-1, 1, -1}, { 1, 1, -1}, - {-1, -1, -1}, { 1, 1, -1}, { 1, -1, -1}, - {-1, -1, -1}, {-1, -1, 1}, {-1, 1, 1}, - {-1, -1, -1}, {-1, 1, 1}, {-1, 1, -1}, - { 1, -1, -1}, { 1, 1, -1}, { 1, 1, 1}, - { 1, -1, -1}, { 1, 1, 1}, { 1, -1, 1}, - {-1, 1, -1}, {-1, 1, 1}, { 1, 1, 1}, - {-1, 1, -1}, { 1, 1, 1}, { 1, 1, -1}, - {-1, -1, -1}, { 1, -1, -1}, { 1, -1, 1}, - {-1, -1, -1}, { 1, -1, 1}, {-1, -1, 1} - }; - pBuffer = g_pRenderContext->CreateVertexBuffer(sizeof(cubeVertices)); + IFileHandle *pHandle = filesystem->Open("game/core/meshes/cube.fmesh_c", FILEMODE_READ); + float *pData = (float*)V_malloc(filesystem->Size(pHandle)); + filesystem->Read(pHandle, pData, filesystem->Size(pHandle)); + pBuffer = g_pRenderContext->CreateVertexBuffer(filesystem->Size(pHandle)); pBuffer->Lock(); void *pMapped = pBuffer->Map(); - V_memcpy(pMapped, cubeVertices, sizeof(cubeVertices)); + V_memcpy(pMapped, pData, filesystem->Size(pHandle)); pBuffer->Unmap(); pBuffer->Unlock(); + V_free(pData); + filesystem->Close(pHandle); IMesh *pMesh = g_pWorldRenderer->CreateMesh("Triangle"); @@ -67,8 +60,8 @@ void CFunnyGameBridge::Init() pMesh->SetVertices(pBuffer); pMesh->SetMaterial(pMaterial); - IMeshInstance *pMeshInstance = g_pWorldRenderer->CreateInstance(pMesh); - pMeshInstance->SetPosition({1,1,1}); + m_pMeshInstance = g_pWorldRenderer->CreateInstance(pMesh); + m_pMeshInstance->SetScale({5, 5, 5}); } void CFunnyGameBridge::Tick( float fDelta ) @@ -77,6 +70,11 @@ void CFunnyGameBridge::Tick( float fDelta ) void CFunnyGameBridge::Frame( float fDelta ) { + versor q; + m_fTime += fDelta; + glm_euler_zyx_quat((vec3){0,m_fTime, 0}, q); + m_pMeshInstance->SetPosition({sin(m_fTime/3)*10,0,0}); + m_pMeshInstance->SetRotation({q[0], q[1], q[2], q[3]}); g_pWorldRenderer->Frame(fDelta); } @@ -89,6 +87,7 @@ void CFunnyGameBridge::Shutdown() void CFunnyGameBridge::ConnectInterface( const char *psz, void *pInterface ) { CONNECT_INTERFACE(RENDER_CONTEXT_INTERFACE_VERSION, g_pRenderContext); + CONNECT_INTERFACE(FILESYSTEM_INTERFACE_VERSION, filesystem); CONNECT_INTERFACE("MainWindow", g_pMainWindow); } diff --git a/game/client/worldrender.cpp b/game/client/worldrender.cpp index 54238c4..a0c0523 100644 --- a/game/client/worldrender.cpp +++ b/game/client/worldrender.cpp @@ -9,11 +9,13 @@ struct ViewBuffer_t { mat4 m_matCameraProjection; + vec4 m_vCameraPosition; }; struct PerMeshData_t { mat4 m_matTranslation; + mat4 m_matRotation; }; class CFunnyMeshInstance; @@ -35,11 +37,15 @@ public: void CFunnyMesh::ConfigureShader( IShader *pShader ) { - pShader->AddLayout(0, 12); + pShader->AddLayout(0, 32); pShader->AddAttribute(0, 0, VERTEX_FORMAT_XYZ32_SFLOAT, 0); - // color + pShader->AddAttribute(0, 1, VERTEX_FORMAT_XY32_SFLOAT, 12); + pShader->AddAttribute(0, 2, VERTEX_FORMAT_XYZ32_SFLOAT, 20); + // albedo pShader->AddOutputImage(0, IMAGE_FORMAT_RGBA8_UNORM); + pShader->SetMultisampling(MULTISAMPLE_TYPE_1_SAMPLES); + pShader->SetDepthImage(IMAGE_FORMAT_D32_SFLOAT); } @@ -70,7 +76,7 @@ public: PerMeshData_t m_data = {}; Quat m_vRotation = {}; Vector m_vPosition = {}; - Vector m_vScale = {}; + Vector m_vScale = { 1, 1, 1 }; }; @@ -91,7 +97,27 @@ void CFunnyMeshInstance::SetScale( Vector vScale ) } void CFunnyMeshInstance::Frame() { + vec3 v; + versor q; + mat4 m; glm_mat4_identity(m_data.m_matTranslation); + glm_mat4_identity(m_data.m_matRotation); + v[0] = m_vPosition.x; + v[1] = m_vPosition.y; + v[2] = m_vPosition.z; + glm_translate(m_data.m_matTranslation, v); + q[0] = m_vRotation.x; + q[1] = m_vRotation.y; + q[2] = m_vRotation.z; + q[3] = m_vRotation.w; + glm_quat_mat4(q, m); + glm_mat4_mul(m_data.m_matTranslation, m, m_data.m_matTranslation); + glm_mat4_mul(m_data.m_matRotation, m, m_data.m_matRotation); + v[0] = m_vScale.x; + v[1] = m_vScale.y; + v[2] = m_vScale.z; + glm_scale_make(m, v); + glm_mat4_mul(m_data.m_matTranslation, m, m_data.m_matTranslation); } @@ -164,7 +190,8 @@ void CFunnyWorldRenderer::Frame( float fDelta ) */ m_pViewBufferData = (ViewBuffer_t*)m_pViewBuffer->Map(); m_pViewBuffer->Lock(); - V_memcpy(m_pViewBufferData, matCamera, sizeof(matCamera)); + V_memcpy(&m_pViewBufferData->m_matCameraProjection, matCamera, sizeof(matCamera)); + V_memcpy(&m_pViewBufferData->m_vCameraPosition, m_vPos, sizeof(vec3)); m_pViewBuffer->Unlock(); m_pViewBuffer->Unmap(); @@ -222,7 +249,7 @@ void CFunnyWorldRenderer::Frame( float fDelta ) continue; m_pRasterCommandList->SetMaterial(mesh->m_pMaterial); m_pRasterCommandList->SetVertexBuffer(0, mesh->m_pVertexBuffer); - m_pRasterCommandList->DrawPrimitives(mesh->m_pVertexBuffer->GetSize()/12, 0, mesh->m_instances.GetSize(), 0); + m_pRasterCommandList->DrawPrimitives(mesh->m_pVertexBuffer->GetSize()/32, 0, mesh->m_instances.GetSize(), 0); } m_pRasterCommandList->EndRecording(); diff --git a/materialsystem/compiledshader.cpp b/materialsystem/compiledshader.cpp index def163a..6c2759c 100644 --- a/materialsystem/compiledshader.cpp +++ b/materialsystem/compiledshader.cpp @@ -125,7 +125,6 @@ void CCompiledShaderManager::ReadFromFile( CCompiledShader *pShader, const char } pFile = filesystem->Open(szFile, FILEMODE_READ); uint32_t u = filesystem->Read(pFile, &stHeader, sizeof(ShaderHeader_t)); - V_printf("Read %s %s\n", szFile, Plat_GetWorkingDir()); objects.Resize(stHeader.m_nNumShaders); lumps.Resize(stHeader.m_nNumLumps); diff --git a/materialsystem/vulkan/commandbuffer.cpp b/materialsystem/vulkan/commandbuffer.cpp index 784a67a..ef9e158 100644 --- a/materialsystem/vulkan/commandbuffer.cpp +++ b/materialsystem/vulkan/commandbuffer.cpp @@ -324,9 +324,18 @@ void CVkCommandBuffer::TryBarrier( int iCurrent, int iCurrentBuffer ) imageMemoryBarrier.dstAccessMask = VulkanGetAccessFlags(barrier.m_eNewDependency); if (barrier.m_eOldDependency == DEPENDENCY_MODE_NEXT_STAGE) { - imageMemoryBarrier.oldLayout = pImage->m_ePreferredLayout; - imageMemoryBarrier.srcStageMask = VulkanGetStageFlags(DEPENDENCY_MODE_NEXT_STAGE); - imageMemoryBarrier.srcAccessMask = VulkanGetAccessFlags(DEPENDENCY_MODE_NEXT_STAGE); + if (pImage->m_eLastUsage == DEPENDENCY_MODE_JUST_CREATED) + { + imageMemoryBarrier.oldLayout = VulkanGetImageLayout(DEPENDENCY_MODE_JUST_CREATED); + imageMemoryBarrier.srcStageMask = VulkanGetStageFlags(DEPENDENCY_MODE_JUST_CREATED); + imageMemoryBarrier.srcAccessMask = VulkanGetAccessFlags(DEPENDENCY_MODE_JUST_CREATED); + } + else + { + imageMemoryBarrier.oldLayout = pImage->m_ePreferredLayout; + imageMemoryBarrier.srcStageMask = VulkanGetStageFlags(DEPENDENCY_MODE_NEXT_STAGE); + imageMemoryBarrier.srcAccessMask = VulkanGetAccessFlags(DEPENDENCY_MODE_NEXT_STAGE); + } } else { imageMemoryBarrier.oldLayout = VulkanGetImageLayout(barrier.m_eOldDependency); imageMemoryBarrier.srcStageMask = VulkanGetStageFlags(barrier.m_eOldDependency); diff --git a/materialsystem/vulkan/rendercontext.cpp b/materialsystem/vulkan/rendercontext.cpp index 7a9f62f..ffe7953 100644 --- a/materialsystem/vulkan/rendercontext.cpp +++ b/materialsystem/vulkan/rendercontext.cpp @@ -120,6 +120,21 @@ VkFormat CVkImage::GetImageFormat( enum EImageFormat eImageFormat ) } } +VkSampleCountFlagBits CVkImage::GetMultisampling( enum EMultisampleType eImageFormat ) +{ + switch ( eImageFormat ) + { + case MULTISAMPLE_TYPE_1_SAMPLES: + return VK_SAMPLE_COUNT_1_BIT; + case MULTISAMPLE_TYPE_2_SAMPLES: + return VK_SAMPLE_COUNT_2_BIT; + case MULTISAMPLE_TYPE_4_SAMPLES: + return VK_SAMPLE_COUNT_4_BIT; + case MULTISAMPLE_TYPE_8_SAMPLES: + return VK_SAMPLE_COUNT_8_BIT; + } +} + void CVkImage::CreateImage( uint32_t nWidth, uint32_t nHeight, EImageFormat eFormat, EMultisampleType eMultisampleType, VkImageUsageFlagBits eUsage ) { VkImageCreateInfo stCreateInfo = {}; @@ -135,7 +150,7 @@ void CVkImage::CreateImage( uint32_t nWidth, uint32_t nHeight, EImageFormat eFor stCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL; stCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; stCreateInfo.format = GetImageFormat(eFormat); - stCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT; + stCreateInfo.samples = GetMultisampling(eMultisampleType); stAlloc.usage = VMA_MEMORY_USAGE_AUTO; diff --git a/materialsystem/vulkan/shader.cpp b/materialsystem/vulkan/shader.cpp index 2871d8f..e69cb1f 100644 --- a/materialsystem/vulkan/shader.cpp +++ b/materialsystem/vulkan/shader.cpp @@ -39,6 +39,10 @@ void CVkShader::SetDepthImage( EImageFormat eFormat ) { m_eDepthFormat = CVkImage::GetImageFormat(eFormat); } +void CVkShader::SetMultisampling( EMultisampleType eFormat ) +{ + m_eMultiSampling = eFormat; +} void CVkShader::DisablePixelShader( bool bDisable ) { @@ -143,7 +147,8 @@ void CVkShader::Build() viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; msaa.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; - msaa.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; + msaa.rasterizationSamples = CVkImage::GetMultisampling(m_eMultiSampling); + render.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO; render.colorAttachmentCount = m_eFormats.GetSize(); diff --git a/materialsystem/vulkan/vulkan_state.h b/materialsystem/vulkan/vulkan_state.h index da518d8..3b248e2 100644 --- a/materialsystem/vulkan/vulkan_state.h +++ b/materialsystem/vulkan/vulkan_state.h @@ -183,6 +183,7 @@ public: static VkImageViewType GetImageViewType( enum EImageType eImageType ); static VkFormat GetImageFormat( enum EImageFormat eImageFormat ); + static VkSampleCountFlagBits GetMultisampling( enum EMultisampleType eImageFormat ); uint32_t m_nWidth; uint32_t m_nHeight; @@ -265,6 +266,7 @@ public: virtual void SetTopology( ETopologyMode eTopology ) override; virtual void AddOutputImage( int iImageIndex, EImageFormat eFormat ) override; virtual void SetDepthImage( EImageFormat eFormat ) override; + virtual void SetMultisampling( EMultisampleType eFormat ) override; virtual void DisablePixelShader( bool bDisable) override; virtual void Build() override; @@ -279,6 +281,7 @@ private: CUtlVector m_layouts; CUtlVector m_attributes; CUtlVector m_eFormats; + EMultisampleType m_eMultiSampling; VkFormat m_eDepthFormat; bool m_bIsFragmentEnabled; diff --git a/perf.data b/perf.data new file mode 100644 index 0000000..abd493c Binary files /dev/null and b/perf.data differ diff --git a/public/materialsystem/imaterialsystem.h b/public/materialsystem/imaterialsystem.h index f72784c..352ff5c 100644 --- a/public/materialsystem/imaterialsystem.h +++ b/public/materialsystem/imaterialsystem.h @@ -54,6 +54,7 @@ enum EImageType enum EMultisampleType { MULTISAMPLE_TYPE_NONE, + MULTISAMPLE_TYPE_1_SAMPLES = MULTISAMPLE_TYPE_NONE, MULTISAMPLE_TYPE_2_SAMPLES, MULTISAMPLE_TYPE_4_SAMPLES, MULTISAMPLE_TYPE_8_SAMPLES, @@ -145,6 +146,7 @@ public: virtual void SetTopology( ETopologyMode eTopology ) = 0; virtual void AddOutputImage( int iImageIndex, EImageFormat eFormat ) = 0; virtual void SetDepthImage( EImageFormat eFormat ) = 0; + virtual void SetMultisampling( EMultisampleType eFormat ) = 0; virtual void DisablePixelShader( bool bDisable) = 0; virtual void Build() = 0; }; diff --git a/tools/blender_funnyasset.py b/tools/blender_funnyasset.py index fbb4f5c..b421aa4 100644 --- a/tools/blender_funnyasset.py +++ b/tools/blender_funnyasset.py @@ -42,21 +42,20 @@ def export_my_format(filepath): for loop_index in tri.loops: vert = mesh.vertices[mesh.loops[loop_index].vertex_index] world_pos = obj.matrix_world @ vert.co - print(world_pos.x) + world_normal = obj.matrix_world @ vert.normal f.write(struct.pack('f', world_pos.x)) - print(world_pos.y) f.write(struct.pack('f', world_pos.y)) - print(world_pos.z) f.write(struct.pack('f', world_pos.z)) if uv_layer: uv = uv_layer[loop_index].uv - print(uv.x) f.write(struct.pack('f', uv.x)) - print(uv.y) f.write(struct.pack('f', uv.y)) else: f.write(struct.pack('f', 0)) f.write(struct.pack('f', 0)) + f.write(struct.pack('f', world_normal.x)) + f.write(struct.pack('f', world_normal.y)) + f.write(struct.pack('f', world_normal.z)) return {'FINISHED'} diff --git a/tools/cube.fmesh_c b/tools/cube.fmesh_c new file mode 100644 index 0000000..1be759f Binary files /dev/null and b/tools/cube.fmesh_c differ