Added SPIRV reflection information to KRShader
This commit is contained in:
@@ -30,12 +30,16 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#include "KRShader.h"
|
#include "KRShader.h"
|
||||||
|
#include "spirv_reflect.h"
|
||||||
|
|
||||||
KRShader::KRShader(KRContext &context, std::string name, std::string extension) : KRResource(context, name)
|
KRShader::KRShader(KRContext &context, std::string name, std::string extension) : KRResource(context, name)
|
||||||
{
|
{
|
||||||
m_pData = new KRDataBlock();
|
m_pData = new KRDataBlock();
|
||||||
m_extension = extension;
|
m_extension = extension;
|
||||||
m_subExtension = KRResource::GetFileExtension(name);
|
m_subExtension = KRResource::GetFileExtension(name);
|
||||||
|
m_reflectionValid = false;
|
||||||
|
|
||||||
|
getReflection();
|
||||||
}
|
}
|
||||||
|
|
||||||
KRShader::KRShader(KRContext &context, std::string name, std::string extension, KRDataBlock *data) : KRResource(context, name)
|
KRShader::KRShader(KRContext &context, std::string name, std::string extension, KRDataBlock *data) : KRResource(context, name)
|
||||||
@@ -43,10 +47,12 @@ KRShader::KRShader(KRContext &context, std::string name, std::string extension,
|
|||||||
m_pData = data;
|
m_pData = data;
|
||||||
m_extension = extension;
|
m_extension = extension;
|
||||||
m_subExtension = KRResource::GetFileExtension(name);
|
m_subExtension = KRResource::GetFileExtension(name);
|
||||||
|
m_reflectionValid = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
KRShader::~KRShader()
|
KRShader::~KRShader()
|
||||||
{
|
{
|
||||||
|
freeReflection();
|
||||||
delete m_pData;
|
delete m_pData;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,3 +106,39 @@ bool KRShader::createShaderModule(VkDevice& device, VkShaderModule& module)
|
|||||||
#endif // KRENGINE_DEBUG_GPU_LABELS
|
#endif // KRENGINE_DEBUG_GPU_LABELS
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void KRShader::parseReflection()
|
||||||
|
{
|
||||||
|
if (m_reflectionValid) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
m_pData->lock();
|
||||||
|
|
||||||
|
// Generate reflection data for a shader
|
||||||
|
SpvReflectResult result = spvReflectCreateShaderModule(m_pData->getSize(), m_pData->getStart(), &m_reflection);
|
||||||
|
if (result != SPV_REFLECT_RESULT_SUCCESS) {
|
||||||
|
// TODO - Log error
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_pData->unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
void KRShader::freeReflection()
|
||||||
|
{
|
||||||
|
if (!m_reflectionValid) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
spvReflectDestroyShaderModule(&m_reflection);
|
||||||
|
m_reflectionValid = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const SpvReflectShaderModule* KRShader::getReflection()
|
||||||
|
{
|
||||||
|
parseReflection();
|
||||||
|
if (m_reflectionValid) {
|
||||||
|
return &m_reflection;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|||||||
@@ -36,6 +36,7 @@
|
|||||||
#include "KRContextObject.h"
|
#include "KRContextObject.h"
|
||||||
#include "KRDataBlock.h"
|
#include "KRDataBlock.h"
|
||||||
#include "KRResource.h"
|
#include "KRResource.h"
|
||||||
|
#include "spirv_reflect.h"
|
||||||
|
|
||||||
class KRShader : public KRResource {
|
class KRShader : public KRResource {
|
||||||
|
|
||||||
@@ -52,12 +53,18 @@ public:
|
|||||||
virtual bool save(KRDataBlock &data);
|
virtual bool save(KRDataBlock &data);
|
||||||
|
|
||||||
KRDataBlock *getData();
|
KRDataBlock *getData();
|
||||||
|
const SpvReflectShaderModule* getReflection();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
std::string m_extension;
|
std::string m_extension;
|
||||||
std::string m_subExtension;
|
std::string m_subExtension;
|
||||||
KRDataBlock *m_pData;
|
KRDataBlock *m_pData;
|
||||||
|
SpvReflectShaderModule m_reflection;
|
||||||
|
bool m_reflectionValid;
|
||||||
|
|
||||||
|
void parseReflection();
|
||||||
|
void freeReflection();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* defined(KRSHADER_H) */
|
#endif /* defined(KRSHADER_H) */
|
||||||
|
|||||||
Reference in New Issue
Block a user