refactor: add minimal material gpu binding
This commit is contained in:
@@ -49,7 +49,7 @@ private:
|
||||
namespace {
|
||||
|
||||
constexpr uint32_t kDescriptorFirstSet = 1;
|
||||
constexpr uint32_t kDescriptorSetCount = 4;
|
||||
constexpr uint32_t kDescriptorSetCount = 5;
|
||||
|
||||
const char kBuiltinForwardHlsl[] = R"(
|
||||
Texture2D gBaseColorTexture : register(t1);
|
||||
@@ -64,6 +64,10 @@ cbuffer PerObjectConstants : register(b1) {
|
||||
float4 gMainLightColorAndFlags;
|
||||
};
|
||||
|
||||
cbuffer MaterialConstants : register(b2) {
|
||||
float4 gBaseColorFactor;
|
||||
};
|
||||
|
||||
struct VSInput {
|
||||
float3 position : POSITION;
|
||||
float3 normal : NORMAL;
|
||||
@@ -87,7 +91,7 @@ PSInput MainVS(VSInput input) {
|
||||
}
|
||||
|
||||
float4 MainPS(PSInput input) : SV_TARGET {
|
||||
float4 baseColor = gBaseColorTexture.Sample(gLinearSampler, input.texcoord);
|
||||
float4 baseColor = gBaseColorTexture.Sample(gLinearSampler, input.texcoord) * gBaseColorFactor;
|
||||
if (gMainLightColorAndFlags.a < 0.5f) {
|
||||
return baseColor;
|
||||
}
|
||||
@@ -139,13 +143,17 @@ layout(std140, binding = 1) uniform PerObjectConstants {
|
||||
vec4 gMainLightColorAndFlags;
|
||||
};
|
||||
|
||||
layout(std140, binding = 2) uniform MaterialConstants {
|
||||
vec4 gBaseColorFactor;
|
||||
};
|
||||
|
||||
in vec3 vNormalWS;
|
||||
in vec2 vTexCoord;
|
||||
|
||||
layout(location = 0) out vec4 fragColor;
|
||||
|
||||
void main() {
|
||||
vec4 baseColor = texture(uBaseColorTexture, vTexCoord);
|
||||
vec4 baseColor = texture(uBaseColorTexture, vTexCoord) * gBaseColorFactor;
|
||||
if (gMainLightColorAndFlags.w < 0.5) {
|
||||
fragColor = baseColor;
|
||||
return;
|
||||
@@ -187,8 +195,8 @@ void main() {
|
||||
)";
|
||||
|
||||
const char kBuiltinForwardVulkanFragmentShader[] = R"(#version 450
|
||||
layout(set = 2, binding = 0) uniform texture2D uBaseColorTexture;
|
||||
layout(set = 3, binding = 0) uniform sampler uLinearSampler;
|
||||
layout(set = 3, binding = 0) uniform texture2D uBaseColorTexture;
|
||||
layout(set = 4, binding = 0) uniform sampler uLinearSampler;
|
||||
|
||||
layout(set = 1, binding = 0, std140) uniform PerObjectConstants {
|
||||
mat4 gProjectionMatrix;
|
||||
@@ -199,13 +207,17 @@ layout(set = 1, binding = 0, std140) uniform PerObjectConstants {
|
||||
vec4 gMainLightColorAndFlags;
|
||||
};
|
||||
|
||||
layout(set = 2, binding = 0, std140) uniform MaterialConstants {
|
||||
vec4 gBaseColorFactor;
|
||||
};
|
||||
|
||||
layout(location = 0) in vec3 vNormalWS;
|
||||
layout(location = 1) in vec2 vTexCoord;
|
||||
|
||||
layout(location = 0) out vec4 fragColor;
|
||||
|
||||
void main() {
|
||||
vec4 baseColor = texture(sampler2D(uBaseColorTexture, uLinearSampler), vTexCoord);
|
||||
vec4 baseColor = texture(sampler2D(uBaseColorTexture, uLinearSampler), vTexCoord) * gBaseColorFactor;
|
||||
if (gMainLightColorAndFlags.w < 0.5) {
|
||||
fragColor = baseColor;
|
||||
return;
|
||||
@@ -277,26 +289,6 @@ RHI::GraphicsPipelineDesc CreatePipelineDesc(
|
||||
return pipelineDesc;
|
||||
}
|
||||
|
||||
const Resources::Texture* FindMaterialTexture(const Resources::Material& material) {
|
||||
static const char* kTextureNames[] = {
|
||||
"baseColorTexture",
|
||||
"_BaseColorTexture",
|
||||
"_MainTex",
|
||||
"albedoTexture",
|
||||
"mainTexture",
|
||||
"texture"
|
||||
};
|
||||
|
||||
for (const char* textureName : kTextureNames) {
|
||||
const Resources::ResourceHandle<Resources::Texture> textureHandle = material.GetTexture(textureName);
|
||||
if (textureHandle.Get() != nullptr && textureHandle->IsValid()) {
|
||||
return textureHandle.Get();
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
BuiltinForwardPipeline::BuiltinForwardPipeline() {
|
||||
@@ -497,6 +489,10 @@ bool BuiltinForwardPipeline::CreatePipelineResources(const RenderContext& contex
|
||||
constantLayout.bindings = &constantBinding;
|
||||
constantLayout.bindingCount = 1;
|
||||
|
||||
RHI::DescriptorSetLayoutDesc materialConstantLayout = {};
|
||||
materialConstantLayout.bindings = &constantBinding;
|
||||
materialConstantLayout.bindingCount = 1;
|
||||
|
||||
RHI::DescriptorSetLayoutBinding textureBinding = {};
|
||||
textureBinding.binding = 0;
|
||||
textureBinding.type = static_cast<uint32_t>(RHI::DescriptorType::SRV);
|
||||
@@ -546,8 +542,9 @@ bool BuiltinForwardPipeline::CreatePipelineResources(const RenderContext& contex
|
||||
RHI::DescriptorSetLayoutDesc setLayouts[kDescriptorSetCount] = {};
|
||||
setLayouts[0] = reservedLayout;
|
||||
setLayouts[1] = constantLayout;
|
||||
setLayouts[2] = textureLayout;
|
||||
setLayouts[3] = samplerLayout;
|
||||
setLayouts[2] = materialConstantLayout;
|
||||
setLayouts[3] = textureLayout;
|
||||
setLayouts[4] = samplerLayout;
|
||||
|
||||
RHI::RHIPipelineLayoutDesc pipelineLayoutDesc = {};
|
||||
pipelineLayoutDesc.setLayouts = setLayouts;
|
||||
@@ -618,10 +615,11 @@ void BuiltinForwardPipeline::DestroyPipelineResources() {
|
||||
}
|
||||
m_perObjectSets.clear();
|
||||
|
||||
for (auto& textureSetPair : m_textureSets) {
|
||||
DestroyOwnedDescriptorSet(textureSetPair.second);
|
||||
for (auto& materialBindingPair : m_materialBindings) {
|
||||
DestroyOwnedDescriptorSet(materialBindingPair.second.constantSet);
|
||||
DestroyOwnedDescriptorSet(materialBindingPair.second.textureSet);
|
||||
}
|
||||
m_textureSets.clear();
|
||||
m_materialBindings.clear();
|
||||
|
||||
if (m_fallbackTextureView != nullptr) {
|
||||
m_fallbackTextureView->Shutdown();
|
||||
@@ -727,44 +725,82 @@ RHI::RHIDescriptorSet* BuiltinForwardPipeline::GetOrCreatePerObjectSet(Core::uin
|
||||
return result.first->second.set;
|
||||
}
|
||||
|
||||
RHI::RHIDescriptorSet* BuiltinForwardPipeline::GetOrCreateTextureSet(RHI::RHIResourceView* textureView) {
|
||||
BuiltinForwardPipeline::CachedMaterialBindings* BuiltinForwardPipeline::GetOrCreateMaterialBindings(
|
||||
const Resources::Material* material,
|
||||
RHI::RHIResourceView* textureView) {
|
||||
if (textureView == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const auto existing = m_textureSets.find(textureView);
|
||||
if (existing != m_textureSets.end()) {
|
||||
return existing->second.set;
|
||||
CachedMaterialBindings& cachedBindings = m_materialBindings[material];
|
||||
|
||||
if (cachedBindings.constantSet.set == nullptr) {
|
||||
RHI::DescriptorPoolDesc constantPoolDesc = {};
|
||||
constantPoolDesc.type = RHI::DescriptorHeapType::CBV_SRV_UAV;
|
||||
constantPoolDesc.descriptorCount = 1;
|
||||
constantPoolDesc.shaderVisible = false;
|
||||
|
||||
cachedBindings.constantSet.pool = m_device->CreateDescriptorPool(constantPoolDesc);
|
||||
if (cachedBindings.constantSet.pool == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
RHI::DescriptorSetLayoutBinding constantBinding = {};
|
||||
constantBinding.binding = 0;
|
||||
constantBinding.type = static_cast<uint32_t>(RHI::DescriptorType::CBV);
|
||||
constantBinding.count = 1;
|
||||
|
||||
RHI::DescriptorSetLayoutDesc constantLayout = {};
|
||||
constantLayout.bindings = &constantBinding;
|
||||
constantLayout.bindingCount = 1;
|
||||
cachedBindings.constantSet.set = cachedBindings.constantSet.pool->AllocateSet(constantLayout);
|
||||
if (cachedBindings.constantSet.set == nullptr) {
|
||||
DestroyOwnedDescriptorSet(cachedBindings.constantSet);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
RHI::DescriptorPoolDesc poolDesc = {};
|
||||
poolDesc.type = RHI::DescriptorHeapType::CBV_SRV_UAV;
|
||||
poolDesc.descriptorCount = 1;
|
||||
poolDesc.shaderVisible = true;
|
||||
if (cachedBindings.textureSet.set == nullptr) {
|
||||
RHI::DescriptorPoolDesc texturePoolDesc = {};
|
||||
texturePoolDesc.type = RHI::DescriptorHeapType::CBV_SRV_UAV;
|
||||
texturePoolDesc.descriptorCount = 1;
|
||||
texturePoolDesc.shaderVisible = true;
|
||||
|
||||
OwnedDescriptorSet descriptorSet = {};
|
||||
descriptorSet.pool = m_device->CreateDescriptorPool(poolDesc);
|
||||
if (descriptorSet.pool == nullptr) {
|
||||
return nullptr;
|
||||
cachedBindings.textureSet.pool = m_device->CreateDescriptorPool(texturePoolDesc);
|
||||
if (cachedBindings.textureSet.pool == nullptr) {
|
||||
DestroyOwnedDescriptorSet(cachedBindings.constantSet);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
RHI::DescriptorSetLayoutBinding textureBinding = {};
|
||||
textureBinding.binding = 0;
|
||||
textureBinding.type = static_cast<uint32_t>(RHI::DescriptorType::SRV);
|
||||
textureBinding.count = 1;
|
||||
|
||||
RHI::DescriptorSetLayoutDesc textureLayout = {};
|
||||
textureLayout.bindings = &textureBinding;
|
||||
textureLayout.bindingCount = 1;
|
||||
cachedBindings.textureSet.set = cachedBindings.textureSet.pool->AllocateSet(textureLayout);
|
||||
if (cachedBindings.textureSet.set == nullptr) {
|
||||
DestroyOwnedDescriptorSet(cachedBindings.textureSet);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
RHI::DescriptorSetLayoutBinding binding = {};
|
||||
binding.binding = 0;
|
||||
binding.type = static_cast<uint32_t>(RHI::DescriptorType::SRV);
|
||||
binding.count = 1;
|
||||
const Core::uint64 materialVersion = material != nullptr ? material->GetChangeVersion() : 0;
|
||||
if (cachedBindings.materialVersion != materialVersion || cachedBindings.textureView != textureView) {
|
||||
const BuiltinForwardMaterialData materialData = BuildBuiltinForwardMaterialData(material);
|
||||
const PerMaterialConstants materialConstants = {
|
||||
materialData.baseColorFactor
|
||||
};
|
||||
|
||||
RHI::DescriptorSetLayoutDesc layout = {};
|
||||
layout.bindings = &binding;
|
||||
layout.bindingCount = 1;
|
||||
descriptorSet.set = descriptorSet.pool->AllocateSet(layout);
|
||||
if (descriptorSet.set == nullptr) {
|
||||
DestroyOwnedDescriptorSet(descriptorSet);
|
||||
return nullptr;
|
||||
cachedBindings.constantSet.set->WriteConstant(0, &materialConstants, sizeof(materialConstants));
|
||||
cachedBindings.textureSet.set->Update(0, textureView);
|
||||
cachedBindings.materialVersion = materialVersion;
|
||||
cachedBindings.textureView = textureView;
|
||||
}
|
||||
|
||||
descriptorSet.set->Update(0, textureView);
|
||||
const auto result = m_textureSets.emplace(textureView, descriptorSet);
|
||||
return result.first->second.set;
|
||||
return &cachedBindings;
|
||||
}
|
||||
|
||||
void BuiltinForwardPipeline::DestroyOwnedDescriptorSet(OwnedDescriptorSet& descriptorSet) {
|
||||
@@ -782,7 +818,7 @@ void BuiltinForwardPipeline::DestroyOwnedDescriptorSet(OwnedDescriptorSet& descr
|
||||
}
|
||||
|
||||
const Resources::Texture* BuiltinForwardPipeline::ResolveTexture(const Resources::Material* material) const {
|
||||
return material != nullptr ? FindMaterialTexture(*material) : nullptr;
|
||||
return ResolveBuiltinBaseColorTexture(material);
|
||||
}
|
||||
|
||||
RHI::RHIResourceView* BuiltinForwardPipeline::ResolveTextureView(
|
||||
@@ -844,16 +880,26 @@ bool BuiltinForwardPipeline::DrawVisibleItem(
|
||||
return false;
|
||||
}
|
||||
|
||||
const Resources::Material* material = ResolveMaterial(visibleItem);
|
||||
RHI::RHIDescriptorSet* constantSet = GetOrCreatePerObjectSet(
|
||||
visibleItem.gameObject != nullptr ? visibleItem.gameObject->GetID() : 0);
|
||||
RHI::RHIDescriptorSet* textureSet = GetOrCreateTextureSet(textureView);
|
||||
if (constantSet == nullptr || textureSet == nullptr || m_samplerSet == nullptr) {
|
||||
CachedMaterialBindings* materialBindings = GetOrCreateMaterialBindings(material, textureView);
|
||||
if (constantSet == nullptr ||
|
||||
materialBindings == nullptr ||
|
||||
materialBindings->constantSet.set == nullptr ||
|
||||
materialBindings->textureSet.set == nullptr ||
|
||||
m_samplerSet == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
constantSet->WriteConstant(0, &constants, sizeof(constants));
|
||||
RHI::RHIDescriptorSet* descriptorSets[] = { constantSet, textureSet, m_samplerSet };
|
||||
commandList->SetGraphicsDescriptorSets(kDescriptorFirstSet, 3, descriptorSets, m_pipelineLayout);
|
||||
RHI::RHIDescriptorSet* descriptorSets[] = {
|
||||
constantSet,
|
||||
materialBindings->constantSet.set,
|
||||
materialBindings->textureSet.set,
|
||||
m_samplerSet
|
||||
};
|
||||
commandList->SetGraphicsDescriptorSets(kDescriptorFirstSet, 4, descriptorSets, m_pipelineLayout);
|
||||
|
||||
if (visibleItem.hasSection) {
|
||||
const Containers::Array<Resources::MeshSection>& sections = visibleItem.mesh->GetSections();
|
||||
|
||||
Reference in New Issue
Block a user