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  /// Provide the shader constant values (uniforms).
120  /// The data values are copied, so you do not have to set them
121  /// each frame if they do not change in value.
122  HDX_API
123  void SetShaderConstants(
124  uint32_t byteSize,
125  const void* data);
126 
127  /// Draw the internal textures to the provided destination textures.
128  /// `depth` is optional.
129  HDX_API
130  void Draw(HgiTextureHandle const& colorDst,
131  HgiTextureHandle const& depthDst);
132 
133  HDX_API
134  void Draw(HgiTextureHandle const& colorDst,
135  HgiTextureHandle const& colorResolveDst,
136  HgiTextureHandle const& depthDst,
137  HgiTextureHandle const& depthResolveDst,
138  GfVec4i const& viewport);
139 
140 private:
141  HdxFullscreenShader() = delete;
142 
143  using TextureMap = std::map<TfToken, HgiTextureHandle>;
144  using BufferMap = std::map<uint32_t, HgiBufferHandle>;
145 
146  // Utility function to create buffer resources.
147  void _CreateBufferResources();
148 
149  // Destroy shader program and the shader functions it holds.
150  void _DestroyShaderProgram();
151 
152  // Utility to create resource bindings
153  bool _CreateResourceBindings(TextureMap const& textures);
154 
155  // Utility to create default vertex buffer descriptor
156  void _CreateVertexBufferDescriptor();
157 
158  // Utility to create a pipeline
159  bool _CreatePipeline(
160  HgiTextureHandle const& colorDst,
161  HgiTextureHandle const& depthDst,
162  bool depthWrite);
163 
164  // Utility to create a texture sampler
165  bool _CreateSampler();
166 
167  // Internal draw method
168  void _Draw(
169  TextureMap const& textures,
170  HgiTextureHandle const& colorDst,
171  HgiTextureHandle const& colorResolveDst,
172  HgiTextureHandle const& depthDst,
173  HgiTextureHandle const& depthResolveDst,
174  GfVec4i const &viewport,
175  bool depthWrite);
176 
177  static HgiShaderFunctionDesc GetFullScreenVertexDesc();
178 
179  // Print shader compile errors.
180  void _PrintCompileErrors();
181 
182  class Hgi* _hgi;
183 
184  std::string _debugName;
185 
186  TextureMap _textures;
187  BufferMap _buffers;
188 
189  TfToken _glslfx;
190  TfToken _shaderName;
191 
192  HgiBufferHandle _indexBuffer;
193  HgiBufferHandle _vertexBuffer;
194  HgiShaderProgramHandle _shaderProgram;
195  HgiResourceBindingsHandle _resourceBindings;
196  HgiGraphicsPipelineHandle _pipeline;
197  HgiSamplerHandle _sampler;
198  HgiVertexBufferDesc _vboDesc;
199 
200  HgiDepthStencilState _depthState;
201 
202  bool _blendingEnabled;
203  HgiBlendFactor _srcColorBlendFactor;
204  HgiBlendFactor _dstColorBlendFactor;
205  HgiBlendOp _colorBlendOp;
206  HgiBlendFactor _srcAlphaBlendFactor;
207  HgiBlendFactor _dstAlphaBlendFactor;
208  HgiBlendOp _alphaBlendOp;
209 
210  HgiAttachmentDesc _attachment0;
211  HgiAttachmentDesc _depthAttachment;
212 
213  std::vector<uint8_t> _constantsData;
214 };
215 
217 
218 #endif // PXR_IMAGING_HDX_FULLSCREENSHADER_H
Definition: vec4i.h:61
HDX_API void BindTextures(TfTokenVector const &names, HgiTextureHandleVector const &textures)
int HgiHandle< class HgiTexture > HgiTextureHandle
#define HDX_API
Definition: api.h:40
const GLuint * textures
Definition: glcorearb.h:416
std::vector< HgiTextureHandle > HgiTextureHandleVector
Definition: texture.h:207
Definition: token.h:87
Definition: core.h:760
HgiBlendFactor
Definition: enums.h:488
HDX_API void Draw(HgiTextureHandle const &colorDst, HgiTextureHandle const &depthDst)
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
HDX_API ~HdxFullscreenShader()
Destroy the fullscreen shader object, releasing GPU resources.
HgiBlendOp
Definition: enums.h:473
HDX_API void SetDepthState(HgiDepthStencilState const &state)
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
GLuint const GLuint * names
Definition: glew.h:2695
HDX_API void SetProgram(TfToken const &glslfx, TfToken const &shaderName, HgiShaderFunctionDesc &fragDesc, HgiShaderFunctionDesc vertDesc=GetFullScreenVertexDesc())
Definition: hgi.h:108
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1394
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)
Definition: format.h:895