2018-10-07 02:53:57 +00:00
|
|
|
#include "hecl/Pipeline.hpp"
|
2019-09-04 21:22:05 +00:00
|
|
|
|
|
|
|
#include <athena/FileReader.hpp>
|
2018-10-07 02:53:57 +00:00
|
|
|
#include <zlib.h>
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
namespace hecl {
|
2018-10-07 02:53:57 +00:00
|
|
|
|
|
|
|
#if HECL_RUNTIME
|
|
|
|
|
|
|
|
PipelineConverterBase* conv = nullptr;
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
class ShaderCacheZipStream : public athena::io::IStreamReader {
|
|
|
|
std::unique_ptr<uint8_t[]> m_compBuf;
|
|
|
|
athena::io::FileReader m_reader;
|
|
|
|
z_stream m_zstrm = {};
|
|
|
|
|
2018-10-07 02:53:57 +00:00
|
|
|
public:
|
2021-06-30 18:20:45 +00:00
|
|
|
explicit ShaderCacheZipStream(const char* path) : m_reader(path) {
|
2018-12-08 05:18:42 +00:00
|
|
|
if (m_reader.hasError())
|
|
|
|
return;
|
|
|
|
if (m_reader.readUint32Big() != SBIG('SHAD'))
|
|
|
|
return;
|
|
|
|
m_compBuf.reset(new uint8_t[4096]);
|
|
|
|
m_zstrm.next_in = m_compBuf.get();
|
|
|
|
m_zstrm.avail_in = 0;
|
|
|
|
inflateInit(&m_zstrm);
|
|
|
|
}
|
2019-08-21 22:20:07 +00:00
|
|
|
~ShaderCacheZipStream() override { inflateEnd(&m_zstrm); }
|
2019-08-21 21:31:20 +00:00
|
|
|
explicit operator bool() const { return m_compBuf.operator bool(); }
|
2019-08-21 22:20:07 +00:00
|
|
|
atUint64 readUBytesToBuf(void* buf, atUint64 len) override {
|
2018-12-08 05:18:42 +00:00
|
|
|
m_zstrm.next_out = (Bytef*)buf;
|
|
|
|
m_zstrm.avail_out = len;
|
|
|
|
m_zstrm.total_out = 0;
|
|
|
|
while (m_zstrm.avail_out != 0) {
|
|
|
|
if (m_zstrm.avail_in == 0) {
|
|
|
|
atUint64 readSz = m_reader.readUBytesToBuf(m_compBuf.get(), 4096);
|
|
|
|
m_zstrm.avail_in = readSz;
|
2018-10-07 02:53:57 +00:00
|
|
|
m_zstrm.next_in = m_compBuf.get();
|
2018-12-08 05:18:42 +00:00
|
|
|
}
|
|
|
|
int inflateRet = inflate(&m_zstrm, Z_NO_FLUSH);
|
|
|
|
if (inflateRet != Z_OK)
|
|
|
|
break;
|
2018-10-07 02:53:57 +00:00
|
|
|
}
|
2018-12-08 05:18:42 +00:00
|
|
|
return m_zstrm.total_out;
|
|
|
|
}
|
2019-08-21 22:20:07 +00:00
|
|
|
void seek(atInt64, athena::SeekOrigin) override {}
|
|
|
|
atUint64 position() const override { return 0; }
|
|
|
|
atUint64 length() const override { return 0; }
|
2018-10-07 02:53:57 +00:00
|
|
|
};
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
template <typename P, typename S>
|
|
|
|
void StageConverter<P, S>::loadFromStream(FactoryCtx& ctx, ShaderCacheZipStream& r) {
|
|
|
|
uint32_t count = r.readUint32Big();
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
|
|
|
uint64_t hash = r.readUint64Big();
|
|
|
|
uint32_t size = r.readUint32Big();
|
|
|
|
StageBinaryData data = MakeStageBinaryData(size);
|
|
|
|
r.readUBytesToBuf(data.get(), size);
|
|
|
|
m_stageCache.insert(std::make_pair(hash, Do<StageTargetTp>(ctx, StageBinary<P, S>(data, size))));
|
|
|
|
}
|
2018-10-07 02:53:57 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
static boo::AdditionalPipelineInfo ReadAdditionalInfo(ShaderCacheZipStream& r) {
|
|
|
|
boo::AdditionalPipelineInfo additionalInfo;
|
|
|
|
additionalInfo.srcFac = boo::BlendFactor(r.readUint32Big());
|
|
|
|
additionalInfo.dstFac = boo::BlendFactor(r.readUint32Big());
|
|
|
|
additionalInfo.prim = boo::Primitive(r.readUint32Big());
|
|
|
|
additionalInfo.depthTest = boo::ZTest(r.readUint32Big());
|
|
|
|
additionalInfo.depthWrite = r.readBool();
|
|
|
|
additionalInfo.colorWrite = r.readBool();
|
|
|
|
additionalInfo.alphaWrite = r.readBool();
|
|
|
|
additionalInfo.culling = boo::CullMode(r.readUint32Big());
|
|
|
|
additionalInfo.patchSize = r.readUint32Big();
|
|
|
|
additionalInfo.overwriteAlpha = r.readBool();
|
|
|
|
additionalInfo.depthAttachment = r.readBool();
|
|
|
|
return additionalInfo;
|
2018-10-07 02:53:57 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
static std::vector<boo::VertexElementDescriptor> ReadVertexFormat(ShaderCacheZipStream& r) {
|
|
|
|
std::vector<boo::VertexElementDescriptor> ret;
|
|
|
|
uint32_t count = r.readUint32Big();
|
|
|
|
ret.reserve(count);
|
2018-10-07 02:53:57 +00:00
|
|
|
|
2019-06-12 02:01:19 +00:00
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2018-12-08 05:18:42 +00:00
|
|
|
ret.emplace_back();
|
|
|
|
ret.back().semantic = boo::VertexSemantic(r.readUint32Big());
|
|
|
|
ret.back().semanticIdx = int(r.readUint32Big());
|
|
|
|
}
|
2018-10-07 02:53:57 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2018-10-07 02:53:57 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
template <typename P>
|
2021-06-30 18:20:45 +00:00
|
|
|
bool PipelineConverter<P>::loadFromFile(FactoryCtx& ctx, const char* path) {
|
2018-12-08 05:18:42 +00:00
|
|
|
ShaderCacheZipStream r(path);
|
|
|
|
if (!r)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_vertexConverter.loadFromStream(ctx, r);
|
|
|
|
m_fragmentConverter.loadFromStream(ctx, r);
|
|
|
|
m_geometryConverter.loadFromStream(ctx, r);
|
|
|
|
m_controlConverter.loadFromStream(ctx, r);
|
|
|
|
m_evaluationConverter.loadFromStream(ctx, r);
|
|
|
|
|
|
|
|
uint32_t count = r.readUint32Big();
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
|
|
|
uint64_t hash = r.readUint64Big();
|
|
|
|
StageRuntimeObject<P, PipelineStage::Vertex> vertex;
|
|
|
|
StageRuntimeObject<P, PipelineStage::Fragment> fragment;
|
|
|
|
StageRuntimeObject<P, PipelineStage::Geometry> geometry;
|
|
|
|
StageRuntimeObject<P, PipelineStage::Control> control;
|
|
|
|
StageRuntimeObject<P, PipelineStage::Evaluation> evaluation;
|
|
|
|
if (uint64_t vhash = r.readUint64Big())
|
|
|
|
vertex = m_vertexConverter.m_stageCache.find(vhash)->second;
|
|
|
|
if (uint64_t fhash = r.readUint64Big())
|
|
|
|
fragment = m_fragmentConverter.m_stageCache.find(fhash)->second;
|
|
|
|
if (uint64_t ghash = r.readUint64Big())
|
|
|
|
geometry = m_geometryConverter.m_stageCache.find(ghash)->second;
|
|
|
|
if (uint64_t chash = r.readUint64Big())
|
|
|
|
control = m_controlConverter.m_stageCache.find(chash)->second;
|
|
|
|
if (uint64_t ehash = r.readUint64Big())
|
|
|
|
evaluation = m_evaluationConverter.m_stageCache.find(ehash)->second;
|
|
|
|
|
|
|
|
boo::AdditionalPipelineInfo additionalInfo = ReadAdditionalInfo(r);
|
|
|
|
std::vector<boo::VertexElementDescriptor> vtxFmt = ReadVertexFormat(r);
|
|
|
|
|
|
|
|
m_pipelineCache.insert(
|
|
|
|
std::make_pair(hash, FinalPipeline<P>(*this, ctx,
|
|
|
|
StageCollection<StageRuntimeObject<P, PipelineStage::Null>>(
|
|
|
|
vertex, fragment, geometry, control, evaluation, additionalInfo,
|
|
|
|
boo::VertexFormatInfo(vtxFmt.size(), vtxFmt.data())))));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-10-07 02:53:57 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
#define SPECIALIZE_STAGE_CONVERTER(P) \
|
|
|
|
template class StageConverter<P, PipelineStage::Vertex>; \
|
|
|
|
template class StageConverter<P, PipelineStage::Fragment>; \
|
|
|
|
template class StageConverter<P, PipelineStage::Geometry>; \
|
|
|
|
template class StageConverter<P, PipelineStage::Control>; \
|
|
|
|
template class StageConverter<P, PipelineStage::Evaluation>;
|
2018-10-07 02:53:57 +00:00
|
|
|
|
|
|
|
#if BOO_HAS_GL
|
|
|
|
template class PipelineConverter<PlatformType::OpenGL>;
|
|
|
|
SPECIALIZE_STAGE_CONVERTER(PlatformType::OpenGL)
|
|
|
|
#endif
|
|
|
|
#if BOO_HAS_VULKAN
|
|
|
|
template class PipelineConverter<PlatformType::Vulkan>;
|
|
|
|
SPECIALIZE_STAGE_CONVERTER(PlatformType::Vulkan)
|
|
|
|
#endif
|
|
|
|
#if _WIN32
|
|
|
|
template class PipelineConverter<PlatformType::D3D11>;
|
|
|
|
SPECIALIZE_STAGE_CONVERTER(PlatformType::D3D11)
|
|
|
|
#endif
|
|
|
|
#if BOO_HAS_METAL
|
|
|
|
template class PipelineConverter<PlatformType::Metal>;
|
|
|
|
SPECIALIZE_STAGE_CONVERTER(PlatformType::Metal)
|
|
|
|
#endif
|
|
|
|
#if BOO_HAS_NX
|
|
|
|
template class PipelineConverter<PlatformType::NX>;
|
|
|
|
SPECIALIZE_STAGE_CONVERTER(PlatformType::NX)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2021-05-24 21:25:31 +00:00
|
|
|
} // namespace hecl
|