Skip to content

Commit

Permalink
Dev/citro3d (#200)
Browse files Browse the repository at this point in the history
* framebuffers work

* actually map triangle fan

* primitives

* text rendering

* use a single vertex buffer on 3ds

* flush on target change

* push some rework for optimizations

* ( ͡° ͜ʖ ͡°) optimized

* canvases, as a treat
  • Loading branch information
TurtleP authored Aug 26, 2023
1 parent f9409fe commit a530dde
Show file tree
Hide file tree
Showing 50 changed files with 2,087 additions and 781 deletions.
19 changes: 5 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ if(NINTENDO_3DS)
# Specify that the executable is a 3DSX file
ctr_create_3dsx(${PROJECT_NAME}
SMDH ${PROJECT_NAME}.smdh
ROMFS ${PROJECT_NAME}_ctr_romfs
)

set(APP_LIBS citro2d citro3d)
Expand Down Expand Up @@ -107,13 +108,6 @@ if(NINTENDO_SWITCH)
ROMFS ${PROJECT_NAME}_hac_romfs
)

target_sources(${PROJECT_NAME} PRIVATE
source/utilities/driver/renderer/polyline/polyline.cpp
source/utilities/driver/renderer/polyline/types/beveljoin.cpp
source/utilities/driver/renderer/polyline/types/miterjoin.cpp
source/utilities/driver/renderer/polyline/types/nonejoin.cpp
)

find_package(Freetype REQUIRED)

set(APP_LIBS deko3d freetype bz2)
Expand Down Expand Up @@ -184,13 +178,6 @@ if (NINTENDO_WIIU)

execute_process(COMMAND patch -d ${CMAKE_CURRENT_BINARY_DIR}/luasocket/libluasocket -N -i ${PROJECT_SOURCE_DIR}/platform/cafe/libraries/luasocket.patch)

target_sources(${PROJECT_NAME} PRIVATE
source/utilities/driver/renderer/polyline/polyline.cpp
source/utilities/driver/renderer/polyline/types/beveljoin.cpp
source/utilities/driver/renderer/polyline/types/miterjoin.cpp
source/utilities/driver/renderer/polyline/types/nonejoin.cpp
)

find_package(Freetype REQUIRED)
set(APP_LIBS freetype bz2)

Expand Down Expand Up @@ -451,6 +438,10 @@ 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/polyline/polyline.cpp
source/utilities/driver/renderer/polyline/types/beveljoin.cpp
source/utilities/driver/renderer/polyline/types/miterjoin.cpp
source/utilities/driver/renderer/polyline/types/nonejoin.cpp
source/utilities/driver/renderer/renderstate.cpp
source/utilities/driver/renderer/samplerstate.cpp
source/utilities/formathandler/formathandler.cpp
Expand Down
16 changes: 8 additions & 8 deletions include/common/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
#define LOVE_TODEG(x) (float)(x * LOVE_M_TODEG)

/* Misc. Math Constants */
#define LOVE_M_E 2.71828182845904523536
#define LOVE_M_LOG2E 1.44269504088896340736
#define LOVE_M_LOG10E 0.434294481903251827651
#define LOVE_M_LN2 0.693147180559945309417
#define LOVE_M_LN10 2.30258509299404568402
#define LOVE_M_SQRT2 1.41421356237309504880
#define LOVE_M_SQRT1_2 0.707106781186547524401
#define LOVE_M_E 2.71828182845904523536
#define LOVE_M_LOG2E 1.44269504088896340736
#define LOVE_M_LOG10E 0.434294481903251827651
#define LOVE_M_LN2 0.693147180559945309417
#define LOVE_M_LN10 2.30258509299404568402
#define LOVE_M_SQRT2 1.41421356237309504880
#define LOVE_M_SQRT1_2 0.707106781186547524401

/* 3DS Texture Limits */
#define LOVE_TEX3DS_MIN 0x08
Expand Down Expand Up @@ -76,7 +76,7 @@ namespace love
this->x = rect[0];
this->y = rect[1];
this->w = rect[2];
this->h = rect[4];
this->h = rect[3];
}

Rect(int x, int y, int width, int height) : x(x), y(y), w(width), h(height)
Expand Down
5 changes: 3 additions & 2 deletions include/common/screen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ namespace love

enum Screen : int8_t;

inline Screen currentScreen = (Screen)0;
static constexpr Screen SCREEN_INVALID = (Screen)-1;
inline Screen currentScreen = (Screen)0;
static constexpr Screen SCREEN_INVALID = (Screen)-1;
static constexpr inline Screen DEFAULT_SCREEN = (Screen)0;

const ScreenInfo& GetScreenInfo(Screen);
std::span<ScreenInfo> GetScreenInfo();
Expand Down
2 changes: 1 addition & 1 deletion include/modules/font/fontmodule.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ namespace love
}

protected:
StrongReference<Data> defaultFontData;
StrongReference<Object> defaultFontData;
};
} // namespace love
53 changes: 37 additions & 16 deletions include/modules/graphics/graphics.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@

#include <objects/quad/quad.hpp>

#if !defined(__3DS__)
#include <utilities/driver/drawcommand.hpp>
#include <utilities/driver/renderer/drawcommand.tcc>

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

#include <utilities/driver/renderer/polyline/types/beveljoin.hpp>
#include <utilities/driver/renderer/polyline/types/miterjoin.hpp>
#include <utilities/driver/renderer/polyline/types/nonejoin.hpp>
#endif
#include <utilities/driver/renderer/polyline/types/beveljoin.hpp>
#include <utilities/driver/renderer/polyline/types/miterjoin.hpp>
#include <utilities/driver/renderer/polyline/types/nonejoin.hpp>

#include <utilities/driver/renderer/renderstate.hpp>
#include <utilities/driver/renderer/samplerstate.hpp>
Expand Down Expand Up @@ -75,6 +73,9 @@ namespace love
int textures;
int fonts;
int64_t textureMemory;

float gpuTime;
float cpuTime;
};

struct DisplayState
Expand Down Expand Up @@ -230,6 +231,10 @@ namespace love
void Present()
{
Renderer<Console::Which>::Instance().Present();

Renderer<Console::Which>::drawCalls = 0;
Renderer<Console::Which>::drawCallsBatched = 0;
Shader<Console::Which>::shaderSwitches = 0;
}

/* graphics state */
Expand Down Expand Up @@ -276,6 +281,20 @@ namespace love
return this->states.back().font;
}

void SetCanvas(Texture<Console::Which>* canvas)
{
this->states.back().renderTarget = canvas;

Renderer<Console::Which>::Instance().BindFramebuffer(canvas);
if (this->states.back().scissor.active)
this->SetScissor(this->states.back().scissor.bounds);
}

Texture<Console::Which>* GetCanvas()
{
return this->states.back().renderTarget;
}

void SetShader()
{
this->states.back().shader.Set(nullptr);
Expand All @@ -296,11 +315,15 @@ namespace love
{
Stats stats {};

stats.drawCalls = Renderer<>::drawCalls;
stats.textures = Texture<>::textureCount;
stats.fonts = Font<>::fontCount;
stats.shaderSwitches = Shader<>::shaderSwitches;
stats.textureMemory = Texture<>::totalGraphicsMemory;
stats.drawCalls = Renderer<>::drawCalls;
stats.textures = Texture<>::textureCount;
stats.fonts = Font<>::fontCount;
stats.shaderSwitches = Shader<>::shaderSwitches;
stats.textureMemory = Texture<>::totalGraphicsMemory;
stats.drawCallsBatched = Renderer<>::drawCallsBatched;

stats.cpuTime = Renderer<>::cpuTime;
stats.gpuTime = Renderer<>::gpuTime;

return stats;
}
Expand Down Expand Up @@ -624,7 +647,6 @@ namespace love

/* PRIMITIVES */

#if !defined(__3DS__)
void Polyline(const std::span<Vector2> points)
{
float halfWidth = this->GetLineWidth() * 0.5f;
Expand Down Expand Up @@ -667,7 +689,7 @@ namespace love
bool is2D = transform.IsAffine2DTransform();

const int count = points.size() - (skipLastVertex ? 1 : 0);
DrawCommand command(count, vertex::PRIMITIVE_TRIANGLE_FAN);
DrawCommand<Console::Which> command(count, vertex::PRIMITIVE_TRIANGLE_FAN);

if (is2D)
transform.TransformXY(command.Positions().get(), points.data(), command.count);
Expand Down Expand Up @@ -898,7 +920,7 @@ namespace love
const auto& transform = this->GetTransform();
bool is2D = transform.IsAffine2DTransform();

DrawCommand command(points.size(), vertex::PRIMITIVE_POINTS);
DrawCommand<Console::Which> command(points.size(), vertex::PRIMITIVE_TRIANGLE_FAN);

if (is2D)
transform.TransformXY(command.Positions().get(), points.data(), points.size());
Expand All @@ -915,7 +937,6 @@ namespace love
{
this->Polyline(points);
}
#endif

/* PRIMITIVES */

Expand Down
16 changes: 12 additions & 4 deletions include/modules/graphics/wrap_graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ namespace Wrap_Graphics

int GetBlendState(lua_State* L);

int SetMeshCullMode(lua_State*L);
int SetMeshCullMode(lua_State* L);

int GetMeshCullMode(lua_State*L);
int GetMeshCullMode(lua_State* L);

int SetFrontFaceWinding(lua_State*L);
int SetFrontFaceWinding(lua_State* L);

int GetFrontFaceWinding(lua_State*L);
int GetFrontFaceWinding(lua_State* L);

int SetDefaultFilter(lua_State* L);

Expand Down Expand Up @@ -144,6 +144,14 @@ namespace Wrap_Graphics

int NewTexture(lua_State* L);

int NewImage(lua_State* L);

int NewCanvas(lua_State* L);

int SetCanvas(lua_State* L);

int GetCanvas(lua_State* L);

int NewQuad(lua_State* L);

int NewTextBatch(lua_State* L);
Expand Down
77 changes: 76 additions & 1 deletion include/objects/font/font.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

#include <utilities/driver/renderer/renderstate.hpp>
#include <utilities/driver/renderer/samplerstate.hpp>
#include <utilities/driver/renderer/vertex.hpp>

#include <objects/rasterizer/rasterizer.tcc>

#include <vector>

namespace love
Expand Down Expand Up @@ -62,6 +62,26 @@ namespace love
int height;
};

template<typename Tex, size_t N>
struct Glyph
{
Tex* texture;
int spacing;
std::array<vertex::Vertex, N> vertices;

int sheet;
};

template<typename Tex>
struct DrawCommand
{
Tex* texture;
int start;
int count;

int sheet;
};

static inline int fontCount = 0;

Font() : lineHeight(1.0f), samplerState {}, dpiScale(1.0f), useSpacesAsTab(false)
Expand Down Expand Up @@ -111,12 +131,67 @@ namespace love
// clang-format on

protected:
static void GetCodepointsFromString(const ColoredStrings& strings, ColoredCodepoints& out)
{
if (strings.empty())
return;

out.codepoints.reserve(strings[0].string.size());

for (const auto& coloredString : strings)
{
if (coloredString.string.size() == 0)
continue;

IndexedColor color = { coloredString.color, (int)out.codepoints.size() };
out.colors.push_back(color);

Font::GetCodepointsFromString(coloredString.string, out.codepoints);
}

if (out.colors.size() == 1)
{
IndexedColor color = out.colors[0];
if (color.index == 0 && color.color == Color(Color::WHITE))
out.colors.pop_back();
}
}

static void GetCodepointsFromString(std::string_view text, Codepoints& out)
{
out.reserve(text.size());

try
{
Utf8Iterator start(text.begin(), text.begin(), text.end());
Utf8Iterator end(text.end(), text.begin(), text.end());

while (start != end)
{
auto glyph = *start++;
out.push_back(glyph);
}
}
catch (utf8::exception& exception)
{
throw love::Exception("UTF-8 decoding error: %s", exception.what());
}
}

static constexpr auto SPACES_PER_TAB = 0x04;
static constexpr uint32_t TAB_GLYPH = 9;
static constexpr uint32_t SPACE_GLYPH = 32;

static constexpr uint32_t NEWLINE_GLYPH = 10;
static constexpr uint32_t CARRIAGE_GLYPH = 13;

float lineHeight;
float height;

SamplerState samplerState;
float dpiScale;
bool useSpacesAsTab;
std::vector<Rasterizer<Console::Which>*> rasterizers;

PixelFormat format;
};
Expand Down
Loading

0 comments on commit a530dde

Please sign in to comment.