37 std::vector<int> data(1024);
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_read_buffer);
56 glBindBuffer(GL_COPY_READ_BUFFER, m_read_buffer);
57 #ifdef USE_BUFFER_STORAGE
59 glBufferStorage(GL_COPY_READ_BUFFER,
sizeof(
int) * data.size(),
nullptr, GL_CLIENT_STORAGE_BIT | GL_MAP_READ_BIT);
61 glBufferStorage(GL_COPY_READ_BUFFER,
sizeof(
int) * data.size(),
nullptr, GL_CLIENT_STORAGE_BIT);
64 glBufferData(GL_COPY_READ_BUFFER,
sizeof(
int) * data.size(),
nullptr, GL_DYNAMIC_READ);
67 #ifdef USE_BUFFER_STORAGE
68 printf(
"!! use buffer storage\n");
70 printf(
"!! use buffer data\n");
79 glDeleteBuffers(1, &m_gpu_buffer1);
80 glDeleteBuffers(1, &m_gpu_buffer2);
81 glDeleteBuffers(1, &m_read_buffer);
88 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_gpu_buffer1);
89 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_gpu_buffer2);
92 glUseProgram(m_program);
93 glDispatchCompute(4, 1, 1);
96 glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
101 glBindBuffer(GL_COPY_READ_BUFFER, m_read_buffer);
102 glCopyBufferSubData(GL_SHADER_STORAGE_BUFFER, GL_COPY_READ_BUFFER, 0, 0,
sizeof(
int)*1024);
109 int *tmp= (
int *) glMapBuffer(GL_COPY_READ_BUFFER, GL_READ_ONLY);
111 for(
unsigned i= 0; i < 1024; i++)
115 glUnmapBuffer(GL_COPY_READ_BUFFER);
119 printf(
"!! use get buffer\n");
121 std::vector<int> tmp(1024);
122 glGetBufferSubData(GL_COPY_READ_BUFFER, 0,
sizeof(
int) * tmp.size(), tmp.data());
123 for(
unsigned i= 0; i < tmp.size(); i++)
131 GLuint m_gpu_buffer1;
132 GLuint m_gpu_buffer2;
133 GLuint m_read_buffer;
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.