From a517455e2ef4f9e16c11cba5c2a0287a67a4cfcf Mon Sep 17 00:00:00 2001 From: assiduous Date: Wed, 27 Sep 2023 23:15:33 -0700 Subject: [PATCH] Reworked texture copying in OpenGL (close #438) --- Graphics/GraphicsEngineOpenGL/CMakeLists.txt | 2 - .../include/GLStubsAndroid.h | 7 +- .../include/GLStubsEmscripten.h | 3 +- .../GraphicsEngineOpenGL/include/GLStubsIOS.h | 1 + .../include/GLTypeConversions.hpp | 23 +- .../include/RenderDeviceGLImpl.hpp | 5 - .../include/TexRegionRender.hpp | 68 ----- .../include/Texture1DArray_GL.hpp | 5 +- .../include/Texture1D_GL.hpp | 5 +- .../include/Texture2DArray_GL.hpp | 5 +- .../include/Texture2D_GL.hpp | 5 +- .../include/Texture3D_GL.hpp | 5 +- .../include/TextureBaseGL.hpp | 13 + .../include/TextureCubeArray_GL.hpp | 5 +- .../include/TextureCube_GL.hpp | 5 +- .../src/EngineFactoryOpenGL.cpp | 5 +- .../GraphicsEngineOpenGL/src/FBOCache.cpp | 15 +- .../src/GLStubsAndroid.cpp | 10 +- .../src/GLTypeConversions.cpp | 21 +- .../src/RenderDeviceGLImpl.cpp | 5 - .../src/TexRegionRender.cpp | 248 ------------------ .../src/Texture1DArray_GL.cpp | 31 ++- .../GraphicsEngineOpenGL/src/Texture1D_GL.cpp | 22 +- .../src/Texture2DArray_GL.cpp | 33 ++- .../GraphicsEngineOpenGL/src/Texture2D_GL.cpp | 24 +- .../GraphicsEngineOpenGL/src/Texture3D_GL.cpp | 32 ++- .../src/TextureBaseGL.cpp | 106 +++----- .../src/TextureCubeArray_GL.cpp | 32 ++- .../src/TextureCube_GL.cpp | 50 ++-- 29 files changed, 321 insertions(+), 470 deletions(-) delete mode 100644 Graphics/GraphicsEngineOpenGL/include/TexRegionRender.hpp delete mode 100644 Graphics/GraphicsEngineOpenGL/src/TexRegionRender.cpp diff --git a/Graphics/GraphicsEngineOpenGL/CMakeLists.txt b/Graphics/GraphicsEngineOpenGL/CMakeLists.txt index e978c8d66..cce40804a 100644 --- a/Graphics/GraphicsEngineOpenGL/CMakeLists.txt +++ b/Graphics/GraphicsEngineOpenGL/CMakeLists.txt @@ -32,7 +32,6 @@ set(INCLUDE include/ShaderResourcesGL.hpp include/ShaderVariableManagerGL.hpp include/SwapChainGLBase.hpp - include/TexRegionRender.hpp include/Texture1D_GL.hpp include/Texture1DArray_GL.hpp include/Texture2D_GL.hpp @@ -87,7 +86,6 @@ set(SOURCE src/ShaderResourceCacheGL.cpp src/ShaderResourcesGL.cpp src/ShaderVariableManagerGL.cpp - src/TexRegionRender.cpp src/Texture1D_GL.cpp src/Texture1DArray_GL.cpp src/Texture2D_GL.cpp diff --git a/Graphics/GraphicsEngineOpenGL/include/GLStubsAndroid.h b/Graphics/GraphicsEngineOpenGL/include/GLStubsAndroid.h index c14e88c61..b8102f7cf 100644 --- a/Graphics/GraphicsEngineOpenGL/include/GLStubsAndroid.h +++ b/Graphics/GraphicsEngineOpenGL/include/GLStubsAndroid.h @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -1287,6 +1287,11 @@ extern PFNGLFRAMEBUFFERTEXTUREPROC glFramebufferTexture; typedef void (GL_APIENTRY* PFNGLFRAMEBUFFERTEXTURE1DPROC) (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level); extern PFNGLFRAMEBUFFERTEXTURE1DPROC glFramebufferTexture1D; +// not supported +#define LOAD_GL_COPY_TEX_SUBIMAGE_1D +typedef void (GL_APIENTRY* PFNGLCOPYTEXSUBIMAGE1DEXTPROC) (GLenum target, GLint level, GLint xoffset, GLint x, GLint y, GLsizei width); +extern PFNGLCOPYTEXSUBIMAGE1DEXTPROC glCopyTexSubImage1D; + // GL_OES_texture_3D #define LOAD_GL_FRAMEBUFFER_TEXTURE_3D typedef void (GL_APIENTRY* PFNGLFRAMEBUFFERTEXTURE3DPROC) (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level, GLint layer); diff --git a/Graphics/GraphicsEngineOpenGL/include/GLStubsEmscripten.h b/Graphics/GraphicsEngineOpenGL/include/GLStubsEmscripten.h index 3b5c84153..d4a802184 100644 --- a/Graphics/GraphicsEngineOpenGL/include/GLStubsEmscripten.h +++ b/Graphics/GraphicsEngineOpenGL/include/GLStubsEmscripten.h @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -684,6 +684,7 @@ static void (*glPolygonMode)(GLenum face, GLenum mode) = nullptr; #define glColorMaski(...) UnsupportedGLFunctionStub("glColorMaski", __VA_ARGS__) #define glFramebufferTexture(...) UnsupportedGLFunctionStub("glFramebufferTexture", __VA_ARGS__) #define glFramebufferTexture1D(...) UnsupportedGLFunctionStub("glFramebufferTexture1D", __VA_ARGS__) +#define glCopyTexSubImage1D(...) UnsupportedGLFunctionStub("glCopyTexSubImage1D", __VA_ARGS__) static void (*glGetQueryObjectui64v)(GLuint id, GLenum pname, GLuint64* params) = nullptr; #define glGenProgramPipelines(...) UnsupportedGLFunctionStub("glGenProgramPipelines", __VA_ARGS__) #define glBindProgramPipeline(...) UnsupportedGLFunctionStub("glBindProgramPipeline", __VA_ARGS__) diff --git a/Graphics/GraphicsEngineOpenGL/include/GLStubsIOS.h b/Graphics/GraphicsEngineOpenGL/include/GLStubsIOS.h index 9405e1a64..3cf905489 100644 --- a/Graphics/GraphicsEngineOpenGL/include/GLStubsIOS.h +++ b/Graphics/GraphicsEngineOpenGL/include/GLStubsIOS.h @@ -635,6 +635,7 @@ static void (*glPolygonMode)(GLenum face, GLenum mode) = nullptr; #define glColorMaski(...) UnsupportedGLFunctionStub("glColorMaski") #define glFramebufferTexture(...) UnsupportedGLFunctionStub("glFramebufferTexture") #define glFramebufferTexture1D(...) UnsupportedGLFunctionStub("glFramebufferTexture1D") +#define glCopyTexSubImage1D(...) UnsupportedGLFunctionStub("glCopyTexSubImage1D") #define glClipControl(...) UnsupportedGLFunctionStub("glClipControl") static void (*glGetQueryObjectui64v)(GLuint id, GLenum pname, GLuint64* params) = nullptr; diff --git a/Graphics/GraphicsEngineOpenGL/include/GLTypeConversions.hpp b/Graphics/GraphicsEngineOpenGL/include/GLTypeConversions.hpp index 9172cef21..e5a95b294 100644 --- a/Graphics/GraphicsEngineOpenGL/include/GLTypeConversions.hpp +++ b/Graphics/GraphicsEngineOpenGL/include/GLTypeConversions.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -27,6 +27,8 @@ #pragma once +#include + namespace Diligent { @@ -453,4 +455,23 @@ ShaderCodeVariableDesc GLDataTypeToShaderCodeVariableDesc(GLenum glDataType); GLint TextureComponentSwizzleToGLTextureSwizzle(TEXTURE_COMPONENT_SWIZZLE Swizzle, GLint IdentitySwizzle); +const char* GetFramebufferStatusString(GLenum Status); + +inline GLenum GetCubeMapFaceBindTarget(Uint32 Slice) +{ + // clang-format off + static constexpr std::array CubeMapFaceTargets = + { + GL_TEXTURE_CUBE_MAP_POSITIVE_X, + GL_TEXTURE_CUBE_MAP_NEGATIVE_X, + GL_TEXTURE_CUBE_MAP_POSITIVE_Y, + GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, + GL_TEXTURE_CUBE_MAP_POSITIVE_Z, + GL_TEXTURE_CUBE_MAP_NEGATIVE_Z + }; + // clang-format on + + return Slice < CubeMapFaceTargets.size() ? CubeMapFaceTargets[Slice] : 0; +} + } // namespace Diligent diff --git a/Graphics/GraphicsEngineOpenGL/include/RenderDeviceGLImpl.hpp b/Graphics/GraphicsEngineOpenGL/include/RenderDeviceGLImpl.hpp index 9723ef661..0d0c29df0 100644 --- a/Graphics/GraphicsEngineOpenGL/include/RenderDeviceGLImpl.hpp +++ b/Graphics/GraphicsEngineOpenGL/include/RenderDeviceGLImpl.hpp @@ -35,7 +35,6 @@ #include "VAOCache.hpp" #include "BaseInterfacesGL.h" #include "FBOCache.hpp" -#include "TexRegionRender.hpp" namespace Diligent { @@ -200,8 +199,6 @@ class RenderDeviceGLImpl : public RenderDeviceBase size_t GetCommandQueueCount() const { return 1; } Uint64 GetCommandQueueMask() const { return Uint64{1}; } - void InitTexRegionRender(); - struct GLDeviceLimits { GLint MaxUniformBlocks; @@ -239,8 +236,6 @@ class RenderDeviceGLImpl : public RenderDeviceBase Threading::SpinLock m_FBOCacheLock; std::unordered_map m_FBOCache; - std::unique_ptr m_pTexRegionRender; - private: virtual void TestTextureFormat(TEXTURE_FORMAT TexFormat) override final; bool CheckExtension(const Char* ExtensionString) const; diff --git a/Graphics/GraphicsEngineOpenGL/include/TexRegionRender.hpp b/Graphics/GraphicsEngineOpenGL/include/TexRegionRender.hpp deleted file mode 100644 index 49045bd2e..000000000 --- a/Graphics/GraphicsEngineOpenGL/include/TexRegionRender.hpp +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2019-2022 Diligent Graphics LLC - * 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * 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 - -namespace Diligent -{ - -// Helper class to facilitate texture copying by rendering -class TexRegionRender -{ -public: - TexRegionRender(class RenderDeviceGLImpl* pDeviceGL); - void SetStates(class DeviceContextGLImpl* pCtxGL); - void RestoreStates(class DeviceContextGLImpl* pCtxGL); - - void Render(class DeviceContextGLImpl* pCtxGL, - ITextureView* pSrcSRV, - RESOURCE_DIMENSION TexType, - TEXTURE_FORMAT TexFormat, - Int32 DstToSrcXOffset, - Int32 DstToSrcYOffset, - Int32 SrcZ, - Int32 SrcMipLevel); - -private: - RefCntAutoPtr m_pVertexShader; - RefCntAutoPtr m_pFragmentShaders[RESOURCE_DIM_NUM_DIMENSIONS * 3]; - RefCntAutoPtr m_pConstantBuffer; - - RefCntAutoPtr m_pPSO[RESOURCE_DIM_NUM_DIMENSIONS * 3]; - RefCntAutoPtr m_pSRB; - IShaderResourceVariable* m_pSrcTexVar = nullptr; - - RefCntAutoPtr m_pOrigPSO; - Uint32 m_OrigStencilRef = 0; - float m_OrigBlendFactors[4] = {}; - Uint32 m_NumRenderTargets = 0; - ITextureView* m_pOrigRTVs[MAX_RENDER_TARGETS] = {}; - RefCntAutoPtr m_pOrigDSV; - std::vector m_OrigViewports; -}; - -} // namespace Diligent diff --git a/Graphics/GraphicsEngineOpenGL/include/Texture1DArray_GL.hpp b/Graphics/GraphicsEngineOpenGL/include/Texture1DArray_GL.hpp index 47f43a87f..297410bf3 100644 --- a/Graphics/GraphicsEngineOpenGL/include/Texture1DArray_GL.hpp +++ b/Graphics/GraphicsEngineOpenGL/include/Texture1DArray_GL.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -64,6 +64,9 @@ class Texture1DArray_GL final : public TextureBaseGL /// Implementation of TextureBaseGL::AttachToFramebuffer() for 1D texture array. virtual void AttachToFramebuffer(const struct TextureViewDesc& ViewDesc, GLenum AttachmentPoint) override final; + + /// Implementation of TextureBaseGL::CopyTexSubimage() for 1D texture array. + virtual void CopyTexSubimage(GLContextState& GLState, const CopyTexSubimageAttribs& Attribs) override final; }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineOpenGL/include/Texture1D_GL.hpp b/Graphics/GraphicsEngineOpenGL/include/Texture1D_GL.hpp index 408895911..b9a85b813 100644 --- a/Graphics/GraphicsEngineOpenGL/include/Texture1D_GL.hpp +++ b/Graphics/GraphicsEngineOpenGL/include/Texture1D_GL.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -64,6 +64,9 @@ class Texture1D_GL final : public TextureBaseGL /// Implementation of TextureBaseGL::AttachToFramebuffer() for 1D texture. virtual void AttachToFramebuffer(const struct TextureViewDesc& ViewDesc, GLenum AttachmentPoint) override final; + + /// Implementation of TextureBaseGL::CopyTexSubimage() for 1D texture. + virtual void CopyTexSubimage(GLContextState& GLState, const CopyTexSubimageAttribs& Attribs) override final; }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineOpenGL/include/Texture2DArray_GL.hpp b/Graphics/GraphicsEngineOpenGL/include/Texture2DArray_GL.hpp index 0e35ab64d..2c47a1bbf 100644 --- a/Graphics/GraphicsEngineOpenGL/include/Texture2DArray_GL.hpp +++ b/Graphics/GraphicsEngineOpenGL/include/Texture2DArray_GL.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -64,6 +64,9 @@ class Texture2DArray_GL final : public TextureBaseGL /// Implementation of TextureBaseGL::AttachToFramebuffer() for 2D texture array. virtual void AttachToFramebuffer(const struct TextureViewDesc& ViewDesc, GLenum AttachmentPoint) override final; + + /// Implementation of TextureBaseGL::CopyTexSubimage() for 2D texture array. + virtual void CopyTexSubimage(GLContextState& GLState, const CopyTexSubimageAttribs& Attribs) override final; }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineOpenGL/include/Texture2D_GL.hpp b/Graphics/GraphicsEngineOpenGL/include/Texture2D_GL.hpp index 4bd7b203c..566adaa79 100644 --- a/Graphics/GraphicsEngineOpenGL/include/Texture2D_GL.hpp +++ b/Graphics/GraphicsEngineOpenGL/include/Texture2D_GL.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -72,6 +72,9 @@ class Texture2D_GL final : public TextureBaseGL /// Implementation of TextureBaseGL::AttachToFramebuffer() for 2D texture. virtual void AttachToFramebuffer(const struct TextureViewDesc& ViewDesc, GLenum AttachmentPoint) override final; + + /// Implementation of TextureBaseGL::CopyTexSubimage() for 2D texture. + virtual void CopyTexSubimage(GLContextState& GLState, const CopyTexSubimageAttribs& Attribs) override final; }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineOpenGL/include/Texture3D_GL.hpp b/Graphics/GraphicsEngineOpenGL/include/Texture3D_GL.hpp index 3e06fb928..652b8db33 100644 --- a/Graphics/GraphicsEngineOpenGL/include/Texture3D_GL.hpp +++ b/Graphics/GraphicsEngineOpenGL/include/Texture3D_GL.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -64,6 +64,9 @@ class Texture3D_GL final : public TextureBaseGL /// Implementation of TextureBaseGL::AttachToFramebuffer() for 3D texture. virtual void AttachToFramebuffer(const struct TextureViewDesc& ViewDesc, GLenum AttachmentPoint) override final; + + /// Implementation of TextureBaseGL::CopyTexSubimage() for 3D texture. + virtual void CopyTexSubimage(GLContextState& GLState, const CopyTexSubimageAttribs& Attribs) override final; }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineOpenGL/include/TextureBaseGL.hpp b/Graphics/GraphicsEngineOpenGL/include/TextureBaseGL.hpp index 9e6fbdd9c..b74f68b87 100644 --- a/Graphics/GraphicsEngineOpenGL/include/TextureBaseGL.hpp +++ b/Graphics/GraphicsEngineOpenGL/include/TextureBaseGL.hpp @@ -121,6 +121,19 @@ class TextureBaseGL : public TextureBase, public AsyncWritab void SetDefaultGLParameters(); + struct CopyTexSubimageAttribs + { + const Box& SrcBox; + + GLint DstMip = 0; + GLint DstLayer = 0; + GLint DstX = 0; + GLint DstY = 0; + GLint DstZ = 0; + }; + virtual void CopyTexSubimage(GLContextState& GLState, const CopyTexSubimageAttribs& Attribs) = 0; + +protected: GLObjectWrappers::GLTextureObj m_GlTexture; RefCntAutoPtr m_pPBO; // For staging textures const GLenum m_BindTarget; diff --git a/Graphics/GraphicsEngineOpenGL/include/TextureCubeArray_GL.hpp b/Graphics/GraphicsEngineOpenGL/include/TextureCubeArray_GL.hpp index 14b27a172..a12f23e9b 100644 --- a/Graphics/GraphicsEngineOpenGL/include/TextureCubeArray_GL.hpp +++ b/Graphics/GraphicsEngineOpenGL/include/TextureCubeArray_GL.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -64,6 +64,9 @@ class TextureCubeArray_GL final : public TextureBaseGL /// Implementation of TextureBaseGL::AttachToFramebuffer() for cube texture array. virtual void AttachToFramebuffer(const struct TextureViewDesc& ViewDesc, GLenum AttachmentPoint) override final; + + /// Implementation of TextureBaseGL::CopyTexSubimage() for cube texture array. + virtual void CopyTexSubimage(GLContextState& GLState, const CopyTexSubimageAttribs& Attribs) override final; }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineOpenGL/include/TextureCube_GL.hpp b/Graphics/GraphicsEngineOpenGL/include/TextureCube_GL.hpp index 541af9658..274938c86 100644 --- a/Graphics/GraphicsEngineOpenGL/include/TextureCube_GL.hpp +++ b/Graphics/GraphicsEngineOpenGL/include/TextureCube_GL.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -64,6 +64,9 @@ class TextureCube_GL final : public TextureBaseGL /// Implementation of TextureBaseGL::AttachToFramebuffer() for cube texture. virtual void AttachToFramebuffer(const struct TextureViewDesc& ViewDesc, GLenum AttachmentPoint) override final; + + /// Implementation of TextureBaseGL::CopyTexSubimage() for cube texture. + virtual void CopyTexSubimage(GLContextState& GLState, const CopyTexSubimageAttribs& Attribs) override final; }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineOpenGL/src/EngineFactoryOpenGL.cpp b/Graphics/GraphicsEngineOpenGL/src/EngineFactoryOpenGL.cpp index a8a6dcfee..e03d9bbde 100644 --- a/Graphics/GraphicsEngineOpenGL/src/EngineFactoryOpenGL.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/EngineFactoryOpenGL.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -228,9 +228,6 @@ void EngineFactoryOpenGLImpl::CreateDeviceAndSwapChainGL(const EngineGLCreateInf pDeviceContextOpenGL->QueryInterface(IID_DeviceContext, reinterpret_cast(ppImmediateContext)); pRenderDeviceOpenGL->SetImmediateContext(0, pDeviceContextOpenGL); - // Need to create immediate context first - pRenderDeviceOpenGL->InitTexRegionRender(); - TSwapChain* pSwapChainGL = NEW_RC_OBJ(RawMemAllocator, "SwapChainGLImpl instance", TSwapChain)(EngineCI, SCDesc, pRenderDeviceOpenGL, pDeviceContextOpenGL); pSwapChainGL->QueryInterface(IID_SwapChain, reinterpret_cast(ppSwapChain)); diff --git a/Graphics/GraphicsEngineOpenGL/src/FBOCache.cpp b/Graphics/GraphicsEngineOpenGL/src/FBOCache.cpp index bdc51ca02..d2b6ef23c 100644 --- a/Graphics/GraphicsEngineOpenGL/src/FBOCache.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/FBOCache.cpp @@ -31,6 +31,7 @@ #include "RenderDeviceGLImpl.hpp" #include "TextureBaseGL.hpp" #include "GLContextState.hpp" +#include "GLTypeConversions.hpp" namespace Diligent { @@ -229,19 +230,7 @@ GLObjectWrappers::GLFrameBufferObj FBOCache::CreateFBO(GLContextState& Contex GLenum Status = glCheckFramebufferStatus(GL_FRAMEBUFFER); if (Status != GL_FRAMEBUFFER_COMPLETE) { - const Char* StatusString = "Unknown"; - switch (Status) - { - // clang-format off - case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: StatusString = "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT"; break; - case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: StatusString = "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT"; break; - case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER: StatusString = "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER"; break; - case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER: StatusString = "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER"; break; - case GL_FRAMEBUFFER_UNSUPPORTED: StatusString = "GL_FRAMEBUFFER_UNSUPPORTED"; break; - case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE: StatusString = "GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE"; break; - case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS: StatusString = "GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS"; break; - // clang-format on - } + const Char* StatusString = GetFramebufferStatusString(Status); LOG_ERROR("Framebuffer is incomplete. FB status: ", StatusString); UNEXPECTED("Framebuffer is incomplete"); } diff --git a/Graphics/GraphicsEngineOpenGL/src/GLStubsAndroid.cpp b/Graphics/GraphicsEngineOpenGL/src/GLStubsAndroid.cpp index a9dfb2e6e..d45d5f538 100644 --- a/Graphics/GraphicsEngineOpenGL/src/GLStubsAndroid.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/GLStubsAndroid.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -173,6 +173,10 @@ DECLARE_GL_FUNCTION( glFramebufferTexture1D, PFNGLFRAMEBUFFERTEXTURE1DPROC, GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level) #endif +#ifdef LOAD_GL_COPY_TEX_SUBIMAGE_1D + DECLARE_GL_FUNCTION( glCopyTexSubImage1D, PFNGLCOPYTEXSUBIMAGE1DEXTPROC, GLenum target, GLint level, GLint xoffset, GLint x, GLint y, GLsizei width) +#endif + #ifdef LOAD_GL_FRAMEBUFFER_TEXTURE_3D DECLARE_GL_FUNCTION( glFramebufferTexture3D, PFNGLFRAMEBUFFERTEXTURE3DPROC, GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level, GLint layer ) #endif @@ -421,6 +425,10 @@ void LoadGLFunctions() LOAD_GL_FUNCTION(glFramebufferTexture1D) #endif +#ifdef LOAD_GL_COPY_TEX_SUBIMAGE_1D + LOAD_GL_FUNCTION(glCopyTexSubImage1D) +#endif + #ifdef LOAD_GL_FRAMEBUFFER_TEXTURE_3D LOAD_GL_FUNCTION2(glFramebufferTexture3D, {{"glFramebufferTexture3DOES", {3,0}}} ) #endif diff --git a/Graphics/GraphicsEngineOpenGL/src/GLTypeConversions.cpp b/Graphics/GraphicsEngineOpenGL/src/GLTypeConversions.cpp index 6a5fbb765..53c139e34 100644 --- a/Graphics/GraphicsEngineOpenGL/src/GLTypeConversions.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/GLTypeConversions.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -1109,4 +1109,23 @@ GLint TextureComponentSwizzleToGLTextureSwizzle(TEXTURE_COMPONENT_SWIZZLE Swizzl } } +const char* GetFramebufferStatusString(GLenum Status) +{ + switch (Status) + { + // clang-format off + case GL_FRAMEBUFFER_COMPLETE: return "GL_FRAMEBUFFER_COMPLETE"; + case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: return "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT"; + case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: return "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT"; + case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER: return "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER"; + case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER: return "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER"; + case GL_FRAMEBUFFER_UNSUPPORTED: return "GL_FRAMEBUFFER_UNSUPPORTED"; + case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE: return "GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE"; + case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS: return "GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS"; + // clang-format on + default: + return "UNKNOWN"; + } +} + } // namespace Diligent diff --git a/Graphics/GraphicsEngineOpenGL/src/RenderDeviceGLImpl.cpp b/Graphics/GraphicsEngineOpenGL/src/RenderDeviceGLImpl.cpp index 28516679a..6ab47f212 100644 --- a/Graphics/GraphicsEngineOpenGL/src/RenderDeviceGLImpl.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/RenderDeviceGLImpl.cpp @@ -340,11 +340,6 @@ RenderDeviceGLImpl::~RenderDeviceGLImpl() IMPLEMENT_QUERY_INTERFACE(RenderDeviceGLImpl, IID_RenderDeviceGL, TRenderDeviceBase) -void RenderDeviceGLImpl::InitTexRegionRender() -{ - m_pTexRegionRender = std::make_unique(this); -} - void RenderDeviceGLImpl::CreateBuffer(const BufferDesc& BuffDesc, const BufferData* pBuffData, IBuffer** ppBuffer, bool bIsDeviceInternal) { auto pDeviceContext = GetImmediateContext(0); diff --git a/Graphics/GraphicsEngineOpenGL/src/TexRegionRender.cpp b/Graphics/GraphicsEngineOpenGL/src/TexRegionRender.cpp deleted file mode 100644 index 44eacf1c4..000000000 --- a/Graphics/GraphicsEngineOpenGL/src/TexRegionRender.cpp +++ /dev/null @@ -1,248 +0,0 @@ -/* - * Copyright 2019-2023 Diligent Graphics LLC - * 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * 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. - */ - -#include "pch.h" - -#include "TexRegionRender.hpp" -#include "RenderDeviceGLImpl.hpp" -#include "DeviceContextGLImpl.hpp" -#include "../../GraphicsTools/interface/MapHelper.hpp" - - -namespace Diligent -{ - -// clang-format off -static const Char* VertexShaderSource = -{ - //To use any built-in input or output in the gl_PerVertex and - //gl_PerFragment blocks in separable program objects, shader code must - //redeclare those blocks prior to use. - // - // Declaring this block causes compilation error on NVidia GLES - "#ifndef GL_ES \n" - "out gl_PerVertex \n" - "{ \n" - " vec4 gl_Position; \n" - "}; \n" - "#endif \n" - - "void main() \n" - "{ \n" - " vec4 Bounds = vec4(-1.0, -1.0, 1.0, 1.0); \n" - " vec2 PosXY[4]; \n" - " PosXY[0] = Bounds.xy; \n" - " PosXY[1] = Bounds.xw; \n" - " PosXY[2] = Bounds.zy; \n" - " PosXY[3] = Bounds.zw; \n" - " gl_Position = vec4(PosXY[gl_VertexID], 0.0, 1.0);\n" - "} \n" -}; -// clang-format on - -TexRegionRender::TexRegionRender(class RenderDeviceGLImpl* pDeviceGL) -{ - ShaderCreateInfo ShaderAttrs; - ShaderAttrs.Desc.Name = "TexRegionRender : Vertex shader"; - ShaderAttrs.Desc.ShaderType = SHADER_TYPE_VERTEX; - ShaderAttrs.Source = VertexShaderSource; - constexpr bool IsInternalDeviceObject = true; - pDeviceGL->CreateShader(ShaderAttrs, &m_pVertexShader, nullptr, IsInternalDeviceObject); - - - const char* SamplerType[RESOURCE_DIM_NUM_DIMENSIONS]{}; - SamplerType[RESOURCE_DIM_TEX_1D] = "sampler1D"; - SamplerType[RESOURCE_DIM_TEX_1D_ARRAY] = "sampler1DArray"; - SamplerType[RESOURCE_DIM_TEX_2D] = "sampler2D"; - SamplerType[RESOURCE_DIM_TEX_2D_ARRAY] = "sampler2DArray", - SamplerType[RESOURCE_DIM_TEX_3D] = "sampler3D"; - // There is no texelFetch() for texture cube [array] - //SamplerType[RESOURCE_DIM_TEX_CUBE] = "samplerCube"; - //SamplerType[RESOURCE_DIM_TEX_CUBE_ARRAY] = "samplerCubeArray"; - - - const char* SrcLocations[RESOURCE_DIM_NUM_DIMENSIONS]{}; - SrcLocations[RESOURCE_DIM_TEX_1D] = "int(gl_FragCoord.x) + Constants.x"; - SrcLocations[RESOURCE_DIM_TEX_1D_ARRAY] = "ivec2(int(gl_FragCoord.x) + Constants.x, Constants.z)"; - SrcLocations[RESOURCE_DIM_TEX_2D] = "ivec2(gl_FragCoord.xy) + Constants.xy"; - SrcLocations[RESOURCE_DIM_TEX_2D_ARRAY] = "ivec3(ivec2(gl_FragCoord.xy) + Constants.xy, Constants.z)", - SrcLocations[RESOURCE_DIM_TEX_3D] = "ivec3(ivec2(gl_FragCoord.xy) + Constants.xy, Constants.z)"; - // There is no texelFetch() for texture cube [array] - //CoordDim[RESOURCE_DIM_TEX_CUBE] = "ivec2(gl_FragCoord.xy)"; - //CoordDim[RESOURCE_DIM_TEX_CUBE_ARRAY] = "ivec2(gl_FragCoord.xy)"; - - BufferDesc CBDesc; - CBDesc.Name = "TexRegionRender: FS constants CB"; - CBDesc.Size = sizeof(Int32) * 4; - CBDesc.Usage = USAGE_DYNAMIC; - CBDesc.BindFlags = BIND_UNIFORM_BUFFER; - CBDesc.CPUAccessFlags = CPU_ACCESS_WRITE; - pDeviceGL->CreateBuffer(CBDesc, nullptr, &m_pConstantBuffer, IsInternalDeviceObject); - - GraphicsPipelineStateCreateInfo PSOCreateInfo; - - auto& GraphicsPipeline = PSOCreateInfo.GraphicsPipeline; - GraphicsPipeline.RasterizerDesc.CullMode = CULL_MODE_NONE; - GraphicsPipeline.RasterizerDesc.FillMode = FILL_MODE_SOLID; - - GraphicsPipeline.DepthStencilDesc.DepthEnable = False; - GraphicsPipeline.DepthStencilDesc.DepthWriteEnable = False; - PSOCreateInfo.pVS = m_pVertexShader; - GraphicsPipeline.PrimitiveTopology = PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP; - - static const char* CmpTypePrefix[3] = {"", "i", "u"}; - for (Int32 Dim = RESOURCE_DIM_TEX_2D; Dim <= RESOURCE_DIM_TEX_3D; ++Dim) - { - const auto* SamplerDim = SamplerType[Dim]; - const auto* SrcLocation = SrcLocations[Dim]; - for (Int32 Fmt = 0; Fmt < 3; ++Fmt) - { - const auto* Prefix = CmpTypePrefix[Fmt]; - String Name = "TexRegionRender : Pixel shader "; - Name.append(Prefix); - Name.append(SamplerDim); - ShaderAttrs.Desc.Name = Name.c_str(); - ShaderAttrs.Desc.ShaderType = SHADER_TYPE_PIXEL; - - std::stringstream SourceSS; - SourceSS << "uniform " << Prefix << SamplerDim << " gSourceTex;\n" - << "layout(location = 0) out " << Prefix - << "vec4 Out;\n" - "uniform cbConstants\n" - "{\n" - " ivec4 Constants;\n" - "};\n" - "void main()\n" - "{\n" - " Out = texelFetch( gSourceTex, " - << SrcLocation - << ", Constants.w );\n" - "}\n"; - - auto Source = SourceSS.str(); - ShaderAttrs.Source = Source.c_str(); - auto& FragmentShader = m_pFragmentShaders[Dim * 3 + Fmt]; - pDeviceGL->CreateShader(ShaderAttrs, &FragmentShader, nullptr, IsInternalDeviceObject); - PSOCreateInfo.pPS = FragmentShader; - - auto& ResourceLayout = PSOCreateInfo.PSODesc.ResourceLayout; - - ResourceLayout.DefaultVariableType = SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC; - ShaderResourceVariableDesc Vars[] = - { - {SHADER_TYPE_PIXEL, "cbConstants", SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE} // - }; - ResourceLayout.NumVariables = _countof(Vars); - ResourceLayout.Variables = Vars; - - pDeviceGL->CreateGraphicsPipelineState(PSOCreateInfo, &m_pPSO[Dim * 3 + Fmt], IsInternalDeviceObject); - } - } - m_pPSO[RESOURCE_DIM_TEX_2D * 3]->CreateShaderResourceBinding(&m_pSRB); - m_pSRB->GetVariableByName(SHADER_TYPE_PIXEL, "cbConstants")->Set(m_pConstantBuffer); - m_pSrcTexVar = m_pSRB->GetVariableByName(SHADER_TYPE_PIXEL, "gSourceTex"); -} - -void TexRegionRender::SetStates(DeviceContextGLImpl* pCtxGL) -{ - pCtxGL->GetRenderTargets(m_NumRenderTargets, m_pOrigRTVs, &m_pOrigDSV); - - Uint32 NumViewports = 0; - pCtxGL->GetViewports(NumViewports, nullptr); - m_OrigViewports.resize(NumViewports); - pCtxGL->GetViewports(NumViewports, m_OrigViewports.data()); - - pCtxGL->GetPipelineState(&m_pOrigPSO, m_OrigBlendFactors, m_OrigStencilRef); -} - -void TexRegionRender::RestoreStates(DeviceContextGLImpl* pCtxGL) -{ - pCtxGL->SetRenderTargets(m_NumRenderTargets, m_pOrigRTVs, m_pOrigDSV, RESOURCE_STATE_TRANSITION_MODE_TRANSITION); - for (Uint32 rt = 0; rt < _countof(m_pOrigRTVs); ++rt) - { - if (m_pOrigRTVs[rt]) - m_pOrigRTVs[rt]->Release(); - m_pOrigRTVs[rt] = nullptr; - } - m_pOrigDSV.Release(); - - if (m_OrigViewports.size()) - pCtxGL->SetViewports((Uint32)m_OrigViewports.size(), m_OrigViewports.data(), 0, 0); - else - pCtxGL->SetViewports(1, nullptr, 0, 0); // set default - - if (m_pOrigPSO) - pCtxGL->SetPipelineState(m_pOrigPSO); - m_pOrigPSO.Release(); - pCtxGL->SetStencilRef(m_OrigStencilRef); - pCtxGL->SetBlendFactors(m_OrigBlendFactors); -} - -void TexRegionRender::Render(DeviceContextGLImpl* pCtxGL, - ITextureView* pSrcSRV, - RESOURCE_DIMENSION TexType, - TEXTURE_FORMAT TexFormat, - Int32 DstToSrcXOffset, - Int32 DstToSrcYOffset, - Int32 SrcZ, - Int32 SrcMipLevel) -{ - { - MapHelper pConstant(pCtxGL, m_pConstantBuffer, MAP_WRITE, MAP_FLAG_DISCARD); - pConstant[0] = DstToSrcXOffset; - pConstant[1] = DstToSrcYOffset; - pConstant[2] = SrcZ; - pConstant[3] = SrcMipLevel; - } - - const auto& TexFmtAttribs = GetTextureFormatAttribs(TexFormat); - Uint32 FSInd = TexType * 3; - if (TexFmtAttribs.ComponentType == COMPONENT_TYPE_SINT) - FSInd += 1; - else if (TexFmtAttribs.ComponentType == COMPONENT_TYPE_UINT) - FSInd += 2; - - if (TexFmtAttribs.ComponentType == COMPONENT_TYPE_SNORM) - { - LOG_WARNING_MESSAGE("CopyData() is performed by rendering to texture.\n" - "There might be an issue in OpenGL driver on NVidia hardware: when rendering to SNORM textures, all negative values are clamped to zero."); - } - - DEV_CHECK_ERR(m_pPSO[FSInd], "TexRegionRender does not support this combination of texture dimension/format"); - - pCtxGL->SetPipelineState(m_pPSO[FSInd]); - m_pSrcTexVar->Set(pSrcSRV); - pCtxGL->CommitShaderResources(m_pSRB, RESOURCE_STATE_TRANSITION_MODE_TRANSITION); - - DrawAttribs DrawAttrs; - DrawAttrs.NumVertices = 4; - pCtxGL->Draw(DrawAttrs); - - m_pSrcTexVar->Set(nullptr); -} - -} // namespace Diligent diff --git a/Graphics/GraphicsEngineOpenGL/src/Texture1DArray_GL.cpp b/Graphics/GraphicsEngineOpenGL/src/Texture1DArray_GL.cpp index 08635f809..bb835610c 100644 --- a/Graphics/GraphicsEngineOpenGL/src/Texture1DArray_GL.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/Texture1DArray_GL.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -186,8 +186,11 @@ void Texture1DArray_GL::AttachToFramebuffer(const TextureViewDesc& ViewDesc, GLe { if (ViewDesc.NumArraySlices == m_Desc.ArraySize) { - glFramebufferTexture(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip); - CHECK_GL_ERROR("Failed to attach texture 1D array to draw framebuffer"); + if (ViewDesc.ViewType == TEXTURE_VIEW_RENDER_TARGET || ViewDesc.ViewType == TEXTURE_VIEW_DEPTH_STENCIL) + { + glFramebufferTexture(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip); + CHECK_GL_ERROR("Failed to attach texture 1D array to draw framebuffer"); + } glFramebufferTexture(GL_READ_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip); CHECK_GL_ERROR("Failed to attach texture 1D array to read framebuffer"); } @@ -195,8 +198,11 @@ void Texture1DArray_GL::AttachToFramebuffer(const TextureViewDesc& ViewDesc, GLe { // Texture name must either be zero or the name of an existing 3D texture, 1D or 2D array texture, // cube map array texture, or multisample array texture. - glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip, ViewDesc.FirstArraySlice); - CHECK_GL_ERROR("Failed to attach texture 1D array to draw framebuffer"); + if (ViewDesc.ViewType == TEXTURE_VIEW_RENDER_TARGET || ViewDesc.ViewType == TEXTURE_VIEW_DEPTH_STENCIL) + { + glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip, ViewDesc.FirstArraySlice); + CHECK_GL_ERROR("Failed to attach texture 1D array to draw framebuffer"); + } glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip, ViewDesc.FirstArraySlice); CHECK_GL_ERROR("Failed to attach texture 1D array to read framebuffer"); } @@ -206,4 +212,19 @@ void Texture1DArray_GL::AttachToFramebuffer(const TextureViewDesc& ViewDesc, GLe } } +void Texture1DArray_GL::CopyTexSubimage(GLContextState& GLState, const CopyTexSubimageAttribs& Attribs) +{ + GLState.BindTexture(-1, GetBindTarget(), GetGLHandle()); + + glCopyTexSubImage2D(GetBindTarget(), + Attribs.DstMip, + Attribs.DstX, + Attribs.DstLayer, + Attribs.SrcBox.MinX, + Attribs.SrcBox.MinY, + Attribs.SrcBox.Width(), + 1); + CHECK_GL_ERROR("Failed to copy subimage data to texture 1D array"); +} + } // namespace Diligent diff --git a/Graphics/GraphicsEngineOpenGL/src/Texture1D_GL.cpp b/Graphics/GraphicsEngineOpenGL/src/Texture1D_GL.cpp index e0ebe516c..163aaaef0 100644 --- a/Graphics/GraphicsEngineOpenGL/src/Texture1D_GL.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/Texture1D_GL.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -179,10 +179,26 @@ void Texture1D_GL::UpdateData(GLContextState& ContextState, void Texture1D_GL::AttachToFramebuffer(const TextureViewDesc& ViewDesc, GLenum AttachmentPoint) { // For glFramebufferTexture1D(), if texture name is not zero, then texture target must be GL_TEXTURE_1D - glFramebufferTexture1D(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_BindTarget, m_GlTexture, ViewDesc.MostDetailedMip); - CHECK_GL_ERROR("Failed to attach texture 1D to draw framebuffer"); + if (ViewDesc.ViewType == TEXTURE_VIEW_RENDER_TARGET || ViewDesc.ViewType == TEXTURE_VIEW_DEPTH_STENCIL) + { + glFramebufferTexture1D(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_BindTarget, m_GlTexture, ViewDesc.MostDetailedMip); + CHECK_GL_ERROR("Failed to attach texture 1D to draw framebuffer"); + } glFramebufferTexture1D(GL_READ_FRAMEBUFFER, AttachmentPoint, m_BindTarget, m_GlTexture, ViewDesc.MostDetailedMip); CHECK_GL_ERROR("Failed to attach texture 1D to read framebuffer"); } +void Texture1D_GL::CopyTexSubimage(GLContextState& GLState, const CopyTexSubimageAttribs& Attribs) +{ + GLState.BindTexture(-1, GetBindTarget(), GetGLHandle()); + + glCopyTexSubImage1D(GetBindTarget(), + Attribs.DstMip, + Attribs.DstX, + Attribs.SrcBox.MinX, + Attribs.SrcBox.MinY, + Attribs.SrcBox.Width()); + CHECK_GL_ERROR("Failed to copy subimage data to texture 1D"); +} + } // namespace Diligent diff --git a/Graphics/GraphicsEngineOpenGL/src/Texture2DArray_GL.cpp b/Graphics/GraphicsEngineOpenGL/src/Texture2DArray_GL.cpp index e6f97e6f4..69ba3508d 100644 --- a/Graphics/GraphicsEngineOpenGL/src/Texture2DArray_GL.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/Texture2DArray_GL.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -258,10 +258,14 @@ void Texture2DArray_GL::UpdateData(GLContextState& ContextState, void Texture2DArray_GL::AttachToFramebuffer(const TextureViewDesc& ViewDesc, GLenum AttachmentPoint) { + if (ViewDesc.NumArraySlices == m_Desc.ArraySize) { - glFramebufferTexture(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip); - CHECK_GL_ERROR("Failed to attach texture 2D array to draw framebuffer"); + if (ViewDesc.ViewType == TEXTURE_VIEW_RENDER_TARGET || ViewDesc.ViewType == TEXTURE_VIEW_DEPTH_STENCIL) + { + glFramebufferTexture(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip); + CHECK_GL_ERROR("Failed to attach texture 2D array to draw framebuffer"); + } glFramebufferTexture(GL_READ_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip); CHECK_GL_ERROR("Failed to attach texture 2D array to read framebuffer"); } @@ -269,8 +273,11 @@ void Texture2DArray_GL::AttachToFramebuffer(const TextureViewDesc& ViewDesc, GLe { // Texture name must either be zero or the name of an existing 3D texture, 1D or 2D array texture, // cube map array texture, or multisample array texture. - glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip, ViewDesc.FirstArraySlice); - CHECK_GL_ERROR("Failed to attach texture 2D array to draw framebuffer"); + if (ViewDesc.ViewType == TEXTURE_VIEW_RENDER_TARGET || ViewDesc.ViewType == TEXTURE_VIEW_DEPTH_STENCIL) + { + glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip, ViewDesc.FirstArraySlice); + CHECK_GL_ERROR("Failed to attach texture 2D array to draw framebuffer"); + } glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip, ViewDesc.FirstArraySlice); CHECK_GL_ERROR("Failed to attach texture 2D array to read framebuffer"); } @@ -280,4 +287,20 @@ void Texture2DArray_GL::AttachToFramebuffer(const TextureViewDesc& ViewDesc, GLe } } +void Texture2DArray_GL::CopyTexSubimage(GLContextState& GLState, const CopyTexSubimageAttribs& Attribs) +{ + GLState.BindTexture(-1, GetBindTarget(), GetGLHandle()); + + glCopyTexSubImage3D(GetBindTarget(), + Attribs.DstMip, + Attribs.DstX, + Attribs.DstY, + Attribs.DstLayer, + Attribs.SrcBox.MinX, + Attribs.SrcBox.MinY, + Attribs.SrcBox.Width(), + Attribs.SrcBox.Height()); + CHECK_GL_ERROR("Failed to copy subimage data to texture 2D array"); +} + } // namespace Diligent diff --git a/Graphics/GraphicsEngineOpenGL/src/Texture2D_GL.cpp b/Graphics/GraphicsEngineOpenGL/src/Texture2D_GL.cpp index 2dd439a58..c4c72b46f 100644 --- a/Graphics/GraphicsEngineOpenGL/src/Texture2D_GL.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/Texture2D_GL.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -279,10 +279,28 @@ void Texture2D_GL::AttachToFramebuffer(const TextureViewDesc& ViewDesc, GLenum A { // For glFramebufferTexture2D(), if texture name is not zero, then texture target must be GL_TEXTURE_2D, // GL_TEXTURE_RECTANGLE or one of the 6 cubemap face targets GL_TEXTURE_CUBE_MAP_POSITIVE_X, ... - glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_BindTarget, m_GlTexture, ViewDesc.MostDetailedMip); - CHECK_GL_ERROR("Failed to attach texture 2D to draw framebuffer"); + if (ViewDesc.ViewType == TEXTURE_VIEW_RENDER_TARGET || ViewDesc.ViewType == TEXTURE_VIEW_DEPTH_STENCIL) + { + glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_BindTarget, m_GlTexture, ViewDesc.MostDetailedMip); + CHECK_GL_ERROR("Failed to attach texture 2D to draw framebuffer"); + } glFramebufferTexture2D(GL_READ_FRAMEBUFFER, AttachmentPoint, m_BindTarget, m_GlTexture, ViewDesc.MostDetailedMip); CHECK_GL_ERROR("Failed to attach texture 2D to read framebuffer"); } +void Texture2D_GL::CopyTexSubimage(GLContextState& GLState, const CopyTexSubimageAttribs& Attribs) +{ + GLState.BindTexture(-1, GetBindTarget(), GetGLHandle()); + + glCopyTexSubImage2D(GetBindTarget(), + Attribs.DstMip, + Attribs.DstX, + Attribs.DstY, + Attribs.SrcBox.MinX, + Attribs.SrcBox.MinY, + Attribs.SrcBox.Width(), + Attribs.SrcBox.Height()); + CHECK_GL_ERROR("Failed to copy subimage data to texture 2D"); +} + } // namespace Diligent diff --git a/Graphics/GraphicsEngineOpenGL/src/Texture3D_GL.cpp b/Graphics/GraphicsEngineOpenGL/src/Texture3D_GL.cpp index f5196f9b9..b6440528e 100644 --- a/Graphics/GraphicsEngineOpenGL/src/Texture3D_GL.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/Texture3D_GL.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -199,8 +199,11 @@ void Texture3D_GL::AttachToFramebuffer(const TextureViewDesc& ViewDesc, GLenum A auto NumDepthSlicesInMip = m_Desc.Depth >> ViewDesc.MostDetailedMip; if (ViewDesc.NumDepthSlices == NumDepthSlicesInMip) { - glFramebufferTexture(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip); - CHECK_GL_ERROR("Failed to attach texture 3D to draw framebuffer"); + if (ViewDesc.ViewType == TEXTURE_VIEW_RENDER_TARGET || ViewDesc.ViewType == TEXTURE_VIEW_DEPTH_STENCIL) + { + glFramebufferTexture(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip); + CHECK_GL_ERROR("Failed to attach texture 3D to draw framebuffer"); + } glFramebufferTexture(GL_READ_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip); CHECK_GL_ERROR("Failed to attach texture 3D to read framebuffer"); } @@ -212,8 +215,11 @@ void Texture3D_GL::AttachToFramebuffer(const TextureViewDesc& ViewDesc, GLenum A // On Android (at least on Intel HW), glFramebufferTexture3D() runs without errors, but the // FBO turns out to be incomplete. glFramebufferTextureLayer() seems to work fine. - glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip, ViewDesc.FirstDepthSlice); - CHECK_GL_ERROR("Failed to attach texture 3D to draw framebuffer"); + if (ViewDesc.ViewType == TEXTURE_VIEW_RENDER_TARGET || ViewDesc.ViewType == TEXTURE_VIEW_DEPTH_STENCIL) + { + glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip, ViewDesc.FirstDepthSlice); + CHECK_GL_ERROR("Failed to attach texture 3D to draw framebuffer"); + } glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip, ViewDesc.FirstDepthSlice); CHECK_GL_ERROR("Failed to attach texture 3D to read framebuffer"); } @@ -223,4 +229,20 @@ void Texture3D_GL::AttachToFramebuffer(const TextureViewDesc& ViewDesc, GLenum A } } +void Texture3D_GL::CopyTexSubimage(GLContextState& GLState, const CopyTexSubimageAttribs& Attribs) +{ + GLState.BindTexture(-1, GetBindTarget(), GetGLHandle()); + + glCopyTexSubImage3D(GetBindTarget(), + Attribs.DstMip, + Attribs.DstX, + Attribs.DstY, + Attribs.DstZ, + Attribs.SrcBox.MinX, + Attribs.SrcBox.MinY, + Attribs.SrcBox.Width(), + Attribs.SrcBox.Height()); + CHECK_GL_ERROR("Failed to copy subimage data to texture 3D"); +} + } // namespace Diligent diff --git a/Graphics/GraphicsEngineOpenGL/src/TextureBaseGL.cpp b/Graphics/GraphicsEngineOpenGL/src/TextureBaseGL.cpp index b77fd053c..5b8110de2 100644 --- a/Graphics/GraphicsEngineOpenGL/src/TextureBaseGL.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/TextureBaseGL.cpp @@ -580,8 +580,8 @@ void TextureBaseGL::CopyData(DeviceContextGLImpl* pDeviceCtxGL, pSrcBox = &SrcBox; } -#if GL_ARB_copy_image const bool IsDefaultBackBuffer = GetGLHandle() == 0; +#if GL_ARB_copy_image // We can't use glCopyImageSubData with the proxy texture of a default framebuffer // because we don't have the texture handle. Resort to quad rendering in this case. if (glCopyImageSubData && !IsDefaultBackBuffer) @@ -611,77 +611,51 @@ void TextureBaseGL::CopyData(DeviceContextGLImpl* pDeviceCtxGL, else #endif { - const auto& FmtAttribs = GetDevice()->GetTextureFormatInfoExt(m_Desc.Format); - if ((FmtAttribs.BindFlags & BIND_RENDER_TARGET) == 0) - { - LOG_ERROR_MESSAGE("Unable to perform copy operation because ", FmtAttribs.Name, " is not a color renderable format"); - return; - } + auto& GLState = pDeviceCtxGL->GetContextState(); - auto* pRenderDeviceGL = GetDevice(); -#ifdef DILIGENT_DEBUG - { - auto& TexViewObjAllocator = pRenderDeviceGL->GetTexViewObjAllocator(); - VERIFY(&TexViewObjAllocator == &m_dbgTexViewObjAllocator, "Texture view allocator does not match allocator provided during texture initialization"); - } -#endif - auto& TexRegionRender = *pRenderDeviceGL->m_pTexRegionRender; - TexRegionRender.SetStates(pDeviceCtxGL); - - // Create temporary SRV for the entire source texture - TextureViewDesc SRVDesc; - SRVDesc.TextureDim = SrcTexDesc.Type; - SRVDesc.ViewType = TEXTURE_VIEW_SHADER_RESOURCE; - ValidatedAndCorrectTextureViewDesc(m_Desc, SRVDesc); - // Note: texture view allocates memory for the copy of the name - // If the name is empty, memory should not be allocated - // We have to provide allocator even though it will never be used - TextureViewGLImpl SRV{ - GetReferenceCounters(), GetDevice(), SRVDesc, pSrcTextureGL, - false, // Do NOT create texture view OpenGL object - true, // The view, like default view, should not - // keep strong reference to the texture - }; + GLObjectWrappers::GLFrameBufferObj ReadFBO{!IsDefaultBackBuffer}; + GLState.BindFBO(ReadFBO); for (Uint32 DepthSlice = 0; DepthSlice < pSrcBox->Depth(); ++DepthSlice) { - // Create temporary RTV for the target subresource - TextureViewDesc RTVDesc; - RTVDesc.TextureDim = m_Desc.Type; - RTVDesc.ViewType = TEXTURE_VIEW_RENDER_TARGET; - RTVDesc.FirstArraySlice = DepthSlice + DstSlice; - RTVDesc.MostDetailedMip = DstMipLevel; - RTVDesc.NumArraySlices = 1; - ValidatedAndCorrectTextureViewDesc(m_Desc, RTVDesc); - // Note: texture view allocates memory for the copy of the name - // If the name is empty, memory should not be allocated - // We have to provide allocator even though it will never be used - TextureViewGLImpl RTV{ - GetReferenceCounters(), GetDevice(), RTVDesc, this, - false, // Do NOT create texture view OpenGL object - true, // The view, like default view, should not - // keep strong reference to the texture - }; - - ITextureView* pRTVs[] = {&RTV}; - pDeviceCtxGL->SetRenderTargets(_countof(pRTVs), pRTVs, nullptr, RESOURCE_STATE_TRANSITION_MODE_TRANSITION); - - // No need to set up the viewport as SetRenderTargets() does that - - TexRegionRender.Render(pDeviceCtxGL, - &SRV, - SrcTexDesc.Type, - SrcTexDesc.Format, - static_cast(pSrcBox->MinX) - static_cast(DstX), - static_cast(pSrcBox->MinY) - static_cast(DstY), - SrcSlice + pSrcBox->MinZ + DepthSlice, - SrcMipLevel); - - // NB: we must unbind RTV before it goes out of scope! - pDeviceCtxGL->ResetRenderTargets(); + if (!IsDefaultBackBuffer) + { + // Attach source subimage to read framebuffer + TextureViewDesc SRVVDesc; + SRVVDesc.TextureDim = SrcTexDesc.Type; + SRVVDesc.ViewType = TEXTURE_VIEW_SHADER_RESOURCE; + SRVVDesc.FirstArraySlice = SrcSlice + pSrcBox->MinZ + DepthSlice; + SRVVDesc.MostDetailedMip = SrcMipLevel; + SRVVDesc.NumArraySlices = 1; + ValidatedAndCorrectTextureViewDesc(m_Desc, SRVVDesc); + + pSrcTextureGL->AttachToFramebuffer(SRVVDesc, GL_COLOR_ATTACHMENT0); + + GLenum Status = glCheckFramebufferStatus(GL_READ_FRAMEBUFFER); + if (Status != GL_FRAMEBUFFER_COMPLETE) + { + const Char* StatusString = GetFramebufferStatusString(Status); + LOG_ERROR("Framebuffer is incomplete. FB status: ", StatusString); + UNEXPECTED("Framebuffer is incomplete"); + } + } + else + { + VERIFY(pSrcBox->Depth() == 1, "Default framebuffer only has one slice"); + } + + CopyTexSubimageAttribs CopyAttribs{*pSrcBox}; + CopyAttribs.DstMip = DstMipLevel; + CopyAttribs.DstLayer = DstSlice; + CopyAttribs.DstX = DstX; + CopyAttribs.DstY = DstY; + CopyAttribs.DstZ = DstZ + DepthSlice; + CopyTexSubimage(GLState, CopyAttribs); } - TexRegionRender.RestoreStates(pDeviceCtxGL); + GLState.InvalidateFBO(); + GLState.BindTexture(-1, GetBindTarget(), GLObjectWrappers::GLTextureObj::Null()); + pDeviceCtxGL->CommitRenderTargets(); } } diff --git a/Graphics/GraphicsEngineOpenGL/src/TextureCubeArray_GL.cpp b/Graphics/GraphicsEngineOpenGL/src/TextureCubeArray_GL.cpp index 0558d7e57..19f7ecfc6 100644 --- a/Graphics/GraphicsEngineOpenGL/src/TextureCubeArray_GL.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/TextureCubeArray_GL.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -267,8 +267,11 @@ void TextureCubeArray_GL::AttachToFramebuffer(const TextureViewDesc& ViewDesc, G if (ViewDesc.NumArraySlices == m_Desc.ArraySize) { // glFramebufferTexture() attaches the given mipmap level as a layered image with the number of layers that the given texture has. - glFramebufferTexture(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip); - CHECK_GL_ERROR("Failed to attach texture cubemap array to draw framebuffer"); + if (ViewDesc.ViewType == TEXTURE_VIEW_RENDER_TARGET || ViewDesc.ViewType == TEXTURE_VIEW_DEPTH_STENCIL) + { + glFramebufferTexture(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip); + CHECK_GL_ERROR("Failed to attach texture cubemap array to draw framebuffer"); + } glFramebufferTexture(GL_READ_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip); CHECK_GL_ERROR("Failed to attach texture cubemap array to read framebuffer"); } @@ -276,8 +279,11 @@ void TextureCubeArray_GL::AttachToFramebuffer(const TextureViewDesc& ViewDesc, G { // Texture name must either be zero or the name of an existing 3D texture, 1D or 2D array texture, // cube map array texture, or multisample array texture. - glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip, ViewDesc.FirstArraySlice); - CHECK_GL_ERROR("Failed to attach texture cubemap array to draw framebuffer"); + if (ViewDesc.ViewType == TEXTURE_VIEW_RENDER_TARGET || ViewDesc.ViewType == TEXTURE_VIEW_DEPTH_STENCIL) + { + glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip, ViewDesc.FirstArraySlice); + CHECK_GL_ERROR("Failed to attach texture cubemap array to draw framebuffer"); + } glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip, ViewDesc.FirstArraySlice); CHECK_GL_ERROR("Failed to attach texture cubemap array to read framebuffer"); } @@ -287,4 +293,20 @@ void TextureCubeArray_GL::AttachToFramebuffer(const TextureViewDesc& ViewDesc, G } } +void TextureCubeArray_GL::CopyTexSubimage(GLContextState& GLState, const CopyTexSubimageAttribs& Attribs) +{ + GLState.BindTexture(-1, GetBindTarget(), GetGLHandle()); + + glCopyTexSubImage3D(GetBindTarget(), + Attribs.DstMip, + Attribs.DstX, + Attribs.DstY, + Attribs.DstLayer, + Attribs.SrcBox.MinX, + Attribs.SrcBox.MinY, + Attribs.SrcBox.Width(), + Attribs.SrcBox.Height()); + CHECK_GL_ERROR("Failed to copy subimage data to texture cube array"); +} + } // namespace Diligent diff --git a/Graphics/GraphicsEngineOpenGL/src/TextureCube_GL.cpp b/Graphics/GraphicsEngineOpenGL/src/TextureCube_GL.cpp index e35228d41..84060be5d 100644 --- a/Graphics/GraphicsEngineOpenGL/src/TextureCube_GL.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/TextureCube_GL.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -143,20 +143,6 @@ TextureCube_GL::~TextureCube_GL() { } -// Move static member initialization out of function to avoid -// potential issues in multithreaded code -// clang-format off -static constexpr GLenum CubeMapFaces[6] = -{ - GL_TEXTURE_CUBE_MAP_POSITIVE_X, - GL_TEXTURE_CUBE_MAP_NEGATIVE_X, - GL_TEXTURE_CUBE_MAP_POSITIVE_Y, - GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, - GL_TEXTURE_CUBE_MAP_POSITIVE_Z, - GL_TEXTURE_CUBE_MAP_NEGATIVE_Z -}; -// clang-format on - void TextureCube_GL::UpdateData(GLContextState& ContextState, Uint32 MipLevel, Uint32 Slice, @@ -169,7 +155,7 @@ void TextureCube_GL::UpdateData(GLContextState& ContextState, // then takes one of GL_TEXTURE_CUBE_MAP_POSITIVE_X ... GL_TEXTURE_CUBE_MAP_NEGATIVE_Z ContextState.BindTexture(-1, m_BindTarget, m_GlTexture); - auto CubeMapFaceBindTarget = CubeMapFaces[Slice]; + auto CubeMapFaceBindTarget = GetCubeMapFaceBindTarget(Slice); // Bind buffer if it is provided; copy from CPU memory otherwise GLuint UnpackBuffer = 0; @@ -269,8 +255,11 @@ void TextureCube_GL::AttachToFramebuffer(const TextureViewDesc& ViewDesc, GLenum { if (ViewDesc.NumArraySlices == m_Desc.ArraySize) { - glFramebufferTexture(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip); - CHECK_GL_ERROR("Failed to attach texture cube to draw framebuffer"); + if (ViewDesc.ViewType == TEXTURE_VIEW_RENDER_TARGET || ViewDesc.ViewType == TEXTURE_VIEW_DEPTH_STENCIL) + { + glFramebufferTexture(GL_DRAW_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip); + CHECK_GL_ERROR("Failed to attach texture cube to draw framebuffer"); + } glFramebufferTexture(GL_READ_FRAMEBUFFER, AttachmentPoint, m_GlTexture, ViewDesc.MostDetailedMip); CHECK_GL_ERROR("Failed to attach texture cube to read framebuffer"); } @@ -279,13 +268,16 @@ void TextureCube_GL::AttachToFramebuffer(const TextureViewDesc& ViewDesc, GLenum // Texture name must either be zero or the name of an existing 3D texture, 1D or 2D array texture, // cube map array texture, or multisample array texture. - auto CubeMapFaceBindTarget = CubeMapFaces[ViewDesc.FirstArraySlice]; + auto CubeMapFaceBindTarget = GetCubeMapFaceBindTarget(ViewDesc.FirstArraySlice); // For glFramebufferTexture2D, if texture is not zero, textarget must be one of GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE, // GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, // GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, // or GL_TEXTURE_2D_MULTISAMPLE. - glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, AttachmentPoint, CubeMapFaceBindTarget, m_GlTexture, ViewDesc.MostDetailedMip); - CHECK_GL_ERROR("Failed to attach texture cube face to draw framebuffer"); + if (ViewDesc.ViewType == TEXTURE_VIEW_RENDER_TARGET || ViewDesc.ViewType == TEXTURE_VIEW_DEPTH_STENCIL) + { + glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, AttachmentPoint, CubeMapFaceBindTarget, m_GlTexture, ViewDesc.MostDetailedMip); + CHECK_GL_ERROR("Failed to attach texture cube face to draw framebuffer"); + } glFramebufferTexture2D(GL_READ_FRAMEBUFFER, AttachmentPoint, CubeMapFaceBindTarget, m_GlTexture, ViewDesc.MostDetailedMip); CHECK_GL_ERROR("Failed to attach texture cube face to read framebuffer"); } @@ -295,4 +287,20 @@ void TextureCube_GL::AttachToFramebuffer(const TextureViewDesc& ViewDesc, GLenum } } +void TextureCube_GL::CopyTexSubimage(GLContextState& GLState, const CopyTexSubimageAttribs& Attribs) +{ + auto CubeMapFaceBindTarget = GetCubeMapFaceBindTarget(Attribs.DstLayer); + GLState.BindTexture(-1, CubeMapFaceBindTarget, GetGLHandle()); + + glCopyTexSubImage2D(CubeMapFaceBindTarget, + Attribs.DstMip, + Attribs.DstX, + Attribs.DstY, + Attribs.SrcBox.MinX, + Attribs.SrcBox.MinY, + Attribs.SrcBox.Width(), + Attribs.SrcBox.Height()); + CHECK_GL_ERROR("Failed to copy subimage data to texture cube"); +} + } // namespace Diligent