mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-09-11 13:48:30 +09:00
[Feat] (MG_Test/Backend/DirectVulkan): test vao vbo ibo?
This commit is contained in:
@@ -10,7 +10,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <chrono>
|
#include <algorithm>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
#include <vulkan/vulkan.h>
|
#include <vulkan/vulkan.h>
|
||||||
@@ -39,6 +39,8 @@
|
|||||||
#include <objc/runtime.h>
|
#include <objc/runtime.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <Includes.h>
|
||||||
|
|
||||||
namespace MobileGL {
|
namespace MobileGL {
|
||||||
void MG_Initialize();
|
void MG_Initialize();
|
||||||
}
|
}
|
||||||
@@ -148,72 +150,55 @@ int main() {
|
|||||||
std::cout << "Offscreen FBO status = 0x" << std::hex << offscreenFboStatus << std::dec << std::endl;
|
std::cout << "Offscreen FBO status = 0x" << std::hex << offscreenFboStatus << std::dec << std::endl;
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
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;
|
GLuint vao = 0;
|
||||||
glGenVertexArrays(1, &vao);
|
glGenVertexArrays(1, &vao);
|
||||||
glBindVertexArray(vao);
|
glBindVertexArray(vao);
|
||||||
|
|
||||||
static constexpr GLfloat kQuadPositions[] = {
|
GLuint shapeVbo = 0;
|
||||||
// triangle 1
|
glGenBuffers(1, &shapeVbo);
|
||||||
-0.6f, -0.6f,
|
glBindBuffer(GL_ARRAY_BUFFER, shapeVbo);
|
||||||
0.6f, -0.6f,
|
glBufferData(
|
||||||
0.6f, 0.6f,
|
GL_ARRAY_BUFFER,
|
||||||
// triangle 2
|
static_cast<GLsizeiptr>((kMaxSegments + 1) * 5 * sizeof(GLfloat)),
|
||||||
0.6f, 0.6f,
|
nullptr,
|
||||||
-0.6f, 0.6f,
|
GL_DYNAMIC_DRAW
|
||||||
-0.6f, -0.6f
|
);
|
||||||
};
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, static_cast<GLsizei>(5 * sizeof(GLfloat)), nullptr);
|
||||||
|
|
||||||
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<GLsizei>(2 * sizeof(GLfloat)), nullptr);
|
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, static_cast<GLsizei>(5 * sizeof(GLfloat)),
|
||||||
GLuint colorVbo = 0;
|
reinterpret_cast<void*>(2 * sizeof(GLfloat)));
|
||||||
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<GLsizei>(3 * sizeof(GLfloat)), nullptr);
|
|
||||||
glEnableVertexAttribArray(1);
|
glEnableVertexAttribArray(1);
|
||||||
|
|
||||||
static constexpr GLushort kQuadIndices[] = {0, 1, 2, 3, 4, 5};
|
GLuint shapeIbo = 0;
|
||||||
GLuint ebo = 0;
|
glGenBuffers(1, &shapeIbo);
|
||||||
glGenBuffers(1, &ebo);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, shapeIbo);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
glBufferData(
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(kQuadIndices), kQuadIndices, GL_STATIC_DRAW);
|
GL_ELEMENT_ARRAY_BUFFER,
|
||||||
|
static_cast<GLsizeiptr>(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 = 0) in vec2 aPos;
|
||||||
layout(location = 1) in vec3 aColor;
|
layout(location = 1) in vec3 aColor;
|
||||||
out vec3 vColor;
|
out vec3 vColor;
|
||||||
uniform float iTime;
|
|
||||||
void main() {
|
void main() {
|
||||||
float wobble = 0.05 * sin(iTime);
|
gl_Position = vec4(aPos, 0.0, 1.0);
|
||||||
gl_Position = vec4(aPos.x, aPos.y + wobble, 0.0, 1.0);
|
|
||||||
vColor = aColor;
|
vColor = aColor;
|
||||||
})";
|
})";
|
||||||
static constexpr const char* kFragmentShaderSource = R"(#version 330 core
|
static constexpr const char* kFragmentShaderSource = R"(#version 330 core
|
||||||
in vec3 vColor;
|
in vec3 vColor;
|
||||||
layout(location = 0) out vec4 outColor;
|
layout(location = 0) out vec4 outColor;
|
||||||
layout(std140, binding = 1) uniform ColorBlock {
|
|
||||||
vec3 uColor;
|
|
||||||
};
|
|
||||||
uniform float iTime;
|
|
||||||
void main() {
|
void main() {
|
||||||
float pulse = 0.5 + 0.5 * sin(iTime * 1.7);
|
outColor = vec4(vColor, 1.0);
|
||||||
outColor = vec4(vColor * uColor * pulse, 1.0);
|
|
||||||
})";
|
})";
|
||||||
|
|
||||||
const GLuint vs = glCreateShader(GL_VERTEX_SHADER);
|
const GLuint vs = glCreateShader(GL_VERTEX_SHADER);
|
||||||
@@ -241,31 +226,17 @@ void main() {
|
|||||||
glDeleteShader(vs);
|
glDeleteShader(vs);
|
||||||
glDeleteShader(fs);
|
glDeleteShader(fs);
|
||||||
|
|
||||||
const GLint iTimeLocation = glGetUniformLocation(program, "iTime");
|
std::vector<GLfloat> dynamicVertices(static_cast<size_t>((kMaxSegments + 1) * 5));
|
||||||
if (iTimeLocation < 0) {
|
std::vector<GLushort> dynamicIndices(static_cast<size_t>(kMaxSegments * 3));
|
||||||
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);
|
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(!glfwWindowShouldClose(window)) {
|
while(!glfwWindowShouldClose(window)) {
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
||||||
|
GLint framebufferWidth = kWindowWidth;
|
||||||
|
GLint framebufferHeight = kWindowHeight;
|
||||||
|
glfwGetFramebufferSize(window, &framebufferWidth, &framebufferHeight);
|
||||||
|
glViewport(0, 0, framebufferWidth, framebufferHeight);
|
||||||
|
|
||||||
const bool useOffscreenPath = ((i / 100) % 2) == 0;
|
const bool useOffscreenPath = ((i / 100) % 2) == 0;
|
||||||
if (useOffscreenPath) {
|
if (useOffscreenPath) {
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, offscreenFbo);
|
glBindFramebuffer(GL_FRAMEBUFFER, offscreenFbo);
|
||||||
@@ -274,17 +245,55 @@ void main() {
|
|||||||
|
|
||||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, offscreenFbo);
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, offscreenFbo);
|
||||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
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);
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
} else {
|
} else {
|
||||||
|
const int segmentCount = std::min(kMaxSegments, 3 + (i / 100));
|
||||||
|
const float radius = 0.75f;
|
||||||
|
const float rotation = static_cast<float>(i) * 0.01f;
|
||||||
|
|
||||||
|
const size_t vertexFloatCount = static_cast<size_t>((segmentCount + 1) * 5);
|
||||||
|
const size_t indexCount = static_cast<size_t>(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<float>(v) / static_cast<float>(segmentCount));
|
||||||
|
const float x = radius * std::cos(theta);
|
||||||
|
const float y = radius * std::sin(theta);
|
||||||
|
const size_t base = static_cast<size_t>((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<size_t>(v * 3);
|
||||||
|
dynamicIndices[ib + 0] = 0;
|
||||||
|
dynamicIndices[ib + 1] = static_cast<GLushort>(v + 1);
|
||||||
|
dynamicIndices[ib + 2] = static_cast<GLushort>((v + 1) % segmentCount + 1);
|
||||||
|
}
|
||||||
|
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
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);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
glUseProgram(program);
|
||||||
|
glBindVertexArray(vao);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, shapeVbo);
|
||||||
|
glBufferSubData(GL_ARRAY_BUFFER, 0, static_cast<GLsizeiptr>(vertexFloatCount * sizeof(GLfloat)), dynamicVertices.data());
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, shapeIbo);
|
||||||
|
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, static_cast<GLsizeiptr>(indexCount * sizeof(GLushort)), dynamicIndices.data());
|
||||||
|
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(indexCount), GL_UNSIGNED_SHORT, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i % 100 == 0) {
|
if (i % 100 == 0) {
|
||||||
std::cout << "frame=" << i
|
std::cout << "frame=" << i
|
||||||
<< " path=" << (useOffscreenPath ? "offscreen-clear+blit" : "default-clear")
|
<< " path=" << (useOffscreenPath ? "offscreen-clear+blit" : "dynamic-vbo-vao-ibo-archimedes")
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
}
|
}
|
||||||
eglSwapBuffers(display, surface);
|
eglSwapBuffers(display, surface);
|
||||||
@@ -292,10 +301,8 @@ void main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
glDeleteProgram(program);
|
glDeleteProgram(program);
|
||||||
glDeleteBuffers(1, &colorUbo);
|
glDeleteBuffers(1, &shapeVbo);
|
||||||
glDeleteBuffers(1, &positionVbo);
|
glDeleteBuffers(1, &shapeIbo);
|
||||||
glDeleteBuffers(1, &colorVbo);
|
|
||||||
glDeleteBuffers(1, &ebo);
|
|
||||||
glDeleteVertexArrays(1, &vao);
|
glDeleteVertexArrays(1, &vao);
|
||||||
glDeleteFramebuffers(1, &offscreenFbo);
|
glDeleteFramebuffers(1, &offscreenFbo);
|
||||||
glDeleteTextures(1, &offscreenTex);
|
glDeleteTextures(1, &offscreenTex);
|
||||||
|
|||||||
Reference in New Issue
Block a user