HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
colorizeSelectionTask.h
Go to the documentation of this file.
1 //
2 // Copyright 2019 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 PXR_IMAGING_HDX_COLORIZE_SELECTION_TASK_H
25 #define PXR_IMAGING_HDX_COLORIZE_SELECTION_TASK_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/base/gf/vec2f.h"
29 #include "pxr/imaging/hdx/api.h"
30 #include "pxr/imaging/hdx/task.h"
31 
33 
35 
36 /// \class HdxColorizeSelectionTaskParams
37 ///
38 /// Input parameters for HdxColorizeSelectionTask
39 ///
41 {
43  : enableSelection(false)
44  , selectionColor(0)
45  , locateColor(0)
46  , enableOutline(false)
47  , outlineRadius(5)
51  {}
52 
57  unsigned int outlineRadius;
58 
62 };
63 
64 /// \class HdxColorizeSelectionTask
65 ///
66 /// A task for taking ID buffer data and turning it into a "selection overlay"
67 /// that can be composited on top of hydra's color output.
68 ///
69 /// If enableOutline is true then instead of overlaying the ID buffer as is, an
70 /// outline with thickness of outlineRadius pixels around the areas with IDs
71 /// will be overlaid. Otherwise, the ID buffer will be overlaid as is.
73 {
74 public:
75  HDX_API
76  HdxColorizeSelectionTask(HdSceneDelegate* delegate, SdfPath const& id);
77 
78  HDX_API
79  ~HdxColorizeSelectionTask() override;
80 
81  /// Hooks for progressive rendering.
82  bool IsConverged() const override;
83 
84  /// Prepare the render pass resources
85  HDX_API
86  void Prepare(HdTaskContext* ctx,
87  HdRenderIndex* renderIndex) override;
88 
89  /// Execute the task
90  HDX_API
91  void Execute(HdTaskContext* ctx) override;
92 
93 protected:
94  /// Sync the render pass resources
95  HDX_API
96  void _Sync(HdSceneDelegate* delegate,
97  HdTaskContext* ctx,
98  HdDirtyBits* dirtyBits) override;
99 
100 private:
101  // The core colorizing logic of this task: given the ID buffers and the
102  // selection buffer, produce a color output at each pixel.
103  void _ColorizeSelection();
104 
105  GfVec4f _GetColorForMode(int mode) const;
106 
107  // Utility function to update the shader uniform parameters.
108  // Returns true if the values were updated. False if unchanged.
109  bool _UpdateParameterBuffer();
110 
111  // Create a new GPU texture for the provided format and pixel data.
112  // If an old texture exists it will be destroyed first.
113  void _CreateTexture(
114  int width,
115  int height,
117  void *data);
118 
119  // This struct must match ParameterBuffer in outline.glslfx.
120  // Be careful to remember the std430 rules.
121  struct _ParameterBuffer
122  {
123  // Size of a colorIn texel - to iterate adjacent texels.
124  GfVec2f texelSize;
125  // Draws outline when enabled, or color overlay when disabled.
126  int enableOutline = 0;
127  // The outline radius (thickness).
128  int radius = 5;
129 
130  bool operator==(const _ParameterBuffer& other) const {
131  return texelSize == other.texelSize &&
132  enableOutline == other.enableOutline &&
133  radius == other.radius;
134  }
135  };
136 
137  // Incoming data
139 
140  int _lastVersion;
141  bool _hasSelection;
142  VtIntArray _selectionOffsets;
143 
144  HdRenderBuffer *_primId;
145  HdRenderBuffer *_instanceId;
146  HdRenderBuffer *_elementId;
147 
148  uint8_t *_outputBuffer;
149  size_t _outputBufferSize;
150  bool _converged;
151 
152  std::unique_ptr<class HdxFullscreenShader> _compositor;
153 
154  _ParameterBuffer _parameterData;
155  HgiTextureHandle _texture;
156  bool _pipelineCreated;
157 };
158 
159 // VtValue requirements
160 HDX_API
161 std::ostream& operator<<(std::ostream& out,
163 HDX_API
165  const HdxColorizeSelectionTaskParams& rhs);
166 HDX_API
168  const HdxColorizeSelectionTaskParams& rhs);
169 
171 
172 #endif // PXR_IMAGING_HDX_COLORIZE_SELECTION_TASK_H
HDX_API bool operator==(const HdxColorizeSelectionTaskParams &lhs, const HdxColorizeSelectionTaskParams &rhs)
HdFormat
Definition: types.h:423
HDX_API std::ostream & operator<<(std::ostream &out, const HdxColorizeSelectionTaskParams &pv)
uint32_t HdDirtyBits
Definition: types.h:158
int HgiHandle< class HgiTexture > HgiTextureHandle
#define HDX_API
Definition: api.h:40
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
bool operator!=(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Inequality operator, does exact floating point comparisons.
Definition: Mat3.h:556
Definition: path.h:291
GLenum mode
Definition: glcorearb.h:99
std::unordered_map< TfToken, VtValue, TfToken::HashFunctor > HdTaskContext
Definition: renderIndex.h:77
HDX_API void Execute(HdTaskContext *ctx) override
Execute the task.
Definition: vec4f.h:62
Definition: task.h:44
HDX_API void Prepare(HdTaskContext *ctx, HdRenderIndex *renderIndex) override
Prepare the render pass resources.
HDX_API HdxColorizeSelectionTask(HdSceneDelegate *delegate, SdfPath const &id)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
Definition: vec2f.h:62
HDX_API ~HdxColorizeSelectionTask() override
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
bool IsConverged() const override
Hooks for progressive rendering.
GLint GLsizei width
Definition: glcorearb.h:103
HDX_API void _Sync(HdSceneDelegate *delegate, HdTaskContext *ctx, HdDirtyBits *dirtyBits) override
Sync the render pass resources.
bool operator==(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Equality operator, does exact floating point comparisons.
Definition: Mat3.h:542
Definition: format.h:895