Pioneer
Material.h
Go to the documentation of this file.
1 // Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef _MATERIAL_H
5 #define _MATERIAL_H
6 /*
7  * Materials are used to apply an appropriate shader and other rendering parameters.
8  * Users request materials from the renderer by filling a MaterialDescriptor structure,
9  * and calling Renderer::CreateMaterial.
10  * Users are responsible for deleting a material they have requested. This is because materials
11  * are rarely shareable.
12  * Material::Apply is called by renderer before drawing, and Unapply after drawing (to restore state).
13  * For the OGL renderer, a Material is always accompanied by a Program.
14  */
15 #include "Color.h"
16 #include "RefCounted.h"
17 #include "graphics/BufferCommon.h"
18 #include "matrix4x4.h"
19 
20 namespace Graphics {
21 
22  class Texture;
23  class RendererOGL;
24  class UniformBuffer;
25 
26  // Shorthand for unique effects
27  // The other descriptor parameters may or may not have effect,
28  // depends on the effect
29  enum EffectType {
48  };
49 
50  // XXX : there must be a better place to put this
52  HAS_ATMOSPHERE = 1 << 0,
53  HAS_ECLIPSES = 1 << 1,
55  HAS_OCTAVES = 1 << 15
56  };
57 
58  // Renderer creates a material that best matches these requirements.
59  // EffectType may override some of the other flags.
61  public:
64  bool alphaTest;
65  bool glowMap;
66  bool ambientMap;
67  bool lighting;
68  bool normalMap;
70  bool usePatterns; //pattern/color system
72  bool instanced;
73  Sint32 textures; //texture count
74  Uint32 dirLights; //set by RendererOGL if lighting == true
75  Uint32 quality; // see: Graphics::MaterialQuality
76 
77  friend bool operator==(const MaterialDescriptor &a, const MaterialDescriptor &b);
78  };
79 
80  /*
81  * Materials contain all the needed information to take some triangles
82  * (or points, or lines) and get colored pixels on screen. They come with
83  * some basic state common to all materials, and any additional state is
84  * defined in <.shaderdef> files found in the data/shaders/<API> directory.
85  *
86  * Material state comes in three main types:
87  *
88  * - Push Constants are simple typed values set every time this material
89  * is used to draw something. The underlying renderer API may have
90  * constraints on how many push constants can be used in a single
91  * material, so they're a good candidate for indexes into a buffer or
92  * texture array, but not for passing large amounts of data. Expect
93  * a worst-case maximum size of 64 bytes (1 matrix4x4f).
94  *
95  * - Textures are, like the name implies, images of any sort and size.
96  * You can only bind a texture to a slot that has the same underlying
97  * texture type (e.g. Texture2D, Cubemap), and it is the calling code's
98  * responsibility to keep the texture alive until the renderer is done
99  * using it (more on that later).
100  *
101  * - Buffers are chunks of data passed from the calling code to the shader
102  * program running on the GPU. The structure of the buffer is determined
103  * by agreement between the calling code and the shader; no translation
104  * is performed by the Material system.
105  * Buffers are allocated in increments of 256 bytes, so if you only have
106  * one or two vectors to send to the shader, please use Push Constants
107  * instead. Trust me, your framerate will thank you.
108  * It is the calling code's responsibility to ensure that buffers set
109  * with SetBufferDynamic are set *at least* once per frame; failing
110  * to observe this will cause the shader to be sent undefined data.
111  *
112  * Now that you know about state, let's talk about ownership.
113  *
114  * Push Constants are easy. Once you set them on a material, they stay and
115  * are used for every draw until you change them again.
116  *
117  * Once you set a texture on a material and draw, it is the calling code's
118  * responsibility to ensure that texture stays alive until the end of the
119  * frame, even if you then set another texture on the material.
120  * Most textures are acquired from the TextureCache and thus this isn't a
121  * problem. If you're doing something special that doesn't involve the
122  * TexCache, stop yourself and ask why; then remember to keep the texture
123  * alive until the next SwapBuffers().
124  *
125  * Buffer objects are simpler - if you call SetBufferDynamic, then the
126  * passed data will be copied and managed until the end of the frame.
127  * However, once SwapBuffers() is called, all dynamic data is rendered
128  * INVALID and must be re-uploaded. Failure to do so will cause the shader
129  * to read garbage data the next time you draw.
130  * If you're reading from dynamic buffer data to control loop execution in
131  * the shader, failure to update the buffer binding WILL cause a GPU hang.
132  * (There, I've saved you twenty minutes of debugging.)
133  *
134  * If your data needs to be shared between multiple materials or only needs
135  * to be updated sporadically, use SetBuffer instead; you'll get the
136  * BufferBinding object from the Renderer interface. If the buffer lives
137  * longer than a frame, you'll need to externally manage its lifetime.
138  *
139  * If you delete (or cause to be deleted by decrementing ref counts) a
140  * Texture or static Buffer object, it is your responsibility to set
141  * the Texture or Buffer reference to <nullptr>. Failure to do so will
142  * result in undefined behavior.
143  *
144  * Additionally, failure to properly set all Buffer bindings which are
145  * actively used by the shader code will likely result in undefined
146  * behavior and possibly a GPU hang or API error.
147  */
148  class Material : public RefCounted {
149  public:
150  Material();
151  virtual ~Material() {}
152 
156  float shininess; //specular power 0-128
157 
158  //XXX may not be necessary. Used by newmodel to check if a material uses patterns
159  const MaterialDescriptor &GetDescriptor() const { return m_descriptor; }
160 
161  virtual bool IsProgramLoaded() const = 0;
162 
163  virtual bool SetTexture(size_t hash, Texture *tex) = 0;
164 
165  // Upload the passed data and assign it to the specified buffer binding point.
166  // The data will live until the end of the frame and its lifetime does not need
167  // to be managed by the calling code. `buffer` is copied and may be deleted safely.
168  virtual bool SetBufferDynamic(size_t hash, void *buffer, size_t size) = 0;
169 
170  // typed overload of SetBufferDynamic
171  template <typename T>
172  bool SetBufferDynamic(size_t hash, T *buffer) { return SetBufferDynamic(hash, static_cast<void *>(buffer), sizeof(T)); }
173 
174  // Set the given buffer object with an externally-managed uniform buffer.
175  virtual bool SetBuffer(size_t hash, BufferBinding<UniformBuffer> uboBinding) = 0;
176 
177  virtual bool SetPushConstant(size_t hash, int i) = 0;
178  virtual bool SetPushConstant(size_t hash, float f) = 0;
179  virtual bool SetPushConstant(size_t hash, vector3f v3) = 0;
180  virtual bool SetPushConstant(size_t hash, vector3f v4, float f4) = 0;
181  virtual bool SetPushConstant(size_t hash, Color c) = 0;
182  virtual bool SetPushConstant(size_t hash, matrix3x3f mat3) = 0;
183  virtual bool SetPushConstant(size_t hash, matrix4x4f mat4) = 0;
184 
185  protected:
188 
189  private:
190  friend class RendererOGL;
191  };
192 
193 } // namespace Graphics
194 
195 #endif
Definition: Material.h:60
bool alphaTest
Definition: Material.h:64
bool lighting
Definition: Material.h:67
bool normalMap
Definition: Material.h:68
bool specularMap
Definition: Material.h:69
Sint32 textures
Definition: Material.h:73
friend bool operator==(const MaterialDescriptor &a, const MaterialDescriptor &b)
Definition: Material.cpp:33
Uint32 quality
Definition: Material.h:75
bool instanced
Definition: Material.h:72
bool usePatterns
Definition: Material.h:70
EffectType effect
Definition: Material.h:63
Uint32 dirLights
Definition: Material.h:74
MaterialDescriptor()
Definition: Material.cpp:16
bool vertexColors
Definition: Material.h:71
bool glowMap
Definition: Material.h:65
bool ambientMap
Definition: Material.h:66
Definition: Material.h:148
virtual bool SetTexture(size_t hash, Texture *tex)=0
Color emissive
Definition: Material.h:155
virtual bool SetPushConstant(size_t hash, int i)=0
virtual bool SetPushConstant(size_t hash, Color c)=0
virtual bool SetBuffer(size_t hash, BufferBinding< UniformBuffer > uboBinding)=0
virtual bool SetPushConstant(size_t hash, vector3f v3)=0
virtual bool SetPushConstant(size_t hash, matrix3x3f mat3)=0
bool SetBufferDynamic(size_t hash, T *buffer)
Definition: Material.h:172
Color diffuse
Definition: Material.h:153
MaterialDescriptor m_descriptor
Definition: Material.h:186
virtual bool SetPushConstant(size_t hash, float f)=0
virtual bool IsProgramLoaded() const =0
virtual bool SetPushConstant(size_t hash, matrix4x4f mat4)=0
virtual ~Material()
Definition: Material.h:151
Color specular
Definition: Material.h:154
const MaterialDescriptor & GetDescriptor() const
Definition: Material.h:159
Material()
Definition: Material.cpp:8
virtual bool SetBufferDynamic(size_t hash, void *buffer, size_t size)=0
float shininess
Definition: Material.h:156
virtual bool SetPushConstant(size_t hash, vector3f v4, float f4)=0
size_t m_renderStateHash
Definition: Material.h:187
Definition: RendererGL.h:37
Definition: Texture.h:106
Definition: RefCounted.h:11
Definition: Background.h:14
EffectType
Definition: Material.h:29
@ EFFECT_BILLBOARD
Definition: Material.h:47
@ EFFECT_GEOSPHERE_SKY
Definition: Material.h:38
@ EFFECT_GASSPHERE_TERRAIN
Definition: Material.h:40
@ EFFECT_SKYBOX
Definition: Material.h:43
@ EFFECT_SHIELD
Definition: Material.h:42
@ EFFECT_BILLBOARD_ATLAS
Definition: Material.h:46
@ EFFECT_GEN_GASGIANT_TEXTURE
Definition: Material.h:45
@ EFFECT_GEOSPHERE_TERRAIN
Definition: Material.h:35
@ EFFECT_GEOSPHERE_TERRAIN_WITH_LAVA
Definition: Material.h:36
@ EFFECT_GEOSPHERE_STAR
Definition: Material.h:39
@ EFFECT_GEOSPHERE_TERRAIN_WITH_WATER
Definition: Material.h:37
@ EFFECT_STARFIELD
Definition: Material.h:33
@ EFFECT_UI
Definition: Material.h:32
@ EFFECT_FRESNEL_SPHERE
Definition: Material.h:41
@ EFFECT_SPHEREIMPOSTOR
Definition: Material.h:44
@ EFFECT_VTXCOLOR
Definition: Material.h:31
@ EFFECT_DEFAULT
Definition: Material.h:30
@ EFFECT_PLANETRING
Definition: Material.h:34
MaterialQuality
Definition: Material.h:51
@ HAS_HEAT_GRADIENT
Definition: Material.h:54
@ HAS_OCTAVES
Definition: Material.h:55
@ HAS_ATMOSPHERE
Definition: Material.h:52
@ HAS_ECLIPSES
Definition: Material.h:53
Definition: Color.h:66
Definition: BufferCommon.h:63