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 float GetDisplayAspect() const { return static_cast<float>(m_width) / static_cast<float>(m_height); }
67 int GetWindowWidth() const { return m_width; }
68 int GetWindowHeight() const { return m_height; }
69 virtual int GetMaximumNumberAASamples() const = 0;
70
71 virtual void SetVSyncEnabled(bool enabled) = 0;
72
73 //get supported minimum for z near and maximum for z far values
74 virtual bool GetNearFarRange(float &near_, float &far_) const = 0;
75
76 virtual bool BeginFrame() = 0;
77 virtual bool EndFrame() = 0;
78 //traditionally gui happens between endframe and swapbuffers
79 virtual bool SwapBuffers() = 0;
80
81 // returns currently bound render target (if any)
83 //set 0 to render to screen
84 virtual bool SetRenderTarget(RenderTarget *) = 0;
85
86 // Copy a portion of one render target to another, optionally scaling the target
87 virtual void CopyRenderTarget(RenderTarget *src, RenderTarget *dst, ViewportExtents srcRect, ViewportExtents dstRect, bool linearFilter = true) = 0;
88
89 // Perform an MSAA resolve from a multisampled render target to regular render target
90 // No scaling can be performed.
92
93 // Set the scissor extents. This has no effect if not drawing with a renderstate using scissorTest.
94 // In particular, the scissor state will not affect clearing the screen.
95 virtual bool SetScissor(ViewportExtents scissor) = 0;
96
97 //clear color and depth buffer
98 virtual bool ClearScreen(const Color &c = Color::BLACK, bool depthBuffer = true) = 0;
99
100 //clear depth buffer
101 virtual bool ClearDepthBuffer() = 0;
102
103 virtual bool SetViewport(ViewportExtents vp) = 0;
104 virtual ViewportExtents GetViewport() const = 0;
105
106 //set the model view matrix
107 virtual bool SetTransform(const matrix4x4f &m) = 0;
108 virtual matrix4x4f GetTransform() const = 0;
109
110 //set projection matrix
111 virtual bool SetPerspectiveProjection(float fov, float aspect, float near_, float far_) = 0;
112 virtual bool SetOrthographicProjection(float xmin, float xmax, float ymin, float ymax, float zmin, float zmax) = 0;
113 virtual bool SetProjection(const matrix4x4f &m) = 0;
114 virtual matrix4x4f GetProjection() const = 0;
115
116 virtual bool SetWireFrameMode(bool enabled) = 0;
117
118 virtual bool SetLightIntensity(Uint32 numlights, const float *intensity) = 0;
119 virtual bool SetLights(Uint32 numlights, const Light *l) = 0;
120 const Light &GetLight(const Uint32 idx) const
121 {
122 assert(idx < 4);
123 return m_lights[idx];
124 }
125 virtual Uint32 GetNumLights() const { return 0; }
126 virtual bool SetAmbientColor(const Color &c) = 0;
127 const Color &GetAmbientColor() const { return m_ambient; }
128
129 //drawing functions
130 // TODO: placeholder API; here until CommandLists are exposed
131 // and all code can safely deal with async drawing
132 virtual bool FlushCommandBuffers() = 0;
133
134 // All drawing commands are assumed to defer execution of the command
135 // until the next commandlist flush. This is to batch GPU data updates
136 // and ensure state changes are minimal and internally consistent.
137 // If the calling code really needs all pending draw commands to be
138 // executed before making state changes, call FlushCommandBuffers to
139 // manually synchronize.
140
141 // Upload and draw the contents of this VertexArray. Should be used for highly dynamic geometry that changes per-frame.
142 // The contents of the VertexArray will be cached internally by the renderer and uploaded in bulk.
143 virtual bool DrawBuffer(const VertexArray *v, Material *m) = 0;
144 // Draw a subregion from an existing vertex+index buffer. Should be used for drawing aggregated vertex streams
145 // generated by middleware (e.g. UI buffers) that are updated once or twice during the frame.
146 // vtxOffset, idxOffset specify the starting element, not the starting byte offset in the buffer
147 virtual bool DrawBufferDynamic(VertexBuffer *v, uint32_t vtxOffset, IndexBuffer *i, uint32_t idxOffset, uint32_t numElems, Material *m) = 0;
148 // Draw a single mesh object (vertex+index buffer) using the given material.
149 virtual bool DrawMesh(MeshObject *, Material *) = 0;
150 // Draw multiple instances of a mesh object using the given material.
152
153 //creates a unique material based on the descriptor. It will not be deleted automatically.
154 virtual Material *CreateMaterial(const std::string &shader, const MaterialDescriptor &descriptor, const RenderStateDesc &stateDescriptor) = 0;
155 // Make a copy of the given material with a possibly new descriptor or render state.
156 virtual Material *CloneMaterial(const Material *mat, const MaterialDescriptor &descriptor, const RenderStateDesc &stateDescriptor) = 0;
157 virtual Texture *CreateTexture(const TextureDescriptor &descriptor) = 0;
158 virtual RenderTarget *CreateRenderTarget(const RenderTargetDesc &) = 0; //returns nullptr if unsupported
163
164 // Create a new mesh object that wraps the given vertex and index buffers.
165 virtual MeshObject *CreateMeshObject(VertexBuffer *vertexBuffer, IndexBuffer *indexBuffer = nullptr) = 0;
166
167 // Create a new mesh object and vertex buffer, and upload data from the given vertex array. Optionally associate the given index buffer.
168 // This is a convenience function to avoid boilerplate needed to set up a vertex buffer and mesh object from a vertex array.
169 // This function is not suitable for transient geometry; prefer DrawBuffer instead.
170 virtual MeshObject *CreateMeshObjectFromArray(const VertexArray *vertexArray, IndexBuffer *indexBuffer = nullptr, BufferUsage usage = BUFFER_USAGE_STATIC) = 0;
171
172 // Return a reference to the render state desc that is used by the given material.
173 virtual const RenderStateDesc &GetMaterialRenderState(const Material *mat) = 0;
174
175 Texture *GetCachedTexture(const std::string &type, const std::string &name);
176 void AddCachedTexture(const std::string &type, const std::string &name, Texture *texture);
177 void RemoveCachedTexture(const std::string &type, const std::string &name);
179
180 const TextureCache &GetTextureCache() { return m_textureCache; }
181
182 virtual bool ReloadShaders() = 0;
183
184 // take a ticket representing the current renderer state. when the ticket
185 // is deleted, the renderer state is restored
186 // XXX state must die
188 public:
190 m_renderer(r)
191 {
192 m_renderer->PushState();
193 m_storedVP = m_renderer->GetViewport();
194 m_storedProj = m_renderer->GetProjection();
195 m_storedMV = m_renderer->GetTransform();
196 m_storedRT = m_renderer->GetRenderTarget();
197 }
198
199 virtual ~StateTicket()
200 {
201 m_renderer->PopState();
202 m_renderer->SetRenderTarget(m_storedRT);
203 m_renderer->SetViewport(m_storedVP);
204 m_renderer->SetTransform(m_storedMV);
205 m_renderer->SetProjection(m_storedProj);
206 }
207
208 StateTicket(const StateTicket &) = delete;
210
211 private:
212 Renderer *m_renderer;
213 RenderTarget *m_storedRT;
214 matrix4x4f m_storedProj;
215 matrix4x4f m_storedMV;
216 ViewportExtents m_storedVP;
217 };
218
219 // Temporarily save the current transform matrix to do non-destructive drawing.
221 public:
224 {
225 }
226
227 MatrixTicket(Renderer *r, const matrix4x4f &newMat) :
228 m_renderer(r)
229 {
230 m_storedMat = m_renderer->GetTransform();
231 m_renderer->SetTransform(newMat);
232 }
233
235 {
236 m_renderer->SetTransform(m_storedMat);
237 }
238
239 MatrixTicket(const MatrixTicket &) = delete;
241
242 private:
243 Renderer *m_renderer;
244 matrix4x4f m_storedMat;
245 };
246
247 virtual bool Screendump(ScreendumpState &sd) { return false; }
248
249 Stats &GetStats() { return m_stats; }
250
251 // Returns a hashed name for referring to material constant slots and other constant-size string names
252 static constexpr size_t GetName(std::string_view s) { return hash_64_fnv1a(s.data(), s.size()); }
253
254 protected:
260 SDL_Window *m_window;
261
262 virtual void PushState() = 0;
263 virtual void PopState() = 0;
264
265 private:
266 TextureCacheMap m_textureCache;
267 };
268
269} // namespace Graphics
270
271#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:220
virtual ~MatrixTicket()
Definition: Renderer.h:234
MatrixTicket(const MatrixTicket &)=delete
MatrixTicket(Renderer *r)
Definition: Renderer.h:222
MatrixTicket & operator=(const MatrixTicket &)=delete
MatrixTicket(Renderer *r, const matrix4x4f &newMat)
Definition: Renderer.h:227
Definition: Renderer.h:187
StateTicket & operator=(const StateTicket &)=delete
StateTicket(Renderer *r)
Definition: Renderer.h:189
StateTicket(const StateTicket &)=delete
virtual ~StateTicket()
Definition: Renderer.h:199
Definition: Renderer.h:45
virtual const char * GetName() const =0
virtual int GetMaximumNumberAASamples() const =0
const Color & GetAmbientColor() const
Definition: Renderer.h:127
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:258
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:66
Stats m_stats
Definition: Renderer.h:259
Stats & GetStats()
Definition: Renderer.h:249
const TextureCache & GetTextureCache()
Definition: Renderer.h:180
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:252
Texture * GetCachedTexture(const std::string &type, const std::string &name)
Definition: Renderer.cpp:26
virtual bool SwapBuffers()=0
int GetWindowHeight() const
Definition: Renderer.h:68
void AddCachedTexture(const std::string &type, const std::string &name, Texture *texture)
Definition: Renderer.cpp:33
virtual bool Screendump(ScreendumpState &sd)
Definition: Renderer.h:247
virtual bool SetTransform(const matrix4x4f &m)=0
int m_width
Definition: Renderer.h:255
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:257
virtual bool ClearScreen(const Color &c=Color::BLACK, bool depthBuffer=true)=0
SDL_Window * m_window
Definition: Renderer.h:260
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:125
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:120
int GetWindowWidth() const
Definition: Renderer.h:67
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:256
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:134
Definition: VertexBuffer.h:43
Definition: Graphics.h:59