Pyrogenesis  trunk
IDevice.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_IDEVICE
19 #define INCLUDED_RENDERER_BACKEND_IDEVICE
20 
21 #include "graphics/Color.h"
22 #include "ps/containers/Span.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 class IDevice
48 {
49 public:
50  struct Capabilities
51  {
52  bool S3TC;
53  bool ARBShaders;
63  bool instancing;
64  };
65 
66  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  */
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  */
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
virtual Backend GetBackend() const =0
AttachmentLoadOp
Load operation is set for each attachment, what should be done with its content on BeginFramebufferPa...
Definition: IFramebuffer.h:36
bool anisotropicFiltering
Definition: IDevice.h:59
virtual bool AcquireNextBackbuffer()=0
Acquires a backbuffer for rendering a frame.
Type
Definition: ITexture.h:36
uint32_t maxSampleCount
Definition: IDevice.h:60
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
virtual const Capabilities & GetCapabilities() const =0
virtual std::unique_ptr< IGraphicsPipelineState > CreateGraphicsPipelineState(const SGraphicsPipelineStateDesc &pipelineStateDesc)=0
Creates a graphics pipeline state.
virtual const std::vector< std::string > & GetExtensions() const =0
virtual bool IsTextureFormatSupported(const Format format) const =0
virtual std::unique_ptr< IDeviceCommandContext > CreateCommandContext()=0
virtual const std::string & GetName() const =0
bool ARBShaders
Definition: IDevice.h:53
bool computeShaders
Definition: IDevice.h:55
Format
Definition: Format.h:27
float maxAnisotropy
Definition: IDevice.h:61
virtual IFramebuffer * GetCurrentBackbuffer(const AttachmentLoadOp colorAttachmentLoadOp, const AttachmentStoreOp colorAttachmentStoreOp, const AttachmentLoadOp depthStencilAttachmentLoadOp, const AttachmentStoreOp depthStencilAttachmentStoreOp)=0
Returns a framebuffer for the current backbuffer with the required attachment operations.
virtual void Present()=0
Presents the backbuffer to the swapchain queue to be flipped on a screen.
bool instancing
Definition: IDevice.h:63
uint32_t maxTextureSize
Definition: IDevice.h:62
bool S3TC
Definition: IDevice.h:52
bool multisampling
Definition: IDevice.h:58
virtual std::unique_ptr< IShaderProgram > CreateShaderProgram(const CStr &name, const CShaderDefines &defines)=0
Definition: IDevice.h:47
virtual std::unique_ptr< IBuffer > CreateBuffer(const char *name, const IBuffer::Type type, const uint32_t size, const bool dynamic)=0
Definition: PipelineState.h:164
virtual std::unique_ptr< IFramebuffer > CreateFramebuffer(const char *name, SColorAttachment *colorAttachment, SDepthStencilAttachment *depthStencilAttachment)=0
Definition: IFramebuffer.h:67
Backend
Definition: Backend.h:27
Represents a mapping of name strings to value strings, for use with #if and #ifdef and similar condit...
Definition: ShaderDefines.h:146
virtual ~IDevice()
Definition: IDevice.h:66
virtual Format GetPreferredDepthStencilFormat(const uint32_t usage, const bool depth, const bool stencil) const =0
Returns the most suitable format for the usage.
virtual void Report(const ScriptRequest &rq, JS::HandleValue settings)=0
virtual 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)=0
unsigned int uint32_t
Definition: wposix_types.h:53
Definition: VideoMode.h:28
bool ARBShadersShadow
Definition: IDevice.h:54
virtual const std::string & GetDriverInformation() const =0
virtual void OnWindowResize(const uint32_t width, const uint32_t height)=0
Should be called on window surface resize.
virtual const std::string & GetVersion() const =0
virtual 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)=0
Definition: IFramebuffer.h:59
Definition: Sampler.h:56
bool debugScopedLabels
Definition: IDevice.h:57
Spidermonkey maintains some &#39;local&#39; state via the JSContext* object.
Definition: ScriptRequest.h:59
IFramebuffer stores attachments which should be used by backend as rendering destinations.
Definition: IFramebuffer.h:84
virtual bool IsFramebufferFormatSupported(const Format format) const =0
virtual std::unique_ptr< IVertexInputLayout > CreateVertexInputLayout(const PS::span< const SVertexAttributeFormat > attributes)=0
Creates a vertex input layout.
bool debugLabels
Definition: IDevice.h:56
Simplifed version of std::span (C++20) as we don&#39;t support the original one yet.
Definition: Span.h:36