Add RHI texture upload and descriptor set fixes
This commit is contained in:
@@ -18,6 +18,24 @@ OpenGLDescriptorSet::~OpenGLDescriptorSet() {
|
||||
Shutdown();
|
||||
}
|
||||
|
||||
DescriptorBinding* OpenGLDescriptorSet::FindBinding(uint32_t binding) {
|
||||
for (auto& descriptorBinding : m_bindings) {
|
||||
if (descriptorBinding.binding == binding) {
|
||||
return &descriptorBinding;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const DescriptorBinding* OpenGLDescriptorSet::FindBinding(uint32_t binding) const {
|
||||
for (const auto& descriptorBinding : m_bindings) {
|
||||
if (descriptorBinding.binding == binding) {
|
||||
return &descriptorBinding;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool OpenGLDescriptorSet::Initialize(OpenGLTextureUnitAllocator* allocator, uint32_t count, const DescriptorSetLayoutDesc& layout) {
|
||||
m_allocator = allocator;
|
||||
m_bindingCount = layout.bindingCount;
|
||||
@@ -38,6 +56,18 @@ bool OpenGLDescriptorSet::Initialize(OpenGLTextureUnitAllocator* allocator, uint
|
||||
m_bindings[i].textureIds.resize(layout.bindings[i].count, 0);
|
||||
m_bindings[i].samplerIds.resize(layout.bindings[i].count, 0);
|
||||
|
||||
if (m_bindings[i].type != DescriptorType::SRV &&
|
||||
m_bindings[i].type != DescriptorType::Sampler &&
|
||||
m_bindings[i].type != DescriptorType::UAV) {
|
||||
m_bindings[i].textureUnits.clear();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (m_allocator == nullptr) {
|
||||
Shutdown();
|
||||
return false;
|
||||
}
|
||||
|
||||
for (uint32_t j = 0; j < layout.bindings[i].count; ++j) {
|
||||
int32_t unit = m_allocator->Allocate();
|
||||
if (unit < 0) {
|
||||
@@ -80,17 +110,13 @@ void OpenGLDescriptorSet::Update(uint32_t offset, RHIResourceView* view) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t bindingIndex = offset;
|
||||
if (bindingIndex >= m_bindings.size()) {
|
||||
DescriptorBinding* binding = FindBinding(offset);
|
||||
if (binding == nullptr || binding->textureIds.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
OpenGLResourceView* glView = static_cast<OpenGLResourceView*>(view);
|
||||
uint32_t textureId = glView->GetTexture();
|
||||
|
||||
if (offset < m_bindings[bindingIndex].textureIds.size()) {
|
||||
m_bindings[bindingIndex].textureIds[offset] = textureId;
|
||||
}
|
||||
binding->textureIds[0] = glView->GetTexture();
|
||||
}
|
||||
|
||||
void OpenGLDescriptorSet::UpdateSampler(uint32_t offset, RHISampler* sampler) {
|
||||
@@ -98,17 +124,13 @@ void OpenGLDescriptorSet::UpdateSampler(uint32_t offset, RHISampler* sampler) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t bindingIndex = offset;
|
||||
if (bindingIndex >= m_bindings.size()) {
|
||||
DescriptorBinding* binding = FindBinding(offset);
|
||||
if (binding == nullptr || binding->samplerIds.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
OpenGLSampler* glSampler = static_cast<OpenGLSampler*>(sampler);
|
||||
uint32_t samplerId = glSampler->GetID();
|
||||
|
||||
if (offset < m_bindings[bindingIndex].samplerIds.size()) {
|
||||
m_bindings[bindingIndex].samplerIds[offset] = samplerId;
|
||||
}
|
||||
binding->samplerIds[0] = glSampler->GetID();
|
||||
}
|
||||
|
||||
void OpenGLDescriptorSet::Bind() {
|
||||
@@ -130,13 +152,9 @@ void OpenGLDescriptorSet::Bind() {
|
||||
uint32_t textureId = binding.textureIds[j];
|
||||
uint32_t samplerId = binding.samplerIds[j];
|
||||
|
||||
if (textureId != 0) {
|
||||
if (textureId != 0 && binding.type != DescriptorType::Sampler) {
|
||||
glActiveTexture(GL_TEXTURE0 + unit);
|
||||
if (binding.type == DescriptorType::Sampler) {
|
||||
glBindTexture(GL_SAMPLER, textureId);
|
||||
} else {
|
||||
glBindTexture(GL_TEXTURE_2D, textureId);
|
||||
}
|
||||
glBindTexture(GL_TEXTURE_2D, textureId);
|
||||
}
|
||||
|
||||
if (samplerId != 0) {
|
||||
@@ -165,10 +183,9 @@ void OpenGLDescriptorSet::Unbind() {
|
||||
}
|
||||
|
||||
uint32_t OpenGLDescriptorSet::GetBindingPoint(uint32_t binding) const {
|
||||
for (size_t i = 0; i < m_bindings.size(); ++i) {
|
||||
if (m_bindings[i].binding == binding && !m_bindings[i].textureUnits.empty()) {
|
||||
return m_bindings[i].textureUnits[0];
|
||||
}
|
||||
const DescriptorBinding* descriptorBinding = FindBinding(binding);
|
||||
if (descriptorBinding != nullptr && !descriptorBinding->textureUnits.empty()) {
|
||||
return descriptorBinding->textureUnits[0];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -183,4 +200,4 @@ void OpenGLDescriptorSet::WriteConstant(uint32_t binding, const void* data, size
|
||||
}
|
||||
|
||||
} // namespace RHI
|
||||
} // namespace XCEngine
|
||||
} // namespace XCEngine
|
||||
|
||||
Reference in New Issue
Block a user