[Fix] (Diligent/PSO): Build InputLayout for PSO.

This commit is contained in:
BZLZHH
2025-06-08 17:29:10 +08:00
parent f3497bd2fa
commit 4dfbc01479
3 changed files with 131 additions and 44 deletions
@@ -26,6 +26,61 @@ namespace MG_Diligent {
bool IsInRenderPass = false; bool IsInRenderPass = false;
bool initialized = false; bool initialized = false;
void BuildInputLayout(GLuint program, VertexArrayState& vaState,
std::vector<Diligent::LayoutElement>& inputLayout) {
inputLayout.clear();
auto& programObj = MG_State_T::programState->programs_[program];
for (const auto& [attribIndex, attrib] : vaState.vaos_[vaState.currentVao_].attribs) {
if (!attrib.enabled) continue;
std::string attribName;
for (auto [name, loc] : programObj.attribLocations) {
if (loc == attribIndex) {
attribName = name;
break;
}
}
MG_State::QueryProgramAttributeLocation(program, attribName.c_str());
if (attribIndex == -1) {
continue;
}
Diligent::LayoutElement element;
element.InputIndex = static_cast<Diligent::Uint32>(attribIndex);
element.BufferSlot = 0;
element.NumComponents = attrib.size;
switch (attrib.type) {
case GL_BYTE: element.ValueType = Diligent::VT_INT8; break;
case GL_UNSIGNED_BYTE: element.ValueType = Diligent::VT_UINT8; break;
case GL_SHORT: element.ValueType = Diligent::VT_INT16; break;
case GL_UNSIGNED_SHORT: element.ValueType = Diligent::VT_UINT16; break;
case GL_INT: element.ValueType = Diligent::VT_INT32; break;
case GL_UNSIGNED_INT: element.ValueType = Diligent::VT_UINT32; break;
case GL_FLOAT: element.ValueType = Diligent::VT_FLOAT32; break;
case GL_HALF_FLOAT: element.ValueType = Diligent::VT_FLOAT16; break;
case GL_DOUBLE: element.ValueType = Diligent::VT_FLOAT64; break;
default: element.ValueType = Diligent::VT_UNDEFINED; break;
}
element.IsNormalized = attrib.normalized;
element.RelativeOffset = static_cast<Diligent::Uint32>(reinterpret_cast<size_t>(attrib.pointer));
inputLayout.push_back(element);
MG_Util::Debug::LogD("Built LayoutElement for attrib '%s' (location %d): "
"NumComponents=%d, ValueType=%d, IsNormalized=%s, RelativeOffset=%u",
attribName.c_str(), attribIndex,
element.NumComponents, element.ValueType,
element.IsNormalized ? "true" : "false",
element.RelativeOffset);
}
}
void PipelineStateManager::ConfigurePSO( void PipelineStateManager::ConfigurePSO(
Diligent::GraphicsPipelineStateCreateInfo &PSOCreateInfo, Diligent::GraphicsPipelineStateCreateInfo &PSOCreateInfo,
GLProgramInfo &programInfo, GLProgramInfo &programInfo,
@@ -101,6 +156,20 @@ namespace MG_Diligent {
Diligent::CULL_MODE_BACK : Diligent::CULL_MODE_NONE; Diligent::CULL_MODE_BACK : Diligent::CULL_MODE_NONE;
rasterizerDesc.FrontCounterClockwise = true; rasterizerDesc.FrontCounterClockwise = true;
if (!programInfo.inputLayout.empty()) {
PSOCreateInfo.GraphicsPipeline.InputLayout.LayoutElements = programInfo.inputLayout.data();
PSOCreateInfo.GraphicsPipeline.InputLayout.NumElements =
static_cast<Diligent::Uint32>(programInfo.inputLayout.size());
MG_Util::Debug::LogD("Configured InputLayout with %zu elements",
programInfo.inputLayout.size());
} else {
MG_Util::Debug::LogW("No input layout elements for PSO");
PSOCreateInfo.GraphicsPipeline.InputLayout.LayoutElements = nullptr;
PSOCreateInfo.GraphicsPipeline.InputLayout.NumElements = 0;
}
ConfigureResourceLayout(PSOCreateInfo, programInfo); ConfigureResourceLayout(PSOCreateInfo, programInfo);
} }
@@ -142,6 +211,8 @@ namespace MG_Diligent {
return it->second; return it->second;
} }
MG_Diligent::BuildInputLayout(program, vaState, programInfo.inputLayout);
Diligent::GraphicsPipelineStateCreateInfo PSOCreateInfo; Diligent::GraphicsPipelineStateCreateInfo PSOCreateInfo;
ConfigurePSO(PSOCreateInfo, programInfo, commonState, vaState, fbInfo); ConfigurePSO(PSOCreateInfo, programInfo, commonState, vaState, fbInfo);
@@ -244,6 +244,9 @@ namespace MG_Diligent {
extern PipelineStateManager g_PSOManager; extern PipelineStateManager g_PSOManager;
void BuildInputLayout(GLuint program, VertexArrayState& vaState,
std::vector<Diligent::LayoutElement>& inputLayout);
extern bool initialized; extern bool initialized;
extern bool IsInRenderPass; extern bool IsInRenderPass;
} }
@@ -43,10 +43,10 @@ namespace MG_GL::GL {
case GL_UNSIGNED_INT_VEC2: return 2 * sizeof(uint32_t); case GL_UNSIGNED_INT_VEC2: return 2 * sizeof(uint32_t);
case GL_UNSIGNED_INT_VEC3: return 3 * sizeof(uint32_t); case GL_UNSIGNED_INT_VEC3: return 3 * sizeof(uint32_t);
case GL_UNSIGNED_INT_VEC4: return 4 * sizeof(uint32_t); case GL_UNSIGNED_INT_VEC4: return 4 * sizeof(uint32_t);
// case GL_DOUBLE: return sizeof(double); // Not supported // case GL_DOUBLE: return sizeof(double); // Not supported
// case GL_DOUBLE_VEC2: return 2 * sizeof(double); // Not supported // case GL_DOUBLE_VEC2: return 2 * sizeof(double); // Not supported
// case GL_DOUBLE_VEC3: return 3 * sizeof(double); // Not supported // case GL_DOUBLE_VEC3: return 3 * sizeof(double); // Not supported
// case GL_DOUBLE_VEC4: return 4 * sizeof(double); // Not supported // case GL_DOUBLE_VEC4: return 4 * sizeof(double); // Not supported
default: return 0; default: return 0;
} }
} }
@@ -226,7 +226,7 @@ namespace MG_GL::GL {
void * mapped; void * mapped;
MG_Util::Debug::LogD("Mapping UBO for program %u", program); MG_Util::Debug::LogD("Mapping UBO for program %u", program);
MG_Diligent::g_pContext->MapBuffer(programInfo.pDefaultUBO, Diligent::MAP_WRITE, MG_Diligent::g_pContext->MapBuffer(programInfo.pDefaultUBO, Diligent::MAP_WRITE,
Diligent::MAP_FLAG_DISCARD, mapped); Diligent::MAP_FLAG_DISCARD, mapped);
if (mapped) { if (mapped) {
MG_Util::Debug::LogD("UBO mapped successfully for program %u. Updating uniform values.", program); MG_Util::Debug::LogD("UBO mapped successfully for program %u. Updating uniform values.", program);
@@ -288,29 +288,29 @@ namespace MG_GL::GL {
memcpy(uboData + offset, uniform.uintData.data(), 4 * sizeof(uint32_t)); memcpy(uboData + offset, uniform.uintData.data(), 4 * sizeof(uint32_t));
break; break;
// Not supported types // Not supported types
// case GL_DOUBLE: // case GL_DOUBLE:
// *reinterpret_cast<double*>(uboData + offset) = uniform.doubleData[0]; // *reinterpret_cast<double*>(uboData + offset) = uniform.doubleData[0];
// break; // break;
// case GL_DOUBLE_VEC2: // case GL_DOUBLE_VEC2:
// memcpy(uboData + offset, uniform.doubleData.data(), 2 * sizeof(double)); // memcpy(uboData + offset, uniform.doubleData.data(), 2 * sizeof(double));
// break; // break;
// case GL_DOUBLE_VEC3: // case GL_DOUBLE_VEC3:
// memcpy(uboData + offset, uniform.doubleData.data(), 3 * sizeof(double)); // memcpy(uboData + offset, uniform.doubleData.data(), 3 * sizeof(double));
// break; // break;
// case GL_DOUBLE_VEC4: // case GL_DOUBLE_VEC4:
// memcpy(uboData + offset, uniform.doubleData.data(), 4 * sizeof(double)); // memcpy(uboData + offset, uniform.doubleData.data(), 4 * sizeof(double));
// break; // break;
// case GL_DOUBLE_MAT2: // case GL_DOUBLE_MAT2:
// memcpy(uboData + offset, uniform.doubleData.data(), 4 * sizeof(double)); // memcpy(uboData + offset, uniform.doubleData.data(), 4 * sizeof(double));
// break; // break;
// case GL_DOUBLE_MAT3: // case GL_DOUBLE_MAT3:
// memcpy(uboData + offset, uniform.doubleData.data(), 9 * sizeof(double)); // memcpy(uboData + offset, uniform.doubleData.data(), 9 * sizeof(double));
// break; // break;
// case GL_DOUBLE_MAT4: // case GL_DOUBLE_MAT4:
// memcpy(uboData + offset, uniform.doubleData.data(), 16 * sizeof(double)); // memcpy(uboData + offset, uniform.doubleData.data(), 16 * sizeof(double));
// break; // break;
// ... // ...
} }
} }
} }
@@ -455,14 +455,12 @@ namespace MG_GL::GL {
if (!pBuffer) { if (!pBuffer) {
Diligent::BufferDesc BuffDesc; Diligent::BufferDesc BuffDesc;
BuffDesc.Name = "Buffer"; std::string bufferName = "Dynamic VBO " + std::to_string(buffer);
BuffDesc.Name = bufferName.c_str();
BuffDesc.Size = bufferObj.data.size(); BuffDesc.Size = bufferObj.data.size();
BuffDesc.Usage = Diligent::USAGE_DYNAMIC; BuffDesc.Usage = Diligent::USAGE_DYNAMIC;
BuffDesc.BindFlags = Diligent::BIND_VERTEX_BUFFER; BuffDesc.BindFlags = Diligent::BIND_VERTEX_BUFFER;
BuffDesc.CPUAccessFlags = Diligent::CPU_ACCESS_WRITE;
if (bufferObj.isDynamic) {
BuffDesc.CPUAccessFlags = Diligent::CPU_ACCESS_WRITE;
}
if (pBuffer) { if (pBuffer) {
pBuffer->Release(); pBuffer->Release();
@@ -476,7 +474,7 @@ namespace MG_GL::GL {
MG_Diligent::g_pContext->MapBuffer( MG_Diligent::g_pContext->MapBuffer(
pBuffer, pBuffer,
Diligent::MAP_WRITE, Diligent::MAP_WRITE,
bufferObj.isDynamic ? Diligent::MAP_FLAG_DISCARD : Diligent::MAP_FLAG_NONE, Diligent::MAP_FLAG_DISCARD,
pMappedData pMappedData
); );
@@ -499,15 +497,13 @@ namespace MG_GL::GL {
if (!pBuffer) { if (!pBuffer) {
Diligent::BufferDesc BuffDesc; Diligent::BufferDesc BuffDesc;
BuffDesc.Name = "IndexBuffer"; std::string bufferName = "Dynamic IBO " + std::to_string(buffer);
BuffDesc.Name = bufferName.c_str();
BuffDesc.Size = bufferObj.data.size(); BuffDesc.Size = bufferObj.data.size();
BuffDesc.Usage = bufferObj.isDynamic ? Diligent::USAGE_DYNAMIC BuffDesc.Usage = bufferObj.isDynamic ? Diligent::USAGE_DYNAMIC
: Diligent::USAGE_DEFAULT; : Diligent::USAGE_DEFAULT;
BuffDesc.BindFlags = Diligent::BIND_INDEX_BUFFER; BuffDesc.BindFlags = Diligent::BIND_INDEX_BUFFER;
BuffDesc.CPUAccessFlags = Diligent::CPU_ACCESS_WRITE;
if (bufferObj.isDynamic) {
BuffDesc.CPUAccessFlags = Diligent::CPU_ACCESS_WRITE;
}
if (pBuffer) { if (pBuffer) {
pBuffer->Release(); pBuffer->Release();
@@ -551,6 +547,23 @@ namespace MG_GL::GL {
} }
if (!vertexBuffers.empty()) { if (!vertexBuffers.empty()) {
MG_Util::Debug::LogD("Setting Vertex Buffers:");
MG_Util::Debug::LogD(" NumBuffers: %u", static_cast<Diligent::Uint32>(vertexBuffers.size()));
for (size_t i = 0; i < vertexBuffers.size(); ++i) {
MG_Util::Debug::LogD(" Buffer %zu:", i);
if (vertexBuffers[i]) {
const auto& desc = vertexBuffers[i]->GetDesc();
MG_Util::Debug::LogD(" Name: %s", desc.Name ? desc.Name : "N/A");
MG_Util::Debug::LogD(" Size: %llu", desc.Size);
MG_Util::Debug::LogD(" Usage: %d", desc.Usage);
MG_Util::Debug::LogD(" BindFlags: %u", desc.BindFlags);
MG_Util::Debug::LogD(" CPUAccessFlags: %u", desc.CPUAccessFlags);
} else {
MG_Util::Debug::LogD(" <null buffer>");
}
MG_Util::Debug::LogD(" Offset: %llu", offsets[i]);
}
MG_Diligent::g_pContext->SetVertexBuffers( MG_Diligent::g_pContext->SetVertexBuffers(
0, 0,
static_cast<Diligent::Uint32>(vertexBuffers.size()), static_cast<Diligent::Uint32>(vertexBuffers.size()),
@@ -565,7 +578,7 @@ namespace MG_GL::GL {
GLuint buffer = pVAO->elementBuffer; GLuint buffer = pVAO->elementBuffer;
auto it = MG_Diligent::g_BufferMap.find(buffer); auto it = MG_Diligent::g_BufferMap.find(buffer);
if (it != MG_Diligent::g_BufferMap.end() && it->second) { if (it != MG_Diligent::g_BufferMap.end() && it->second) {
Diligent::Uint64 offset = reinterpret_cast<uintptr_t>(indices); auto offset = reinterpret_cast<uintptr_t>(indices);
MG_Diligent::g_pContext->SetIndexBuffer( MG_Diligent::g_pContext->SetIndexBuffer(
it->second, it->second,