1 /*
2  *  BSD 3-Clause License
3  *  
4  *  Copyright (c) 2016, Mango-Engine Team
5  *  All rights reserved.
6  *  
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions are met:
9  *  
10  *  * Redistributions of source code must retain the above copyright notice, this
11  *    list of conditions and the following disclaimer.
12  *  
13  *  * Redistributions in binary form must reproduce the above copyright notice,
14  *    this list of conditions and the following disclaimer in the documentation
15  *    and/or other materials provided with the distribution.
16  *  
17  *  * Neither the name of the copyright holder nor the names of its
18  *    contributors may be used to endorse or promote products derived from
19  *    this software without specific prior written permission.
20  *  
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 module mango_engine.graphics.backend;
33 
34 import mango_engine.logging;
35 
36 /++
37     Factory generation mixin for base classes.
38     This generates the interface code to construct
39     the backend varient of the class depending on
40     which backend is selected, and what backends
41     were compiled in.
42 +/
43 template GenFactory(string object_) {
44     const char[] GenFactory = "
45     version(mango_GLBackend) {
46         if(backend == GraphicsBackendType.API_OPENGL)
47             return new GL" ~ object_ ~ "();
48     }
49     /*
50     version(mango_VKBackend) {
51         if(backend == GraphicsBackendType.API_VULKAN)
52             return new VK" ~ object_ ~ "();
53     }
54     */
55     throw new Exception(\"No backends avaliable, was it compiled in?\");
56     ";
57 }
58 
59 /++
60     Factory generation mixin for base classes.
61     This generates the interface code to construct
62     the backend varient of the class depending on
63     which backend is selected, and what backends
64     were compiled in.
65 +/
66 template GenFactory(string object_, string params) {
67     const char[] GenFactory = "
68     version(mango_GLBackend) {
69         if(backend == GraphicsBackendType.API_OPENGL)
70             return new GL" ~ object_ ~ "(" ~ params ~ ");
71     }
72     /*
73     version(mango_VKBackend) {
74         if(backend == GraphicsBackendType.API_VULKAN)
75             return new VK" ~ object_ ~ "(" ~ params ~ ");
76     }
77     */
78     throw new Exception(\"No backends avaliable, was it compiled in?\");
79     ";
80 }
81 
82 /++
83     Base class for a video backend implementation.
84     This class handles loading system libraries required
85     by the backend, and prepares the libraries to be
86     used.
87 
88     All backends must extend this class.
89 +/
90 abstract class Backend {
91     protected shared Logger _logger;
92 
93     @property Logger logger() @trusted nothrow { return cast(Logger) _logger; }
94 
95     this(Logger logger) @trusted nothrow {
96         this._logger = cast(shared) logger;
97     }
98 
99     /++
100         Load system libraries required by the backend.
101         Additional options can be passed.
102 
103         Params:
104             args =  Can be any additional options passed to the
105                     backend. Consult the backend documentation
106                     for information on keys and values.
107     +/
108     abstract void loadLibraries(in string[string] args = null) @system;
109 
110     /// Call any initialization code required by the Backend. May be overriden.
111     void doInit() @system {
112         
113     }
114 
115     /// Call any de-initialization code required by the Backend. May be overriden.
116     void doDestroy() @system {
117 
118     }
119 }
120 
121 /// LibraryLoadException is called when a system library fails to load.
122 class LibraryLoadException : Exception {
123     /// Default constructor
124     this(in string library, in string message) {
125         super("Failed to laod library \"" ~ library ~ "\": " ~ message);
126     }
127 }
128 
129 /// BackendException is called when there is a failure in the backend.
130 class BackendException : Exception {
131     /// Default constructor
132     this(in string message) {
133         super(message);
134     }
135 }