Lighting still looks horrible, but whatever for now

This commit is contained in:
Dynamitos
2024-05-04 09:25:13 +02:00
parent 247d6a54fb
commit f0fd9a7ae7
24 changed files with 563 additions and 386 deletions
+104 -98
View File
@@ -94,12 +94,18 @@ void MeshLoader::loadTextures(const aiScene* scene, const std::filesystem::path&
constexpr const char* KEY_DIFFUSE_COLOR = "k_d";
constexpr const char* KEY_SPECULAR_COLOR = "k_s";
constexpr const char* KEY_AMBIENT_COLOR = "k_a";
constexpr const char* KEY_NORMAL = "n";
constexpr const char* KEY_SHININESS = "k_shiny";
constexpr const char* KEY_ROUGHNESS = "k_r";
constexpr const char* KEY_METALLIC = "k_m";
constexpr const char* KEY_DIFFUSE_TEXTURE = "tex_d";
constexpr const char* KEY_SPECULAR_TEXTURE = "tex_s";
constexpr const char* KEY_AMBIENT_TEXTURE = "tex_a";
constexpr const char* KEY_NORMAL_TEXTURE = "tex_n";
constexpr const char* KEY_SHININESS_TEXTURE = "tex_shiny";
constexpr const char* KEY_ROUGHNESS_TEXTURE = "tex_r";
constexpr const char* KEY_METALLIC_TEXTURE = "tex_m";
constexpr const char* KEY_AMBIENT_OCCLUSION_TEXTURE = "tex_ao";
void MeshLoader::loadMaterials(const aiScene* scene, const Map<std::string, PTextureAsset>& textures, const std::string& baseName, const std::filesystem::path& meshDirectory, const std::string& importPath, Array<PMaterialInstanceAsset>& globalMaterials)
{
@@ -125,18 +131,26 @@ void MeshLoader::loadMaterials(const aiScene* scene, const Map<std::string, PTex
size_t uniformSize = 0;
uint32 bindingCounter = 1;
auto addScalarParameter = [&](std::string paramKey, const char* matKey, int type, int index)
{
float scalar;
material->Get(matKey, type, index, scalar);
expressions.add(new FloatParameter(paramKey, scalar, uniformSize, 0));
uniformSize += sizeof(float);
parameters.add(paramKey);
};
auto addVectorParameter = [&](std::string paramKey, const char* matKey, int type, int index)
{
aiColor3D color;
material->Get(matKey, type, index, color);
expressions.add(new VectorParameter(paramKey, Vector(color.r, color.g, color.b), uniformSize, 0));
uniformSize = (uniformSize + sizeof(Vector4) - 1) / sizeof(Vector4) * sizeof(Vector4);
expressions.add(new VectorParameter(paramKey, Vector(color.r, color.g, color.b), uniformSize, 0));
uniformSize += sizeof(Vector);
parameters.add(paramKey);
};
auto addTextureParameter = [&](std::string paramKey, aiTextureType type, int index, std::string& result)
auto addTextureParameter = [&](std::string paramKey, aiTextureType type, int index, std::string& result, StaticArray<int32, 4> extractMask = {0, 1, 2, -1})
{
aiString texPath;
aiTextureMapping mapping;
@@ -169,12 +183,14 @@ void MeshLoader::loadMaterials(const aiScene* scene, const Map<std::string, PTex
AssetImporter::importTexture(TextureImportArgs{
.filePath = meshDirectory / texFilename,
.importPath = importPath,
.type = type == aiTextureType_NORMALS ? TextureImportType::TEXTURE_NORMAL : TextureImportType::TEXTURE_2D,
});
texture = AssetRegistry::findTexture(importPath, texFilename.stem().string());
}
else
{
std::cout << "couldnt find " << texPath.C_Str() << std::endl;
return;
}
expressions.add(new TextureParameter(textureKey, texture, bindingCounter));
materialLayout->addDescriptorBinding(Gfx::DescriptorBinding{
@@ -228,7 +244,7 @@ void MeshLoader::loadMaterials(const aiScene* scene, const Map<std::string, PTex
expressions.back()->inputs["coords"].source = fmt::format("input.texCoords[{0}]", uvIndex);
std::string colorExtract = fmt::format("{0}Extract{1}", paramKey, index);
expressions.add(new SwizzleExpression({ 0, 1, 2, -1 }));
expressions.add(new SwizzleExpression(extractMask));
expressions.back()->key = colorExtract;
expressions.back()->inputs["target"].source = sampleKey;
//TODO: extract alpha, set opacity
@@ -289,125 +305,115 @@ void MeshLoader::loadMaterials(const aiScene* scene, const Map<std::string, PTex
};
// Diffuse
addVectorParameter(KEY_DIFFUSE_COLOR, AI_MATKEY_COLOR_DIFFUSE);
addVectorParameter(KEY_SPECULAR_COLOR, AI_MATKEY_COLOR_SPECULAR);
addVectorParameter(KEY_AMBIENT_COLOR, AI_MATKEY_COLOR_AMBIENT);
std::string outputDiffuse = KEY_DIFFUSE_COLOR;
std::string outputSpecular = KEY_SPECULAR_COLOR;
std::string outputAmbient = KEY_AMBIENT_COLOR;
std::string outputNormal = "";
uint32 numDiffuseTextures = material->GetTextureCount(aiTextureType_DIFFUSE);
for (uint32 i = 0; i < numDiffuseTextures; ++i)
{
addTextureParameter(KEY_DIFFUSE_TEXTURE, aiTextureType_DIFFUSE, i, outputDiffuse);
}
// Specular
addVectorParameter(KEY_SPECULAR_COLOR, AI_MATKEY_COLOR_SPECULAR);
std::string outputSpecular = KEY_SPECULAR_COLOR;
uint32 numSpecular = material->GetTextureCount(aiTextureType_SPECULAR);
for (uint32 i = 0; i < numSpecular; ++i)
{
addTextureParameter(KEY_SPECULAR_TEXTURE, aiTextureType_SPECULAR, i, outputSpecular);
}
uint32 numAmbient = material->GetTextureCount(aiTextureType_AMBIENT);
for (uint32 i = 0; i < numSpecular; ++i)
{
addTextureParameter(KEY_AMBIENT_COLOR, aiTextureType_AMBIENT, i, outputAmbient);
}
// Normal
std::string outputNormal = "";
uint32 numNormal = material->GetTextureCount(aiTextureType_NORMALS);
if (numNormal > 1)
for (uint32 i = 0; i < numNormal; ++i)
{
std::cout << "More than 1 normal??" << std::endl;
addTextureParameter(KEY_NORMAL_TEXTURE, aiTextureType_NORMALS, i, outputNormal);
}
else if (numNormal == 1)
// Ambient Color
addVectorParameter(KEY_AMBIENT_COLOR, AI_MATKEY_COLOR_AMBIENT);
std::string outputAmbient = KEY_AMBIENT_COLOR;
uint32 numAmbient = material->GetTextureCount(aiTextureType_AMBIENT);
for (uint32 i = 0; i < numAmbient; ++i)
{
aiString texPath;
aiTextureMapping mapping;
uint32 uvIndex;
if (material->GetTexture(aiTextureType_NORMALS, 0, &texPath, &mapping, &uvIndex) != AI_SUCCESS)
{
std::cout << "fuck" << std::endl;
}
std::string textureKey = fmt::format("NormalTexture");
auto texFilename = std::filesystem::path(texPath.C_Str());
PTextureAsset texture;
if (textures.contains(texFilename.string()))
{
texture = textures[texFilename.string()];
}
else if (std::filesystem::exists(texFilename))
{
AssetImporter::importTexture(TextureImportArgs{
.filePath = texFilename,
.importPath = importPath,
});
texture = AssetRegistry::findTexture(importPath, texFilename.stem().string());
}
if (texture != nullptr)
{
expressions.add(new TextureParameter(textureKey, texture, bindingCounter));
materialLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.binding = bindingCounter,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
.shaderStages = Gfx::SE_SHADER_STAGE_FRAGMENT_BIT,
});
parameters.add(textureKey);
bindingCounter++;
std::string samplerKey = "NormalSampler";
SamplerCreateInfo samplerInfo = {};
expressions.add(new SamplerParameter(samplerKey, graphics->createSampler(samplerInfo), bindingCounter));
materialLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.binding = bindingCounter,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLER,
.shaderStages = Gfx::SE_SHADER_STAGE_FRAGMENT_BIT,
});
parameters.add(samplerKey);
bindingCounter++;
std::string sampleKey = "NormalSample";
expressions.add(new SampleExpression());
expressions.back()->key = sampleKey;
expressions.back()->inputs["texture"].source = textureKey;
expressions.back()->inputs["sampler"].source = samplerKey;
expressions.back()->inputs["coords"].source = fmt::format("input.texCoords[{0}]", uvIndex);
std::string normalExtract = "NormalExtract";
expressions.add(new SwizzleExpression({ 0, 1, 2, -1 }));
expressions.back()->key = normalExtract;
expressions.back()->inputs["target"].source = sampleKey;
std::string mulKey = "NormalMul";
expressions.add(new MulExpression());
expressions.back()->key = mulKey;
expressions.back()->inputs["lhs"].source = "2";
expressions.back()->inputs["rhs"].source = normalExtract;
std::string subKey = "NormalSub";
expressions.add(new SubExpression());
expressions.back()->key = subKey;
expressions.back()->inputs["lhs"].source = mulKey;
expressions.back()->inputs["rhs"].source = "float3(1,1,1)";
outputNormal = subKey;
}
addTextureParameter(KEY_AMBIENT_TEXTURE, aiTextureType_AMBIENT, i, outputAmbient);
}
// Shininess
addScalarParameter(KEY_SHININESS, AI_MATKEY_SHININESS);
std::string outputShininess = KEY_SHININESS;
uint32 numShiny = material->GetTextureCount(aiTextureType_SHININESS);
for (uint32 i = 0; i < numShiny; ++i)
{
addTextureParameter(KEY_SHININESS_TEXTURE, aiTextureType_SHININESS, i, outputShininess, { 0, -1, -1, -1 });
}
// Roughness
addScalarParameter(KEY_ROUGHNESS, AI_MATKEY_ROUGHNESS_FACTOR);
std::string outputRoughness = KEY_ROUGHNESS;
uint32 numRoughness = material->GetTextureCount(aiTextureType_DIFFUSE_ROUGHNESS);
for (uint32 i = 0; i < numRoughness; ++i)
{
addTextureParameter(KEY_ROUGHNESS_TEXTURE, aiTextureType_DIFFUSE_ROUGHNESS, i, outputRoughness, { 0, -1, -1, -1 });
}
// Metallic
addScalarParameter(KEY_METALLIC, AI_MATKEY_METALLIC_FACTOR);
std::string outputMetallic = KEY_METALLIC;
uint32 numMetallic = material->GetTextureCount(aiTextureType_METALNESS);
for (uint32 i = 0; i < numMetallic; ++i)
{
addTextureParameter(KEY_METALLIC_TEXTURE, aiTextureType_METALNESS, i, outputMetallic, { 0, -1, -1, -1 });
}
// Ambient Occlusion
std::string outputAO = "";
uint32 numAO = material->GetTextureCount(aiTextureType_AMBIENT_OCCLUSION);
for (uint32 i = 0; i < numAO; ++i)
{
addTextureParameter(KEY_AMBIENT_OCCLUSION_TEXTURE, aiTextureType_AMBIENT_OCCLUSION, i, outputAO, { 0, -1, -1, -1 });
}
MaterialNode brdf;
brdf.profile = "BlinnPhong";
brdf.variables["baseColor"] = outputDiffuse;
//brdf.variables["specular"] = outputSpecular;
if (!outputNormal.empty())
{
brdf.variables["normal"] = outputNormal;
}
aiShadingMode mode;
material->Get(AI_MATKEY_SHADING_MODEL, mode);
switch (mode) {
case aiShadingMode_Blinn:
brdf.profile = "Phong";
brdf.variables["specular"] = outputSpecular;
brdf.variables["ambient"] = outputAmbient;
brdf.variables["shininess"] = outputShininess;
break;
case aiShadingMode_Phong:
brdf.profile = "BlinnPhong";
brdf.variables["specularColor"] = outputSpecular;
brdf.variables["ambient"] = outputAmbient;
break;
case aiShadingMode_Toon:
brdf.profile = "CelShading";
break;
case aiShadingMode_CookTorrance:
brdf.profile = "CookTorrance";
brdf.variables["roughness"] = outputRoughness;
brdf.variables["metallic"] = outputMetallic;
if (!outputAO.empty())
{
brdf.variables["ambientOcclusion"] = outputAmbient;
}
break;
default:
throw std::logic_error("Todo");
};
materialLayout->create();
OMaterialAsset baseMat = new MaterialAsset(importPath, materialName);
baseMat->material = new Material(graphics,
std::move(materialLayout),
+116 -82
View File
@@ -51,100 +51,134 @@ PTextureAsset TextureLoader::getPlaceholderTexture()
return placeholderAsset;
}
#define KTX_ASSERT(x) { auto error = x; if(error != KTX_SUCCESS) { std::cout << ktxErrorString(error) << std::endl; abort(); } }
void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset)
{
int totalWidth = 0, totalHeight = 0, n = 0;
unsigned char* data = stbi_load(args.filePath.string().c_str(), &totalWidth, &totalHeight, &n, 4);
ktxTexture2* kTexture = nullptr;
ktxTextureCreateInfo createInfo = {
.vkFormat = VK_FORMAT_R8G8B8A8_UNORM,
.baseDepth = 1,
.numLevels = 1,
.numLayers = 1,
.isArray = false,
.generateMipmaps = false,
};
if (args.type == TextureImportType::TEXTURE_CUBEMAP)
// manually transcode ktx textures using toktx
if (args.filePath.extension().compare("ktx") != 0)
{
uint32 faceWidth = totalWidth / 4;
// uint32 faceHeight = totalHeight / 3;
// Cube map
createInfo.baseWidth = totalWidth / 4;
createInfo.baseHeight = totalHeight / 3;
createInfo.numFaces = 6;
createInfo.numDimensions = 2;
ktxTexture2_Create(&createInfo,
KTX_TEXTURE_CREATE_ALLOC_STORAGE,
&kTexture);
auto loadCubeFace = [&kTexture, &faceWidth, &totalWidth, &data](int xPos, int yPos, int faceName)
{
std::vector<unsigned char> vec(faceWidth * faceWidth * 4);
for (uint32 y = 0; y < faceWidth; ++y)
{
for (uint32 x = 0; x < faceWidth; ++x)
{
int imgX = x + (xPos * faceWidth);
int imgY = y + (yPos * faceWidth);
std::memcpy(&vec[(x + (faceWidth * y)) * 4], &data[(imgX + (totalWidth * imgY)) * 4], 4);
}
}
ktxTexture_SetImageFromMemory(ktxTexture(kTexture),
0, 0, faceName, vec.data(), vec.size());
};
loadCubeFace(2, 1, 0); // +X
loadCubeFace(0, 1, 1); // -X
loadCubeFace(1, 0, 2); // +Y
loadCubeFace(1, 2, 3); // -Y
loadCubeFace(1, 1, 4); // +Z
loadCubeFace(3, 1, 5); // -Z
}
else
{
createInfo.baseWidth = totalWidth;
createInfo.baseHeight = totalHeight;
createInfo.numFaces = 1;
createInfo.numDimensions = 1 + (totalHeight > 1);
ktxTexture2_Create(&createInfo,
KTX_TEXTURE_CREATE_ALLOC_STORAGE,
&kTexture);
ktxTexture_SetImageFromMemory(ktxTexture(kTexture),
0, 0, 0, data, totalWidth * totalHeight * 4 * sizeof(unsigned char));
auto ktxFile = args.filePath;
ktxFile.replace_extension("ktx");
std::stringstream ss;
ss << "toktx --encode etc1s " << ktxFile << " " << args.filePath;
system(ss.str().c_str());
args.filePath = ktxFile;
}
ktxBasisParams params2 = {
.structSize = sizeof(ktxBasisParams),
.uastc = false,
.threadCount = std::thread::hardware_concurrency(),
.compressionLevel = 0,
.qualityLevel = 1,
};
ktxTexture2* ktxHandle;
KTX_ASSERT(ktxTexture_CreateFromNamedFile(args.filePath.string().c_str(), 0, (ktxTexture**) & ktxHandle));
//ktx_error_code_e error = ktxTexture2_CompressBasisEx(kTexture, &params2);
//assert(error == KTX_SUCCESS);
ktx_uint8_t* dest;
ktx_size_t size;
ktxTexture_WriteToMemory(ktxTexture(kTexture), &dest, &size);
//int totalWidth = 0, totalHeight = 0, n = 0;
//unsigned char* data = stbi_load(args.filePath.string().c_str(), &totalWidth, &totalHeight, &n, 4);
//ktxTexture2* kTexture = nullptr;
//VkFormat format = VK_FORMAT_R8G8B8A8_UNORM;
//ktxTextureCreateInfo createInfo = {
// .vkFormat = (uint32)format,
// .baseDepth = 1,
// .numLevels = 1,
// .numLayers = 1,
// .isArray = false,
// .generateMipmaps = false,
//};
//
//if (args.type == TextureImportType::TEXTURE_CUBEMAP)
//{
// uint32 faceWidth = totalWidth / 4;
// // uint32 faceHeight = totalHeight / 3;
// // Cube map
// createInfo.baseWidth = totalWidth / 4;
// createInfo.baseHeight = totalHeight / 3;
// createInfo.numFaces = 6;
// createInfo.numDimensions = 2;
Array<uint8> memory(size);
std::memcpy(memory.data(), dest, size);
free(dest);
stbi_image_free(data);
// KTX_ASSERT(ktxTexture2_Create(&createInfo,
// KTX_TEXTURE_CREATE_ALLOC_STORAGE,
// &kTexture));
// auto loadCubeFace = [&kTexture, &faceWidth, &totalWidth, &data](int xPos, int yPos, int faceName)
// {
// std::vector<unsigned char> vec(faceWidth * faceWidth * 4);
// for (uint32 y = 0; y < faceWidth; ++y)
// {
// for (uint32 x = 0; x < faceWidth; ++x)
// {
// int imgX = x + (xPos * faceWidth);
// int imgY = y + (yPos * faceWidth);
// std::memcpy(&vec[(x + (faceWidth * y)) * 4], &data[(imgX + (totalWidth * imgY)) * 4], 4);
// }
// }
// ktxTexture_SetImageFromMemory(ktxTexture(kTexture),
// 0, 0, faceName, vec.data(), vec.size());
// };
// loadCubeFace(2, 1, 0); // +X
// loadCubeFace(0, 1, 1); // -X
// loadCubeFace(1, 0, 2); // +Y
// loadCubeFace(1, 2, 3); // -Y
// loadCubeFace(1, 1, 4); // +Z
// loadCubeFace(3, 1, 5); // -Z
//}
//else
//{
// createInfo.baseWidth = totalWidth;
// createInfo.baseHeight = totalHeight;
// createInfo.numFaces = 1;
// createInfo.numDimensions = 1 + (totalHeight > 1);
// ktxTexture2_Create(&createInfo,
// KTX_TEXTURE_CREATE_ALLOC_STORAGE,
// &kTexture);
// ktxTexture_SetImageFromMemory(ktxTexture(kTexture),
// 0, 0, 0, data, totalWidth * totalHeight * n * sizeof(unsigned char));
//}
//ktxTexture_WriteToNamedFile(ktxTexture(kTexture), args.filePath.replace_extension(".ktx").string().c_str());
//ktxBasisParams basisParams = {
// .structSize = sizeof(ktxBasisParams),
// .uastc = true,
// .threadCount = std::thread::hardware_concurrency(),
// .normalMap = normalMap,
// .uastcFlags = KTX_PACK_UASTC_LEVEL_VERYSLOW,
// .uastcRDO = true,
// .uastcRDOQualityScalar = 1,
//};
//KTX_ASSERT(ktxTexture2_CompressBasisEx(kTexture, &basisParams));
//KTX_ASSERT(ktxTexture2_DeflateZstd(kTexture, 3));
//char writer[100];
//snprintf(writer, sizeof(writer), "%s version %s", "SeeleEngine", "0.0.1");
//ktxHashList_AddKVPair(&kTexture->kvDataHead, KTX_WRITER_KEY,
// (ktx_uint32_t)strlen(writer) + 1,
// writer);
//uint8* texData;
//size_t texSize;
//KTX_ASSERT(ktxTexture_WriteToMemory(ktxTexture(kTexture), &texData, &texSize));
//
//stbi_image_free(data);
ArchiveBuffer temp(graphics);
Serialization::save(temp, memory);
temp.rewind();
textureAsset->load(temp);
if (textureAsset->getName().empty())
{
return;
}
AssetRegistry::get().saveAsset(textureAsset, TextureAsset::IDENTIFIER, textureAsset->getFolderPath(), textureAsset->getName());
std::string path = (std::filesystem::path(args.importPath) / textureAsset->getName()).string().append(".asset");
auto assetStream = AssetRegistry::createWriteStream(std::move(path), std::ios::binary);
ArchiveBuffer buffer(graphics);
// write identifier
Serialization::save(buffer, TextureAsset::IDENTIFIER);
// write name
Serialization::save(buffer, textureAsset->getName());
// write folder
Serialization::save(buffer, textureAsset->getFolderPath());
// write asset data
Serialization::save(buffer, args.filePath.string());
buffer.writeToStream(assetStream);
buffer.rewind();
AssetRegistry::get().loadAsset(buffer);
}
+2
View File
@@ -12,6 +12,7 @@ DECLARE_NAME_REF(Gfx, Texture2D)
enum class TextureImportType
{
TEXTURE_2D,
TEXTURE_NORMAL,
TEXTURE_CUBEMAP,
};
struct TextureImportArgs
@@ -20,6 +21,7 @@ struct TextureImportArgs
std::string importPath;
TextureImportType type = TextureImportType::TEXTURE_2D;
Gfx::SeImageUsageFlagBits usage = Gfx::SE_IMAGE_USAGE_SAMPLED_BIT;
uint32 numChannels = 4;
};
class TextureLoader
{