Skip to content
This repository has been archived by the owner on Apr 7, 2021. It is now read-only.

Commit

Permalink
Add perspective division to texture matrix processing
Browse files Browse the repository at this point in the history
  • Loading branch information
jackoalan committed Oct 1, 2017
1 parent 0270b2c commit 6f86b57
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 27 deletions.
16 changes: 10 additions & 6 deletions lib/Backend/GLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,19 @@ std::string GLSL::makeVert(const char* glslVer, unsigned col, unsigned uv, unsig
" gl_Position = proj * vtf.mvPos;\n";
}

retval += " vec4 tmpProj;\n";

int tcgIdx = 0;
for (const TexCoordGen& tcg : m_tcgs)
{
if (tcg.m_mtx < 0)
retval += hecl::Format(" vtf.tcgs[%u] = %s;\n", tcgIdx,
EmitTexGenSource2(tcg.m_src, tcg.m_uvIdx).c_str());
else
retval += hecl::Format(" vtf.tcgs[%u] = (texMtxs[%u].postMtx * vec4(%s((texMtxs[%u].mtx * %s).xyz), 1.0)).xy;\n",
tcgIdx, tcg.m_mtx, tcg.m_norm ? "normalize" : "",
tcg.m_mtx, EmitTexGenSource4(tcg.m_src, tcg.m_uvIdx).c_str());
retval += hecl::Format(" tmpProj = texMtxs[%u].postMtx * vec4(%s((texMtxs[%u].mtx * %s).xyz), 1.0);\n"
" 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;
}

Expand All @@ -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,
EmitTexGenSource2(extTex.src, extTex.uvIdx).c_str());
else
retval += hecl::Format(" vtf.extTcgs[%u] = (texMtxs[%u].postMtx * vec4(%s((texMtxs[%u].mtx * %s).xyz), 1.0)).xy;\n",
i, extTex.mtxIdx, extTex.normalize ? "normalize" : "",
extTex.mtxIdx, EmitTexGenSource4(extTex.src, extTex.uvIdx).c_str());
retval += hecl::Format(" tmpProj = texMtxs[%u].postMtx * vec4(%s((texMtxs[%u].mtx * %s).xyz), 1.0);\n"
" 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)
Expand Down
25 changes: 16 additions & 9 deletions lib/Backend/HLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,19 @@ std::string HLSL::makeVert(unsigned col, unsigned uv, unsigned w,
" vtf.mvpPos = mul(proj, vtf.mvPos);\n";
}

retval += " float4 tmpProj;\n";

int tcgIdx = 0;
for (const TexCoordGen& tcg : m_tcgs)
{
if (tcg.m_mtx < 0)
retval += hecl::Format(" vtf.tcgs[%u] = %s;\n", tcgIdx,
EmitTexGenSource2(tcg.m_src, tcg.m_uvIdx).c_str());
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,
tcg.m_norm ? "normalize" : "", tcg.m_mtx, EmitTexGenSource4(tcg.m_src, tcg.m_uvIdx).c_str());
retval += hecl::Format(" tmpProj = mul(texMtxs[%u].postMtx, float4(%s(mul(texMtxs[%u].mtx, %s).xyz), 1.0));\n"
" 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;
}

Expand All @@ -198,9 +202,10 @@ std::string HLSL::makeVert(unsigned col, unsigned uv, unsigned w,
retval += hecl::Format(" vtf.extTcgs[%u] = %s;\n", i,
EmitTexGenSource2(extTex.src, extTex.uvIdx).c_str());
else
retval += hecl::Format(" vtf.extTcgs[%u] = mul(texMtxs[%u].postMtx, float4(%s(mul(texMtxs[%u].mtx, %s).xyz), 1.0)).xy;\n",
i, extTex.mtxIdx, extTex.normalize ? "normalize" : "",
extTex.mtxIdx, EmitTexGenSource4(extTex.src, extTex.uvIdx).c_str());
retval += hecl::Format(" tmpProj = mul(texMtxs[%u].postMtx, float4(%s(mul(texMtxs[%u].mtx, %s).xyz), 1.0));\n"
" 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)
Expand Down Expand Up @@ -235,7 +240,8 @@ std::string HLSL::makeFrag(bool alphaTest, ReflectionType reflectionType,
texMapDecl += hecl::Format("Texture2D reflectionTex : register(t%u);\n",
m_texMapEnd);
std::string retval =
"SamplerState samp : register(s0);\n" +
"SamplerState samp : register(s0);\n"
"SamplerState clampSamp : register(s1);\n" +
GenerateVertToFragStruct(0, reflectionType != ReflectionType::None) +
texMapDecl + "\n" +
lightingSrc + "\n" +
Expand All @@ -246,7 +252,7 @@ std::string HLSL::makeFrag(bool alphaTest, ReflectionType reflectionType,
if (m_lighting)
{
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
retval += " float4 lighting = float4(1.0,1.0,1.0,1.0);\n";
}
Expand Down Expand Up @@ -308,7 +314,8 @@ std::string HLSL::makeFrag(bool alphaTest, ReflectionType reflectionType,
}

std::string retval =
"SamplerState samp : register(s0);\n" +
"SamplerState samp : register(s0);\n"
"SamplerState clampSamp : register(s1);\n" +
GenerateVertToFragStruct(extTexCount, reflectionType != ReflectionType::None) +
texMapDecl + "\n" +
lightingSrc + "\n" +
Expand All @@ -320,7 +327,7 @@ std::string HLSL::makeFrag(bool alphaTest, ReflectionType reflectionType,
if (m_lighting)
{
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
retval += " float4 lighting = float4(1.0,1.0,1.0,1.0);\n";
}
Expand Down
34 changes: 25 additions & 9 deletions lib/Backend/Metal.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "hecl/Backend/Metal.hpp"
#if BOO_HAS_METAL
#include "hecl/Runtime.hpp"
#include <athena/MemoryReader.hpp>
#include <athena/MemoryWriter.hpp>
#include <boo/graphicsdev/Metal.hpp>
Expand Down Expand Up @@ -190,15 +189,19 @@ std::string Metal::makeVert(unsigned col, unsigned uv, unsigned w,
" vtf.mvpPos = vu.proj * vtf.mvPos;\n";
}

retval += " float4 tmpProj;\n";

int tcgIdx = 0;
for (const TexCoordGen& tcg : m_tcgs)
{
if (tcg.m_mtx < 0)
retval += hecl::Format(" vtf.tcgs%u = %s;\n", tcgIdx,
EmitTexGenSource2(tcg.m_src, tcg.m_uvIdx).c_str());
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,
tcg.m_norm ? "normalize" : "", tcg.m_mtx, EmitTexGenSource4(tcg.m_src, tcg.m_uvIdx).c_str());
retval += hecl::Format(" tmpProj = texMtxs[%u].postMtx * float4(%s((texMtxs[%u].mtx * %s).xyz), 1.0);\n"
" 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;
}

Expand All @@ -209,8 +212,10 @@ std::string Metal::makeVert(unsigned col, unsigned uv, unsigned w,
retval += hecl::Format(" vtf.extTcgs%u = %s;\n", i,
EmitTexGenSource2(extTex.src, extTex.uvIdx).c_str());
else
retval += hecl::Format(" vtf.extTcgs%u = (texMtxs[%u].postMtx * float4(%s((texMtxs[%u].mtx * %s).xyz), 1.0)).xy;\n", i, extTex.mtxIdx,
extTex.normalize ? "normalize" : "", extTex.mtxIdx, EmitTexGenSource4(extTex.src, extTex.uvIdx).c_str());
retval += hecl::Format(" tmpProj = texMtxs[%u].postMtx * float4(%s((texMtxs[%u].mtx * %s).xyz), 1.0);\n"
" 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)
Expand Down Expand Up @@ -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"
"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" +
GenerateFragOutStruct() + "\n" +
lightingSrc + "\n" +
Expand All @@ -276,7 +282,7 @@ std::string Metal::makeFrag(size_t blockCount, const char** blockNames, bool alp
if (m_lighting)
{
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
retval += " float4 lighting = float4(1.0,1.0,1.0,1.0);\n";
}
Expand Down Expand Up @@ -312,6 +318,10 @@ std::string Metal::makeFrag(size_t blockCount, const char** blockNames, bool alp
if (post.m_source)
postSrc = post.m_source;

std::string lightingEntry;
if (lighting.m_entry)
lightingEntry = lighting.m_entry;

std::string postEntry;
if (post.m_entry)
postEntry = post.m_entry;
Expand Down Expand Up @@ -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"
"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" +
GenerateFragOutStruct() + "\n" +
lightingSrc + "\n" +
Expand All @@ -384,7 +395,12 @@ std::string Metal::makeFrag(size_t blockCount, const char** blockNames, bool alp
if (m_lighting)
{
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
retval += " float4 lighting = float4(1.0,1.0,1.0,1.0);\n";
}
Expand Down
5 changes: 3 additions & 2 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ struct HECLApplicationCallback : boo::IApplicationCallback
gfxF->commitTransaction([&](boo::IGraphicsDataFactory::Context& ctx) -> bool
{
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) */
hecl::HMDLMeta testMeta;
Expand Down Expand Up @@ -149,7 +150,7 @@ struct HECLApplicationCallback : boo::IApplicationCallback
tex[i][j][3] = 0xff;
}
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 */
vubo = ctx.newDynamicBuffer(boo::BufferUse::Uniform, sizeof(VertexUBO), 1);
Expand Down

0 comments on commit 6f86b57

Please sign in to comment.