1 /++
2 Module for loading a library of open graphics.
3 
4 Macros:
5     LREF = <a href="#$1">$1</a>
6     HREF = <a href="$1">$2</a>
7 
8 Authors: $(HREF https://github.com/TodNaz,TodNaz)
9 Copyright: Copyright (c) 2020 - 2021, TodNaz.
10 License: $(HREF https://github.com/TodNaz/Tida/blob/master/LICENSE,MIT)
11 +/
12 module tida.gl;
13 
14 public import bindbc.opengl;
15 
16 __gshared int[2] _glVersionSpecifed;
17 __gshared string _glslVersion;
18 
19 @property int[2] glVersionSpecifed() @trusted
20 {
21     return _glVersionSpecifed;
22 }
23 
24 @property string glslVersion() @trusted
25 {
26     return _glslVersion;
27 }
28 
29 /++
30 The function loads the `OpenGL` libraries for hardware graphics acceleration.
31 
32 Throws:
33 `Exception` if the library was not found or the context was not created to 
34 implement hardware acceleration.
35 +/
36 void loadGraphicsLibrary() @trusted
37 {
38     import std.exception : enforce;
39     import std.conv : to;
40 
41     bool valid(GLSupport value) {
42         return value != GLSupport.noContext &&
43                value != GLSupport.badLibrary &&
44                value != GLSupport.noLibrary; 
45     }
46 
47     GLSupport retValue = loadOpenGL();
48     enforce!Exception(valid(retValue), 
49     "The library was not loaded or the context was not created!");
50 
51     glGetIntegerv(GL_MAJOR_VERSION, &_glVersionSpecifed[0]);
52     glGetIntegerv(GL_MINOR_VERSION, &_glVersionSpecifed[1]);
53    _glslVersion = glGetString(GL_SHADING_LANGUAGE_VERSION).to!string[0 .. 4];
54 }
55 
56 alias ExtList = string[];
57 
58 enum Extensions : string
59 {
60     textureCompression = "GL_ARB_texture_compression",
61     textureArray = "GL_EXT_texture_array"
62 }
63 
64 bool hasExtensions(ExtList list, string name) @trusted
65 {
66     import std.algorithm : canFind;
67 
68     if (list.length == 0)
69         list = glExtensionsList();
70 
71     return list.canFind(name);
72 }
73 
74 ExtList glExtensionsList() @trusted
75 {
76     import std.conv : to;
77     import std..string : split;
78 
79     int numExtensions = 0;
80     glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions);
81 
82     string[] extensions;
83 
84     foreach (i; 0 .. numExtensions)
85     {
86         extensions ~= glGetStringi(GL_EXTENSIONS, i).to!string;
87     }
88 
89     return extensions;
90 }
91 
92 // Texture compressed
93 alias FCompressedTexImage2DARB = void function(GLenum target,
94                                                int level,
95                                                GLenum internalformat,
96                                                GLsizei width,
97                                                GLsizei height,
98                                                int border,
99                                                GLsizei imagesize,
100                                                void* data);
101 
102 __gshared
103 {
104     FCompressedTexImage2DARB glCompressedTexImage2DARB;
105 }
106 
107 alias glCompressedTexImage2D = glCompressedTexImage2DARB;
108 
109 enum
110 {
111     GL_COMPRESSED_RGBA_ARB = 0x84EE,
112     GL_COMPRESSED_RGB_ARB = 0x84ED,
113     GL_COMPRESSED_ALPHA_ARB = 0x84E9,
114 
115     GL_TEXTURE_COMPRESSION_HINT_ARB = 0x84EF,
116     GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB = 0x86A0,
117     GL_TEXTURE_COMPRESSED_ARB = 0x86A1,
118     GL_NUM_COMPRESSED_TEXTURE_FORMATS_ARB = 0x86A2,
119     GL_COMPRESSED_TEXTURE_FORMATS_ARB = 0x86A3
120 }
121 
122 bool extTextureCompressionLoad() @trusted
123 {
124     import bindbc.opengl.util;
125 
126     if (!hasExtensions(null, "GL_ARB_texture_compression"))
127         return false;
128 
129     if (!loadExtendedGLSymbol(  cast(void**) &glCompressedTexImage2DARB,
130                                 "glCompressedTexImage2DARB"))
131         return false;
132 
133     return true;
134 }
135 
136 // Texture array ext 
137 alias FFramebufferTextureLayerEXT = void function();
138 
139 __gshared
140 {
141     FFramebufferTextureLayerEXT glFramebufferTextureLayerEXT;
142 }
143 
144 enum
145 {
146     GL_TEXTURE_1D_ARRAY_EX = 0x8C18,
147     GL_TEXTURE_2D_ARRAY_EXT = 0x8C1A,
148 
149     GL_TEXTURE_BINDING_1D_ARRAY_EXT = 0x8C1C,
150     GL_TEXTURE_BINDING_2D_ARRAY_EXT = 0x8C1D,
151     GL_MAX_ARRAY_TEXTURE_LAYERS_EXT = 0x88FF,
152     GL_COMPARE_REF_DEPTH_TO_TEXTURE_EXT = 0x884E
153 }
154 
155 bool extTextureArrayLoad() @trusted
156 {
157     import bindbc.opengl.util;
158 
159     if (!hasExtensions(null, Extensions.textureArray))
160         return false;
161 
162     // glFramebufferTextureLayerEXT
163     if (!loadExtendedGLSymbol(cast(void**) &glFramebufferTextureLayerEXT,
164                               "glFramebufferTextureLayerEXT"))
165         return false;
166 
167     return true;
168 }