diff --git a/samples/03_model_render/CMakeLists.txt b/samples/03_model_render/CMakeLists.txt index d8b64f2..82653d9 100644 --- a/samples/03_model_render/CMakeLists.txt +++ b/samples/03_model_render/CMakeLists.txt @@ -15,6 +15,8 @@ add_executable(model_render model_render.cpp ) add_shader(model_render shader/model.vert.glsl) add_shader(model_render shader/model.frag.glsl) +add_shader(model_render shader/model.vs.hlsl) +add_shader(model_render shader/model.ps.hlsl) target_link_libraries(model_render PRIVATE aster_core) target_link_libraries(model_render PRIVATE util_helper) diff --git a/samples/03_model_render/pipeline_utils.h b/samples/03_model_render/pipeline_utils.h index a30754e..c8a78af 100644 --- a/samples/03_model_render/pipeline_utils.h +++ b/samples/03_model_render/pipeline_utils.h @@ -14,8 +14,8 @@ struct RenderResourceManager; struct Swapchain; struct Device; -constexpr auto VERTEX_SHADER_FILE = "shader/model.vert.glsl.spv"; -constexpr auto FRAGMENT_SHADER_FILE = "shader/model.frag.glsl.spv"; +constexpr auto VERTEX_SHADER_FILE = "shader/model.vs.hlsl.spv"; +constexpr auto FRAGMENT_SHADER_FILE = "shader/model.ps.hlsl.spv"; struct Vertex { diff --git a/samples/03_model_render/shader/bindless_structs.hlsli b/samples/03_model_render/shader/bindless_structs.hlsli new file mode 100644 index 0000000..94792c7 --- /dev/null +++ b/samples/03_model_render/shader/bindless_structs.hlsli @@ -0,0 +1,52 @@ + +typedef float4 PositionData; +typedef float2 UVData; +typedef float4 NormalData; + +struct MaterialData +{ + float4 m_AlbedoFactor; + float3 m_EmissionFactor; + float m_MetalFactor; + float m_RoughFactor; + uint m_AlbedoTex; + uint m_NormalTex; + uint m_MetalRoughTex; + uint m_OcclusionTex; + uint m_EmissionTex; +}; + +struct Block +{ + uint vertexBufferHandle; + uint uvBufferHandle; + uint materialBufferHandle; + uint m_VertexOffset; + int m_NormalOffset; + int m_TexCoord0Offset; + uint m_FirstIndex; + uint m_IndexCount; + int m_MaterialIdx; +}; + +struct Camera +{ + row_major float4x4 model; + row_major float4x4 view; + row_major float4x4 proj; +}; + +#define INVALID_HANDLE 0xFFFFFFFF + +[[vk::binding(0, 0)]] StructuredBuffer vertexBuffer[]; +[[vk::binding(0, 0)]] StructuredBuffer uvBuffer[]; +[[vk::binding(0, 0)]] StructuredBuffer normalBuffer[]; +[[vk::binding(0, 0)]] StructuredBuffer materialsBuffer[]; + +[[vk::binding(1, 0)]] Texture2D textures[]; +[[vk::binding(1, 0)]] SamplerState immutableSamplers[]; + +[[vk::binding(0, 1)]] ConstantBuffer camera; + +[[vk::push_constant]] +Block pcb; \ No newline at end of file diff --git a/samples/03_model_render/shader/model.ps.hlsl b/samples/03_model_render/shader/model.ps.hlsl new file mode 100644 index 0000000..0955601 --- /dev/null +++ b/samples/03_model_render/shader/model.ps.hlsl @@ -0,0 +1,44 @@ +#include "bindless_structs.hlsli" + +struct FS_Input +{ + float2 inUV : TEXCOORD0; +}; + +struct FS_Output +{ + float4 outColor : SV_Target0; +}; + +struct Float4 +{ + float4 value; +}; + +float4 GetAlbedo(uint materialBufferId, int materialId, float2 uv0) +{ + if (materialId < 0) + { + return float4(1.0f, 0.0f, 1.0f, 1.0f); // Magenta + } + else + { + float4 albedoFactor = materialsBuffer[materialBufferId][materialId].m_AlbedoFactor; + uint albedoTexId = materialsBuffer[materialBufferId][materialId].m_AlbedoTex; + if (albedoTexId == INVALID_HANDLE) + { + return albedoFactor; + } + else + { + return textures[albedoTexId].Sample(immutableSamplers[albedoTexId], uv0); + } + } +} + +FS_Output main(FS_Input stage_input) +{ + FS_Output stage_output; + stage_output.outColor = GetAlbedo(pcb.materialBufferHandle, pcb.m_MaterialIdx, stage_input.inUV); + return stage_output; +} diff --git a/samples/03_model_render/shader/model.vs.hlsl b/samples/03_model_render/shader/model.vs.hlsl new file mode 100644 index 0000000..85bef85 --- /dev/null +++ b/samples/03_model_render/shader/model.vs.hlsl @@ -0,0 +1,37 @@ +#include "bindless_structs.hlsli" + +struct VS_Input +{ + uint vertexIndex : SV_VertexID; +}; + +struct VS_Output +{ + UVData outUV : TEXCOORD0; + float4 position : SV_Position; +}; + +float2 GetUV(uint bufferId, uint vertexIdx) +{ + if (bufferId == INVALID_HANDLE) + { + return 0.0f.xx; + } + else + { + return uvBuffer[bufferId][vertexIdx]; + } +} + +float3 GetPosition(uint bufferId, uint vertexIdx) +{ + return vertexBuffer[bufferId][vertexIdx].xyz; +} + +VS_Output main(VS_Input stage_input) +{ + VS_Output stage_output; + stage_output.outUV = GetUV(pcb.uvBufferHandle, stage_input.vertexIndex); + stage_output.position = mul(float4(GetPosition(pcb.vertexBufferHandle, stage_input.vertexIndex), 1.0f), mul(camera.model, mul(camera.view, camera.proj))); + return stage_output; +}