34 std::vector<int> data(1024);
35 for(
unsigned i= 0; i < data.size(); i++)
39 glGenBuffers(1, &m_gpu_buffer1);
40 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_gpu_buffer1);
41 #ifdef USE_BUFFER_STORAGE
42 glBufferStorage(GL_SHADER_STORAGE_BUFFER,
sizeof(
int) * data.size(), data.data(), 0);
44 glBufferData(GL_SHADER_STORAGE_BUFFER,
sizeof(
int) * data.size(), data.data(), GL_STATIC_COPY);
47 glGenBuffers(1, &m_gpu_buffer2);
48 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_gpu_buffer2);
49 #ifdef USE_BUFFER_STORAGE
50 glBufferStorage(GL_SHADER_STORAGE_BUFFER,
sizeof(
int) * data.size(),
nullptr, 0);
52 glBufferData(GL_SHADER_STORAGE_BUFFER,
sizeof(
int) * data.size(),
nullptr, GL_STATIC_COPY);
55 glGenBuffers(1, &m_gpu_count);
56 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, m_gpu_count);
57 #ifdef USE_BUFFER_STORAGE
58 glBufferStorage(GL_SHADER_STORAGE_BUFFER,
sizeof(
int),
nullptr, 0);
60 glBufferData(GL_SHADER_STORAGE_BUFFER,
sizeof(
int),
nullptr, GL_STATIC_COPY);
63 glGenBuffers(1, &m_read_buffer);
64 glBindBuffer(GL_COPY_READ_BUFFER, m_read_buffer);
65 #ifdef USE_BUFFER_STORAGE
67 glBufferStorage(GL_COPY_READ_BUFFER,
sizeof(
int) * data.size(),
nullptr, GL_CLIENT_STORAGE_BIT | GL_MAP_READ_BIT);
69 glBufferStorage(GL_COPY_READ_BUFFER,
sizeof(
int) * data.size(),
nullptr, GL_CLIENT_STORAGE_BIT);
72 glBufferData(GL_COPY_READ_BUFFER,
sizeof(
int) * data.size(),
nullptr, GL_DYNAMIC_READ);
75 #ifdef USE_BUFFER_STORAGE
76 printf(
"!! use buffer storage\n");
78 printf(
"!! use buffer data\n");
87 glDeleteBuffers(1, &m_gpu_buffer1);
88 glDeleteBuffers(1, &m_gpu_buffer2);
89 glDeleteBuffers(1, &m_read_buffer);
96 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_gpu_buffer1);
97 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_gpu_buffer2);
99 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, m_gpu_count);
102 glClearBufferData(GL_SHADER_STORAGE_BUFFER, GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT, &zero);
104 glUseProgram(m_program);
105 glDispatchCompute(4, 1, 1);
107 glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
109 glBindBuffer(GL_COPY_READ_BUFFER, m_read_buffer);
110 glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_gpu_buffer2);
111 glCopyBufferSubData(GL_SHADER_STORAGE_BUFFER, GL_COPY_READ_BUFFER, 0, 0,
sizeof(
int)*1024);
127 printf(
"!! use get buffer\n");
131 glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_gpu_count);
132 glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0,
sizeof(
int), &n);
137 std::vector<int> tmp(n);
138 glGetBufferSubData(GL_COPY_READ_BUFFER, 0,
sizeof(
int) * tmp.size(), tmp.data());
140 for(
unsigned i= 0; i < tmp.size(); i++)
148 GLuint m_gpu_buffer1;
149 GLuint m_gpu_buffer2;
151 GLuint m_read_buffer;
App(const int width, const int height, const int major=3, const int minor=3, const int samples=0)
constructeur, dimensions de la fenetre et version d'openGL.
int run()
execution de l'application.
void printf(Text &text, const int px, const int py, const char *format,...)
affiche un texte a la position x, y. meme utilisation que printf().
GLuint read_program(const char *filename, const char *definitions)
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.
int release_program(const GLuint program)
detruit les shaders et le program.
int quit()
a deriver pour detruire les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
int render()
a deriver pour afficher les objets. renvoie 1 pour continuer, 0 pour fermer l'application.
int init()
a deriver pour creer les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.