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_IDEVICE
19 : #define INCLUDED_RENDERER_BACKEND_IDEVICE
20 :
21 : #include "graphics/Color.h"
22 : #include "ps/containers/Span.h"
23 : #include "renderer/backend/Backend.h"
24 : #include "renderer/backend/Format.h"
25 : #include "renderer/backend/IBuffer.h"
26 : #include "renderer/backend/IDevice.h"
27 : #include "renderer/backend/IDeviceCommandContext.h"
28 : #include "renderer/backend/IFramebuffer.h"
29 : #include "renderer/backend/IShaderProgram.h"
30 : #include "renderer/backend/ITexture.h"
31 : #include "renderer/backend/PipelineState.h"
32 : #include "scriptinterface/ScriptForward.h"
33 :
34 : #include <memory>
35 : #include <string>
36 : #include <vector>
37 :
38 : class CShaderDefines;
39 : class CStr;
40 :
41 : namespace Renderer
42 : {
43 :
44 : namespace Backend
45 : {
46 :
47 8 : class IDevice
48 : {
49 : public:
50 : struct Capabilities
51 : {
52 : bool S3TC;
53 : bool ARBShaders;
54 : bool ARBShadersShadow;
55 : bool computeShaders;
56 : bool debugLabels;
57 : bool debugScopedLabels;
58 : bool multisampling;
59 : bool anisotropicFiltering;
60 : uint32_t maxSampleCount;
61 : float maxAnisotropy;
62 : uint32_t maxTextureSize;
63 : bool instancing;
64 : };
65 :
66 8 : virtual ~IDevice() {}
67 :
68 : virtual Backend GetBackend() const = 0;
69 :
70 : virtual const std::string& GetName() const = 0;
71 : virtual const std::string& GetVersion() const = 0;
72 : virtual const std::string& GetDriverInformation() const = 0;
73 : virtual const std::vector<std::string>& GetExtensions() const = 0;
74 :
75 : virtual void Report(const ScriptRequest& rq, JS::HandleValue settings) = 0;
76 :
77 : virtual std::unique_ptr<IDeviceCommandContext> CreateCommandContext() = 0;
78 :
79 : /**
80 : * Creates a graphics pipeline state. It's a caller responsibility to
81 : * guarantee a lifespan of IShaderProgram stored in the description.
82 : */
83 : virtual std::unique_ptr<IGraphicsPipelineState> CreateGraphicsPipelineState(
84 : const SGraphicsPipelineStateDesc& pipelineStateDesc) = 0;
85 :
86 : /**
87 : * Creates a vertex input layout. It's recommended to use as few different
88 : * layouts as posible.
89 : */
90 : virtual std::unique_ptr<IVertexInputLayout> CreateVertexInputLayout(
91 : const PS::span<const SVertexAttributeFormat> attributes) = 0;
92 :
93 : virtual std::unique_ptr<ITexture> CreateTexture(
94 : const char* name, const ITexture::Type type, const uint32_t usage,
95 : const Format format, const uint32_t width, const uint32_t height,
96 : const Sampler::Desc& defaultSamplerDesc, const uint32_t MIPLevelCount, const uint32_t sampleCount) = 0;
97 :
98 : virtual std::unique_ptr<ITexture> CreateTexture2D(
99 : const char* name, const uint32_t usage,
100 : const Format format, const uint32_t width, const uint32_t height,
101 : const Sampler::Desc& defaultSamplerDesc, const uint32_t MIPLevelCount = 1, const uint32_t sampleCount = 1) = 0;
102 :
103 : /**
104 : * @see IFramebuffer
105 : *
106 : * The color attachment and the depth-stencil attachment should not be
107 : * nullptr at the same time. There should not be many different clear
108 : * colors along all color attachments for all framebuffers created for
109 : * the device.
110 : *
111 : * @return A valid framebuffer if it was created successfully else nullptr.
112 : */
113 : virtual std::unique_ptr<IFramebuffer> CreateFramebuffer(
114 : const char* name, SColorAttachment* colorAttachment,
115 : SDepthStencilAttachment* depthStencilAttachment) = 0;
116 :
117 : virtual std::unique_ptr<IBuffer> CreateBuffer(
118 : const char* name, const IBuffer::Type type, const uint32_t size, const bool dynamic) = 0;
119 :
120 : virtual std::unique_ptr<IShaderProgram> CreateShaderProgram(
121 : const CStr& name, const CShaderDefines& defines) = 0;
122 :
123 : /**
124 : * Acquires a backbuffer for rendering a frame.
125 : *
126 : * @return True if it was successfully acquired and we can render to it.
127 : */
128 : virtual bool AcquireNextBackbuffer() = 0;
129 :
130 : /**
131 : * Returns a framebuffer for the current backbuffer with the required
132 : * attachment operations. It should not be called if the last
133 : * AcquireNextBackbuffer call returned false.
134 : *
135 : * It's guaranteed that for the same acquired backbuffer this function returns
136 : * a framebuffer with the same attachments and properties except load and
137 : * store operations.
138 : *
139 : * @return The last successfully acquired framebuffer that wasn't
140 : * presented.
141 : */
142 : virtual IFramebuffer* GetCurrentBackbuffer(
143 : const AttachmentLoadOp colorAttachmentLoadOp,
144 : const AttachmentStoreOp colorAttachmentStoreOp,
145 : const AttachmentLoadOp depthStencilAttachmentLoadOp,
146 : const AttachmentStoreOp depthStencilAttachmentStoreOp) = 0;
147 :
148 : /**
149 : * Presents the backbuffer to the swapchain queue to be flipped on a
150 : * screen. Should be called only if the last AcquireNextBackbuffer call
151 : * returned true.
152 : */
153 : virtual void Present() = 0;
154 :
155 : /**
156 : * Should be called on window surface resize. It's the device owner
157 : * responsibility to call that function. Shouldn't be called during
158 : * rendering to an acquired backbuffer.
159 : */
160 : virtual void OnWindowResize(const uint32_t width, const uint32_t height) = 0;
161 :
162 : virtual bool IsTextureFormatSupported(const Format format) const = 0;
163 :
164 : virtual bool IsFramebufferFormatSupported(const Format format) const = 0;
165 :
166 : /**
167 : * Returns the most suitable format for the usage. Returns
168 : * Format::UNDEFINED if there is no such format.
169 : */
170 : virtual Format GetPreferredDepthStencilFormat(
171 : const uint32_t usage, const bool depth, const bool stencil) const = 0;
172 :
173 : virtual const Capabilities& GetCapabilities() const = 0;
174 : };
175 :
176 : } // namespace Backend
177 :
178 : } // namespace Renderer
179 :
180 : #endif // INCLUDED_RENDERER_BACKEND_IDEVICE
|