Loading...
Searching...
No Matches
renderer.h
1//
2// Copyright 2018 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_IMAGING_PLUGIN_HD_EMBREE_RENDERER_H
8#define PXR_IMAGING_PLUGIN_HD_EMBREE_RENDERER_H
9
10#include "pxr/pxr.h"
11
12#include "pxr/imaging/hd/renderThread.h"
13#include "pxr/imaging/hd/renderPassState.h"
14
16#include "pxr/base/gf/rect2i.h"
17
18#include <embree3/rtcore.h>
19#include <embree3/rtcore_ray.h>
20
21#include <random>
22#include <atomic>
23
24PXR_NAMESPACE_OPEN_SCOPE
25
38{
39public:
42
45
48 void SetScene(RTCScene scene);
49
52 void SetDataWindow(const GfRect2i &dataWindow);
53
57 void SetCamera(const GfMatrix4d& viewMatrix, const GfMatrix4d& projMatrix);
58
61 void SetAovBindings(HdRenderPassAovBindingVector const &aovBindings);
62
65 HdRenderPassAovBindingVector const& GetAovBindings() const {
66 return _aovBindings;
67 }
68
72 void SetSamplesToConvergence(int samplesToConvergence);
73
77 void SetAmbientOcclusionSamples(int ambientOcclusionSamples);
78
82 void SetEnableSceneColors(bool enableSceneColors);
83
89 void Render(HdRenderThread *renderThread);
90
92 void Clear();
93
96
99
100private:
101 // Validate the internal consistency of aov bindings provided to
102 // SetAovBindings. If the aov bindings are invalid, this will issue
103 // appropriate warnings. If the function returns false, Render() will fail
104 // early.
105 //
106 // This function thunks itself using _aovBindingsNeedValidation and
107 // _aovBindingsValid.
108 // \return True if the aov bindings are valid for rendering.
109 bool _ValidateAovBindings();
110
111 // Return the clear color to use for the given VtValue.
112 static GfVec4f _GetClearColor(VtValue const& clearValue);
113
114 // Render square tiles of pixels. This function is one unit of threadpool
115 // work. For each tile, iterate over pixels in the tile, generating camera
116 // rays, and following them/calculating color with _TraceRay. This function
117 // renders all tiles between tileStart and tileEnd.
118 void _RenderTiles(HdRenderThread *renderThread,
119 size_t tileStart, size_t tileEnd);
120
121 // Cast a ray into the scene and if it hits an object, write to the bound
122 // aov buffers.
123 void _TraceRay(unsigned int x, unsigned int y,
124 GfVec3f const& origin, GfVec3f const& dir,
125 std::default_random_engine &random);
126
127 // Compute the color at the given ray hit.
128 GfVec4f _ComputeColor(RTCRayHit const& rayHit,
129 std::default_random_engine &random,
130 GfVec4f const& clearColor);
131 // Compute the depth at the given ray hit.
132 bool _ComputeDepth(RTCRayHit const& rayHit, float *depth, bool clip);
133 // Compute the given ID at the given ray hit.
134 bool _ComputeId(RTCRayHit const& rayHit, TfToken const& idType, int32_t *id);
135 // Compute the normal at the given ray hit.
136 bool _ComputeNormal(RTCRayHit const& rayHit, GfVec3f *normal, bool eye);
137 // Compute a primvar at the given ray hit.
138 bool _ComputePrimvar(RTCRayHit const& rayHit, TfToken const& primvar,
139 GfVec3f *value);
140
141 // Compute the ambient occlusion term at a given point by firing rays
142 // from "position" in the hemisphere centered on "normal"; the occlusion
143 // factor is the fraction of those rays that are visible.
144 //
145 // Modulating surface color by occlusionFactor is similar to taking
146 // the light contribution of an infinitely far, pure white dome light.
147 float _ComputeAmbientOcclusion(GfVec3f const& position,
148 GfVec3f const& normal,
149 std::default_random_engine &random);
150
151 // The bound aovs for this renderer.
152 HdRenderPassAovBindingVector _aovBindings;
153 // Parsed AOV name tokens.
154 HdParsedAovTokenVector _aovNames;
155
156 // Do the aov bindings need to be re-validated?
157 bool _aovBindingsNeedValidation;
158 // Are the aov bindings valid?
159 bool _aovBindingsValid;
160
161 // Data window - as in CameraUtilFraming.
162 GfRect2i _dataWindow;
163
164 // The width of the render buffers.
165 unsigned int _width;
166 // The height of the render buffers.
167 unsigned int _height;
168
169 // View matrix: world space to camera space.
170 GfMatrix4d _viewMatrix;
171 // Projection matrix: camera space to NDC space.
172 GfMatrix4d _projMatrix;
173 // The inverse view matrix: camera space to world space.
174 GfMatrix4d _inverseViewMatrix;
175 // The inverse projection matrix: NDC space to camera space.
176 GfMatrix4d _inverseProjMatrix;
177
178 // Our handle to the embree scene.
179 RTCScene _scene;
180
181 // How many samples should we render to convergence?
182 int _samplesToConvergence;
183 // How many samples should we use for ambient occlusion?
184 int _ambientOcclusionSamples;
185 // Should we enable scene colors?
186 bool _enableSceneColors;
187
188 // How many samples have been completed.
189 std::atomic<int> _completedSamples;
190};
191
192PXR_NAMESPACE_CLOSE_SCOPE
193
194#endif // PXR_IMAGING_PLUGIN_HD_EMBREE_RENDERER_H
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:71
A 2D rectangle with integer coordinates.
Definition: rect2i.h:43
Basic type for a vector of 3 float components.
Definition: vec3f.h:46
Basic type for a vector of 4 float components.
Definition: vec4f.h:46
HdEmbreeRenderer implements a renderer on top of Embree's raycasting abilities.
Definition: renderer.h:38
void SetSamplesToConvergence(int samplesToConvergence)
Set how many samples to render before considering an image converged.
void SetAmbientOcclusionSamples(int ambientOcclusionSamples)
Set how many samples to use for ambient occlusion.
void SetEnableSceneColors(bool enableSceneColors)
Sets whether to use scene colors while rendering.
void Render(HdRenderThread *renderThread)
Rendering entrypoint: add one sample per pixel to the whole sample buffer, and then loop until the im...
HdEmbreeRenderer()
Renderer constructor.
~HdEmbreeRenderer()
Renderer destructor.
void MarkAovBuffersUnconverged()
Mark the aov buffers as unconverged.
void SetAovBindings(HdRenderPassAovBindingVector const &aovBindings)
Set the aov bindings to use for rendering.
int GetCompletedSamples() const
Get the number of samples completed so far.
void SetScene(RTCScene scene)
Set the embree scene that this renderer should raycast into.
void SetCamera(const GfMatrix4d &viewMatrix, const GfMatrix4d &projMatrix)
Set the camera to use for rendering.
void Clear()
Clear the bound aov buffers (typically before rendering).
void SetDataWindow(const GfRect2i &dataWindow)
Set the data window to fill (same meaning as in CameraUtilFraming with coordinate system also being y...
HdRenderPassAovBindingVector const & GetAovBindings() const
Get the aov bindings being used for rendering.
Definition: renderer.h:65
HdRenderThread is a utility that specific render delegates can choose to use depending on their needs...
Definition: renderThread.h:129
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:147