gKit2 light
Loading...
Searching...
No Matches
tuto_storage.cpp File Reference

alignement des donnees / storage buffers. More...

#include <cstddef>
#include <cassert>
#include <vector>
#include "window.h"
#include "vec.h"
#include "color.h"
#include "program.h"

Go to the source code of this file.

Classes

struct  glsl::gscalar< T >
struct  glsl::gvec2< T >
struct  glsl::gvec3< T >
struct  glsl::gvec4< T >

Typedefs

typedef gscalar< float > glsl::gfloat
typedef gscalar< int > glsl::gint
typedef gscalar< unsigned int > glsl::guint
typedef gscalar< bool > glsl::gbool
typedef gvec2< float > glsl::vec2
typedef gvec2< int > glsl::ivec2
typedef gvec2< unsigned int > glsl::uvec2
typedef gvec2< int > glsl::bvec2
typedef gvec3< float > glsl::vec3
typedef gvec3< int > glsl::ivec3
typedef gvec3< unsigned int > glsl::uvec3
typedef gvec3< int > glsl::bvec3
typedef gvec4< float > glsl::vec4
typedef gvec4< int > glsl::ivec4
typedef gvec4< unsigned int > glsl::uvec4
typedef gvec4< int > glsl::bvec4

Functions

const char * glsl_string (const GLenum type)
int print_storage (const GLuint program)
int init ()
int quit ()
int main (int argc, char **argv)

Variables

GLuint program

Detailed Description

alignement des donnees / storage buffers.

Definition in file tuto_storage.cpp.

Typedef Documentation

◆ gfloat

typedef gscalar< float > glsl::gfloat

Definition at line 30 of file tuto_storage.cpp.

◆ gint

typedef gscalar< int > glsl::gint

Definition at line 31 of file tuto_storage.cpp.

◆ guint

typedef gscalar< unsigned int > glsl::guint

Definition at line 32 of file tuto_storage.cpp.

◆ gbool

typedef gscalar< bool > glsl::gbool

Definition at line 33 of file tuto_storage.cpp.

◆ vec2

typedef gvec2< float > glsl::vec2

Definition at line 49 of file tuto_storage.cpp.

◆ ivec2

typedef gvec2< int > glsl::ivec2

Definition at line 50 of file tuto_storage.cpp.

◆ uvec2

typedef gvec2< unsigned int > glsl::uvec2

Definition at line 51 of file tuto_storage.cpp.

◆ bvec2

typedef gvec2< int > glsl::bvec2

Definition at line 52 of file tuto_storage.cpp.

◆ vec3

typedef gvec3< float > glsl::vec3

Definition at line 72 of file tuto_storage.cpp.

◆ ivec3

typedef gvec3< int > glsl::ivec3

Definition at line 73 of file tuto_storage.cpp.

◆ uvec3

typedef gvec3< unsigned int > glsl::uvec3

Definition at line 74 of file tuto_storage.cpp.

◆ bvec3

typedef gvec3< int > glsl::bvec3

Definition at line 75 of file tuto_storage.cpp.

◆ vec4

typedef gvec4< float > glsl::vec4

Definition at line 92 of file tuto_storage.cpp.

◆ ivec4

typedef gvec4< int > glsl::ivec4

Definition at line 93 of file tuto_storage.cpp.

◆ uvec4

typedef gvec4< unsigned int > glsl::uvec4

Definition at line 94 of file tuto_storage.cpp.

◆ bvec4

typedef gvec4< int > glsl::bvec4

Definition at line 95 of file tuto_storage.cpp.

Function Documentation

◆ glsl_string()

const char * glsl_string ( const GLenum type)

Definition at line 100 of file tuto_storage.cpp.

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}

◆ print_storage()

int print_storage ( const GLuint program)

Definition at line 126 of file tuto_storage.cpp.

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}
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

◆ init()

int init ( )

Definition at line 203 of file tuto_storage.cpp.

204{
205 // compile le shader program, le program est selectionne
206 program= read_program("gkit2_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}
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

◆ quit()

int quit ( )

Definition at line 251 of file tuto_storage.cpp.

252{
253 release_program(program);
254 return 0;
255}
int release_program(const GLuint program)
detruit les shaders et le program.
Definition program.cpp:240

◆ main()

int main ( int argc,
char ** argv )

Definition at line 258 of file tuto_storage.cpp.

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}
Context create_context(Window window)
cree et configure un contexte opengl
Definition window.cpp:358
void release_window(Window window)
destruction de la fenetre.
Definition window.cpp:328
Window create_window(const int w, const int h, const int major, const int minor)
creation d'une fenetre pour l'application.
Definition window.cpp:262
void release_context(Context context)
detruit le contexte openGL.
Definition window.cpp:439
int init(std::vector< const char * > &options)

Variable Documentation

◆ program

GLuint program

Definition at line 201 of file tuto_storage.cpp.