-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTexture.cpp
More file actions
220 lines (202 loc) · 5.57 KB
/
Copy pathTexture.cpp
File metadata and controls
220 lines (202 loc) · 5.57 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "Texture.h"
#include "VulkanContext.h"
#include "VulkanImageBuffer.h"
#include "Core/ThreadSyncManager.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <External/stb/stb_image_write.h>
#include <cstring>
namespace sh::render
{
Texture::Texture(TextureFormat format, uint32_t width, uint32_t height, bool bUseMipmap) :
context(nullptr),
format(format), width(width), height(height),
aniso(1)
{
bSRGB = CheckSRGB();
bGenerateMipmap = bUseMipmap;
uint32_t mipLevels = bUseMipmap ? static_cast<uint32_t>(std::floor(std::log2(std::max(width, height)))) + 1 : 1;
pixels.resize(mipLevels);
int mipWidth = width;
int mipHeight = height;
for (int i = 0; i < mipLevels; ++i)
{
pixels[i].resize(mipWidth * mipHeight * GetTextureFormatPixelSize(format));
mipWidth = std::max(1, mipWidth / 2);
mipHeight = std::max(1, mipHeight / 2);
}
}
Texture::Texture(Texture&& other) noexcept :
context(other.context),
format(other.format), width(other.width), height(other.height),
pixels(std::move(other.pixels)), textureBuffer(std::move(other.textureBuffer)),
onBufferUpdate(std::move(other.onBufferUpdate)),
aniso(other.aniso),
bSRGB(other.bSRGB), bSetDataDirty(other.bSetDataDirty)
{
if (other.bDirty.test_and_set(std::memory_order::memory_order_acquire))
bDirty.test_and_set(std::memory_order::memory_order_relaxed);
}
Texture::~Texture() = default;
SH_RENDER_API void Texture::SyncDirty()
{
if (!bDirty.test_and_set(std::memory_order::memory_order_acquire))
core::ThreadSyncManager::PushSyncable(*this);
}
SH_RENDER_API void Texture::Sync()
{
if (bSetDataDirty)
{
CreateTextureBuffer();
bSetDataDirty = false;
}
bDirty.clear(std::memory_order::memory_order_relaxed);
}
SH_RENDER_API void Texture::OnPropertyChanged(const core::reflection::Property& prop)
{
if (prop.GetName() == core::Util::ConstexprHash("aniso"))
{
SetAnisoLevel(aniso);
}
else if (prop.GetName() == core::Util::ConstexprHash("bSRGB"))
{
SetSRGB(bSRGB);
}
else if (prop.GetName() == core::Util::ConstexprHash("bGenerateMipmap"))
{
SetGenerateMipmap(bGenerateMipmap);
}
else if (prop.GetName() == core::Util::ConstexprHash("filtering"))
{
SetFiltering(filtering);
}
}
SH_RENDER_API void Texture::SetPixelData(std::vector<uint8_t> pixels, uint32_t mipLevel)
{
assert(this->pixels[mipLevel].size() == pixels.size());
this->pixels[mipLevel] = std::move(pixels);
if (textureBuffer != nullptr)
{
bSetDataDirty = true;
SyncDirty();
}
}
SH_RENDER_API void Texture::SetPixelData(const uint8_t* pixels, std::size_t size, uint32_t mipLevel)
{
assert(this->pixels[mipLevel].size() == size);
this->pixels[mipLevel].resize(size);
std::memcpy(this->pixels[mipLevel].data(), pixels, size);
if (textureBuffer != nullptr)
{
bSetDataDirty = true;
SyncDirty();
}
}
SH_RENDER_API auto Texture::GetPixelData(uint32_t mipLevel) const -> const std::vector<Byte>&
{
assert(pixels.size() > mipLevel);
return pixels[mipLevel];
}
SH_RENDER_API void Texture::Build(const IRenderContext& context)
{
if (textureBuffer == nullptr)
{
this->context = &context;
CreateTextureBuffer();
}
}
SH_RENDER_API auto Texture::GetTextureBuffer() const -> ITextureBuffer*
{
return textureBuffer.get();
}
SH_RENDER_API void Texture::ChangeTextureFormat(TextureFormat target)
{
if (format == target)
return;
format = target;
bSRGB = CheckSRGB();
if (textureBuffer != nullptr)
{
bSetDataDirty = true;
SyncDirty();
}
}
SH_RENDER_API auto Texture::GetMipLevel() const -> uint32_t
{
if (!bGenerateMipmap)
return 1;
return pixels.size();
}
SH_RENDER_API void sh::render::Texture::SetAnisoLevel(uint32_t aniso)
{
this->aniso = aniso;
if (textureBuffer != nullptr)
{
bSetDataDirty = true;
SyncDirty();
}
}
SH_RENDER_API void Texture::SetGenerateMipmap(bool bGenerate)
{
bGenerateMipmap = bGenerate;
bSetDataDirty = true;
SyncDirty();
}
SH_RENDER_API void Texture::SetSRGB(bool bSRGB)
{
if (bSRGB)
{
if (format == TextureFormat::RGB24)
ChangeTextureFormat(TextureFormat::SRGB24);
else if (format == TextureFormat::RGBA32)
ChangeTextureFormat(TextureFormat::SRGBA32);
}
else
{
if (format == TextureFormat::SRGB24)
ChangeTextureFormat(TextureFormat::RGB24);
else if (format == TextureFormat::SRGBA32)
ChangeTextureFormat(TextureFormat::RGBA32);
}
}
SH_RENDER_API void Texture::SetFiltering(Filtering filter)
{
filtering = filter;
if (textureBuffer != nullptr)
{
bSetDataDirty = true;
SyncDirty();
}
}
SH_RENDER_API void Texture::ExportToPNG(const std::filesystem::path& path)
{
stbi_write_png(path.u8string().c_str(), width, height, GetTextureFormatChannel(format), pixels[0].data(), width * GetTextureFormatChannel(format));
}
void Texture::CreateTextureBuffer()
{
if (textureBuffer == nullptr)
{
if (context->GetRenderAPIType() == RenderAPI::Vulkan)
textureBuffer = std::make_unique<vk::VulkanImageBuffer>();
}
ITextureBuffer::CreateInfo ci{};
ci.width = width;
ci.height = height;
ci.format = format;
ci.aniso = aniso;
ci.filtering = static_cast<uint32_t>(filtering);
ci.mipLevel = pixels.size();
ci.bMSAAImg = false;
ci.bRenderTarget = false;
textureBuffer->Create(*context, ci);
const int mipLevel = bGenerateMipmap ? pixels.size() : 1;
for (int m = 0; m < mipLevel; ++m)
textureBuffer->SetData(pixels[m].data(), m);
onBufferUpdate.Notify(this);
}
auto Texture::CheckSRGB() const -> bool
{
if (format == TextureFormat::SRGB24 || format == TextureFormat::SRGBA32)
return true;
return false;
}
}//namespace