LumixEngine/src/renderer/texture.h
2019-10-26 13:05:16 +02:00

102 lines
2.1 KiB
C++

#pragma once
#include "engine/resource.h"
#include "engine/stream.h"
#include "gpu/gpu.h"
namespace Lumix
{
struct IInputStream;
struct IOutputStream;
class Renderer;
#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()
class LUMIX_RENDERER_API Texture final : public Resource
{
public:
enum class Flags : u32 {
SRGB = 1 << 0,
CLAMP_U = 1 << 1,
CLAMP_V = 1 << 2,
CLAMP_W = 1 << 3,
POINT = 1 << 4
};
public:
Texture(const Path& path, ResourceManager& resource_manager, Renderer& renderer, IAllocator& allocator);
~Texture();
ResourceType getType() const override { return TYPE; }
bool create(int w, int h, gpu::TextureFormat format, const void* data, u32 size);
void destroy();
const u8* getData() const { return (const u8*)data.getData(); }
u8* getData() { return (u8*)data.getMutableData(); }
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;
u32 getGPUFlags() const;
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,
bool upper_left_origin,
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;
gpu::TextureHandle handle;
IAllocator& allocator;
int data_reference;
OutputMemoryStream data;
Renderer& renderer;
private:
void unload() override;
bool load(u64 size, const u8* mem) override;
bool loadTGA(IInputStream& file);
};
} // namespace Lumix