From 25a5fe0757706807d7fef6fadddee0dca8ead526 Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Wed, 18 Feb 2026 07:49:29 +0800 Subject: [PATCH] [Feat] (MG_Test/Backend/DirectVulkan): test vao vbo ibo? --- .../MG_Test/Backend/DirectVulkan/TestExec.cpp | 161 +++++++++--------- 1 file changed, 84 insertions(+), 77 deletions(-) diff --git a/MobileGL/MG_Test/Backend/DirectVulkan/TestExec.cpp b/MobileGL/MG_Test/Backend/DirectVulkan/TestExec.cpp index 715ff0ad..7039e901 100644 --- a/MobileGL/MG_Test/Backend/DirectVulkan/TestExec.cpp +++ b/MobileGL/MG_Test/Backend/DirectVulkan/TestExec.cpp @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include @@ -39,6 +39,8 @@ #include #endif +#include + namespace MobileGL { void MG_Initialize(); } @@ -148,72 +150,55 @@ int main() { std::cout << "Offscreen FBO status = 0x" << std::hex << offscreenFboStatus << std::dec << std::endl; glBindFramebuffer(GL_FRAMEBUFFER, 0); + static constexpr GLint kWindowWidth = 800; + static constexpr GLint kWindowHeight = 600; + glViewport(0, 0, kWindowWidth, kWindowHeight); + + static constexpr int kMaxSegments = 192; + static constexpr float kPi = 3.14159265358979323846f; + GLuint vao = 0; glGenVertexArrays(1, &vao); glBindVertexArray(vao); - static constexpr GLfloat kQuadPositions[] = { - // triangle 1 - -0.6f, -0.6f, - 0.6f, -0.6f, - 0.6f, 0.6f, - // triangle 2 - 0.6f, 0.6f, - -0.6f, 0.6f, - -0.6f, -0.6f - }; - - static constexpr GLfloat kQuadColors[] = { - // triangle 1: RGB - 1.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f, - 0.0f, 0.0f, 1.0f, - // triangle 2: RGB - 1.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f, - 0.0f, 0.0f, 1.0f - }; - - GLuint positionVbo = 0; - glGenBuffers(1, &positionVbo); - glBindBuffer(GL_ARRAY_BUFFER, positionVbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(kQuadPositions), kQuadPositions, GL_STATIC_DRAW); - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, static_cast(2 * sizeof(GLfloat)), nullptr); + GLuint shapeVbo = 0; + glGenBuffers(1, &shapeVbo); + glBindBuffer(GL_ARRAY_BUFFER, shapeVbo); + glBufferData( + GL_ARRAY_BUFFER, + static_cast((kMaxSegments + 1) * 5 * sizeof(GLfloat)), + nullptr, + GL_DYNAMIC_DRAW + ); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, static_cast(5 * sizeof(GLfloat)), nullptr); glEnableVertexAttribArray(0); - - GLuint colorVbo = 0; - glGenBuffers(1, &colorVbo); - glBindBuffer(GL_ARRAY_BUFFER, colorVbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(kQuadColors), kQuadColors, GL_STATIC_DRAW); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, static_cast(3 * sizeof(GLfloat)), nullptr); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, static_cast(5 * sizeof(GLfloat)), + reinterpret_cast(2 * sizeof(GLfloat))); glEnableVertexAttribArray(1); - static constexpr GLushort kQuadIndices[] = {0, 1, 2, 3, 4, 5}; - GLuint ebo = 0; - glGenBuffers(1, &ebo); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(kQuadIndices), kQuadIndices, GL_STATIC_DRAW); + GLuint shapeIbo = 0; + glGenBuffers(1, &shapeIbo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, shapeIbo); + glBufferData( + GL_ELEMENT_ARRAY_BUFFER, + static_cast(kMaxSegments * 3 * sizeof(GLushort)), + nullptr, + GL_DYNAMIC_DRAW + ); -static constexpr const char* kVertexShaderSource = R"(#version 330 core + static constexpr const char* kVertexShaderSource = R"(#version 330 core layout(location = 0) in vec2 aPos; layout(location = 1) in vec3 aColor; out vec3 vColor; -uniform float iTime; void main() { - float wobble = 0.05 * sin(iTime); - gl_Position = vec4(aPos.x, aPos.y + wobble, 0.0, 1.0); + gl_Position = vec4(aPos, 0.0, 1.0); vColor = aColor; })"; static constexpr const char* kFragmentShaderSource = R"(#version 330 core in vec3 vColor; layout(location = 0) out vec4 outColor; -layout(std140, binding = 1) uniform ColorBlock { - vec3 uColor; -}; -uniform float iTime; void main() { - float pulse = 0.5 + 0.5 * sin(iTime * 1.7); - outColor = vec4(vColor * uColor * pulse, 1.0); + outColor = vec4(vColor, 1.0); })"; const GLuint vs = glCreateShader(GL_VERTEX_SHADER); @@ -241,31 +226,17 @@ void main() { glDeleteShader(vs); glDeleteShader(fs); - const GLint iTimeLocation = glGetUniformLocation(program, "iTime"); - if (iTimeLocation < 0) { - std::cerr << "Failed to get uniform location for iTime" << std::endl; - return 1; - } - - const GLuint colorBlockIndex = glGetUniformBlockIndex(program, "ColorBlock"); - if (colorBlockIndex == GL_INVALID_INDEX) { - std::cerr << "Failed to get uniform block index for ColorBlock" << std::endl; - return 1; - } - glUniformBlockBinding(program, colorBlockIndex, 1); - - GLuint colorUbo = 0; - glGenBuffers(1, &colorUbo); - glBindBuffer(GL_UNIFORM_BUFFER, colorUbo); - alignas(16) GLfloat uboColorData[4] = {1.0f, 0.6f, 0.2f, 0.0f}; - glBufferData(GL_UNIFORM_BUFFER, sizeof(uboColorData), uboColorData, GL_DYNAMIC_DRAW); - glBindBufferBase(GL_UNIFORM_BUFFER, 1, colorUbo); - glBindBuffer(GL_UNIFORM_BUFFER, 0); - + std::vector dynamicVertices(static_cast((kMaxSegments + 1) * 5)); + std::vector dynamicIndices(static_cast(kMaxSegments * 3)); int i = 0; while(!glfwWindowShouldClose(window)) { glfwPollEvents(); + GLint framebufferWidth = kWindowWidth; + GLint framebufferHeight = kWindowHeight; + glfwGetFramebufferSize(window, &framebufferWidth, &framebufferHeight); + glViewport(0, 0, framebufferWidth, framebufferHeight); + const bool useOffscreenPath = ((i / 100) % 2) == 0; if (useOffscreenPath) { glBindFramebuffer(GL_FRAMEBUFFER, offscreenFbo); @@ -274,17 +245,55 @@ void main() { glBindFramebuffer(GL_READ_FRAMEBUFFER, offscreenFbo); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); - glBlitFramebuffer(0, 0, 256, 256, 0, 0, 800, 600, GL_COLOR_BUFFER_BIT, GL_NEAREST); + glBlitFramebuffer(0, 0, 256, 256, 0, 0, framebufferWidth, framebufferHeight, GL_COLOR_BUFFER_BIT, GL_NEAREST); glBindFramebuffer(GL_FRAMEBUFFER, 0); } else { + const int segmentCount = std::min(kMaxSegments, 3 + (i / 100)); + const float radius = 0.75f; + const float rotation = static_cast(i) * 0.01f; + + const size_t vertexFloatCount = static_cast((segmentCount + 1) * 5); + const size_t indexCount = static_cast(segmentCount * 3); + + dynamicVertices[0] = 0.0f; + dynamicVertices[1] = 0.0f; + dynamicVertices[2] = 0.95f; + dynamicVertices[3] = 0.95f; + dynamicVertices[4] = 0.95f; + + for (int v = 0; v < segmentCount; ++v) { + const float theta = rotation + (2.0f * kPi * static_cast(v) / static_cast(segmentCount)); + const float x = radius * std::cos(theta); + const float y = radius * std::sin(theta); + const size_t base = static_cast((v + 1) * 5); + dynamicVertices[base + 0] = x; + dynamicVertices[base + 1] = y; + dynamicVertices[base + 2] = 0.5f + 0.5f * std::cos(theta); + dynamicVertices[base + 3] = 0.5f + 0.5f * std::sin(theta); + dynamicVertices[base + 4] = 0.9f; + + const size_t ib = static_cast(v * 3); + dynamicIndices[ib + 0] = 0; + dynamicIndices[ib + 1] = static_cast(v + 1); + dynamicIndices[ib + 2] = static_cast((v + 1) % segmentCount + 1); + } + glBindFramebuffer(GL_FRAMEBUFFER, 0); - glClearColor(0.0f, 1.0f, 0.0f, 1.0f); + glClearColor(0.05f, 0.08f, 0.12f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); + + glUseProgram(program); + glBindVertexArray(vao); + glBindBuffer(GL_ARRAY_BUFFER, shapeVbo); + glBufferSubData(GL_ARRAY_BUFFER, 0, static_cast(vertexFloatCount * sizeof(GLfloat)), dynamicVertices.data()); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, shapeIbo); + glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, static_cast(indexCount * sizeof(GLushort)), dynamicIndices.data()); + glDrawElements(GL_TRIANGLES, static_cast(indexCount), GL_UNSIGNED_SHORT, nullptr); } if (i % 100 == 0) { std::cout << "frame=" << i - << " path=" << (useOffscreenPath ? "offscreen-clear+blit" : "default-clear") + << " path=" << (useOffscreenPath ? "offscreen-clear+blit" : "dynamic-vbo-vao-ibo-archimedes") << std::endl; } eglSwapBuffers(display, surface); @@ -292,10 +301,8 @@ void main() { } glDeleteProgram(program); - glDeleteBuffers(1, &colorUbo); - glDeleteBuffers(1, &positionVbo); - glDeleteBuffers(1, &colorVbo); - glDeleteBuffers(1, &ebo); + glDeleteBuffers(1, &shapeVbo); + glDeleteBuffers(1, &shapeIbo); glDeleteVertexArrays(1, &vao); glDeleteFramebuffers(1, &offscreenFbo); glDeleteTextures(1, &offscreenTex);