Line data Source code
1 : /* Copyright (C) 2023 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 "renderer/backend/Format.h"
22 : #include "renderer/backend/gl/Buffer.h"
23 : #include "renderer/backend/gl/DeviceForward.h"
24 : #include "renderer/backend/gl/Framebuffer.h"
25 : #include "renderer/backend/gl/ShaderProgram.h"
26 : #include "renderer/backend/gl/Texture.h"
27 : #include "renderer/backend/IDevice.h"
28 : #include "scriptinterface/ScriptForward.h"
29 :
30 : #include <memory>
31 : #include <string>
32 : #include <tuple>
33 : #include <unordered_map>
34 : #include <vector>
35 :
36 : typedef struct SDL_Window SDL_Window;
37 : typedef void* SDL_GLContext;
38 :
39 : namespace Renderer
40 : {
41 :
42 : namespace Backend
43 : {
44 :
45 : namespace GL
46 : {
47 :
48 : class CDeviceCommandContext;
49 :
50 0 : class CDevice final : public IDevice
51 : {
52 : public:
53 : ~CDevice() override;
54 :
55 : /**
56 : * Creates the GL device and the GL context for the window if it presents.
57 : */
58 : static std::unique_ptr<IDevice> Create(SDL_Window* window, const bool arb);
59 :
60 0 : Backend GetBackend() const override { return m_ARB ? Backend::GL_ARB : Backend::GL; }
61 :
62 0 : const std::string& GetName() const override { return m_Name; }
63 0 : const std::string& GetVersion() const override { return m_Version; }
64 0 : const std::string& GetDriverInformation() const override { return m_DriverInformation; }
65 0 : const std::vector<std::string>& GetExtensions() const override { return m_Extensions; }
66 :
67 : void Report(const ScriptRequest& rq, JS::HandleValue settings) override;
68 :
69 : std::unique_ptr<IDeviceCommandContext> CreateCommandContext() override;
70 :
71 : std::unique_ptr<IGraphicsPipelineState> CreateGraphicsPipelineState(
72 : const SGraphicsPipelineStateDesc& pipelineStateDesc) override;
73 :
74 : std::unique_ptr<IVertexInputLayout> CreateVertexInputLayout(
75 : const PS::span<const SVertexAttributeFormat> attributes) override;
76 :
77 0 : CDeviceCommandContext* GetActiveCommandContext() { return m_ActiveCommandContext; }
78 :
79 : std::unique_ptr<ITexture> CreateTexture(
80 : const char* name, const ITexture::Type type, const uint32_t usage,
81 : const Format format, const uint32_t width, const uint32_t height,
82 : const Sampler::Desc& defaultSamplerDesc, const uint32_t MIPLevelCount, const uint32_t sampleCount) override;
83 :
84 : std::unique_ptr<ITexture> CreateTexture2D(
85 : const char* name, const uint32_t usage,
86 : const Format format, const uint32_t width, const uint32_t height,
87 : const Sampler::Desc& defaultSamplerDesc, const uint32_t MIPLevelCount = 1, const uint32_t sampleCount = 1) override;
88 :
89 : std::unique_ptr<IFramebuffer> CreateFramebuffer(
90 : const char* name, SColorAttachment* colorAttachment,
91 : SDepthStencilAttachment* depthStencilAttachment) override;
92 :
93 : std::unique_ptr<IBuffer> CreateBuffer(
94 : const char* name, const IBuffer::Type type, const uint32_t size, const bool dynamic) override;
95 :
96 : std::unique_ptr<IShaderProgram> CreateShaderProgram(
97 : const CStr& name, const CShaderDefines& defines) override;
98 :
99 : bool AcquireNextBackbuffer() override;
100 :
101 : IFramebuffer* GetCurrentBackbuffer(
102 : const AttachmentLoadOp colorAttachmentLoadOp,
103 : const AttachmentStoreOp colorAttachmentStoreOp,
104 : const AttachmentLoadOp depthStencilAttachmentLoadOp,
105 : const AttachmentStoreOp depthStencilAttachmentStoreOp) override;
106 :
107 : void Present() override;
108 :
109 : void OnWindowResize(const uint32_t width, const uint32_t height) override;
110 :
111 0 : bool UseFramebufferInvalidating() const { return m_UseFramebufferInvalidating; }
112 :
113 : bool IsTextureFormatSupported(const Format format) const override;
114 :
115 : bool IsFramebufferFormatSupported(const Format format) const override;
116 :
117 : Format GetPreferredDepthStencilFormat(
118 : const uint32_t usage, const bool depth, const bool stencil) const override;
119 :
120 0 : const Capabilities& GetCapabilities() const override { return m_Capabilities; }
121 :
122 : private:
123 : CDevice();
124 :
125 : SDL_Window* m_Window = nullptr;
126 : SDL_GLContext m_Context = nullptr;
127 : int m_SurfaceDrawableWidth = 0, m_SurfaceDrawableHeight = 0;
128 :
129 : bool m_ARB = false;
130 :
131 : std::string m_Name;
132 : std::string m_Version;
133 : std::string m_DriverInformation;
134 : std::vector<std::string> m_Extensions;
135 :
136 : // GL can have the only one command context at once.
137 : // TODO: remove as soon as we have no GL code outside backend, currently
138 : // it's used only as a helper for transition.
139 : CDeviceCommandContext* m_ActiveCommandContext = nullptr;
140 :
141 : using BackbufferKey = std::tuple<
142 : AttachmentLoadOp, AttachmentStoreOp,
143 : AttachmentLoadOp, AttachmentStoreOp>;
144 : struct BackbufferKeyHash
145 : {
146 : size_t operator()(const BackbufferKey& key) const;
147 : };
148 : // We use std::unordered_map to avoid storing sizes of Attachment*Op
149 : // enumerations. If it becomes a performance issue we'll replace it
150 : // by an array.
151 : std::unordered_map<
152 : BackbufferKey, std::unique_ptr<CFramebuffer>, BackbufferKeyHash> m_Backbuffers;
153 : bool m_BackbufferAcquired = false;
154 : bool m_UseFramebufferInvalidating = false;
155 :
156 : Capabilities m_Capabilities{};
157 : };
158 :
159 : } // namespace GL
160 :
161 : } // namespace Backend
162 :
163 : } // namespace Renderer
164 :
165 : #endif // INCLUDED_RENDERER_BACKEND_GL_DEVICE
|