Cleanup Shader code and struct alignment.
This commit is contained in:
parent
1747339072
commit
bad0a850a1
|
|
@ -29,23 +29,8 @@
|
||||||
constexpr u32 MAX_FRAMES_IN_FLIGHT = 3;
|
constexpr u32 MAX_FRAMES_IN_FLIGHT = 3;
|
||||||
constexpr auto MODEL_FILE = "model/OrientationTest.glb";
|
constexpr auto MODEL_FILE = "model/OrientationTest.glb";
|
||||||
|
|
||||||
struct ImageFile
|
|
||||||
{
|
|
||||||
u8 *m_Data = nullptr;
|
|
||||||
u32 m_Width = 0;
|
|
||||||
u32 m_Height = 0;
|
|
||||||
u32 m_NumChannels = 0;
|
|
||||||
bool m_Constant = false;
|
|
||||||
|
|
||||||
bool Load(cstr fileName);
|
|
||||||
bool Load(vec4 color);
|
|
||||||
[[nodiscard]] usize GetSize() const;
|
|
||||||
~ImageFile();
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Camera
|
struct Camera
|
||||||
{
|
{
|
||||||
mat4 m_Model;
|
|
||||||
mat4 m_View;
|
mat4 m_View;
|
||||||
mat4 m_Perspective;
|
mat4 m_Perspective;
|
||||||
};
|
};
|
||||||
|
|
@ -94,7 +79,6 @@ main(int, char **)
|
||||||
Pipeline pipeline = CreatePipeline(&device, &swapchain, &resourceManager);
|
Pipeline pipeline = CreatePipeline(&device, &swapchain, &resourceManager);
|
||||||
|
|
||||||
Camera camera = {
|
Camera camera = {
|
||||||
.m_Model = {1.0f},
|
|
||||||
.m_View = glm::lookAt(vec3(0.0f, 12.0f, 12.0f), vec3(0.0f), vec3(0.0f, 1.0f, 0.0f)),
|
.m_View = glm::lookAt(vec3(0.0f, 12.0f, 12.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),
|
||||||
|
|
@ -326,65 +310,3 @@ main(int, char **)
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
|
||||||
ImageFile::Load(vec4 color)
|
|
||||||
{
|
|
||||||
constexpr usize size = 512llu * 512llu * 4llu;
|
|
||||||
u8 *pData = new u8[size];
|
|
||||||
|
|
||||||
const vec4 color255 = 255.999f * color;
|
|
||||||
const glm::vec<4, u8> color8 = color255;
|
|
||||||
|
|
||||||
for (usize i = 0; i < size; i += 4)
|
|
||||||
{
|
|
||||||
memcpy(pData + i, &color8, sizeof color8);
|
|
||||||
}
|
|
||||||
|
|
||||||
m_Data = pData;
|
|
||||||
m_Constant = true;
|
|
||||||
m_Height = 512;
|
|
||||||
m_Width = 512;
|
|
||||||
m_NumChannels = 4;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
ImageFile::Load(cstr fileName)
|
|
||||||
{
|
|
||||||
int width, height, nrChannels;
|
|
||||||
m_Data = stbi_load(fileName, &width, &height, &nrChannels, 4);
|
|
||||||
ERROR_IF(!m_Data, "Could not load {}", fileName);
|
|
||||||
|
|
||||||
if (!m_Data)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_Width = width;
|
|
||||||
m_Height = height;
|
|
||||||
m_NumChannels = 4;
|
|
||||||
m_Constant = false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
usize
|
|
||||||
ImageFile::GetSize() const
|
|
||||||
{
|
|
||||||
return Cast<usize>(m_Width) * m_Height * m_NumChannels;
|
|
||||||
}
|
|
||||||
|
|
||||||
ImageFile::~ImageFile()
|
|
||||||
{
|
|
||||||
if (m_Constant)
|
|
||||||
{
|
|
||||||
delete[] Cast<u8 *>(m_Data);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
stbi_image_free(m_Data);
|
|
||||||
}
|
|
||||||
m_Data = nullptr;
|
|
||||||
}
|
|
||||||
|
|
@ -1,9 +1,4 @@
|
||||||
|
|
||||||
typedef float4 PositionData;
|
|
||||||
typedef float2 UVData;
|
|
||||||
typedef float4 NormalData;
|
|
||||||
typedef float4 ColorData;
|
|
||||||
|
|
||||||
struct VertexData
|
struct VertexData
|
||||||
{
|
{
|
||||||
float4 m_Normal;
|
float4 m_Normal;
|
||||||
|
|
@ -42,14 +37,13 @@ struct Block
|
||||||
|
|
||||||
struct Camera
|
struct Camera
|
||||||
{
|
{
|
||||||
float4x4 model;
|
|
||||||
float4x4 view;
|
float4x4 view;
|
||||||
float4x4 proj;
|
float4x4 proj;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define INVALID_HANDLE 0xFFFFFFFF
|
#define INVALID_HANDLE 0xFFFFFFFF
|
||||||
|
|
||||||
[[vk::binding(0, 0)]] StructuredBuffer<PositionData> 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[];
|
||||||
|
|
|
||||||
|
|
@ -17,12 +17,17 @@ float4 ArrayToVector(float arr[4])
|
||||||
return float4(arr[0], arr[1], arr[2], arr[3]);
|
return float4(arr[0], arr[1], arr[2], arr[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float4 ArrayToVector(float4 arr)
|
||||||
|
{
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
float4 GetAlbedo(uint materialBufferId, int materialId, float2 uv)
|
float4 GetAlbedo(uint materialBufferId, int materialId, float2 uv)
|
||||||
{
|
{
|
||||||
uint albedoTexId = materialsBuffer[materialBufferId][materialId].m_AlbedoTex;
|
uint albedoTexId = materialsBuffer[materialBufferId][materialId].m_AlbedoTex;
|
||||||
if (albedoTexId == INVALID_HANDLE)
|
if (albedoTexId == INVALID_HANDLE)
|
||||||
{
|
{
|
||||||
return ArrayToVector(materialsBuffer[materialBufferId][materialId].m_AlbedoFactor);
|
return (float4) materialsBuffer[materialBufferId][materialId].m_AlbedoFactor;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -13,34 +13,38 @@ struct VS_Output
|
||||||
float4 position : SV_Position;
|
float4 position : SV_Position;
|
||||||
};
|
};
|
||||||
|
|
||||||
float2 GetUV(uint bufferId, uint vertexIdx)
|
float2 GetUV(uint vertexIdx)
|
||||||
{
|
{
|
||||||
return (bufferId == INVALID_HANDLE) ? 0.0f.xx : vertexDataBuffer[bufferId][vertexIdx].m_TexCoord0.xy;
|
uint bufferId = pcb.vertexDataHandle;
|
||||||
|
return vertexDataBuffer[bufferId][vertexIdx].m_TexCoord0.xy;
|
||||||
}
|
}
|
||||||
|
|
||||||
float4 GetPosition(uint bufferId, uint vertexIdx)
|
float4 GetPosition(uint vertexIdx)
|
||||||
{
|
{
|
||||||
|
uint bufferId = pcb.vertexBufferHandle;
|
||||||
return float4(vertexBuffer[bufferId][vertexIdx].xyz, 1.0f);
|
return float4(vertexBuffer[bufferId][vertexIdx].xyz, 1.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
float4 GetColor(uint bufferId, uint vertexIdx)
|
float4 GetColor(uint vertexIdx)
|
||||||
{
|
{
|
||||||
return (bufferId == INVALID_HANDLE) ? float4(1.0f, 0.0f, 1.0f, 1.0f) : vertexDataBuffer[bufferId][vertexIdx].m_Color0;
|
uint bufferId = pcb.vertexDataHandle;
|
||||||
|
return vertexDataBuffer[bufferId][vertexIdx].m_Color0;
|
||||||
}
|
}
|
||||||
|
|
||||||
float4x4 GetModel(uint bufferId, uint index)
|
float4x4 GetModel(uint index)
|
||||||
{
|
{
|
||||||
|
uint bufferId = pcb.nodeBufferHandle;
|
||||||
return nodeBuffer[bufferId][index].transform;
|
return nodeBuffer[bufferId][index].transform;
|
||||||
}
|
}
|
||||||
|
|
||||||
VS_Output main(VS_Input stage_input)
|
VS_Output main(VS_Input stage_input)
|
||||||
{
|
{
|
||||||
VS_Output output;
|
VS_Output output;
|
||||||
output.outPosition = GetPosition(pcb.vertexBufferHandle, stage_input.vertexIndex);
|
output.outPosition = GetPosition(stage_input.vertexIndex);
|
||||||
output.outUV = GetUV(pcb.vertexDataHandle, stage_input.vertexIndex);
|
output.outUV = GetUV(stage_input.vertexIndex);
|
||||||
output.outColor = GetColor(pcb.vertexDataHandle, stage_input.vertexIndex);
|
output.outColor = GetColor(stage_input.vertexIndex);
|
||||||
|
|
||||||
float4 globalPosition = mul(camera.model, mul(GetModel(pcb.nodeBufferHandle, pcb.m_NodeIdx), GetPosition(pcb.vertexBufferHandle, stage_input.vertexIndex)));
|
float4 globalPosition = mul(GetModel(pcb.m_NodeIdx), GetPosition(stage_input.vertexIndex));
|
||||||
float4 clipSpace = mul(camera.view, globalPosition);
|
float4 clipSpace = mul(camera.view, globalPosition);
|
||||||
output.position = mul(camera.proj, clipSpace);
|
output.position = mul(camera.proj, clipSpace);
|
||||||
return output;
|
return output;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue