Refactor renderer draw extraction to section-level items
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include "Components/MeshFilterComponent.h"
|
||||
#include "Components/MeshRendererComponent.h"
|
||||
#include "RHI/RHICommandList.h"
|
||||
#include "Rendering/RenderMaterialUtility.h"
|
||||
#include "Rendering/RenderSurface.h"
|
||||
#include "Resources/Material/Material.h"
|
||||
#include "Resources/Texture/Texture.h"
|
||||
@@ -250,8 +251,12 @@ bool BuiltinForwardPipeline::Render(
|
||||
commandList->SetPipelineState(m_pipelineState);
|
||||
commandList->SetPrimitiveTopology(RHI::PrimitiveTopology::TriangleList);
|
||||
|
||||
for (const VisibleRenderObject& visibleObject : sceneData.visibleObjects) {
|
||||
DrawVisibleObject(context, sceneData, visibleObject);
|
||||
for (const VisibleRenderItem& visibleItem : sceneData.visibleItems) {
|
||||
if (!MatchesBuiltinPass(ResolveMaterial(visibleItem), BuiltinMaterialPass::Forward)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
DrawVisibleItem(context, sceneData, visibleItem);
|
||||
}
|
||||
|
||||
if (surface.IsAutoTransitionEnabled()) {
|
||||
@@ -506,42 +511,13 @@ void BuiltinForwardPipeline::DestroyPipelineResources() {
|
||||
m_initialized = false;
|
||||
}
|
||||
|
||||
const Resources::Material* BuiltinForwardPipeline::ResolveMaterial(
|
||||
const VisibleRenderObject& visibleObject,
|
||||
uint32_t materialIndex) const {
|
||||
if (visibleObject.meshRenderer != nullptr && materialIndex < visibleObject.meshRenderer->GetMaterialCount()) {
|
||||
if (const Resources::Material* material = visibleObject.meshRenderer->GetMaterial(materialIndex)) {
|
||||
return material;
|
||||
}
|
||||
}
|
||||
|
||||
if (visibleObject.mesh != nullptr && materialIndex < visibleObject.mesh->GetMaterials().Size()) {
|
||||
if (const Resources::Material* material = visibleObject.mesh->GetMaterials()[materialIndex]) {
|
||||
return material;
|
||||
}
|
||||
}
|
||||
|
||||
if (visibleObject.meshRenderer != nullptr && visibleObject.meshRenderer->GetMaterialCount() > 0) {
|
||||
if (const Resources::Material* material = visibleObject.meshRenderer->GetMaterial(0)) {
|
||||
return material;
|
||||
}
|
||||
}
|
||||
|
||||
if (visibleObject.mesh != nullptr && visibleObject.mesh->GetMaterials().Size() > 0) {
|
||||
return visibleObject.mesh->GetMaterials()[0];
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const Resources::Texture* BuiltinForwardPipeline::ResolveTexture(const Resources::Material* material) const {
|
||||
return material != nullptr ? FindMaterialTexture(*material) : nullptr;
|
||||
}
|
||||
|
||||
RHI::RHIResourceView* BuiltinForwardPipeline::ResolveTextureView(
|
||||
const VisibleRenderObject& visibleObject,
|
||||
uint32_t materialIndex) {
|
||||
const Resources::Material* material = ResolveMaterial(visibleObject, materialIndex);
|
||||
const VisibleRenderItem& visibleItem) {
|
||||
const Resources::Material* material = ResolveMaterial(visibleItem);
|
||||
const Resources::Texture* texture = ResolveTexture(material);
|
||||
if (texture != nullptr) {
|
||||
const RenderResourceCache::CachedTexture* cachedTexture = m_resourceCache.GetOrCreateTexture(m_device, texture);
|
||||
@@ -553,11 +529,11 @@ RHI::RHIResourceView* BuiltinForwardPipeline::ResolveTextureView(
|
||||
return m_fallbackTextureView;
|
||||
}
|
||||
|
||||
bool BuiltinForwardPipeline::DrawVisibleObject(
|
||||
bool BuiltinForwardPipeline::DrawVisibleItem(
|
||||
const RenderContext& context,
|
||||
const RenderSceneData& sceneData,
|
||||
const VisibleRenderObject& visibleObject) {
|
||||
const RenderResourceCache::CachedMesh* cachedMesh = m_resourceCache.GetOrCreateMesh(m_device, visibleObject.mesh);
|
||||
const VisibleRenderItem& visibleItem) {
|
||||
const RenderResourceCache::CachedMesh* cachedMesh = m_resourceCache.GetOrCreateMesh(m_device, visibleItem.mesh);
|
||||
if (cachedMesh == nullptr || cachedMesh->vertexBufferView == nullptr) {
|
||||
return false;
|
||||
}
|
||||
@@ -575,37 +551,10 @@ bool BuiltinForwardPipeline::DrawVisibleObject(
|
||||
const PerObjectConstants constants = {
|
||||
sceneData.cameraData.projection,
|
||||
sceneData.cameraData.view,
|
||||
visibleObject.localToWorld.Transpose()
|
||||
visibleItem.localToWorld.Transpose()
|
||||
};
|
||||
|
||||
const Containers::Array<Resources::MeshSection>& sections = visibleObject.mesh->GetSections();
|
||||
const bool hasSections = !sections.Empty();
|
||||
|
||||
if (hasSections) {
|
||||
for (size_t sectionIndex = 0; sectionIndex < sections.Size(); ++sectionIndex) {
|
||||
const Resources::MeshSection& section = sections[sectionIndex];
|
||||
RHI::RHIResourceView* textureView = ResolveTextureView(visibleObject, section.materialID);
|
||||
if (textureView == nullptr) {
|
||||
continue;
|
||||
}
|
||||
|
||||
m_constantSet->WriteConstant(0, &constants, sizeof(constants));
|
||||
m_textureSet->Update(0, textureView);
|
||||
RHI::RHIDescriptorSet* descriptorSets[] = { m_constantSet, m_textureSet, m_samplerSet };
|
||||
commandList->SetGraphicsDescriptorSets(kDescriptorFirstSet, 3, descriptorSets, m_pipelineLayout);
|
||||
|
||||
if (cachedMesh->indexBufferView != nullptr && section.indexCount > 0) {
|
||||
// MeshLoader flattens section indices into a single global index buffer.
|
||||
commandList->DrawIndexed(section.indexCount, 1, section.startIndex, 0, 0);
|
||||
} else if (section.vertexCount > 0) {
|
||||
commandList->Draw(section.vertexCount, 1, section.baseVertex, 0);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
RHI::RHIResourceView* textureView = ResolveTextureView(visibleObject, 0);
|
||||
RHI::RHIResourceView* textureView = ResolveTextureView(visibleItem);
|
||||
if (textureView == nullptr) {
|
||||
return false;
|
||||
}
|
||||
@@ -615,6 +564,23 @@ bool BuiltinForwardPipeline::DrawVisibleObject(
|
||||
RHI::RHIDescriptorSet* descriptorSets[] = { m_constantSet, m_textureSet, m_samplerSet };
|
||||
commandList->SetGraphicsDescriptorSets(kDescriptorFirstSet, 3, descriptorSets, m_pipelineLayout);
|
||||
|
||||
if (visibleItem.hasSection) {
|
||||
const Containers::Array<Resources::MeshSection>& sections = visibleItem.mesh->GetSections();
|
||||
if (visibleItem.sectionIndex >= sections.Size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const Resources::MeshSection& section = sections[visibleItem.sectionIndex];
|
||||
if (cachedMesh->indexBufferView != nullptr && section.indexCount > 0) {
|
||||
// MeshLoader flattens section indices into a single global index buffer.
|
||||
commandList->DrawIndexed(section.indexCount, 1, section.startIndex, 0, 0);
|
||||
} else if (section.vertexCount > 0) {
|
||||
commandList->Draw(section.vertexCount, 1, section.baseVertex, 0);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (cachedMesh->indexBufferView != nullptr && cachedMesh->indexCount > 0) {
|
||||
commandList->DrawIndexed(cachedMesh->indexCount, 1, 0, 0, 0);
|
||||
} else if (cachedMesh->vertexCount > 0) {
|
||||
|
||||
Reference in New Issue
Block a user