Skip to content

Commit a38c437

Browse files
use maxMip+1 instead
fix bugs during virtual texture `free`
1 parent 7eae24c commit a38c437

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

include/nbl/asset/utils/ICPUVirtualTexture.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,11 +457,10 @@ class ICPUVirtualTexture final : public IVirtualTexture<ICPUImageView, ICPUSampl
457457
const E_FORMAT format = getFormatInLayer(_addr.pgTab_layer);
458458
ICPUVTResidentStorage* storage = static_cast<ICPUVTResidentStorage*>(getStorageForFormatClass(getFormatClass(format)));
459459
if (!storage)
460-
return nullptr;
460+
return false;
461461

462462
//free physical pages
463463
VkExtent3D extent = {static_cast<uint32_t>(_addr.origsize_x), static_cast<uint32_t>(_addr.origsize_y), 1u};
464-
const uint32_t levelCount = _addr.maxMip+1u;
465464

466465
#ifdef _NBL_DEBUG
467466
CFillImageFilter::state_type fill;
@@ -478,7 +477,7 @@ class ICPUVirtualTexture final : public IVirtualTexture<ICPUImageView, ICPUSampl
478477
std::fill(m_addrsArray->begin(), m_addrsArray->end(), IVTResidentStorage::phys_pg_addr_alctr_t::invalid_address);
479478

480479
auto* const bufptr = reinterpret_cast<uint8_t*>(m_pageTable->getBuffer()->getPointer());
481-
for (uint32_t i = 0u; i < levelCount; ++i)
480+
for (uint32_t i=0u; i<core::max(_addr.maxMip,1u); ++i)
482481
{
483482
const uint32_t w = neededPageCountForSide(extent.width, i);
484483
const uint32_t h = neededPageCountForSide(extent.height, i);

include/nbl/asset/utils/IVirtualTexture.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class IVirtualTexture : public core::IReferenceCounted, public IVirtualTextureBa
139139
uint64_t pgTab_x : 8;
140140
uint64_t pgTab_y : 8;
141141
uint64_t pgTab_layer : 8;
142-
uint64_t maxMip : 4; // value 0x0fu means, the texture takes only 1 page of physical storage (miptail)
142+
uint64_t maxMip : 4; // this is number of mip-maps plus 1 that the texture will have in the virtual texture (before the mip-tail)
143143
uint64_t wrap_x : 2;
144144
uint64_t wrap_y : 2;
145145

@@ -210,8 +210,8 @@ class IVirtualTexture : public core::IReferenceCounted, public IVirtualTextureBa
210210
texData.pgTab_y = _offset.y;
211211
texData.pgTab_layer = _offset.z;
212212

213-
const uint32_t maxMip = _mipCount - 1u - m_pgSzxy_log2;
214-
assert(static_cast<int32_t>(maxMip) >= -1); // only textures of size at least half page size must be packed
213+
const uint32_t maxMip = _mipCount-m_pgSzxy_log2;
214+
assert(static_cast<int32_t>(maxMip) >= 0); // only textures of size at least half page size must be packed
215215
texData.maxMip = maxMip;
216216

217217
texData.wrap_x = SMasterTextureData::ETC_to_EWM(_wrapu);
@@ -720,7 +720,7 @@ class IVirtualTexture : public core::IReferenceCounted, public IVirtualTextureBa
720720

721721
bool validateAliasCreation(const SMasterTextureData& _addr, E_FORMAT _viewingFormat, const IImage::SSubresourceRange& _subresRelativeToMaster)
722722
{
723-
if (_subresRelativeToMaster.baseMipLevel+_subresRelativeToMaster.levelCount > _addr.maxMip+1u)
723+
if (_subresRelativeToMaster.baseMipLevel+_subresRelativeToMaster.levelCount > _addr.maxMip)
724724
return false;
725725
return true;
726726
}

include/nbl/builtin/glsl/virtual_texturing/impl_functions.glsl

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ uvec2 nbl_glsl_unpackWrapModes(in uvec2 texData)
2626
{
2727
return (texData >> uvec2(28u, 30u))& uvec2(0x03u);
2828
}
29-
int nbl_glsl_unpackMaxMipInVT(in uvec2 texData)
29+
uint nbl_glsl_unpackMaxMipInVT(in uvec2 texData)
3030
{
31-
uint mm = (texData.y >> 24) & 0x0fu;
32-
return mm == 0x0fu ? -1 : int(mm);
31+
return bitfieldExtract(texData.y,24,4);
3332
}
3433
vec3 nbl_glsl_unpackVirtualUV(in uvec2 texData)
3534
{
@@ -87,7 +86,7 @@ vec3 nbl_glsl_vTexture_helper(in uint formatID, in vec3 virtualUV, in int clippe
8786

8887
#if _NBL_VT_FLOAT_VIEWS_COUNT
8988
// textureGrad emulation
90-
vec4 nbl_glsl_vTextureGrad_impl(in uint formatID, in vec3 virtualUV, in mat2 dOriginalScaledUV, in int originalMaxFullMip)
89+
vec4 nbl_glsl_vTextureGrad_impl(in uint formatID, in vec3 virtualUV, in mat2 dOriginalScaledUV, in uint originalMaxFullMip)
9190
{
9291
// returns what would have been `textureGrad(originalTexture,gOriginalUV[0],gOriginalUV[1])
9392
// https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/chap15.html#textures-normalized-operations
@@ -119,15 +118,15 @@ vec4 nbl_glsl_vTextureGrad_impl(in uint formatID, in vec3 virtualUV, in mat2 dOr
119118
// are we performing minification
120119
bool positiveLoD = LoD>0.0;
121120
// magnification samples LoD 0, else clip to max representable in VT
122-
int clippedLoD = positiveLoD ? min(LoD_high,originalMaxFullMip):0; // originalMaxFullMip is always -1 in case of no miplevel taking at least 1 full page
123-
clippedLoD = originalMaxFullMip < 0 ? 0 : clippedLoD;
121+
int clippedLoD = positiveLoD ? min(LoD_high,int(originalMaxFullMip)-1):0; // originalMaxFullMip is always -1 in case of no miplevel taking at least 1 full page
122+
clippedLoD = originalMaxFullMip<1u ? 0 : clippedLoD;
124123

125124
// if minification is being performaed then get tail position
126125
int levelInTail = LoD_high-clippedLoD;
127126
// have to do trilinear only if doing minification AND larger than 1x1 footprint
128127
bool haveToDoTrilinear = levelInTail<int(_NBL_VT_IMPL_PAGE_SZ_LOG2) && positiveLoD;
129128
levelInTail = haveToDoTrilinear ? levelInTail:(positiveLoD ? int(_NBL_VT_IMPL_PAGE_SZ_LOG2):0);
130-
levelInTail += (originalMaxFullMip < 0) ? 1 : 0;
129+
levelInTail += originalMaxFullMip<1u ? 1 : 0;
131130

132131
// get the higher resolution mip-map level
133132
vec3 hiPhysCoord = nbl_glsl_vTexture_helper(formatID,virtualUV,clippedLoD,levelInTail);
@@ -136,7 +135,7 @@ vec4 nbl_glsl_vTextureGrad_impl(in uint formatID, in vec3 virtualUV, in mat2 dOr
136135
// speculative if (haveToDoTrilinear)
137136
{
138137
// now we have absolute guarantees that both LoD_high and LoD_low are in the valid original mip range
139-
bool highNotInLastFull = originalMaxFullMip>=0 && LoD_high<originalMaxFullMip;
138+
bool highNotInLastFull = originalMaxFullMip>=1u && LoD_high<(int(originalMaxFullMip)-1);
140139
clippedLoD = highNotInLastFull ? (clippedLoD+1):clippedLoD;
141140
levelInTail = highNotInLastFull ? levelInTail:(levelInTail+1);
142141
levelInTail = min(levelInTail, int(_NBL_VT_IMPL_PAGE_SZ_LOG2));
@@ -193,7 +192,7 @@ vec4 nbl_glsl_vTextureGrad(in uvec2 _texData, in vec2 uv, in mat2 dUV)
193192
#endif //_NBL_VT_FLOAT_VIEWS_COUNT
194193

195194
#if _NBL_VT_INT_VIEWS_COUNT
196-
ivec4 nbl_glsl_iVTextureLod_impl(in uint formatID, in vec3 virtualUV, in uint lod, in int originalMaxFullMip)
195+
ivec4 nbl_glsl_iVTextureLod_impl(in uint formatID, in vec3 virtualUV, in uint lod, in uint originalMaxFullMip)
197196
{
198197
int nonnegativeLod = int(lod);
199198
int clippedLoD = min(nonnegativeLod,originalMaxFullMip);
@@ -235,7 +234,7 @@ ivec4 nbl_glsl_iVTextureLod(in uvec2 _texData, in vec2 uv, in uint lod)
235234
#endif //_NBL_VT_INT_VIEWS_COUNT
236235

237236
#if _NBL_VT_UINT_VIEWS_COUNT
238-
uvec4 nbl_glsl_uVTextureLod_impl(in uint formatID, in vec3 virtualUV, in uint lod, in int originalMaxFullMip)
237+
uvec4 nbl_glsl_uVTextureLod_impl(in uint formatID, in vec3 virtualUV, in uint lod, in uint originalMaxFullMip)
239238
{
240239
int nonnegativeLod = int(lod);
241240
int clippedLoD = min(nonnegativeLod,originalMaxFullMip);

0 commit comments

Comments
 (0)