1 module gfx.gl3.context;
2 
3 import gfx.bindings.opengl.gl : Gl;
4 import gfx.core.rc : AtomicRefCounted;
5 import gfx.graal.format : Format;
6 
7 enum GlProfile
8 {
9     core,
10     compatibility
11 }
12 
13 struct GlAttribs
14 {
15     enum profile = GlProfile.core;
16     enum doublebuffer = true;
17 
18     uint majorVersion = 3;
19     uint minorVersion = 1;
20 
21     uint samples = 1;
22 
23     @property Format colorFormat() const pure {
24         return _colorFormat;
25     }
26     @property Format depthStencilFormat() const pure {
27         return _depthStencilFormat;
28     }
29 
30     private Format _colorFormat = Format.rgba8_uNorm;
31     private Format _depthStencilFormat = Format.d24s8_uNorm;
32 
33     @property uint decimalVersion() const pure {
34         return majorVersion * 10 + minorVersion;
35     }
36 }
37 
38 immutable uint[] glVersions = [
39     46, 45, 44, 43, 42, 41, 40,
40     33, 32, 31, // 30
41     // 21, 20,
42     // 15, 14, 13, 12, 11, 10,
43 ];
44 
45 uint glslVersion(in uint glVersion) pure {
46     if (glVersion >= 33) return 10*glVersion;
47     switch(glVersion) {
48     case 32:    return 150;
49     case 31:    return 140;
50     case 30:    return 130;
51     case 21:    return 120;
52     case 20:    return 110;
53     default:    assert(false);
54     }
55 }
56 
57 interface GlContext : AtomicRefCounted
58 {
59     @property Gl gl();
60 
61     @property GlAttribs attribs();
62 
63     bool makeCurrent(size_t nativeHandle);
64 
65     void doneCurrent();
66 
67     @property bool current();
68 
69     @property int swapInterval()
70     in { assert(current); }
71 
72     @property void swapInterval(int interval)
73     in { assert(current); }
74 
75     void swapBuffers(size_t nativeHandle)
76     in { assert(current); }
77 
78     size_t createDummy();
79     void releaseDummy(size_t dummy);
80 }
81 
82 immutable string[] glRequiredExtensions = [
83     "GL_ARB_framebuffer_object"
84 ];
85 immutable string[] glOptionalExtensions = [
86     "GL_ARB_buffer_storage",
87     "GL_ARB_texture_storage",
88     "GL_ARB_sampler_object"
89 ];
90 
91 string[] glAvailableExtensions(Gl gl)
92 {
93     import gfx.bindings.opengl.gl : GLint, GL_EXTENSIONS, GL_NUM_EXTENSIONS;
94 
95     GLint num;
96     gl.GetIntegerv(GL_NUM_EXTENSIONS, &num);
97     string[] exts;
98     foreach (i; 0 .. num)
99     {
100         import std.string : fromStringz;
101         auto cStr = cast(const(char)*)gl.GetStringi(GL_EXTENSIONS, i);
102         exts ~= fromStringz(cStr).idup;
103     }
104     return exts;
105 }