All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
drawBatch.h
1 //
2 // Copyright 2016 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_HD_ST_DRAW_BATCH_H
25 #define PXR_IMAGING_HD_ST_DRAW_BATCH_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hdSt/api.h"
29 #include "pxr/imaging/hd/version.h"
30 
31 #include "pxr/imaging/hdSt/resourceBinder.h"
32 #include "pxr/imaging/hd/repr.h"
33 #include "pxr/imaging/hdSt/shaderCode.h"
34 
35 #include <memory>
36 #include <vector>
37 
38 PXR_NAMESPACE_OPEN_SCOPE
39 
40 class HdStDrawItem;
42 
43 using HdSt_DrawBatchSharedPtr = std::shared_ptr<class HdSt_DrawBatch>;
44 using HdSt_DrawBatchSharedPtrVector = std::vector<HdSt_DrawBatchSharedPtr>;
45 using HdSt_GeometricShaderSharedPtr =
46  std::shared_ptr<class HdSt_GeometricShader>;
47 using HdStGLSLProgramSharedPtr= std::shared_ptr<class HdStGLSLProgram>;
48 
49 using HdStRenderPassStateSharedPtr = std::shared_ptr<class HdStRenderPassState>;
50 using HdStResourceRegistrySharedPtr =
51  std::shared_ptr<class HdStResourceRegistry>;
52 
61 class HdSt_DrawBatch
62 {
63 public:
64  HDST_API
65  HdSt_DrawBatch(HdStDrawItemInstance * drawItemInstance);
66 
67  HDST_API
68  virtual ~HdSt_DrawBatch();
69 
73  HDST_API
74  bool Append(HdStDrawItemInstance * drawItemInstance);
75 
78  HDST_API
79  bool Rebuild();
80 
83  virtual bool Validate(bool deepValidation) = 0;
84 
86  virtual void PrepareDraw(
87  HdStRenderPassStateSharedPtr const &renderPassState,
88  HdStResourceRegistrySharedPtr const &resourceRegistry) = 0;
89 
91  virtual void ExecuteDraw(
92  HdStRenderPassStateSharedPtr const &renderPassState,
93  HdStResourceRegistrySharedPtr const & resourceRegistry) = 0;
94 
98  HDST_API
99  virtual void DrawItemInstanceChanged(HdStDrawItemInstance const* instance);
100 
102  HDST_API
103  virtual void SetEnableTinyPrimCulling(bool tinyPrimCulling);
104 
105 protected:
106  HDST_API
107  virtual void _Init(HdStDrawItemInstance * drawItemInstance);
108 
114  class _DrawingProgram {
115  public:
116  _DrawingProgram() {}
117 
118  HDST_API
119  bool CompileShader(
120  HdStDrawItem const *drawItem,
121  bool indirect,
122  HdStResourceRegistrySharedPtr const &resourceRegistry);
123 
124  HdStGLSLProgramSharedPtr GetGLSLProgram() const {
125  return _glslProgram;
126  }
127 
130  const HdSt_ResourceBinder &GetBinder() const {
131  return _resourceBinder;
132  }
133 
134  void Reset() {
135  _glslProgram.reset();
136  _surfaceShader.reset();
137  _geometricShader.reset();
138  _resourceBinder = HdSt_ResourceBinder();
139  _shaders.clear();
140  }
141 
142  void SetSurfaceShader(HdStShaderCodeSharedPtr shader) {
143  _surfaceShader = shader;
144  }
145 
146  const HdStShaderCodeSharedPtr &GetSurfaceShader() {
147  return _surfaceShader;
148  }
149 
150  void SetGeometricShader(HdSt_GeometricShaderSharedPtr shader) {
151  _geometricShader = shader;
152  }
153 
154  const HdSt_GeometricShaderSharedPtr &GetGeometricShader() {
155  return _geometricShader;
156  }
157 
160  void SetShaders(HdStShaderCodeSharedPtrVector shaders) {
161  _shaders = shaders;
162  }
163 
166  const HdStShaderCodeSharedPtrVector &GetShaders() const {
167  return _shaders;
168  }
169 
172  HdStShaderCodeSharedPtrVector GetComposedShaders() const {
173  HdStShaderCodeSharedPtrVector shaders = _shaders;
174  if (_surfaceShader) {
175  shaders.push_back(_surfaceShader);
176  }
177  return shaders;
178  }
179 
180  protected:
181  // overrides populate customBindings and enableInstanceDraw which
182  // will be used to determine if glVertexAttribDivisor needs to be
183  // enabled or not.
184  HDST_API
185  virtual void _GetCustomBindings(
186  HdBindingRequestVector *customBindings,
187  bool *enableInstanceDraw) const;
188 
189  HDST_API
190  virtual bool _Link(HdStGLSLProgramSharedPtr const & glslProgram);
191 
192  private:
193  HdStGLSLProgramSharedPtr _glslProgram;
194  HdSt_ResourceBinder _resourceBinder;
195  HdStShaderCodeSharedPtrVector _shaders;
196  HdSt_GeometricShaderSharedPtr _geometricShader;
197  HdStShaderCodeSharedPtr _surfaceShader;
198  };
199 
200  HDST_API
201  _DrawingProgram & _GetDrawingProgram(
202  HdStRenderPassStateSharedPtr const &state,
203  bool indirect,
204  HdStResourceRegistrySharedPtr const &resourceRegistry);
205 
206 protected:
207  HDST_API
208  static bool _IsAggregated(HdStDrawItem const *drawItem0,
209  HdStDrawItem const *drawItem1);
210 
211  std::vector<HdStDrawItemInstance const*> _drawItemInstances;
212 
213 private:
214  _DrawingProgram _program;
215  HdStShaderCode::ID _shaderHash;
216 };
217 
218 
219 PXR_NAMESPACE_CLOSE_SCOPE
220 
221 #endif // PXR_IMAGING_HD_ST_DRAW_BATCH_H
A container to store instance state for a drawitem.