gKit2 light
tuto_storage.cpp
Go to the documentation of this file.
1 
3 
4 #include <cstddef>
5 #include <cassert>
6 
7 #include <vector>
8 
9 #include "window.h"
10 
11 #include "vec.h"
12 #include "color.h"
13 #include "program.h"
14 
15 
16 namespace glsl
17 {
18  // type de base alignes sur 4 octets
19  template < typename T >
20  struct alignas(4) gscalar
21  {
22  alignas(4) T x;
23 
24  gscalar( ) : x(T()) {}
25  gscalar( const T& v ) : x(v) {}
26  gscalar& operator= ( const T& v ) { x= v; return *this; }
27  operator T ( ) { return x; }
28  };
29 
30  typedef gscalar<float> gfloat;
31  typedef gscalar<int> gint;
33  typedef gscalar<bool> gbool;
34 
35  // vec2, alignes sur 2 * alignement type de base du vecteur
36  template < typename T >
37  struct alignas(8) gvec2
38  {
39  alignas(4) T x, y;
40 
41  gvec2( ) {}
42  gvec2( const gvec2<T>& v ) : x(v.x), y(v.y) {}
43  gvec2( const ::vec2& v ) : x(v.x), y(v.y) {}
44  gvec2& operator= ( const gvec2<T>& v ) { x= v.x; y= v.y; return *this; }
45  gvec2& operator= ( const ::vec2& v ) { x= v.x; y= v.y; return *this; }
46  operator ::vec2 ( ) { return ::vec2(float(x), float(y)); }
47  };
48 
49  typedef gvec2<float> vec2;
50  typedef gvec2<int> ivec2;
51  typedef gvec2<unsigned int> uvec2;
52  typedef gvec2<int> bvec2;
53 
54  // vec3, alignes sur 4 * alignement type de base du vecteur
55  template < typename T >
56  struct alignas(16) gvec3
57  {
58  alignas(4) T x, y, z;
59 
60  gvec3( ) {}
61  gvec3( const gvec3<T>& v ) : x(v.x), y(v.y), z(v.z) {}
62  gvec3( const ::vec3& v ) : x(v.x), y(v.y), z(v.z) {}
63  gvec3( const Point& v ) : x(v.x), y(v.y), z(v.z) {}
64  gvec3( const Vector& v ) : x(v.x), y(v.y), z(v.z) {}
65  gvec3& operator= ( const gvec3<T>& v ) { x= v.x; y= v.y; z= v.z; return *this; }
66  gvec3& operator= ( const ::vec3& v ) { x= v.x; y= v.y; z= v.z; return *this; }
67  gvec3& operator= ( const Point& v ) { x= v.x; y= v.y; z= v.z; return *this; }
68  gvec3& operator= ( const Vector& v ) { x= v.x; y= v.y; z= v.z; return *this; }
69  operator ::vec3 ( ) { return ::vec3(float(x), float(y), float(y)); }
70  };
71 
72  typedef gvec3<float> vec3;
73  typedef gvec3<int> ivec3;
74  typedef gvec3<unsigned int> uvec3;
75  typedef gvec3<int> bvec3;
76 
77  // vec4, alignes sur 4 * alignement type de base du vecteur
78  template < typename T >
79  struct alignas(16) gvec4
80  {
81  alignas(4) T x, y, z, w;
82 
83  gvec4( ) {}
84  gvec4( const gvec4<T>& v ) : x(v.x), y(v.y), z(v.z), w(v.w) {}
85  gvec4( const ::vec4& v ) : x(v.x), y(v.y), z(v.z), w(v.w) {}
86  gvec4& operator= ( const gvec4<T>& v ) { x(v.x), y(v.y), z(v.z), w(v.w) ; return *this; }
87  gvec4& operator= ( const ::vec4& v ) { x(v.x), y(v.y), z(v.z), w(v.w) ; return *this; }
88  gvec4& operator= ( const Color& c ) { x= c.r; y= c.g; z= c.b; w= c.a; return *this; }
89  operator ::vec4 ( ) { return ::vec4(float(x), float(y), float(y), float(w)); }
90  };
91 
92  typedef gvec4<float> vec4;
93  typedef gvec4<int> ivec4;
94  typedef gvec4<unsigned int> uvec4;
95  typedef gvec4<int> bvec4;
96 }
97 
98 
99 // utilitaire : renvoie la chaine de caracteres pour un type glsl.
100 const char *glsl_string( const GLenum type )
101 {
102  switch(type)
103  {
104  case GL_BOOL:
105  return "bool";
106  case GL_UNSIGNED_INT:
107  return "uint";
108  case GL_INT:
109  return "int";
110  case GL_FLOAT:
111  return "float";
112  case GL_FLOAT_VEC2:
113  return "vec2";
114  case GL_FLOAT_VEC3:
115  return "vec3";
116  case GL_FLOAT_VEC4:
117  return "vec4";
118  case GL_FLOAT_MAT4:
119  return "mat4";
120 
121  default:
122  return "";
123  }
124 }
125 
126 int print_storage( const GLuint program )
127 {
128  if(program == 0)
129  {
130  printf("[error] program 0, no storage buffers...\n");
131  return -1;
132  }
133 
134  // recupere le nombre de storage buffers
135  GLint buffer_count= 0;
136  glGetProgramInterfaceiv(program, GL_SHADER_STORAGE_BLOCK, GL_ACTIVE_RESOURCES, &buffer_count);
137  if(buffer_count == 0)
138  return 0;
139 
140  for(int i= 0; i < buffer_count; i++)
141  {
142  // recupere le nom du storage buffer
143  char bname[1024]= { 0 };
144  glGetProgramResourceName(program, GL_SHADER_STORAGE_BLOCK, i, sizeof(bname), NULL, bname);
145 
146  // et le binding
147  GLint binding= 0;
148  {
149  GLenum prop[]= { GL_BUFFER_BINDING };
150  glGetProgramResourceiv(program, GL_SHADER_STORAGE_BLOCK, i, 1, prop, 1, NULL, &binding);
151  }
152 
153  printf(" buffer '%s' binding %d\n", bname, binding);
154 
155  // nombre de variables declarees
156  GLint variable_count= 0;
157  {
158  GLenum prop[]= { GL_NUM_ACTIVE_VARIABLES };
159  glGetProgramResourceiv(program, GL_SHADER_STORAGE_BLOCK, i, 1, prop, 1, NULL, &variable_count);
160  }
161 
162  // identifidants des variables
163  std::vector<GLint> variables(variable_count);
164  {
165  GLenum prop[]= { GL_ACTIVE_VARIABLES };
166  glGetProgramResourceiv(program, GL_SHADER_STORAGE_BLOCK, i, 1, prop, variable_count, NULL, variables.data());
167  }
168 
169  for(int k= 0; k < variable_count; k++)
170  {
171  // organisation des variables dans le buffer
172  GLenum props[]= { GL_OFFSET, GL_TYPE, GL_ARRAY_SIZE, GL_ARRAY_STRIDE, GL_MATRIX_STRIDE, GL_IS_ROW_MAJOR, GL_TOP_LEVEL_ARRAY_STRIDE };
173  const int size= sizeof(props) / sizeof(GLenum);
174 
175  GLint params[size]= {};
176  glGetProgramResourceiv(program, GL_BUFFER_VARIABLE, variables[k], size, props, size, NULL, params);
177 
178  // nom de la variable
179  char vname[1024]= { 0 };
180  glGetProgramResourceName(program, GL_BUFFER_VARIABLE, variables[k], sizeof(vname), NULL, vname);
181 
182  printf(" '%s %s': offset %d", glsl_string(params[1]), vname, params[0]);
183  if(params[2] > 1)
184  printf(", array size %d", params[2]);
185 
186  printf(", stride %d", params[3]);
187 
188  // organisation des matrices
189  if(params[1] == GL_FLOAT_MAT4 || params[1] == GL_FLOAT_MAT3)
190  printf(", %s, matrix stride %d", params[5] ? "row major" : "column major", params[4]);
191 
192  printf(", top level stride %d\n", params[6]);
193  }
194  }
195 
196  return 0;
197 }
198 
199 
200 // application
201 GLuint program;
202 
203 int init( )
204 {
205  // compile le shader program, le program est selectionne
206  program= read_program("tutos/storage.glsl");
207  program_print_errors(program);
208 
209  print_storage(program);
210 
211 
212  struct TriangleCPU
213  {
214  vec3 a;
215  vec3 b;
216  vec3 c;
217  };
218 
219  printf("cpu:\n");
220  printf(" a %d\n", (int) offsetof(TriangleCPU, a));
221  printf(" a.x %d\n", (int) offsetof(TriangleCPU, a.x));
222  printf(" a.y %d\n", (int) offsetof(TriangleCPU, a.y));
223  printf(" a.z %d\n", (int) offsetof(TriangleCPU, a.z));
224  printf(" b %d\n", (int) offsetof(TriangleCPU, b));
225  printf(" c %d\n", (int) offsetof(TriangleCPU, c));
226  printf("= %dB\n", (int) sizeof(TriangleCPU));
227 
228 
229  struct TriangleGLSL
230  {
231  glsl::vec3 a;
232  glsl::vec3 b;
233  glsl::vec3 c;
234 
235  TriangleGLSL( const Point& _a, const Point& _b, const Point& _c ) : a(_a), b(_b), c(_c) {}
236  TriangleGLSL( const vec3& _a, const vec3& _b, const vec3& _c ) : a(_a), b(_b), c(_c) {}
237  };
238 
239  printf("glsl:\n");
240  printf(" a %d\n", (int) offsetof(TriangleGLSL, a));
241  printf(" a.x %d\n", (int) offsetof(TriangleGLSL, a.x));
242  printf(" a.y %d\n", (int) offsetof(TriangleGLSL, a.y));
243  printf(" a.z %d\n", (int) offsetof(TriangleGLSL, a.z));
244  printf(" b %d\n", (int) offsetof(TriangleGLSL, b));
245  printf(" c %d\n", (int) offsetof(TriangleGLSL, c));
246  printf("= %dB\n", (int) sizeof(TriangleGLSL));
247 
248  return 0;
249 }
250 
251 int quit( )
252 {
253  release_program(program);
254  return 0;
255 }
256 
257 
258 int main( int argc, char **argv )
259 {
260  // etape 1 : creer la fenetre
261  Window window= create_window(1024, 640, 4, 3); // openGL version 4.3
262  if(window == nullptr)
263  return 1;
264 
265  // etape 2 : creer un contexte opengl pour pouvoir dessiner
266  Context context= create_context(window);
267  if(context == nullptr)
268  return 1;
269 
270  // etape 3 : creation des objets
271  if(init() < 0)
272  {
273  printf("[error] init failed.\n");
274  return 1;
275  }
276 
277  // etape 5 : nettoyage
278  quit();
279  release_context(context);
280  release_window(window);
281  return 0;
282 }
283 
Context create_context(Window window)
cree et configure un contexte opengl
Definition: window.cpp:356
void release_window(Window window)
destruction de la fenetre.
Definition: window.cpp:325
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
Window create_window(const int w, const int h, const int major, const int minor, const int samples)
creation d'une fenetre pour l'application.
Definition: window.cpp:259
void release_context(Context context)
detruit le contexte openGL.
Definition: window.cpp:422
GLuint read_program(const char *filename, const char *definitions)
Definition: program.cpp:204
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.
Definition: program.cpp:432
int release_program(const GLuint program)
detruit les shaders et le program.
Definition: program.cpp:211
int init(std::vector< const char * > &options)
Definition: shader_kit.cpp:92
representation d'une couleur (rgba) transparente ou opaque.
Definition: color.h:14
representation d'un point 3d.
Definition: vec.h:21
representation d'un vecteur 3d.
Definition: vec.h:59
vecteur generique, utilitaire.
Definition: vec.h:146