1 //
2 // Copyright 2019 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_RENDER_BUFFER_H
25 #define PXR_IMAGING_HD_ST_RENDER_BUFFER_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/base/gf/vec3i.h"
29 #include "pxr/imaging/hdSt/api.h"
30 #include "pxr/imaging/hd/renderBuffer.h"
31 #include "pxr/imaging/hgi/hgi.h"
32 #include "pxr/imaging/hgi/enums.h"
33 #include "pxr/imaging/hgi/texture.h"
34 
35 PXR_NAMESPACE_OPEN_SCOPE
36 
37 class HdStResourceRegistry;
38 class HdStTextureIdentifier;
39 using HdStDynamicUvTextureObjectSharedPtr =
40     std::shared_ptr<class HdStDynamicUvTextureObject>;
41 
42 class HdStRenderBuffer : public HdRenderBuffer
43 {
44 public:
45     HDST_API
46     HdStRenderBuffer(HdStResourceRegistry *resourceRegistry, SdfPath const& id);
47 
48     HDST_API
49     ~HdStRenderBuffer() override;
50 
51     HDST_API
52     void Sync(HdSceneDelegate *sceneDelegate,
53               HdRenderParam *renderParam,
54               HdDirtyBits *dirtyBits) override;
55 
56     HDST_API
57     bool Allocate(GfVec3i const& dimensions,
58                   HdFormat format,
59                   bool multiSampled) override;
60 
61     HDST_API
62     unsigned int GetWidth() const override;
63 
64     HDST_API
65     unsigned int GetHeight() const override;
66 
67     HDST_API
68     unsigned int GetDepth() const override;
69 
70     HDST_API
GetFormat()71     HdFormat GetFormat() const override {return _format;}
72 
73     HDST_API
74     bool IsMultiSampled() const override;
75 
76     /// Map the buffer for reading. The control flow should be Map(),
77     /// before any I/O, followed by memory access, followed by Unmap() when
78     /// done.
79     ///   \return The address of the buffer.
80     HDST_API
81     void* Map() override;
82 
83     /// Unmap the buffer.
84     HDST_API
85     void Unmap() override;
86 
87     /// Return whether any clients have this buffer mapped currently.
88     ///   \return True if the buffer is currently mapped by someone.
89     HDST_API
IsMapped()90     bool IsMapped() const override {
91         return _mappers.load() != 0;
92     }
93 
94     /// Is the buffer converged?
95     ///   \return True if the buffer is converged (not currently being
96     ///           rendered to).
97     HDST_API
IsConverged()98     bool IsConverged() const override {
99         return true;
100     }
101 
102     /// Resolve the sample buffer into final values.
103     HDST_API
104     void Resolve() override;
105 
106     /// Returns the texture handle.
107     HDST_API
108     VtValue GetResource(bool multiSampled) const override;
109 
110     /// The identifier that can be passed to, e.g.,
111     /// HdStResourceRegistry::AllocateTextureHandle so that a
112     /// shader can bind this buffer as texture.
113     HDST_API
114     HdStTextureIdentifier GetTextureIdentifier(
115         bool multiSampled);
116 
117 protected:
118     void _Deallocate() override;
119 
120 private:
121     // HdRenderBuffer::Allocate should take a scene delegate or
122     // resource registry so that we do not need to save it here.
123     HdStResourceRegistry * _resourceRegistry;
124 
125     // Format saved here (somewhat redundantely) since the
126     // Hgi texture descriptor holds an HgiFormat instead of HdFormat.
127     HdFormat _format;
128 
129     uint32_t _msaaSampleCount;
130 
131     // The GPU texture resource
132     HdStDynamicUvTextureObjectSharedPtr _textureObject;
133 
134     // The GPU multi-sample texture resource (optional)
135     HdStDynamicUvTextureObjectSharedPtr _textureMSAAObject;
136 
137     // The number of callers mapping this buffer.
138     std::atomic<int> _mappers;
139     // Texels are temp captured into this buffer between map and unmap.
140     std::vector<uint8_t> _mappedBuffer;
141 };
142 
143 PXR_NAMESPACE_CLOSE_SCOPE
144 
145 #endif
146