1 /+
2 The MIT License (MIT)
3 
4     Copyright (c) <2013> <Oleg Butko (deviator), Anton Akzhigitov (Akzwar)>
5 
6     Permission is hereby granted, free of charge, to any person obtaining a copy
7     of this software and associated documentation files (the "Software"), to deal
8     in the Software without restriction, including without limitation the rights
9     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10     copies of the Software, and to permit persons to whom the Software is
11     furnished to do so, subject to the following conditions:
12 
13     The above copyright notice and this permission notice shall be included in
14     all copies or substantial portions of the Software.
15 
16     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22     THE SOFTWARE.
23 +/
24 
25 module des.gl.base.type;
26 
27 import std.stdio;
28 import std..string;
29 
30 import derelict.opengl3.gl3;
31 
32 public import des.math.linear.vector;
33 public import des.util.arch;
34 public import des.util.logsys;
35 
36 ///
37 class DesGLException : Exception 
38 {
39     ///
40     this( string msg, string file=__FILE__, size_t line=__LINE__ ) pure nothrow @safe
41     { super( msg, file, line ); } 
42 }
43 
44 ///
45 enum GLType
46 {
47     UNSIGNED_BYTE  = GL_UNSIGNED_BYTE,  /// `GL_UNSIGNED_BYTE`
48     BYTE           = GL_BYTE,           /// `GL_BYTE`
49     UNSIGNED_SHORT = GL_UNSIGNED_SHORT, /// `GL_UNSIGNED_SHORT`
50     SHORT          = GL_SHORT,          /// `GL_SHORT`
51     UNSIGNED_INT   = GL_UNSIGNED_INT,   /// `GL_UNSIGNED_INT`
52     INT            = GL_INT,            /// `GL_INT`
53     FLOAT          = GL_FLOAT,          /// `GL_FLOAT`
54 }
55 
56 ///
57 size_t sizeofGLType( GLType type )
58 {
59     final switch(type)
60     {
61     case GLType.BYTE:
62     case GLType.UNSIGNED_BYTE:
63         return byte.sizeof;
64 
65     case GLType.SHORT:
66     case GLType.UNSIGNED_SHORT:
67         return short.sizeof;
68 
69     case GLType.INT:
70     case GLType.UNSIGNED_INT:
71         return int.sizeof;
72 
73     case GLType.FLOAT:
74         return float.sizeof;
75     }
76 }
77 
78 ///
79 GLType toGLType(T)() @property
80 {
81     static if( is( T == ubyte ) )
82         return GLType.UNSIGNED_BYTE;
83     else static if( is( T == byte ) )
84         return GLType.BYTE;
85     else static if( is( T == ushort ) )
86         return GLType.UNSIGNED_SHORT;
87     else static if( is( T == short ) )
88         return GLType.SHORT;
89     else static if( is( T == uint ) )
90         return GLType.UNSIGNED_INT;
91     else static if( is( T == int ) )
92         return GLType.INT;
93     else static if( is( T == float ) )
94         return GLType.FLOAT;
95     else
96     {
97         pragma(msg, "no GLType for ", T );
98         static assert(0);
99     }
100 }
101 
102 ///
103 unittest
104 {
105     assert( toGLType!ubyte == GLType.UNSIGNED_BYTE );
106     assert( toGLType!byte == GLType.BYTE );
107     assert( toGLType!ushort == GLType.UNSIGNED_SHORT );
108     assert( toGLType!short == GLType.SHORT );
109     assert( toGLType!uint == GLType.UNSIGNED_INT );
110     assert( toGLType!int == GLType.INT );
111     assert( toGLType!float == GLType.FLOAT );
112 }
113 
114 ///
115 enum GLError
116 {
117     NO                = GL_NO_ERROR,          /// `GL_NO_ERROR`
118     INVALID_ENUM      = GL_INVALID_ENUM,      /// `GL_INVALID_ENUM`
119     INVALID_VALUE     = GL_INVALID_VALUE,     /// `GL_INVALID_VALUE`
120     INVALID_OPERATION = GL_INVALID_OPERATION, /// `GL_INVALID_OPERATION`
121     STACK_OVERFLOW    = 0x0503,               /// `0x0503`
122     STACK_UNDERFLOW   = 0x0504,               /// `0x0504`
123     OUT_OF_MEMORY     = GL_OUT_OF_MEMORY,     /// `GL_OUT_OF_MEMORY`
124     INVALID_FRAMEBUFFER_OPERATION = 0x0506    /// `0x0506`
125 }
126 
127 /// glGetError, if has error throw exception
128 void checkGL( string file=__FILE__, size_t line=__LINE__ )()
129 {
130     GLError err = cast(GLError)glGetError();
131 
132     if( err != GLError.NO )
133         throw new DesGLException( format("%s", err), file, line );
134 }
135 
136 /// glGetError, no throw exception, output to logger error
137 void ntCheckGL( string file=__FILE__, size_t line=__LINE__ )() nothrow
138 {
139     try checkGL!(file,line);
140     catch( DesGLException e )
141         logger.error( ntFormat( "GL ERROR at [%s:%d] %s", e.file, e.line, e.msg ) );
142     catch( Exception e )
143         logger.error( ntFormat( "[%s:%d] %s", e.file, e.line, e.msg ) );
144 }
145 
146 /// call `checkGL` after function call
147 template checkGLCall(alias fnc, string file=__FILE__, size_t line=__LINE__, Args...)
148 {
149     auto checkGLCall(Args...)( Args args )
150     {
151         scope(exit) debug checkGL!(file,line);
152         static if( is( typeof(fnc(args)) == void ) ) fnc( args );
153         else return fnc( args );
154     }
155 }
156 
157 /// call `ntCheckGL` after function call
158 template ntCheckGLCall(alias fnc, string file=__FILE__, size_t line=__LINE__, Args...)
159 {
160     auto ntCheckGLCall(Args...)( Args args ) nothrow
161     {
162         scope(exit) debug ntCheckGL!(file,line);
163         static if( is( typeof(fnc(args)) == void ) ) fnc( args );
164         else return fnc( args );
165     }
166 }