Improving Material shader code generation
This commit is contained in:
@@ -10,7 +10,7 @@ struct ComputeShaderInput
|
||||
uint groupIndex : SV_GroupIndex;
|
||||
};
|
||||
|
||||
layout(set = 0, binding = 0)
|
||||
layout(set = 0, binding = 0, std430)
|
||||
cbuffer DispatchParams
|
||||
{
|
||||
uint3 numThreadGroups;
|
||||
@@ -18,7 +18,7 @@ cbuffer DispatchParams
|
||||
uint3 numThreads;
|
||||
uint pad1;
|
||||
}
|
||||
layout(set = 0, binding = 2)
|
||||
layout(set = 0, binding = 2, std430)
|
||||
RWStructuredBuffer<Frustum> out_Frustums;
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import InputGeometry;
|
||||
import Material;
|
||||
import VERTEX_INPUT_IMPORT;
|
||||
import MATERIAL_IMPORT;
|
||||
|
||||
struct ViewParams
|
||||
{
|
||||
@@ -10,6 +12,11 @@ struct ViewParams
|
||||
layout(set = 0, binding = 0)
|
||||
ParameterBlock<ViewParams> gViewParams;
|
||||
|
||||
|
||||
layout(set = 1)
|
||||
type_param TMaterial : IMaterial;
|
||||
ParameterBlock<TMaterial> gMaterial;
|
||||
|
||||
struct ModelParameter
|
||||
{
|
||||
float4x4 modelMatrix;
|
||||
@@ -18,16 +25,21 @@ struct ModelParameter
|
||||
[[vk::push_constant]]
|
||||
ConstantBuffer<ModelParameter> gModelParams;
|
||||
|
||||
struct VertexShaderOutput
|
||||
struct VertexStageOutput
|
||||
{
|
||||
float4 out_Position : SV_Position;
|
||||
float4 position : SV_Position;
|
||||
}
|
||||
[shader("vertex")]
|
||||
VertexShaderOutput depthPrepass(PositionOnlyVertexInput input)
|
||||
{
|
||||
VertexShaderOutput out;
|
||||
float4 worldPos = mul(gModelParams.modelMatrix, float4(input.getVertexPosition(), 1));
|
||||
float4 viewPos = mul(gViewParams.viewMatrix, worldPos);
|
||||
out.out_Position = mul(gViewParams.projectionMatrix, viewPos);
|
||||
return out;
|
||||
VertexStageOutput vertexMain(PositionOnlyVertexShaderInput input)
|
||||
{
|
||||
VertexStageOutput output;
|
||||
|
||||
float3 worldPosition = input.getWorldPosition();
|
||||
worldPosition += gMaterial.getWorldOffset();
|
||||
float4 clipSpacePosition;
|
||||
|
||||
float4 viewSpacePosition = mul(gViewParams.viewMatrix, float4(worldPosition, 1));
|
||||
clipSpacePosition = mul(gViewParams.projectionMatrix, viewSpacePosition);
|
||||
output.position = clipSpacePosition;
|
||||
return output;
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
import LightEnv;
|
||||
import BRDF;
|
||||
import Material;
|
||||
import TexturedMaterial;
|
||||
import FlatColorMaterial;
|
||||
import Common;
|
||||
//TODO revert to pre processed shader attributes
|
||||
import StaticMeshShaderAttributes;
|
||||
|
||||
import VERTEX_INPUT_IMPORT;
|
||||
import MATERIAL_IMPORT;
|
||||
import PrimitiveSceneData;
|
||||
import MaterialParameter;
|
||||
|
||||
@@ -14,9 +13,9 @@ import MaterialParameter;
|
||||
//layout(set = 0, binding = 3)
|
||||
//RWTexture2D<uint2> lightGrid;
|
||||
|
||||
layout(set = 1, std430)
|
||||
//type_param TMaterial : IMaterial;
|
||||
ParameterBlock<TexturedMaterial> gMaterial;
|
||||
type_param TMaterial : IMaterial;
|
||||
layout(set = 1)
|
||||
ParameterBlock<TMaterial> gMaterial;
|
||||
|
||||
struct VertexStageOutput
|
||||
{
|
||||
@@ -31,7 +30,7 @@ VertexStageOutput vertexMain(
|
||||
{
|
||||
VertexStageOutput output;
|
||||
VertexValueCache cache = input.getVertexCache();
|
||||
float3 worldPosition = input.getWorldPosition(cache);
|
||||
float3 worldPosition = input.getWorldPosition();
|
||||
float4 clipSpacePosition;
|
||||
|
||||
float3x3 tangentToLocal = cache.tangentToLocal;
|
||||
@@ -52,7 +51,7 @@ float4 fragmentMain(
|
||||
) : SV_Target
|
||||
{
|
||||
MaterialFragmentParameter materialParams = input.getMaterialParameter(position);
|
||||
BlinnPhong brdf = gMaterial.prepare(materialParams);
|
||||
TMaterial.BRDF brdf = gMaterial.prepare(materialParams);
|
||||
|
||||
float3 viewDir = normalize(materialParams.viewDir);
|
||||
|
||||
|
||||
@@ -50,26 +50,51 @@
|
||||
"clearCoatGloss": {
|
||||
"type": "float",
|
||||
"default": "1.0f"
|
||||
},
|
||||
"worldOffset": {
|
||||
"type": "float3",
|
||||
"default": "float3(0, 0, 0)"
|
||||
},
|
||||
"textureSampler": {
|
||||
"type": "SamplerState"
|
||||
}
|
||||
},
|
||||
"code": [
|
||||
"result.baseColor = diffuseTexture.Sample(textureSampler, input.texCoords[0] * uvScale).xyz;",
|
||||
"result.metallic = 0;",
|
||||
"float3 bumpMapNormal = normalTexture.Sample(textureSampler, input.texCoords[0] * uvScale).xyz;",
|
||||
"bumpMapNormal = 2.0 * bumpMapNormal - float3(1.0, 1.0, 1.0);",
|
||||
"result.normal = input.transformLocalToWorld(bumpMapNormal);",
|
||||
"result.specular = specularTexture.Sample(textureSampler, input.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;"
|
||||
]
|
||||
|
||||
"code": {
|
||||
"baseColor": [
|
||||
"return diffuseTexture.Sample(textureSampler, input.texCoords[0] * uvScale).xyz;"
|
||||
],
|
||||
"metallic": [
|
||||
"return 0;"
|
||||
],
|
||||
"normal": [
|
||||
"return normalTexture.Sample(textureSampler, input.texCoords[0] * uvScale).xyz;"
|
||||
],
|
||||
"specular": [
|
||||
"return specularTexture.Sample(textureSampler, input.texCoords[0] * uvScale).x;"
|
||||
],
|
||||
"roughness": [
|
||||
"return roughness;"
|
||||
],
|
||||
"specularTint": [
|
||||
"return specularTint;"
|
||||
],
|
||||
"anisotropic": [
|
||||
"return anisotropic;"
|
||||
],
|
||||
"sheen": [
|
||||
"return sheen;"
|
||||
],
|
||||
"sheenTint": [
|
||||
"return sheenTint;"
|
||||
],
|
||||
"clearCoat": [
|
||||
"return clearCoat;"
|
||||
],
|
||||
"clearCoatGloss": [
|
||||
"return clearCoatGloss;"
|
||||
],
|
||||
"worldOffset": [
|
||||
"return worldOffset;"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -10,15 +10,9 @@ struct BlinnPhong : IBRDF
|
||||
float3 baseColor;
|
||||
float metallic = 0;
|
||||
float3 normal = float3(0, 1, 0);
|
||||
float subsurface = 0;
|
||||
float specular = 0.5;
|
||||
float roughness = 0.5;
|
||||
float specularTint = 0;
|
||||
float anisotropic = 0;
|
||||
float sheen = 0;
|
||||
float sheenTint = 0.5f;
|
||||
float clearCoat = 0;
|
||||
float clearCoatGloss = 1;
|
||||
float sheen = 1.f;
|
||||
|
||||
float3 evaluate(float3 view, float3 light, float3 surfaceNormal, float3 tangent, float3 biTangent, float3 lightColor)
|
||||
{
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import StaticMeshShaderAttributes;
|
||||
import MaterialParameter;
|
||||
import BRDF;
|
||||
import Common;
|
||||
|
||||
interface ILightEnv
|
||||
{
|
||||
float3 illuminate<B:IBRDF>(VertexShaderInput input, B brdf, float3 wo);
|
||||
float3 illuminate<B:IBRDF>(MaterialFragmentParameter input, B brdf, float3 wo);
|
||||
};
|
||||
|
||||
struct DirectionalLight : ILightEnv
|
||||
@@ -70,5 +69,5 @@ struct Lights
|
||||
uint numPointLights;
|
||||
};
|
||||
|
||||
layout(set = 0, binding = 1, std430)
|
||||
layout(set = 0, binding = 1)
|
||||
ConstantBuffer<Lights> gLightEnv;
|
||||
|
||||
@@ -6,4 +6,13 @@ interface IMaterial
|
||||
{
|
||||
associatedtype BRDF : IBRDF;
|
||||
BRDF prepare(MaterialFragmentParameter input);
|
||||
|
||||
float3 getWorldOffset();
|
||||
|
||||
float3 getBaseColor(MaterialFragmentParameter input);
|
||||
float getMetallic(MaterialFragmentParameter input);
|
||||
float3 getNormal(MaterialFragmentParameter input);
|
||||
float getSpecular(MaterialFragmentParameter input);
|
||||
float getRoughness(MaterialFragmentParameter input);
|
||||
float getSheen(MaterialFragmentParameter input);
|
||||
};
|
||||
|
||||
@@ -5,7 +5,7 @@ struct MaterialVertexParameter
|
||||
#if NUM_MATERIAL_TEXCOORDS
|
||||
float2 texCoords[NUM_MATERIAL_TEXCOORDS];
|
||||
#endif
|
||||
#if USE_INSTANCING
|
||||
#ifdef USE_INSTANCING
|
||||
float4x4 instanceLocalToWorld;
|
||||
float3 instanceLocalPosition;
|
||||
float4 perInstanceParams;
|
||||
@@ -26,7 +26,7 @@ struct MaterialFragmentParameter
|
||||
float4 clipPosition;
|
||||
float4 vertexColor;
|
||||
float3x3 tangentToWorld;
|
||||
#if USE_INSTANCING
|
||||
#ifdef USE_INSTANCING
|
||||
float3 perInstanceParams;
|
||||
#endif
|
||||
#if NUM_MATERIAL_TEXCOORDS
|
||||
@@ -37,4 +37,11 @@ struct MaterialFragmentParameter
|
||||
float3 result = mul(tangentToWorld, tangentSpaceNormal);
|
||||
return result;
|
||||
}
|
||||
float3 transformNormalTexture(float3 rawNormal)
|
||||
{
|
||||
rawNormal = 2.0 * rawNormal - float3(1.0, 1.0, 1.0);
|
||||
return transformNormalToWorld(rawNormal);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
+7
-1
@@ -114,7 +114,7 @@ struct VertexShaderInput
|
||||
return result;
|
||||
}
|
||||
|
||||
float3 getWorldPosition(VertexValueCache cache)
|
||||
float3 getWorldPosition()
|
||||
{
|
||||
float4x4 localToWorld = gSceneData.localToWorld;
|
||||
float3 rotatedPosition = localToWorld[0].xyz * position.xxx + localToWorld[1].xyz * position.yyy + localToWorld[2].xyz * position.zzz;
|
||||
@@ -191,4 +191,10 @@ struct PositionOnlyVertexShaderInput
|
||||
float3 instanceTransform2;
|
||||
float3 instanceTransform3;
|
||||
#endif // USE_INSTANCING
|
||||
float3 getWorldPosition()
|
||||
{
|
||||
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;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user