HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
colorCorrectionTask.h
Go to the documentation of this file.
1 //
2 // Copyright 2018 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_COLORCORRECTION_TASK_H
25 #define HDX_COLORCORRECTION_TASK_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/sdf/path.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"
40 
41 #include <string>
42 
44 
45 /// \class HdxColorCorrectionTaskParams
46 ///
47 /// ColorCorrectionTask parameters.
48 ///
49 /// `lut3dSizeOCIO`: We default to 65 (0-64) samples which works well with
50 // OCIO resampling.
51 ///
53 {
54  HDX_API
56 
57  // Switch between HdColorCorrectionTokens.
58  // We default to 'disabled' to be backwards compatible with clients that are
59  // still running with sRGB buffers.
61 
62  // 'display', 'view', 'colorspace' and 'look' are options the client may
63  // supply to configure OCIO. If one is not provided the default values
64  // is substituted. You can find the values for these strings inside the
65  // profile/config .ocio file. For example:
66  //
67  // displays:
68  // rec709g22:
69  // !<View> {name: studio, colorspace: linear, looks: studio_65_lg2}
70  //
75 
76  // The width, height and depth used for the GPU LUT 3d texture.
78 
79  // The name of the aov to color correct
81 };
82 
83 
84 /// \class HdxColorCorrectionTask
85 ///
86 /// A task for performing color correction (and optionally color grading) on a
87 /// color buffer to transform its color for display.
88 ///
90 {
91 public:
92  HDX_API
93  HdxColorCorrectionTask(HdSceneDelegate* delegate, SdfPath const& id);
94 
95  HDX_API
96  ~HdxColorCorrectionTask() override;
97 
98  /// Prepare the tasks resources
99  HDX_API
100  void Prepare(HdTaskContext* ctx,
101  HdRenderIndex* renderIndex) override;
102 
103  /// Execute the color correction task
104  HDX_API
105  void Execute(HdTaskContext* ctx) override;
106 
107 protected:
108  /// Sync the render pass resources
109  HDX_API
110  void _Sync(HdSceneDelegate* delegate,
111  HdTaskContext* ctx,
112  HdDirtyBits* dirtyBits) override;
113 
114 private:
115  HdxColorCorrectionTask() = delete;
116  HdxColorCorrectionTask(const HdxColorCorrectionTask &) = delete;
117  HdxColorCorrectionTask &operator =(const HdxColorCorrectionTask &) = delete;
118 
119  // Description of a texture resource and sampler
120  struct _TextureSamplerDesc {
121  HgiTextureDesc textureDesc;
122  HgiSamplerDesc samplerDesc;
123  std::vector<float> samples;
124  };
125 
126  // Description of a buffer resource
127  struct _UniformBufferDesc {
128  std::string typeName;
130  std::vector<uint8_t> data;
131  uint32_t dataSize;
132  uint32_t count;
133  };
135 
136  // Description of resources required by OCIO GPU implementation
137  struct _OCIOResources {
138  std::vector<_TextureSamplerDesc> luts;
139  std::vector<_UniformBufferDesc> ubos;
140  std::vector<unsigned char> constantValues;
141  std::string gpuShaderText;
142  };
143 
144  // Utility to query OCIO for required resources
145  static void
146  _CreateOpenColorIOResources(Hgi *hgi,
148  _OCIOResources *result);
149 
150  // Utility to check if OCIO should be used
151  bool _GetUseOcio() const;
152 
153  // Utility function to create the GL program for color correction
154  bool _CreateShaderResources();
155 
156  // OCIO version-specific code for shader code generation.
157  std::string _CreateOpenColorIOShaderCode(std::string &ocioGpuShaderText,
158  HgiShaderFunctionDesc &fragDesc);
159 
160  // Utility function to create buffer resources.
161  bool _CreateBufferResources();
162 
163  // Utility to create resource bindings
164  bool _CreateResourceBindings(HgiTextureHandle const& aovTexture);
165 
166  // OCIO version-specific code for setting LUT bindings.
167  void _CreateOpenColorIOLUTBindings(HgiResourceBindingsDesc &resourceDesc);
168 
169  // Utility to create a pipeline
170  bool _CreatePipeline(HgiTextureHandle const& aovTexture);
171 
172  // Utility to create an AOV sampler
173  bool _CreateAovSampler();
174 
175  // Apply color correction to the currently bound framebuffer.
176  void _ApplyColorCorrection(HgiTextureHandle const& aovTexture);
177 
178  // OCIO version-specific code for setting constants.
179  void _SetConstants(HgiGraphicsCmds *gfxCmds);
180 
181  // Destroy shader program and the shader functions it holds.
182  void _DestroyShaderProgram();
183 
184  // Print shader compile errors.
185  void _PrintCompileErrors();
186 
187 private: // data
188 
190  _OCIOResources _ocioResources;
191 
192  HgiAttachmentDesc _attachment0;
193  HgiBufferHandle _indexBuffer;
194  HgiBufferHandle _vertexBuffer;
195  HgiSamplerHandle _aovSampler;
196 
197  struct TextureSamplerInfo
198  {
199  unsigned char dim;
200  std::string texName;
201  HgiTextureHandle texHandle;
202  std::string samplerName;
203  HgiSamplerHandle samplerHandle;
204  };
205  std::vector<TextureSamplerInfo> _textureLUTs;
206 
207  struct BufferInfo
208  {
209  std::string typeName;
211  uint32_t count;
213  };
214  std::vector<BufferInfo> _bufferConstants;
215 
216  HgiShaderProgramHandle _shaderProgram;
217  HgiResourceBindingsHandle _resourceBindings;
218  HgiGraphicsPipelineHandle _pipeline;
219  float _screenSize[2];
220 
221  WorkDispatcher _workDispatcher;
222 };
223 
224 // VtValue requirements
225 HDX_API
226 std::ostream& operator<<(std::ostream& out, const HdxColorCorrectionTaskParams& pv);
227 HDX_API
229  const HdxColorCorrectionTaskParams& rhs);
230 HDX_API
232  const HdxColorCorrectionTaskParams& rhs);
233 
234 
236 
237 #endif
GLboolean * data
Definition: glcorearb.h:131
friend struct HdxColorCorrectionTask_UboBuilder
uint32_t HdDirtyBits
Definition: types.h:158
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
int HgiHandle< class HgiTexture > HgiTextureHandle
**But if you need a result
Definition: thread.h:613
#define HDX_API
Definition: api.h:40
GLenum const GLfloat * params
Definition: glcorearb.h:105
HDX_API void Prepare(HdTaskContext *ctx, HdRenderIndex *renderIndex) override
Prepare the tasks resources.
HDX_API std::ostream & operator<<(std::ostream &out, const HdxColorCorrectionTaskParams &pv)
Definition: token.h:87
HDX_API ~HdxColorCorrectionTask() override
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
GLsizei samples
Definition: glcorearb.h:1298
std::unordered_map< TfToken, VtValue, TfToken::HashFunctor > HdTaskContext
Definition: renderIndex.h:77
Definition: task.h:44
Definition: hgi.h:110
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
Execute the color correction task.
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
GLint GLsizei count
Definition: glcorearb.h:405