|
| 1 | +#ifdef GL_ARB_derivative_control |
| 2 | + #extension GL_ARB_derivative_control : enable |
| 3 | +#endif |
| 4 | + |
| 5 | +vec4 quantize3Bit(in vec4 color) { |
| 6 | + return vec4(round(color.rgb * 8.0) / 8.0, step(0.5, color.a)); |
| 7 | +} |
| 8 | + |
| 9 | +vec4 quantize4Bit(in vec4 color) { |
| 10 | + return round(color * 16.0) / 16.0; // (16 seems more accurate than 15) |
| 11 | +} |
| 12 | + |
| 13 | +vec4 quantizeTexture(uint flags, vec4 color) { |
| 14 | + vec4 colorQuant = flagSelect(flags, TEX_FLAG_4BIT, color, quantize4Bit(color)); |
| 15 | + colorQuant = flagSelect(flags, TEX_FLAG_3BIT, colorQuant, quantize3Bit(colorQuant)); |
| 16 | + colorQuant.rgb = linearToGamma(colorQuant.rgb); |
| 17 | + return flagSelect(flags, TEX_FLAG_MONO, colorQuant.rgba, colorQuant.rrrr); |
| 18 | +} |
| 19 | + |
| 20 | +vec2 mirrorUV(const vec2 uvIn, const vec2 uvBound) |
| 21 | +{ |
| 22 | + vec2 uvMod2 = mod(uvIn, uvBound * 2.0 + 1.0); |
| 23 | + return mix(uvMod2, (uvBound * 2.0) - uvMod2, step(uvBound, uvMod2)); |
| 24 | +} |
| 25 | + |
| 26 | +vec4 wrappedMirrorSample(const sampler2D tex, vec2 uv, const vec2 mask, const vec2 highMinusLow, const vec2 isClamp, const vec2 isMirror) |
| 27 | +{ |
| 28 | + const ivec2 texSize = textureSize(tex, 0); |
| 29 | + |
| 30 | + // first apply clamping if enabled (clamp S/T, low S/T -> high S/T) |
| 31 | + const vec2 uvClamp = clamp(uv, vec2(0.0), highMinusLow); |
| 32 | + uv = mix(uv, uvClamp, isClamp); |
| 33 | + |
| 34 | + // then mirror the result if needed (mirror S/T) |
| 35 | + const vec2 uvMirror = mirrorUV(uv, mask - 0.5); |
| 36 | + uv = mix(uv, uvMirror, isMirror); |
| 37 | + |
| 38 | + // clamp again (mask S/T), this is also done to avoid OOB texture access |
| 39 | + uv = mod(uv, min(texSize, mask)); |
| 40 | + |
| 41 | + return texelFetch(tex, ivec2(floor(uv)), 0); |
| 42 | +} |
| 43 | + |
| 44 | +vec4 sampleSampler(in const sampler2D tex, in const TileConf tileConf, in vec2 uvCoord, in const uint texFilter) { |
| 45 | + // https://github.com/rt64/rt64/blob/61aa08f517cd16c1dbee4e097768b08e2a060307/src/shaders/TextureSampler.hlsli#L156-L276 |
| 46 | + const ivec2 texSize = textureSize(tex, 0); |
| 47 | + |
| 48 | + uvCoord *= tileConf.shift; |
| 49 | + |
| 50 | +#ifdef SIMULATE_LOW_PRECISION |
| 51 | + // Simulates the lower precision of the hardware's coordinate interpolation. |
| 52 | + uvCoord = round(uvCoord * LOW_PRECISION) / LOW_PRECISION; |
| 53 | +#endif |
| 54 | + |
| 55 | + uvCoord -= tileConf.low; |
| 56 | + |
| 57 | + const vec2 isClamp = step(tileConf.mask, vec2(1.0)); // if mask is negated, clamp |
| 58 | + const vec2 isMirror = step(tileConf.high, vec2(0.0)); // if high is negated, mirror |
| 59 | + const vec2 mask = abs(tileConf.mask); |
| 60 | + const vec2 highMinusLow = abs(tileConf.high) - abs(tileConf.low); |
| 61 | + |
| 62 | + if (texFilter != G_TF_POINT) { |
| 63 | + uvCoord -= 0.5 * tileConf.shift; |
| 64 | + const vec2 texelBaseInt = floor(uvCoord); |
| 65 | + const vec4 sample00 = wrappedMirrorSample(tex, texelBaseInt, mask, highMinusLow, isClamp, isMirror); |
| 66 | + const vec4 sample01 = wrappedMirrorSample(tex, texelBaseInt + vec2(0, 1), mask, highMinusLow, isClamp, isMirror); |
| 67 | + const vec4 sample10 = wrappedMirrorSample(tex, texelBaseInt + vec2(1, 0), mask, highMinusLow, isClamp, isMirror); |
| 68 | + const vec4 sample11 = wrappedMirrorSample(tex, texelBaseInt + vec2(1, 1), mask, highMinusLow, isClamp, isMirror); |
| 69 | + const vec2 fracPart = uvCoord - texelBaseInt; |
| 70 | +#ifdef USE_LINEAR_FILTER |
| 71 | + return quantizeTexture(tileConf.flags, mix(mix(sample00, sample10, fracPart.x), mix(sample01, sample11, fracPart.x), fracPart.y)); |
| 72 | +#else |
| 73 | + if (texFilter == G_TF_AVERAGE && all(lessThanEqual(vec2(1 / LOW_PRECISION), abs(fracPart - 0.5)))) { |
| 74 | + return quantizeTexture(tileConf.flags, (sample00 + sample01 + sample10 + sample11) / 4.0f); |
| 75 | + } |
| 76 | + else { |
| 77 | + // Originally written by ArthurCarvalho |
| 78 | + // Sourced from https://www.emutalk.net/threads/emulating-nintendo-64-3-sample-bilinear-filtering-using-shaders.54215/ |
| 79 | + vec4 tri0 = mix(sample00, sample10, fracPart.x) + (sample01 - sample00) * fracPart.y; |
| 80 | + vec4 tri1 = mix(sample11, sample01, 1.0 - fracPart.x) + (sample10 - sample11) * (1.0 - fracPart.y); |
| 81 | + return quantizeTexture(tileConf.flags, mix(tri0, tri1, step(1.0, fracPart.x + fracPart.y))); |
| 82 | + } |
| 83 | +#endif |
| 84 | + } |
| 85 | + else { |
| 86 | + return quantizeTexture(tileConf.flags, wrappedMirrorSample(tex, ivec2(floor(uvCoord)), mask, highMinusLow, isClamp, isMirror)); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +vec4 sampleIndex(in const uint textureIndex, in const vec2 uvCoord, in const uint texFilter) { |
| 91 | + TileConf tileConf = material.texConfs[textureIndex]; |
| 92 | + switch (textureIndex) { |
| 93 | + default: return sampleSampler(tex0, tileConf, uvCoord, texFilter); |
| 94 | + case 1: return sampleSampler(tex1, tileConf, uvCoord, texFilter); |
| 95 | + case 2: return sampleSampler(tex2, tileConf, uvCoord, texFilter); |
| 96 | + case 3: return sampleSampler(tex3, tileConf, uvCoord, texFilter); |
| 97 | + case 4: return sampleSampler(tex4, tileConf, uvCoord, texFilter); |
| 98 | + case 5: return sampleSampler(tex5, tileConf, uvCoord, texFilter); |
| 99 | + case 6: return sampleSampler(tex6, tileConf, uvCoord, texFilter); |
| 100 | + case 7: return sampleSampler(tex7, tileConf, uvCoord, texFilter); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +void computeLOD( |
| 105 | + inout uint tileIndex0, |
| 106 | + inout uint tileIndex1, |
| 107 | + const bool textLOD, |
| 108 | + const uint textDetail, |
| 109 | + const float minLod, |
| 110 | + const vec2 dx, |
| 111 | + const vec2 dy, |
| 112 | + const bool perspectiveOverflow, // this should be possible from what I've read in parallel-rdp, can always be removed |
| 113 | + out float lodFrac |
| 114 | +) { |
| 115 | + const bool sharpen = textDetail == G_TD_SHARPEN; |
| 116 | + const bool detail = textDetail == G_TD_DETAIL; |
| 117 | + const bool clam = textDetail == G_TD_CLAMP; |
| 118 | + |
| 119 | + const vec2 dfd = max(dx, dy); |
| 120 | + // TODO: should this value be scaled by clipping planes? |
| 121 | + const float maxDist = max(dfd.x, dfd.y); |
| 122 | + |
| 123 | + const uint mipBase = uint(floor(log2(maxDist))); |
| 124 | + const bool distant = perspectiveOverflow || maxDist >= 16384.0; |
| 125 | + const bool aboveCount = mipBase >= material.mipCount; |
| 126 | + const bool maxDistant = distant || aboveCount; |
| 127 | + const bool magnify = maxDist < 1.0; |
| 128 | + |
| 129 | + const float detailFrac = max(minLod, maxDist) - float(sharpen); |
| 130 | + const float magnifedFrac = mix(float(maxDistant), detailFrac, float(!clam)); |
| 131 | + const float distantFrac = float(distant || (aboveCount && clam)); |
| 132 | + const float notClampedFrac = max(maxDist / pow(2, max(mipBase, 0)) - 1.0, minLod); |
| 133 | + |
| 134 | + const float notMagnifedFrac = mix(distantFrac, notClampedFrac, !maxDistant || !clam); |
| 135 | + lodFrac = mix(notMagnifedFrac, magnifedFrac, float(!distant && magnify)); |
| 136 | + |
| 137 | + if (textLOD) { |
| 138 | + const uint tileOffset = maxDistant ? material.mipCount : (mipBase * int(!(maxDistant && clam))); |
| 139 | + tileIndex0 = tileIndex0 + tileOffset; |
| 140 | + tileIndex1 = tileIndex0; |
| 141 | + if (detail) { |
| 142 | + tileIndex1 += (int(!(maxDistant || magnify)) + 1); |
| 143 | + tileIndex0 += int(!magnify); |
| 144 | + } else { |
| 145 | + tileIndex1 += uint(!maxDistant && (sharpen || !magnify)); |
| 146 | + } |
| 147 | + tileIndex0 &= 7; |
| 148 | + tileIndex1 &= 7; |
| 149 | + } |
| 150 | +} |
0 commit comments