Pyrogenesis  trunk
Device.h
Go to the documentation of this file.
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 
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 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  Backend GetBackend() const override { return m_ARB ? Backend::GL_ARB : Backend::GL; }
61 
62  const std::string& GetName() const override { return m_Name; }
63  const std::string& GetVersion() const override { return m_Version; }
64  const std::string& GetDriverInformation() const override { return m_DriverInformation; }
65  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 
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 
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 
112 
113  bool IsTextureFormatSupported(const Format format) const override;
114 
115  bool IsFramebufferFormatSupported(const Format format) const override;
116 
118  const uint32_t usage, const bool depth, const bool stencil) const override;
119 
120  const Capabilities& GetCapabilities() const override { return m_Capabilities; }
121 
122 private:
123  CDevice();
124 
125  SDL_Window* m_Window = nullptr;
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.
140 
141  using BackbufferKey = std::tuple<
143  AttachmentLoadOp, AttachmentStoreOp>;
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;
155 
157 };
158 
159 } // namespace GL
160 
161 } // namespace Backend
162 
163 } // namespace Renderer
164 
165 #endif // INCLUDED_RENDERER_BACKEND_GL_DEVICE
AttachmentLoadOp
Load operation is set for each attachment, what should be done with its content on BeginFramebufferPa...
Definition: IFramebuffer.h:36
void OnWindowResize(const uint32_t width, const uint32_t height) override
Should be called on window surface resize.
Definition: Device.cpp:984
Definition: DeviceCommandContext.h:49
bool m_UseFramebufferInvalidating
Definition: Device.h:154
bool m_ARB
Definition: Device.h:129
const Capabilities & GetCapabilities() const override
Definition: Device.h:120
Type
Definition: ITexture.h:36
AttachmentStoreOp
Store operation is set for each attachment, what should be done with its content on EndFramebufferPas...
Definition: IFramebuffer.h:51
Type
Definition: IBuffer.h:34
const std::string & GetVersion() const override
Definition: Device.h:63
std::unique_ptr< IShaderProgram > CreateShaderProgram(const CStr &name, const CShaderDefines &defines) override
Definition: Device.cpp:916
bool UseFramebufferInvalidating() const
Definition: Device.h:111
std::unique_ptr< ITexture > CreateTexture(const char *name, const ITexture::Type type, const uint32_t usage, const Format format, const uint32_t width, const uint32_t height, const Sampler::Desc &defaultSamplerDesc, const uint32_t MIPLevelCount, const uint32_t sampleCount) override
Definition: Device.cpp:884
std::string m_Version
Definition: Device.h:132
std::unique_ptr< IVertexInputLayout > CreateVertexInputLayout(const PS::span< const SVertexAttributeFormat > attributes) override
Creates a vertex input layout.
Definition: Device.cpp:878
void Present() override
Presents the backbuffer to the swapchain queue to be flipped on a screen.
Definition: Device.cpp:959
void Report(const ScriptRequest &rq, JS::HandleValue settings) override
Definition: Device.cpp:473
CDeviceCommandContext * GetActiveCommandContext()
Definition: Device.h:77
std::unique_ptr< IGraphicsPipelineState > CreateGraphicsPipelineState(const SGraphicsPipelineStateDesc &pipelineStateDesc) override
Creates a graphics pipeline state.
Definition: Device.cpp:872
size_t operator()(const BackbufferKey &key) const
Definition: Device.cpp:929
std::tuple< AttachmentLoadOp, AttachmentStoreOp, AttachmentLoadOp, AttachmentStoreOp > BackbufferKey
Definition: Device.h:143
Format
Definition: Format.h:27
std::unordered_map< BackbufferKey, std::unique_ptr< CFramebuffer >, BackbufferKeyHash > m_Backbuffers
Definition: Device.h:152
Backend GetBackend() const override
Definition: Device.h:60
void * SDL_GLContext
Definition: Device.h:37
pthread_key_t key
Definition: wpthread.cpp:140
Definition: IDevice.h:47
std::string m_DriverInformation
Definition: Device.h:133
Definition: PipelineState.h:164
SDL_GLContext m_Context
Definition: Device.h:126
const std::string & GetDriverInformation() const override
Definition: Device.h:64
Definition: IFramebuffer.h:67
Backend
Definition: Backend.h:27
~CDevice() override
Definition: Device.cpp:467
struct SDL_Window SDL_Window
Definition: VideoMode.h:26
static std::unique_ptr< IDevice > Create(SDL_Window *window, const bool arb)
Creates the GL device and the GL context for the window if it presents.
Definition: Device.cpp:229
int m_SurfaceDrawableHeight
Definition: Device.h:127
bool IsFramebufferFormatSupported(const Format format) const override
Definition: Device.cpp:1040
Represents a mapping of name strings to value strings, for use with #if and #ifdef and similar condit...
Definition: ShaderDefines.h:146
std::unique_ptr< IBuffer > CreateBuffer(const char *name, const IBuffer::Type type, const uint32_t size, const bool dynamic) override
Definition: Device.cpp:910
std::vector< std::string > m_Extensions
Definition: Device.h:134
const std::string & GetName() const override
Definition: Device.h:62
unsigned int uint32_t
Definition: wposix_types.h:53
std::unique_ptr< IDeviceCommandContext > CreateCommandContext() override
Definition: Device.cpp:865
Definition: VideoMode.h:28
Format GetPreferredDepthStencilFormat(const uint32_t usage, const bool depth, const bool stencil) const override
Returns the most suitable format for the usage.
Definition: Device.cpp:1061
int m_SurfaceDrawableWidth
Definition: Device.h:127
std::unique_ptr< IFramebuffer > CreateFramebuffer(const char *name, SColorAttachment *colorAttachment, SDepthStencilAttachment *depthStencilAttachment) override
Definition: Device.cpp:902
SDL_Window * m_Window
Definition: Device.h:125
Definition: IFramebuffer.h:59
Definition: Sampler.h:56
IFramebuffer * GetCurrentBackbuffer(const AttachmentLoadOp colorAttachmentLoadOp, const AttachmentStoreOp colorAttachmentStoreOp, const AttachmentLoadOp depthStencilAttachmentLoadOp, const AttachmentStoreOp depthStencilAttachmentStoreOp) override
Returns a framebuffer for the current backbuffer with the required attachment operations.
Definition: Device.cpp:939
const std::vector< std::string > & GetExtensions() const override
Definition: Device.h:65
bool IsTextureFormatSupported(const Format format) const override
Definition: Device.cpp:992
Spidermonkey maintains some &#39;local&#39; state via the JSContext* object.
Definition: ScriptRequest.h:59
std::string m_Name
Definition: Device.h:131
IFramebuffer stores attachments which should be used by backend as rendering destinations.
Definition: IFramebuffer.h:84
bool m_BackbufferAcquired
Definition: Device.h:153
CDeviceCommandContext * m_ActiveCommandContext
Definition: Device.h:139
Capabilities m_Capabilities
Definition: Device.h:156
std::unique_ptr< ITexture > CreateTexture2D(const char *name, const uint32_t usage, const Format format, const uint32_t width, const uint32_t height, const Sampler::Desc &defaultSamplerDesc, const uint32_t MIPLevelCount=1, const uint32_t sampleCount=1) override
Definition: Device.cpp:893
Definition: Device.h:50
bool AcquireNextBackbuffer() override
Acquires a backbuffer for rendering a frame.
Definition: Device.cpp:922
Simplifed version of std::span (C++20) as we don&#39;t support the original one yet.
Definition: Span.h:36