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