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 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_EFFECTS_SHADER_H
25 #define PXR_IMAGING_HDX_EFFECTS_SHADER_H
26 
27 #include "pxr/pxr.h"
28 
29 #include "pxr/imaging/hdx/api.h"
30 
31 #include "pxr/imaging/hgi/buffer.h"
36 
37 #include "pxr/base/gf/vec4i.h"
38 
39 #include <vector>
40 
42 
43 class Hgi;
44 
45 using HgiGraphicsCmdsUniquePtr = std::unique_ptr<class HgiGraphicsCmds>;
46 
47 
48 /// \class HdxEffectsShader
49 ///
50 /// This class provides functionality to create and manage a single
51 /// HgiGraphicsPipeline instance and to issue draw calls to that instance.
52 ///
53 /// Sub-classes should define the actual interface for issuing the draw call
54 /// leveraging the common functionality this class provides to facilitate that.
55 ///
56 /// It is primarily intended to be used for full screen passes that perform a
57 /// sceen-space effect. As an example, the HdxFullscreenShader class inherits
58 /// from this class and makes use of the functions defined here to set up its
59 /// pipeline and issue draw commands.
60 ///
62 {
63 public:
64  HDX_API
65  virtual ~HdxEffectsShader();
66 
67  // Print shader compile errors for the shader function.
68  static void PrintCompileErrors(
69  const HgiShaderFunctionHandle& shaderFn);
70 
71  // Print shader compile errors for the shader program and any functions
72  // it references.
73  static void PrintCompileErrors(
74  const HgiShaderProgramHandle& shaderProgram);
75 
76 protected:
77  HdxEffectsShader() = delete;
78  HdxEffectsShader(const HdxEffectsShader&) = delete;
80 
81  /// Create a new shader object.
82  /// \param hgi Hgi instance to use to create any GPU resources.
83  /// \param debugName Name used to tag GPU resources to aid in debugging.
84  HDX_API
86  Hgi* hgi,
87  const std::string& debugName);
88 
89  HDX_API
91  const HgiAttachmentDescVector& colorAttachmentDescs);
92 
93  HDX_API
95  const HgiAttachmentDesc& depthAttachmentDesc);
96 
97  HDX_API
98  void _SetPrimitiveType(
99  HgiPrimitiveType primitiveType);
100 
101  HDX_API
102  void _SetShaderProgram(
103  const HgiShaderProgramHandle& shaderProgram);
104 
105  HDX_API
107  const HgiVertexBufferDescVector& vertexBufferDescs);
108 
109  HDX_API
111  const HgiDepthStencilState& depthStencilState);
112 
113  HDX_API
115  const HgiMultiSampleState& multiSampleState);
116 
117  HDX_API
119  const HgiRasterizationState& rasterizationState);
120 
121  HDX_API
122  void _SetShaderConstants(
123  uint32_t byteSize,
124  const void* data,
125  HgiShaderStage stageUsage);
126 
127  HDX_API
128  void _SetTextureBindings(
130 
131  HDX_API
132  void _SetBufferBindings(
134 
135  // Creates a graphics commands object, records draw commands to it via the
136  // overridden _RecordDrawCmds, and then submits them.
137  HDX_API
139  const HgiTextureHandleVector& colorTextures,
140  const HgiTextureHandleVector& colorResolveTextures,
141  const HgiTextureHandle& depthTexture,
142  const HgiTextureHandle& depthResolveTexture,
143  const GfVec4i& viewport);
144 
145  // Sub-classes should override and invoke one or more calls to
146  // _DrawNonIndexed or _DrawIndexed.
147  HDX_API
148  virtual void _RecordDrawCmds() = 0;
149 
150  // Sets the vertex buffer and invokes HgiGraphicsCmds::Draw.
151  HDX_API
152  void _DrawNonIndexed(
153  const HgiBufferHandle& vertexBuffer,
154  uint32_t vertexCount,
155  uint32_t baseVertex,
156  uint32_t instanceCount,
157  uint32_t baseInstance);
158 
159  // Sets the vertex buffer and invokes HgiGraphicsCmds::DrawIndexed with the
160  // provided index buffer.
161  HDX_API
162  void _DrawIndexed(
163  const HgiBufferHandle& vertexBuffer,
164  const HgiBufferHandle& indexBuffer,
165  uint32_t indexCount,
166  uint32_t indexBufferByteOffset,
167  uint32_t baseVertex,
168  uint32_t instanceCount,
169  uint32_t baseInstance);
170 
171  HDX_API
172  Hgi* _GetHgi() const;
173 
174  HDX_API
175  void _DestroyShaderProgram(HgiShaderProgramHandle* shaderProgram);
176 
177  HDX_API
178  const std::string& _GetDebugName() const;
179 
180 private:
181  void _CreatePipeline(
182  const HgiTextureHandleVector& colorTextures,
183  const HgiTextureHandleVector& colorResolveTextures,
184  const HgiTextureHandle& depthTexture,
185  const HgiTextureHandle& depthResolveTexture);
186  void _DestroyPipeline();
187 
188  void _CreateResourceBindings();
189  void _DestroyResourceBindings();
190 
191  Hgi* _hgi;
192  const std::string _debugName;
193  HgiGraphicsPipelineDesc _pipelineDesc;
194  HgiGraphicsPipelineHandle _pipeline;
195  std::vector<uint8_t> _constantsData;
196  HgiResourceBindingsDesc _resourceBindingsDesc;
197  HgiResourceBindingsHandle _resourceBindings;
198  HgiGraphicsCmdsUniquePtr _gfxCmds;
199 };
200 
202 
203 #endif // PXR_IMAGING_HDX_EFFECTS_SHADER_H
Definition: vec4i.h:60
std::vector< HgiTextureBindDesc > HgiTextureBindDescVector
virtual HDX_API ~HdxEffectsShader()
HDX_API const std::string & _GetDebugName() const
std::vector< HgiAttachmentDesc > HgiAttachmentDescVector
std::vector< HgiBufferBindDesc > HgiBufferBindDescVector
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
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:40
HDX_API void _SetRasterizationState(const HgiRasterizationState &rasterizationState)
std::vector< HgiTextureHandle > HgiTextureHandleVector
Definition: texture.h:213
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:610
std::unique_ptr< class HgiGraphicsCmds > HgiGraphicsCmdsUniquePtr
Definition: effectsShader.h:45
HDX_API void _DestroyShaderProgram(HgiShaderProgramHandle *shaderProgram)
HDX_API void _SetBufferBindings(const HgiBufferBindDescVector &buffers)
HDX_API void _SetShaderProgram(const HgiShaderProgramHandle &shaderProgram)
Definition: hgi.h:110
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:1432
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:91
HgiBits HgiShaderStage
Definition: enums.h:371
const GLuint * textures
Definition: glcorearb.h:416
HDX_API void _SetColorAttachments(const HgiAttachmentDescVector &colorAttachmentDescs)
virtual HDX_API void _RecordDrawCmds()=0
Definition: format.h:895
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)