Path: blob/master/GPU/Common/DepalettizeShaderCommon.cpp
3186 views
// Copyright (c) 2014- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include <cstdio>1819#include "Common/GPU/Shader.h"20#include "Common/GPU/ShaderWriter.h"2122#include "Common/StringUtils.h"23#include "Common/Log.h"24#include "Common/LogReporting.h"25#include "GPU/Common/GPUStateUtils.h"26#include "GPU/Common/DepalettizeShaderCommon.h"27#include "GPU/Common/Draw2D.h"2829static const InputDef vsInputs[2] = {30{ "vec2", "a_position", Draw::SEM_POSITION, },31{ "vec2", "a_texcoord0", Draw::SEM_TEXCOORD0, },32};3334// TODO: Deduplicate with TextureShaderCommon.cpp35static const SamplerDef samplers[2] = {36{ 0, "tex", SamplerFlags::ARRAY_ON_VULKAN },37{ 1, "pal" },38};3940static const VaryingDef varyings[1] = {41{ "vec2", "v_texcoord", Draw::SEM_TEXCOORD0, 0, "highp" },42};4344// Uses integer instructions available since OpenGL 3.0, ES 3.0 (and 2.0 with extensions), and of course Vulkan and D3D11.45void GenerateDepalShader300(ShaderWriter &writer, const DepalConfig &config) {46const int shift = config.shift;47const int mask = config.mask;4849writer.C(" vec2 texcoord = v_texcoord;\n");5051// Implement the swizzle we need to simulate, if a game uses 8888 framebuffers and any other mode than "6" to access depth textures.52// This implements the "2" mode swizzle (it fixes up the Y direction but not X. See comments on issue #15898, Tantalus games)53// NOTE: This swizzle can be made to work with any power-of-2 resolution scaleFactor by shifting54// the bits around, but not sure how to handle 3x scaling. For now this is 1x-only (rough edges at higher resolutions).55if (config.bufferFormat == GE_FORMAT_DEPTH16) {56if (config.depthUpperBits == 0x2) {57writer.C(R"(58int x = int((texcoord.x / scaleFactor) * texSize.x);59int xclear = x & 0x01F0;60int temp = (x - xclear) | ((x >> 1) & 0xF0) | ((x << 4) & 0x100);61texcoord.x = (float(temp) / texSize.x) * scaleFactor;62)");63}64}6566// Sampling turns our texture into floating point. To avoid this, might be able67// to declare them as isampler2D objects, but these require integer textures, which needs more work.68// Anyhow, we simply work around this by converting back to integer, which is fine.69// Use the mask to skip reading some components.7071// TODO: Since we actually have higher precision color data here, we might want to apply a dithering pattern here72// in the 5551, 565 and 4444 modes. This would benefit Test Drive which renders at 16-bit on the real hardware73// and dithers immediately, while we render at higher color depth and thus don't dither resulting in banding74// when we sample it at low color depth like this.7576// An alternative would be to have a special mode where we keep some extra precision here and sample the CLUT linearly - works for ramps such77// as those that Test Drive uses for its color remapping. But would need game specific flagging.7879writer.C(" vec4 color = ").SampleTexture2D("tex", "texcoord").C(";\n");8081int shiftedMask = mask << shift;82switch (config.bufferFormat) {83case GE_FORMAT_CLUT8:84writer.C(" int index = int(color.r * 255.99);\n");85break;86case GE_FORMAT_8888:87if (shiftedMask & 0xFF) writer.C(" int r = int(color.r * 255.99);\n"); else writer.C(" int r = 0;\n");88if (shiftedMask & 0xFF00) writer.C(" int g = int(color.g * 255.99);\n"); else writer.C(" int g = 0;\n");89if (shiftedMask & 0xFF0000) writer.C(" int b = int(color.b * 255.99);\n"); else writer.C(" int b = 0;\n");90if (shiftedMask & 0xFF000000) writer.C(" int a = int(color.a * 255.99);\n"); else writer.C(" int a = 0;\n");91writer.C(" int index = (a << 24) | (b << 16) | (g << 8) | (r);\n");92break;93case GE_FORMAT_4444:94if (shiftedMask & 0xF) writer.C(" int r = int(color.r * 15.99);\n"); else writer.C(" int r = 0;\n");95if (shiftedMask & 0xF0) writer.C(" int g = int(color.g * 15.99);\n"); else writer.C(" int g = 0;\n");96if (shiftedMask & 0xF00) writer.C(" int b = int(color.b * 15.99);\n"); else writer.C(" int b = 0;\n");97if (shiftedMask & 0xF000) writer.C(" int a = int(color.a * 15.99);\n"); else writer.C(" int a = 0;\n");98writer.C(" int index = (a << 12) | (b << 8) | (g << 4) | (r);\n");99break;100case GE_FORMAT_565:101if (shiftedMask & 0x1F) writer.C(" int r = int(color.r * 31.99);\n"); else writer.C(" int r = 0;\n");102if (shiftedMask & 0x7E0) writer.C(" int g = int(color.g * 63.99);\n"); else writer.C(" int g = 0;\n");103if (shiftedMask & 0xF800) writer.C(" int b = int(color.b * 31.99);\n"); else writer.C(" int b = 0;\n");104writer.C(" int index = (b << 11) | (g << 5) | (r);\n");105break;106case GE_FORMAT_5551:107if (config.textureFormat == GE_TFMT_CLUT8) {108// SOCOM case. We need to make sure the next few lines load the right bits, see below.109shiftedMask <<= 8;110}111if (shiftedMask & 0x1F) writer.C(" int r = int(color.r * 31.99);\n"); else writer.C(" int r = 0;\n");112if (shiftedMask & 0x3E0) writer.C(" int g = int(color.g * 31.99);\n"); else writer.C(" int g = 0;\n");113if (shiftedMask & 0x7C00) writer.C(" int b = int(color.b * 31.99);\n"); else writer.C(" int b = 0;\n");114if (shiftedMask & 0x8000) writer.C(" int a = int(color.a);\n"); else writer.C(" int a = 0;\n");115writer.C(" int index = (a << 15) | (b << 10) | (g << 5) | (r);\n");116117if (config.textureFormat == GE_TFMT_CLUT8) {118// SOCOM case. #16210119// To debug the issue, remove this shift to see the texture (check for clamping etc).120writer.C(" index >>= 8;\n");121}122123break;124case GE_FORMAT_DEPTH16:125// Decode depth buffer.126writer.C(" float depth = (color.x - z_offset) * z_scale * 65535.0f;\n");127128if (config.bufferFormat == GE_FORMAT_DEPTH16 && config.textureFormat == GE_TFMT_5650) {129// Convert depth to 565, without going through a CLUT.130// TODO: Make "depal without a CLUT" a separate concept, to avoid redundantly creating a CLUT texture.131writer.C(" int idepth = int(clamp(depth, 0.0, 65535.0));\n");132writer.C(" float r = float(idepth & 31) / 31.0;\n");133writer.C(" float g = float((idepth >> 5) & 63) / 63.0;\n");134writer.C(" float b = float((idepth >> 11) & 31) / 31.0;\n");135writer.C(" vec4 outColor = vec4(r, g, b, 1.0);\n");136return;137}138139writer.C(" int index = int(clamp(depth, 0.0, 65535.0));\n");140break;141default:142break;143}144145float texturePixels = 512.0f;146147if (shift) {148writer.F(" index = (int(uint(index) >> uint(%d)) & 0x%02x)", shift, mask);149} else {150writer.F(" index = (index & 0x%02x)", mask);151}152if (config.startPos) {153writer.F(" | %d;\n", config.startPos); // '|' matches what we have in gstate.h154} else {155writer.F(";\n");156}157158writer.F(" vec2 uv = vec2((float(index) + 0.5) * %f, 0.0);\n", 1.0f / texturePixels);159writer.C(" vec4 outColor = ").SampleTexture2D("pal", "uv").C(";\n");160}161162// FP only, to suit GL(ES) 2.0 and DX9163void GenerateDepalShaderFloat(ShaderWriter &writer, const DepalConfig &config) {164char lookupMethod[128] = "index.r";165166const int shift = config.shift;167const int mask = config.mask;168169if (config.bufferFormat == GE_FORMAT_DEPTH16) {170DepthScaleFactors factors = GetDepthScaleFactors(gstate_c.UseFlags());171writer.ConstFloat("z_scale", factors.ScaleU16());172writer.ConstFloat("z_offset", factors.Offset());173}174175writer.C(" vec4 index = ").SampleTexture2D("tex", "v_texcoord").C(";\n");176177float index_multiplier = 1.0f;178// pixelformat is the format of the texture we are sampling.179bool formatOK = true;180switch (config.bufferFormat) {181case GE_FORMAT_CLUT8:182if (shift == 0 && mask == 0xFF) {183// Easy peasy.184snprintf(lookupMethod, sizeof(lookupMethod), "index.r");185formatOK = true;186} else {187// Deal with this if we find it.188formatOK = false;189}190break;191case GE_FORMAT_8888:192if ((mask & (mask + 1)) == 0) {193// If the value has all bits contiguous (bitmask check above), we can mod by it + 1.194const char *rgba = "rrrrrrrrggggggggbbbbbbbbaaaaaaaa";195const u8 rgba_shift = shift & 7;196if (rgba_shift == 0 && mask == 0xFF) {197snprintf(lookupMethod, sizeof(lookupMethod), "index.%c", rgba[shift]);198} else {199snprintf(lookupMethod, sizeof(lookupMethod), "mod(index.%c * %f, %d.0)", rgba[shift], 255.99f / (1 << rgba_shift), mask + 1);200index_multiplier = 1.0f / 256.0f;201// Format was OK if there weren't bits from another component.202formatOK = mask <= 255 - (1 << rgba_shift);203}204} else {205formatOK = false;206}207break;208case GE_FORMAT_4444:209if ((mask & (mask + 1)) == 0 && shift < 16) {210const char *rgba = "rrrrggggbbbbaaaa";211const u8 rgba_shift = shift & 3;212if (rgba_shift == 0 && mask == 0xF) {213snprintf(lookupMethod, sizeof(lookupMethod), "index.%c", rgba[shift]);214index_multiplier = 15.0f / 256.0f;215} else {216// Let's divide and mod to get the right bits. A common case is shift=0, mask=01.217snprintf(lookupMethod, sizeof(lookupMethod), "mod(index.%c * %f, %d.0)", rgba[shift], 15.99f / (1 << rgba_shift), mask + 1);218index_multiplier = 1.0f / 256.0f;219formatOK = mask <= 15 - (1 << rgba_shift);220}221} else {222formatOK = false;223}224break;225case GE_FORMAT_565:226if ((mask & (mask + 1)) == 0 && shift < 16) {227const u8 shifts[16] = { 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4 };228const int multipliers[16] = { 31, 31, 31, 31, 31, 63, 63, 63, 63, 63, 63, 31, 31, 31, 31, 31 };229const char *rgba = "rrrrrggggggbbbbb";230const u8 rgba_shift = shifts[shift];231if (rgba_shift == 0 && mask == multipliers[shift]) {232snprintf(lookupMethod, sizeof(lookupMethod), "index.%c", rgba[shift]);233index_multiplier = multipliers[shift] / 256.0f;234} else {235// We just need to divide the right component by the right value, and then mod against the mask.236// A common case is shift=1, mask=0f.237snprintf(lookupMethod, sizeof(lookupMethod), "mod(index.%c * %f, %d.0)", rgba[shift], ((float)multipliers[shift] + 0.99f) / (1 << rgba_shift), mask + 1);238index_multiplier = 1.0f / 256.0f;239formatOK = mask <= multipliers[shift] - (1 << rgba_shift);240}241} else {242formatOK = false;243}244break;245case GE_FORMAT_5551:246if (config.textureFormat == GE_TFMT_CLUT8 && mask == 0xFF && shift == 0) {247// Follow the intent here, and ignore g (and let's not round unnecessarily).248snprintf(lookupMethod, sizeof(lookupMethod), "floor(floor(index.a) * 128.0 + index.b * 64.0)");249index_multiplier = 1.0f / 256.0f;250// SOCOM case. #16210251} else if ((mask & (mask + 1)) == 0 && shift < 16) {252const char *rgba = "rrrrrgggggbbbbba";253const u8 rgba_shift = shift % 5;254if (rgba_shift == 0 && mask == 0x1F) {255snprintf(lookupMethod, sizeof(lookupMethod), "index.%c", rgba[shift]);256index_multiplier = 31.0f / 256.0f;257} else if (shift == 15 && mask == 1) {258snprintf(lookupMethod, sizeof(lookupMethod), "index.%c", rgba[shift]);259index_multiplier = 1.0f / 256.0f;260} else {261// A isn't possible here.262snprintf(lookupMethod, sizeof(lookupMethod), "mod(index.%c * %f, %d.0)", rgba[shift], 31.99f / (1 << rgba_shift), mask + 1);263index_multiplier = 1.0f / 256.0f;264formatOK = mask <= 31 - (1 << rgba_shift);265}266} else {267formatOK = false;268}269break;270case GE_FORMAT_DEPTH16:271{272// TODO: I think we can handle most scenarios here, but texturing from depth buffers requires an extension on ES 2.0 anyway.273// Not on D3D9 though, so this path is still relevant.274275if (config.bufferFormat == GE_FORMAT_DEPTH16 && config.textureFormat == GE_TFMT_5650) {276// Convert depth to 565, without going through a CLUT.277writer.C(" float depth = (index.x - z_offset) * z_scale;\n");278writer.C(" float idepth = floor(clamp(depth, 0.0, 65535.0));\n");279writer.C(" float r = mod(idepth, 32.0) / 31.0;\n");280writer.C(" float g = mod(floor(idepth / 32.0), 64.0) / 63.0;\n");281writer.C(" float b = mod(floor(idepth / 2048.0), 32.0) / 31.0;\n");282writer.C(" vec4 outColor = vec4(r, g, b, 1.0);\n");283return;284}285286if (shift < 16) {287index_multiplier = 1.0f / (float)(1 << shift);288truncate_cpy(lookupMethod, "((index.x - z_offset) * z_scale)");289290if ((mask & (mask + 1)) != 0) {291// But we'll try with the above anyway.292formatOK = false;293}294} else {295formatOK = false;296}297break;298}299default:300break;301}302303// We always use 512-sized textures now.304float texturePixels = 512.f;305index_multiplier *= 0.5f;306307// Adjust index_multiplier, similar to the use of 15.99 instead of 16 in the ES 3 path.308// index_multiplier -= 0.01f / texturePixels;309310if (!formatOK) {311ERROR_LOG_REPORT_ONCE(depal, Log::G3D, "%s depal unsupported: shift=%d mask=%02x offset=%d", GeBufferFormatToString(config.bufferFormat), shift, mask, config.startPos);312}313314// Offset by half a texel (plus clutBase) to turn NEAREST filtering into FLOOR.315// Technically, the clutBase should be |'d, not added, but that's hard with floats.316float texel_offset = ((float)config.startPos + 0.5f) / texturePixels;317writer.F(" float coord = (%s * %f) + %f;\n", lookupMethod, index_multiplier, texel_offset);318writer.C(" vec4 outColor = ").SampleTexture2D("pal", "vec2(coord, 0.0)").C(";\n");319}320321void GenerateDepalSmoothed(ShaderWriter &writer, const DepalConfig &config) {322const char *sourceChannel = "error";323float indexMultiplier = 31.0f;324325if (config.bufferFormat == GE_FORMAT_5551) {326_dbg_assert_(config.mask == 0x1F);327switch (config.shift) {328case 0: sourceChannel = "r"; break;329case 5: sourceChannel = "g"; break;330case 10: sourceChannel = "b"; break;331default: _dbg_assert_(false);332}333} else if (config.bufferFormat == GE_FORMAT_565) {334_dbg_assert_(config.mask == 0x1F || config.mask == 0x3F);335switch (config.shift) {336case 0: sourceChannel = "r"; break;337case 5: sourceChannel = "g"; indexMultiplier = 63.0f; break;338case 11: sourceChannel = "b"; break;339default: _dbg_assert_(false);340}341} else {342_dbg_assert_(false);343}344345writer.C(" float index = ").SampleTexture2D("tex", "v_texcoord").F(".%s * %0.1f;\n", sourceChannel, indexMultiplier);346float texturePixels = 512.f;347writer.F(" float coord = (index + 0.5) * %f;\n", 1.0 / texturePixels);348writer.C(" vec4 outColor = ").SampleTexture2D("pal", "vec2(coord, 0.0)").C(";\n");349}350351void GenerateDepalFs(ShaderWriter &writer, const DepalConfig &config) {352writer.DeclareSamplers(samplers);353writer.HighPrecisionFloat();354writer.BeginFSMain(config.bufferFormat == GE_FORMAT_DEPTH16 ? g_draw2Duniforms : Slice<UniformDef>::empty(), varyings);355if (config.smoothedDepal) {356// Handles a limited set of cases, but doesn't need any integer math so we don't357// need two variants.358GenerateDepalSmoothed(writer, config);359} else {360switch (writer.Lang().shaderLanguage) {361case GLSL_1xx:362GenerateDepalShaderFloat(writer, config);363break;364case GLSL_VULKAN:365case GLSL_3xx:366case HLSL_D3D11:367// Use the float shader for the SOCOM special.368if (config.bufferFormat == GE_FORMAT_5551 && config.textureFormat == GE_TFMT_CLUT8) {369GenerateDepalShaderFloat(writer, config);370} else {371GenerateDepalShader300(writer, config);372}373break;374default:375_assert_msg_(false, "Shader language not supported for depal: %d", (int)writer.Lang().shaderLanguage);376}377}378writer.EndFSMain("outColor");379}380381382