All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
indirectDrawBatch.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 HDST_INDIRECT_DRAW_BATCH_H
25 #define HDST_INDIRECT_DRAW_BATCH_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hd/version.h"
29 #include "pxr/imaging/hdSt/api.h"
30 #include "pxr/imaging/hdSt/dispatchBuffer.h"
31 #include "pxr/imaging/hdSt/drawBatch.h"
32 #include "pxr/imaging/hdSt/persistentBuffer.h"
33 
34 #include <vector>
35 
36 PXR_NAMESPACE_OPEN_SCOPE
37 
38 
39 typedef std::vector<HdBindingRequest> HdBindingRequestVector;
40 
49 class HdSt_IndirectDrawBatch : public HdSt_DrawBatch {
50 public:
51  HDST_API
52  HdSt_IndirectDrawBatch(HdStDrawItemInstance * drawItemInstance);
53  HDST_API
54  ~HdSt_IndirectDrawBatch();
55 
56  // HdSt_DrawBatch overrides
57  HDST_API
58  virtual bool Validate(bool deepValidation) override;
59 
61  HDST_API
62  virtual void PrepareDraw(
63  HdStRenderPassStateSharedPtr const &renderPassState,
64  HdStResourceRegistrySharedPtr const &resourceRegistry) override;
65 
67  HDST_API
68  virtual void ExecuteDraw(
69  HdStRenderPassStateSharedPtr const &renderPassState,
70  HdStResourceRegistrySharedPtr const &resourceRegistry) override;
71 
72  HDST_API
73  virtual void DrawItemInstanceChanged(
74  HdStDrawItemInstance const* instance) override;
75 
76  HDST_API
77  virtual void SetEnableTinyPrimCulling(bool tinyPrimCulling) override;
78 
80  HDST_API
81  static bool IsEnabledGPUFrustumCulling();
82 
85  HDST_API
86  static bool IsEnabledGPUCountVisibleInstances();
87 
89  HDST_API
90  static bool IsEnabledGPUInstanceFrustumCulling();
91 
92 protected:
93  HDST_API
94  virtual void _Init(HdStDrawItemInstance * drawItemInstance) override;
95 
96 private:
97  void _ValidateCompatibility(
98  HdStBufferArrayRangeGLSharedPtr const& constantBar,
99  HdStBufferArrayRangeGLSharedPtr const& indexBar,
100  HdStBufferArrayRangeGLSharedPtr const& topologyVisibilityBar,
101  HdStBufferArrayRangeGLSharedPtr const& elementBar,
102  HdStBufferArrayRangeGLSharedPtr const& fvarBar,
103  HdStBufferArrayRangeGLSharedPtr const& vertexBar,
104  int instancerNumLevels,
105  HdStBufferArrayRangeGLSharedPtr const& instanceIndexBar,
106  std::vector<HdStBufferArrayRangeGLSharedPtr> const& instanceBars) const;
107 
108  // Culling requires custom resource binding.
109  class _CullingProgram : public _DrawingProgram {
110  public:
111  _CullingProgram()
112  : _useDrawArrays(false)
113  , _useInstanceCulling(false)
114  , _bufferArrayHash(0) { }
115  void Initialize(bool useDrawArrays, bool useInstanceCulling,
116  size_t bufferArrayHash);
117  protected:
118  // _DrawingProgram overrides
119  virtual void _GetCustomBindings(
120  HdBindingRequestVector *customBindings,
121  bool *enableInstanceDraw) const;
122  virtual bool _Link(HdStGLSLProgramSharedPtr const & glslProgram);
123  private:
124  bool _useDrawArrays;
125  bool _useInstanceCulling;
126  size_t _bufferArrayHash;
127  };
128 
129  _CullingProgram &_GetCullingProgram(
130  HdStResourceRegistrySharedPtr const &resourceRegistry);
131 
132  void _CompileBatch(HdStResourceRegistrySharedPtr const &resourceRegistry);
133 
134  void _GPUFrustumCulling(HdStDrawItem const *item,
135  HdStRenderPassStateSharedPtr const &renderPassState,
136  HdStResourceRegistrySharedPtr const &resourceRegistry);
137 
138  void _GPUFrustumCullingXFB(HdStDrawItem const *item,
139  HdStRenderPassStateSharedPtr const &renderPassState,
140  HdStResourceRegistrySharedPtr const &resourceRegistry);
141 
142  void _BeginGPUCountVisibleInstances(
143  HdStResourceRegistrySharedPtr const &resourceRegistry);
144 
145  // GLsync is not defined in gl.h. It's defined in spec as an opaque pointer:
146  typedef struct __GLsync *GLsync;
147  void _EndGPUCountVisibleInstances(GLsync resultSync, size_t * result);
148 
149  HdStDispatchBufferSharedPtr _dispatchBuffer;
150  HdStDispatchBufferSharedPtr _dispatchBufferCullInput;
151 
152  std::vector<GLuint> _drawCommandBuffer;
153  bool _drawCommandBufferDirty;
154  size_t _bufferArraysHash;
155 
156  HdStPersistentBufferSharedPtr _resultBuffer;
157 
158  size_t _numVisibleItems;
159  size_t _numTotalVertices;
160  size_t _numTotalElements;
161 
162  _CullingProgram _cullingProgram;
163  bool _useTinyPrimCulling;
164  bool _dirtyCullingProgram;
165 
166  bool _useDrawArrays;
167  bool _useInstancing;
168  bool _useGpuCulling;
169  bool _useGpuInstanceCulling;
170 
171  int _instanceCountOffset;
172  int _cullInstanceCountOffset;
173 
174  // We'll use this fence to signal when GPU frustum culling is
175  // complete if we need to read back result data from the GPU.
176  GLsync _cullResultSync;
177 };
178 
179 
180 PXR_NAMESPACE_CLOSE_SCOPE
181 
182 #endif // HDST_INDIRECT_DRAW_BATCH_H
A container to store instance state for a drawitem.