HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fullscreenShader.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 PXR_IMAGING_HDX_FULLSCREENSHADER_H
8 #define PXR_IMAGING_HDX_FULLSCREENSHADER_H
9 
10 #include "pxr/pxr.h"
11 
12 #include "pxr/imaging/hdx/api.h"
14 
15 #include "pxr/imaging/hgi/buffer.h"
19 
20 #include "pxr/base/gf/vec4i.h"
21 
22 #include "pxr/base/tf/token.h"
23 
24 #include <map>
25 #include <vector>
26 
28 
29 class Hgi;
30 class HioGlslfx;
31 
32 /// \class HdxFullscreenShader
33 ///
34 /// This class is a utility for rendering deep raytracer or aov output
35 /// (color/depth) to a hgi texture. This lets callers composite results
36 /// into existing scenes.
37 ///
39 {
40 public:
41  /// Create a new fullscreen shader object.
42  /// 'debugName' is assigned to the fullscreen pass as gpu debug group that
43  /// is helpful when inspecting the frame on a gpu debugger.
44  HDX_API
45  HdxFullscreenShader(Hgi* hgi, const std::string& debugName);
46 
47  /// Destroy the fullscreen shader object, releasing GPU resources.
48  HDX_API
49  ~HdxFullscreenShader() override;
50 
51  /// Set the program for the class to use for its fragment shader.
52  /// The vertex shader is always hdx/shaders/fullscreen.glslfx,
53  /// "FullScreenVertex", which draws a full-screen triangle.
54  /// The fragment shader should expect an interpolated input parameter with
55  /// the name "uvOut", and whatever textures, constants, or buffers it
56  /// requires.
57  /// \param glslfxPath The path to the glslfx file where the fragment
58  /// shader is located.
59  /// \param shaderName The (technique) name of the fragment shader.
60  /// \param fragDesc Describes inputs, outputs and stage of fragment
61  /// shader.
62  HDX_API
63  void SetProgram(
64  const TfToken& glslfxPath,
65  const TfToken& shaderName,
66  HgiShaderFunctionDesc& fragDesc);
67 
68  /// Bypasses any cache checking or HioGlslfx processing and just re-creates
69  /// the shader program using the "FullScreenVertex" shader and the provided
70  /// fragment shader description.
71  HDX_API
72  void SetProgram(
73  const HgiShaderFunctionDesc& fragDesc);
74 
75  /// Bind (externally managed) buffers to the shader program.
76  /// This function can be used to bind buffers to a custom shader program.
77  /// The lifetime of buffers is managed by the caller. HdxFullscreenShader
78  /// does not take ownership. To update values in the buffer, the client can
79  /// use a blitCmds to copy new data into their buffer.
80  /// Buffers will be bound at the indices corresponding to their position in
81  /// the provided vector.
82  HDX_API
84 
85  /// Bind (externally managed) textures to the shader program and use the
86  /// optionally provided samplers for each one.
87  /// This function can be used to bind textures to a custom shader program.
88  /// The lifetime of textures is managed by the caller as are the samplers.
89  /// HdxFullscreenShader does not take ownership of them.
90  /// Textures will be bound at the indices corresponding to their position in
91  /// the provided vector. Each texture will use the corresponding sampler
92  /// that is provided. If no samplers are provided, or an invalid sampler
93  /// is provided for a texture, a default sampler will be used.
94  HDX_API
95  void BindTextures(
98 
99  /// By default HdxFullscreenShader creates a pipeline object that enables
100  /// depth testing and enables depth write if there is a depth texture.
101  /// This function allows you to override the depth and stencil state.
102  HDX_API
104 
105  /// By default HdxFullscreenShader uses no blending (opaque).
106  /// This function allows you to override blend state (e.g. alpha blending)
107  HDX_API
108  void SetBlendState(
109  bool enableBlending,
110  HgiBlendFactor srcColorBlendFactor,
111  HgiBlendFactor dstColorBlendFactor,
112  HgiBlendOp colorBlendOp,
113  HgiBlendFactor srcAlphaBlendFactor,
114  HgiBlendFactor dstAlphaBlendFactor,
115  HgiBlendOp alphaBlendOp);
116 
117  /// By default HdxFullscreenShader uses LoadOpDontCare and StoreOpStore.
118  /// This function allows you to override the attachment load and store op.
119  HDX_API
121  HgiAttachmentLoadOp attachmentLoadOp,
122  HgiAttachmentStoreOp attachmentStoreOp);
123 
124  /// Provide the shader constant values (uniforms).
125  /// The data values are copied, so you do not have to set them
126  /// each frame if they do not change in value.
127  HDX_API
128  void SetShaderConstants(
129  uint32_t byteSize,
130  const void* data);
131 
132  /// Draw the internal textures to the provided destination textures.
133  /// `depth` is optional.
134  HDX_API
135  void Draw(HgiTextureHandle const& colorDst,
136  HgiTextureHandle const& depthDst);
137 
138  HDX_API
139  void Draw(HgiTextureHandle const& colorDst,
140  HgiTextureHandle const& colorResolveDst,
141  HgiTextureHandle const& depthDst,
142  HgiTextureHandle const& depthResolveDst,
143  GfVec4i const& viewport);
144 
145 private:
146  HdxFullscreenShader() = delete;
147  HdxFullscreenShader(const HdxFullscreenShader&) = delete;
148  HdxFullscreenShader& operator=(const HdxFullscreenShader&) = delete;
149 
150  // Utility function to create buffer resources.
151  void _CreateBufferResources();
152 
153  // Utility to set resource bindings.
154  void _SetResourceBindings();
155 
156  // Utility to create default vertex buffer descriptor.
157  void _SetVertexBufferDescriptor();
158 
159  // Utility to create and get the default texture sampler.
160  HgiSamplerHandle _GetDefaultSampler();
161 
162  // Utility to set the default program.
163  void _SetDefaultProgram(bool writeDepth);
164 
165  // Internal draw method
166  void _Draw(
167  HgiTextureHandle const& colorDst,
168  HgiTextureHandle const& colorResolveDst,
169  HgiTextureHandle const& depthDst,
170  HgiTextureHandle const& depthResolveDst,
171  GfVec4i const &viewport);
172 
173  void _RecordDrawCmds() override;
174 
175  // Print shader compile errors.
176  void _PrintCompileErrors();
177 
178  HgiTextureHandleVector _textures;
179  HgiSamplerHandleVector _samplers;
180  HgiBufferHandleVector _buffers;
181 
182  TfToken _glslfxPath;
183  TfToken _shaderName;
184 
185  HgiBufferHandle _indexBuffer;
186  HgiBufferHandle _vertexBuffer;
187  HgiShaderProgramHandle _shaderProgram;
188  HgiSamplerHandle _defaultSampler;
189 
190  HgiDepthStencilState _depthStencilState;
191 
192  HgiAttachmentDesc _colorAttachment;
193  HgiAttachmentDesc _depthAttachment;
194 };
195 
197 
198 #endif // PXR_IMAGING_HDX_FULLSCREENSHADER_H
Definition: vec4i.h:43
HgiAttachmentLoadOp
Definition: enums.h:258
HDX_API void SetProgram(const TfToken &glslfxPath, const TfToken &shaderName, HgiShaderFunctionDesc &fragDesc)
HDX_API void BindBuffers(HgiBufferHandleVector const &buffers)
GLuint * samplers
Definition: glcorearb.h:1653
int HgiHandle< class HgiTexture > HgiTextureHandle
#define HDX_API
Definition: api.h:23
std::vector< HgiTextureHandle > HgiTextureHandleVector
Definition: texture.h:196
Definition: token.h:70
HgiAttachmentStoreOp
Definition: enums.h:278
HgiBlendFactor
Definition: enums.h:484
std::vector< HgiBufferHandle > HgiBufferHandleVector
Definition: buffer.h:135
HDX_API void Draw(HgiTextureHandle const &colorDst, HgiTextureHandle const &depthDst)
const GLuint * buffers
Definition: glcorearb.h:661
HgiBlendOp
Definition: enums.h:469
HDX_API void SetDepthState(HgiDepthStencilState const &state)
HDX_API ~HdxFullscreenShader() override
Destroy the fullscreen shader object, releasing GPU resources.
Definition: hgi.h:93
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
HDX_API void SetShaderConstants(uint32_t byteSize, const void *data)
HDX_API void BindTextures(HgiTextureHandleVector const &textures, HgiSamplerHandleVector const &samplers=HgiSamplerHandleVector())
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
HDX_API void SetBlendState(bool enableBlending, HgiBlendFactor srcColorBlendFactor, HgiBlendFactor dstColorBlendFactor, HgiBlendOp colorBlendOp, HgiBlendFactor srcAlphaBlendFactor, HgiBlendFactor dstAlphaBlendFactor, HgiBlendOp alphaBlendOp)
std::vector< HgiSamplerHandle > HgiSamplerHandleVector
Definition: sampler.h:143
const GLuint * textures
Definition: glcorearb.h:416
HDX_API void SetAttachmentLoadStoreOp(HgiAttachmentLoadOp attachmentLoadOp, HgiAttachmentStoreOp attachmentStoreOp)
state
Definition: core.h:2289
Definition: format.h:1821