diff --git a/include/opengl.hpp b/include/opengl.hpp index 8cb1613e..37e9302c 100644 --- a/include/opengl.hpp +++ b/include/opengl.hpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -388,18 +389,18 @@ namespace OpenGL { void bind() { glBindBuffer(GL_ARRAY_BUFFER, m_handle); } void free() { glDeleteBuffers(1, &m_handle); } - // Reallocates the buffer on every call. Prefer the sub version if possible. - template - void bufferVerts(VertType* vertices, int vertCount, GLenum usage = GL_DYNAMIC_DRAW) { - glBufferData(GL_ARRAY_BUFFER, sizeof(VertType) * vertCount, vertices, usage); - } + // Reallocates the buffer on every call. Prefer the sub version if possible. + template + void bufferVerts(std::span vertices, GLenum usage = GL_DYNAMIC_DRAW) { + glBufferData(GL_ARRAY_BUFFER, sizeof(VertType) * vertices.size(), vertices.data(), usage); + } - // Only use if you used createFixedSize - template - void bufferVertsSub(VertType* vertices, int vertCount, GLintptr offset = 0) { - glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(VertType) * vertCount, vertices); - } - }; + // Only use if you used createFixedSize + template + void bufferVertsSub(std::span vertices, GLintptr offset = 0) { + glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(VertType) * vertices.size(), vertices.data()); + } + }; enum DepthFunc { Never = GL_NEVER, // Depth test never passes diff --git a/include/renderer_gl/renderer_gl.hpp b/include/renderer_gl/renderer_gl.hpp index ac11a813..8182e426 100644 --- a/include/renderer_gl/renderer_gl.hpp +++ b/include/renderer_gl/renderer_gl.hpp @@ -1,5 +1,7 @@ #pragma once #include +#include + #include "helpers.hpp" #include "logger.hpp" #include "opengl.hpp" @@ -68,12 +70,14 @@ public: Renderer(GPU& gpu, const std::array& internalRegs) : gpu(gpu), regs(internalRegs) {} void reset(); - void display(); // Display the 3DS screen contents to the window - void initGraphicsContext(); // Initialize graphics context - void getGraphicsContext(); // Set up graphics context for rendering - void clearBuffer(u32 startAddress, u32 endAddress, u32 value, u32 control); // Clear a GPU buffer in VRAM - void displayTransfer(u32 inputAddr, u32 outputAddr, u32 inputSize, u32 outputSize, u32 flags); // Perform display transfer - void drawVertices(OpenGL::Primitives primType, Vertex* vertices, u32 count); // Draw the given vertices + void display(); // Display the 3DS screen contents to the window + void initGraphicsContext(); // Initialize graphics context + void getGraphicsContext(); // Set up graphics context for rendering + void clearBuffer(u32 startAddress, u32 endAddress, u32 value, u32 control); // Clear a GPU buffer in VRAM + void displayTransfer( + u32 inputAddr, u32 outputAddr, u32 inputSize, u32 outputSize, u32 flags + ); // Perform display transfer + void drawVertices(OpenGL::Primitives primType, std::span vertices); // Draw the given vertices void setFBSize(u32 width, u32 height) { fbSize.x() = width; diff --git a/src/core/PICA/gpu.cpp b/src/core/PICA/gpu.cpp index 9cbd17a8..f0e832a2 100644 --- a/src/core/PICA/gpu.cpp +++ b/src/core/PICA/gpu.cpp @@ -1,7 +1,10 @@ #include "PICA/gpu.hpp" + +#include +#include + #include "PICA/float_types.hpp" #include "PICA/regs.hpp" -#include using namespace Floats; @@ -41,7 +44,7 @@ void GPU::drawArrays(bool indexed) { drawArrays(); } -Vertex* vertices = new Vertex[Renderer::vertexBufferSize]; +static std::array vertices; template void GPU::drawArrays() { @@ -205,7 +208,7 @@ void GPU::drawArrays() { OpenGL::Triangle, OpenGL::TriangleStrip, OpenGL::TriangleFan, OpenGL::Triangle }; const auto shape = primTypes[primType]; - renderer.drawVertices(shape, vertices, vertexCount); + renderer.drawVertices(shape, std::span(vertices).first(vertexCount)); } Vertex GPU::getImmediateModeVertex() { diff --git a/src/core/PICA/regs.cpp b/src/core/PICA/regs.cpp index a5e2c6b9..26feaf9d 100644 --- a/src/core/PICA/regs.cpp +++ b/src/core/PICA/regs.cpp @@ -157,7 +157,7 @@ void GPU::writeInternalReg(u32 index, u32 value, u32 mask) { // If we've reached 3 verts, issue a draw call // Handle rendering depending on the primitive type if (immediateModeVertIndex == 3) { - renderer.drawVertices(OpenGL::Triangle, &immediateModeVertices[0], 3); + renderer.drawVertices(OpenGL::Triangle, immediateModeVertices); switch (primType) { // Triangle or geometry primitive. Draw a triangle and discard all vertices diff --git a/src/core/renderer_gl/renderer_gl.cpp b/src/core/renderer_gl/renderer_gl.cpp index 6da0dd70..7fbb9e94 100644 --- a/src/core/renderer_gl/renderer_gl.cpp +++ b/src/core/renderer_gl/renderer_gl.cpp @@ -265,7 +265,7 @@ void Renderer::setupBlending() { } } -void Renderer::drawVertices(OpenGL::Primitives primType, Vertex* vertices, u32 count) { +void Renderer::drawVertices(OpenGL::Primitives primType, std::span vertices) { // Adjust alpha test if necessary const u32 alphaControl = regs[PICAInternalRegs::AlphaTestConfig]; if (alphaControl != oldAlphaControl) { @@ -352,8 +352,8 @@ void Renderer::drawVertices(OpenGL::Primitives primType, Vertex* vertices, u32 c } } - vbo.bufferVertsSub(vertices, count); - OpenGL::draw(primType, count); + vbo.bufferVertsSub(vertices); + OpenGL::draw(primType, vertices.size()); } constexpr u32 topScreenBuffer = 0x1f000000;