From 0aced702938c161871ebb19c6e133a88b07fc906 Mon Sep 17 00:00:00 2001 From: TurtleP Date: Wed, 1 May 2024 19:14:15 -0400 Subject: [PATCH] =?UTF-8?q?we're=20getting=20there=E2=84=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 1 - include/driver/display/Renderer.tcc | 216 +++++++++++++++- include/driver/graphics/DrawCommand.hpp | 122 +++++++++ include/modules/graphics/BatchedDrawState.hpp | 29 --- include/modules/graphics/Graphics.tcc | 84 ------- include/modules/graphics/vertex.hpp | 7 + platform/ctr/CMakeLists.txt | 1 - platform/ctr/assets/shaders/main.v.pica | 11 +- .../include/driver/display/Framebuffer.hpp | 5 +- .../ctr/include/driver/display/Renderer.hpp | 68 +++++ .../ctr/include/modules/graphics/Graphics.hpp | 6 +- .../ctr/include/modules/graphics/Shader.hpp | 2 + .../include/modules/graphics/StreamBuffer.hpp | 8 - platform/ctr/romfs/shaders/main_v_pica.shbin | Bin 356 -> 368 bytes .../ctr/source/driver/display/Framebuffer.cpp | 29 +-- .../ctr/source/driver/display/Renderer.cpp | 84 +++++-- .../ctr/source/modules/graphics/Graphics.cpp | 78 +----- .../ctr/source/modules/graphics/Shader.cpp | 6 + .../source/modules/graphics/StreamBuffer.cpp | 18 +- .../ctr/source/modules/graphics/Texture.cpp | 23 +- source/modules/graphics/Graphics.cpp | 235 +----------------- source/modules/graphics/Quad.cpp | 18 +- source/modules/graphics/StreamBuffer.cpp | 32 --- source/modules/graphics/vertex.cpp | 19 +- 24 files changed, 531 insertions(+), 571 deletions(-) create mode 100644 include/driver/graphics/DrawCommand.hpp delete mode 100644 include/modules/graphics/BatchedDrawState.hpp delete mode 100644 platform/ctr/include/modules/graphics/StreamBuffer.hpp delete mode 100644 source/modules/graphics/StreamBuffer.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f85ec6c7..b06f442a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -358,7 +358,6 @@ source/modules/filesystem/wrap_Filesystem.cpp source/modules/graphics/Graphics.cpp source/modules/graphics/renderstate.cpp source/modules/graphics/samplerstate.cpp -source/modules/graphics/StreamBuffer.cpp source/modules/graphics/Shader.cpp source/modules/graphics/Quad.cpp source/modules/graphics/Volatile.cpp diff --git a/include/driver/display/Renderer.tcc b/include/driver/display/Renderer.tcc index ddc1f345..8c235e07 100644 --- a/include/driver/display/Renderer.tcc +++ b/include/driver/display/Renderer.tcc @@ -2,10 +2,16 @@ #include "common/Singleton.tcc" +#include "modules/graphics/Shader.tcc" #include "modules/graphics/renderstate.hpp" #include "modules/graphics/vertex.hpp" +#include "driver/graphics/DrawCommand.hpp" + #include +#include + +#define BUFFER_OFFSET(i) ((char*)NULL + (i)) namespace love { @@ -21,6 +27,39 @@ namespace love RENDERER_INFO_DEVICE }; + struct BatchedVertexData + { + Vertex* stream; + }; + + struct BatchedDrawState + { + Vertex* vertices; + size_t verticesSize; + + StreamBuffer::MapInfo vertexMap = StreamBuffer::MapInfo(); + + StreamBuffer* indexBuffer; + StreamBuffer::MapInfo indexMap = StreamBuffer::MapInfo(); + + PrimitiveType primitiveMode = PRIMITIVE_TRIANGLES; + ShaderBase::StandardShader shader = ShaderBase::STANDARD_DEFAULT; + CommonFormat format = CommonFormat::NONE; + + StrongRef texture; + + int vertexCount = 0; + int indexCount = 0; + + bool flushing = false; + }; + + RendererBase() : batchedDrawState {} + { + size_t size = sizeof(uint16_t) * LOVE_UINT16_MAX; + this->batchedDrawState.indexBuffer = new StreamBuffer(BUFFERUSAGE_INDEX, size); + } + size_t getVertexCount() const { return this->renderCtx.vertexCount; @@ -31,6 +70,171 @@ namespace love return this->data; } + BatchedVertexData requestBatchDraw(const DrawCommand& command) + { + BatchedDrawState& state = this->batchedDrawState; + + bool shouldFlush = false; + bool shouldResize = false; + + // clang-format off + if (command.primitiveMode != state.primitiveMode + || command.format != state.format + || ((command.indexMode != TRIANGLEINDEX_NONE) != (state.indexCount > 0)) + || command.texture != state.texture + || command.shaderType != state.shader) + { + shouldFlush = true; + } + // clang-format on + + int totalVertices = state.vertexCount + command.vertexCount; + + if (totalVertices > LOVE_UINT16_MAX && command.indexMode != TRIANGLEINDEX_NONE) + shouldFlush = true; + + int requestedIndexCount = getIndexCount(command.indexMode, command.vertexCount); + size_t requestedIndexSize = requestedIndexCount * sizeof(uint16_t); + + size_t newDataSize = 0; + size_t bufferSizes[2] = { 0, 0 }; + + { + size_t stride = getFormatStride(command.format); + size_t dataSize = stride * totalVertices; + + if (state.vertices != nullptr && dataSize > state.verticesSize) + shouldFlush = true; + + if (dataSize > state.verticesSize) + { + bufferSizes[0] = std::max(dataSize, state.verticesSize * 1.1f); + shouldResize = true; + } + + newDataSize = stride * command.vertexCount; + } + + if (command.indexMode != TRIANGLEINDEX_NONE) + { + size_t dataSize = (state.indexCount + requestedIndexCount) * sizeof(uint16_t); + + if (state.indexMap.data != nullptr && dataSize > state.indexMap.size) + shouldFlush = true; + + if (dataSize > state.indexBuffer->getUsableSize()) + { + bufferSizes[1] = std::max(dataSize, state.indexBuffer->getSize() * 2); + shouldResize = true; + } + } + + if (shouldFlush || shouldResize) + { + flushBatchedDraws(); + + state.primitiveMode = command.primitiveMode; + state.format = command.format; + state.texture = command.texture; + state.shader = command.shaderType; + } + + if (state.vertexCount == 0) + { + if (ShaderBase::isDefaultActive()) + ShaderBase::attachDefault(state.shader); + } + + if (shouldResize) + { + if (state.verticesSize < bufferSizes[0]) + { + linearFree(state.vertices); + state.vertices = (Vertex*)linearAlloc(bufferSizes[0]); + } + + if (state.indexBuffer->getSize() < bufferSizes[1]) + { + state.indexBuffer->release(); + state.indexBuffer = new StreamBuffer(BUFFERUSAGE_INDEX, bufferSizes[1]); + } + } + + if (command.indexMode != TRIANGLEINDEX_NONE) + { + if (state.indexMap.data == nullptr) + state.indexMap = state.indexBuffer->map(); + + uint16_t* indices = (uint16_t*)state.indexMap.data; + fillIndices(command.indexMode, state.vertexCount, command.vertexCount, indices); + + state.indexMap.data += requestedIndexSize; + } + + BatchedVertexData data {}; + + if (newDataSize > 0) + { + if (state.vertexMap.data == nullptr) + { + const auto size = command.vertexCount * sizeof(Vertex); + state.vertexMap = StreamBuffer::MapInfo((uint8_t*)state.vertices, size); + } + + data.stream = (Vertex*)state.vertexMap.data; + state.vertexMap.data += newDataSize; + } + + state.vertexCount += command.vertexCount; + state.indexCount += requestedIndexCount; + + return data; + } + + virtual void updateUniforms() = 0; + + void flushBatchedDraws() + { + BatchedDrawState& state = this->batchedDrawState; + + if ((state.vertexCount == 0 && state.indexCount == 0) || state.flushing) + return; + + size_t usedSizes[2] = { 0, 0 }; + + this->updateUniforms(); + + if (state.format != CommonFormat::NONE) + state.vertexMap = StreamBuffer::MapInfo(); + + state.flushing = true; + + if (state.indexCount > 0) + { + DrawIndexedCommand command {}; + command.primitiveType = state.primitiveMode; + command.indexCount = state.indexCount; + command.indexType = INDEX_UINT16; + command.indexBufferOffset = state.indexBuffer->unmap(); + command.texture = nullptr; + + this->draw(command); + state.indexMap = StreamBuffer::MapInfo(); + } + + if (usedSizes[1] > 0) + state.indexBuffer->markUsed(usedSizes[1]); + + state.vertexCount = 0; + state.indexCount = 0; + state.flushing = false; + } + + static void flushBatchedDrawsGlobal() + { + RendererBase::getInstance().flushBatchedDraws(); + } + protected: struct ContextBase { @@ -43,17 +247,15 @@ namespace love Rect scissor; Rect viewport; + + ShaderBase::StandardShader shader = ShaderBase::STANDARD_DEFAULT; }; + virtual void draw(const DrawIndexedCommand& command) = 0; + bool initialized = false; bool inFrame = false; - Vertex* data; - - struct RenderContext - { - size_t vertexCount; - size_t indexCount; - } renderCtx; + BatchedDrawState batchedDrawState; }; } // namespace love diff --git a/include/driver/graphics/DrawCommand.hpp b/include/driver/graphics/DrawCommand.hpp new file mode 100644 index 00000000..fc13dec6 --- /dev/null +++ b/include/driver/graphics/DrawCommand.hpp @@ -0,0 +1,122 @@ +#pragma once + +#include "common/Exception.hpp" + +#include "modules/graphics/Shader.tcc" +#include "modules/graphics/Texture.tcc" +#include "modules/graphics/vertex.hpp" + +#if defined(__3DS__) + #include <3ds.h> + + #define _alloc(size) linearAlloc(size) + #define _free(ptr) linearFree(ptr) +#else + #define _alloc(size) malloc(size) + #define _free(ptr) free(ptr) +#endif + +namespace love +{ + class StreamBuffer final : public Object + { + public: + struct MapInfo + { + uint8_t* data = nullptr; + size_t size = 0; + + MapInfo() + {} + + MapInfo(uint8_t* data, size_t size) : data(data), size(size) + {} + }; + + StreamBuffer(BufferUsage usage, size_t size) : + usage(usage), + data(nullptr), + bufferSize(size), + frameGPUReadOffset(0) + { + this->data = (uint8_t*)_alloc(size); + + if (this->data == nullptr) + throw love::Exception(E_OUT_OF_MEMORY); + + std::memset(this->data, 0, size); + } + + virtual ~StreamBuffer() + { + _free(this->data); + } + + MapInfo map() + { + return MapInfo(this->data, this->bufferSize); + } + + size_t unmap() + { + return (size_t)this->data; + } + + size_t getSize() const + { + return this->bufferSize; + } + + size_t getUsableSize() const + { + return this->bufferSize - this->frameGPUReadOffset; + } + + BufferUsage getMode() const + { + return this->usage; + } + + void markUsed(size_t) + {} + + ptrdiff_t getHandle() const + { + return 0; + } + + private: + BufferUsage usage; + + uint8_t* data; + size_t bufferSize; + + size_t frameGPUReadOffset; + }; + + struct DrawCommand + { + PrimitiveType primitiveMode = PRIMITIVE_TRIANGLES; + ShaderBase::StandardShader shaderType = ShaderBase::STANDARD_DEFAULT; + TriangleIndexMode indexMode = TRIANGLEINDEX_NONE; + CommonFormat format = CommonFormat::NONE; + + TextureBase* texture = nullptr; + int vertexCount = 0; + }; + + struct DrawIndexedCommand + { + PrimitiveType primitiveType = PRIMITIVE_TRIANGLES; + int indexCount = 0; + int instanceCount = 1; + + IndexDataType indexType = INDEX_UINT16; + size_t lastPosition = 0; + + size_t indexBufferOffset = 0; + + TextureBase* texture; + CullMode cullMode = CULL_NONE; + }; +} // namespace love diff --git a/include/modules/graphics/BatchedDrawState.hpp b/include/modules/graphics/BatchedDrawState.hpp deleted file mode 100644 index a0ff5df9..00000000 --- a/include/modules/graphics/BatchedDrawState.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include "modules/graphics/Shader.tcc" -#include "modules/graphics/Texture.tcc" -#include "modules/graphics/vertex.hpp" - -#include "modules/graphics/StreamBuffer.hpp" - -namespace love -{ - struct BatchedDrawState - { - StreamBufferBase* vb[2]; - StreamBufferBase* indexBuffer = nullptr; - - PrimitiveType primitiveMode = PRIMITIVE_TRIANGLES; - CommonFormat formats[2] = { CommonFormat::NONE, CommonFormat::NONE }; - StrongRef texture; - ShaderBase::StandardShader shaderType = ShaderBase::STANDARD_DEFAULT; - - StreamBufferBase::MapInfo vbMap[2] = { StreamBufferBase::MapInfo(), StreamBufferBase::MapInfo() }; - StreamBufferBase::MapInfo indexBufferMap = StreamBufferBase::MapInfo(); - - int vertexCount; - int indexCount; - - bool flushing = false; - }; -} // namespace love diff --git a/include/modules/graphics/Graphics.tcc b/include/modules/graphics/Graphics.tcc index 15fbefb6..a2a0235e 100644 --- a/include/modules/graphics/Graphics.tcc +++ b/include/modules/graphics/Graphics.tcc @@ -17,8 +17,6 @@ #include "modules/graphics/renderstate.hpp" #include "modules/graphics/samplerstate.hpp" -#include "modules/graphics/BatchedDrawState.hpp" - #include #include @@ -206,88 +204,8 @@ namespace love void restoreState(const DisplayState& state); - struct BatchedVertexData - { - void* stream[2]; - }; - - struct BatchedDrawCommand - { - PrimitiveType primitiveMode = PRIMITIVE_TRIANGLES; - CommonFormat formats[2] = { CommonFormat::NONE, CommonFormat::NONE }; - TriangleIndexMode indexMode = TRIANGLEINDEX_NONE; - int vertexCount = 0; - TextureBase* texture = nullptr; - ShaderBase::StandardShader shaderType = ShaderBase::STANDARD_DEFAULT; - }; - - struct DrawIndexedCommand - { - PrimitiveType primitiveType = PRIMITIVE_TRIANGLES; - - const VertexAttributes* attributes; - const BufferBindings* buffers; - - int indexCount = 0; - int instanceCount = 1; - - IndexDataType indexType = INDEX_UINT16; - Resource* indexBuffer; - size_t indexBufferOffset = 0; - - Buffer* indirectBuffer = nullptr; - size_t indirectBufferOffset = 0; - - TextureBase* texture = nullptr; - - CullMode cullMode = CULL_NONE; - - DrawIndexedCommand(const VertexAttributes* attributes, const BufferBindings* buffers, - Resource* indexBuffer) : - attributes(attributes), - buffers(buffers), - indexBuffer(indexBuffer) - {} - }; - - struct DrawCommand - { - PrimitiveType primitiveType = PRIMITIVE_TRIANGLES; - - const VertexAttributes* attributes; - const BufferBindings* buffers; - - int vertexStart = 0; - int vertexCount = 0; - int instanceCount = 1; - - Resource* vertexBuffer; - - Buffer* indirectBuffer = nullptr; - size_t indirectBufferOffset = 0; - - TextureBase* texture = nullptr; - - CullMode cullMode = CULL_NONE; - - DrawCommand(const VertexAttributes* attributes, const BufferBindings* buffers) : - attributes(attributes), - buffers(buffers) - {} - }; - - BatchedVertexData requestBatchedDraw(const BatchedDrawCommand& command); - - void flushBatchedDraws(); - - static void flushBatchedDrawsGlobal(); - void restoreStateChecked(const DisplayState& state); - virtual void draw(const DrawCommand& command) = 0; - - virtual void draw(const DrawIndexedCommand& command) = 0; - /* TODO: implement when they exist */ bool isRenderTargetActive() const { @@ -672,8 +590,6 @@ namespace love int pixelWidth; int pixelHeight; - BatchedDrawState batchedDrawState; - int drawCallsBatched; int drawCalls; }; diff --git a/include/modules/graphics/vertex.hpp b/include/modules/graphics/vertex.hpp index 9f52aa27..57a1f26c 100644 --- a/include/modules/graphics/vertex.hpp +++ b/include/modules/graphics/vertex.hpp @@ -235,6 +235,13 @@ namespace love using Vertex = XYf_STf_RGBAf; + inline void DEBUG_VERTEX(const Vertex& v) + { + std::printf("Position: %.2f, %.2f\n", v.x, v.y); + std::printf("Texture Coordinates: %f, %f\n", v.s, v.t); + std::printf("Color: %.2f, %.2f, %.2f, %.2f\n", v.color.r, v.color.g, v.color.b, v.color.a); + } + inline CommonFormat getSinglePositionFormat(bool is2D) { return is2D ? CommonFormat::XYf : CommonFormat::XYf; diff --git a/platform/ctr/CMakeLists.txt b/platform/ctr/CMakeLists.txt index bee8c1ff..a9c485a5 100644 --- a/platform/ctr/CMakeLists.txt +++ b/platform/ctr/CMakeLists.txt @@ -43,7 +43,6 @@ source/driver/display/Renderer.cpp source/driver/EventQueue.cpp source/modules/audio/Source.cpp source/modules/graphics/Graphics.cpp -source/modules/graphics/StreamBuffer.cpp source/modules/graphics/Shader.cpp source/modules/graphics/Texture.cpp source/modules/joystick/Joystick.cpp diff --git a/platform/ctr/assets/shaders/main.v.pica b/platform/ctr/assets/shaders/main.v.pica index 89815251..dc192012 100644 --- a/platform/ctr/assets/shaders/main.v.pica +++ b/platform/ctr/assets/shaders/main.v.pica @@ -10,19 +10,20 @@ ; Inputs .in inPosition v0 -.in inColor v1 -.in inTexCoord v2 +.in inTexCoord v1 +.in inColor v2 ; Outputs .out outPosition position -.out outColor color .out outTexCoord0 texcoord0 +.out outColor color ; void main() .proc main ; r0 = vec4(inPosition, 1.0) - mov r0.xyz, inPosition - mov r0.w, ones + mov r0.xy, inPosition + mov r0.z, zeros + mov r0.w, ones ; pos = mdlvMtx * inPosition; dp4 r1.x, mdlvMtx[0], r0 diff --git a/platform/ctr/include/driver/display/Framebuffer.hpp b/platform/ctr/include/driver/display/Framebuffer.hpp index 2ccf670c..5aee3daf 100644 --- a/platform/ctr/include/driver/display/Framebuffer.hpp +++ b/platform/ctr/include/driver/display/Framebuffer.hpp @@ -18,8 +18,6 @@ namespace love void destroy(); - void setViewport(const Rect& viewport); - void setScissor(const Rect& scissor); C3D_RenderTarget* get() const @@ -42,8 +40,7 @@ namespace love static constexpr auto DISPLAY_TRANSFER_FLAGS = GX_TRANSFER_FLIP_VERT(0) | GX_TRANSFER_OUT_TILED(0) | GX_TRANSFER_RAW_COPY(0) | - GX_TRANSFER_IN_FORMAT(GX_TRANSFER_FMT_RGBA8) | - GX_TRANSFER_OUT_FORMAT(GX_TRANSFER_FMT_RGB8) | + GX_TRANSFER_IN_FORMAT(GX_TRANSFER_FMT_RGBA8) | GX_TRANSFER_OUT_FORMAT(GX_TRANSFER_FMT_RGB8) | GX_TRANSFER_SCALING(GX_TRANSFER_SCALE_NO); C3D_RenderTarget* target; diff --git a/platform/ctr/include/driver/display/Renderer.hpp b/platform/ctr/include/driver/display/Renderer.hpp index 3c4b41c3..ac3898cd 100644 --- a/platform/ctr/include/driver/display/Renderer.hpp +++ b/platform/ctr/include/driver/display/Renderer.hpp @@ -11,11 +11,21 @@ #include "modules/graphics/renderstate.hpp" #include "modules/graphics/samplerstate.hpp" +#include "driver/graphics/DrawCommand.hpp" + namespace love { class Renderer : public RendererBase { public: + enum TexEnvMode + { + TEXENV_PRIMITIVE, + TEXENV_TEXTURE, + TEXENV_FONT, + TEXENV_MAX_ENUM + }; + Renderer(); void initialize(); @@ -44,6 +54,10 @@ namespace love void setSamplerState(C3D_Tex* texture, SamplerState state); + void draw(const DrawIndexedCommand& command) override; + + virtual void updateUniforms() override; + void setWideMode(bool wide) { this->modeChanged([this, wide]() { gfxSetWide(wide); }); @@ -132,10 +146,64 @@ namespace love this->createFramebuffers(); } + void setPrimitiveAttribute() + { + C3D_TexEnv* env = C3D_GetTexEnv(0); + C3D_TexEnvInit(env); + + C3D_TexEnvSrc(env, C3D_Both, GPU_PRIMARY_COLOR, GPU_PRIMARY_COLOR, GPU_PRIMARY_COLOR); + C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE); + } + + void setTextureAttribute() + { + C3D_TexEnv* env = C3D_GetTexEnv(0); + C3D_TexEnvInit(env); + + C3D_TexEnvSrc(env, C3D_Both, GPU_TEXTURE0, GPU_PRIMARY_COLOR, GPU_PRIMARY_COLOR); + C3D_TexEnvFunc(env, C3D_Both, GPU_MODULATE); + } + + void setFontAttribute() + { + C3D_TexEnv* env = C3D_GetTexEnv(0); + C3D_TexEnvInit(env); + + C3D_TexEnvSrc(env, C3D_RGB, GPU_PRIMARY_COLOR, GPU_PRIMARY_COLOR, GPU_PRIMARY_COLOR); + C3D_TexEnvFunc(env, C3D_RGB, GPU_REPLACE); + + C3D_TexEnvSrc(env, C3D_Alpha, GPU_PRIMARY_COLOR, GPU_TEXTURE0, GPU_PRIMARY_COLOR); + C3D_TexEnvFunc(env, C3D_Alpha, GPU_MODULATE); + } + + void setAttribute(TexEnvMode mode) + { + if (this->context.mode == mode || mode == TEXENV_MAX_ENUM) + return; + + switch (mode) + { + default: + case TEXENV_PRIMITIVE: + this->setPrimitiveAttribute(); + break; + case TEXENV_TEXTURE: + this->setTextureAttribute(); + break; + case TEXENV_FONT: + this->setFontAttribute(); + break; + } + + this->context.mode = mode; + } + struct Context : public ContextBase { C3D_Mtx modelView; C3D_Mtx projection; + TexEnvMode mode = TEXENV_MAX_ENUM; + C3D_BufInfo buffer; Framebuffer target; } context; diff --git a/platform/ctr/include/modules/graphics/Graphics.hpp b/platform/ctr/include/modules/graphics/Graphics.hpp index c555e96e..1b285d59 100644 --- a/platform/ctr/include/modules/graphics/Graphics.hpp +++ b/platform/ctr/include/modules/graphics/Graphics.hpp @@ -31,11 +31,7 @@ namespace love void draw(Drawable* drawable, const Matrix4& matrix); - void draw(TextureBase* texture, Quad* quad, const Matrix4& matrix); - - virtual void draw(const DrawCommand& command) override; - - virtual void draw(const DrawIndexedCommand& command) override; + void draw(Texture* texture, Quad* quad, const Matrix4& matrix); bool isActive() const; diff --git a/platform/ctr/include/modules/graphics/Shader.hpp b/platform/ctr/include/modules/graphics/Shader.hpp index 0d1a595a..02c741f7 100644 --- a/platform/ctr/include/modules/graphics/Shader.hpp +++ b/platform/ctr/include/modules/graphics/Shader.hpp @@ -23,6 +23,8 @@ namespace love ptrdiff_t getHandle() const override; + void updateUniforms(const C3D_Mtx& mdlView, const C3D_Mtx& proj); + private: struct TextureUnit { diff --git a/platform/ctr/include/modules/graphics/StreamBuffer.hpp b/platform/ctr/include/modules/graphics/StreamBuffer.hpp deleted file mode 100644 index 6cc41578..00000000 --- a/platform/ctr/include/modules/graphics/StreamBuffer.hpp +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -#include "modules/graphics/StreamBuffer.tcc" - -namespace love -{ - StreamBufferBase* createStreamBuffer(BufferUsage usage, size_t size); -} diff --git a/platform/ctr/romfs/shaders/main_v_pica.shbin b/platform/ctr/romfs/shaders/main_v_pica.shbin index 0a1c138687ab0c2e711764ce51e875bc9e696780..2fbf8cde7d56ad5b223899bb4872e83e5702e7f0 100644 GIT binary patch delta 199 zcmaFD^nt0~CCtZ(k%56>4G;s_0YHjD1Bm&6I01+`fOr8AqX9q05A1$SK+No+#KjV! z#KoGS#Kl&j#MR8f#L*(b#L=q3#L;HK#KA70;KQ!q-~-g(k;4ozWf2#I2HHEZ*piWN z;#z-2W(GzEexNWD5Q1nJ;07`nflzF+ETf7*NNR<1etuC3LuQ_HeolVT0T2V(0YHjD1BiKnI0lH>fp`iKqX9q05A1$S9!gxy5lUPv z8A@EN6-r!9984U|5=target, screen, side, Framebuffer::DISPLAY_TRANSFER_FLAGS); + C3D_RenderTargetSetOutput(this->target, screen, side, DISPLAY_TRANSFER_FLAGS); this->width = info.width; this->height = info.height; + + // clang-format off + Mtx_OrthoTilt(&this->projection, 0.0f, (float)this->width, (float)this->height, 0.0f, Z_NEAR, Z_FAR, true); + // clang-format on + + this->viewport = { 0, 0, this->width, this->height }; + this->scissor = { 0, 0, this->width, this->height }; + + this->setScissor(Rect::EMPTY); } void Framebuffer::destroy() @@ -46,19 +55,6 @@ namespace love return { (int)left, (int)top, (int)right, (int)bottom }; } - void Framebuffer::setViewport(const Rect& viewport) - { - auto result = calculateBounds(viewport, this->width, this->height); - - if (result == Rect::EMPTY) - result = { 0, 0, this->width, this->height }; - - Mtx_OrthoTilt(&this->projection, (float)result.x, (float)this->width, (float)this->height, - (float)result.h, Framebuffer::Z_NEAR, Framebuffer::Z_FAR, true); - - this->viewport = result; - } - void Framebuffer::setScissor(const Rect& scissor) { const bool enabled = scissor != Rect::EMPTY; @@ -67,10 +63,9 @@ namespace love auto result = calculateBounds(scissor, this->width, this->height); if (result == Rect::EMPTY) - result = { 0, 0, this->width, this->height }; - - C3D_SetScissor(mode, result.x, result.y, result.w, result.h); + result = calculateBounds(this->scissor, this->width, this->height); + C3D_SetScissor(mode, result.y, result.x, result.h, result.w); this->scissor = result; } } // namespace love diff --git a/platform/ctr/source/driver/display/Renderer.cpp b/platform/ctr/source/driver/display/Renderer.cpp index f336be40..67ee46f5 100644 --- a/platform/ctr/source/driver/display/Renderer.cpp +++ b/platform/ctr/source/driver/display/Renderer.cpp @@ -26,15 +26,31 @@ namespace love C3D_AttrInfo* attributes = C3D_GetAttrInfo(); AttrInfo_Init(attributes); - AttrInfo_AddLoader(attributes, 0, GPU_FLOAT, 3); - AttrInfo_AddLoader(attributes, 1, GPU_FLOAT, 4); - AttrInfo_AddLoader(attributes, 2, GPU_FLOAT, 2); + AttrInfo_AddLoader(attributes, 0, GPU_FLOAT, 2); //< position + AttrInfo_AddLoader(attributes, 1, GPU_FLOAT, 2); //< texcoord + AttrInfo_AddLoader(attributes, 2, GPU_FLOAT, 4); //< color Mtx_Identity(&this->context.modelView); Mtx_Identity(&this->context.projection); this->set3DMode(true); + this->batchedDrawState.vertices = (Vertex*)linearAlloc(sizeof(Vertex) * LOVE_UINT16_MAX); + this->batchedDrawState.verticesSize = LOVE_UINT16_MAX; + + if (!this->batchedDrawState.vertices) + throw love::Exception("Failed to allocate vertex buffer."); + + BufInfo_Init(&this->context.buffer); + + if (BufInfo_Add(&this->context.buffer, this->batchedDrawState.vertices, sizeof(Vertex), 3, 0x210) < 0) + { + linearFree(this->batchedDrawState.vertices); + throw love::Exception("Failed to initialize vertex buffer."); + } + + C3D_SetBufInfo(&this->context.buffer); + this->initialized = true; } @@ -42,6 +58,8 @@ namespace love { this->destroyFramebuffers(); + linearFree(this->batchedDrawState.vertices); + C3D_Fini(); gfxExit(); } @@ -88,13 +106,29 @@ namespace love void Renderer::bindFramebuffer() { this->ensureInFrame(); + this->flushBatchedDraws(); this->context.target = this->targets[currentScreen]; auto viewport = this->context.target.getViewport(); - C3D_FrameDrawOn(this->context.target.get()); this->setViewport(viewport, this->context.target.get()->linked); + C3D_FrameDrawOn(this->context.target.get()); + } + + void Renderer::present() + { + if (this->inFrame) + { + this->flushBatchedDraws(); + + C3D_FrameEnd(0); + this->inFrame = false; + } + } + + void Renderer::updateUniforms() + { if (this->context.dirtyProjection) { if (Shader::current != nullptr) @@ -109,23 +143,8 @@ namespace love } } - void Renderer::present() - { - if (this->inFrame) - { - C3D_FrameEnd(0); - this->inFrame = false; - } - } - - // void Renderer::render(BatchedDrawCommand& command) - // {} - void Renderer::setViewport(const Rect& viewport, bool tilt) { - if (this->context.viewport == viewport) - return; - this->context.viewport = viewport; if (viewport.h == GSP_SCREEN_WIDTH && tilt) @@ -146,9 +165,10 @@ namespace love } } + // clang-format off auto* ortho = tilt ? Mtx_OrthoTilt : Mtx_Ortho; - ortho(&this->context.projection, 0.0f, viewport.w, viewport.h, 0.0f, Framebuffer::Z_NEAR, - Framebuffer::Z_FAR, true); + ortho(&this->context.projection, 0.0f, viewport.w, viewport.h, 0.0f, Framebuffer::Z_NEAR, Framebuffer::Z_FAR, true); + // clang-format on this->context.dirtyProjection = true; C3D_SetViewport(0, 0, (uint32_t)viewport.w, (uint32_t)viewport.h); @@ -233,6 +253,28 @@ namespace love C3D_TexSetWrap(texture, wrapU, wrapV); } + void Renderer::draw(const DrawIndexedCommand& command) + { + if (command.texture != nullptr) + { + this->setAttribute(TEXENV_TEXTURE); + C3D_TexBind(0, (C3D_Tex*)command.texture->getHandle()); + } + else + this->setAttribute(TEXENV_PRIMITIVE); + + GPU_Primitive_t primitiveType; + if (!Renderer::getConstant(command.primitiveType, primitiveType)) + throw love::Exception("Invalid primitive type: {:d}.", (int)command.primitiveType); + + decltype(C3D_UNSIGNED_BYTE) indexType; + if (!Renderer::getConstant(command.indexType, indexType)) + throw love::Exception("Invalid index type: {:d}.", (int)command.indexType); + + const void* elements = (const void*)command.indexBufferOffset; + C3D_DrawElements(primitiveType, command.indexCount, indexType, elements); + } + GPU_TEXTURE_WRAP_PARAM Renderer::getWrapMode(SamplerState::WrapMode mode) { switch (mode) diff --git a/platform/ctr/source/modules/graphics/Graphics.cpp b/platform/ctr/source/modules/graphics/Graphics.cpp index 696d3dda..e8ba4d95 100644 --- a/platform/ctr/source/modules/graphics/Graphics.cpp +++ b/platform/ctr/source/modules/graphics/Graphics.cpp @@ -53,8 +53,6 @@ namespace love this->clear(colors.size() > 0 ? colors[0] : OptionalColor(), stencil, depth); return; } - - this->flushBatchedDraws(); } void Graphics::present() @@ -62,7 +60,6 @@ namespace love if (!this->isActive()) return; - this->flushBatchedDraws(); Renderer::getInstance().present(); this->drawCalls = 0; @@ -109,15 +106,6 @@ namespace love if (!Volatile::loadAll()) std::printf("Failed to load all volatile objects.\n"); - // clang-format off - if (this->batchedDrawState.vb[0] == nullptr) - { - this->batchedDrawState.vb[0] = createStreamBuffer(BUFFERUSAGE_VERTEX, 1024 * 1024 * 1); - this->batchedDrawState.vb[1] = createStreamBuffer(BUFFERUSAGE_VERTEX, 256 * 1024 * 1); - this->batchedDrawState.indexBuffer = createStreamBuffer(BUFFERUSAGE_INDEX, sizeof(uint16_t) * LOVE_UINT16_MAX); - } - // clang-format on - this->restoreState(this->states.back()); for (int index = 0; index < 1; index++) @@ -164,7 +152,7 @@ namespace love drawable->draw(*this, matrix); } - void Graphics::draw(TextureBase* texture, Quad* quad, const Matrix4& matrix) + void Graphics::draw(Texture* texture, Quad* quad, const Matrix4& matrix) { texture->draw(*this, quad, matrix); } @@ -180,70 +168,6 @@ namespace love return readable && supported; } - void Graphics::draw(const DrawCommand& command) - { - // C3D_SetBufInfo((C3D_BufInfo*)command->getHandle()); - - // auto* env = C3D_GetTexEnv(0); - // C3D_TexEnvInit(env); - - // if (command.texture == nullptr) - // { - // C3D_TexEnvSrc(env, C3D_Both, GPU_PRIMARY_COLOR, GPU_PRIMARY_COLOR, GPU_PRIMARY_COLOR); - // C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE); - // } - // else - // { - // C3D_TexBind(0, (C3D_Tex*)command.texture->getHandle()); - // C3D_TexEnvSrc(env, C3D_Both, GPU_TEXTURE0, GPU_PRIMARY_COLOR, GPU_PRIMARY_COLOR); - // C3D_TexEnvFunc(env, C3D_Both, GPU_MODULATE); - // } - - // GPU_Primitive_t primitive; - // if (!Renderer::getConstant(command.primitiveType, primitive)) - // throw love::Exception("Invalid primitive type: {:d}", (int)command.primitiveType); - - // C3D_DrawArrays(primitive, command.vertexStart, command.vertexCount); - - // ++this->drawCalls; - } - - void Graphics::draw(const DrawIndexedCommand& command) - { - std::printf("Binding C3D_BufInfo\n"); - C3D_SetBufInfo((C3D_BufInfo*)command.indexBuffer->getHandle()); - std::printf("Resetting TexEnv\n"); - auto* env = C3D_GetTexEnv(0); - C3D_TexEnvInit(env); - - if (command.texture == nullptr) - { - std::printf("Setting up texture environment for no texture\n"); - C3D_TexEnvSrc(env, C3D_Both, GPU_PRIMARY_COLOR, GPU_PRIMARY_COLOR, GPU_PRIMARY_COLOR); - C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE); - } - else - { - std::printf("Setting up texture environment for texture\n"); - C3D_TexBind(0, (C3D_Tex*)command.texture->getHandle()); - C3D_TexEnvSrc(env, C3D_Both, GPU_TEXTURE0, GPU_PRIMARY_COLOR, GPU_PRIMARY_COLOR); - C3D_TexEnvFunc(env, C3D_Both, GPU_MODULATE); - } - - GPU_Primitive_t primitive; - if (!Renderer::getConstant(command.primitiveType, primitive)) - throw love::Exception("Invalid primitive type: {:d}", (int)command.primitiveType); - - decltype(C3D_UNSIGNED_BYTE) type; - if (!Renderer::getConstant(command.indexType, type)) - throw love::Exception("Invalid index type: {:d}", (int)command.indexType); - std::printf("Drawing elements\n"); - const void* indices = BUFFER_OFFSET(command.indexBufferOffset); - C3D_DrawElements(primitive, command.indexCount, type, indices); - - ++this->drawCalls; - } - bool Graphics::is3D() const { return gfxIs3D(); diff --git a/platform/ctr/source/modules/graphics/Shader.cpp b/platform/ctr/source/modules/graphics/Shader.cpp index 9ff7ab1f..0dadd81d 100644 --- a/platform/ctr/source/modules/graphics/Shader.cpp +++ b/platform/ctr/source/modules/graphics/Shader.cpp @@ -51,6 +51,12 @@ namespace love current = this; } + void Shader::updateUniforms(const C3D_Mtx& mdlView, const C3D_Mtx& proj) + { + C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, this->locations.mdlvMtx, &mdlView); + C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, this->locations.projMtx, &proj); + } + ptrdiff_t Shader::getHandle() const { return 0; diff --git a/platform/ctr/source/modules/graphics/StreamBuffer.cpp b/platform/ctr/source/modules/graphics/StreamBuffer.cpp index cac3b13c..45bb2b67 100644 --- a/platform/ctr/source/modules/graphics/StreamBuffer.cpp +++ b/platform/ctr/source/modules/graphics/StreamBuffer.cpp @@ -9,29 +9,16 @@ namespace love class StreamBufferClientMemory final : public StreamBufferBase { public: - StreamBufferClientMemory(BufferUsage mode, size_t size) : - StreamBufferBase(mode, size), - data(nullptr), - vbo(new C3D_BufInfo()) + StreamBufferClientMemory(BufferUsage mode, size_t size) : StreamBufferBase(mode, size), data(nullptr) { this->data = (uint8_t*)linearAlloc(size); if (this->data == nullptr) throw love::Exception("Failed to allocate memory for StreamBufferClientMemory"); - - BufInfo_Init(this->vbo); - - int result = BufInfo_Add(this->vbo, this->data, sizeof(Vertex), 3, 0x210); - - if (result != 0) - throw love::Exception("Failed to create StreamBufferClientMemory"); } virtual ~StreamBufferClientMemory() { - delete this->vbo; - this->vbo = nullptr; - linearFree(this->data); } @@ -55,12 +42,11 @@ namespace love ptrdiff_t getHandle() const override { - return (ptrdiff_t)this->vbo; + return 0; } private: uint8_t* data; - C3D_BufInfo* vbo; }; StreamBufferBase* createStreamBuffer(BufferUsage usage, size_t size) diff --git a/platform/ctr/source/modules/graphics/Texture.cpp b/platform/ctr/source/modules/graphics/Texture.cpp index 77038339..b374dbed 100644 --- a/platform/ctr/source/modules/graphics/Texture.cpp +++ b/platform/ctr/source/modules/graphics/Texture.cpp @@ -2,6 +2,7 @@ #include "modules/graphics/Graphics.hpp" #include "driver/display/Renderer.hpp" +#include "driver/graphics/DrawCommand.hpp" namespace love { @@ -173,38 +174,34 @@ namespace love const auto& viewport = quad->getViewport(); - Vector2 physicalDim = { (float)this->texture->width, (float)this->texture->height }; - Vector2 virtualDim = { (float)this->pixelWidth, (float)this->pixelHeight }; + Vector2 physicalDim { (float)this->texture->width, (float)this->texture->height }; + Vector2 virtualDim { (float)this->pixelWidth, (float)this->pixelHeight }; refreshQuad(quad, viewport, virtualDim, physicalDim, this->renderTarget); const auto& transform = graphics.getTransform(); bool is2D = transform.isAffine2DTransform(); - Graphics::BatchedDrawCommand command {}; - command.formats[0] = getSinglePositionFormat(is2D); - command.formats[1] = CommonFormat::STf_RGBAf; + DrawCommand command {}; + command.format = CommonFormat::XYf_STf_RGBAf; command.indexMode = TRIANGLEINDEX_QUADS; command.vertexCount = 4; command.texture = this; - Graphics::BatchedVertexData data = graphics.requestBatchedDraw(command); + auto data = Renderer::getInstance().requestBatchDraw(command); Matrix4 translated(transform, matrix); if (is2D) - translated.transformXY((Vector2*)data.stream[0], quad->getVertexPositions(), 4); - else - translated.transformXY0((Vector3*)data.stream[0], quad->getVertexPositions(), 4); + translated.transformXY(data.stream, quad->getVertexPositions(), 4); const auto* texCoords = quad->getTextureCoordinates(); - STf_RGBAf* vertices = (STf_RGBAf*)data.stream[1]; for (int index = 0; index < 4; index++) { - vertices[index].s = texCoords[index].x; - vertices[index].t = texCoords[index].y; - vertices[index].color = graphics.getColor(); + data.stream[index].s = texCoords[index].x; + data.stream[index].t = texCoords[index].y; + data.stream[index].color = graphics.getColor(); } } diff --git a/source/modules/graphics/Graphics.cpp b/source/modules/graphics/Graphics.cpp index ade2e6a6..befdb4f8 100644 --- a/source/modules/graphics/Graphics.cpp +++ b/source/modules/graphics/Graphics.cpp @@ -10,8 +10,7 @@ namespace love pixelHeight(0), created(false), active(true), - deviceProjectionMatrix(), - batchedDrawState() + deviceProjectionMatrix() { this->transformStack.reserve(16); this->transformStack.push_back(Matrix4()); @@ -24,18 +23,8 @@ namespace love } GraphicsBase::~GraphicsBase() - { this->states.clear(); - - if (this->batchedDrawState.vb[0]) - this->batchedDrawState.vb[0]->release(); - - if (this->batchedDrawState.vb[1]) - this->batchedDrawState.vb[1]->release(); - - if (this->batchedDrawState.indexBuffer) - this->batchedDrawState.indexBuffer->release(); } void GraphicsBase::restoreState(const DisplayState& state) @@ -130,226 +119,6 @@ namespace love this->resetProjection(); } - GraphicsBase::BatchedVertexData GraphicsBase::requestBatchedDraw( - const GraphicsBase::BatchedDrawCommand& command) - { - BatchedDrawState& state = this->batchedDrawState; - - bool shouldFlush = false; - bool shouldResize = false; - - if (command.primitiveMode != state.primitiveMode || command.formats[0] != state.formats[0] || - command.formats[1] != state.formats[1] || - ((command.indexMode != TRIANGLEINDEX_NONE) != (state.indexCount > 0)) || - command.texture != state.texture || command.shaderType != state.shaderType) - { - shouldFlush = true; - } - - int totalVertices = state.vertexCount + command.vertexCount; - - if (totalVertices > LOVE_UINT16_MAX && command.indexMode != TRIANGLEINDEX_NONE) - shouldFlush = true; - - int requestedIndexCount = getIndexCount(command.indexMode, command.vertexCount); - size_t requestedSize = requestedIndexCount * sizeof(uint16_t); - - size_t newDataSizes[2] = { 0, 0 }; //< position, vertices - size_t bufferSizes[3] = { 0, 0, 0 }; - - for (int index = 0; index < 2; index++) - { - if (command.formats[index] == CommonFormat::NONE) - continue; - - size_t stride = getFormatStride(command.formats[index]); - size_t dataSize = stride * totalVertices; - - if (state.vbMap[index].data != nullptr && dataSize > state.vbMap[index].size) - shouldFlush = true; - - if (dataSize > state.vb[index]->getUsableSize()) - { - bufferSizes[index] = std::max(dataSize, state.vb[index]->getSize() * 2); - shouldResize = true; - } - - newDataSizes[index] = stride * command.vertexCount; - } - - if (command.indexMode != TRIANGLEINDEX_NONE) - { - size_t dataSize = (state.indexCount + requestedIndexCount) * sizeof(uint16_t); - - if (state.indexBufferMap.data != nullptr && dataSize > state.indexBufferMap.size) - shouldFlush = true; - - if (dataSize > state.indexBuffer->getUsableSize()) - { - bufferSizes[2] = std::max(dataSize, state.indexBuffer->getSize() * 2); - shouldResize = true; - } - } - - if (shouldFlush || shouldResize) - { - flushBatchedDraws(); - - state.primitiveMode = command.primitiveMode; - state.formats[0] = command.formats[0]; - state.formats[1] = command.formats[1]; - state.texture = command.texture; - state.shaderType = command.shaderType; - } - - if (state.vertexCount == 0) - { - if (ShaderBase::isDefaultActive()) - ShaderBase::attachDefault(state.shaderType); - } - - if (shouldResize) - { - for (int index = 0; index < 2; index++) - { - if (state.vb[index]->getSize() < bufferSizes[index]) - { - state.vb[index]->release(); - state.vb[index] = createStreamBuffer(BUFFERUSAGE_VERTEX, bufferSizes[index]); - } - } - - if (state.indexBuffer->getSize() < bufferSizes[2]) - { - state.indexBuffer->release(); - state.indexBuffer = createStreamBuffer(BUFFERUSAGE_INDEX, bufferSizes[2]); - } - } - - if (command.indexMode != TRIANGLEINDEX_NONE) - { - if (state.indexBufferMap.data == nullptr) - state.indexBufferMap = state.indexBuffer->map(requestedSize); - - uint16_t* indices = (uint16_t*)state.indexBufferMap.data; - fillIndices(command.indexMode, state.vertexCount, command.vertexCount, indices); - - state.indexBufferMap.data += requestedSize; - } - - BatchedVertexData data {}; - - for (int index = 0; index < 2; index++) - { - if (newDataSizes[index] > 0) - { - if (state.vbMap[index].data == nullptr) - state.vbMap[index] = state.vb[index]->map(newDataSizes[index]); - - data.stream[index] = state.vbMap[index].data; - state.vbMap[index].data += newDataSizes[index]; - } - } - - if (state.vertexCount > 0) - this->drawCallsBatched++; - - state.vertexCount += command.vertexCount; - state.indexCount += requestedIndexCount; - - return data; - } - - void GraphicsBase::flushBatchedDraws() - { - BatchedDrawState& state = this->batchedDrawState; - - if ((state.vertexCount == 0 && state.indexCount == 0) || state.flushing) - return; - - VertexAttributes attributes {}; - BufferBindings buffers {}; - - size_t usedSizes[3] = { 0, 0, 0 }; - - for (int index = 0; index < 2; index++) - { - if (state.formats[index] == CommonFormat::NONE) - continue; - - attributes.setCommonFormat(state.formats[index], (uint8_t)index); - - usedSizes[index] = getFormatStride(state.formats[index]) * state.vertexCount; - - size_t offset = state.vb[index]->unmap(usedSizes[index]); - buffers.set(index, state.vb[index], offset); - - state.vbMap[index] = StreamBufferBase::MapInfo(); - } - - if (attributes.enableBits == 0) - return; - - state.flushing = true; - - Color newColor = this->getColor(); - if (attributes.isEnabled(ATTRIB_COLOR)) - this->setColor(Color::WHITE); - - this->pushIdentityTransform(); - - if (state.indexCount > 0) - { - usedSizes[2] = sizeof(uint16_t) * state.indexCount; - - DrawIndexedCommand command(&attributes, &buffers, state.indexBuffer); - command.primitiveType = state.primitiveMode; - command.indexCount = state.indexCount; - command.indexType = INDEX_UINT16; - command.indexBufferOffset = state.indexBuffer->unmap(usedSizes[2]); - command.texture = nullptr; - - this->draw(command); - state.indexBufferMap = StreamBufferBase::MapInfo(); - } - else - { - DrawCommand command(&attributes, &buffers); - command.primitiveType = state.primitiveMode; - command.vertexCount = 0; - command.vertexStart = state.vertexCount; - command.texture = state.texture; - - this->draw(command); - } - - for (int index = 0; index < 2; index++) - { - if (usedSizes[index] > 0) - state.vb[index]->markUsed(usedSizes[index]); - } - - if (usedSizes[2] > 0) - state.indexBuffer->markUsed(usedSizes[2]); - - this->popTransform(); - - if (attributes.isEnabled(ATTRIB_COLOR)) - this->setColor(newColor); - - state.vertexCount = 0; - state.indexCount = 0; - state.flushing = false; - } - - void GraphicsBase::flushBatchedDrawsGlobal() - { - auto* instance = getInstance(M_GRAPHICS); - - if (instance != nullptr) - instance->flushBatchedDraws(); - } - void GraphicsBase::setScissor(const Rect& scissor) { auto& state = this->states.back(); @@ -374,8 +143,6 @@ namespace love Stats stats {}; stats.drawCalls = this->drawCalls; - if (this->batchedDrawState.vertexCount > 0) - stats.drawCalls++; stats.drawCallsBatched = this->drawCallsBatched; stats.textures = TextureBase::textureCount; diff --git a/source/modules/graphics/Quad.cpp b/source/modules/graphics/Quad.cpp index 3db97465..1cab82db 100644 --- a/source/modules/graphics/Quad.cpp +++ b/source/modules/graphics/Quad.cpp @@ -16,22 +16,22 @@ namespace love Quad::~Quad() {} - void Quad::refresh(const Viewport& viewport, double sourceWidth, double sourceHeight) + void Quad::refresh(const Viewport& v, double sourceWidth, double sourceHeight) { - this->viewport = viewport; + this->viewport = v; this->sourceWidth = sourceWidth; this->sourceHeight = sourceHeight; this->vertexPositions[0] = Vector2(0.0f, 0.0f); - this->vertexPositions[1] = Vector2(0.0f, viewport.h); - this->vertexPositions[2] = Vector2(viewport.w, viewport.h); - this->vertexPositions[3] = Vector2(viewport.w, 0.0f); + this->vertexPositions[1] = Vector2(0.0f, (float)v.h); + this->vertexPositions[2] = Vector2((float)v.w, 0.0f); + this->vertexPositions[3] = Vector2((float)v.w, (float)v.h); // clang-format off - this->textureCoordinates[0] = Vector2(viewport.x / sourceWidth, viewport.y / sourceHeight); - this->textureCoordinates[1] = Vector2(viewport.x / sourceWidth, (viewport.y + viewport.h) / sourceHeight); - this->textureCoordinates[2] = Vector2((viewport.x + viewport.w) / sourceWidth, (viewport.y + viewport.h) / sourceHeight); - this->textureCoordinates[3] = Vector2((viewport.x + viewport.w) / sourceWidth, viewport.y / sourceHeight); + this->textureCoordinates[0] = Vector2(float(v.x / sourceWidth), float(v.y / sourceHeight)); + this->textureCoordinates[1] = Vector2(float(v.x / sourceWidth), float((v.y + v.h) / sourceHeight)); + this->textureCoordinates[2] = Vector2(float((v.x + v.w) / sourceWidth), float(v.y / sourceHeight)); + this->textureCoordinates[3] = Vector2(float((v.x + v.w) / sourceWidth), float((v.y + v.h) / sourceHeight)); // clang-format on } } // namespace love diff --git a/source/modules/graphics/StreamBuffer.cpp b/source/modules/graphics/StreamBuffer.cpp deleted file mode 100644 index 687fdaa0..00000000 --- a/source/modules/graphics/StreamBuffer.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Copyright (c) 2006-2024 LOVE Development Team - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - **/ - -#include "common/Exception.hpp" - -#include "modules/graphics/StreamBuffer.tcc" - -namespace love -{ - StreamBufferBase::StreamBufferBase(BufferUsage mode, size_t size) : - bufferSize(size), - frameGPUReadOffset(0), - mode(mode) - {} -} // namespace love diff --git a/source/modules/graphics/vertex.cpp b/source/modules/graphics/vertex.cpp index ec2ed52d..5bee17ac 100644 --- a/source/modules/graphics/vertex.cpp +++ b/source/modules/graphics/vertex.cpp @@ -64,8 +64,9 @@ namespace love indices[i++] = vertexStart + index + 1 + (index & 1); indices[i++] = vertexStart + index + 2 - (index & 1); } + + break; } - break; case TRIANGLEINDEX_FAN: { int i = 0; @@ -75,13 +76,14 @@ namespace love indices[i++] = vertexStart + index - 1; indices[i++] = vertexStart + index; } + + break; } - break; case TRIANGLEINDEX_QUADS: { - // 0---3 - // | \ | - // 1---2 + // 0---2 + // | / | + // 1---3 int count = vertexCount / 4; for (int i = 0; i < count; i++) { @@ -93,11 +95,12 @@ namespace love indices[ii + 2] = vi + 2; indices[ii + 3] = vi + 2; - indices[ii + 4] = vi + 3; - indices[ii + 5] = vi + 0; + indices[ii + 4] = vi + 1; + indices[ii + 5] = vi + 3; } + + break; } - break; } }