13
13
#include " Context.h"
14
14
#include " render/renderer/buffer/Buffer.h"
15
15
#include " utils/Descriptor.h"
16
+ #include " utils/TypeAdaptor.h"
16
17
17
18
#define STB_IMAGE_IMPLEMENTATION
18
19
#include < stb_image.h>
21
22
22
23
namespace Siege ::Vulkan
23
24
{
24
- Texture2D::Texture2D (const char * name, Usage texUsage)
25
+ Texture2D::Texture2D (const char * name, Utils::TextureFilter filter, Usage texUsage)
25
26
{
26
27
LoadTexture (Constants::DEFAULT_TEXTURE_2D, Constants::DEFAULT_TEXTURE_SIZE, 16 , 16 , texUsage);
27
28
28
- VkSamplerCreateInfo samplerInfo = Utils::Descriptor::SamplerCreateInfo (VK_FILTER_LINEAR);
29
+ VkSamplerCreateInfo samplerInfo =
30
+ Utils::Descriptor::SamplerCreateInfo (Utils::ToVkFilter (filter));
29
31
30
32
info = {image.GetInfo ()};
31
33
@@ -37,11 +39,12 @@ Texture2D::Texture2D(const char* name, Usage texUsage)
37
39
info.usage = texUsage;
38
40
}
39
41
40
- Texture2D::Texture2D (const char * name, const char * filePath)
42
+ Texture2D::Texture2D (const char * name, const char * filePath, Utils::TextureFilter filter )
41
43
{
42
44
LoadFromFile (filePath);
43
45
44
- VkSamplerCreateInfo samplerInfo = Utils::Descriptor::SamplerCreateInfo (VK_FILTER_LINEAR);
46
+ VkSamplerCreateInfo samplerInfo =
47
+ Utils::Descriptor::SamplerCreateInfo (Utils::ToVkFilter (filter));
45
48
46
49
info = {image.GetInfo ()};
47
50
@@ -55,11 +58,13 @@ Texture2D::Texture2D(const char* name,
55
58
size_t size,
56
59
uint32_t width,
57
60
uint32_t height,
61
+ Utils::TextureFilter filter,
58
62
Usage texUsage)
59
63
{
60
64
LoadTexture (pixels, size, width, height, texUsage);
61
65
62
- VkSamplerCreateInfo samplerInfo = Utils::Descriptor::SamplerCreateInfo (VK_FILTER_LINEAR);
66
+ VkSamplerCreateInfo samplerInfo =
67
+ Utils::Descriptor::SamplerCreateInfo (Utils::ToVkFilter (filter));
63
68
64
69
info = {image.GetInfo ()};
65
70
@@ -91,6 +96,10 @@ void Texture2D::LoadFromFile(const char* filePath)
91
96
int texWidth, texHeight, texChannels;
92
97
93
98
stbi_uc* pixels = stbi_load (filePath, &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
99
+ defer ([pixels] {
100
+ stbi_image_free (pixels);
101
+ ;
102
+ });
94
103
95
104
CC_ASSERT (pixels, " Failed to load image file!" )
96
105
@@ -104,6 +113,8 @@ void Texture2D::LoadFromFile(const char* filePath)
104
113
memcpy (pixelPtr, pixels, sizeof (uint8_t ) * imageSize);
105
114
106
115
Buffer::Buffer stagingBuffer;
116
+ defer ([&stagingBuffer] { Buffer::DestroyBuffer (stagingBuffer); });
117
+
107
118
Buffer::CreateBuffer (imageSize,
108
119
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
109
120
// specifies that data is accessible on the CPU.
@@ -115,8 +126,6 @@ void Texture2D::LoadFromFile(const char* filePath)
115
126
116
127
Buffer::CopyData (stagingBuffer, imageSize, pixelPtr);
117
128
118
- stbi_image_free (pixels);
119
-
120
129
extent = {static_cast <uint32_t >(texWidth), static_cast <uint32_t >(texHeight)};
121
130
122
131
Utils::Extent3D imageExtent {static_cast <uint32_t >(texWidth),
@@ -125,8 +134,6 @@ void Texture2D::LoadFromFile(const char* filePath)
125
134
image = Image ({Utils::RGBASRGB, imageExtent, Vulkan::Utils::USAGE_TEXTURE, 1 , 1 });
126
135
127
136
image.CopyBuffer (stagingBuffer.buffer , imageExtent);
128
-
129
- Buffer::DestroyBuffer (stagingBuffer);
130
137
}
131
138
132
139
void Texture2D::LoadTexture (const uint8_t * pixels,
0 commit comments