Implement the internal scaling as SSAA

This commit is contained in:
Maschell 2020-08-13 13:26:45 +02:00
parent 9494f515eb
commit 5b387ffec1
4 changed files with 43 additions and 39 deletions

View File

@ -139,7 +139,7 @@ private:
void loadGlyphData(FT_Bitmap *bmp, ftgxCharData *charData); void loadGlyphData(FT_Bitmap *bmp, ftgxCharData *charData);
void copyTextureToFramebuffer(CVideo *pVideo, GX2Texture *tex, int16_t screenX, int16_t screenY, int16_t screenZ, const glm::vec4 &color, const float &textBlur, const float &colorBlurIntensity, const glm::vec4 &blurColor, void copyTextureToFramebuffer(CVideo *pVideo, GX2Texture *tex, int16_t screenX, int16_t screenY, int16_t screenZ, const glm::vec4 &color, const float &textBlur, const float &colorBlurIntensity, const glm::vec4 &blurColor,
const float &internalRenderingScale); const float &superSamplingScale);
public: public:
FreeTypeGX(const uint8_t *fontBuffer, FT_Long bufferSize, bool lastFace = false); FreeTypeGX(const uint8_t *fontBuffer, FT_Long bufferSize, bool lastFace = false);
@ -147,7 +147,8 @@ public:
~FreeTypeGX(); ~FreeTypeGX();
uint16_t drawText(CVideo *pVideo, int16_t x, int16_t y, int16_t z, const wchar_t *text, int16_t pixelSize, const glm::vec4 &color, uint16_t drawText(CVideo *pVideo, int16_t x, int16_t y, int16_t z, const wchar_t *text, int16_t pixelSize, const glm::vec4 &color,
uint16_t textStyling, uint16_t textWidth, const float &textBlur, const float &colorBlurIntensity, const glm::vec4 &blurColor, const float &internalRenderingScale); uint16_t textStyling, uint16_t textWidth, const float &textBlur, const float &colorBlurIntensity, const glm::vec4 &blurColor, const float &superSamplingScale);
uint16_t getWidth(const wchar_t *text, int16_t pixelSize); uint16_t getWidth(const wchar_t *text, int16_t pixelSize);

View File

@ -106,6 +106,11 @@ public:
} }
//!Get fontsize //!Get fontsize
void setSSAA(int32_t ssaa) {
this->internalSSAA = ssaa;
};
int32_t getFontSize() { int32_t getFontSize() {
return size; return size;
}; };
@ -142,6 +147,7 @@ public:
}; };
protected: protected:
static FreeTypeGX *presentFont; static FreeTypeGX *presentFont;
static int32_t presetSSAA;
static int32_t presetSize; static int32_t presetSize;
static int32_t presetMaxWidth; static int32_t presetMaxWidth;
static float presetInternalRenderingScale; static float presetInternalRenderingScale;
@ -178,7 +184,7 @@ protected:
float blurGlowIntensity; float blurGlowIntensity;
float blurAlpha; float blurAlpha;
glm::vec4 blurGlowColor; glm::vec4 blurGlowColor;
float internalRenderingScale; int32_t internalSSAA;
}; };
#endif #endif

View File

@ -344,10 +344,8 @@ int16_t FreeTypeGX::getStyleOffsetHeight(int16_t format, uint16_t pixelSize) {
*/ */
uint16_t FreeTypeGX::drawText(CVideo *video, int16_t x, int16_t y, int16_t z, const wchar_t *text, int16_t pixelSize, const glm::vec4 &color, uint16_t textStyle, uint16_t textWidth, const float &textBlur, const float &colorBlurIntensity, uint16_t FreeTypeGX::drawText(CVideo *video, int16_t x, int16_t y, int16_t z, const wchar_t *text, int16_t pixelSize, const glm::vec4 &color, uint16_t textStyle, uint16_t textWidth, const float &textBlur, const float &colorBlurIntensity,
const glm::vec4 &blurColor, const float &internalRenderingScale) { const glm::vec4 &blurColor, const float &superSamplingScale) {
if (!text) { if (!text) { return 0; }
return 0;
}
uint16_t fullTextWidth = (textWidth > 0) ? textWidth : getWidth(text, pixelSize); uint16_t fullTextWidth = (textWidth > 0) ? textWidth : getWidth(text, pixelSize);
uint16_t x_pos = x, printed = 0; uint16_t x_pos = x, printed = 0;
@ -371,7 +369,7 @@ uint16_t FreeTypeGX::drawText(CVideo *video, int16_t x, int16_t y, int16_t z, co
x_pos += (pairDelta.x >> 6); x_pos += (pairDelta.x >> 6);
} }
copyTextureToFramebuffer(video, glyphData->texture, x_pos + glyphData->renderOffsetX + x_offset, y + glyphData->renderOffsetY - y_offset, z, color, textBlur, colorBlurIntensity, blurColor, internalRenderingScale); copyTextureToFramebuffer(video, glyphData->texture, x_pos + glyphData->renderOffsetX + x_offset, y + glyphData->renderOffsetY - y_offset, z, color, textBlur, colorBlurIntensity, blurColor, superSamplingScale);
x_pos += glyphData->glyphAdvanceX; x_pos += glyphData->glyphAdvanceX;
++printed; ++printed;
@ -506,15 +504,15 @@ void FreeTypeGX::getOffset(const wchar_t *text, int16_t pixelSize, uint16_t widt
* @param color Color to apply to the texture. * @param color Color to apply to the texture.
*/ */
void FreeTypeGX::copyTextureToFramebuffer(CVideo *pVideo, GX2Texture *texture, int16_t x, int16_t y, int16_t z, const glm::vec4 &color, const float &defaultBlur, const float &blurIntensity, const glm::vec4 &blurColor, void FreeTypeGX::copyTextureToFramebuffer(CVideo *pVideo, GX2Texture *texture, int16_t x, int16_t y, int16_t z, const glm::vec4 &color, const float &defaultBlur, const float &blurIntensity, const glm::vec4 &blurColor,
const float &internalRenderingScale) { const float &superSamplingScale) {
static const float imageAngle = 0.0f; static const float imageAngle = 0.0f;
static const float blurScale = (2.0f / (internalRenderingScale)); static const float blurScale = (2.0f);
float offsetLeft = blurScale * ((float) x + 0.5f * (float) texture->surface.width) * (float) pVideo->getWidthScaleFactor(); float offsetLeft = blurScale * (1.0f / superSamplingScale) * ((float) x + 0.5f * (float) texture->surface.width) * (float) pVideo->getWidthScaleFactor();
float offsetTop = blurScale * ((float) y - 0.5f * (float) texture->surface.height) * (float) pVideo->getHeightScaleFactor(); float offsetTop = blurScale * (1.0f / superSamplingScale) * ((float) y - 0.5f * (float) texture->surface.height) * (float) pVideo->getHeightScaleFactor();
float widthScale = blurScale * (float) texture->surface.width * pVideo->getWidthScaleFactor(); float widthScale = blurScale * (1.0f / superSamplingScale) * (float) texture->surface.width * pVideo->getWidthScaleFactor();
float heightScale = blurScale * (float) texture->surface.height * pVideo->getHeightScaleFactor(); float heightScale = blurScale * (1.0f / superSamplingScale) * (float) texture->surface.height * pVideo->getHeightScaleFactor();
glm::vec3 positionOffsets(offsetLeft, offsetTop, (float) z); glm::vec3 positionOffsets(offsetLeft, offsetTop, (float) z);

View File

@ -19,8 +19,8 @@
#include <gui/video/CVideo.h> #include <gui/video/CVideo.h>
FreeTypeGX *GuiText::presentFont = NULL; FreeTypeGX *GuiText::presentFont = NULL;
int32_t GuiText::presetSSAA = 2;
int32_t GuiText::presetSize = 28; int32_t GuiText::presetSize = 28;
float GuiText::presetInternalRenderingScale = 2.0f; //Lets render the font at the doubled size. This make it even smoother!
int32_t GuiText::presetMaxWidth = 0xFFFF; int32_t GuiText::presetMaxWidth = 0xFFFF;
int32_t GuiText::presetAlignment = ALIGN_CENTER | ALIGN_MIDDLE; int32_t GuiText::presetAlignment = ALIGN_CENTER | ALIGN_MIDDLE;
GX2ColorF32 GuiText::presetColor = (GX2ColorF32) { GX2ColorF32 GuiText::presetColor = (GX2ColorF32) {
@ -43,6 +43,7 @@ GuiText::GuiText() {
alpha = presetColor.a; alpha = presetColor.a;
alignment = presetAlignment; alignment = presetAlignment;
maxWidth = presetMaxWidth; maxWidth = presetMaxWidth;
internalSSAA = presetSSAA;
wrapMode = 0; wrapMode = 0;
textWidth = 0; textWidth = 0;
font = presentFont; font = presentFont;
@ -54,7 +55,6 @@ GuiText::GuiText() {
blurGlowIntensity = 0.0f; blurGlowIntensity = 0.0f;
blurAlpha = 0.0f; blurAlpha = 0.0f;
blurGlowColor = glm::vec4(0.0f); blurGlowColor = glm::vec4(0.0f);
internalRenderingScale = presetInternalRenderingScale;
} }
GuiText::GuiText(const char *t, int32_t s, const glm::vec4 &c) { GuiText::GuiText(const char *t, int32_t s, const glm::vec4 &c) {
@ -65,6 +65,7 @@ GuiText::GuiText(const char *t, int32_t s, const glm::vec4 &c) {
alpha = c[3]; alpha = c[3];
alignment = ALIGN_CENTER | ALIGN_MIDDLE; alignment = ALIGN_CENTER | ALIGN_MIDDLE;
maxWidth = presetMaxWidth; maxWidth = presetMaxWidth;
internalSSAA = presetSSAA;
wrapMode = 0; wrapMode = 0;
textWidth = 0; textWidth = 0;
font = presentFont; font = presentFont;
@ -76,7 +77,6 @@ GuiText::GuiText(const char *t, int32_t s, const glm::vec4 &c) {
blurGlowIntensity = 0.0f; blurGlowIntensity = 0.0f;
blurAlpha = 0.0f; blurAlpha = 0.0f;
blurGlowColor = glm::vec4(0.0f); blurGlowColor = glm::vec4(0.0f);
internalRenderingScale = presetInternalRenderingScale;
if (t) { if (t) {
text = FreeTypeGX::charToWideChar(t); text = FreeTypeGX::charToWideChar(t);
@ -96,6 +96,7 @@ GuiText::GuiText(const wchar_t *t, int32_t s, const glm::vec4 &c) {
alpha = c[3]; alpha = c[3];
alignment = ALIGN_CENTER | ALIGN_MIDDLE; alignment = ALIGN_CENTER | ALIGN_MIDDLE;
maxWidth = presetMaxWidth; maxWidth = presetMaxWidth;
internalSSAA = presetSSAA;
wrapMode = 0; wrapMode = 0;
textWidth = 0; textWidth = 0;
font = presentFont; font = presentFont;
@ -107,7 +108,6 @@ GuiText::GuiText(const wchar_t *t, int32_t s, const glm::vec4 &c) {
blurGlowIntensity = 0.0f; blurGlowIntensity = 0.0f;
blurAlpha = 0.0f; blurAlpha = 0.0f;
blurGlowColor = glm::vec4(0.0f); blurGlowColor = glm::vec4(0.0f);
internalRenderingScale = presetInternalRenderingScale;
if (t) { if (t) {
text = new(std::nothrow) wchar_t[wcslen(t) + 1]; text = new(std::nothrow) wchar_t[wcslen(t) + 1];
@ -132,6 +132,7 @@ GuiText::GuiText(const char *t) {
alpha = presetColor.a; alpha = presetColor.a;
alignment = presetAlignment; alignment = presetAlignment;
maxWidth = presetMaxWidth; maxWidth = presetMaxWidth;
internalSSAA = presetSSAA;
wrapMode = 0; wrapMode = 0;
textWidth = 0; textWidth = 0;
font = presentFont; font = presentFont;
@ -143,7 +144,6 @@ GuiText::GuiText(const char *t) {
blurGlowIntensity = 0.0f; blurGlowIntensity = 0.0f;
blurAlpha = 0.0f; blurAlpha = 0.0f;
blurGlowColor = glm::vec4(0.0f); blurGlowColor = glm::vec4(0.0f);
internalRenderingScale = presetInternalRenderingScale;
if (t) { if (t) {
text = FreeTypeGX::charToWideChar(t); text = FreeTypeGX::charToWideChar(t);
@ -511,21 +511,16 @@ void GuiText::draw(CVideo *pVideo) {
color[3] = getAlpha(); color[3] = getAlpha();
blurGlowColor[3] = blurAlpha * getAlpha(); blurGlowColor[3] = blurAlpha * getAlpha();
float finalRenderingScale = 2.0f * internalRenderingScale; auto internalRenderingScale = internalSSAA == 0 ? 1 : internalSSAA << 1;
int32_t newSize = size * getScale() * finalRenderingScale; int32_t normal_size = currentSize * getScale();
int32_t normal_size = size * getScale(); int32_t internalRenderingSize = normal_size * internalRenderingScale;
if (newSize != currentSize) { auto textWidth = font->getWidth(text, normal_size);
currentSize = normal_size;
if (text) { float x_pos = getCenterX() * internalRenderingScale;
textWidth = font->getWidth(text, normal_size); float y_pos = getCenterY() * internalRenderingScale;
}
}
float x_pos = getCenterX() * finalRenderingScale;
float y_pos = getCenterY() * finalRenderingScale;
if (maxWidth > 0 && maxWidth <= textWidth) { if (maxWidth > 0 && maxWidth <= textWidth) {
if (wrapMode == DOTTED) { // text dotted if (wrapMode == DOTTED) { // text dotted
@ -537,23 +532,26 @@ void GuiText::draw(CVideo *pVideo) {
textDynWidth.resize(textDyn.size()); textDynWidth.resize(textDyn.size());
for (uint32_t i = 0; i < textDynWidth.size(); i++) { for (uint32_t i = 0; i < textDynWidth.size(); i++) {
textDynWidth[i] = font->getWidth(textDyn[i], newSize); textDynWidth[i] = font->getWidth(textDyn[i], internalRenderingSize);
} }
} }
if (textDyn.size() > 0) { if (textDyn.size() > 0) {
font->drawText(pVideo, x_pos, y_pos, getDepth(), textDyn[textDyn.size() - 1], newSize, color, alignment, textDynWidth[textDyn.size() - 1], defaultBlur, blurGlowIntensity, blurGlowColor, finalRenderingScale); font->drawText(pVideo, x_pos, y_pos, getDepth(), textDyn[textDyn.size() - 1], internalRenderingSize, color, alignment, textDynWidth[textDyn.size() - 1], defaultBlur, blurGlowIntensity, blurGlowColor, internalRenderingScale);
} }
return;
} else if (wrapMode == SCROLL_HORIZONTAL) { } else if (wrapMode == SCROLL_HORIZONTAL) {
scrollText(pVideo->getFrameCount()); scrollText(pVideo->getFrameCount());
if (textDyn.size() > 0) { if (textDyn.size() > 0) {
font->drawText(pVideo, x_pos, y_pos, getDepth(), textDyn[textDyn.size() - 1], newSize, color, alignment, maxWidth * finalRenderingScale, defaultBlur, blurGlowIntensity, blurGlowColor, finalRenderingScale); font->drawText(pVideo, x_pos, y_pos, getDepth(), textDyn[textDyn.size() - 1], internalRenderingSize, color, alignment, maxWidth * internalRenderingScale * getScale(), defaultBlur, blurGlowIntensity, blurGlowColor,
internalRenderingScale);
} }
return;
} else if (wrapMode == WRAP) { } else if (wrapMode == WRAP) {
int32_t lineheight = newSize + 6; int32_t lineheight = internalRenderingSize + 6;
int32_t yoffset = 0; int32_t yoffset = 0;
int32_t voffset = 0; int32_t voffset = 0;
@ -565,7 +563,7 @@ void GuiText::draw(CVideo *pVideo) {
textDynWidth.resize(textDyn.size()); textDynWidth.resize(textDyn.size());
for (uint32_t i = 0; i < textDynWidth.size(); i++) { for (uint32_t i = 0; i < textDynWidth.size(); i++) {
textDynWidth[i] = font->getWidth(textDyn[i], newSize); textDynWidth[i] = font->getWidth(textDyn[i], internalRenderingSize);
} }
} }
@ -574,12 +572,13 @@ void GuiText::draw(CVideo *pVideo) {
} }
for (uint32_t i = 0; i < textDyn.size(); i++) { for (uint32_t i = 0; i < textDyn.size(); i++) {
font->drawText(pVideo, x_pos, y_pos + voffset + yoffset, getDepth(), textDyn[i], newSize, color, alignment, textDynWidth[i], defaultBlur, blurGlowIntensity, blurGlowColor, finalRenderingScale); font->drawText(pVideo, x_pos, y_pos + y_offset + voffset, getDepth(), textDyn[i], internalRenderingSize, color, alignment, textDynWidth[i], defaultBlur, blurGlowIntensity, blurGlowColor, internalRenderingScale);
yoffset -= lineheight; yoffset -= lineheight;
} }
return;
} }
} else {
uint16_t newtextWidth = font->getWidth(text, newSize);
font->drawText(pVideo, x_pos, y_pos, getDepth(), text, newSize, color, alignment, newtextWidth, defaultBlur, blurGlowIntensity, blurGlowColor, finalRenderingScale);
} }
auto newTextWidth = font->getWidth(text, internalRenderingSize);
font->drawText(pVideo, x_pos, y_pos, getDepth(), text, internalRenderingSize, color, alignment, newTextWidth, defaultBlur, blurGlowIntensity, blurGlowColor, internalRenderingScale);
textMutex.unlock();
} }