Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "SwapChain.h" 21 : 22 : #include "lib/config2.h" 23 : #include "lib/debug.h" 24 : #include "lib/hash.h" 25 : #include "ps/ConfigDB.h" 26 : #include "ps/CLogger.h" 27 : #include "ps/Profile.h" 28 : #include "renderer/backend/gl/Device.h" 29 : 30 : #include <SDL_video.h> 31 : 32 : namespace Renderer 33 : { 34 : 35 : namespace Backend 36 : { 37 : 38 : namespace GL 39 : { 40 : 41 : namespace 42 : { 43 : 44 : } // anonymous namespace 45 : 46 : // static 47 0 : std::unique_ptr<CSwapChain> CSwapChain::Create( 48 : CDevice* device, SDL_Window* window, 49 : int surfaceDrawableWidth, int surfaceDrawableHeight, 50 : const bool vsync) 51 : { 52 0 : std::unique_ptr<CSwapChain> swapChain(new CSwapChain()); 53 0 : swapChain->m_Device = device; 54 0 : swapChain->m_Window = window; 55 0 : swapChain->m_SurfaceDrawableWidth = surfaceDrawableWidth; 56 0 : swapChain->m_SurfaceDrawableHeight = surfaceDrawableHeight; 57 0 : SDL_GL_SetSwapInterval(vsync ? 1 : 0); 58 0 : return swapChain; 59 0 : } 60 : 61 0 : CSwapChain::CSwapChain() = default; 62 : 63 0 : CSwapChain::~CSwapChain() 64 : { 65 0 : } 66 : 67 0 : IDevice* CSwapChain::GetDevice() 68 : { 69 0 : return m_Device; 70 : } 71 : 72 0 : bool CSwapChain::AcquireNextBackbuffer() 73 : { 74 0 : ENSURE(!m_BackbufferAcquired); 75 0 : m_BackbufferAcquired = true; 76 0 : return true; 77 : } 78 : 79 0 : size_t CSwapChain::BackbufferKeyHash::operator()(const BackbufferKey& key) const 80 : { 81 0 : size_t seed = 0; 82 0 : hash_combine(seed, std::get<0>(key)); 83 0 : hash_combine(seed, std::get<1>(key)); 84 0 : hash_combine(seed, std::get<2>(key)); 85 0 : hash_combine(seed, std::get<3>(key)); 86 0 : return seed; 87 : } 88 : 89 0 : IFramebuffer* CSwapChain::GetCurrentBackbuffer( 90 : const AttachmentLoadOp colorAttachmentLoadOp, 91 : const AttachmentStoreOp colorAttachmentStoreOp, 92 : const AttachmentLoadOp depthStencilAttachmentLoadOp, 93 : const AttachmentStoreOp depthStencilAttachmentStoreOp) 94 : { 95 0 : const BackbufferKey key{ 96 : colorAttachmentLoadOp, colorAttachmentStoreOp, 97 0 : depthStencilAttachmentLoadOp, depthStencilAttachmentStoreOp}; 98 0 : auto it = m_Backbuffers.find(key); 99 0 : if (it == m_Backbuffers.end()) 100 : { 101 0 : it = m_Backbuffers.emplace(key, CFramebuffer::CreateBackbuffer( 102 : m_Device, m_SurfaceDrawableWidth, m_SurfaceDrawableHeight, 103 : colorAttachmentLoadOp, colorAttachmentStoreOp, 104 : depthStencilAttachmentLoadOp, depthStencilAttachmentStoreOp)).first; 105 : } 106 0 : return it->second.get(); 107 : } 108 : 109 0 : void CSwapChain::Present() 110 : { 111 0 : ENSURE(m_BackbufferAcquired); 112 0 : m_BackbufferAcquired = false; 113 : 114 0 : if (m_Window) 115 : { 116 0 : PROFILE3("swap buffers"); 117 0 : SDL_GL_SwapWindow(m_Window); 118 0 : } 119 : 120 : #if defined(NDEBUG) 121 0 : if (!g_ConfigDB.Get("gl.checkerrorafterswap", false)) 122 0 : return; 123 : #endif 124 0 : PROFILE3("error check"); 125 : // We have to check GL errors after SwapBuffer to avoid possible 126 : // synchronizations during rendering. 127 0 : if (GLenum err = glGetError()) 128 0 : ONCE(LOGERROR("GL error {:#04x} occurred", err)); 129 : } 130 : 131 : } // namespace GL 132 : 133 : } // namespace Backend 134 : 135 : } // namespace Renderer