LkEngine 0.1.2
 
Loading...
Searching...
No Matches
Image.h
1#pragma once
2
3#include <filesystem>
4
5//#include <stb/stb_image.h>
6//#include <stb/stb_image_resize2.h>
7
9#include "LkEngine/Core/LObject/ObjectPtr.h"
10#include "LkEngine/Core/Memory/Buffer.h"
11
12#include "BlendingSpecification.h"
13#include "Color.h"
14#include "TextureEnum.h"
15
16#include "LkEngine/Core/Event/AssetEvent.h"
17
18namespace LkEngine {
19
21 {
22 std::string Path{};
23 std::string Name{};
24 uint32_t Width = 1;
25 uint32_t Height = 1;
26 bool bGenerateMips = true;
27 bool bFlipVertical = false;
28
29 EImageFormat Format = EImageFormat::RGBA;
30 ETextureWrap SamplerWrap = ETextureWrap::Clamp;
31 ETextureFilter SamplerFilter = ETextureFilter::Linear;
32
33 ETextureUniformType UniformType = ETextureUniformType::Diffuse;
34
35 bool bStorage = false;
36 bool bStoreLocally = false;
37
42 bool bUseReadDimensions = false;
43
44 std::string DebugName{};
45 };
46
48 {
49 uint32_t BaseMip = 0;
50 uint32_t MipCount = UINT_MAX;
51 uint32_t BaseLayer = 0;
52 uint32_t LayerCount = UINT_MAX;
53 };
54
56 {
57 glm::vec4 FloatValues;
58 glm::ivec4 IntValues;
59 glm::uvec4 UIntValues;
60 };
61
62 namespace ImageUtils
63 {
64 FORCEINLINE uint32_t GetMemorySize(const EImageFormat ImageFormat, const uint32_t Width, const uint32_t Height);
65 FORCEINLINE uint32_t CalculateMipCount(const uint32_t Width, const uint32_t Height);
66 FORCEINLINE uint32_t BytesPerPixel(const EImageFormat ImageFormat);
67 }
68
70 {
71 uint32_t Width = 1;
72 uint32_t Height = 1;
73 uint32_t Mips = 1;
74 uint32_t Layers = 1;
75 std::string Path = "";
76 uint64_t Size = 0;
77
78 EImageFormat Format = EImageFormat::RGBA;
79 EImageUsage Usage = EImageUsage::Texture;
80 ETextureWrap Wrap = ETextureWrap::Clamp;
81 ETextureFilter Filter = ETextureFilter::Linear;
82 ETextureAnistropicFiltering AnistropicFiltering = ETextureAnistropicFiltering::Trilnear;
83
84 bool bDeinterleaved = false;
85 bool bTransfer = false;
86 bool bFlipVertical = false;
87
88 std::string Name{};
89 std::string DebugName{};
90
91 FImageSpecification() = default;
92 FImageSpecification(const FTextureSpecification& InSpecification)
93 : Width(InSpecification.Width)
94 , Height(InSpecification.Height)
95 , Path(InSpecification.Path)
96 , Format(InSpecification.Format)
97 , Filter(InSpecification.SamplerFilter)
98 , Wrap(InSpecification.SamplerWrap)
99 , Name(InSpecification.Name)
100 , DebugName(InSpecification.DebugName)
101 {
102 InSpecification.bGenerateMips
103 ? Mips = ImageUtils::CalculateMipCount(InSpecification.Width, InSpecification.Height)
104 : Mips = 1;
105 Size = ImageUtils::GetMemorySize(InSpecification.Format, InSpecification.Width, InSpecification.Height);
106 }
107 ~FImageSpecification() = default;
108 };
109
110 //-------------------------------------------------------------------------------
111 // Image
112 //-------------------------------------------------------------------------------
113 class LImage : public LObject
114 {
115 public:
116 virtual ~LImage() = default;
117
118 virtual void SetData(const void* InData) = 0;
119 virtual void Resize(const uint32_t InWidth, const uint32_t InHeight) = 0;
120
121 virtual FBuffer GetBuffer() const = 0;
122 virtual FBuffer& GetBuffer() = 0;
123
124 virtual void Invalidate() = 0;
125 virtual void RT_Invalidate() = 0;
126
127 virtual LRendererID& GetRendererID() = 0;
128 virtual LRendererID GetRendererID() const = 0;
129
130 virtual uint32_t GetWidth() const = 0;
131 virtual uint32_t GetHeight() const = 0;
132
133 virtual const FImageSpecification& GetSpecification() const = 0;
134
135 virtual void Release() = 0;
136 virtual void AllocateMemory(const uint64_t InSize) = 0;
137
139 static TObjectPtr<LImage> Create(const FImageSpecification& InSpecification, FBuffer InBuffer);
140 static TObjectPtr<LImage> Create(const FImageSpecification& InSpecification, void* InData = nullptr);
141 };
142
143
147 class LImage2D : public LImage
148 {
149 public:
150 virtual ~LImage2D() = default;
151
152 virtual void Resize(const uint32_t InWidth, const uint32_t InHeight) = 0;
153
155 static TObjectPtr<LImage2D> Create(const FImageSpecification& InSpecification, FBuffer InBuffer);
156 static TObjectPtr<LImage2D> Create(const FImageSpecification& InSpecification, void* InData = nullptr);
157 };
158
159
160 namespace ImageUtils
161 {
162 FORCEINLINE int64_t GetImageFormat(const EImageFormat ImageFormat)
163 {
164 switch (ImageFormat)
165 {
166 case EImageFormat::RGBA: return 4;
167 case EImageFormat::RGBA32F: return 16;
168 case EImageFormat::None: return 0;
169 }
170
171 return 0;
172 }
173
174 FORCEINLINE uint32_t GetFormatBPP(const EImageFormat ImageFormat)
175 {
176 switch (ImageFormat)
177 {
178 case EImageFormat::RGB:
179 case EImageFormat::RGBA8: return 4;
180 case EImageFormat::RGBA: return 4;
181 case EImageFormat::RGBA16F: return 2 * 4;
182 case EImageFormat::RGBA32F: return 4 * 4;
183 }
184
185 LK_CORE_ASSERT(false, "GetFormatBPP failed, format not recognized");
186 return 0;
187 }
188
189 FORCEINLINE uint32_t GetMemorySize(const EImageFormat ImageFormat, const uint32_t Width, const uint32_t Height)
190 {
191 return (Width * Height * GetFormatBPP(ImageFormat));
192 }
193
194 FORCEINLINE uint32_t BytesPerPixel(const EImageFormat ImageFormat)
195 {
196 switch (ImageFormat)
197 {
198 case EImageFormat::RGBA: return 4;
199 case EImageFormat::RGBA32F: return 16;
200 case EImageFormat::None: return 0;
201 }
202
203 return 0;
204 }
205
206 FORCEINLINE uint32_t CalculateMipCount(const uint32_t Width, const uint32_t Height)
207 {
208 return static_cast<uint32_t>(std::floor(std::log2(glm::min(Width, Height))) + 1);
209 }
210
211 FORCEINLINE bool IsDepthFormat(const EImageFormat ImageFormat)
212 {
213 switch (ImageFormat)
214 {
215 case EImageFormat::DEPTH24STENCIL8: return true;
216 }
217
218 return false;
219 }
220
221 }
222
223 namespace Enum
224 {
225 static const char* ToString(const EImageFormat ImageFormat)
226 {
227 switch (ImageFormat)
228 {
229 case EImageFormat::RG8: return "RG8";
230 case EImageFormat::RGB: return "RGB";
231 case EImageFormat::RGBA: return "RGBA";
232 case EImageFormat::RG16F: return "RG16F";
233 case EImageFormat::RGBA8: return "RGBA8";
234 case EImageFormat::RGBA16F: return "RGBA16F";
235 case EImageFormat::RGBA32F: return "RGBA32F";
236 case EImageFormat::SRGB: return "SRGB";
237 case EImageFormat::SRGBA: return "SRGBA";
238 case EImageFormat::RED32F: return "RED32F";
239 case EImageFormat::RED8UI: return "RED8UI";
240 case EImageFormat::RED8UN: return "RED8UN";
241 case EImageFormat::RED16UI: return "RED16UI";
242 case EImageFormat::RED32UI: return "RED32UI";
243 case EImageFormat::B10R11G11UF: return "B10R11G11UF";
244 case EImageFormat::DEPTH24STENCIL8: return "DEPTH24STENCIL8";
245 }
246
247 LK_CORE_ASSERT(false, "Invalid image format");
248 return "";
249 }
250 }
251
252}
LObject implementation.
Definition Image.h:148
static TObjectPtr< LImage2D > Create(const FImageSpecification &InSpecification, FBuffer InBuffer)
Definition Image.cpp:41
Definition Image.h:114
static TObjectPtr< LImage > Create(const FImageSpecification &InSpecification, FBuffer InBuffer)
Definition Image.cpp:11
Definition Object.h:46
Definition ObjectPtr.h:102
uint32_t LRendererID
Definition CoreTypes.h:30
Definition Asset.h:11
Definition Buffer.h:9
Definition Image.h:70
Definition Image.h:21
bool bUseReadDimensions
Definition Image.h:42
Definition Image.h:56