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