如何绘制存储在 SSBO 中的顶点?
Posted
技术标签:
【中文标题】如何绘制存储在 SSBO 中的顶点?【英文标题】:How do I draw vertices that are stored in a SSBO? 【发布时间】:2020-01-07 22:29:40 【问题描述】:这是OpenGL and loading/reading data in AoSoA (hybrid SoA) format之后的问题。
我正在尝试使用着色器存储缓冲区对象 (SSBO) 来存储以AoSoA 格式表示的顶点数据。我无法绘制顶点,这显然意味着我在某处做错了什么。问题是我似乎无法弄清楚什么或在哪里。上面最初问题的答案似乎表明我不应该使用顶点属性数组,所以问题就变成了,给定我将要呈现的代码,我该如何渲染这个 SSBO?
顶点数据结构
constexpr auto VECTOR_WIDTH = 4;
constexpr auto VERTEX_COUNT = 16;
struct VertexData
std::array<float, VECTOR_WIDTH> px;
std::array<float, VECTOR_WIDTH> py;
;
// Later stored in a std::vector
std::vector<VertexData> vertices(VERTEX_COUNT / VECTOR_WIDTH);
顶点着色器(这真的应该是计算着色器吗?)
struct Vertex4
float px[4]; // position x
float py[4]; // position y
;
layout(std430, binding=0) buffer VertexData
Vertex4 vertices[];
;
void main()
int dataIx = gl_VertexID / 4;
int vertexIx = gl_VertexID % 4;
vec2 vertexPosition = vec2(vertices[dataIx].px[vertexIx], vertices[dataIx].py[vertexIx]);
分配vertexPosition
索引
// Do I need this? Where do I use it? glEnableVertexAttribArray(position_attrib_index)?
const GLuint position_attrib_index = 0;
glBindAttribLocation(program, position_attrib_index, "vertexPosition");
SSBO 设置
const GLuint ssbo_binding_point = 0;
GLuint ssbo;
glGenBuffers(1, &ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
//glBufferStorage(GL_SHADER_STORAGE_BUFFER, vertices.size() * sizeof(VertexData), vertices.data(), GL_MAP_WRITE_BIT);
glBufferData(GL_SHADER_STORAGE_BUFFER, vertices.size() * sizeof(VertexData), vertices.data(), GL_STATIC_DRAW);
const auto block_index = glGetProgramResourceIndex(program, GL_SHADER_STORAGE_BLOCK, "VertexData");
glShaderStorageBlockBinding(program, block_index, ssbo_binding_point);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, ssbo_binding_point, ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
渲染循环
while (!glfwWindowShouldClose(window))
process_input(window);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
// ???
glfwSwapBuffers(window);
glfwPollEvents();
我似乎无法弄清楚这应该如何工作。抓住稻草,我还尝试创建一个 VAO,稍后调用 glDrawArrays(GL_POINTS, 0, VERTEX_COUNT)
,但它也没有工作:
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glEnableVertexAttribArray(position_attrib_index);
glVertexAttribPointer(position_attrib_index, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
在我看来,我应该使用position_attrib_index
(应该指向vertexPosition
)来做某事,问题是为了什么?
完整示例代码
需要 OpenGL 4.3、GLEW 和 GLFW 构建命令示例:g++ -std=c++17 main.cpp -lGLEW -lglfw -lGL -o ssbo
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <array>
#include <iostream>
#include <vector>
void process_input(GLFWwindow *window)
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
void glfw_error_callback(int error_code, const char *description)
std::cerr << "GLFW Error: [" << error_code << "] " << description << '\n';
void framebuffer_size_callback(GLFWwindow *window, int width, int height)
glViewport(0, 0, width, height);
auto create_glfw_window()
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
return glfwCreateWindow(800, 600, "OpenGL and AoSoA layout", nullptr, nullptr);
void set_callbacks(GLFWwindow *window)
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetErrorCallback(glfw_error_callback);
void print_versions()
std::cout << "Using GLFW " << glfwGetVersionString() << '\n';
std::cout << "Using GLEW " << glewGetString(GLEW_VERSION) << '\n';
bool init_loader()
GLenum err = glewInit();
if (GLEW_OK != err)
std::cerr << "GLEW error: " << glewGetErrorString(err);
return err == GLEW_OK;
void GLAPIENTRY MessageCallback(
GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const void* userParam = nullptr)
std::cerr << "[GL DEBUG] " << (type == GL_DEBUG_TYPE_ERROR ? "Error: " : "") << message << '\n';
constexpr auto VECTOR_WIDTH = 4;
constexpr auto VERTEX_COUNT = 16;
struct VertexData
std::array<float, VECTOR_WIDTH> px;
std::array<float, VECTOR_WIDTH> py;
;
static const char* vertex_shader_source =
"#version 430\n"
"struct Vertex4\n"
"\n"
" float px[4]; // position x\n"
" float py[4]; // position y\n"
";\n"
"layout(std430, binding=0) buffer VertexData\n"
"\n"
" Vertex4 vertices[];\n"
";\n"
"void main()\n"
"\n"
" int dataIx = gl_VertexID / 4;\n"
" int vertexIx = gl_VertexID % 4;\n"
" vec2 vertexPosition = vec2(vertices[dataIx].px[vertexIx], vertices[dataIx].py[vertexIx]);\n"
"\n";
static const char* fragment_shader_source =
"#version 430\n"
"out vec4 out_color;\n"
"void main()\n"
"\n"
" out_color = vec4(1.0, 0.5, 0.5, 0.25);\n"
"\n";
int main(int argc, char *argv[])
glewExperimental = GL_TRUE;
auto window = create_glfw_window();
if (window == nullptr)
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
glfwMakeContextCurrent(window);
set_callbacks(window);
init_loader();
print_versions();
glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback(MessageCallback, nullptr);
std::vector<VertexData> vertices(VERTEX_COUNT / VECTOR_WIDTH);
vertices[0] =
-0.75f, 0.75f, 0.75f, -0.75f,
-0.75f, -0.75f, 0.75f, 0.75f
;
vertices[1] =
-0.50f, 0.50f, 0.50f, -0.50f,
-0.50f, -0.50f, 0.50f, 0.50f,
;
vertices[2] =
-0.25f, 0.25f, 0.25f, -0.25f,
-0.25f, -0.25f, 0.25f, 0.25f,
;
vertices[3] =
-0.05f, 0.05f, 0.05f, -0.05f,
-0.05f, -0.05f, 0.05f, 0.05f,
;
auto vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertex_shader_source, nullptr);
glCompileShader(vertex_shader);
auto fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragment_shader_source, nullptr);
glCompileShader(fragment_shader);
auto program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
const GLuint position_attrib_index = 0;
glBindAttribLocation(program, position_attrib_index, "vertexPosition");
glLinkProgram(program);
//glUseProgram(program);
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
//
// SSBO
//
const GLuint ssbo_binding_point = 0;
GLuint ssbo;
glGenBuffers(1, &ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
//glBufferStorage(GL_SHADER_STORAGE_BUFFER, vertices.size() * sizeof(VertexData), vertices.data(), GL_MAP_WRITE_BIT);
glBufferData(GL_SHADER_STORAGE_BUFFER, vertices.size() * sizeof(VertexData), vertices.data(), GL_STATIC_DRAW);
const auto block_index = glGetProgramResourceIndex(program, GL_SHADER_STORAGE_BLOCK, "VertexData");
glShaderStorageBlockBinding(program, block_index, ssbo_binding_point);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, ssbo_binding_point, ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
//
// VAO
//
//GLuint vao;
//glGenVertexArrays(1, &vao);
//glBindVertexArray(vao);
//glEnableVertexAttribArray(position_attrib_index);
//glVertexAttribPointer(position_attrib_index, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glClearColor(0.15f, 0.15f, 0.2f, 1.0f);
glPointSize(10.0f);
while (!glfwWindowShouldClose(window))
process_input(window);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
//glDrawArrays(GL_POINTS, 0, VERTEX_COUNT);
glfwSwapBuffers(window);
glfwPollEvents();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
【问题讨论】:
"这真的应该是一个计算着色器吗?" ...为什么会这样?事实上,这篇文章的大部分内容似乎都非常......对于您正在尝试的内容或这些概念如何相互关联感到困惑。比如“Assign vertexPosition index”:为什么你认为函数中的变量有属性索引? @ybungalobill:应该吗?这只有在顶点数据已经在剪辑空间中时才有意义。 @NicolBolas 在这一点上,困惑是我的世纪词汇。关于vertexPosition
,因为这或多或少是你在另一个问题中给我的着色器,我只是假设我可以像这样把它拉出来(我以前没有使用过着色器)。
【参考方案1】:
这是使用您拥有的数据进行平局的正确方法:
glBindVertexArray(vao);
glDrawArrays(GL_POINTS, 0, VERTEX_COUNT);
但是,您的问题是您的顶点着色器没有写入gl_Position
,因此没有任何内容被光栅化(无论发生什么未定义的行为)。您应该如下设置着色器中顶点的位置:
//...
out gl_PerVertex
vec4 gl_Position;
;
void main()
int dataIx = gl_VertexID / 4;
int vertexIx = gl_VertexID % 4;
vec2 vertexPosition = vec2(vertices[dataIx].px[vertexIx], vertices[dataIx].py[vertexIx]);
gl_Position = vec4(vertexPosition, 0, 1);
你可以去掉“Assign vertexPosition index”,你的VAO不需要任何属性。
【讨论】:
这给了我一个错误:Array object is not active.
。但是,如果我改为创建 VAO (glGenVertexArrays(1, &vao)
+ glBindVertexArray(vao)
,然后在渲染循环中删除对 glBindVertexArray(0)
的调用,它就可以工作了……最后。所以我是一个非常快乐的露营者。
@thomas_f:糟糕,我的错误。 ;) 我认为这是有效的,因为它在早期版本中是允许的,并且 nVidia 似乎支持它作为扩展。以上是关于如何绘制存储在 SSBO 中的顶点?的主要内容,如果未能解决你的问题,请参考以下文章