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.vulkan.vk_backend;
33 
34 import mango_engine.mango;
35 import mango_engine.logging;
36 import mango_engine.graphics.backend;
37 
38 version(mango_VKBackend) import erupted;
39 
40 import derelict.glfw3;
41 import derelict.freeimage.freeimage;
42 
43 version(mango_VKBackend) mixin DerelictGLFW3_VulkanBind;
44 
45 package void checkSupport() @safe { // Check if we were compiled with Vulkan support.
46     if(!mango_hasVKSupport()) {
47         throw new Exception("Mango-Engine was not compiled with Vulkan Support!");
48     }
49 }
50 
51 class VKBackend : Backend {
52 
53     this(Logger logger) @safe {
54         super(logger);
55     }
56 
57     override {
58         void loadLibraries(in string[string] args = null) @system {
59             checkSupport();
60             
61             loadGLFW();
62             loadFI();
63         }
64 
65         void doInit() @system {
66             if(!glfwInit()) {
67                 // GLFW failed to initalize
68                 throw new LibraryLoadException("GLFW", "glfwInit() Failed!");
69             }
70 
71             if(!glfwVulkanSupported()) {
72                 throw new BackendException("GLFW: Vulkan is not supported on this system!");
73             }
74         }
75     }
76     
77     /*
78     private shared void loadVulkan() @system {
79         try {
80             
81         } catch(Exception e) {
82             throw new LibraryLoadException("Vulkan", e.toString());
83         }
84     }
85     */
86     
87     private void loadGLFW() @system { // Load code for GLFW
88         version(Windows) {
89             //------------------------------- Windows Load Code
90             try {
91                 DerelictGLFW3.load("lib\\glfw3.dll");
92                 version(mango_VKBackend) DerelictGLFW3_loadVulkan();
93             } catch(Exception e) {
94                 throw new LibraryLoadException("GLFW", e.toString());
95             }
96             //------------------------------- End Windows Load Code
97         } else { // All other OS
98             try {
99                 DerelictGLFW3.load();
100                 version(mango_VKBackend) DerelictGLFW3_loadVulkan();
101             } catch(Exception e) {
102                 throw new LibraryLoadException("GLFW", e.toString());
103             }
104         }
105     }
106 
107     private void loadFI() @system { // Load code for FreeImage
108         version(Windows) {
109             //------------------------------- Windows Load Code
110             try {
111                 DerelictFI.load("lib\\FreeImage.dll");
112             } catch(Exception e) {
113                 throw new LibraryLoadException("FreeImage", e.toString());
114             }
115             //------------------------------- End Windows Load Code
116         } else { // All other OS
117             try {
118                 DerelictFI.load();
119             } catch(Exception e) {
120                 throw new LibraryLoadException("FreeImage", e.toString());
121             }
122         }
123     }
124 }