1 2 module dglsl.gspl; 3 4 import derelict.opengl3.gl3; 5 6 import std..string; 7 8 import dglsl.type; 9 10 /// glUniform 11 void glUniform(GLint location, GLfloat v0) { 12 glUniform1f(location, v0); 13 } 14 15 void glUniform(GLint location, GLfloat v0, GLfloat v1) { 16 glUniform2f(location, v0, v1); 17 } 18 19 void glUniform(GLint location, GLfloat v0, GLfloat v1, GLfloat v2) { 20 glUniform3f(location, v0, v1, v2); 21 } 22 23 void glUniform(GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) { 24 glUniform4f(location, v0, v1, v2, v3); 25 } 26 27 void glUniform(GLint location, GLint v0) { 28 glUniform1i(location, v0); 29 } 30 31 void glUniform(GLint location, GLint v0, GLint v1) { 32 glUniform2i(location, v0, v1); 33 } 34 35 void glUniform(GLint location, GLint v0, GLint v1, GLint v2) { 36 glUniform3i(location, v0, v1, v2); 37 } 38 39 void glUniform(GLint location, GLint v0, GLint v1, GLint v2, GLint v3) { 40 glUniform4i(location, v0, v1, v2, v3); 41 } 42 43 void glUniform1v(GLint location, GLsizei count, const GLfloat[] value) { 44 glUniform1fv(location, count, value.ptr); 45 } 46 47 void glUniform2v(GLint location, GLsizei count, const GLfloat[] value) { 48 glUniform2fv(location, count, value.ptr); 49 } 50 51 void glUniform3v(GLint location, GLsizei count, const GLfloat[] value) { 52 glUniform3fv(location, count, value.ptr); 53 } 54 55 void glUniform4v(GLint location, GLsizei count, const GLfloat[] value) { 56 glUniform4fv(location, count, value.ptr); 57 } 58 59 void glUniform1v(GLint location, GLsizei count, const GLint[] value) { 60 glUniform1iv(location, count, value.ptr); 61 } 62 63 void glUniform2v(GLint location, GLsizei count, const GLint[] value) { 64 glUniform2iv(location, count, value.ptr); 65 } 66 67 void glUniform3v(GLint location, GLsizei count, const GLint[] value) { 68 glUniform3iv(location, count, value.ptr); 69 } 70 71 void glUniform4v(GLint location, GLsizei count, const GLint[] value) { 72 glUniform4iv(location, count, value.ptr); 73 } 74 75 void glUniformMatrix(GLint location, GLboolean transpose, const GLfloat[4] value) { 76 glUniformMatrix2fv(location, 1, transpose, value.ptr); 77 } 78 79 void glUniformMatrix(GLint location, GLboolean transpose, const GLfloat[9] value) { 80 glUniformMatrix3fv(location, 1, transpose, value.ptr); 81 } 82 83 void glUniformMatrix(GLint location, GLboolean transpose, const GLfloat[16] value) { 84 glUniformMatrix4fv(location, 1, transpose, value.ptr); 85 } 86 87 void glUniform(int dim)(GLint location, Vector!(float, dim) v) { 88 mixin(`glUniform%dfv(location, 1, v.value_ptr);`.format(dim)); 89 } 90 91 void glUniform(int dim)(GLint location, Matrix!(float, dim, dim) m, GLboolean transpose = false) { 92 mixin(`glUniformMatrix%dfv(location, 1, transpose, m.value_ptr);`.format(dim)); 93 } 94 95 void glUniform(int column, int row)(GLint location, Matrix!(float, column, row) m, GLboolean transpose = false) if (column != row) { 96 import std..string; 97 mixin(`glUniformMatrix%dx%dfv(location, 1, transpose, m.value_ptr);`.format(column, row)); 98 } 99 100 101 enum GLErrorCode { 102 noError = GL_NO_ERROR, 103 invalidEnum = GL_INVALID_ENUM, 104 invalidValue = GL_INVALID_VALUE, 105 invalidOperation = GL_INVALID_OPERATION, 106 invalidFramebufferOperation = GL_INVALID_FRAMEBUFFER_OPERATION, 107 outOfMemory = GL_OUT_OF_MEMORY, 108 } 109 110 111 // glGetError 112 void glCheckError(string file = __FILE__, int line = __LINE__) { 113 debug final switch (cast(GLErrorCode)(glGetError())) { 114 case GLErrorCode.noError: 115 break; 116 case GLErrorCode.invalidEnum: 117 throw new Exception("Invalid enum error: An unacceptable value is specified for an enumerated argument.", file, line); 118 case GLErrorCode.invalidValue: 119 throw new Exception("Invalid value error: A numeric argument is out of range.", file, line); 120 case GLErrorCode.invalidOperation: 121 throw new Exception("Invalid operation error: The specified operation is not allowed in the current state.", file, line); 122 case GLErrorCode.invalidFramebufferOperation: 123 throw new Exception("Invalid framebuffer operation error: The framebuffer object is not complete.", file, line); 124 case GLErrorCode.outOfMemory: 125 throw new Exception("Out of memory error: There is not enough memory left to execute the command.", file, line); 126 } 127 } 128 129 130 // opengl device information 131 void glShowInfo() { 132 import std.stdio; 133 import std.conv; 134 GLint i; 135 136 writefln("Version: %s", glGetString(GL_VERSION).to!string); 137 writefln("Vendor: %s", glGetString(GL_VENDOR).to!string); 138 writefln("Renderer: %s", glGetString(GL_RENDERER).to!string); 139 writefln("GLSL Version: %s", glGetString(GL_SHADING_LANGUAGE_VERSION).to!string); 140 141 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &i); 142 writefln("Max texture size (RGBA32): %d * %d", i / 4, i / 4); 143 144 stdout.flush(); 145 }