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