From 3bc6009158f7c341d399d720f58d375d97f962d0 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Fri, 31 Dec 2021 10:23:59 -0800 Subject: [PATCH] samplerjit: Refactor sampler ID calculation. Make it the same as pixel func IDs. --- GPU/Software/FuncId.cpp | 137 +++++++++++++++++++++++++ GPU/Software/FuncId.h | 3 + GPU/Software/RasterizerRectangle.cpp | 13 ++- GPU/Software/Sampler.cpp | 148 +-------------------------- GPU/Software/Sampler.h | 13 ++- 5 files changed, 158 insertions(+), 156 deletions(-) diff --git a/GPU/Software/FuncId.cpp b/GPU/Software/FuncId.cpp index b5545de6b4..37fdef66b2 100644 --- a/GPU/Software/FuncId.cpp +++ b/GPU/Software/FuncId.cpp @@ -338,3 +338,140 @@ std::string DescribePixelFuncID(const PixelFuncID &id) { desc.resize(desc.size() - 1); return desc; } + +void ComputeSamplerID(SamplerID *id_out) { + SamplerID id{}; + + id.useStandardBufw = true; + id.hasStandardMips = true; + int maxLevel = gstate.isMipmapEnabled() ? gstate.getTextureMaxLevel() : 0; + int lastWidth = -1; + for (int i = 0; i <= maxLevel; ++i) { + if (gstate.getTextureAddress(i) == 0) + id.hasInvalidPtr = true; + int w = gstate.getTextureWidth(i); + if (w != (gstate.texbufwidth[i] & 0x00001FFF)) + id.useStandardBufw = false; + if (lastWidth != -1 && lastWidth != w * 2) + id.hasStandardMips = false; + lastWidth = w; + } + id.hasAnyMips = maxLevel != 0; + + id.texfmt = gstate.getTextureFormat(); + id.swizzle = gstate.isTextureSwizzled(); + // Only CLUT4 can use separate CLUTs per mimap. + id.useSharedClut = gstate.getTextureFormat() != GE_TFMT_CLUT4 || maxLevel == 0 || !gstate.isMipmapEnabled() || gstate.isClutSharedForMipmaps(); + if (gstate.isTextureFormatIndexed()) { + id.clutfmt = gstate.getClutPaletteFormat(); + id.hasClutMask = gstate.getClutIndexMask() != 0xFF; + id.hasClutShift = gstate.getClutIndexShift() != 0; + id.hasClutOffset = gstate.getClutIndexStartPos() != 0; + } + + id.clampS = gstate.isTexCoordClampedS(); + id.clampT = gstate.isTexCoordClampedT(); + id.width0Shift = gstate.texsize[0] & 0xF; + id.height0Shift = (gstate.texsize[0] >> 8) & 0xF; + + id.useTextureAlpha = gstate.isTextureAlphaUsed(); + id.useColorDoubling = gstate.isColorDoublingEnabled(); + id.texFunc = gstate.getTextureFunction(); + if (id.texFunc > GE_TEXFUNC_ADD) + id.texFunc = GE_TEXFUNC_ADD; + + *id_out = id; +} + +std::string DescribeSamplerID(const SamplerID &id) { + std::string name; + switch (id.TexFmt()) { + case GE_TFMT_5650: name = "5650"; break; + case GE_TFMT_5551: name = "5551"; break; + case GE_TFMT_4444: name = "4444"; break; + case GE_TFMT_8888: name = "8888"; break; + case GE_TFMT_CLUT4: name = "CLUT4"; break; + case GE_TFMT_CLUT8: name = "CLUT8"; break; + case GE_TFMT_CLUT16: name = "CLUT16"; break; + case GE_TFMT_CLUT32: name = "CLUT32"; break; + case GE_TFMT_DXT1: name = "DXT1"; break; + case GE_TFMT_DXT3: name = "DXT3"; break; + case GE_TFMT_DXT5: name = "DXT5"; break; + } + switch (id.ClutFmt()) { + case GE_CMODE_16BIT_BGR5650: + switch (id.TexFmt()) { + case GE_TFMT_CLUT4: + case GE_TFMT_CLUT8: + case GE_TFMT_CLUT16: + case GE_TFMT_CLUT32: + name += ":C5650"; + break; + default: + // Ignore 0 clutfmt when no clut. + break; + } + break; + case GE_CMODE_16BIT_ABGR5551: name += ":C5551"; break; + case GE_CMODE_16BIT_ABGR4444: name += ":C4444"; break; + case GE_CMODE_32BIT_ABGR8888: name += ":C8888"; break; + } + if (id.swizzle) { + name += ":SWZ"; + } + if (!id.useSharedClut) { + name += ":CMIP"; + } + if (id.hasInvalidPtr) { + name += ":INV"; + } + if (id.hasClutMask) { + name += ":CMASK"; + } + if (id.hasClutShift) { + name += ":CSHF"; + } + if (id.hasClutOffset) { + name += ":COFF"; + } + if (id.clampS || id.clampT) { + name += std::string(":CL") + (id.clampS ? "S" : "") + (id.clampT ? "T" : ""); + } + if (!id.useStandardBufw) { + name += ":BUFW"; + } + if (!id.hasStandardMips) { + name += ":XMIP"; + } else if (id.hasAnyMips) { + name += ":MIP"; + } + if (id.linear) { + name += ":LERP"; + } + if (id.useTextureAlpha) { + name += ":A"; + } + if (id.useColorDoubling) { + name += ":DBL"; + } + switch (id.texFunc) { + case GE_TEXFUNC_MODULATE: + name += ":MOD"; + break; + case GE_TEXFUNC_DECAL: + name += ":DECAL"; + break; + case GE_TEXFUNC_BLEND: + name += ":BLEND"; + break; + case GE_TEXFUNC_REPLACE: + break; + case GE_TEXFUNC_ADD: + name += ":ADD"; + default: + break; + } + name += StringFromFormat(":W%dH%d", 1 << id.width0Shift, 1 << id.height0Shift); + + return name; +} diff --git a/GPU/Software/FuncId.h b/GPU/Software/FuncId.h index 4e47922f1c..54b6a71f5f 100644 --- a/GPU/Software/FuncId.h +++ b/GPU/Software/FuncId.h @@ -201,3 +201,6 @@ struct hash { void ComputePixelFuncID(PixelFuncID *id); std::string DescribePixelFuncID(const PixelFuncID &id); + +void ComputeSamplerID(SamplerID *id); +std::string DescribeSamplerID(const SamplerID &id); diff --git a/GPU/Software/RasterizerRectangle.cpp b/GPU/Software/RasterizerRectangle.cpp index b91f8d6157..02b1ad7465 100644 --- a/GPU/Software/RasterizerRectangle.cpp +++ b/GPU/Software/RasterizerRectangle.cpp @@ -84,8 +84,15 @@ void DrawSprite(const VertexData& v0, const VertexData& v1) { if (Memory::IsValidAddress(texaddr)) texptr = Memory::GetPointerUnchecked(texaddr); + // These look at gstate. + SamplerID samplerID; + ComputeSamplerID(&samplerID); + PixelFuncID pixelID; + ComputePixelFuncID(&pixelID); + ScreenCoords pprime(v0.screenpos.x, v0.screenpos.y, 0); - Sampler::NearestFunc nearestFunc = Sampler::GetNearestFunc(); // Looks at gstate. + Sampler::NearestFunc nearestFunc = Sampler::GetNearestFunc(samplerID); + Rasterizer::SingleFunc drawPixel = Rasterizer::GetSingleFunc(pixelID); DrawingCoords pos0 = TransformUnit::ScreenToDrawing(v0.screenpos); // Include the ending pixel based on its center, not start. @@ -99,10 +106,6 @@ void DrawSprite(const VertexData& v0, const VertexData& v1) { bool isWhite = v1.color0 == Vec4(255, 255, 255, 255); - PixelFuncID pixelID; - ComputePixelFuncID(&pixelID); - Rasterizer::SingleFunc drawPixel = Rasterizer::GetSingleFunc(pixelID); - constexpr int MIN_LINES_PER_THREAD = 32; if (gstate.isTextureMapEnabled()) { diff --git a/GPU/Software/Sampler.cpp b/GPU/Software/Sampler.cpp index 5b4a9cfb4f..4cb6a932d6 100644 --- a/GPU/Software/Sampler.cpp +++ b/GPU/Software/Sampler.cpp @@ -63,9 +63,8 @@ bool DescribeCodePtr(const u8 *ptr, std::string &name) { return true; } -NearestFunc GetNearestFunc() { - SamplerID id; - jitCache->ComputeSamplerID(&id, false); +NearestFunc GetNearestFunc(SamplerID id) { + id.linear = false; NearestFunc jitted = jitCache->GetNearest(id); if (jitted) { return jitted; @@ -74,9 +73,8 @@ NearestFunc GetNearestFunc() { return &SampleNearest; } -LinearFunc GetLinearFunc() { - SamplerID id; - jitCache->ComputeSamplerID(&id, true); +LinearFunc GetLinearFunc(SamplerID id) { + id.linear = true; LinearFunc jitted = jitCache->GetLinear(id); if (jitted) { return jitted; @@ -112,144 +110,6 @@ void SamplerJitCache::Clear() { addresses_.clear(); } -void SamplerJitCache::ComputeSamplerID(SamplerID *id_out, bool linear) { - SamplerID id{}; - - id.useStandardBufw = true; - id.hasStandardMips = true; - int maxLevel = gstate.isMipmapEnabled() ? gstate.getTextureMaxLevel() : 0; - int lastWidth = -1; - for (int i = 0; i <= maxLevel; ++i) { - if (gstate.getTextureAddress(i) == 0) - id.hasInvalidPtr = true; - int w = gstate.getTextureWidth(i); - if (w != (gstate.texbufwidth[i] & 0x00001FFF)) - id.useStandardBufw = false; - if (lastWidth != -1 && lastWidth != w * 2) - id.hasStandardMips = false; - lastWidth = w; - } - id.hasAnyMips = maxLevel != 0; - - id.texfmt = gstate.getTextureFormat(); - id.swizzle = gstate.isTextureSwizzled(); - // Only CLUT4 can use separate CLUTs per mimap. - id.useSharedClut = gstate.getTextureFormat() != GE_TFMT_CLUT4 || maxLevel == 0 || !gstate.isMipmapEnabled() || gstate.isClutSharedForMipmaps(); - if (gstate.isTextureFormatIndexed()) { - id.clutfmt = gstate.getClutPaletteFormat(); - id.hasClutMask = gstate.getClutIndexMask() != 0xFF; - id.hasClutShift = gstate.getClutIndexShift() != 0; - id.hasClutOffset = gstate.getClutIndexStartPos() != 0; - } - id.linear = linear; - - id.clampS = gstate.isTexCoordClampedS(); - id.clampT = gstate.isTexCoordClampedT(); - id.width0Shift = gstate.texsize[0] & 0xF; - id.height0Shift = (gstate.texsize[0] >> 8) & 0xF; - - id.useTextureAlpha = gstate.isTextureAlphaUsed(); - id.useColorDoubling = gstate.isColorDoublingEnabled(); - id.texFunc = gstate.getTextureFunction(); - if (id.texFunc > GE_TEXFUNC_ADD) - id.texFunc = GE_TEXFUNC_ADD; - - *id_out = id; -} - -std::string SamplerJitCache::DescribeSamplerID(const SamplerID &id) { - std::string name; - switch (id.TexFmt()) { - case GE_TFMT_5650: name = "5650"; break; - case GE_TFMT_5551: name = "5551"; break; - case GE_TFMT_4444: name = "4444"; break; - case GE_TFMT_8888: name = "8888"; break; - case GE_TFMT_CLUT4: name = "CLUT4"; break; - case GE_TFMT_CLUT8: name = "CLUT8"; break; - case GE_TFMT_CLUT16: name = "CLUT16"; break; - case GE_TFMT_CLUT32: name = "CLUT32"; break; - case GE_TFMT_DXT1: name = "DXT1"; break; - case GE_TFMT_DXT3: name = "DXT3"; break; - case GE_TFMT_DXT5: name = "DXT5"; break; - } - switch (id.ClutFmt()) { - case GE_CMODE_16BIT_BGR5650: - switch (id.TexFmt()) { - case GE_TFMT_CLUT4: - case GE_TFMT_CLUT8: - case GE_TFMT_CLUT16: - case GE_TFMT_CLUT32: - name += ":C5650"; - break; - default: - // Ignore 0 clutfmt when no clut. - break; - } - break; - case GE_CMODE_16BIT_ABGR5551: name += ":C5551"; break; - case GE_CMODE_16BIT_ABGR4444: name += ":C4444"; break; - case GE_CMODE_32BIT_ABGR8888: name += ":C8888"; break; - } - if (id.swizzle) { - name += ":SWZ"; - } - if (!id.useSharedClut) { - name += ":CMIP"; - } - if (id.hasInvalidPtr) { - name += ":INV"; - } - if (id.hasClutMask) { - name += ":CMASK"; - } - if (id.hasClutShift) { - name += ":CSHF"; - } - if (id.hasClutOffset) { - name += ":COFF"; - } - if (id.clampS || id.clampT) { - name += std::string(":CL") + (id.clampS ? "S" : "") + (id.clampT ? "T" : ""); - } - if (!id.useStandardBufw) { - name += ":BUFW"; - } - if (!id.hasStandardMips) { - name += ":XMIP"; - } else if (id.hasAnyMips) { - name += ":MIP"; - } - if (id.linear) { - name += ":LERP"; - } - if (id.useTextureAlpha) { - name += ":A"; - } - if (id.useColorDoubling) { - name += ":DBL"; - } - switch (id.texFunc) { - case GE_TEXFUNC_MODULATE: - name += ":MOD"; - break; - case GE_TEXFUNC_DECAL: - name += ":DECAL"; - break; - case GE_TEXFUNC_BLEND: - name += ":BLEND"; - break; - case GE_TEXFUNC_REPLACE: - break; - case GE_TEXFUNC_ADD: - name += ":ADD"; - default: - break; - } - name += StringFromFormat(":W%dH%d", 1 << id.width0Shift, 1 << id.height0Shift); - - return name; -} - void SamplerJitCache::Describe(const std::string &message) { descriptions_[GetCodePointer()] = message; } diff --git a/GPU/Software/Sampler.h b/GPU/Software/Sampler.h index 3b751e70d2..71e02f8269 100644 --- a/GPU/Software/Sampler.h +++ b/GPU/Software/Sampler.h @@ -34,10 +34,10 @@ namespace Sampler { #endif typedef Rasterizer::Vec4IntResult (SOFTRAST_CALL *NearestFunc)(int u, int v, const u8 *tptr, int bufw, int level); -NearestFunc GetNearestFunc(); +NearestFunc GetNearestFunc(SamplerID id); typedef Rasterizer::Vec4IntResult (SOFTRAST_CALL *LinearFunc)(float s, float t, int x, int y, Rasterizer::Vec4IntArg prim_color, const u8 **tptr, const int *bufw, int level, int levelFrac); -LinearFunc GetLinearFunc(); +LinearFunc GetLinearFunc(SamplerID id); struct Funcs { NearestFunc nearest; @@ -45,8 +45,10 @@ struct Funcs { }; static inline Funcs GetFuncs() { Funcs f; - f.nearest = GetNearestFunc(); - f.linear = GetLinearFunc(); + SamplerID id; + ComputeSamplerID(&id); + f.nearest = GetNearestFunc(id); + f.linear = GetLinearFunc(id); return f; } @@ -59,15 +61,12 @@ class SamplerJitCache : public Rasterizer::CodeBlock { public: SamplerJitCache(); - void ComputeSamplerID(SamplerID *id_out, bool linear); - // Returns a pointer to the code to run. NearestFunc GetNearest(const SamplerID &id); LinearFunc GetLinear(const SamplerID &id); void Clear(); std::string DescribeCodePtr(const u8 *ptr); - std::string DescribeSamplerID(const SamplerID &id); private: NearestFunc Compile(const SamplerID &id);