HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
visualizeAovTask.h
Go to the documentation of this file.
1 //
2 // Copyright 2021 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef HDX_VISUALIZE_AOV_TASK_H
8 #define HDX_VISUALIZE_AOV_TASK_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/base/gf/vec3i.h"
12 #include "pxr/imaging/hdx/api.h"
13 #include "pxr/imaging/hdx/task.h"
14 #include "pxr/imaging/hdx/tokens.h"
16 #include "pxr/imaging/hgi/buffer.h"
21 #include "pxr/usd/sdf/path.h"
22 
24 
26 
27 /// \class HdxVisualizeAovTask
28 ///
29 /// A task for visualizing non-color AOVs such as depth, normals, primId.
30 ///
31 /// Different kernels are used depending on the AOV:
32 /// Depth: Renormalized from the range [0.0, 1.0] to [min, max] depth
33 /// to provide better contrast.
34 /// Normals: Transform each component from [-1.0, 1.0] tp [0.0, 1.0] so that
35 /// negative components don't appear black.
36 /// Ids: Integer ids are colorized by multiplying by a large prime and
37 /// shuffling resulting bits so that neighboring ids are easily
38 /// distinguishable.
39 /// Other Aovs: A fallback kernel that transfers the AOV contents into a
40 /// float texture is used.
41 ///
42 /// This task updates the 'color' entry of the task context with the colorized
43 /// texture contents.
44 ///
46 {
47 public:
49 
50  HDX_API
51  HdxVisualizeAovTask(HdSceneDelegate* delegate, SdfPath const& id);
52 
53  HDX_API
54  ~HdxVisualizeAovTask() override;
55 
56  HDX_API
57  void Prepare(HdTaskContext* ctx,
58  HdRenderIndex* renderIndex) override;
59 
60  HDX_API
61  void Execute(HdTaskContext* ctx) override;
62 
63 protected:
64  HDX_API
65  void _Sync(HdSceneDelegate* delegate,
66  HdTaskContext* ctx,
67  HdDirtyBits* dirtyBits) override;
68 
69 private:
70  // Enumeration of visualization kernels
71  enum VizKernel {
72  VizKernelDepth = 0,
73  VizKernelId,
74  VizKernelNormal,
75  VizKernelFallback,
76  VizKernelNone
77  };
78 
79  HdxVisualizeAovTask() = delete;
80  HdxVisualizeAovTask(const HdxVisualizeAovTask &) = delete;
81  HdxVisualizeAovTask &operator =(const HdxVisualizeAovTask &) = delete;
82 
83  // Returns true if the enum member was updated, indicating that the kernel
84  // to be used has changed.
85  bool _UpdateVizKernel(TfToken const &aovName);
86 
87  // Returns a token used in sampling the texture based on the kernel used.
88  TfToken const& _GetTextureIdentifierForShader() const;
89 
90  // Returns the fragment shader mixin based on the kernel used.
91  TfToken const& _GetFragmentMixin() const;
92 
93  // ------------- Hgi resource creation/deletion utilities ------------------
94  // Utility function to create the GL program for color correction
95  bool _CreateShaderResources(HgiTextureDesc const& inputAovTextureDesc);
96 
97  // Utility function to create buffer resources.
98  bool _CreateBufferResources();
99 
100  // Utility to create resource bindings
101  bool _CreateResourceBindings(HgiTextureHandle const& inputAovTexture);
102 
103  // Utility to create a pipeline
104  bool _CreatePipeline(HgiTextureDesc const& outputTextureDesc);
105 
106  // Utility to create a texture sampler
107  bool _CreateSampler(HgiTextureDesc const& inputAovTextureDesc);
108 
109  // Create texture to write the colorized results into.
110  bool _CreateOutputTexture(GfVec3i const &dimensions);
111 
112  // Destroy shader program and the shader functions it holds.
113  void _DestroyShaderProgram();
114 
115  // Print shader compile errors.
116  void _PrintCompileErrors();
117  // -------------------------------------------------------------------------
118 
119  // Readback the depth AOV on the CPU to update min, max values.
120  void _UpdateMinMaxDepth(HgiTextureHandle const &inputAovTexture);
121 
122  // Execute the appropriate kernel and update the task context 'color' entry.
123  void _ApplyVisualizationKernel(HgiTextureHandle const& outputTexture);
124 
125  // Kernel dependent resources
126  HgiTextureHandle _outputTexture;
127  GfVec3i _outputTextureDimensions;
128  HgiAttachmentDesc _outputAttachmentDesc;
129  HgiShaderProgramHandle _shaderProgram;
130  HgiResourceBindingsHandle _resourceBindings;
131  HgiGraphicsPipelineHandle _pipeline;
132 
133  // Kernel independent resources
134  HgiBufferHandle _indexBuffer;
135  HgiBufferHandle _vertexBuffer;
136  HgiSamplerHandle _sampler;
137 
138  float _screenSize[2];
139  float _minMaxDepth[2];
140  VizKernel _vizKernel;
141 };
142 
143 
144 /// \class HdxVisualizeAovTaskParams
145 ///
146 /// `aovName`: The name of the aov to visualize.
147 ///
148 /// The Hgi texture resource backing the AOV is retreived from the task context
149 /// instead of fetching the render buffer prim via its render index path.
150 /// HdxAovInputTask is responsible for updating the task context entry for
151 /// the active AOV.
152 ///
154 {
155  HDX_API
157 
159 };
160 
161 // VtValue requirements
162 HDX_API
163 std::ostream& operator<<(std::ostream& out, const HdxVisualizeAovTaskParams& pv);
164 HDX_API
165 bool operator==(const HdxVisualizeAovTaskParams& lhs,
166  const HdxVisualizeAovTaskParams& rhs);
167 HDX_API
168 bool operator!=(const HdxVisualizeAovTaskParams& lhs,
169  const HdxVisualizeAovTaskParams& rhs);
170 
172 
173 #endif
HDX_API HdxVisualizeAovTaskParams()
uint32_t HdDirtyBits
Definition: types.h:143
HDX_API void Prepare(HdTaskContext *ctx, HdRenderIndex *renderIndex) override
int HgiHandle< class HgiTexture > HgiTextureHandle
#define HDX_API
Definition: api.h:23
Definition: token.h:70
HDX_API std::ostream & operator<<(std::ostream &out, const HdxVisualizeAovTaskParams &pv)
bool operator!=(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Inequality operator, does exact floating point comparisons.
Definition: Mat3.h:556
HDX_API ~HdxVisualizeAovTask() override
Definition: vec3i.h:43
Definition: path.h:273
std::unordered_map< TfToken, VtValue, TfToken::HashFunctor > HdTaskContext
Definition: renderIndex.h:61
Definition: task.h:27
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
HDX_API void Execute(HdTaskContext *ctx) override
HDX_API void _Sync(HdSceneDelegate *delegate, HdTaskContext *ctx, HdDirtyBits *dirtyBits) override
bool operator==(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Equality operator, does exact floating point comparisons.
Definition: Mat3.h:542