Skip to content

Commit

Permalink
Fix bug where texture LODs wouldn't load.
Browse files Browse the repository at this point in the history
  • Loading branch information
Duttenheim committed Aug 24, 2023
1 parent 96fb626 commit 872c189
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 36 deletions.
2 changes: 1 addition & 1 deletion code/render/coregraphics/meshloader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ MeshLoader::MeshLoader()
this->failResourceName = "sysmsh:error.nvx";
this->async = true;

this->streamerThreadName = "Mesh Pool Streamer Thread";
this->streamerThreadName = "Mesh Streamer Thread";

// Setup vertex layouts
CoreGraphics::VertexLayoutCreateInfo vlCreateInfo;
Expand Down
4 changes: 2 additions & 2 deletions code/render/coregraphics/textureloader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ TextureLoader::TextureLoader()
this->placeholderResourceName = "systex:white.dds";
this->failResourceName = "systex:error.dds";

this->streamerThreadName = "Texture Pool Streamer Thread";
this->streamerThreadName = "Texture Streamer Thread";
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -117,7 +117,7 @@ TextureLoader::LoadFromStream(Ids::Id32 entry, const Util::StringAtom& tag, cons
// now load texture by walking through all images and mips
for (int i = 0; i < ctx.num_faces(); i++)
{
for (int j = streamData->lowestLod; j < ctx.num_mipmaps(i); j++)
for (int j = streamData->lowestLod; j < ctx.num_mipmaps(i) - streamData->lowestLod; j++)
{
// Perform a texture update
CoreGraphics::TextureUpdate(
Expand Down
6 changes: 3 additions & 3 deletions code/render/decals/decalcontext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ DecalContext::SetupDecalPBR(
pbrDecalAllocator.Set<DecalPBR_Normal>(decal, normal);
pbrDecalAllocator.Set<DecalPBR_Material>(decal, material);

Resources::SetMaxLOD(albedo, 0.0f, false);
Resources::SetMaxLOD(normal, 0.0f, false);
Resources::SetMaxLOD(material, 0.0f, false);
Resources::SetMinLod(albedo, 0.0f, false);
Resources::SetMinLod(normal, 0.0f, false);
Resources::SetMinLod(material, 0.0f, false);

genericDecalAllocator.Set<Decal_Transform>(cid.id, transform);
genericDecalAllocator.Set<Decal_Type>(cid.id, PBRDecal);
Expand Down
4 changes: 2 additions & 2 deletions code/render/graphics/environmentcontext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ EnvironmentContext::Create(const Graphics::GraphicsEntityId sun)
[](const Resources::ResourceId id)
{
envState.defaultEnvironmentMap = id;
Resources::SetMaxLOD(id, 0.0f, false);
Resources::SetMinLod(id, 0.0f, false);
});

envState.defaultIrradianceMap = Resources::CreateResource("systex:sky_irr.dds"_atm, "system"_atm,
[](const Resources::ResourceId id)
{
envState.defaultIrradianceMap = id;
Resources::SetMaxLOD(id, 0.0f, false);
Resources::SetMinLod(id, 0.0f, false);
});
}

Expand Down
9 changes: 6 additions & 3 deletions code/render/materials/material.cc
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,18 @@ MaterialSetTexture(const MaterialId mat, IndexT name, const CoreGraphics::Textur
void
MaterialAddLODTexture(const MaterialId mat, const Resources::ResourceId tex)
{
Threading::CriticalScope scope(&materialTextureLoadSection);
materialAllocator.Get<Material_LODTextures>(mat.resourceId).Append(tex);
materialAllocator.Set<Material_MinLOD>(mat.resourceId, 1.0f);

// When a new texture is added, make sure to update it's LOD as well
Resources::SetMinLod(tex, materialAllocator.Get<Material_MinLOD>(mat.resourceId), false);
}

//------------------------------------------------------------------------------
/**
*/
void
MaterialSetHighestLod(const MaterialId mat, float lod)
MaterialSetLowestLod(const MaterialId mat, float lod)
{
Threading::CriticalScope scope(&materialTextureLoadSection);
Util::Array<Resources::ResourceId>& textures = materialAllocator.Get<Material_LODTextures>(mat.resourceId);
Expand All @@ -247,7 +250,7 @@ MaterialSetHighestLod(const MaterialId mat, float lod)

for (IndexT i = 0; i < textures.Size(); i++)
{
Resources::SetMaxLOD(textures[i], lod, false);
Resources::SetMinLod(textures[i], lod, false);
}
}

Expand Down
2 changes: 1 addition & 1 deletion code/render/materials/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void MaterialSetTexture(const MaterialId mat, IndexT name, const CoreGraphics::T
/// Add texture to LOD update
void MaterialAddLODTexture(const MaterialId mat, const Resources::ResourceId tex);
/// Update LOD for material
void MaterialSetHighestLod(const MaterialId mat, float lod);
void MaterialSetLowestLod(const MaterialId mat, float lod);

/// Apply material
void MaterialApply(const MaterialId id, const CoreGraphics::CmdBufferId buf, IndexT index);
Expand Down
7 changes: 3 additions & 4 deletions code/render/models/modelcontext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ ModelContext::Setup(
nodeInstances.renderable.origBoundingBoxes.Append(boundingBox);
nodeInstances.renderable.nodeLodDistances.Append(Util::MakeTuple(FLT_MAX, FLT_MAX));
nodeInstances.renderable.nodeLods.Append(0.0f);
nodeInstances.renderable.textureLods.Append(0.0f);
nodeInstances.renderable.textureLods.Append(1.0f);
nodeInstances.renderable.nodeFlags.Append(Models::NodeInstanceFlags::NodeInstance_Active);
nodeInstances.renderable.nodeMaterials.Append(material);
nodeInstances.renderable.nodeShaderConfigs.Append(MaterialGetShaderConfig(material));
Expand Down Expand Up @@ -638,12 +638,12 @@ ModelContext::UpdateTransforms(const Graphics::FrameContext& ctx)
// calculate view vector to calculate LOD
Math::vec4 viewVector = context->cameraTransform.position - transform.position;
float viewDistance = length(viewVector);
float textureLod = viewDistance - 38.5f;
float textureLod = Math::max(0.0f, (viewDistance - 10.0f) / 30.5f);

if (textureLod < nodeInstances.renderable.textureLods[j])
{
// Notify materials system this LOD might be used (this is a bit shitty in comparison to actually using texture sampling feedback)
Materials::MaterialSetHighestLod(nodeInstances.renderable.nodeMaterials[j], textureLod);
Materials::MaterialSetLowestLod(nodeInstances.renderable.nodeMaterials[j], textureLod);
nodeInstances.renderable.textureLods[j] = textureLod;
}

Expand Down Expand Up @@ -714,7 +714,6 @@ ModelContext::UpdateTransforms(const Graphics::FrameContext& ctx)
}, nodeInstanceStateRanges.Size(), 256, renderCtx, { &lodUpdateCounter }, &ConstantsUpdateCounter, &ModelContext::completionEvent);
}


//------------------------------------------------------------------------------
/**
*/
Expand Down
9 changes: 0 additions & 9 deletions code/render/models/nodes/shaderstatenode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,6 @@ ShaderStateNode::~ShaderStateNode()
// empty
}

//------------------------------------------------------------------------------
/**
*/
void
ShaderStateNode::SetMaxLOD(const float lod)
{
Materials::MaterialSetHighestLod(this->material, lod);
}

//------------------------------------------------------------------------------
/**
*/
Expand Down
2 changes: 0 additions & 2 deletions code/render/models/nodes/shaderstatenode.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ class ShaderStateNode : public TransformNode

/// get surface
const Materials::MaterialId GetMaterial() const { return this->material; };
/// trigger an LOD update
void SetMaxLOD(const float lod);

/// Create resource table
static Util::FixedArray<CoreGraphics::ResourceTableId> CreateResourceTables();
Expand Down
4 changes: 2 additions & 2 deletions code/render/terrain/terraincontext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1082,8 +1082,8 @@ TerrainContext::SetupTerrain(

runtimeInfo.decisionMap = Resources::CreateResource(decisionMap, "terrain"_atm, nullptr, nullptr, true);
runtimeInfo.heightMap = Resources::CreateResource(heightMap, "terrain"_atm, nullptr, nullptr, true);
Resources::SetMaxLOD(runtimeInfo.decisionMap, 0.0f, false);
Resources::SetMaxLOD(runtimeInfo.heightMap, 0.0f, false);
Resources::SetMinLod(runtimeInfo.decisionMap, 0.0f, false);
Resources::SetMinLod(runtimeInfo.heightMap, 0.0f, false);

runtimeInfo.worldWidth = terrainState.settings.worldSizeX;
runtimeInfo.worldHeight = terrainState.settings.worldSizeZ;
Expand Down
2 changes: 1 addition & 1 deletion code/resource/resources/resourceloader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ ResourceLoader::ReloadResource(const Resources::ResourceId& id, std::function<vo
/**
*/
void
ResourceLoader::SetMaxLOD(const Resources::ResourceId& id, const float lod, bool immediate)
ResourceLoader::SetMinLod(const Resources::ResourceId& id, const float lod, bool immediate)
{
if (immediate)
{
Expand Down
2 changes: 1 addition & 1 deletion code/resource/resources/resourceloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class ResourceLoader : public Core::RefCounted
void ReloadResource(const Resources::ResourceId& id, std::function<void(const Resources::ResourceId)> success, std::function<void(const Resources::ResourceId)> failed);

/// begin updating a resources lod
void SetMaxLOD(const Resources::ResourceId& id, const float lod, bool immediate);
void SetMinLod(const Resources::ResourceId& id, const float lod, bool immediate);

protected:
friend class ResourceServer;
Expand Down
10 changes: 5 additions & 5 deletions code/resource/resources/resourceserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ResourceServer : public Core::RefCounted
/// reload resource
void ReloadResource(const ResourceName& res, std::function<void(const Resources::ResourceId)> success = nullptr, std::function<void(const Resources::ResourceId)> failed = nullptr);
/// stream in a new LOD
void SetMaxLOD(const ResourceId& id, float lod, bool immediate);
void SetMinLod(const ResourceId& id, float lod, bool immediate);

/// get type of resource pool this resource was allocated with
Core::Rtti* GetType(const Resources::ResourceId id);
Expand Down Expand Up @@ -170,7 +170,7 @@ ResourceServer::ReloadResource(const ResourceName& res, std::function<void(const
/**
*/
inline void
ResourceServer::SetMaxLOD(const ResourceId& id, float lod, bool immediate)
ResourceServer::SetMinLod(const ResourceId& id, float lod, bool immediate)
{
// get id of loader
const Ids::Id8 loaderid = id.cacheIndex;
Expand All @@ -180,7 +180,7 @@ ResourceServer::SetMaxLOD(const ResourceId& id, float lod, bool immediate)
const Ptr<ResourceLoader>& loader = this->loaders[loaderid].downcast<ResourceLoader>();

// update LOD
loader->SetMaxLOD(id, lod, immediate);
loader->SetMinLod(id, lod, immediate);
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -319,9 +319,9 @@ CreateResource(const ResourceName& res, const METADATA& metaData, const Util::St
/**
*/
inline void
SetMaxLOD(const ResourceId& id, float lod, bool immediate)
SetMinLod(const ResourceId& id, float lod, bool immediate)
{
return ResourceServer::Instance()->SetMaxLOD(id, lod, immediate);
return ResourceServer::Instance()->SetMinLod(id, lod, immediate);
}

//------------------------------------------------------------------------------
Expand Down

0 comments on commit 872c189

Please sign in to comment.