Re-aded ability to change shaders

Bugged, replacing shaders results in object being rendered in the old position
This commit is contained in:
Kah Wei 2022-11-19 17:30:48 +08:00
parent f3a44d2ead
commit 58e6a3c4c2
6 changed files with 33 additions and 14 deletions

View File

@ -174,7 +174,6 @@ namespace SHADE
bool shaderChanged = false;
const auto* SHADER_INFO = SHAssetManager::GetData<SHShaderAsset>(currentMatSpec->fragShader);
const std::string SHADER_NAME = SHADER_INFO ? SHADER_INFO->name : "Unknown Shader";
ImGui::BeginDisabled();
isDirty |= SHEditorWidgets::DragDropReadOnlyField<AssetID>
(
"Fragment Shader", SHADER_NAME.data(),
@ -182,7 +181,6 @@ namespace SHADE
[this](const AssetID& id) { currentMatSpec->fragShader = id; },
SHDragDrop::DRAG_RESOURCE
);
ImGui::EndDisabled();
// Load the shader to access it's data
auto fragShader = SHResourceManager::LoadOrGet<SHVkShaderModule>(currentMatSpec->fragShader);

View File

@ -507,9 +507,13 @@ namespace SHADE
return;
}
// Nothing to draw
if (subBatches.empty())
return;
// Bind all required objects before drawing
static std::array<uint32_t, 1> dynamicOffset{ 0 };
cmdBuffer->BeginLabeledSegment("SHBatch");
cmdBuffer->BeginLabeledSegment("SHBatch for Pipeline #" + std::to_string(pipeline.GetId().Data.Index));
cmdBuffer->BindPipeline(pipeline);
cmdBuffer->BindVertexBuffer(SHGraphicsConstants::VertexBufferBindings::TRANSFORM, transformDataBuffer[frameIndex], 0);
cmdBuffer->BindVertexBuffer(SHGraphicsConstants::VertexBufferBindings::INTEGER_DATA, instancedIntegerBuffer[frameIndex], 0);

View File

@ -55,13 +55,14 @@ namespace SHADE
void SHSuperBatch::Remove(const SHRenderable* renderable) noexcept
{
const Handle<SHVkPipeline> PIPELINE = renderable->GetMaterial()->GetBaseMaterial()->GetPipeline();
Handle<SHMaterial> baseMat = renderable->GetMaterial()->GetBaseMaterial();
const Handle<SHVkPipeline> PIPELINE = baseMat->HasPipelineChanged() ? baseMat->GetPrevPipeline() : baseMat->GetPipeline();
// Check if we have a Batch with the same pipeline yet
auto batch = std::find_if(batches.begin(), batches.end(), [&](const SHBatch& batch)
{
return batch.GetPipeline() == PIPELINE;
});
{
return batch.GetPipeline() == PIPELINE;
});
// Attempt to remove if it exists
if (batch == batches.end())

View File

@ -890,7 +890,9 @@ namespace SHADE
Handle<SHMaterialInstance> prevMaterial = renderable.HasMaterialChanged() ? renderable.GetPrevMaterial() : renderable.GetMaterial();
if (prevMaterial)
{
Handle<SHSuperBatch> oldSuperBatch = prevMaterial->GetBaseMaterial()->GetPipeline()->GetPipelineState().GetSubpass()->GetSuperBatch();
Handle<SHMaterial> baseMat = prevMaterial->GetBaseMaterial();
Handle<SHVkPipeline> prevPipeline = baseMat->HasPipelineChanged() ? baseMat->GetPrevPipeline() : baseMat->GetPipeline();
Handle<SHSuperBatch> oldSuperBatch = prevPipeline->GetPipelineState().GetSubpass()->GetSuperBatch();
oldSuperBatch->Remove(&renderable);
}
@ -905,6 +907,14 @@ namespace SHADE
// Unset change flag
renderable.ResetChangedFlag();
}
// Unset all material old pipeline since we would have finished processing
auto gfxSystem = reinterpret_cast<SHGraphicsSystem*>(system);
auto [matBegin, matEnd] = gfxSystem->resourceManager.GetDenseAccess<SHMaterial>();
for (auto iter = matBegin; iter != matEnd; ++iter)
{
iter->ForgetOldPipeline();
}
}
#pragma endregion ROUTINES

View File

@ -21,6 +21,8 @@ namespace SHADE
if (_pipeline == pipeline)
return;
// Mark old pipeline and set new pipeline
oldPipeline = pipeline;
pipeline = _pipeline;
// Set up properties based on the pipeline
@ -41,9 +43,6 @@ namespace SHADE
// Reset since pipeline changed
ResetProperties();
// Mark changed so that we know to update dependent material instances
propertiesChanged = true;
}
Handle<SHVkPipeline> SHMaterial::GetPipeline() const
@ -111,4 +110,9 @@ namespace SHADE
{
propertiesChanged = false;
}
void SHMaterial::ForgetOldPipeline() noexcept
{
oldPipeline = {};
}
}

View File

@ -44,6 +44,7 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/
void SetPipeline(Handle<SHVkPipeline> _pipeline);
Handle<SHVkPipeline> GetPipeline() const;
Handle<SHVkPipeline> GetPrevPipeline() const { return oldPipeline; };
/*-----------------------------------------------------------------------------*/
/* Property Functions */
@ -68,10 +69,11 @@ namespace SHADE
/* Query Functions */
/*-----------------------------------------------------------------------------*/
Handle<SHShaderBlockInterface> GetShaderBlockInterface() const noexcept;
bool HasPipelineChanged() const noexcept { return pipelineChanged; }
bool HasPipelineChanged() const noexcept { return oldPipeline; }
bool HasPropertiesChanged() const noexcept { return propertiesChanged; }
bool HasChanged() const noexcept { return pipelineChanged || propertiesChanged; }
bool HasChanged() const noexcept { return oldPipeline || propertiesChanged; }
void ClearChangeFlag() noexcept;
void ForgetOldPipeline() noexcept;
private:
/*-----------------------------------------------------------------------------*/
@ -81,7 +83,7 @@ namespace SHADE
std::unique_ptr<char> propMemory;
Byte propMemorySize = 0;
bool propertiesChanged = true;
bool pipelineChanged = true;
Handle<SHVkPipeline> oldPipeline;
/*-----------------------------------------------------------------------------*/
/* Helper Functions */