From 611d26a7c8254d15f8aa9250969e3357feea5e64 Mon Sep 17 00:00:00 2001 From: Mr-Wiseguy Date: Wed, 1 May 2024 04:19:46 -0400 Subject: [PATCH] Added missing changes in shader include files for dithering --- src/shaders/FbCommon.hlsli | 8 +++--- src/shaders/Formats.hlsli | 59 ++++++++++++++++++++++++++++++++++---- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/src/shaders/FbCommon.hlsli b/src/shaders/FbCommon.hlsli index dc82f08..28303b7 100644 --- a/src/shaders/FbCommon.hlsli +++ b/src/shaders/FbCommon.hlsli @@ -103,10 +103,10 @@ uint Float4ToUINT8(float4 i, uint fmt, bool oddColumn) { } } -uint Float4ToUINT16(float4 i, uint fmt) { +uint Float4ToUINT16(float4 i, uint fmt, uint dither) { switch (fmt) { case G_IM_FMT_RGBA: - return Float4ToRGBA16(i); + return Float4ToRGBA16(i, dither); // TODO case G_IM_FMT_CI: return 0; @@ -141,7 +141,7 @@ uint Float4ToUINT32(float4 i, uint fmt) { } } -uint Float4ToUINT(float4 i, uint siz, uint fmt, bool oddColumn) { +uint Float4ToUINT(float4 i, uint siz, uint fmt, bool oddColumn, uint dither) { switch (siz) { // TODO case G_IM_SIZ_4b: @@ -149,7 +149,7 @@ uint Float4ToUINT(float4 i, uint siz, uint fmt, bool oddColumn) { case G_IM_SIZ_8b: return Float4ToUINT8(i, fmt, oddColumn); case G_IM_SIZ_16b: - return Float4ToUINT16(i, fmt); + return Float4ToUINT16(i, fmt, dither); case G_IM_SIZ_32b: return Float4ToUINT32(i, fmt); // Invalid pixel size. diff --git a/src/shaders/Formats.hlsli b/src/shaders/Formats.hlsli index 9854a0b..ab3dd16 100644 --- a/src/shaders/Formats.hlsli +++ b/src/shaders/Formats.hlsli @@ -6,6 +6,53 @@ // Color conversion formulas sourced from Tharo. +static const uint DitherPatternBayer[16] = { + 0, 4, 1, 5, + 4, 0, 5, 1, + 3, 7, 2, 6, + 7, 3, 6, 2 +}; + +static const uint DitherPatternMagicSquare[16] = { + 0, 6, 1, 7, + 4, 2, 5, 3, + 3, 5, 2, 4, + 7, 1, 6, 0 +}; + +uint DitherPatternIndex(uint2 coord) { + return ((coord.y & 3) << 2) + (coord.x & 3); +} + +uint DitherPatternValue(uint pattern, uint2 coord, uint randomSeed) { + switch (pattern) { + case 0: // MAGICSQ + return DitherPatternMagicSquare[DitherPatternIndex(coord)]; + case 1: // BAYER + return DitherPatternBayer[DitherPatternIndex(coord)]; + case 2: // NOISE + return randomSeed & 7; + case 3: // DISABLE + default: + return 0; + } +} + +uint AlphaDitherValue(uint colorDither, uint alphaDither, uint2 coord, uint randomSeed) { + // Only the first bit of color dither is used here for pattern selection. + switch (alphaDither) { + case 0: // PATTERN + return DitherPatternValue(colorDither & 1, coord, randomSeed); + case 1: // NOTPATTERN + return (~DitherPatternValue(colorDither & 1, coord, randomSeed)) & 7; + case 2: // NOISE + return randomSeed & 7; + case 3: // DISABLE + default: + return 0; + } +} + float4 I4ToFloat4(uint i4) { uint i = (i4 << 4) | i4; return i / 255.0f; @@ -45,15 +92,15 @@ float4 RGBA16ToFloat4(uint rgba16) { ); } -uint Float4ToRGBA16(float4 i) { +uint Float4ToRGBA16(float4 i, uint dither) { uint r = round(clamp(i.r * 255.0f, 0.0f, 255.0f)); uint g = round(clamp(i.g * 255.0f, 0.0f, 255.0f)); uint b = round(clamp(i.b * 255.0f, 0.0f, 255.0f)); - int cvgModulo = round(i.a * 255.0f) % 8; - uint a = (cvgModulo & 0x4) ? 1 : 0; - r = r >> 3; - g = g >> 3; - b = b >> 3; + int cvgModulo = round(i.a * 255.0f) % 8; + uint a = (cvgModulo & 0x4) ? 1 : 0; + r = min(r + dither, 255) >> 3; + g = min(g + dither, 255) >> 3; + b = min(b + dither, 255) >> 3; return (r << 11) | (g << 6) | (b << 1) | a; }