Skip to content

Commit 1e536fe

Browse files
Hydrogent: keep all material data in a single buffer
1 parent 9e49e1a commit 1e536fe

File tree

6 files changed

+162
-163
lines changed

6 files changed

+162
-163
lines changed

Hydrogent/interface/HnMaterial.hpp

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class HnMaterial final : public pxr::HdMaterial
6262
{
6363
public:
6464
static HnMaterial* Create(const pxr::SdfPath& id);
65-
static HnMaterial* CreateFallback(HnTextureRegistry& TexRegistry, const USD_Renderer& UsdRenderer);
65+
static HnMaterial* CreateFallback(HnRenderDelegate& RenderDelegate);
6666

6767
~HnMaterial();
6868

@@ -78,8 +78,8 @@ class HnMaterial final : public pxr::HdMaterial
7878
/// Creates an SRB cache that should be passed to UpdateSRB().
7979
static RefCntAutoPtr<IObject> CreateSRBCache();
8080

81-
bool UpdateSRB(HnRenderDelegate& RendererDelegate);
82-
void BindMaterialAttribsBuffer(HnRenderDelegate& RendererDelegate);
81+
bool UpdateSRB(HnRenderDelegate& RenderDelegate);
82+
void BindMaterialAttribsBuffer(HnRenderDelegate& RenderDelegate);
8383

8484
IShaderResourceBinding* GetSRB() const { return m_SRB; }
8585
IShaderResourceBinding* GetSRB(Uint32 PrimitiveAttribsOffset) const
@@ -88,10 +88,11 @@ class HnMaterial final : public pxr::HdMaterial
8888
m_PrimitiveAttribsVar->SetBufferOffset(PrimitiveAttribsOffset);
8989
return m_SRB;
9090
}
91-
void SetMaterialAttribsBufferOffset(Uint32 Offset) const
91+
void ApplyMaterialAttribsBufferOffset() const
9292
{
9393
VERIFY_EXPR(m_MaterialAttribsVar != nullptr);
94-
m_MaterialAttribsVar->SetBufferOffset(Offset);
94+
VERIFY_EXPR(m_PBRMaterialAttribsBufferOffset != ~0u);
95+
m_MaterialAttribsVar->SetBufferOffset(m_PBRMaterialAttribsBufferOffset);
9596
}
9697
void SetJointsBufferOffset(Uint32 Offset) const
9798
{
@@ -132,7 +133,12 @@ class HnMaterial final : public pxr::HdMaterial
132133
}
133134

134135
Uint32 GetPBRPrimitiveAttribsBufferRange() const { return m_PBRPrimitiveAttribsBufferRange; }
135-
Uint32 GetPBRMaterailAttribsBufferRange() const { return m_PBRMaterialAttribsBufferRange; }
136+
Uint32 GetPBRMaterialAttribsSize() const { return m_PBRMaterialAttribsSize; }
137+
Uint32 GetPBRMaterialAttribsBufferOffset() const
138+
{
139+
VERIFY_EXPR(m_PBRMaterialAttribsBufferOffset != ~0u);
140+
return m_PBRMaterialAttribsBufferOffset;
141+
}
136142

137143
private:
138144
HnMaterial(pxr::SdfPath const& id);
@@ -142,15 +148,14 @@ class HnMaterial final : public pxr::HdMaterial
142148
// \remarks Sync() is not called on fallback material,
143149
// but we need to initialize default textures,
144150
// so we have to use this special constructor.
145-
HnMaterial(HnTextureRegistry& TexRegistry, const USD_Renderer& UsdRenderer);
151+
HnMaterial(HnRenderDelegate& RenderDelegate);
146152

147153
// A mapping from the texture name to the texture coordinate set index in m_TexCoords array (e.g. "diffuseColor" -> 0)
148154
// The same index is set in m_ShaderTextureAttribs[].UVSelector for the corresponding texture.
149155
// The name of the primvar that contains the texture coordinates is given by m_TexCoords[index].PrimVarName (e.g. "st0").
150156
using TexNameToCoordSetMapType = std::unordered_map<pxr::TfToken, size_t, pxr::TfToken::HashFunctor>;
151157
void AllocateTextures(const HnMaterialNetwork& Network,
152-
HnTextureRegistry& TexRegistry,
153-
const USD_Renderer& UsdRenderer);
158+
HnRenderDelegate& RenderDelegate);
154159
void InitTextureAttribs(const HnMaterialNetwork& Network,
155160
HnTextureRegistry& TexRegistry,
156161
const USD_Renderer& UsdRenderer,
@@ -161,6 +166,7 @@ class HnMaterial final : public pxr::HdMaterial
161166
HnTextureRegistry::TextureHandleSharedPtr GetDefaultTexture(HnTextureRegistry& TexRegistry, const pxr::TfToken& Name);
162167

163168
void ProcessMaterialNetwork(const HnMaterialNetwork& Network);
169+
void AllocateBufferSpace(HnRenderDelegate& RenderDelegate);
164170

165171
private:
166172
pxr::TfToken m_Tag;
@@ -175,6 +181,7 @@ class HnMaterial final : public pxr::HdMaterial
175181
GLTF::Material m_MaterialData;
176182

177183
std::atomic<bool> m_TextureAddressingAttribsDirty{false};
184+
std::atomic<bool> m_GPUDataDirty{true};
178185

179186
// The names of the primvars that contain unique texture coordinate sets for this material (e.g. "st0", "st1").
180187
// The index in this array for texture N is given by m_ShaderTextureAttribs[N].UVSelector.
@@ -184,11 +191,24 @@ class HnMaterial final : public pxr::HdMaterial
184191
Uint32 m_PBRPrimitiveAttribsBufferRange = 0;
185192

186193
// The range that is used to bind the cbMaterialAttribs buffer.
194+
// This is the maximum of all material attribs buffer sizes for
195+
// all materials that use the same SRB.
187196
Uint32 m_PBRMaterialAttribsBufferRange = 0;
188197

198+
PBR_Renderer::PSO_FLAGS m_PSOFlags = PBR_Renderer::PSO_FLAG_NONE;
199+
200+
// Material attribs shader data size in bytes.
201+
Uint32 m_PBRMaterialAttribsSize = 0;
202+
203+
// The offset in the cbMaterialAttribs buffer.
204+
Uint32 m_PBRMaterialAttribsBufferOffset = ~0u;
205+
189206
// Current texture storage version
190207
Uint32 m_TexRegistryStorageVersion = 0;
191208

209+
// Current material attribs buffer version
210+
Uint32 m_MaterialAttribsBufferVersion = ~0u;
211+
192212
ShaderTextureIndexingIdType m_ShaderTextureIndexingId = 0;
193213
};
194214

Hydrogent/interface/HnRenderDelegate.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,6 @@ class HnRenderDelegate final : public pxr::HdRenderDelegate
404404
IRenderStateCache* GetRenderStateCache() const { return m_pRenderStateCache; }
405405
IBuffer* GetFrameAttribsCB() const { return m_FrameAttribsCB; }
406406
IBuffer* GetPrimitiveAttribsCB() const { return m_PrimitiveAttribsCB; }
407-
IBuffer* GetMaterialAttribsCB() const { return m_MaterialAttribsCB; }
408407

409408
IShaderResourceBinding* GetMainPassFrameAttribsSRB() const { return m_MainPassFrameAttribsSRB; }
410409
IShaderResourceBinding* GetShadowPassFrameAttribsSRB(Uint32 LightId) const;
@@ -436,7 +435,6 @@ class HnRenderDelegate final : public pxr::HdRenderDelegate
436435

437436
RefCntAutoPtr<GLTF::ResourceManager> m_ResourceMgr;
438437
RefCntAutoPtr<IBuffer> m_PrimitiveAttribsCB;
439-
RefCntAutoPtr<IBuffer> m_MaterialAttribsCB;
440438
RefCntAutoPtr<IObject> m_MaterialSRBCache;
441439
std::shared_ptr<USD_Renderer> m_USDRenderer;
442440

Hydrogent/interface/HnRenderPass.hpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,6 @@ class HnRenderPass final : public pxr::HdRenderPass
177177
// in the material's SRB.
178178
Uint32 PrimitiveAttribsBufferRange = 0;
179179

180-
/// Offset in the material attribs buffer.
181-
Uint32 MaterialAttribsOffset = 0;
182-
183180
IBuffer* IndexBuffer = nullptr;
184181

185182
std::array<IBuffer*, VERTEX_BUFFER_SLOT_COUNT> VertexBuffers = {};
@@ -196,8 +193,7 @@ class HnRenderPass final : public pxr::HdRenderPass
196193
void UpdateDrawListGPUResources(RenderState& State);
197194
void UpdateDrawListItemGPUResources(DrawListItem& ListItem, RenderState& State, DRAW_LIST_ITEM_DIRTY_FLAGS DirtyFlags);
198195

199-
size_t UpdateMaterialAttribsBuffer(RenderState& State, size_t FirstItemIdx);
200-
void RenderPendingDrawItems(RenderState& State);
196+
void RenderPendingDrawItems(RenderState& State);
201197

202198
GraphicsPipelineDesc GetGraphicsDesc(const HnRenderPassState& RPState, bool UseStripTopology) const;
203199

@@ -219,7 +215,6 @@ class HnRenderPass final : public pxr::HdRenderPass
219215
{
220216
const DrawListItem& ListItem;
221217
const Uint32 PrimitiveAttribsOffset;
222-
const Uint32 MaterialAttribsOffset;
223218
Uint32 JointsBufferOffset = ~0u;
224219
Uint32 DrawCount = 1;
225220
};

0 commit comments

Comments
 (0)