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 // False whenever Filter Type is Nearest, regardless of the Mipmapped
52 // property: mip levels are averaged, so they would blur pixel art at
53 // distance on every backend. GetMipLevels() follows the same rule.
54 bool IsMipmapped() const;
55 bool IsRenderTarget() const;
56 bool IsSrgb() const;
57 bool IsForcedHighQuality() const;
58
59 uint32_t GetWidth() const;
60 uint32_t GetHeight() const;
61 uint32_t GetMipLevels() const;
62 uint32_t GetLayers() const;
63 PixelFormat GetFormat() const;
64 FilterType GetFilterType() const;
65 WrapMode GetWrapMode() const;
66 int32_t GetLowQualityDownsampleFactor() const;
67
68 // Maximum normalized UV that contains actual content. (1, 1) = the full
69 // physical texture is content (no padding). Less than 1 in either axis
70 // means the platform-side resource padded the texture (e.g. 3DS PoT
71 // requirement on a 240x135 source produces a 256x256 physical texture
72 // with content in the top-left 240/256, 135/256 region — UV max is then
73 // (240/256, 135/256)). Renderers that draw a UV [0,1] rectangle should
74 // multiply their UVs by this so they only sample the content area.
75 glm::vec2 GetUVMax() const { return mUvMax; }
76 void SetUVMax(glm::vec2 uvMax) { mUvMax = uvMax; }
77
78 void SetFormat(PixelFormat format);
79 void SetFilterType(FilterType filterType);
80 void SetWrapMode(WrapMode wrapMode);
81 void SetForceHighQuality(bool forceHq);
82
83 static bool HandlePropChange(class Datum* datum, uint32_t index, const void* newValue);
84
85#if EDITOR
86 // Tear down and rebuild the GPU resource from mPixels after a sampler-
87 // affecting change (filter, wrap, mipmapped). Bumps the resource
88 // generation. Editor-only: the runtime frees mPixels in Create().
89 void RecreateResource();
90#endif
91
92 const std::vector<uint8_t>& GetPixels() const { return mPixels; }
93
94 // Keep mPixels resident after Create() in runtime builds too (the editor
95 // always keeps it, to save the asset). Opt in for runtime-generated
96 // textures whose pixels must be read back on the CPU -- a webcam/photo
97 // snapshot that gets PNG-encoded and uploaded, a procedural image that
98 // gets saved. Costs width * height * 4 bytes per texture, so leave it off
99 // for anything that is only ever drawn (a streaming video/webcam feed).
100 // Set it before Create(); UpdatePixels() keeps the copy in sync while on.
101 void SetRetainPixels(bool retain) { mRetainPixels = retain; }
102 bool GetRetainPixels() const { return mRetainPixels; }
103
104 // Bumped every time the backing GPU resource is torn down and rebuilt --
105 // Create(), Destroy(), or an editor property change (Filter Type, Wrap Mode,
106 // Mipmapped) that forces a rebuild behind an unchanged Texture*.
107 //
108 // Anything caching a backend handle *derived* from the resource must key on
109 // this as well as the Texture pointer. The ImGui descriptor sets the editor
110 // builds from the Vulkan image view + sampler are the motivating case: a
111 // pointer-only cache keeps handing ImGui a descriptor set pointing at the
112 // freed image, and the GPU faults a few frames later (device lost / TDR).
113 uint32_t GetResourceGeneration() const { return mResourceGeneration; }
114
115protected:
116
117 uint32_t mWidth;
118 uint32_t mHeight;
119 uint32_t mMipLevels;
120 uint32_t mLayers;
126 bool mSrgb;
129 // Placed here on purpose: it lands in the 3 bytes of padding before mUvMax
130 // (three 4-byte enums + four bools + one uint8 = 17 bytes from a 4-aligned
131 // base; glm::vec2 needs 4-byte alignment), so it changes neither
132 // sizeof(Texture) nor any existing member offset. Already-built addon DLLs
133 // that include this header stay ABI-compatible. See SetRetainPixels().
134 bool mRetainPixels = false;
135
136 // Content UV maximum. See GetUVMax() doc above. Default (1,1) — set by the
137 // graphics backend when it has to pad the physical texture beyond the
138 // logical content size (e.g. 3DS PoT requirement).
139 glm::vec2 mUvMax = glm::vec2(1.0f, 1.0f);
140
141 // See GetResourceGeneration().
142 uint32_t mResourceGeneration = 0;
143
144 // This pixel array is used as an intermediate storage between LoadStream() and Create()
145 // It is cleared and shrunk within Create() except when compiled for EDITOR
146 std::vector<uint8_t> mPixels;
147
148 // Graphics Resource
150};
Platform
Definition EngineTypes.h:32
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:125
virtual bool Import(const std::string &path, ImportOptions *options=nullptr)
Definition Asset.cpp:342
virtual void Create()
Definition Asset.cpp:78
virtual void SaveStream(Stream &stream, Platform platform)
Definition Asset.cpp:334
virtual glm::vec4 GetTypeColor()
Definition Asset.cpp:352
virtual const char * GetTypeImportExt()
Definition Asset.cpp:362
virtual const char * GetTypeName()
Definition Asset.cpp:357
virtual void GatherProperties(std::vector< Property > &outProps) override
Definition Asset.cpp:347
virtual void LoadStream(Stream &stream, Platform platform)
Definition Asset.cpp:320
virtual void Destroy()
Definition Asset.cpp:93
Definition Datum.h:169
Definition Asset.h:114
Definition Stream.h:21
Definition Texture.h:10
PixelFormat mFormat
Definition Texture.h:121
uint32_t mLayers
Definition Texture.h:120
WrapMode mWrapMode
Definition Texture.h:123
FilterType mFilterType
Definition Texture.h:122
uint32_t mWidth
Definition Texture.h:117
bool mSrgb
Definition Texture.h:126
bool GetRetainPixels() const
Definition Texture.h:102
std::vector< uint8_t > mPixels
Definition Texture.h:146
bool mMipmapped
Definition Texture.h:124
TextureResource mResource
Definition Texture.h:149
bool mForceHighQuality
Definition Texture.h:127
bool mRenderTarget
Definition Texture.h:125
DECLARE_ASSET(Texture, Asset)
uint32_t GetResourceGeneration() const
Definition Texture.h:113
uint32_t mHeight
Definition Texture.h:118
glm::vec2 GetUVMax() const
Definition Texture.h:75
void SetUVMax(glm::vec2 uvMax)
Definition Texture.h:76
const std::vector< uint8_t > & GetPixels() const
Definition Texture.h:92
void SetRetainPixels(bool retain)
Definition Texture.h:101
uint32_t mMipLevels
Definition Texture.h:119
uint8_t mLowQualityDownsampleFactor
Definition Texture.h:128
Definition GraphicsTypes.h:126