Pioneer
Loading...
Searching...
No Matches
Renderer.h
Go to the documentation of this file.
1// Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
2// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3
4#ifndef _RENDERER_H
5#define _RENDERER_H
6
7#include "Graphics.h"
8#include "Light.h"
9#include "Stats.h"
10#include "Types.h"
11#include "core/StringHash.h"
13#include "matrix4x4.h"
14#include <map>
15#include <memory>
16
17struct SDL_Window;
18
19namespace Graphics {
20
21 /*
22 * Renderer base class. A Renderer draws points, lines, triangles.
23 * It is also used to create render states, materials and vertex/index buffers.
24 */
25
26 class IndexBuffer;
27 class InstanceBuffer;
28 class Material;
29 class MaterialDescriptor;
30 class MeshObject;
31 class RenderState;
32 class RenderTarget;
33 class Texture;
34 class TextureDescriptor;
35 class UniformBuffer;
36 class VertexArray;
37 class VertexBuffer;
38
39 struct VertexBufferDesc;
40 struct RenderStateDesc;
41 struct RenderTargetDesc;
42
43 // Renderer base, functions return false if
44 // failed/unsupported
45 class Renderer {
46 private:
47 typedef std::pair<std::string, std::string> TextureCacheKey;
48 typedef std::map<TextureCacheKey, RefCountedPtr<Texture> *> TextureCacheMap;
49
50 public:
51 using TextureCache = TextureCacheMap;
52
53 Renderer(SDL_Window *win, int width, int height);
54 virtual ~Renderer();
55
56 virtual const char *GetName() const = 0;
57 virtual RendererType GetRendererType() const = 0;
58
59 virtual void WriteRendererInfo(std::ostream &out) const {}
60
61 virtual void CheckRenderErrors(const char *func = nullptr, const int line = -1) const {}
62
63 virtual bool SupportsInstancing() = 0;
64
65 SDL_Window *GetSDLWindow() const { return m_window; }
66 virtual void OnWindowResized() {};
67
68 float GetDisplayAspect() const { return static_cast<float>(m_width) / static_cast<float>(m_height); }
69 int GetWindowWidth() const { return m_width; }
70 int GetWindowHeight() const { return m_height; }
71 virtual int GetMaximumNumberAASamples() const = 0;
72
73 virtual void SetVSyncEnabled(bool enabled) = 0;
74
75 //get supported minimum for z near and maximum for z far values
76 virtual bool GetNearFarRange(float &near_, float &far_) const = 0;
77
78 virtual bool BeginFrame() = 0;
79 virtual bool EndFrame() = 0;
80 //traditionally gui happens between endframe and swapbuffers
81 virtual bool SwapBuffers() = 0;
82
83 // returns currently bound render target (if any)
85 //set 0 to render to screen
86 virtual bool SetRenderTarget(RenderTarget *) = 0;
87
88 // Copy a portion of one render target to another, optionally scaling the target
89 virtual void CopyRenderTarget(RenderTarget *src, RenderTarget *dst, ViewportExtents srcRect, ViewportExtents dstRect, bool linearFilter = true) = 0;
90
91 // Perform an MSAA resolve from a multisampled render target to regular render target
92 // No scaling can be performed.
94
95 // Set the scissor extents. This has no effect if not drawing with a renderstate using scissorTest.
96 // In particular, the scissor state will not affect clearing the screen.
97 virtual bool SetScissor(ViewportExtents scissor) = 0;
98
99 //clear color and depth buffer
100 virtual bool ClearScreen(const Color &c = Color::BLACK, bool depthBuffer = true) = 0;
101
102 //clear depth buffer
103 virtual bool ClearDepthBuffer() = 0;
104
105 virtual bool SetViewport(ViewportExtents vp) = 0;
106 virtual ViewportExtents GetViewport() const = 0;
107
108 //set the model view matrix
109 virtual bool SetTransform(const matrix4x4f &m) = 0;
110 virtual matrix4x4f GetTransform() const = 0;
111
112 //set projection matrix
113 virtual bool SetPerspectiveProjection(float fov, float aspect, float near_, float far_) = 0;
114 virtual bool SetOrthographicProjection(float xmin, float xmax, float ymin, float ymax, float zmin, float zmax) = 0;
115 virtual bool SetProjection(const matrix4x4f &m) = 0;
116 virtual matrix4x4f GetProjection() const = 0;
117
118 virtual bool SetWireFrameMode(bool enabled) = 0;
119
120 virtual bool SetLightIntensity(Uint32 numlights, const float *intensity) = 0;
121 virtual bool SetLights(Uint32 numlights, const Light *l) = 0;
122 const Light &GetLight(const Uint32 idx) const
123 {
124 assert(idx < 4);
125 return m_lights[idx];
126 }
127 virtual Uint32 GetNumLights() const { return 0; }
128 virtual bool SetAmbientColor(const Color &c) = 0;
129 const Color &GetAmbientColor() const { return m_ambient; }
130
131 //drawing functions
132 // TODO: placeholder API; here until CommandLists are exposed
133 // and all code can safely deal with async drawing
134 virtual bool FlushCommandBuffers() = 0;
135
136 // All drawing commands are assumed to defer execution of the command
137 // until the next commandlist flush. This is to batch GPU data updates
138 // and ensure state changes are minimal and internally consistent.
139 // If the calling code really needs all pending draw commands to be
140 // executed before making state changes, call FlushCommandBuffers to
141 // manually synchronize.
142
143 // Upload and draw the contents of this VertexArray. Should be used for highly dynamic geometry that changes per-frame.
144 // The contents of the VertexArray will be cached internally by the renderer and uploaded in bulk.
145 virtual bool DrawBuffer(const VertexArray *v, Material *m) = 0;
146 // Draw a subregion from an existing vertex+index buffer. Should be used for drawing aggregated vertex streams
147 // generated by middleware (e.g. UI buffers) that are updated once or twice during the frame.
148 // vtxOffset, idxOffset specify the starting element, not the starting byte offset in the buffer
149 virtual bool DrawBufferDynamic(VertexBuffer *v, uint32_t vtxOffset, IndexBuffer *i, uint32_t idxOffset, uint32_t numElems, Material *m) = 0;
150 // Draw a single mesh object (vertex+index buffer) using the given material.
151 virtual bool DrawMesh(MeshObject *, Material *) = 0;
152 // Draw multiple instances of a mesh object using the given material.
154
155 //creates a unique material based on the descriptor. It will not be deleted automatically.
156 virtual Material *CreateMaterial(const std::string &shader, const MaterialDescriptor &descriptor, const RenderStateDesc &stateDescriptor) = 0;
157 // Make a copy of the given material with a possibly new descriptor or render state.
158 virtual Material *CloneMaterial(const Material *mat, const MaterialDescriptor &descriptor, const RenderStateDesc &stateDescriptor) = 0;
159 virtual Texture *CreateTexture(const TextureDescriptor &descriptor) = 0;
160 virtual RenderTarget *CreateRenderTarget(const RenderTargetDesc &) = 0; //returns nullptr if unsupported
165
166 // Create a new mesh object that wraps the given vertex and index buffers.
167 virtual MeshObject *CreateMeshObject(VertexBuffer *vertexBuffer, IndexBuffer *indexBuffer = nullptr) = 0;
168
169 // Create a new mesh object and vertex buffer, and upload data from the given vertex array. Optionally associate the given index buffer.
170 // This is a convenience function to avoid boilerplate needed to set up a vertex buffer and mesh object from a vertex array.
171 // This function is not suitable for transient geometry; prefer DrawBuffer instead.
172 virtual MeshObject *CreateMeshObjectFromArray(const VertexArray *vertexArray, IndexBuffer *indexBuffer = nullptr, BufferUsage usage = BUFFER_USAGE_STATIC) = 0;
173
174 // Return a reference to the render state desc that is used by the given material.
175 virtual const RenderStateDesc &GetMaterialRenderState(const Material *mat) = 0;
176
177 Texture *GetCachedTexture(const std::string &type, const std::string &name);
178 void AddCachedTexture(const std::string &type, const std::string &name, Texture *texture);
179 void RemoveCachedTexture(const std::string &type, const std::string &name);
181
182 const TextureCache &GetTextureCache() { return m_textureCache; }
183
184 virtual bool ReloadShaders() = 0;
185
186 // take a ticket representing the current renderer state. when the ticket
187 // is deleted, the renderer state is restored
188 // XXX state must die
190 public:
192 m_renderer(r)
193 {
194 m_renderer->PushState();
195 m_storedVP = m_renderer->GetViewport();
196 m_storedProj = m_renderer->GetProjection();
197 m_storedMV = m_renderer->GetTransform();
198 m_storedRT = m_renderer->GetRenderTarget();
199 }
200
201 virtual ~StateTicket()
202 {
203 m_renderer->PopState();
204 m_renderer->SetRenderTarget(m_storedRT);
205 m_renderer->SetViewport(m_storedVP);
206 m_renderer->SetTransform(m_storedMV);
207 m_renderer->SetProjection(m_storedProj);
208 }
209
210 StateTicket(const StateTicket &) = delete;
212
213 private:
214 Renderer *m_renderer;
215 RenderTarget *m_storedRT;
216 matrix4x4f m_storedProj;
217 matrix4x4f m_storedMV;
218 ViewportExtents m_storedVP;
219 };
220
221 // Temporarily save the current transform matrix to do non-destructive drawing.
223 public:
226 {
227 }
228
229 MatrixTicket(Renderer *r, const matrix4x4f &newMat) :
230 m_renderer(r)
231 {
232 m_storedMat = m_renderer->GetTransform();
233 m_renderer->SetTransform(newMat);
234 }
235
237 {
238 m_renderer->SetTransform(m_storedMat);
239 }
240
241 MatrixTicket(const MatrixTicket &) = delete;
243
244 private:
245 Renderer *m_renderer;
246 matrix4x4f m_storedMat;
247 };
248
249 virtual bool Screendump(ScreendumpState &sd) { return false; }
250
251 Stats &GetStats() { return m_stats; }
252
253 // Returns a hashed name for referring to material constant slots and other constant-size string names
254 static constexpr size_t GetName(std::string_view s) { return hash_64_fnv1a(s.data(), s.size()); }
255
256 protected:
262 SDL_Window *m_window;
263
264 virtual void PushState() = 0;
265 virtual void PopState() = 0;
266
267 private:
268 TextureCacheMap m_textureCache;
269 };
270
271} // namespace Graphics
272
273#endif
constexpr uint64_t hash_64_fnv1a(const char *const data, size_t len)
Definition FNV1a.h:29
Definition VertexBuffer.h:102
Definition VertexBuffer.h:127
Definition Light.h:14
Definition Material.h:60
Definition Material.h:148
Definition VertexBuffer.h:156
Definition RenderTarget.h:38
Definition Renderer.h:222
virtual ~MatrixTicket()
Definition Renderer.h:236
MatrixTicket(const MatrixTicket &)=delete
MatrixTicket(Renderer *r)
Definition Renderer.h:224
MatrixTicket & operator=(const MatrixTicket &)=delete
MatrixTicket(Renderer *r, const matrix4x4f &newMat)
Definition Renderer.h:229
Definition Renderer.h:189
StateTicket & operator=(const StateTicket &)=delete
StateTicket(Renderer *r)
Definition Renderer.h:191
StateTicket(const StateTicket &)=delete
virtual ~StateTicket()
Definition Renderer.h:201
Definition Renderer.h:45
virtual const char * GetName() const =0
virtual int GetMaximumNumberAASamples() const =0
const Color & GetAmbientColor() const
Definition Renderer.h:129
virtual void OnWindowResized()
Definition Renderer.h:66
virtual MeshObject * CreateMeshObjectFromArray(const VertexArray *vertexArray, IndexBuffer *indexBuffer=nullptr, BufferUsage usage=BUFFER_USAGE_STATIC)=0
virtual bool DrawBufferDynamic(VertexBuffer *v, uint32_t vtxOffset, IndexBuffer *i, uint32_t idxOffset, uint32_t numElems, Material *m)=0
virtual VertexBuffer * CreateVertexBuffer(const VertexBufferDesc &)=0
virtual bool SetProjection(const matrix4x4f &m)=0
Light m_lights[4]
Definition Renderer.h:260
virtual bool SetOrthographicProjection(float xmin, float xmax, float ymin, float ymax, float zmin, float zmax)=0
virtual bool ReloadShaders()=0
virtual bool SetWireFrameMode(bool enabled)=0
virtual bool SetPerspectiveProjection(float fov, float aspect, float near_, float far_)=0
virtual void PushState()=0
virtual bool SetLightIntensity(Uint32 numlights, const float *intensity)=0
virtual bool SupportsInstancing()=0
virtual ViewportExtents GetViewport() const =0
virtual Texture * CreateTexture(const TextureDescriptor &descriptor)=0
virtual bool SetLights(Uint32 numlights, const Light *l)=0
virtual bool SetScissor(ViewportExtents scissor)=0
float GetDisplayAspect() const
Definition Renderer.h:68
Stats m_stats
Definition Renderer.h:261
Stats & GetStats()
Definition Renderer.h:251
const TextureCache & GetTextureCache()
Definition Renderer.h:182
virtual Material * CloneMaterial(const Material *mat, const MaterialDescriptor &descriptor, const RenderStateDesc &stateDescriptor)=0
virtual void ResolveRenderTarget(RenderTarget *src, RenderTarget *dst, ViewportExtents rect)=0
virtual bool FlushCommandBuffers()=0
static constexpr size_t GetName(std::string_view s)
Definition Renderer.h:254
Texture * GetCachedTexture(const std::string &type, const std::string &name)
Definition Renderer.cpp:26
virtual bool SwapBuffers()=0
int GetWindowHeight() const
Definition Renderer.h:70
void AddCachedTexture(const std::string &type, const std::string &name, Texture *texture)
Definition Renderer.cpp:33
virtual bool Screendump(ScreendumpState &sd)
Definition Renderer.h:249
virtual bool SetTransform(const matrix4x4f &m)=0
int m_width
Definition Renderer.h:257
virtual bool DrawMesh(MeshObject *, Material *)=0
void RemoveCachedTexture(const std::string &type, const std::string &name)
Definition Renderer.cpp:39
void RemoveAllCachedTextures()
Definition Renderer.cpp:47
virtual void PopState()=0
virtual RenderTarget * GetRenderTarget()=0
Color m_ambient
Definition Renderer.h:259
virtual bool ClearScreen(const Color &c=Color::BLACK, bool depthBuffer=true)=0
SDL_Window * m_window
Definition Renderer.h:262
virtual UniformBuffer * CreateUniformBuffer(Uint32 size, BufferUsage)=0
virtual bool BeginFrame()=0
virtual RenderTarget * CreateRenderTarget(const RenderTargetDesc &)=0
virtual bool DrawMeshInstanced(MeshObject *, Material *, InstanceBuffer *)=0
virtual bool DrawBuffer(const VertexArray *v, Material *m)=0
virtual Uint32 GetNumLights() const
Definition Renderer.h:127
TextureCacheMap TextureCache
Definition Renderer.h:51
virtual void CheckRenderErrors(const char *func=nullptr, const int line=-1) const
Definition Renderer.h:61
virtual void SetVSyncEnabled(bool enabled)=0
virtual void WriteRendererInfo(std::ostream &out) const
Definition Renderer.h:59
virtual bool ClearDepthBuffer()=0
virtual InstanceBuffer * CreateInstanceBuffer(Uint32 size, BufferUsage)=0
SDL_Window * GetSDLWindow() const
Definition Renderer.h:65
virtual const RenderStateDesc & GetMaterialRenderState(const Material *mat)=0
virtual matrix4x4f GetTransform() const =0
virtual matrix4x4f GetProjection() const =0
virtual RendererType GetRendererType() const =0
virtual IndexBuffer * CreateIndexBuffer(Uint32 size, BufferUsage, IndexBufferSize=INDEX_BUFFER_32BIT)=0
virtual bool SetViewport(ViewportExtents vp)=0
const Light & GetLight(const Uint32 idx) const
Definition Renderer.h:122
int GetWindowWidth() const
Definition Renderer.h:69
virtual bool SetAmbientColor(const Color &c)=0
virtual void CopyRenderTarget(RenderTarget *src, RenderTarget *dst, ViewportExtents srcRect, ViewportExtents dstRect, bool linearFilter=true)=0
virtual ~Renderer()
Definition Renderer.cpp:20
virtual bool EndFrame()=0
virtual bool SetRenderTarget(RenderTarget *)=0
virtual bool GetNearFarRange(float &near_, float &far_) const =0
virtual MeshObject * CreateMeshObject(VertexBuffer *vertexBuffer, IndexBuffer *indexBuffer=nullptr)=0
virtual Material * CreateMaterial(const std::string &shader, const MaterialDescriptor &descriptor, const RenderStateDesc &stateDescriptor)=0
int m_height
Definition Renderer.h:258
Definition Stats.h:19
Definition Texture.h:51
Definition Texture.h:103
Definition UniformBuffer.h:11
Definition VertexArray.h:22
Definition VertexBuffer.h:65
Definition Background.h:14
RendererType
Definition Graphics.h:18
IndexBufferSize
Definition Types.h:76
@ INDEX_BUFFER_32BIT
Definition Types.h:78
BufferUsage
Definition Types.h:65
@ BUFFER_USAGE_STATIC
Definition Types.h:66
Definition Color.h:66
static const Color4ub BLACK
Definition Color.h:155
Definition RenderState.h:10
Definition RenderTarget.h:20
Definition Graphics.h:132
Definition VertexBuffer.h:43
Definition Graphics.h:60