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 HDEMBREE_RENDERER_H
25 #define HDEMBREE_RENDERER_H
26 
27 #include "pxr/pxr.h"
28 
29 #include "pxr/imaging/hd/renderThread.h"
30 #include "pxr/imaging/hd/renderPassState.h"
31 #include "pxr/imaging/hd/tokens.h"
32 
33 #include "pxr/base/gf/matrix4d.h"
34 
35 #include <embree2/rtcore.h>
36 #include <embree2/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 
69  void SetViewport(unsigned int width, unsigned int height);
70 
74  void SetCamera(const GfMatrix4d& viewMatrix, const GfMatrix4d& projMatrix);
75 
78  void SetAovBindings(HdRenderPassAovBindingVector const &aovBindings);
79 
82  HdRenderPassAovBindingVector const& GetAovBindings() const {
83  return _aovBindings;
84  }
85 
89  void SetSamplesToConvergence(int samplesToConvergence);
90 
94  void SetAmbientOcclusionSamples(int ambientOcclusionSamples);
95 
99  void SetEnableSceneColors(bool enableSceneColors);
100 
106  void Render(HdRenderThread *renderThread);
107 
109  void Clear();
110 
113 
115  int GetCompletedSamples() const;
116 
117 private:
118  // Validate the internal consistency of aov bindings provided to
119  // SetAovBindings. If the aov bindings are invalid, this will issue
120  // appropriate warnings. If the function returns false, Render() will fail
121  // early.
122  //
123  // This function thunks itself using _aovBindingsNeedValidation and
124  // _aovBindingsValid.
125  // \return True if the aov bindings are valid for rendering.
126  bool _ValidateAovBindings();
127 
128  // Return the clear color to use for the given VtValue.
129  static GfVec4f _GetClearColor(VtValue const& clearValue);
130 
131  // Render square tiles of pixels. This function is one unit of threadpool
132  // work. For each tile, iterate over pixels in the tile, generating camera
133  // rays, and following them/calculating color with _TraceRay. This function
134  // renders all tiles between tileStart and tileEnd.
135  void _RenderTiles(HdRenderThread *renderThread,
136  size_t tileStart, size_t tileEnd);
137 
138  // Cast a ray into the scene and if it hits an object, write to the bound
139  // aov buffers.
140  void _TraceRay(unsigned int x, unsigned int y,
141  GfVec3f const& origin, GfVec3f const& dir,
142  std::default_random_engine &random);
143 
144  // Compute the color at the given ray hit.
145  GfVec4f _ComputeColor(RTCRay const& rayHit,
146  std::default_random_engine &random,
147  GfVec4f const& clearColor);
148  // Compute the depth at the given ray hit.
149  bool _ComputeDepth(RTCRay const& rayHit, float *depth, bool ndc);
150  // Compute the given ID at the given ray hit.
151  bool _ComputeId(RTCRay const& rayHit, TfToken const& idType, int32_t *id);
152  // Compute the normal at the given ray hit.
153  bool _ComputeNormal(RTCRay const& rayHit, GfVec3f *normal, bool eye);
154  // Compute a primvar at the given ray hit.
155  bool _ComputePrimvar(RTCRay const& rayHit, TfToken const& primvar,
156  GfVec3f *value);
157 
158  // Compute the ambient occlusion term at a given point by firing rays
159  // from "position" in the hemisphere centered on "normal"; the occlusion
160  // factor is the fraction of those rays that are visible.
161  //
162  // Modulating surface color by occlusionFactor is similar to taking
163  // the light contribution of an infinitely far, pure white dome light.
164  float _ComputeAmbientOcclusion(GfVec3f const& position,
165  GfVec3f const& normal,
166  std::default_random_engine &random);
167 
168  // The bound aovs for this renderer.
169  HdRenderPassAovBindingVector _aovBindings;
170  // Parsed AOV name tokens.
171  HdParsedAovTokenVector _aovNames;
172 
173  // Do the aov bindings need to be re-validated?
174  bool _aovBindingsNeedValidation;
175  // Are the aov bindings valid?
176  bool _aovBindingsValid;
177 
178  // The width of the viewport we're rendering into.
179  unsigned int _width;
180  // The height of the viewport we're rendering into.
181  unsigned int _height;
182 
183  // View matrix: world space to camera space.
184  GfMatrix4d _viewMatrix;
185  // Projection matrix: camera space to NDC space.
186  GfMatrix4d _projMatrix;
187  // The inverse view matrix: camera space to world space.
188  GfMatrix4d _inverseViewMatrix;
189  // The inverse projection matrix: NDC space to camera space.
190  GfMatrix4d _inverseProjMatrix;
191 
192  // Our handle to the embree scene.
193  RTCScene _scene;
194 
195  // How many samples should we render to convergence?
196  int _samplesToConvergence;
197  // How many samples should we use for ambient occlusion?
198  int _ambientOcclusionSamples;
199  // Should we enable scene colors?
200  bool _enableSceneColors;
201 
202  // How many samples have been completed.
203  std::atomic<int> _completedSamples;
204 };
205 
206 PXR_NAMESPACE_CLOSE_SCOPE
207 
208 #endif // HDEMBREE_RENDERER_H
void SetViewport(unsigned int width, unsigned int height)
Specify a new viewport size for the sample/color buffer.
void MarkAovBuffersUnconverged()
Mark the aov buffers as unconverged.
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:89
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.
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:182
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:82