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"
31 #include "pxr/imaging/hdx/task.h"
32 
33 #include "pxr/imaging/hgi/buffer.h"
35 
37 
38 /// \class HdxColorizeSelectionTaskParams
39 ///
40 /// Input parameters for HdxColorizeSelectionTask
41 ///
43 {
45  : enableSelection(false)
46  , selectionColor(0)
47  , locateColor(0)
48  , enableOutline(false)
49  , outlineRadius(5)
53  {}
54 
59  unsigned int outlineRadius;
60 
64 };
65 
66 /// \class HdxColorizeSelectionTask
67 ///
68 /// A task for taking ID buffer data and turning it into a "selection overlay"
69 /// that can be composited on top of hydra's color output.
70 ///
71 /// If enableOutline is true then instead of overlaying the ID buffer as is, an
72 /// outline with thickness of outlineRadius pixels around the areas with IDs
73 /// will be overlaid. Otherwise, the ID buffer will be overlaid as is.
75 {
76 public:
77  HDX_API
78  HdxColorizeSelectionTask(HdSceneDelegate* delegate, SdfPath const& id);
79 
80  HDX_API
81  virtual ~HdxColorizeSelectionTask();
82 
83  /// Hooks for progressive rendering.
84  virtual bool IsConverged() const override;
85 
86  /// Prepare the render pass resources
87  HDX_API
88  virtual void Prepare(HdTaskContext* ctx,
89  HdRenderIndex* renderIndex) override;
90 
91  /// Execute the task
92  HDX_API
93  virtual void Execute(HdTaskContext* ctx) override;
94 
95 protected:
96  /// Sync the render pass resources
97  HDX_API
98  virtual void _Sync(HdSceneDelegate* delegate,
99  HdTaskContext* ctx,
100  HdDirtyBits* dirtyBits) override;
101 
102 private:
103  // The core colorizing logic of this task: given the ID buffers and the
104  // selection buffer, produce a color output at each pixel.
105  void _ColorizeSelection();
106 
107  GfVec4f _GetColorForMode(int mode) const;
108 
109  // Utility function to create a storage buffer for the shader parameters.
110  void _CreateParameterBuffer();
111 
112  // Create a new GPU texture for the provided format and pixel data.
113  // If an old texture exists it will be destroyed first.
114  void _CreateTexture(
115  int width,
116  int height,
118  void *data);
119 
120  // This struct must match ParameterBuffer in outline.glslfx.
121  // Be careful to remember the std430 rules.
122  struct _ParameterBuffer
123  {
124  // Size of a colorIn texel - to iterate adjacent texels.
125  GfVec2f texelSize;
126  // Draws outline when enabled, or color overlay when disabled.
127  int enableOutline = 0;
128  // The outline radius (thickness).
129  int radius = 5;
130 
131  bool operator==(const _ParameterBuffer& other) const {
132  return texelSize == other.texelSize &&
133  enableOutline == other.enableOutline &&
134  radius == other.radius;
135  }
136  };
137 
138  // Incoming data
140 
141  int _lastVersion;
142  bool _hasSelection;
143  VtIntArray _selectionOffsets;
144 
145  HdRenderBuffer *_primId;
146  HdRenderBuffer *_instanceId;
147  HdRenderBuffer *_elementId;
148 
149  uint8_t *_outputBuffer;
150  size_t _outputBufferSize;
151  bool _converged;
152 
153  std::unique_ptr<HdxFullscreenShader> _compositor;
154 
155  _ParameterBuffer _parameterData;
156  HgiBufferHandle _parameterBuffer;
157  HgiTextureHandle _texture;
158  bool _pipelineCreated;
159 };
160 
161 // VtValue requirements
162 HDX_API
163 std::ostream& operator<<(std::ostream& out,
165 HDX_API
167  const HdxColorizeSelectionTaskParams& rhs);
168 HDX_API
170  const HdxColorizeSelectionTaskParams& rhs);
171 
173 
174 #endif // PXR_IMAGING_HDX_COLORIZE_SELECTION_TASK_H
HDX_API bool operator==(const HdxColorizeSelectionTaskParams &lhs, const HdxColorizeSelectionTaskParams &rhs)
HdFormat
Definition: types.h:382
HDX_API std::ostream & operator<<(std::ostream &out, const HdxColorizeSelectionTaskParams &pv)
GLenum mode
Definition: glew.h:2163
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1252
uint32_t HdDirtyBits
Definition: types.h:127
virtual HDX_API void Execute(HdTaskContext *ctx) override
Execute the task.
GLint GLint GLint GLint GLint GLint GLsizei GLsizei height
Definition: glew.h:1252
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: glew.h:1254
virtual HDX_API ~HdxColorizeSelectionTask()
#define HDX_API
Definition: api.h:40
virtual bool IsConverged() const override
Hooks for progressive rendering.
virtual HDX_API void Prepare(HdTaskContext *ctx, HdRenderIndex *renderIndex) override
Prepare the render pass resources.
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
bool operator!=(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Inequality operator, does exact floating point comparisons.
Definition: Mat3.h:563
Definition: path.h:288
std::unordered_map< TfToken, VtValue, TfToken::HashFunctor > HdTaskContext
Definition: renderIndex.h:73
Definition: vec4f.h:63
Definition: task.h:44
HDX_API HdxColorizeSelectionTask(HdSceneDelegate *delegate, SdfPath const &id)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
Definition: vec2f.h:63
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
virtual 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:549