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 orthoFromViewport2DI(const UT_DimRect &rect, bool reverse_depth);
116  void ortho3DW(float l, float r, float b, float t,
117  float n, float f);
118  void window3DW(float l, float r, float b, float t,
119  float n, float f);
120 
121  // Generates a matrix equivalent to glOrtho.
122  static UT_DMatrix4 getOrthoMatrix(float l, float r,
123  float b, float t,
124  float n, float f);
125  // Generates a matrix equivalent to glFrustum.
126  static UT_DMatrix4 getPerspectiveMatrix(float l, float r,
127  float b, float t,
128  float n, float f);
129  // Generates a matrix equivalent to gluLookAt.
130  static UT_DMatrix4 getLookatMatrix(const UT_Vector3F &eye,
131  const UT_Vector3F &target,
132  const UT_Vector3F &up);
133 
134  // Maps screen space coordinates to a world space position and a ray
135  // into the scene. Lets you know where in the scene the mouse is.
136  static bool mapWorld(fpreal xs, fpreal ys,
137  const UT_DimRect &viewport,
138  const UT_Matrix4D &proj,
139  const UT_Matrix4D &view,
140  fpreal *x1, fpreal *y1, fpreal *z1,
141  fpreal *x2, fpreal *y2, fpreal *z2);
142  // Maps world space coordinates into a screen space position. This is the
143  // pixel location of an object in the scene.
144  static bool mapScreen(fpreal xw, fpreal yw, fpreal zw,
145  const UT_DimRect &viewport,
146  const UT_Matrix4D &proj,
147  const UT_Matrix4D &view,
148  fpreal *xs, fpreal *ys);
149 
150  void getProjectiveTextureTransform(const RE_Light *light,
151  bool bias, UT_Matrix4 &mat);
152 
153  static void debugOutput(bool enable);
154 
155  /// Set the device pixel ratio to use when scaling renders.
156  void setDevicePixelRatio(int ratio) {
157  myDevicePixelRatio = ratio;
158  }
159 
160  /// Return the given numeric after scaling it by the renderer's
161  /// device pixel ratio.
162  int devicePixelRatio() const {
164  {
165  return myDevicePixelRatio;
166  }
167 
168  return 1;
169  }
170 
171 protected:
172  void setRender(RE_Render *r);
173 
174 private:
175  void flush(bool render_complete);
176  void updatePixelRatios();
177 
178  RE_Render *myRender;
179  UT_Array<UT_DimRect> myViewportStack;
180  UT_Array<RE_ViewportCoordSpace> myViewportCoordSpaceStack;
181  UT_Array<UT_DimRect> myScissorRectStack;
182  UT_Array<bool> myScissorEnableStack;
183  UT_Array<RE_ViewportCoordSpace> myScissorCoordSpaceStack;
184  UT_Array<UT_BoundingRect> myOrthoStack;
185  RE_Uniform myClipPlane0Uniform;
186  RE_PerContextData *myContextData;
187 
188  /// Flag to prevent recursive flushing of render data.
189  bool myFlushing;
190 
191  /// Keeps track of whether this object is being used for dawing
192  /// into a viewport or drawing regular UI.
193  bool myViewportRenderer;
194 
195  /// Cache our two available fonts.
196  static RE_Font *theViewportFont;
197  static RE_Font *theViewportSymbolFont;
198 
199  /// The device pixel ratio used to scale renders.
200  /// This corresponds to the backing scale factor on macOS retina displays.
201  int myDevicePixelRatio = 1;
202 };
203 
204 inline bool
205 RE_RenderFlush::flushRender(bool render_complete)
206 {
207  // Ignore nested flush calls.
208  if (myFlushing)
209  return false;
210 
211  // If this is the last flush of a render pass, we can reset our Z value
212  // because the next thing we draw is a whole new render.
213  if (render_complete)
214  resetCurrentZ();
215 
216  // If we have nothing to do, bail out immediately.
217  if (!hasAnyData())
218  return false;
219 
220  flush(render_complete);
221 
222  return true;
223 }
224 
225 #endif // __RE_RenderFlush__
226 
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