LumixEngine/src/renderer/texture.h

138 lines
2.7 KiB
C
Raw Normal View History

2014-06-16 21:18:15 +02:00
#pragma once
2016-05-10 08:24:31 +02:00
#include "engine/resource.h"
2019-10-26 13:05:16 +02:00
#include "engine/stream.h"
2019-10-24 21:53:19 +02:00
#include "gpu/gpu.h"
2014-06-16 21:18:15 +02:00
namespace Lumix
{
2019-06-11 01:09:14 +02:00
struct IInputStream;
struct IOutputStream;
2020-02-21 22:09:11 +01:00
struct Renderer;
2014-06-16 21:18:15 +02:00
2017-05-20 01:22:24 +02:00
#pragma pack(1)
struct TGAHeader
{
u8 idLength;
u8 colourMapType;
u8 dataType;
u16 colourMapOrigin;
u16 colourMapLength;
u8 colourMapDepth;
u16 xOrigin;
u16 yOrigin;
u16 width;
u16 height;
u8 bitsPerPixel;
u8 imageDescriptor;
};
2021-04-22 20:32:10 +02:00
struct LBCHeader {
static constexpr u32 MAGIC = 'LBC_';
enum Flags {
CUBEMAP = 1 << 0,
IS_3D = 1 << 1
};
u32 magic = MAGIC;
u32 version = 0;
u32 w = 0;
u32 h = 0;
u32 slices = 0;
u32 mips = 0;
u32 flags = 0;
gpu::TextureFormat format;
};
2017-05-20 01:22:24 +02:00
struct RawTextureHeader {
enum class ChannelType : u32 {
U8,
U16,
FLOAT
};
2019-11-17 19:45:54 +01:00
static constexpr u32 LAST_VERSION = 0;
static constexpr u32 MAGIC = '_LTR';
u32 magic = MAGIC;
2019-11-17 19:45:54 +01:00
u32 version = LAST_VERSION;
u32 width;
u32 height;
u32 depth;
ChannelType channel_type;
u32 channels_count;
bool is_array = false;
u8 padding[3];
};
#pragma pack()
static_assert(sizeof(LBCHeader) == 32);
static_assert(sizeof(RawTextureHeader) == 32);
2020-12-25 18:11:09 +01:00
struct LUMIX_RENDERER_API Texture final : Resource {
2019-06-13 17:26:52 +02:00
enum class Flags : u32 {
SRGB = 1 << 0,
2019-08-19 13:17:22 +02:00
CLAMP_U = 1 << 1,
CLAMP_V = 1 << 2,
CLAMP_W = 1 << 3,
2019-11-17 19:45:54 +01:00
POINT = 1 << 4,
2021-02-14 15:12:46 +01:00
ANISOTROPIC = 1 << 5
2019-06-13 17:26:52 +02:00
};
2019-06-26 18:52:52 +02:00
Texture(const Path& path, ResourceManager& resource_manager, Renderer& renderer, IAllocator& allocator);
2019-06-13 17:26:52 +02:00
~Texture();
ResourceType getType() const override { return TYPE; }
2019-11-24 23:08:42 +01:00
bool create(u32 w, u32 h, gpu::TextureFormat format, const void* data, u32 size);
2019-06-13 17:26:52 +02:00
void destroy();
2020-05-01 16:14:00 +02:00
const u8* getData() const { return data.data(); }
u8* getData() { return data.getMutableData(); }
2019-06-13 17:26:52 +02:00
void addDataReference();
void removeDataReference();
2019-11-24 23:08:42 +01:00
void onDataUpdated(u32 x, u32 y, u32 w, u32 h);
2019-06-13 17:26:52 +02:00
void save();
void setSRGB(bool enable) { setFlags(enable ? flags | u32(Flags::SRGB) : flags & ~u32(Flags::SRGB)); }
void setFlags(u32 flags);
bool getFlag(Flags flag);
void setFlag(Flags flag, bool value);
2019-11-24 23:08:42 +01:00
u32 getPixelNearest(u32 x, u32 y) const;
2019-06-13 17:26:52 +02:00
u32 getPixel(float x, float y) const;
2020-11-26 20:25:14 +01:00
gpu::TextureFlags getGPUFlags() const;
2019-06-13 17:26:52 +02:00
2021-04-22 20:32:10 +02:00
static u8* getLBCInfo(const void* data, gpu::TextureDesc& desc);
2019-06-13 17:26:52 +02:00
static bool saveTGA(IOutputStream* file,
int width,
int height,
2019-11-24 23:08:42 +01:00
gpu::TextureFormat format,
2019-06-13 17:26:52 +02:00
const u8* image_dest,
2019-09-12 16:45:17 +02:00
bool upper_left_origin,
2019-06-13 17:26:52 +02:00
const Path& path,
IAllocator& allocator);
static const ResourceType TYPE;
2019-11-24 23:08:42 +01:00
u32 width;
u32 height;
u32 depth;
u32 mips;
gpu::TextureFormat format;
2019-06-13 17:26:52 +02:00
bool is_cubemap;
u32 flags;
2019-10-24 21:53:19 +02:00
gpu::TextureHandle handle;
2019-06-13 17:26:52 +02:00
IAllocator& allocator;
2019-11-24 23:08:42 +01:00
u32 data_reference;
2019-10-26 13:05:16 +02:00
OutputMemoryStream data;
2019-06-13 17:26:52 +02:00
Renderer& renderer;
private:
void unload() override;
bool load(u64 size, const u8* mem) override;
bool loadTGA(IInputStream& file);
2014-06-16 21:18:15 +02:00
};
2017-05-23 19:57:11 +02:00
} // namespace Lumix