Reworking tangent lighting
This commit is contained in:
@@ -103,8 +103,10 @@ void taskMain(
|
|||||||
viewFrustum.sides[1] = computePlane(origin, corners[1], corners[3]);
|
viewFrustum.sides[1] = computePlane(origin, corners[1], corners[3]);
|
||||||
viewFrustum.sides[2] = computePlane(origin, corners[0], corners[1]);
|
viewFrustum.sides[2] = computePlane(origin, corners[0], corners[1]);
|
||||||
viewFrustum.sides[3] = computePlane(origin, corners[3], corners[2]);
|
viewFrustum.sides[3] = computePlane(origin, corners[3], corners[2]);
|
||||||
|
meshVisible = true;
|
||||||
|
#ifdef DEPTH_CULLING
|
||||||
meshVisible = mesh.bounding.insideFrustum(viewFrustum) && isBoxVisible(mesh.bounding);
|
meshVisible = mesh.bounding.insideFrustum(viewFrustum) && isBoxVisible(mesh.bounding);
|
||||||
//meshVisible = true;
|
#endif
|
||||||
}
|
}
|
||||||
GroupMemoryBarrierWithGroupSync();
|
GroupMemoryBarrierWithGroupSync();
|
||||||
if(!meshVisible)
|
if(!meshVisible)
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ struct DirectionalLight : ILightEnv
|
|||||||
|
|
||||||
float3 illuminate<B:IBRDF>(LightingParameter params, B brdf)
|
float3 illuminate<B:IBRDF>(LightingParameter params, B brdf)
|
||||||
{
|
{
|
||||||
float3 dir_TS = mul(params.tbn, -normalize(direction.xyz));
|
float3 dir_TS = mul(params.worldToTangent, -normalize(direction.xyz));
|
||||||
return brdf.evaluate(params.viewDir_TS, dir_TS, color.xyz);
|
return brdf.evaluate(params.viewDir_TS, dir_TS, color.xyz);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -26,7 +26,7 @@ struct PointLight : ILightEnv
|
|||||||
|
|
||||||
float3 illuminate<B:IBRDF>(LightingParameter params, B brdf)
|
float3 illuminate<B:IBRDF>(LightingParameter params, B brdf)
|
||||||
{
|
{
|
||||||
float3 pos_TS = mul(params.tbn, position_WS.xyz);
|
float3 pos_TS = mul(params.worldToTangent, position_WS.xyz);
|
||||||
float3 lightDir_TS = pos_TS.xyz - params.position_TS;
|
float3 lightDir_TS = pos_TS.xyz - params.position_TS;
|
||||||
float d = length(lightDir_TS);
|
float d = length(lightDir_TS);
|
||||||
float illuminance = max(1 - d / colorRange.w, 0);
|
float illuminance = max(1 - d / colorRange.w, 0);
|
||||||
|
|||||||
@@ -7,34 +7,40 @@ struct MaterialParameter
|
|||||||
float3 vertexColor;
|
float3 vertexColor;
|
||||||
};
|
};
|
||||||
|
|
||||||
// data used by light environment
|
|
||||||
struct LightingParameter
|
struct LightingParameter
|
||||||
{
|
{
|
||||||
float3x3 tbn;
|
float3x3 worldToTangent;
|
||||||
float3 normal_TS;
|
float3 viewDir_TS;
|
||||||
float3 position_TS;
|
float3 position_TS;
|
||||||
float3 viewDir_TS;
|
}
|
||||||
};
|
|
||||||
|
// Constructs a TBN matrix from a unpacked qtangent for example for after vertex interpolation in the fragment shader
|
||||||
|
float3x3 constructTBNFromQTangent(float4 q){
|
||||||
|
q = normalize(q); // Ensure that the quaternion is normalized in case it is not, for example after interpolation and so on
|
||||||
|
float3 t2 = q.xyz * 2.0, tx = q.xxx * t2.xyz, ty = q.yyy * t2.xyz, tz = q.www * t2.xyz;
|
||||||
|
float3 tangent = float3(1.0 - (ty.y + (q.z * t2.z)), tx.y + tz.z, tx.z - tz.y);
|
||||||
|
float3 normal = float3(tx.z + tz.y, ty.z - tz.x, 1.0 - (tx.x + ty.y));
|
||||||
|
return float3x3(tangent, cross(tangent, normal) * ((q.w < 0.0) ? -1.0 : 1.0), normal);
|
||||||
|
}
|
||||||
|
|
||||||
// data passed to fragment shader
|
// data passed to fragment shader
|
||||||
struct FragmentParameter
|
struct FragmentParameter
|
||||||
{
|
{
|
||||||
float4 position_CS : SV_Position;
|
float4 position_CS : SV_Position;
|
||||||
#ifndef POS_ONLY
|
#ifndef POS_ONLY
|
||||||
float3 normal_WS : NORMALWS;
|
float4 qTangent : QTANGENT;
|
||||||
float3 tangent_WS : TANGENTWS;
|
|
||||||
float3 biTangent_WS : BITANGENTWS;
|
|
||||||
float3 position_WS : POSITIONWS;
|
float3 position_WS : POSITIONWS;
|
||||||
|
float3 position_TS : POSITIONTS;
|
||||||
|
float3 viewDir_TS : VIEWDIRTS;
|
||||||
float3 vertexColor : COLOR;
|
float3 vertexColor : COLOR;
|
||||||
float4 texCoords0 : TEXCOORDS0;
|
float4 texCoords0 : TEXCOORDS0;
|
||||||
float4 texCoords1 : TEXCOORDS1;
|
float4 texCoords1 : TEXCOORDS1;
|
||||||
float4 texCoords2 : TEXCOORDS2;
|
float4 texCoords2 : TEXCOORDS2;
|
||||||
float4 texCoords3 : TEXCOORDS3;
|
float4 texCoords3 : TEXCOORDS3;
|
||||||
MaterialParameter getMaterialParameter()
|
MaterialParameter getMaterialParameter()
|
||||||
{
|
{
|
||||||
MaterialParameter result;
|
MaterialParameter result;
|
||||||
result.position_WS = position_WS;
|
result.position_WS = position_WS;
|
||||||
result.vertexColor = vertexColor;
|
|
||||||
result.texCoords[0] = texCoords0.xy;
|
result.texCoords[0] = texCoords0.xy;
|
||||||
result.texCoords[1] = texCoords0.zw;
|
result.texCoords[1] = texCoords0.zw;
|
||||||
result.texCoords[2] = texCoords1.xy;
|
result.texCoords[2] = texCoords1.xy;
|
||||||
@@ -43,22 +49,18 @@ struct FragmentParameter
|
|||||||
result.texCoords[5] = texCoords2.zw;
|
result.texCoords[5] = texCoords2.zw;
|
||||||
result.texCoords[6] = texCoords3.xy;
|
result.texCoords[6] = texCoords3.xy;
|
||||||
result.texCoords[7] = texCoords3.zw;
|
result.texCoords[7] = texCoords3.zw;
|
||||||
return result;
|
result.vertexColor = vertexColor;
|
||||||
}
|
return result;
|
||||||
LightingParameter getLightingParameter()
|
}
|
||||||
{
|
|
||||||
LightingParameter result;
|
LightingParameter getLightingParameter()
|
||||||
float3x3 tbn = float3x3(normalize(tangent_WS), normalize(biTangent_WS), normalize(normal_WS));
|
|
||||||
result.tbn = tbn;
|
|
||||||
result.position_TS = mul(tbn, position_WS);
|
|
||||||
result.viewDir_TS = mul(tbn, normalize(pViewParams.cameraPosition_WS.xyz - position_WS));
|
|
||||||
result.normal_TS = mul(tbn, normal_WS);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
float3x3 getTangentToWorld()
|
|
||||||
{
|
{
|
||||||
float3x3 tbn = float3x3(normalize(tangent_WS), normalize(biTangent_WS), normalize(normal_WS));
|
float3x3 worldToTangent = transpose(constructTBNFromQTangent(qTangent));
|
||||||
return transpose(tbn);
|
LightingParameter result;
|
||||||
|
result.worldToTangent = worldToTangent;
|
||||||
|
result.viewDir_TS = viewDir_TS;
|
||||||
|
result.position_TS = position_TS;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
static FragmentParameter interpolate(FragmentParameter f0, FragmentParameter f1, FragmentParameter f2, float3 barycentricCoords)
|
static FragmentParameter interpolate(FragmentParameter f0, FragmentParameter f1, FragmentParameter f2, float3 barycentricCoords)
|
||||||
@@ -66,9 +68,7 @@ struct FragmentParameter
|
|||||||
FragmentParameter result;
|
FragmentParameter result;
|
||||||
result.position_CS = f0.position_CS * barycentricCoords.x + f1.position_CS * barycentricCoords.y + f2.position_CS * barycentricCoords.z;
|
result.position_CS = f0.position_CS * barycentricCoords.x + f1.position_CS * barycentricCoords.y + f2.position_CS * barycentricCoords.z;
|
||||||
#ifndef POS_ONLY
|
#ifndef POS_ONLY
|
||||||
result.normal_WS = f0.normal_WS * barycentricCoords.x + f1.normal_WS * barycentricCoords.y + f2.normal_WS * barycentricCoords.z;
|
result.qTangent = f0.qTangent * barycentricCoords.x + f1.qTangent * barycentricCoords.y + f2.qTangent * barycentricCoords.z;
|
||||||
result.tangent_WS = f0.tangent_WS * barycentricCoords.x + f1.tangent_WS * barycentricCoords.y + f2.tangent_WS * barycentricCoords.z;
|
|
||||||
result.biTangent_WS = f0.biTangent_WS * barycentricCoords.x + f1.biTangent_WS * barycentricCoords.y + f2.biTangent_WS * barycentricCoords.z;
|
|
||||||
result.position_WS = f0.position_WS * barycentricCoords.x + f1.position_WS * barycentricCoords.y + f2.position_WS * barycentricCoords.z;
|
result.position_WS = f0.position_WS * barycentricCoords.x + f1.position_WS * barycentricCoords.y + f2.position_WS * barycentricCoords.z;
|
||||||
result.vertexColor = f0.vertexColor * barycentricCoords.x + f1.vertexColor * barycentricCoords.y + f2.vertexColor * barycentricCoords.z;
|
result.vertexColor = f0.vertexColor * barycentricCoords.x + f1.vertexColor * barycentricCoords.y + f2.vertexColor * barycentricCoords.z;
|
||||||
//for(uint i = 0; i < MAX_TEXCOORDS; ++i)
|
//for(uint i = 0; i < MAX_TEXCOORDS; ++i)
|
||||||
@@ -85,9 +85,7 @@ struct VertexAttributes
|
|||||||
{
|
{
|
||||||
float3 position_MS;
|
float3 position_MS;
|
||||||
#ifndef POS_ONLY
|
#ifndef POS_ONLY
|
||||||
float3 normal_MS;
|
float4 qTangent;
|
||||||
float3 tangent_MS;
|
|
||||||
float3 biTangent_MS;
|
|
||||||
float3 vertexColor;
|
float3 vertexColor;
|
||||||
float2 texCoords[MAX_TEXCOORDS];
|
float2 texCoords[MAX_TEXCOORDS];
|
||||||
#endif
|
#endif
|
||||||
@@ -100,15 +98,12 @@ struct VertexAttributes
|
|||||||
FragmentParameter result;
|
FragmentParameter result;
|
||||||
result.position_CS = clipPos;
|
result.position_CS = clipPos;
|
||||||
#ifndef POS_ONLY
|
#ifndef POS_ONLY
|
||||||
float3x3 normalMatrix = float3x3(transformMatrix);
|
result.qTangent = qTangent;
|
||||||
float3 T = mul(normalMatrix, tangent_MS);
|
float3x3 tbn = transpose(constructTBNFromQTangent(qTangent));
|
||||||
float3 N = mul(normalMatrix, normal_MS);
|
result.position_TS = mul(tbn, worldPos.xyz);
|
||||||
float3 B = mul(normalMatrix, biTangent_MS);
|
|
||||||
result.normal_WS = N;
|
|
||||||
result.tangent_WS = T;
|
|
||||||
result.biTangent_WS = B;
|
|
||||||
result.position_WS = worldPos.xyz;
|
|
||||||
result.vertexColor = vertexColor;
|
result.vertexColor = vertexColor;
|
||||||
|
result.position_WS = worldPos.xyz;
|
||||||
|
result.viewDir_TS = mul(tbn, pViewParams.cameraPosition_WS.xyz - worldPos.xyz);
|
||||||
result.texCoords0 = float4(texCoords[0], texCoords[1]);
|
result.texCoords0 = float4(texCoords[0], texCoords[1]);
|
||||||
result.texCoords1 = float4(texCoords[2], texCoords[3]);
|
result.texCoords1 = float4(texCoords[2], texCoords[3]);
|
||||||
result.texCoords2 = float4(texCoords[4], texCoords[5]);
|
result.texCoords2 = float4(texCoords[4], texCoords[5]);
|
||||||
|
|||||||
@@ -8,33 +8,22 @@ struct StaticMeshVertexData
|
|||||||
{
|
{
|
||||||
return value / 65535.0f;
|
return value / 65535.0f;
|
||||||
}
|
}
|
||||||
float3 xAxis( float4 qQuat )
|
|
||||||
{
|
float3x3 decodeQTangentUI32(uint v){
|
||||||
float fTy = 2.0 * qQuat.y;
|
float4 q = float4(((float3(int3(uint3((uint3(v) >> uint3(0u, 10u, 20u)) & uint2(0x3ffu, 0x1ffu).xxy)) - int2(512, 256).xxy)) / float2(511.0, 255.0).xxy) * 0.7071067811865475, 0.0);
|
||||||
float fTz = 2.0 * qQuat.z;
|
q.w = sqrt(1.0 - clamp(dot(q.xyz, q.xyz), 0.0, 1.0));
|
||||||
float fTwy = fTy * qQuat.w;
|
q = normalize(float4[4](q.wxyz, q.xwyz, q.xywz, q.xyzw)[uint((v >> 30u) & 0x3u)]);
|
||||||
float fTwz = fTz * qQuat.w;
|
float3 t2 = q.xyz * 2.0, tx = q.xxx * t2.xyz, ty = q.yyy * t2.xyz, tz = q.www * t2.xyz;
|
||||||
float fTxy = fTy * qQuat.x;
|
float3 tangent = float3(1.0 - (ty.y + (q.z * t2.z)), tx.y + tz.z, tx.z - tz.y);
|
||||||
float fTxz = fTz * qQuat.x;
|
float3 normal = float3(tx.z + tz.y, ty.z - tz.x, 1.0 - (tx.x + ty.y));
|
||||||
float fTyy = fTy * qQuat.y;
|
return float3x3(tangent, cross(tangent, normal) * (((v & (1u << 29u)) != 0u) ? -1.0 : 1.0), normal);
|
||||||
float fTzz = fTz * qQuat.z;
|
|
||||||
|
|
||||||
return float3( 1.0-(fTyy+fTzz), fTxy+fTwz, fTxz-fTwy );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float3 yAxis( float4 qQuat )
|
// Decodes the UI32 encoded qtangent into a unpacked qtangent for further processing like vertex interpolation and so on
|
||||||
{
|
float4 decodeQTangentUI32Raw(uint v){
|
||||||
float fTx = 2.0 * qQuat.x;
|
float4 q = float4(((float3(int3(uint3((uint3(v) >> uint3(0u, 10u, 20u)) & uint2(0x3ffu, 0x1ffu).xxy)) - int2(512, 256).xxy)) / float2(511.0, 255.0).xxy) * 0.7071067811865475, 0.0);
|
||||||
float fTy = 2.0 * qQuat.y;
|
q.w = sqrt(1.0 - clamp(dot(q.xyz, q.xyz), 0.0, 1.0));
|
||||||
float fTz = 2.0 * qQuat.z;
|
return normalize(float4[4](q.wxyz, q.xwyz, q.xywz, q.xyzw)[uint((v >> 30u) & 0x3u)]) * (((v & (1u << 29u)) != 0u) ? -1.0 : 1.0);
|
||||||
float fTwx = fTx * qQuat.w;
|
|
||||||
float fTwz = fTz * qQuat.w;
|
|
||||||
float fTxx = fTx * qQuat.x;
|
|
||||||
float fTxy = fTy * qQuat.x;
|
|
||||||
float fTyz = fTz * qQuat.y;
|
|
||||||
float fTzz = fTz * qQuat.z;
|
|
||||||
|
|
||||||
return float3( fTxy-fTwz, 1.0-(fTxx+fTzz), fTyz+fTwx );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
VertexAttributes getAttributes(uint index)
|
VertexAttributes getAttributes(uint index)
|
||||||
@@ -42,11 +31,7 @@ struct StaticMeshVertexData
|
|||||||
VertexAttributes attributes;
|
VertexAttributes attributes;
|
||||||
attributes.position_MS = float3(positions[index * 3 + 0], positions[index * 3 + 1], positions[index * 3 + 2]);
|
attributes.position_MS = float3(positions[index * 3 + 0], positions[index * 3 + 1], positions[index * 3 + 2]);
|
||||||
#ifndef POS_ONLY
|
#ifndef POS_ONLY
|
||||||
float4 qtangent = normalize(qtangents[index]);
|
attributes.qTangent = decodeQTangentUI32Raw(qtangents[index]);
|
||||||
attributes.normal_MS = xAxis(qtangent);
|
|
||||||
attributes.tangent_MS = yAxis(qtangent);
|
|
||||||
float biNormalReflection = sign(qtangents[index].w);
|
|
||||||
attributes.biTangent_MS = cross(attributes.normal_MS, attributes.tangent_MS) * biNormalReflection;
|
|
||||||
for(uint i = 0; i < MAX_TEXCOORDS; ++i)
|
for(uint i = 0; i < MAX_TEXCOORDS; ++i)
|
||||||
{
|
{
|
||||||
attributes.texCoords[i] = float2(uint16ToFloat(texCoords[i][index * 2 + 0]), uint16ToFloat(texCoords[i][index * 2 + 1]));
|
attributes.texCoords[i] = float2(uint16ToFloat(texCoords[i][index * 2 + 0]), uint16ToFloat(texCoords[i][index * 2 + 1]));
|
||||||
@@ -56,7 +41,7 @@ struct StaticMeshVertexData
|
|||||||
return attributes;
|
return attributes;
|
||||||
}
|
}
|
||||||
StructuredBuffer<float> positions;
|
StructuredBuffer<float> positions;
|
||||||
StructuredBuffer<float4> qtangents;
|
StructuredBuffer<uint> qtangents;
|
||||||
StructuredBuffer<uint16_t> color;
|
StructuredBuffer<uint16_t> color;
|
||||||
StructuredBuffer<uint16_t> texCoords[MAX_TEXCOORDS];
|
StructuredBuffer<uint16_t> texCoords[MAX_TEXCOORDS];
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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) {
|
if (node->mNumMeshes > 0) {
|
||||||
meshNodes.add(node);
|
meshNodes.add(node);
|
||||||
return;
|
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,
|
void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialInstanceAsset>& materials, Array<OMesh>& globalMeshes,
|
||||||
Component::Collider& collider) {
|
Component::Collider& collider) {
|
||||||
List<std::function<void()>> work;
|
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) {
|
for (size_t i = 0; i < MAX_TEXCOORDS; ++i) {
|
||||||
texCoords[i].resize(mesh->mNumVertices);
|
texCoords[i].resize(mesh->mNumVertices);
|
||||||
}
|
}
|
||||||
Array<Quaternion> normals(mesh->mNumVertices);
|
Array<uint32> normals(mesh->mNumVertices);
|
||||||
Array<U16Vector> colors(mesh->mNumVertices);
|
Array<U16Vector> colors(mesh->mNumVertices);
|
||||||
|
|
||||||
for (int32 i = 0; i < mesh->mNumVertices; ++i) {
|
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};
|
Matrix3 tbn = {normal, biTangent, tangent};
|
||||||
|
|
||||||
Quaternion qTangent(tbn);
|
normals[i] = encodeQTangent(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;
|
|
||||||
|
|
||||||
if (mesh->HasVertexColors(0)) {
|
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);
|
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{
|
globalMeshes[meshIndex]->blas = graphics->createBottomLevelAccelerationStructure(Gfx::BottomLevelASCreateInfo{
|
||||||
.mesh = globalMeshes[meshIndex],
|
.mesh = globalMeshes[meshIndex],
|
||||||
});
|
});
|
||||||
//vertexData->registerBottomLevelAccelerationStructure(globalMeshes[meshIndex]->blas);
|
// vertexData->registerBottomLevelAccelerationStructure(globalMeshes[meshIndex]->blas);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
getThreadPool().runAndWait(std::move(work));
|
getThreadPool().runAndWait(std::move(work));
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
#include "MinimalEngine.h"
|
#include "MinimalEngine.h"
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
|
||||||
|
|
||||||
struct aiScene;
|
struct aiScene;
|
||||||
struct aiTexel;
|
struct aiTexel;
|
||||||
struct aiNode;
|
struct aiNode;
|
||||||
@@ -26,6 +25,9 @@ class MeshLoader {
|
|||||||
void importAsset(MeshImportArgs args);
|
void importAsset(MeshImportArgs args);
|
||||||
|
|
||||||
private:
|
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,
|
void loadTextures(const aiScene* scene, const std::filesystem::path& meshDirectory, const std::string& importPath,
|
||||||
Array<PTextureAsset>& textures);
|
Array<PTextureAsset>& textures);
|
||||||
void loadMaterials(const aiScene* scene, const Array<PTextureAsset>& textures, const std::string& baseName,
|
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;
|
PTextureAsset ref = asset;
|
||||||
asset->setStatus(Asset::Status::Loading);
|
asset->setStatus(Asset::Status::Loading);
|
||||||
AssetRegistry::get().registerTexture(std::move(asset));
|
AssetRegistry::get().registerTexture(std::move(asset));
|
||||||
getThreadPool().runAsync([=](){import(args, ref);});
|
import(args, ref);
|
||||||
}
|
}
|
||||||
|
|
||||||
PTextureAsset TextureLoader::getPlaceholderTexture() { return placeholderAsset; }
|
PTextureAsset TextureLoader::getPlaceholderTexture() { return placeholderAsset; }
|
||||||
@@ -112,11 +112,11 @@ void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset) {
|
|||||||
.structSize = sizeof(ktxBasisParams),
|
.structSize = sizeof(ktxBasisParams),
|
||||||
.uastc = true,
|
.uastc = true,
|
||||||
.threadCount = 14,
|
.threadCount = 14,
|
||||||
.uastcFlags = KTX_PACK_UASTC_LEVEL_FASTER,
|
.uastcFlags = KTX_PACK_UASTC_LEVEL_FASTEST,
|
||||||
.uastcRDO = true,
|
.uastcRDO = true,
|
||||||
};
|
};
|
||||||
KTX_ASSERT(ktxTexture2_CompressBasisEx(kTexture, &basisParams));
|
//KTX_ASSERT(ktxTexture2_CompressBasisEx(kTexture, &basisParams));
|
||||||
KTX_ASSERT(ktxTexture2_DeflateZstd(kTexture, 20));
|
//KTX_ASSERT(ktxTexture2_DeflateZstd(kTexture, 20));
|
||||||
|
|
||||||
char writer[100];
|
char writer[100];
|
||||||
snprintf(writer, sizeof(writer), "%s version %s", "SeeleEngine", "0.0.1");
|
snprintf(writer, sizeof(writer), "%s version %s", "SeeleEngine", "0.0.1");
|
||||||
|
|||||||
+4
-4
@@ -109,10 +109,10 @@ int main() {
|
|||||||
//AssetImporter::importTexture(TextureImportArgs{
|
//AssetImporter::importTexture(TextureImportArgs{
|
||||||
// .filePath = sourcePath / "import/textures/wgen.png",
|
// .filePath = sourcePath / "import/textures/wgen.png",
|
||||||
//});
|
//});
|
||||||
//AssetImporter::importMesh(MeshImportArgs{
|
AssetImporter::importMesh(MeshImportArgs{
|
||||||
// .filePath = sourcePath / "import/models/after-the-rain-vr-sound/source/Whitechapel.glb",
|
.filePath = sourcePath / "import/models/after-the-rain-vr-sound/source/Whitechapel.glb",
|
||||||
// .importPath = "Whitechapel",
|
.importPath = "Whitechapel",
|
||||||
//});
|
});
|
||||||
// AssetImporter::importMesh(MeshImportArgs{521
|
// AssetImporter::importMesh(MeshImportArgs{521
|
||||||
// .filePath = sourcePath / "import/models/city-suburbs/source/city-suburbs.obj",
|
// .filePath = sourcePath / "import/models/city-suburbs/source/city-suburbs.obj",
|
||||||
// .importPath = "suburbs",
|
// .importPath = "suburbs",
|
||||||
|
|||||||
@@ -178,9 +178,9 @@ void DepthCullingPass::render() {
|
|||||||
command->bindDescriptor({viewParamsSet, vertexData->getVertexDataSet(), vertexData->getInstanceDataSet(), set});
|
command->bindDescriptor({viewParamsSet, vertexData->getVertexDataSet(), vertexData->getInstanceDataSet(), set});
|
||||||
VertexData::DrawCallOffsets offsets = {
|
VertexData::DrawCallOffsets offsets = {
|
||||||
.instanceOffset = 0,
|
.instanceOffset = 0,
|
||||||
.floatOffset = 0,
|
|
||||||
.samplerOffset = 0,
|
|
||||||
.textureOffset = 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),
|
command->pushConstants(Gfx::SE_SHADER_STAGE_TASK_BIT_EXT | Gfx::SE_SHADER_STAGE_VERTEX_BIT, 0, sizeof(VertexData::DrawCallOffsets),
|
||||||
&offsets);
|
&offsets);
|
||||||
|
|||||||
@@ -32,9 +32,9 @@ void StaticMeshVertexData::loadTexCoords(uint64 offset, uint64 index, const Arra
|
|||||||
dirty = true;
|
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);
|
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());
|
// normals->updateContents(offset * sizeof(Vector4), data.size() * sizeof(Vector4), data.data());
|
||||||
dirty = true;
|
dirty = true;
|
||||||
}
|
}
|
||||||
@@ -60,10 +60,10 @@ void StaticMeshVertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveB
|
|||||||
Serialization::save(buffer, tex[i]);
|
Serialization::save(buffer, tex[i]);
|
||||||
}
|
}
|
||||||
Array<Vector> pos(numVertices);
|
Array<Vector> pos(numVertices);
|
||||||
Array<Quaternion> nor(numVertices);
|
Array<uint32> nor(numVertices);
|
||||||
Array<U16Vector> col(numVertices);
|
Array<U16Vector> col(numVertices);
|
||||||
std::memcpy(pos.data(), posData.data() + offset, numVertices * sizeof(Vector));
|
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));
|
std::memcpy(col.data(), colData.data() + offset, numVertices * sizeof(U16Vector));
|
||||||
Serialization::save(buffer, pos);
|
Serialization::save(buffer, pos);
|
||||||
Serialization::save(buffer, nor);
|
Serialization::save(buffer, nor);
|
||||||
@@ -84,7 +84,7 @@ uint64 StaticMeshVertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer) {
|
|||||||
result += tex[i].size() * sizeof(U16Vector2);
|
result += tex[i].size() * sizeof(U16Vector2);
|
||||||
}
|
}
|
||||||
Array<Vector> pos;
|
Array<Vector> pos;
|
||||||
Array<Quaternion> nor;
|
Array<uint32> nor;
|
||||||
Array<U16Vector> col;
|
Array<U16Vector> col;
|
||||||
Serialization::load(buffer, pos);
|
Serialization::load(buffer, pos);
|
||||||
Serialization::load(buffer, nor);
|
Serialization::load(buffer, nor);
|
||||||
@@ -93,7 +93,7 @@ uint64 StaticMeshVertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer) {
|
|||||||
loadNormals(offset, nor);
|
loadNormals(offset, nor);
|
||||||
loadColors(offset, col);
|
loadColors(offset, col);
|
||||||
result += pos.size() * sizeof(Vector);
|
result += pos.size() * sizeof(Vector);
|
||||||
result += nor.size() * sizeof(Quaternion);
|
result += nor.size() * sizeof(uint32);
|
||||||
result += col.size() * sizeof(U16Vector);
|
result += col.size() * sizeof(U16Vector);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -154,7 +154,7 @@ void StaticMeshVertexData::updateBuffers() {
|
|||||||
normals = graphics->createShaderBuffer(ShaderBufferCreateInfo{
|
normals = graphics->createShaderBuffer(ShaderBufferCreateInfo{
|
||||||
.sourceData =
|
.sourceData =
|
||||||
{
|
{
|
||||||
.size = verticesAllocated * sizeof(Quaternion),
|
.size = verticesAllocated * sizeof(uint32),
|
||||||
.data = (uint8*)norData.data(),
|
.data = (uint8*)norData.data(),
|
||||||
},
|
},
|
||||||
.name = "Normals",
|
.name = "Normals",
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ class StaticMeshVertexData : public VertexData {
|
|||||||
static StaticMeshVertexData* getInstance();
|
static StaticMeshVertexData* getInstance();
|
||||||
void loadPositions(uint64 offset, const Array<Vector>& data);
|
void loadPositions(uint64 offset, const Array<Vector>& data);
|
||||||
void loadTexCoords(uint64 offset, uint64 index, const Array<U16Vector2>& 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);
|
void loadColors(uint64 offset, const Array<U16Vector>& data);
|
||||||
virtual void serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer) override;
|
virtual void serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer) override;
|
||||||
virtual uint64 deserializeMesh(MeshId id, 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);
|
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::ODescriptorLayout Graphics::createDescriptorLayout(const std::string& name) { return new DescriptorLayout(this, name); }
|
||||||
|
|
||||||
Gfx::OPipelineLayout Graphics::createPipelineLayout(const std::string& name, Gfx::PPipelineLayout baseLayout) {
|
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::PComputePipeline createComputePipeline(Gfx::ComputePipelineCreateInfo createInfo) override;
|
||||||
virtual Gfx::OSampler createSampler(const SamplerCreateInfo& 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::ODescriptorLayout createDescriptorLayout(const std::string& name = "") override;
|
||||||
virtual Gfx::OPipelineLayout createPipelineLayout(const std::string& name = "", Gfx::PPipelineLayout baseLayout = nullptr) 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) {
|
for (size_t i = 0; i < signature->getParameterCount(); ++i) {
|
||||||
auto param = signature->getParameterByIndex(i);
|
auto param = signature->getParameterByIndex(i);
|
||||||
layout->addMapping(param->getName(), param->getBindingIndex());
|
layout->addMapping(param->getName(), param->getBindingIndex());
|
||||||
std::cout << param->getName() << ": " << param->getBindingIndex() << std::endl;
|
//std::cout << param->getName() << ": " << param->getBindingIndex() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// workaround
|
// workaround
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
namespace Seele {
|
namespace Seele {
|
||||||
class ThreadPool {
|
class ThreadPool {
|
||||||
public:
|
public:
|
||||||
ThreadPool(uint32 numWorkers = 1);
|
ThreadPool(uint32 numWorkers = 14);
|
||||||
~ThreadPool();
|
~ThreadPool();
|
||||||
void runAndWait(List<std::function<void()>> functions);
|
void runAndWait(List<std::function<void()>> functions);
|
||||||
void runAsync(std::function<void()> func);
|
void runAsync(std::function<void()> func);
|
||||||
|
|||||||
Reference in New Issue
Block a user