Reducing startup time significantly
This commit is contained in:
@@ -15,17 +15,19 @@ layout(set=5)
|
||||
ParameterBlock<LightCullingData> pLightCullingData;
|
||||
|
||||
[shader("pixel")]
|
||||
float4 fragmentMain(in MaterialParameter params : PARAMETER) : SV_Target
|
||||
float4 fragmentMain(in FragmentParameter params : PARAMETER) : SV_Target
|
||||
{
|
||||
let brdf = pMaterial.prepare(params);
|
||||
MaterialParameter materialParams = params.getMaterialParameter();
|
||||
LightingParameter lightingParams = params.getLightingParameter();
|
||||
let brdf = pMaterial.prepare(materialParams);
|
||||
float3 result = float3(0, 0, 0);
|
||||
for(int i = 0; i < pLightEnv.numDirectionalLights; ++i)
|
||||
{
|
||||
result += pLightEnv.directionalLights[i].illuminate(params, brdf);
|
||||
result += pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
|
||||
}
|
||||
for(int i = 0; i < pLightEnv.numPointLights; ++i)
|
||||
{
|
||||
result += pLightEnv.pointLights[i].illuminate(params, brdf);
|
||||
result += pLightEnv.pointLights[i].illuminate(lightingParams, brdf);
|
||||
}
|
||||
return float4(result, 1.0f);
|
||||
}
|
||||
|
||||
@@ -15,11 +15,12 @@ layout(set=2)
|
||||
ParameterBlock<Scene> pScene;
|
||||
|
||||
[shader("vertex")]
|
||||
VertexAttributes vertexMain(
|
||||
void vertexMain(
|
||||
uint vertexId: SV_VertexID,
|
||||
uint instanceId: SV_InstanceID,
|
||||
out FragmentParameter params: PARAMETER,
|
||||
){
|
||||
InstanceData inst = pScene.instances[instanceId];
|
||||
VertexAttributes attr = pVertexData.getAttributes(vertexId, inst.transformMatrix);
|
||||
return attr;
|
||||
params = attr.getParameter(inst.transformMatrix);
|
||||
}
|
||||
@@ -29,7 +29,7 @@ struct BlinnPhong : IBRDF
|
||||
float3 h = normalize(lightDir_TS + viewDir_TS);
|
||||
float nDotH = saturate(dot(normal, h));
|
||||
|
||||
return baseColor * (nDotL + nDotH) * lightColor;
|
||||
return baseColor;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import MaterialParameter;
|
||||
|
||||
interface ILightEnv
|
||||
{
|
||||
float3 illuminate<B:IBRDF>(MaterialParameter input, B brdf);
|
||||
float3 illuminate<B:IBRDF>(LightingParameter input, B brdf);
|
||||
};
|
||||
|
||||
struct DirectionalLight : ILightEnv
|
||||
@@ -12,10 +12,10 @@ struct DirectionalLight : ILightEnv
|
||||
float4 color;
|
||||
float4 direction;
|
||||
|
||||
float3 illuminate<B:IBRDF>(MaterialParameter input, B brdf)
|
||||
float3 illuminate<B:IBRDF>(LightingParameter params, B brdf)
|
||||
{
|
||||
float3 lightDir_TS = normalize(direction.xyz);
|
||||
return brdf.evaluate(input.viewDir_TS, -lightDir_TS, color.xyz);
|
||||
float3 lightDir_TS = mul(params.tbn, normalize(direction.xyz));
|
||||
return brdf.evaluate(params.viewDir_TS, -lightDir_TS, color.xyz);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -24,41 +24,37 @@ struct PointLight : ILightEnv
|
||||
float4 position_WS;
|
||||
float4 colorRange;
|
||||
|
||||
float3 illuminate<B:IBRDF>(MaterialParameter input, B brdf)
|
||||
float3 illuminate<B:IBRDF>(LightingParameter params, B brdf)
|
||||
{
|
||||
float3 position_TS = position_WS.xyz;
|
||||
float3 lightDir_TS = position_TS - input.position_TS;
|
||||
float3 position_TS = mul(params.tbn, position_WS.xyz);
|
||||
float3 lightDir_TS = position_TS - params.position_TS;
|
||||
float d = length(lightDir_TS);
|
||||
float illuminance = max(1 - d / colorRange.w, 0);
|
||||
return illuminance * brdf.evaluate(input.viewDir_TS, normalize(lightDir_TS), colorRange.xyz);
|
||||
return illuminance * brdf.evaluate(params.viewDir_TS, normalize(lightDir_TS), colorRange.xyz);
|
||||
}
|
||||
|
||||
bool insidePlane(Plane plane)
|
||||
{
|
||||
return dot(plane.n, getViewPos().xyz) - plane.d < -colorRange.w;
|
||||
return true;//dot(plane.n, getViewPos().xyz) - plane.d < -colorRange.w;
|
||||
}
|
||||
|
||||
bool insideFrustum(Frustum frustum, float zNear, float zFar)
|
||||
{
|
||||
bool result = true;
|
||||
|
||||
if(getViewPos().z - colorRange.w > zNear || getViewPos().z + colorRange.w < zFar)
|
||||
{
|
||||
//result = false;
|
||||
}
|
||||
for(int i = 0; i < 4 && result; ++i)
|
||||
{
|
||||
if(insidePlane(frustum.sides[i]))
|
||||
{
|
||||
//result = false;
|
||||
}
|
||||
}
|
||||
//if(getViewPos().z - colorRange.w > zNear || getViewPos().z + colorRange.w < zFar)
|
||||
//{
|
||||
// //result = false;
|
||||
//}
|
||||
//for(int i = 0; i < 4 && result; ++i)
|
||||
//{
|
||||
// if(insidePlane(frustum.sides[i]))
|
||||
// {
|
||||
// //result = false;
|
||||
// }
|
||||
//}
|
||||
return result;
|
||||
}
|
||||
float3 getViewPos()
|
||||
{
|
||||
return mul(pViewParams.viewMatrix, position_WS).xyz;
|
||||
}
|
||||
};
|
||||
|
||||
struct LightEnv
|
||||
|
||||
@@ -1,17 +1,80 @@
|
||||
import Common;
|
||||
|
||||
struct MaterialParameter
|
||||
{
|
||||
float3 position_TS;
|
||||
float3 worldPosition;
|
||||
float2 texCoords;
|
||||
float3 normal;
|
||||
float3 tangent;
|
||||
float3 biTangent;
|
||||
float3 viewDir_TS;
|
||||
float3 vertexColor;
|
||||
}
|
||||
float3 position_TS : POSITION0;
|
||||
float3 position_WS : POSITION1;
|
||||
float2 texCoords : TEXCOORDS0;
|
||||
float3 vertexColor : COLOR0;
|
||||
};
|
||||
|
||||
// data used by light environment
|
||||
struct LightingParameter
|
||||
{
|
||||
// world to tangent space
|
||||
float3x3 tbn;
|
||||
float3 position_TS;
|
||||
float3 viewDir_TS;
|
||||
};
|
||||
|
||||
// data passed to fragment shader
|
||||
struct FragmentParameter
|
||||
{
|
||||
float3 position_TS;
|
||||
float3 viewDir_TS;
|
||||
float3 normal_WS;
|
||||
float3 tangent_WS;
|
||||
float3 biTangent_WS;
|
||||
float3 position_WS;
|
||||
float4 position_CS : SV_Position;
|
||||
float2 texCoords;
|
||||
float3 vertexColor;
|
||||
MaterialParameter getMaterialParameter()
|
||||
{
|
||||
MaterialParameter result;
|
||||
result.position_TS = position_TS;
|
||||
result.position_WS = position_WS;
|
||||
result.texCoords = texCoords;
|
||||
result.vertexColor = vertexColor;
|
||||
return result;
|
||||
}
|
||||
LightingParameter getLightingParameter()
|
||||
{
|
||||
LightingParameter result;
|
||||
result.tbn = transpose(float3x3(normalize(tangent_WS), normalize(biTangent_WS), normalize(normal_WS)));
|
||||
result.position_TS = position_TS;
|
||||
result.viewDir_TS = viewDir_TS;
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
// data retrieved from VertexData
|
||||
struct VertexAttributes
|
||||
{
|
||||
MaterialParameter parameter : PARAMETER;
|
||||
float4 clipPosition: SV_POSITION;
|
||||
float3 normal_MS;
|
||||
float3 tangent_MS;
|
||||
float3 biTangent_MS;
|
||||
float3 position_WS;
|
||||
float4 position_CS;
|
||||
float2 texCoords;
|
||||
float3 vertexColor;
|
||||
FragmentParameter getParameter(float4x4 transformMatrix)
|
||||
{
|
||||
float3 tangent_WS = mul(transformMatrix, float4(normalize(tangent_MS), 0)).xyz;
|
||||
float3 biTangent_WS = mul(transformMatrix, float4(normalize(biTangent_MS), 0)).xyz;
|
||||
float3 normal_WS = mul(transformMatrix, float4(normalize(normal_MS), 0)).xyz;
|
||||
// Transforms from world space into tangent space
|
||||
float3x3 tbn = transpose(float3x3(tangent_WS, biTangent_WS, normal_WS));
|
||||
FragmentParameter result;
|
||||
result.position_TS = mul(tbn, position_WS);
|
||||
result.viewDir_TS = mul(tbn, pViewParams.cameraPos_WS.xyz - position_WS);
|
||||
result.normal_WS = normal_WS;
|
||||
result.tangent_WS = tangent_WS;
|
||||
result.biTangent_WS = biTangent_WS;
|
||||
result.position_WS = position_WS;
|
||||
result.position_CS = position_CS;
|
||||
result.texCoords = texCoords;
|
||||
result.vertexColor = vertexColor;
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -7,19 +7,17 @@ struct StaticMeshVertexData : IVertexData
|
||||
VertexAttributes getAttributes(uint index, float4x4 transform)
|
||||
{
|
||||
VertexAttributes attributes;
|
||||
MaterialParameter params;
|
||||
float4 localPos = float4(positions[3 * index + 0], positions[3 * index + 1], positions[3 * index + 2], 1);
|
||||
float4 worldPos = mul(transform, localPos);
|
||||
float4 modelPos = float4(positions[3 * index + 0], positions[3 * index + 1], positions[3 * index + 2], 1);
|
||||
float4 worldPos = mul(transform, modelPos);
|
||||
float4 viewPos = mul(pViewParams.viewMatrix, worldPos);
|
||||
float4 clipPos = mul(pViewParams.projectionMatrix, viewPos);
|
||||
params.worldPosition = worldPos.xyz;
|
||||
params.texCoords = float2(texCoords[2 * index + 0], texCoords[2 * index + 1]);
|
||||
params.normal = float3(normals[3 * index + 0], normals[3 * index + 1], normals[3 * index + 2]);
|
||||
params.tangent = float3(tangents[3 * index + 0], tangents[3 * index + 1], tangents[3 * index + 2]);
|
||||
params.biTangent = float3(biTangents[3 * index + 0], biTangents[3 * index + 1], biTangents[3 * index + 2]);
|
||||
params.vertexColor = float3(color[3 * index + 0], color[3 * index + 1], color[3 * index + 2]);
|
||||
attributes.parameter = params;
|
||||
attributes.clipPosition = clipPos;
|
||||
attributes.normal_MS = float3(normals[3 * index + 0], normals[3 * index + 1], normals[3 * index + 2]);
|
||||
attributes.tangent_MS = float3(tangents[3 * index + 0], tangents[3 * index + 1], tangents[3 * index + 2]);
|
||||
attributes.biTangent_MS = float3(biTangents[3 * index + 0], biTangents[3 * index + 1], biTangents[3 * index + 2]);
|
||||
attributes.position_WS = worldPos.xyz;
|
||||
attributes.position_CS = clipPos;
|
||||
attributes.texCoords = float2(texCoords[2 * index + 0], texCoords[2 * index + 1]);
|
||||
attributes.vertexColor = float3(color[3 * index + 0], color[3 * index + 1], color[3 * index + 2]);
|
||||
return attributes;
|
||||
}
|
||||
StructuredBuffer<float> positions;
|
||||
|
||||
Reference in New Issue
Block a user