Macros for shader extensions to determine blend factors

This commit is contained in:
Jack Andersen 2018-10-20 18:13:25 -10:00
parent 808e46a4ef
commit a230eeb3b1
10 changed files with 77 additions and 9 deletions

View File

@ -31,6 +31,11 @@ def write_out_material(writebuf, mat, mesh_obj):
transparent = True transparent = True
writebuf(struct.pack('b', int(transparent))) writebuf(struct.pack('b', int(transparent)))
# If this returns true, the material geometry will be split into contiguous faces
def should_split_into_contiguous_faces(mat):
return mat.game_settings.alpha_blend != 'OPAQUE' and \
'retro_depth_sort' in mat and mat['retro_depth_sort']
# Takes a Blender 'Mesh' object (not the datablock) # Takes a Blender 'Mesh' object (not the datablock)
# and performs a one-shot conversion process to HMDL # and performs a one-shot conversion process to HMDL
def cook(writebuf, mesh_obj, output_mode, max_skin_banks, use_luv=False): def cook(writebuf, mesh_obj, output_mode, max_skin_banks, use_luv=False):
@ -125,7 +130,7 @@ def cook(writebuf, mesh_obj, output_mode, max_skin_banks, use_luv=False):
# Generate material meshes (if opaque) # Generate material meshes (if opaque)
for mat_idx in sorted_material_idxs: for mat_idx in sorted_material_idxs:
mat = mesh_obj.data.materials[mat_idx] mat = mesh_obj.data.materials[mat_idx]
if mat.game_settings.alpha_blend != 'OPAQUE': if should_split_into_contiguous_faces(mat):
continue continue
mat_faces_rem = [] mat_faces_rem = []
for face in bm_master.faces: for face in bm_master.faces:
@ -161,7 +166,7 @@ def cook(writebuf, mesh_obj, output_mode, max_skin_banks, use_luv=False):
# Generate island meshes (if transparent) # Generate island meshes (if transparent)
for mat_idx in sorted_material_idxs: for mat_idx in sorted_material_idxs:
mat = mesh_obj.data.materials[mat_idx] mat = mesh_obj.data.materials[mat_idx]
if mat.game_settings.alpha_blend == 'OPAQUE': if not should_split_into_contiguous_faces(mat):
continue continue
mat_faces_rem = [] mat_faces_rem = []
for face in bm_master.faces: for face in bm_master.faces:

View File

@ -16,10 +16,12 @@ struct GLSL : ProgrammableCommon
const TextureInfo* extTexs, ReflectionType reflectionType) const; const TextureInfo* extTexs, ReflectionType reflectionType) const;
std::string makeFrag(size_t blockCount, const char** blockNames, std::string makeFrag(size_t blockCount, const char** blockNames,
bool alphaTest, ReflectionType reflectionType, bool alphaTest, ReflectionType reflectionType,
BlendFactor srcFactor, BlendFactor dstFactor,
const Function& lighting=Function()) const; const Function& lighting=Function()) const;
std::string makeFrag(size_t blockCount, const char** blockNames, std::string makeFrag(size_t blockCount, const char** blockNames,
bool alphaTest, bool alphaTest,
ReflectionType reflectionType, ReflectionType reflectionType,
BlendFactor srcFactor, BlendFactor dstFactor,
const Function& lighting, const Function& lighting,
const Function& post, const Function& post,
size_t extTexCount, const TextureInfo* extTexs) const; size_t extTexCount, const TextureInfo* extTexs) const;

View File

@ -13,9 +13,11 @@ struct HLSL : ProgrammableCommon
const TextureInfo* extTexs, ReflectionType reflectionType) const; const TextureInfo* extTexs, ReflectionType reflectionType) const;
std::string makeFrag(size_t blockCount, const char** blockNames, std::string makeFrag(size_t blockCount, const char** blockNames,
bool alphaTest, ReflectionType reflectionType, bool alphaTest, ReflectionType reflectionType,
BlendFactor srcFactor, BlendFactor dstFactor,
const Function& lighting=Function()) const; const Function& lighting=Function()) const;
std::string makeFrag(size_t blockCount, const char** blockNames, std::string makeFrag(size_t blockCount, const char** blockNames,
bool alphaTest, ReflectionType reflectionType, bool alphaTest, ReflectionType reflectionType,
BlendFactor srcFactor, BlendFactor dstFactor,
const Function& lighting, const Function& lighting,
const Function& post, size_t extTexCount, const Function& post, size_t extTexCount,
const TextureInfo* extTexs) const; const TextureInfo* extTexs) const;

View File

@ -13,9 +13,12 @@ struct Metal : ProgrammableCommon
const TextureInfo* extTexs, ReflectionType reflectionType) const; const TextureInfo* extTexs, ReflectionType reflectionType) const;
std::string makeFrag(size_t blockCount, const char** blockNames, bool alphaTest, std::string makeFrag(size_t blockCount, const char** blockNames, bool alphaTest,
ReflectionType reflectionType, ReflectionType reflectionType,
BlendFactor srcFactor, BlendFactor dstFactor,
const Function& lighting=Function()) const; const Function& lighting=Function()) const;
std::string makeFrag(size_t blockCount, const char** blockNames, bool alphaTest, std::string makeFrag(size_t blockCount, const char** blockNames, bool alphaTest,
ReflectionType reflectionType, const Function& lighting, ReflectionType reflectionType,
BlendFactor srcFactor, BlendFactor dstFactor,
const Function& lighting,
const Function& post, size_t extTexCount, const Function& post, size_t extTexCount,
const TextureInfo* extTexs) const; const TextureInfo* extTexs) const;

View File

@ -42,6 +42,8 @@ struct ProgrammableCommon : IBackend
using IBackend::reset; using IBackend::reset;
void reset(const IR& ir, Diagnostics& diag, const char* backendName); void reset(const IR& ir, Diagnostics& diag, const char* backendName);
static const char* BlendFactorToDefine(BlendFactor factor, BlendFactor defaultFactor);
private: private:
unsigned addTexCoordGen(TexGenSrc src, int uvIdx, int mtx, bool normalize); unsigned addTexCoordGen(TexGenSrc src, int uvIdx, int mtx, bool normalize);
unsigned addTexSampling(unsigned mapIdx, unsigned tcgIdx); unsigned addTexSampling(unsigned mapIdx, unsigned tcgIdx);

View File

@ -79,7 +79,8 @@ public:
{ {
return m_backend.makeFrag(m_extension.blockCount, m_extension.blockNames, return m_backend.makeFrag(m_extension.blockCount, m_extension.blockNames,
m_tag.getDepthWrite() && m_backend.m_blendDst == hecl::Backend::BlendFactor::InvSrcAlpha, m_tag.getDepthWrite() && m_backend.m_blendDst == hecl::Backend::BlendFactor::InvSrcAlpha,
m_tag.getReflectionType(), m_extension.lighting, m_extension.post, m_tag.getReflectionType(), m_backend.m_blendSrc, m_backend.m_blendDst,
m_extension.lighting, m_extension.post,
m_extension.texCount, m_extension.texs); m_extension.texCount, m_extension.texs);
} }
const hecl::Backend::ShaderTag& getTag() const { return m_tag; } const hecl::Backend::ShaderTag& getTag() const { return m_tag; }

View File

@ -221,7 +221,8 @@ std::string GLSL::makeVert(unsigned col, unsigned uv, unsigned w,
} }
std::string GLSL::makeFrag(size_t blockCount, const char** blockNames, bool alphaTest, std::string GLSL::makeFrag(size_t blockCount, const char** blockNames, bool alphaTest,
ReflectionType reflectionType, const Function& lighting) const ReflectionType reflectionType, BlendFactor srcFactor, BlendFactor dstFactor,
const Function& lighting) const
{ {
std::string lightingSrc; std::string lightingSrc;
if (!lighting.m_source.empty()) if (!lighting.m_source.empty())
@ -246,6 +247,8 @@ std::string GLSL::makeFrag(size_t blockCount, const char** blockNames, bool alph
std::string retval = std::string retval =
std::string("#extension GL_ARB_shader_image_load_store: enable\n") + std::string("#extension GL_ARB_shader_image_load_store: enable\n") +
"#define BLEND_SRC_" + BlendFactorToDefine(srcFactor, m_blendSrc) + "\n" +
"#define BLEND_DST_" + BlendFactorToDefine(dstFactor, m_blendDst) + "\n" +
GenerateVertToFragStruct(0, reflectionType != ReflectionType::None) + GenerateVertToFragStruct(0, reflectionType != ReflectionType::None) +
(!alphaTest ? (!alphaTest ?
"#ifdef GL_ARB_shader_image_load_store\n" "#ifdef GL_ARB_shader_image_load_store\n"
@ -285,6 +288,7 @@ std::string GLSL::makeFrag(size_t blockCount, const char** blockNames, bool alph
std::string GLSL::makeFrag(size_t blockCount, const char** blockNames, std::string GLSL::makeFrag(size_t blockCount, const char** blockNames,
bool alphaTest, bool alphaTest,
ReflectionType reflectionType, ReflectionType reflectionType,
BlendFactor srcFactor, BlendFactor dstFactor,
const Function& lighting, const Function& lighting,
const Function& post, const Function& post,
size_t extTexCount, const TextureInfo* extTexs) const size_t extTexCount, const TextureInfo* extTexs) const
@ -327,6 +331,8 @@ std::string GLSL::makeFrag(size_t blockCount, const char** blockNames,
std::string retval = std::string retval =
std::string("#extension GL_ARB_shader_image_load_store: enable\n") + std::string("#extension GL_ARB_shader_image_load_store: enable\n") +
"#define BLEND_SRC_" + BlendFactorToDefine(srcFactor, m_blendSrc) + "\n" +
"#define BLEND_DST_" + BlendFactorToDefine(dstFactor, m_blendDst) + "\n" +
GenerateVertToFragStruct(extTexCount, reflectionType != ReflectionType::None) + GenerateVertToFragStruct(extTexCount, reflectionType != ReflectionType::None) +
(!alphaTest ? (!alphaTest ?
"\n#ifdef GL_ARB_shader_image_load_store\n" "\n#ifdef GL_ARB_shader_image_load_store\n"

View File

@ -216,6 +216,7 @@ std::string HLSL::makeVert(unsigned col, unsigned uv, unsigned w,
std::string HLSL::makeFrag(size_t blockCount, const char** blockNames, std::string HLSL::makeFrag(size_t blockCount, const char** blockNames,
bool alphaTest, ReflectionType reflectionType, bool alphaTest, ReflectionType reflectionType,
BlendFactor srcFactor, BlendFactor dstFactor,
const Function& lighting) const const Function& lighting) const
{ {
std::string lightingSrc; std::string lightingSrc;
@ -238,6 +239,8 @@ std::string HLSL::makeFrag(size_t blockCount, const char** blockNames,
texMapDecl += hecl::Format("Texture2D reflectionTex : register(t%u);\n", texMapDecl += hecl::Format("Texture2D reflectionTex : register(t%u);\n",
m_texMapEnd); m_texMapEnd);
std::string retval = std::string retval =
std::string("#define BLEND_SRC_") + BlendFactorToDefine(srcFactor, m_blendSrc) + "\n" +
"#define BLEND_DST_" + BlendFactorToDefine(dstFactor, m_blendDst) + "\n" +
"SamplerState samp : register(s0);\n" "SamplerState samp : register(s0);\n"
"SamplerState clampSamp : register(s1);\n" "SamplerState clampSamp : register(s1);\n"
"SamplerState reflectSamp : register(s2);\n" + "SamplerState reflectSamp : register(s2);\n" +
@ -275,6 +278,7 @@ std::string HLSL::makeFrag(size_t blockCount, const char** blockNames,
std::string HLSL::makeFrag(size_t blockCount, const char** blockNames, std::string HLSL::makeFrag(size_t blockCount, const char** blockNames,
bool alphaTest, ReflectionType reflectionType, bool alphaTest, ReflectionType reflectionType,
BlendFactor srcFactor, BlendFactor dstFactor,
const Function& lighting, const Function& lighting,
const Function& post, size_t extTexCount, const Function& post, size_t extTexCount,
const TextureInfo* extTexs) const const TextureInfo* extTexs) const
@ -315,6 +319,8 @@ std::string HLSL::makeFrag(size_t blockCount, const char** blockNames,
} }
std::string retval = std::string retval =
std::string("#define BLEND_SRC_") + BlendFactorToDefine(srcFactor, m_blendSrc) + "\n" +
"#define BLEND_DST_" + BlendFactorToDefine(dstFactor, m_blendDst) + "\n" +
"SamplerState samp : register(s0);\n" "SamplerState samp : register(s0);\n"
"SamplerState clampSamp : register(s1);\n" "SamplerState clampSamp : register(s1);\n"
"SamplerState reflectSamp : register(s2);\n" + "SamplerState reflectSamp : register(s2);\n" +

View File

@ -222,7 +222,9 @@ std::string Metal::makeVert(unsigned col, unsigned uv, unsigned w,
} }
std::string Metal::makeFrag(size_t blockCount, const char** blockNames, bool alphaTest, std::string Metal::makeFrag(size_t blockCount, const char** blockNames, bool alphaTest,
ReflectionType reflectionType, const Function& lighting) const ReflectionType reflectionType,
BlendFactor srcFactor, BlendFactor dstFactor,
const Function& lighting) const
{ {
std::string lightingSrc; std::string lightingSrc;
if (!lighting.m_source.empty()) if (!lighting.m_source.empty())
@ -249,7 +251,9 @@ std::string Metal::makeFrag(size_t blockCount, const char** blockNames, bool alp
blockCall += hecl::Format("block%" PRISize, i); blockCall += hecl::Format("block%" PRISize, i);
} }
std::string retval = "#include <metal_stdlib>\nusing namespace metal;\n" + std::string retval = std::string("#include <metal_stdlib>\nusing namespace metal;\n") +
"#define BLEND_SRC_" + BlendFactorToDefine(srcFactor, m_blendSrc) + "\n" +
"#define BLEND_DST_" + BlendFactorToDefine(dstFactor, m_blendDst) + "\n" +
GenerateVertToFragStruct(0, reflectionType != ReflectionType::None) + "\n" + GenerateVertToFragStruct(0, reflectionType != ReflectionType::None) + "\n" +
GenerateFragOutStruct() + "\n" + GenerateFragOutStruct() + "\n" +
lightingSrc + "\n" + lightingSrc + "\n" +
@ -301,7 +305,9 @@ std::string Metal::makeFrag(size_t blockCount, const char** blockNames, bool alp
} }
std::string Metal::makeFrag(size_t blockCount, const char** blockNames, bool alphaTest, std::string Metal::makeFrag(size_t blockCount, const char** blockNames, bool alphaTest,
ReflectionType reflectionType, const Function& lighting, ReflectionType reflectionType,
BlendFactor srcFactor, BlendFactor dstFactor,
const Function& lighting,
const Function& post, size_t extTexCount, const Function& post, size_t extTexCount,
const TextureInfo* extTexs) const const TextureInfo* extTexs) const
{ {
@ -361,7 +367,9 @@ std::string Metal::makeFrag(size_t blockCount, const char** blockNames, bool alp
blockCall += hecl::Format("block%" PRISize, i); blockCall += hecl::Format("block%" PRISize, i);
} }
std::string retval = "#include <metal_stdlib>\nusing namespace metal;\n" + std::string retval = std::string("#include <metal_stdlib>\nusing namespace metal;\n") +
"#define BLEND_SRC_" + BlendFactorToDefine(srcFactor, m_blendSrc) + "\n" +
"#define BLEND_DST_" + BlendFactorToDefine(dstFactor, m_blendDst) + "\n" +
GenerateVertToFragStruct(extTexCount, reflectionType != ReflectionType::None) + "\n" + GenerateVertToFragStruct(extTexCount, reflectionType != ReflectionType::None) + "\n" +
GenerateFragOutStruct() + "\n" + GenerateFragOutStruct() + "\n" +
lightingSrc + "\n" + lightingSrc + "\n" +

View File

@ -4,6 +4,39 @@
namespace hecl::Backend namespace hecl::Backend
{ {
const char* ProgrammableCommon::BlendFactorToDefine(BlendFactor factor, BlendFactor defaultFactor)
{
switch (factor)
{
case BlendFactor::Zero:
return "ZERO";
case BlendFactor::One:
return "ONE";
case BlendFactor::SrcColor:
return "SRCCOLOR";
case BlendFactor::InvSrcColor:
return "INVSRCCOLOR";
case BlendFactor::DstColor:
return "DSTCOLOR";
case BlendFactor::InvDstColor:
return "INVDSTCOLOR";
case BlendFactor::SrcAlpha:
return "SRCALPHA";
case BlendFactor::InvSrcAlpha:
return "INVSRCALPHA";
case BlendFactor::DstAlpha:
return "DSTALPHA";
case BlendFactor::InvDstAlpha:
return "INVDSTALPHA";
case BlendFactor::SrcColor1:
return "SRCCOLOR1";
case BlendFactor::InvSrcColor1:
return "INVSRCCOLOR1";
default:
return BlendFactorToDefine(defaultFactor, BlendFactor::Zero);
}
}
unsigned ProgrammableCommon::addTexCoordGen(TexGenSrc src, int uvIdx, int mtx, bool normalize) unsigned ProgrammableCommon::addTexCoordGen(TexGenSrc src, int uvIdx, int mtx, bool normalize)
{ {
for (unsigned i=0 ; i<m_tcgs.size() ; ++i) for (unsigned i=0 ; i<m_tcgs.size() ; ++i)