LumixEngine/src/renderer/texture.h

123 lines
2.4 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;
2018-07-11 23:35:34 +02:00
class 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;
};
#pragma pack()
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;
};
2018-08-19 17:35:37 +02:00
class LUMIX_RENDERER_API Texture final : public Resource
2014-06-16 21:18:15 +02:00
{
2019-06-13 17:26:52 +02:00
public:
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,
2019-06-13 17:26:52 +02:00
};
public:
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-10-24 21:53:19 +02:00
bool create(int w, int h, gpu::TextureFormat format, const void* data, u32 size);
2019-06-13 17:26:52 +02:00
void destroy();
2019-10-26 13:05:16 +02:00
const u8* getData() const { return (const u8*)data.getData(); }
u8* getData() { return (u8*)data.getMutableData(); }
2019-06-13 17:26:52 +02:00
void addDataReference();
void removeDataReference();
void onDataUpdated(int x, int y, int w, int h);
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);
u32 getPixelNearest(int x, int y) const;
u32 getPixel(float x, float y) const;
2019-10-24 21:53:19 +02:00
u32 getGPUFlags() const;
2019-06-13 17:26:52 +02:00
static unsigned int compareTGA(IInputStream* file1, IInputStream* file2, int difference, IAllocator& allocator);
static bool saveTGA(IOutputStream* file,
int width,
int height,
int bytes_per_pixel,
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;
public:
int width;
int height;
int bytes_per_pixel;
int depth;
int layers;
int mips;
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;
int 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