Refactor material shaders generator to use modular features as extensions

This commit is contained in:
Wojtek Figat
2021-02-04 15:27:38 +01:00
parent 9e6243adcc
commit b5847eb0d6
10 changed files with 86 additions and 100 deletions

View File

@@ -70,8 +70,6 @@ void DeferredMaterialShader::Bind(BindParameters& params)
int32 srv = 0;
// Setup features
if (_info.TessellationMode != TessellationMethod::None)
TessellationFeature::Bind(params, cb, srv);
const bool useLightmap = _info.BlendMode == MaterialBlendMode::Opaque && LightmapFeature::Bind(params, cb, srv);
// Setup parameters

View File

@@ -79,8 +79,6 @@ void ForwardMaterialShader::Bind(BindParameters& params)
int32 srv = 0;
// Setup features
if (_info.TessellationMode != TessellationMethod::None)
TessellationFeature::Bind(params, cb, srv);
// Setup parameters
MaterialParameter::BindMeta bindMeta;

View File

@@ -159,7 +159,7 @@ void TessellationFeature::Generate(GeneratorData& data)
{
data.Template = TEXT("Tessellation.hlsl");
data.ConstantsSize = 0;
data.ResourcesCount = SRVs;
data.ResourcesCount = 0;
}
void LightmapFeature::Generate(GeneratorData& data)
@@ -169,4 +169,11 @@ void LightmapFeature::Generate(GeneratorData& data)
data.ResourcesCount = SRVs;
}
void DistortionFeature::Generate(GeneratorData& data)
{
data.Template = TEXT("Distortion.hlsl");
data.ConstantsSize = 0;
data.ResourcesCount = 0;
}
#endif

View File

@@ -43,11 +43,6 @@ struct ForwardShadingFeature : MaterialShaderFeature
// Material shader feature that adds geometry hardware tessellation (using Hull and Domain shaders).
struct TessellationFeature : MaterialShaderFeature
{
enum { SRVs = 0 };
static void Bind(MaterialShader::BindParameters& params, byte*& cb, int32& srv)
{
}
#if USE_EDITOR
static void Generate(GeneratorData& data);
#endif
@@ -68,3 +63,11 @@ struct LightmapFeature : MaterialShaderFeature
static void Generate(GeneratorData& data);
#endif
};
// Material shader feature that adds distortion vectors rendering pass.
struct DistortionFeature : MaterialShaderFeature
{
#if USE_EDITOR
static void Generate(GeneratorData& data);
#endif
};

View File

@@ -72,16 +72,23 @@ void ParticleMaterialShader::Bind(BindParameters& params)
auto& view = params.RenderContext.View;
auto cache = params.RenderContext.List;
auto& drawCall = *params.FirstDrawCall;
const uint32 sortedIndicesOffset = drawCall.Particle.Module->SortedIndicesOffset;
const auto cb0 = _shader->GetCB(0);
const bool hasCb0 = cb0->GetSize() != 0;
ASSERT(hasCb0 && "TODO: fix it"); // TODO: always make cb pointer valid even if cb is missing
const auto cb1 = _shader->GetCB(1);
const bool hasCb1 = cb1->GetSize() != 0;
const uint32 sortedIndicesOffset = drawCall.Particle.Module->SortedIndicesOffset;
const bool hasCb1 = cb1 && cb1->GetSize() != 0;
byte* cb = _cb0Data.Get();
auto materialData = reinterpret_cast<ParticleMaterialShaderData*>(cb);
cb += sizeof(ParticleMaterialShaderData);
int32 srv = 0;
// Setup features
// Setup parameters
MaterialParameter::BindMeta bindMeta;
bindMeta.Context = context;
bindMeta.Constants = hasCb0 ? _cb0Data.Get() + sizeof(ParticleMaterialShaderData) : nullptr;
bindMeta.Constants = cb;
bindMeta.Input = nullptr;
bindMeta.Buffers = params.RenderContext.Buffers;
bindMeta.CanSampleDepth = GPUDevice::Instance->Limits.HasReadOnlyDepth;

View File

@@ -63,8 +63,6 @@ void TerrainMaterialShader::Bind(BindParameters& params)
int32 srv = 3;
// Setup features
if (_info.TessellationMode != TessellationMethod::None)
TessellationFeature::Bind(params, cb, srv);
const bool useLightmap = LightmapFeature::Bind(params, cb, srv);
// Setup parameters

View File

@@ -186,6 +186,8 @@ bool MaterialGenerator::Generate(WriteStream& source, MaterialInfo& materialInfo
ADD_FEATURE(TessellationFeature);
if (materialInfo.BlendMode == MaterialBlendMode::Opaque)
ADD_FEATURE(LightmapFeature);
if (materialInfo.BlendMode != MaterialBlendMode::Opaque && (materialInfo.FeaturesFlags & MaterialFeaturesFlags::DisableDistortion) == 0)
ADD_FEATURE(DistortionFeature);
break;
case MaterialDomain::Terrain:
if (materialInfo.TessellationMode != TessellationMethod::None)
@@ -193,6 +195,10 @@ bool MaterialGenerator::Generate(WriteStream& source, MaterialInfo& materialInfo
ADD_FEATURE(LightmapFeature);
break;
default:
case MaterialDomain::Particle:
if (materialInfo.BlendMode != MaterialBlendMode::Opaque && (materialInfo.FeaturesFlags & MaterialFeaturesFlags::DisableDistortion) == 0)
ADD_FEATURE(DistortionFeature);
break;
break;
}
#undef ADD_FEATURE