Starting to refactor into mesh shading

This commit is contained in:
Dynamitos
2023-10-07 19:29:53 +02:00
parent fcc4fc12d4
commit 1b6e1a8453
42 changed files with 488 additions and 1577 deletions
+3 -3
View File
@@ -9,15 +9,15 @@ struct ViewParameter
float4 cameraPos_WS;
float2 screenDimensions;
}
layout(set = INDEX_VIEW_PARAMS, binding = 0, std430)
ConstantBuffer<ViewParameter> gViewParams;
//layout(set = INDEX_VIEW_PARAMS, binding = 0, std430)
ParameterBlock<ViewParameter> viewParams;
// Convert screen space coordinates to view space.
float4 screenToClip( float4 screen )
{
// Convert to normalized texture coordinates
float2 texCoord = screen.xy / gViewParams.screenDimensions;
float2 texCoord = screen.xy / viewParams.screenDimensions;
// Convert to clip space
return float4( float2( texCoord.x, 1.0f-texCoord.y ) * 2.0f - 1.0f, screen.z, screen.w );
-27
View File
@@ -1,27 +0,0 @@
import LightEnv;
import Material;
import BRDF;
import MaterialParameter;
struct FlatColorMaterial : IMaterial
{
float3 diffuseColor;
float specularity;
typedef BlinnPhong BRDF;
BlinnPhong prepare(MaterialFragmentParameter input)
{
BlinnPhong result;
result.baseColor = diffuseColor;
result.specular = specularity;
result.normal = normalize(input.worldNormal);
result.roughness = 0.5;
result.specularTint = 0;
result.anisotropic = 1;
result.sheen = 1;
result.sheenTint = 0.5;
result.clearCoat = 0;
result.clearCoatGloss = 0;
return result;
}
};
+18 -8
View File
@@ -61,11 +61,21 @@ struct PointLight : ILightEnv
}
};
layout(set = INDEX_LIGHT_ENV, binding = 0, std430)
ShaderBuffer<DirectionalLight> directionalLights;
layout(set = INDEX_LIGHT_ENV, binding = 1, std430)
ConstantBuffer<uint> numDirectionalLights;
layout(set = INDEX_LIGHT_ENV, binding = 2, std430)
ShaderBuffer<PointLight> pointLights;
layout(set = INDEX_LIGHT_ENV, binding = 3, std430)
ConstantBuffer<uint> numPointLights;
struct LightEnv
{
StructuredBuffer<DirectionalLight> directionalLights;
uint numDirectionalLights;
StructureBuffer<PointLight> pointLights;
uint numPointLights;
};
ParameterBlock<LightEnv> gLightEnv;
//layout(set = INDEX_LIGHT_ENV, binding = 0, std430)
//ShaderBuffer<DirectionalLight> directionalLights;
//layout(set = INDEX_LIGHT_ENV, binding = 1, std430)
//ConstantBuffer<uint> numDirectionalLights;
//layout(set = INDEX_LIGHT_ENV, binding = 2, std430)
//ShaderBuffer<PointLight> pointLights;
//layout(set = INDEX_LIGHT_ENV, binding = 3, std430)
//ConstantBuffer<uint> numPointLights;
-1
View File
@@ -8,5 +8,4 @@ interface IMaterial
BRDF prepare(MaterialFragmentParameter input);
};
layout(set = INDEX_MATERIAL, binding = 0, std430)
ParameterBlock<IMaterial> gMaterial;
-37
View File
@@ -1,37 +0,0 @@
struct MaterialVertexParameter
{
float3 worldPosition;
float3 viewPosition;
float3x3 worldToTangent;
#if NUM_MATERIAL_TEXCOORDS
float2 texCoords[NUM_MATERIAL_TEXCOORDS];
#endif
#ifdef USE_INSTANCING
float4x4 instanceLocalToWorld;
float3 instanceLocalPosition;
float4 perInstanceParams;
uint instanceId;
#endif
//float3 preSkinnedPosition;
//float3 perSkinnedNormal;
float4 vertexColor;
};
struct MaterialFragmentParameter
{
float3 position_TS;
float3 viewDir_TS;
float4 vertexColor;
float3x3 worldToTangent;
#ifdef USE_INSTANCING
float3 perInstanceParams;
#endif
#if NUM_MATERIAL_TEXCOORDS
float2 texCoords[NUM_MATERIAL_TEXCOORDS];
#endif
float3 transformWorldToTangent(float3 vec)
{
return mul(worldToTangent, vec);
}
};
+21
View File
@@ -0,0 +1,21 @@
struct MeshletDescription
{
uint32_t vertexCount;
uint32_t primitiveCount;
uint32_t vertexOffset;
uint32_t primitiveOffset;
};
struct MeshletData
{
StructuredBuffer<MeshletDescription> meshletInfos;
StructuredBuffer<uint8_t> primitiveIndices;
StructuredBuffer<uint32_t> vertexIndices;
};
static const uint MAX_VERTICES = 64;
static const uint MAX_PRIMITIVES = 126;
static const uint GROUP_SIZE = 32;
ParameterBlock<MeshletData> meshlets;
-21
View File
@@ -1,21 +0,0 @@
import Material;
import InputGeometry;
struct ParallaxMaterial : IMaterial
{
Texture2D<float4> diffuseTexture;
Texture2D<float4> specularTexture;
Texture2D<float4> displacementTexture;
SamplerState textureSampler;
float specularity;
typedef BlinnPhong BRDF;
BlinnPhong prepare(InputGeometry geometry)
{
BlinnPhong blinn;
blinn.baseColor = diffuseTexture.Sample(textureSampler, geometry.getTexCoords()).xyz;
blinn.specularColor = specularTexture.Sample(textureSampler, geometry.getTexCoords()).xyz;
blinn.specular = specularity;
return blinn;
}
};
-12
View File
@@ -1,12 +0,0 @@
struct Particle
{
float3 position;
float mass;
float3 velocity;
float age;
float3 forceAccumulator;
float life;
float color;
float3 pad;
};
-17
View File
@@ -1,17 +0,0 @@
struct PrimitiveSceneData
{
float4x4 modelToWorld;
float4x4 worldToModel;
float4 actorLocation;
};
layout(set = INDEX_SCENE_DATA, binding = 0, std430)
ShaderBuffer<PrimitiveSceneData> gSceneData;
[[vk::push_constant]]
ConstantBuffer<uint> gSceneDataIndex;
PrimitiveSceneData getSceneData()
{
return gSceneData[gSceneDataIndex];
}
+20
View File
@@ -0,0 +1,20 @@
struct InstanceData
{
float4x4 transformMatrix;
};
struct MeshData
{
uint numMeshlets;
uint meshletOffset;
uint numInstances;
uint instanceOffset;
};
struct Scene
{
StructuredBuffer<InstanceData> instances;
StructuredBuffer<MeshData> meshData;
};
ParameterBlock<Scene> scene;
@@ -0,0 +1,41 @@
import VertexData;
import Common;
import Scene;
struct StaticMeshVertexAttributes : VertexAttributes
{
float4 position: SV_Position;
float2 texCoords: TEXCOORD0;
float3 normal: NORMAL0;
float4 getPosition()
{
return position;
}
float2 getTexCoord()
{
return texCoords;
}
float3 getNormal()
{
return normal;
}
}
struct StaticMeshVertexData : VertexData
{
StaticMeshVertexAttributes getAttributes(uint index, InstanceData inst)
{
StaticMeshVertexAttributes attr;
float4 worldPos = mul(inst.transformMatrix, positions[index]);
float4 viewPos = mul(viewParams.viewMatrix, worldPos);
float4 clipPos = mul(viewParams.projectionMatrix, viewPos);
attr.position = clipPos;
attr.texCoords = texCoords[index];
attr.normal = normals[index];
return attr;
}
StructuredBuffer<float4> positions;
StructuredBuffer<float2> texCoords;
StructuredBuffer<float3> normals;
}
ParameterBlock<StaticMeshVertexData> vertexData;
-173
View File
@@ -1,173 +0,0 @@
import Common;
import MaterialParameter;
import PrimitiveSceneData;
struct VertexValueCache
{
//This struct is passed between vertex and fragment stage
//which means, that it is passed as out mat3x3 in glsl
//but Slang for some reason puts a layout(row_major) above
//every attribute, including this matrix, but matrix layout
//qualifiers are illegal for non-uniform and buffer fields,
//leading to a compiler error
float3 position_MS;
float3 tangent;
float3 biTangent;
float3 normal;
float4 color;
#if NUM_MATERIAL_TEXCOORDS
float2 texCoords[NUM_MATERIAL_TEXCOORDS];
#endif
float4x4 getModelToTangent()
{
return transpose(float4x4(
float4(normalize(tangent), 0.0f),
float4(normalize(biTangent), 0.0f),
float4(normalize(normal), 0.0f),
float4(0, 0, 0, 1)
));
}
float4x4 getWorldToTangent()
{
return transpose(float4x4(
normalize(mul(getSceneData().worldToModel, float4(tangent, 0))),
normalize(mul(getSceneData().worldToModel, float4(biTangent, 0))),
normalize(mul(getSceneData().worldToModel, float4(normal, 0))),
float4(0, 0, 0, 1)
));
}
MaterialFragmentParameter getFragmentParameters()
{
MaterialFragmentParameter result = (MaterialFragmentParameter)0;
float4x4 worldToTangent = getWorldToTangent();
float4 cameraPos_TS = mul(worldToTangent, gViewParams.cameraPos_WS);
float4 position_TS = mul(getModelToTangent(), float4(position_MS, 1.0f));
result.worldToTangent = float3x3(worldToTangent);
result.position_TS = position_TS.xyz;
result.viewDir_TS = normalize((cameraPos_TS - position_TS).xyz);
result.vertexColor = color;
for(uint i = 0; i < NUM_MATERIAL_TEXCOORDS; ++i)
{
result.texCoords[i] = texCoords[i];
}
return result;
}
#if USE_INSTANCING
float4 instanceOrigin;
float3 instanceTransform1;
float3 instanceTransform2;
float3 instanceTransform3;
float4x4 getInstanceTransform()
{
return float4x4(
float4(instanceTransform1.xyz, 0.0f),
float4(instanceTransform2.xyz, 0.0f),
float4(instanceTransform3.xyz, 0.0f),
float4(instanceOrigin.xyz, 1.0f)
);
}
#endif // USE_INSTANCING
};
struct VertexShaderInput
{
float3 position;
float3 normal;
float3 tangent;
float3 biTangent;
float4 color;
#if NUM_MATERIAL_TEXCOORDS
#if NUM_MATERIAL_TEXCOORDS > 1
float4 packedTexCoords[NUM_MATERIAL_TEXCOORDS / 2];
#endif
#if NUM_MATERIAL_TEXCOORDS == 1
float2 packedTexCoords2;
#endif
#if NUM_MATERIAL_TEXCOORDS == 3
float2 packedTexCoords2;
#endif
#if NUM_MATERIAL_TEXCOORDS == 5
float2 packedTexCoords2;
#endif
#if NUM_MATERIAL_TEXCOORDS == 7
float2 packedTexCoords2;
#endif
#endif // NUM_MATERIAL_TEXCOORDS
#if USE_INSTANCING
float4 instanceOrigin;
float3 instanceTransform1;
float3 instanceTransform2;
float3 instanceTransform3;
#endif // USE_INSTANCING
float3 getModelPosition()
{
return position;
}
float3 getWorldPosition()
{
return mul(getSceneData().modelToWorld, float4(position, 1.0f)).xyz;
}
VertexValueCache getVertexCache()
{
VertexValueCache cache;
cache.position_MS = position;
cache.tangent = tangent;
cache.biTangent = biTangent;
cache.normal = normal;
cache.color = color;
#if USE_INSTANCING
cache.instanceTransform1 = instanceTransform1;
cache.instanceTransform2 = instanceTransform2;
cache.instanceTransform3 = instanceTransform3;
cache.instanceOrigin = instanceOrigin;
#endif
cache.texCoords[0] = packedTexCoords2;
return cache;
}
MaterialVertexParameter getMaterialVertexParameters(VertexValueCache cache, float3 worldPosition, float3 viewPosition)
{
MaterialVertexParameter result;
result.worldPosition = worldPosition;
result.viewPosition = viewPosition;
result.vertexColor = cache.color;
result.worldToTangent = float3x3(cache.getWorldToTangent());
// TODO instancing
/*for(int i = 0; i < NUM_MATERIAL_TEXCOORDS-1; i+=2)
{
result.texCoords[i] = packedTexCoords[i/2].xy;
if(i+1 < NUM_MATERIAL_TEXCOORDS)
{
result.texCoords = packedTexCoords[i / 2].zw;
}
}*/
result.texCoords[0] = packedTexCoords2;
return result;
}
};
struct PositionOnlyVertexShaderInput
{
float3 position;
#if USE_INSTANCING
float4 instanceOrigin;
float3 instanceTransform1;
float3 instanceTransform2;
float3 instanceTransform3;
#endif // USE_INSTANCING
float3 getWorldPosition()
{
return mul(getSceneData().modelToWorld, float4(position, 1.0f)).xyz;
}
};
-42
View File
@@ -1,42 +0,0 @@
import LightEnv;
import Material;
import BRDF;
import MaterialParameter;
struct TexturedMaterial : IMaterial
{
Texture2D diffuseTexture;
Texture2D specularTexture;
Texture2D normalTexture;
float uvScale;
float metallic = 0;
float subsurface = 0;
float roughness = 0.5;
float specularTint = 0;
float anisotropic = 0;
float sheen = 0;
float sheenTint = 0.5f;
float clearCoat = 0;
float clearCoatGloss = 1;
SamplerState textureSampler;
typedef BlinnPhong BRDF;
BlinnPhong prepare(MaterialFragmentParameter geometry)
{
BlinnPhong result;
result.baseColor = diffuseTexture.Sample(textureSampler, geometry.texCoords[0] * uvScale).xyz;
result.metallic = 0;
float3 bumpMapNormal = normalTexture.Sample(textureSampler, geometry.texCoords[0] * uvScale).xyz;
bumpMapNormal = 2.0 * bumpMapNormal - float3(1.0, 1.0, 1.0);
result.normal = geometry.transformNormalToWorld(bumpMapNormal);
result.specular = specularTexture.Sample(textureSampler, geometry.texCoords[0] * uvScale).x;
result.roughness = roughness;
result.specularTint = specularTint;
result.anisotropic = anisotropic;
result.sheen = sheen;
result.sheenTint = sheenTint;
result.clearCoat = clearCoat;
result.clearCoatGloss = clearCoatGloss;
return result;
}
};
+13
View File
@@ -0,0 +1,13 @@
import Scene;
interface VertexAttributes
{
float4 getPosition();
float2 getTexCoord();
float3 getNormal();
}
interface VertexData
{
VertexAttributes getAttributes(uint index, InstanceData inst);
};