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/imaging/garch/gl.h"
32 #include "pxr/base/gf/vec2i.h"
33 #include "pxr/imaging/hgi/buffer.h"
38 
40 
41 class Hgi;
42 
43 /// \class HdxFullscreenShader
44 ///
45 /// This class is a utility for rendering deep raytracer or aov output
46 /// (color/depth) to a hgi texture. This lets callers composite results
47 /// into existing scenes.
48 ///
50 public:
51  /// Create a new fullscreen shader object.
52  /// 'debugName' is assigned to the fullscreen pass as gpu debug group that
53  /// is helpful when inspecting the frame on a gpu debugger.
54  HDX_API
55  HdxFullscreenShader(Hgi* hgi, std::string const& debugName);
56 
57  /// Destroy the fullscreen shader object, releasing GPU resources.
58  HDX_API
60 
61  /// Set the program for the class to use for its fragment shader.
62  /// The vertex shader is always hdx/shaders/fullscreen.glslfx,
63  /// "FullScreenVertex", which draws a full-screen triangle.
64  /// The fragment shader should expect a varying called "uv", and
65  /// whatever textures or uniforms have been passed in by the caller.
66  /// \param glslfx The name of the glslfx file where the fragment shader
67  /// is located.
68  /// \param technique The technique name of the fragment shader.
69  HDX_API
70  void SetProgram(TfToken const& glslfx, TfToken const& technique);
71 
72  /// Bind a (externally managed) buffer to the shader program.
73  /// This function can be used to bind buffers to a custom shader program.
74  /// The lifetime of the buffer is managed by the caller. HdxFullscreenShader
75  /// does not take ownership. To update values in the buffer, the client can
76  /// use a blitCmds to copy new data into their buffer.
77  /// If an invalid 'buffer' is passed, the binding will be cleared.
78  HDX_API
79  void BindBuffer(HgiBufferHandle const& buffer, uint32_t bindingIndex);
80 
81  /// Bind (externally managed) textures to the shader program.
82  /// This function can be used to bind textures to a custom shader program.
83  /// The lifetime of textures is managed by the caller. HdxFullscreenShader
84  /// does not take ownership.
85  /// If an invalid 'texture' is passed, the binding will be cleared.
86  HDX_API
87  void BindTextures(
88  TfTokenVector const& names,
90 
91  /// By default HdxFullscreenShader creates a pipeline object that enables
92  /// depth testing and enables depth write if there is a depth texture.
93  /// This function allows you to override the depth and stencil state.
94  HDX_API
95  void SetDepthState(HgiDepthStencilState const& state);
96 
97  /// By default HdxFullscreenShader uses no blending (opaque).
98  /// This function allows you to override blend state (e.g. alpha blending)
99  HDX_API
100  void SetBlendState(
101  bool enableBlending,
102  HgiBlendFactor srcColorBlendFactor,
103  HgiBlendFactor dstColorBlendFactor,
104  HgiBlendOp colorBlendOp,
105  HgiBlendFactor srcAlphaBlendFactor,
106  HgiBlendFactor dstAlphaBlendFactor,
107  HgiBlendOp alphaBlendOp);
108 
109  /// Draw the internal textures to the provided destination textures.
110  /// `depth` is optional.
111  HDX_API
112  void Draw(HgiTextureHandle const& colorDst,
113  HgiTextureHandle const& depthDst);
114 
115 private:
116  HdxFullscreenShader() = delete;
117 
118  using TextureMap = std::map<TfToken, HgiTextureHandle>;
119  using BufferMap = std::map<uint32_t, HgiBufferHandle>;
120 
121  // Utility function to create buffer resources.
122  void _CreateBufferResources();
123 
124  // Destroy shader program and the shader functions it holds.
125  void _DestroyShaderProgram();
126 
127  // Utility to create resource bindings
128  bool _CreateResourceBindings(TextureMap const& textures);
129 
130  // Utility to create default vertex buffer descriptor
131  void _CreateVertexBufferDescriptor();
132 
133  // Utility to create a pipeline
134  bool _CreatePipeline(
135  HgiTextureHandle const& colorDst,
136  HgiTextureHandle const& depthDst,
137  bool depthWrite);
138 
139  // Internal draw method
140  void _Draw(TextureMap const& textures,
141  HgiTextureHandle const& colorDst,
142  HgiTextureHandle const& depthDst,
143  bool depthWrite);
144 
145  // Print shader compile errors.
146  void _PrintCompileErrors();
147 
148  class Hgi* _hgi;
149 
150  std::string _debugName;
151 
152  TextureMap _textures;
153  BufferMap _buffers;
154 
155  TfToken _glslfx;
156  TfToken _shaderName;
157 
158  HgiBufferHandle _indexBuffer;
159  HgiBufferHandle _vertexBuffer;
160  HgiShaderProgramHandle _shaderProgram;
161  HgiResourceBindingsHandle _resourceBindings;
162  HgiGraphicsPipelineHandle _pipeline;
163  HgiVertexBufferDesc _vboDesc;
164 
165  HgiDepthStencilState _depthState;
166 
167  bool _blendingEnabled;
168  HgiBlendFactor _srcColorBlendFactor;
169  HgiBlendFactor _dstColorBlendFactor;
170  HgiBlendOp _colorBlendOp;
171  HgiBlendFactor _srcAlphaBlendFactor;
172  HgiBlendFactor _dstAlphaBlendFactor;
173  HgiBlendOp _alphaBlendOp;
174 
175  HgiAttachmentDesc _attachment0;
176  HgiAttachmentDesc _depthAttachment;
177 };
178 
180 
181 #endif // PXR_IMAGING_HDX_FULLSCREENSHADER_H
HDX_API void BindTextures(TfTokenVector const &names, HgiTextureHandleVector const &textures)
#define HDX_API
Definition: api.h:40
std::vector< HgiTextureHandle > HgiTextureHandleVector
Definition: texture.h:163
GLsizei GLuint * textures
Definition: glew.h:4043
Definition: token.h:87
HgiBlendFactor
Definition: enums.h:420
GLuint buffer
Definition: glew.h:1680
HDX_API void Draw(HgiTextureHandle const &colorDst, HgiTextureHandle const &depthDst)
HDX_API ~HdxFullscreenShader()
Destroy the fullscreen shader object, releasing GPU resources.
GLuint const GLuint * names
Definition: glew.h:2690
HgiBlendOp
Definition: enums.h:405
HDX_API void SetDepthState(HgiDepthStencilState const &state)
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:446
Definition: hgi.h:106
GLsizei const GLchar *const * string
Definition: glew.h:1844
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
#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)
HDX_API void SetProgram(TfToken const &glslfx, TfToken const &technique)