Cleaned and Formatted Shaders.

This commit is contained in:
Anish Bhobe 2024-07-23 00:12:47 +02:00
parent 0d5af2b525
commit 870b18b1fa
6 changed files with 129 additions and 145 deletions

View File

@ -16,7 +16,6 @@ struct DirectionalLight
u32 m_UnusedPadding0_;
u32 m_Color_; // LSB is used for flags. (R G B Flags)
f32 m_Intensity;
};
struct PointLight

View File

@ -349,10 +349,9 @@ ModelLoader::LoadModel(cstr path, cstr name, bool batched)
}
}
#pragma endregion
#pragma region Vertex Data
vertexData.resize(vertexPositions.size());
#pragma region Normal
// Normal Coords
if (prim.attributes.contains(ANormal))
{
@ -395,8 +394,7 @@ ModelLoader::LoadModel(cstr path, cstr name, bool batched)
}
}
}
#pragma endregion
#pragma region UV0
// UV0
if (prim.attributes.contains(ATexCoord0))
{
@ -421,8 +419,7 @@ ModelLoader::LoadModel(cstr path, cstr name, bool batched)
}
}
}
#pragma endregion
#pragma region Color
if (prim.attributes.contains(AColor0))
{
tinygltf::Accessor *colorAccessor = &model.accessors[prim.attributes[AColor0]];

View File

@ -34,6 +34,7 @@ struct Camera
{
mat4 m_View;
mat4 m_Perspective;
vec4 m_Position;
};
int
@ -84,6 +85,7 @@ main(int, char **)
.m_View = glm::lookAt(vec3(0.0f, 2.0f, 2.0f), vec3(0.0f), vec3(0.0f, 1.0f, 0.0f)),
.m_Perspective = glm::perspective(
70_deg, Cast<f32>(swapchain.m_Extent.width) / Cast<f32>(swapchain.m_Extent.height), 0.1f, 100.0f),
.m_Position = vec4{0.0f, 2.0f, 2.0f, 1.0f},
};
lightManager.AddDirectional(vec3(0.0f, -1.0f, 0.0f), {0.0f, 1.0f, 0.0f});

View File

@ -1,56 +1,57 @@
struct VertexData
{
float4 m_Normal;
float2 m_TexCoord0;
float2 m_TexCoord1;
float4 m_Color0;
float4 Normal;
float2 TexCoord0;
float2 TexCoord1;
float4 Color0;
};
struct TransformData
{
float4x4 transform;
float4x4 normalTransform;
float4x4 Transform;
float4x4 NormalTransform;
};
struct MaterialData
{
float m_AlbedoFactor[4];
float m_EmissionFactor[3];
float m_MetalFactor;
float m_RoughFactor;
uint m_AlbedoTex;
uint m_NormalTex;
uint m_MetalRoughTex;
uint m_OcclusionTex;
uint m_EmissionTex;
float AlbedoFactor[4];
float EmissionFactor[3];
float MetalFactor;
float RoughFactor;
uint AlbedoTex;
uint NormalTex;
uint MetalRoughTex;
uint OcclusionTex;
uint EmissionTex;
};
struct Block
{
uint vertexBufferHandle;
uint vertexDataHandle;
uint materialBufferHandle;
uint nodeBufferHandle;
uint lightHandle;
uint pointLightIndexer;
uint directionLightIndexer;
int m_MaterialIdx;
uint m_NodeIdx;
uint VertexBufferHandle;
uint VertexDataHandle;
uint MaterialBufferHandle;
uint NodeBufferHandle;
uint LightHandle;
uint PointLightIndexer;
uint DirectionLightIndexer;
int MaterialIdx;
uint NodeIdx;
};
struct Light
{
float m_Position[3];
float m_Range;
uint m_Color;
float m_Intensity;
float Position[3];
float Range;
uint Color;
float Intensity;
};
struct Camera
struct CameraData
{
float4x4 view;
float4x4 proj;
float4x4 View;
float4x4 Projection;
float4 Position;
};
// Little Endian storage. First short is least significant.
@ -59,16 +60,16 @@ struct Camera
#define INVALID_HANDLE 0xFFFFFFFF
[[vk::binding(0, 0)]] StructuredBuffer<float4> vertexBuffer[];
[[vk::binding(0, 0)]] StructuredBuffer<VertexData> vertexDataBuffer[];
[[vk::binding(0, 0)]] StructuredBuffer<MaterialData> materialsBuffer[];
[[vk::binding(0, 0)]] StructuredBuffer<TransformData> nodeBuffer[];
[[vk::binding(0, 0)]] StructuredBuffer<Light> lightBuffer[];
[[vk::binding(0, 0)]] StructuredBuffer<float4> VertexBuffer[];
[[vk::binding(0, 0)]] StructuredBuffer<VertexData> VertexDataBuffer[];
[[vk::binding(0, 0)]] StructuredBuffer<MaterialData> MaterialsBuffer[];
[[vk::binding(0, 0)]] StructuredBuffer<TransformData> NodeBuffer[];
[[vk::binding(0, 0)]] StructuredBuffer<Light> LightBuffer[];
[[vk::binding(1, 0)]] Texture2D<float4> textures[];
[[vk::binding(1, 0)]] SamplerState immutableSamplers[];
[[vk::binding(1, 0)]] Texture2D<float4> Textures[];
[[vk::binding(1, 0)]] SamplerState ImmutableSamplers[];
[[vk::binding(0, 1)]] ConstantBuffer<Camera> camera;
[[vk::binding(0, 1)]] ConstantBuffer<CameraData> Camera;
[[vk::push_constant]]
Block pcb;
Block PushConstant;

View File

@ -2,114 +2,99 @@
struct FS_Input
{
float4 inPosition : POSITION;
float4 inNormal : NORMAL;
float4 inColor : COLOR0;
float2 inUV : TEXCOORD0;
float4 InPosition : POSITION;
float4 InNormal : NORMAL;
float4 InColor : COLOR0;
float2 InUV0 : TEXCOORD0;
};
struct FS_Output
{
float4 outColor : SV_Target0;
float4 ColorTarget : SV_Target0;
};
float4 GetAlbedo(uint materialBufferId, int materialId, float2 uv)
float4 GetAlbedo(int MaterialIdx, float2 UV)
{
uint albedoTexId = materialsBuffer[materialBufferId][materialId].m_AlbedoTex;
uint albedoTexId = MaterialsBuffer[PushConstant.MaterialBufferHandle][MaterialIdx].AlbedoTex;
if (albedoTexId == INVALID_HANDLE)
{
return (float4) materialsBuffer[materialBufferId][materialId].m_AlbedoFactor;
return (float4) MaterialsBuffer[PushConstant.MaterialBufferHandle][MaterialIdx].AlbedoFactor;
}
else
{
return textures[albedoTexId].Sample(immutableSamplers[albedoTexId], uv);
return Textures[albedoTexId].Sample(ImmutableSamplers[albedoTexId], UV);
}
}
float3 GetDirectionalLightInfluence(float3 normal)
float3 GetDirectionalLightInfluence(float3 Normal)
{
if (pcb.lightHandle == INVALID_HANDLE)
if (PushConstant.LightHandle == INVALID_HANDLE)
return float3(0.0f, 0.0f, 0.0f);
uint count = IndexerCount(pcb.directionLightIndexer);
uint Count = IndexerCount(PushConstant.DirectionLightIndexer);
float3 contrib = 0.0f;
for (uint i = 0; i < count; ++i)
float3 Contrib = 0.0f;
for (uint i = 0; i < Count; ++i)
{
Light light = lightBuffer[pcb.lightHandle][i];
float3 lightDir = - (float3) light.m_Position; // Position is actually direction for directionalLight; LightDir is Direction towards the light (-direction)
float diff = max(dot(normal, lightDir), 0.0f);
Light Light = LightBuffer[PushConstant.LightHandle][i];
float3 LightDir = - (float3) Light.Position; // Position is actually direction for directionalLight; LightDir is Direction towards the light (-direction)
float DiffuseFactor = max(dot(Normal, LightDir), 0.0f);
int ur = (light.m_Color & 0xFF000000) >> 24;
int ug = (light.m_Color & 0x00FF0000) >> 16;
int ub = (light.m_Color & 0x0000FF00) >> 8;
float r = ur;
float g = ug;
float b = ub;
float R = (Light.Color & 0xFF000000) >> 24;
float G = (Light.Color & 0x00FF0000) >> 16;
float B = (Light.Color & 0x0000FF00) >> 8;
float3 color = float3(r, g, b) * 0.00392156862f; // 0.00392156862 = 1/255
float3 Color = float3(R, G, B) * 0.00392156862f; // 0.00392156862 = 1/255
float3 diffuse = diff * color;
float3 Diffuse = DiffuseFactor * Color;
contrib += (light.m_Range < 0 ? float3(0.0f, 0.0f, 0.0f) : diffuse);
Contrib += (Light.Range < 0 ? float3(0.0f, 0.0f, 0.0f) : Diffuse);
}
return contrib;
return Contrib;
}
float3 GetPointLightInfluence(float3 position, float3 normal)
float3 GetPointLightInfluence(float3 Position, float3 Normal)
{
if (pcb.lightHandle == INVALID_HANDLE)
if (PushConstant.LightHandle == INVALID_HANDLE)
return float3(0.0f, 0.0f, 0.0f);
uint offset = IndexerOffset(pcb.pointLightIndexer);
uint count = IndexerCount(pcb.pointLightIndexer);
uint Offset = IndexerOffset(PushConstant.PointLightIndexer);
uint Count = IndexerCount(PushConstant.PointLightIndexer);
float3 contrib = 0.0f;
for (uint i = 0; i < count; ++i)
float3 Contrib = 0.0f;
for (uint i = 0; i < Count; ++i)
{
Light light = lightBuffer[pcb.lightHandle][i + offset];
float3 lightDir = normalize(((float3)light.m_Position) - position);
float diff = max(dot(normal, lightDir), 0.0f);
Light Light = LightBuffer[PushConstant.LightHandle][i + Offset];
float3 LightDir = normalize(((float3)Light.Position) - Position);
float DiffuseFactor = max(dot(Normal, LightDir), 0.0f);
int ur = (light.m_Color & 0xFF000000) >> 24;
int ug = (light.m_Color & 0x00FF0000) >> 16;
int ub = (light.m_Color & 0x0000FF00) >> 8;
float r = ur;
float g = ug;
float b = ub;
float R = (Light.Color & 0xFF000000) >> 24;
float G = (Light.Color & 0x00FF0000) >> 16;
float B = (Light.Color & 0x0000FF00) >> 8;
float3 color = float3(r, g, b) * 0.00392156862f; // 0.00392156862 = 1/255
float3 Color = float3(R, G, B) * 0.00392156862f; // 0.00392156862 = 1/255
float3 diffuse = diff * color;
float3 Diffuse = DiffuseFactor * Color;
contrib += (light.m_Range < 0 ? float3(0.0f, 0.0f, 0.0f) : diffuse);
Contrib += (Light.Range < 0 ? float3(0.0f, 0.0f, 0.0f) : Diffuse);
}
return contrib;
return Contrib;
}
FS_Output
main(FS_Input stage_input)
FS_Output main(FS_Input StageInput)
{
float3 Ambient = float3(0.02f, 0.02f, 0.02f);
//// Hereby assume that we always have a point light at
//float3 lightPos = float3(6.0f, 6.0f, 6.0f);
//// with
//float3 lightColor = float3(0.7f, 0.7f, 0.7f); //float3(0.7f, 0.4f, 0.1f);
//// and
float3 ambient = float3(0.02f, 0.02f, 0.02f);
float3 Normal = normalize(StageInput.InNormal.xyz);
float3 Position = StageInput.InPosition.xyz;
float3 norm = normalize(stage_input.inNormal.xyz);
float3 pos = stage_input.inPosition.xyz;
float4 ObjColor = PushConstant.MaterialIdx < 0 ? StageInput.InColor : GetAlbedo(PushConstant.MaterialIdx, StageInput.InUV0);
float4 objColor = pcb.m_MaterialIdx < 0 ? stage_input.inColor : GetAlbedo(pcb.materialBufferHandle, pcb.m_MaterialIdx, stage_input.inUV);
float3 Diffuse = GetDirectionalLightInfluence(Normal) + GetPointLightInfluence(Position, Normal);
float3 diffuse = GetDirectionalLightInfluence(norm) + GetPointLightInfluence(pos, norm);
FS_Output output;
output.outColor = float4(objColor.rgb * (diffuse + ambient), objColor.a);
return output;
FS_Output Output;
Output.ColorTarget = float4(ObjColor.rgb * (Diffuse + Ambient), ObjColor.a);
return Output;
}

View File

@ -2,66 +2,66 @@
struct VS_Input
{
uint vertexIndex : SV_VertexID;
uint VertexIndex : SV_VertexID;
};
struct VS_Output
{
float4 worldPosition : POSITION;
float4 worldNormal : NORMAL;
float4 outColor : COLOR0;
float2 outUV : TEXCOORD0;
float4 screenPosition : SV_Position;
float4 WorldPosition : POSITION;
float4 WorldNormal : NORMAL;
float4 VertexColor : COLOR0;
float2 UV0 : TEXCOORD0;
float4 VertexPosition : SV_Position;
};
float2 GetUV(uint vertexIdx)
float2 GetUV(uint VertexIdx)
{
uint bufferId = pcb.vertexDataHandle;
return vertexDataBuffer[bufferId][vertexIdx].m_TexCoord0.xy;
uint BufferId = PushConstant.VertexDataHandle;
return VertexDataBuffer[BufferId][VertexIdx].TexCoord0.xy;
}
float4 GetPosition(uint vertexIdx)
float4 GetPosition(uint VertexIdx)
{
uint bufferId = pcb.vertexBufferHandle;
return float4(vertexBuffer[bufferId][vertexIdx].xyz, 1.0f);
uint BufferId = PushConstant.VertexBufferHandle;
return float4(VertexBuffer[BufferId][VertexIdx].xyz, 1.0f);
}
float4 GetNormal(uint vertexIdx)
float4 GetNormal(uint VertexIdx)
{
uint bufferId = pcb.vertexDataHandle;
return vertexDataBuffer[bufferId][vertexIdx].m_Normal;
uint BufferId = PushConstant.VertexDataHandle;
return VertexDataBuffer[BufferId][VertexIdx].Normal;
}
float4 GetColor(uint vertexIdx)
float4 GetColor(uint VertexIdx)
{
uint bufferId = pcb.vertexDataHandle;
return vertexDataBuffer[bufferId][vertexIdx].m_Color0;
uint BufferId = PushConstant.VertexDataHandle;
return VertexDataBuffer[BufferId][VertexIdx].Color0;
}
float4x4 GetModelTransform(uint index)
float4x4 GetNodeTransform(uint NodeIndex)
{
uint bufferId = pcb.nodeBufferHandle;
return nodeBuffer[bufferId][index].transform;
uint BufferId = PushConstant.NodeBufferHandle;
return NodeBuffer[BufferId][NodeIndex].Transform;
}
float4x4 GetNormalTransform(uint index)
float4x4 GetNormalTransform(uint NodeIndex)
{
uint bufferId = pcb.nodeBufferHandle;
return nodeBuffer[bufferId][index].normalTransform;
uint BufferId = PushConstant.NodeBufferHandle;
return NodeBuffer[BufferId][NodeIndex].NormalTransform;
}
VS_Output main(VS_Input stage_input)
VS_Output main(VS_Input StageInput)
{
VS_Output output;
VS_Output Output;
float4 globalPosition = mul(GetModelTransform(pcb.m_NodeIdx), GetPosition(stage_input.vertexIndex));
float4 clipSpace = mul(camera.view, globalPosition);
float4 GlobalPosition = mul(GetNodeTransform(PushConstant.NodeIdx), GetPosition(StageInput.VertexIndex));
float4 ClipSpace = mul(Camera.View, GlobalPosition);
output.screenPosition = mul(camera.proj, clipSpace);
output.worldPosition = globalPosition;
output.outUV = GetUV(stage_input.vertexIndex);
output.outColor = GetColor(stage_input.vertexIndex);
Output.VertexPosition = mul(Camera.Projection, ClipSpace);
Output.WorldPosition = GlobalPosition;
Output.UV0 = GetUV(StageInput.VertexIndex);
Output.VertexColor = GetColor(StageInput.VertexIndex);
output.worldNormal = mul(GetNormalTransform(pcb.m_NodeIdx), GetNormal(stage_input.vertexIndex));
return output;
Output.WorldNormal = mul(GetNormalTransform(PushConstant.NodeIdx), GetNormal(StageInput.VertexIndex));
return Output;
}