Cleaned and Formatted Shaders.
This commit is contained in:
parent
0d5af2b525
commit
870b18b1fa
|
|
@ -16,7 +16,6 @@ struct DirectionalLight
|
||||||
u32 m_UnusedPadding0_;
|
u32 m_UnusedPadding0_;
|
||||||
u32 m_Color_; // LSB is used for flags. (R G B Flags)
|
u32 m_Color_; // LSB is used for flags. (R G B Flags)
|
||||||
f32 m_Intensity;
|
f32 m_Intensity;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct PointLight
|
struct PointLight
|
||||||
|
|
|
||||||
|
|
@ -349,10 +349,9 @@ ModelLoader::LoadModel(cstr path, cstr name, bool batched)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#pragma endregion
|
#pragma endregion
|
||||||
|
#pragma region Vertex Data
|
||||||
vertexData.resize(vertexPositions.size());
|
vertexData.resize(vertexPositions.size());
|
||||||
|
|
||||||
#pragma region Normal
|
|
||||||
// Normal Coords
|
// Normal Coords
|
||||||
if (prim.attributes.contains(ANormal))
|
if (prim.attributes.contains(ANormal))
|
||||||
{
|
{
|
||||||
|
|
@ -395,8 +394,7 @@ ModelLoader::LoadModel(cstr path, cstr name, bool batched)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#pragma endregion
|
|
||||||
#pragma region UV0
|
|
||||||
// UV0
|
// UV0
|
||||||
if (prim.attributes.contains(ATexCoord0))
|
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))
|
if (prim.attributes.contains(AColor0))
|
||||||
{
|
{
|
||||||
tinygltf::Accessor *colorAccessor = &model.accessors[prim.attributes[AColor0]];
|
tinygltf::Accessor *colorAccessor = &model.accessors[prim.attributes[AColor0]];
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@ struct Camera
|
||||||
{
|
{
|
||||||
mat4 m_View;
|
mat4 m_View;
|
||||||
mat4 m_Perspective;
|
mat4 m_Perspective;
|
||||||
|
vec4 m_Position;
|
||||||
};
|
};
|
||||||
|
|
||||||
int
|
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_View = glm::lookAt(vec3(0.0f, 2.0f, 2.0f), vec3(0.0f), vec3(0.0f, 1.0f, 0.0f)),
|
||||||
.m_Perspective = glm::perspective(
|
.m_Perspective = glm::perspective(
|
||||||
70_deg, Cast<f32>(swapchain.m_Extent.width) / Cast<f32>(swapchain.m_Extent.height), 0.1f, 100.0f),
|
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});
|
lightManager.AddDirectional(vec3(0.0f, -1.0f, 0.0f), {0.0f, 1.0f, 0.0f});
|
||||||
|
|
|
||||||
|
|
@ -1,56 +1,57 @@
|
||||||
|
|
||||||
struct VertexData
|
struct VertexData
|
||||||
{
|
{
|
||||||
float4 m_Normal;
|
float4 Normal;
|
||||||
float2 m_TexCoord0;
|
float2 TexCoord0;
|
||||||
float2 m_TexCoord1;
|
float2 TexCoord1;
|
||||||
float4 m_Color0;
|
float4 Color0;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct TransformData
|
struct TransformData
|
||||||
{
|
{
|
||||||
float4x4 transform;
|
float4x4 Transform;
|
||||||
float4x4 normalTransform;
|
float4x4 NormalTransform;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct MaterialData
|
struct MaterialData
|
||||||
{
|
{
|
||||||
float m_AlbedoFactor[4];
|
float AlbedoFactor[4];
|
||||||
float m_EmissionFactor[3];
|
float EmissionFactor[3];
|
||||||
float m_MetalFactor;
|
float MetalFactor;
|
||||||
float m_RoughFactor;
|
float RoughFactor;
|
||||||
uint m_AlbedoTex;
|
uint AlbedoTex;
|
||||||
uint m_NormalTex;
|
uint NormalTex;
|
||||||
uint m_MetalRoughTex;
|
uint MetalRoughTex;
|
||||||
uint m_OcclusionTex;
|
uint OcclusionTex;
|
||||||
uint m_EmissionTex;
|
uint EmissionTex;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Block
|
struct Block
|
||||||
{
|
{
|
||||||
uint vertexBufferHandle;
|
uint VertexBufferHandle;
|
||||||
uint vertexDataHandle;
|
uint VertexDataHandle;
|
||||||
uint materialBufferHandle;
|
uint MaterialBufferHandle;
|
||||||
uint nodeBufferHandle;
|
uint NodeBufferHandle;
|
||||||
uint lightHandle;
|
uint LightHandle;
|
||||||
uint pointLightIndexer;
|
uint PointLightIndexer;
|
||||||
uint directionLightIndexer;
|
uint DirectionLightIndexer;
|
||||||
int m_MaterialIdx;
|
int MaterialIdx;
|
||||||
uint m_NodeIdx;
|
uint NodeIdx;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Light
|
struct Light
|
||||||
{
|
{
|
||||||
float m_Position[3];
|
float Position[3];
|
||||||
float m_Range;
|
float Range;
|
||||||
uint m_Color;
|
uint Color;
|
||||||
float m_Intensity;
|
float Intensity;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Camera
|
struct CameraData
|
||||||
{
|
{
|
||||||
float4x4 view;
|
float4x4 View;
|
||||||
float4x4 proj;
|
float4x4 Projection;
|
||||||
|
float4 Position;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Little Endian storage. First short is least significant.
|
// Little Endian storage. First short is least significant.
|
||||||
|
|
@ -59,16 +60,16 @@ struct Camera
|
||||||
|
|
||||||
#define INVALID_HANDLE 0xFFFFFFFF
|
#define INVALID_HANDLE 0xFFFFFFFF
|
||||||
|
|
||||||
[[vk::binding(0, 0)]] StructuredBuffer<float4> vertexBuffer[];
|
[[vk::binding(0, 0)]] StructuredBuffer<float4> VertexBuffer[];
|
||||||
[[vk::binding(0, 0)]] StructuredBuffer<VertexData> vertexDataBuffer[];
|
[[vk::binding(0, 0)]] StructuredBuffer<VertexData> VertexDataBuffer[];
|
||||||
[[vk::binding(0, 0)]] StructuredBuffer<MaterialData> materialsBuffer[];
|
[[vk::binding(0, 0)]] StructuredBuffer<MaterialData> MaterialsBuffer[];
|
||||||
[[vk::binding(0, 0)]] StructuredBuffer<TransformData> nodeBuffer[];
|
[[vk::binding(0, 0)]] StructuredBuffer<TransformData> NodeBuffer[];
|
||||||
[[vk::binding(0, 0)]] StructuredBuffer<Light> lightBuffer[];
|
[[vk::binding(0, 0)]] StructuredBuffer<Light> LightBuffer[];
|
||||||
|
|
||||||
[[vk::binding(1, 0)]] Texture2D<float4> textures[];
|
[[vk::binding(1, 0)]] Texture2D<float4> Textures[];
|
||||||
[[vk::binding(1, 0)]] SamplerState immutableSamplers[];
|
[[vk::binding(1, 0)]] SamplerState ImmutableSamplers[];
|
||||||
|
|
||||||
[[vk::binding(0, 1)]] ConstantBuffer<Camera> camera;
|
[[vk::binding(0, 1)]] ConstantBuffer<CameraData> Camera;
|
||||||
|
|
||||||
[[vk::push_constant]]
|
[[vk::push_constant]]
|
||||||
Block pcb;
|
Block PushConstant;
|
||||||
|
|
@ -2,114 +2,99 @@
|
||||||
|
|
||||||
struct FS_Input
|
struct FS_Input
|
||||||
{
|
{
|
||||||
float4 inPosition : POSITION;
|
float4 InPosition : POSITION;
|
||||||
float4 inNormal : NORMAL;
|
float4 InNormal : NORMAL;
|
||||||
float4 inColor : COLOR0;
|
float4 InColor : COLOR0;
|
||||||
float2 inUV : TEXCOORD0;
|
float2 InUV0 : TEXCOORD0;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct FS_Output
|
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)
|
if (albedoTexId == INVALID_HANDLE)
|
||||||
{
|
{
|
||||||
return (float4) materialsBuffer[materialBufferId][materialId].m_AlbedoFactor;
|
return (float4) MaterialsBuffer[PushConstant.MaterialBufferHandle][MaterialIdx].AlbedoFactor;
|
||||||
}
|
}
|
||||||
else
|
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);
|
return float3(0.0f, 0.0f, 0.0f);
|
||||||
|
|
||||||
uint count = IndexerCount(pcb.directionLightIndexer);
|
uint Count = IndexerCount(PushConstant.DirectionLightIndexer);
|
||||||
|
|
||||||
float3 contrib = 0.0f;
|
float3 Contrib = 0.0f;
|
||||||
for (uint i = 0; i < count; ++i)
|
for (uint i = 0; i < Count; ++i)
|
||||||
{
|
{
|
||||||
Light light = lightBuffer[pcb.lightHandle][i];
|
Light Light = LightBuffer[PushConstant.LightHandle][i];
|
||||||
float3 lightDir = - (float3) light.m_Position; // Position is actually direction for directionalLight; LightDir is Direction towards the light (-direction)
|
float3 LightDir = - (float3) Light.Position; // Position is actually direction for directionalLight; LightDir is Direction towards the light (-direction)
|
||||||
float diff = max(dot(normal, lightDir), 0.0f);
|
float DiffuseFactor = max(dot(Normal, LightDir), 0.0f);
|
||||||
|
|
||||||
int ur = (light.m_Color & 0xFF000000) >> 24;
|
float R = (Light.Color & 0xFF000000) >> 24;
|
||||||
int ug = (light.m_Color & 0x00FF0000) >> 16;
|
float G = (Light.Color & 0x00FF0000) >> 16;
|
||||||
int ub = (light.m_Color & 0x0000FF00) >> 8;
|
float B = (Light.Color & 0x0000FF00) >> 8;
|
||||||
|
|
||||||
float r = ur;
|
|
||||||
float g = ug;
|
|
||||||
float b = ub;
|
|
||||||
|
|
||||||
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);
|
return float3(0.0f, 0.0f, 0.0f);
|
||||||
|
|
||||||
uint offset = IndexerOffset(pcb.pointLightIndexer);
|
uint Offset = IndexerOffset(PushConstant.PointLightIndexer);
|
||||||
uint count = IndexerCount(pcb.pointLightIndexer);
|
uint Count = IndexerCount(PushConstant.PointLightIndexer);
|
||||||
|
|
||||||
float3 contrib = 0.0f;
|
float3 Contrib = 0.0f;
|
||||||
for (uint i = 0; i < count; ++i)
|
for (uint i = 0; i < Count; ++i)
|
||||||
{
|
{
|
||||||
Light light = lightBuffer[pcb.lightHandle][i + offset];
|
Light Light = LightBuffer[PushConstant.LightHandle][i + Offset];
|
||||||
float3 lightDir = normalize(((float3)light.m_Position) - position);
|
float3 LightDir = normalize(((float3)Light.Position) - Position);
|
||||||
float diff = max(dot(normal, lightDir), 0.0f);
|
float DiffuseFactor = max(dot(Normal, LightDir), 0.0f);
|
||||||
|
|
||||||
int ur = (light.m_Color & 0xFF000000) >> 24;
|
float R = (Light.Color & 0xFF000000) >> 24;
|
||||||
int ug = (light.m_Color & 0x00FF0000) >> 16;
|
float G = (Light.Color & 0x00FF0000) >> 16;
|
||||||
int ub = (light.m_Color & 0x0000FF00) >> 8;
|
float B = (Light.Color & 0x0000FF00) >> 8;
|
||||||
|
|
||||||
float r = ur;
|
|
||||||
float g = ug;
|
|
||||||
float b = ub;
|
|
||||||
|
|
||||||
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
|
FS_Output main(FS_Input StageInput)
|
||||||
main(FS_Input stage_input)
|
|
||||||
{
|
{
|
||||||
|
float3 Ambient = float3(0.02f, 0.02f, 0.02f);
|
||||||
|
|
||||||
//// Hereby assume that we always have a point light at
|
float3 Normal = normalize(StageInput.InNormal.xyz);
|
||||||
//float3 lightPos = float3(6.0f, 6.0f, 6.0f);
|
float3 Position = StageInput.InPosition.xyz;
|
||||||
//// 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 norm = normalize(stage_input.inNormal.xyz);
|
float4 ObjColor = PushConstant.MaterialIdx < 0 ? StageInput.InColor : GetAlbedo(PushConstant.MaterialIdx, StageInput.InUV0);
|
||||||
float3 pos = stage_input.inPosition.xyz;
|
|
||||||
|
|
||||||
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.ColorTarget = float4(ObjColor.rgb * (Diffuse + Ambient), ObjColor.a);
|
||||||
FS_Output output;
|
return Output;
|
||||||
output.outColor = float4(objColor.rgb * (diffuse + ambient), objColor.a);
|
|
||||||
return output;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,66 +2,66 @@
|
||||||
|
|
||||||
struct VS_Input
|
struct VS_Input
|
||||||
{
|
{
|
||||||
uint vertexIndex : SV_VertexID;
|
uint VertexIndex : SV_VertexID;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct VS_Output
|
struct VS_Output
|
||||||
{
|
{
|
||||||
float4 worldPosition : POSITION;
|
float4 WorldPosition : POSITION;
|
||||||
float4 worldNormal : NORMAL;
|
float4 WorldNormal : NORMAL;
|
||||||
float4 outColor : COLOR0;
|
float4 VertexColor : COLOR0;
|
||||||
float2 outUV : TEXCOORD0;
|
float2 UV0 : TEXCOORD0;
|
||||||
float4 screenPosition : SV_Position;
|
float4 VertexPosition : SV_Position;
|
||||||
};
|
};
|
||||||
|
|
||||||
float2 GetUV(uint vertexIdx)
|
float2 GetUV(uint VertexIdx)
|
||||||
{
|
{
|
||||||
uint bufferId = pcb.vertexDataHandle;
|
uint BufferId = PushConstant.VertexDataHandle;
|
||||||
return vertexDataBuffer[bufferId][vertexIdx].m_TexCoord0.xy;
|
return VertexDataBuffer[BufferId][VertexIdx].TexCoord0.xy;
|
||||||
}
|
}
|
||||||
|
|
||||||
float4 GetPosition(uint vertexIdx)
|
float4 GetPosition(uint VertexIdx)
|
||||||
{
|
{
|
||||||
uint bufferId = pcb.vertexBufferHandle;
|
uint BufferId = PushConstant.VertexBufferHandle;
|
||||||
return float4(vertexBuffer[bufferId][vertexIdx].xyz, 1.0f);
|
return float4(VertexBuffer[BufferId][VertexIdx].xyz, 1.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
float4 GetNormal(uint vertexIdx)
|
float4 GetNormal(uint VertexIdx)
|
||||||
{
|
{
|
||||||
uint bufferId = pcb.vertexDataHandle;
|
uint BufferId = PushConstant.VertexDataHandle;
|
||||||
return vertexDataBuffer[bufferId][vertexIdx].m_Normal;
|
return VertexDataBuffer[BufferId][VertexIdx].Normal;
|
||||||
}
|
}
|
||||||
|
|
||||||
float4 GetColor(uint vertexIdx)
|
float4 GetColor(uint VertexIdx)
|
||||||
{
|
{
|
||||||
uint bufferId = pcb.vertexDataHandle;
|
uint BufferId = PushConstant.VertexDataHandle;
|
||||||
return vertexDataBuffer[bufferId][vertexIdx].m_Color0;
|
return VertexDataBuffer[BufferId][VertexIdx].Color0;
|
||||||
}
|
}
|
||||||
|
|
||||||
float4x4 GetModelTransform(uint index)
|
float4x4 GetNodeTransform(uint NodeIndex)
|
||||||
{
|
{
|
||||||
uint bufferId = pcb.nodeBufferHandle;
|
uint BufferId = PushConstant.NodeBufferHandle;
|
||||||
return nodeBuffer[bufferId][index].transform;
|
return NodeBuffer[BufferId][NodeIndex].Transform;
|
||||||
}
|
}
|
||||||
|
|
||||||
float4x4 GetNormalTransform(uint index)
|
float4x4 GetNormalTransform(uint NodeIndex)
|
||||||
{
|
{
|
||||||
uint bufferId = pcb.nodeBufferHandle;
|
uint BufferId = PushConstant.NodeBufferHandle;
|
||||||
return nodeBuffer[bufferId][index].normalTransform;
|
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 GlobalPosition = mul(GetNodeTransform(PushConstant.NodeIdx), GetPosition(StageInput.VertexIndex));
|
||||||
float4 clipSpace = mul(camera.view, globalPosition);
|
float4 ClipSpace = mul(Camera.View, GlobalPosition);
|
||||||
|
|
||||||
output.screenPosition = mul(camera.proj, clipSpace);
|
Output.VertexPosition = mul(Camera.Projection, ClipSpace);
|
||||||
output.worldPosition = globalPosition;
|
Output.WorldPosition = GlobalPosition;
|
||||||
output.outUV = GetUV(stage_input.vertexIndex);
|
Output.UV0 = GetUV(StageInput.VertexIndex);
|
||||||
output.outColor = GetColor(stage_input.vertexIndex);
|
Output.VertexColor = GetColor(StageInput.VertexIndex);
|
||||||
|
|
||||||
output.worldNormal = mul(GetNormalTransform(pcb.m_NodeIdx), GetNormal(stage_input.vertexIndex));
|
Output.WorldNormal = mul(GetNormalTransform(PushConstant.NodeIdx), GetNormal(StageInput.VertexIndex));
|
||||||
return output;
|
return Output;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue