1
1

util.d 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. module three.gl.util;
  2. public import derelict.opengl3.gl3;
  3. import std.traits : ReturnType;
  4. ReturnType!func glCheck(alias func, string file = __FILE__, size_t line = __LINE__, string mod = __MODULE__, string funcd = __FUNCTION__, string pretty = __PRETTY_FUNCTION__, Args...)(Args args) nothrow {
  5. import std.stdio;
  6. import std.stdio : stderr;
  7. import std.array : join;
  8. import std.range : repeat;
  9. import std.string : format;
  10. try{
  11. debug scope(exit) {
  12. GLenum err = glGetError();
  13. if(err != GL_NO_ERROR) {
  14. stderr.writeln("\n===============================");
  15. stderr.writeln("File: ", file, "\nLine: ", line, "\nModule: ",mod, "\nFunction: ",funcd, "\n",pretty);
  16. stderr.writeln("-------------------------------");
  17. stderr.writefln(`OpenGL function "%s(%s)" failed: "%s."`, func.stringof, format("%s".repeat(Args.length).join(", "), args), glErrorString(err));
  18. stderr.writeln("=============================== \n");
  19. assert(false);
  20. }
  21. }
  22. }
  23. catch(Exception e){
  24. }
  25. debug if(func is null) {
  26. try{
  27. stderr.writefln("%s is null! OpenGL loaded? Required OpenGL version not supported?".format(func.stringof));
  28. }
  29. catch(Exception e){
  30. assert(false);
  31. }
  32. assert(false);
  33. }
  34. return func(args);
  35. }
  36. string glErrorString(GLenum error) pure @safe nothrow @nogc {
  37. final switch(error) {
  38. case GL_NO_ERROR: return "no error";
  39. case GL_INVALID_ENUM: return "invalid enum";
  40. case GL_INVALID_VALUE: return "invalid value";
  41. case GL_INVALID_OPERATION: return "invalid operation";
  42. //case GL_STACK_OVERFLOW: return "stack overflow";
  43. //case GL_STACK_UNDERFLOW: return "stack underflow";
  44. case GL_INVALID_FRAMEBUFFER_OPERATION: return "invalid framebuffer operation";
  45. case GL_OUT_OF_MEMORY: return "out of memory";
  46. }
  47. assert(false, "invalid enum");
  48. }
  49. //==============================================================================
  50. ///
  51. template toGlType(T) {
  52. static if(is(T == byte)) {
  53. enum toGlType = GL_BYTE;
  54. } else static if(is(T == ubyte)) {
  55. enum toGlType = GL_UNSIGNED_BYTE;
  56. } else static if(is(T == short)) {
  57. enum toGlType = GL_SHORT;
  58. } else static if(is(T == ushort)) {
  59. enum toGlType = GL_UNSIGNED_SHORT;
  60. } else static if(is(T == int)) {
  61. enum toGlType = GL_INT;
  62. } else static if(is(T == uint)) {
  63. enum toGlType = GL_UNSIGNED_INT;
  64. } else static if(is(T == float)) {
  65. enum toGlType = GL_FLOAT;
  66. } else static if(is(T == double)) {
  67. enum toGlType = GL_DOUBLE;
  68. } else {
  69. static assert(false, T.stringof ~ " cannot be represented as GLenum");
  70. }
  71. }
  72. //==============================================================================
  73. ///
  74. template sizeofGlType(GLenum t) {
  75. static if(t == GL_BYTE) {
  76. enum sizeofGlType = byte.sizeof;
  77. } else static if(t == GL_UNSIGNED_BYTE) {
  78. enum sizeofGlType = ubyte.sizeof;
  79. } else static if(t == GL_SHORT) {
  80. enum sizeofGlType = short.sizeof;
  81. } else static if(t == GL_UNSIGNED_SHORT) {
  82. enum sizeofGlType = ushort.sizeof;
  83. } else static if(t == GL_INT) {
  84. enum sizeofGlType = int.sizeof;
  85. } else static if(t == GL_UNSIGNED_INT) {
  86. enum sizeofGlType = uint.sizeof;
  87. } else static if(t == GL_FLOAT) {
  88. enum sizeofGlType = float.sizeof;
  89. } else static if(t == GL_DOUBLE) {
  90. enum sizeofGlType = double.sizeof;
  91. } else {
  92. static assert(false, T.stringof ~ " cannot be represented as D-Type");
  93. }
  94. }