All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
renderer.h
1 //
2 // Copyright 2018 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_IMAGING_PLUGIN_HD_EMBREE_RENDERER_H
25 #define PXR_IMAGING_PLUGIN_HD_EMBREE_RENDERER_H
26 
27 #include "pxr/pxr.h"
28 
29 #include "pxr/imaging/hd/renderThread.h"
30 #include "pxr/imaging/hd/renderPassState.h"
31 
32 #include "pxr/base/gf/matrix4d.h"
33 #include "pxr/base/gf/rect2i.h"
34 
35 #include <embree3/rtcore.h>
36 #include <embree3/rtcore_ray.h>
37 
38 #include <random>
39 #include <atomic>
40 
41 PXR_NAMESPACE_OPEN_SCOPE
42 
54 class HdEmbreeRenderer final {
55 public:
58 
61 
64  void SetScene(RTCScene scene);
65 
68  void SetDataWindow(const GfRect2i &dataWindow);
69 
73  void SetCamera(const GfMatrix4d& viewMatrix, const GfMatrix4d& projMatrix);
74 
77  void SetAovBindings(HdRenderPassAovBindingVector const &aovBindings);
78 
81  HdRenderPassAovBindingVector const& GetAovBindings() const {
82  return _aovBindings;
83  }
84 
88  void SetSamplesToConvergence(int samplesToConvergence);
89 
93  void SetAmbientOcclusionSamples(int ambientOcclusionSamples);
94 
98  void SetEnableSceneColors(bool enableSceneColors);
99 
105  void Render(HdRenderThread *renderThread);
106 
108  void Clear();
109 
112 
114  int GetCompletedSamples() const;
115 
116 private:
117  // Validate the internal consistency of aov bindings provided to
118  // SetAovBindings. If the aov bindings are invalid, this will issue
119  // appropriate warnings. If the function returns false, Render() will fail
120  // early.
121  //
122  // This function thunks itself using _aovBindingsNeedValidation and
123  // _aovBindingsValid.
124  // \return True if the aov bindings are valid for rendering.
125  bool _ValidateAovBindings();
126 
127  // Return the clear color to use for the given VtValue.
128  static GfVec4f _GetClearColor(VtValue const& clearValue);
129 
130  // Render square tiles of pixels. This function is one unit of threadpool
131  // work. For each tile, iterate over pixels in the tile, generating camera
132  // rays, and following them/calculating color with _TraceRay. This function
133  // renders all tiles between tileStart and tileEnd.
134  void _RenderTiles(HdRenderThread *renderThread,
135  size_t tileStart, size_t tileEnd);
136 
137  // Cast a ray into the scene and if it hits an object, write to the bound
138  // aov buffers.
139  void _TraceRay(unsigned int x, unsigned int y,
140  GfVec3f const& origin, GfVec3f const& dir,
141  std::default_random_engine &random);
142 
143  // Compute the color at the given ray hit.
144  GfVec4f _ComputeColor(RTCRayHit const& rayHit,
145  std::default_random_engine &random,
146  GfVec4f const& clearColor);
147  // Compute the depth at the given ray hit.
148  bool _ComputeDepth(RTCRayHit const& rayHit, float *depth, bool clip);
149  // Compute the given ID at the given ray hit.
150  bool _ComputeId(RTCRayHit const& rayHit, TfToken const& idType, int32_t *id);
151  // Compute the normal at the given ray hit.
152  bool _ComputeNormal(RTCRayHit const& rayHit, GfVec3f *normal, bool eye);
153  // Compute a primvar at the given ray hit.
154  bool _ComputePrimvar(RTCRayHit const& rayHit, TfToken const& primvar,
155  GfVec3f *value);
156 
157  // Compute the ambient occlusion term at a given point by firing rays
158  // from "position" in the hemisphere centered on "normal"; the occlusion
159  // factor is the fraction of those rays that are visible.
160  //
161  // Modulating surface color by occlusionFactor is similar to taking
162  // the light contribution of an infinitely far, pure white dome light.
163  float _ComputeAmbientOcclusion(GfVec3f const& position,
164  GfVec3f const& normal,
165  std::default_random_engine &random);
166 
167  // The bound aovs for this renderer.
168  HdRenderPassAovBindingVector _aovBindings;
169  // Parsed AOV name tokens.
170  HdParsedAovTokenVector _aovNames;
171 
172  // Do the aov bindings need to be re-validated?
173  bool _aovBindingsNeedValidation;
174  // Are the aov bindings valid?
175  bool _aovBindingsValid;
176 
177  // Data window - as in CameraUtilFraming.
178  GfRect2i _dataWindow;
179 
180  // The width of the render buffers.
181  unsigned int _width;
182  // The height of the render buffers.
183  unsigned int _height;
184 
185  // View matrix: world space to camera space.
186  GfMatrix4d _viewMatrix;
187  // Projection matrix: camera space to NDC space.
188  GfMatrix4d _projMatrix;
189  // The inverse view matrix: camera space to world space.
190  GfMatrix4d _inverseViewMatrix;
191  // The inverse projection matrix: NDC space to camera space.
192  GfMatrix4d _inverseProjMatrix;
193 
194  // Our handle to the embree scene.
195  RTCScene _scene;
196 
197  // How many samples should we render to convergence?
198  int _samplesToConvergence;
199  // How many samples should we use for ambient occlusion?
200  int _ambientOcclusionSamples;
201  // Should we enable scene colors?
202  bool _enableSceneColors;
203 
204  // How many samples have been completed.
205  std::atomic<int> _completedSamples;
206 };
207 
208 PXR_NAMESPACE_CLOSE_SCOPE
209 
210 #endif // PXR_IMAGING_PLUGIN_HD_EMBREE_RENDERER_H
void MarkAovBuffersUnconverged()
Mark the aov buffers as unconverged.
A 2D rectangle with integer coordinates.
Definition: rect2i.h:61
Basic type for a vector of 3 float components.
Definition: vec3f.h:63
~HdEmbreeRenderer()
Renderer destructor.
void SetEnableSceneColors(bool enableSceneColors)
Sets whether to use scene colors while rendering.
HdEmbreeRenderer()
Renderer constructor.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
void SetCamera(const GfMatrix4d &viewMatrix, const GfMatrix4d &projMatrix)
Set the camera to use for rendering.
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
void SetSamplesToConvergence(int samplesToConvergence)
Set how many samples to render before considering an image converged.
void SetScene(RTCScene scene)
Set the embree scene that this renderer should raycast into.
void SetAmbientOcclusionSamples(int ambientOcclusionSamples)
Set how many samples to use for ambient occlusion.
Basic type for a vector of 4 float components.
Definition: vec4f.h:63
void Clear()
Clear the bound aov buffers (typically before rendering).
int GetCompletedSamples() const
Get the number of samples completed so far.
HdEmbreeRenderer implements a renderer on top of Embree&#39;s raycasting abilities.
Definition: renderer.h:54
void SetAovBindings(HdRenderPassAovBindingVector const &aovBindings)
Set the aov bindings to use for rendering.
void SetDataWindow(const GfRect2i &dataWindow)
Set the data window to fill (same meaning as in CameraUtilFraming with coordinate system also being y...
HdRenderThread is a utility that specific render delegates can choose to use depending on their needs...
Definition: renderThread.h:146
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:168
void Render(HdRenderThread *renderThread)
Rendering entrypoint: add one sample per pixel to the whole sample buffer, and then loop until the im...
HdRenderPassAovBindingVector const & GetAovBindings() const
Get the aov bindings being used for rendering.
Definition: renderer.h:81