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
84 // Bumped every time the backing GPU resource is torn down and rebuilt --
85 // Create(), Destroy(), or an editor property change (Filter Type, Wrap Mode,
86 // Mipmapped) that forces a rebuild behind an unchanged Texture*.
87 //
88 // Anything caching a backend handle *derived* from the resource must key on
89 // this as well as the Texture pointer. The ImGui descriptor sets the editor
90 // builds from the Vulkan image view + sampler are the motivating case: a
91 // pointer-only cache keeps handing ImGui a descriptor set pointing at the
92 // freed image, and the GPU faults a few frames later (device lost / TDR).
93 uint32_t GetResourceGeneration() const { return mResourceGeneration; }
94
95protected:
96
97 uint32_t mWidth;
98 uint32_t mHeight;
99 uint32_t mMipLevels;
100 uint32_t mLayers;
106 bool mSrgb;
109
110 // Content UV maximum. See GetUVMax() doc above. Default (1,1) — set by the
111 // graphics backend when it has to pad the physical texture beyond the
112 // logical content size (e.g. 3DS PoT requirement).
113 glm::vec2 mUvMax = glm::vec2(1.0f, 1.0f);
114
115 // See GetResourceGeneration().
116 uint32_t mResourceGeneration = 0;
117
118 // This pixel array is used as an intermediate storage between LoadStream() and Create()
119 // It is cleared and shrunk within Create() except when compiled for EDITOR
120 std::vector<uint8_t> mPixels;
121
122 // Graphics Resource
124};
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:120
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:109
Definition Stream.h:21
Definition Texture.h:10
PixelFormat mFormat
Definition Texture.h:101
uint32_t mLayers
Definition Texture.h:100
WrapMode mWrapMode
Definition Texture.h:103
FilterType mFilterType
Definition Texture.h:102
uint32_t mWidth
Definition Texture.h:97
bool mSrgb
Definition Texture.h:106
std::vector< uint8_t > mPixels
Definition Texture.h:120
bool mMipmapped
Definition Texture.h:104
TextureResource mResource
Definition Texture.h:123
bool mForceHighQuality
Definition Texture.h:107
bool mRenderTarget
Definition Texture.h:105
DECLARE_ASSET(Texture, Asset)
uint32_t GetResourceGeneration() const
Definition Texture.h:93
uint32_t mHeight
Definition Texture.h:98
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:99
uint8_t mLowQualityDownsampleFactor
Definition Texture.h:108
Definition GraphicsTypes.h:126