HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
effectsShader.h
Go to the documentation of this file.
1 //
2 // Copyright 2023 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_HDX_EFFECTS_SHADER_H
8 #define PXR_IMAGING_HDX_EFFECTS_SHADER_H
9 
10 #include "pxr/pxr.h"
11 
12 #include "pxr/imaging/hdx/api.h"
13 
14 #include "pxr/imaging/hgi/buffer.h"
19 
20 #include "pxr/base/gf/vec4i.h"
21 
22 #include <vector>
23 
25 
26 class Hgi;
27 
28 using HgiGraphicsCmdsUniquePtr = std::unique_ptr<class HgiGraphicsCmds>;
29 
30 
31 /// \class HdxEffectsShader
32 ///
33 /// This class provides functionality to create and manage a single
34 /// HgiGraphicsPipeline instance and to issue draw calls to that instance.
35 ///
36 /// Sub-classes should define the actual interface for issuing the draw call
37 /// leveraging the common functionality this class provides to facilitate that.
38 ///
39 /// It is primarily intended to be used for full screen passes that perform a
40 /// sceen-space effect. As an example, the HdxFullscreenShader class inherits
41 /// from this class and makes use of the functions defined here to set up its
42 /// pipeline and issue draw commands.
43 ///
45 {
46 public:
47  HDX_API
48  virtual ~HdxEffectsShader();
49 
50  // Print shader compile errors for the shader function.
51  static void PrintCompileErrors(
52  const HgiShaderFunctionHandle& shaderFn);
53 
54  // Print shader compile errors for the shader program and any functions
55  // it references.
56  static void PrintCompileErrors(
57  const HgiShaderProgramHandle& shaderProgram);
58 
59 protected:
60  HdxEffectsShader() = delete;
61  HdxEffectsShader(const HdxEffectsShader&) = delete;
63 
64  /// Create a new shader object.
65  /// \param hgi Hgi instance to use to create any GPU resources.
66  /// \param debugName Name used to tag GPU resources to aid in debugging.
67  HDX_API
69  Hgi* hgi,
70  const std::string& debugName);
71 
72  HDX_API
74  const HgiAttachmentDescVector& colorAttachmentDescs);
75 
76  HDX_API
78  const HgiAttachmentDesc& depthAttachmentDesc);
79 
80  HDX_API
81  void _SetPrimitiveType(
82  HgiPrimitiveType primitiveType);
83 
84  HDX_API
85  void _SetShaderProgram(
86  const HgiShaderProgramHandle& shaderProgram);
87 
88  HDX_API
90  const HgiVertexBufferDescVector& vertexBufferDescs);
91 
92  HDX_API
94  const HgiDepthStencilState& depthStencilState);
95 
96  HDX_API
98  const HgiMultiSampleState& multiSampleState);
99 
100  HDX_API
102  const HgiRasterizationState& rasterizationState);
103 
104  HDX_API
105  void _SetShaderConstants(
106  uint32_t byteSize,
107  const void* data,
108  HgiShaderStage stageUsage);
109 
110  HDX_API
111  void _SetTextureBindings(
113 
114  HDX_API
115  void _SetBufferBindings(
117 
118  // Creates a graphics commands object, records draw commands to it via the
119  // overridden _RecordDrawCmds, and then submits them.
120  HDX_API
122  const HgiTextureHandleVector& colorTextures,
123  const HgiTextureHandleVector& colorResolveTextures,
124  const HgiTextureHandle& depthTexture,
125  const HgiTextureHandle& depthResolveTexture,
126  const GfVec4i& viewport);
127 
128  // Sub-classes should override and invoke one or more calls to
129  // _DrawNonIndexed or _DrawIndexed.
130  HDX_API
131  virtual void _RecordDrawCmds() = 0;
132 
133  // Sets the vertex buffer and invokes HgiGraphicsCmds::Draw.
134  HDX_API
135  void _DrawNonIndexed(
136  const HgiBufferHandle& vertexBuffer,
137  uint32_t vertexCount,
138  uint32_t baseVertex,
139  uint32_t instanceCount,
140  uint32_t baseInstance);
141 
142  // Sets the vertex buffer and invokes HgiGraphicsCmds::DrawIndexed with the
143  // provided index buffer.
144  HDX_API
145  void _DrawIndexed(
146  const HgiBufferHandle& vertexBuffer,
147  const HgiBufferHandle& indexBuffer,
148  uint32_t indexCount,
149  uint32_t indexBufferByteOffset,
150  uint32_t baseVertex,
151  uint32_t instanceCount,
152  uint32_t baseInstance);
153 
154  HDX_API
155  Hgi* _GetHgi() const;
156 
157  HDX_API
158  void _DestroyShaderProgram(HgiShaderProgramHandle* shaderProgram);
159 
160  HDX_API
161  const std::string& _GetDebugName() const;
162 
163 private:
164  void _CreatePipeline(
165  const HgiTextureHandleVector& colorTextures,
166  const HgiTextureHandleVector& colorResolveTextures,
167  const HgiTextureHandle& depthTexture,
168  const HgiTextureHandle& depthResolveTexture);
169  void _DestroyPipeline();
170 
171  void _CreateResourceBindings();
172  void _DestroyResourceBindings();
173 
174  Hgi* _hgi;
175  const std::string _debugName;
176  HgiGraphicsPipelineDesc _pipelineDesc;
177  HgiGraphicsPipelineHandle _pipeline;
178  std::vector<uint8_t> _constantsData;
179  HgiResourceBindingsDesc _resourceBindingsDesc;
180  HgiResourceBindingsHandle _resourceBindings;
181  HgiGraphicsCmdsUniquePtr _gfxCmds;
182 };
183 
185 
186 #endif // PXR_IMAGING_HDX_EFFECTS_SHADER_H
Definition: vec4i.h:43
std::vector< HgiTextureBindDesc > HgiTextureBindDescVector
virtual HDX_API ~HdxEffectsShader()
HDX_API const std::string & _GetDebugName() const
std::vector< HgiAttachmentDesc > HgiAttachmentDescVector
std::vector< HgiBufferBindDesc > HgiBufferBindDescVector
HDX_API void _DrawIndexed(const HgiBufferHandle &vertexBuffer, const HgiBufferHandle &indexBuffer, uint32_t indexCount, uint32_t indexBufferByteOffset, uint32_t baseVertex, uint32_t instanceCount, uint32_t baseInstance)
HdxEffectsShader()=delete
HDX_API void _SetTextureBindings(const HgiTextureBindDescVector &textures)
int HgiHandle< class HgiTexture > HgiTextureHandle
#define HDX_API
Definition: api.h:23
HDX_API void _SetRasterizationState(const HgiRasterizationState &rasterizationState)
std::vector< HgiTextureHandle > HgiTextureHandleVector
Definition: texture.h:196
std::vector< HgiVertexBufferDesc > HgiVertexBufferDescVector
HDX_API void _SetMultiSampleState(const HgiMultiSampleState &multiSampleState)
const GLuint * buffers
Definition: glcorearb.h:661
HdxEffectsShader & operator=(const HdxEffectsShader &)=delete
HgiPrimitiveType
Definition: enums.h:596
std::unique_ptr< class HgiGraphicsCmds > HgiGraphicsCmdsUniquePtr
Definition: effectsShader.h:28
HDX_API void _DestroyShaderProgram(HgiShaderProgramHandle *shaderProgram)
HDX_API void _SetBufferBindings(const HgiBufferBindDescVector &buffers)
HDX_API void _SetShaderProgram(const HgiShaderProgramHandle &shaderProgram)
Definition: hgi.h:93
static void PrintCompileErrors(const HgiShaderFunctionHandle &shaderFn)
HDX_API void _SetShaderConstants(uint32_t byteSize, const void *data, HgiShaderStage stageUsage)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
HDX_API void _DrawNonIndexed(const HgiBufferHandle &vertexBuffer, uint32_t vertexCount, uint32_t baseVertex, uint32_t instanceCount, uint32_t baseInstance)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
HgiBits HgiShaderStage
Definition: enums.h:357
const GLuint * textures
Definition: glcorearb.h:416
HDX_API void _SetColorAttachments(const HgiAttachmentDescVector &colorAttachmentDescs)
virtual HDX_API void _RecordDrawCmds()=0
Definition: format.h:1821
HDX_API void _SetPrimitiveType(HgiPrimitiveType primitiveType)
HDX_API void _SetVertexBufferDescs(const HgiVertexBufferDescVector &vertexBufferDescs)
HDX_API void _CreateAndSubmitGraphicsCmds(const HgiTextureHandleVector &colorTextures, const HgiTextureHandleVector &colorResolveTextures, const HgiTextureHandle &depthTexture, const HgiTextureHandle &depthResolveTexture, const GfVec4i &viewport)
HDX_API Hgi * _GetHgi() const
HDX_API void _SetDepthStencilState(const HgiDepthStencilState &depthStencilState)
HDX_API void _SetDepthAttachment(const HgiAttachmentDesc &depthAttachmentDesc)