Reworking tangent lighting
This commit is contained in:
@@ -452,7 +452,7 @@ void MeshLoader::loadMaterials(const aiScene* scene, const Array<PTextureAsset>&
|
||||
}
|
||||
}
|
||||
|
||||
void findMeshRoots(aiNode* node, List<aiNode*>& meshNodes) {
|
||||
void MeshLoader::findMeshRoots(aiNode* node, List<aiNode*>& meshNodes) {
|
||||
if (node->mNumMeshes > 0) {
|
||||
meshNodes.add(node);
|
||||
return;
|
||||
@@ -462,6 +462,40 @@ void findMeshRoots(aiNode* node, List<aiNode*>& meshNodes) {
|
||||
}
|
||||
}
|
||||
|
||||
uint32 MeshLoader::encodeQTangent(Matrix3 m) {
|
||||
float r = (glm::determinant(m) ? -1.0 : 1.0);
|
||||
m[2] *= r;
|
||||
float t = m[0][0] + (m[1][1] + m[2][2]);
|
||||
Vector4 q;
|
||||
if (t > 2.9999999) {
|
||||
q = Vector4(0.0, 0.0, 0.0, 1.0);
|
||||
} else if (t > 0.0000001) {
|
||||
float s = sqrt(1.0 + t) * 2.0;
|
||||
q = Vector4(Vector(m[1][2] - m[2][1], m[2][0] - m[0][2], m[0][1] - m[1][0]) / s, s * 0.25);
|
||||
} else if ((m[0][0] > m[1][1]) && (m[0][0] > m[2][2])) {
|
||||
float s = sqrt(1.0 + (m[0][0] - (m[1][1] + m[2][2]))) * 2.0;
|
||||
q = Vector4(s * 0.25, Vector(m[1][0] + m[0][1], m[2][0] + m[0][2], m[1][2] - m[2][1]) / s);
|
||||
} else if (m[1][1] > m[2][2]) {
|
||||
float s = sqrt(1.0 + (m[1][1] - (m[0][0] + m[2][2]))) * 2.0;
|
||||
q = Vector4(Vector(m[1][0] + m[0][1], m[2][1] + m[1][2], m[2][0] - m[0][2]) / s, s * 0.25);
|
||||
q = Vector4(q.x, q.w, q.y, q.z);
|
||||
} else {
|
||||
float s = sqrt(1.0 + (m[2][2] - (m[0][0] + m[1][1]))) * 2.0;
|
||||
q = Vector4(Vector(m[2][0] + m[0][2], m[2][1] + m[1][2], m[0][1] - m[1][0]) / s, s * 0.25);
|
||||
q = Vector4(q.x, q.y, q.w, q.z);
|
||||
}
|
||||
Vector4 qAbs = abs(q = glm::normalize(q));
|
||||
int maxComponentIndex = (qAbs.x > qAbs.y) ? ((qAbs.x > qAbs.z) ? ((qAbs.x > qAbs.w) ? 0 : 3) : ((qAbs.z > qAbs.w) ? 2 : 3))
|
||||
: ((qAbs.y > qAbs.z) ? ((qAbs.y > qAbs.w) ? 1 : 3) : ((qAbs.z > qAbs.w) ? 2 : 3));
|
||||
Vector components[4] = {Vector(q.y, q.z, q.w), Vector(q.x, q.z, q.w), Vector(q.x, q.y, q.w), Vector(q.x, q.y, q.z)};
|
||||
|
||||
q = Vector4(components[maxComponentIndex] * float(((q[maxComponentIndex] < 0.0) ? -1.0 : 1.0) * 1.4142135623730951), q.w);
|
||||
return ((uint32(round(glm::clamp(q.x * 511.0, -511.0, 511.0) + 512.0)) & 0x3ffu) << 0u) |
|
||||
((uint32(round(glm::clamp(q.y * 511.0, -511.0, 511.0) + 512.0)) & 0x3ffu) << 10u) |
|
||||
((uint32(round(glm::clamp(q.z * 255.0, -255.0, 255.0) + 256.0)) & 0x1ffu) << 20u) |
|
||||
((uint32(((dot(cross(m[0], m[2]), m[1]) * r) < 0.0) ? 1u : 0u) & 0x1u) << 29u) | ((uint32(maxComponentIndex) & 0x3u) << 30u);
|
||||
}
|
||||
|
||||
void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialInstanceAsset>& materials, Array<OMesh>& globalMeshes,
|
||||
Component::Collider& collider) {
|
||||
List<std::function<void()>> work;
|
||||
@@ -483,7 +517,7 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialIns
|
||||
for (size_t i = 0; i < MAX_TEXCOORDS; ++i) {
|
||||
texCoords[i].resize(mesh->mNumVertices);
|
||||
}
|
||||
Array<Quaternion> normals(mesh->mNumVertices);
|
||||
Array<uint32> normals(mesh->mNumVertices);
|
||||
Array<U16Vector> colors(mesh->mNumVertices);
|
||||
|
||||
for (int32 i = 0; i < mesh->mNumVertices; ++i) {
|
||||
@@ -504,25 +538,7 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialIns
|
||||
}
|
||||
Matrix3 tbn = {normal, biTangent, tangent};
|
||||
|
||||
Quaternion qTangent(tbn);
|
||||
qTangent = glm::normalize(qTangent);
|
||||
|
||||
if (qTangent.w < 0)
|
||||
qTangent = -qTangent;
|
||||
|
||||
const float bias = 1.0f / 32767.0f;
|
||||
|
||||
if (qTangent.w < bias) {
|
||||
double normFactor = sqrt(1 - bias * bias);
|
||||
qTangent.w = bias;
|
||||
qTangent.x *= normFactor;
|
||||
qTangent.y *= normFactor;
|
||||
qTangent.z *= normFactor;
|
||||
}
|
||||
Vector naturalBinormal = glm::cross(tangent, normal);
|
||||
if (glm::dot(naturalBinormal, biTangent) <= 0)
|
||||
qTangent = -qTangent;
|
||||
normals[i] = qTangent;
|
||||
normals[i] = encodeQTangent(tbn);
|
||||
|
||||
if (mesh->HasVertexColors(0)) {
|
||||
colors[i] = U16Vector(mesh->mColors[0][i].r * 65535, mesh->mColors[0][i].g * 65535, mesh->mColors[0][i].b * 65535);
|
||||
@@ -559,7 +575,7 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialIns
|
||||
globalMeshes[meshIndex]->blas = graphics->createBottomLevelAccelerationStructure(Gfx::BottomLevelASCreateInfo{
|
||||
.mesh = globalMeshes[meshIndex],
|
||||
});
|
||||
//vertexData->registerBottomLevelAccelerationStructure(globalMeshes[meshIndex]->blas);
|
||||
// vertexData->registerBottomLevelAccelerationStructure(globalMeshes[meshIndex]->blas);
|
||||
});
|
||||
}
|
||||
getThreadPool().runAndWait(std::move(work));
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
#include "MinimalEngine.h"
|
||||
#include <filesystem>
|
||||
|
||||
|
||||
struct aiScene;
|
||||
struct aiTexel;
|
||||
struct aiNode;
|
||||
@@ -26,6 +25,9 @@ class MeshLoader {
|
||||
void importAsset(MeshImportArgs args);
|
||||
|
||||
private:
|
||||
void findMeshRoots(aiNode* node, List<aiNode*>& meshNodes);
|
||||
uint32 encodeQTangent(Matrix3 m);
|
||||
|
||||
void loadTextures(const aiScene* scene, const std::filesystem::path& meshDirectory, const std::string& importPath,
|
||||
Array<PTextureAsset>& textures);
|
||||
void loadMaterials(const aiScene* scene, const Array<PTextureAsset>& textures, const std::string& baseName,
|
||||
|
||||
@@ -42,7 +42,7 @@ void TextureLoader::importAsset(TextureImportArgs args) {
|
||||
PTextureAsset ref = asset;
|
||||
asset->setStatus(Asset::Status::Loading);
|
||||
AssetRegistry::get().registerTexture(std::move(asset));
|
||||
getThreadPool().runAsync([=](){import(args, ref);});
|
||||
import(args, ref);
|
||||
}
|
||||
|
||||
PTextureAsset TextureLoader::getPlaceholderTexture() { return placeholderAsset; }
|
||||
@@ -112,11 +112,11 @@ void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset) {
|
||||
.structSize = sizeof(ktxBasisParams),
|
||||
.uastc = true,
|
||||
.threadCount = 14,
|
||||
.uastcFlags = KTX_PACK_UASTC_LEVEL_FASTER,
|
||||
.uastcFlags = KTX_PACK_UASTC_LEVEL_FASTEST,
|
||||
.uastcRDO = true,
|
||||
};
|
||||
KTX_ASSERT(ktxTexture2_CompressBasisEx(kTexture, &basisParams));
|
||||
KTX_ASSERT(ktxTexture2_DeflateZstd(kTexture, 20));
|
||||
//KTX_ASSERT(ktxTexture2_CompressBasisEx(kTexture, &basisParams));
|
||||
//KTX_ASSERT(ktxTexture2_DeflateZstd(kTexture, 20));
|
||||
|
||||
char writer[100];
|
||||
snprintf(writer, sizeof(writer), "%s version %s", "SeeleEngine", "0.0.1");
|
||||
|
||||
+4
-4
@@ -109,10 +109,10 @@ int main() {
|
||||
//AssetImporter::importTexture(TextureImportArgs{
|
||||
// .filePath = sourcePath / "import/textures/wgen.png",
|
||||
//});
|
||||
//AssetImporter::importMesh(MeshImportArgs{
|
||||
// .filePath = sourcePath / "import/models/after-the-rain-vr-sound/source/Whitechapel.glb",
|
||||
// .importPath = "Whitechapel",
|
||||
//});
|
||||
AssetImporter::importMesh(MeshImportArgs{
|
||||
.filePath = sourcePath / "import/models/after-the-rain-vr-sound/source/Whitechapel.glb",
|
||||
.importPath = "Whitechapel",
|
||||
});
|
||||
// AssetImporter::importMesh(MeshImportArgs{521
|
||||
// .filePath = sourcePath / "import/models/city-suburbs/source/city-suburbs.obj",
|
||||
// .importPath = "suburbs",
|
||||
|
||||
@@ -178,9 +178,9 @@ void DepthCullingPass::render() {
|
||||
command->bindDescriptor({viewParamsSet, vertexData->getVertexDataSet(), vertexData->getInstanceDataSet(), set});
|
||||
VertexData::DrawCallOffsets offsets = {
|
||||
.instanceOffset = 0,
|
||||
.floatOffset = 0,
|
||||
.samplerOffset = 0,
|
||||
.textureOffset = 0,
|
||||
.samplerOffset = 0,
|
||||
.floatOffset = 0,
|
||||
};
|
||||
command->pushConstants(Gfx::SE_SHADER_STAGE_TASK_BIT_EXT | Gfx::SE_SHADER_STAGE_VERTEX_BIT, 0, sizeof(VertexData::DrawCallOffsets),
|
||||
&offsets);
|
||||
|
||||
@@ -32,9 +32,9 @@ void StaticMeshVertexData::loadTexCoords(uint64 offset, uint64 index, const Arra
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
void StaticMeshVertexData::loadNormals(uint64 offset, const Array<Quaternion>& data) {
|
||||
void StaticMeshVertexData::loadNormals(uint64 offset, const Array<uint32>& data) {
|
||||
assert(offset + data.size() <= head);
|
||||
std::memcpy(norData.data() + offset, data.data(), data.size() * sizeof(Quaternion));
|
||||
std::memcpy(norData.data() + offset, data.data(), data.size() * sizeof(uint32));
|
||||
// normals->updateContents(offset * sizeof(Vector4), data.size() * sizeof(Vector4), data.data());
|
||||
dirty = true;
|
||||
}
|
||||
@@ -60,10 +60,10 @@ void StaticMeshVertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveB
|
||||
Serialization::save(buffer, tex[i]);
|
||||
}
|
||||
Array<Vector> pos(numVertices);
|
||||
Array<Quaternion> nor(numVertices);
|
||||
Array<uint32> nor(numVertices);
|
||||
Array<U16Vector> col(numVertices);
|
||||
std::memcpy(pos.data(), posData.data() + offset, numVertices * sizeof(Vector));
|
||||
std::memcpy(nor.data(), norData.data() + offset, numVertices * sizeof(Quaternion));
|
||||
std::memcpy(nor.data(), norData.data() + offset, numVertices * sizeof(uint32));
|
||||
std::memcpy(col.data(), colData.data() + offset, numVertices * sizeof(U16Vector));
|
||||
Serialization::save(buffer, pos);
|
||||
Serialization::save(buffer, nor);
|
||||
@@ -84,7 +84,7 @@ uint64 StaticMeshVertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer) {
|
||||
result += tex[i].size() * sizeof(U16Vector2);
|
||||
}
|
||||
Array<Vector> pos;
|
||||
Array<Quaternion> nor;
|
||||
Array<uint32> nor;
|
||||
Array<U16Vector> col;
|
||||
Serialization::load(buffer, pos);
|
||||
Serialization::load(buffer, nor);
|
||||
@@ -93,7 +93,7 @@ uint64 StaticMeshVertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer) {
|
||||
loadNormals(offset, nor);
|
||||
loadColors(offset, col);
|
||||
result += pos.size() * sizeof(Vector);
|
||||
result += nor.size() * sizeof(Quaternion);
|
||||
result += nor.size() * sizeof(uint32);
|
||||
result += col.size() * sizeof(U16Vector);
|
||||
return result;
|
||||
}
|
||||
@@ -154,7 +154,7 @@ void StaticMeshVertexData::updateBuffers() {
|
||||
normals = graphics->createShaderBuffer(ShaderBufferCreateInfo{
|
||||
.sourceData =
|
||||
{
|
||||
.size = verticesAllocated * sizeof(Quaternion),
|
||||
.size = verticesAllocated * sizeof(uint32),
|
||||
.data = (uint8*)norData.data(),
|
||||
},
|
||||
.name = "Normals",
|
||||
|
||||
@@ -14,7 +14,7 @@ class StaticMeshVertexData : public VertexData {
|
||||
static StaticMeshVertexData* getInstance();
|
||||
void loadPositions(uint64 offset, const Array<Vector>& data);
|
||||
void loadTexCoords(uint64 offset, uint64 index, const Array<U16Vector2>& data);
|
||||
void loadNormals(uint64 offset, const Array<Quaternion>& data);
|
||||
void loadNormals(uint64 offset, const Array<uint32>& data);
|
||||
void loadColors(uint64 offset, const Array<U16Vector>& data);
|
||||
virtual void serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer) override;
|
||||
virtual uint64 deserializeMesh(MeshId id, ArchiveBuffer& buffer) override;
|
||||
|
||||
@@ -298,12 +298,6 @@ Gfx::OSampler Graphics::createSampler(const SamplerCreateInfo& createInfo) {
|
||||
return new Sampler(this, vkInfo);
|
||||
}
|
||||
|
||||
Gfx::OComputeShader Graphics::createComputeShaderFromBinary(std::string_view binaryName) {
|
||||
OComputeShader shader = new ComputeShader(this);
|
||||
shader->create(binaryName);
|
||||
return shader;
|
||||
}
|
||||
|
||||
Gfx::ODescriptorLayout Graphics::createDescriptorLayout(const std::string& name) { return new DescriptorLayout(this, name); }
|
||||
|
||||
Gfx::OPipelineLayout Graphics::createPipelineLayout(const std::string& name, Gfx::PPipelineLayout baseLayout) {
|
||||
|
||||
@@ -67,8 +67,6 @@ class Graphics : public Gfx::Graphics {
|
||||
virtual Gfx::PComputePipeline createComputePipeline(Gfx::ComputePipelineCreateInfo createInfo) override;
|
||||
virtual Gfx::OSampler createSampler(const SamplerCreateInfo& createInfo) override;
|
||||
|
||||
virtual Gfx::OComputeShader createComputeShaderFromBinary(std::string_view binaryName) override;
|
||||
|
||||
virtual Gfx::ODescriptorLayout createDescriptorLayout(const std::string& name = "") override;
|
||||
virtual Gfx::OPipelineLayout createPipelineLayout(const std::string& name = "", Gfx::PPipelineLayout baseLayout = nullptr) override;
|
||||
|
||||
|
||||
@@ -142,7 +142,7 @@ void Seele::beginCompilation(const ShaderCompilationInfo& info, SlangCompileTarg
|
||||
for (size_t i = 0; i < signature->getParameterCount(); ++i) {
|
||||
auto param = signature->getParameterByIndex(i);
|
||||
layout->addMapping(param->getName(), param->getBindingIndex());
|
||||
std::cout << param->getName() << ": " << param->getBindingIndex() << std::endl;
|
||||
//std::cout << param->getName() << ": " << param->getBindingIndex() << std::endl;
|
||||
}
|
||||
|
||||
// workaround
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
namespace Seele {
|
||||
class ThreadPool {
|
||||
public:
|
||||
ThreadPool(uint32 numWorkers = 1);
|
||||
ThreadPool(uint32 numWorkers = 14);
|
||||
~ThreadPool();
|
||||
void runAndWait(List<std::function<void()>> functions);
|
||||
void runAsync(std::function<void()> func);
|
||||
|
||||
Reference in New Issue
Block a user