Pioneer
Renderer.h
Go to the documentation of this file.
1 // Copyright © 2008-2021 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 "libs.h"
12 #include "matrix4x4.h"
13 #include <map>
14 #include <memory>
15 
16 namespace Graphics {
17 
18  /*
19  * Renderer base class. A Renderer draws points, lines, triangles.
20  * It is also used to create render states, materials and vertex/index buffers.
21  */
22 
23  class Material;
24  class MaterialDescriptor;
25  class RenderState;
26  class RenderTarget;
27  class Texture;
28  class TextureDescriptor;
29  class VertexArray;
30  class VertexBuffer;
31  class IndexBuffer;
32  class InstanceBuffer;
33  struct VertexBufferDesc;
34  struct RenderStateDesc;
35  struct RenderTargetDesc;
36 
37  // Renderer base, functions return false if
38  // failed/unsupported
39  class Renderer {
40  private:
41  typedef std::pair<std::string, std::string> TextureCacheKey;
42  typedef std::map<TextureCacheKey, RefCountedPtr<Texture> *> TextureCacheMap;
43 
44  public:
45  using TextureCache = TextureCacheMap;
46 
47  Renderer(SDL_Window *win, int width, int height);
48  virtual ~Renderer();
49 
50  virtual const char *GetName() const = 0;
51  virtual RendererType GetRendererType() const = 0;
52 
53  virtual void WriteRendererInfo(std::ostream &out) const {}
54 
55  virtual void CheckRenderErrors(const char *func = nullptr, const int line = -1) const {}
56 
57  virtual bool SupportsInstancing() = 0;
58 
59  SDL_Window *GetSDLWindow() const { return m_window; }
60  float GetDisplayAspect() const { return static_cast<float>(m_width) / static_cast<float>(m_height); }
61  int GetWindowWidth() const { return m_width; }
62  int GetWindowHeight() const { return m_height; }
63  virtual int GetMaximumNumberAASamples() const = 0;
64 
65  //get supported minimum for z near and maximum for z far values
66  virtual bool GetNearFarRange(float &near_, float &far_) const = 0;
67 
68  virtual bool BeginFrame() = 0;
69  virtual bool EndFrame() = 0;
70  //traditionally gui happens between endframe and swapbuffers
71  virtual bool SwapBuffers() = 0;
72 
73  //set 0 to render to screen
74  virtual bool SetRenderTarget(RenderTarget *) = 0;
75 
76  //clear color and depth buffer
77  virtual bool ClearScreen() = 0;
78  //clear depth buffer
79  virtual bool ClearDepthBuffer() = 0;
80  virtual bool SetClearColor(const Color &c) = 0;
81 
82  virtual bool SetViewport(Viewport vp) = 0;
83  virtual Viewport GetViewport() const = 0;
84 
85  //set the model view matrix
86  virtual bool SetTransform(const matrix4x4f &m) = 0;
87  virtual matrix4x4f GetTransform() const = 0;
88 
89  //set projection matrix
90  virtual bool SetPerspectiveProjection(float fov, float aspect, float near_, float far_) = 0;
91  virtual bool SetOrthographicProjection(float xmin, float xmax, float ymin, float ymax, float zmin, float zmax) = 0;
92  virtual bool SetProjection(const matrix4x4f &m) = 0;
93  virtual matrix4x4f GetProjection() const = 0;
94 
95  virtual bool SetRenderState(RenderState *) = 0;
96 
97  // XXX maybe GL-specific. maybe should be part of the render state
98  virtual bool SetDepthRange(double znear, double zfar) = 0;
99  virtual bool ResetDepthRange() = 0;
100 
101  virtual bool SetWireFrameMode(bool enabled) = 0;
102 
103  virtual bool SetLights(Uint32 numlights, const Light *l) = 0;
104  const Light &GetLight(const Uint32 idx) const
105  {
106  assert(idx < 4);
107  return m_lights[idx];
108  }
109  virtual Uint32 GetNumLights() const { return 0; }
110  virtual bool SetAmbientColor(const Color &c) = 0;
111  const Color &GetAmbientColor() const { return m_ambient; }
112 
113  virtual bool SetScissor(bool enabled, const vector2f &pos = vector2f(0.0f), const vector2f &size = vector2f(0.0f)) = 0;
114 
115  //drawing functions
116  //2d drawing is generally understood to be for gui use (unlit, ortho projection)
117  //unindexed triangle draw
118  virtual bool DrawTriangles(const VertexArray *vertices, RenderState *state, Material *material, PrimitiveType type = TRIANGLES) = 0;
119  //high amount of textured quads for particles etc
120  virtual bool DrawPointSprites(const Uint32 count, const vector3f *positions, RenderState *rs, Material *material, float size) = 0;
121  virtual bool DrawPointSprites(const Uint32 count, const vector3f *positions, const vector2f *offsets, const float *sizes, RenderState *rs, Material *material) = 0;
122  //complex unchanging geometry that is worthwhile to store in VBOs etc.
125  // instanced variations of the above
128 
129  //creates a unique material based on the descriptor. It will not be deleted automatically.
130  virtual Material *CreateMaterial(const MaterialDescriptor &descriptor) = 0;
131  virtual Texture *CreateTexture(const TextureDescriptor &descriptor) = 0;
133  //returns 0 if unsupported
136  virtual IndexBuffer *CreateIndexBuffer(Uint32 size, BufferUsage) = 0;
137  virtual InstanceBuffer *CreateInstanceBuffer(Uint32 size, BufferUsage) = 0;
138 
139  Texture *GetCachedTexture(const std::string &type, const std::string &name);
140  void AddCachedTexture(const std::string &type, const std::string &name, Texture *texture);
141  void RemoveCachedTexture(const std::string &type, const std::string &name);
143 
144  const TextureCache &GetTextureCache() { return m_textureCache; }
145 
146  virtual bool ReloadShaders() = 0;
147 
148  // take a ticket representing the current renderer state. when the ticket
149  // is deleted, the renderer state is restored
150  // XXX state must die
151  class StateTicket {
152  public:
154  m_renderer(r)
155  {
156  m_renderer->PushState();
157  m_storedVP = m_renderer->GetViewport();
158  m_storedProj = m_renderer->GetProjection();
159  m_storedMV = m_renderer->GetTransform();
160  }
161 
162  virtual ~StateTicket()
163  {
164  m_renderer->PopState();
165  m_renderer->SetViewport(m_storedVP);
166  m_renderer->SetTransform(m_storedMV);
167  m_renderer->SetProjection(m_storedProj);
168  }
169 
170  StateTicket(const StateTicket &) = delete;
171  StateTicket &operator=(const StateTicket &) = delete;
172 
173  private:
174  Renderer *m_renderer;
175  matrix4x4f m_storedProj;
176  matrix4x4f m_storedMV;
177  Viewport m_storedVP;
178  };
179 
180  // Temporarily save the current transform matrix to do non-destructive drawing.
181  // XXX state has died, does this need to die further?
182  class MatrixTicket {
183  public:
185  MatrixTicket(r, r->GetTransform())
186  {}
187 
188  MatrixTicket(Renderer *r, const matrix4x4f &newMat) :
189  m_renderer(r)
190  {
191  m_storedMat = m_renderer->GetTransform();
192  m_renderer->SetTransform(newMat);
193  }
194 
195  virtual ~MatrixTicket()
196  {
197  m_renderer->SetTransform(m_storedMat);
198  }
199 
200  MatrixTicket(const MatrixTicket &) = delete;
201  MatrixTicket &operator=(const MatrixTicket &) = delete;
202 
203  private:
204  Renderer *m_renderer;
205  matrix4x4f m_storedMat;
206  };
207 
208  virtual bool Screendump(ScreendumpState &sd) { return false; }
209  virtual bool FrameGrab(ScreendumpState &sd) { return false; }
210 
211  Stats &GetStats() { return m_stats; }
212 
213  protected:
214  int m_width;
215  int m_height;
219  SDL_Window *m_window;
220 
221  virtual void PushState() = 0;
222  virtual void PopState() = 0;
223 
224  private:
225  TextureCacheMap m_textureCache;
226  };
227 
228 } // namespace Graphics
229 
230 #endif
Definition: VertexBuffer.h:114
Definition: VertexBuffer.h:136
Definition: Light.h:14
Definition: Material.h:57
Definition: Material.h:81
Definition: RenderState.h:25
Definition: RenderTarget.h:38
Definition: Renderer.h:182
virtual ~MatrixTicket()
Definition: Renderer.h:195
MatrixTicket(const MatrixTicket &)=delete
MatrixTicket & operator=(const MatrixTicket &)=delete
MatrixTicket(Renderer *r)
Definition: Renderer.h:184
MatrixTicket(Renderer *r, const matrix4x4f &newMat)
Definition: Renderer.h:188
Definition: Renderer.h:151
StateTicket & operator=(const StateTicket &)=delete
StateTicket(Renderer *r)
Definition: Renderer.h:153
StateTicket(const StateTicket &)=delete
virtual ~StateTicket()
Definition: Renderer.h:162
Definition: Renderer.h:39
Renderer(SDL_Window *win, int width, int height)
Definition: Renderer.cpp:9
virtual int GetMaximumNumberAASamples() const =0
virtual bool DrawPointSprites(const Uint32 count, const vector3f *positions, const vector2f *offsets, const float *sizes, RenderState *rs, Material *material)=0
virtual bool DrawBufferIndexedInstanced(VertexBuffer *, IndexBuffer *, RenderState *, Material *, InstanceBuffer *, PrimitiveType=TRIANGLES)=0
virtual bool SetClearColor(const Color &c)=0
virtual bool SetProjection(const matrix4x4f &m)=0
Light m_lights[4]
Definition: Renderer.h:217
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 const char * GetName() const =0
virtual void PushState()=0
virtual bool SupportsInstancing()=0
virtual bool DrawBuffer(VertexBuffer *, RenderState *, Material *, PrimitiveType type=TRIANGLES)=0
virtual bool FrameGrab(ScreendumpState &sd)
Definition: Renderer.h:209
virtual bool SetLights(Uint32 numlights, const Light *l)=0
const Light & GetLight(const Uint32 idx) const
Definition: Renderer.h:104
float GetDisplayAspect() const
Definition: Renderer.h:60
virtual InstanceBuffer * CreateInstanceBuffer(Uint32 size, BufferUsage)=0
Stats m_stats
Definition: Renderer.h:218
virtual bool SetRenderState(RenderState *)=0
virtual bool DrawTriangles(const VertexArray *vertices, RenderState *state, Material *material, PrimitiveType type=TRIANGLES)=0
virtual RenderTarget * CreateRenderTarget(const RenderTargetDesc &)=0
const Color & GetAmbientColor() const
Definition: Renderer.h:111
virtual bool SetViewport(Viewport vp)=0
virtual bool DrawBufferInstanced(VertexBuffer *, RenderState *, Material *, InstanceBuffer *, PrimitiveType type=TRIANGLES)=0
virtual bool SetDepthRange(double znear, double zfar)=0
virtual Material * CreateMaterial(const MaterialDescriptor &descriptor)=0
SDL_Window * GetSDLWindow() const
Definition: Renderer.h:59
Texture * GetCachedTexture(const std::string &type, const std::string &name)
Definition: Renderer.cpp:23
virtual bool SwapBuffers()=0
virtual IndexBuffer * CreateIndexBuffer(Uint32 size, BufferUsage)=0
int GetWindowHeight() const
Definition: Renderer.h:62
void AddCachedTexture(const std::string &type, const std::string &name, Texture *texture)
Definition: Renderer.cpp:30
virtual bool Screendump(ScreendumpState &sd)
Definition: Renderer.h:208
virtual bool SetTransform(const matrix4x4f &m)=0
int m_width
Definition: Renderer.h:214
void RemoveCachedTexture(const std::string &type, const std::string &name)
Definition: Renderer.cpp:36
void RemoveAllCachedTextures()
Definition: Renderer.cpp:44
virtual void PopState()=0
Color m_ambient
Definition: Renderer.h:216
virtual Texture * CreateTexture(const TextureDescriptor &descriptor)=0
virtual bool ResetDepthRange()=0
SDL_Window * m_window
Definition: Renderer.h:219
virtual bool BeginFrame()=0
virtual Uint32 GetNumLights() const
Definition: Renderer.h:109
TextureCacheMap TextureCache
Definition: Renderer.h:45
virtual void CheckRenderErrors(const char *func=nullptr, const int line=-1) const
Definition: Renderer.h:55
virtual void WriteRendererInfo(std::ostream &out) const
Definition: Renderer.h:53
virtual bool ClearDepthBuffer()=0
virtual RenderState * CreateRenderState(const RenderStateDesc &)=0
virtual bool SetScissor(bool enabled, const vector2f &pos=vector2f(0.0f), const vector2f &size=vector2f(0.0f))=0
virtual VertexBuffer * CreateVertexBuffer(const VertexBufferDesc &)=0
const TextureCache & GetTextureCache()
Definition: Renderer.h:144
virtual matrix4x4f GetTransform() const =0
virtual matrix4x4f GetProjection() const =0
virtual RendererType GetRendererType() const =0
virtual Viewport GetViewport() const =0
virtual bool DrawBufferIndexed(VertexBuffer *, IndexBuffer *, RenderState *, Material *, PrimitiveType=TRIANGLES)=0
int GetWindowWidth() const
Definition: Renderer.h:61
virtual bool SetAmbientColor(const Color &c)=0
virtual bool ClearScreen()=0
virtual ~Renderer()
Definition: Renderer.cpp:17
virtual bool EndFrame()=0
Stats & GetStats()
Definition: Renderer.h:211
virtual bool SetRenderTarget(RenderTarget *)=0
virtual bool GetNearFarRange(float &near_, float &far_) const =0
virtual bool DrawPointSprites(const Uint32 count, const vector3f *positions, RenderState *rs, Material *material, float size)=0
int m_height
Definition: Renderer.h:215
Definition: Stats.h:19
Definition: Texture.h:54
Definition: Texture.h:106
Definition: VertexArray.h:19
Definition: VertexBuffer.h:80
Definition: Background.h:13
RendererType
Definition: Graphics.h:17
PrimitiveType
Definition: Types.h:46
@ TRIANGLES
Definition: Types.h:51
BufferUsage
Definition: Types.h:34
Definition: Color.h:66
Definition: RenderState.h:10
Definition: RenderTarget.h:20
Definition: Graphics.h:95
Definition: VertexBuffer.h:41
Definition: Graphics.h:58
vector2< float > vector2f
Definition: vector2.h:126