work on shader compiler

This commit is contained in:
2026-02-05 23:03:03 +02:00
parent 4bfbcaa4a6
commit 7ac98cf9ba
6 changed files with 107 additions and 3 deletions

View File

@@ -17,11 +17,15 @@ COMMON
struct PS_INPUT
{
float4 m_vPosition : SV_Position;
float4 m_vPosition : SV_POSITION;
}
}
VS
{
cbuffer CameraInfo
{
float4x4 g_matViewProj;
}
struct VS_INPUT
{
float3 m_vPosition: POSITION;
@@ -30,7 +34,7 @@ VS
PS_INPUT vsMain( VS_INPUT i )
{
PS_INPUT o;
o.m_vPosition = { i.m_vPosition, 1 };
o.m_vPosition = mul(g_matViewProj, float4(i.m_vPosition, 1.0));
return o;
}
}

View File

@@ -0,0 +1,43 @@
#define COMMON using namespace Common; namespace Common
#ifdef VS_SHADER
#define VS using namespace VertexShader; namespace VertexShader
#else
#define VS namespace VertexShader_DO_NOT_USE
#endif
#ifdef PS_SHADER
#define PS using namespace PixelShader; namespace PixelShader
#else
#define PS namespace PixelShader_DO_NOT_USE
#endif
COMMON
{
struct PS_INPUT
{
float4 m_vPosition : SV_Position;
}
}
VS
{
struct VS_INPUT
{
float3 m_vPosition: POSITION;
}
PS_INPUT vsMain( VS_INPUT i )
{
PS_INPUT o;
o.m_vPosition = { i.m_vPosition, 1 };
return o;
}
}
PS
{
float4 psMain( PS_INPUT i )
{
return float4(1);
}
}

View File

@@ -281,6 +281,8 @@ public:
CVkMaterial( IShader *pShader );
virtual ~CVkMaterial() override;
virtual uint32_t GetResourceByName( const char *szString );
virtual void VSSetShaderResource( uint32_t uRegister, IRenderingObject *pResource ) override;
virtual void VSSetConstantsBuffer( uint32_t uRegister, IBuffer *pConstants ) override;

View File

@@ -6,6 +6,7 @@
struct VulkanDescriptor_t
{
char szName[32];
VkDescriptorType eDescriptorType;
uint32_t uBinding;
uint32_t uSet;

View File

@@ -4,6 +4,7 @@
#include "tier1/utlstring.h"
#include "tier2/ifilesystem.h"
#include "vulkan/vulkan.h"
#include "materialsystem/vulkan_shadermeta.h"
using namespace slang;
@@ -20,6 +21,27 @@ private:
void CheckDiagnostics();
void CompileShaderStage( EShaderStage eStage, const char *szMain, ISession *pSession, IModule *pModule, CCompiledShader *pShader );
void ReadChildren( DeclReflection::IteratedList pChildren )
{
for (auto *c: pChildren)
{
if (c->getKind() == DeclReflection::Kind::Unsupported)
continue;
if (c->getKind() == DeclReflection::Kind::Namespace)
{
V_printf("Namespace: %s\n", c->getName());
ReadChildren(c->getChildren());
}
if (c->getKind() == DeclReflection::Kind::Variable)
{
ReadChildren(c->getChildren());
}
if (c->getKind() == DeclReflection::Kind::Struct)
{
V_printf("Struct: %s\n", c->getName());
}
}
}
};
EXPOSE_INTERFACE(CSlangVulkanSpirvShaderCompiler, IShaderCompiler, SLANG_SHADER_COMPILER_SPIRV_VULKAN)
@@ -31,6 +53,7 @@ void CSlangVulkanSpirvShaderCompiler::CompileShaderStage( EShaderStage eStage, c
IComponentType *pLinked = NULL;
IBlob *pBinary = NULL;
ShaderObject_t *pShaderObject = NULL;
ProgramLayout *pProgramLayout = NULL;
switch (eStage)
{
@@ -44,6 +67,30 @@ void CSlangVulkanSpirvShaderCompiler::CompileShaderStage( EShaderStage eStage, c
break;
}
pProgramLayout = pModule->getLayout();
uint32_t uCount = pProgramLayout->getParameterCount();
CUtlVector<VulkanDescriptor_t> inputs;
VulkanDescriptor_t input = {};
for ( uint32_t u = 0; u < uCount; u++ )
{
VariableLayoutReflection *pVar = pProgramLayout->getParameterByIndex(u);
input = {};
switch(pVar->getType()->getKind())
{
case slang::TypeReflection::Kind::ConstantBuffer:
input.eDescriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
break;
default:
break;
}
V_strncpy(input.szName, pVar->getName(), 32);
input.uBinding = pVar->getBindingIndex();
input.uSet = pVar->getBindingSpace();
inputs.AppendTail(input);
}
pModule->findAndCheckEntryPoint(szMain, eSlangStage, &pEntryPoint, &m_pDiagnostics);
CheckDiagnostics();
@@ -57,7 +104,14 @@ void CSlangVulkanSpirvShaderCompiler::CompileShaderStage( EShaderStage eStage, c
pShaderObject->m_eBackend = SHADER_BACKEND_SPIRV_VULKAN;
pShaderObject->m_eStage = eStage;
pShaderObject->m_nDataLump = pShader->AllocateLump(pBinary->getBufferSize());
VulkanInputMetaData_t stMetadata = {};
stMetadata.nDescriptorsCount = uCount;
stMetadata.pDescriptorSets = pShader->AllocateLump(sizeof(VulkanDescriptor_t)*inputs.GetSize());
V_memcpy(pShader->GetLumpPtr(stMetadata.pDescriptorSets), inputs.GetData(), sizeof(VulkanDescriptor_t)*inputs.GetSize());
pShaderObject->m_nMetadataLump = pShader->AllocateLump(sizeof(VulkanInputMetaData_t));
V_memcpy(pShader->GetLumpPtr(pShaderObject->m_nDataLump), pBinary->getBufferPointer(), pBinary->getBufferSize());
V_memcpy(pShader->GetLumpPtr(pShaderObject->m_nMetadataLump), &stMetadata, sizeof(VulkanInputMetaData_t));
pEntryPoint->release();
}