HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GR_Light.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: GR_Light.h (GR Library, C++)
7  *
8  * COMMENTS:
9  * Light list and base class for all lights.
10  * Specific lights are defined in GR_LightTypes.h.
11  */
12 #ifndef GR_Light_h
13 #define GR_Light_h
14 
15 #include "GR_API.h"
16 #include "GR_Defines.h"
17 #include "GR_SceneItem.h"
18 
19 #include <RE/RE_RenderContext.h>
20 #include <UT/UT_Array.h>
21 #include <UT/UT_BoundingBox.h>
22 #include <UT/UT_Map.h>
23 #include <UT/UT_Matrix4.h>
24 #include <UT/UT_Set.h>
25 #include <UT/UT_StringHolder.h>
26 #include <UT/UT_UniquePtr.h>
27 #include <UT/UT_VectorTypes.h>
28 #include <UT/UT_Vector4.h>
29 #include <UT/UT_IntArray.h>
30 
31 class RV_Instance;
32 class RV_Render;
33 class RV_ShaderBlock;
34 class RV_ShaderProgram;
37 class RV_TextureRef;
38 class RV_VKImage;
40 
41 /// Base class for all light types
43 {
44 public:
45  enum LightType
46  {
51  AREA
52  };
53 
54  LightType type() const { return myType; }
55 
56  bool isHeadlight() const { return myIsHeadlight; }
57  void setHeadlight(bool e) { myIsHeadlight = e; }
58 
59  bool isEnabled() const { return myIsEnabled; }
60  void setEnabled(bool e) { myIsEnabled = e; }
61 
62  bool canShadow() const { return myCanShadow; }
63  void setCanShadow(bool e) { myCanShadow = e; }
64 
65  bool shadowEnabled() const { return myShadowEnabled; }
66  void setShadowEnabled(bool s) { myShadowEnabled = s; }
67 
68  void setLightID(exint id) { setID(id); }
69 
70  void setInstanceTransforms(const UT_Matrix4FArray &transforms,
71  const UT_Vector4FArray &colors);
72  bool isInstanced() const { return (myInstanceParms.size()>0); }
73  int numInstances() const { return myInstanceParms.size(); }
74 
75  bool initSceneSetForRender(
76  RV_Render *r,
78  UT_UniquePtr<RV_ShaderBlock> &scene_block,
80  bool initLightSetForRender(RV_Render *r,
82  bool bindSets(RV_Render *r, RV_ShaderProgramBase *shader);
83  virtual bool initBlocks(RV_Render *r, const GR_CommonDispOption &opts) = 0;
84  bool isDirty() const { return myDirtyFlag; }
85 
86  // returns number of required shadow render passes (see DM_RenderVulkan.C)
87  virtual unsigned setupShadowRenders(RE_RenderContext,
88  GR_RenderMode &out_render_mode,
89  bool scene_changed,
90  unsigned shadowmap_size,
91  const GR_DistantShadowMapParms&) = 0;
92  virtual RV_ShaderProgram *getShader(bool shadows, bool per_sample,
93  bool is_antialiased,
94  bool is_raytrace) = 0;
95  static RV_ShaderProgram *getAmbientOcclusionShader(bool is_raytrace);
96 
97  virtual bool canRayTraceShadow() const { return false; }
98 
99  // Returns true if the shadowmap render was started, false otherwise.
100  // Eg. returns false when the shadowmap is already up-to-date.
102  unsigned shadowmap_index) {}
105  virtual bool needsShadowMap() = 0;
106  virtual bool hasShadowMap() = 0;
108  virtual void clearShadowMap() {}
109 
110  // Secondary "gsplats-excluded" shadow map used to suppress gsplats
111  // self-shadowing.
113  GR_RenderMode &out_render_mode,
114  bool scene_changed,
115  unsigned shadowmap_size,
117  { return 0; }
119  unsigned shadowmap_index) {}
122  virtual bool hasGsplatShadowMap() { return false; }
124  virtual void clearGsplatShadowMap() {}
125 
126  static bool initShaders(RV_Instance *inst);
127  static void cleanupShaders();
128 
129  void setLightLink(const UT_StringHolder &link) { myLightLink=link; }
130  const UT_StringHolder &lightLink() const { return myLightLink; }
131  virtual void setShadowMask(const UT_StringHolder &mask)
132  { myShadowMask=mask; }
133  const UT_StringHolder &shadowMask() const { return myShadowMask; }
134 
135 protected:
136  GR_Light(const UT_StringHolder &name, LightType type);
137  ~GR_Light() override;
138 
140 
141  bool privInitBlocks(RV_Render *r,
142  void *light_block, int light_size,
143  void *shadow_block, int shadow_size);
144  void privBindTexture(RV_Render *r, const UT_StringHolder &map_name,
145  int rel_op_id, RV_TextureRef &map_id,
146  const UT_StringHolder &sampler_name,
147  const GR_CommonDispOption &opts,
148  RV_TextureParms *tex_parms = nullptr,
149  bool is_cube = false);
150  void initTexParms(RV_Render *r, const GR_CommonDispOption &opts,
151  RV_TextureParms &parms) const;
152 
153  static RV_ShaderProgram *privGetShader(exint tags);
154 
159 
164 
165  enum
166  {
167  GLOBAL_SET = 0,
168  LIGHT_SET = 1
169  };
170 
171  struct LightTag
172  {
173  exint Cube =0;
175  exint Distant =0;
176  exint Ellipse =0;
178  exint Forward =0;
179  exint Point =0;
180  exint Rectangle =0;
181  exint Shadow =0;
182  exint Spot =0;
183  exint Ambient =0;
184  exint Env =0;
185  exint PerSample =0;
186  exint AmbOcclusion =0;
187  exint RayTrace =0;
188  exint QualityShadows =0;
189  };
190 
191  static LightTag Tag;
192 private:
193  struct InstanceParms
194  {
197  };
198  UT_Array<InstanceParms> myInstanceParms;
199  UT_StringHolder myLightLink;
200  LightType myType;
201  uint32
202  myIsHeadlight :1,
203  myCanShadow :1,
204  myShadowEnabled: 1,
205  myIsEnabled :1;
206 
207 };
208 
209 static inline void intrusive_ptr_add_ref(GR_Light *gl) { gl->incref(); }
210 static inline void intrusive_ptr_release(GR_Light *gl) { gl->decref(); }
211 
212 // List of lights currently contributing to the scene
214 {
215 public:
216  uint32 getLightMask(const UT_Set<int> &active) const;
217 
218  bool useHeadlights() const { return myUseHeadlights; }
219  void setUseHeadlights(bool v) { myUseHeadlights = v; }
220 
221  void setShadowingLight(int id) { myShadowingLight = id; }
223  { return getLight(myShadowingLight).get(); }
224 
225  exint entries() const { return myList.entries(); }
226 
228  {
229  exint count = myList.entries() - myBaseIndex;
230  if (myReducedIndices.entries())
231  count = SYSmin(myReducedIndices.entries() - myBaseIndex, count);
232  if (myMaxLights >= 0)
233  count = SYSmin(myMaxLights, count);
234  return count;
235  }
236 
237  bool hasLight(int id) const
238  {
239  auto entry = myLightMap.find(id);
240  return (entry != myLightMap.end());
241  }
242 
243  // Fetch by light ID
244  GR_LightPtr getLight(int id) const
245  {
246  auto entry = myLightMap.find(id);
247  if(entry != myLightMap.end())
248  return myList(entry->second);
249  return GR_LightPtr();
250  }
251 
252  // Fetch by order in the light list (for masking)
254  {
255  return myList(index);
256  }
257 
259  {
260  index = myBaseIndex + index;
261  if (myReducedIndices.isValidIndex(index))
262  index = myReducedIndices(index);
263  if (myList.isValidIndex(index))
264  return myList(index);
265  return GR_LightPtr();
266  }
267 
268  void add(int id, GR_Light *new_light)
269  {
270  UT_ASSERT(!myLightMap.contains(id));
271  myLightMap[id] = int(myList.entries());
272  myList.append(new_light);
273  }
274  bool remove(int id)
275  {
276  auto entry = myLightMap.find(id);
277  if(entry != myLightMap.end())
278  {
279  int index = entry->second;
280  myLightMap.clear();
281  myList.removeIndex(index);
282  for(int i=0; i<myList.entries(); i++)
283  myLightMap[myList(i)->id()] = i;
284  return true;
285  }
286  return false;
287  }
288  void removeAll();
289  void removeAllBut(const UT_Set<int> &active_lights);
290  void removeHeadLights();
291 
292  bool hasRemapping() const
293  {
294  return myBaseIndex != 0 || myMaxLights != -1
295  || !myReducedIndices.isEmpty();
296  }
297 
298  void setBaseIndex(exint idx) { myBaseIndex = idx; }
299  void setMaxLights(exint max) { myMaxLights = max; }
300  void setInUseIndices(const UT_IntArray &list) { myReducedIndices = list; }
301 
303  {
304  myBaseIndex = 0;
305  myMaxLights = -1;
306  myReducedIndices.clear();
307  }
308 
309 private:
310  bool myUseHeadlights = false;
311  UT_Map<int,int> myLightMap;
312  UT_Array<GR_LightPtr> myList;
313  int myShadowingLight = -1;
314 
315  exint myBaseIndex = 0;
316  exint myMaxLights = -1;
317  UT_IntArray myReducedIndices;
318 };
319 
320 #endif
A collection of Vulkan UBO, SSBO, and Image shader bindings (descriptor set)
bool isInstanced() const
Definition: GR_Light.h:72
void setLightID(exint id)
Definition: GR_Light.h:68
virtual void finishGsplatShadowRenders(RE_RenderContext)
Definition: GR_Light.h:121
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
void setCanShadow(bool e)
Definition: GR_Light.h:63
UT_UniquePtr< RV_ShaderBlock > myShadowBlock
Definition: GR_Light.h:157
UT_UniquePtr< RV_ShaderBlock > myInstanceBlock
Definition: GR_Light.h:158
Base class for various things that can appear in a scene outside of geometry.
Definition: GR_SceneItem.h:19
virtual void startGsplatShadowRender(RE_RenderContext, unsigned shadowmap_index)
Definition: GR_Light.h:118
bool mySetBound
Definition: GR_Light.h:162
const GLdouble * v
Definition: glcorearb.h:837
void removeRemappings()
Definition: GR_Light.h:302
void decref()
Definition: GR_SceneItem.h:33
const UT_StringHolder & shadowMask() const
Definition: GR_Light.h:133
int64 exint
Definition: SYS_Types.h:125
void setInUseIndices(const UT_IntArray &list)
Definition: GR_Light.h:300
GLdouble s
Definition: glad.h:3009
Opaque reference to a texture stored in the RV_TextureCache.
Definition: RV_TextureRef.h:21
GR_LightPtr getLightByIndex(int index) const
Definition: GR_Light.h:253
virtual void clearShadowMap()
Definition: GR_Light.h:108
bool hasLight(int id) const
Definition: GR_Light.h:237
UT_IntrusivePtr< GR_Light > GR_LightPtr
Definition: GR_Defines.h:412
virtual unsigned setupGsplatShadowRenders(RE_RenderContext, GR_RenderMode &out_render_mode, bool scene_changed, unsigned shadowmap_size, const GR_DistantShadowMapParms &)
Definition: GR_Light.h:112
UT_NON_COPYABLE(GR_SceneItem)
Temporary container for either a RV_Render and an RE_Render.
virtual void clearGsplatShadowMap()
Definition: GR_Light.h:124
LightType type() const
Definition: GR_Light.h:54
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
bool hasRemapping() const
Definition: GR_Light.h:292
void setShadowEnabled(bool s)
Definition: GR_Light.h:66
void add(int id, GR_Light *new_light)
Definition: GR_Light.h:268
virtual void startShadowRender(RE_RenderContext rc, unsigned shadowmap_index)
Definition: GR_Light.h:101
void setBaseIndex(exint idx)
Definition: GR_Light.h:298
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
void setID(int id)
Definition: GR_SceneItem.h:40
GR_LightPtr getLight(int id) const
Definition: GR_Light.h:244
GR_RenderMode
Definition: GR_Defines.h:48
const GR_Light * getShadowingLight() const
Definition: GR_Light.h:222
void setMaxLights(exint max)
Definition: GR_Light.h:299
bool myDirtyFlag
Definition: GR_Light.h:160
bool canShadow() const
Definition: GR_Light.h:62
GLint GLuint mask
Definition: glcorearb.h:124
virtual bool canRayTraceShadow() const
Definition: GR_Light.h:97
Base class for all light types.
Definition: GR_Light.h:42
RV_API void cleanupShaders()
const UT_StringHolder & lightLink() const
Definition: GR_Light.h:130
bool myShadowDirtyFlag
Definition: GR_Light.h:161
#define GR_API
Definition: GR_API.h:10
static LightTag Tag
Definition: GR_Light.h:191
Wrapper around hboost::intrusive_ptr.
UT_UniquePtr< RV_ShaderBlock > myLightBlock
Definition: GR_Light.h:156
GLuint id
Definition: glcorearb.h:655
bool isEnabled() const
Definition: GR_Light.h:59
GLuint const GLchar * name
Definition: glcorearb.h:786
bool useHeadlights() const
Definition: GR_Light.h:218
void setLightLink(const UT_StringHolder &link)
Definition: GR_Light.h:129
virtual void finishGsplatShadowRender(RE_RenderContext)
Definition: GR_Light.h:120
bool isDirty() const
Definition: GR_Light.h:84
GA_API const UT_StringHolder transform
Handle to the main interface of Vulkan.
Definition: RV_Instance.h:50
that also have some descendant prim *whose name begins with which in turn has a child named baz where *the predicate active
void setEnabled(bool e)
Definition: GR_Light.h:60
int numInstances() const
Definition: GR_Light.h:73
virtual bool hasGsplatShadowMap()
Definition: GR_Light.h:122
virtual void attachShadowMap(RE_RenderContext)
Definition: GR_Light.h:107
RV_API bool initShaders(RV_Instance *inst)
GLuint shader
Definition: glcorearb.h:785
GA_API const UT_StringHolder parms
virtual void finishShadowRenders(RE_RenderContext)
Definition: GR_Light.h:104
UT_StringHolder myShadowMask
Definition: GR_Light.h:163
GLuint color
Definition: glcorearb.h:1261
GR_LightPtr getInUseLightByIndex(int index) const
Definition: GR_Light.h:258
UT_UniquePtr< RV_ShaderVariableSet > myLightSet
Definition: GR_Light.h:155
GLuint index
Definition: glcorearb.h:786
void setShadowingLight(int id)
Definition: GR_Light.h:221
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
unsigned int uint32
Definition: SYS_Types.h:40
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:165
bool shadowEnabled() const
Definition: GR_Light.h:65
void setHeadlight(bool e)
Definition: GR_Light.h:57
exint entries() const
Definition: GR_Light.h:225
GLboolean r
Definition: glcorearb.h:1222
exint inUseLightCount() const
Definition: GR_Light.h:227
virtual void finishShadowRender(RE_RenderContext)
Definition: GR_Light.h:103
#define SYSmin(a, b)
Definition: SYS_Math.h:1953
virtual void attachGsplatShadowMap(RE_RenderContext)
Definition: GR_Light.h:123
bool isHeadlight() const
Definition: GR_Light.h:56
void incref()
Definition: GR_SceneItem.h:31
virtual void setShadowMask(const UT_StringHolder &mask)
Definition: GR_Light.h:131
GLint GLsizei count
Definition: glcorearb.h:405
void setUseHeadlights(bool v)
Definition: GR_Light.h:219