Skip to content

Commit

Permalink
Only enable resource tracking in Debug builds
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Aug 6, 2024
1 parent 359a0b8 commit 8ad4466
Show file tree
Hide file tree
Showing 23 changed files with 280 additions and 58 deletions.
5 changes: 5 additions & 0 deletions CMake/functions/devilutionx_library.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ function(add_devilutionx_library NAME)
target_link_libraries(${NAME} PUBLIC "$<${ASAN_GENEX}:-fsanitize=address;-fsanitize-recover=address>")
endif()

if(DEVILUTIONX_RESOURCE_TRACKING_ENABLED)
genex_for_option(DEVILUTIONX_RESOURCE_TRACKING_ENABLED)
target_compile_definitions(${NAME} PUBLIC "$<${DEVILUTIONX_RESOURCE_TRACKING_ENABLED_GENEX}:DEVILUTIONX_RESOURCE_TRACKING_ENABLED>")
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
genex_for_option(DEVILUTIONX_STATIC_CXX_STDLIB)
target_link_libraries(${NAME} PUBLIC $<${DEVILUTIONX_STATIC_CXX_STDLIB_GENEX}:-static-libgcc;-static-libstdc++>)
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ DEBUG_OPTION(ASAN "Enable address sanitizer")
DEBUG_OPTION(UBSAN "Enable undefined behaviour sanitizer")
option(TSAN "Enable thread sanitizer (not compatible with ASAN=ON)" OFF)
DEBUG_OPTION(DEBUG "Enable debug mode in engine")
DEBUG_OPTION(DEVILUTIONX_RESOURCE_TRACKING_ENABLED "Enable resource tracking")
option(GPERF "Build with GPerfTools profiler" OFF)
cmake_dependent_option(GPERF_HEAP_FIRST_GAME_ITERATION "Save heap profile of the first game iteration" OFF "GPERF" OFF)
option(ENABLE_CODECOVERAGE "Instrument code for code coverage (only enabled with BUILD_TESTING)" OFF)
Expand Down
14 changes: 12 additions & 2 deletions Source/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,14 +495,24 @@ void CreateHalfSizeItemSprites()
const Surface halfSurface = ownedHalfSurface.subregion(0, 0, itemSurface.w() / 2, itemSurface.h() / 2);
SDL_Rect halfSurfaceRect = MakeSdlRect(0, 0, halfSurface.w(), halfSurface.h());
SDL_SetClipRect(halfSurface.surface, &halfSurfaceRect);
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
std::string name = StrCat("runtime\\objcurs_half_size\\", cursId);
#endif
BilinearDownscaleByHalf8(itemSurface.surface, paletteTransparencyLookup, halfSurface.surface, 1);
HalfSizeItemSprites[outputIndex].emplace(SurfaceToClx(std::string(name), /*trnName=*/ {}, halfSurface, 1, 1));
HalfSizeItemSprites[outputIndex].emplace(SurfaceToClx(
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
std::string(name), /*trnName=*/ {},
#endif
halfSurface, 1, 1));

SDL_FillRect(itemSurface.surface, nullptr, 1);
ClxDrawTRN(itemSurface, { 0, itemSurface.h() }, itemSprite, redTrn);
BilinearDownscaleByHalf8(itemSurface.surface, paletteTransparencyLookup, halfSurface.surface, 1);
HalfSizeItemSpritesRed[outputIndex].emplace(SurfaceToClx(std::move(name), /*trnName=*/"red", halfSurface, 1, 1));
HalfSizeItemSpritesRed[outputIndex].emplace(SurfaceToClx(
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
std::move(name), /*trnName=*/"red",
#endif
halfSurface, 1, 1));
};

size_t outputIndex = 0;
Expand Down
178 changes: 145 additions & 33 deletions Source/engine/clx_sprite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ class ClxSpriteList {

ClxSpriteList(const OwnedClxSpriteList &owned);

[[nodiscard]] OwnedClxSpriteList clone(std::string_view name, std::string_view trnName = {}) const;
[[nodiscard]] OwnedClxSpriteList clone(
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
std::string_view name, std::string_view trnName = {}
#endif
) const;

[[nodiscard]] constexpr uint32_t numSprites() const
{
Expand Down Expand Up @@ -336,8 +340,13 @@ class OwnedClxSpriteListOrSheet;
/**
* @brief Implicitly convertible to `ClxSpriteList` and owns its data.
*/
class OwnedClxSpriteList : public OwnedResource<OwnedClxSpriteList> {
class OwnedClxSpriteList
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
: public OwnedResource<OwnedClxSpriteList>
#endif
{
public:
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
explicit OwnedClxSpriteList(std::string_view name, std::string_view trnName, std::unique_ptr<uint8_t[]> &&data)
: OwnedResource<OwnedClxSpriteList>(name, trnName)
, data_(std::move(data))
Expand All @@ -351,20 +360,38 @@ class OwnedClxSpriteList : public OwnedResource<OwnedClxSpriteList> {
{
assert(data_ != nullptr);
}
#endif

explicit OwnedClxSpriteList(ResourceStoreHandle &&handle, std::unique_ptr<uint8_t[]> &&data)
: OwnedResource<OwnedClxSpriteList>(std::move(handle))
, data_(std::move(data))
explicit OwnedClxSpriteList(
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
ResourceStoreHandle &&handle,
#endif
std::unique_ptr<uint8_t[]> &&data)
:
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
OwnedResource<OwnedClxSpriteList>(std::move(handle))
,
#endif
data_(std::move(data))
{
assert(data_ != nullptr);
}

OwnedClxSpriteList(OwnedClxSpriteList &&) noexcept = default;
OwnedClxSpriteList &operator=(OwnedClxSpriteList &&) noexcept = default;

[[nodiscard]] OwnedClxSpriteList clone(std::string_view trnName = {}) const
[[nodiscard]] OwnedClxSpriteList clone(
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
std::string_view trnName = {}
#endif
) const
{
return ClxSpriteList { *this }.clone(resourceName(), trnName);
return ClxSpriteList { *this }.clone(
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
resourceName(),
trnName
#endif
);
}

[[nodiscard]] ClxSprite operator[](size_t spriteIndex) const
Expand Down Expand Up @@ -398,19 +425,33 @@ inline ClxSpriteList::ClxSpriteList(const OwnedClxSpriteList &owned)
{
}

inline OwnedClxSpriteList ClxSpriteList::clone(std::string_view name, std::string_view trnName) const
inline OwnedClxSpriteList ClxSpriteList::clone(
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
std::string_view name, std::string_view trnName
#endif
) const
{
const size_t size = dataSize();
std::unique_ptr<uint8_t[]> data { new uint8_t[size] };
memcpy(data.get(), data_, size);
return OwnedClxSpriteList { name, trnName, std::move(data) };
return OwnedClxSpriteList {
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
name, trnName,
#endif
std::move(data)
};
}

/**
* @brief Implicitly convertible to `ClxSpriteSheet` and owns its data.
*/
class OwnedClxSpriteSheet : public OwnedResource<OwnedClxSpriteSheet> {
class OwnedClxSpriteSheet
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
: public OwnedResource<OwnedClxSpriteSheet>
#endif
{
public:
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
OwnedClxSpriteSheet(std::string_view name, std::string_view trnName, std::unique_ptr<uint8_t[]> &&data, uint16_t numLists)
: OwnedResource<OwnedClxSpriteSheet>(name, trnName)
, data_(std::move(data))
Expand All @@ -428,10 +469,19 @@ class OwnedClxSpriteSheet : public OwnedResource<OwnedClxSpriteSheet> {
assert(data_ != nullptr);
assert(numLists > 0);
}

explicit OwnedClxSpriteSheet(ResourceStoreHandle &&handle, std::unique_ptr<uint8_t[]> &&data, uint16_t numLists)
: OwnedResource<OwnedClxSpriteSheet>(std::move(handle))
, data_(std::move(data))
#endif

explicit OwnedClxSpriteSheet(
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
ResourceStoreHandle &&handle,
#endif
std::unique_ptr<uint8_t[]> &&data, uint16_t numLists)
:
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
OwnedResource<OwnedClxSpriteSheet>(std::move(handle))
,
#endif
data_(std::move(data))
, num_lists_(numLists)
{
assert(data_ != nullptr);
Expand Down Expand Up @@ -507,7 +557,11 @@ class ClxSpriteListOrSheet {

ClxSpriteListOrSheet(const OwnedClxSpriteListOrSheet &listOrSheet);

[[nodiscard]] OwnedClxSpriteListOrSheet clone(std::string_view name, std::string_view trnName = {}) const;
[[nodiscard]] OwnedClxSpriteListOrSheet clone(
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
std::string_view name, std::string_view trnName = {}
#endif
) const;

[[nodiscard]] constexpr ClxSpriteList list() const
{
Expand Down Expand Up @@ -546,48 +600,87 @@ class OptionalOwnedClxSpriteListOrSheet;
/**
* @brief A CLX sprite list or a sprite sheet (list of lists).
*/
class OwnedClxSpriteListOrSheet : public OwnedResource<OwnedClxSpriteListOrSheet> {
class OwnedClxSpriteListOrSheet
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
: public OwnedResource<OwnedClxSpriteListOrSheet>
#endif
{
public:
static OwnedClxSpriteListOrSheet fromBuffer(
std::string_view name, std::string_view trnName, std::unique_ptr<uint8_t[]> &&data, size_t size)
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
std::string_view name, std::string_view trnName,
#endif
std::unique_ptr<uint8_t[]> &&data, size_t size)
{
const uint16_t numLists = GetNumListsFromClxListOrSheetBuffer(data.get(), size);
return OwnedClxSpriteListOrSheet { name, trnName, std::move(data), numLists };
}

explicit OwnedClxSpriteListOrSheet(std::string_view name, std::string_view trnName, std::unique_ptr<uint8_t[]> &&data, uint16_t numLists)
: OwnedResource<OwnedClxSpriteListOrSheet>(name, trnName)
, data_(std::move(data))
return OwnedClxSpriteListOrSheet {
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
name, trnName,
#endif
std::move(data), numLists
};
}

explicit OwnedClxSpriteListOrSheet(
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
std::string_view name, std::string_view trnName,
#endif
std::unique_ptr<uint8_t[]> &&data, uint16_t numLists)
:
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
OwnedResource<OwnedClxSpriteListOrSheet>(name, trnName)
,
#endif
data_(std::move(data))
, num_lists_(numLists)
{
}

#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
explicit OwnedClxSpriteListOrSheet(std::string &&name, std::string &&trnName, std::unique_ptr<uint8_t[]> &&data, uint16_t numLists)
: OwnedResource<OwnedClxSpriteListOrSheet>(std::move(name), std::move(trnName))
, data_(std::move(data))
, num_lists_(numLists)
{
}
#endif

explicit OwnedClxSpriteListOrSheet(OwnedClxSpriteSheet &&sheet) noexcept
: OwnedResource<OwnedClxSpriteListOrSheet>(std::move(sheet.handle_))
, data_(std::move(sheet.data_))
:
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
OwnedResource<OwnedClxSpriteListOrSheet>(std::move(sheet.handle_))
,
#endif
data_(std::move(sheet.data_))
, num_lists_(sheet.num_lists_)
{
}

explicit OwnedClxSpriteListOrSheet(OwnedClxSpriteList &&list) noexcept
: OwnedResource<OwnedClxSpriteListOrSheet>(std::move(list.handle_))
, data_(std::move(list.data_))
:
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
OwnedResource<OwnedClxSpriteListOrSheet>(std::move(list.handle_))
,
#endif
data_(std::move(list.data_))
{
}

OwnedClxSpriteListOrSheet(OwnedClxSpriteListOrSheet &&) noexcept = default;
OwnedClxSpriteListOrSheet &operator=(OwnedClxSpriteListOrSheet &&) noexcept = default;

[[nodiscard]] OwnedClxSpriteListOrSheet clone(std::string_view trnName = {}) const
[[nodiscard]] OwnedClxSpriteListOrSheet clone(
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
std::string_view trnName = {}
#endif
) const
{
return ClxSpriteListOrSheet { *this }.clone(resourceName(), trnName);
return ClxSpriteListOrSheet { *this }.clone(
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
resourceName(),
trnName
#endif
);
}

[[nodiscard]] ClxSpriteList list() const &
Expand All @@ -599,7 +692,12 @@ class OwnedClxSpriteListOrSheet : public OwnedResource<OwnedClxSpriteListOrSheet
[[nodiscard]] OwnedClxSpriteList list() &&
{
assert(num_lists_ == 0);
return OwnedClxSpriteList { std::move(handle_), std::move(data_) };
return OwnedClxSpriteList {
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
std::move(handle_),
#endif
std::move(data_)
};
}

[[nodiscard]] ClxSpriteSheet sheet() const &
Expand All @@ -611,7 +709,12 @@ class OwnedClxSpriteListOrSheet : public OwnedResource<OwnedClxSpriteListOrSheet
[[nodiscard]] OwnedClxSpriteSheet sheet() &&
{
assert(num_lists_ != 0);
return OwnedClxSpriteSheet { std::move(handle_), std::move(data_), num_lists_ };
return OwnedClxSpriteSheet {
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
std::move(handle_),
#endif
std::move(data_), num_lists_
};
}

[[nodiscard]] bool isSheet() const
Expand Down Expand Up @@ -643,12 +746,21 @@ inline ClxSpriteListOrSheet::ClxSpriteListOrSheet(const OwnedClxSpriteListOrShee
{
}

inline OwnedClxSpriteListOrSheet ClxSpriteListOrSheet::clone(std::string_view name, std::string_view trnName) const
inline OwnedClxSpriteListOrSheet ClxSpriteListOrSheet::clone(
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
std::string_view name, std::string_view trnName
#endif
) const
{
const size_t size = this->dataSize();
std::unique_ptr<uint8_t[]> data { new uint8_t[size] };
memcpy(data.get(), data_, size);
return OwnedClxSpriteListOrSheet { name, trnName, std::move(data), num_lists_ };
return OwnedClxSpriteListOrSheet {
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
name, trnName,
#endif
std::move(data), num_lists_
};
}

/**
Expand Down
6 changes: 5 additions & 1 deletion Source/engine/load_cel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ OwnedClxSpriteListOrSheet LoadCelListOrSheet(const char *pszName, PointerOrValue
#ifdef DEBUG_CEL_TO_CL2_SIZE
std::cout << path;
#endif
return CelToClx(pszName, /*trnName=*/ {}, data.get(), size, widthOrWidths);
return CelToClx(
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
pszName, /*trnName=*/ {},
#endif
data.get(), size, widthOrWidths);
#endif
}

Expand Down
6 changes: 5 additions & 1 deletion Source/engine/load_cl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ OwnedClxSpriteListOrSheet LoadCl2ListOrSheet(const char *pszName, PointerOrValue
#else
size_t size;
std::unique_ptr<uint8_t[]> data = LoadFileInMem<uint8_t>(path, &size);
return Cl2ToClx(pszName, /*trnName=*/ {}, std::move(data), size, widthOrWidths);
return Cl2ToClx(
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
pszName, /*trnName=*/ {},
#endif
std::move(data), size, widthOrWidths);
#endif
}

Expand Down
14 changes: 12 additions & 2 deletions Source/engine/load_cl2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,19 @@ OwnedClxSpriteSheet LoadMultipleCl2Sheet(std::string_view name, tl::function_ref
accumulatedSize += size;
}
#ifdef UNPACKED_MPQS
return OwnedClxSpriteSheet { name, /*trnName=*/ {}, std::move(data), static_cast<uint16_t>(count) };
return OwnedClxSpriteSheet {
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
name, /*trnName=*/ {},
#endif
std::move(data), static_cast<uint16_t>(count)
};
#else
return Cl2ToClx(name, /*trnName=*/ {}, std::move(data), accumulatedSize, frameWidth).sheet();
return Cl2ToClx(
#ifdef DEVILUTIONX_RESOURCE_TRACKING_ENABLED
name, /*trnName=*/ {},
#endif
std::move(data), accumulatedSize, frameWidth)
.sheet();
#endif
}

Expand Down
Loading

0 comments on commit 8ad4466

Please sign in to comment.