Skip to content

Commit

Permalink
expose Quads to lua
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtleP committed May 4, 2024
1 parent 6dbd00a commit 0584a6b
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 137 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ source/modules/graphics/Volatile.cpp
source/modules/graphics/vertex.cpp
source/modules/graphics/wrap_Graphics.cpp
source/modules/graphics/wrap_Texture.cpp
source/modules/graphics/wrap_Quad.cpp
source/modules/graphics/Texture.cpp
source/modules/image/CompressedImageData.cpp
source/modules/image/CompressedSlice.cpp
Expand Down
3 changes: 3 additions & 0 deletions include/driver/graphics/DrawCommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,7 @@ namespace love

bool flushing = false;
};

static constexpr auto INIT_INDEX_BUFFER_SIZE = sizeof(uint16_t) * LOVE_UINT16_MAX;
static constexpr auto INIT_VERTEX_BUFFER_SIZE = 64 * 1024 * 1;
} // namespace love
17 changes: 12 additions & 5 deletions include/driver/graphics/StreamBuffer.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace love
usage(usage),
data(nullptr),
bufferSize(size),
frameIndex(0),
frameGPUReadOffset(0)
{}

Expand All @@ -33,14 +34,16 @@ namespace love

MapInfo map()
{
return MapInfo(this->data, this->bufferSize - this->frameGPUReadOffset);
}
MapInfo info {};

size_t unmap(size_t)
{
return (size_t)this->data;
info.size = this->bufferSize - this->frameGPUReadOffset;
info.data = this->data + (this->frameIndex * this->bufferSize) + this->frameGPUReadOffset;

return info;
}

virtual size_t unmap(size_t) = 0;

size_t getSize() const
{
return this->bufferSize;
Expand All @@ -63,15 +66,19 @@ namespace love

void nextFrame()
{
this->frameIndex = (this->frameIndex + 1) % BUFFER_FRAMES;
this->frameGPUReadOffset = 0;
}

protected:
static constexpr int BUFFER_FRAMES = 2;

BufferUsage usage;

uint8_t* data;
size_t bufferSize;

size_t frameIndex;
size_t frameGPUReadOffset;
};
} // namespace love
6 changes: 3 additions & 3 deletions include/modules/graphics/Graphics.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ namespace love
return info;
}

BatchedVertexData requestBatchDraw(const BatchedDrawCommand& command);
BatchedVertexData requestBatchedDraw(const BatchedDrawCommand& command);

void flushBatchedDraws();

Expand Down Expand Up @@ -566,9 +566,9 @@ namespace love
return 1.0;
}

void polyline(const std::span<Vector2> vertices);
void polyline(std::span<const Vector2> vertices);

void polygon(DrawMode mode, const std::span<Vector2> vertices, bool skipLastVertex = true);
void polygon(DrawMode mode, std::span<const Vector2> vertices, bool skipLastVertex = true);

void rectangle(DrawMode mode, float x, float y, float w, float h);

Expand Down
12 changes: 12 additions & 0 deletions include/modules/graphics/Quad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ namespace love
return this->viewport;
}

void setLayer(int layer)
{
this->layer = layer;
}

int getLayer() const
{
return this->layer;
}

double getTextureWidth() const
{
return this->sourceWidth;
Expand Down Expand Up @@ -65,5 +75,7 @@ namespace love
Viewport viewport;
double sourceWidth;
double sourceHeight;

int layer;
};
} // namespace love
39 changes: 5 additions & 34 deletions include/modules/graphics/Shader.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace love
public:
static Type type;

static int shaderSwitches;

enum StandardShader
{
STANDARD_DEFAULT,
Expand All @@ -29,43 +31,12 @@ namespace love
static ShaderBase* current;
static ShaderBase* standardShaders[STANDARD_MAX_ENUM];

virtual ~ShaderBase()
{
for (int index = 0; index < STANDARD_MAX_ENUM; index++)
{
if (this == standardShaders[index])
standardShaders[index] = nullptr;
}

if (current == this)
this->attachDefault(STANDARD_DEFAULT);
}
virtual ~ShaderBase();

virtual void attach() = 0;

static void attachDefault(StandardShader type)
{
auto* defaultShader = standardShaders[type];

if (defaultShader == nullptr)
{
current = nullptr;
return;
}

if (current != defaultShader)
defaultShader->attach();
}

static bool isDefaultActive()
{
for (int index = 0; index < STANDARD_MAX_ENUM; index++)
{
if (current == standardShaders[index])
return true;
}
static void attachDefault(StandardShader type);

return false;
}
static bool isDefaultActive();
};
} // namespace love
24 changes: 24 additions & 0 deletions include/modules/graphics/wrap_Quad.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "common/luax.hpp"
#include "modules/graphics/Quad.hpp"

namespace love
{
Quad* luax_checkquad(lua_State* L, int index);

int open_quad(lua_State* L);
} // namespace love

namespace Wrap_Quad
{
int setViewport(lua_State* L);

int getViewport(lua_State* L);

int getTextureDimensions(lua_State* L);

int setLayer(lua_State* L);

int getLayer(lua_State* L);
} // namespace Wrap_Quad
37 changes: 25 additions & 12 deletions platform/ctr/include/driver/graphics/StreamBuffer.hpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,62 @@
#pragma once

#include "driver/graphics/StreamBuffer.tcc"
#include "modules/graphics/Volatile.hpp"

#include <3ds.h>
#include <citro3d.h>

namespace love
{
class StreamBuffer final : public StreamBufferBase
class StreamBuffer final : public StreamBufferBase, public Volatile
{
public:
StreamBuffer(BufferUsage usage, size_t size) : StreamBufferBase(usage, size), buffer(nullptr)
{
this->data = (uint8_t*)linearAlloc(size);
this->loadVolatile();
}

bool loadVolatile()
{
const auto size = this->getSize() * BUFFER_FRAMES;
this->data = (uint8_t*)linearAlloc(size);

if (this->data == nullptr)
throw love::Exception(E_OUT_OF_MEMORY);
return false;

if (usage != BUFFERUSAGE_VERTEX)
return;
if (this->usage != BufferUsage::BUFFERUSAGE_VERTEX)
return true;

this->buffer = new C3D_BufInfo();
BufInfo_Init(this->buffer);

BufInfo_Add(this->buffer, this->data, sizeof(Vertex), 3, 0x210);
C3D_SetBufInfo(this->buffer);

return true;
}

~StreamBuffer()
void unloadVolatile()
{
linearFree(this->data);

delete this->buffer;
this->buffer = nullptr;
}

void bind(const void* offset)
~StreamBuffer()
{
if (this->buffer == nullptr)
return;
this->unloadVolatile();
}

BufInfo_Add(this->buffer, offset, sizeof(Vertex), 3, 0x210);
C3D_SetBufInfo(this->buffer);
size_t unmap(size_t) override
{
return (size_t)this->data;
}

ptrdiff_t getHandle() const override
{
return (ptrdiff_t)this->buffer;
return 0;
}

private:
Expand Down
74 changes: 12 additions & 62 deletions platform/ctr/source/modules/graphics/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ namespace love
// clang-format off
if (this->batchedDrawState.vertexBuffer == nullptr)
{
this->batchedDrawState.indexBuffer = new StreamBuffer(BUFFERUSAGE_INDEX, sizeof(uint16_t) * LOVE_UINT16_MAX);
this->batchedDrawState.vertexBuffer = new StreamBuffer(BUFFERUSAGE_VERTEX, 64 * 1024 * 1);
this->batchedDrawState.indexBuffer = new StreamBuffer(BUFFERUSAGE_INDEX, INIT_INDEX_BUFFER_SIZE);
this->batchedDrawState.vertexBuffer = new StreamBuffer(BUFFERUSAGE_VERTEX, INIT_VERTEX_BUFFER_SIZE);
}
// clang-format on

Expand Down Expand Up @@ -173,79 +173,29 @@ namespace love

void Graphics::points(Vector2* positions, const Color* colors, int count)
{
const float twoPi = float(LOVE_M_PI * 2);
const int extraPoints = 2;

const float pointSize = this->states.back().pointSize;

int points = this->calculateEllipsePoints(pointSize, pointSize);
const float shift = twoPi / (float)points;

const Matrix4& transform = this->getTransform();
bool is2D = transform.isAffine2DTransform();

BatchedDrawCommand command {};
command.format = CommonFormat::XYf_STf_RGBAf;
command.indexMode = TRIANGLEINDEX_FAN;
command.vertexCount = count * (points + extraPoints);

BatchedVertexData data = this->requestBatchDraw(command);

XYf_STf_RGBAf* stream = (XYf_STf_RGBAf*)data.stream;
const auto pointSize = this->states.back().pointSize;

for (int index = 0; index < count; index++)
{
const float x = positions[index].x;
const float y = positions[index].y;

float phi = 0.0f;
const auto& position = positions[index];
auto& color = colors[index];

stream[0].x = x;
stream[0].y = y;
gammaCorrectColor(this->getColor());

for (int j = 1; j <= points; ++j, phi += shift)
{
stream[j].x = x + pointSize * std::cos(phi);
stream[j].y = y + pointSize * std::sin(phi);
}

stream[points + 1] = stream[0];
stream += (points + extraPoints);
}

if (is2D)
transform.transformXY(stream, positions, command.vertexCount);

if (!colors)
{
Color color = this->getColor();

for (int index = 0; index < command.vertexCount; index++)
stream[index].color = color;

return;
}

Color color = this->getColor();
gammaCorrectColor(color);

if (isGammaCorrect())
{
for (int index = 0; index < command.vertexCount; index++)
if (isGammaCorrect())
{
Color current = colors[index];

gammaCorrectColor(current);
current *= color;
unGammaCorrectColor(current);

stream[index].color = current;
this->setColor(current);
}
}
else
{
for (int index = 0; index < command.vertexCount; index++)
stream[index].color = colors[index];
else
this->setColor(color);

this->circle(DRAW_FILL, position.x, position.y, pointSize);
}
}

Expand Down
Loading

0 comments on commit 0584a6b

Please sign in to comment.