working on rendering
This commit is contained in:
75
shadercompiler/__build.cpp
Normal file
75
shadercompiler/__build.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#include "c.h"
|
||||
#include "helper.h"
|
||||
#include "ld.h"
|
||||
#include "target.h"
|
||||
|
||||
CUtlString shadercompiler_lib;
|
||||
CUtlString shadercompiler_exe;
|
||||
DECLARE_BUILD_STAGE(libshadercompiler)
|
||||
{
|
||||
CProject_t stProject;
|
||||
LinkProject_t stLinkProject;
|
||||
CUtlString szOutput;
|
||||
|
||||
stProject.m_szName = "shadercompiler";
|
||||
stProject.m_target = Target_t::HostTarget();
|
||||
stProject.bFPIC = true;
|
||||
stProject.includeDirectories = {
|
||||
"public",
|
||||
"external/slang/include",
|
||||
"external/Vulkan-Headers/include",
|
||||
};
|
||||
stProject.files = {
|
||||
"shadercompiler/slang/vulkan_spirv.cpp",
|
||||
"materialsystem/compiledshader.cpp",
|
||||
};
|
||||
|
||||
stLinkProject = ccompiler->Compile(&stProject);
|
||||
stLinkProject.linkType = ELINK_STATIC_LIBRARY;
|
||||
szOutput = linker->Link(&stLinkProject);
|
||||
shadercompiler_lib = szOutput;
|
||||
|
||||
return 1;
|
||||
}
|
||||
DECLARE_BUILD_STAGE(shadercompiler)
|
||||
{
|
||||
CProject_t stProject;
|
||||
LinkProject_t stLinkProject;
|
||||
CUtlString szOutput;
|
||||
|
||||
stProject.m_szName = "fsc";
|
||||
stProject.m_target = Target_t::HostTarget();
|
||||
stProject.includeDirectories = {"public"};
|
||||
stProject.files = {
|
||||
"shadercompiler/main.cpp",
|
||||
};
|
||||
|
||||
stLinkProject = ccompiler->Compile(&stProject);
|
||||
stLinkProject.linkType = ELINK_EXECUTABLE;
|
||||
stLinkProject.objects.AppendTail((Object_t){tier0_lib});
|
||||
stLinkProject.objects.AppendTail((Object_t){tier1_lib});
|
||||
stLinkProject.objects.AppendTail((Object_t){tier2_lib});
|
||||
stLinkProject.objects.AppendTail((Object_t){shadercompiler_lib});
|
||||
stLinkProject.libraries = {"slang-compiler","slang-glslang-2025.24.2"};
|
||||
stLinkProject.libraryDirectories = {"external/linux"};
|
||||
szOutput = linker->Link(&stLinkProject);
|
||||
filesystem2->MakeDirectory(CUtlString("%s/bin/tools", szOutputDir.GetString()));
|
||||
filesystem2->CopyFile(CUtlString("%s/bin/tools", szOutputDir.GetString()), szOutput);
|
||||
filesystem2->CopyFile(CUtlString("%s/bin/tools", szOutputDir.GetString()), "external/linux/libslang-compiler.so.0.2025.24.2");
|
||||
filesystem2->CopyFile(CUtlString("%s/bin/tools", szOutputDir.GetString()), "external/linux/libslang-glslang-2025.24.2.so");
|
||||
shadercompiler_exe = CUtlString("%s/bin/tools/fsc", szOutputDir.GetString());
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
DECLARE_BUILD_STAGE(compileshaders)
|
||||
{
|
||||
if (CommandLine()->CheckParam("-nofsc"))
|
||||
return 0;
|
||||
|
||||
CUtlVector<CUtlString> args = {"-i", "funnyassets", "-o", "build/funnygame/assets"};
|
||||
|
||||
runner->Run(shadercompiler_exe, args);
|
||||
runner->Wait();
|
||||
return 0;
|
||||
};
|
||||
61
shadercompiler/main.cpp
Normal file
61
shadercompiler/main.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#include "tier0/lib.h"
|
||||
#include "tier1/commandline.h"
|
||||
#include "tier1/appinit.h"
|
||||
#include "tier1/utlstring.h"
|
||||
#include "tier2/ifilesystem.h"
|
||||
#include "shadercompiler/icompiler.h"
|
||||
#include "materialsystem/compiledshadermgr.h"
|
||||
|
||||
static IShaderCompiler *s_pVulkanSpirvCompiler;
|
||||
static const char *s_szGameRoot;
|
||||
|
||||
void CompileShader( const char *szShader )
|
||||
{
|
||||
CCompiledShader shader;
|
||||
|
||||
if (V_strcmp("shader",Plat_GetExtension(szShader)))
|
||||
return;
|
||||
|
||||
s_pVulkanSpirvCompiler->CompileShader(szShader, &shader);
|
||||
|
||||
Plat_MakeDir("build/funnygame/assets", 0755);
|
||||
Plat_MakeDir("build/funnygame/assets/shaders", 0755);
|
||||
|
||||
CUtlString szOutputDirectory = szShader;
|
||||
szOutputDirectory.RemoveHead(strlen(s_szGameRoot)+1);
|
||||
szOutputDirectory.AppendHead("build/funnygame/assets/");
|
||||
szOutputDirectory.AppendTail("_c");
|
||||
printf("%s\n",szOutputDirectory.GetString());
|
||||
|
||||
|
||||
CompiledShaderManager()->WriteToFile(&shader, szOutputDirectory);
|
||||
};
|
||||
|
||||
void PrintHelp()
|
||||
{
|
||||
printf("Usage: fsc -i [shader folder] -o [game root]");
|
||||
Plat_Exit(0);
|
||||
}
|
||||
|
||||
int main( int c, char **v )
|
||||
{
|
||||
CUtlString szExePath = Plat_GetExecutablePath();
|
||||
|
||||
CommandLine()->CreateCommandLine(c, v);
|
||||
filesystem->Init();
|
||||
|
||||
s_pVulkanSpirvCompiler = (IShaderCompiler*)CreateInterface(SLANG_SHADER_COMPILER_SPIRV_VULKAN, NULL);
|
||||
s_pVulkanSpirvCompiler->Init();
|
||||
|
||||
const char *szInputDirectory = CommandLine()->ParamValue("-i");
|
||||
if (!szInputDirectory)
|
||||
PrintHelp();
|
||||
s_szGameRoot = szInputDirectory;
|
||||
|
||||
CUtlString szShaderDirectory = CUtlString("%s/shaders\n", szInputDirectory);
|
||||
|
||||
Plat_ListDirRecursive(szInputDirectory, CompileShader, NULL);
|
||||
s_pVulkanSpirvCompiler->Shutdown();
|
||||
|
||||
return 0;
|
||||
}
|
||||
0
shadercompiler/slang/metal_code.cpp
Normal file
0
shadercompiler/slang/metal_code.cpp
Normal file
144
shadercompiler/slang/vulkan_spirv.cpp
Normal file
144
shadercompiler/slang/vulkan_spirv.cpp
Normal file
@@ -0,0 +1,144 @@
|
||||
#include "shadercompiler/icompiler.h"
|
||||
#include "slang.h"
|
||||
#include "tier0/mem.h"
|
||||
#include "tier1/utlstring.h"
|
||||
#include "tier2/ifilesystem.h"
|
||||
#include "vulkan/vulkan.h"
|
||||
|
||||
using namespace slang;
|
||||
|
||||
static IGlobalSession *s_pGlobalSession;
|
||||
static ISession *s_pSession;
|
||||
class CSlangVulkanSpirvShaderCompiler: public IShaderCompiler
|
||||
{
|
||||
public:
|
||||
virtual void Init() override;
|
||||
virtual void Shutdown() override;
|
||||
virtual void CompileShader( const char *szInput, CCompiledShader *pShader ) override;
|
||||
private:
|
||||
IBlob *m_pDiagnostics = NULL;
|
||||
void CheckDiagnostics();
|
||||
|
||||
void CompileShaderStage( EShaderStage eStage, const char *szMain, ISession *pSession, IModule *pModule, CCompiledShader *pShader );
|
||||
};
|
||||
|
||||
EXPOSE_INTERFACE(CSlangVulkanSpirvShaderCompiler, IShaderCompiler, SLANG_SHADER_COMPILER_SPIRV_VULKAN)
|
||||
|
||||
void CSlangVulkanSpirvShaderCompiler::CompileShaderStage( EShaderStage eStage, const char *szMain, ISession *pSession, IModule *pModule, CCompiledShader *pShader )
|
||||
{
|
||||
SlangStage eSlangStage;
|
||||
IEntryPoint *pEntryPoint = NULL;
|
||||
IComponentType *pLinked = NULL;
|
||||
IBlob *pBinary = NULL;
|
||||
ShaderObject_t *pShaderObject = NULL;
|
||||
|
||||
switch (eStage)
|
||||
{
|
||||
case SHADER_STAGE_VERTEX:
|
||||
eSlangStage = SLANG_STAGE_VERTEX;
|
||||
break;
|
||||
case SHADER_STAGE_PIXEL:
|
||||
eSlangStage = SLANG_STAGE_PIXEL;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
pModule->findAndCheckEntryPoint(szMain, eSlangStage, &pEntryPoint, &m_pDiagnostics);
|
||||
CheckDiagnostics();
|
||||
|
||||
pEntryPoint->link(&pLinked, &m_pDiagnostics);
|
||||
CheckDiagnostics();
|
||||
|
||||
pLinked->getEntryPointCode(0, 0, &pBinary, &m_pDiagnostics);
|
||||
CheckDiagnostics();
|
||||
|
||||
pShaderObject = pShader->AllocateShader();
|
||||
pShaderObject->m_eBackend = SHADER_BACKEND_SPIRV_VULKAN;
|
||||
pShaderObject->m_eStage = eStage;
|
||||
pShaderObject->m_nDataLump = pShader->AllocateLump(pBinary->getBufferSize());
|
||||
V_memcpy(pShader->GetLumpPtr(pShaderObject->m_nDataLump), pBinary->getBufferPointer(), pBinary->getBufferSize());
|
||||
|
||||
pEntryPoint->release();
|
||||
}
|
||||
|
||||
void CSlangVulkanSpirvShaderCompiler::CompileShader( const char *szInput, CCompiledShader *pShader )
|
||||
{
|
||||
SessionDesc stSessionDesc = {};
|
||||
TargetDesc stTargetDesc = {};
|
||||
PreprocessorMacroDesc stStageMacroDesc = {};
|
||||
const char *szMainName;
|
||||
IModule *pModule = NULL;
|
||||
ISession *pSession = NULL;
|
||||
IBlob *pShaderSourceBlob = NULL;
|
||||
|
||||
IFileHandle *pFile = filesystem->Open(szInput, FILEMODE_READ);
|
||||
const char *szShaderSource = filesystem->ReadString(pFile);
|
||||
|
||||
int i = 0;
|
||||
|
||||
pShaderSourceBlob = slang_createBlob(szShaderSource, pFile->Size());
|
||||
filesystem->Close(pFile);
|
||||
|
||||
printf(" SLANG %s\n", szInput);
|
||||
|
||||
stSessionDesc.targetCount = 1;
|
||||
stSessionDesc.targets = &stTargetDesc;
|
||||
|
||||
stTargetDesc.format = SLANG_SPIRV;
|
||||
stTargetDesc.profile = s_pGlobalSession->findProfile("spirv_1_6");
|
||||
|
||||
|
||||
for ( i = 0; i<SHADER_STAGE_COUNT; i++ )
|
||||
{
|
||||
szMainName = NULL;
|
||||
switch (i)
|
||||
{
|
||||
case SHADER_STAGE_VERTEX:
|
||||
szMainName = "vsMain";
|
||||
stStageMacroDesc = { "VS_SHADER", "Enabled" };
|
||||
break;
|
||||
case SHADER_STAGE_PIXEL:
|
||||
szMainName = "psMain";
|
||||
stStageMacroDesc = { "PS_SHADER", "Enabled" };
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
stSessionDesc.preprocessorMacroCount = 1;
|
||||
stSessionDesc.preprocessorMacros = &stStageMacroDesc;
|
||||
if ( szMainName == NULL )
|
||||
continue;
|
||||
pSession = NULL;
|
||||
s_pGlobalSession->createSession(stSessionDesc, &pSession);
|
||||
|
||||
pModule = pSession->loadModuleFromSource("main", szInput, pShaderSourceBlob, &m_pDiagnostics);
|
||||
CheckDiagnostics();
|
||||
|
||||
CompileShaderStage((EShaderStage)i, szMainName, pSession, pModule, pShader);
|
||||
pModule->release();
|
||||
pSession->release();
|
||||
}
|
||||
|
||||
V_free((void*)szShaderSource);
|
||||
}
|
||||
void CSlangVulkanSpirvShaderCompiler::CheckDiagnostics()
|
||||
{
|
||||
|
||||
if (m_pDiagnostics)
|
||||
{
|
||||
V_printf("%s\n",(const char*)m_pDiagnostics->getBufferPointer());
|
||||
Plat_Exit(0);
|
||||
}
|
||||
};
|
||||
|
||||
void CSlangVulkanSpirvShaderCompiler::Init()
|
||||
{
|
||||
createGlobalSession(&s_pGlobalSession);
|
||||
}
|
||||
|
||||
void CSlangVulkanSpirvShaderCompiler::Shutdown()
|
||||
{
|
||||
s_pGlobalSession->Release();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user