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 : #ifndef INCLUDED_RENDERER_BACKEND_GL_DEVICE 19 : #define INCLUDED_RENDERER_BACKEND_GL_DEVICE 20 : 21 : #include "lib/config2.h" 22 : #include "ps/CStr.h" 23 : #include "renderer/backend/Backend.h" 24 : #include "renderer/backend/IBuffer.h" 25 : #include "renderer/backend/IDevice.h" 26 : #include "renderer/backend/IFramebuffer.h" 27 : #include "renderer/backend/ITexture.h" 28 : #include "renderer/backend/gl/DeviceForward.h" 29 : 30 : #if CONFIG2_GLES 31 : #include "external_libraries/opengles2_wrapper.h" 32 : #else 33 : #include <glad/gl.h> 34 : #endif 35 : 36 : #include <cstddef> 37 : #include <cstdint> 38 : #include <js/TypeDecls.h> 39 : #include <memory> 40 : #include <span> 41 : #include <string> 42 : #include <tuple> 43 : #include <unordered_map> 44 : #include <vector> 45 : 46 : namespace Renderer::Backend::GL { class CDeviceCommandContext; } 47 : namespace Renderer::Backend::GL { class CFramebuffer; } 48 : 49 : typedef struct SDL_Window SDL_Window; 50 : typedef void* SDL_GLContext; 51 : 52 : namespace Renderer 53 : { 54 : 55 : namespace Backend 56 : { 57 : 58 : namespace GL 59 : { 60 : 61 : class CDevice final : public IDevice 62 : { 63 : public: 64 : ~CDevice() override; 65 : 66 : /** 67 : * Creates the GL device and the GL context for the window if it presents. 68 : */ 69 : static std::unique_ptr<IDevice> Create(SDL_Window* window); 70 : 71 0 : Backend GetBackend() const override { return Backend::GL; } 72 : 73 0 : const std::string& GetName() const override { return m_Name; } 74 0 : const std::string& GetVersion() const override { return m_Version; } 75 0 : const std::string& GetDriverInformation() const override { return m_DriverInformation; } 76 0 : const std::vector<std::string>& GetExtensions() const override { return m_Extensions; } 77 : 78 : void Report(const Script::Request& rq, JS::HandleValue settings) override; 79 : 80 : std::unique_ptr<IDeviceCommandContext> CreateCommandContext() override; 81 : 82 : std::unique_ptr<ISwapChain> CreateSwapChain( 83 : const char* name, SDL_Window* window, 84 : int surfaceDrawableWidth, int surfaceDrawableHeight, 85 : const bool vsync, std::unique_ptr<ISwapChain> oldSwapChain) override; 86 : 87 : void WaitUntilIdle() override; 88 : 89 : std::unique_ptr<IGraphicsPipelineState> CreateGraphicsPipelineState( 90 : const SGraphicsPipelineStateDesc& pipelineStateDesc) override; 91 : 92 : std::unique_ptr<IComputePipelineState> CreateComputePipelineState( 93 : const SComputePipelineStateDesc& pipelineStateDesc) override; 94 : 95 : std::unique_ptr<IVertexInputLayout> CreateVertexInputLayout( 96 : const std::span<const SVertexAttributeFormat> attributes) override; 97 : 98 0 : CDeviceCommandContext* GetActiveCommandContext() { return m_ActiveCommandContext; } 99 : 100 : std::unique_ptr<ITexture> CreateTexture( 101 : const char* name, const ITexture::Type type, const uint32_t usage, 102 : const Format format, const uint32_t width, const uint32_t height, 103 : const Sampler::Desc& defaultSamplerDesc, const uint32_t MIPLevelCount, const uint32_t sampleCount) override; 104 : 105 : std::unique_ptr<ITexture> CreateTexture2D( 106 : const char* name, const uint32_t usage, 107 : const Format format, const uint32_t width, const uint32_t height, 108 : const Sampler::Desc& defaultSamplerDesc, const uint32_t MIPLevelCount = 1, const uint32_t sampleCount = 1) override; 109 : 110 : std::unique_ptr<IFramebuffer> CreateFramebuffer( 111 : const char* name, SColorAttachment* colorAttachment, 112 : SDepthStencilAttachment* depthStencilAttachment) override; 113 : 114 : std::unique_ptr<IBuffer> CreateBuffer( 115 : const char* name, const IBuffer::Type type, const uint32_t size, const uint32_t usage) override; 116 : 117 : std::unique_ptr<IShaderProgram> CreateShaderProgram( 118 : const CStr& name, const CShaderDefines& defines) override; 119 : 120 0 : bool UseFramebufferInvalidating() const { return m_UseFramebufferInvalidating; } 121 : 122 : bool IsTextureFormatSupported(const Format format) const override; 123 : 124 : bool IsFramebufferFormatSupported(const Format format) const override; 125 : 126 : Format GetPreferredDepthStencilFormat( 127 : const uint32_t usage, const bool depth, const bool stencil) const override; 128 : 129 : uint32_t AllocateQuery() override; 130 : 131 : void FreeQuery(const uint32_t handle) override; 132 : 133 : bool IsQueryResultAvailable(const uint32_t handle) const override; 134 : 135 : uint64_t GetQueryResult(const uint32_t handle) override; 136 : 137 : void InsertTimestampQuery(const uint32_t handle); 138 : 139 0 : const Capabilities& GetCapabilities() const override { return m_Capabilities; } 140 : 141 : void CollectStatistics(StatisticsVector& statistics) const override; 142 : 143 : private: 144 : CDevice(); 145 : 146 : SDL_Window* m_Window = nullptr; 147 : SDL_GLContext m_Context = nullptr; 148 : 149 : std::string m_Name; 150 : std::string m_Version; 151 : std::string m_DriverInformation; 152 : std::vector<std::string> m_Extensions; 153 : 154 : // GL can have the only one command context at once. 155 : // TODO: remove as soon as we have no GL code outside backend, currently 156 : // it's used only as a helper for transition. 157 : CDeviceCommandContext* m_ActiveCommandContext = nullptr; 158 : 159 : bool m_UseFramebufferInvalidating = false; 160 : 161 0 : struct Query 162 : { 163 : GLuint query{}; 164 : bool occupied{}; 165 : }; 166 : std::vector<Query> m_Queries; 167 : 168 : Capabilities m_Capabilities{}; 169 : 170 : bool m_R8Supported{false}; 171 : }; 172 : 173 : } // namespace GL 174 : 175 : } // namespace Backend 176 : 177 : } // namespace Renderer 178 : 179 : #endif // INCLUDED_RENDERER_BACKEND_GL_DEVICE