Fixing slang compilation temporarily, renaming vertexfactory
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import LightEnv;
|
||||
import Material;
|
||||
import BRDF;
|
||||
import InputGeometry;
|
||||
import MaterialParameter;
|
||||
|
||||
struct FlatColorMaterial : IMaterial
|
||||
{
|
||||
@@ -9,12 +9,12 @@ struct FlatColorMaterial : IMaterial
|
||||
float specularity;
|
||||
|
||||
typedef BlinnPhong BRDF;
|
||||
BlinnPhong prepare(MaterialPixelParameter input)
|
||||
BlinnPhong prepare(MaterialFragmentParameter input)
|
||||
{
|
||||
BlinnPhong result;
|
||||
result.baseColor = diffuseColor;
|
||||
result.specular = specularity;
|
||||
result.normal = normalize(input.normal);
|
||||
result.normal = normalize(input.worldNormal);
|
||||
result.roughness = 0.5;
|
||||
result.specularTint = 0;
|
||||
result.anisotropic = 1;
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
interface IVertexShaderInput
|
||||
{
|
||||
//internally, the vertex layout is stored in vec4 for faster access,
|
||||
//but here we unwrap them for convenience
|
||||
float3 getVertexPosition();
|
||||
float2 getTexCoords();
|
||||
float3 getNormal();
|
||||
float3 getTangent();
|
||||
float3 getBiTangent();
|
||||
};
|
||||
|
||||
struct PositionOnlyVertexInput : IVertexShaderInput
|
||||
{
|
||||
float4 position;
|
||||
|
||||
float3 getVertexPosition() { return position.xyz; }
|
||||
float2 getTexCoords() { return float2(0, 0); }
|
||||
float3 getNormal() { return float3(0, 1, 0); }
|
||||
float3 getTangent() { return float3(1, 0, 0); }
|
||||
float3 getBiTangent() { return float3(0, 0, 1); }
|
||||
};
|
||||
|
||||
struct PositionAndNormalVertexInput : IVertexShaderInput
|
||||
{
|
||||
float4 position;
|
||||
float4 normal;
|
||||
|
||||
float3 getVertexPosition() { return position.xyz; }
|
||||
float2 getTexCoords() { return float2(0, 0); }
|
||||
float3 getNormal() { return normal.xyz; }
|
||||
float3 getTangent() { return float3(1, 0, 0); }
|
||||
float3 getBiTangent() { return float3(0, 0, 1); }
|
||||
};
|
||||
|
||||
struct InputGeometry : IVertexShaderInput
|
||||
{
|
||||
float4 position;
|
||||
float2 texCoord;
|
||||
float4 normal;
|
||||
float4 tangent;
|
||||
float4 bitangent;
|
||||
|
||||
float3 getVertexPosition() { return position.xyz; }
|
||||
float2 getTexCoords() { return texCoord; }
|
||||
float3 getNormal() { return normal.xyz; }
|
||||
float3 getTangent() { return tangent.xyz; }
|
||||
float3 getBiTangent() { return bitangent.xyz; }
|
||||
};
|
||||
|
||||
struct MaterialPixelParameter
|
||||
{
|
||||
float3 position;
|
||||
float2 texCoord;
|
||||
float3 viewDir;
|
||||
float3 normal;
|
||||
float3 tangent;
|
||||
float3 biTangent;
|
||||
float4 clipPosition;
|
||||
float3 transformLocalToWorld(float3 input)
|
||||
{
|
||||
float3 unitNormal = normalize(normal);
|
||||
float3 unitTangent = normalize(tangent);
|
||||
unitTangent = normalize(unitTangent - dot(unitTangent, unitNormal) * unitNormal);
|
||||
float3 unitBitangent = cross(unitTangent, unitNormal);
|
||||
float3x3 tbn = float3x3(unitTangent, unitBitangent, unitNormal);
|
||||
float3 result = mul(tbn, input);
|
||||
result = normalize(result);
|
||||
return result;
|
||||
}
|
||||
};
|
||||
@@ -1,10 +1,11 @@
|
||||
import InputGeometry;
|
||||
import StaticMeshShaderAttributes;
|
||||
import MaterialParameter;
|
||||
import BRDF;
|
||||
import Common;
|
||||
|
||||
interface ILightEnv
|
||||
{
|
||||
float3 illuminate<B:IBRDF>(InputGeometry input, B brdf, float3 wo);
|
||||
float3 illuminate<B:IBRDF>(VertexShaderInput input, B brdf, float3 wo);
|
||||
};
|
||||
|
||||
struct DirectionalLight : ILightEnv
|
||||
@@ -13,9 +14,9 @@ struct DirectionalLight : ILightEnv
|
||||
float4 direction;
|
||||
float4 intensity;
|
||||
|
||||
float3 illuminate<B:IBRDF>(MaterialPixelParameter input, B brdf, float3 wo)
|
||||
float3 illuminate<B:IBRDF>(MaterialFragmentParameter input, B brdf, float3 wo)
|
||||
{
|
||||
return intensity.xyz * brdf.evaluate(wo, direction.xyz, input.normal, input.tangent, input.biTangent, color.xyz);
|
||||
return intensity.xyz * brdf.evaluate(wo, direction.xyz, input.worldNormal, input.worldTangent, input.worldBiTangent, color.xyz);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -26,13 +27,13 @@ struct PointLight : ILightEnv
|
||||
float3 color;
|
||||
float range;
|
||||
|
||||
float3 illuminate<B:IBRDF>(MaterialPixelParameter input, B brdf, float3 viewDir)
|
||||
float3 illuminate<B:IBRDF>(MaterialFragmentParameter input, B brdf, float3 viewDir)
|
||||
{
|
||||
float3 lightVec = positionWS.xyz - input.position;
|
||||
float3 lightVec = positionWS.xyz - input.worldPosition;
|
||||
float d = length(lightVec);
|
||||
float3 direction = normalize(lightVec);
|
||||
float illuminance = max(1 - d / range, 0);
|
||||
return illuminance * brdf.evaluate(viewDir, direction, input.normal, input.tangent, input.biTangent, color);
|
||||
return illuminance * brdf.evaluate(viewDir, direction, input.worldNormal, input.worldTangent, input.worldBiTangent, color);
|
||||
}
|
||||
|
||||
bool insidePlane(Plane plane)
|
||||
@@ -68,3 +69,6 @@ struct Lights
|
||||
uint numDirectionalLights;
|
||||
uint numPointLights;
|
||||
};
|
||||
|
||||
layout(set = 0, binding = 1, std430)
|
||||
ConstantBuffer<Lights> gLightEnv;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import Common;
|
||||
import BRDF;
|
||||
import InputGeometry;
|
||||
import MaterialParameter;
|
||||
|
||||
interface IMaterial
|
||||
{
|
||||
associatedtype BRDF : IBRDF;
|
||||
BRDF prepare(MaterialPixelParameter geometry);
|
||||
BRDF prepare(MaterialFragmentParameter input);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
struct MaterialVertexParameter
|
||||
{
|
||||
float3 worldPosition;
|
||||
float3x3 tangentToWorld;
|
||||
#if NUM_MATERIAL_TEXCOORDS
|
||||
float2 texCoords[NUM_MATERIAL_TEXCOORDS];
|
||||
#endif
|
||||
#if USE_INSTANCING
|
||||
float4x4 instanceLocalToWorld;
|
||||
float3 instanceLocalPosition;
|
||||
float4 perInstanceParams;
|
||||
uint instanceId;
|
||||
#endif
|
||||
//float3 preSkinnedPosition;
|
||||
//float3 perSkinnedNormal;
|
||||
float4 vertexColor;
|
||||
};
|
||||
|
||||
struct MaterialFragmentParameter
|
||||
{
|
||||
float3 worldPosition;
|
||||
float3 viewDir;
|
||||
float3 worldNormal;
|
||||
float3 worldTangent;
|
||||
float3 worldBiTangent;
|
||||
float4 clipPosition;
|
||||
float4 vertexColor;
|
||||
float3x3 tangentToWorld;
|
||||
#if USE_INSTANCING
|
||||
float3 perInstanceParams;
|
||||
#endif
|
||||
#if NUM_MATERIAL_TEXCOORDS
|
||||
float2 texCoords[NUM_MATERIAL_TEXCOORDS];
|
||||
#endif
|
||||
float3 transformNormalToWorld(float3 tangentSpaceNormal)
|
||||
{
|
||||
float3 result = mul(tangentToWorld, tangentSpaceNormal);
|
||||
return result;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
struct PrimitiveSceneData
|
||||
{
|
||||
float4x4 localToWorld;
|
||||
float4x4 worldToLocal;
|
||||
};
|
||||
|
||||
|
||||
struct ViewParameter
|
||||
{
|
||||
float4x4 viewMatrix;
|
||||
float4x4 projectionMatrix;
|
||||
float4 cameraPos_WS;
|
||||
};
|
||||
layout(set = 0)
|
||||
ConstantBuffer<ViewParameter> gViewParams;
|
||||
|
||||
layout(set = 2)
|
||||
ConstantBuffer<PrimitiveSceneData> gSceneData;
|
||||
|
||||
@@ -0,0 +1,194 @@
|
||||
import MaterialParameter;
|
||||
import PrimitiveSceneData;
|
||||
|
||||
struct VertexValueCache
|
||||
{
|
||||
float3x3 tangentToLocal;
|
||||
float3x3 tangentToWorld;
|
||||
float tangentToWorldSign;
|
||||
float4 color;
|
||||
|
||||
#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 ShaderAttributeInterpolation
|
||||
{
|
||||
float3 worldPosition;
|
||||
float3 tangentX;
|
||||
float4 tangentZ;
|
||||
float4 color;
|
||||
#if NUM_MATERIAL_TEXCOORDS
|
||||
float4 packedTexCoords[(NUM_MATERIAL_TEXCOORDS+1) / 2];
|
||||
#endif // NUM_MATERIAL_TEXCOORDS
|
||||
#if USE_INSTANCING
|
||||
float4 perInstanceParams;
|
||||
#endif
|
||||
|
||||
MaterialFragmentParameter getMaterialParameter(float4 clipSpacePosition)
|
||||
{
|
||||
MaterialFragmentParameter result;
|
||||
#if NUM_MATERIAL_TEXCOORDS
|
||||
for(int i = 0; i < NUM_MATERIAL_TEXCOORDS; ++i)
|
||||
{
|
||||
if(i % 2)
|
||||
{
|
||||
result.texCoords[i] = packedTexCoords[i / 2].zw;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.texCoords[i] = packedTexCoords[i / 2].xy;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
float3 tangentY = cross(tangentZ.xyz, tangentX) * tangentZ.w;
|
||||
result.tangentToWorld[0] = cross(tangentY, tangentZ.xyz) * tangentZ.w;
|
||||
result.tangentToWorld[1] = tangentY;
|
||||
result.tangentToWorld[2] = tangentZ.xyz;
|
||||
result.clipPosition = clipSpacePosition;
|
||||
result.worldPosition = worldPosition;
|
||||
result.viewDir = gViewParams.cameraPos_WS.xyz - worldPosition;
|
||||
result.worldNormal = tangentZ.xyz;
|
||||
result.worldTangent = tangentX;
|
||||
result.worldBiTangent = tangentY;
|
||||
result.vertexColor = color;
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
struct VertexShaderInput
|
||||
{
|
||||
float4 position;
|
||||
float3 tangentX;
|
||||
float4 tangentZ;
|
||||
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
|
||||
|
||||
float3x3 calcTangentToLocal(out float tangentSign)
|
||||
{
|
||||
float3x3 result;
|
||||
tangentSign = tangentZ.w;
|
||||
|
||||
float3 tangentY = cross(tangentZ.xyz, tangentX) * tangentZ.w;
|
||||
result[0] = cross(tangentY, tangentZ.xyz) * tangentZ.w;
|
||||
result[1] = tangentY;
|
||||
result[2] = tangentZ.xyz;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
float3 getWorldPosition(VertexValueCache cache)
|
||||
{
|
||||
float4x4 localToWorld = gSceneData.localToWorld;
|
||||
float3 rotatedPosition = localToWorld[0].xyz * position.xxx + localToWorld[1].xyz * position.yyy + localToWorld[2].xyz * position.zzz;
|
||||
return rotatedPosition + localToWorld[3].xyz;
|
||||
}
|
||||
|
||||
VertexValueCache getVertexCache()
|
||||
{
|
||||
VertexValueCache cache;
|
||||
cache.color = color;
|
||||
#if USE_INSTANCING
|
||||
cache.instanceTransform1 = instanceTransform1;
|
||||
cache.instanceTransform2 = instanceTransform2;
|
||||
cache.instanceTransform3 = instanceTransform3;
|
||||
cache.instanceOrigin = instanceOrigin;
|
||||
#endif
|
||||
|
||||
float tangentSign;
|
||||
cache.tangentToLocal = calcTangentToLocal(tangentSign);
|
||||
float3x3 localToWorld = float3x3(gSceneData.localToWorld[0].xyz, gSceneData.localToWorld[1].xyz, gSceneData.localToWorld[2].xyz);
|
||||
cache.tangentToWorld = mul(cache.tangentToLocal, localToWorld);
|
||||
cache.tangentToWorldSign = tangentSign;
|
||||
|
||||
return cache;
|
||||
}
|
||||
|
||||
MaterialVertexParameter getMaterialVertexParameters(VertexValueCache cache, float3 worldPosition, float3x3 tangentToLocal)
|
||||
{
|
||||
MaterialVertexParameter result;
|
||||
result.worldPosition = worldPosition;
|
||||
result.vertexColor = cache.color;
|
||||
result.tangentToWorld = cache.tangentToWorld;
|
||||
// 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;
|
||||
}
|
||||
|
||||
ShaderAttributeInterpolation getInterpolants(VertexValueCache cache, MaterialVertexParameter vertexParams)
|
||||
{
|
||||
ShaderAttributeInterpolation result = (ShaderAttributeInterpolation)0;
|
||||
result.tangentX = tangentX;
|
||||
result.tangentZ = tangentZ;
|
||||
result.color = color;
|
||||
result.worldPosition = vertexParams.worldPosition;
|
||||
for(int i = 0; i < NUM_MATERIAL_TEXCOORDS; ++i)
|
||||
{
|
||||
if(i % 2)
|
||||
{
|
||||
result.packedTexCoords[i / 2].zw = vertexParams.texCoords[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
result.packedTexCoords[i / 2].xy = vertexParams.texCoords[i];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
struct PositionOnlyVertexShaderInput
|
||||
{
|
||||
float4 position;
|
||||
#if USE_INSTANCING
|
||||
float4 instanceOrigin;
|
||||
float3 instanceTransform1;
|
||||
float3 instanceTransform2;
|
||||
float3 instanceTransform3;
|
||||
#endif // USE_INSTANCING
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
import LightEnv;
|
||||
import Material;
|
||||
import BRDF;
|
||||
import InputGeometry;
|
||||
import MaterialParameter;
|
||||
|
||||
struct TexturedMaterial : IMaterial
|
||||
{
|
||||
@@ -21,15 +21,15 @@ struct TexturedMaterial : IMaterial
|
||||
SamplerState textureSampler;
|
||||
|
||||
typedef BlinnPhong BRDF;
|
||||
BlinnPhong prepare(MaterialPixelParameter geometry)
|
||||
BlinnPhong prepare(MaterialFragmentParameter geometry)
|
||||
{
|
||||
BlinnPhong result;
|
||||
result.baseColor = diffuseTexture.Sample(textureSampler, geometry.texCoord * uvScale).xyz;
|
||||
result.baseColor = diffuseTexture.Sample(textureSampler, geometry.texCoords[0] * uvScale).xyz;
|
||||
result.metallic = 0;
|
||||
float3 bumpMapNormal = normalTexture.Sample(textureSampler, geometry.texCoord * uvScale).xyz;
|
||||
float3 bumpMapNormal = normalTexture.Sample(textureSampler, geometry.texCoords[0] * uvScale).xyz;
|
||||
bumpMapNormal = 2.0 * bumpMapNormal - float3(1.0, 1.0, 1.0);
|
||||
result.normal = geometry.transformLocalToWorld(bumpMapNormal);
|
||||
result.specular = specularTexture.Sample(textureSampler, geometry.texCoord * uvScale).x;
|
||||
result.normal = geometry.transformNormalToWorld(bumpMapNormal);
|
||||
result.specular = specularTexture.Sample(textureSampler, geometry.texCoords[0] * uvScale).x;
|
||||
result.roughness = roughness;
|
||||
result.specularTint = specularTint;
|
||||
result.anisotropic = anisotropic;
|
||||
|
||||
Reference in New Issue
Block a user