From 9ef228503abe24820985e431ef942c6ebb312442 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Sat, 24 Jul 2021 15:58:55 -0700 Subject: [PATCH] VideoCommon: Provide raw texdims to shaders --- Source/Core/VideoCommon/ConstantManager.h | 2 +- Source/Core/VideoCommon/PixelShaderGen.cpp | 11 ++++++----- Source/Core/VideoCommon/PixelShaderManager.cpp | 13 +++++-------- Source/Core/VideoCommon/UberShaderPixel.cpp | 9 +++++---- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/Source/Core/VideoCommon/ConstantManager.h b/Source/Core/VideoCommon/ConstantManager.h index fe80767112..6046450dec 100644 --- a/Source/Core/VideoCommon/ConstantManager.h +++ b/Source/Core/VideoCommon/ConstantManager.h @@ -21,7 +21,7 @@ struct PixelShaderConstants std::array colors; std::array kcolors; int4 alpha; - std::array texdims; + std::array texdims; std::array zbias; std::array indtexscale; std::array indtexmtx; diff --git a/Source/Core/VideoCommon/PixelShaderGen.cpp b/Source/Core/VideoCommon/PixelShaderGen.cpp index 97adb46a97..6f11c605fd 100644 --- a/Source/Core/VideoCommon/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/PixelShaderGen.cpp @@ -393,7 +393,7 @@ void WritePixelShaderCommonHeader(ShaderCode& out, APIType api_type, out.Write("\tint4 " I_COLORS "[4];\n" "\tint4 " I_KCOLORS "[4];\n" "\tint4 " I_ALPHA ";\n" - "\tfloat4 " I_TEXDIMS "[8];\n" + "\tint4 " I_TEXDIMS "[8];\n" "\tint4 " I_ZBIAS "[2];\n" "\tint4 " I_INDTEXSCALE "[2];\n" "\tint4 " I_INDTEXMTX "[6];\n" @@ -812,7 +812,7 @@ ShaderCode GeneratePixelShaderCode(APIType api_type, const ShaderHostConfig& hos { out.Write("\tint2 fixpoint_uv{} = int2(", i); out.Write("(tex{}.z == 0.0 ? tex{}.xy : tex{}.xy / tex{}.z)", i, i, i, i); - out.Write(" * " I_TEXDIMS "[{}].zw);\n", i); + out.Write(" * float2(" I_TEXDIMS "[{}].zw * 128));\n", i); // TODO: S24 overflows here? } } @@ -1436,13 +1436,14 @@ static void SampleTexture(ShaderCode& out, std::string_view texcoords, std::stri if (api_type == APIType::D3D) { - out.Write("iround(255.0 * Tex[{}].Sample(samp[{}], float3({}.xy * " I_TEXDIMS - "[{}].xy, {}))).{};\n", + out.Write("iround(255.0 * Tex[{}].Sample(samp[{}], float3({}.xy / float2(" I_TEXDIMS + "[{}].xy * 128), {}))).{};\n", texmap, texmap, texcoords, texmap, stereo ? "layer" : "0.0", texswap); } else { - out.Write("iround(255.0 * texture(samp[{}], float3({}.xy * " I_TEXDIMS "[{}].xy, {}))).{};\n", + out.Write("iround(255.0 * texture(samp[{}], float3({}.xy / float2(" I_TEXDIMS + "[{}].xy * 128), {}))).{};\n", texmap, texcoords, texmap, stereo ? "layer" : "0.0", texswap); } } diff --git a/Source/Core/VideoCommon/PixelShaderManager.cpp b/Source/Core/VideoCommon/PixelShaderManager.cpp index 436db9dd50..3a232903af 100644 --- a/Source/Core/VideoCommon/PixelShaderManager.cpp +++ b/Source/Core/VideoCommon/PixelShaderManager.cpp @@ -273,16 +273,13 @@ void PixelShaderManager::SetDestAlphaChanged() void PixelShaderManager::SetTexDims(int texmapid, u32 width, u32 height) { - float rwidth = 1.0f / (width * 128.0f); - float rheight = 1.0f / (height * 128.0f); - // TODO: move this check out to callee. There we could just call this function on texture changes // or better, use textureSize() in glsl - if (constants.texdims[texmapid][0] != rwidth || constants.texdims[texmapid][1] != rheight) + if (constants.texdims[texmapid][0] != width || constants.texdims[texmapid][1] != height) dirty = true; - constants.texdims[texmapid][0] = rwidth; - constants.texdims[texmapid][1] = rheight; + constants.texdims[texmapid][0] = width; + constants.texdims[texmapid][1] = height; } void PixelShaderManager::SetZTextureBias() @@ -382,8 +379,8 @@ void PixelShaderManager::SetZTextureOpChanged() void PixelShaderManager::SetTexCoordChanged(u8 texmapid) { TCoordInfo& tc = bpmem.texcoords[texmapid]; - constants.texdims[texmapid][2] = (float)(tc.s.scale_minus_1 + 1) * 128.0f; - constants.texdims[texmapid][3] = (float)(tc.t.scale_minus_1 + 1) * 128.0f; + constants.texdims[texmapid][2] = tc.s.scale_minus_1 + 1; + constants.texdims[texmapid][3] = tc.t.scale_minus_1 + 1; dirty = true; } diff --git a/Source/Core/VideoCommon/UberShaderPixel.cpp b/Source/Core/VideoCommon/UberShaderPixel.cpp index c124a0b5db..833bbe1d0f 100644 --- a/Source/Core/VideoCommon/UberShaderPixel.cpp +++ b/Source/Core/VideoCommon/UberShaderPixel.cpp @@ -301,8 +301,8 @@ ShaderCode GenPixelShader(APIType api_type, const ShaderHostConfig& host_config, " else\n" " fixedPoint_uv = fixedPoint_uv >> " I_INDTEXSCALE "[{} >> 1].zw;\n" "\n" - " {} = sampleTexture(texmap, float3(float2(fixedPoint_uv) * " I_TEXDIMS - "[texmap].xy, {})).abg;\n" + " {} = sampleTexture(texmap, float3(float2(fixedPoint_uv) / float2(" I_TEXDIMS + "[texmap].xy * 128), {})).abg;\n" "}}", in_index_name, in_index_name, in_index_name, in_index_name, out_var_name, stereo ? "float(layer)" : "0.0"); @@ -786,7 +786,7 @@ ShaderCode GenPixelShader(APIType api_type, const ShaderHostConfig& host_config, { out.Write(" int2 fixpoint_uv{} = int2(", i); out.Write("(tex{}.z == 0.0 ? tex{}.xy : tex{}.xy / tex{}.z)", i, i, i, i); - out.Write(" * " I_TEXDIMS "[{}].zw);\n", i); + out.Write(" * float2(" I_TEXDIMS "[{}].zw * 128));\n", i); // TODO: S24 overflows here? } @@ -910,7 +910,8 @@ ShaderCode GenPixelShader(APIType api_type, const ShaderHostConfig& host_config, " uint sampler_num = {};\n", BitfieldExtract<&TwoTevStageOrders::texmap0>("ss.order")); out.Write("\n" - " float2 uv = (float2(tevcoord.xy)) * " I_TEXDIMS "[sampler_num].xy;\n"); + " float2 uv = (float2(tevcoord.xy)) / float2(" I_TEXDIMS + "[sampler_num].xy * 128);\n"); out.Write(" int4 color = sampleTexture(sampler_num, float3(uv, {}));\n", stereo ? "float(layer)" : "0.0"); out.Write(" uint swap = {};\n",