Skip to content

Commit 88dab26

Browse files
committed
WIP: trying to fix the texture
- Swapped from inversing from the CPU to the GPU, so it goes faster - Next issue to fix is the Repeating
1 parent dc53bb5 commit 88dab26

7 files changed

Lines changed: 70 additions & 24 deletions

File tree

Core/Source/Lux/Asset/TextureImporter.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <iostream>
1010

1111
namespace Lux {
12-
Buffer TextureImporter::ToBufferFromFile(const std::filesystem::path& path, ImageFormat& outFormat, uint32_t& outWidth, uint32_t& outHeight)
12+
Buffer TextureImporter::ToBufferFromFile(const std::filesystem::path& path, ImageFormat& outFormat, uint32_t& outWidth, uint32_t& outHeight, bool flipVertically)
1313
{
1414
FileStatus fileStatus = FileSystem::TryOpenFileAndWait(path, 100);
1515
Buffer imageBuffer;
@@ -20,6 +20,7 @@ namespace Lux {
2020
void* tmp;
2121
size_t size = 0;
2222

23+
stbi_set_flip_vertically_on_load(flipVertically ? 1 : 0);
2324
if (stbi_is_hdr(pathString.c_str()))
2425
{
2526
tmp = stbi_loadf(pathString.c_str(), &width, &height, &channels, 4);
@@ -31,8 +32,7 @@ namespace Lux {
3132
}
3233
else
3334
{
34-
stbi_set_flip_vertically_on_load(1);
35-
tmp = stbi_load(pathString.c_str(), &width, &height, &channels, 4);
35+
tmp = stbi_load(pathString.c_str(), &width, &height, &channels, 4);
3636
if (tmp)
3737
{
3838
size = width * height * 4;
@@ -56,38 +56,44 @@ namespace Lux {
5656
return imageBuffer;
5757
}
5858

59-
Buffer TextureImporter::ToBufferFromMemory(Buffer buffer, ImageFormat& outFormat, uint32_t& outWidth, uint32_t& outHeight)
59+
Buffer TextureImporter::ToBufferFromMemory(Buffer buffer, ImageFormat& outFormat, uint32_t& outWidth, uint32_t& outHeight, bool flipVertically)
6060
{
6161
Buffer imageBuffer;
6262

6363
bool isSRGB = (outFormat == ImageFormat::SRGB) || (outFormat == ImageFormat::SRGBA);
6464

6565
int width, height, channels;
6666
void* tmp;
67-
size_t size;
67+
size_t size = 0;
6868

69+
stbi_set_flip_vertically_on_load(flipVertically ? 1 : 0);
6970
if (stbi_is_hdr_from_memory((const stbi_uc*)buffer.Data, (int)buffer.Size))
7071
{
7172
tmp = (byte*)stbi_loadf_from_memory((const stbi_uc*)buffer.Data, (int)buffer.Size, &width, &height, &channels, STBI_rgb_alpha);
72-
size = width * height * 4 * sizeof(float);
73-
outFormat = ImageFormat::RGBA32F;
73+
if (tmp)
74+
{
75+
size = width * height * 4 * sizeof(float);
76+
outFormat = ImageFormat::RGBA32F;
77+
}
7478
}
7579
else
7680
{
77-
stbi_set_flip_vertically_on_load(1);
78-
tmp = stbi_load_from_memory((const stbi_uc*)buffer.Data, (int)buffer.Size, &width, &height, &channels, STBI_rgb_alpha);
79-
size = width * height * 4;
80-
outFormat = isSRGB ? ImageFormat::SRGBA : ImageFormat::RGBA;
81+
tmp = stbi_load_from_memory((const stbi_uc*)buffer.Data, (int)buffer.Size, &width, &height, &channels, STBI_rgb_alpha);
82+
if (tmp)
83+
{
84+
size = width * height * 4;
85+
outFormat = isSRGB ? ImageFormat::SRGBA : ImageFormat::RGBA;
86+
}
8187
}
8288

89+
if (!tmp)
90+
return {};
91+
8392
imageBuffer.Data = new byte[size]; // avoid `malloc+delete[]` mismatch.
8493
imageBuffer.Size = size;
8594
memcpy(imageBuffer.Data, tmp, size);
8695
stbi_image_free(tmp);
8796

88-
if (!imageBuffer.Data)
89-
return {};
90-
9197
outWidth = width;
9298
outHeight = height;
9399
return imageBuffer;

Core/Source/Lux/Asset/TextureImporter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ namespace Lux {
1111
class TextureImporter
1212
{
1313
public:
14-
static Buffer ToBufferFromFile(const std::filesystem::path& path, ImageFormat& outFormat, uint32_t& outWidth, uint32_t& outHeight);
15-
static Buffer ToBufferFromMemory(Buffer buffer, ImageFormat& outFormat, uint32_t& outWidth, uint32_t& outHeight);
14+
static Buffer ToBufferFromFile(const std::filesystem::path& path, ImageFormat& outFormat, uint32_t& outWidth, uint32_t& outHeight, bool flipVertically = true);
15+
static Buffer ToBufferFromMemory(Buffer buffer, ImageFormat& outFormat, uint32_t& outWidth, uint32_t& outHeight, bool flipVertically = true);
1616
private:
1717
const std::filesystem::path m_Path;
1818
};

Core/Source/Lux/Asset/TextureSerializer.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,36 @@
1111

1212
namespace Lux
1313
{
14+
namespace
15+
{
16+
bool PathStartsWith(const std::filesystem::path& path, const std::filesystem::path& directory)
17+
{
18+
const std::filesystem::path normalizedPath = path.lexically_normal();
19+
const std::filesystem::path normalizedDirectory = directory.lexically_normal();
20+
21+
auto pathIt = normalizedPath.begin();
22+
for (auto directoryIt = normalizedDirectory.begin(); directoryIt != normalizedDirectory.end(); ++directoryIt, ++pathIt)
23+
{
24+
if (pathIt == normalizedPath.end() || *pathIt != *directoryIt)
25+
return false;
26+
}
27+
28+
return true;
29+
}
30+
31+
bool IsMeshSourceTexture(const std::filesystem::path& assetPath)
32+
{
33+
Ref<Project> project = Project::GetActive();
34+
if (!project)
35+
return false;
36+
37+
const std::filesystem::path meshSourcePath = project->GetConfig().MeshSourcePath;
38+
if (meshSourcePath.empty())
39+
return false;
40+
41+
return PathStartsWith(assetPath, meshSourcePath);
42+
}
43+
}
1444
void TextureSerializer::Serialize(const AssetMetadata& metadata, const Ref<Asset>& asset) const
1545
{
1646
// Source-format textures (.png/.jpg/…) are their own serialized form – nothing to write back.
@@ -38,6 +68,11 @@ namespace Lux
3868
}
3969

4070
TextureSpecification spec;
71+
if (IsMeshSourceTexture(metadata.FilePath))
72+
{
73+
spec.FlipVertically = false;
74+
spec.MaxAnisotropy = 16.0f;
75+
}
4176
Ref<Texture2D> texture = Texture2D::Create(spec, filepath);
4277
if (!texture || !texture->Loaded())
4378
return false;

Core/Source/Lux/Renderer/Texture.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ namespace Lux {
105105
spec.GenerateMips = false; // SRGBA cannot be used as storage image for compute-based mip generation
106106
spec.SamplerWrap = texture->m_Specification.SamplerWrap;
107107
spec.SamplerFilter = texture->m_Specification.SamplerFilter;
108+
spec.MaxAnisotropy = texture->m_Specification.MaxAnisotropy;
108109

109110
// Create the SRGBA image with all mip levels pre-allocated (matching source texture)
110111
ImageSpecification imageSpec;
@@ -132,6 +133,7 @@ namespace Lux {
132133
samplerDesc.minFilter = samplerDesc.magFilter = samplerDesc.mipFilter = Utils::NVRHISamplerFilter(spec.SamplerFilter);
133134
samplerDesc.addressU = Utils::NVRHISamplerWrap(spec.SamplerWrap);
134135
samplerDesc.addressV = samplerDesc.addressW = samplerDesc.addressU;
136+
samplerDesc.maxAnisotropy = spec.MaxAnisotropy;
135137
srgbImage->GetImageInfo().Sampler = device->createSampler(samplerDesc);
136138

137139
// Copy all mip levels from source texture to SRGBA texture using GPU copy
@@ -165,12 +167,12 @@ namespace Lux {
165167

166168
//specification.GenerateMips = true;
167169

168-
m_ImageData = TextureImporter::ToBufferFromFile(filepath, m_Specification.Format, m_Specification.Width, m_Specification.Height);
170+
m_ImageData = TextureImporter::ToBufferFromFile(filepath, m_Specification.Format, m_Specification.Width, m_Specification.Height, m_Specification.FlipVertically);
169171
if (!m_ImageData)
170172
{
171173
// TODO(Yan): move this to asset manager
172174
LUX_CORE_ERROR("Failed to load texture from file: {}", filepath);
173-
m_ImageData = TextureImporter::ToBufferFromFile("Resources/Textures/ErrorTexture.png", m_Specification.Format, m_Specification.Width, m_Specification.Height);
175+
m_ImageData = TextureImporter::ToBufferFromFile("Resources/Textures/ErrorTexture.png", m_Specification.Format, m_Specification.Width, m_Specification.Height, m_Specification.FlipVertically);
174176
}
175177

176178
ImageSpecification imageSpec;
@@ -192,12 +194,12 @@ namespace Lux {
192194
{
193195
Utils::ValidateSpecification(specification);
194196

195-
m_ImageData = TextureImporter::ToBufferFromFile(filepath, m_Specification.Format, m_Specification.Width, m_Specification.Height);
197+
m_ImageData = TextureImporter::ToBufferFromFile(filepath, m_Specification.Format, m_Specification.Width, m_Specification.Height, m_Specification.FlipVertically);
196198
if (!m_ImageData)
197199
{
198200
// TODO(Yan): move this to asset manager
199201
LUX_CORE_ERROR("Failed to load texture from file: {}", filepath);
200-
m_ImageData = TextureImporter::ToBufferFromFile("Resources/Textures/ErrorTexture.png", m_Specification.Format, m_Specification.Width, m_Specification.Height);
202+
m_ImageData = TextureImporter::ToBufferFromFile("Resources/Textures/ErrorTexture.png", m_Specification.Format, m_Specification.Width, m_Specification.Height, m_Specification.FlipVertically);
201203
}
202204

203205
ImageSpecification imageSpec;
@@ -225,11 +227,11 @@ namespace Lux {
225227

226228
if (m_Specification.Height == 0)
227229
{
228-
m_ImageData = TextureImporter::ToBufferFromMemory(Buffer(data.Data, m_Specification.Width), m_Specification.Format, m_Specification.Width, m_Specification.Height);
230+
m_ImageData = TextureImporter::ToBufferFromMemory(Buffer(data.Data, m_Specification.Width), m_Specification.Format, m_Specification.Width, m_Specification.Height, m_Specification.FlipVertically);
229231
if (!m_ImageData)
230232
{
231233
// TODO(Yan): move this to asset manager
232-
m_ImageData = TextureImporter::ToBufferFromFile("Resources/Textures/ErrorTexture.png", m_Specification.Format, m_Specification.Width, m_Specification.Height);
234+
m_ImageData = TextureImporter::ToBufferFromFile("Resources/Textures/ErrorTexture.png", m_Specification.Format, m_Specification.Width, m_Specification.Height, m_Specification.FlipVertically);
233235
}
234236

235237
Utils::ValidateSpecification(m_Specification);
@@ -325,6 +327,7 @@ namespace Lux {
325327
samplerDesc.minFilter = samplerDesc.magFilter = samplerDesc.mipFilter = Utils::NVRHISamplerFilter(m_Specification.SamplerFilter);
326328
samplerDesc.addressU = Utils::NVRHISamplerWrap(m_Specification.SamplerWrap);
327329
samplerDesc.addressV = samplerDesc.addressW = samplerDesc.addressU;
330+
samplerDesc.maxAnisotropy = m_Specification.MaxAnisotropy;
328331

329332
info.Sampler = device->createSampler(samplerDesc);
330333

Core/Source/Lux/Renderer/Texture.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ namespace Lux {
1818
uint32_t Height = 1;
1919
TextureWrap SamplerWrap = TextureWrap::Repeat;
2020
TextureFilter SamplerFilter = TextureFilter::Linear;
21+
float MaxAnisotropy = 1.0f;
2122

2223
bool GenerateMips = true;
2324
bool Storage = false;
2425
bool StoreLocally = false;
26+
bool FlipVertically = true;
2527

2628
std::string DebugName;
2729
};

Editor/Resources/Shaders/LuxPBR_Static.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void main()
5555

5656
Output.WorldPosition = worldPosition.xyz;
5757
Output.Normal = mat3(transform) * a_Normal;
58-
Output.TexCoord = a_TexCoord;
58+
Output.TexCoord = vec2(a_TexCoord.x, 1.0 - a_TexCoord.y);
5959
Output.WorldNormals = mat3(transform) * mat3(a_Tangent, a_Binormal, a_Normal);
6060
Output.WorldTransform = mat3(transform);
6161
Output.Binormal = a_Binormal;

Editor/Resources/Shaders/LuxPBR_Transparent.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void main()
5454

5555
Output.WorldPosition = worldPosition.xyz;
5656
Output.Normal = mat3(transform) * a_Normal;
57-
Output.TexCoord = a_TexCoord;
57+
Output.TexCoord = vec2(a_TexCoord.x, 1.0 - a_TexCoord.y);
5858
Output.WorldNormals = mat3(transform) * mat3(a_Tangent, a_Binormal, a_Normal);
5959
Output.WorldTransform = mat3(transform);
6060
Output.Binormal = a_Binormal;

0 commit comments

Comments
 (0)