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/vec4i.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 <map>
39 #include <vector>
40 
41 PXR_NAMESPACE_OPEN_SCOPE
42 
43 class Hgi;
44 
52 {
53 public:
57  HDX_API
58  HdxFullscreenShader(Hgi* hgi, std::string const& debugName);
59 
61  HDX_API
63 
74  HDX_API
75  void SetProgram(
76  TfToken const& glslfx,
77  TfToken const& shaderName,
78  HgiShaderFunctionDesc &fragDesc,
79  HgiShaderFunctionDesc vertDesc = GetFullScreenVertexDesc()
80  );
81 
88  HDX_API
89  void BindBuffer(HgiBufferHandle const& buffer, uint32_t bindingIndex);
90 
96  HDX_API
97  void BindTextures(
98  TfTokenVector const& names,
99  HgiTextureHandleVector const& textures);
100 
104  HDX_API
105  void SetDepthState(HgiDepthStencilState const& state);
106 
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 
121  HDX_API
123  HgiAttachmentLoadOp attachmentLoadOp,
124  HgiAttachmentStoreOp attachmentStoreOp);
125 
129  HDX_API
130  void SetShaderConstants(
131  uint32_t byteSize,
132  const void* data);
133 
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 
226 PXR_NAMESPACE_CLOSE_SCOPE
227 
228 #endif // PXR_IMAGING_HDX_FULLSCREENSHADER_H
Basic type for a vector of 4 int components.
Definition: vec4i.h:61
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:110
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 SetAttachmentLoadStoreOp(HgiAttachmentLoadOp attachmentLoadOp, HgiAttachmentStoreOp attachmentStoreOp)
By default HdxFullscreenShader uses LoadOpDontCare and StoreOpStore.
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).