-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture2D.cpp
More file actions
206 lines (157 loc) · 6.34 KB
/
Texture2D.cpp
File metadata and controls
206 lines (157 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//
// Copyright (c) 2020-present Caps Collective & contributors
// Originally authored by Jonathan Moallem (@jonjondev) & Aryeh Zinn (@Raelr)
//
// This code is released under an unmodified zlib license.
// For conditions of distribution and use, please see:
// https://opensource.org/licenses/Zlib
//
#include "Texture2D.h"
#include <resources/PackFile.h>
#include <resources/ResourceSystem.h>
#include <resources/Texture2DData.h>
#include <utils/Defer.h>
#include "Constants.h"
#include "Context.h"
#include "render/renderer/buffer/Buffer.h"
#include "utils/Descriptor.h"
#include "utils/TypeAdaptor.h"
namespace Siege::Vulkan
{
Texture2D::Texture2D(const char* name, Utils::TextureFilter filter, Usage texUsage)
{
LoadTexture(Constants::DEFAULT_TEXTURE_2D, Constants::DEFAULT_TEXTURE_SIZE, 16, 16, texUsage);
VkSamplerCreateInfo samplerInfo =
Utils::Descriptor::SamplerCreateInfo(Utils::ToVkFilter(filter));
info = {image.GetInfo()};
vkCreateSampler(Context::GetVkLogicalDevice(), &samplerInfo, nullptr, &info.sampler);
// TODO(Aryeh): Make this configurable
id = INTERN_STR(name);
info.usage = texUsage;
}
Texture2D::Texture2D(const char* name, const char* filePath, Utils::TextureFilter filter)
{
LoadFromFile(filePath);
VkSamplerCreateInfo samplerInfo =
Utils::Descriptor::SamplerCreateInfo(Utils::ToVkFilter(filter));
info = {image.GetInfo()};
vkCreateSampler(Context::GetVkLogicalDevice(), &samplerInfo, nullptr, &info.sampler);
id = INTERN_STR(name);
}
Texture2D::Texture2D(const char* name,
const uint8_t* pixels,
size_t size,
uint32_t width,
uint32_t height,
Utils::TextureFilter filter,
Usage texUsage)
{
LoadTexture(pixels, size, width, height, texUsage);
VkSamplerCreateInfo samplerInfo =
Utils::Descriptor::SamplerCreateInfo(Utils::ToVkFilter(filter));
info = {image.GetInfo()};
vkCreateSampler(Context::GetVkLogicalDevice(), &samplerInfo, nullptr, &info.sampler);
info.usage = texUsage;
id = INTERN_STR(name);
}
Texture2D::Texture2D(Texture2D&& other)
{
Swap(other);
}
Texture2D::~Texture2D()
{
Free();
}
Texture2D& Texture2D::operator=(Texture2D&& other)
{
Swap(other);
return *this;
}
void Texture2D::LoadFromFile(const char* filePath)
{
PackFile* packFile = ResourceSystem::GetInstance().GetPackFile();
std::shared_ptr<Texture2DData> texture2dData =
packFile->FindDataDeserialised<Texture2DData>(filePath);
Buffer::Buffer stagingBuffer;
defer([&stagingBuffer] { Buffer::DestroyBuffer(stagingBuffer); });
Buffer::CreateBuffer(texture2dData->GetImageSize(),
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
// specifies that data is accessible on the CPU.
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
// Ensures that CPU and GPU memory are consistent across both devices.
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
OUT stagingBuffer.buffer,
OUT stagingBuffer.bufferMemory);
Buffer::CopyData(stagingBuffer, texture2dData->GetImageSize(), texture2dData->pixels.data());
extent = {static_cast<uint32_t>(texture2dData->texWidth),
static_cast<uint32_t>(texture2dData->texHeight)};
Utils::Extent3D imageExtent {static_cast<uint32_t>(texture2dData->texWidth),
static_cast<uint32_t>(texture2dData->texHeight),
1};
image = Image({Utils::RGBASRGB, imageExtent, Vulkan::Utils::USAGE_TEXTURE, 1, 1});
image.CopyBuffer(stagingBuffer.buffer, imageExtent);
}
void Texture2D::LoadTexture(const uint8_t* pixels,
size_t size,
uint32_t width,
uint32_t height,
Usage texUsage)
{
Buffer::Buffer stagingBuffer;
Buffer::CreateBuffer(size,
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
// specifies that data is accessible on the CPU.
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
// Ensures that CPU and GPU memory are consistent across both devices.
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
OUT stagingBuffer.buffer,
OUT stagingBuffer.bufferMemory);
Buffer::CopyData(stagingBuffer, size, pixels);
extent = {width, height};
Utils::Extent3D imageExtent {width, height, 1};
image = Image({(Utils::ImageFormat) texUsage, imageExtent, Vulkan::Utils::USAGE_TEXTURE, 1, 1});
image.CopyBuffer(stagingBuffer.buffer, imageExtent);
Buffer::DestroyBuffer(stagingBuffer);
}
void Texture2D::CopyToRegion(uint8_t* pixels,
unsigned long size,
Utils::Extent3D copyExtent,
Utils::Offset3D copyOffset)
{
Buffer::Buffer stagingBuffer;
defer([&stagingBuffer]() { Buffer::DestroyBuffer(stagingBuffer); });
Buffer::CreateBuffer(size,
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
// specifies that data is accessible on the CPU.
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
// Ensures that CPU and GPU memory are consistent across both devices.
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
OUT stagingBuffer.buffer,
OUT stagingBuffer.bufferMemory);
Buffer::CopyData(stagingBuffer, size, pixels);
image.CopyBuffer(stagingBuffer.buffer, copyExtent, copyOffset);
}
void Texture2D::Free()
{
auto device = Context::GetVkLogicalDevice();
if (device == nullptr) return;
vkDestroySampler(device, info.sampler, nullptr);
image.Free();
info = {{}, nullptr};
}
void Texture2D::Swap(Texture2D& other)
{
auto tmpId = id;
auto tmpImage = std::move(image);
auto tmpInfo = info;
auto tmpExtent = extent;
id = other.id;
image = std::move(other.image);
info = other.info;
extent = other.extent;
other.id = tmpId;
other.image = std::move(tmpImage);
other.info = tmpInfo;
other.extent = tmpExtent;
}
} // namespace Siege::Vulkan