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"
30 #include "pxr/imaging/hd/types.h"
31 #include "pxr/base/gf/vec4i.h"
32 #include "pxr/imaging/hgi/buffer.h"
37 
38 #include <map>
39 #include <vector>
40 
42 
43 class Hgi;
44 
45 /// \class HdxFullscreenShader
46 ///
47 /// This class is a utility for rendering deep raytracer or aov output
48 /// (color/depth) to a hgi texture. This lets callers composite results
49 /// into existing scenes.
50 ///
52 {
53 public:
54  /// Create a new fullscreen shader object.
55  /// 'debugName' is assigned to the fullscreen pass as gpu debug group that
56  /// is helpful when inspecting the frame on a gpu debugger.
57  HDX_API
58  HdxFullscreenShader(Hgi* hgi, std::string const& debugName);
59 
60  /// Destroy the fullscreen shader object, releasing GPU resources.
61  HDX_API
63 
64  /// Set the program for the class to use for its fragment shader.
65  /// The vertex shader is always hdx/shaders/fullscreen.glslfx,
66  /// "FullScreenVertex", which draws a full-screen triangle.
67  /// The fragment shader should expect a varying called "uv", and
68  /// whatever textures or uniforms have been passed in by the caller.
69  /// \param glslfx The name of the glslfx file where the fragment shader
70  /// is located.
71  /// \param shaderName The (technique) name of the fragment shader.
72  /// \param vertDesc Describes inputs, outputs and stage of vertex shader.
73  /// \param fragDesc Describes inputs, outputs and stage of fragment shader.
74  HDX_API
75  void SetProgram(
76  TfToken const& glslfx,
77  TfToken const& shaderName,
78  HgiShaderFunctionDesc &fragDesc,
79  HgiShaderFunctionDesc vertDesc = GetFullScreenVertexDesc()
80  );
81 
82  /// Bind a (externally managed) buffer to the shader program.
83  /// This function can be used to bind buffers to a custom shader program.
84  /// The lifetime of the buffer is managed by the caller. HdxFullscreenShader
85  /// does not take ownership. To update values in the buffer, the client can
86  /// use a blitCmds to copy new data into their buffer.
87  /// If an invalid 'buffer' is passed, the binding will be cleared.
88  HDX_API
89  void BindBuffer(HgiBufferHandle const& buffer, uint32_t bindingIndex);
90 
91  /// Bind (externally managed) textures to the shader program.
92  /// This function can be used to bind textures to a custom shader program.
93  /// The lifetime of textures is managed by the caller. HdxFullscreenShader
94  /// does not take ownership.
95  /// If an invalid 'texture' is passed, the binding will be cleared.
96  HDX_API
97  void BindTextures(
98  TfTokenVector const& names,
100 
101  /// By default HdxFullscreenShader creates a pipeline object that enables
102  /// depth testing and enables depth write if there is a depth texture.
103  /// This function allows you to override the depth and stencil state.
104  HDX_API
105  void SetDepthState(HgiDepthStencilState const& state);
106 
107  /// By default HdxFullscreenShader uses no blending (opaque).
108  /// This function allows you to override blend state (e.g. alpha blending)
109  HDX_API
110  void SetBlendState(
111  bool enableBlending,
112  HgiBlendFactor srcColorBlendFactor,
113  HgiBlendFactor dstColorBlendFactor,
114  HgiBlendOp colorBlendOp,
115  HgiBlendFactor srcAlphaBlendFactor,
116  HgiBlendFactor dstAlphaBlendFactor,
117  HgiBlendOp alphaBlendOp);
118 
119  /// By default HdxFullscreenShader uses LoadOpDontCare and StoreOpStore.
120  /// This function allows you to override the attachment load and store op.
121  HDX_API
123  HgiAttachmentLoadOp attachmentLoadOp,
124  HgiAttachmentStoreOp attachmentStoreOp);
125 
126  /// Provide the shader constant values (uniforms).
127  /// The data values are copied, so you do not have to set them
128  /// each frame if they do not change in value.
129  HDX_API
130  void SetShaderConstants(
131  uint32_t byteSize,
132  const void* data);
133 
134  /// Draw the internal textures to the provided destination textures.
135  /// `depth` is optional.
136  HDX_API
137  void Draw(HgiTextureHandle const& colorDst,
138  HgiTextureHandle const& depthDst);
139 
140  HDX_API
141  void Draw(HgiTextureHandle const& colorDst,
142  HgiTextureHandle const& colorResolveDst,
143  HgiTextureHandle const& depthDst,
144  HgiTextureHandle const& depthResolveDst,
145  GfVec4i const& viewport);
146 
147 private:
148  HdxFullscreenShader() = delete;
149 
150  using TextureMap = std::map<TfToken, HgiTextureHandle>;
151  using BufferMap = std::map<uint32_t, HgiBufferHandle>;
152 
153  // Utility function to create buffer resources.
154  void _CreateBufferResources();
155 
156  // Destroy shader program and the shader functions it holds.
157  void _DestroyShaderProgram();
158 
159  // Utility to create resource bindings
160  bool _CreateResourceBindings(TextureMap const& textures);
161 
162  // Utility to create default vertex buffer descriptor
163  void _CreateVertexBufferDescriptor();
164 
165  // Utility to create a pipeline
166  bool _CreatePipeline(
167  HgiTextureHandle const& colorDst,
168  HgiTextureHandle const& depthDst,
169  bool depthWrite);
170 
171  // Utility to create a texture sampler
172  bool _CreateSampler();
173 
174  // Internal draw method
175  void _Draw(
176  TextureMap const& textures,
177  HgiTextureHandle const& colorDst,
178  HgiTextureHandle const& colorResolveDst,
179  HgiTextureHandle const& depthDst,
180  HgiTextureHandle const& depthResolveDst,
181  GfVec4i const &viewport,
182  bool depthWrite);
183 
184  static HgiShaderFunctionDesc GetFullScreenVertexDesc();
185 
186  // Print shader compile errors.
187  void _PrintCompileErrors();
188 
189  class Hgi* _hgi;
190 
191  std::string _debugName;
192 
193  TextureMap _textures;
194  BufferMap _buffers;
195 
196  TfToken _glslfx;
197  TfToken _shaderName;
198 
199  HgiBufferHandle _indexBuffer;
200  HgiBufferHandle _vertexBuffer;
201  HgiShaderProgramHandle _shaderProgram;
202  HgiResourceBindingsHandle _resourceBindings;
203  HgiGraphicsPipelineHandle _pipeline;
204  HgiSamplerHandle _sampler;
205  HgiVertexBufferDesc _vboDesc;
206 
207  HgiDepthStencilState _depthState;
208 
209  bool _blendingEnabled;
210  HgiBlendFactor _srcColorBlendFactor;
211  HgiBlendFactor _dstColorBlendFactor;
212  HgiBlendOp _colorBlendOp;
213  HgiBlendFactor _srcAlphaBlendFactor;
214  HgiBlendFactor _dstAlphaBlendFactor;
215  HgiBlendOp _alphaBlendOp;
216 
217  HgiAttachmentLoadOp _attachmentLoadOp;
218  HgiAttachmentStoreOp _attachmentStoreOp;
219 
220  HgiAttachmentDesc _attachment0;
221  HgiAttachmentDesc _depthAttachment;
222 
223  std::vector<uint8_t> _constantsData;
224 };
225 
227 
228 #endif // PXR_IMAGING_HDX_FULLSCREENSHADER_H
Definition: vec4i.h:60
HDX_API void BindTextures(TfTokenVector const &names, HgiTextureHandleVector const &textures)
HgiAttachmentLoadOp
Definition: enums.h:272
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
int HgiHandle< class HgiTexture > HgiTextureHandle
#define HDX_API
Definition: api.h:40
std::vector< HgiTextureHandle > HgiTextureHandleVector
Definition: texture.h:207
Definition: token.h:87
HgiAttachmentStoreOp
Definition: enums.h:292
Definition: core.h:760
HgiBlendFactor
Definition: enums.h:497
HDX_API void Draw(HgiTextureHandle const &colorDst, HgiTextureHandle const &depthDst)
HDX_API ~HdxFullscreenShader()
Destroy the fullscreen shader object, releasing GPU resources.
HgiBlendOp
Definition: enums.h:482
HDX_API void SetDepthState(HgiDepthStencilState const &state)
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
HDX_API void SetProgram(TfToken const &glslfx, TfToken const &shaderName, HgiShaderFunctionDesc &fragDesc, HgiShaderFunctionDesc vertDesc=GetFullScreenVertexDesc())
Definition: hgi.h:110
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
HDX_API void SetShaderConstants(uint32_t byteSize, const void *data)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
HDX_API void BindBuffer(HgiBufferHandle const &buffer, uint32_t bindingIndex)
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