From f96b609123cda765397c6015bd0450603e6d37a1 Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Sun, 25 Aug 2024 03:49:07 +0300 Subject: [PATCH] GL: Actually upload data to stream buffers --- include/PICA/draw_acceleration.hpp | 4 ++- include/renderer_gl/gl_state.hpp | 9 ------- src/core/PICA/draw_acceleration.cpp | 19 ++++++++------ src/core/renderer_gl/gl_state.cpp | 3 --- src/core/renderer_gl/renderer_gl.cpp | 38 +++++++++++++++++++++++----- 5 files changed, 46 insertions(+), 27 deletions(-) diff --git a/include/PICA/draw_acceleration.hpp b/include/PICA/draw_acceleration.hpp index bd3e428d..2ec3f318 100644 --- a/include/PICA/draw_acceleration.hpp +++ b/include/PICA/draw_acceleration.hpp @@ -11,15 +11,16 @@ namespace PICA { struct AttributeInfo { u8* data; u32 offset; + u32 size; u8 type; u8 componentCount; bool fixed; + bool isPadding; std::array fixedValue; // For fixed attributes }; - u8* vertexBuffer; u8* indexBuffer; // Minimum and maximum index in the index buffer for a draw call @@ -31,5 +32,6 @@ namespace PICA { bool canBeAccelerated; bool indexed; + bool useShortIndices; }; } // namespace PICA \ No newline at end of file diff --git a/include/renderer_gl/gl_state.hpp b/include/renderer_gl/gl_state.hpp index e5591ea0..4085cabc 100644 --- a/include/renderer_gl/gl_state.hpp +++ b/include/renderer_gl/gl_state.hpp @@ -38,7 +38,6 @@ struct GLStateManager { GLuint stencilMask; GLuint boundVAO; - GLuint boundVBO; GLuint currentProgram; GLuint boundUBO; @@ -173,13 +172,6 @@ struct GLStateManager { } } - void bindVBO(GLuint handle) { - if (boundVBO != handle) { - boundVBO = handle; - glBindBuffer(GL_ARRAY_BUFFER, handle); - } - } - void useProgram(GLuint handle) { if (currentProgram != handle) { currentProgram = handle; @@ -195,7 +187,6 @@ struct GLStateManager { } void bindVAO(const OpenGL::VertexArray& vao) { bindVAO(vao.handle()); } - void bindVBO(const OpenGL::VertexBuffer& vbo) { bindVBO(vbo.handle()); } void useProgram(const OpenGL::Program& program) { useProgram(program.handle()); } void setColourMask(bool r, bool g, bool b, bool a) { diff --git a/src/core/PICA/draw_acceleration.cpp b/src/core/PICA/draw_acceleration.cpp index 5fc21e48..22b1f041 100644 --- a/src/core/PICA/draw_acceleration.cpp +++ b/src/core/PICA/draw_acceleration.cpp @@ -12,7 +12,6 @@ void GPU::getAcceleratedDrawInfo(PICA::DrawAcceleration& accel, bool indexed) { const u32 vertexBase = ((regs[PICA::InternalRegs::VertexAttribLoc] >> 1) & 0xfffffff) * 16; const u32 vertexCount = regs[PICA::InternalRegs::VertexCountReg]; // Total # of vertices to transfer - accel.vertexBuffer = getPointerPhys(vertexBase); if (indexed) { u32 indexBufferConfig = regs[PICA::InternalRegs::IndexBufferConfig]; u32 indexBufferPointer = vertexBase + (indexBufferConfig & 0xfffffff); @@ -22,11 +21,12 @@ void GPU::getAcceleratedDrawInfo(PICA::DrawAcceleration& accel, bool indexed) { u16 maximumIndex = 0; // Check whether the index buffer uses u16 indices or u8 - bool shortIndex = Helpers::getBit<31>(indexBufferConfig); // Indicates whether vert indices are 16-bit or 8-bit + accel.useShortIndices = Helpers::getBit<31>(indexBufferConfig); // Indicates whether vert indices are 16-bit or 8-bit // Calculate the minimum and maximum indices used in the index buffer, so we'll only upload them - if (shortIndex) { + if (accel.useShortIndices) { u16* indexBuffer16 = reinterpret_cast(indexBuffer); + for (int i = 0; i < vertexCount; i++) { u16 index = indexBuffer16[i]; minimumIndex = std::min(minimumIndex, index); @@ -84,6 +84,7 @@ void GPU::getAcceleratedDrawInfo(PICA::DrawAcceleration& accel, bool indexed) { attributeOffset += (index - 11) << 2; attr.data = nullptr; + attr.isPadding = true; continue; } @@ -91,18 +92,19 @@ void GPU::getAcceleratedDrawInfo(PICA::DrawAcceleration& accel, bool indexed) { const u32 attribType = attribInfo & 0x3; // Type of attribute (sbyte/ubyte/short/float) const u32 size = (attribInfo >> 2) + 1; // Total number of components + // Size of each component based on the attribute type + static constexpr u32 sizePerComponent[4] = {1, 1, 2, 4}; + attr.componentCount = size; attr.offset = attributeOffset; + attr.size = size * sizePerComponent[attribType]; attr.type = attribType; + attr.isPadding = false; + attributeOffset += attr.size; // Get a pointer to the data where this attribute is stored const u32 attrAddress = vertexBase + attr.offset + (accel.minimumIndex * attrData.size); attr.data = getPointerPhys(attrAddress); - - // Size of each component based on the attribute type - static constexpr u32 sizePerComponent[4] = {1, 1, 2, 4}; - attributeOffset += size * sizePerComponent[attribType]; - attrCount += 1; } @@ -114,6 +116,7 @@ void GPU::getAcceleratedDrawInfo(PICA::DrawAcceleration& accel, bool indexed) { attr.fixed = true; // Set the data pointer to nullptr in order to catch any potential bugs attr.data = nullptr; + attr.isPadding = false; for (int i = 0; i < 4; i++) { attr.fixedValue[i] = fixedAttr[i].toFloat32(); diff --git a/src/core/renderer_gl/gl_state.cpp b/src/core/renderer_gl/gl_state.cpp index 3d1c0681..785cac41 100644 --- a/src/core/renderer_gl/gl_state.cpp +++ b/src/core/renderer_gl/gl_state.cpp @@ -73,10 +73,7 @@ void GLStateManager::resetVAO() { } void GLStateManager::resetBuffers() { - boundVBO = 0; boundUBO = 0; - - glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_UNIFORM_BUFFER, 0); } diff --git a/src/core/renderer_gl/renderer_gl.cpp b/src/core/renderer_gl/renderer_gl.cpp index 3b2d1d70..4ed1eac1 100644 --- a/src/core/renderer_gl/renderer_gl.cpp +++ b/src/core/renderer_gl/renderer_gl.cpp @@ -97,7 +97,7 @@ void RendererGL::initGraphicsContextInternal() { glBufferData(GL_UNIFORM_BUFFER, PICAShader::totalUniformSize(), nullptr, GL_DYNAMIC_DRAW); vbo.createFixedSize(sizeof(Vertex) * vertexBufferSize * 2, GL_STREAM_DRAW); - gl.bindVBO(vbo); + vbo.bind(); // Initialize the VAO used when not using hw shaders defaultVAO.create(); gl.bindVAO(defaultVAO); @@ -439,7 +439,7 @@ void RendererGL::drawVertices(PICA::PrimType primType, std::span v const auto primitiveTopology = primTypes[static_cast(primType)]; gl.disableScissor(); - gl.bindVBO(vbo); + vbo.bind(); gl.bindVAO(usingAcceleratedShader ? hwShaderVAO : defaultVAO); gl.enableClipPlane(0); // Clipping plane 0 is always enabled @@ -1135,11 +1135,37 @@ void RendererGL::accelerateVertexUpload(ShaderUnit& shaderUnit, PICA::DrawAccele GL_FLOAT, // 3: Float }; + const u32 vertexCount = accel->maximumIndex - accel->minimumIndex + 1; + + // Update index buffer if necessary + if (accel->indexed) { + const bool shortIndex = accel->useShortIndices; + const usize indexBufferSize = usize(vertexCount) * (shortIndex ? sizeof(u16) : sizeof(u8)); + + auto indexBufferRes = hwIndexBuffer->Map(4, indexBufferSize); + std::memcpy(indexBufferRes.pointer, accel->indexBuffer, indexBufferSize); + hwIndexBuffer->Unmap(indexBufferSize); + } + + auto vertexBufferRes = hwVertexBuffer->Map(4, accel->vertexDataSize); + u8* vertexData = static_cast(vertexBufferRes.pointer); + for (int i = 0; i < totalAttribCount; i++) { const auto& attrib = accel->attributeInfo[i]; - printf( - "%s attribute starting from offset %d with a size of %d components\n", attrib.fixed ? "Fixed" : "Variable", (!attrib.fixed) ? attrib.offset : 0, - !attrib.fixed ? attrib.componentCount : 4 - ); + + if (attrib.fixed) { + Helpers::panic("Fixed attribute!"); + } else { + if (attrib.isPadding) { + continue; + } + + const u32 attributeSize = attrib.size * vertexCount; + + std::memcpy(vertexData, attrib.data, attributeSize); + vertexData += attributeSize; + } } + + hwVertexBuffer->Unmap(accel->vertexDataSize); } \ No newline at end of file