VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
texture.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Created by Arnis Lektauers on 12.09.2025.
5//
6#pragma once
7
8#include <memory>
9#include <cstddef>
10#include <vector>
11
12#include "constants.h"
13#include "gpu.h"
14
15namespace visutwin::canvas
16{
17 enum class TextureEncoding
18 {
20 RGBP = 1,
21 RGBM = 2
22 };
23
24 class GraphicsDevice;
25 struct DeviceVRAM;
26
46
56 class Texture : public std::enable_shared_from_this<Texture>
57 {
58 public:
59 explicit Texture(GraphicsDevice* graphicsDevice, const TextureOptions& options = TextureOptions{});
60
61 ~Texture();
62
63 uint32_t width() const { return _width; }
64
65 uint32_t height() const { return _height; }
66
67 uint32_t depth() const { return _depth; }
68
69 bool isArray() const { return _arrayLength > 0; }
70
71 void upload();
72
73 bool needsUpload() const { return _needsUpload; }
74 bool needsMipmapsUpload() const { return _needsMipmapsUpload; }
75
76 void setNeedsUpload(const bool needsUpload) { _needsUpload = needsUpload; }
77 void setNeedsMipmapsUpload(const bool needsMipmapsUpload) { _needsMipmapsUpload = needsMipmapsUpload; }
78
79 bool hasLevels() const { return !_levels.empty(); }
80 uint32_t getNumLevels() const { return _numLevels; }
81 void* getLevel(uint32_t mipLevel) const;
82 size_t getLevelDataSize(uint32_t mipLevel, uint32_t face = 0) const;
83 void setLevelData(uint32_t mipLevel, const uint8_t* data, size_t dataSize, uint32_t face = 0);
84
85 bool isCubemap() const { return _cubemap; }
86
87 void* getFaceData(uint32_t mipLevel, uint32_t face) const;
88 void* getArrayData(uint32_t mipLevel, uint32_t index) const;
89
90 bool isVolume() const { return _volume; }
91
92 uint32_t getArrayLength() const { return _arrayLength; }
93
94 bool mipmaps() const { return _mipmaps; }
95 void setMipmaps(bool mipmaps);
96
97 PixelFormat format() const { return _format; }
98 bool storage() const { return _storage; }
99
100 GraphicsDevice* device() const { return _device; }
101 gpu::HardwareTexture* impl() const { return _impl.get(); }
102
103 const std::string& name() const { return _name; }
104 TextureEncoding encoding() const { return _encoding; }
105 void setEncoding(TextureEncoding value) { _encoding = value; }
106
107 // Resize the texture
108 void resize(uint32_t width, uint32_t height, uint32_t depth = 1);
109
110 void setMinFilter(FilterMode filter);
111
112 void setMagFilter(FilterMode filter);
113
114 void setAddressU(AddressMode address);
115
116 void setAddressV(AddressMode address);
117
118 void setAddressW(AddressMode address);
119
120 protected:
121 virtual void propertyChanged(TextureProperty flag);
122
123 private:
124 void updateNumLevels();
125 void clearLevels();
126
127 void recreateImpl(bool enableUpload = true);
128
129 void dirtyAll();
130
131 // Update VRAM size tracking
132 void adjustVramSizeTracking(DeviceVRAM& vram, int64_t size);
133
134 static uint32_t _nextId;
135
136 std::string _name;
138
139 GraphicsDevice* _device;
140
141 uint32_t _width;
142 uint32_t _height;
143 uint32_t _depth;
144 uint32_t _arrayLength;
145
146 PixelFormat _format;
147
148 uint32_t _id;
149
150 bool _compressed;
151 bool _integerFormat;
152
153 FilterMode _minFilter;
154 FilterMode _magFilter;
155
156 bool _cubemap;
157 bool _volume;
158 bool _mipmaps;
159 bool _storage = false;
160
161 TextureProjection _projection;
162
163 uint32_t _numLevels = 0;
164 uint32_t _numLevelsRequested;
165
166 std::vector<std::vector<void*>> _levels;
167 std::vector<std::vector<size_t>> _levelDataSizes;
168 std::vector<std::vector<std::vector<uint8_t>>> _levelStorage;
169 std::vector<std::vector<bool>> _levelsUpdated;
170
171 std::unique_ptr<gpu::HardwareTexture> _impl;
172
173 bool _needsUpload = false;
174 bool _needsMipmapsUpload = false;
175 bool _mipmapsUploaded = false;
176
177 size_t _gpuSize = 0;
178
179 TexHint _profilerHint;
180
181 AddressMode _addressU = ADDRESS_REPEAT;
182 AddressMode _addressV = ADDRESS_REPEAT;
183 AddressMode _addressW = ADDRESS_REPEAT;
184
185 uint32_t _renderVersionDirty = 0; // Version tracking
186 };
187}
Abstract GPU interface for resource creation, state management, and draw submission.
size_t getLevelDataSize(uint32_t mipLevel, uint32_t face=0) const
Definition texture.cpp:155
Texture(GraphicsDevice *graphicsDevice, const TextureOptions &options=TextureOptions{})
Definition texture.cpp:19
GraphicsDevice * device() const
Definition texture.h:100
bool hasLevels() const
Definition texture.h:79
uint32_t width() const
Definition texture.h:63
PixelFormat format() const
Definition texture.h:97
bool isCubemap() const
Definition texture.h:85
uint32_t getNumLevels() const
Definition texture.h:80
void setLevelData(uint32_t mipLevel, const uint8_t *data, size_t dataSize, uint32_t face=0)
Definition texture.cpp:163
void * getLevel(uint32_t mipLevel) const
Definition texture.cpp:147
void setMinFilter(FilterMode filter)
Definition texture.cpp:271
void setAddressW(AddressMode address)
Definition texture.cpp:311
const std::string & name() const
Definition texture.h:103
bool isVolume() const
Definition texture.h:90
virtual void propertyChanged(TextureProperty flag)
Definition texture.cpp:323
uint32_t height() const
Definition texture.h:65
gpu::HardwareTexture * impl() const
Definition texture.h:101
uint32_t getArrayLength() const
Definition texture.h:92
bool isArray() const
Definition texture.h:69
void setMipmaps(bool mipmaps)
Definition texture.cpp:260
uint32_t depth() const
Definition texture.h:67
void resize(uint32_t width, uint32_t height, uint32_t depth=1)
Definition texture.cpp:219
TextureEncoding encoding() const
Definition texture.h:104
bool needsUpload() const
Definition texture.h:73
void setAddressU(AddressMode address)
Definition texture.cpp:295
void setNeedsUpload(const bool needsUpload)
Definition texture.h:76
void * getFaceData(uint32_t mipLevel, uint32_t face) const
Definition texture.cpp:201
void setNeedsMipmapsUpload(const bool needsMipmapsUpload)
Definition texture.h:77
bool storage() const
Definition texture.h:98
bool mipmaps() const
Definition texture.h:94
bool needsMipmapsUpload() const
Definition texture.h:74
void setMagFilter(FilterMode filter)
Definition texture.cpp:283
void setAddressV(AddressMode address)
Definition texture.cpp:303
void setEncoding(TextureEncoding value)
Definition texture.h:105
void * getArrayData(uint32_t mipLevel, uint32_t index) const
Definition texture.cpp:211
TextureProjection projection
Definition texture.h:38