From 68bb0c88ba7d427550dd9ac5a796ff2d6d05262d Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Mon, 4 Mar 2019 22:54:00 -0800 Subject: Updated GL engine factory headers and structures --- Graphics/GraphicsEngineOpenGL/CMakeLists.txt | 5 +- .../interface/EngineFactoryOpenGL.h | 123 +++++++++++ .../interface/EngineGLAttribs.h | 48 ---- .../interface/RenderDeviceFactoryOpenGL.h | 124 ----------- .../src/EngineFactoryOpenGL.cpp | 243 +++++++++++++++++++++ .../src/RenderDeviceFactoryOpenGL.cpp | 243 --------------------- 6 files changed, 368 insertions(+), 418 deletions(-) create mode 100644 Graphics/GraphicsEngineOpenGL/interface/EngineFactoryOpenGL.h delete mode 100644 Graphics/GraphicsEngineOpenGL/interface/EngineGLAttribs.h delete mode 100644 Graphics/GraphicsEngineOpenGL/interface/RenderDeviceFactoryOpenGL.h create mode 100644 Graphics/GraphicsEngineOpenGL/src/EngineFactoryOpenGL.cpp delete mode 100644 Graphics/GraphicsEngineOpenGL/src/RenderDeviceFactoryOpenGL.cpp (limited to 'Graphics/GraphicsEngineOpenGL') diff --git a/Graphics/GraphicsEngineOpenGL/CMakeLists.txt b/Graphics/GraphicsEngineOpenGL/CMakeLists.txt index bfde3d05..6f69366b 100644 --- a/Graphics/GraphicsEngineOpenGL/CMakeLists.txt +++ b/Graphics/GraphicsEngineOpenGL/CMakeLists.txt @@ -39,10 +39,9 @@ set(INTERFACE interface/BufferGL.h interface/BufferViewGL.h interface/DeviceContextGL.h - interface/EngineGLAttribs.h + interface/EngineFactoryOpenGL.h interface/FenceGL.h interface/PipelineStateGL.h - interface/RenderDeviceFactoryOpenGL.h interface/RenderDeviceGL.h interface/SamplerGL.h interface/ShaderGL.h @@ -56,6 +55,7 @@ set(SOURCE src/BufferGLImpl.cpp src/BufferViewGLImpl.cpp src/DeviceContextGLImpl.cpp + src/EngineFactoryOpenGL.cpp src/FBOCache.cpp src/FenceGLImpl.cpp src/GLContextState.cpp @@ -64,7 +64,6 @@ set(SOURCE src/GLProgramResources.cpp src/GLTypeConversions.cpp src/PipelineStateGLImpl.cpp - src/RenderDeviceFactoryOpenGL.cpp src/RenderDeviceGLImpl.cpp src/SamplerGLImpl.cpp src/ShaderGLImpl.cpp diff --git a/Graphics/GraphicsEngineOpenGL/interface/EngineFactoryOpenGL.h b/Graphics/GraphicsEngineOpenGL/interface/EngineFactoryOpenGL.h new file mode 100644 index 00000000..63627856 --- /dev/null +++ b/Graphics/GraphicsEngineOpenGL/interface/EngineFactoryOpenGL.h @@ -0,0 +1,123 @@ +/* Copyright 2015-2019 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#pragma once + +/// \file +/// Declaration of functions that create OpenGL-based engine implementation + +#include + +#include "../../GraphicsEngine/interface/RenderDevice.h" +#include "../../GraphicsEngine/interface/DeviceContext.h" +#include "../../GraphicsEngine/interface/SwapChain.h" + +#include "../../HLSL2GLSLConverterLib/interface/HLSL2GLSLConverter.h" + + +#if PLATFORM_ANDROID || PLATFORM_LINUX || PLATFORM_MACOS || PLATFORM_IOS || (PLATFORM_WIN32 && !defined(_MSC_VER)) + + // https://gcc.gnu.org/wiki/Visibility +# define API_QUALIFIER __attribute__((visibility("default"))) + +#elif PLATFORM_WIN32 + +# define API_QUALIFIER + +#else +# error Unsupported platform +#endif + +namespace Diligent +{ + +class IEngineFactoryOpenGL +{ +public: + virtual void CreateDeviceAndSwapChainGL(const EngineGLCreateInfo& EngineCI, + IRenderDevice** ppDevice, + IDeviceContext** ppImmediateContext, + const SwapChainDesc& SCDesc, + ISwapChain** ppSwapChain ) = 0; + virtual void CreateHLSL2GLSLConverter(IHLSL2GLSLConverter** ppConverter) = 0; + + virtual void AttachToActiveGLContext(const EngineGLCreateInfo& EngineCI, + IRenderDevice** ppDevice, + IDeviceContext** ppImmediateContext) = 0; +}; + + +#if ENGINE_DLL && PLATFORM_WIN32 && defined(_MSC_VER) + +# define EXPLICITLY_LOAD_ENGINE_GL_DLL 1 + + typedef IEngineFactoryOpenGL* (*GetEngineFactoryOpenGLType)(); + + static bool LoadGraphicsEngineOpenGL(GetEngineFactoryOpenGLType& GetFactoryFunc) + { + GetFactoryFunc = nullptr; + std::string LibName = "GraphicsEngineOpenGL_"; + +# if _WIN64 + LibName += "64"; +# else + LibName += "32"; +# endif + +# ifdef _DEBUG + LibName += "d"; +# else + LibName += "r"; +# endif + + LibName += ".dll"; + auto hModule = LoadLibraryA( LibName.c_str() ); + if( hModule == NULL ) + { + std::stringstream ss; + ss << "Failed to load " << LibName << " library.\n"; + OutputDebugStringA(ss.str().c_str()); + return false; + } + + GetFactoryFunc = reinterpret_cast( GetProcAddress(hModule, "GetEngineFactoryOpenGL") ); + if( GetFactoryFunc == NULL ) + { + std::stringstream ss; + ss << "Failed to load GetEngineFactoryOpenGL() from " << LibName << " library.\n"; + OutputDebugStringA(ss.str().c_str()); + FreeLibrary( hModule ); + return false; + } + return true; + } + +#else + + // Do not forget to call System.loadLibrary("GraphicsEngineOpenGL") in Java on Android! + API_QUALIFIER + IEngineFactoryOpenGL* GetEngineFactoryOpenGL(); + +#endif + +} diff --git a/Graphics/GraphicsEngineOpenGL/interface/EngineGLAttribs.h b/Graphics/GraphicsEngineOpenGL/interface/EngineGLAttribs.h deleted file mode 100644 index 89fd0590..00000000 --- a/Graphics/GraphicsEngineOpenGL/interface/EngineGLAttribs.h +++ /dev/null @@ -1,48 +0,0 @@ -/* Copyright 2015-2019 Egor Yusov - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. - * - * In no event and under no legal theory, whether in tort (including negligence), - * contract, or otherwise, unless required by applicable law (such as deliberate - * and grossly negligent acts) or agreed to in writing, shall any Contributor be - * liable for any damages, including any direct, indirect, special, incidental, - * or consequential damages of any character arising as a result of this License or - * out of the use or inability to use the software (including but not limited to damages - * for loss of goodwill, work stoppage, computer failure or malfunction, or any and - * all other commercial damages or losses), even if such Contributor has been advised - * of the possibility of such damages. - */ - -#pragma once - -/// \file -/// Definition of the Engine OpenGL/GLES attribs - -#include "../../GraphicsEngine/interface/GraphicsTypes.h" - -namespace Diligent -{ - /// Attributes of the OpenGL-based engine implementation - struct EngineGLAttribs : public EngineCreationAttribs - { - /// Native window handle - - /// * On Win32 platform, this is a window handle (HWND) - /// * On Android platform, this is a pointer to the native window (ANativeWindow*) - /// * On Linux, this is the native window (Window) - void *pNativeWndHandle = nullptr; - -#if PLATFORM_LINUX - /// For linux platform only, this is the pointer to the display - void *pDisplay = nullptr; -#endif - }; -} diff --git a/Graphics/GraphicsEngineOpenGL/interface/RenderDeviceFactoryOpenGL.h b/Graphics/GraphicsEngineOpenGL/interface/RenderDeviceFactoryOpenGL.h deleted file mode 100644 index 93b8d8f4..00000000 --- a/Graphics/GraphicsEngineOpenGL/interface/RenderDeviceFactoryOpenGL.h +++ /dev/null @@ -1,124 +0,0 @@ -/* Copyright 2015-2019 Egor Yusov - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. - * - * In no event and under no legal theory, whether in tort (including negligence), - * contract, or otherwise, unless required by applicable law (such as deliberate - * and grossly negligent acts) or agreed to in writing, shall any Contributor be - * liable for any damages, including any direct, indirect, special, incidental, - * or consequential damages of any character arising as a result of this License or - * out of the use or inability to use the software (including but not limited to damages - * for loss of goodwill, work stoppage, computer failure or malfunction, or any and - * all other commercial damages or losses), even if such Contributor has been advised - * of the possibility of such damages. - */ - -#pragma once - -/// \file -/// Declaration of functions that create OpenGL-based engine implementation - -#include - -#include "../../GraphicsEngine/interface/RenderDevice.h" -#include "../../GraphicsEngine/interface/DeviceContext.h" -#include "../../GraphicsEngine/interface/SwapChain.h" - -#include "../../HLSL2GLSLConverterLib/interface/HLSL2GLSLConverter.h" - -#include "EngineGLAttribs.h" - -#if PLATFORM_ANDROID || PLATFORM_LINUX || PLATFORM_MACOS || PLATFORM_IOS || (PLATFORM_WIN32 && !defined(_MSC_VER)) - - // https://gcc.gnu.org/wiki/Visibility -# define API_QUALIFIER __attribute__((visibility("default"))) - -#elif PLATFORM_WIN32 - -# define API_QUALIFIER - -#else -# error Unsupported platform -#endif - -namespace Diligent -{ - -class IEngineFactoryOpenGL -{ -public: - virtual void CreateDeviceAndSwapChainGL(const EngineGLAttribs& CreationAttribs, - IRenderDevice **ppDevice, - IDeviceContext **ppImmediateContext, - const SwapChainDesc& SCDesc, - ISwapChain **ppSwapChain ) = 0; - virtual void CreateHLSL2GLSLConverter(IHLSL2GLSLConverter **ppConverter) = 0; - - virtual void AttachToActiveGLContext( const EngineGLAttribs& CreationAttribs, - IRenderDevice **ppDevice, - IDeviceContext **ppImmediateContext ) = 0; -}; - - -#if ENGINE_DLL && PLATFORM_WIN32 && defined(_MSC_VER) - -# define EXPLICITLY_LOAD_ENGINE_GL_DLL 1 - - typedef IEngineFactoryOpenGL* (*GetEngineFactoryOpenGLType)(); - - static bool LoadGraphicsEngineOpenGL(GetEngineFactoryOpenGLType &GetFactoryFunc) - { - GetFactoryFunc = nullptr; - std::string LibName = "GraphicsEngineOpenGL_"; - -# if _WIN64 - LibName += "64"; -# else - LibName += "32"; -# endif - -# ifdef _DEBUG - LibName += "d"; -# else - LibName += "r"; -# endif - - LibName += ".dll"; - auto hModule = LoadLibraryA( LibName.c_str() ); - if( hModule == NULL ) - { - std::stringstream ss; - ss << "Failed to load " << LibName << " library.\n"; - OutputDebugStringA(ss.str().c_str()); - return false; - } - - GetFactoryFunc = reinterpret_cast( GetProcAddress(hModule, "GetEngineFactoryOpenGL") ); - if( GetFactoryFunc == NULL ) - { - std::stringstream ss; - ss << "Failed to load GetEngineFactoryOpenGL() from " << LibName << " library.\n"; - OutputDebugStringA(ss.str().c_str()); - FreeLibrary( hModule ); - return false; - } - return true; - } - -#else - - // Do not forget to call System.loadLibrary("GraphicsEngineOpenGL") in Java on Android! - API_QUALIFIER - IEngineFactoryOpenGL* GetEngineFactoryOpenGL(); - -#endif - -} diff --git a/Graphics/GraphicsEngineOpenGL/src/EngineFactoryOpenGL.cpp b/Graphics/GraphicsEngineOpenGL/src/EngineFactoryOpenGL.cpp new file mode 100644 index 00000000..04d1ce83 --- /dev/null +++ b/Graphics/GraphicsEngineOpenGL/src/EngineFactoryOpenGL.cpp @@ -0,0 +1,243 @@ +/* Copyright 2015-2019 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +/// \file +/// Routines that initialize OpenGL/GLES-based engine implementation + +#include "pch.h" +#include "RenderDeviceFactoryOpenGL.h" +#include "RenderDeviceGLImpl.h" +#include "DeviceContextGLImpl.h" +#include "EngineMemory.h" +#include "HLSL2GLSLConverterObject.h" + +#if PLATFORM_IOS +# include "SwapChainGLIOS.h" +#else +# include "SwapChainGLImpl.h" +#endif + +#if PLATFORM_ANDROID +# include "RenderDeviceGLESImpl.h" +#endif + +namespace Diligent +{ + +#if PLATFORM_WIN32 || PLATFORM_UNIVERSAL_WINDOWS || PLATFORM_LINUX || PLATFORM_MACOS + typedef RenderDeviceGLImpl TRenderDeviceGLImpl; + typedef SwapChainGLImpl TSwapChain; +#elif PLATFORM_ANDROID + typedef RenderDeviceGLESImpl TRenderDeviceGLImpl; + typedef SwapChainGLImpl TSwapChain; +#elif PLATFORM_IOS + typedef RenderDeviceGLImpl TRenderDeviceGLImpl; + typedef SwapChainGLIOS TSwapChain; +#else +# error Unsupported platform +#endif + +/// Engine factory for OpenGL implementation +class EngineFactoryOpenGLImpl : public IEngineFactoryOpenGL +{ +public: + static EngineFactoryOpenGLImpl* GetInstance() + { + static EngineFactoryOpenGLImpl TheFactory; + return &TheFactory; + } + + virtual void CreateDeviceAndSwapChainGL(const EngineGLCreateInfo& EngineCI, + IRenderDevice** ppDevice, + IDeviceContext** ppImmediateContext, + const SwapChainDesc& SCDesc, + ISwapChain** ppSwapChain )override final; + + virtual void CreateHLSL2GLSLConverter(IHLSL2GLSLConverter** ppConverter)override final; + + virtual void AttachToActiveGLContext(const EngineGLCreateInfo& EngineCI, + IRenderDevice** ppDevice, + IDeviceContext** ppImmediateContext )override final; +}; + + + +/// Creates render device, device context and swap chain for OpenGL/GLES-based engine implementation + +/// \param [in] EngineCI - Engine creation attributes. +/// \param [out] ppDevice - Address of the memory location where pointer to +/// the created device will be written. +/// \param [out] ppImmediateContext - Address of the memory location where pointers to +/// the immediate context will be written. +/// \param [in] SCDesc - Swap chain description. +/// \param [out] ppSwapChain - Address of the memory location where pointer to the new +/// swap chain will be written. +void EngineFactoryOpenGLImpl::CreateDeviceAndSwapChainGL(const EngineGLCreateInfo& EngineCI, + IRenderDevice** ppDevice, + IDeviceContext** ppImmediateContext, + const SwapChainDesc& SCDesc, + ISwapChain** ppSwapChain) +{ + if (EngineCI.DebugMessageCallback != nullptr) + SetDebugMessageCallback(EngineCI.DebugMessageCallback); + + VERIFY( ppDevice && ppImmediateContext && ppSwapChain, "Null pointer provided" ); + if( !ppDevice || !ppImmediateContext || !ppSwapChain ) + return; + + *ppDevice = nullptr; + *ppImmediateContext = nullptr; + *ppSwapChain = nullptr; + + try + { + SetRawAllocator(EngineCI.pRawMemAllocator); + auto &RawMemAllocator = GetRawAllocator(); + + RenderDeviceGLImpl *pRenderDeviceOpenGL( NEW_RC_OBJ(RawMemAllocator, "TRenderDeviceGLImpl instance", TRenderDeviceGLImpl)(RawMemAllocator, EngineCI) ); + pRenderDeviceOpenGL->QueryInterface(IID_RenderDevice, reinterpret_cast(ppDevice) ); + + DeviceContextGLImpl *pDeviceContextOpenGL( NEW_RC_OBJ(RawMemAllocator, "DeviceContextGLImpl instance", DeviceContextGLImpl)(pRenderDeviceOpenGL, false ) ); + // We must call AddRef() (implicitly through QueryInterface()) because pRenderDeviceOpenGL will + // keep a weak reference to the context + pDeviceContextOpenGL->QueryInterface(IID_DeviceContext, reinterpret_cast(ppImmediateContext) ); + pRenderDeviceOpenGL->SetImmediateContext(pDeviceContextOpenGL); + + TSwapChain *pSwapChainGL = NEW_RC_OBJ(RawMemAllocator, "SwapChainGLImpl instance", TSwapChain)(EngineCI, SCDesc, pRenderDeviceOpenGL, pDeviceContextOpenGL ); + pSwapChainGL->QueryInterface(IID_SwapChain, reinterpret_cast(ppSwapChain) ); + + pDeviceContextOpenGL->SetSwapChain(pSwapChainGL); + // Bind default framebuffer and viewport + pDeviceContextOpenGL->SetRenderTargets( 0, nullptr, nullptr, RESOURCE_STATE_TRANSITION_MODE_TRANSITION ); + pDeviceContextOpenGL->SetViewports( 1, nullptr, 0, 0 ); + } + catch( const std::runtime_error & ) + { + if( *ppDevice ) + { + (*ppDevice)->Release(); + *ppDevice = nullptr; + } + + if( *ppImmediateContext ) + { + (*ppImmediateContext)->Release(); + *ppImmediateContext = nullptr; + } + + if( *ppSwapChain ) + { + (*ppSwapChain)->Release(); + *ppSwapChain = nullptr; + } + + LOG_ERROR( "Failed to initialize OpenGL-based render device" ); + } +} + + +/// Creates render device, device context and attaches to existing GL context + +/// \param [in] EngineCI - Engine creation attributes. +/// \param [out] ppDevice - Address of the memory location where pointer to +/// the created device will be written. +/// \param [out] ppImmediateContext - Address of the memory location where pointers to +/// the immediate context will be written. +void EngineFactoryOpenGLImpl::AttachToActiveGLContext(const EngineGLCreateInfo& EngineCI, + IRenderDevice** ppDevice, + IDeviceContext** ppImmediateContext ) +{ + if (EngineCI.DebugMessageCallback != nullptr) + SetDebugMessageCallback(EngineCI.DebugMessageCallback); + + VERIFY( ppDevice && ppImmediateContext, "Null pointer provided" ); + if( !ppDevice || !ppImmediateContext ) + return; + + *ppDevice = nullptr; + *ppImmediateContext = nullptr; + + try + { + SetRawAllocator(EngineCI.pRawMemAllocator); + auto &RawMemAllocator = GetRawAllocator(); + + RenderDeviceGLImpl *pRenderDeviceOpenGL( NEW_RC_OBJ(RawMemAllocator, "TRenderDeviceGLImpl instance", TRenderDeviceGLImpl)(RawMemAllocator, EngineCI) ); + pRenderDeviceOpenGL->QueryInterface(IID_RenderDevice, reinterpret_cast(ppDevice) ); + + DeviceContextGLImpl *pDeviceContextOpenGL( NEW_RC_OBJ(RawMemAllocator, "DeviceContextGLImpl instance", DeviceContextGLImpl)(pRenderDeviceOpenGL, false ) ); + // We must call AddRef() (implicitly through QueryInterface()) because pRenderDeviceOpenGL will + // keep a weak reference to the context + pDeviceContextOpenGL->QueryInterface(IID_DeviceContext, reinterpret_cast(ppImmediateContext) ); + pRenderDeviceOpenGL->SetImmediateContext(pDeviceContextOpenGL); + } + catch( const std::runtime_error & ) + { + if( *ppDevice ) + { + (*ppDevice)->Release(); + *ppDevice = nullptr; + } + + if( *ppImmediateContext ) + { + (*ppImmediateContext)->Release(); + *ppImmediateContext = nullptr; + } + + LOG_ERROR( "Failed to initialize OpenGL-based render device" ); + } +} + +#ifdef DOXYGEN +/// Loads OpenGL-based engine implementation and exports factory functions +/// \param [out] GetFactoryFunc - Pointer to the function that returns pointer to the factory for +/// the OpenGL engine implementation +/// See EngineFactoryOpenGLImpl::CreateDeviceAndSwapChainGL(). +/// \remarks Depending on the configuration and platform, the function loads different dll: +/// Platform\\Configuration | Debug | Release +/// --------------------------|------------------------------|---------------------------- +/// Win32/x86 | GraphicsEngineOpenGL_32d.dll | GraphicsEngineOpenGL_32r.dll +/// Win32/x64 | GraphicsEngineOpenGL_64d.dll | GraphicsEngineOpenGL_64r.dll +/// +/// To load the library on Android, it is necessary to call System.loadLibrary("GraphicsEngineOpenGL") from Java. +void LoadGraphicsEngineOpenGL(GetEngineFactoryOpenGLType &GetFactoryFunc) +{ + // This function is only required because DoxyGen refuses to generate documentation for a static function when SHOW_FILES==NO + #error This function must never be compiled; +} +#endif + +void EngineFactoryOpenGLImpl::CreateHLSL2GLSLConverter(IHLSL2GLSLConverter** ppConverter) +{ + HLSL2GLSLConverterObject *pConverter( NEW_RC_OBJ(GetRawAllocator(), "HLSL2GLSLConverterObject instance", HLSL2GLSLConverterObject)() ); + pConverter->QueryInterface( IID_HLSL2GLSLConverter, reinterpret_cast(ppConverter) ); +} + +API_QUALIFIER +Diligent::IEngineFactoryOpenGL* GetEngineFactoryOpenGL() +{ + return Diligent::EngineFactoryOpenGLImpl::GetInstance(); +} + +} diff --git a/Graphics/GraphicsEngineOpenGL/src/RenderDeviceFactoryOpenGL.cpp b/Graphics/GraphicsEngineOpenGL/src/RenderDeviceFactoryOpenGL.cpp deleted file mode 100644 index 474f914e..00000000 --- a/Graphics/GraphicsEngineOpenGL/src/RenderDeviceFactoryOpenGL.cpp +++ /dev/null @@ -1,243 +0,0 @@ -/* Copyright 2015-2019 Egor Yusov - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. - * - * In no event and under no legal theory, whether in tort (including negligence), - * contract, or otherwise, unless required by applicable law (such as deliberate - * and grossly negligent acts) or agreed to in writing, shall any Contributor be - * liable for any damages, including any direct, indirect, special, incidental, - * or consequential damages of any character arising as a result of this License or - * out of the use or inability to use the software (including but not limited to damages - * for loss of goodwill, work stoppage, computer failure or malfunction, or any and - * all other commercial damages or losses), even if such Contributor has been advised - * of the possibility of such damages. - */ - -/// \file -/// Routines that initialize OpenGL/GLES-based engine implementation - -#include "pch.h" -#include "RenderDeviceFactoryOpenGL.h" -#include "RenderDeviceGLImpl.h" -#include "DeviceContextGLImpl.h" -#include "EngineMemory.h" -#include "HLSL2GLSLConverterObject.h" - -#if PLATFORM_IOS -# include "SwapChainGLIOS.h" -#else -# include "SwapChainGLImpl.h" -#endif - -#if PLATFORM_ANDROID -# include "RenderDeviceGLESImpl.h" -#endif - -namespace Diligent -{ - -#if PLATFORM_WIN32 || PLATFORM_UNIVERSAL_WINDOWS || PLATFORM_LINUX || PLATFORM_MACOS - typedef RenderDeviceGLImpl TRenderDeviceGLImpl; - typedef SwapChainGLImpl TSwapChain; -#elif PLATFORM_ANDROID - typedef RenderDeviceGLESImpl TRenderDeviceGLImpl; - typedef SwapChainGLImpl TSwapChain; -#elif PLATFORM_IOS - typedef RenderDeviceGLImpl TRenderDeviceGLImpl; - typedef SwapChainGLIOS TSwapChain; -#else -# error Unsupported platform -#endif - -/// Engine factory for OpenGL implementation -class EngineFactoryOpenGLImpl : public IEngineFactoryOpenGL -{ -public: - static EngineFactoryOpenGLImpl* GetInstance() - { - static EngineFactoryOpenGLImpl TheFactory; - return &TheFactory; - } - - virtual void CreateDeviceAndSwapChainGL(const EngineGLAttribs& CreationAttribs, - IRenderDevice **ppDevice, - IDeviceContext **ppImmediateContext, - const SwapChainDesc& SCDesc, - ISwapChain **ppSwapChain )override final; - - virtual void CreateHLSL2GLSLConverter(IHLSL2GLSLConverter **ppConverter)override final; - - virtual void AttachToActiveGLContext( const EngineGLAttribs& CreationAttribs, - IRenderDevice **ppDevice, - IDeviceContext **ppImmediateContext )override final; -}; - - - -/// Creates render device, device context and swap chain for OpenGL/GLES-based engine implementation - -/// \param [in] CreationAttribs - Engine creation attributes. -/// \param [out] ppDevice - Address of the memory location where pointer to -/// the created device will be written. -/// \param [out] ppImmediateContext - Address of the memory location where pointers to -/// the immediate context will be written. -/// \param [in] SCDesc - Swap chain description. -/// \param [out] ppSwapChain - Address of the memory location where pointer to the new -/// swap chain will be written. -void EngineFactoryOpenGLImpl::CreateDeviceAndSwapChainGL(const EngineGLAttribs& CreationAttribs, - IRenderDevice **ppDevice, - IDeviceContext **ppImmediateContext, - const SwapChainDesc& SCDesc, - ISwapChain **ppSwapChain ) -{ - if (CreationAttribs.DebugMessageCallback != nullptr) - SetDebugMessageCallback(CreationAttribs.DebugMessageCallback); - - VERIFY( ppDevice && ppImmediateContext && ppSwapChain, "Null pointer provided" ); - if( !ppDevice || !ppImmediateContext || !ppSwapChain ) - return; - - *ppDevice = nullptr; - *ppImmediateContext = nullptr; - *ppSwapChain = nullptr; - - try - { - SetRawAllocator(CreationAttribs.pRawMemAllocator); - auto &RawMemAllocator = GetRawAllocator(); - - RenderDeviceGLImpl *pRenderDeviceOpenGL( NEW_RC_OBJ(RawMemAllocator, "TRenderDeviceGLImpl instance", TRenderDeviceGLImpl)(RawMemAllocator, CreationAttribs) ); - pRenderDeviceOpenGL->QueryInterface(IID_RenderDevice, reinterpret_cast(ppDevice) ); - - DeviceContextGLImpl *pDeviceContextOpenGL( NEW_RC_OBJ(RawMemAllocator, "DeviceContextGLImpl instance", DeviceContextGLImpl)(pRenderDeviceOpenGL, false ) ); - // We must call AddRef() (implicitly through QueryInterface()) because pRenderDeviceOpenGL will - // keep a weak reference to the context - pDeviceContextOpenGL->QueryInterface(IID_DeviceContext, reinterpret_cast(ppImmediateContext) ); - pRenderDeviceOpenGL->SetImmediateContext(pDeviceContextOpenGL); - - TSwapChain *pSwapChainGL = NEW_RC_OBJ(RawMemAllocator, "SwapChainGLImpl instance", TSwapChain)(CreationAttribs, SCDesc, pRenderDeviceOpenGL, pDeviceContextOpenGL ); - pSwapChainGL->QueryInterface(IID_SwapChain, reinterpret_cast(ppSwapChain) ); - - pDeviceContextOpenGL->SetSwapChain(pSwapChainGL); - // Bind default framebuffer and viewport - pDeviceContextOpenGL->SetRenderTargets( 0, nullptr, nullptr, RESOURCE_STATE_TRANSITION_MODE_TRANSITION ); - pDeviceContextOpenGL->SetViewports( 1, nullptr, 0, 0 ); - } - catch( const std::runtime_error & ) - { - if( *ppDevice ) - { - (*ppDevice)->Release(); - *ppDevice = nullptr; - } - - if( *ppImmediateContext ) - { - (*ppImmediateContext)->Release(); - *ppImmediateContext = nullptr; - } - - if( *ppSwapChain ) - { - (*ppSwapChain)->Release(); - *ppSwapChain = nullptr; - } - - LOG_ERROR( "Failed to initialize OpenGL-based render device" ); - } -} - - -/// Creates render device, device context and attaches to existing GL context - -/// \param [in] CreationAttribs - Engine creation attributes. -/// \param [out] ppDevice - Address of the memory location where pointer to -/// the created device will be written. -/// \param [out] ppImmediateContext - Address of the memory location where pointers to -/// the immediate context will be written. -void EngineFactoryOpenGLImpl::AttachToActiveGLContext( const EngineGLAttribs& CreationAttribs, - IRenderDevice **ppDevice, - IDeviceContext **ppImmediateContext ) -{ - if (CreationAttribs.DebugMessageCallback != nullptr) - SetDebugMessageCallback(CreationAttribs.DebugMessageCallback); - - VERIFY( ppDevice && ppImmediateContext, "Null pointer provided" ); - if( !ppDevice || !ppImmediateContext ) - return; - - *ppDevice = nullptr; - *ppImmediateContext = nullptr; - - try - { - SetRawAllocator(CreationAttribs.pRawMemAllocator); - auto &RawMemAllocator = GetRawAllocator(); - - RenderDeviceGLImpl *pRenderDeviceOpenGL( NEW_RC_OBJ(RawMemAllocator, "TRenderDeviceGLImpl instance", TRenderDeviceGLImpl)(RawMemAllocator, CreationAttribs) ); - pRenderDeviceOpenGL->QueryInterface(IID_RenderDevice, reinterpret_cast(ppDevice) ); - - DeviceContextGLImpl *pDeviceContextOpenGL( NEW_RC_OBJ(RawMemAllocator, "DeviceContextGLImpl instance", DeviceContextGLImpl)(pRenderDeviceOpenGL, false ) ); - // We must call AddRef() (implicitly through QueryInterface()) because pRenderDeviceOpenGL will - // keep a weak reference to the context - pDeviceContextOpenGL->QueryInterface(IID_DeviceContext, reinterpret_cast(ppImmediateContext) ); - pRenderDeviceOpenGL->SetImmediateContext(pDeviceContextOpenGL); - } - catch( const std::runtime_error & ) - { - if( *ppDevice ) - { - (*ppDevice)->Release(); - *ppDevice = nullptr; - } - - if( *ppImmediateContext ) - { - (*ppImmediateContext)->Release(); - *ppImmediateContext = nullptr; - } - - LOG_ERROR( "Failed to initialize OpenGL-based render device" ); - } -} - -#ifdef DOXYGEN -/// Loads OpenGL-based engine implementation and exports factory functions -/// \param [out] GetFactoryFunc - Pointer to the function that returns pointer to the factory for -/// the OpenGL engine implementation -/// See EngineFactoryOpenGLImpl::CreateDeviceAndSwapChainGL(). -/// \remarks Depending on the configuration and platform, the function loads different dll: -/// Platform\\Configuration | Debug | Release -/// --------------------------|------------------------------|---------------------------- -/// Win32/x86 | GraphicsEngineOpenGL_32d.dll | GraphicsEngineOpenGL_32r.dll -/// Win32/x64 | GraphicsEngineOpenGL_64d.dll | GraphicsEngineOpenGL_64r.dll -/// -/// To load the library on Android, it is necessary to call System.loadLibrary("GraphicsEngineOpenGL") from Java. -void LoadGraphicsEngineOpenGL(GetEngineFactoryOpenGLType &GetFactoryFunc) -{ - // This function is only required because DoxyGen refuses to generate documentation for a static function when SHOW_FILES==NO - #error This function must never be compiled; -} -#endif - -void EngineFactoryOpenGLImpl::CreateHLSL2GLSLConverter(IHLSL2GLSLConverter **ppConverter) -{ - HLSL2GLSLConverterObject *pConverter( NEW_RC_OBJ(GetRawAllocator(), "HLSL2GLSLConverterObject instance", HLSL2GLSLConverterObject)() ); - pConverter->QueryInterface( IID_HLSL2GLSLConverter, reinterpret_cast(ppConverter) ); -} - -API_QUALIFIER -Diligent::IEngineFactoryOpenGL* GetEngineFactoryOpenGL() -{ - return Diligent::EngineFactoryOpenGLImpl::GetInstance(); -} - -} -- cgit v1.2.3