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  /// Note that this also sets the depth loadOp to Load if a nontrivial depth
103  //// compare is active.
104  HDX_API
106 
107  /// By default HdxFullscreenShader uses no blending (opaque).
108  /// This function allows you to override blend state (e.g. alpha blending).
109  /// Note that this also sets the color loadOp to Load if blending is on.
110  HDX_API
111  void SetBlendState(
112  bool enableBlending,
113  HgiBlendFactor srcColorBlendFactor,
114  HgiBlendFactor dstColorBlendFactor,
115  HgiBlendOp colorBlendOp,
116  HgiBlendFactor srcAlphaBlendFactor,
117  HgiBlendFactor dstAlphaBlendFactor,
118  HgiBlendOp alphaBlendOp);
119 
120  /// Ask HdxFullscreenShader to do a color/depth clear. Note that this
121  /// sets loadOp to Clear, and should be used if you're using the fullscreen
122  /// shader as a background/clear pass, to make sure especially that
123  /// depth/stencil get cleared correctly.
124  HDX_API
125  void SetClearState(
126  GfVec4f clearColor,
127  GfVec4f clearDepth);
128 
129  /// Provide the shader constant values (uniforms).
130  /// The data values are copied, so you do not have to set them
131  /// each frame if they do not change in value.
132  HDX_API
133  void SetShaderConstants(
134  uint32_t byteSize,
135  const void* data);
136 
137  /// Draw the internal textures to the provided destination textures.
138  /// `depth` is optional.
139  HDX_API
140  void Draw(HgiTextureHandle const& colorDst,
141  HgiTextureHandle const& depthDst);
142 
143  HDX_API
144  void Draw(HgiTextureHandle const& colorDst,
145  HgiTextureHandle const& colorResolveDst,
146  HgiTextureHandle const& depthDst,
147  HgiTextureHandle const& depthResolveDst,
148  GfVec4i const& viewport);
149 
150 private:
151  HdxFullscreenShader() = delete;
152  HdxFullscreenShader(const HdxFullscreenShader&) = delete;
153  HdxFullscreenShader& operator=(const HdxFullscreenShader&) = delete;
154 
155  // Utility function to create buffer resources.
156  void _CreateBufferResources();
157 
158  // Utility to set resource bindings.
159  void _SetResourceBindings();
160 
161  // Utility to create default vertex buffer descriptor.
162  void _SetVertexBufferDescriptor();
163 
164  // Utility to create and get the default texture sampler.
165  HgiSamplerHandle _GetDefaultSampler();
166 
167  // Utility to set the default program.
168  void _SetDefaultProgram(bool writeDepth);
169 
170  // Internal draw method
171  void _Draw(
172  HgiTextureHandle const& colorDst,
173  HgiTextureHandle const& colorResolveDst,
174  HgiTextureHandle const& depthDst,
175  HgiTextureHandle const& depthResolveDst,
176  GfVec4i const &viewport);
177 
178  void _RecordDrawCmds() override;
179 
180  // Print shader compile errors.
181  void _PrintCompileErrors();
182 
183  HgiTextureHandleVector _textures;
184  HgiSamplerHandleVector _samplers;
185  HgiBufferHandleVector _buffers;
186 
187  TfToken _glslfxPath;
188  TfToken _shaderName;
189 
190  HgiBufferHandle _indexBuffer;
191  HgiBufferHandle _vertexBuffer;
192  HgiShaderProgramHandle _shaderProgram;
193  HgiSamplerHandle _defaultSampler;
194 
195  HgiDepthStencilState _depthStencilState;
196 
197  HgiAttachmentDesc _colorAttachment;
198  HgiAttachmentDesc _depthAttachment;
199 };
200 
202 
203 #endif // PXR_IMAGING_HDX_FULLSCREENSHADER_H
Definition: vec4i.h:43
HDX_API void SetProgram(const TfToken &glslfxPath, const TfToken &shaderName, HgiShaderFunctionDesc &fragDesc)
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
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:197
Definition: token.h:70
HgiBlendFactor
Definition: enums.h:491
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:476
HDX_API void SetDepthState(HgiDepthStencilState const &state)
HDX_API ~HdxFullscreenShader() override
Destroy the fullscreen shader object, releasing GPU resources.
Definition: vec4f.h:45
Definition: hgi.h:94
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
state
Definition: core.h:2289
HDX_API void SetClearState(GfVec4f clearColor, GfVec4f clearDepth)
Definition: format.h:1821