improved physics, added better caching

This commit is contained in:
2025-06-05 22:02:53 +03:00
parent 5d85ebd85f
commit 64c0f41884
36 changed files with 651 additions and 106 deletions

View File

@@ -19,3 +19,5 @@ CUtlVector<CUtlString> all_IncludeDirectories = {
#include "game/server/__build.cpp"
#include "game/client/__build.cpp"
#include "funnyassets/__build.cpp"

View File

@@ -9,10 +9,12 @@ CUtlVector<CUtlString> engine_CompiledFiles = {
"engine/filesystem.cpp",
"engine/server.cpp",
"engine/engine.cpp",
"engine/physics.cpp",
/* rendering */
"engine/vk_video.cpp",
"engine/vk_mesh.cpp",
"engine/vk_postprocessing.cpp",
/* entities */
"engine/baseentity.cpp",
@@ -57,6 +59,7 @@ int engine_build()
const char *szGameName = ICommandLine::ParamValue("-game");
if (szGameName == NULL)
szGameName = "funnygame";
IFileSystem2::MakeDirectory(CUtlString("build/%s/game/bin",szGameName));
IFileSystem2::CopyFile(CUtlString("build/%s/game/bin",szGameName), outputProject);

View File

@@ -65,7 +65,7 @@ void C_BrushEntity::Spawn()
float uv[2];
};
pAlbedo = ITextureManager::LoadTexture("gfx/bricks.png");
pAlbedo = ITextureManager::LoadTexture("textures/bricks.png");
CBrushEntity* pBrushEntity = (CBrushEntity*)pEntity;
uint32_t numVertices = 15*pBrushEntity->m_mesh.GetSize();
vertexBuffer = IRenderer::CreateVertexBuffer(numVertices*4);
@@ -104,7 +104,6 @@ void C_BrushEntity::Destroy()
void C_BrushEntity::Think( float fDelta )
{
material.m.albedo = ITextureManager::GetTexture(pAlbedo);
IRenderer::SetMaterial(&material);
mat4 matrix;
glm_mat4_zero(matrix);
for (int i = 0; i < 9; i++) {
@@ -115,6 +114,7 @@ void C_BrushEntity::Think( float fDelta )
matrix[3][1] = pEntity->m_position[1];
matrix[3][2] = pEntity->m_position[2];
mesh->SetMatrix(matrix);
mesh->SetMaterial(&material);
mesh->Draw();
};

View File

@@ -17,7 +17,6 @@ void C_Light::Precache()
void C_Light::Spawn()
{
};
void C_Light::Destroy()

View File

@@ -52,6 +52,18 @@ void IEngine_Signal(int sig)
void IEngine::Init()
{
/* trap signals */
#ifdef __linux
signal(SIGHUP, IEngine_Signal);
signal(SIGINT, IEngine_Signal);
signal(SIGQUIT, IEngine_Signal);
signal(SIGILL, IEngine_Signal);
signal(SIGTRAP, IEngine_Signal);
signal(SIGIOT, IEngine_Signal);
signal(SIGBUS, IEngine_Signal);
signal(SIGFPE, IEngine_Signal);
signal(SIGSEGV, IEngine_Signal);
signal(SIGTERM, IEngine_Signal);
#endif
IFileSystem::InitFilesystem();
IVideo::Init();

97
engine/physics.cpp Normal file
View File

@@ -0,0 +1,97 @@
#include "physics.h"
#include "cglm/mat4.h"
#include "physics_gen.h"
void CPxCollider::Spawn( float fFriction )
{
};
void CPxCollider::Destroy()
{
};
void CPxBallMesh::Spawn( float fFriction )
{
m_pCollider = px_ball(m_fRadius, {.friction = fFriction});
};
void CPxBallMesh::Destroy()
{
};
void CPxBoxMesh::Spawn( float fFriction )
{
m_pCollider = px_box(m_fRadius[0], m_fRadius[1], m_fRadius[2], {.friction = fFriction});
};
void CPxBoxMesh::Destroy()
{
};
void CPxTriangleMesh::Spawn( float fFriction )
{
};
void CPxTriangleMesh::Destroy()
{
};
void CPxRigidBody::Spawn( CPxCollider *pCollider, px_matrix matrix, px_rigidbody_params params )
{
m_pRigidBody = px_rigidbody(px, pCollider->m_pCollider, matrix, params);
};
px_vec3 CPxRigidBody::GetPosition( void )
{
return px_getposition(px, m_pRigidBody);
};
px_matrix CPxRigidBody::GetMatrix ( void )
{
return px_getmatrix(px, m_pRigidBody);
};
void CPxRigidBody::Destroy()
{
};
void CPxStaticBody::Spawn( CPxCollider *pCollider, px_matrix matrix, px_rigidbody_params params )
{
px_staticbody(px, pCollider->m_pCollider, matrix);
};
px_vec3 CPxStaticBody::GetPosition( void )
{
return px_getposition(px, m_pCollider);
};
px_matrix CPxStaticBody::GetMatrix ( void )
{
return px_getmatrix(px, m_pCollider);
};
void CPxStaticBody::Destroy()
{
}
;
void CPxFixedBody::Spawn( CPxCollider *pCollider, px_matrix matrix, px_rigidbody_params params )
{
px_fixedbody(px, pCollider->m_pCollider);
};
px_vec3 CPxFixedBody::GetPosition( void )
{
return px_getposition(px, m_pCollider);
};
px_matrix CPxFixedBody::GetMatrix ( void )
{
return px_getmatrix(px, m_pCollider);
};
void CPxFixedBody::Destroy()
{
};

View File

@@ -15,6 +15,8 @@ void IServer::LoadGame( const char *psz )
#endif
#ifdef __linux__
g_serverdll = Plat_LoadLibrary(CUtlString("%s/bin/libserver.so", psz));
Plat_LoadLibrary(CUtlString("%s/bin/libclient.so", psz));
#endif
void (*GameLoadfn)() = (void(*)())Plat_GetProc(g_serverdll, "IGame_Load");
if (!GameLoadfn)

View File

@@ -26,6 +26,7 @@ public:
void SetVertexBuffer( IVertexBuffer *pBuffer ) override;
void SetIndexBuffer( IIndexBuffer *pBuffer ) override;
void SetMaterial( IMaterial *pMaterial ) override;
void Draw() override;
Material_t m_material;
@@ -76,14 +77,17 @@ void CMesh::SetIndexBuffer( IIndexBuffer *pBuffer )
m_pIndexBuffer = (CIndexBuffer*)pBuffer;
}
void CMesh::SetMaterial( IMaterial *pMaterial )
{
if (pMaterial == 0)
return;
m_material = pMaterial->m;
}
CUtlVector<CMesh> g_drawnMeshes;
void CMesh::Draw()
{
if (!g_pCurrentMaterial)
m_material = {};
else
m_material = g_pCurrentMaterial->m;
g_drawnMeshes.AppendTail(*this);
}
@@ -365,7 +369,7 @@ void IMeshRenderer::Frame( float fDelta )
for (auto &mesh: g_drawnMeshes)
{
VkDeviceSize offset = 0;
uint32_t textureID = 0;
uint32_t textureID = mesh.m_material.albedo;
vkCmdPushConstants(g_vkCommandBuffer, g_meshPipeline.m_layout, VK_SHADER_STAGE_ALL, 0, 64, &mesh.m_matrix);
vkCmdPushConstants(g_vkCommandBuffer, g_meshPipeline.m_layout, VK_SHADER_STAGE_ALL, 64, 4, &textureID);
vkCmdBindVertexBuffers(g_vkCommandBuffer, 0, 1, &mesh.m_pVertexBuffer->m_buffer.m_buffer, &offset);
@@ -391,7 +395,7 @@ void IMeshRenderer::Frame( float fDelta )
.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
.dstAccessMask = 0,
.oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
.newLayout = VK_IMAGE_LAYOUT_GENERAL,
.image = g_meshColor.m_image,
.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}
};
@@ -401,22 +405,6 @@ void IMeshRenderer::Frame( float fDelta )
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
0, 0, NULL, 0, NULL, 1, &barrier);
barrier = {
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
.dstAccessMask = 0,
.oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
.image = g_meshColor.m_image,
.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}
};
vkCmdPipelineBarrier(g_vkCommandBuffer,
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
0, 0, NULL, 0, NULL, 1, &barrier);
g_drawnMeshes = CUtlVector<CMesh>();
}

View File

@@ -0,0 +1,136 @@
#include "rendering.h"
#include "vk_helper.h"
#include "vk_video.h"
#include "vulkan/vulkan_core.h"
vk_shader_t post_agxShader = {};
vk_comppipeline_t post_agxPipeline = {};
VkDescriptorPool post_descriptorPool;
VkDescriptorSet post_descriptorSet;
void IPostProcessRenderer::Init()
{
post_agxShader.Create("gfx/agx_comp.spv", VK_SHADER_STAGE_COMPUTE_BIT);
CUtlVector<VkDescriptorSetLayoutBinding> bindings = {
{
.binding = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
},
{
.binding = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
}
};
post_agxPipeline.Create(post_agxShader, bindings, 0);
CUtlVector<VkDescriptorPoolSize> pools;
for (auto &binding: bindings)
{
VkDescriptorPoolSize dps = {};
dps.type = binding.descriptorType;
dps.descriptorCount = binding.descriptorCount;
pools.AppendTail(dps);
}
VkDescriptorPoolCreateInfo poolInfo = {};
poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
poolInfo.poolSizeCount = pools.GetSize();
poolInfo.pPoolSizes = pools.GetData();
poolInfo.maxSets = 1;
vkCreateDescriptorPool(g_vkDevice, &poolInfo, NULL, &post_descriptorPool);
VkDescriptorSetAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = post_descriptorPool;
allocInfo.descriptorSetCount = 1;
allocInfo.pSetLayouts = &post_agxPipeline.m_descriptorSetLayout;
vkAllocateDescriptorSets(g_vkDevice, &allocInfo, &post_descriptorSet);
}
void IPostProcessRenderer::Frame(float fDelta)
{
CUtlVector<VkImageMemoryBarrier> barriers = {
{
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.srcAccessMask = 0,
.dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
.oldLayout = VK_IMAGE_LAYOUT_GENERAL,
.newLayout = VK_IMAGE_LAYOUT_GENERAL,
.image = g_meshColor.m_image,
.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}
},
{
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.srcAccessMask = 0,
.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.newLayout = VK_IMAGE_LAYOUT_GENERAL,
.image = g_swapchainImage,
.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}
}
};
vkCmdPipelineBarrier(g_vkCommandBuffer,
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
0, 0, NULL, 0, NULL, barriers.GetSize(), barriers.GetData());
CUtlVector<VkWriteDescriptorSet> writes(2);
for (auto &write: writes)
{
write = {};
write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
write.dstSet = post_descriptorSet;
write.dstArrayElement = 0;
}
VkDescriptorImageInfo dii1 = {
.imageView = g_meshColor.m_imageView,
.imageLayout = VK_IMAGE_LAYOUT_GENERAL,
.sampler = NULL,
};
VkDescriptorImageInfo dii2 = {
.imageView = g_swapchainImageView,
.imageLayout = VK_IMAGE_LAYOUT_GENERAL,
.sampler = NULL,
};
writes[0].dstBinding = 0;
writes[0].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
writes[0].descriptorCount = 1;
writes[0].pImageInfo = &dii1;
writes[1].dstBinding = 1;
writes[1].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
writes[1].descriptorCount = 1;
writes[1].pImageInfo = &dii2;
vkUpdateDescriptorSets(g_vkDevice, writes.GetSize(), writes.GetData(), 0, NULL);
vkCmdBindPipeline(g_vkCommandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, post_agxPipeline.m_pipeline);
vkCmdBindDescriptorSets(g_vkCommandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, post_agxPipeline.m_layout, 0, 1, &post_descriptorSet, 0, 0);
vkCmdDispatch(g_vkCommandBuffer, (g_nWindowWidth+31)/32, (g_nWindowHeight+31)/32, 1);
barriers = {
{
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
.dstAccessMask = 0,
.oldLayout = VK_IMAGE_LAYOUT_GENERAL,
.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
.image = g_swapchainImage,
.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}
}
};
vkCmdPipelineBarrier(g_vkCommandBuffer,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
0, 0, NULL, 0, NULL, barriers.GetSize(), barriers.GetData());
}

View File

@@ -1,19 +1,26 @@
#include "cglm/mat4.h"
#include "filesystem.h"
#include "rendering.h"
#include "tier0/lib.h"
#include "tier1/utlvector.h"
#include "vk_helper.h"
#include "tier0/platform.h"
#include "rendering.h"
#include "vk_helper.h"
#include "vulkan/vulkan_core.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define VULKAN_RENDERING_IMPLEMENTATION
#include "vk_video.h"
VkSampler g_invalidTextureSampler;
vk_buffer_t g_cameraProperties;
CameraProjection *g_cameraDataMap;
mat4 g_cameraView;
IMaterial *g_pDefaultMaterial;
IMaterial *g_pCurrentMaterial;
@@ -60,19 +67,22 @@ void IVulkan::Init()
g_meshDepth.Create(1280, 720, VK_FORMAT_D32_SFLOAT, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_SAMPLE_COUNT_1_BIT);
g_meshDepthMSAA.Create(1280, 720, VK_FORMAT_D32_SFLOAT, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT, VK_SAMPLE_COUNT_4_BIT);
g_meshColor.Create(1280, 720, VK_FORMAT_R16G16B16A16_SFLOAT, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_SAMPLE_COUNT_1_BIT);
g_meshColor.Create(1280, 720, VK_FORMAT_R16G16B16A16_SFLOAT, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_STORAGE_BIT, VK_SAMPLE_COUNT_1_BIT);
g_meshColorMSAA.Create(1280, 720, VK_FORMAT_R16G16B16A16_SFLOAT, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT, VK_SAMPLE_COUNT_4_BIT);
IMeshRenderer::Init();
IPostProcessRenderer::Init();
};
void IVulkan::Frame()
{
glm_mat4_identity(g_cameraDataMap->viewprojection);
glm_perspective(glm_rad(90),(float)g_nWindowWidth/g_nWindowHeight, 0.01, 100, g_cameraDataMap->viewprojection);
glm_rotate(g_cameraDataMap->viewprojection, glm_rad(90), (vec4){1,0,0,0});
glm_scale(g_cameraDataMap->viewprojection, (vec4){1,-1,1,1});
glm_rotate(g_cameraDataMap->viewprojection, glm_rad(90), (vec4){0,0,1,0});
mat4 perspective;
glm_mat4_inv(g_cameraView, g_cameraDataMap->viewprojection);
glm_perspective(glm_rad(90),(float)g_nWindowWidth/g_nWindowHeight, 0.01, 100, perspective);
glm_rotate(perspective, glm_rad(90), (vec4){1,0,0,0});
glm_scale(perspective, (vec4){1,-1,1,1});
glm_rotate(perspective, glm_rad(90), (vec4){0,0,1,0});
glm_mat4_mul(perspective,g_cameraDataMap->viewprojection,g_cameraDataMap->viewprojection);
if (g_bConfigNotify)
{
@@ -82,12 +92,13 @@ void IVulkan::Frame()
g_meshColorMSAA.Destroy();
g_meshDepth.Create(g_nWindowWidth, g_nWindowHeight, VK_FORMAT_D32_SFLOAT, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_SAMPLE_COUNT_1_BIT);
g_meshDepthMSAA.Create(g_nWindowWidth, g_nWindowHeight, VK_FORMAT_D32_SFLOAT, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT, VK_SAMPLE_COUNT_1_BIT);
g_meshColor.Create(g_nWindowWidth, g_nWindowHeight, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_SAMPLE_COUNT_1_BIT);
g_meshDepthMSAA.Create(g_nWindowWidth, g_nWindowHeight, VK_FORMAT_D32_SFLOAT, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT, VK_SAMPLE_COUNT_4_BIT);
g_meshColor.Create(g_nWindowWidth, g_nWindowHeight, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_STORAGE_BIT, VK_SAMPLE_COUNT_1_BIT);
g_meshColorMSAA.Create(g_nWindowWidth, g_nWindowHeight, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT, VK_SAMPLE_COUNT_4_BIT);
}
IMeshRenderer::Frame(0);
IPostProcessRenderer::Frame(0);
};
void vk_shader_t::Create( const char *szPath, VkShaderStageFlagBits shaderStage )
@@ -237,6 +248,46 @@ void vk_tripipeline_t::Create(
void vk_tripipeline_t::Destroy()
{
}
void vk_comppipeline_t::Create(
vk_shader_t &shader,
CUtlVector<VkDescriptorSetLayoutBinding> &bindings,
uint32_t pushConstantsSize
)
{
VkPushConstantRange pushConstantRange = {};
pushConstantRange.stageFlags = VK_SHADER_STAGE_ALL;
pushConstantRange.offset = 0;
pushConstantRange.size = pushConstantsSize;
VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo = {};
descriptorSetLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
descriptorSetLayoutCreateInfo.bindingCount = bindings.GetSize();
descriptorSetLayoutCreateInfo.pBindings = bindings.GetData();
vkCreateDescriptorSetLayout(g_vkDevice, &descriptorSetLayoutCreateInfo, NULL, &m_descriptorSetLayout);
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = {};
pipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutCreateInfo.setLayoutCount = 1;
pipelineLayoutCreateInfo.pSetLayouts = &m_descriptorSetLayout;
if (pushConstantsSize != 0)
{
pipelineLayoutCreateInfo.pushConstantRangeCount = 1;
pipelineLayoutCreateInfo.pPushConstantRanges = &pushConstantRange;
}
vkCreatePipelineLayout(g_vkDevice, &pipelineLayoutCreateInfo, NULL, &m_layout);
VkComputePipelineCreateInfo cpci = {};
cpci.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO;
cpci.stage = shader.m_stageCreateInfo;
cpci.layout = m_layout;
vkCreateComputePipelines(g_vkDevice, NULL, 1, &cpci, NULL, &m_pipeline);
}
void vk_comppipeline_t::Destroy()
{
}
void vk_buffer_t::Create(size_t size, VkBufferUsageFlags usage)
@@ -500,6 +551,7 @@ ITexture *ITextureManager::LoadTexture( const char *szName )
FileHandle_t file = IFileSystem::Open(szName, IFILE_READ);
if (!file)
Plat_FatalErrorFunc("Failed to load %s\n", szName);
V_printf("cool %s\n",szName);
CUtlBuffer<stbi_uc> buffer(IFileSystem::Size(file));
IFileSystem::Read(file, buffer.GetMemory(), buffer.GetSize());
@@ -523,12 +575,6 @@ IMaterial *IRenderer::LoadMaterial( const char *szMaterial )
IFileSystem::Close(file);
}
void IRenderer::SetMaterial( IMaterial *pMaterial )
{
g_pCurrentMaterial = pMaterial;
}
IVertexBuffer *IRenderer::CreateVertexBuffer( uint32_t uSize )
{
CVertexBuffer *pBuffer = new CVertexBuffer();

View File

@@ -99,7 +99,7 @@ void IVideo_SwapchainInit()
swapchainCreateInfo.imageFormat = selectedFormat.format;
swapchainCreateInfo.imageColorSpace = selectedFormat.colorSpace;
swapchainCreateInfo.presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
swapchainCreateInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchainCreateInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_STORAGE_BIT;
swapchainCreateInfo.preTransform = surfaceCapatibilities.currentTransform;
swapchainCreateInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
swapchainCreateInfo.imageArrayLayers = 1;

View File

@@ -19,6 +19,7 @@ CLDProject CCProject::Compile()
unsigned int hash = GenerateProjectHash();
for (auto &file: files)
{
V_printf(" CC %s\n", file.GetString());
CUtlString szOutputFile = CUtlString("%s/cc/%u_%s/%s/%s.o",FPC_TEMPORAL_DIRNAME, hash, m_szName.GetString(), IFileSystem2::OwnDirectory(), file.GetString());
CUtlString szOutputDir;
CUtlVector<CUtlString> args;
@@ -83,7 +84,7 @@ void CCProject::GenerateCompileCommands()
V_fseek(f, -2, SEEK_CUR);
V_fprintf(f, "\n\t\t],\n");
V_fprintf(f, "\t\t\"file\": \"%s\",\n", file.m_szName.GetString());
V_fprintf(f, "\t\t\"directory\": \"%s\"\n", IFileSystem2::OwnDirectory());
V_fprintf(f, "\t\t\"directory\": \"%s\"\n", IFileSystem2::BuildDirectory());
V_fprintf(f, "\t},\n");
};
V_fseek(f, -2, SEEK_CUR);

View File

@@ -43,6 +43,15 @@ void IFileSystem2::CopyFile( const char *szDestination, const char *szOrigin )
};
IRunner::Run("cp", args);
}
void IFileSystem2::CopyDirectory( const char *szDestination, const char *szOrigin )
{
CUtlVector<CUtlString> args = {
"-r",
CUtlString(szOrigin),
CUtlString(szDestination),
};
IRunner::Run("cp", args);
}
void IFileSystem2::MakeDirectory( const char *psz )
{

View File

@@ -1,4 +1,5 @@
#include "ld.h"
#include "helper.h"
#include "libgen.h"
CUtlString CLDProject::Link( void )
@@ -23,7 +24,20 @@ CUtlString CLDProject::Link( void )
IFileSystem2::MakeDirectory(szOutputDir);
if (linkType == ELINK_STATIC_LIBRARY)
{
CUtlVector<CUtlString> args = {
V_printf(" AR %s\n", m_szName.GetString());
bool shouldRecompile = false;
CUtlVector<CUtlString> args;
for (auto object: objects)
{
if (IFileSystem2::ShouldRecompile(object.m_szObjectFile,szOutputFile))
{
shouldRecompile = true;
break;
}
}
if (!shouldRecompile)
goto compiled;
args = {
"rcs",
szOutputFile
};
@@ -31,7 +45,20 @@ CUtlString CLDProject::Link( void )
args.AppendTail(object.m_szObjectFile);
IRunner::Run("ar", args);
} else {
CUtlVector<CUtlString> args = {
V_printf(" LINK %s\n", m_szName.GetString());
bool shouldRecompile = false;
CUtlVector<CUtlString> args;
for (auto object: objects)
{
if (IFileSystem2::ShouldRecompile(object.m_szObjectFile,szOutputFile))
{
shouldRecompile = true;
break;
}
}
if (!shouldRecompile)
goto compiled;
args = {
"-rdynamic",
"-o",
szOutputFile,
@@ -47,5 +74,6 @@ CUtlString CLDProject::Link( void )
}
IRunner::Run("clang++", args);
}
compiled:
return szOutputFile;
};

View File

@@ -16,7 +16,6 @@ int IRunner::Run(CUtlString szName, CUtlVector<CUtlString>& args)
execargs.AppendTail(szName);
for (auto &arg: args)
{
V_printf("%s\n",arg.GetString());
execargs.AppendTail(arg);
}
execargs.AppendTail(0);

View File

@@ -7,6 +7,7 @@
#include "tier1/utlvector.h"
#include "signal.h"
#include "libgen.h"
#include <unistd.h>
CUtlString owndir;
int build()
@@ -50,7 +51,9 @@ void IEngine_Signal(int sig)
int main(int c, char **v)
{
CUtlString buildcppDir = IFileSystem2::OwnDirectory();
char path[1024];
CUtlString buildcppDir = getcwd(path, 1024);
owndir = buildcppDir;
char *szBuildcppDir = buildcppDir.GetString();
findbuild:

46
funnyassets/__build.cpp Normal file
View File

@@ -0,0 +1,46 @@
#include "helper.h"
#include "c.h"
#include "ld.h"
#include "runner.h"
#include "tier1/utlstring.h"
#include "tier1/commandline.h"
void build_shader( const char *szName )
{
const char *szGameName = ICommandLine::ParamValue("-game");
if (szGameName == NULL)
szGameName = "funnygame";
IFileSystem2::MakeDirectory(CUtlString("build/%s/assets/gfx",szGameName));
CUtlVector<CUtlString> slang_args = {
CUtlString("funnyassets/gfx_shaders/%s.slang", szName),
"-o",
CUtlString("build/%s/assets/gfx/%s.spv", szGameName, szName),
};
IRunner::Run("slangc", slang_args);
};
int assets_build()
{
IFileSystem2::CopyDirectory("build", "tools");
const char *szGameName = ICommandLine::ParamValue("-game");
if (szGameName == NULL)
szGameName = "funnygame";
IFileSystem2::CopyDirectory(CUtlString("build/%s/assets",szGameName), "funnyassets/maps");
IFileSystem2::CopyDirectory(CUtlString("build/%s/assets",szGameName), "funnyassets/gfx");
IFileSystem2::CopyDirectory(CUtlString("build/%s/assets",szGameName), "funnyassets/textures");
IFileSystem2::CopyDirectory(CUtlString("build/%s/assets",szGameName), "funnyassets/materials");
build_shader("mesh_frag");
build_shader("mesh_vert");
build_shader("agx_comp");
CUtlVector<CUtlString> python_args = {
"build/tools/makepak64.py",
CUtlString("build/%s/assets", szGameName),
CUtlString("build/%s/game/%s/%s.pak", szGameName, szGameName, "rtt"),
};
IRunner::Run("python", python_args);
return 0;
};
DECLARE_BUILD_STAGE(assets, assets_build);

View File

@@ -0,0 +1,93 @@
[[vk::binding(0)]]
RWTexture2D<float4> g_imageIn;
[[vk::binding(1)]]
RWTexture2D<float4> g_imageOut;
#define AGX_LOOK 2
float3 agxDefaultContrastApprox(float3 x) {
float3 x2 = x * x;
float3 x4 = x2 * x2;
return + 15.5 * x4 * x2
- 40.14 * x4 * x
+ 31.96 * x4
- 6.868 * x2 * x
+ 0.4298 * x2
+ 0.1191 * x
- 0.00232;
}
float3 agx(float3 val) {
const float3x3 agx_mat = float3x3(
0.842479062253094, 0.0423282422610123, 0.0423756549057051,
0.0784335999999992, 0.878468636469772, 0.0784336,
0.0792237451477643, 0.0791661274605434, 0.879142973793104);
const float min_ev = -12.47393f;
const float max_ev = 4.026069f;
val = mul(agx_mat, val);
val = clamp(log2(val), min_ev, max_ev);
val = (val - min_ev) / (max_ev - min_ev);
val = agxDefaultContrastApprox(val);
return val;
}
float3 agxEotf(float3 val) {
const float3x3 agx_mat_inv = float3x3(
1.19687900512017, -0.0528968517574562, -0.0529716355144438,
-0.0980208811401368, 1.15190312990417, -0.0980434501171241,
-0.0990297440797205, -0.0989611768448433, 1.15107367264116);
val = mul(agx_mat_inv, val);
val = pow(val, float3(2.2));
return val;
}
float3 agxLook(float3 val) {
float3 offset = float3(0.0);
float3 slope = float3(1.0);
float3 power = float3(1.0);
float sat = 1.0;
#if AGX_LOOK == 1
slope = float3(1.0, 0.9, 0.5);
power = float3(0.8);
sat = 0.8;
#elif AGX_LOOK == 2
slope = float3(1.0);
power = float3(1.35, 1.35, 1.35);
sat = 1.4;
#endif
// ASC CDL
val = pow(val * slope + offset, power);
const float3 lw = float3(0.2126, 0.7152, 0.0722);
float luma = dot(val, lw);
return luma + sat * (val - luma);
}
[shader("compute")]
[numthreads(32,32,1)]
void main(uint3 threadId: SV_DispatchThreadID)
{
uint2 coord = threadId.xy;
float3 color = g_imageIn.Load(coord).xyz;
color = pow(color,2.2);
color = agx(color);
color = agxLook(color);
color = agxEotf(color);
color = pow(color,0.45);
g_imageOut[coord] = float4(color,1);
};

View File

@@ -1,27 +0,0 @@
uint32_t hash32(uint32_t x) {
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = (x >> 16) ^ x;
return x;
}
[[vk::binding(1)]]
uniform Sampler2D textures[];
struct VertexOutput
{
float4 position: SV_Position;
float2 uv: TEXCOORD0;
};
float4 main(
VertexOutput input,
uint triid: SV_PrimitiveID,
uniform uint textureID,
) : SV_TARGET
{
return float4(textures[textureID].Sample(input.uv).xyz, 1);
}

View File

@@ -1,27 +0,0 @@
struct VertexInput
{
float3 position: POSITION;
float2 uv: TEXCOORD0;
};
struct VertexOutput
{
float4 position: SV_Position;
float2 uv: TEXCOORD0;
};
[[vk::binding(0)]]
cbuffer CameraInfo
{
float4x4 projection;
};
VertexOutput main(
VertexInput input,
)
{
VertexOutput output;
output.position = mul(projection, float4(input.position,1));
output.uv = input.uv;
return output;
}

View File

@@ -10,7 +10,7 @@ uint32_t hash32(uint32_t x) {
[[vk::binding(1)]]
uniform Sampler2D textures[];
[shader("fragment")]
float4 main(
VertexOutput input,
uint triid: SV_PrimitiveID,

View File

@@ -12,6 +12,7 @@ cbuffer CameraInfo
float4x4 projection;
};
[shader("vertex")]
VertexOutput main(
VertexInput input,
uniform PushConstants constants,

Binary file not shown.

View File

@@ -102,3 +102,6 @@
"origin" "10 0 4"
"intensity" "4.0"
}
{
"classname" "player"
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 405 KiB

After

Width:  |  Height:  |  Size: 863 KiB

View File

@@ -25,6 +25,7 @@ int client_build()
const char *szGameName = ICommandLine::ParamValue("-game");
if (szGameName == NULL)
szGameName = "funnygame";
IFileSystem2::MakeDirectory(CUtlString("build/%s/game/%s/bin",szGameName, szGameName));
IFileSystem2::CopyFile(CUtlString("build/%s/game/%s/bin",szGameName,szGameName), outputProject);
return 0;

View File

@@ -1,4 +1,22 @@
#include "baseentity.h"
#include "cglm/affine-pre.h"
#include "cglm/mat4.h"
#include "physics.h"
class CBasePlayer: public CBaseEntity
{
public:
virtual void Precache ( void ) override;
virtual void Spawn( void ) override;
virtual void ReadParameter( const char *szName, const char *szValue ) override;
virtual void Destroy( void ) override;
virtual void Think( float fDelta ) override;
CPxBoxMesh m_Collider;
CPxRigidBody m_RigidBody;
};
class C_BasePlayer: public C_BaseEntity
{
@@ -10,6 +28,9 @@ public:
};
extern mat4 g_cameraView;
void C_BasePlayer::Precache()
{
@@ -17,7 +38,6 @@ void C_BasePlayer::Precache()
void C_BasePlayer::Spawn()
{
};
void C_BasePlayer::Destroy()
@@ -26,6 +46,9 @@ void C_BasePlayer::Destroy()
}
void C_BasePlayer::Think( float fDelta )
{
CBasePlayer *pPlayer = (CBasePlayer*)pEntity;
glm_mat4_identity(g_cameraView);
V_memcpy(&g_cameraView[3][0], pPlayer->m_RigidBody.GetPosition().m, 12);
};

View File

@@ -26,6 +26,7 @@ int server_build()
const char *szGameName = ICommandLine::ParamValue("-game");
if (szGameName == NULL)
szGameName = "funnygame";
IFileSystem2::MakeDirectory(CUtlString("build/%s/game/%s/bin",szGameName, szGameName));
IFileSystem2::CopyFile(CUtlString("build/%s/game/%s/bin",szGameName,szGameName), outputProject);
return 0;

View File

@@ -1,4 +1,5 @@
#include "baseentity.h"
#include "physics.h"
class CBasePlayer: public CBaseEntity
{
@@ -8,8 +9,10 @@ public:
virtual void ReadParameter( const char *szName, const char *szValue ) override;
virtual void Destroy( void ) override;
virtual void Think( float fDelta ) override;
};
CPxBoxMesh m_Collider;
CPxRigidBody m_RigidBody;
};
void CBasePlayer::Precache()
{
@@ -18,7 +21,24 @@ void CBasePlayer::Precache()
void CBasePlayer::Spawn()
{
m_Collider.m_fRadius[0] = 1;
m_Collider.m_fRadius[1] = 1;
m_Collider.m_fRadius[2] = 1;
m_Collider.Spawn(0.0);
px_matrix matrix = {
.m = {
1,0,0,0,
0,1,0,0,
0,0,1,0,
5,0,0,1,
}
};
m_RigidBody.Spawn(&m_Collider, matrix, {
.gravity_scale = 1,
.lockrotx = 1,
.lockroty = 1,
.lockrotz = 1,
});
};
void CBasePlayer::ReadParameter( const char *szName, const char *szValue )
{

View File

@@ -20,6 +20,11 @@ int launcher_build()
ldProject.linkType = ELINK_EXECUTABLE;
CUtlString outputProject = ldProject.Link();
const char *szGameName = ICommandLine::ParamValue("-game");
if (szGameName == NULL)
szGameName = "funnygame";
IFileSystem2::MakeDirectory(CUtlString("build/%s/game/bin",szGameName));
IFileSystem2::CopyFile(CUtlString("build/%s/game/bin/%s", szGameName, szGameName), outputProject);
return 0;
};

0
public/input.h Normal file
View File

View File

@@ -3,6 +3,7 @@
#include "tier0/lib.h"
#include "stdint.h"
#include "tier1/utlvector.h"
typedef void Collider;
@@ -22,4 +23,67 @@ typedef struct u128 {
extern funnyphysics *px;
class CPxCollider
{
public:
virtual void Spawn( float fFriction = 0.5 ) = 0;
virtual void Destroy( void );
Collider *m_pCollider;
};
class CPxBallMesh: public CPxCollider
{
public:
virtual void Spawn( float fFriction = 0.5 ) override;
virtual void Destroy( void ) override;
float m_fRadius;
};
class CPxBoxMesh: public CPxCollider
{
public:
virtual void Spawn( float fFriction = 0.5 ) override;
virtual void Destroy( void ) override;
float m_fRadius[3];
};
class CPxTriangleMesh: public CPxCollider
{
public:
virtual void Spawn( float fFriction = 0.5 ) override;
virtual void Destroy( void ) override;
};
class CPxRigidBody
{
public:
void Spawn( CPxCollider *pCollider, px_matrix matrix, px_rigidbody_params params );
px_vec3 GetPosition( void );
px_matrix GetMatrix ( void );
void Destroy( void );
CPxCollider *m_pCollider;
RigidBodyHandle *m_pRigidBody;
};
class CPxStaticBody
{
public:
void Spawn( CPxCollider *pCollider, px_matrix matrix, px_rigidbody_params params );
px_vec3 GetPosition( void );
px_matrix GetMatrix ( void );
void Destroy( void );
CPxCollider *m_pCollider;
};
class CPxFixedBody
{
public:
void Spawn( CPxCollider *pCollider, px_matrix matrix, px_rigidbody_params params );
px_vec3 GetPosition( void );
px_matrix GetMatrix ( void );
void Destroy( void );
CPxCollider *m_pCollider;
};
#endif

View File

@@ -93,6 +93,7 @@ public:
virtual void SetVertexBuffer( IVertexBuffer *pBuffer ) = 0;
virtual void SetIndexBuffer( IIndexBuffer *pBuffer ) = 0;
virtual void SetMaterial( IMaterial *pMaterial ) = 0;
virtual void Draw() = 0;
};
@@ -121,4 +122,11 @@ public:
static ITexture *LoadTexture( const char *szName );
};
interface IPostProcessRenderer
{
public:
static void Init();
static void Frame( float fDelta );
};
#endif

View File

@@ -48,7 +48,15 @@ struct vk_tripipeline_t
struct vk_comppipeline_t
{
void Create(
vk_shader_t &shader,
CUtlVector<VkDescriptorSetLayoutBinding> &bindings,
uint32_t pushConstantsSize
);
void Destroy();
VkDescriptorSetLayout m_descriptorSetLayout;
VkPipelineLayout m_layout;
VkPipeline m_pipeline;
};
struct vk_buffer_t

View File

@@ -152,7 +152,7 @@ pub unsafe extern "C" fn px_rigidbody(px_world: *mut funnyphysics, collider: *mu
let c = &mut *collider;
let px = px_world.as_mut().unwrap();
let rigid_body = RigidBodyBuilder::dynamic()
.translation(vector![m.m[3],m.m[7],m.m[11]])
.translation(vector![m.m[12],m.m[13],m.m[14]])
.can_sleep(false)
.gravity_scale(1.0)
.enabled_rotations(params.lockrotx==0, params.lockroty==0, params.lockrotz==0)

View File

@@ -27,6 +27,7 @@ int tier0_build()
const char *szGameName = ICommandLine::ParamValue("-game");
if (szGameName == NULL)
szGameName = "funnygame";
IFileSystem2::MakeDirectory(CUtlString("build/%s/game/bin",szGameName));
IFileSystem2::CopyFile(CUtlString("build/%s/game/bin",szGameName), outputProject);
return 0;