Per-stage shader hashing

This commit is contained in:
Jack Andersen 2019-06-05 14:07:50 -10:00
parent 5e60131062
commit b7aa3e06d2
10 changed files with 134 additions and 48 deletions

View File

@ -8,15 +8,18 @@ class CStopwatch {
public:
CStopwatch() : m_start(std::chrono::steady_clock::now()) {}
void report(const char* name) const {
printf("%s %f\n", name,
std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - m_start).count() /
1000000.f);
double report(const char* name) const {
double t = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now() - m_start).count() / 1000000.0;
printf("%s %f\n", name, t);
return t;
}
void reportReset(const char* name) {
double reportReset(const char* name) {
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
printf("%s %f\n", name, std::chrono::duration_cast<std::chrono::microseconds>(now - m_start).count() / 1000000.f);
double t = std::chrono::duration_cast<std::chrono::microseconds>(now - m_start).count() / 1000000.0;
printf("%s %f\n", name, t);
m_start = now;
return t;
}
};
} // namespace urde

View File

@ -4,6 +4,7 @@
#include "Graphics/CLight.hpp"
#include "hecl/HMDLMeta.hpp"
#include "hecl/Runtime.hpp"
#include "hecl/CVarManager.hpp"
#include "boo/graphicsdev/Metal.hpp"
#include "Shaders/CModelShaders.hpp"
#include "Graphics/CBooRenderer.hpp"
@ -372,8 +373,8 @@ CBooModel::ModelInstance* CBooModel::PushNewModelInstance(int sharedLayoutBuf) {
texs[8] = g_disintegrateTexture;
else
texs[8] = g_Renderer->x220_sphereRamp.get();
} else if (idx == EExtendedShader::LightingCubeReflection ||
idx == EExtendedShader::LightingCubeReflectionWorldShadow) {
} else if (hecl::com_cubemaps->toBoolean() && (idx == EExtendedShader::LightingCubeReflection ||
idx == EExtendedShader::LightingCubeReflectionWorldShadow)) {
if (m_lastDrawnReflectionCube)
texs[11] = m_lastDrawnReflectionCube.get();
else
@ -531,8 +532,17 @@ void CBooModel::DrawSurfaces(const CModelFlags& flags) const {
static EExtendedShader ResolveExtendedShader(const MaterialSet::Material& data, const CModelFlags& flags) {
bool noZWrite = flags.m_noZWrite || !data.flags.depthWrite();
/* Ensure cubemap extension shaders fall back to non-cubemap equivalents if necessary */
EExtendedShader intermediateExtended = flags.m_extendedShader;
if (!hecl::com_cubemaps->toBoolean() || g_Renderer->IsThermalVisorHotPass() || g_Renderer->IsThermalVisorActive()) {
if (intermediateExtended == EExtendedShader::LightingCubeReflection)
intermediateExtended = EExtendedShader::Lighting;
else if (intermediateExtended == EExtendedShader::LightingCubeReflectionWorldShadow)
intermediateExtended = EExtendedShader::WorldShadow;
}
EExtendedShader extended = EExtendedShader::Flat;
if (flags.m_extendedShader == EExtendedShader::Lighting) {
if (intermediateExtended == EExtendedShader::Lighting) {
/* Transform lighting into thermal cold if the thermal visor is active */
if (g_Renderer->IsThermalVisorHotPass())
return EExtendedShader::LightingAlphaWrite;
@ -578,8 +588,8 @@ static EExtendedShader ResolveExtendedShader(const MaterialSet::Material& data,
} else {
extended = EExtendedShader::Lighting;
}
} else if (flags.m_extendedShader < EExtendedShader::MAX) {
extended = flags.m_extendedShader;
} else if (intermediateExtended < EExtendedShader::MAX) {
extended = intermediateExtended;
}
return extended;
@ -950,7 +960,7 @@ boo::ObjToken<boo::IGraphicsBufferD> CBooModel::UpdateUniformData(const CModelFl
}
/* Invalidate instances if new reflection cube being drawn */
if ((flags.m_extendedShader == EExtendedShader::LightingCubeReflection ||
if (hecl::com_cubemaps->toBoolean() && (flags.m_extendedShader == EExtendedShader::LightingCubeReflection ||
flags.m_extendedShader == EExtendedShader::LightingCubeReflectionWorldShadow) &&
m_lastDrawnReflectionCube != g_reflectionCube) {
const_cast<CBooModel*>(this)->m_lastDrawnReflectionCube = g_reflectionCube;

View File

@ -1,6 +1,7 @@
#include "CModelShaders.hpp"
#include "Graphics/CLight.hpp"
#include "hecl/Pipeline.hpp"
#include "CStopwatch.hpp"
namespace urde {

View File

@ -65,6 +65,7 @@
namespace hecl {
extern CVar* com_enableCheats;
extern CVar* com_developer;
extern CVar* com_cubemaps;
}; // namespace hecl
namespace urde {

View File

@ -13,6 +13,7 @@
#include "Graphics/CBooRenderer.hpp"
#include "CTimeProvider.hpp"
#include "Graphics/CSkinnedModel.hpp"
#include "hecl/CVarManager.hpp"
namespace urde {
static CMaterialList MakeActorMaterialList(const CMaterialList& materialList, const CActorParameters& params) {
@ -432,11 +433,13 @@ void CActor::_CreateShadow() {
}
void CActor::_CreateReflectionCube() {
if (hecl::com_cubemaps->toBoolean()) {
CGraphics::CommitResources([this](boo::IGraphicsDataFactory::Context& ctx) {
m_reflectionCube = ctx.newCubeRenderTexture(CUBEMAP_RES, CUBEMAP_MIPS);
return true;
} BooTrace);
}
}
void CActor::SetCallTouch(bool callTouch) { xe5_28_callTouch = callTouch; }

View File

@ -14,6 +14,7 @@
#include "Character/CActorLights.hpp"
#include "GuiSys/CStringTable.hpp"
#include "Audio/CSfxManager.hpp"
#include "hecl/CVarManager.hpp"
namespace urde {
@ -234,6 +235,7 @@ void CWorldTransManager::DrawEnabled() {
CActorLights lights(0, zeus::skZero3f, 4, 4, 0, 0, 0, 0.1f);
lights.BuildFakeLightList(x4_modelData->x1a0_lights, zeus::CColor{0.1f, 0.1f, 0.1f, 1.0f});
if (m_reflectionCube[0]) {
SViewport backupVp = g_Viewport;
constexpr float width = CUBEMAP_RES;
CGraphics::g_BooMainCommandQueue->setRenderTarget(m_reflectionCube[0], 0);
@ -267,6 +269,7 @@ void CWorldTransManager::DrawEnabled() {
CBooRenderer::BindMainDrawTarget();
g_Renderer->SetViewport(backupVp.x0_left, backupVp.x4_top, backupVp.x8_width, backupVp.xc_height);
}
float wsAspect = CWideScreenFilter::SetViewportToMatch(1.f);
@ -378,7 +381,7 @@ void CWorldTransManager::EnableTransition(const CAnimRes& samusRes, CAssetId pla
x30_type = ETransType::Enabled;
x4_modelData.reset(new SModelDatas(samusRes));
if (!m_reflectionCube[0])
if (!m_reflectionCube[0] && hecl::com_cubemaps->toBoolean())
CGraphics::CommitResources([this](boo::IGraphicsDataFactory::Context& ctx) {
m_reflectionCube[0] = ctx.newCubeRenderTexture(CUBEMAP_RES, CUBEMAP_MIPS);
m_reflectionCube[1] = ctx.newCubeRenderTexture(CUBEMAP_RES, CUBEMAP_MIPS);

View File

@ -66,7 +66,7 @@ public:
const boo::AdditionalPipelineInfo PipelineInfo;
bool HasTessellation;
static constexpr bool HasHash = false;
static constexpr uint64_t Hash() { return 0; }
static constexpr bool HasStageHash = false;
const SFluidPlaneShaderInfo& info() const { return m_info; }
};
@ -93,7 +93,7 @@ public:
const boo::VertexFormatInfo VtxFmt;
const boo::AdditionalPipelineInfo PipelineInfo;
static constexpr bool HasHash = false;
static constexpr uint64_t Hash() { return 0; }
static constexpr bool HasStageHash = false;
const SFluidPlaneDoorShaderInfo& info() const { return m_info; }
};

View File

@ -32,9 +32,14 @@ struct SModelShadersInfo {
class Shader_CModelShaders : public hecl::GeneralShader {
const SModelShadersInfo& m_info;
uint64_t m_vertHash, m_fragHash;
static uint64_t BuildVertHash(const SModelShadersInfo& in);
static uint64_t BuildFragHash(const SModelShadersInfo& in);
public:
Shader_CModelShaders(const SModelShadersInfo& in)
: m_info(in)
, m_vertHash(BuildVertHash(in))
, m_fragHash(BuildFragHash(in))
, VtxFmt(in.m_vtxFmt)
, PipelineInfo(in.m_additionalInfo) {}
@ -42,8 +47,12 @@ public:
const boo::AdditionalPipelineInfo PipelineInfo;
static constexpr bool HasHash = true;
uint64_t Hash() const { return m_info.m_hash; }
static constexpr bool HasStageHash = true;
template <typename S> uint64_t StageHash() const;
const SModelShadersInfo& info() const { return m_info; }
};
template <> inline uint64_t Shader_CModelShaders::StageHash<hecl::PipelineStage::Vertex>() const { return m_vertHash; }
template <> inline uint64_t Shader_CModelShaders::StageHash<hecl::PipelineStage::Fragment>() const { return m_fragHash; }
template <typename P, typename S>
class StageObject_CModelShaders : public hecl::StageBinary<P, S> {

View File

@ -141,6 +141,39 @@ static std::string _BuildVS(const SModelShadersInfo& info) {
return vertOut.str();
}
template <typename T>
static void _Hash(XXH64_state_t& st, T val) {
XXH64_update(&st, &val, sizeof(val));
}
uint64_t Shader_CModelShaders::BuildVertHash(const SModelShadersInfo& info) {
XXH64_state_t st;
XXH64_reset(&st, 0);
_Hash(st, info.m_tag.getColorCount());
_Hash(st, info.m_tag.getUvCount());
_Hash(st, info.m_tag.getSkinSlotCount());
_Hash(st, info.m_tag.getWeightCount());
for (const auto& chunk : info.m_material.chunks) {
if (auto passChunk = chunk.get_if<SModelShadersInfo::Material::PASS>()) {
_Hash(st, passChunk->type);
_Hash(st, passChunk->uvAnimType);
_Hash(st, passChunk->source);
_Hash(st, passChunk->shouldNormalizeUv());
} else if (auto clrChunk = chunk.get_if<SModelShadersInfo::Material::CLR>()) {
_Hash(st, clrChunk->type);
}
}
_Hash(st, info.m_extension.noReflection);
_Hash(st, info.m_tag.getReflectionType());
for (size_t i = 0; i < info.m_extension.texCount; ++i) {
const auto& extTex = info.m_extension.texs[i];
_Hash(st, extTex.mtxIdx);
_Hash(st, extTex.src);
_Hash(st, extTex.normalize);
}
return XXH64_digest(&st);
}
static std::string _BuildFS(const SModelShadersInfo& info) {
std::stringstream fragOut;
fragOut << CMODELSHADERS_COMMON_GLSL_SV;
@ -197,6 +230,29 @@ static std::string _BuildFS(const SModelShadersInfo& info) {
return fragOut.str();
}
uint64_t Shader_CModelShaders::BuildFragHash(const SModelShadersInfo& info) {
XXH64_state_t st;
XXH64_reset(&st, 0);
XXH64_update(&st, info.m_extension.shaderMacro, strlen(info.m_extension.shaderMacro));
_Hash(st, info.m_material.shaderType);
_Hash(st, info.m_additionalInfo.srcFac);
_Hash(st, info.m_additionalInfo.dstFac);
for (const auto& chunk : info.m_material.chunks) {
if (auto passChunk = chunk.get_if<SModelShadersInfo::Material::PASS>()) {
_Hash(st, passChunk->alpha);
_Hash(st, passChunk->type);
} else if (auto clrChunk = chunk.get_if<SModelShadersInfo::Material::CLR>()) {
_Hash(st, clrChunk->type);
}
}
_Hash(st, info.m_tag.getAlphaTest());
_Hash(st, info.m_extension.forceAlphaTest);
_Hash(st, info.m_extension.diffuseOnly);
_Hash(st, info.m_extension.noReflection);
_Hash(st, info.m_tag.getReflectionType());
return XXH64_digest(&st);
}
template <>
std::string StageObject_CModelShaders<hecl::PlatformType::OpenGL, hecl::PipelineStage::Vertex>::BuildShader(
const SModelShadersInfo& in) {

2
hecl

@ -1 +1 @@
Subproject commit b435752ebb2238cd103257d1d05cd827416036df
Subproject commit 3cf0712f03b8be7d29b151a36a53d5ece415cab8