Skip to content

Commit

Permalink
rendering improvements and stuff
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Feger <piepie62@users.noreply.github.com>
  • Loading branch information
TurtleP and piepie62 committed Nov 28, 2023
1 parent b4dd2db commit 81af561
Show file tree
Hide file tree
Showing 30 changed files with 418 additions and 223 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ test
*.wuhb

*.zip
debug.py
debug.py
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ target_sources(${PROJECT_NAME} PRIVATE
source/utilities/decoder/types/mp3decoder.cpp
source/utilities/decoder/types/vorbisdecoder.cpp
source/utilities/decoder/types/wavedecoder.cpp
source/utilities/driver/renderer/drawcommand.cpp
source/utilities/driver/renderer/polyline/polyline.cpp
source/utilities/driver/renderer/polyline/types/beveljoin.cpp
source/utilities/driver/renderer/polyline/types/miterjoin.cpp
Expand Down
7 changes: 3 additions & 4 deletions include/modules/graphics/graphics.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <objects/spritebatch/spritebatch.hpp>
#include <objects/textbatch/textbatch.hpp>

#include <utilities/driver/renderer/drawcommand.tcc>
#include <utilities/driver/renderer/drawcommand.hpp>

#include <utilities/driver/renderer/polyline/polyline.hpp>

Expand Down Expand Up @@ -1169,8 +1169,7 @@ namespace love
DrawCommand command(count, vertex::PRIMITIVE_TRIANGLE_FAN);

if (is2D)
transform.TransformXY(std::span(command.Positions().get(), command.count),
points);
transform.TransformXY(std::span(*command.Positions()), points);

command.FillVertices(this->GetColor());

Expand Down Expand Up @@ -1401,7 +1400,7 @@ namespace love
DrawCommand command(points.size(), vertex::PRIMITIVE_POINTS);

if (is2D)
transform.TransformXY(std::span(command.Positions().get(), points.size()), points);
transform.TransformXY(std::span(*command.Positions()), points);

if (colors.size() > 1)
command.FillVertices(colors);
Expand Down
4 changes: 3 additions & 1 deletion include/objects/bmfontrasterizer/bmfontrasterizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace love

int GetGlyphSpacing(uint32_t glyph) const override;

int GetGlyphWidth(uint32_t glyph) const override;

int GetGlyphIndex(uint32_t glyph) const override;

GlyphData* GetGlyphDataForIndex(int index) const override;
Expand Down Expand Up @@ -62,4 +64,4 @@ namespace love
bool unicode;
int lineHeight;
};
} // namespace love
} // namespace love
79 changes: 71 additions & 8 deletions include/objects/font/font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,14 @@ namespace love
int spacing;
std::array<vertex::Vertex, 0x06> vertices;

int sheet;
bool requiresDraw;
};

struct DrawCommand
{
TextureHandle* texture;
int start;
int count;

int sheet;
};

static inline int fontCount = 0;
Expand Down Expand Up @@ -115,17 +113,43 @@ namespace love

float GetBaseline() const;

std::vector<DrawCommand> GenerateVertices(const ColoredCodepoints& codepoints, Range range,
const Color& color,
std::span<vertex::Vertex>& vertices,
float extraSpacing = 0.0f, Vector2 offset = {},
TextShaper::TextInfo* info = nullptr)
{
return RealGenerateVertices(codepoints, range, color, vertices, extraSpacing, offset,
info);
}

std::vector<DrawCommand> GenerateVerticesFormatted(const ColoredCodepoints& codepoints,
const Color& color, float wrap,
AlignMode align,
std::span<vertex::Vertex>& vertices,
TextShaper::TextInfo* info = nullptr)
{
return RealGenerateVerticesFormatted(codepoints, color, wrap, align, vertices, info);
}

std::vector<DrawCommand> GenerateVertices(const ColoredCodepoints& codepoints, Range range,
const Color& color,
std::vector<vertex::Vertex>& vertices,
float extraSpacing = 0.0f, Vector2 offset = {},
TextShaper::TextInfo* info = nullptr);
TextShaper::TextInfo* info = nullptr)
{
return RealGenerateVertices(codepoints, range, color, vertices, extraSpacing, offset,
info);
}

std::vector<DrawCommand> GenerateVerticesFormatted(const ColoredCodepoints& codepoints,
const Color& color, float wrap,
AlignMode align,
std::vector<vertex::Vertex>& vertices,
TextShaper::TextInfo* info = nullptr);
TextShaper::TextInfo* info = nullptr)
{
return RealGenerateVerticesFormatted(codepoints, color, wrap, align, vertices, info);
}

void SetLineHeight(float height);

Expand Down Expand Up @@ -161,6 +185,10 @@ namespace love

static constexpr int MAX_TEXTURE_SIZE = 2048;

void AllocateVertices(std::span<vertex::Vertex>& vertices, size_t count);

void AllocateVertices(std::vector<vertex::Vertex>& vertices, size_t count);

bool LoadVolatile();

void UnloadVolatile();
Expand All @@ -177,7 +205,24 @@ namespace love

void Render(Graphics<Console::ALL>& graphics, const Matrix4& transform,
const std::vector<DrawCommand>& drawCommands,
const std::vector<vertex::Vertex>& vertices);
const std::span<vertex::Vertex>& vertices);

template<typename Container>
requires std::same_as<Container, std::vector<vertex::Vertex>> ||
std::same_as<Container, std::span<vertex::Vertex>>
std::vector<DrawCommand> RealGenerateVertices(const ColoredCodepoints& codepoints,
Range range, const Color& color,
Container& vertices,
float extraSpacing = 0.0f,
Vector2 offset = {},
TextShaper::TextInfo* info = nullptr);

template<typename Container>
requires std::same_as<Container, std::vector<vertex::Vertex>> ||
std::same_as<Container, std::span<vertex::Vertex>>
std::vector<DrawCommand> RealGenerateVerticesFormatted(
const ColoredCodepoints& codepoints, const Color& color, float wrap, AlignMode align,
Container& vertices, TextShaper::TextInfo* info = nullptr);

int textureX;
int textureY;
Expand All @@ -191,12 +236,12 @@ namespace love
#if defined(__3DS__)
std::vector<C3D_Tex> textures;
#else
std::vector<StrongReference<Texture<Console::Which> > > textures;
std::vector<StrongReference<Texture<Console::Which>>> textures;
#endif

StrongReference<TextShaper> shaper;

std::map<uint32_t, Glyph> glyphs;
std::unordered_map<uint32_t, Glyph> glyphs;
std::unordered_map<uint64_t, float> kernings;

static constexpr auto SPACES_PER_TAB = 0x04;
Expand All @@ -214,4 +259,22 @@ namespace love

PixelFormat format;
};

extern template std::vector<Font::DrawCommand> Font::RealGenerateVertices(
const ColoredCodepoints& codepoints, Range range, const Color& color,
std::span<vertex::Vertex>& vertices, float extraSpacing = 0.0f, Vector2 offset = {},
TextShaper::TextInfo* info = nullptr);

extern template std::vector<Font::DrawCommand> Font::RealGenerateVertices(
const ColoredCodepoints& codepoints, Range range, const Color& color,
std::vector<vertex::Vertex>& vertices, float extraSpacing = 0.0f, Vector2 offset = {},
TextShaper::TextInfo* info = nullptr);

extern template std::vector<Font::DrawCommand> Font::RealGenerateVerticesFormatted(
const ColoredCodepoints& codepoints, const Color& color, float wrap, AlignMode align,
std::span<vertex::Vertex>& vertices, TextShaper::TextInfo* info = nullptr);

extern template std::vector<Font::DrawCommand> Font::RealGenerateVerticesFormatted(
const ColoredCodepoints& codepoints, const Color& color, float wrap, AlignMode align,
std::vector<vertex::Vertex>& vertices, TextShaper::TextInfo* info = nullptr);
} // namespace love
4 changes: 3 additions & 1 deletion include/objects/imagerasterizer/imagerasterizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace love

int GetGlyphSpacing(uint32_t glyph) const override;

int GetGlyphWidth(uint32_t glyph) const override;

int GetGlyphIndex(uint32_t glyph) const override;

GlyphData* GetGlyphDataForIndex(int index) const override;
Expand Down Expand Up @@ -54,4 +56,4 @@ namespace love

Color32 spacer;
};
} // namespace love
} // namespace love
2 changes: 2 additions & 0 deletions include/objects/rasterizer/rasterizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ namespace love

virtual int GetGlyphSpacing(uint32_t glyph) const = 0;

virtual int GetGlyphWidth(uint32_t glyph) const = 0;

virtual int GetGlyphIndex(uint32_t glyph) const = 0;

GlyphData* GetGlyphData(uint32_t glyph) const;
Expand Down
5 changes: 5 additions & 0 deletions include/objects/truetyperasterizer/truetyperasterizer.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ namespace love

int GetGlyphSpacing(uint32_t glyph) const override;

int GetGlyphWidth(uint32_t glyph) const override
{
return GetGlyphSpacing(glyph);
}

int GetGlyphIndex(uint32_t glyph) const override;

GlyphData* GetGlyphDataForIndex(int index) const override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace love
using namespace vertex;

#if defined(__3DS__)
#include <citro3d.h>
using Handle = C3D_Tex;
#else
using Handle = Texture<Console::Which>;
Expand All @@ -25,6 +26,8 @@ namespace love
DrawCommand()
{}

std::span<vertex::Vertex> AllocateVertices(size_t count);

DrawCommand(size_t count, PrimitiveType type = PRIMITIVE_TRIANGLES,
Shader<>::StandardShader shader = Shader<>::STANDARD_DEFAULT) :
positions {},
Expand All @@ -39,8 +42,8 @@ namespace love

try
{
this->positions = std::make_unique<Vector2[]>(count);
this->vertices = std::make_unique<Vertex[]>(count);
this->positions = std::vector<Vector2>(count);
this->vertices = AllocateVertices(count);
}
catch (std::bad_alloc&)
{
Expand All @@ -59,47 +62,50 @@ namespace love

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

DrawCommand Clone()
DrawCommand(std::span<vertex::Vertex> vertices, PrimitiveType type,
Shader<>::StandardShader shader, CommonFormat format) :
vertices(vertices),
count(vertices.size()),
size(count * VERTEX_SIZE),
format(format),
type(type),
shader(shader)
{
/* init count, size, shader, and type */
DrawCommand clone(this->count, this->type, this->shader);
clone.format = this->format;
clone.handles = this->handles;

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());
if (this->count == 0)
throw love::Exception("Vertex count cannot be zero.");
}

return clone;
std::optional<std::vector<Vector2>>& Positions()
{
return this->positions;
}

const std::unique_ptr<Vector2[]>& Positions() const
const std::optional<std::vector<Vector2>>& Positions() const
{
return this->positions;
}

const std::span<Vector2> GetPositions() const
std::span<const Vector2> GetPositions() const
{
return std::span<Vector2>(this->positions.get(), this->count);
return this->positions ? *this->positions : std::span<const Vector2> {};
}

const std::unique_ptr<Vertex[]>& Vertices() const
std::span<const Vertex> Vertices() const
{
return this->vertices;
return std::span<const Vertex>(this->vertices.data(), this->vertices.size());
}

const std::span<Vertex> GetVertices() const
std::span<Vertex> Vertices()
{
return std::span<Vertex>(this->vertices.get(), this->count);
return this->vertices;
}

/* primitive */
Expand All @@ -110,7 +116,7 @@ namespace love
// clang-format off
this->vertices[index] =
{
.position = { this->positions[index].x, this->positions[index].y, 0 },
.position = { (*this->positions)[index].x, (*this->positions)[index].y, 0 },
.color = color.array(),
.texcoord = { 0.0f, 0.0f }
};
Expand All @@ -126,7 +132,7 @@ namespace love
// clang-format off
this->vertices[index] =
{
.position = { this->positions[index].x, this->positions[index].y, 0 },
.position = { (*this->positions)[index].x, (*this->positions)[index].y, 0 },
.color = colors[index].array(),
.texcoord = { 0, 0 }
};
Expand All @@ -142,7 +148,7 @@ namespace love
// clang-format off
this->vertices[index] =
{
.position = { this->positions[index].x, this->positions[index].y, 0 },
.position = { (*this->positions)[index].x, (*this->positions)[index].y, 0 },
.color = colors[index].array(),
.texcoord = { 0, 0 }
};
Expand All @@ -158,7 +164,7 @@ namespace love
// clang-format off
this->vertices[index] =
{
.position = { this->positions[index].x, this->positions[index].y, 0 },
.position = { (*this->positions)[index].x, (*this->positions)[index].y, 0 },
.color = color.array(),
.texcoord = { textureCoords[index].x, textureCoords[index].y }
};
Expand All @@ -174,7 +180,7 @@ namespace love
// clang-format off
this->vertices[index] =
{
.position = { this->positions[index].x, this->positions[index].y, 0 },
.position = { (*this->positions)[index].x, (*this->positions)[index].y, 0 },
.color = source[index].color,
.texcoord = source[index].texcoord
};
Expand All @@ -183,8 +189,8 @@ namespace love
}

public:
std::unique_ptr<Vector2[]> positions;
std::unique_ptr<Vertex[]> vertices;
std::optional<std::vector<Vector2>> positions;
std::span<Vertex> vertices;

size_t count;
size_t size;
Expand Down
Loading

0 comments on commit 81af561

Please sign in to comment.