Skip to content

Commit 4188226

Browse files
committed
added texture resolution limiting setting, lua platform helpers
1 parent 448b0f1 commit 4188226

7 files changed

Lines changed: 164 additions & 18 deletions

File tree

Content/Documentation/ScriptingAPI-Documentation.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ These are some helpers to aid in debugging:
137137
- ReturnToEditor() -- returns control to the editor and kills running scripts
138138
- IsThisDebugBuild() : bool -- returns true if this is a debug build, false otherwise
139139

140+
Helpers to determine the current platform, if need to implement specific functionality:
141+
- IsPlatformWindows() : bool
142+
- IsPlatformLinux() : bool
143+
- IsPlatformMACOS() : bool
144+
- IsPlatformIOS() : bool
145+
- IsPlatformPS5() : bool
146+
- IsPlatformXBOX() : bool
147+
140148
## Engine Bindings
141149
The scripting API provides functions for the developer to manipulate engine behaviour or query it for information.
142150

@@ -518,6 +526,11 @@ A texture image data.
518526
float edge_smoothness = 0.04) -- creates a lens distortion normal map (16-bit precision)
519527
- Save(string filename) -- saves texture into a file. Provide the extension in the filename, it should be one of the following: .JPG, .PNG, .TGA, .BMP, .DDS
520528

529+
- SetTextureResolutionLimit(int resolution) -- Set the highest allowed texture asset resolution (only for DDS textures that contain mipmaps)
530+
- GetTextureResolutionLimit() : int
531+
- SetTextureResolutionLimit(float threshold) -- Set threshold relative to memory budget for streaming. If memory usage is below threshold, streaming will work regularly.If memory usage is above threshold, streaming will try to reduce usage
532+
- GetTextureResolutionLimit() : float
533+
521534
```lua
522535
GradientType = {
523536
Linear = 0,

WickedEngine/wiLua.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "wiTimer.h"
3030
#include "wiVector.h"
3131
#include "wiVersion.h"
32+
#include "wiPlatform.h"
3233

3334
#include <memory>
3435

@@ -72,6 +73,61 @@ namespace wi::lua
7273
return 0;
7374
}
7475

76+
int IsPlatformWindows(lua_State* L)
77+
{
78+
#ifdef PLATFORM_WINDOWS_DESKTOP
79+
wi::lua::SSetBool(L, true);
80+
#else
81+
wi::lua::SSetBool(L, false);
82+
#endif // PLATFORM_WINDOWS_DESKTOP
83+
return 1;
84+
}
85+
int IsPlatformLinux(lua_State* L)
86+
{
87+
#ifdef PLATFORM_LINUX
88+
wi::lua::SSetBool(L, true);
89+
#else
90+
wi::lua::SSetBool(L, false);
91+
#endif // PLATFORM_LINUX
92+
return 1;
93+
}
94+
int IsPlatformMACOS(lua_State* L)
95+
{
96+
#ifdef PLATFORM_MACOS
97+
wi::lua::SSetBool(L, true);
98+
#else
99+
wi::lua::SSetBool(L, false);
100+
#endif // PLATFORM_MACOS
101+
return 1;
102+
}
103+
int IsPlatformIOS(lua_State* L)
104+
{
105+
#ifdef PLATFORM_IOS
106+
wi::lua::SSetBool(L, true);
107+
#else
108+
wi::lua::SSetBool(L, false);
109+
#endif // PLATFORM_IOS
110+
return 1;
111+
}
112+
int IsPlatformPS5(lua_State* L)
113+
{
114+
#ifdef PLATFORM_PS5
115+
wi::lua::SSetBool(L, true);
116+
#else
117+
wi::lua::SSetBool(L, false);
118+
#endif // PLATFORM_PS5
119+
return 1;
120+
}
121+
int IsPlatformXBOX(lua_State* L)
122+
{
123+
#ifdef PLATFORM_XBOX
124+
wi::lua::SSetBool(L, true);
125+
#else
126+
wi::lua::SSetBool(L, false);
127+
#endif // PLATFORM_XBOX
128+
return 1;
129+
}
130+
75131
void PostErrorMsg(lua_State* L)
76132
{
77133
const char* str = lua_tostring(L, -1);
@@ -289,6 +345,13 @@ namespace wi::lua
289345
RegisterFunc("GetCreditsString", GetCreditsString);
290346
RegisterFunc("GetSupportersString", GetSupportersString);
291347

348+
RegisterFunc("IsPlatformWindows", IsPlatformWindows);
349+
RegisterFunc("IsPlatformLinux", IsPlatformLinux);
350+
RegisterFunc("IsPlatformMACOS", IsPlatformMACOS);
351+
RegisterFunc("IsPlatformIOS", IsPlatformIOS);
352+
RegisterFunc("IsPlatformPS5", IsPlatformPS5);
353+
RegisterFunc("IsPlatformXBOX", IsPlatformXBOX);
354+
292355
Vector_BindLua::Bind();
293356
Matrix_BindLua::Bind();
294357
Application_BindLua::Bind();

WickedEngine/wiResourceManager.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ namespace wi
4040
//static constexpr size_t streaming_texture_min_size = 4096; // 4KB is the minimum texture memory alignment
4141
static constexpr size_t streaming_texture_min_size = 64 * 1024; // 64KB is the usual texture memory alignment, this allows higher base tex size than 4KB
4242

43+
// Texture resolution limit
44+
static uint32_t max_texture_resolution = ~0u;
45+
4346
struct ResourceInternal
4447
{
4548
resourcemanager::Flags flags = resourcemanager::Flags::NONE;
@@ -524,7 +527,20 @@ namespace wi
524527
}
525528
}
526529

530+
527531
int mip_offset = 0;
532+
533+
// mipmap reduction for resolution limit:
534+
while (desc.width > max_texture_resolution && desc.height > max_texture_resolution && desc.mip_levels > 1)
535+
{
536+
desc.width = std::max(desc.width >> 1, 1u);
537+
desc.height = std::max(desc.height >> 1, 1u);
538+
desc.depth = std::max(desc.depth >> 1, 1u);
539+
desc.mip_levels -= 1;
540+
mip_offset++;
541+
}
542+
543+
// mipmap reduction for streaming resources:
528544
if (has_flag(flags, Flags::STREAMING))
529545
{
530546
// Remember full mipcount for streaming:
@@ -543,8 +559,8 @@ namespace wi
543559
// Reduce mip map count that will be uploaded to GPU:
544560
while (desc.mip_levels > 1 && desc.depth == 1 && desc.array_size == 1 && ComputeTextureMemorySizeInBytes(desc) > streaming_texture_min_size)
545561
{
546-
desc.width >>= 1;
547-
desc.height >>= 1;
562+
desc.width = std::max(desc.width >> 1, 1u);
563+
desc.height = std::max(desc.height >> 1, 1u);
548564
desc.mip_levels -= 1;
549565
mip_offset++;
550566
}
@@ -1405,6 +1421,15 @@ namespace wi
14051421
}
14061422
}
14071423

1424+
void SetTextureResolutionLimit(uint32_t resolution)
1425+
{
1426+
max_texture_resolution = resolution;
1427+
}
1428+
uint32_t GetTextureResolutionLimit()
1429+
{
1430+
return max_texture_resolution;
1431+
}
1432+
14081433
void Serialize_READ(wi::Archive& archive, ResourceSerializer& seri)
14091434
{
14101435
assert(archive.IsReadMode());

WickedEngine/wiResourceManager.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ namespace wi
101101
// Invalidate all resources
102102
void Clear();
103103

104+
// Set the highest allowed texture asset resolution (only for DDS textures that contain mipmaps)
105+
void SetTextureResolutionLimit(uint32_t resolution);
106+
uint32_t GetTextureResolutionLimit();
107+
104108
// Set threshold relative to memory budget for streaming
105109
// If memory usage is below threshold, streaming will work regularly
106110
// If memory usage is above threshold, streaming will try to reduce usage

WickedEngine/wiTerrain.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@
2323
#define NOSPARSE
2424
#endif // __APPLE__
2525

26+
// Logical texture resolution limits:
27+
const uint32_t min_virtual_resolution = SVT_TILE_SIZE;
28+
const uint32_t max_virtual_resolution = 65536u;
29+
30+
#ifdef NOSPARSE
31+
// Try to account for memory increase in no-sparse mode, reduce atlas size:
32+
const uint32_t atlas_physical_width = 16384u;
33+
const uint32_t atlas_physical_height = 8192u;
34+
#else
35+
const uint32_t atlas_physical_width = 16384u;
36+
const uint32_t atlas_physical_height = 16384u;
37+
#endif // NOSPARSE
38+
2639
using namespace wi::ecs;
2740
using namespace wi::scene;
2841
using namespace wi::graphics;
@@ -1570,21 +1583,13 @@ namespace wi::terrain
15701583

15711584
if (!atlas.IsValid())
15721585
{
1573-
#ifdef NOSPARSE
1574-
// Try to account for memory increase in no-sparse mode, reduce atlas size:
1575-
const uint32_t physical_width = 16384u;
1576-
const uint32_t physical_height = 8192u;
1577-
#else
1578-
const uint32_t physical_width = 16384u;
1579-
const uint32_t physical_height = 16384u;
1580-
#endif // NOSPARSE
15811586
GPUBufferDesc tile_pool_desc;
15821587

15831588
for (uint32_t map_type = 0; map_type < arraysize(atlas.maps); ++map_type)
15841589
{
15851590
TextureDesc desc;
1586-
desc.width = physical_width;
1587-
desc.height = physical_height;
1591+
desc.width = atlas_physical_width;
1592+
desc.height = atlas_physical_height;
15881593
#ifndef NOSPARSE
15891594
desc.misc_flags = ResourceMiscFlag::SPARSE;
15901595
#endif // NOSPARSE
@@ -1649,8 +1654,8 @@ namespace wi::terrain
16491654
assert(success);
16501655
#endif // NOSPARSE
16511656

1652-
atlas.physical_tile_count_x = uint8_t(physical_width / SVT_TILE_SIZE_PADDED);
1653-
atlas.physical_tile_count_y = uint8_t(physical_height / SVT_TILE_SIZE_PADDED);
1657+
atlas.physical_tile_count_x = uint8_t(atlas_physical_width / SVT_TILE_SIZE_PADDED);
1658+
atlas.physical_tile_count_y = uint8_t(atlas_physical_height / SVT_TILE_SIZE_PADDED);
16541659
atlas.physical_tiles.resize(size_t(atlas.physical_tile_count_x) * size_t(atlas.physical_tile_count_y));
16551660

16561661
uint64_t init_frames = 0;
@@ -1703,9 +1708,7 @@ namespace wi::terrain
17031708
}
17041709
VirtualTexture& vt = *chunk_data.vt;
17051710

1706-
const uint32_t min_resolution = SVT_TILE_SIZE;
1707-
const uint32_t max_resolution = 65536u;
1708-
const uint32_t required_resolution = dist < 2 ? max_resolution : min_resolution;
1711+
const uint32_t required_resolution = dist < 2 ? max_virtual_resolution : min_virtual_resolution;
17091712
//const uint32_t required_resolution = std::max(min_resolution, max_resolution >> std::min(7, std::max(0, dist - 1)));
17101713

17111714
if (vt.resolution != required_resolution)

WickedEngine/wiTexture_BindLua.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,40 @@ namespace wi::lua
250250
return 1;
251251
}
252252

253+
254+
int SetTextureResolutionLimit(lua_State* L)
255+
{
256+
int argc = wi::lua::SGetArgCount(L);
257+
if (argc < 1)
258+
{
259+
wi::lua::SError(L, "SetTextureResolutionLimit(int resolution) missing argument!");
260+
return 0;
261+
}
262+
wi::resourcemanager::SetTextureResolutionLimit(wi::lua::SGetInt(L, 1));
263+
return 0;
264+
}
265+
int GetTextureResolutionLimit(lua_State* L)
266+
{
267+
wi::lua::SSetInt(L, wi::resourcemanager::GetTextureResolutionLimit());
268+
return 1;
269+
}
270+
int SetStreamingMemoryThreshold(lua_State* L)
271+
{
272+
int argc = wi::lua::SGetArgCount(L);
273+
if (argc < 1)
274+
{
275+
wi::lua::SError(L, "SetStreamingMemoryThreshold(float threshold) missing argument!");
276+
return 0;
277+
}
278+
wi::resourcemanager::SetStreamingMemoryThreshold(wi::lua::SGetFloat(L, 1));
279+
return 0;
280+
}
281+
int GetStreamingMemoryThreshold(lua_State* L)
282+
{
283+
wi::lua::SSetFloat(L, wi::resourcemanager::GetStreamingMemoryThreshold());
284+
return 1;
285+
}
286+
253287
void Texture_BindLua::Bind()
254288
{
255289
static bool initialized = false;
@@ -258,6 +292,10 @@ namespace wi::lua
258292
initialized = true;
259293
Luna<Texture_BindLua>::Register(wi::lua::GetLuaState());
260294
Luna<Texture_BindLua>::push_global(wi::lua::GetLuaState(), "texturehelper");
295+
wi::lua::RegisterFunc("SetTextureResolutionLimit", SetTextureResolutionLimit);
296+
wi::lua::RegisterFunc("GetTextureResolutionLimit", GetTextureResolutionLimit);
297+
wi::lua::RegisterFunc("SetTextureResolutionLimit", SetTextureResolutionLimit);
298+
wi::lua::RegisterFunc("GetTextureResolutionLimit", GetTextureResolutionLimit);
261299
wi::lua::RunText(R"(
262300
GradientType = {
263301
Linear = 0,

WickedEngine/wiVersion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace wi::version
99
// minor features, major updates, breaking compatibility changes
1010
const int minor = 72;
1111
// minor bug fixes, alterations, refactors, updates
12-
const int revision = 73;
12+
const int revision = 74;
1313

1414
const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision);
1515

0 commit comments

Comments
 (0)