增加pipelineCreateInfo的JSON加载代码(未完工)

This commit is contained in:
hyzboy 2019-05-14 20:17:53 +08:00
parent 35e63146d1
commit f18bcfd0b4
2 changed files with 180 additions and 82 deletions

View File

@ -109,10 +109,10 @@ private:
pipeline_creater->CloseCullFace(); pipeline_creater->CloseCullFace();
pipeline_creater->Set(PRIM_TRIANGLES); pipeline_creater->Set(PRIM_TRIANGLES);
SaveToJSON(OS_TEXT("pipeline.json"),pipeline_creater->GetInfo());
pipeline=pipeline_creater->Create(); pipeline=pipeline_creater->Create();
SaveToJSON(OS_TEXT("pipeline.json"),pipeline_creater->GetInfo());
delete pipeline_creater; delete pipeline_creater;
return pipeline; return pipeline;
} }

View File

@ -8,22 +8,40 @@ using namespace hgl;
namespace namespace
{ {
#define JSON_BEGIN(struct_name) Json::Value ToJSON(const struct_name *state) \ #define TO_JSON_BEGIN(struct_name) Json::Value ToJSON(const struct_name *state) \
{ \ { \
Json::Value root; Json::Value root;
#define JSON_END return root; \ #define TO_JSON_END return root; \
} }
#define JSON_STRUCT(name) root[#name]=ToJSON(&(state->name)); #define TO_JSON_STRUCT(name) root[#name]=ToJSON(&(state->name));
#define JSON_OBJECT(name) root[#name]=ToJSON(state->name); #define TO_JSON_OBJECT(name) root[#name]=ToJSON(state->name);
#define JSON_BOOL(name) root[#name]=bool(state->name); #define TO_JSON_BOOL(name) root[#name]=bool(state->name);
#define JSON_INT32(name) root[#name]=Json::Int(state->name); #define TO_JSON_INT32(name) root[#name]=Json::Int(state->name);
#define JSON_UINT32(name) root[#name]=Json::UInt(state->name); #define TO_JSON_UINT32(name) root[#name]=Json::UInt(state->name);
#define JSON_INT64(name) root[#name]=Json::Int64(state->name); #define TO_JSON_INT64(name) root[#name]=Json::Int64(state->name);
#define JSON_UINT64(name) root[#name]=Json::UInt64(state->name); #define TO_JSON_UINT64(name) root[#name]=Json::UInt64(state->name);
#define JSON_FLOAT(name) root[#name]=state->name; #define TO_JSON_FLOAT(name) root[#name]=state->name;
#define JSON_STRING(name) root[#name]=state->name; #define TO_JSON_STRING(name) root[#name]=state->name;
#define FROM_JSON_BEGIN(struct_name) bool FromJSON(const Json::Value &root,struct_name *state) \
{ \
#define FROM_JSON_END return true; \
}
#define CHECK_MEMBER(name) if(!root.isMember(#name))return(false)
#define FROM_JSON_STRUCT(name) CHECK_MEMBER(name);if(!FromJSON(root[#name],&(state->name)))return(false);
#define FROM_JSON_OBJECT(name) CHECK_MEMBER(name);if(!FromJSON(root[#name],state->name))return(false);
#define FROM_JSON_BOOL(name) CHECK_MEMBER(name);state->name=root[#name].asBool();
#define FROM_JSON_ENUM(type,name) CHECK_MEMBER(name);state->name=type(root[#name].asUInt());
#define FROM_JSON_INT32(name) CHECK_MEMBER(name);state->name=root[#name].asInt();
#define FROM_JSON_UINT32(name) CHECK_MEMBER(name);state->name=root[#name].asUInt();
#define FROM_JSON_INT64(name) CHECK_MEMBER(name);state->name=root[#name].asInt64();
#define FROM_JSON_UINT64(name) CHECK_MEMBER(name);state->name=root[#name].asUInt64();
#define FROM_JSON_FLOAT(name) CHECK_MEMBER(name);state->name=root[#name].asFloat();
#define FROM_JSON_STRING(name) CHECK_MEMBER(name);state->name=root[#name].asCString();
template<typename T> template<typename T>
Json::Value ToJSON_Array(const T *values,const uint count) Json::Value ToJSON_Array(const T *values,const uint count)
@ -39,7 +57,19 @@ namespace
return items; return items;
} }
#define JSON_ARRAY(name,count) root[#name]=ToJSON_Array(state->name,count); #define TO_JSON_ARRAY(name,count) root[#name]=ToJSON_Array(state->name,count);
template<typename T> void FromJSON_Array(const Json::Value &root,T *object,const uint count);
template<> void FromJSON_Array(const Json::Value &root,float *object,const uint count)
{
for(uint i=0;i<count;i++)
{
*object=root[i].asFloat();
++object;
}
}
#define FROM_JSON_ARRAY(name,count) FromJSON_Array(root[#name],state->name,count);
template<typename T> template<typename T>
Json::Value ToJSON_StructArray(const T *object,const uint count) Json::Value ToJSON_StructArray(const T *object,const uint count)
@ -55,87 +85,148 @@ namespace
return items; return items;
} }
#define JSON_STRUCT_ARRAY(name,count) root[#name]=ToJSON_StructArray(state->name,count) #define TO_JSON_STRUCT_ARRAY(name,count) root[#name]=ToJSON_StructArray(state->name,count)
JSON_BEGIN(VkStencilOpState) template<typename T>
JSON_UINT32(failOp) void FromJSON_StructArray(const Json::Value &root,T *objects,const uint count)
JSON_UINT32(passOp) {
JSON_UINT32(depthFailOp) for(uint i=0;i<count;i++)
JSON_UINT32(compareOp) {
JSON_UINT32(compareMask) FromJSON(root[i],objects);
JSON_UINT32(writeMask) ++objects;
JSON_UINT32(reference) }
JSON_END }
JSON_BEGIN(VkPipelineDepthStencilStateCreateInfo) #define FROM_JSON_STRUCT_ARRAY(name,count) FromJSON_StructArray(root[#name],state->name,count);
JSON_BOOL (depthTestEnable)
JSON_BOOL (depthWriteEnable)
JSON_UINT32 (depthCompareOp)
JSON_BOOL (depthBoundsTestEnable)
JSON_BOOL (stencilTestEnable)
JSON_STRUCT (front)
JSON_STRUCT (back)
JSON_FLOAT (minDepthBounds)
JSON_FLOAT (maxDepthBounds)
JSON_END
JSON_BEGIN(VkPipelineColorBlendAttachmentState) TO_JSON_BEGIN(VkStencilOpState)
JSON_BOOL(blendEnable) TO_JSON_UINT32(failOp)
JSON_UINT32(srcColorBlendFactor) TO_JSON_UINT32(passOp)
JSON_UINT32(dstColorBlendFactor) TO_JSON_UINT32(depthFailOp)
JSON_UINT32(colorBlendOp) TO_JSON_UINT32(compareOp)
JSON_UINT32(srcAlphaBlendFactor) TO_JSON_UINT32(compareMask)
JSON_UINT32(dstAlphaBlendFactor) TO_JSON_UINT32(writeMask)
JSON_UINT32(alphaBlendOp) TO_JSON_UINT32(reference)
JSON_UINT32(colorWriteMask) TO_JSON_END
JSON_END
JSON_BEGIN(VkPipelineColorBlendStateCreateInfo) FROM_JSON_BEGIN(VkStencilOpState)
JSON_BOOL(logicOpEnable) FROM_JSON_ENUM(VkStencilOp,failOp)
JSON_UINT32(logicOp) FROM_JSON_ENUM(VkStencilOp,passOp)
JSON_ARRAY(blendConstants,4); FROM_JSON_ENUM(VkStencilOp,depthFailOp)
JSON_STRUCT_ARRAY(pAttachments,state->attachmentCount); FROM_JSON_ENUM(VkCompareOp,compareOp)
JSON_END FROM_JSON_UINT32(compareMask)
FROM_JSON_UINT32(writeMask)
FROM_JSON_UINT32(reference)
FROM_JSON_END
JSON_BEGIN(VkPipelineRasterizationStateCreateInfo) TO_JSON_BEGIN(VkPipelineDepthStencilStateCreateInfo)
JSON_BOOL(depthClampEnable) TO_JSON_BOOL (depthTestEnable)
JSON_BOOL(rasterizerDiscardEnable) TO_JSON_BOOL (depthWriteEnable)
JSON_UINT32(polygonMode) TO_JSON_UINT32 (depthCompareOp)
JSON_UINT32(cullMode) TO_JSON_BOOL (depthBoundsTestEnable)
JSON_UINT32(frontFace) TO_JSON_BOOL (stencilTestEnable)
JSON_BOOL(depthBiasEnable) TO_JSON_STRUCT (front)
JSON_FLOAT(depthBiasConstantFactor) TO_JSON_STRUCT (back)
JSON_FLOAT(depthBiasClamp) TO_JSON_FLOAT (minDepthBounds)
JSON_FLOAT(depthBiasSlopeFactor) TO_JSON_FLOAT (maxDepthBounds)
JSON_FLOAT(lineWidth) TO_JSON_END
JSON_END
JSON_BEGIN(VkPipelineMultisampleStateCreateInfo) FROM_JSON_BEGIN(VkPipelineDepthStencilStateCreateInfo)
JSON_UINT32(rasterizationSamples) FROM_JSON_BOOL (depthTestEnable)
JSON_BOOL(sampleShadingEnable) FROM_JSON_BOOL (depthWriteEnable)
JSON_FLOAT(minSampleShading) FROM_JSON_ENUM (VkCompareOp,depthCompareOp)
JSON_BOOL(alphaToCoverageEnable) FROM_JSON_BOOL (depthBoundsTestEnable)
JSON_BOOL(alphaToOneEnable) FROM_JSON_BOOL (stencilTestEnable)
JSON_END FROM_JSON_STRUCT(front)
FROM_JSON_STRUCT(back)
FROM_JSON_FLOAT (minDepthBounds)
FROM_JSON_FLOAT (maxDepthBounds)
FROM_JSON_END
JSON_BEGIN(VkPipelineInputAssemblyStateCreateInfo) TO_JSON_BEGIN(VkPipelineColorBlendAttachmentState)
JSON_UINT32(topology) TO_JSON_BOOL(blendEnable)
JSON_BOOL(primitiveRestartEnable) TO_JSON_UINT32(srcColorBlendFactor)
JSON_END TO_JSON_UINT32(dstColorBlendFactor)
TO_JSON_UINT32(colorBlendOp)
TO_JSON_UINT32(srcAlphaBlendFactor)
TO_JSON_UINT32(dstAlphaBlendFactor)
TO_JSON_UINT32(alphaBlendOp)
TO_JSON_UINT32(colorWriteMask)
TO_JSON_END
FROM_JSON_BEGIN(VkPipelineColorBlendAttachmentState)
FROM_JSON_BOOL(blendEnable)
FROM_JSON_ENUM(VkBlendFactor,srcColorBlendFactor)
FROM_JSON_ENUM(VkBlendFactor,dstColorBlendFactor)
FROM_JSON_ENUM(VkBlendOp,colorBlendOp)
FROM_JSON_ENUM(VkBlendFactor,srcAlphaBlendFactor)
FROM_JSON_ENUM(VkBlendFactor,dstAlphaBlendFactor)
FROM_JSON_ENUM(VkBlendOp,alphaBlendOp)
FROM_JSON_UINT32(colorWriteMask)
FROM_JSON_END
TO_JSON_BEGIN(VkPipelineColorBlendStateCreateInfo)
TO_JSON_BOOL(logicOpEnable)
TO_JSON_UINT32(logicOp)
TO_JSON_ARRAY(blendConstants,4);
TO_JSON_UINT32(attachmentCount);
TO_JSON_STRUCT_ARRAY(pAttachments,state->attachmentCount);
TO_JSON_END
FROM_JSON_BEGIN(VkPipelineColorBlendStateCreateInfo)
FROM_JSON_BOOL(logicOpEnable)
FROM_JSON_ENUM(VkLogicOp,logicOp)
FROM_JSON_ARRAY(blendConstants,4);
FROM_JSON_UINT32(attachmentCount);
FROM_JSON_STRUCT_ARRAY(pAttachments,state->attachmentCount);
FROM_JSON_END
TO_JSON_BEGIN(VkPipelineRasterizationStateCreateInfo)
TO_JSON_BOOL(depthClampEnable)
TO_JSON_BOOL(rasterizerDiscardEnable)
TO_JSON_UINT32(polygonMode)
TO_JSON_UINT32(cullMode)
TO_JSON_UINT32(frontFace)
TO_JSON_BOOL(depthBiasEnable)
TO_JSON_FLOAT(depthBiasConstantFactor)
TO_JSON_FLOAT(depthBiasClamp)
TO_JSON_FLOAT(depthBiasSlopeFactor)
TO_JSON_FLOAT(lineWidth)
TO_JSON_END
TO_JSON_BEGIN(VkPipelineMultisampleStateCreateInfo)
TO_JSON_UINT32(rasterizationSamples)
TO_JSON_BOOL(sampleShadingEnable)
TO_JSON_FLOAT(minSampleShading)
TO_JSON_BOOL(alphaToCoverageEnable)
TO_JSON_BOOL(alphaToOneEnable)
TO_JSON_END
TO_JSON_BEGIN(VkPipelineInputAssemblyStateCreateInfo)
TO_JSON_UINT32(topology)
TO_JSON_BOOL(primitiveRestartEnable)
TO_JSON_END
TO_JSON_BEGIN(VkGraphicsPipelineCreateInfo)
TO_JSON_OBJECT(pDepthStencilState)
TO_JSON_OBJECT(pColorBlendState)
TO_JSON_OBJECT(pRasterizationState)
TO_JSON_OBJECT(pMultisampleState)
TO_JSON_OBJECT(pInputAssemblyState)
TO_JSON_END
const Json::String STRING_VER="ver",
STRING_VULKAN="vulkan",
STRING_PIPELINE="pipeline";
}//namespace }//namespace
void SaveToJSON(const OSString &filename,const VkGraphicsPipelineCreateInfo *state) void SaveToJSON(const OSString &filename,const VkGraphicsPipelineCreateInfo *state)
{ {
Json::Value root; Json::Value root;
root["ver"]=100; root[STRING_VER]=100;
root["vulkan"]=100; root[STRING_VULKAN]=100;
root[STRING_PIPELINE]=ToJSON(state);
JSON_OBJECT(pDepthStencilState)
JSON_OBJECT(pColorBlendState)
JSON_OBJECT(pRasterizationState)
JSON_OBJECT(pMultisampleState)
JSON_OBJECT(pInputAssemblyState)
SaveJson(root,filename); SaveJson(root,filename);
} }
@ -147,5 +238,12 @@ bool LoadFromJSON(const OSString &filename,const VkGraphicsPipelineCreateInfo *s
if(!LoadJson(root,filename)) if(!LoadJson(root,filename))
return(false); return(false);
if(!root.isMember(STRING_VER)) return(false);
if(!root.isMember(STRING_VULKAN)) return(false);
if(!root.isMember(STRING_PIPELINE)) return(false);
const uint file_ver =root[STRING_VER].asUInt(),
vulkan_ver =root[STRING_VULKAN].asUInt();
FromJSON(root[STRING_PIPELINE],state);
} }