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#if EDITOR
39 // Install a fixed-up RGBA8 pixel buffer at new dimensions and (re)create
40 // the GPU resource. Used by TextureImportFixupModal after the user picks
41 // Pad / Resize on a non-POT import where Create() was deferred.
42 void FinalizeDeferredImport(std::vector<uint8_t>&& pixels, uint32_t width, uint32_t height);
43#endif
44
45 // Decode an in-memory PNG/JPG/TGA/BMP buffer into `out` and Create() it.
46 // Runtime-safe (used by HTTP responses to materialise textures from URLs).
47 // Returns false on decode failure or unsupported dimensions.
48 static bool LoadFromMemory(const uint8_t* data, size_t size, Texture& out);
49
50 void SetMipmapped(bool mipmapped);
51 bool IsMipmapped() const;
52 bool IsRenderTarget() const;
53 bool IsSrgb() const;
54 bool IsForcedHighQuality() const;
55
56 uint32_t GetWidth() const;
57 uint32_t GetHeight() const;
58 uint32_t GetMipLevels() const;
59 uint32_t GetLayers() const;
60 PixelFormat GetFormat() const;
61 FilterType GetFilterType() const;
62 WrapMode GetWrapMode() const;
63 int32_t GetLowQualityDownsampleFactor() const;
64
65 // Maximum normalized UV that contains actual content. (1, 1) = the full
66 // physical texture is content (no padding). Less than 1 in either axis
67 // means the platform-side resource padded the texture (e.g. 3DS PoT
68 // requirement on a 240x135 source produces a 256x256 physical texture
69 // with content in the top-left 240/256, 135/256 region — UV max is then
70 // (240/256, 135/256)). Renderers that draw a UV [0,1] rectangle should
71 // multiply their UVs by this so they only sample the content area.
72 glm::vec2 GetUVMax() const { return mUvMax; }
73 void SetUVMax(glm::vec2 uvMax) { mUvMax = uvMax; }
74
75 void SetFormat(PixelFormat format);
76 void SetFilterType(FilterType filterType);
77 void SetWrapMode(WrapMode wrapMode);
78 void SetForceHighQuality(bool forceHq);
79
80 static bool HandlePropChange(class Datum* datum, uint32_t index, const void* newValue);
81
82 const std::vector<uint8_t>& GetPixels() const { return mPixels; }
83
84protected:
85
86 uint32_t mWidth;
87 uint32_t mHeight;
88 uint32_t mMipLevels;
89 uint32_t mLayers;
95 bool mSrgb;
98
99 // Content UV maximum. See GetUVMax() doc above. Default (1,1) — set by the
100 // graphics backend when it has to pad the physical texture beyond the
101 // logical content size (e.g. 3DS PoT requirement).
102 glm::vec2 mUvMax = glm::vec2(1.0f, 1.0f);
103
104 // This pixel array is used as an intermediate storage between LoadStream() and Create()
105 // It is cleared and shrunk within Create() except when compiled for EDITOR
106 std::vector<uint8_t> mPixels;
107
108 // Graphics Resource
110};
Platform
Definition EngineTypes.h:31
PixelFormat
Definition GraphicsTypes.h:45
FilterType
Definition GraphicsTypes.h:103
WrapMode
Definition GraphicsTypes.h:111
#define POLYPHASE_API
Definition PolyphaseAPI.h:31
Definition Asset.h:119
virtual bool Import(const std::string &path, ImportOptions *options=nullptr)
Definition Asset.cpp:292
virtual void Create()
Definition Asset.cpp:77
virtual void SaveStream(Stream &stream, Platform platform)
Definition Asset.cpp:284
virtual glm::vec4 GetTypeColor()
Definition Asset.cpp:302
virtual const char * GetTypeImportExt()
Definition Asset.cpp:312
virtual const char * GetTypeName()
Definition Asset.cpp:307
virtual void GatherProperties(std::vector< Property > &outProps) override
Definition Asset.cpp:297
virtual void LoadStream(Stream &stream, Platform platform)
Definition Asset.cpp:270
virtual void Destroy()
Definition Asset.cpp:92
Definition Datum.h:169
Definition Asset.h:108
Definition Stream.h:21
Definition Texture.h:10
PixelFormat mFormat
Definition Texture.h:90
uint32_t mLayers
Definition Texture.h:89
WrapMode mWrapMode
Definition Texture.h:92
FilterType mFilterType
Definition Texture.h:91
uint32_t mWidth
Definition Texture.h:86
bool mSrgb
Definition Texture.h:95
std::vector< uint8_t > mPixels
Definition Texture.h:106
bool mMipmapped
Definition Texture.h:93
TextureResource mResource
Definition Texture.h:109
bool mForceHighQuality
Definition Texture.h:96
bool mRenderTarget
Definition Texture.h:94
DECLARE_ASSET(Texture, Asset)
uint32_t mHeight
Definition Texture.h:87
glm::vec2 GetUVMax() const
Definition Texture.h:72
void SetUVMax(glm::vec2 uvMax)
Definition Texture.h:73
const std::vector< uint8_t > & GetPixels() const
Definition Texture.h:82
uint32_t mMipLevels
Definition Texture.h:88
uint8_t mLowQualityDownsampleFactor
Definition Texture.h:97
Definition GraphicsTypes.h:126