2
0
mirror of https://github.com/AxioDL/metaforce.git synced 2025-07-09 13:25:53 +00:00

Add perspective division to texture matrix processing

This commit is contained in:
Jack Andersen 2017-09-30 18:25:49 -10:00
parent 8b952894b1
commit 7c3bac2951
5 changed files with 55 additions and 27 deletions

2
hecl/extern/boo vendored

@ -1 +1 @@
Subproject commit 71b8893dde162f0b902b241adf30b5b19c9462c0 Subproject commit 2a49a8d4475bd97125bffacbe77b73e3724a45be

View File

@ -186,6 +186,8 @@ std::string GLSL::makeVert(const char* glslVer, unsigned col, unsigned uv, unsig
" gl_Position = proj * vtf.mvPos;\n"; " gl_Position = proj * vtf.mvPos;\n";
} }
retval += " vec4 tmpProj;\n";
int tcgIdx = 0; int tcgIdx = 0;
for (const TexCoordGen& tcg : m_tcgs) for (const TexCoordGen& tcg : m_tcgs)
{ {
@ -193,9 +195,10 @@ std::string GLSL::makeVert(const char* glslVer, unsigned col, unsigned uv, unsig
retval += hecl::Format(" vtf.tcgs[%u] = %s;\n", tcgIdx, retval += hecl::Format(" vtf.tcgs[%u] = %s;\n", tcgIdx,
EmitTexGenSource2(tcg.m_src, tcg.m_uvIdx).c_str()); EmitTexGenSource2(tcg.m_src, tcg.m_uvIdx).c_str());
else else
retval += hecl::Format(" vtf.tcgs[%u] = (texMtxs[%u].postMtx * vec4(%s((texMtxs[%u].mtx * %s).xyz), 1.0)).xy;\n", retval += hecl::Format(" tmpProj = texMtxs[%u].postMtx * vec4(%s((texMtxs[%u].mtx * %s).xyz), 1.0);\n"
tcgIdx, tcg.m_mtx, tcg.m_norm ? "normalize" : "", " vtf.tcgs[%u] = (tmpProj / tmpProj.w).xy;\n",
tcg.m_mtx, EmitTexGenSource4(tcg.m_src, tcg.m_uvIdx).c_str()); tcg.m_mtx, tcg.m_norm ? "normalize" : "",
tcg.m_mtx, EmitTexGenSource4(tcg.m_src, tcg.m_uvIdx).c_str(), tcgIdx);
++tcgIdx; ++tcgIdx;
} }
@ -206,9 +209,10 @@ std::string GLSL::makeVert(const char* glslVer, unsigned col, unsigned uv, unsig
retval += hecl::Format(" vtf.extTcgs[%u] = %s;\n", i, retval += hecl::Format(" vtf.extTcgs[%u] = %s;\n", i,
EmitTexGenSource2(extTex.src, extTex.uvIdx).c_str()); EmitTexGenSource2(extTex.src, extTex.uvIdx).c_str());
else else
retval += hecl::Format(" vtf.extTcgs[%u] = (texMtxs[%u].postMtx * vec4(%s((texMtxs[%u].mtx * %s).xyz), 1.0)).xy;\n", retval += hecl::Format(" tmpProj = texMtxs[%u].postMtx * vec4(%s((texMtxs[%u].mtx * %s).xyz), 1.0);\n"
i, extTex.mtxIdx, extTex.normalize ? "normalize" : "", " vtf.extTcgs[%u] = (tmpProj / tmpProj.w).xy;\n",
extTex.mtxIdx, EmitTexGenSource4(extTex.src, extTex.uvIdx).c_str()); extTex.mtxIdx, extTex.normalize ? "normalize" : "",
extTex.mtxIdx, EmitTexGenSource4(extTex.src, extTex.uvIdx).c_str(), i);
} }
if (reflectionType != ReflectionType::None) if (reflectionType != ReflectionType::None)

View File

@ -179,6 +179,8 @@ std::string HLSL::makeVert(unsigned col, unsigned uv, unsigned w,
" vtf.mvpPos = mul(proj, vtf.mvPos);\n"; " vtf.mvpPos = mul(proj, vtf.mvPos);\n";
} }
retval += " float4 tmpProj;\n";
int tcgIdx = 0; int tcgIdx = 0;
for (const TexCoordGen& tcg : m_tcgs) for (const TexCoordGen& tcg : m_tcgs)
{ {
@ -186,8 +188,10 @@ std::string HLSL::makeVert(unsigned col, unsigned uv, unsigned w,
retval += hecl::Format(" vtf.tcgs[%u] = %s;\n", tcgIdx, retval += hecl::Format(" vtf.tcgs[%u] = %s;\n", tcgIdx,
EmitTexGenSource2(tcg.m_src, tcg.m_uvIdx).c_str()); EmitTexGenSource2(tcg.m_src, tcg.m_uvIdx).c_str());
else else
retval += hecl::Format(" vtf.tcgs[%u] = mul(texMtxs[%u].postMtx, float4(%s(mul(texMtxs[%u].mtx, %s).xyz), 1.0)).xy;\n", tcgIdx, tcg.m_mtx, retval += hecl::Format(" tmpProj = mul(texMtxs[%u].postMtx, float4(%s(mul(texMtxs[%u].mtx, %s).xyz), 1.0));\n"
tcg.m_norm ? "normalize" : "", tcg.m_mtx, EmitTexGenSource4(tcg.m_src, tcg.m_uvIdx).c_str()); " vtf.tcgs[%u] = (tmpProj / tmpProj.w).xy;\n", tcg.m_mtx,
tcg.m_norm ? "normalize" : "", tcg.m_mtx,
EmitTexGenSource4(tcg.m_src, tcg.m_uvIdx).c_str(), tcgIdx);
++tcgIdx; ++tcgIdx;
} }
@ -198,9 +202,10 @@ std::string HLSL::makeVert(unsigned col, unsigned uv, unsigned w,
retval += hecl::Format(" vtf.extTcgs[%u] = %s;\n", i, retval += hecl::Format(" vtf.extTcgs[%u] = %s;\n", i,
EmitTexGenSource2(extTex.src, extTex.uvIdx).c_str()); EmitTexGenSource2(extTex.src, extTex.uvIdx).c_str());
else else
retval += hecl::Format(" vtf.extTcgs[%u] = mul(texMtxs[%u].postMtx, float4(%s(mul(texMtxs[%u].mtx, %s).xyz), 1.0)).xy;\n", retval += hecl::Format(" tmpProj = mul(texMtxs[%u].postMtx, float4(%s(mul(texMtxs[%u].mtx, %s).xyz), 1.0));\n"
i, extTex.mtxIdx, extTex.normalize ? "normalize" : "", " vtf.extTcgs[%u] = (tmpProj / tmpProj.w).xy;\n",
extTex.mtxIdx, EmitTexGenSource4(extTex.src, extTex.uvIdx).c_str()); extTex.mtxIdx, extTex.normalize ? "normalize" : "", extTex.mtxIdx,
EmitTexGenSource4(extTex.src, extTex.uvIdx).c_str(), i);
} }
if (reflectionType != ReflectionType::None) if (reflectionType != ReflectionType::None)
@ -235,7 +240,8 @@ std::string HLSL::makeFrag(bool alphaTest, ReflectionType reflectionType,
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 =
"SamplerState samp : register(s0);\n" + "SamplerState samp : register(s0);\n"
"SamplerState clampSamp : register(s1);\n" +
GenerateVertToFragStruct(0, reflectionType != ReflectionType::None) + GenerateVertToFragStruct(0, reflectionType != ReflectionType::None) +
texMapDecl + "\n" + texMapDecl + "\n" +
lightingSrc + "\n" + lightingSrc + "\n" +
@ -246,7 +252,7 @@ std::string HLSL::makeFrag(bool alphaTest, ReflectionType reflectionType,
if (m_lighting) if (m_lighting)
{ {
if (lighting.m_entry) if (lighting.m_entry)
retval += hecl::Format(" float4 lighting = %s(vtf.mvPos, vtf.mvNorm);\n", lighting.m_entry); retval += hecl::Format(" float4 lighting = %s(vtf.mvPos, vtf.mvNorm, vtf);\n", lighting.m_entry);
else else
retval += " float4 lighting = float4(1.0,1.0,1.0,1.0);\n"; retval += " float4 lighting = float4(1.0,1.0,1.0,1.0);\n";
} }
@ -308,7 +314,8 @@ std::string HLSL::makeFrag(bool alphaTest, ReflectionType reflectionType,
} }
std::string retval = std::string retval =
"SamplerState samp : register(s0);\n" + "SamplerState samp : register(s0);\n"
"SamplerState clampSamp : register(s1);\n" +
GenerateVertToFragStruct(extTexCount, reflectionType != ReflectionType::None) + GenerateVertToFragStruct(extTexCount, reflectionType != ReflectionType::None) +
texMapDecl + "\n" + texMapDecl + "\n" +
lightingSrc + "\n" + lightingSrc + "\n" +
@ -320,7 +327,7 @@ std::string HLSL::makeFrag(bool alphaTest, ReflectionType reflectionType,
if (m_lighting) if (m_lighting)
{ {
if (lighting.m_entry) if (lighting.m_entry)
retval += hecl::Format(" float4 lighting = %s(vtf.mvPos, vtf.mvNorm);\n", lighting.m_entry); retval += hecl::Format(" float4 lighting = %s(vtf.mvPos, vtf.mvNorm, vtf);\n", lighting.m_entry);
else else
retval += " float4 lighting = float4(1.0,1.0,1.0,1.0);\n"; retval += " float4 lighting = float4(1.0,1.0,1.0,1.0);\n";
} }

View File

@ -1,6 +1,5 @@
#include "hecl/Backend/Metal.hpp" #include "hecl/Backend/Metal.hpp"
#if BOO_HAS_METAL #if BOO_HAS_METAL
#include "hecl/Runtime.hpp"
#include <athena/MemoryReader.hpp> #include <athena/MemoryReader.hpp>
#include <athena/MemoryWriter.hpp> #include <athena/MemoryWriter.hpp>
#include <boo/graphicsdev/Metal.hpp> #include <boo/graphicsdev/Metal.hpp>
@ -190,6 +189,8 @@ std::string Metal::makeVert(unsigned col, unsigned uv, unsigned w,
" vtf.mvpPos = vu.proj * vtf.mvPos;\n"; " vtf.mvpPos = vu.proj * vtf.mvPos;\n";
} }
retval += " float4 tmpProj;\n";
int tcgIdx = 0; int tcgIdx = 0;
for (const TexCoordGen& tcg : m_tcgs) for (const TexCoordGen& tcg : m_tcgs)
{ {
@ -197,8 +198,10 @@ std::string Metal::makeVert(unsigned col, unsigned uv, unsigned w,
retval += hecl::Format(" vtf.tcgs%u = %s;\n", tcgIdx, retval += hecl::Format(" vtf.tcgs%u = %s;\n", tcgIdx,
EmitTexGenSource2(tcg.m_src, tcg.m_uvIdx).c_str()); EmitTexGenSource2(tcg.m_src, tcg.m_uvIdx).c_str());
else else
retval += hecl::Format(" vtf.tcgs%u = (texMtxs[%u].postMtx * float4(%s((texMtxs[%u].mtx * %s).xyz), 1.0)).xy;\n", tcgIdx, tcg.m_mtx, retval += hecl::Format(" tmpProj = texMtxs[%u].postMtx * float4(%s((texMtxs[%u].mtx * %s).xyz), 1.0);\n"
tcg.m_norm ? "normalize" : "", tcg.m_mtx, EmitTexGenSource4(tcg.m_src, tcg.m_uvIdx).c_str()); " vtf.tcgs%u = (tmpProj / tmpProj.w).xy;\n", tcg.m_mtx,
tcg.m_norm ? "normalize" : "", tcg.m_mtx,
EmitTexGenSource4(tcg.m_src, tcg.m_uvIdx).c_str(), tcgIdx);
++tcgIdx; ++tcgIdx;
} }
@ -209,8 +212,10 @@ std::string Metal::makeVert(unsigned col, unsigned uv, unsigned w,
retval += hecl::Format(" vtf.extTcgs%u = %s;\n", i, retval += hecl::Format(" vtf.extTcgs%u = %s;\n", i,
EmitTexGenSource2(extTex.src, extTex.uvIdx).c_str()); EmitTexGenSource2(extTex.src, extTex.uvIdx).c_str());
else else
retval += hecl::Format(" vtf.extTcgs%u = (texMtxs[%u].postMtx * float4(%s((texMtxs[%u].mtx * %s).xyz), 1.0)).xy;\n", i, extTex.mtxIdx, retval += hecl::Format(" tmpProj = texMtxs[%u].postMtx * float4(%s((texMtxs[%u].mtx * %s).xyz), 1.0);\n"
extTex.normalize ? "normalize" : "", extTex.mtxIdx, EmitTexGenSource4(extTex.src, extTex.uvIdx).c_str()); " vtf.extTcgs%u = (tmpProj / tmpProj.w).xy;\n", extTex.mtxIdx,
extTex.normalize ? "normalize" : "", extTex.mtxIdx,
EmitTexGenSource4(extTex.src, extTex.uvIdx).c_str(), i);
} }
if (reflectionType != ReflectionType::None) if (reflectionType != ReflectionType::None)
@ -250,7 +255,8 @@ std::string Metal::makeFrag(size_t blockCount, const char** blockNames, bool alp
} }
std::string retval = "#include <metal_stdlib>\nusing namespace metal;\n" std::string retval = "#include <metal_stdlib>\nusing namespace metal;\n"
"constexpr sampler samp(address::repeat, filter::linear, mip_filter::linear);\n" + "constexpr sampler samp(address::repeat, filter::linear, mip_filter::linear);\n"
"constexpr sampler clampSamp(address::clamp_to_border, border_color::opaque_white, filter::linear, mip_filter::linear);\n" +
GenerateVertToFragStruct(0, reflectionType != ReflectionType::None) + "\n" + GenerateVertToFragStruct(0, reflectionType != ReflectionType::None) + "\n" +
GenerateFragOutStruct() + "\n" + GenerateFragOutStruct() + "\n" +
lightingSrc + "\n" + lightingSrc + "\n" +
@ -276,7 +282,7 @@ std::string Metal::makeFrag(size_t blockCount, const char** blockNames, bool alp
if (m_lighting) if (m_lighting)
{ {
if (lighting.m_entry) if (lighting.m_entry)
retval += hecl::Format(" float4 lighting = %s(%s, vtf.mvPos, vtf.mvNorm);\n", lighting.m_entry, blockCall.c_str()); retval += hecl::Format(" float4 lighting = %s(%s, vtf.mvPos, vtf.mvNorm, vtf);\n", lighting.m_entry, blockCall.c_str());
else else
retval += " float4 lighting = float4(1.0,1.0,1.0,1.0);\n"; retval += " float4 lighting = float4(1.0,1.0,1.0,1.0);\n";
} }
@ -312,6 +318,10 @@ std::string Metal::makeFrag(size_t blockCount, const char** blockNames, bool alp
if (post.m_source) if (post.m_source)
postSrc = post.m_source; postSrc = post.m_source;
std::string lightingEntry;
if (lighting.m_entry)
lightingEntry = lighting.m_entry;
std::string postEntry; std::string postEntry;
if (post.m_entry) if (post.m_entry)
postEntry = post.m_entry; postEntry = post.m_entry;
@ -357,7 +367,8 @@ std::string Metal::makeFrag(size_t blockCount, const char** blockNames, bool alp
} }
std::string retval = "#include <metal_stdlib>\nusing namespace metal;\n" std::string retval = "#include <metal_stdlib>\nusing namespace metal;\n"
"constexpr sampler samp(address::repeat, filter::linear, mip_filter::linear);\n" + "constexpr sampler samp(address::repeat, filter::linear, mip_filter::linear);\n"
"constexpr sampler clampSamp(address::clamp_to_border, border_color::opaque_white, filter::linear, mip_filter::linear);\n" +
GenerateVertToFragStruct(extTexCount, reflectionType != ReflectionType::None) + "\n" + GenerateVertToFragStruct(extTexCount, reflectionType != ReflectionType::None) + "\n" +
GenerateFragOutStruct() + "\n" + GenerateFragOutStruct() + "\n" +
lightingSrc + "\n" + lightingSrc + "\n" +
@ -384,7 +395,12 @@ std::string Metal::makeFrag(size_t blockCount, const char** blockNames, bool alp
if (m_lighting) if (m_lighting)
{ {
if (lighting.m_entry) if (lighting.m_entry)
retval += hecl::Format(" float4 lighting = %s(%s, vtf.mvPos, vtf.mvNorm);\n", lighting.m_entry, blockCall.c_str()); {
if (!strncmp(lighting.m_entry, "EXT", 3))
retval += " float4 lighting = " + lightingEntry + "(" + blockCall + ", vtf.mvPos, vtf.mvNorm, vtf, " + (extTexCall.size() ? (extTexCall + ", ") : "") + ");\n";
else
retval += " float4 lighting = " + lightingEntry + "(" + blockCall + ", vtf.mvPos, vtf.mvNorm, vtf);\n";
}
else else
retval += " float4 lighting = float4(1.0,1.0,1.0,1.0);\n"; retval += " float4 lighting = float4(1.0,1.0,1.0,1.0);\n";
} }

View File

@ -98,7 +98,8 @@ struct HECLApplicationCallback : boo::IApplicationCallback
gfxF->commitTransaction([&](boo::IGraphicsDataFactory::Context& ctx) -> bool gfxF->commitTransaction([&](boo::IGraphicsDataFactory::Context& ctx) -> bool
{ {
boo::SWindowRect mainWindowRect = m_mainWindow->getWindowFrame(); boo::SWindowRect mainWindowRect = m_mainWindow->getWindowFrame();
renderTex = ctx.newRenderTexture(mainWindowRect.size[0], mainWindowRect.size[1], false, false); renderTex = ctx.newRenderTexture(mainWindowRect.size[0], mainWindowRect.size[1],
boo::TextureClampMode::Repeat, 0, 0);
/* Generate meta structure (usually statically serialized) */ /* Generate meta structure (usually statically serialized) */
hecl::HMDLMeta testMeta; hecl::HMDLMeta testMeta;
@ -149,7 +150,7 @@ struct HECLApplicationCallback : boo::IApplicationCallback
tex[i][j][3] = 0xff; tex[i][j][3] = 0xff;
} }
boo::ITexture* texture = boo::ITexture* texture =
ctx.newStaticTexture(256, 256, 1, boo::TextureFormat::RGBA8, tex, 256*256*4); ctx.newStaticTexture(256, 256, 1, boo::TextureFormat::RGBA8, boo::TextureClampMode::Repeat, tex, 256*256*4);
/* Make vertex uniform buffer */ /* Make vertex uniform buffer */
vubo = ctx.newDynamicBuffer(boo::BufferUse::Uniform, sizeof(VertexUBO), 1); vubo = ctx.newDynamicBuffer(boo::BufferUse::Uniform, sizeof(VertexUBO), 1);