refactor: image and obj data loading

This commit is contained in:
Jan
2024-09-24 12:01:42 +01:00
parent 5fee875495
commit 5cc52c42cd
97 changed files with 1784 additions and 1878 deletions

View File

@ -1326,7 +1326,6 @@ namespace IW3
// void/*IDirect3DTexture9*/* map;
// void/*IDirect3DVolumeTexture9*/* volmap;
// void/*IDirect3DCubeTexture9*/* cubemap;
Texture* texture;
GfxImageLoadDef* loadDef;
};

View File

@ -1009,7 +1009,6 @@ namespace IW4
// IDirect3DTexture9* map;
// IDirect3DVolumeTexture9* volmap;
// IDirect3DCubeTexture9* cubemap;
Texture* texture;
GfxImageLoadDef* loadDef;
};

View File

@ -1143,9 +1143,6 @@ namespace IW5
// IDirect3DTexture9* map;
// IDirect3DVolumeTexture9* volmap;
// IDirect3DCubeTexture9* cubemap;
#ifndef __ida
Texture* texture;
#endif
GfxImageLoadDef* loadDef;
};

View File

@ -1024,7 +1024,6 @@ namespace T5
// IDirect3DTexture9* map;
// IDirect3DVolumeTexture9* volmap;
// IDirect3DCubeTexture9* cubemap;
Texture* texture;
GfxImageLoadDef* loadDef;
};

View File

@ -2,62 +2,8 @@
#include "Utils/Pack.h"
#include <cctype>
using namespace T6;
int Common::Com_HashKey(const char* str, const int maxLen)
{
if (str == nullptr)
return 0;
int hash = 0;
for (int i = 0; i < maxLen; i++)
{
if (str[i] == '\0')
break;
hash += str[i] * (0x77 + i);
}
return hash ^ ((hash ^ (hash >> 10)) >> 10);
}
int Common::Com_HashString(const char* str)
{
if (!str)
return 0;
auto result = 0x1505;
auto offset = 0;
while (str[offset])
{
const auto c = tolower(str[offset++]);
result = c + 33 * result;
}
return result;
}
int Common::Com_HashString(const char* str, const int len)
{
if (!str)
return 0;
int result = 0x1505;
int offset = 0;
while (str[offset])
{
if (len > 0 && offset >= len)
break;
const int c = tolower(str[offset++]);
result = c + 33 * result;
}
return result;
}
PackedTexCoords Common::Vec2PackTexCoords(const float (&in)[2])
{
return PackedTexCoords{pack32::Vec2PackTexCoordsUV(in)};

View File

@ -2,14 +2,64 @@
#include "T6.h"
#include <cctype>
namespace T6
{
class Common
{
public:
static int Com_HashKey(const char* str, int maxLen);
static int Com_HashString(const char* str);
static int Com_HashString(const char* str, int len);
static constexpr int Com_HashKey(const char* str, const int maxLen)
{
if (str == nullptr)
return 0;
int hash = 0;
for (int i = 0; i < maxLen; i++)
{
if (str[i] == '\0')
break;
hash += str[i] * (0x77 + i);
}
return hash ^ ((hash ^ (hash >> 10)) >> 10);
}
static constexpr int Com_HashString(const char* str)
{
if (!str)
return 0;
auto result = 0x1505;
auto offset = 0;
while (str[offset])
{
const auto c = tolower(str[offset++]);
result = c + 33 * result;
}
return result;
}
static constexpr int Com_HashString(const char* str, const int len)
{
if (!str)
return 0;
int result = 0x1505;
int offset = 0;
while (str[offset])
{
if (len > 0 && offset >= len)
break;
const int c = tolower(str[offset++]);
result = c + 33 * result;
}
return result;
}
static constexpr uint32_t R_HashString(const char* str, uint32_t hash)
{

View File

@ -848,7 +848,6 @@ namespace T6
union GfxTexture
{
void /*ID3D11ShaderResourceView*/* basemap;
Texture* texture;
GfxImageLoadDef* loadDef;
};

View File

@ -77,6 +77,11 @@ uint8_t* Texture::GetBufferForMipLevel(const int mipLevel)
return GetBufferForMipLevel(mipLevel, 0);
}
const uint8_t* Texture::GetBufferForMipLevel(const int mipLevel) const
{
return GetBufferForMipLevel(mipLevel, 0);
}
// ==============================================
// ================ Texture2D ===================
// ==============================================
@ -183,6 +188,31 @@ uint8_t* Texture2D::GetBufferForMipLevel(const int mipLevel, const int face)
return &m_data[bufferOffset];
}
const uint8_t* Texture2D::GetBufferForMipLevel(const int mipLevel, const int face) const
{
assert(mipLevel >= 0);
assert(mipLevel < (m_has_mip_maps ? GetMipMapCount() : 1));
assert(face == 0);
assert(m_data);
if (mipLevel < 0 || mipLevel >= (m_has_mip_maps ? GetMipMapCount() : 1))
return nullptr;
if (face != 0)
return nullptr;
if (!m_data)
return nullptr;
size_t bufferOffset = 0;
for (int previousMipLevel = 0; previousMipLevel < mipLevel; previousMipLevel++)
{
bufferOffset += GetSizeOfMipLevel(previousMipLevel);
}
return &m_data[bufferOffset];
}
// ==============================================
// =============== TextureCube ==================
// ==============================================
@ -251,6 +281,32 @@ uint8_t* TextureCube::GetBufferForMipLevel(const int mipLevel, const int face)
return &m_data[bufferOffset + GetSizeOfMipLevel(mipLevel) * face];
}
const uint8_t* TextureCube::GetBufferForMipLevel(const int mipLevel, const int face) const
{
assert(mipLevel >= 0);
assert(mipLevel < (m_has_mip_maps ? GetMipMapCount() : 1));
assert(face >= 0);
assert(face < FACE_COUNT);
assert(m_data);
if (mipLevel < 0 || mipLevel >= (m_has_mip_maps ? GetMipMapCount() : 1))
return nullptr;
if (face < 0 || face >= FACE_COUNT)
return nullptr;
if (!m_data)
return nullptr;
size_t bufferOffset = 0;
for (int previousMipLevel = 0; previousMipLevel < mipLevel; previousMipLevel++)
{
bufferOffset += GetSizeOfMipLevel(previousMipLevel) * FACE_COUNT;
}
return &m_data[bufferOffset + GetSizeOfMipLevel(mipLevel) * face];
}
// ==============================================
// ================ Texture3D ===================
// ==============================================
@ -360,3 +416,28 @@ uint8_t* Texture3D::GetBufferForMipLevel(const int mipLevel, const int face)
return &m_data[bufferOffset];
}
const uint8_t* Texture3D::GetBufferForMipLevel(const int mipLevel, const int face) const
{
assert(mipLevel >= 0);
assert(mipLevel < (m_has_mip_maps ? GetMipMapCount() : 1));
assert(face == 0);
assert(m_data);
if (mipLevel < 0 || mipLevel >= (m_has_mip_maps ? GetMipMapCount() : 1))
return nullptr;
if (face != 0)
return nullptr;
if (!m_data)
return nullptr;
size_t bufferOffset = 0;
for (int previousMipLevel = 0; previousMipLevel < mipLevel; previousMipLevel++)
{
bufferOffset += GetSizeOfMipLevel(previousMipLevel);
}
return &m_data[bufferOffset];
}

View File

@ -3,7 +3,7 @@
#include <cstdint>
enum class TextureType
enum class TextureType : std::uint8_t
{
T_2D,
T_CUBE,
@ -28,23 +28,25 @@ public:
Texture& operator=(const Texture& other) = delete;
virtual TextureType GetTextureType() const = 0;
const ImageFormat* GetFormat() const;
[[nodiscard]] virtual TextureType GetTextureType() const = 0;
[[nodiscard]] const ImageFormat* GetFormat() const;
virtual unsigned GetWidth() const = 0;
virtual unsigned GetHeight() const = 0;
virtual unsigned GetDepth() const = 0;
virtual int GetFaceCount() const = 0;
[[nodiscard]] virtual unsigned GetWidth() const = 0;
[[nodiscard]] virtual unsigned GetHeight() const = 0;
[[nodiscard]] virtual unsigned GetDepth() const = 0;
[[nodiscard]] virtual int GetFaceCount() const = 0;
void Allocate();
bool Empty() const;
[[nodiscard]] bool Empty() const;
virtual size_t GetSizeOfMipLevel(int mipLevel) const = 0;
virtual uint8_t* GetBufferForMipLevel(int mipLevel, int face) = 0;
uint8_t* GetBufferForMipLevel(int mipLevel);
[[nodiscard]] virtual size_t GetSizeOfMipLevel(int mipLevel) const = 0;
[[nodiscard]] virtual uint8_t* GetBufferForMipLevel(int mipLevel, int face) = 0;
[[nodiscard]] virtual const uint8_t* GetBufferForMipLevel(int mipLevel, int face) const = 0;
[[nodiscard]] uint8_t* GetBufferForMipLevel(int mipLevel);
[[nodiscard]] const uint8_t* GetBufferForMipLevel(int mipLevel) const;
bool HasMipMaps() const;
virtual int GetMipMapCount() const = 0;
[[nodiscard]] bool HasMipMaps() const;
[[nodiscard]] virtual int GetMipMapCount() const = 0;
};
class Texture2D : public Texture
@ -63,17 +65,18 @@ public:
Texture2D& operator=(const Texture2D& other) = delete;
Texture2D& operator=(Texture2D&& other) noexcept;
TextureType GetTextureType() const override;
[[nodiscard]] TextureType GetTextureType() const override;
unsigned GetWidth() const override;
unsigned GetHeight() const override;
unsigned GetDepth() const override;
int GetFaceCount() const override;
[[nodiscard]] unsigned GetWidth() const override;
[[nodiscard]] unsigned GetHeight() const override;
[[nodiscard]] unsigned GetDepth() const override;
[[nodiscard]] int GetFaceCount() const override;
size_t GetSizeOfMipLevel(int mipLevel) const override;
uint8_t* GetBufferForMipLevel(int mipLevel, int face) override;
[[nodiscard]] size_t GetSizeOfMipLevel(int mipLevel) const override;
[[nodiscard]] uint8_t* GetBufferForMipLevel(int mipLevel, int face) override;
[[nodiscard]] const uint8_t* GetBufferForMipLevel(int mipLevel, int face) const override;
int GetMipMapCount() const override;
[[nodiscard]] int GetMipMapCount() const override;
};
class TextureCube final : public Texture2D
@ -90,11 +93,12 @@ public:
TextureCube& operator=(const TextureCube& other) = delete;
TextureCube& operator=(TextureCube&& other) noexcept;
TextureType GetTextureType() const override;
[[nodiscard]] TextureType GetTextureType() const override;
int GetFaceCount() const override;
[[nodiscard]] int GetFaceCount() const override;
uint8_t* GetBufferForMipLevel(int mipLevel, int face) override;
[[nodiscard]] uint8_t* GetBufferForMipLevel(int mipLevel, int face) override;
[[nodiscard]] const uint8_t* GetBufferForMipLevel(int mipLevel, int face) const override;
};
class Texture3D final : public Texture
@ -113,15 +117,16 @@ public:
Texture3D& operator=(const Texture3D& other) = delete;
Texture3D& operator=(Texture3D&& other) noexcept;
TextureType GetTextureType() const override;
[[nodiscard]] TextureType GetTextureType() const override;
unsigned GetWidth() const override;
unsigned GetHeight() const override;
unsigned GetDepth() const override;
int GetFaceCount() const override;
[[nodiscard]] unsigned GetWidth() const override;
[[nodiscard]] unsigned GetHeight() const override;
[[nodiscard]] unsigned GetDepth() const override;
[[nodiscard]] int GetFaceCount() const override;
size_t GetSizeOfMipLevel(int mipLevel) const override;
uint8_t* GetBufferForMipLevel(int mipLevel, int face) override;
[[nodiscard]] size_t GetSizeOfMipLevel(int mipLevel) const override;
[[nodiscard]] uint8_t* GetBufferForMipLevel(int mipLevel, int face) override;
[[nodiscard]] const uint8_t* GetBufferForMipLevel(int mipLevel, int face) const override;
int GetMipMapCount() const override;
[[nodiscard]] int GetMipMapCount() const override;
};

View File

@ -104,7 +104,7 @@ void TextureConverter::SetPixelFunctions(const unsigned inBitCount, const unsign
}
}
TextureConverter::TextureConverter(Texture* inputTexture, const ImageFormat* targetFormat)
TextureConverter::TextureConverter(const Texture* inputTexture, const ImageFormat* targetFormat)
: m_input_texture(inputTexture),
m_output_texture(nullptr),
m_input_format(inputTexture->GetFormat()),
@ -117,15 +117,17 @@ void TextureConverter::CreateOutputTexture()
switch (m_input_texture->GetTextureType())
{
case TextureType::T_2D:
m_output_texture = new Texture2D(m_output_format, m_input_texture->GetWidth(), m_input_texture->GetHeight(), m_input_texture->HasMipMaps());
m_output_texture =
std::make_unique<Texture2D>(m_output_format, m_input_texture->GetWidth(), m_input_texture->GetHeight(), m_input_texture->HasMipMaps());
break;
case TextureType::T_CUBE:
m_output_texture = new TextureCube(m_output_format, m_input_texture->GetWidth(), m_input_texture->GetHeight(), m_input_texture->HasMipMaps());
m_output_texture =
std::make_unique<TextureCube>(m_output_format, m_input_texture->GetWidth(), m_input_texture->GetHeight(), m_input_texture->HasMipMaps());
break;
case TextureType::T_3D:
m_output_texture = new Texture3D(
m_output_texture = std::make_unique<Texture3D>(
m_output_format, m_input_texture->GetWidth(), m_input_texture->GetHeight(), m_input_texture->GetDepth(), m_input_texture->HasMipMaps());
break;
default:
@ -202,7 +204,7 @@ void TextureConverter::ConvertUnsignedToUnsigned()
}
}
Texture* TextureConverter::Convert()
std::unique_ptr<Texture> TextureConverter::Convert()
{
CreateOutputTexture();
@ -216,5 +218,5 @@ Texture* TextureConverter::Convert()
assert(false);
}
return m_output_texture;
return std::move(m_output_texture);
}

View File

@ -3,17 +3,16 @@
#include "Texture.h"
#include <functional>
#include <memory>
class TextureConverter
{
Texture* m_input_texture;
Texture* m_output_texture;
const ImageFormat* m_input_format;
const ImageFormat* m_output_format;
public:
TextureConverter(const Texture* inputTexture, const ImageFormat* targetFormat);
std::function<uint64_t(const void* offset, unsigned bitCount)> m_read_pixel_func;
std::function<void(void* offset, uint64_t pixel, unsigned bitCount)> m_write_pixel_func;
std::unique_ptr<Texture> Convert();
private:
static constexpr uint64_t Mask1(unsigned length);
void SetPixelFunctions(unsigned inBitCount, unsigned outBitCount);
@ -22,8 +21,11 @@ class TextureConverter
void ReorderUnsignedToUnsigned() const;
void ConvertUnsignedToUnsigned();
public:
TextureConverter(Texture* inputTexture, const ImageFormat* targetFormat);
std::function<uint64_t(const void* offset, unsigned bitCount)> m_read_pixel_func;
std::function<void(void* offset, uint64_t pixel, unsigned bitCount)> m_write_pixel_func;
Texture* Convert();
const Texture* m_input_texture;
std::unique_ptr<Texture> m_output_texture;
const ImageFormat* m_input_format;
const ImageFormat* m_output_format;
};