HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RE_RenderFlush.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: RE_RenderFlush.h ( RE Library, C++)
7  */
8 
9 #ifndef __RE_RenderFlush__
10 #define __RE_RenderFlush__
11 
12 #include "RE_API.h"
13 #include "RE_ShaderHandle.h"
14 #include "RE_Uniform.h"
15 #include "RE_RenderUI.h"
16 #include "RE_Server.h"
17 #include "RE_Types.h"
18 #include <UT/UT_BoundingRect.h>
19 #include <UT/UT_Rect.h>
20 
21 class RE_Light;
22 class RE_PerContextData;
23 
25 {
26 public:
28  ~RE_RenderFlush() override;
29 
30  /// Tag used to distinguish overrides which try to only manipulate the
31  /// cached state without issuing OpenGL commands. Some commands with
32  /// this tag may still have to query the OpenGL state. These overrides
33  /// are used to manipulate the cached state during Qt painting as that
34  /// is what is restored when switching to OpenGL drawing.
35  static struct CacheOnlyTag {} CacheOnly;
36 
37  /// Flag this object as being used to render into a 3D viewport.
38  void setViewportRenderer();
39  static RE_Font &getViewportFont();
40 
41  /// Holds a pointer to per-context data we use to provide OGL buffers.
42  void setContextData(RE_PerContextData *data);
43  RE_PerContextData *getContextData() { return myContextData; }
44 
45  // Override for setting the base Z value.
46  void setZStart(fpreal32 zStart) override;
47  // Override to reuse OGL Buffers.
48  void draw(RE_Render *r) override;
49  // Flush render commands built up to this point.
50  bool flushRender(bool render_complete = false);
51 
52  // TRANSFORMATION ------------------------------------------------------
53  void pushViewport();
54  void pushViewport(CacheOnlyTag);
55  void popViewport();
56  void popViewport(CacheOnlyTag);
57 
58  // Called by RE_OGLRender when our viewport changes.
59  void handleViewportUpdate(
60  const UT_DimRect &rect, RE_ViewportCoordSpace coord_space);
61  void handleViewportUpdate(
62  const UT_DimRect &rect, RE_ViewportCoordSpace coord_space,
63  CacheOnlyTag);
64  void handleScissorRectUpdate(
65  const UT_DimRect &rect, RE_ViewportCoordSpace coord_space);
66  void handleScissorRectUpdate(
67  const UT_DimRect &rect, RE_ViewportCoordSpace coord_space,
68  CacheOnlyTag);
69  void handleScissorEnableUpdate(bool enable);
70  void handleScissorEnableUpdate(bool enable, CacheOnlyTag);
71 
72  // Clipping plane support.
73  void clipPlaneDisable(int which);
74  void clipPlaneEnable(int which, const UT_Vector4D &plane);
75 
76  UT_Vector4D planeToEyeCoords(const UT_Vector4D &plane);
77 
78  // NB: The following methods to push/pop the clip plane assume that they
79  // occur within a clipPlaneEnable()/clipPlaneDisable() block, hence
80  // the "replace" prefix.
81  void replacePushClipPlane(int which, const UT_Vector4D &plane,
82  bool xform_to_eye_coords,
83  RE_UniformStackEntry &stack_entry);
84  void replacePopClipPlane(int which,
85  const RE_UniformStackEntry &stack_entry);
86 
87  void getClipPlane(int which, UT_Vector4D &plane) const;
88 
89  // Matrix operations.
90  void pushMatrix(bool all_matrices = true,
92  void popMatrix(bool all_matrices = true,
94 
95  void loadIdentityMatrix(RE_MatrixMode mmode = RE_MATRIX_VIEWING);
96  void loadMatrix(const UT_Matrix4 &m,
98  void loadMatrix(const UT_DMatrix4 &m,
100 
101  void getMatrix(UT_Matrix4 &m,
103  void getMatrix(UT_DMatrix4 &m,
105 
106  void multiplyMatrix(const UT_Matrix4 &m);
107  void multiplyMatrix(const UT_DMatrix4 &m);
108 
109  void translate(float x, float y, float z = 0.0);
110  void scale(float x = 1.0, float y = 1.0, float z = 1.0);
111  void rotate(float angle, char axis);
112 
113  void ortho2DW(float l, float r, float b, float t);
114  void orthoFromViewport2DI(const UT_DimRect &rect);
115  void ortho3DW(float l, float r, float b, float t,
116  float n, float f);
117  void window3DW(float l, float r, float b, float t,
118  float n, float f);
119 
120  // Generates a matrix equivalent to glOrtho.
121  static UT_DMatrix4 getOrthoMatrix(float l, float r,
122  float b, float t,
123  float n, float f);
124  // Generates a matrix equivalent to glFrustum.
125  static UT_DMatrix4 getPerspectiveMatrix(float l, float r,
126  float b, float t,
127  float n, float f);
128  // Generates a matrix equivalent to gluLookAt.
129  static UT_DMatrix4 getLookatMatrix(const UT_Vector3F &eye,
130  const UT_Vector3F &target,
131  const UT_Vector3F &up);
132 
133  // Maps screen space coordinates to a world space position and a ray
134  // into the scene. Lets you know where in the scene the mouse is.
135  static bool mapWorld(fpreal xs, fpreal ys,
136  const UT_DimRect &viewport,
137  const UT_Matrix4D &proj,
138  const UT_Matrix4D &view,
139  fpreal *x1, fpreal *y1, fpreal *z1,
140  fpreal *x2, fpreal *y2, fpreal *z2);
141  // Maps world space coordinates into a screen space position. This is the
142  // pixel location of an object in the scene.
143  static bool mapScreen(fpreal xw, fpreal yw, fpreal zw,
144  const UT_DimRect &viewport,
145  const UT_Matrix4D &proj,
146  const UT_Matrix4D &view,
147  fpreal *xs, fpreal *ys);
148 
149  void getProjectiveTextureTransform(const RE_Light *light,
150  bool bias, UT_Matrix4 &mat);
151 
152  static void debugOutput(bool enable);
153 
154  /// Set the device pixel ratio to use when scaling renders.
155  void setDevicePixelRatio(int ratio) {
156  myDevicePixelRatio = ratio;
157  }
158 
159  /// Return the given numeric after scaling it by the renderer's
160  /// device pixel ratio.
161  int devicePixelRatio() const {
163  {
164  return myDevicePixelRatio;
165  }
166 
167  return 1;
168  }
169 
170 protected:
171  void setRender(RE_Render *r);
172 
173 private:
174  void flush(bool render_complete);
175  void updatePixelRatios();
176 
177  RE_Render *myRender;
178  UT_Array<UT_DimRect> myViewportStack;
179  UT_Array<RE_ViewportCoordSpace> myViewportCoordSpaceStack;
180  UT_Array<UT_DimRect> myScissorRectStack;
181  UT_Array<bool> myScissorEnableStack;
182  UT_Array<RE_ViewportCoordSpace> myScissorCoordSpaceStack;
183  UT_Array<UT_BoundingRect> myOrthoStack;
184  RE_Uniform myClipPlane0Uniform;
185  RE_PerContextData *myContextData;
186 
187  /// Flag to prevent recursive flushing of render data.
188  bool myFlushing;
189 
190  /// Keeps track of whether this object is being used for dawing
191  /// into a viewport or drawing regular UI.
192  bool myViewportRenderer;
193 
194  /// Cache our two available fonts.
195  static RE_Font *theViewportFont;
196  static RE_Font *theViewportSymbolFont;
197 
198  /// The device pixel ratio used to scale renders.
199  /// This corresponds to the backing scale factor on macOS retina displays.
200  int myDevicePixelRatio = 1;
201 };
202 
203 inline bool
204 RE_RenderFlush::flushRender(bool render_complete)
205 {
206  // Ignore nested flush calls.
207  if (myFlushing)
208  return false;
209 
210  // If this is the last flush of a render pass, we can reset our Z value
211  // because the next thing we draw is a whole new render.
212  if (render_complete)
213  resetCurrentZ();
214 
215  // If we have nothing to do, bail out immediately.
216  if (!hasAnyData())
217  return false;
218 
219  flush(render_complete);
220 
221  return true;
222 }
223 
224 #endif // __RE_RenderFlush__
225 
void setDevicePixelRatio(int ratio)
Set the device pixel ratio to use when scaling renders.
#define RE_API
Definition: RE_API.h:10
static bool useNewUISkin()
SIM_API const UT_StringHolder angle
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
virtual void setZStart(fpreal32 zStart)
void resetCurrentZ()
GLint y
Definition: glcorearb.h:103
float fpreal32
Definition: SYS_Types.h:200
RE_MatrixMode
Definition: RE_Types.h:453
GLdouble GLdouble x2
Definition: glad.h:2349
GA_API const UT_StringHolder scale
GLdouble n
Definition: glcorearb.h:2008
GLfloat f
Definition: glcorearb.h:1926
bool hasAnyData()
int devicePixelRatio() const
GLdouble y1
Definition: glad.h:2349
GLenum target
Definition: glcorearb.h:1667
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GLint GLenum GLint x
Definition: glcorearb.h:409
ImageBuf OIIO_API rotate(const ImageBuf &src, float angle, string_view filtername=string_view(), float filterwidth=0.0f, bool recompute_roi=false, ROI roi={}, int nthreads=0)
RE_ViewportCoordSpace
Definition: RE_Types.h:930
GLdouble t
Definition: glad.h:2397
virtual void draw(RE_Render *r)
RE_PerContextData * getContextData()
fpreal64 fpreal
Definition: SYS_Types.h:283
GLboolean r
Definition: glcorearb.h:1222
PUGI__FN char_t * translate(char_t *buffer, const char_t *from, const char_t *to, size_t to_length)
Definition: pugixml.cpp:8574
GLdouble GLdouble GLdouble y2
Definition: glad.h:2349
bool flushRender(bool render_complete=false)
Definition: RE_Uniform.h:389
Definition: format.h:1821