boundingBoxTask.h
1 //
2 // Copyright 2022 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_BOUNDINGBOX_TASK_H
25 #define HDX_BOUNDINGBOX_TASK_H
26 
27 #include "pxr/pxr.h"
28 
29 #include "pxr/base/gf/matrix4f.h"
30 
31 #include "pxr/imaging/hdx/api.h"
32 #include "pxr/imaging/hdx/task.h"
33 #include "pxr/imaging/hdx/tokens.h"
34 
35 #include "pxr/imaging/hd/camera.h"
36 
37 #include "pxr/imaging/hgi/attachmentDesc.h"
38 #include "pxr/imaging/hgi/buffer.h"
39 #include "pxr/imaging/hgi/graphicsCmds.h"
40 #include "pxr/imaging/hgi/graphicsPipeline.h"
41 #include "pxr/imaging/hgi/resourceBindings.h"
42 #include "pxr/imaging/hgi/shaderProgram.h"
43 #include "pxr/imaging/hgi/texture.h"
44 
45 #include "pxr/usd/sdf/path.h"
46 
47 PXR_NAMESPACE_OPEN_SCOPE
48 
50 
56 {
57  using BBoxVector = std::vector<GfBBox3d>;
58 
59  HDX_API
61  : aovName()
62  , bboxes()
63  , color(1)
64  , dashSize(3)
65  {}
66 
67  TfToken aovName;
68 
69  // Data provided by the application
70  BBoxVector bboxes;
71  GfVec4f color;
72  float dashSize;
73 };
74 
80 {
81 public:
82  HDX_API
83  HdxBoundingBoxTask(HdSceneDelegate* delegate, const SdfPath& id);
84 
85  HDX_API
86  ~HdxBoundingBoxTask() override;
87 
89  HDX_API
90  void Prepare(HdTaskContext* ctx,
91  HdRenderIndex* renderIndex) override;
92 
94  HDX_API
95  void Execute(HdTaskContext* ctx) override;
96 
97 protected:
99  HDX_API
100  void _Sync(HdSceneDelegate* delegate,
101  HdTaskContext* ctx,
102  HdDirtyBits* dirtyBits) override;
103 
104 private:
105  HdxBoundingBoxTask() = delete;
106  HdxBoundingBoxTask(const HdxBoundingBoxTask&) = delete;
107  HdxBoundingBoxTask &operator =(const HdxBoundingBoxTask&) = delete;
108 
109  // Utility function to create the shader for drawing dashed lines.
110  bool _CreateShaderResources();
111 
112  // Utility function to create buffer resources.
113  bool _CreateBufferResources();
114 
115  // Utility to create resource bindings
116  bool _CreateResourceBindings();
117 
118  // Utility to create a pipeline.
119  bool _CreatePipeline(
120  const HgiTextureHandle& colorTexture,
121  const HgiTextureHandle& depthTexture);
122 
123  // Utility to get the view and projection matrix from the camera.
124  GfMatrix4d _ComputeViewProjectionMatrix(
125  const HdStRenderPassState& hdStRenderPassState) const;
126 
127  // Utility to set the shader constants for drawing.
128  void _UpdateShaderConstants(
129  HgiGraphicsCmds* gfxCmds,
130  const GfVec4i& gfxViewport,
131  const HdStRenderPassState& hdStRenderPassState);
132 
133  // Create and submit the draw commands.
134  void _DrawBBoxes(
135  const HgiTextureHandle& colorTexture,
136  const HgiTextureHandle& depthTexture,
137  const HdStRenderPassState& hdStRenderPassState);
138 
139  // Destroy shader program and the shader functions it holds.
140  void _DestroyShaderProgram();
141 
142  // Print shader compile errors.
143  void _PrintCompileErrors();
144 
145  HgiAttachmentDesc _colorAttachment;
146  HgiAttachmentDesc _depthAttachment;
147 
148  HgiBufferHandle _vertexBuffer;
149  size_t _maxTransforms;
150  HgiBufferHandle _transformsBuffer;
151  HgiShaderProgramHandle _shaderProgram;
152  HgiResourceBindingsHandle _resourceBindings;
153  HgiGraphicsPipelineHandle _pipeline;
154 
155  HdxBoundingBoxTaskParams _params;
156 };
157 
158 // VtValue requirements
159 HDX_API
160 std::ostream& operator<<(std::ostream& out, const HdxBoundingBoxTaskParams& pv);
161 HDX_API
162 bool operator==(const HdxBoundingBoxTaskParams& lhs,
163  const HdxBoundingBoxTaskParams& rhs);
164 HDX_API
165 bool operator!=(const HdxBoundingBoxTaskParams& lhs,
166  const HdxBoundingBoxTaskParams& rhs);
167 
168 PXR_NAMESPACE_CLOSE_SCOPE
169 
170 #endif
Basic type for a vector of 4 int components.
Definition: vec4i.h:61
HDX_API void Execute(HdTaskContext *ctx) override
Execute the bounding box task.
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:120
A graphics API independent abstraction of graphics commands.
Definition: graphicsCmds.h:47
Describes the properties of a framebuffer attachment.
AR_API bool operator!=(const ArAssetInfo &lhs, const ArAssetInfo &rhs)
A set of rendering parameters used among render passes.
AR_API bool operator==(const ArAssetInfo &lhs, const ArAssetInfo &rhs)
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
Adapter class providing data exchange with the client scene graph.
A task for rendering bounding boxes.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
Basic type for a vector of 4 float components.
Definition: vec4f.h:63
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].
HDX_API void _Sync(HdSceneDelegate *delegate, HdTaskContext *ctx, HdDirtyBits *dirtyBits) override
Sync the render pass resources.
HDX_API void Prepare(HdTaskContext *ctx, HdRenderIndex *renderIndex) override
Prepare the bounding box task resources.
BoundingBoxTask parameters.