Skip to content

Commit

Permalink
fixes for fonts on 3ds, needs to be optimized more
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtleP committed Nov 16, 2023
1 parent d8c295b commit 98a550f
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 100 deletions.
53 changes: 27 additions & 26 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -236,30 +236,34 @@ FetchContent_Declare(lua-https

FetchContent_GetProperties(lua-https)
if(NOT lua-https_POPULATED)
FetchContent_Populate(lua-https)
add_subdirectory(${lua-https_SOURCE_DIR} ${lua-https_BINARY_DIR})
endif()
FetchContent_Populate(lua-https)

add_library(lua-https STATIC
${lua-https_SOURCE_DIR}/src/common/config.h
${lua-https_SOURCE_DIR}/src/common/Connection.h
${lua-https_SOURCE_DIR}/src/common/ConnectionClient.h
${lua-https_SOURCE_DIR}/src/common/HTTPRequest.cpp
${lua-https_SOURCE_DIR}/src/common/HTTPRequest.h
${lua-https_SOURCE_DIR}/src/common/HTTPS.cpp
${lua-https_SOURCE_DIR}/src/common/HTTPS.h
${lua-https_SOURCE_DIR}/src/common/HTTPSClient.cpp
${lua-https_SOURCE_DIR}/src/common/HTTPSClient.h
${lua-https_SOURCE_DIR}/src/common/PlaintextConnection.cpp
${lua-https_SOURCE_DIR}/src/common/PlaintextConnection.h
${lua-https_SOURCE_DIR}/src/generic/CurlClient.cpp
${lua-https_SOURCE_DIR}/src/generic/CurlClient.h
${lua-https_SOURCE_DIR}/src/generic/OpenSSLConnection.cpp
${lua-https_SOURCE_DIR}/src/generic/OpenSSLConnection.h
${lua-https_SOURCE_DIR}/src/lua/luahttps.cpp
)

add_library(lua-https STATIC
${lua-https_SOURCE_DIR}/src/common/config.h
${lua-https_SOURCE_DIR}/src/common/Connection.h
${lua-https_SOURCE_DIR}/src/common/ConnectionClient.h
${lua-https_SOURCE_DIR}/src/common/HTTPRequest.cpp
${lua-https_SOURCE_DIR}/src/common/HTTPRequest.h
${lua-https_SOURCE_DIR}/src/common/HTTPS.cpp
${lua-https_SOURCE_DIR}/src/common/HTTPS.h
${lua-https_SOURCE_DIR}/src/common/HTTPSClient.cpp
${lua-https_SOURCE_DIR}/src/common/HTTPSClient.h
${lua-https_SOURCE_DIR}/src/common/PlaintextConnection.cpp
${lua-https_SOURCE_DIR}/src/common/PlaintextConnection.h
${lua-https_SOURCE_DIR}/src/generic/CurlClient.cpp
${lua-https_SOURCE_DIR}/src/generic/CurlClient.h
${lua-https_SOURCE_DIR}/src/generic/OpenSSLConnection.cpp
${lua-https_SOURCE_DIR}/src/generic/OpenSSLConnection.h
${lua-https_SOURCE_DIR}/src/lua/luahttps.cpp
)
target_compile_definitions(lua-https PRIVATE HTTPS_BACKEND_CURL HTTPS_BACKEND_CURL_LINKED)

# link curl
pkg_check_modules(libcurl REQUIRED IMPORTED_TARGET libcurl)
target_link_libraries(lua-https PRIVATE PkgConfig::lua51 PkgConfig::libcurl)
endif()

target_compile_definitions(lua-https PRIVATE HTTPS_BACKEND_CURL HTTPS_BACKEND_CURL_LINKED)

# luasocket
add_library(luasocket
Expand Down Expand Up @@ -336,11 +340,8 @@ pkg_check_modules(liblz4 REQUIRED IMPORTED_TARGET liblz4)
target_link_libraries(${PROJECT_NAME} PRIVATE PkgConfig::liblz4)

# link lua5.1
target_link_libraries(${PROJECT_NAME} PRIVATE PkgConfig::lua51)

# link curl
pkg_check_modules(libcurl REQUIRED IMPORTED_TARGET libcurl)
target_link_libraries(lua-https PRIVATE PkgConfig::lua51 PkgConfig::libcurl)
target_link_libraries(${PROJECT_NAME} PRIVATE PkgConfig::lua51)

# link everything else
target_link_libraries(${PROJECT_NAME} PRIVATE
Expand Down
23 changes: 22 additions & 1 deletion include/utilities/driver/renderer/drawcommand.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,35 @@ namespace love
}
}

DrawCommand(size_t count, Shader<>::StandardShader shader, CommonFormat format) :
count(count),
size(count * VERTEX_SIZE),
format(format),
shader(shader)
{
if (count == 0)
throw love::Exception("Vertex count cannot be zero.");

try
{
this->vertices = std::make_unique<Vertex[]>(count);
}
catch (std::bad_alloc&)
{
throw love::Exception("Out of memory.");
}
}

DrawCommand Clone()
{
/* init count, size, shader, and type */
DrawCommand clone(this->count, this->type, this->shader);
clone.format = this->format;
clone.handles = this->handles;

std::copy_n(this->Positions().get(), this->count, clone.Positions().get());
if (this->positions)
std::copy_n(this->Positions().get(), this->count, clone.Positions().get());

std::copy_n(this->Vertices().get(), this->count, clone.Vertices().get());

return clone;
Expand Down
122 changes: 49 additions & 73 deletions source/objects/font/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ const Font::Glyph& Font::AddGlyph(TextShaper::GlyphIndex glyphIndex)

_glyph.sheet = sheetInfo.index;
_glyph.texture = &this->textures[_glyph.sheet];
_glyph.spacing = data->GetAdvance();

left = sheetInfo.left;
top = sheetInfo.top;
Expand Down Expand Up @@ -422,31 +421,31 @@ float Font::GetKerning(const std::string& left, const std::string& right)
return this->shaper->GetKerning(left, right);
}

static bool shouldCreateCommand(const Font::DrawCommand& command, const Font::Glyph& glyph)
{
if (Console::Is(Console::CTR))
return command.sheet != glyph.sheet;

return command.texture != glyph.texture;
}

struct PositionedGlyph
{
const TextShaper::GlyphPosition* position;
const Font::Glyph* glyph;
Color color;
};

static int glyphCompare(const void* a, const void* b)
static bool sortGlyphs(const PositionedGlyph& left, const PositionedGlyph& right)
{
const auto* glyph = (const PositionedGlyph*)a;
const auto* other = (const PositionedGlyph*)b;
if (Console::Is(Console::CTR))
{
const auto result = left.glyph->texture - right.glyph->texture;
return result < 0;
}

const int result = glyph->glyph->sheet - other->glyph->sheet;
return left.glyph->texture < right.glyph->texture;
}

if (result == 0)
return (int)glyph->glyph - (int)other->glyph;
static inline bool isSimilarCommand(const Font::DrawCommand& previous,
std::vector<love::Font::DrawCommand>::iterator current)
{
if (Console::Is(Console::CTR))
return previous.sheet == current->sheet;

return result;
return previous.texture == current->texture;
}

std::vector<Font::DrawCommand> Font::GenerateVertices(const ColoredCodepoints& codepoints,
Expand All @@ -462,43 +461,27 @@ std::vector<Font::DrawCommand> Font::GenerateVertices(const ColoredCodepoints& c
&colors, info);

size_t vertexStartSize = vertices.size();
vertices.resize(vertexStartSize + positions.size() * 6);

const auto linearColor = Graphics<>::GammaCorrectColor(color);
auto currentColor = linearColor;

int currentColorIndex = 0;
int colorCount = colors.size();

/* sort glyphs on 3DS beforehand */
std::vector<PositionedGlyph> glyphs {};

{
for (int index = 0; index < (int)positions.size(); index++)
{
const auto& info = positions[index];
const auto& glyph = this->FindGlyph(info.glyphIndex);

glyphs.emplace_back(&info, &glyph);
}
}

if (Console::Is(Console::CTR))
std::qsort(glyphs.data(), glyphs.size(), sizeof(PositionedGlyph), glyphCompare);

std::vector<DrawCommand> commands {};
glyphs.reserve(positions.size());

for (int index = 0; index < (int)positions.size(); index++)
{
const auto& info = *glyphs[index].position;
const auto& info = positions[index];
uint32_t cacheId = this->textureCacheID;

const auto& glyph = *glyphs[index].glyph;
const auto& glyph = this->FindGlyph(info.glyphIndex);

if (cacheId != this->textureCacheID)
{
index = -1;
commands.clear();
glyphs.clear();
vertices.resize(vertexStartSize);
currentColorIndex = 0;
currentColor = color;
Expand All @@ -522,54 +505,46 @@ std::vector<Font::DrawCommand> Font::GenerateVertices(const ColoredCodepoints& c
currentColorIndex++;
}

glyphs.emplace_back(&info, &glyph, currentColor);
}

std::sort(glyphs.begin(), glyphs.end(), sortGlyphs);

std::vector<DrawCommand> commands {};

for (int index = 0; index < (int)glyphs.size(); index++)
{
const auto& info = *glyphs[index].position;

const auto& glyph = *glyphs[index].glyph;
const auto& color = glyphs[index].color;

if (glyph.texture != nullptr)
{
for (int j = 0; j < (int)glyph.vertices.size(); j++)
{
vertices.push_back(glyph.vertices[j]);
vertices.back().position[0] += info.position.x;
vertices.back().position[1] += info.position.y;
vertices.back().color = currentColor.array();
vertices.back().color = color.array();
}

// check if a glyph texture or sheet has changed since last iteration
if (commands.empty() || shouldCreateCommand(commands.back(), glyph))
if (commands.empty() || commands.back().texture != glyph.texture)
{
DrawCommand command {};
command.start = (int)vertices.size() - 6;
command.count = 0;
command.texture = glyph.texture;
command.sheet = glyph.sheet;

commands.push_back(command);
DrawCommand& command = commands.emplace_back();
command.start = (int)vertices.size() - 0x06;
command.count = 0x0;
command.texture = glyph.texture;
}

commands.back().count += 6;
commands.back().count += 0x06;
}
}

const auto drawSort = [](const DrawCommand& left, const DrawCommand& right) -> bool {
if (left.texture != right.texture)
return left.texture < right.texture;
else
return left.start < right.start;
};

if (!Console::Is(Console::CTR))
std::sort(commands.begin(), commands.end(), drawSort);

return commands;
}

static inline bool isSimilarCommand(const Font::DrawCommand& previous,
std::vector<love::Font::DrawCommand>::iterator current)
{
if (Console::Is(Console::CTR))
return previous.sheet == current->sheet;

return previous.texture == current->texture;
}

std::vector<Font::DrawCommand> Font::GenerateVerticesFormatted(
const ColoredCodepoints& codepoints, const Color& color, float wrap, AlignMode align,
std::vector<vertex::Vertex>& vertices, TextShaper::TextInfo* info)
Expand Down Expand Up @@ -688,15 +663,16 @@ void Font::Render(Graphics<>& graphics, const Matrix4& transform,
for (const auto& command : drawCommands)
{
/* total vertices for the quads */
love::DrawCommand drawCommand(command.count);
CommonFormat format = CommonFormat::TEXTURE;
if (Console::Is(Console::CTR))
format = CommonFormat::FONT;

if (!Console::Is(Console::CTR))
{
drawCommand.shader = Shader<>::STANDARD_TEXTURE;
drawCommand.format = CommonFormat::TEXTURE;
}
else
drawCommand.format = CommonFormat::FONT;
auto shader = Shader<>::STANDARD_TEXTURE;
if (Console::Is(Console::CTR))
shader = Shader<>::STANDARD_DEFAULT;

love::DrawCommand drawCommand(command.count, shader, format);
drawCommand.type = PrimitiveType::PRIMITIVE_TRIANGLES;

/* texture to use - single texture */
drawCommand.handles = { command.texture };
Expand Down

0 comments on commit 98a550f

Please sign in to comment.