visualizeAovTask.h
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"
32 #include "pxr/imaging/hgi/attachmentDesc.h"
33 #include "pxr/imaging/hgi/buffer.h"
34 #include "pxr/imaging/hgi/graphicsPipeline.h"
35 #include "pxr/imaging/hgi/resourceBindings.h"
36 #include "pxr/imaging/hgi/shaderProgram.h"
37 #include "pxr/imaging/hgi/texture.h"
38 #include "pxr/usd/sdf/path.h"
39 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
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 
167 {
168  HDX_API
170 
171  TfToken aovName;
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 
184 PXR_NAMESPACE_CLOSE_SCOPE
185 
186 #endif
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:120
Describes the properties of a framebuffer attachment.
A task for visualizing non-color AOVs such as depth, normals, primId.
AR_API bool operator!=(const ArAssetInfo &lhs, const ArAssetInfo &rhs)
AR_API bool operator==(const ArAssetInfo &lhs, const ArAssetInfo &rhs)
Describes the properties needed to create a GPU texture.
Definition: texture.h:107
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
aovName: The name of the aov to visualize.
Adapter class providing data exchange with the client scene graph.
Basic type for a vector of 3 int components.
Definition: vec3i.h:61
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
Base class for (some) tasks in Hdx that provides common progressive rendering and Hgi functionality.
Definition: task.h:44
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].