gKit2 light
Loading...
Searching...
No Matches
tuto_count_buffer.cpp
Go to the documentation of this file.
1
3
4#include <vector>
5
6#include "app.h"
7#include "program.h"
8
9/*
10 cree 2 buffers dans la memoire privee du gpu, soit avec glBufferData(GL_STATIC) comme d'habitude,
11 soit avec glBufferStorage() introduit par opengl 4.4
12
13 pour relire les resultats qui se trouvent dans un buffer prive du gpu, il faut d'abord copier les valeurs dans un buffer accessible par l'application,
14 puis relire enfin les valeurs dans l'application, soit en utilisant glGetBufferSubData() soit en utilisant glMapBuffer()
15
16 les differentes solutions sont dispo pour tester, il suffit de definir (ou pas) USE_BUFFER_STORAGE et USE_MAP.
17 la configuration choisie est affichee par init().
18 */
19
20
21#define USE_BUFFER_STORAGE
22//~ #define USE_MAP
23
24
25struct ReadBuffer : public App
26{
27 ReadBuffer( ) : App(1280, 768, 4,3) {}
28
29 int init( )
30 {
31 {
32 if(!GLAD_GL_KHR_shader_subgroup)
33 printf("no shader_subgroups.\n");
34
35 if(GLAD_GL_KHR_shader_subgroup)
36 {
37 printf("shader_subgroups:\n");
38
39 GLint size= 0;
40 glGetIntegerv(GL_SUBGROUP_SIZE_KHR, &size);
41 printf("subgroup size %d\n", size);
42
43 GLint stages= 0;
44 glGetIntegerv(GL_SUBGROUP_SUPPORTED_STAGES_KHR, &stages);
45 printf("stages:\n");
46 if(stages & GL_COMPUTE_SHADER_BIT) printf(" compute\n"); else printf(" no compute\n");
47 if(stages & GL_VERTEX_SHADER_BIT) printf(" vertex\n"); else printf(" no vertex\n");
48 if(stages & GL_TESS_CONTROL_SHADER_BIT) printf(" tesselation control\n"); else printf(" no tesselation control\n");
49 if(stages & GL_TESS_EVALUATION_SHADER_BIT) printf(" tesselation evaluation\n"); else printf(" no tesselation evaluation\n");
50 if(stages & GL_GEOMETRY_SHADER_BIT) printf(" geometry\n"); else printf(" no geometry\n");
51 if(stages & GL_FRAGMENT_SHADER_BIT) printf(" fragment\n"); else printf(" no fragment\n");
52 if(stages & GL_MESH_SHADER_BIT_EXT) printf(" mesh\n"); else printf(" no mesh\n");
53 if(stages & GL_TASK_SHADER_BIT_EXT) printf(" mesh task\n"); else printf(" no mesh task\n");
54
55 GLint features;
56 glGetIntegerv(GL_SUBGROUP_SUPPORTED_FEATURES_KHR, &features);
57 printf("features:\n");
58 if(features & GL_SUBGROUP_FEATURE_BASIC_BIT_KHR) printf(" basic\n"); else printf(" no basic\n");
59 if(features & GL_SUBGROUP_FEATURE_VOTE_BIT_KHR) printf(" vote\n"); else printf(" no vote\n");
60 if(features & GL_SUBGROUP_FEATURE_ARITHMETIC_BIT_KHR) printf(" arithmetic\n"); else printf(" no arithmetic\n");
61 if(features & GL_SUBGROUP_FEATURE_BALLOT_BIT_KHR) printf(" ballot\n"); else printf(" no ballot\n");
62 if(features & GL_SUBGROUP_FEATURE_SHUFFLE_BIT_KHR) printf(" shuffle\n"); else printf(" no shuffle\n");
63 if(features & GL_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT_KHR) printf(" shuffle relative\n"); else printf(" no shuffle relative\n");
64 if(features & GL_SUBGROUP_FEATURE_CLUSTERED_BIT_KHR) printf(" clustered\n"); else printf(" no clustered\n");
65 if(features & GL_SUBGROUP_FEATURE_QUAD_BIT_KHR) printf(" quad\n"); else printf(" no quad\n");
66 }
67 }
68
69 m_program= read_program("gkit2_tutos/M2/count_buffer.glsl");
70 program_print_errors(m_program);
71
72 std::vector<int> data(1024);
73 for(unsigned i= 0; i < data.size(); i++)
74 data[i]= i % 16;
75
76
77 glGenBuffers(1, &m_gpu_buffer1);
78 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_gpu_buffer1);
79 #ifdef USE_BUFFER_STORAGE
80 glBufferStorage(GL_SHADER_STORAGE_BUFFER, sizeof(int) * data.size(), data.data(), 0);
81 #else
82 glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(int) * data.size(), data.data(), GL_STATIC_COPY);
83 #endif
84
85 glGenBuffers(1, &m_gpu_buffer2);
86 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_gpu_buffer2);
87 #ifdef USE_BUFFER_STORAGE
88 glBufferStorage(GL_SHADER_STORAGE_BUFFER, sizeof(int) * data.size(), nullptr, 0);
89 #else
90 glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(int) * data.size(), nullptr, GL_STATIC_COPY);
91 #endif
92
93 glGenBuffers(1, &m_gpu_count);
94 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, m_gpu_count);
95 #ifdef USE_BUFFER_STORAGE
96 glBufferStorage(GL_SHADER_STORAGE_BUFFER, sizeof(int), nullptr, 0);
97 #else
98 glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(int), nullptr, GL_STATIC_COPY);
99 #endif
100
101 glGenBuffers(1, &m_read_buffer);
102 glBindBuffer(GL_COPY_READ_BUFFER, m_read_buffer);
103 #ifdef USE_BUFFER_STORAGE
104 #ifdef USE_MAP
105 glBufferStorage(GL_COPY_READ_BUFFER, sizeof(int) * data.size(), nullptr, GL_CLIENT_STORAGE_BIT | GL_MAP_READ_BIT);
106 #else
107 glBufferStorage(GL_COPY_READ_BUFFER, sizeof(int) * data.size(), nullptr, GL_CLIENT_STORAGE_BIT);
108 #endif
109 #else
110 glBufferData(GL_COPY_READ_BUFFER, sizeof(int) * data.size(), nullptr, GL_DYNAMIC_READ);
111 #endif
112
113 #ifdef USE_BUFFER_STORAGE
114 printf("!! use buffer storage\n");
115 #else
116 printf("!! use buffer data\n");
117 #endif
118
119 return 0;
120 }
121
122 int quit( )
123 {
124 release_program(m_program);
125 glDeleteBuffers(1, &m_gpu_buffer1);
126 glDeleteBuffers(1, &m_gpu_buffer2);
127 glDeleteBuffers(1, &m_read_buffer);
128
129 return 0;
130 }
131
132 int render( )
133 {
134 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_gpu_buffer1);
135 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_gpu_buffer2);
136
137 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, m_gpu_count);
138 // remet le compteur a zero
139 int zero= 0;
140 glClearBufferData(GL_SHADER_STORAGE_BUFFER, GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT, &zero);
141
142 glUseProgram(m_program);
143 glDispatchCompute(4, 1, 1);
144
145 glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
146
147 glBindBuffer(GL_COPY_READ_BUFFER, m_read_buffer);
148 glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_gpu_buffer2);
149 glCopyBufferSubData(GL_SHADER_STORAGE_BUFFER, GL_COPY_READ_BUFFER, 0, 0, sizeof(int)*1024);
150
151 #ifdef USE_MAP
152 printf("!! use map\n");
153
154 // TODO: recupere n, le nombre de valeurs...
155
156 int *tmp= (int *) glMapBuffer(GL_COPY_READ_BUFFER, GL_READ_ONLY);
157 {
158 for(unsigned i= 0; i < 1024; i++)
159 printf("%d ", tmp[i]);
160 printf("\n");
161 }
162 glUnmapBuffer(GL_COPY_READ_BUFFER);
163
164 #else
165 printf("!! use get buffer\n");
166
167 // recupere le nombre de valeurs stockees dans le buffer resultat
168 int n= 0;
169 glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_gpu_count);
170 glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, sizeof(int), &n);
171
172 printf("%d values\n", n);
173
174 // recupere les valeurs
175 std::vector<int> tmp(n);
176 glGetBufferSubData(GL_COPY_READ_BUFFER, 0, sizeof(int) * tmp.size(), tmp.data());
177
178 for(unsigned i= 0; i < tmp.size(); i++)
179 printf("%d ", tmp[i]);
180 printf("\n");
181 #endif
182
183 return 0;
184 }
185
186 GLuint m_gpu_buffer1;
187 GLuint m_gpu_buffer2;
188 GLuint m_gpu_count;
189 GLuint m_read_buffer;
190 GLuint m_program;
191};
192
193int main( )
194{
195 ReadBuffer app;
196 app.run();
197
198 return 0;
199}
int run()
execution de l'application.
Definition app.cpp:36
App(const int width, const int height, const int major=3, const int minor=3)
constructeur, dimensions de la fenetre et version d'openGL.
Definition app.cpp:11
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().
Definition text.cpp:140
GLuint read_program(const char *filename, const char *definitions)
Definition program.cpp:217
int program_print_errors(const GLuint program, const char *filename)
affiche les erreurs de compilation.
Definition program.cpp:461
int release_program(const GLuint program)
detruit les shaders et le program.
Definition program.cpp:240
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.