-
-
Save krisr/edc3eee52f09a52c75bd4e88fbd88de7 to your computer and use it in GitHub Desktop.
transform feedback issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cassert> | |
#include <iostream> | |
#include <OpenGL/gl3.h> | |
#include <OpenGL/gl3ext.h> | |
#include <GLFW/glfw3.h> | |
#include <array> | |
#include <vector> | |
#include <sys/time.h> | |
static inline uint64_t mmGetMicrosecondsTime() { struct timeval lntime; gettimeofday( &lntime, 0 ); return ( (uint64_t)lntime.tv_sec * 1000000 ) + (uint64_t)lntime.tv_usec; } | |
void error_callback(int error, const char* description) { | |
fprintf(stderr, "Error: %s\n", description); | |
} | |
const char* vsTransformSource = | |
"#version 330\n" | |
"layout(location = 0) in float vertex;\n" | |
"out float vOut;\n" | |
"void main() {\n" | |
" vOut = 1.0;\n" | |
"}\n"; | |
const char* vsSource2 = | |
"#version 330\n" | |
"layout(location = 0) in vec3 position;\n" | |
" void main() {\n" | |
" gl_PositionWorld = vec4(position.x, 0.0, 0.0, 1.0); \n" | |
"}\n"; | |
const char* fragSource = | |
"#version 330\n" | |
"layout(location = 0)out vec4 fragColor;\n" | |
"void main() {\n" | |
" fragColor = vec4(1.0);\n" | |
"}\n"; | |
#define GL(fn, ...) gl##fn( __VA_ARGS__) | |
int main(int argument_count, char *arguments[]) { | |
assert(glfwInit()); | |
glfwSetErrorCallback(error_callback); | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); | |
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); | |
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | |
GLFWwindow *window = glfwCreateWindow(1024, 768, "Transform Test", nullptr, nullptr); | |
assert(window); | |
glfwMakeContextCurrent(window); | |
std::cout << glGetString(GL_VERSION) << std::endl; | |
std::array<float, 10> seed; | |
GLuint bufa, bufb; | |
auto prepareBuffer = [&seed](GLuint& buf) { | |
GL(GenBuffers, 1, &buf); | |
GL(BindBuffer, GL_ARRAY_BUFFER, buf); | |
GL(BufferData, GL_ARRAY_BUFFER, sizeof(float) * seed.size(), &seed[0], GL_STREAM_DRAW); | |
GL(BindBuffer, GL_ARRAY_BUFFER, 0); | |
}; | |
prepareBuffer(bufa); | |
prepareBuffer(bufb); | |
auto prepareShader = [](const char* source, GLenum type) { | |
GLuint vsHandle = GL(CreateShader, type); | |
glShaderSource(vsHandle, 1, &source, 0); | |
GL(CompileShader, vsHandle); | |
GLint isCompiled = 0; | |
GL(GetShaderiv, vsHandle, GL_COMPILE_STATUS, (int *)&isCompiled); | |
if(isCompiled == GL_FALSE) | |
{ | |
GLint maxLength = 0; | |
glGetProgramiv(vsHandle, GL_INFO_LOG_LENGTH, &maxLength); | |
GLchar *log = new char[maxLength]; | |
GLint result; | |
glGetShaderInfoLog(vsHandle, maxLength, &result, log); | |
std::cerr << "** Shader compile error: " << log << std::endl; | |
exit(1); | |
} | |
return vsHandle; | |
}; | |
auto createProgram = [&prepareShader](const char* vert, const char* frag, int varyings_count, const GLchar ** varyings) { | |
GLuint programHandle = glCreateProgram(); | |
GLuint vertexShader, fragmentShader; | |
vertexShader = prepareShader(vert, GL_VERTEX_SHADER); | |
GL(AttachShader, programHandle, vertexShader); | |
if (frag) { | |
fragmentShader = prepareShader(frag, GL_FRAGMENT_SHADER); | |
GL(AttachShader, programHandle, fragmentShader); | |
} | |
if (varyings_count > 0) { | |
GL(TransformFeedbackVaryings, programHandle, varyings_count, varyings, GL_INTERLEAVED_ATTRIBS); | |
} | |
GL(LinkProgram, programHandle); | |
GL(DetachShader, programHandle, vertexShader); | |
if (frag) | |
GL(DetachShader, programHandle, fragmentShader); | |
return programHandle; | |
}; | |
const char* varyings[1] = { "vOut" }; | |
auto programHandle = createProgram(vsTransformSource, nullptr, 1, varyings); | |
GLuint vaoa; | |
GL(GenVertexArrays, 1, &vaoa); | |
GL(BindVertexArray, vaoa); | |
GL(BindBuffer, GL_ARRAY_BUFFER, bufa); | |
GL(EnableVertexAttribArray, 0); | |
GL(VertexAttribPointer, 0, 1, GL_FLOAT, GL_FALSE, sizeof(float), (void*)0); | |
GL(BindVertexArray, 0); | |
GLuint vaob; | |
GL(GenVertexArrays, 1, &vaob); | |
GL(BindVertexArray, vaob); | |
GL(BindBuffer, GL_ARRAY_BUFFER, bufb); | |
GL(EnableVertexAttribArray, 0); | |
GL(VertexAttribPointer, 0, 1, GL_FLOAT, GL_FALSE, sizeof(float), (void*)0); | |
GL(BindVertexArray, 0); | |
GL(UseProgram, programHandle); | |
glfwSwapInterval(0); | |
size_t count = 0; | |
while (!glfwWindowShouldClose(window)) { | |
GL(Enable, GL_RASTERIZER_DISCARD); | |
GL(BindVertexArray, vaoa); | |
GL(BindBufferBase, GL_TRANSFORM_FEEDBACK_BUFFER, 0, bufb); | |
auto start = mmGetMicrosecondsTime(); | |
glBeginTransformFeedback(GL_POINTS); | |
glDrawArrays(GL_POINTS, 0, seed.size()); | |
glEndTransformFeedback(); | |
auto end = mmGetMicrosecondsTime(); | |
printf("Time %llu\n", (end-start)); | |
start = end; | |
glClear(GL_COLOR_BUFFER_BIT); | |
std::swap(bufa, bufb); | |
std::swap(vaoa, vaob); | |
GL(Disable, GL_RASTERIZER_DISCARD); | |
GL(BindVertexArray, 0); | |
glfwSwapBuffers(window); | |
glfwPollEvents(); | |
} | |
glfwTerminate(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Time 4758
Time 4470
Time 23567
Time 3903
Time 9763
Time 4206
Time 4315
Time 4117
Time 5039
Time 15102
Time 4992
Time 3554
Time 3507
Time 4492
Time 3876
Time 12927