All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fullscreenShader.h
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/vec2i.h"
32 #include "pxr/imaging/hgi/buffer.h"
33 #include "pxr/imaging/hgi/graphicsPipeline.h"
34 #include "pxr/imaging/hgi/resourceBindings.h"
35 #include "pxr/imaging/hgi/shaderProgram.h"
36 #include "pxr/imaging/hgi/texture.h"
37 
38 #include <vector>
39 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
42 class Hgi;
43 
51 {
52 public:
56  HDX_API
57  HdxFullscreenShader(Hgi* hgi, std::string const& debugName);
58 
60  HDX_API
62 
73  HDX_API
74  void SetProgram(
75  TfToken const& glslfx,
76  TfToken const& shaderName,
77  HgiShaderFunctionDesc &fragDesc,
78  HgiShaderFunctionDesc vertDesc = GetFullScreenVertexDesc()
79  );
80 
87  HDX_API
88  void BindBuffer(HgiBufferHandle const& buffer, uint32_t bindingIndex);
89 
95  HDX_API
96  void BindTextures(
97  TfTokenVector const& names,
98  HgiTextureHandleVector const& textures);
99 
103  HDX_API
104  void SetDepthState(HgiDepthStencilState const& state);
105 
108  HDX_API
109  void SetBlendState(
110  bool enableBlending,
111  HgiBlendFactor srcColorBlendFactor,
112  HgiBlendFactor dstColorBlendFactor,
113  HgiBlendOp colorBlendOp,
114  HgiBlendFactor srcAlphaBlendFactor,
115  HgiBlendFactor dstAlphaBlendFactor,
116  HgiBlendOp alphaBlendOp);
117 
121  HDX_API
122  void SetShaderConstants(
123  uint32_t byteSize,
124  const void* data);
125 
128  HDX_API
129  void Draw(HgiTextureHandle const& colorDst,
130  HgiTextureHandle const& depthDst);
131 
132 private:
133  HdxFullscreenShader() = delete;
134 
135  using TextureMap = std::map<TfToken, HgiTextureHandle>;
136  using BufferMap = std::map<uint32_t, HgiBufferHandle>;
137 
138  // Utility function to create buffer resources.
139  void _CreateBufferResources();
140 
141  // Destroy shader program and the shader functions it holds.
142  void _DestroyShaderProgram();
143 
144  // Utility to create resource bindings
145  bool _CreateResourceBindings(TextureMap const& textures);
146 
147  // Utility to create default vertex buffer descriptor
148  void _CreateVertexBufferDescriptor();
149 
150  // Utility to create a pipeline
151  bool _CreatePipeline(
152  HgiTextureHandle const& colorDst,
153  HgiTextureHandle const& depthDst,
154  bool depthWrite);
155 
156  // Utility to create a texture sampler
157  bool _CreateSampler();
158 
159  // Internal draw method
160  void _Draw(TextureMap const& textures,
161  HgiTextureHandle const& colorDst,
162  HgiTextureHandle const& depthDst,
163  bool depthWrite);
164 
165  static HgiShaderFunctionDesc GetFullScreenVertexDesc();
166 
167  // Print shader compile errors.
168  void _PrintCompileErrors();
169 
170  class Hgi* _hgi;
171 
172  std::string _debugName;
173 
174  TextureMap _textures;
175  BufferMap _buffers;
176 
177  TfToken _glslfx;
178  TfToken _shaderName;
179 
180  HgiBufferHandle _indexBuffer;
181  HgiBufferHandle _vertexBuffer;
182  HgiShaderProgramHandle _shaderProgram;
183  HgiResourceBindingsHandle _resourceBindings;
184  HgiGraphicsPipelineHandle _pipeline;
185  HgiSamplerHandle _sampler;
186  HgiVertexBufferDesc _vboDesc;
187 
188  HgiDepthStencilState _depthState;
189 
190  bool _blendingEnabled;
191  HgiBlendFactor _srcColorBlendFactor;
192  HgiBlendFactor _dstColorBlendFactor;
193  HgiBlendOp _colorBlendOp;
194  HgiBlendFactor _srcAlphaBlendFactor;
195  HgiBlendFactor _dstAlphaBlendFactor;
196  HgiBlendOp _alphaBlendOp;
197 
198  HgiAttachmentDesc _attachment0;
199  HgiAttachmentDesc _depthAttachment;
200 
201  std::vector<uint8_t> _constantsData;
202 };
203 
204 PXR_NAMESPACE_CLOSE_SCOPE
205 
206 #endif // PXR_IMAGING_HDX_FULLSCREENSHADER_H
HDX_API void Draw(HgiTextureHandle const &colorDst, HgiTextureHandle const &depthDst)
Draw the internal textures to the provided destination textures.
Describes the properties of a framebuffer attachment.
HDX_API ~HdxFullscreenShader()
Destroy the fullscreen shader object, releasing GPU resources.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
HDX_API void BindTextures(TfTokenVector const &names, HgiTextureHandleVector const &textures)
Bind (externally managed) textures to the shader program.
HDX_API void BindBuffer(HgiBufferHandle const &buffer, uint32_t bindingIndex)
Bind a (externally managed) buffer to the shader program.
Hydra Graphics Interface.
Definition: hgi.h:106
HDX_API void SetShaderConstants(uint32_t byteSize, const void *data)
Provide the shader constant values (uniforms).
This class is a utility for rendering deep raytracer or aov output (color/depth) to a hgi texture...
HDX_API void SetDepthState(HgiDepthStencilState const &state)
By default HdxFullscreenShader creates a pipeline object that enables depth testing and enables depth...
Properties to configure depth and stencil test.
Describes the attributes of a vertex buffer.
Describes the properties needed to create a GPU shader function.
HDX_API void SetProgram(TfToken const &glslfx, TfToken const &shaderName, HgiShaderFunctionDesc &fragDesc, HgiShaderFunctionDesc vertDesc=GetFullScreenVertexDesc())
Set the program for the class to use for its fragment shader.
HDX_API void SetBlendState(bool enableBlending, HgiBlendFactor srcColorBlendFactor, HgiBlendFactor dstColorBlendFactor, HgiBlendOp colorBlendOp, HgiBlendFactor srcAlphaBlendFactor, HgiBlendFactor dstAlphaBlendFactor, HgiBlendOp alphaBlendOp)
By default HdxFullscreenShader uses no blending (opaque).