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