Skip to content

Commit

Permalink
feat: fix translucency pass
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Pollind <mpollind@gmail.com>
  • Loading branch information
pollend committed Jan 3, 2024
1 parent f218900 commit 9b5114d
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 89 deletions.
15 changes: 13 additions & 2 deletions HPL2/include/graphics/RendererDeferred2.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ namespace hpl {
MaterialSet& resolveSet(MaterialSetType set);
};
ResourceMaterial& resolveResourceMaterial(cMaterial* material);
uint32_t resolveObjectSlot(uint32_t uid, std::function<void(uint32_t)> initializeHandler);

UniqueViewportData<ViewportData> m_boundViewportData;

Expand Down Expand Up @@ -217,10 +218,17 @@ namespace hpl {
std::array<SharedBuffer, ForgeRenderer::SwapChainLength> m_perSceneInfoBuffer;
std::array<SharedBuffer, ForgeRenderer::SwapChainLength> m_indirectDrawArgsBuffer;

std::array<SharedSampler, resource::MaterialSceneSamplersCount> m_materialSampler;
folly::F14ValueMap<uint32_t, uint32_t> m_objectDescriptorLookup;

SharedTexture m_emptyTexture2D;
SharedTexture m_emptyTextureCube;
Image* m_dissolveImage;

uint32_t m_activeFrame = 0;
uint32_t m_objectIndex = 0;
uint32_t m_indirectDrawIndex = 0;

cRenderList m_rendererList;

SharedRootSignature m_lightClusterRootSignature;
Expand Down Expand Up @@ -299,8 +307,11 @@ namespace hpl {
BlendPipelines m_translucencyRefractionPipline;
BlendPipelines m_translucencyRefractionPiplineNoDepth;

BlendPipelines m_translucencyIlluminationPipline;
BlendPipelines m_translucencyIlluminationPiplineNoDepth;
SharedPipeline m_translucencyIlluminationPipline;
SharedPipeline m_translucencyIlluminationPiplineNoDepth;

//BlendPipelines m_translucencyIlluminationPipline;
//BlendPipelines m_translucencyIlluminationPiplineNoDepth;

CopyTextureSubpass4 m_copySubpass;

Expand Down
13 changes: 13 additions & 0 deletions HPL2/include/graphics/SceneResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ namespace hpl::resource {
};

struct WaterMaterial {
uint32_t m_diffuseTextureIndex;
uint32_t m_normalTextureIndex;
uint32_t m_cubemapTextureIndex;
uint32_t m_config;

float m_refractionScale;
float m_frenselBias;
float m_frenselPow;
float m_reflectionFadeStart;

float m_reflectionFadeEnd;
float m_waveSpeed;
float m_waveAmplitude;
float m_waveFreq;
};
}
14 changes: 8 additions & 6 deletions HPL2/resource/scene_defs.h.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,22 @@ STRUCT(TranslucentMaterial) {
};

STRUCT(WaterMaterial) {
DATA(uint, diffuseTextureIndex, None);
DATA(uint, normalTextureIndex, None);
DATA(uint, cubeMapTextureIndex, None);
DATA(uint, config, None);

DATA(float, refractionScale, None);
DATA(float, frenselBias, None);
DATA(float, frenselPow, None);

DATA(float, frenselPos, None);
DATA(float, reflectionFadeStart, None);


DATA(float, reflectionFadeEnd, None);
DATA(float, waveSpeed, None);
DATA(float, waveAmplitude, None);

DATA(float, waveFreq, None);
DATA(uint, _pad0, None);
DATA(uint, _pad1, None);
DATA(uint, _pad2, None);

};


Expand Down
4 changes: 2 additions & 2 deletions HPL2/resource/translucency_shade.frag.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ STRUCT(PsIn)
DATA(float3, normal, NORMAL);
DATA(float3, tangent, TANGENT);
DATA(float3, bitangent, BITANGENT);
DATA(float4, color, COLOR);
DATA(float4, Color, COLOR);
};

STRUCT(PsOut)
Expand All @@ -37,7 +37,7 @@ PsOut PS_MAIN(PsIn In, SV_PrimitiveID(uint) primitiveID)

#ifndef USE_ILLUMINATION
SampleSceneTextureFloat4(material.diffuseTextureIndex, Get(sceneSampler), In.uv.xy, finalColor);
finalColor *= In.color;
finalColor *= In.Color;
#endif

float finalAlpha = obj.alphaAmount;
Expand Down
4 changes: 2 additions & 2 deletions HPL2/resource/translucency_shade.vert.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ STRUCT(VSOutput)
DATA(float3, normal, NORMAL);
DATA(float3, tangent, TANGENT);
DATA(float3, bitangent, BITANGENT);
DATA(float4, color, COLOR);
DATA(float4, Color, COLOR);
};

VSOutput VS_MAIN(VSInput In, SV_InstanceID(uint) InstanceID)
Expand All @@ -46,7 +46,7 @@ VSOutput VS_MAIN(VSInput In, SV_InstanceID(uint) InstanceID)
Out.tangent = normalize(mul(normalMat, In.Tangent.xyz));
Out.bitangent = normalize(mul(normalMat, cross(In.Tangent.xyz, In.Normal.xyz)));
Out.Position = mul(modelViewPrj, float4(In.Position.xyz, 1.0));
Out.color = In.Color;
Out.Color = In.Color;

RETURN(Out);
}
Expand Down
126 changes: 49 additions & 77 deletions HPL2/sources/graphics/RendererDeferred2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -910,35 +910,7 @@ namespace hpl {
return true;
});
}
struct {
SharedShader* shader;
SharedPipeline* pipline;
SharedPipeline* piplineNoDepth;
eMaterialBlendMode blendMode;
} translucencyIlluminationPipelineInit[] = {
{ &m_translucencyIlluminationShaderAdd,
&m_translucencyIlluminationPipline.m_pipelineBlendAdd,
&m_translucencyIlluminationPiplineNoDepth.m_pipelineBlendAdd,
eMaterialBlendMode_Add },
{ &m_translucencyIlluminationShaderMul,
&m_translucencyIlluminationPipline.m_pipelineBlendMul,
&m_translucencyIlluminationPiplineNoDepth.m_pipelineBlendMul,
eMaterialBlendMode_Mul },
{ &m_translucencyIlluminationShaderMulX2,
&m_translucencyIlluminationPipline.m_pipelineBlendMulX2,
&m_translucencyIlluminationPiplineNoDepth.m_pipelineBlendMulX2,
eMaterialBlendMode_MulX2 },
{ &m_translucencyIlluminationShaderAlpha,
&m_translucencyIlluminationPipline.m_pipelineBlendAlpha,
&m_translucencyIlluminationPiplineNoDepth.m_pipelineBlendAlpha,
eMaterialBlendMode_Alpha },
{ &m_translucencyIlluminationShaderPremulAlpha,
&m_translucencyIlluminationPipline.m_pipelineBlendPremulAlpha,
&m_translucencyIlluminationPiplineNoDepth.m_pipelineBlendPremulAlpha,
eMaterialBlendMode_PremulAlpha },
};

for (auto& init : translucencyIlluminationPipelineInit) {
{
BlendStateDesc blendStateDesc{};
blendStateDesc.mColorWriteMasks[0] = ColorMask::COLOR_MASK_ALL;
blendStateDesc.mRenderTargetMask = BLEND_STATE_TARGET_0;
Expand All @@ -956,29 +928,29 @@ namespace hpl {
pipelineSettings.mDepthStencilFormat = DepthBufferFormat;
pipelineSettings.pRootSignature = m_sceneRootSignature.m_handle;
pipelineSettings.pVertexLayout = &vertexLayout;
pipelineSettings.pShaderProgram = init.shader->m_handle;
pipelineSettings.pShaderProgram = m_translucencyIlluminationShaderAdd.m_handle;

RasterizerStateDesc rasterizerStateDesc = {};
rasterizerStateDesc.mCullMode = CULL_MODE_FRONT;
rasterizerStateDesc.mFrontFace = FRONT_FACE_CCW;
pipelineSettings.pRasterizerState = &rasterizerStateDesc;

blendStateDesc.mSrcFactors[0] = hpl::HPL2BlendTable[init.blendMode].src;
blendStateDesc.mDstFactors[0] = hpl::HPL2BlendTable[init.blendMode].dst;
blendStateDesc.mBlendModes[0] = hpl::HPL2BlendTable[init.blendMode].mode;
blendStateDesc.mSrcFactors[0] = hpl::HPL2BlendTable[eMaterialBlendMode_Add].src;
blendStateDesc.mDstFactors[0] = hpl::HPL2BlendTable[eMaterialBlendMode_Add].dst;
blendStateDesc.mBlendModes[0] = hpl::HPL2BlendTable[eMaterialBlendMode_Add].mode;

blendStateDesc.mSrcAlphaFactors[0] = hpl::HPL2BlendTable[init.blendMode].srcAlpha;
blendStateDesc.mDstAlphaFactors[0] = hpl::HPL2BlendTable[init.blendMode].dstAlpha;
blendStateDesc.mBlendAlphaModes[0] = hpl::HPL2BlendTable[init.blendMode].alphaMode;
blendStateDesc.mSrcAlphaFactors[0] = hpl::HPL2BlendTable[eMaterialBlendMode_Add].srcAlpha;
blendStateDesc.mDstAlphaFactors[0] = hpl::HPL2BlendTable[eMaterialBlendMode_Add].dstAlpha;
blendStateDesc.mBlendAlphaModes[0] = hpl::HPL2BlendTable[eMaterialBlendMode_Add].alphaMode;

pipelineSettings.pDepthState = &depthStateDesc;
init.pipline->Load(forgeRenderer->Rend(), [&](Pipeline** pipeline) {
m_translucencyIlluminationPipline.Load(forgeRenderer->Rend(), [&](Pipeline** pipeline) {
addPipeline(forgeRenderer->Rend(), &pipelineDesc, pipeline);
return true;
});

pipelineSettings.pDepthState = &noDepthStateDesc;
init.piplineNoDepth->Load(forgeRenderer->Rend(), [&](Pipeline** pipeline) {
m_translucencyIlluminationPiplineNoDepth.Load(forgeRenderer->Rend(), [&](Pipeline** pipeline) {
addPipeline(forgeRenderer->Rend(), &pipelineDesc, pipeline);
return true;
});
Expand Down Expand Up @@ -1742,27 +1714,8 @@ namespace hpl {

auto& resourceMaterial = resolveResourceMaterial(material);
auto& materialSet = resourceMaterial.resolveSet(MaterialSetType::PrimarySet);

const uint32_t slot = frameVars.m_objectIndex++;

BufferUpdateDesc updateDesc = { m_objectUniformBuffer[frame.m_frameIndex].m_handle, sizeof(resource::SceneObject) * slot };
beginUpdateResource(&updateDesc);
auto& uniformObjectData = (*reinterpret_cast<resource::SceneObject*>(updateDesc.pMappedData));
uniformObjectData.m_dissolveAmount = renderable->GetCoverageAmount();
uniformObjectData.m_modelMat = modelMat;
uniformObjectData.m_invModelMat = inverse(modelMat);
uniformObjectData.m_lightLevel = 1.0f;
uniformObjectData.m_alphaAmount = 1.0f;
for (auto& fogArea : fogRenderData) {
uniformObjectData.m_alphaAmount *= detail::GetFogAreaVisibilityForObject(fogArea, *apFrustum, renderable);
}
uniformObjectData.m_illuminationAmount = renderable->GetIlluminationAmount();
uniformObjectData.m_materialIndex = materialSet.m_slot.get();
uniformObjectData.m_materialType = static_cast<uint32_t>(descriptor.m_id);
uniformObjectData.m_vertexOffset = packet.m_unified.m_subAllocation->vertexOffset();
uniformObjectData.m_indexOffset = packet.m_unified.m_subAllocation->indexOffset();
uniformObjectData.m_uvMat = cMath::ToForgeMatrix4(material->GetUvMatrix().GetTranspose());
endUpdateResource(&updateDesc);

if (!materialSet.m_slot.isValid()) {
const auto alphaMapImage = material->GetImage(eMaterialTexture_Alpha);
Expand Down Expand Up @@ -1798,16 +1751,8 @@ namespace hpl {
translucenctMaterial.m_frenselPow = descriptor.m_translucent.m_frenselPow;
translucenctMaterial.m_rimMulLight = descriptor.m_translucent.m_rimLightMul;
translucenctMaterial.m_rimMulPower = descriptor.m_translucent.m_rimLightPow;
translucenctMaterial.m_config = ((alphaMapImage &&
TinyImageFormat_ChannelCount(static_cast<TinyImageFormat>(
alphaMapImage->GetTexture().m_handle->mFormat)) == 1)
? resource::IsAlphaSingleChannel
: 0) |
((heightMapImage &&
TinyImageFormat_ChannelCount(
static_cast<TinyImageFormat>(heightMapImage->GetTexture().m_handle->mFormat)) == 1)
? resource::IsHeightSingleChannel
: 0);
translucenctMaterial.m_config = ((alphaMapImage &&TinyImageFormat_ChannelCount(static_cast<TinyImageFormat>(alphaMapImage->GetTexture().m_handle->mFormat)) == 1) ? resource::IsAlphaSingleChannel : 0) |
((heightMapImage && TinyImageFormat_ChannelCount(static_cast<TinyImageFormat>(heightMapImage->GetTexture().m_handle->mFormat)) == 1) ? resource::IsHeightSingleChannel : 0);
endUpdateResource(&updateDesc);
break;
}
Expand All @@ -1820,8 +1765,36 @@ namespace hpl {
break;
}
}
BufferUpdateDesc updateDesc = { m_objectUniformBuffer[frame.m_frameIndex].m_handle, sizeof(resource::SceneObject) * slot };
beginUpdateResource(&updateDesc);
auto& uniformObjectData = (*reinterpret_cast<resource::SceneObject*>(updateDesc.pMappedData));
uniformObjectData.m_dissolveAmount = renderable->GetCoverageAmount();
uniformObjectData.m_modelMat = modelMat;
uniformObjectData.m_invModelMat = inverse(modelMat);
uniformObjectData.m_lightLevel = 1.0f;
uniformObjectData.m_alphaAmount = 1.0f;
if (material->IsAffectedByLightLevel()) {
uniformObjectData.m_lightLevel = detail::calcLightLevel(m_rendererList.GetLights(), renderable);
}
for (auto& fogArea : fogRenderData) {
uniformObjectData.m_alphaAmount *= detail::GetFogAreaVisibilityForObject(fogArea, *apFrustum, renderable);
}
uniformObjectData.m_illuminationAmount = renderable->GetIlluminationAmount();
uniformObjectData.m_materialIndex = materialSet.m_slot.get();
uniformObjectData.m_materialType = static_cast<uint32_t>(descriptor.m_id);
uniformObjectData.m_vertexOffset = packet.m_unified.m_subAllocation->vertexOffset();
uniformObjectData.m_indexOffset = packet.m_unified.m_subAllocation->indexOffset();
uniformObjectData.m_uvMat = cMath::ToForgeMatrix4(material->GetUvMatrix().GetTranspose());
endUpdateResource(&updateDesc);

translucenctArgs.push_back({
(cubeMap && !isRefraction) ? TranslucentDrawType::TranslucencyIllumination : TranslucentDrawType::Translucency,
([&]() {
if (descriptor.m_id == MaterialID::Water) {
return TranslucentDrawType::Water;
}
return (cubeMap && !isRefraction) ? TranslucentDrawType::TranslucencyIllumination
: TranslucentDrawType::Translucency;
})(),
renderable,
isRefraction,
isReflection,
Expand Down Expand Up @@ -1906,6 +1879,9 @@ namespace hpl {
uniformObjectData.m_modelMat = modelMat;
uniformObjectData.m_invModelMat = inverse(modelMat);
uniformObjectData.m_lightLevel = 1.0f;
if (material->IsAffectedByLightLevel()) {
uniformObjectData.m_lightLevel = detail::calcLightLevel(m_rendererList.GetLights(), renderable);
}
uniformObjectData.m_illuminationAmount = renderable->GetIlluminationAmount();
uniformObjectData.m_materialIndex = materialSet.m_slot.get();
uniformObjectData.m_materialType = static_cast<uint32_t>(material->Descriptor().m_id);
Expand Down Expand Up @@ -2375,18 +2351,15 @@ namespace hpl {

if (arg.m_refraction) {
if (pMaterial->GetDepthTest()) {
cmdBindPipeline(
cmd, m_translucencyRefractionPipline.getPipelineByBlendMode(pMaterial->GetBlendMode()).m_handle);
cmdBindPipeline(cmd, m_translucencyRefractionPipline.getPipelineByBlendMode(pMaterial->GetBlendMode()).m_handle);
} else {
cmdBindPipeline(
cmd, m_translucencyRefractionPiplineNoDepth.getPipelineByBlendMode(pMaterial->GetBlendMode()).m_handle);
cmdBindPipeline(cmd, m_translucencyRefractionPiplineNoDepth.getPipelineByBlendMode(pMaterial->GetBlendMode()).m_handle);
}
} else {
if (pMaterial->GetDepthTest()) {
cmdBindPipeline(cmd, m_translucencyPipline.getPipelineByBlendMode(pMaterial->GetBlendMode()).m_handle);
} else {
cmdBindPipeline(
cmd, m_translucencyPiplineNoDepth.getPipelineByBlendMode(pMaterial->GetBlendMode()).m_handle);
cmdBindPipeline(cmd, m_translucencyPiplineNoDepth.getPipelineByBlendMode(pMaterial->GetBlendMode()).m_handle);
}
}

Expand All @@ -2399,10 +2372,9 @@ namespace hpl {

if (arg.type == TranslucentDrawType::TranslucencyIllumination) {
if (pMaterial->GetDepthTest()) {
cmdBindPipeline(cmd, m_translucencyPipline.getPipelineByBlendMode(pMaterial->GetBlendMode()).m_handle);
cmdBindPipeline(cmd, m_translucencyIlluminationPipline.m_handle);
} else {
cmdBindPipeline(
cmd, m_translucencyPiplineNoDepth.getPipelineByBlendMode(pMaterial->GetBlendMode()).m_handle);
cmdBindPipeline(cmd, m_translucencyIlluminationPiplineNoDepth.m_handle);
}
cmdDrawIndexedInstanced(cmd, arg.numberIndecies, arg.indexOffset, 1, arg.vertexOffset, arg.indirectDrawIndex);
}
Expand Down

0 comments on commit 9b5114d

Please sign in to comment.