Fix T54105: random walk SSS missing in branched indirect paths.
[blender-staging.git] / intern / gpudirect / dvpapi.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2015, Blender Foundation
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Blender Foundation.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file gpudirect/dvpapi.c
29  *  \ingroup gpudirect
30  */
31
32 #ifdef WIN32
33
34 #include <stdlib.h>
35 #include "dvpapi.h"
36
37 extern "C" {
38 #include "BLI_dynlib.h"
39 }
40
41 #define KDVPAPI_Name "dvp.dll"
42
43 typedef DVPStatus (DVPAPIENTRY * PFNDVPINITGLCONTEXT) (uint32_t flags);
44 typedef DVPStatus (DVPAPIENTRY * PFNDVPCLOSEGLCONTEXT) (void);
45 typedef DVPStatus (DVPAPIENTRY * PFNDVPGETLIBRARYVERSION)(uint32_t *major, uint32_t *minor);
46
47 static uint32_t __dvpMajorVersion = 0;
48 static uint32_t __dvpMinorVersion = 0;
49 static PFNDVPGETLIBRARYVERSION __dvpGetLibrayVersion = NULL;
50 static PFNDVPINITGLCONTEXT __dvpInitGLContext = NULL;
51 static PFNDVPCLOSEGLCONTEXT __dvpCloseGLContext = NULL;
52 PFNDVPBEGIN __dvpBegin = NULL;
53 PFNDVPEND __dvpEnd = NULL;
54 PFNDVPCREATEBUFFER __dvpCreateBuffer = NULL;
55 PFNDVPDESTROYBUFFER __dvpDestroyBuffer = NULL;
56 PFNDVPFREEBUFFER __dvpFreeBuffer = NULL;
57 PFNDVPMEMCPYLINED __dvpMemcpyLined = NULL;
58 PFNDVPMEMCPY __dvpMemcpy = NULL;
59 PFNDVPIMPORTSYNCOBJECT __dvpImportSyncObject = NULL;
60 PFNDVPFREESYNCOBJECT __dvpFreeSyncObject = NULL;
61 PFNDVPMAPBUFFERENDAPI __dvpMapBufferEndAPI = NULL;
62 PFNDVPMAPBUFFERWAITDVP __dvpMapBufferWaitDVP = NULL;
63 PFNDVPMAPBUFFERENDDVP __dvpMapBufferEndDVP = NULL;
64 PFNDVPMAPBUFFERWAITAPI __dvpMapBufferWaitAPI = NULL;
65 PFNDVPBINDTOGLCTX __dvpBindToGLCtx = NULL;
66 PFNDVPGETREQUIREDCONSTANTSGLCTX __dvpGetRequiredConstantsGLCtx = NULL;
67 PFNDVPCREATEGPUTEXTUREGL __dvpCreateGPUTextureGL = NULL;
68 PFNDVPUNBINDFROMGLCTX __dvpUnbindFromGLCtx = NULL;
69
70 static DynamicLibrary *__dvpLibrary = NULL;
71
72 DVPStatus dvpGetLibrayVersion(uint32_t *major, uint32_t *minor)
73 {
74         if (!__dvpLibrary)
75                 return DVP_STATUS_ERROR;
76         *major = __dvpMajorVersion;
77         *minor = __dvpMinorVersion;
78         return DVP_STATUS_OK;
79 }
80
81 DVPStatus dvpInitGLContext(uint32_t flags)
82 {
83         DVPStatus status;
84         if (!__dvpLibrary) {
85                 __dvpLibrary = BLI_dynlib_open(KDVPAPI_Name);
86                 if (!__dvpLibrary) {
87                         return DVP_STATUS_ERROR;
88                 }
89 //              "?dvpInitGLContext@@YA?AW4DVPStatus@@I@Z";
90                 __dvpInitGLContext = (PFNDVPINITGLCONTEXT)BLI_dynlib_find_symbol(__dvpLibrary, "?dvpInitGLContext@@YA?AW4DVPStatus@@I@Z");
91                 __dvpCloseGLContext = (PFNDVPCLOSEGLCONTEXT)BLI_dynlib_find_symbol(__dvpLibrary, "?dvpCloseGLContext@@YA?AW4DVPStatus@@XZ");
92                 __dvpGetLibrayVersion = (PFNDVPGETLIBRARYVERSION)BLI_dynlib_find_symbol(__dvpLibrary, "?dvpGetLibrayVersion@@YA?AW4DVPStatus@@PEAI0@Z");
93                 __dvpBegin = (PFNDVPBEGIN)BLI_dynlib_find_symbol(__dvpLibrary, "?dvpBegin@@YA?AW4DVPStatus@@XZ");
94                 __dvpEnd = (PFNDVPEND)BLI_dynlib_find_symbol(__dvpLibrary, "?dvpEnd@@YA?AW4DVPStatus@@XZ");
95                 __dvpCreateBuffer = (PFNDVPCREATEBUFFER)BLI_dynlib_find_symbol(__dvpLibrary, "?dvpCreateBuffer@@YA?AW4DVPStatus@@PEAUDVPSysmemBufferDescRec@@PEA_K@Z");
96                 __dvpDestroyBuffer = (PFNDVPDESTROYBUFFER)BLI_dynlib_find_symbol(__dvpLibrary, "?dvpDestroyBuffer@@YA?AW4DVPStatus@@_K@Z");
97                 __dvpFreeBuffer = (PFNDVPFREEBUFFER)BLI_dynlib_find_symbol(__dvpLibrary, "?dvpFreeBuffer@@YA?AW4DVPStatus@@_K@Z");
98                 __dvpMemcpyLined = (PFNDVPMEMCPYLINED)BLI_dynlib_find_symbol(__dvpLibrary, "?dvpMemcpyLined@@YA?AW4DVPStatus@@_K0I000III@Z");
99                 __dvpMemcpy = (PFNDVPMEMCPY)BLI_dynlib_find_symbol(__dvpLibrary, "?dvpMemcpy2D@@YA?AW4DVPStatus@@_K0I000IIIII@Z");
100                 __dvpImportSyncObject = (PFNDVPIMPORTSYNCOBJECT)BLI_dynlib_find_symbol(__dvpLibrary, "?dvpImportSyncObject@@YA?AW4DVPStatus@@PEAUDVPSyncObjectDescRec@@PEA_K@Z");
101                 __dvpFreeSyncObject = (PFNDVPFREESYNCOBJECT)BLI_dynlib_find_symbol(__dvpLibrary, "?dvpFreeSyncObject@@YA?AW4DVPStatus@@_K@Z");
102                 __dvpMapBufferEndAPI = (PFNDVPMAPBUFFERENDAPI)BLI_dynlib_find_symbol(__dvpLibrary, "?dvpMapBufferEndAPI@@YA?AW4DVPStatus@@_K@Z");
103                 __dvpMapBufferWaitDVP = (PFNDVPMAPBUFFERWAITDVP)BLI_dynlib_find_symbol(__dvpLibrary, "?dvpMapBufferWaitDVP@@YA?AW4DVPStatus@@_K@Z");
104                 __dvpMapBufferEndDVP = (PFNDVPMAPBUFFERENDDVP)BLI_dynlib_find_symbol(__dvpLibrary, "?dvpMapBufferEndDVP@@YA?AW4DVPStatus@@_K@Z");
105                 __dvpMapBufferWaitAPI = (PFNDVPMAPBUFFERWAITAPI)BLI_dynlib_find_symbol(__dvpLibrary, "?dvpMapBufferWaitAPI@@YA?AW4DVPStatus@@_K@Z");
106                 __dvpBindToGLCtx = (PFNDVPBINDTOGLCTX)BLI_dynlib_find_symbol(__dvpLibrary, "?dvpBindToGLCtx@@YA?AW4DVPStatus@@_K@Z");
107                 __dvpGetRequiredConstantsGLCtx = (PFNDVPGETREQUIREDCONSTANTSGLCTX)BLI_dynlib_find_symbol(__dvpLibrary, "?dvpGetRequiredConstantsGLCtx@@YA?AW4DVPStatus@@PEAI00000@Z");
108                 __dvpCreateGPUTextureGL = (PFNDVPCREATEGPUTEXTUREGL)BLI_dynlib_find_symbol(__dvpLibrary, "?dvpCreateGPUTextureGL@@YA?AW4DVPStatus@@IPEA_K@Z");
109                 __dvpUnbindFromGLCtx = (PFNDVPUNBINDFROMGLCTX)BLI_dynlib_find_symbol(__dvpLibrary, "?dvpUnbindFromGLCtx@@YA?AW4DVPStatus@@_K@Z");
110                 if (!__dvpInitGLContext ||
111                         !__dvpCloseGLContext ||
112                         !__dvpGetLibrayVersion ||
113                         !__dvpBegin ||
114                         !__dvpEnd ||
115                         !__dvpCreateBuffer ||
116                         !__dvpDestroyBuffer ||
117                         !__dvpFreeBuffer ||
118                         !__dvpMemcpyLined ||
119                         !__dvpMemcpy ||
120                         !__dvpImportSyncObject ||
121                         !__dvpFreeSyncObject ||
122                         !__dvpMapBufferEndAPI ||
123                         !__dvpMapBufferWaitDVP ||
124                         !__dvpMapBufferEndDVP ||
125                         !__dvpMapBufferWaitAPI ||
126                         !__dvpBindToGLCtx ||
127                         !__dvpGetRequiredConstantsGLCtx ||
128                         !__dvpCreateGPUTextureGL ||
129                         !__dvpUnbindFromGLCtx)
130                 {
131                         return DVP_STATUS_ERROR;
132                 }
133                 // check that the library version is what we want
134                 if ((status = __dvpGetLibrayVersion(&__dvpMajorVersion, &__dvpMinorVersion)) != DVP_STATUS_OK)
135                         return status;
136                 if (__dvpMajorVersion != DVP_MAJOR_VERSION || __dvpMinorVersion < DVP_MINOR_VERSION)
137                         return DVP_STATUS_ERROR;
138         }
139         return (!__dvpInitGLContext) ? DVP_STATUS_ERROR : __dvpInitGLContext(flags);
140 }
141
142 DVPStatus dvpCloseGLContext(void)
143 {
144         return (!__dvpCloseGLContext) ? DVP_STATUS_ERROR : __dvpCloseGLContext();
145 }
146
147 #endif // WIN32