Lots of shader changes, basic primitive writing
This commit is contained in:
@@ -12,14 +12,8 @@ struct LightCullingData
|
||||
layout(set=5)
|
||||
ParameterBlock<LightCullingData> pLightCullingData;
|
||||
|
||||
struct FragmentOutput
|
||||
{
|
||||
float4 color : SV_Target0;
|
||||
uint meshletId : SV_Target1;
|
||||
};
|
||||
|
||||
[shader("pixel")]
|
||||
FragmentOutput fragmentMain(in FragmentParameter params)
|
||||
float4 fragmentMain(in FragmentParameter params) : SV_Target
|
||||
{
|
||||
LightingParameter lightingParams = params.getLightingParameter();
|
||||
MaterialParameter materialParams = params.getMaterialParameter();
|
||||
@@ -41,9 +35,6 @@ FragmentOutput fragmentMain(in FragmentParameter params)
|
||||
// gamma correction
|
||||
result = result / (result + float3(1.0));
|
||||
result = pow(result, float3(1.0/2.2));
|
||||
FragmentOutput output;
|
||||
output.color = float4(result, 1.0f);
|
||||
output.meshletId = params.meshletId;
|
||||
return output;
|
||||
return float4(result, 1.0f);
|
||||
}
|
||||
|
||||
@@ -7,25 +7,31 @@ groupshared uint head;
|
||||
[numthreads(TASK_GROUP_SIZE, 1, 1)]
|
||||
[shader("amplification")]
|
||||
void taskMain(
|
||||
uint threadID: SV_GroupIndex,
|
||||
uint groupID: SV_GroupID,
|
||||
){
|
||||
InstanceData instance = pScene.instances[pOffsets.instanceOffset + groupID];
|
||||
MeshData mesh = pScene.meshData[pOffsets.instanceOffset + groupID];
|
||||
//head = 0;
|
||||
//GroupMemoryBarrierWithGroupSync();
|
||||
//for(uint i = threadID; i < mesh.numMeshlets; i += TASK_GROUP_SIZE)
|
||||
//{
|
||||
// uint m = mesh.meshletOffset + i;
|
||||
// MeshletDescription meshlet = pScene.meshletInfos[m];
|
||||
// MeshletCullingInfo culling = pScene.culledMeshlets[instance.meshletCullingOffset];
|
||||
// if(culling.anyVisible())
|
||||
// {
|
||||
// uint index;
|
||||
// InterlockedAdd(head, 1, index);
|
||||
// p.culledMeshlets[index] = m;
|
||||
// }
|
||||
//}
|
||||
//GroupMemoryBarrierWithGroupSync();
|
||||
//DispatchMesh(head, 1, 1, p);
|
||||
uint threadID: SV_GroupIndex,
|
||||
uint groupID: SV_GroupID, )
|
||||
{
|
||||
MeshData mesh = pScene.meshData[pOffsets.instanceOffset + groupID];
|
||||
if (threadID == 0)
|
||||
{
|
||||
head = 0;
|
||||
p.instanceId = pOffsets.instanceOffset + groupID;
|
||||
p.meshletOffset = mesh.meshletOffset;
|
||||
p.cullingOffset = pScene.cullingOffsets[p.instanceId];
|
||||
}
|
||||
GroupMemoryBarrierWithGroupSync();
|
||||
for (uint i = threadID; i < mesh.numMeshlets; i += TASK_GROUP_SIZE)
|
||||
{
|
||||
uint m = p.meshletOffset + i;
|
||||
uint cull = p.cullingOffset + i;
|
||||
MeshletDescription meshlet = pScene.meshletInfos[m];
|
||||
MeshletCullingInfo culling = pScene.culledMeshlets[cull];
|
||||
if(false)
|
||||
{
|
||||
uint index;
|
||||
InterlockedAdd(head, 1, index);
|
||||
p.culledMeshlets[index] = i;
|
||||
}
|
||||
}
|
||||
GroupMemoryBarrierWithGroupSync();
|
||||
DispatchMesh(head, 1, 1, p);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import Scene;
|
||||
|
||||
groupshared MeshPayload p;
|
||||
groupshared uint head;
|
||||
groupshared MeshData mesh;
|
||||
|
||||
[numthreads(TASK_GROUP_SIZE, 1, 1)]
|
||||
[shader("amplification")]
|
||||
@@ -13,22 +14,25 @@ void taskMain(
|
||||
if (threadID == 0)
|
||||
{
|
||||
head = 0;
|
||||
mesh = pScene.meshData[pOffsets.instanceOffset + groupID];
|
||||
p.instanceId = pOffsets.instanceOffset + groupID;
|
||||
p.meshletOffset = mesh.meshletOffset;
|
||||
p.cullingOffset = pScene.cullingOffsets[p.instanceId];
|
||||
}
|
||||
GroupMemoryBarrierWithGroupSync();
|
||||
MeshData mesh = pScene.meshData[p.instanceId];
|
||||
for (uint i = threadID; i < mesh.numMeshlets; i += TASK_GROUP_SIZE)
|
||||
{
|
||||
uint m = mesh.meshletOffset + i;
|
||||
//MeshletDescription meshlet = pScene.meshletInfos[m];
|
||||
//MeshletCullingInfo culling = pScene.culledMeshlets[pScene.cullingOffsets[p.instanceId] + i];
|
||||
//if(culling.anyVisible())
|
||||
uint m = p.meshletOffset + i;
|
||||
uint cull = p.cullingOffset + i;
|
||||
MeshletDescription meshlet = pScene.meshletInfos[m];
|
||||
MeshletCullingInfo culling = pScene.culledMeshlets[cull];
|
||||
if(culling.anyVisible())
|
||||
{
|
||||
uint index;
|
||||
InterlockedAdd(head, 1, index);
|
||||
p.culledMeshlets[i] = m;
|
||||
p.culledMeshlets[index] = i;
|
||||
}
|
||||
}
|
||||
GroupMemoryBarrierWithGroupSync();
|
||||
DispatchMesh(mesh.numMeshlets, 1, 1, p);
|
||||
DispatchMesh(head, 1, 1, p);
|
||||
}
|
||||
|
||||
@@ -5,21 +5,25 @@ import MaterialParameter;
|
||||
|
||||
struct PrimitiveAttributes
|
||||
{
|
||||
uint cull: SV_CullPrimitive;
|
||||
bool cull: SV_CullPrimitive;
|
||||
};
|
||||
|
||||
[numthreads(MESH_GROUP_SIZE, 1, 1)]
|
||||
[outputtopology("triangle")]
|
||||
[shader("mesh")]
|
||||
void meshMain(
|
||||
in uint threadID: SV_GroupIndex,
|
||||
in uint groupID: SV_GroupID,
|
||||
in payload MeshPayload meshPayload,
|
||||
out vertices FragmentParameter vertices[MAX_VERTICES],
|
||||
out indices uint3 indices[MAX_PRIMITIVES]
|
||||
){
|
||||
InstanceData inst = pScene.instances[meshPayload.instanceId];
|
||||
MeshletDescription m = pScene.meshletInfos[meshPayload.culledMeshlets[groupID]];
|
||||
in uint threadID: SV_GroupIndex,
|
||||
in uint groupID: SV_GroupID,
|
||||
in payload MeshPayload meshPayload,
|
||||
out vertices FragmentParameter vertices[MAX_VERTICES],
|
||||
out indices uint3 indices[MAX_PRIMITIVES],
|
||||
out primitives PrimitiveAttributes prim[MAX_PRIMITIVES]
|
||||
) {
|
||||
// meshlet number relative to start for this instance
|
||||
uint meshletNumber = meshPayload.culledMeshlets[groupID];
|
||||
InstanceData inst = pScene.instances[meshPayload.instanceId];
|
||||
MeshletDescription m = pScene.meshletInfos[meshPayload.meshletOffset + meshletNumber];
|
||||
MeshletCullingInfo cull = pScene.culledMeshlets[meshPayload.cullingOffset + meshletNumber];
|
||||
SetMeshOutputCounts(m.vertexCount, m.primitiveCount);
|
||||
|
||||
for(uint i = threadID; i < MAX_PRIMITIVES; i += MESH_GROUP_SIZE)
|
||||
@@ -29,7 +33,8 @@ void meshMain(
|
||||
uint local_idx0 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 0];
|
||||
uint local_idx1 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 1];
|
||||
uint local_idx2 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 2];
|
||||
indices[p] = uint3(local_idx0, local_idx1, local_idx2);
|
||||
indices[p] = uint3(local_idx0, local_idx1, local_idx2);
|
||||
prim[p].cull = cull.triangleCulled(p);
|
||||
}
|
||||
}
|
||||
for(uint i = threadID; i < MAX_VERTICES; i += MESH_GROUP_SIZE)
|
||||
@@ -42,8 +47,6 @@ void meshMain(
|
||||
#else
|
||||
VertexAttributes attr = pVertexData.getAttributes(m.indicesOffset + vertexIndex);
|
||||
#endif
|
||||
|
||||
attr.meshletId = -1;
|
||||
vertices[v] = attr.getParameter(inst.transformMatrix);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import Common;
|
||||
import Scene;
|
||||
import VertexData;
|
||||
import MaterialParameter;
|
||||
|
||||
struct PrimitiveAttributes
|
||||
{
|
||||
uint32_t prim : SV_PrimitiveID;
|
||||
bool cull: SV_CullPrimitive;
|
||||
};
|
||||
|
||||
[numthreads(MESH_GROUP_SIZE, 1, 1)]
|
||||
[outputtopology("triangle")]
|
||||
[shader("mesh")]
|
||||
void meshMain(
|
||||
in uint threadID: SV_GroupIndex,
|
||||
in uint groupID: SV_GroupID,
|
||||
in payload MeshPayload meshPayload,
|
||||
out vertices FragmentParameter vertices[MAX_VERTICES],
|
||||
out indices uint3 indices[MAX_PRIMITIVES],
|
||||
out primitives PrimitiveAttributes prim[MAX_PRIMITIVES]
|
||||
) {
|
||||
// meshlet number relative to start for this instance
|
||||
uint meshletNumber = meshPayload.culledMeshlets[groupID];
|
||||
InstanceData inst = pScene.instances[meshPayload.instanceId];
|
||||
MeshletDescription m = pScene.meshletInfos[meshPayload.meshletOffset + meshletNumber];
|
||||
MeshletCullingInfo cull = pScene.culledMeshlets[meshPayload.cullingOffset + meshletNumber];
|
||||
SetMeshOutputCounts(m.vertexCount, m.primitiveCount);
|
||||
|
||||
for(uint i = threadID; i < MAX_PRIMITIVES; i += MESH_GROUP_SIZE)
|
||||
{
|
||||
uint p = min(i, m.primitiveCount - 1);
|
||||
{
|
||||
uint local_idx0 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 0];
|
||||
uint local_idx1 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 1];
|
||||
uint local_idx2 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 2];
|
||||
indices[p] = uint3(local_idx0, local_idx1, local_idx2);
|
||||
prim[p].cull = cull.triangleCulled(p);
|
||||
//prim[p].prim = encodePrimitive(p, meshletNumber, meshPayload.instanceId);
|
||||
}
|
||||
}
|
||||
for(uint i = threadID; i < MAX_VERTICES; i += MESH_GROUP_SIZE)
|
||||
{
|
||||
uint v = min(i, m.vertexCount - 1);
|
||||
{
|
||||
uint vertexIndex = pScene.vertexIndices[m.vertexOffset + v];
|
||||
#ifdef POS_ONLY
|
||||
VertexAttributes attr = pVertexData.getPosition(m.indicesOffset + vertexIndex);
|
||||
#else
|
||||
VertexAttributes attr = pVertexData.getAttributes(m.indicesOffset + vertexIndex);
|
||||
#endif
|
||||
vertices[v] = attr.getParameter(inst.transformMatrix);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import Scene;
|
||||
import MaterialParameter;
|
||||
|
||||
[shader("pixel")]
|
||||
uint fragmentMain(in uint32_t encoded : SV_PrimitiveID) : SV_Target
|
||||
{
|
||||
return encoded;
|
||||
}
|
||||
@@ -26,7 +26,6 @@ struct FragmentParameter
|
||||
float3 biTangent_WS : TANGENT1;
|
||||
float3 position_WS : POSITION2;
|
||||
float3 vertexColor : COLOR0;
|
||||
uint meshletId : POSITION3;
|
||||
float2 texCoords[MAX_TEXCOORDS] : TEXCOORD0;
|
||||
MaterialParameter getMaterialParameter()
|
||||
{
|
||||
@@ -50,6 +49,13 @@ struct FragmentParameter
|
||||
}
|
||||
};
|
||||
|
||||
// data passed to visibility render
|
||||
struct VisibilityParameter
|
||||
{
|
||||
uint32_t triangleIndex : POSITION5;
|
||||
uint32_t meshletId : POSITION6;
|
||||
};
|
||||
|
||||
// data retrieved from VertexData
|
||||
struct VertexAttributes
|
||||
{
|
||||
@@ -58,7 +64,6 @@ struct VertexAttributes
|
||||
float3 tangent_MS;
|
||||
float3 biTangent_MS;
|
||||
float3 vertexColor;
|
||||
uint meshletId;
|
||||
float2 texCoords[MAX_TEXCOORDS];
|
||||
FragmentParameter getParameter(float4x4 transformMatrix)
|
||||
{
|
||||
@@ -66,19 +71,18 @@ struct VertexAttributes
|
||||
float4 worldPos = mul(transformMatrix, modelPos);
|
||||
float4 viewPos = mul(pViewParams.viewMatrix, worldPos);
|
||||
float4 clipPos = mul(pViewParams.projectionMatrix, viewPos);
|
||||
FragmentParameter result;
|
||||
result.position_CS = clipPos;
|
||||
float3x3 normalMatrix = float3x3(transformMatrix);
|
||||
float3 T = mul(normalMatrix, tangent_MS);
|
||||
float3 N = mul(normalMatrix, normal_MS);
|
||||
float3 B = mul(normalMatrix, biTangent_MS);
|
||||
FragmentParameter result;
|
||||
result.position_CS = clipPos;
|
||||
result.cameraPos_WS = pViewParams.cameraPos_WS.xyz;
|
||||
result.normal_WS = N;
|
||||
result.tangent_WS = T;
|
||||
result.biTangent_WS = B;
|
||||
result.position_WS = worldPos.xyz;
|
||||
result.vertexColor = vertexColor;
|
||||
result.meshletId = meshletId;
|
||||
for(uint i = 0; i < MAX_TEXCOORDS; ++i)
|
||||
{
|
||||
result.texCoords[i] = texCoords[i];
|
||||
|
||||
+67
-40
@@ -2,49 +2,54 @@ import Bounding;
|
||||
|
||||
struct MeshletDescription
|
||||
{
|
||||
AABB bounding;
|
||||
uint32_t vertexCount;
|
||||
uint32_t primitiveCount;
|
||||
uint32_t vertexOffset;
|
||||
uint32_t primitiveOffset;
|
||||
float3 color;
|
||||
uint32_t indicesOffset;
|
||||
AABB bounding;
|
||||
uint32_t vertexCount;
|
||||
uint32_t primitiveCount;
|
||||
uint32_t vertexOffset;
|
||||
uint32_t primitiveOffset;
|
||||
float3 color;
|
||||
uint32_t indicesOffset;
|
||||
};
|
||||
|
||||
struct MeshData
|
||||
{
|
||||
AABB bounding;
|
||||
uint32_t numMeshlets;
|
||||
uint32_t meshletOffset;
|
||||
uint32_t firstIndex;
|
||||
uint32_t numIndices;
|
||||
AABB bounding;
|
||||
uint32_t numMeshlets;
|
||||
uint32_t meshletOffset;
|
||||
uint32_t firstIndex;
|
||||
uint32_t numIndices;
|
||||
};
|
||||
|
||||
static const uint MAX_VERTICES = 256;
|
||||
static const uint MAX_PRIMITIVES = 256;
|
||||
static const uint TASK_GROUP_SIZE = 128;
|
||||
static const uint MESH_GROUP_SIZE = 32;
|
||||
static const uint64_t MAX_VERTICES = 256;
|
||||
static const uint64_t MAX_PRIMITIVES = 256;
|
||||
static const uint64_t TASK_GROUP_SIZE = 128;
|
||||
static const uint64_t MESH_GROUP_SIZE = 32;
|
||||
static const uint64_t MAX_MESHLETS_PER_INSTANCE = 2048;
|
||||
|
||||
struct InstanceData
|
||||
{
|
||||
float4x4 transformMatrix;
|
||||
float4x4 inverseTransformMatrix;
|
||||
float4x4 transformMatrix;
|
||||
float4x4 inverseTransformMatrix;
|
||||
};
|
||||
|
||||
struct MeshletCullingInfo
|
||||
{
|
||||
uint64_t cull[MAX_PRIMITIVES / 64];
|
||||
// lookup if a specific triangle is visible
|
||||
bool triangleVisible(uint32_t primIndex)
|
||||
{
|
||||
uint32_t arrIdx = primIndex / 64;
|
||||
uint32_t cullIdx = primIndex % 64;
|
||||
return (cull[arrIdx] & (1 << cullIdx)) != 0;
|
||||
}
|
||||
bool anyVisible()
|
||||
{
|
||||
return (cull[0] | cull[1] | cull[2] | cull[3]) != 0;
|
||||
}
|
||||
uint64_t visible[MAX_PRIMITIVES / 64];
|
||||
// lookup if a specific triangle is visible
|
||||
bool triangleVisible(uint32_t primIndex)
|
||||
{
|
||||
uint32_t arrIdx = primIndex / 64;
|
||||
uint32_t cullIdx = primIndex % 64;
|
||||
return (visible[arrIdx] & (1 << cullIdx)) != 0;
|
||||
}
|
||||
bool triangleCulled(uint32_t primIndex)
|
||||
{
|
||||
return !triangleVisible(primIndex);
|
||||
}
|
||||
bool anyVisible()
|
||||
{
|
||||
return (visible[0] + visible[1] + visible[2] + visible[3]) != 0;
|
||||
}
|
||||
};
|
||||
|
||||
struct DrawCallOffsets
|
||||
@@ -56,20 +61,42 @@ ConstantBuffer<DrawCallOffsets> pOffsets;
|
||||
|
||||
struct Scene
|
||||
{
|
||||
StructuredBuffer<InstanceData> instances;
|
||||
StructuredBuffer<MeshData> meshData;
|
||||
StructuredBuffer<MeshletDescription> meshletInfos;
|
||||
StructuredBuffer<uint8_t> primitiveIndices;
|
||||
StructuredBuffer<uint32_t> vertexIndices;
|
||||
StructuredBuffer<MeshletCullingInfo> culledMeshlets;
|
||||
StructuredBuffer<uint32_t> cullingOffsets;
|
||||
StructuredBuffer<InstanceData> instances;
|
||||
StructuredBuffer<MeshData> meshData;
|
||||
StructuredBuffer<MeshletDescription> meshletInfos;
|
||||
StructuredBuffer<uint8_t> primitiveIndices;
|
||||
StructuredBuffer<uint32_t> vertexIndices;
|
||||
StructuredBuffer<MeshletCullingInfo> culledMeshlets;
|
||||
StructuredBuffer<uint32_t> cullingOffsets;
|
||||
};
|
||||
layout(set=2)
|
||||
layout(set = 2)
|
||||
ParameterBlock<Scene> pScene;
|
||||
|
||||
uint32_t encodePrimitive(uint32_t primitiveId, uint32_t meshletId, uint32_t instanceId)
|
||||
{
|
||||
uint32_t encoded = instanceId;
|
||||
encoded *= MAX_MESHLETS_PER_INSTANCE;
|
||||
encoded += meshletId;
|
||||
encoded *= MAX_PRIMITIVES;
|
||||
encoded += primitiveId;
|
||||
return encoded;
|
||||
}
|
||||
|
||||
uint3 decodePrimitive(uint64_t encoded)
|
||||
{
|
||||
uint prim = uint(encoded % MAX_PRIMITIVES);
|
||||
encoded = encoded / MAX_PRIMITIVES;
|
||||
uint meshletId = uint(encoded % MAX_MESHLETS_PER_INSTANCE);
|
||||
encoded = encoded / MAX_MESHLETS_PER_INSTANCE;
|
||||
uint instanceId = uint(encoded);
|
||||
return uint3(prim, meshletId, instanceId);
|
||||
}
|
||||
|
||||
struct MeshPayload
|
||||
{
|
||||
uint instanceId;
|
||||
uint culledMeshlets[2048];
|
||||
uint instanceId;
|
||||
uint meshletOffset;
|
||||
uint cullingOffset;
|
||||
uint culledMeshlets[MAX_MESHLETS_PER_INSTANCE];
|
||||
};
|
||||
|
||||
|
||||
@@ -22,14 +22,6 @@ struct StaticMeshVertexData : IVertexData
|
||||
{
|
||||
VertexAttributes attributes;
|
||||
attributes.position_MS = float3(positions[3 * index + 0], positions[3 * index + 1], positions[3 * index + 2]);
|
||||
attributes.normal_MS = float3(0, 0, 0);
|
||||
attributes.tangent_MS = float3(0, 0, 0);
|
||||
attributes.biTangent_MS = float3(0, 0, 0);
|
||||
for (uint i = 0; i < MAX_TEXCOORDS; ++i)
|
||||
{
|
||||
attributes.texCoords[i] = float2(0, 0);
|
||||
}
|
||||
attributes.vertexColor = float3(0, 0, 0);
|
||||
return attributes;
|
||||
}
|
||||
StructuredBuffer<float> positions;
|
||||
|
||||
Reference in New Issue
Block a user