Use single color materialinstead of shader for LOD Preview

This commit is contained in:
Wojtek Figat
2021-10-07 09:29:22 +02:00
parent 98ca7c1726
commit 0bd5f63ae4
4 changed files with 22 additions and 170 deletions

View File

@@ -3,83 +3,46 @@
#if USE_EDITOR
#include "LODPreview.h"
#include "Engine/Core/Types/Variant.h"
#include "Engine/Content/Content.h"
#include "Engine/Content/Assets/Model.h"
#include "Engine/Graphics/GPUDevice.h"
#include "Engine/Graphics/GPUPipelineState.h"
#include "Engine/Graphics/Shaders/GPUShader.h"
#include "Engine/Graphics/Shaders/GPUConstantBuffer.h"
#include "Engine/Graphics/RenderTask.h"
#include "Engine/Renderer/DrawCall.h"
#include "Engine/Renderer/RenderList.h"
PACK_STRUCT(struct SingleColorShaderData {
Matrix ViewProjectionMatrix;
Matrix WorldMatrix;
Color Color;
Vector3 Dummy0;
float LODDitherFactor;
});
LODPreviewMaterialShader::LODPreviewMaterialShader()
{
_psModel.CreatePipelineStates();
_shader = Content::LoadAsyncInternal<Shader>(TEXT("Shaders/Editor/SingleColor"));
if (!_shader)
return;
#if COMPILE_WITH_DEV_ENV
_shader.Get()->OnReloading.Bind<LODPreviewMaterialShader, &LODPreviewMaterialShader::OnShaderReloading>(this);
#endif
_material = Content::LoadAsyncInternal<Material>(TEXT("Editor/DebugMaterials/Single Color Surface"));
}
#if COMPILE_WITH_DEV_ENV
void LODPreviewMaterialShader::OnShaderReloading(Asset* obj)
{
_psModel.Release();
}
#endif
const MaterialInfo& LODPreviewMaterialShader::GetInfo() const
{
return _info;
if (_material)
return _material->GetInfo();
return MaterialInfo();
}
bool LODPreviewMaterialShader::IsReady() const
{
return _shader && _shader->IsLoaded();
return _material && _material->IsReady();
}
bool LODPreviewMaterialShader::CanUseInstancing(InstancingHandler& handler) const
{
handler = { SurfaceDrawCallHandler::GetHash, SurfaceDrawCallHandler::CanBatch, SurfaceDrawCallHandler::WriteDrawCall, };
return true;
return _material->CanUseInstancing(handler);
}
DrawPass LODPreviewMaterialShader::GetDrawModes() const
{
return DrawPass::GBuffer;
return _material->GetDrawModes();
}
void LODPreviewMaterialShader::Bind(BindParameters& params)
{
auto context = params.GPUContext;
auto& drawCall = *params.FirstDrawCall;
auto shader = _shader->GetShader();
auto cb = shader->GetCB(0);
const int32 psIndex = params.DrawCallsCount == 1 ? 0 : 1;
auto ps = _psModel[psIndex];
if (!ps->IsValid())
{
auto psDesc = GPUPipelineState::Description::Default;
psDesc.VS = shader->GetVS("VS_Model", psIndex);
psDesc.PS = shader->GetPS("PS_GBuffer");
ps->Init(psDesc);
}
// Find the LOD that produced this draw call
int32 lodIndex = 0;
auto& drawCall = *params.FirstDrawCall;
for (auto& e : Content::GetAssetsRaw())
{
auto model = ScriptingObject::Cast<Model>(e.Value);
@@ -103,27 +66,17 @@ void LODPreviewMaterialShader::Bind(BindParameters& params)
}
// Bind
if (cb && cb->GetSize())
{
ASSERT_LOW_LAYER(cb->GetSize() == sizeof(SingleColorShaderData));
SingleColorShaderData data;
Matrix::Transpose(params.RenderContext.View.Frustum.GetMatrix(), data.ViewProjectionMatrix);
Matrix::Transpose(drawCall.World, data.WorldMatrix);
data.LODDitherFactor = drawCall.Surface.LODDitherFactor;
const Color colors[MODEL_MAX_LODS] = {
Color::White,
Color::Red,
Color::Orange,
Color::Yellow,
Color::Green,
Color::Blue,
};
ASSERT(lodIndex < MODEL_MAX_LODS);
data.Color = colors[lodIndex];
context->UpdateCB(cb, &data);
context->BindCB(0, cb);
}
context->SetState(ps);
const Color colors[MODEL_MAX_LODS] = {
Color::White,
Color::Red,
Color::Orange,
Color::Yellow,
Color::Green,
Color::Blue,
};
const Color color(colors[lodIndex]);
_material->SetParameterValue(TEXT("Color"), Variant(color));
_material->Bind(params);
}
#endif