Polyphase Game Engine
Loading...
Searching...
No Matches
Texture.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include "glm/glm.hpp"
5#include "Asset.h"
6
8
10{
11
12public:
13
15
16 Texture();
17 virtual ~Texture();
18
19 TextureResource* GetResource();
20
21 // Asset Interface
22 virtual void LoadStream(Stream& stream, Platform platform) override;
23 virtual void SaveStream(Stream& stream, Platform platform) override;
24 virtual void Create() override;
25 virtual void Destroy() override;
26 virtual bool Import(const std::string& path, ImportOptions* options) override;
27 virtual void GatherProperties(std::vector<Property>& outProps) override;
28 virtual glm::vec4 GetTypeColor() override;
29 virtual const char* GetTypeName() override;
30 virtual const char* GetTypeImportExt() override;
31
32 void Init(uint32_t width, uint32_t height, uint8_t* data);
33
34 // Streams a new RGBA8 pixel buffer into the existing GPU image.
35 // The texture must already be Create()'d, dimensions must match, and byteSize must equal width * height * 4.
36 void UpdatePixels(const uint8_t* data, size_t byteSize);
37
38 // Decode an in-memory PNG/JPG/TGA/BMP buffer into `out` and Create() it.
39 // Runtime-safe (used by HTTP responses to materialise textures from URLs).
40 // Returns false on decode failure or unsupported dimensions.
41 static bool LoadFromMemory(const uint8_t* data, size_t size, Texture& out);
42
43 void SetMipmapped(bool mipmapped);
44 bool IsMipmapped() const;
45 bool IsRenderTarget() const;
46 bool IsSrgb() const;
47 bool IsForcedHighQuality() const;
48
49 uint32_t GetWidth() const;
50 uint32_t GetHeight() const;
51 uint32_t GetMipLevels() const;
52 uint32_t GetLayers() const;
53 PixelFormat GetFormat() const;
54 FilterType GetFilterType() const;
55 WrapMode GetWrapMode() const;
56 int32_t GetLowQualityDownsampleFactor() const;
57
58 // Maximum normalized UV that contains actual content. (1, 1) = the full
59 // physical texture is content (no padding). Less than 1 in either axis
60 // means the platform-side resource padded the texture (e.g. 3DS PoT
61 // requirement on a 240x135 source produces a 256x256 physical texture
62 // with content in the top-left 240/256, 135/256 region — UV max is then
63 // (240/256, 135/256)). Renderers that draw a UV [0,1] rectangle should
64 // multiply their UVs by this so they only sample the content area.
65 glm::vec2 GetUVMax() const { return mUvMax; }
66 void SetUVMax(glm::vec2 uvMax) { mUvMax = uvMax; }
67
68 void SetFormat(PixelFormat format);
69 void SetFilterType(FilterType filterType);
70 void SetWrapMode(WrapMode wrapMode);
71 void SetForceHighQuality(bool forceHq);
72
73 static bool HandlePropChange(class Datum* datum, uint32_t index, const void* newValue);
74
75 const std::vector<uint8_t>& GetPixels() const { return mPixels; }
76
77protected:
78
79 uint32_t mWidth;
80 uint32_t mHeight;
81 uint32_t mMipLevels;
82 uint32_t mLayers;
88 bool mSrgb;
91
92 // Content UV maximum. See GetUVMax() doc above. Default (1,1) — set by the
93 // graphics backend when it has to pad the physical texture beyond the
94 // logical content size (e.g. 3DS PoT requirement).
95 glm::vec2 mUvMax = glm::vec2(1.0f, 1.0f);
96
97 // This pixel array is used as an intermediate storage between LoadStream() and Create()
98 // It is cleared and shrunk within Create() except when compiled for EDITOR
99 std::vector<uint8_t> mPixels;
100
101 // Graphics Resource
103};
Platform
Definition EngineTypes.h:31
PixelFormat
Definition GraphicsTypes.h:45
FilterType
Definition GraphicsTypes.h:102
WrapMode
Definition GraphicsTypes.h:110
#define POLYPHASE_API
Definition PolyphaseAPI.h:31
Definition Asset.h:113
virtual bool Import(const std::string &path, ImportOptions *options=nullptr)
Definition Asset.cpp:244
virtual void Create()
Definition Asset.cpp:77
virtual void SaveStream(Stream &stream, Platform platform)
Definition Asset.cpp:236
virtual glm::vec4 GetTypeColor()
Definition Asset.cpp:254
virtual const char * GetTypeImportExt()
Definition Asset.cpp:264
virtual const char * GetTypeName()
Definition Asset.cpp:259
virtual void GatherProperties(std::vector< Property > &outProps) override
Definition Asset.cpp:249
virtual void LoadStream(Stream &stream, Platform platform)
Definition Asset.cpp:222
virtual void Destroy()
Definition Asset.cpp:87
Definition Datum.h:164
Definition Asset.h:102
Definition Stream.h:21
Definition Texture.h:10
PixelFormat mFormat
Definition Texture.h:83
uint32_t mLayers
Definition Texture.h:82
WrapMode mWrapMode
Definition Texture.h:85
FilterType mFilterType
Definition Texture.h:84
uint32_t mWidth
Definition Texture.h:79
bool mSrgb
Definition Texture.h:88
std::vector< uint8_t > mPixels
Definition Texture.h:99
bool mMipmapped
Definition Texture.h:86
TextureResource mResource
Definition Texture.h:102
bool mForceHighQuality
Definition Texture.h:89
bool mRenderTarget
Definition Texture.h:87
DECLARE_ASSET(Texture, Asset)
uint32_t mHeight
Definition Texture.h:80
glm::vec2 GetUVMax() const
Definition Texture.h:65
void SetUVMax(glm::vec2 uvMax)
Definition Texture.h:66
const std::vector< uint8_t > & GetPixels() const
Definition Texture.h:75
uint32_t mMipLevels
Definition Texture.h:81
uint8_t mLowQualityDownsampleFactor
Definition Texture.h:90
Definition GraphicsTypes.h:125