Browse code

Add offset to Members

Robert Cranston authored on 08/03/2023 23:08:40
Showing 1 changed files
... ...
@@ -399,8 +399,15 @@ either
399 399
 -   `T` itself, if `T` is a [scalar][]
400 400
 
401 401
 then `constexpr auto GLTraits::members<T>()` returns an instance of the
402
-[empty][] `struct GLTraits::Members<typename T, typename... Values>` where
403
-`Values...` are the ("member") types above, in the order they occur in `T`.
402
+[empty][] `struct GLTraits::Members<typename T, typename... Members>` where
403
+each of `Members...` is `GLTraits::Member<typename Value, std::size_t offset>`
404
+that describe the ("member") types above, in the order they occur in `T`.
405
+`Value` is the type of the member and `offset` is the offset of the member in
406
+`T`. Note that `offset` is only correct if the padding in `T` is minimal while
407
+still satisfying the [`alignof`][] of the member types. The author knows of no
408
+compiler that breaks this assumption unless explicitly instructed (e.g. with
409
+[`alignas`][], which are not taken into account), however the standard allows
410
+it.
404 411
 
405 412
 An example use case would be along the lines of:
406 413
 
... ...
@@ -408,26 +415,22 @@ An example use case would be along the lines of:
408 415
 template<
409 416
     typename    T,
410 417
     typename    Value,
411
-    typename... Values
418
+    std::size_t offset,
419
+    typename... Members
412 420
 >
413 421
 void vertex_setup(
414
-    GLTraits::Members<T, Value, Values...>,
415
-    std::size_t offset   = 0,
416
-    GLint       location = 0
422
+    GLTraits::Members<T, GLTraits::Member<Value, offset>, Members...>,
423
+    GLint location = 0
417 424
 )
418 425
 {
419
-    auto unaligned = offset % alignof(Value);
420
-    if (unaligned)
421
-        offset += alignof(Value) - unaligned;
422 426
     GLTraits::Value<Value>::vertex_attrib_pointer(location, offset, sizeof(T));
423 427
     vertex_setup(
424
-        GLTraits::Members<T, Values...>{},
425
-        offset   + sizeof(Value),
428
+        GLTraits::Members<T, Members...>{},
426 429
         location + GLTraits::Value<Value>::columns
427 430
     );
428 431
 }
429 432
 template<typename T>
430
-void vertex_setup(GLTraits::Members<T>, std::size_t, GLint)
433
+void vertex_setup(GLTraits::Members<T>, GLint)
431 434
 {}
432 435
 
433 436
 struct Vertex
... ...
@@ -462,6 +465,8 @@ version [`magic_get`][]) on whose talks this implementation is based:
462 465
 [class]: https://en.cppreference.com/w/cpp/types/is_class
463 466
 [array]: https://en.cppreference.com/w/cpp/types/is_array
464 467
 [scalar]: https://en.cppreference.com/w/cpp/types/is_scalar
468
+[`alignof`]: https://en.cppreference.com/w/cpp/language/alignof
469
+[`alignas`]: https://en.cppreference.com/w/cpp/language/alignas
465 470
 [`tests/vertex_setup.cpp`]: tests/vertex_setup.cpp
466 471
 [`doc/vertex_setup`]: doc/vertex_setup
467 472
 [`doc/vertex_setup.i`]: doc/vertex_setup.i
Browse code

Add T to Members

Robert Cranston authored on 08/03/2023 23:32:20
Showing 1 changed files
... ...
@@ -399,19 +399,19 @@ either
399 399
 -   `T` itself, if `T` is a [scalar][]
400 400
 
401 401
 then `constexpr auto GLTraits::members<T>()` returns an instance of the
402
-[empty][] `struct GLTraits::Members<typename... Values>` where `Values...` are
403
-the ("member") types above, in the order they occur in `T`.
402
+[empty][] `struct GLTraits::Members<typename T, typename... Values>` where
403
+`Values...` are the ("member") types above, in the order they occur in `T`.
404 404
 
405 405
 An example use case would be along the lines of:
406 406
 
407 407
 ```cpp
408 408
 template<
409
+    typename    T,
409 410
     typename    Value,
410 411
     typename... Values
411 412
 >
412 413
 void vertex_setup(
413
-    GLTraits::Members<Value, Values...>,
414
-    std::size_t stride,
414
+    GLTraits::Members<T, Value, Values...>,
415 415
     std::size_t offset   = 0,
416 416
     GLint       location = 0
417 417
 )
... ...
@@ -419,15 +419,15 @@ void vertex_setup(
419 419
     auto unaligned = offset % alignof(Value);
420 420
     if (unaligned)
421 421
         offset += alignof(Value) - unaligned;
422
-    GLTraits::Value<Value>::vertex_attrib_pointer(location, offset, stride);
422
+    GLTraits::Value<Value>::vertex_attrib_pointer(location, offset, sizeof(T));
423 423
     vertex_setup(
424
-        GLTraits::Members<Values...>{},
425
-        stride,
424
+        GLTraits::Members<T, Values...>{},
426 425
         offset   + sizeof(Value),
427 426
         location + GLTraits::Value<Value>::columns
428 427
     );
429 428
 }
430
-void vertex_setup(GLTraits::Members<>, std::size_t, std::size_t, GLint)
429
+template<typename T>
430
+void vertex_setup(GLTraits::Members<T>, std::size_t, GLint)
431 431
 {}
432 432
 
433 433
 struct Vertex
... ...
@@ -440,7 +440,7 @@ struct Vertex
440 440
     GLubyte    flags;
441 441
     GLdouble   unaligned;
442 442
 };
443
-vertex_setup(GLTraits::members<Vertex>(), sizeof(Vertex));
443
+vertex_setup(GLTraits::members<Vertex>());
444 444
 ```
445 445
 
446 446
 This use case is tested in [`tests/vertex_setup.cpp`][]. To verify that the
Browse code

Add Members

Robert Cranston authored on 01/03/2023 23:07:13
Showing 1 changed files
... ...
@@ -1,6 +1,6 @@
1 1
 # [`gltraits`][]
2 2
 
3
-A [C++11][]/[OpenGL][]>=[1.0][](/[GLM][]) [trait][]s library.
3
+A [C++11][](/[C++14][])/[OpenGL][]>=[1.0][](/[GLM][]) [trait][]s library.
4 4
 
5 5
 This library seeks to unify some parts of the OpenGL [API][] to ease [generic
6 6
 programming][]. It also provides sensible default arguments, [optional][debug]
... ...
@@ -46,6 +46,7 @@ further.
46 46
 
47 47
 [`gltraits`]: https://git.rcrnstn.net/rcrnstn/gltraits
48 48
 [C++11]: https://en.wikipedia.org/wiki/C++11
49
+[C++14]: https://en.wikipedia.org/wiki/C++14
49 50
 [OpenGL]: https://en.wikipedia.org/wiki/OpenGL
50 51
 [1.0]: https://en.wikipedia.org/wiki/OpenGL#Version_history
51 52
 [GLM]: https://glm.g-truc.net
... ...
@@ -383,6 +384,92 @@ signatures for `glFramebufferTexture1D` and `glFramebufferTexture{2,3}D`.)
383 384
 [immutable storage]: https://www.khronos.org/opengl/wiki/Texture_Storage#Immutable_storage
384 385
 [pixel transfer parameters]: https://www.khronos.org/opengl/wiki/Pixel_Transfer#Pixel_transfer_parameters
385 386
 
387
+### Member
388
+
389
+`GLTraits::members()` provides compile time reflection for types consisting
390
+(only) of types supported by `GLTraits::Value`. This feature is only available
391
+if [C++14][] or above is supported.
392
+
393
+If `T` is a non-[empty][] [trivially copyable][] [standard-layout][] type and
394
+there exists template specializations of `GLTraits::Value` for the types of
395
+either
396
+
397
+-   public member variables of `T`, if `T` is a [class][], or
398
+-   elements of `T`, if `T` is an [array][], or
399
+-   `T` itself, if `T` is a [scalar][]
400
+
401
+then `constexpr auto GLTraits::members<T>()` returns an instance of the
402
+[empty][] `struct GLTraits::Members<typename... Values>` where `Values...` are
403
+the ("member") types above, in the order they occur in `T`.
404
+
405
+An example use case would be along the lines of:
406
+
407
+```cpp
408
+template<
409
+    typename    Value,
410
+    typename... Values
411
+>
412
+void vertex_setup(
413
+    GLTraits::Members<Value, Values...>,
414
+    std::size_t stride,
415
+    std::size_t offset   = 0,
416
+    GLint       location = 0
417
+)
418
+{
419
+    auto unaligned = offset % alignof(Value);
420
+    if (unaligned)
421
+        offset += alignof(Value) - unaligned;
422
+    GLTraits::Value<Value>::vertex_attrib_pointer(location, offset, stride);
423
+    vertex_setup(
424
+        GLTraits::Members<Values...>{},
425
+        stride,
426
+        offset   + sizeof(Value),
427
+        location + GLTraits::Value<Value>::columns
428
+    );
429
+}
430
+void vertex_setup(GLTraits::Members<>, std::size_t, std::size_t, GLint)
431
+{}
432
+
433
+struct Vertex
434
+{
435
+    glm::vec3  position;
436
+    glm::vec2  tex_coord;
437
+    glm::mat3  tbn;
438
+    glm::ivec4 bone_indices;
439
+    glm::vec4  bone_weights;
440
+    GLubyte    flags;
441
+    GLdouble   unaligned;
442
+};
443
+vertex_setup(GLTraits::members<Vertex>(), sizeof(Vertex));
444
+```
445
+
446
+This use case is tested in [`tests/vertex_setup.cpp`][]. To verify that the
447
+compiler sees through all the templates, a script that builds it and
448
+disassembles it available in [`doc/vertex_setup`][] and its output in
449
+[`doc/vertex_setup.i`][].
450
+
451
+It is recommended to use a library based on `gltraits` that abstracts this
452
+further.
453
+
454
+Many thanks to Antony Polukhin (the author of [Boost.PFR][] and its standalone
455
+version [`magic_get`][]) on whose talks this implementation is based:
456
+
457
+-   [CppCon 2016: C++14 Reflections Without Macros, Markup nor External Tooling][]
458
+-   [Meeting C++ 2018: Better C++14 reflections][]
459
+
460
+[trivially copyable]: https://en.cppreference.com/w/cpp/types/is_trivially_copyable
461
+[standard-layout]: https://en.cppreference.com/w/cpp/types/is_standard_layout
462
+[class]: https://en.cppreference.com/w/cpp/types/is_class
463
+[array]: https://en.cppreference.com/w/cpp/types/is_array
464
+[scalar]: https://en.cppreference.com/w/cpp/types/is_scalar
465
+[`tests/vertex_setup.cpp`]: tests/vertex_setup.cpp
466
+[`doc/vertex_setup`]: doc/vertex_setup
467
+[`doc/vertex_setup.i`]: doc/vertex_setup.i
468
+[Boost.PFR]: https://www.boost.org/libs/pfr
469
+[`magic_get`]: https://github.com/apolukhin/magic_get
470
+[CppCon 2016: C++14 Reflections Without Macros, Markup nor External Tooling]: https://www.youtube.com/watch?v=abdeAew3gmQ
471
+[Meeting C++ 2018: Better C++14 reflections]: https://www.youtube.com/watch?v=UlNUNxLtBI0
472
+
386 473
 ## Building
387 474
 
388 475
 See [`BUILDING.md`][].
Browse code

Add Texture

Robert Cranston authored on 28/02/2023 02:21:38
Showing 1 changed files
... ...
@@ -29,6 +29,21 @@ These constants are provided in `GLTraits::Value<glm::uvec3>`. Of course, this
29 29
 becomes even more valuable when `glm::uvec3` is replaced by some unknown
30 30
 template parameter.
31 31
 
32
+The function call seen in the video
33
+
34
+```cpp
35
+glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32UI, WindowWidth, WindowHeight, 0, GL_RGB_INTEGER, GL_UNSIGNED_INT, NULL);
36
+```
37
+
38
+can be replaced with
39
+
40
+```cpp
41
+GLTraits::Texture<2>::tex_image<glm::uvec3>(GL_TEXTURE_2D, {WindowWidth, WindowHeight});
42
+```
43
+
44
+It is recommended to use a library based on `gltraits` that abstracts this
45
+further.
46
+
32 47
 [`gltraits`]: https://git.rcrnstn.net/rcrnstn/gltraits
33 48
 [C++11]: https://en.wikipedia.org/wiki/C++11
34 49
 [OpenGL]: https://en.wikipedia.org/wiki/OpenGL
... ...
@@ -204,6 +219,170 @@ For `object_types` equal to  `GL_SHADER`, `GL_PROGRAM`, and
204 219
 specializations of `GLTraits::Object`. Consult the source for the definitions
205 220
 and usage examples.
206 221
 
222
+### Texture
223
+
224
+The [empty][] `struct GLTraits::Texture<std::size_t N>` is template specialized
225
+on the following values.
226
+
227
+-   `1`. 1D textures.
228
+-   `2`. 2D textures.
229
+-   `3`. 3D textures.
230
+
231
+`GLTraits::Texture` contains the following type definitions.
232
+
233
+-   `Size`. An alias for `std::array<GLsizei, N>`.
234
+-   `Offset`. An alias for `std::array<GLsizei, N>`.
235
+-   `CopySize`. An alias for `std::array<GLsizei, min(N, 2)>`.
236
+-   `CopyOffset`. An alias for `std::array<GLsizei, 2>`.
237
+
238
+`GLTraits::Texture` contains the following `static constexpr` member variables.
239
+
240
+-   `default_target`. This is the default target, but many other targets can be
241
+    used with the member functions described below. E.g. for `N = 2`
242
+    `default_target` is `GL_TEXTURE_2D`, but valid `target` function arguments
243
+    are `GL_{,PROXY}_TEXTURE_{2D,RECTANGLE,1D_ARRAY}`,
244
+    `GL_TEXTURE_CUBE_MAP_{POSITIVE,NEGATIVE}_{X,Y,Z}`, and
245
+    `GL_PROXY_TEXTURE_CUBE_MAP`.
246
+-   `default_binding`. Likewise, this is the default binding.
247
+
248
+`GLTraits::Texture` contains the following `static` member functions.
249
+
250
+-   ```cpp
251
+    template<typename Value = GLubyte>
252
+    void tex_image(
253
+        GLenum        target,
254
+        Size          size,
255
+        GLenum        internal_format = 0,
256
+        Value const * data            = nullptr,
257
+        GLenum        format          = 0,
258
+        GLenum        type            = 0,
259
+        GLint         level           = 0,
260
+        GLint         border          = 0
261
+    )
262
+    ```
263
+
264
+    Generalization of `glTexImage{1,2,3}D`.
265
+
266
+    OpenGL requires that `format` matches the kind of [image format][]
267
+    specified by `internal_format` (even if `data` is `nullptr`!). Moreover, if
268
+    `internal_format` specifies an unsized image format, `type` may be used by
269
+    the implementation when choosing a size (perhaps even if that size is
270
+    slow!). Therefore, if `format` and/or `type` is `0` (the default) sensible
271
+    values will be chosen based on the given (or automatically chosen, see
272
+    below) `internal_format` (note that there is no requirement that the number
273
+    of color components of `internal_format` and `format` match):
274
+
275
+    -   [Color formats][]: `GLTraits::Value<Value>` member variables.
276
+    -   [Depth/stencil formats][]: `GL_DEPTH_STENCIL`, `GL_UNSIGNED_INT_24_8`.
277
+    -   [Depth formats][]: `GL_DEPTH_COMPONENT`, `GL_UNSIGNED_INT`.
278
+    -   [Stencil formats][]: `GL_STENCIL_INDEX`, `GL_UNSIGNED_BYTE`.
279
+
280
+-   ```cpp
281
+    template<typename Value = GLubyte>
282
+    void {tex,texture}_storage(
283
+        {GLenum,GLuint} {target,texture},
284
+        Size            size,
285
+        GLenum          internal_format = 0,
286
+        GLsizei         levels          = 1
287
+    )
288
+    ```
289
+
290
+    Generalizations of `gl{Tex,Texture}Storage{1,2,3}D`, which both use
291
+    [immutable storage][], where the latter uses [Direct State Access][].
292
+
293
+-   ```cpp
294
+    template<typename Value = GLubyte>
295
+    void {tex,texture}_sub_image(
296
+        {GLenum,GLuint} {target,texture},
297
+        Size            size,
298
+        Value const *   data,
299
+        GLenum          format = 0,
300
+        GLenum          type   = 0,
301
+        Offset          offset = {},
302
+        GLint           level  = 0
303
+    )
304
+    ```
305
+
306
+    Generalizations of `gl{Tex,Texture}SubImage{1,2,3}D`, where the latter uses
307
+    [Direct State Access][].
308
+
309
+-   ```cpp
310
+    void copy_{tex,texture}_sub_image(
311
+        {GLenum,GLuint} {target,texture},
312
+        CopySize        copy_size,
313
+        CopyOffset      copy_offset = {},
314
+        Offset          offset      = {},
315
+        GLint           level       = 0
316
+    )
317
+    ```
318
+
319
+    Generalizations of `glCopy{Tex,Texture}SubImage{1,2,3}D`, where the latter
320
+    uses [Direct State Access][].
321
+
322
+-   ```cpp
323
+    void compressed_tex_image(
324
+        GLenum       target,
325
+        Size         size,
326
+        GLenum       internal_format,
327
+        GLsizei      data_size = 0,
328
+        void const * data      = nullptr,
329
+        GLint        level     = 0,
330
+        GLint        border    = 0
331
+    )
332
+    ```
333
+
334
+    Generalization of `glCompressedTexImage{1,2,3}D`.
335
+
336
+-   ```cpp
337
+    void compressed_{tex,texture}_sub_image(
338
+        {GLenum,GLuint} {target,texture},
339
+        Size            size,
340
+        GLsizei         data_size,
341
+        void const *    data,
342
+        GLenum          internal_format,
343
+        Offset          offset = {},
344
+        GLint           level  = 0
345
+    )
346
+    ```
347
+
348
+    Generalizations of `glCompressed{Tex,Texture}SubImage{1,2,3}D`, where the
349
+    latter uses [Direct State Access][].
350
+
351
+For the member functions that are template parameterized on `Value`, when
352
+`internal_format`, `format` and/or `type` is `0` (the default), the value used
353
+will be taken from the corresponding `GLTraits::Value<Value>` member variable.
354
+
355
+Note that when the `Value` template type parameter is deduced from `data` the
356
+automatically chosen `format` may not be correct. E.g. it is reasonable for
357
+`data` to be a `GLubyte` pointer to RGBA values in which case `format` must be
358
+manually specified as `GL_RGBA` since the automatically chosen `format` would
359
+be `GL_RED`.
360
+
361
+Note that the [pixel transfer parameters][] are not modified, this must be
362
+handled manually if required.
363
+
364
+(There is no `copy_tex_image(...)` because OpenGL does not provide
365
+`glCopyTexImage3D`, only `glCopyTexImage{1,2}D`.)
366
+
367
+(There is no `{tex_image,tex_storage,texture_storage}_multisample(...)` because
368
+OpenGL does not provide `gl{TexImage,TexStorage,TextureStorage}1DMultisample`,
369
+only `gl{TexImage,TexStorage,TextureStorage}{2,3}DMultisample`.)
370
+
371
+(There is no `compressed_texture_image(...)` because OpenGL does not provide
372
+`glCompressedTextureImage{1,2,3}D`, only the `Sub` variants.)
373
+
374
+(There is no `framebuffer_texture(...)` because OpenGL provides incompatible
375
+signatures for `glFramebufferTexture1D` and `glFramebufferTexture{2,3}D`.)
376
+
377
+[image format]: https://www.khronos.org/opengl/wiki/Image_Format
378
+[color formats]: https://www.khronos.org/opengl/wiki/Image_Format#Color_formats
379
+[depth/stencil formats]: https://www.khronos.org/opengl/wiki/Image_Format#Depth_stencil_formats
380
+[depth formats]: https://www.khronos.org/opengl/wiki/Image_Format#Depth_formats
381
+[stencil formats]: https://www.khronos.org/opengl/wiki/Image_Format#Stencil_only
382
+[Direct State Access]: https://www.khronos.org/opengl/wiki/Direct_State_Access
383
+[immutable storage]: https://www.khronos.org/opengl/wiki/Texture_Storage#Immutable_storage
384
+[pixel transfer parameters]: https://www.khronos.org/opengl/wiki/Pixel_Transfer#Pixel_transfer_parameters
385
+
207 386
 ## Building
208 387
 
209 388
 See [`BUILDING.md`][].
Browse code

Add Object

Robert Cranston authored on 28/02/2023 02:19:27
Showing 1 changed files
... ...
@@ -154,6 +154,56 @@ alias for `glm::vec3`.
154 154
 [`glDisableVertexAttribArray`]: https://registry.khronos.org/OpenGL-Refpages/gl4/html/glEnableVertexAttribArray.xhtml
155 155
 [`glEnableVertexAttribArray`]: https://registry.khronos.org/OpenGL-Refpages/gl4/html/glEnableVertexAttribArray.xhtml
156 156
 
157
+### Object
158
+
159
+The [empty][] `struct GLTraits::Object<GLenum object_type>` is template
160
+specialized on the following values.
161
+
162
+-   `GL_TEXTURE`
163
+-   `GL_BUFFER`
164
+-   `GL_QUERY`
165
+-   `GL_PROGRAM`
166
+-   `GL_SHADER`
167
+-   `GL_VERTEX_ARRAY`
168
+-   `GL_FRAMEBUFFER`
169
+-   `GL_RENDERBUFFER`
170
+-   `GL_SAMPLER`
171
+-   `GL_TRANSFORM_FEEDBACK`
172
+-   `GL_PROGRAM_PIPELINE`
173
+
174
+`GLTraits::Object` contains the following `static constexpr` member variables.
175
+
176
+-   `char const name[]`
177
+
178
+`GLTraits::Object` contains the following `static` member functions.
179
+
180
+-   ```cpp
181
+    template<typename... Args>
182
+    void gen_objects(GLsizei n, GLuint * objects, Args... args)
183
+    ```
184
+
185
+    Generalization of `glGen*s` and `glCreate`.
186
+
187
+-   ```cpp
188
+    void delete_objects(GLsizei n, GLuint * objects)
189
+    ```
190
+
191
+    Generalization of `glDelete*s` and `glDelete*`.
192
+
193
+For `object_types` equal to  `GL_SHADER`, `GL_PROGRAM`, and
194
+`GL_PROGRAM_PIPELINE`, `GLTraits::Object` additionally contains the following
195
+`static` member functions.
196
+
197
+-   ```cpp
198
+    std::string info_log(GLuint object)
199
+    ```
200
+
201
+    Generalization of `glGet*InfoLog`.
202
+
203
+`GLTRAITS_OBJECT*` macros are defined to ease the definition of new template
204
+specializations of `GLTraits::Object`. Consult the source for the definitions
205
+and usage examples.
206
+
157 207
 ## Building
158 208
 
159 209
 See [`BUILDING.md`][].
Browse code

Add ValueID

Robert Cranston authored on 28/02/2023 02:16:10
Showing 1 changed files
... ...
@@ -80,6 +80,10 @@ on the following types.
80 80
 -   `GLenum       internal_format_compressed`
81 81
 -   `GLenum       internal_format_compressed_srgb`
82 82
 -   `bool         integer`
83
+-   `GLenum       id`
84
+
85
+`id` is guaranteed to be unique to this `Value` and is equal to `glsl` for all
86
+`Value`s except `GL{,u}{byte,short}`, for which it is equal to `type`.
83 87
 
84 88
 `GLTraits::Value` contains the following `static` member functions.
85 89
 
... ...
@@ -126,6 +130,20 @@ automatically by `vertex_attrib{,_pointer}(...)` above.
126 130
 specializations of `GLTraits::Value`. Consult the source for the definitions
127 131
 and usage examples.
128 132
 
133
+The [empty][] `struct GLTraits::ValueID<GLenum id>` is template specialized on
134
+the different values of `GLTraits::Value<Value>::id`.
135
+
136
+`GLTraits::ValueID` contains the following type definitions.
137
+
138
+-   `Value`. An alias for the template type parameter for which
139
+    `GLTraits::Value<Value>::id == id` holds.
140
+
141
+`GLTraits::ValueID` provides a compile time mapping from `GLenum id` back to
142
+the type `Value`. E.g. `GLTraits::ValueID<GL_FLOAT>::Value` is an alias for
143
+`GLfloat`. This works for all supported types, including those provided by
144
+[GLM][] if enabled, e.g. `GLTraits::ValueID<GL_FLOAT_VEC3>::Value` would be an
145
+alias for `glm::vec3`.
146
+
129 147
 [empty]: https://en.cppreference.com/w/cpp/types/is_empty
130 148
 [uniform]: https://www.khronos.org/opengl/wiki/Uniform_(GLSL)
131 149
 [shader]: https://www.khronos.org/opengl/wiki/Shader
Browse code

Add Value

Robert Cranston authored on 15/10/2022 19:02:09
Showing 1 changed files
... ...
@@ -13,6 +13,22 @@ brought to light. Therefore, it may be useful for learning and understanding
13 13
 the OpenGL API (although this use case may be limited due to the (ab)use of C++
14 14
 language features).
15 15
 
16
+This header-only library makes heavy use of macros. For easy inspection of the
17
+results, a script that runs the preprocessor is available in
18
+[`doc/preprocess`][] and its output in [`doc/preprocess.hpp`][].
19
+
20
+A typical use case is demonstrated by this quote from [OGLDEV on YouTube][]:
21
+
22
+> Notice, in order to make this an unsigned integer texture we use `GL_RGB32UI`
23
+> as the `internal_format`, `GL_RGB_INTEGER` as the `format`, and
24
+> `GL_UNSIGNED_INT` as the data `type`. Combining all these formats and types
25
+> correctly in OpenGL can often be a pain, and I literally pulled the last few
26
+> pieces of hair from my head trying to get this to work.
27
+
28
+These constants are provided in `GLTraits::Value<glm::uvec3>`. Of course, this
29
+becomes even more valuable when `glm::uvec3` is replaced by some unknown
30
+template parameter.
31
+
16 32
 [`gltraits`]: https://git.rcrnstn.net/rcrnstn/gltraits
17 33
 [C++11]: https://en.wikipedia.org/wiki/C++11
18 34
 [OpenGL]: https://en.wikipedia.org/wiki/OpenGL
... ...
@@ -26,6 +42,99 @@ language features).
26 42
 [version]: https://en.wikipedia.org/wiki/OpenGL#Version_history
27 43
 [extension]: https://www.khronos.org/opengl/wiki/OpenGL_Extension
28 44
 [GLSL]: https://www.khronos.org/opengl/wiki/OpenGL_Shading_Language
45
+[`doc/preprocess`]: doc/preprocess
46
+[`doc/preprocess.hpp`]: doc/preprocess.hpp
47
+[OGLDEV on YouTube]: https://www.youtube.com/watch?v=71G-PVpaVk8&t=5m17s
48
+
49
+## Usage
50
+
51
+### Value
52
+
53
+The [empty][] `struct GLTraits::Value<typename Value>` is template specialized
54
+on the following types.
55
+
56
+-   `GLfloat`
57
+-   `bool`
58
+-   `GL{,u}byte`
59
+-   `GL{,u}short`
60
+-   `GL{,u}int`
61
+-   `GLdouble`
62
+
63
+[GLM][] support is enabled if `glm/glm.hpp` is included (specifically, if
64
+`GLM_VERSION` is defined) before inclusion of `gltraits.hpp`. In that case
65
+`GLTraits::Value` is additionally template specialized on the following types.
66
+
67
+-   `glm::{,i,u,d}vec{2,3,4}`
68
+-   `glm::{,d}mat{2{,x3,x4},3{x2,,x4},4{x2,x3,}}`
69
+
70
+`GLTraits::Value` contains the following `static constexpr` member variables.
71
+
72
+-   `char   const name[]`
73
+-   `GLint        columns`
74
+-   `GLint        rows`
75
+-   `GLenum       glsl`
76
+-   `GLenum       format`
77
+-   `GLenum       type`
78
+-   `GLenum       internal_format`
79
+-   `GLenum       internal_format_srgb`
80
+-   `GLenum       internal_format_compressed`
81
+-   `GLenum       internal_format_compressed_srgb`
82
+-   `bool         integer`
83
+
84
+`GLTraits::Value` contains the following `static` member functions.
85
+
86
+-   ```cpp
87
+    void uniform(GLint location, Value const & value)
88
+    ```
89
+
90
+    Generalization of `glUniform*`.
91
+
92
+    Uploads `value` to the [uniform][] indicated by `location` of the current
93
+    [shader][] program.
94
+
95
+-   ```cpp
96
+    void vertex_attrib(GLint location, Value const & value)
97
+    ```
98
+
99
+    Generalization of `glVertexAttrib*`.
100
+
101
+    Uploads `value` to the [non-array attribute][] indicated by `location`.
102
+    Note that [`glDisableVertexAttribArray`][] is not called (for performance).
103
+
104
+-   ```cpp
105
+    void vertex_attrib_pointer(
106
+        GLint       location,
107
+        std::size_t offset = 0,
108
+        std::size_t stride = sizeof(Value)
109
+    )
110
+    ```
111
+
112
+    Generalization of `glVertexAttrib*Pointer`.
113
+
114
+    Sets the [format][] as well as the [offset and stride][] of the [array
115
+    attribute][] indicated by `location`. Note that
116
+    [`glEnableVertexAttribArray`][] is not called (for performance).
117
+
118
+If `location` is `-1`, the above calls will do nothing. No error will be
119
+generated in this case.
120
+
121
+Note that matrix types (e.g. from [GLM][], if enabled), occupy several
122
+consecutive attribute locations (one per column), which are all handled
123
+automatically by `vertex_attrib{,_pointer}(...)` above.
124
+
125
+`GLTRAITS_VALUE*` macros are defined to ease the definition of new template
126
+specializations of `GLTraits::Value`. Consult the source for the definitions
127
+and usage examples.
128
+
129
+[empty]: https://en.cppreference.com/w/cpp/types/is_empty
130
+[uniform]: https://www.khronos.org/opengl/wiki/Uniform_(GLSL)
131
+[shader]: https://www.khronos.org/opengl/wiki/Shader
132
+[non-array attribute]: https://www.khronos.org/opengl/wiki/Vertex_Specification#Non-array_attribute_values
133
+[format]: https://www.khronos.org/opengl/wiki/Vertex_Specification#Vertex_format
134
+[offset and stride]: https://www.khronos.org/opengl/wiki/Vertex_Specification#Vertex_buffer_offset_and_stride
135
+[array attribute]: https://www.khronos.org/opengl/wiki/Vertex_Specification#Vertex_Buffer_Object
136
+[`glDisableVertexAttribArray`]: https://registry.khronos.org/OpenGL-Refpages/gl4/html/glEnableVertexAttribArray.xhtml
137
+[`glEnableVertexAttribArray`]: https://registry.khronos.org/OpenGL-Refpages/gl4/html/glEnableVertexAttribArray.xhtml
29 138
 
30 139
 ## Building
31 140
 
Browse code

Add project

Robert Cranston authored on 14/10/2022 21:57:18
Showing 1 changed files
... ...
@@ -27,6 +27,12 @@ language features).
27 27
 [extension]: https://www.khronos.org/opengl/wiki/OpenGL_Extension
28 28
 [GLSL]: https://www.khronos.org/opengl/wiki/OpenGL_Shading_Language
29 29
 
30
+## Building
31
+
32
+See [`BUILDING.md`][].
33
+
34
+[`BUILDING.md`]: BUILDING.md
35
+
30 36
 ## License
31 37
 
32 38
 Licensed under the [ISC License][] unless otherwise noted, see the
Browse code

Add license

Robert Cranston authored on 14/10/2022 21:54:35
Showing 1 changed files
... ...
@@ -26,3 +26,11 @@ language features).
26 26
 [version]: https://en.wikipedia.org/wiki/OpenGL#Version_history
27 27
 [extension]: https://www.khronos.org/opengl/wiki/OpenGL_Extension
28 28
 [GLSL]: https://www.khronos.org/opengl/wiki/OpenGL_Shading_Language
29
+
30
+## License
31
+
32
+Licensed under the [ISC License][] unless otherwise noted, see the
33
+[`LICENSE`][] file.
34
+
35
+[ISC License]: https://choosealicense.com/licenses/isc
36
+[`LICENSE`]: LICENSE
Browse code

Add readme

Robert Cranston authored on 14/10/2022 21:54:22
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,28 @@
1
+# [`gltraits`][]
2
+
3
+A [C++11][]/[OpenGL][]>=[1.0][](/[GLM][]) [trait][]s library.
4
+
5
+This library seeks to unify some parts of the OpenGL [API][] to ease [generic
6
+programming][]. It also provides sensible default arguments, [optional][debug]
7
+[check][]s for [version][]/[extension][] support, and optional support for
8
+[OpenGL Mathematics (GLM)][GLM] (which provides [GLSL][]-like types).
9
+
10
+A more philosophical description: it aims to make the implicit symmetries of
11
+the OpenGL API explicit. In the process, some wrinkles in the symmetry are also
12
+brought to light. Therefore, it may be useful for learning and understanding
13
+the OpenGL API (although this use case may be limited due to the (ab)use of C++
14
+language features).
15
+
16
+[`gltraits`]: https://git.rcrnstn.net/rcrnstn/gltraits
17
+[C++11]: https://en.wikipedia.org/wiki/C++11
18
+[OpenGL]: https://en.wikipedia.org/wiki/OpenGL
19
+[1.0]: https://en.wikipedia.org/wiki/OpenGL#Version_history
20
+[GLM]: https://glm.g-truc.net
21
+[trait]: https://en.wikipedia.org/wiki/Trait_(computer_programming)
22
+[API]: https://en.wikipedia.org/wiki/API
23
+[generic programming]: https://en.wikipedia.org/wiki/Generic_programming
24
+[debug]: https://git.rcrnstn.net/rcrnstn/glbase#debug
25
+[check]: https://git.rcrnstn.net/rcrnstn/glbase#check
26
+[version]: https://en.wikipedia.org/wiki/OpenGL#Version_history
27
+[extension]: https://www.khronos.org/opengl/wiki/OpenGL_Extension
28
+[GLSL]: https://www.khronos.org/opengl/wiki/OpenGL_Shading_Language