Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Device.h
Go to the documentation of this file.
1/* Copyright (C) 2024 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
36typedef struct SDL_Window SDL_Window;
37typedef void* SDL_GLContext;
38
39namespace Renderer
40{
41
42namespace Backend
43{
44
45namespace GL
46{
47
48class CDeviceCommandContext;
49
50class CDevice final : public IDevice
51{
52public:
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<IComputePipelineState> CreateComputePipelineState(
75 const SComputePipelineStateDesc& pipelineStateDesc) override;
76
77 std::unique_ptr<IVertexInputLayout> CreateVertexInputLayout(
78 const PS::span<const SVertexAttributeFormat> attributes) override;
79
81
82 std::unique_ptr<ITexture> CreateTexture(
83 const char* name, const ITexture::Type type, const uint32_t usage,
84 const Format format, const uint32_t width, const uint32_t height,
85 const Sampler::Desc& defaultSamplerDesc, const uint32_t MIPLevelCount, const uint32_t sampleCount) override;
86
87 std::unique_ptr<ITexture> CreateTexture2D(
88 const char* name, const uint32_t usage,
89 const Format format, const uint32_t width, const uint32_t height,
90 const Sampler::Desc& defaultSamplerDesc, const uint32_t MIPLevelCount = 1, const uint32_t sampleCount = 1) override;
91
92 std::unique_ptr<IFramebuffer> CreateFramebuffer(
93 const char* name, SColorAttachment* colorAttachment,
94 SDepthStencilAttachment* depthStencilAttachment) override;
95
96 std::unique_ptr<IBuffer> CreateBuffer(
97 const char* name, const IBuffer::Type type, const uint32_t size, const uint32_t usage) override;
98
99 std::unique_ptr<IShaderProgram> CreateShaderProgram(
100 const CStr& name, const CShaderDefines& defines) override;
101
102 bool AcquireNextBackbuffer() override;
103
105 const AttachmentLoadOp colorAttachmentLoadOp,
106 const AttachmentStoreOp colorAttachmentStoreOp,
107 const AttachmentLoadOp depthStencilAttachmentLoadOp,
108 const AttachmentStoreOp depthStencilAttachmentStoreOp) override;
109
110 void Present() override;
111
112 void OnWindowResize(const uint32_t width, const uint32_t height) override;
113
115
116 bool IsTextureFormatSupported(const Format format) const override;
117
118 bool IsFramebufferFormatSupported(const Format format) const override;
119
121 const uint32_t usage, const bool depth, const bool stencil) const override;
122
123 const Capabilities& GetCapabilities() const override { return m_Capabilities; }
124
125private:
127
131
132 bool m_ARB = false;
133
134 std::string m_Name;
135 std::string m_Version;
137 std::vector<std::string> m_Extensions;
138
139 // GL can have the only one command context at once.
140 // TODO: remove as soon as we have no GL code outside backend, currently
141 // it's used only as a helper for transition.
143
144 using BackbufferKey = std::tuple<
148 {
149 size_t operator()(const BackbufferKey& key) const;
150 };
151 // We use std::unordered_map to avoid storing sizes of Attachment*Op
152 // enumerations. If it becomes a performance issue we'll replace it
153 // by an array.
154 std::unordered_map<
155 BackbufferKey, std::unique_ptr<CFramebuffer>, BackbufferKeyHash> m_Backbuffers;
158
160};
161
162} // namespace GL
163
164} // namespace Backend
165
166} // namespace Renderer
167
168#endif // INCLUDED_RENDERER_BACKEND_GL_DEVICE
struct SDL_Window SDL_Window
Definition: VideoMode.h:26
Represents a mapping of name strings to value strings, for use with #if and #ifdef and similar condit...
Definition: ShaderDefines.h:147
Simplifed version of std::span (C++20) as we don't support the original one yet.
Definition: Span.h:37
Definition: DeviceCommandContext.h:50
Definition: Device.h:51
std::string m_Version
Definition: Device.h:135
void Present() override
Presents the backbuffer to the swapchain queue to be flipped on a screen.
Definition: Device.cpp:955
Capabilities m_Capabilities
Definition: Device.h:159
bool AcquireNextBackbuffer() override
Acquires a backbuffer for rendering a frame.
Definition: Device.cpp:918
std::vector< std::string > m_Extensions
Definition: Device.h:137
std::unordered_map< BackbufferKey, std::unique_ptr< CFramebuffer >, BackbufferKeyHash > m_Backbuffers
Definition: Device.h:155
std::unique_ptr< IDeviceCommandContext > CreateCommandContext() override
Definition: Device.cpp:855
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:1057
int m_SurfaceDrawableHeight
Definition: Device.h:130
~CDevice() override
Definition: Device.cpp:457
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:880
bool m_ARB
Definition: Device.h:132
SDL_Window * m_Window
Definition: Device.h:128
const std::string & GetDriverInformation() const override
Definition: Device.h:64
std::unique_ptr< IGraphicsPipelineState > CreateGraphicsPipelineState(const SGraphicsPipelineStateDesc &pipelineStateDesc) override
Creates a graphics pipeline state.
Definition: Device.cpp:862
SDL_GLContext m_Context
Definition: Device.h:129
std::unique_ptr< IBuffer > CreateBuffer(const char *name, const IBuffer::Type type, const uint32_t size, const uint32_t usage) override
Definition: Device.cpp:906
std::unique_ptr< IVertexInputLayout > CreateVertexInputLayout(const PS::span< const SVertexAttributeFormat > attributes) override
Creates a vertex input layout.
Definition: Device.cpp:874
std::string m_Name
Definition: Device.h:134
bool UseFramebufferInvalidating() const
Definition: Device.h:114
CDeviceCommandContext * GetActiveCommandContext()
Definition: Device.h:80
void OnWindowResize(const uint32_t width, const uint32_t height) override
Should be called on window surface resize.
Definition: Device.cpp:980
const std::string & GetName() const override
Definition: Device.h:62
CDeviceCommandContext * m_ActiveCommandContext
Definition: Device.h:142
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:210
bool m_UseFramebufferInvalidating
Definition: Device.h:157
bool IsFramebufferFormatSupported(const Format format) const override
Definition: Device.cpp:1036
std::unique_ptr< IFramebuffer > CreateFramebuffer(const char *name, SColorAttachment *colorAttachment, SDepthStencilAttachment *depthStencilAttachment) override
Definition: Device.cpp:898
std::unique_ptr< IShaderProgram > CreateShaderProgram(const CStr &name, const CShaderDefines &defines) override
Definition: Device.cpp:912
const Capabilities & GetCapabilities() const override
Definition: Device.h:123
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:889
std::tuple< AttachmentLoadOp, AttachmentStoreOp, AttachmentLoadOp, AttachmentStoreOp > BackbufferKey
Definition: Device.h:146
std::string m_DriverInformation
Definition: Device.h:136
const std::string & GetVersion() const override
Definition: Device.h:63
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:935
bool IsTextureFormatSupported(const Format format) const override
Definition: Device.cpp:988
Backend GetBackend() const override
Definition: Device.h:60
const std::vector< std::string > & GetExtensions() const override
Definition: Device.h:65
void Report(const ScriptRequest &rq, JS::HandleValue settings) override
Definition: Device.cpp:463
int m_SurfaceDrawableWidth
Definition: Device.h:130
bool m_BackbufferAcquired
Definition: Device.h:156
std::unique_ptr< IComputePipelineState > CreateComputePipelineState(const SComputePipelineStateDesc &pipelineStateDesc) override
Creates a compute pipeline state.
Definition: Device.cpp:868
Type
Definition: IBuffer.h:35
Definition: IDevice.h:48
IFramebuffer stores attachments which should be used by backend as rendering destinations.
Definition: IFramebuffer.h:85
Type
Definition: ITexture.h:37
Spidermonkey maintains some 'local' state via the JSContext* object.
Definition: ScriptRequest.h:60
void * SDL_GLContext
Definition: Device.h:37
Format
Definition: Format.h:28
AttachmentStoreOp
Store operation is set for each attachment, what should be done with its content on EndFramebufferPas...
Definition: IFramebuffer.h:52
Backend
Definition: Backend.h:28
AttachmentLoadOp
Load operation is set for each attachment, what should be done with its content on BeginFramebufferPa...
Definition: IFramebuffer.h:37
Definition: VideoMode.h:29
size_t operator()(const BackbufferKey &key) const
Definition: Device.cpp:925
Definition: IFramebuffer.h:60
Definition: PipelineState.h:175
Definition: IFramebuffer.h:68
Definition: PipelineState.h:165
Definition: Sampler.h:57
unsigned int uint32_t
Definition: wposix_types.h:53
pthread_key_t key
Definition: wpthread.cpp:140